]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf.c
PR25840, Null pointer dereference in objdump
[thirdparty/binutils-gdb.git] / bfd / elf.c
CommitLineData
252b5132 1/* ELF executable support for BFD.
340b6d91 2
b3adc24a 3 Copyright (C) 1993-2020 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 *);
ef10c3ac 54static bfd_boolean swap_out_syms (bfd *, struct elf_strtab_hash **, int) ;
718175fa 55static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
276da9b3 56 file_ptr offset, size_t align);
50b2bdb7 57
252b5132
RH
58/* Swap version information in and out. The version information is
59 currently size independent. If that ever changes, this code will
60 need to move into elfcode.h. */
61
62/* Swap in a Verdef structure. */
63
64void
217aa764
AM
65_bfd_elf_swap_verdef_in (bfd *abfd,
66 const Elf_External_Verdef *src,
67 Elf_Internal_Verdef *dst)
252b5132 68{
dc810e39
AM
69 dst->vd_version = H_GET_16 (abfd, src->vd_version);
70 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
71 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
72 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
73 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
74 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
75 dst->vd_next = H_GET_32 (abfd, src->vd_next);
252b5132
RH
76}
77
78/* Swap out a Verdef structure. */
79
80void
217aa764
AM
81_bfd_elf_swap_verdef_out (bfd *abfd,
82 const Elf_Internal_Verdef *src,
83 Elf_External_Verdef *dst)
252b5132 84{
dc810e39
AM
85 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
86 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
87 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
88 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
89 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
90 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
91 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
252b5132
RH
92}
93
94/* Swap in a Verdaux structure. */
95
96void
217aa764
AM
97_bfd_elf_swap_verdaux_in (bfd *abfd,
98 const Elf_External_Verdaux *src,
99 Elf_Internal_Verdaux *dst)
252b5132 100{
dc810e39
AM
101 dst->vda_name = H_GET_32 (abfd, src->vda_name);
102 dst->vda_next = H_GET_32 (abfd, src->vda_next);
252b5132
RH
103}
104
105/* Swap out a Verdaux structure. */
106
107void
217aa764
AM
108_bfd_elf_swap_verdaux_out (bfd *abfd,
109 const Elf_Internal_Verdaux *src,
110 Elf_External_Verdaux *dst)
252b5132 111{
dc810e39
AM
112 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
113 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
252b5132
RH
114}
115
116/* Swap in a Verneed structure. */
117
118void
217aa764
AM
119_bfd_elf_swap_verneed_in (bfd *abfd,
120 const Elf_External_Verneed *src,
121 Elf_Internal_Verneed *dst)
252b5132 122{
dc810e39
AM
123 dst->vn_version = H_GET_16 (abfd, src->vn_version);
124 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
125 dst->vn_file = H_GET_32 (abfd, src->vn_file);
126 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
127 dst->vn_next = H_GET_32 (abfd, src->vn_next);
252b5132
RH
128}
129
130/* Swap out a Verneed structure. */
131
132void
217aa764
AM
133_bfd_elf_swap_verneed_out (bfd *abfd,
134 const Elf_Internal_Verneed *src,
135 Elf_External_Verneed *dst)
252b5132 136{
dc810e39
AM
137 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
138 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
139 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
140 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
141 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
252b5132
RH
142}
143
144/* Swap in a Vernaux structure. */
145
146void
217aa764
AM
147_bfd_elf_swap_vernaux_in (bfd *abfd,
148 const Elf_External_Vernaux *src,
149 Elf_Internal_Vernaux *dst)
252b5132 150{
dc810e39
AM
151 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
152 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
153 dst->vna_other = H_GET_16 (abfd, src->vna_other);
154 dst->vna_name = H_GET_32 (abfd, src->vna_name);
155 dst->vna_next = H_GET_32 (abfd, src->vna_next);
252b5132
RH
156}
157
158/* Swap out a Vernaux structure. */
159
160void
217aa764
AM
161_bfd_elf_swap_vernaux_out (bfd *abfd,
162 const Elf_Internal_Vernaux *src,
163 Elf_External_Vernaux *dst)
252b5132 164{
dc810e39
AM
165 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
166 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
167 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
168 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
169 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
252b5132
RH
170}
171
172/* Swap in a Versym structure. */
173
174void
217aa764
AM
175_bfd_elf_swap_versym_in (bfd *abfd,
176 const Elf_External_Versym *src,
177 Elf_Internal_Versym *dst)
252b5132 178{
dc810e39 179 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
252b5132
RH
180}
181
182/* Swap out a Versym structure. */
183
184void
217aa764
AM
185_bfd_elf_swap_versym_out (bfd *abfd,
186 const Elf_Internal_Versym *src,
187 Elf_External_Versym *dst)
252b5132 188{
dc810e39 189 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
252b5132
RH
190}
191
192/* Standard ELF hash function. Do not change this function; you will
193 cause invalid hash tables to be generated. */
3a99b017 194
252b5132 195unsigned long
217aa764 196bfd_elf_hash (const char *namearg)
252b5132 197{
3a99b017 198 const unsigned char *name = (const unsigned char *) namearg;
252b5132
RH
199 unsigned long h = 0;
200 unsigned long g;
201 int ch;
202
203 while ((ch = *name++) != '\0')
204 {
205 h = (h << 4) + ch;
206 if ((g = (h & 0xf0000000)) != 0)
207 {
208 h ^= g >> 24;
209 /* The ELF ABI says `h &= ~g', but this is equivalent in
210 this case and on some machines one insn instead of two. */
211 h ^= g;
212 }
213 }
32dfa85d 214 return h & 0xffffffff;
252b5132
RH
215}
216
fdc90cb4
JJ
217/* DT_GNU_HASH hash function. Do not change this function; you will
218 cause invalid hash tables to be generated. */
219
220unsigned long
221bfd_elf_gnu_hash (const char *namearg)
222{
223 const unsigned char *name = (const unsigned char *) namearg;
224 unsigned long h = 5381;
225 unsigned char ch;
226
227 while ((ch = *name++) != '\0')
228 h = (h << 5) + h + ch;
229 return h & 0xffffffff;
230}
231
0c8d6e5c
AM
232/* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
233 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
b34976b6 234bfd_boolean
0c8d6e5c 235bfd_elf_allocate_object (bfd *abfd,
0ffa91dd 236 size_t object_size,
4dfe6ac6 237 enum elf_target_id object_id)
252b5132 238{
0ffa91dd
NC
239 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
240 abfd->tdata.any = bfd_zalloc (abfd, object_size);
241 if (abfd->tdata.any == NULL)
242 return FALSE;
252b5132 243
0ffa91dd 244 elf_object_id (abfd) = object_id;
c0355132
AM
245 if (abfd->direction != read_direction)
246 {
247 struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
248 if (o == NULL)
249 return FALSE;
250 elf_tdata (abfd)->o = o;
251 elf_program_header_size (abfd) = (bfd_size_type) -1;
252 }
b34976b6 253 return TRUE;
252b5132
RH
254}
255
0ffa91dd
NC
256
257bfd_boolean
ae95ffa6 258bfd_elf_make_object (bfd *abfd)
0ffa91dd 259{
ae95ffa6 260 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
0ffa91dd 261 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
ae95ffa6 262 bed->target_id);
0ffa91dd
NC
263}
264
b34976b6 265bfd_boolean
217aa764 266bfd_elf_mkcorefile (bfd *abfd)
252b5132 267{
c044fabd 268 /* I think this can be done just like an object file. */
228e534f
AM
269 if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
270 return FALSE;
271 elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
272 return elf_tdata (abfd)->core != NULL;
252b5132
RH
273}
274
6d5944fc 275char *
217aa764 276bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
252b5132
RH
277{
278 Elf_Internal_Shdr **i_shdrp;
f075ee0c 279 bfd_byte *shstrtab = NULL;
dc810e39
AM
280 file_ptr offset;
281 bfd_size_type shstrtabsize;
252b5132
RH
282
283 i_shdrp = elf_elfsections (abfd);
74f2e02b
AM
284 if (i_shdrp == 0
285 || shindex >= elf_numsections (abfd)
286 || i_shdrp[shindex] == 0)
f075ee0c 287 return NULL;
252b5132 288
f075ee0c 289 shstrtab = i_shdrp[shindex]->contents;
252b5132
RH
290 if (shstrtab == NULL)
291 {
c044fabd 292 /* No cached one, attempt to read, and cache what we read. */
252b5132
RH
293 offset = i_shdrp[shindex]->sh_offset;
294 shstrtabsize = i_shdrp[shindex]->sh_size;
c6c60d09
JJ
295
296 /* Allocate and clear an extra byte at the end, to prevent crashes
297 in case the string table is not terminated. */
3471d59d 298 if (shstrtabsize + 1 <= 1
06614111 299 || bfd_seek (abfd, offset, SEEK_SET) != 0
2bb3687b
AM
300 || (shstrtab = _bfd_alloc_and_read (abfd, shstrtabsize + 1,
301 shstrtabsize)) == NULL)
302 {
3471d59d
CC
303 /* Once we've failed to read it, make sure we don't keep
304 trying. Otherwise, we'll keep allocating space for
305 the string table over and over. */
306 i_shdrp[shindex]->sh_size = 0;
c6c60d09
JJ
307 }
308 else
309 shstrtab[shstrtabsize] = '\0';
217aa764 310 i_shdrp[shindex]->contents = shstrtab;
252b5132 311 }
f075ee0c 312 return (char *) shstrtab;
252b5132
RH
313}
314
315char *
217aa764
AM
316bfd_elf_string_from_elf_section (bfd *abfd,
317 unsigned int shindex,
318 unsigned int strindex)
252b5132
RH
319{
320 Elf_Internal_Shdr *hdr;
321
322 if (strindex == 0)
323 return "";
324
74f2e02b
AM
325 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
326 return NULL;
327
252b5132
RH
328 hdr = elf_elfsections (abfd)[shindex];
329
06614111
NC
330 if (hdr->contents == NULL)
331 {
332 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
333 {
334 /* PR 17512: file: f057ec89. */
695344c0 335 /* xgettext:c-format */
871b3ab2 336 _bfd_error_handler (_("%pB: attempt to load strings from"
63a5468a 337 " a non-string section (number %d)"),
06614111
NC
338 abfd, shindex);
339 return NULL;
340 }
b1fa9dd6 341
06614111
NC
342 if (bfd_elf_get_str_section (abfd, shindex) == NULL)
343 return NULL;
344 }
eed5def8
NC
345 else
346 {
347 /* PR 24273: The string section's contents may have already
348 been loaded elsewhere, eg because a corrupt file has the
349 string section index in the ELF header pointing at a group
350 section. So be paranoid, and test that the last byte of
351 the section is zero. */
352 if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
353 return NULL;
354 }
252b5132
RH
355
356 if (strindex >= hdr->sh_size)
357 {
1b3a8575 358 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
4eca0228 359 _bfd_error_handler
695344c0 360 /* xgettext:c-format */
2dcf00ce
AM
361 (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
362 abfd, strindex, (uint64_t) hdr->sh_size,
1b3a8575 363 (shindex == shstrndx && strindex == hdr->sh_name
252b5132 364 ? ".shstrtab"
1b3a8575 365 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
45b222d6 366 return NULL;
252b5132
RH
367 }
368
369 return ((char *) hdr->contents) + strindex;
370}
371
6cdc0ccc
AM
372/* Read and convert symbols to internal format.
373 SYMCOUNT specifies the number of symbols to read, starting from
374 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
375 are non-NULL, they are used to store the internal symbols, external
b7c368d0
NC
376 symbols, and symbol section index extensions, respectively.
377 Returns a pointer to the internal symbol buffer (malloced if necessary)
378 or NULL if there were no symbols or some kind of problem. */
6cdc0ccc
AM
379
380Elf_Internal_Sym *
217aa764
AM
381bfd_elf_get_elf_syms (bfd *ibfd,
382 Elf_Internal_Shdr *symtab_hdr,
383 size_t symcount,
384 size_t symoffset,
385 Elf_Internal_Sym *intsym_buf,
386 void *extsym_buf,
387 Elf_External_Sym_Shndx *extshndx_buf)
6cdc0ccc
AM
388{
389 Elf_Internal_Shdr *shndx_hdr;
217aa764 390 void *alloc_ext;
df622259 391 const bfd_byte *esym;
6cdc0ccc
AM
392 Elf_External_Sym_Shndx *alloc_extshndx;
393 Elf_External_Sym_Shndx *shndx;
4dd07732 394 Elf_Internal_Sym *alloc_intsym;
6cdc0ccc
AM
395 Elf_Internal_Sym *isym;
396 Elf_Internal_Sym *isymend;
9c5bfbb7 397 const struct elf_backend_data *bed;
6cdc0ccc 398 size_t extsym_size;
1f4361a7 399 size_t amt;
6cdc0ccc
AM
400 file_ptr pos;
401
e44a2c9c
AM
402 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
403 abort ();
404
6cdc0ccc
AM
405 if (symcount == 0)
406 return intsym_buf;
407
408 /* Normal syms might have section extension entries. */
409 shndx_hdr = NULL;
6a40cf0c
NC
410 if (elf_symtab_shndx_list (ibfd) != NULL)
411 {
412 elf_section_list * entry;
413 Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
414
415 /* Find an index section that is linked to this symtab section. */
416 for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
315350be
NC
417 {
418 /* PR 20063. */
419 if (entry->hdr.sh_link >= elf_numsections (ibfd))
420 continue;
421
422 if (sections[entry->hdr.sh_link] == symtab_hdr)
423 {
424 shndx_hdr = & entry->hdr;
425 break;
426 };
427 }
6a40cf0c
NC
428
429 if (shndx_hdr == NULL)
430 {
431 if (symtab_hdr == & elf_symtab_hdr (ibfd))
432 /* Not really accurate, but this was how the old code used to work. */
433 shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr;
434 /* Otherwise we do nothing. The assumption is that
435 the index table will not be needed. */
436 }
437 }
6cdc0ccc
AM
438
439 /* Read the symbols. */
440 alloc_ext = NULL;
441 alloc_extshndx = NULL;
4dd07732 442 alloc_intsym = NULL;
6cdc0ccc
AM
443 bed = get_elf_backend_data (ibfd);
444 extsym_size = bed->s->sizeof_sym;
1f4361a7
AM
445 if (_bfd_mul_overflow (symcount, extsym_size, &amt))
446 {
447 bfd_set_error (bfd_error_file_too_big);
448 intsym_buf = NULL;
449 goto out;
450 }
6cdc0ccc
AM
451 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
452 if (extsym_buf == NULL)
453 {
1f4361a7 454 alloc_ext = bfd_malloc (amt);
6cdc0ccc
AM
455 extsym_buf = alloc_ext;
456 }
457 if (extsym_buf == NULL
458 || bfd_seek (ibfd, pos, SEEK_SET) != 0
459 || bfd_bread (extsym_buf, amt, ibfd) != amt)
460 {
461 intsym_buf = NULL;
462 goto out;
463 }
464
465 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
466 extshndx_buf = NULL;
467 else
468 {
1f4361a7
AM
469 if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
470 {
471 bfd_set_error (bfd_error_file_too_big);
472 intsym_buf = NULL;
473 goto out;
474 }
6cdc0ccc
AM
475 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
476 if (extshndx_buf == NULL)
477 {
1f4361a7 478 alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
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 {
1f4361a7
AM
492 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
493 {
494 bfd_set_error (bfd_error_file_too_big);
495 goto out;
496 }
497 alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
4dd07732 498 intsym_buf = alloc_intsym;
6cdc0ccc
AM
499 if (intsym_buf == NULL)
500 goto out;
501 }
502
503 /* Convert the symbols to internal form. */
504 isymend = intsym_buf + symcount;
a50b1753 505 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
07d6d2b8 506 shndx = extshndx_buf;
6cdc0ccc
AM
507 isym < isymend;
508 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
8384fb8f
AM
509 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
510 {
511 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
695344c0 512 /* xgettext:c-format */
871b3ab2 513 _bfd_error_handler (_("%pB symbol number %lu references"
63a5468a 514 " nonexistent SHT_SYMTAB_SHNDX section"),
4eca0228 515 ibfd, (unsigned long) symoffset);
4dd07732
AM
516 if (alloc_intsym != NULL)
517 free (alloc_intsym);
8384fb8f
AM
518 intsym_buf = NULL;
519 goto out;
520 }
6cdc0ccc
AM
521
522 out:
523 if (alloc_ext != NULL)
524 free (alloc_ext);
525 if (alloc_extshndx != NULL)
526 free (alloc_extshndx);
527
528 return intsym_buf;
529}
530
5cab59f6
AM
531/* Look up a symbol name. */
532const char *
be8dd2ca
AM
533bfd_elf_sym_name (bfd *abfd,
534 Elf_Internal_Shdr *symtab_hdr,
26c61ae5
L
535 Elf_Internal_Sym *isym,
536 asection *sym_sec)
5cab59f6 537{
26c61ae5 538 const char *name;
5cab59f6 539 unsigned int iname = isym->st_name;
be8dd2ca 540 unsigned int shindex = symtab_hdr->sh_link;
26c61ae5 541
138f35cc
JJ
542 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
543 /* Check for a bogus st_shndx to avoid crashing. */
4fbb74a6 544 && isym->st_shndx < elf_numsections (abfd))
5cab59f6
AM
545 {
546 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
547 shindex = elf_elfheader (abfd)->e_shstrndx;
548 }
549
26c61ae5
L
550 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
551 if (name == NULL)
552 name = "(null)";
553 else if (sym_sec && *name == '\0')
fd361982 554 name = bfd_section_name (sym_sec);
26c61ae5
L
555
556 return name;
5cab59f6
AM
557}
558
dbb410c3
AM
559/* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
560 sections. The first element is the flags, the rest are section
561 pointers. */
562
563typedef union elf_internal_group {
564 Elf_Internal_Shdr *shdr;
565 unsigned int flags;
566} Elf_Internal_Group;
567
b885599b
AM
568/* Return the name of the group signature symbol. Why isn't the
569 signature just a string? */
570
571static const char *
217aa764 572group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
b885599b 573{
9dce4196 574 Elf_Internal_Shdr *hdr;
9dce4196
AM
575 unsigned char esym[sizeof (Elf64_External_Sym)];
576 Elf_External_Sym_Shndx eshndx;
577 Elf_Internal_Sym isym;
b885599b 578
13792e9d
L
579 /* First we need to ensure the symbol table is available. Make sure
580 that it is a symbol table section. */
4fbb74a6
AM
581 if (ghdr->sh_link >= elf_numsections (abfd))
582 return NULL;
13792e9d
L
583 hdr = elf_elfsections (abfd) [ghdr->sh_link];
584 if (hdr->sh_type != SHT_SYMTAB
585 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
b885599b
AM
586 return NULL;
587
9dce4196
AM
588 /* Go read the symbol. */
589 hdr = &elf_tdata (abfd)->symtab_hdr;
6cdc0ccc
AM
590 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
591 &isym, esym, &eshndx) == NULL)
b885599b 592 return NULL;
9dce4196 593
26c61ae5 594 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
b885599b
AM
595}
596
dbb410c3
AM
597/* Set next_in_group list pointer, and group name for NEWSECT. */
598
b34976b6 599static bfd_boolean
217aa764 600setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
dbb410c3
AM
601{
602 unsigned int num_group = elf_tdata (abfd)->num_group;
603
604 /* If num_group is zero, read in all SHT_GROUP sections. The count
605 is set to -1 if there are no SHT_GROUP sections. */
606 if (num_group == 0)
607 {
608 unsigned int i, shnum;
609
610 /* First count the number of groups. If we have a SHT_GROUP
611 section with just a flag word (ie. sh_size is 4), ignore it. */
9ad5cbcf 612 shnum = elf_numsections (abfd);
dbb410c3 613 num_group = 0;
08a40648 614
44534af3 615#define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
1783205a 616 ( (shdr)->sh_type == SHT_GROUP \
44534af3 617 && (shdr)->sh_size >= minsize \
1783205a
NC
618 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
619 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
08a40648 620
dbb410c3
AM
621 for (i = 0; i < shnum; i++)
622 {
623 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
1783205a 624
44534af3 625 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
dbb410c3
AM
626 num_group += 1;
627 }
628
629 if (num_group == 0)
20dbb49d
L
630 {
631 num_group = (unsigned) -1;
632 elf_tdata (abfd)->num_group = num_group;
ce497010 633 elf_tdata (abfd)->group_sect_ptr = NULL;
20dbb49d
L
634 }
635 else
dbb410c3
AM
636 {
637 /* We keep a list of elf section headers for group sections,
638 so we can find them quickly. */
1f4361a7 639 size_t amt;
d0fb9a8d 640
20dbb49d 641 elf_tdata (abfd)->num_group = num_group;
1f4361a7
AM
642 amt = num_group * sizeof (Elf_Internal_Shdr *);
643 elf_tdata (abfd)->group_sect_ptr
644 = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
dbb410c3 645 if (elf_tdata (abfd)->group_sect_ptr == NULL)
b34976b6 646 return FALSE;
dbb410c3 647 num_group = 0;
ce497010 648
dbb410c3
AM
649 for (i = 0; i < shnum; i++)
650 {
651 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
1783205a 652
44534af3 653 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
dbb410c3 654 {
973ffd63 655 unsigned char *src;
dbb410c3
AM
656 Elf_Internal_Group *dest;
657
07d6d2b8
AM
658 /* Make sure the group section has a BFD section
659 attached to it. */
660 if (!bfd_section_from_shdr (abfd, i))
661 return FALSE;
662
dbb410c3
AM
663 /* Add to list of sections. */
664 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
665 num_group += 1;
666
667 /* Read the raw contents. */
1f4361a7
AM
668 BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0);
669 shdr->contents = NULL;
670 if (_bfd_mul_overflow (shdr->sh_size,
671 sizeof (*dest) / 4, &amt)
1f4361a7 672 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
2bb3687b
AM
673 || !(shdr->contents
674 = _bfd_alloc_and_read (abfd, amt, shdr->sh_size)))
493a3386
NC
675 {
676 _bfd_error_handler
695344c0 677 /* xgettext:c-format */
871b3ab2 678 (_("%pB: invalid size field in group section"
2dcf00ce
AM
679 " header: %#" PRIx64 ""),
680 abfd, (uint64_t) shdr->sh_size);
493a3386
NC
681 bfd_set_error (bfd_error_bad_value);
682 -- num_group;
493a3386
NC
683 continue;
684 }
708d7d0d 685
dbb410c3
AM
686 /* Translate raw contents, a flag word followed by an
687 array of elf section indices all in target byte order,
688 to the flag word followed by an array of elf section
689 pointers. */
690 src = shdr->contents + shdr->sh_size;
691 dest = (Elf_Internal_Group *) (shdr->contents + amt);
06614111 692
dbb410c3
AM
693 while (1)
694 {
695 unsigned int idx;
696
697 src -= 4;
698 --dest;
699 idx = H_GET_32 (abfd, src);
700 if (src == shdr->contents)
701 {
327301a4 702 dest->shdr = NULL;
dbb410c3 703 dest->flags = idx;
b885599b
AM
704 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
705 shdr->bfd_section->flags
706 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
dbb410c3
AM
707 break;
708 }
4bba0fb1 709 if (idx < shnum)
bae363f1
L
710 {
711 dest->shdr = elf_elfsections (abfd)[idx];
712 /* PR binutils/23199: All sections in a
713 section group should be marked with
714 SHF_GROUP. But some tools generate
715 broken objects without SHF_GROUP. Fix
716 them up here. */
717 dest->shdr->sh_flags |= SHF_GROUP;
718 }
4bba0fb1
AM
719 if (idx >= shnum
720 || dest->shdr->sh_type == SHT_GROUP)
dbb410c3 721 {
4eca0228 722 _bfd_error_handler
4bba0fb1
AM
723 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
724 abfd, i);
725 dest->shdr = NULL;
dbb410c3 726 }
dbb410c3
AM
727 }
728 }
729 }
493a3386
NC
730
731 /* PR 17510: Corrupt binaries might contain invalid groups. */
732 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
733 {
734 elf_tdata (abfd)->num_group = num_group;
735
736 /* If all groups are invalid then fail. */
737 if (num_group == 0)
738 {
739 elf_tdata (abfd)->group_sect_ptr = NULL;
740 elf_tdata (abfd)->num_group = num_group = -1;
4eca0228 741 _bfd_error_handler
871b3ab2 742 (_("%pB: no valid group sections found"), abfd);
493a3386
NC
743 bfd_set_error (bfd_error_bad_value);
744 }
745 }
dbb410c3
AM
746 }
747 }
748
749 if (num_group != (unsigned) -1)
750 {
564e11c9
JW
751 unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
752 unsigned int j;
dbb410c3 753
564e11c9 754 for (j = 0; j < num_group; j++)
dbb410c3 755 {
564e11c9
JW
756 /* Begin search from previous found group. */
757 unsigned i = (j + search_offset) % num_group;
758
dbb410c3 759 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
ce497010 760 Elf_Internal_Group *idx;
0c54f692 761 bfd_size_type n_elt;
ce497010
NC
762
763 if (shdr == NULL)
764 continue;
765
766 idx = (Elf_Internal_Group *) shdr->contents;
0c54f692
NC
767 if (idx == NULL || shdr->sh_size < 4)
768 {
769 /* See PR 21957 for a reproducer. */
770 /* xgettext:c-format */
871b3ab2 771 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
0c54f692
NC
772 abfd, shdr->bfd_section);
773 elf_tdata (abfd)->group_sect_ptr[i] = NULL;
774 bfd_set_error (bfd_error_bad_value);
775 return FALSE;
776 }
ce497010 777 n_elt = shdr->sh_size / 4;
dbb410c3
AM
778
779 /* Look through this group's sections to see if current
780 section is a member. */
781 while (--n_elt != 0)
782 if ((++idx)->shdr == hdr)
783 {
e0e8c97f 784 asection *s = NULL;
dbb410c3
AM
785
786 /* We are a member of this group. Go looking through
787 other members to see if any others are linked via
788 next_in_group. */
789 idx = (Elf_Internal_Group *) shdr->contents;
790 n_elt = shdr->sh_size / 4;
791 while (--n_elt != 0)
4bba0fb1
AM
792 if ((++idx)->shdr != NULL
793 && (s = idx->shdr->bfd_section) != NULL
945906ff 794 && elf_next_in_group (s) != NULL)
dbb410c3
AM
795 break;
796 if (n_elt != 0)
797 {
dbb410c3
AM
798 /* Snarf the group name from other member, and
799 insert current section in circular list. */
945906ff
AM
800 elf_group_name (newsect) = elf_group_name (s);
801 elf_next_in_group (newsect) = elf_next_in_group (s);
802 elf_next_in_group (s) = newsect;
dbb410c3
AM
803 }
804 else
805 {
dbb410c3
AM
806 const char *gname;
807
b885599b
AM
808 gname = group_signature (abfd, shdr);
809 if (gname == NULL)
b34976b6 810 return FALSE;
945906ff 811 elf_group_name (newsect) = gname;
dbb410c3
AM
812
813 /* Start a circular list with one element. */
945906ff 814 elf_next_in_group (newsect) = newsect;
dbb410c3 815 }
b885599b 816
9dce4196
AM
817 /* If the group section has been created, point to the
818 new member. */
dbb410c3 819 if (shdr->bfd_section != NULL)
945906ff 820 elf_next_in_group (shdr->bfd_section) = newsect;
b885599b 821
564e11c9
JW
822 elf_tdata (abfd)->group_search_offset = i;
823 j = num_group - 1;
dbb410c3
AM
824 break;
825 }
826 }
827 }
828
945906ff 829 if (elf_group_name (newsect) == NULL)
dbb410c3 830 {
695344c0 831 /* xgettext:c-format */
871b3ab2 832 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
4eca0228 833 abfd, newsect);
493a3386 834 return FALSE;
dbb410c3 835 }
b34976b6 836 return TRUE;
dbb410c3
AM
837}
838
3d7f7666 839bfd_boolean
dd863624 840_bfd_elf_setup_sections (bfd *abfd)
3d7f7666
L
841{
842 unsigned int i;
843 unsigned int num_group = elf_tdata (abfd)->num_group;
844 bfd_boolean result = TRUE;
dd863624
L
845 asection *s;
846
847 /* Process SHF_LINK_ORDER. */
848 for (s = abfd->sections; s != NULL; s = s->next)
849 {
850 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
851 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
852 {
853 unsigned int elfsec = this_hdr->sh_link;
854 /* FIXME: The old Intel compiler and old strip/objcopy may
855 not set the sh_link or sh_info fields. Hence we could
856 get the situation where elfsec is 0. */
857 if (elfsec == 0)
858 {
4fbb74a6 859 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
a859124d
AM
860 bed->link_order_error_handler
861 /* xgettext:c-format */
862 (_("%pB: warning: sh_link not set for section `%pA'"),
863 abfd, s);
dd863624
L
864 }
865 else
866 {
91d6fa6a 867 asection *linksec = NULL;
25bbc984 868
4fbb74a6
AM
869 if (elfsec < elf_numsections (abfd))
870 {
871 this_hdr = elf_elfsections (abfd)[elfsec];
91d6fa6a 872 linksec = this_hdr->bfd_section;
4fbb74a6 873 }
25bbc984
L
874
875 /* PR 1991, 2008:
876 Some strip/objcopy may leave an incorrect value in
877 sh_link. We don't want to proceed. */
91d6fa6a 878 if (linksec == NULL)
25bbc984 879 {
4eca0228 880 _bfd_error_handler
695344c0 881 /* xgettext:c-format */
871b3ab2 882 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
c08bb8dd 883 s->owner, elfsec, s);
25bbc984
L
884 result = FALSE;
885 }
886
91d6fa6a 887 elf_linked_to_section (s) = linksec;
dd863624
L
888 }
889 }
53720c49
AM
890 else if (this_hdr->sh_type == SHT_GROUP
891 && elf_next_in_group (s) == NULL)
892 {
4eca0228 893 _bfd_error_handler
695344c0 894 /* xgettext:c-format */
871b3ab2 895 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
53720c49
AM
896 abfd, elf_section_data (s)->this_idx);
897 result = FALSE;
898 }
dd863624 899 }
3d7f7666 900
dd863624 901 /* Process section groups. */
3d7f7666
L
902 if (num_group == (unsigned) -1)
903 return result;
904
905 for (i = 0; i < num_group; i++)
906 {
907 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
4b0e8a5f
NC
908 Elf_Internal_Group *idx;
909 unsigned int n_elt;
3d7f7666 910
4b0e8a5f
NC
911 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
912 if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
913 {
4eca0228 914 _bfd_error_handler
695344c0 915 /* xgettext:c-format */
871b3ab2 916 (_("%pB: section group entry number %u is corrupt"),
4b0e8a5f
NC
917 abfd, i);
918 result = FALSE;
919 continue;
920 }
921
922 idx = (Elf_Internal_Group *) shdr->contents;
923 n_elt = shdr->sh_size / 4;
1b786873 924
3d7f7666 925 while (--n_elt != 0)
24d3e51b
NC
926 {
927 ++ idx;
928
929 if (idx->shdr == NULL)
930 continue;
931 else if (idx->shdr->bfd_section)
932 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
db4677b8
AM
933 else if (idx->shdr->sh_type != SHT_RELA
934 && idx->shdr->sh_type != SHT_REL)
24d3e51b
NC
935 {
936 /* There are some unknown sections in the group. */
937 _bfd_error_handler
938 /* xgettext:c-format */
871b3ab2 939 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
24d3e51b
NC
940 abfd,
941 idx->shdr->sh_type,
942 bfd_elf_string_from_elf_section (abfd,
943 (elf_elfheader (abfd)
944 ->e_shstrndx),
945 idx->shdr->sh_name),
946 shdr->bfd_section);
947 result = FALSE;
948 }
949 }
3d7f7666 950 }
24d3e51b 951
3d7f7666
L
952 return result;
953}
954
72adc230
AM
955bfd_boolean
956bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
957{
958 return elf_next_in_group (sec) != NULL;
959}
960
cb7f4b29
AM
961const char *
962bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
963{
964 if (elf_sec_group (sec) != NULL)
965 return elf_group_name (sec);
966 return NULL;
967}
968
f6fe1ccd
L
969static char *
970convert_debug_to_zdebug (bfd *abfd, const char *name)
971{
972 unsigned int len = strlen (name);
973 char *new_name = bfd_alloc (abfd, len + 2);
974 if (new_name == NULL)
975 return NULL;
976 new_name[0] = '.';
977 new_name[1] = 'z';
978 memcpy (new_name + 2, name + 1, len);
979 return new_name;
980}
981
982static char *
983convert_zdebug_to_debug (bfd *abfd, const char *name)
984{
985 unsigned int len = strlen (name);
986 char *new_name = bfd_alloc (abfd, len);
987 if (new_name == NULL)
988 return NULL;
989 new_name[0] = '.';
990 memcpy (new_name + 1, name + 2, len - 1);
991 return new_name;
992}
993
cc5277b1
ML
994/* This a copy of lto_section defined in GCC (lto-streamer.h). */
995
996struct lto_section
997{
998 int16_t major_version;
999 int16_t minor_version;
1000 unsigned char slim_object;
1001
1002 /* Flags is a private field that is not defined publicly. */
1003 uint16_t flags;
1004};
1005
252b5132
RH
1006/* Make a BFD section from an ELF section. We store a pointer to the
1007 BFD section in the bfd_section field of the header. */
1008
b34976b6 1009bfd_boolean
217aa764
AM
1010_bfd_elf_make_section_from_shdr (bfd *abfd,
1011 Elf_Internal_Shdr *hdr,
6dc132d9
L
1012 const char *name,
1013 int shindex)
252b5132
RH
1014{
1015 asection *newsect;
1016 flagword flags;
9c5bfbb7 1017 const struct elf_backend_data *bed;
502794d4 1018 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132
RH
1019
1020 if (hdr->bfd_section != NULL)
4e011fb5 1021 return TRUE;
252b5132
RH
1022
1023 newsect = bfd_make_section_anyway (abfd, name);
1024 if (newsect == NULL)
b34976b6 1025 return FALSE;
252b5132 1026
1829f4b2
AM
1027 hdr->bfd_section = newsect;
1028 elf_section_data (newsect)->this_hdr = *hdr;
6dc132d9 1029 elf_section_data (newsect)->this_idx = shindex;
1829f4b2 1030
2f89ff8d
L
1031 /* Always use the real type/flags. */
1032 elf_section_type (newsect) = hdr->sh_type;
1033 elf_section_flags (newsect) = hdr->sh_flags;
1034
252b5132
RH
1035 newsect->filepos = hdr->sh_offset;
1036
252b5132
RH
1037 flags = SEC_NO_FLAGS;
1038 if (hdr->sh_type != SHT_NOBITS)
1039 flags |= SEC_HAS_CONTENTS;
dbb410c3 1040 if (hdr->sh_type == SHT_GROUP)
7bdf4127 1041 flags |= SEC_GROUP;
252b5132
RH
1042 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1043 {
1044 flags |= SEC_ALLOC;
1045 if (hdr->sh_type != SHT_NOBITS)
1046 flags |= SEC_LOAD;
1047 }
1048 if ((hdr->sh_flags & SHF_WRITE) == 0)
1049 flags |= SEC_READONLY;
1050 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1051 flags |= SEC_CODE;
1052 else if ((flags & SEC_LOAD) != 0)
1053 flags |= SEC_DATA;
f5fa8ca2
JJ
1054 if ((hdr->sh_flags & SHF_MERGE) != 0)
1055 {
1056 flags |= SEC_MERGE;
1057 newsect->entsize = hdr->sh_entsize;
f5fa8ca2 1058 }
84865015
NC
1059 if ((hdr->sh_flags & SHF_STRINGS) != 0)
1060 flags |= SEC_STRINGS;
dbb410c3
AM
1061 if (hdr->sh_flags & SHF_GROUP)
1062 if (!setup_group (abfd, hdr, newsect))
b34976b6 1063 return FALSE;
13ae64f3
JJ
1064 if ((hdr->sh_flags & SHF_TLS) != 0)
1065 flags |= SEC_THREAD_LOCAL;
18ae9cc1
L
1066 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1067 flags |= SEC_EXCLUDE;
252b5132 1068
df3a023b
AM
1069 switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
1070 {
1071 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1072 but binutils as of 2019-07-23 did not set the EI_OSABI header
1073 byte. */
1074 case ELFOSABI_NONE:
1075 case ELFOSABI_GNU:
1076 case ELFOSABI_FREEBSD:
1077 if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
1078 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1079 break;
1080 }
1081
3d2b39cf 1082 if ((flags & SEC_ALLOC) == 0)
7a6cc5fb 1083 {
3d2b39cf
L
1084 /* The debugging sections appear to be recognized only by name,
1085 not any sort of flag. Their SEC_ALLOC bits are cleared. */
3d2b39cf
L
1086 if (name [0] == '.')
1087 {
bb294208
AM
1088 if (strncmp (name, ".debug", 6) == 0
1089 || strncmp (name, ".gnu.linkonce.wi.", 17) == 0
1090 || strncmp (name, ".zdebug", 7) == 0)
1091 flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
1092 else if (strncmp (name, GNU_BUILD_ATTRS_SECTION_NAME, 21) == 0
1093 || strncmp (name, ".note.gnu", 9) == 0)
502794d4
CE
1094 {
1095 flags |= SEC_ELF_OCTETS;
1096 opb = 1;
1097 }
bb294208
AM
1098 else if (strncmp (name, ".line", 5) == 0
1099 || strncmp (name, ".stab", 5) == 0
1100 || strcmp (name, ".gdb_index") == 0)
3d2b39cf
L
1101 flags |= SEC_DEBUGGING;
1102 }
1103 }
252b5132 1104
502794d4
CE
1105 if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
1106 || !bfd_set_section_size (newsect, hdr->sh_size)
1107 || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign)))
1108 return FALSE;
1109
252b5132
RH
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
8c803a2d
AM
1120 if (!bfd_set_section_flags (newsect, flags))
1121 return FALSE;
1122
fa152c49
JW
1123 bed = get_elf_backend_data (abfd);
1124 if (bed->elf_backend_section_flags)
8c803a2d 1125 if (!bed->elf_backend_section_flags (hdr))
b34976b6 1126 return FALSE;
fa152c49 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
8c803a2d 1143 if ((newsect->flags & SEC_ALLOC) != 0)
252b5132
RH
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 {
8c803a2d 1169 if ((newsect->flags & SEC_LOAD) == 0)
88967714 1170 newsect->lma = (phdr->p_paddr
502794d4 1171 + hdr->sh_addr - phdr->p_vaddr) / opb;
88967714
AM
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
502794d4 1181 + hdr->sh_offset - phdr->p_offset) / opb;
88967714
AM
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. */
8c803a2d 1197 if ((newsect->flags & SEC_DEBUGGING)
4a114e3e
L
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;
fd361982 1269 bfd_rename_section (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
cc5277b1
ML
1278 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1279 section. */
1280 const char *lto_section_name = ".gnu.lto_.lto.";
1281 if (strncmp (name, lto_section_name, strlen (lto_section_name)) == 0)
1282 {
1283 struct lto_section lsection;
1284 if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
1285 sizeof (struct lto_section)))
1286 abfd->lto_slim_object = lsection.slim_object;
1287 }
1288
b34976b6 1289 return TRUE;
252b5132
RH
1290}
1291
84865015
NC
1292const char *const bfd_elf_section_type_names[] =
1293{
252b5132
RH
1294 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1295 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1296 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1297};
1298
1049f94e 1299/* ELF relocs are against symbols. If we are producing relocatable
252b5132
RH
1300 output, and the reloc is against an external symbol, and nothing
1301 has given us any additional addend, the resulting reloc will also
1302 be against the same symbol. In such a case, we don't want to
1303 change anything about the way the reloc is handled, since it will
1304 all be done at final link time. Rather than put special case code
1305 into bfd_perform_relocation, all the reloc types use this howto
1306 function. It just short circuits the reloc if producing
1049f94e 1307 relocatable output against an external symbol. */
252b5132 1308
252b5132 1309bfd_reloc_status_type
217aa764
AM
1310bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1311 arelent *reloc_entry,
1312 asymbol *symbol,
1313 void *data ATTRIBUTE_UNUSED,
1314 asection *input_section,
1315 bfd *output_bfd,
1316 char **error_message ATTRIBUTE_UNUSED)
1317{
1318 if (output_bfd != NULL
252b5132
RH
1319 && (symbol->flags & BSF_SECTION_SYM) == 0
1320 && (! reloc_entry->howto->partial_inplace
1321 || reloc_entry->addend == 0))
1322 {
1323 reloc_entry->address += input_section->output_offset;
1324 return bfd_reloc_ok;
1325 }
1326
1327 return bfd_reloc_continue;
1328}
1329\f
84865015
NC
1330/* Returns TRUE if section A matches section B.
1331 Names, addresses and links may be different, but everything else
1332 should be the same. */
1333
1334static bfd_boolean
5522f910
NC
1335section_match (const Elf_Internal_Shdr * a,
1336 const Elf_Internal_Shdr * b)
84865015 1337{
ac85e67c
AM
1338 if (a->sh_type != b->sh_type
1339 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1340 || a->sh_addralign != b->sh_addralign
1341 || a->sh_entsize != b->sh_entsize)
1342 return FALSE;
1343 if (a->sh_type == SHT_SYMTAB
1344 || a->sh_type == SHT_STRTAB)
1345 return TRUE;
1346 return a->sh_size == b->sh_size;
84865015
NC
1347}
1348
1349/* Find a section in OBFD that has the same characteristics
1350 as IHEADER. Return the index of this section or SHN_UNDEF if
1351 none can be found. Check's section HINT first, as this is likely
1352 to be the correct section. */
1353
1354static unsigned int
5cc4ca83
ST
1355find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1356 const unsigned int hint)
84865015
NC
1357{
1358 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1359 unsigned int i;
1360
a55c9876
NC
1361 BFD_ASSERT (iheader != NULL);
1362
1363 /* See PR 20922 for a reproducer of the NULL test. */
5cc4ca83
ST
1364 if (hint < elf_numsections (obfd)
1365 && oheaders[hint] != NULL
a55c9876 1366 && section_match (oheaders[hint], iheader))
84865015
NC
1367 return hint;
1368
1369 for (i = 1; i < elf_numsections (obfd); i++)
1370 {
1371 Elf_Internal_Shdr * oheader = oheaders[i];
1372
a55c9876
NC
1373 if (oheader == NULL)
1374 continue;
84865015
NC
1375 if (section_match (oheader, iheader))
1376 /* FIXME: Do we care if there is a potential for
1377 multiple matches ? */
1378 return i;
1379 }
1380
1381 return SHN_UNDEF;
1382}
1383
5522f910
NC
1384/* PR 19938: Attempt to set the ELF section header fields of an OS or
1385 Processor specific section, based upon a matching input section.
1386 Returns TRUE upon success, FALSE otherwise. */
07d6d2b8 1387
5522f910
NC
1388static bfd_boolean
1389copy_special_section_fields (const bfd *ibfd,
1390 bfd *obfd,
1391 const Elf_Internal_Shdr *iheader,
1392 Elf_Internal_Shdr *oheader,
1393 const unsigned int secnum)
1394{
1395 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1396 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1397 bfd_boolean changed = FALSE;
1398 unsigned int sh_link;
1399
1400 if (oheader->sh_type == SHT_NOBITS)
1401 {
1402 /* This is a feature for objcopy --only-keep-debug:
1403 When a section's type is changed to NOBITS, we preserve
1404 the sh_link and sh_info fields so that they can be
1405 matched up with the original.
1406
1407 Note: Strictly speaking these assignments are wrong.
1408 The sh_link and sh_info fields should point to the
1409 relevent sections in the output BFD, which may not be in
1410 the same location as they were in the input BFD. But
1411 the whole point of this action is to preserve the
1412 original values of the sh_link and sh_info fields, so
1413 that they can be matched up with the section headers in
1414 the original file. So strictly speaking we may be
1415 creating an invalid ELF file, but it is only for a file
1416 that just contains debug info and only for sections
1417 without any contents. */
1418 if (oheader->sh_link == 0)
1419 oheader->sh_link = iheader->sh_link;
1420 if (oheader->sh_info == 0)
1421 oheader->sh_info = iheader->sh_info;
1422 return TRUE;
1423 }
1424
1425 /* Allow the target a chance to decide how these fields should be set. */
a859124d
AM
1426 if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1427 iheader, oheader))
5522f910
NC
1428 return TRUE;
1429
1430 /* We have an iheader which might match oheader, and which has non-zero
1431 sh_info and/or sh_link fields. Attempt to follow those links and find
1432 the section in the output bfd which corresponds to the linked section
1433 in the input bfd. */
1434 if (iheader->sh_link != SHN_UNDEF)
1435 {
4f3ca05b
NC
1436 /* See PR 20931 for a reproducer. */
1437 if (iheader->sh_link >= elf_numsections (ibfd))
1438 {
76cfced5 1439 _bfd_error_handler
4f3ca05b 1440 /* xgettext:c-format */
9793eb77 1441 (_("%pB: invalid sh_link field (%d) in section number %d"),
4f3ca05b
NC
1442 ibfd, iheader->sh_link, secnum);
1443 return FALSE;
1444 }
1445
5522f910
NC
1446 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1447 if (sh_link != SHN_UNDEF)
1448 {
1449 oheader->sh_link = sh_link;
1450 changed = TRUE;
1451 }
1452 else
1453 /* FIXME: Should we install iheader->sh_link
1454 if we could not find a match ? */
76cfced5 1455 _bfd_error_handler
695344c0 1456 /* xgettext:c-format */
9793eb77 1457 (_("%pB: failed to find link section for section %d"), obfd, secnum);
5522f910
NC
1458 }
1459
1460 if (iheader->sh_info)
1461 {
1462 /* The sh_info field can hold arbitrary information, but if the
1463 SHF_LINK_INFO flag is set then it should be interpreted as a
1464 section index. */
1465 if (iheader->sh_flags & SHF_INFO_LINK)
1466 {
1467 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1468 iheader->sh_info);
1469 if (sh_link != SHN_UNDEF)
1470 oheader->sh_flags |= SHF_INFO_LINK;
1471 }
1472 else
1473 /* No idea what it means - just copy it. */
1474 sh_link = iheader->sh_info;
1475
1476 if (sh_link != SHN_UNDEF)
1477 {
1478 oheader->sh_info = sh_link;
1479 changed = TRUE;
1480 }
1481 else
76cfced5 1482 _bfd_error_handler
695344c0 1483 /* xgettext:c-format */
9793eb77 1484 (_("%pB: failed to find info section for section %d"), obfd, secnum);
5522f910
NC
1485 }
1486
1487 return changed;
1488}
07d6d2b8 1489
0ac4564e
L
1490/* Copy the program header and other data from one object module to
1491 another. */
252b5132 1492
b34976b6 1493bfd_boolean
217aa764 1494_bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
2d502050 1495{
5522f910
NC
1496 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1497 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1498 const struct elf_backend_data *bed;
84865015
NC
1499 unsigned int i;
1500
2d502050 1501 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
84865015 1502 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 1503 return TRUE;
2d502050 1504
57b828ef
L
1505 if (!elf_flags_init (obfd))
1506 {
1507 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1508 elf_flags_init (obfd) = TRUE;
1509 }
2d502050 1510
0ac4564e 1511 elf_gp (obfd) = elf_gp (ibfd);
57b828ef
L
1512
1513 /* Also copy the EI_OSABI field. */
1514 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1515 elf_elfheader (ibfd)->e_ident[EI_OSABI];
104d59d1 1516
5522f910
NC
1517 /* If set, copy the EI_ABIVERSION field. */
1518 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1519 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1520 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
07d6d2b8 1521
104d59d1
JM
1522 /* Copy object attributes. */
1523 _bfd_elf_copy_obj_attributes (ibfd, obfd);
63b9bbb7 1524
84865015
NC
1525 if (iheaders == NULL || oheaders == NULL)
1526 return TRUE;
63b9bbb7 1527
5522f910
NC
1528 bed = get_elf_backend_data (obfd);
1529
1530 /* Possibly copy other fields in the section header. */
84865015 1531 for (i = 1; i < elf_numsections (obfd); i++)
63b9bbb7 1532 {
84865015
NC
1533 unsigned int j;
1534 Elf_Internal_Shdr * oheader = oheaders[i];
63b9bbb7 1535
5522f910
NC
1536 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1537 because of a special case need for generating separate debug info
1538 files. See below for more details. */
84865015
NC
1539 if (oheader == NULL
1540 || (oheader->sh_type != SHT_NOBITS
5522f910
NC
1541 && oheader->sh_type < SHT_LOOS))
1542 continue;
1543
1544 /* Ignore empty sections, and sections whose
1545 fields have already been initialised. */
1546 if (oheader->sh_size == 0
84865015
NC
1547 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1548 continue;
63b9bbb7 1549
84865015 1550 /* Scan for the matching section in the input bfd.
5522f910
NC
1551 First we try for a direct mapping between the input and output sections. */
1552 for (j = 1; j < elf_numsections (ibfd); j++)
1553 {
1554 const Elf_Internal_Shdr * iheader = iheaders[j];
1555
1556 if (iheader == NULL)
1557 continue;
1558
1559 if (oheader->bfd_section != NULL
1560 && iheader->bfd_section != NULL
1561 && iheader->bfd_section->output_section != NULL
1562 && iheader->bfd_section->output_section == oheader->bfd_section)
1563 {
1564 /* We have found a connection from the input section to the
1565 output section. Attempt to copy the header fields. If
1566 this fails then do not try any further sections - there
1567 should only be a one-to-one mapping between input and output. */
1568 if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1569 j = elf_numsections (ibfd);
1570 break;
1571 }
1572 }
1573
1574 if (j < elf_numsections (ibfd))
1575 continue;
1576
1577 /* That failed. So try to deduce the corresponding input section.
84865015
NC
1578 Unfortunately we cannot compare names as the output string table
1579 is empty, so instead we check size, address and type. */
1580 for (j = 1; j < elf_numsections (ibfd); j++)
1581 {
5522f910 1582 const Elf_Internal_Shdr * iheader = iheaders[j];
84865015 1583
5522f910
NC
1584 if (iheader == NULL)
1585 continue;
1586
1587 /* Try matching fields in the input section's header.
1588 Since --only-keep-debug turns all non-debug sections into
84865015
NC
1589 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1590 input type. */
1591 if ((oheader->sh_type == SHT_NOBITS
1592 || iheader->sh_type == oheader->sh_type)
5522f910
NC
1593 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1594 == (oheader->sh_flags & ~ SHF_INFO_LINK)
84865015
NC
1595 && iheader->sh_addralign == oheader->sh_addralign
1596 && iheader->sh_entsize == oheader->sh_entsize
1597 && iheader->sh_size == oheader->sh_size
1598 && iheader->sh_addr == oheader->sh_addr
1599 && (iheader->sh_info != oheader->sh_info
1600 || iheader->sh_link != oheader->sh_link))
63b9bbb7 1601 {
5522f910
NC
1602 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1603 break;
63b9bbb7
NC
1604 }
1605 }
5522f910
NC
1606
1607 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1608 {
1609 /* Final attempt. Call the backend copy function
1610 with a NULL input section. */
a859124d
AM
1611 (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1612 NULL, oheader);
5522f910 1613 }
63b9bbb7
NC
1614 }
1615
b34976b6 1616 return TRUE;
2d502050
L
1617}
1618
cedc298e
L
1619static const char *
1620get_segment_type (unsigned int p_type)
1621{
1622 const char *pt;
1623 switch (p_type)
1624 {
1625 case PT_NULL: pt = "NULL"; break;
1626 case PT_LOAD: pt = "LOAD"; break;
1627 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1628 case PT_INTERP: pt = "INTERP"; break;
1629 case PT_NOTE: pt = "NOTE"; break;
1630 case PT_SHLIB: pt = "SHLIB"; break;
1631 case PT_PHDR: pt = "PHDR"; break;
1632 case PT_TLS: pt = "TLS"; break;
1633 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
2b05f1b7 1634 case PT_GNU_STACK: pt = "STACK"; break;
cedc298e
L
1635 case PT_GNU_RELRO: pt = "RELRO"; break;
1636 default: pt = NULL; break;
1637 }
1638 return pt;
1639}
1640
f0b79d91
L
1641/* Print out the program headers. */
1642
b34976b6 1643bfd_boolean
217aa764 1644_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
252b5132 1645{
a50b1753 1646 FILE *f = (FILE *) farg;
252b5132
RH
1647 Elf_Internal_Phdr *p;
1648 asection *s;
1649 bfd_byte *dynbuf = NULL;
1650
1651 p = elf_tdata (abfd)->phdr;
1652 if (p != NULL)
1653 {
1654 unsigned int i, c;
1655
1656 fprintf (f, _("\nProgram Header:\n"));
1657 c = elf_elfheader (abfd)->e_phnum;
1658 for (i = 0; i < c; i++, p++)
1659 {
cedc298e 1660 const char *pt = get_segment_type (p->p_type);
252b5132
RH
1661 char buf[20];
1662
cedc298e 1663 if (pt == NULL)
252b5132 1664 {
cedc298e
L
1665 sprintf (buf, "0x%lx", p->p_type);
1666 pt = buf;
252b5132 1667 }
dc810e39 1668 fprintf (f, "%8s off 0x", pt);
60b89a18 1669 bfd_fprintf_vma (abfd, f, p->p_offset);
252b5132 1670 fprintf (f, " vaddr 0x");
60b89a18 1671 bfd_fprintf_vma (abfd, f, p->p_vaddr);
252b5132 1672 fprintf (f, " paddr 0x");
60b89a18 1673 bfd_fprintf_vma (abfd, f, p->p_paddr);
252b5132
RH
1674 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1675 fprintf (f, " filesz 0x");
60b89a18 1676 bfd_fprintf_vma (abfd, f, p->p_filesz);
252b5132 1677 fprintf (f, " memsz 0x");
60b89a18 1678 bfd_fprintf_vma (abfd, f, p->p_memsz);
252b5132
RH
1679 fprintf (f, " flags %c%c%c",
1680 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1681 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1682 (p->p_flags & PF_X) != 0 ? 'x' : '-');
dc810e39
AM
1683 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1684 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
252b5132
RH
1685 fprintf (f, "\n");
1686 }
1687 }
1688
1689 s = bfd_get_section_by_name (abfd, ".dynamic");
1690 if (s != NULL)
1691 {
cb33740c 1692 unsigned int elfsec;
dc810e39 1693 unsigned long shlink;
252b5132
RH
1694 bfd_byte *extdyn, *extdynend;
1695 size_t extdynsize;
217aa764 1696 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
252b5132
RH
1697
1698 fprintf (f, _("\nDynamic Section:\n"));
1699
eea6121a 1700 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
252b5132
RH
1701 goto error_return;
1702
1703 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 1704 if (elfsec == SHN_BAD)
252b5132 1705 goto error_return;
dc810e39 1706 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
252b5132
RH
1707
1708 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1709 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1710
1711 extdyn = dynbuf;
06614111
NC
1712 /* PR 17512: file: 6f427532. */
1713 if (s->size < extdynsize)
1714 goto error_return;
eea6121a 1715 extdynend = extdyn + s->size;
1036838a 1716 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
07d6d2b8 1717 Fix range check. */
1036838a 1718 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
252b5132
RH
1719 {
1720 Elf_Internal_Dyn dyn;
ad9563d6 1721 const char *name = "";
252b5132 1722 char ab[20];
b34976b6 1723 bfd_boolean stringp;
ad9563d6 1724 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 1725
217aa764 1726 (*swap_dyn_in) (abfd, extdyn, &dyn);
252b5132
RH
1727
1728 if (dyn.d_tag == DT_NULL)
1729 break;
1730
b34976b6 1731 stringp = FALSE;
252b5132
RH
1732 switch (dyn.d_tag)
1733 {
1734 default:
ad9563d6
CM
1735 if (bed->elf_backend_get_target_dtag)
1736 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1737
1738 if (!strcmp (name, ""))
1739 {
cd9af601 1740 sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag);
ad9563d6
CM
1741 name = ab;
1742 }
252b5132
RH
1743 break;
1744
b34976b6 1745 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
252b5132
RH
1746 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1747 case DT_PLTGOT: name = "PLTGOT"; break;
1748 case DT_HASH: name = "HASH"; break;
1749 case DT_STRTAB: name = "STRTAB"; break;
1750 case DT_SYMTAB: name = "SYMTAB"; break;
1751 case DT_RELA: name = "RELA"; break;
1752 case DT_RELASZ: name = "RELASZ"; break;
1753 case DT_RELAENT: name = "RELAENT"; break;
1754 case DT_STRSZ: name = "STRSZ"; break;
1755 case DT_SYMENT: name = "SYMENT"; break;
1756 case DT_INIT: name = "INIT"; break;
1757 case DT_FINI: name = "FINI"; break;
b34976b6
AM
1758 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1759 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
252b5132
RH
1760 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1761 case DT_REL: name = "REL"; break;
1762 case DT_RELSZ: name = "RELSZ"; break;
1763 case DT_RELENT: name = "RELENT"; break;
1764 case DT_PLTREL: name = "PLTREL"; break;
1765 case DT_DEBUG: name = "DEBUG"; break;
1766 case DT_TEXTREL: name = "TEXTREL"; break;
1767 case DT_JMPREL: name = "JMPREL"; break;
94558834
L
1768 case DT_BIND_NOW: name = "BIND_NOW"; break;
1769 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1770 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1771 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1772 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
b34976b6 1773 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
94558834
L
1774 case DT_FLAGS: name = "FLAGS"; break;
1775 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1776 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
d48188b9 1777 case DT_CHECKSUM: name = "CHECKSUM"; break;
94558834
L
1778 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1779 case DT_MOVEENT: name = "MOVEENT"; break;
1780 case DT_MOVESZ: name = "MOVESZ"; break;
1781 case DT_FEATURE: name = "FEATURE"; break;
1782 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1783 case DT_SYMINSZ: name = "SYMINSZ"; break;
1784 case DT_SYMINENT: name = "SYMINENT"; break;
b34976b6
AM
1785 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1786 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1787 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
94558834
L
1788 case DT_PLTPAD: name = "PLTPAD"; break;
1789 case DT_MOVETAB: name = "MOVETAB"; break;
1790 case DT_SYMINFO: name = "SYMINFO"; break;
1791 case DT_RELACOUNT: name = "RELACOUNT"; break;
1792 case DT_RELCOUNT: name = "RELCOUNT"; break;
1793 case DT_FLAGS_1: name = "FLAGS_1"; break;
252b5132
RH
1794 case DT_VERSYM: name = "VERSYM"; break;
1795 case DT_VERDEF: name = "VERDEF"; break;
1796 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1797 case DT_VERNEED: name = "VERNEED"; break;
1798 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
b34976b6 1799 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
94558834 1800 case DT_USED: name = "USED"; break;
b34976b6 1801 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
fdc90cb4 1802 case DT_GNU_HASH: name = "GNU_HASH"; break;
252b5132
RH
1803 }
1804
ad9563d6 1805 fprintf (f, " %-20s ", name);
252b5132 1806 if (! stringp)
a1f3c56e
AN
1807 {
1808 fprintf (f, "0x");
1809 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1810 }
252b5132
RH
1811 else
1812 {
1813 const char *string;
dc810e39 1814 unsigned int tagv = dyn.d_un.d_val;
252b5132 1815
dc810e39 1816 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
252b5132
RH
1817 if (string == NULL)
1818 goto error_return;
1819 fprintf (f, "%s", string);
1820 }
1821 fprintf (f, "\n");
1822 }
1823
1824 free (dynbuf);
1825 dynbuf = NULL;
1826 }
1827
1828 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1829 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1830 {
fc0e6df6 1831 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
b34976b6 1832 return FALSE;
252b5132
RH
1833 }
1834
1835 if (elf_dynverdef (abfd) != 0)
1836 {
1837 Elf_Internal_Verdef *t;
1838
1839 fprintf (f, _("\nVersion definitions:\n"));
1840 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1841 {
1842 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
d0fb9a8d
JJ
1843 t->vd_flags, t->vd_hash,
1844 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1845 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
252b5132
RH
1846 {
1847 Elf_Internal_Verdaux *a;
1848
1849 fprintf (f, "\t");
1850 for (a = t->vd_auxptr->vda_nextptr;
1851 a != NULL;
1852 a = a->vda_nextptr)
d0fb9a8d
JJ
1853 fprintf (f, "%s ",
1854 a->vda_nodename ? a->vda_nodename : "<corrupt>");
252b5132
RH
1855 fprintf (f, "\n");
1856 }
1857 }
1858 }
1859
1860 if (elf_dynverref (abfd) != 0)
1861 {
1862 Elf_Internal_Verneed *t;
1863
1864 fprintf (f, _("\nVersion References:\n"));
1865 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1866 {
1867 Elf_Internal_Vernaux *a;
1868
d0fb9a8d
JJ
1869 fprintf (f, _(" required from %s:\n"),
1870 t->vn_filename ? t->vn_filename : "<corrupt>");
252b5132
RH
1871 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1872 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
d0fb9a8d
JJ
1873 a->vna_flags, a->vna_other,
1874 a->vna_nodename ? a->vna_nodename : "<corrupt>");
252b5132
RH
1875 }
1876 }
1877
b34976b6 1878 return TRUE;
252b5132
RH
1879
1880 error_return:
1881 if (dynbuf != NULL)
1882 free (dynbuf);
b34976b6 1883 return FALSE;
252b5132
RH
1884}
1885
7e6e972f
L
1886/* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
1887 and return symbol version for symbol version itself. */
bb4d2ac2
L
1888
1889const char *
1081065c
L
1890_bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
1891 bfd_boolean base_p,
1892 bfd_boolean *hidden)
bb4d2ac2
L
1893{
1894 const char *version_string = NULL;
1895 if (elf_dynversym (abfd) != 0
1896 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1897 {
1898 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1899
1900 *hidden = (vernum & VERSYM_HIDDEN) != 0;
1901 vernum &= VERSYM_VERSION;
1902
1903 if (vernum == 0)
1904 version_string = "";
1f6f5dba
L
1905 else if (vernum == 1
1906 && (vernum > elf_tdata (abfd)->cverdefs
1907 || (elf_tdata (abfd)->verdef[0].vd_flags
1908 == VER_FLG_BASE)))
7e6e972f 1909 version_string = base_p ? "Base" : "";
bb4d2ac2 1910 else if (vernum <= elf_tdata (abfd)->cverdefs)
7e6e972f
L
1911 {
1912 const char *nodename
1913 = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1914 version_string = ((base_p || strcmp (symbol->name, nodename))
1915 ? nodename : "");
1916 }
bb4d2ac2
L
1917 else
1918 {
1919 Elf_Internal_Verneed *t;
1920
7a815dd5 1921 version_string = _("<corrupt>");
bb4d2ac2
L
1922 for (t = elf_tdata (abfd)->verref;
1923 t != NULL;
1924 t = t->vn_nextref)
1925 {
1926 Elf_Internal_Vernaux *a;
1927
1928 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1929 {
1930 if (a->vna_other == vernum)
1931 {
1932 version_string = a->vna_nodename;
1933 break;
1934 }
1935 }
1936 }
1937 }
1938 }
1939 return version_string;
1940}
1941
252b5132
RH
1942/* Display ELF-specific fields of a symbol. */
1943
1944void
217aa764
AM
1945bfd_elf_print_symbol (bfd *abfd,
1946 void *filep,
1947 asymbol *symbol,
1948 bfd_print_symbol_type how)
252b5132 1949{
a50b1753 1950 FILE *file = (FILE *) filep;
252b5132
RH
1951 switch (how)
1952 {
1953 case bfd_print_symbol_name:
1954 fprintf (file, "%s", symbol->name);
1955 break;
1956 case bfd_print_symbol_more:
1957 fprintf (file, "elf ");
60b89a18 1958 bfd_fprintf_vma (abfd, file, symbol->value);
cd9af601 1959 fprintf (file, " %x", symbol->flags);
252b5132
RH
1960 break;
1961 case bfd_print_symbol_all:
1962 {
4e8a9624
AM
1963 const char *section_name;
1964 const char *name = NULL;
9c5bfbb7 1965 const struct elf_backend_data *bed;
7a13edea 1966 unsigned char st_other;
dbb410c3 1967 bfd_vma val;
bb4d2ac2
L
1968 const char *version_string;
1969 bfd_boolean hidden;
c044fabd 1970
252b5132 1971 section_name = symbol->section ? symbol->section->name : "(*none*)";
587ff49e
RH
1972
1973 bed = get_elf_backend_data (abfd);
1974 if (bed->elf_backend_print_symbol_all)
c044fabd 1975 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
587ff49e
RH
1976
1977 if (name == NULL)
1978 {
7ee38065 1979 name = symbol->name;
217aa764 1980 bfd_print_symbol_vandf (abfd, file, symbol);
587ff49e
RH
1981 }
1982
252b5132
RH
1983 fprintf (file, " %s\t", section_name);
1984 /* Print the "other" value for a symbol. For common symbols,
1985 we've already printed the size; now print the alignment.
1986 For other symbols, we have no specified alignment, and
1987 we've printed the address; now print the size. */
dcf6c779 1988 if (symbol->section && bfd_is_com_section (symbol->section))
dbb410c3
AM
1989 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1990 else
1991 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1992 bfd_fprintf_vma (abfd, file, val);
252b5132
RH
1993
1994 /* If we have version information, print it. */
60bb06bc
L
1995 version_string = _bfd_elf_get_symbol_version_string (abfd,
1996 symbol,
1081065c 1997 TRUE,
60bb06bc 1998 &hidden);
bb4d2ac2 1999 if (version_string)
252b5132 2000 {
bb4d2ac2 2001 if (!hidden)
252b5132
RH
2002 fprintf (file, " %-11s", version_string);
2003 else
2004 {
2005 int i;
2006
2007 fprintf (file, " (%s)", version_string);
2008 for (i = 10 - strlen (version_string); i > 0; --i)
2009 putc (' ', file);
2010 }
2011 }
2012
2013 /* If the st_other field is not zero, print it. */
7a13edea 2014 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
c044fabd 2015
7a13edea
NC
2016 switch (st_other)
2017 {
2018 case 0: break;
2019 case STV_INTERNAL: fprintf (file, " .internal"); break;
2020 case STV_HIDDEN: fprintf (file, " .hidden"); break;
2021 case STV_PROTECTED: fprintf (file, " .protected"); break;
2022 default:
2023 /* Some other non-defined flags are also present, so print
2024 everything hex. */
2025 fprintf (file, " 0x%02x", (unsigned int) st_other);
2026 }
252b5132 2027
587ff49e 2028 fprintf (file, " %s", name);
252b5132
RH
2029 }
2030 break;
2031 }
2032}
252b5132
RH
2033\f
2034/* ELF .o/exec file reading */
2035
c044fabd 2036/* Create a new bfd section from an ELF section header. */
252b5132 2037
b34976b6 2038bfd_boolean
217aa764 2039bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
252b5132 2040{
4fbb74a6
AM
2041 Elf_Internal_Shdr *hdr;
2042 Elf_Internal_Ehdr *ehdr;
2043 const struct elf_backend_data *bed;
90937f86 2044 const char *name;
bf67003b
NC
2045 bfd_boolean ret = TRUE;
2046 static bfd_boolean * sections_being_created = NULL;
5a4b0ccc 2047 static bfd * sections_being_created_abfd = NULL;
bf67003b 2048 static unsigned int nesting = 0;
252b5132 2049
4fbb74a6
AM
2050 if (shindex >= elf_numsections (abfd))
2051 return FALSE;
2052
bf67003b
NC
2053 if (++ nesting > 3)
2054 {
2055 /* PR17512: A corrupt ELF binary might contain a recursive group of
67ce483b 2056 sections, with each the string indices pointing to the next in the
bf67003b
NC
2057 loop. Detect this here, by refusing to load a section that we are
2058 already in the process of loading. We only trigger this test if
2059 we have nested at least three sections deep as normal ELF binaries
5a4b0ccc
NC
2060 can expect to recurse at least once.
2061
2062 FIXME: It would be better if this array was attached to the bfd,
2063 rather than being held in a static pointer. */
2064
2065 if (sections_being_created_abfd != abfd)
2066 sections_being_created = NULL;
bf67003b
NC
2067 if (sections_being_created == NULL)
2068 {
446f7ed5
AM
2069 size_t amt = elf_numsections (abfd) * sizeof (bfd_boolean);
2070 sections_being_created = (bfd_boolean *) bfd_zalloc (abfd, amt);
96d3b80f
AM
2071 if (sections_being_created == NULL)
2072 return FALSE;
5a4b0ccc 2073 sections_being_created_abfd = abfd;
bf67003b
NC
2074 }
2075 if (sections_being_created [shindex])
2076 {
4eca0228 2077 _bfd_error_handler
871b3ab2 2078 (_("%pB: warning: loop in section dependencies detected"), abfd);
bf67003b
NC
2079 return FALSE;
2080 }
2081 sections_being_created [shindex] = TRUE;
2082 }
2083
4fbb74a6
AM
2084 hdr = elf_elfsections (abfd)[shindex];
2085 ehdr = elf_elfheader (abfd);
2086 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1b3a8575 2087 hdr->sh_name);
933d961a 2088 if (name == NULL)
bf67003b 2089 goto fail;
252b5132 2090
4fbb74a6 2091 bed = get_elf_backend_data (abfd);
252b5132
RH
2092 switch (hdr->sh_type)
2093 {
2094 case SHT_NULL:
2095 /* Inactive section. Throw it away. */
bf67003b 2096 goto success;
252b5132 2097
bf67003b
NC
2098 case SHT_PROGBITS: /* Normal section with contents. */
2099 case SHT_NOBITS: /* .bss section. */
2100 case SHT_HASH: /* .hash section. */
2101 case SHT_NOTE: /* .note section. */
25e27870
L
2102 case SHT_INIT_ARRAY: /* .init_array section. */
2103 case SHT_FINI_ARRAY: /* .fini_array section. */
2104 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
7f1204bb 2105 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
fdc90cb4 2106 case SHT_GNU_HASH: /* .gnu.hash section. */
bf67003b
NC
2107 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2108 goto success;
252b5132 2109
797fc050 2110 case SHT_DYNAMIC: /* Dynamic linking information. */
6dc132d9 2111 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b
NC
2112 goto fail;
2113
cfcac11d
NC
2114 if (hdr->sh_link > elf_numsections (abfd))
2115 {
caa83f8b 2116 /* PR 10478: Accept Solaris binaries with a sh_link
cfcac11d
NC
2117 field set to SHN_BEFORE or SHN_AFTER. */
2118 switch (bfd_get_arch (abfd))
2119 {
caa83f8b 2120 case bfd_arch_i386:
cfcac11d
NC
2121 case bfd_arch_sparc:
2122 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
2123 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
2124 break;
2125 /* Otherwise fall through. */
2126 default:
bf67003b 2127 goto fail;
cfcac11d
NC
2128 }
2129 }
2130 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
bf67003b 2131 goto fail;
cfcac11d 2132 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
797fc050
AM
2133 {
2134 Elf_Internal_Shdr *dynsymhdr;
2135
2136 /* The shared libraries distributed with hpux11 have a bogus
2137 sh_link field for the ".dynamic" section. Find the
2138 string table for the ".dynsym" section instead. */
2139 if (elf_dynsymtab (abfd) != 0)
2140 {
2141 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2142 hdr->sh_link = dynsymhdr->sh_link;
2143 }
2144 else
2145 {
2146 unsigned int i, num_sec;
2147
2148 num_sec = elf_numsections (abfd);
2149 for (i = 1; i < num_sec; i++)
2150 {
2151 dynsymhdr = elf_elfsections (abfd)[i];
2152 if (dynsymhdr->sh_type == SHT_DYNSYM)
2153 {
2154 hdr->sh_link = dynsymhdr->sh_link;
2155 break;
2156 }
2157 }
2158 }
2159 }
bf67003b 2160 goto success;
797fc050 2161
bf67003b 2162 case SHT_SYMTAB: /* A symbol table. */
252b5132 2163 if (elf_onesymtab (abfd) == shindex)
bf67003b 2164 goto success;
252b5132 2165
a50b2160 2166 if (hdr->sh_entsize != bed->s->sizeof_sym)
bf67003b
NC
2167 goto fail;
2168
3337c1e5 2169 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
eee3b786
AM
2170 {
2171 if (hdr->sh_size != 0)
bf67003b 2172 goto fail;
eee3b786
AM
2173 /* Some assemblers erroneously set sh_info to one with a
2174 zero sh_size. ld sees this as a global symbol count
2175 of (unsigned) -1. Fix it here. */
2176 hdr->sh_info = 0;
bf67003b 2177 goto success;
eee3b786 2178 }
bf67003b 2179
16ad13ec
NC
2180 /* PR 18854: A binary might contain more than one symbol table.
2181 Unusual, but possible. Warn, but continue. */
2182 if (elf_onesymtab (abfd) != 0)
2183 {
4eca0228 2184 _bfd_error_handler
695344c0 2185 /* xgettext:c-format */
871b3ab2 2186 (_("%pB: warning: multiple symbol tables detected"
63a5468a 2187 " - ignoring the table in section %u"),
16ad13ec
NC
2188 abfd, shindex);
2189 goto success;
2190 }
252b5132 2191 elf_onesymtab (abfd) = shindex;
6a40cf0c
NC
2192 elf_symtab_hdr (abfd) = *hdr;
2193 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
252b5132
RH
2194 abfd->flags |= HAS_SYMS;
2195
2196 /* Sometimes a shared object will map in the symbol table. If
08a40648
AM
2197 SHF_ALLOC is set, and this is a shared object, then we also
2198 treat this section as a BFD section. We can not base the
2199 decision purely on SHF_ALLOC, because that flag is sometimes
2200 set in a relocatable object file, which would confuse the
2201 linker. */
252b5132
RH
2202 if ((hdr->sh_flags & SHF_ALLOC) != 0
2203 && (abfd->flags & DYNAMIC) != 0
6dc132d9
L
2204 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2205 shindex))
bf67003b 2206 goto fail;
252b5132 2207
1b3a8575
AM
2208 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2209 can't read symbols without that section loaded as well. It
2210 is most likely specified by the next section header. */
6a40cf0c
NC
2211 {
2212 elf_section_list * entry;
2213 unsigned int i, num_sec;
1b3a8575 2214
6a40cf0c
NC
2215 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2216 if (entry->hdr.sh_link == shindex)
2217 goto success;
2218
2219 num_sec = elf_numsections (abfd);
2220 for (i = shindex + 1; i < num_sec; i++)
2221 {
2222 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2223
2224 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2225 && hdr2->sh_link == shindex)
2226 break;
2227 }
2228
2229 if (i == num_sec)
2230 for (i = 1; i < shindex; i++)
1b3a8575
AM
2231 {
2232 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
6a40cf0c 2233
1b3a8575
AM
2234 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2235 && hdr2->sh_link == shindex)
2236 break;
2237 }
6a40cf0c
NC
2238
2239 if (i != shindex)
2240 ret = bfd_section_from_shdr (abfd, i);
2241 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2242 goto success;
2243 }
252b5132 2244
bf67003b 2245 case SHT_DYNSYM: /* A dynamic symbol table. */
252b5132 2246 if (elf_dynsymtab (abfd) == shindex)
bf67003b 2247 goto success;
252b5132 2248
a50b2160 2249 if (hdr->sh_entsize != bed->s->sizeof_sym)
bf67003b
NC
2250 goto fail;
2251
eee3b786
AM
2252 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2253 {
2254 if (hdr->sh_size != 0)
bf67003b
NC
2255 goto fail;
2256
eee3b786
AM
2257 /* Some linkers erroneously set sh_info to one with a
2258 zero sh_size. ld sees this as a global symbol count
2259 of (unsigned) -1. Fix it here. */
2260 hdr->sh_info = 0;
bf67003b 2261 goto success;
eee3b786 2262 }
bf67003b 2263
16ad13ec
NC
2264 /* PR 18854: A binary might contain more than one dynamic symbol table.
2265 Unusual, but possible. Warn, but continue. */
2266 if (elf_dynsymtab (abfd) != 0)
2267 {
4eca0228 2268 _bfd_error_handler
695344c0 2269 /* xgettext:c-format */
871b3ab2 2270 (_("%pB: warning: multiple dynamic symbol tables detected"
63a5468a 2271 " - ignoring the table in section %u"),
16ad13ec
NC
2272 abfd, shindex);
2273 goto success;
2274 }
252b5132
RH
2275 elf_dynsymtab (abfd) = shindex;
2276 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2277 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2278 abfd->flags |= HAS_SYMS;
2279
2280 /* Besides being a symbol table, we also treat this as a regular
2281 section, so that objcopy can handle it. */
bf67003b
NC
2282 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2283 goto success;
252b5132 2284
bf67003b 2285 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
6a40cf0c
NC
2286 {
2287 elf_section_list * entry;
9ad5cbcf 2288
6a40cf0c
NC
2289 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2290 if (entry->ndx == shindex)
2291 goto success;
07d6d2b8 2292
7a6e0d89 2293 entry = bfd_alloc (abfd, sizeof (*entry));
6a40cf0c
NC
2294 if (entry == NULL)
2295 goto fail;
2296 entry->ndx = shindex;
2297 entry->hdr = * hdr;
2298 entry->next = elf_symtab_shndx_list (abfd);
2299 elf_symtab_shndx_list (abfd) = entry;
2300 elf_elfsections (abfd)[shindex] = & entry->hdr;
2301 goto success;
2302 }
9ad5cbcf 2303
bf67003b 2304 case SHT_STRTAB: /* A string table. */
252b5132 2305 if (hdr->bfd_section != NULL)
bf67003b
NC
2306 goto success;
2307
252b5132
RH
2308 if (ehdr->e_shstrndx == shindex)
2309 {
2310 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2311 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
bf67003b 2312 goto success;
252b5132 2313 }
bf67003b 2314
1b3a8575
AM
2315 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2316 {
2317 symtab_strtab:
2318 elf_tdata (abfd)->strtab_hdr = *hdr;
2319 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
bf67003b 2320 goto success;
1b3a8575 2321 }
bf67003b 2322
1b3a8575
AM
2323 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2324 {
2325 dynsymtab_strtab:
2326 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2327 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2328 elf_elfsections (abfd)[shindex] = hdr;
2329 /* We also treat this as a regular section, so that objcopy
2330 can handle it. */
bf67003b
NC
2331 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2332 shindex);
2333 goto success;
1b3a8575 2334 }
252b5132 2335
1b3a8575
AM
2336 /* If the string table isn't one of the above, then treat it as a
2337 regular section. We need to scan all the headers to be sure,
2338 just in case this strtab section appeared before the above. */
2339 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2340 {
2341 unsigned int i, num_sec;
252b5132 2342
1b3a8575
AM
2343 num_sec = elf_numsections (abfd);
2344 for (i = 1; i < num_sec; i++)
2345 {
2346 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2347 if (hdr2->sh_link == shindex)
2348 {
933d961a
JJ
2349 /* Prevent endless recursion on broken objects. */
2350 if (i == shindex)
bf67003b 2351 goto fail;
1b3a8575 2352 if (! bfd_section_from_shdr (abfd, i))
bf67003b 2353 goto fail;
1b3a8575
AM
2354 if (elf_onesymtab (abfd) == i)
2355 goto symtab_strtab;
2356 if (elf_dynsymtab (abfd) == i)
2357 goto dynsymtab_strtab;
2358 }
2359 }
2360 }
bf67003b
NC
2361 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2362 goto success;
252b5132
RH
2363
2364 case SHT_REL:
2365 case SHT_RELA:
2366 /* *These* do a lot of work -- but build no sections! */
2367 {
2368 asection *target_sect;
d4730f92 2369 Elf_Internal_Shdr *hdr2, **p_hdr;
9ad5cbcf 2370 unsigned int num_sec = elf_numsections (abfd);
d4730f92 2371 struct bfd_elf_section_data *esdt;
252b5132 2372
aa2ca951
JJ
2373 if (hdr->sh_entsize
2374 != (bfd_size_type) (hdr->sh_type == SHT_REL
a50b2160 2375 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
bf67003b 2376 goto fail;
a50b2160 2377
03ae5f59 2378 /* Check for a bogus link to avoid crashing. */
4fbb74a6 2379 if (hdr->sh_link >= num_sec)
03ae5f59 2380 {
4eca0228 2381 _bfd_error_handler
695344c0 2382 /* xgettext:c-format */
871b3ab2 2383 (_("%pB: invalid link %u for reloc section %s (index %u)"),
4eca0228 2384 abfd, hdr->sh_link, name, shindex);
bf67003b
NC
2385 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2386 shindex);
2387 goto success;
03ae5f59
ILT
2388 }
2389
252b5132
RH
2390 /* For some incomprehensible reason Oracle distributes
2391 libraries for Solaris in which some of the objects have
2392 bogus sh_link fields. It would be nice if we could just
2393 reject them, but, unfortunately, some people need to use
2394 them. We scan through the section headers; if we find only
2395 one suitable symbol table, we clobber the sh_link to point
83b89087
L
2396 to it. I hope this doesn't break anything.
2397
2398 Don't do it on executable nor shared library. */
2399 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
2400 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
252b5132
RH
2401 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
2402 {
9ad5cbcf 2403 unsigned int scan;
252b5132
RH
2404 int found;
2405
2406 found = 0;
9ad5cbcf 2407 for (scan = 1; scan < num_sec; scan++)
252b5132
RH
2408 {
2409 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
2410 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
2411 {
2412 if (found != 0)
2413 {
2414 found = 0;
2415 break;
2416 }
2417 found = scan;
2418 }
2419 }
2420 if (found != 0)
2421 hdr->sh_link = found;
2422 }
2423
2424 /* Get the symbol table. */
1b3a8575
AM
2425 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2426 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
252b5132 2427 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
bf67003b 2428 goto fail;
252b5132 2429
a4bcd733
AM
2430 /* If this is an alloc section in an executable or shared
2431 library, or the reloc section does not use the main symbol
2432 table we don't treat it as a reloc section. BFD can't
2433 adequately represent such a section, so at least for now,
2434 we don't try. We just present it as a normal section. We
2435 also can't use it as a reloc section if it points to the
2436 null section, an invalid section, another reloc section, or
2437 its sh_link points to the null section. */
2438 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2439 && (hdr->sh_flags & SHF_ALLOC) != 0)
83b89087 2440 || hdr->sh_link == SHN_UNDEF
a4bcd733 2441 || hdr->sh_link != elf_onesymtab (abfd)
185ef66d 2442 || hdr->sh_info == SHN_UNDEF
185ef66d
AM
2443 || hdr->sh_info >= num_sec
2444 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2445 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
bf67003b
NC
2446 {
2447 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2448 shindex);
2449 goto success;
2450 }
252b5132
RH
2451
2452 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
bf67003b
NC
2453 goto fail;
2454
252b5132
RH
2455 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2456 if (target_sect == NULL)
bf67003b 2457 goto fail;
252b5132 2458
d4730f92
BS
2459 esdt = elf_section_data (target_sect);
2460 if (hdr->sh_type == SHT_RELA)
2461 p_hdr = &esdt->rela.hdr;
252b5132 2462 else
d4730f92
BS
2463 p_hdr = &esdt->rel.hdr;
2464
a7ba3896
NC
2465 /* PR 17512: file: 0b4f81b7.
2466 Also see PR 24456, for a file which deliberately has two reloc
2467 sections. */
06614111 2468 if (*p_hdr != NULL)
a7ba3896 2469 {
a859124d 2470 if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
a8e14f4c
NC
2471 {
2472 _bfd_error_handler
2473 /* xgettext:c-format */
a859124d
AM
2474 (_("%pB: warning: secondary relocation section '%s' "
2475 "for section %pA found - ignoring"),
a8e14f4c
NC
2476 abfd, name, target_sect);
2477 }
a7ba3896
NC
2478 goto success;
2479 }
a8e14f4c 2480
ef53be89 2481 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
d4730f92 2482 if (hdr2 == NULL)
bf67003b 2483 goto fail;
252b5132 2484 *hdr2 = *hdr;
d4730f92 2485 *p_hdr = hdr2;
252b5132 2486 elf_elfsections (abfd)[shindex] = hdr2;
056bafd4
MR
2487 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2488 * bed->s->int_rels_per_ext_rel);
252b5132
RH
2489 target_sect->flags |= SEC_RELOC;
2490 target_sect->relocation = NULL;
2491 target_sect->rel_filepos = hdr->sh_offset;
bf572ba0
MM
2492 /* In the section to which the relocations apply, mark whether
2493 its relocations are of the REL or RELA variety. */
72730e0c 2494 if (hdr->sh_size != 0)
d4730f92
BS
2495 {
2496 if (hdr->sh_type == SHT_RELA)
2497 target_sect->use_rela_p = 1;
2498 }
252b5132 2499 abfd->flags |= HAS_RELOC;
bf67003b 2500 goto success;
252b5132 2501 }
252b5132
RH
2502
2503 case SHT_GNU_verdef:
2504 elf_dynverdef (abfd) = shindex;
2505 elf_tdata (abfd)->dynverdef_hdr = *hdr;
bf67003b
NC
2506 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2507 goto success;
252b5132
RH
2508
2509 case SHT_GNU_versym:
a50b2160 2510 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
bf67003b
NC
2511 goto fail;
2512
252b5132
RH
2513 elf_dynversym (abfd) = shindex;
2514 elf_tdata (abfd)->dynversym_hdr = *hdr;
bf67003b
NC
2515 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2516 goto success;
252b5132
RH
2517
2518 case SHT_GNU_verneed:
2519 elf_dynverref (abfd) = shindex;
2520 elf_tdata (abfd)->dynverref_hdr = *hdr;
bf67003b
NC
2521 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2522 goto success;
252b5132
RH
2523
2524 case SHT_SHLIB:
bf67003b 2525 goto success;
252b5132 2526
dbb410c3 2527 case SHT_GROUP:
44534af3 2528 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
bf67003b
NC
2529 goto fail;
2530
6dc132d9 2531 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b
NC
2532 goto fail;
2533
bf67003b 2534 goto success;
dbb410c3 2535
252b5132 2536 default:
104d59d1
JM
2537 /* Possibly an attributes section. */
2538 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2539 || hdr->sh_type == bed->obj_attrs_section_type)
2540 {
2541 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b 2542 goto fail;
104d59d1 2543 _bfd_elf_parse_attributes (abfd, hdr);
bf67003b 2544 goto success;
104d59d1
JM
2545 }
2546
252b5132 2547 /* Check for any processor-specific section types. */
3eb70a79 2548 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
bf67003b 2549 goto success;
3eb70a79
L
2550
2551 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2552 {
2553 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2554 /* FIXME: How to properly handle allocated section reserved
2555 for applications? */
4eca0228 2556 _bfd_error_handler
695344c0 2557 /* xgettext:c-format */
871b3ab2 2558 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2559 abfd, hdr->sh_type, name);
3eb70a79 2560 else
bf67003b
NC
2561 {
2562 /* Allow sections reserved for applications. */
2563 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2564 shindex);
2565 goto success;
2566 }
3eb70a79
L
2567 }
2568 else if (hdr->sh_type >= SHT_LOPROC
2569 && hdr->sh_type <= SHT_HIPROC)
2570 /* FIXME: We should handle this section. */
4eca0228 2571 _bfd_error_handler
695344c0 2572 /* xgettext:c-format */
871b3ab2 2573 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2574 abfd, hdr->sh_type, name);
3eb70a79 2575 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
ff15b240
NC
2576 {
2577 /* Unrecognised OS-specific sections. */
2578 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2579 /* SHF_OS_NONCONFORMING indicates that special knowledge is
08a40648 2580 required to correctly process the section and the file should
ff15b240 2581 be rejected with an error message. */
4eca0228 2582 _bfd_error_handler
695344c0 2583 /* xgettext:c-format */
871b3ab2 2584 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2585 abfd, hdr->sh_type, name);
ff15b240 2586 else
bf67003b
NC
2587 {
2588 /* Otherwise it should be processed. */
2589 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2590 goto success;
2591 }
ff15b240 2592 }
3eb70a79
L
2593 else
2594 /* FIXME: We should handle this section. */
4eca0228 2595 _bfd_error_handler
695344c0 2596 /* xgettext:c-format */
871b3ab2 2597 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2598 abfd, hdr->sh_type, name);
3eb70a79 2599
bf67003b 2600 goto fail;
252b5132
RH
2601 }
2602
bf67003b
NC
2603 fail:
2604 ret = FALSE;
2605 success:
e5b470e2 2606 if (sections_being_created && sections_being_created_abfd == abfd)
bf67003b
NC
2607 sections_being_created [shindex] = FALSE;
2608 if (-- nesting == 0)
5a4b0ccc
NC
2609 {
2610 sections_being_created = NULL;
2611 sections_being_created_abfd = abfd;
2612 }
bf67003b 2613 return ret;
252b5132
RH
2614}
2615
87d72d41 2616/* Return the local symbol specified by ABFD, R_SYMNDX. */
ec338859 2617
87d72d41
AM
2618Elf_Internal_Sym *
2619bfd_sym_from_r_symndx (struct sym_cache *cache,
2620 bfd *abfd,
2621 unsigned long r_symndx)
ec338859 2622{
ec338859
AM
2623 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2624
a5d1b3b5
AM
2625 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2626 {
2627 Elf_Internal_Shdr *symtab_hdr;
2628 unsigned char esym[sizeof (Elf64_External_Sym)];
2629 Elf_External_Sym_Shndx eshndx;
ec338859 2630
a5d1b3b5
AM
2631 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2632 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
87d72d41 2633 &cache->sym[ent], esym, &eshndx) == NULL)
a5d1b3b5 2634 return NULL;
9ad5cbcf 2635
a5d1b3b5
AM
2636 if (cache->abfd != abfd)
2637 {
2638 memset (cache->indx, -1, sizeof (cache->indx));
2639 cache->abfd = abfd;
2640 }
2641 cache->indx[ent] = r_symndx;
ec338859 2642 }
a5d1b3b5 2643
87d72d41 2644 return &cache->sym[ent];
ec338859
AM
2645}
2646
252b5132
RH
2647/* Given an ELF section number, retrieve the corresponding BFD
2648 section. */
2649
2650asection *
91d6fa6a 2651bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
252b5132 2652{
91d6fa6a 2653 if (sec_index >= elf_numsections (abfd))
252b5132 2654 return NULL;
91d6fa6a 2655 return elf_elfsections (abfd)[sec_index]->bfd_section;
252b5132
RH
2656}
2657
b35d266b 2658static const struct bfd_elf_special_section special_sections_b[] =
2f89ff8d 2659{
0112cd26 2660 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 2661 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2662};
2663
b35d266b 2664static const struct bfd_elf_special_section special_sections_c[] =
7f4d3958 2665{
0112cd26 2666 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
1ff6de03 2667 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2668 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2669};
2670
b35d266b 2671static const struct bfd_elf_special_section special_sections_d[] =
7f4d3958 2672{
07d6d2b8
AM
2673 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2674 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
a9a72a65
DE
2675 /* There are more DWARF sections than these, but they needn't be added here
2676 unless you have to cope with broken compilers that don't emit section
2677 attributes or you want to help the user writing assembler. */
07d6d2b8
AM
2678 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2679 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2680 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2681 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
0112cd26 2682 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
07d6d2b8
AM
2683 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2684 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2685 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2686 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2687};
2688
b35d266b 2689static const struct bfd_elf_special_section special_sections_f[] =
7f4d3958 2690{
07d6d2b8 2691 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
6f9dbcd4 2692 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8 2693 { NULL, 0 , 0, 0, 0 }
7f4d3958
L
2694};
2695
b35d266b 2696static const struct bfd_elf_special_section special_sections_g[] =
7f4d3958 2697{
0112cd26 2698 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2699 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2700 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2701 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
0112cd26
NC
2702 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2703 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
07d6d2b8
AM
2704 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2705 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2706 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2707 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2708};
2709
b35d266b 2710static const struct bfd_elf_special_section special_sections_h[] =
7f4d3958 2711{
07d6d2b8
AM
2712 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2713 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2714};
2715
b35d266b 2716static const struct bfd_elf_special_section special_sections_i[] =
7f4d3958 2717{
07d6d2b8 2718 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
6f9dbcd4 2719 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2720 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2721 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2722};
2723
b35d266b 2724static const struct bfd_elf_special_section special_sections_l[] =
7f4d3958 2725{
0112cd26 2726 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2727 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2728};
2729
b35d266b 2730static const struct bfd_elf_special_section special_sections_n[] =
7f4d3958 2731{
0112cd26 2732 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
07d6d2b8
AM
2733 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2734 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2735};
2736
b35d266b 2737static const struct bfd_elf_special_section special_sections_p[] =
7f4d3958 2738{
6f9dbcd4 2739 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2740 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2741 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2742};
2743
b35d266b 2744static const struct bfd_elf_special_section special_sections_r[] =
7f4d3958 2745{
0112cd26
NC
2746 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2747 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
07d6d2b8
AM
2748 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2749 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2750 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2751};
2752
b35d266b 2753static const struct bfd_elf_special_section special_sections_s[] =
7f4d3958 2754{
0112cd26
NC
2755 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2756 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2757 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
60ff4dc4
HPN
2758 /* See struct bfd_elf_special_section declaration for the semantics of
2759 this special case where .prefix_length != strlen (.prefix). */
2760 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
07d6d2b8 2761 { NULL, 0, 0, 0, 0 }
2f89ff8d
L
2762};
2763
b35d266b 2764static const struct bfd_elf_special_section special_sections_t[] =
7f4d3958 2765{
07d6d2b8
AM
2766 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2767 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
0112cd26 2768 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
07d6d2b8 2769 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2770};
2771
1b315056
CS
2772static const struct bfd_elf_special_section special_sections_z[] =
2773{
07d6d2b8
AM
2774 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2775 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
1b315056
CS
2776 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2777 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2778 { NULL, 0, 0, 0, 0 }
1b315056
CS
2779};
2780
e4c93b56 2781static const struct bfd_elf_special_section * const special_sections[] =
7f4d3958 2782{
7f4d3958 2783 special_sections_b, /* 'b' */
98ece1b3 2784 special_sections_c, /* 'c' */
7f4d3958
L
2785 special_sections_d, /* 'd' */
2786 NULL, /* 'e' */
2787 special_sections_f, /* 'f' */
2788 special_sections_g, /* 'g' */
2789 special_sections_h, /* 'h' */
2790 special_sections_i, /* 'i' */
2791 NULL, /* 'j' */
2792 NULL, /* 'k' */
2793 special_sections_l, /* 'l' */
2794 NULL, /* 'm' */
2795 special_sections_n, /* 'n' */
2796 NULL, /* 'o' */
2797 special_sections_p, /* 'p' */
2798 NULL, /* 'q' */
2799 special_sections_r, /* 'r' */
2800 special_sections_s, /* 's' */
2801 special_sections_t, /* 't' */
1b315056
CS
2802 NULL, /* 'u' */
2803 NULL, /* 'v' */
2804 NULL, /* 'w' */
2805 NULL, /* 'x' */
2806 NULL, /* 'y' */
2807 special_sections_z /* 'z' */
7f4d3958
L
2808};
2809
551b43fd
AM
2810const struct bfd_elf_special_section *
2811_bfd_elf_get_special_section (const char *name,
2812 const struct bfd_elf_special_section *spec,
2813 unsigned int rela)
2f89ff8d
L
2814{
2815 int i;
7f4d3958 2816 int len;
7f4d3958 2817
551b43fd 2818 len = strlen (name);
7f4d3958 2819
551b43fd 2820 for (i = 0; spec[i].prefix != NULL; i++)
7dcb9820
AM
2821 {
2822 int suffix_len;
551b43fd 2823 int prefix_len = spec[i].prefix_length;
7dcb9820
AM
2824
2825 if (len < prefix_len)
2826 continue;
551b43fd 2827 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
7dcb9820
AM
2828 continue;
2829
551b43fd 2830 suffix_len = spec[i].suffix_length;
7dcb9820
AM
2831 if (suffix_len <= 0)
2832 {
2833 if (name[prefix_len] != 0)
2834 {
2835 if (suffix_len == 0)
2836 continue;
2837 if (name[prefix_len] != '.'
2838 && (suffix_len == -2
551b43fd 2839 || (rela && spec[i].type == SHT_REL)))
7dcb9820
AM
2840 continue;
2841 }
2842 }
2843 else
2844 {
2845 if (len < prefix_len + suffix_len)
2846 continue;
2847 if (memcmp (name + len - suffix_len,
551b43fd 2848 spec[i].prefix + prefix_len,
7dcb9820
AM
2849 suffix_len) != 0)
2850 continue;
2851 }
551b43fd 2852 return &spec[i];
7dcb9820 2853 }
2f89ff8d
L
2854
2855 return NULL;
2856}
2857
7dcb9820 2858const struct bfd_elf_special_section *
29ef7005 2859_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2f89ff8d 2860{
551b43fd
AM
2861 int i;
2862 const struct bfd_elf_special_section *spec;
29ef7005 2863 const struct elf_backend_data *bed;
2f89ff8d
L
2864
2865 /* See if this is one of the special sections. */
551b43fd
AM
2866 if (sec->name == NULL)
2867 return NULL;
2f89ff8d 2868
29ef7005
L
2869 bed = get_elf_backend_data (abfd);
2870 spec = bed->special_sections;
2871 if (spec)
2872 {
2873 spec = _bfd_elf_get_special_section (sec->name,
2874 bed->special_sections,
2875 sec->use_rela_p);
2876 if (spec != NULL)
2877 return spec;
2878 }
2879
551b43fd
AM
2880 if (sec->name[0] != '.')
2881 return NULL;
2f89ff8d 2882
551b43fd 2883 i = sec->name[1] - 'b';
1b315056 2884 if (i < 0 || i > 'z' - 'b')
551b43fd
AM
2885 return NULL;
2886
2887 spec = special_sections[i];
2f89ff8d 2888
551b43fd
AM
2889 if (spec == NULL)
2890 return NULL;
2891
2892 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2f89ff8d
L
2893}
2894
b34976b6 2895bfd_boolean
217aa764 2896_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
252b5132
RH
2897{
2898 struct bfd_elf_section_data *sdata;
551b43fd 2899 const struct elf_backend_data *bed;
7dcb9820 2900 const struct bfd_elf_special_section *ssect;
252b5132 2901
f0abc2a1
AM
2902 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2903 if (sdata == NULL)
2904 {
a50b1753 2905 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
07d6d2b8 2906 sizeof (*sdata));
f0abc2a1
AM
2907 if (sdata == NULL)
2908 return FALSE;
217aa764 2909 sec->used_by_bfd = sdata;
f0abc2a1 2910 }
bf572ba0 2911
551b43fd
AM
2912 /* Indicate whether or not this section should use RELA relocations. */
2913 bed = get_elf_backend_data (abfd);
2914 sec->use_rela_p = bed->default_use_rela_p;
2915
8c803a2d
AM
2916 /* Set up ELF section type and flags for newly created sections, if
2917 there is an ABI mandated section. */
2918 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2919 if (ssect != NULL)
2f89ff8d 2920 {
8c803a2d
AM
2921 elf_section_type (sec) = ssect->type;
2922 elf_section_flags (sec) = ssect->attr;
2f89ff8d
L
2923 }
2924
f592407e 2925 return _bfd_generic_new_section_hook (abfd, sec);
252b5132
RH
2926}
2927
2928/* Create a new bfd section from an ELF program header.
2929
2930 Since program segments have no names, we generate a synthetic name
2931 of the form segment<NUM>, where NUM is generally the index in the
2932 program header table. For segments that are split (see below) we
2933 generate the names segment<NUM>a and segment<NUM>b.
2934
2935 Note that some program segments may have a file size that is different than
2936 (less than) the memory size. All this means is that at execution the
2937 system must allocate the amount of memory specified by the memory size,
2938 but only initialize it with the first "file size" bytes read from the
2939 file. This would occur for example, with program segments consisting
2940 of combined data+bss.
2941
2942 To handle the above situation, this routine generates TWO bfd sections
2943 for the single program segment. The first has the length specified by
2944 the file size of the segment, and the second has the length specified
2945 by the difference between the two sizes. In effect, the segment is split
d5191d0c 2946 into its initialized and uninitialized parts.
252b5132
RH
2947
2948 */
2949
b34976b6 2950bfd_boolean
217aa764
AM
2951_bfd_elf_make_section_from_phdr (bfd *abfd,
2952 Elf_Internal_Phdr *hdr,
91d6fa6a 2953 int hdr_index,
a50b1753 2954 const char *type_name)
252b5132
RH
2955{
2956 asection *newsect;
2957 char *name;
2958 char namebuf[64];
d4c88bbb 2959 size_t len;
252b5132 2960 int split;
502794d4 2961 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132
RH
2962
2963 split = ((hdr->p_memsz > 0)
2964 && (hdr->p_filesz > 0)
2965 && (hdr->p_memsz > hdr->p_filesz));
d5191d0c
AM
2966
2967 if (hdr->p_filesz > 0)
252b5132 2968 {
91d6fa6a 2969 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
d5191d0c 2970 len = strlen (namebuf) + 1;
a50b1753 2971 name = (char *) bfd_alloc (abfd, len);
d5191d0c
AM
2972 if (!name)
2973 return FALSE;
2974 memcpy (name, namebuf, len);
2975 newsect = bfd_make_section (abfd, name);
2976 if (newsect == NULL)
2977 return FALSE;
502794d4
CE
2978 newsect->vma = hdr->p_vaddr / opb;
2979 newsect->lma = hdr->p_paddr / opb;
d5191d0c
AM
2980 newsect->size = hdr->p_filesz;
2981 newsect->filepos = hdr->p_offset;
2982 newsect->flags |= SEC_HAS_CONTENTS;
2983 newsect->alignment_power = bfd_log2 (hdr->p_align);
2984 if (hdr->p_type == PT_LOAD)
252b5132 2985 {
d5191d0c
AM
2986 newsect->flags |= SEC_ALLOC;
2987 newsect->flags |= SEC_LOAD;
2988 if (hdr->p_flags & PF_X)
2989 {
2990 /* FIXME: all we known is that it has execute PERMISSION,
2991 may be data. */
2992 newsect->flags |= SEC_CODE;
2993 }
2994 }
2995 if (!(hdr->p_flags & PF_W))
2996 {
2997 newsect->flags |= SEC_READONLY;
252b5132 2998 }
252b5132
RH
2999 }
3000
d5191d0c 3001 if (hdr->p_memsz > hdr->p_filesz)
252b5132 3002 {
d5191d0c
AM
3003 bfd_vma align;
3004
91d6fa6a 3005 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
d4c88bbb 3006 len = strlen (namebuf) + 1;
a50b1753 3007 name = (char *) bfd_alloc (abfd, len);
252b5132 3008 if (!name)
b34976b6 3009 return FALSE;
d4c88bbb 3010 memcpy (name, namebuf, len);
252b5132
RH
3011 newsect = bfd_make_section (abfd, name);
3012 if (newsect == NULL)
b34976b6 3013 return FALSE;
502794d4
CE
3014 newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
3015 newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
eea6121a 3016 newsect->size = hdr->p_memsz - hdr->p_filesz;
d5191d0c
AM
3017 newsect->filepos = hdr->p_offset + hdr->p_filesz;
3018 align = newsect->vma & -newsect->vma;
3019 if (align == 0 || align > hdr->p_align)
3020 align = hdr->p_align;
3021 newsect->alignment_power = bfd_log2 (align);
252b5132
RH
3022 if (hdr->p_type == PT_LOAD)
3023 {
d5191d0c
AM
3024 /* Hack for gdb. Segments that have not been modified do
3025 not have their contents written to a core file, on the
3026 assumption that a debugger can find the contents in the
3027 executable. We flag this case by setting the fake
3028 section size to zero. Note that "real" bss sections will
3029 always have their contents dumped to the core file. */
3030 if (bfd_get_format (abfd) == bfd_core)
3031 newsect->size = 0;
252b5132
RH
3032 newsect->flags |= SEC_ALLOC;
3033 if (hdr->p_flags & PF_X)
3034 newsect->flags |= SEC_CODE;
3035 }
3036 if (!(hdr->p_flags & PF_W))
3037 newsect->flags |= SEC_READONLY;
3038 }
3039
b34976b6 3040 return TRUE;
252b5132
RH
3041}
3042
864619bb
KS
3043static bfd_boolean
3044_bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3045{
3046 /* The return value is ignored. Build-ids are considered optional. */
3047 if (templ->xvec->flavour == bfd_target_elf_flavour)
3048 return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3049 (templ, offset);
3050 return FALSE;
3051}
3052
b34976b6 3053bfd_boolean
91d6fa6a 3054bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
20cfcaae 3055{
9c5bfbb7 3056 const struct elf_backend_data *bed;
20cfcaae
NC
3057
3058 switch (hdr->p_type)
3059 {
3060 case PT_NULL:
91d6fa6a 3061 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
20cfcaae
NC
3062
3063 case PT_LOAD:
864619bb
KS
3064 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3065 return FALSE;
3066 if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3067 _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3068 return TRUE;
20cfcaae
NC
3069
3070 case PT_DYNAMIC:
91d6fa6a 3071 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
20cfcaae
NC
3072
3073 case PT_INTERP:
91d6fa6a 3074 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
20cfcaae
NC
3075
3076 case PT_NOTE:
91d6fa6a 3077 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
b34976b6 3078 return FALSE;
276da9b3
L
3079 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3080 hdr->p_align))
b34976b6
AM
3081 return FALSE;
3082 return TRUE;
20cfcaae
NC
3083
3084 case PT_SHLIB:
91d6fa6a 3085 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
20cfcaae
NC
3086
3087 case PT_PHDR:
91d6fa6a 3088 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
20cfcaae 3089
811072d8 3090 case PT_GNU_EH_FRAME:
91d6fa6a 3091 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
811072d8
RM
3092 "eh_frame_hdr");
3093
2b05f1b7 3094 case PT_GNU_STACK:
91d6fa6a 3095 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
9ee5e499 3096
8c37241b 3097 case PT_GNU_RELRO:
91d6fa6a 3098 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
8c37241b 3099
20cfcaae 3100 default:
8c1acd09 3101 /* Check for any processor-specific program segment types. */
20cfcaae 3102 bed = get_elf_backend_data (abfd);
91d6fa6a 3103 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
20cfcaae
NC
3104 }
3105}
3106
d4730f92
BS
3107/* Return the REL_HDR for SEC, assuming there is only a single one, either
3108 REL or RELA. */
3109
3110Elf_Internal_Shdr *
3111_bfd_elf_single_rel_hdr (asection *sec)
3112{
3113 if (elf_section_data (sec)->rel.hdr)
3114 {
3115 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3116 return elf_section_data (sec)->rel.hdr;
3117 }
3118 else
3119 return elf_section_data (sec)->rela.hdr;
3120}
3121
3e19fb8f
L
3122static bfd_boolean
3123_bfd_elf_set_reloc_sh_name (bfd *abfd,
3124 Elf_Internal_Shdr *rel_hdr,
3125 const char *sec_name,
3126 bfd_boolean use_rela_p)
3127{
3128 char *name = (char *) bfd_alloc (abfd,
3129 sizeof ".rela" + strlen (sec_name));
3130 if (name == NULL)
3131 return FALSE;
3132
3133 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3134 rel_hdr->sh_name =
3135 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3136 FALSE);
3137 if (rel_hdr->sh_name == (unsigned int) -1)
3138 return FALSE;
3139
3140 return TRUE;
3141}
3142
d4730f92
BS
3143/* Allocate and initialize a section-header for a new reloc section,
3144 containing relocations against ASECT. It is stored in RELDATA. If
3145 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3146 relocations. */
23bc299b 3147
5d13b3b3 3148static bfd_boolean
217aa764 3149_bfd_elf_init_reloc_shdr (bfd *abfd,
d4730f92 3150 struct bfd_elf_section_reloc_data *reldata,
f6fe1ccd 3151 const char *sec_name,
3e19fb8f
L
3152 bfd_boolean use_rela_p,
3153 bfd_boolean delay_st_name_p)
23bc299b 3154{
d4730f92 3155 Elf_Internal_Shdr *rel_hdr;
9c5bfbb7 3156 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 3157
d4730f92 3158 BFD_ASSERT (reldata->hdr == NULL);
ef53be89 3159 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
d4730f92 3160 reldata->hdr = rel_hdr;
23bc299b 3161
3e19fb8f
L
3162 if (delay_st_name_p)
3163 rel_hdr->sh_name = (unsigned int) -1;
3164 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3165 use_rela_p))
b34976b6 3166 return FALSE;
23bc299b
MM
3167 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3168 rel_hdr->sh_entsize = (use_rela_p
3169 ? bed->s->sizeof_rela
3170 : bed->s->sizeof_rel);
72de5009 3171 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
28e07a05 3172 rel_hdr->sh_flags = 0;
23bc299b
MM
3173 rel_hdr->sh_addr = 0;
3174 rel_hdr->sh_size = 0;
3175 rel_hdr->sh_offset = 0;
3176
b34976b6 3177 return TRUE;
23bc299b
MM
3178}
3179
94be91de
JB
3180/* Return the default section type based on the passed in section flags. */
3181
3182int
3183bfd_elf_get_default_section_type (flagword flags)
3184{
0e41bebb 3185 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
2e76e85a 3186 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
94be91de
JB
3187 return SHT_NOBITS;
3188 return SHT_PROGBITS;
3189}
3190
d4730f92
BS
3191struct fake_section_arg
3192{
3193 struct bfd_link_info *link_info;
3194 bfd_boolean failed;
3195};
3196
252b5132
RH
3197/* Set up an ELF internal section header for a section. */
3198
252b5132 3199static void
d4730f92 3200elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
252b5132 3201{
d4730f92 3202 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
9c5bfbb7 3203 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 3204 struct bfd_elf_section_data *esd = elf_section_data (asect);
252b5132 3205 Elf_Internal_Shdr *this_hdr;
0414f35b 3206 unsigned int sh_type;
0ce398f1 3207 const char *name = asect->name;
3e19fb8f 3208 bfd_boolean delay_st_name_p = FALSE;
233bf4f8 3209 bfd_vma mask;
252b5132 3210
d4730f92 3211 if (arg->failed)
252b5132
RH
3212 {
3213 /* We already failed; just get out of the bfd_map_over_sections
08a40648 3214 loop. */
252b5132
RH
3215 return;
3216 }
3217
d4730f92 3218 this_hdr = &esd->this_hdr;
252b5132 3219
f6fe1ccd 3220 if (arg->link_info)
0ce398f1 3221 {
f6fe1ccd
L
3222 /* ld: compress DWARF debug sections with names: .debug_*. */
3223 if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
3224 && (asect->flags & SEC_DEBUGGING)
3225 && name[1] == 'd'
3226 && name[6] == '_')
3227 {
3228 /* Set SEC_ELF_COMPRESS to indicate this section should be
3229 compressed. */
3230 asect->flags |= SEC_ELF_COMPRESS;
dd905818 3231 /* If this section will be compressed, delay adding section
3e19fb8f
L
3232 name to section name section after it is compressed in
3233 _bfd_elf_assign_file_positions_for_non_load. */
3234 delay_st_name_p = TRUE;
f6fe1ccd
L
3235 }
3236 }
3237 else if ((asect->flags & SEC_ELF_RENAME))
3238 {
3239 /* objcopy: rename output DWARF debug section. */
3240 if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
3241 {
3242 /* When we decompress or compress with SHF_COMPRESSED,
3243 convert section name from .zdebug_* to .debug_* if
3244 needed. */
3245 if (name[1] == 'z')
3246 {
3247 char *new_name = convert_zdebug_to_debug (abfd, name);
3248 if (new_name == NULL)
3249 {
3250 arg->failed = TRUE;
3251 return;
3252 }
3253 name = new_name;
3254 }
3255 }
3256 else if (asect->compress_status == COMPRESS_SECTION_DONE)
0ce398f1 3257 {
f6fe1ccd
L
3258 /* PR binutils/18087: Compression does not always make a
3259 section smaller. So only rename the section when
3260 compression has actually taken place. If input section
3261 name is .zdebug_*, we should never compress it again. */
3262 char *new_name = convert_debug_to_zdebug (abfd, name);
0ce398f1
L
3263 if (new_name == NULL)
3264 {
3265 arg->failed = TRUE;
3266 return;
3267 }
f6fe1ccd
L
3268 BFD_ASSERT (name[1] != 'z');
3269 name = new_name;
0ce398f1
L
3270 }
3271 }
3272
3e19fb8f
L
3273 if (delay_st_name_p)
3274 this_hdr->sh_name = (unsigned int) -1;
3275 else
252b5132 3276 {
3e19fb8f
L
3277 this_hdr->sh_name
3278 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3279 name, FALSE);
3280 if (this_hdr->sh_name == (unsigned int) -1)
3281 {
3282 arg->failed = TRUE;
3283 return;
3284 }
252b5132
RH
3285 }
3286
a4d8e49b 3287 /* Don't clear sh_flags. Assembler may set additional bits. */
252b5132
RH
3288
3289 if ((asect->flags & SEC_ALLOC) != 0
3290 || asect->user_set_vma)
502794d4 3291 this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
252b5132
RH
3292 else
3293 this_hdr->sh_addr = 0;
3294
3295 this_hdr->sh_offset = 0;
eea6121a 3296 this_hdr->sh_size = asect->size;
252b5132 3297 this_hdr->sh_link = 0;
c86934ce
NC
3298 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3299 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3300 {
4eca0228 3301 _bfd_error_handler
695344c0 3302 /* xgettext:c-format */
9793eb77 3303 (_("%pB: error: alignment power %d of section `%pA' is too big"),
c08bb8dd 3304 abfd, asect->alignment_power, asect);
c86934ce
NC
3305 arg->failed = TRUE;
3306 return;
3307 }
233bf4f8
AM
3308 /* Set sh_addralign to the highest power of two given by alignment
3309 consistent with the section VMA. Linker scripts can force VMA. */
3310 mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3311 this_hdr->sh_addralign = mask & -mask;
252b5132
RH
3312 /* The sh_entsize and sh_info fields may have been set already by
3313 copy_private_section_data. */
3314
3315 this_hdr->bfd_section = asect;
3316 this_hdr->contents = NULL;
3317
3cddba1e
L
3318 /* If the section type is unspecified, we set it based on
3319 asect->flags. */
98ece1b3
AM
3320 if ((asect->flags & SEC_GROUP) != 0)
3321 sh_type = SHT_GROUP;
98ece1b3 3322 else
94be91de 3323 sh_type = bfd_elf_get_default_section_type (asect->flags);
98ece1b3 3324
3cddba1e 3325 if (this_hdr->sh_type == SHT_NULL)
98ece1b3
AM
3326 this_hdr->sh_type = sh_type;
3327 else if (this_hdr->sh_type == SHT_NOBITS
3328 && sh_type == SHT_PROGBITS
3329 && (asect->flags & SEC_ALLOC) != 0)
3cddba1e 3330 {
98ece1b3
AM
3331 /* Warn if we are changing a NOBITS section to PROGBITS, but
3332 allow the link to proceed. This can happen when users link
3333 non-bss input sections to bss output sections, or emit data
3334 to a bss output section via a linker script. */
4eca0228 3335 _bfd_error_handler
871b3ab2 3336 (_("warning: section `%pA' type changed to PROGBITS"), asect);
98ece1b3 3337 this_hdr->sh_type = sh_type;
3cddba1e
L
3338 }
3339
2f89ff8d 3340 switch (this_hdr->sh_type)
252b5132 3341 {
2f89ff8d 3342 default:
2f89ff8d
L
3343 break;
3344
3345 case SHT_STRTAB:
2f89ff8d
L
3346 case SHT_NOTE:
3347 case SHT_NOBITS:
3348 case SHT_PROGBITS:
3349 break;
606851fb
AM
3350
3351 case SHT_INIT_ARRAY:
3352 case SHT_FINI_ARRAY:
3353 case SHT_PREINIT_ARRAY:
3354 this_hdr->sh_entsize = bed->s->arch_size / 8;
3355 break;
2f89ff8d
L
3356
3357 case SHT_HASH:
c7ac6ff8 3358 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2f89ff8d 3359 break;
5de3bf90 3360
2f89ff8d 3361 case SHT_DYNSYM:
252b5132 3362 this_hdr->sh_entsize = bed->s->sizeof_sym;
2f89ff8d
L
3363 break;
3364
3365 case SHT_DYNAMIC:
252b5132 3366 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2f89ff8d
L
3367 break;
3368
3369 case SHT_RELA:
3370 if (get_elf_backend_data (abfd)->may_use_rela_p)
3371 this_hdr->sh_entsize = bed->s->sizeof_rela;
3372 break;
3373
3374 case SHT_REL:
3375 if (get_elf_backend_data (abfd)->may_use_rel_p)
3376 this_hdr->sh_entsize = bed->s->sizeof_rel;
3377 break;
3378
3379 case SHT_GNU_versym:
252b5132 3380 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2f89ff8d
L
3381 break;
3382
3383 case SHT_GNU_verdef:
252b5132
RH
3384 this_hdr->sh_entsize = 0;
3385 /* objcopy or strip will copy over sh_info, but may not set
08a40648
AM
3386 cverdefs. The linker will set cverdefs, but sh_info will be
3387 zero. */
252b5132
RH
3388 if (this_hdr->sh_info == 0)
3389 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3390 else
3391 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3392 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2f89ff8d
L
3393 break;
3394
3395 case SHT_GNU_verneed:
252b5132
RH
3396 this_hdr->sh_entsize = 0;
3397 /* objcopy or strip will copy over sh_info, but may not set
08a40648
AM
3398 cverrefs. The linker will set cverrefs, but sh_info will be
3399 zero. */
252b5132
RH
3400 if (this_hdr->sh_info == 0)
3401 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3402 else
3403 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3404 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2f89ff8d
L
3405 break;
3406
3407 case SHT_GROUP:
1783205a 3408 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2f89ff8d 3409 break;
fdc90cb4
JJ
3410
3411 case SHT_GNU_HASH:
3412 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3413 break;
dbb410c3 3414 }
252b5132
RH
3415
3416 if ((asect->flags & SEC_ALLOC) != 0)
3417 this_hdr->sh_flags |= SHF_ALLOC;
3418 if ((asect->flags & SEC_READONLY) == 0)
3419 this_hdr->sh_flags |= SHF_WRITE;
3420 if ((asect->flags & SEC_CODE) != 0)
3421 this_hdr->sh_flags |= SHF_EXECINSTR;
f5fa8ca2
JJ
3422 if ((asect->flags & SEC_MERGE) != 0)
3423 {
3424 this_hdr->sh_flags |= SHF_MERGE;
3425 this_hdr->sh_entsize = asect->entsize;
f5fa8ca2 3426 }
84865015
NC
3427 if ((asect->flags & SEC_STRINGS) != 0)
3428 this_hdr->sh_flags |= SHF_STRINGS;
1126897b 3429 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
dbb410c3 3430 this_hdr->sh_flags |= SHF_GROUP;
13ae64f3 3431 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
704afa60
JJ
3432 {
3433 this_hdr->sh_flags |= SHF_TLS;
3a800eb9
AM
3434 if (asect->size == 0
3435 && (asect->flags & SEC_HAS_CONTENTS) == 0)
704afa60 3436 {
3a800eb9 3437 struct bfd_link_order *o = asect->map_tail.link_order;
b34976b6 3438
704afa60 3439 this_hdr->sh_size = 0;
3a800eb9
AM
3440 if (o != NULL)
3441 {
704afa60 3442 this_hdr->sh_size = o->offset + o->size;
3a800eb9
AM
3443 if (this_hdr->sh_size != 0)
3444 this_hdr->sh_type = SHT_NOBITS;
3445 }
704afa60
JJ
3446 }
3447 }
18ae9cc1
L
3448 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3449 this_hdr->sh_flags |= SHF_EXCLUDE;
252b5132 3450
d4730f92
BS
3451 /* If the section has relocs, set up a section header for the
3452 SHT_REL[A] section. If two relocation sections are required for
3453 this section, it is up to the processor-specific back-end to
3454 create the other. */
3455 if ((asect->flags & SEC_RELOC) != 0)
3456 {
3457 /* When doing a relocatable link, create both REL and RELA sections if
3458 needed. */
3459 if (arg->link_info
3460 /* Do the normal setup if we wouldn't create any sections here. */
3461 && esd->rel.count + esd->rela.count > 0
0e1862bb
L
3462 && (bfd_link_relocatable (arg->link_info)
3463 || arg->link_info->emitrelocations))
d4730f92
BS
3464 {
3465 if (esd->rel.count && esd->rel.hdr == NULL
28e07a05 3466 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
db4677b8 3467 FALSE, delay_st_name_p))
d4730f92
BS
3468 {
3469 arg->failed = TRUE;
3470 return;
3471 }
3472 if (esd->rela.count && esd->rela.hdr == NULL
28e07a05 3473 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
db4677b8 3474 TRUE, delay_st_name_p))
d4730f92
BS
3475 {
3476 arg->failed = TRUE;
3477 return;
3478 }
3479 }
3480 else if (!_bfd_elf_init_reloc_shdr (abfd,
3481 (asect->use_rela_p
3482 ? &esd->rela : &esd->rel),
f6fe1ccd 3483 name,
3e19fb8f
L
3484 asect->use_rela_p,
3485 delay_st_name_p))
db4677b8 3486 {
d4730f92 3487 arg->failed = TRUE;
db4677b8
AM
3488 return;
3489 }
d4730f92
BS
3490 }
3491
252b5132 3492 /* Check for processor-specific section types. */
0414f35b 3493 sh_type = this_hdr->sh_type;
e1fddb6b
AO
3494 if (bed->elf_backend_fake_sections
3495 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
db4677b8
AM
3496 {
3497 arg->failed = TRUE;
3498 return;
3499 }
252b5132 3500
42bb2e33 3501 if (sh_type == SHT_NOBITS && asect->size != 0)
0414f35b
AM
3502 {
3503 /* Don't change the header type from NOBITS if we are being
42bb2e33 3504 called for objcopy --only-keep-debug. */
0414f35b
AM
3505 this_hdr->sh_type = sh_type;
3506 }
252b5132
RH
3507}
3508
bcacc0f5
AM
3509/* Fill in the contents of a SHT_GROUP section. Called from
3510 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3511 when ELF targets use the generic linker, ld. Called for ld -r
3512 from bfd_elf_final_link. */
dbb410c3 3513
1126897b 3514void
217aa764 3515bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
dbb410c3 3516{
a50b1753 3517 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
9dce4196 3518 asection *elt, *first;
dbb410c3 3519 unsigned char *loc;
b34976b6 3520 bfd_boolean gas;
dbb410c3 3521
7e4111ad
L
3522 /* Ignore linker created group section. See elfNN_ia64_object_p in
3523 elfxx-ia64.c. */
ce5aecf8
AM
3524 if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3525 || sec->size == 0
dbb410c3
AM
3526 || *failedptr)
3527 return;
3528
bcacc0f5
AM
3529 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3530 {
3531 unsigned long symindx = 0;
3532
3533 /* elf_group_id will have been set up by objcopy and the
3534 generic linker. */
3535 if (elf_group_id (sec) != NULL)
3536 symindx = elf_group_id (sec)->udata.i;
1126897b 3537
bcacc0f5
AM
3538 if (symindx == 0)
3539 {
3540 /* If called from the assembler, swap_out_syms will have set up
6a541707
NC
3541 elf_section_syms.
3542 PR 25699: A corrupt input file could contain bogus group info. */
3543 if (elf_section_syms (abfd) == NULL)
3544 {
3545 *failedptr = TRUE;
3546 return;
3547 }
bcacc0f5
AM
3548 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3549 }
3550 elf_section_data (sec)->this_hdr.sh_info = symindx;
3551 }
3552 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
1126897b 3553 {
bcacc0f5
AM
3554 /* The ELF backend linker sets sh_info to -2 when the group
3555 signature symbol is global, and thus the index can't be
3556 set until all local symbols are output. */
53720c49
AM
3557 asection *igroup;
3558 struct bfd_elf_section_data *sec_data;
3559 unsigned long symndx;
3560 unsigned long extsymoff;
bcacc0f5
AM
3561 struct elf_link_hash_entry *h;
3562
53720c49
AM
3563 /* The point of this little dance to the first SHF_GROUP section
3564 then back to the SHT_GROUP section is that this gets us to
3565 the SHT_GROUP in the input object. */
3566 igroup = elf_sec_group (elf_next_in_group (sec));
3567 sec_data = elf_section_data (igroup);
3568 symndx = sec_data->this_hdr.sh_info;
3569 extsymoff = 0;
bcacc0f5
AM
3570 if (!elf_bad_symtab (igroup->owner))
3571 {
3572 Elf_Internal_Shdr *symtab_hdr;
3573
3574 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3575 extsymoff = symtab_hdr->sh_info;
3576 }
3577 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3578 while (h->root.type == bfd_link_hash_indirect
3579 || h->root.type == bfd_link_hash_warning)
3580 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3581
3582 elf_section_data (sec)->this_hdr.sh_info = h->indx;
1126897b 3583 }
dbb410c3 3584
1126897b 3585 /* The contents won't be allocated for "ld -r" or objcopy. */
b34976b6 3586 gas = TRUE;
dbb410c3
AM
3587 if (sec->contents == NULL)
3588 {
b34976b6 3589 gas = FALSE;
a50b1753 3590 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
9dce4196
AM
3591
3592 /* Arrange for the section to be written out. */
3593 elf_section_data (sec)->this_hdr.contents = sec->contents;
dbb410c3
AM
3594 if (sec->contents == NULL)
3595 {
b34976b6 3596 *failedptr = TRUE;
dbb410c3
AM
3597 return;
3598 }
3599 }
3600
eea6121a 3601 loc = sec->contents + sec->size;
dbb410c3 3602
9dce4196
AM
3603 /* Get the pointer to the first section in the group that gas
3604 squirreled away here. objcopy arranges for this to be set to the
3605 start of the input section group. */
3606 first = elt = elf_next_in_group (sec);
dbb410c3
AM
3607
3608 /* First element is a flag word. Rest of section is elf section
3609 indices for all the sections of the group. Write them backwards
3610 just to keep the group in the same order as given in .section
3611 directives, not that it matters. */
3612 while (elt != NULL)
3613 {
9dce4196 3614 asection *s;
9dce4196 3615
9dce4196 3616 s = elt;
415f38a6
AM
3617 if (!gas)
3618 s = s->output_section;
3619 if (s != NULL
3620 && !bfd_is_abs_section (s))
01e1a5bc 3621 {
db4677b8 3622 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
28e07a05
AM
3623 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3624
3625 if (elf_sec->rel.hdr != NULL
3626 && (gas
3627 || (input_elf_sec->rel.hdr != NULL
3628 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
db4677b8 3629 {
28e07a05 3630 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
db4677b8
AM
3631 loc -= 4;
3632 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3633 }
28e07a05
AM
3634 if (elf_sec->rela.hdr != NULL
3635 && (gas
3636 || (input_elf_sec->rela.hdr != NULL
3637 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
db4677b8 3638 {
28e07a05 3639 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
db4677b8
AM
3640 loc -= 4;
3641 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3642 }
01e1a5bc 3643 loc -= 4;
db4677b8 3644 H_PUT_32 (abfd, elf_sec->this_idx, loc);
01e1a5bc 3645 }
945906ff 3646 elt = elf_next_in_group (elt);
9dce4196
AM
3647 if (elt == first)
3648 break;
dbb410c3
AM
3649 }
3650
7bdf4127
AB
3651 loc -= 4;
3652 BFD_ASSERT (loc == sec->contents);
dbb410c3 3653
9dce4196 3654 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
dbb410c3
AM
3655}
3656
bce964aa
AM
3657/* Given NAME, the name of a relocation section stripped of its
3658 .rel/.rela prefix, return the section in ABFD to which the
3659 relocations apply. */
bd53a53a
L
3660
3661asection *
bce964aa
AM
3662_bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3663{
3664 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3665 section likely apply to .got.plt or .got section. */
3666 if (get_elf_backend_data (abfd)->want_got_plt
3667 && strcmp (name, ".plt") == 0)
3668 {
3669 asection *sec;
3670
3671 name = ".got.plt";
3672 sec = bfd_get_section_by_name (abfd, name);
3673 if (sec != NULL)
3674 return sec;
3675 name = ".got";
3676 }
3677
3678 return bfd_get_section_by_name (abfd, name);
3679}
3680
3681/* Return the section to which RELOC_SEC applies. */
3682
3683static asection *
3684elf_get_reloc_section (asection *reloc_sec)
bd53a53a
L
3685{
3686 const char *name;
3687 unsigned int type;
3688 bfd *abfd;
bce964aa 3689 const struct elf_backend_data *bed;
bd53a53a
L
3690
3691 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3692 if (type != SHT_REL && type != SHT_RELA)
3693 return NULL;
3694
3695 /* We look up the section the relocs apply to by name. */
3696 name = reloc_sec->name;
bce964aa
AM
3697 if (strncmp (name, ".rel", 4) != 0)
3698 return NULL;
3699 name += 4;
3700 if (type == SHT_RELA && *name++ != 'a')
3701 return NULL;
bd53a53a 3702
bd53a53a 3703 abfd = reloc_sec->owner;
bce964aa
AM
3704 bed = get_elf_backend_data (abfd);
3705 return bed->get_reloc_section (abfd, name);
bd53a53a
L
3706}
3707
252b5132
RH
3708/* Assign all ELF section numbers. The dummy first section is handled here
3709 too. The link/info pointers for the standard section types are filled
3710 in here too, while we're at it. */
3711
b34976b6 3712static bfd_boolean
da9f89d4 3713assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
252b5132
RH
3714{
3715 struct elf_obj_tdata *t = elf_tdata (abfd);
3716 asection *sec;
3e19fb8f 3717 unsigned int section_number;
252b5132 3718 Elf_Internal_Shdr **i_shdrp;
47cc2cf5 3719 struct bfd_elf_section_data *d;
3516e984 3720 bfd_boolean need_symtab;
446f7ed5 3721 size_t amt;
252b5132
RH
3722
3723 section_number = 1;
3724
2b0f7ef9
JJ
3725 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3726
da9f89d4 3727 /* SHT_GROUP sections are in relocatable files only. */
7bdf4127 3728 if (link_info == NULL || !link_info->resolve_section_groups)
252b5132 3729 {
ef53be89 3730 size_t reloc_count = 0;
14f2c699 3731
da9f89d4 3732 /* Put SHT_GROUP sections first. */
04dd1667 3733 for (sec = abfd->sections; sec != NULL; sec = sec->next)
47cc2cf5 3734 {
5daa8fe7 3735 d = elf_section_data (sec);
da9f89d4
L
3736
3737 if (d->this_hdr.sh_type == SHT_GROUP)
08a40648 3738 {
5daa8fe7 3739 if (sec->flags & SEC_LINKER_CREATED)
da9f89d4
L
3740 {
3741 /* Remove the linker created SHT_GROUP sections. */
5daa8fe7 3742 bfd_section_list_remove (abfd, sec);
da9f89d4 3743 abfd->section_count--;
da9f89d4 3744 }
08a40648 3745 else
4fbb74a6 3746 d->this_idx = section_number++;
da9f89d4 3747 }
14f2c699
L
3748
3749 /* Count relocations. */
3750 reloc_count += sec->reloc_count;
47cc2cf5 3751 }
14f2c699
L
3752
3753 /* Clear HAS_RELOC if there are no relocations. */
3754 if (reloc_count == 0)
3755 abfd->flags &= ~HAS_RELOC;
47cc2cf5
PB
3756 }
3757
3758 for (sec = abfd->sections; sec; sec = sec->next)
3759 {
3760 d = elf_section_data (sec);
3761
3762 if (d->this_hdr.sh_type != SHT_GROUP)
4fbb74a6 3763 d->this_idx = section_number++;
3e19fb8f
L
3764 if (d->this_hdr.sh_name != (unsigned int) -1)
3765 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
d4730f92 3766 if (d->rel.hdr)
2b0f7ef9 3767 {
d4730f92 3768 d->rel.idx = section_number++;
3e19fb8f
L
3769 if (d->rel.hdr->sh_name != (unsigned int) -1)
3770 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
2b0f7ef9 3771 }
d4730f92
BS
3772 else
3773 d->rel.idx = 0;
23bc299b 3774
d4730f92 3775 if (d->rela.hdr)
2b0f7ef9 3776 {
d4730f92 3777 d->rela.idx = section_number++;
3e19fb8f
L
3778 if (d->rela.hdr->sh_name != (unsigned int) -1)
3779 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
2b0f7ef9 3780 }
23bc299b 3781 else
d4730f92 3782 d->rela.idx = 0;
252b5132
RH
3783 }
3784
3516e984
L
3785 need_symtab = (bfd_get_symcount (abfd) > 0
3786 || (link_info == NULL
3787 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3788 == HAS_RELOC)));
3789 if (need_symtab)
252b5132 3790 {
12bd6957 3791 elf_onesymtab (abfd) = section_number++;
2b0f7ef9 3792 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4fbb74a6 3793 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
9ad5cbcf 3794 {
7a6e0d89 3795 elf_section_list *entry;
6a40cf0c
NC
3796
3797 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
3798
7a6e0d89 3799 entry = bfd_zalloc (abfd, sizeof (*entry));
6a40cf0c
NC
3800 entry->ndx = section_number++;
3801 elf_symtab_shndx_list (abfd) = entry;
3802 entry->hdr.sh_name
9ad5cbcf 3803 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
b34976b6 3804 ".symtab_shndx", FALSE);
6a40cf0c 3805 if (entry->hdr.sh_name == (unsigned int) -1)
b34976b6 3806 return FALSE;
9ad5cbcf 3807 }
12bd6957 3808 elf_strtab_sec (abfd) = section_number++;
2b0f7ef9 3809 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
252b5132
RH
3810 }
3811
dd905818
NC
3812 elf_shstrtab_sec (abfd) = section_number++;
3813 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3814 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3815
1c52a645
L
3816 if (section_number >= SHN_LORESERVE)
3817 {
695344c0 3818 /* xgettext:c-format */
871b3ab2 3819 _bfd_error_handler (_("%pB: too many sections: %u"),
1c52a645
L
3820 abfd, section_number);
3821 return FALSE;
3822 }
3823
9ad5cbcf 3824 elf_numsections (abfd) = section_number;
252b5132
RH
3825 elf_elfheader (abfd)->e_shnum = section_number;
3826
3827 /* Set up the list of section header pointers, in agreement with the
3828 indices. */
446f7ed5
AM
3829 amt = section_number * sizeof (Elf_Internal_Shdr *);
3830 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
252b5132 3831 if (i_shdrp == NULL)
b34976b6 3832 return FALSE;
252b5132 3833
a50b1753 3834 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
07d6d2b8 3835 sizeof (Elf_Internal_Shdr));
252b5132
RH
3836 if (i_shdrp[0] == NULL)
3837 {
3838 bfd_release (abfd, i_shdrp);
b34976b6 3839 return FALSE;
252b5132 3840 }
252b5132
RH
3841
3842 elf_elfsections (abfd) = i_shdrp;
3843
12bd6957 3844 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3516e984 3845 if (need_symtab)
252b5132 3846 {
12bd6957 3847 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4fbb74a6 3848 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
9ad5cbcf 3849 {
6a40cf0c
NC
3850 elf_section_list * entry = elf_symtab_shndx_list (abfd);
3851 BFD_ASSERT (entry != NULL);
3852 i_shdrp[entry->ndx] = & entry->hdr;
3853 entry->hdr.sh_link = elf_onesymtab (abfd);
9ad5cbcf 3854 }
12bd6957
AM
3855 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3856 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
252b5132 3857 }
38ce5b11 3858
252b5132
RH
3859 for (sec = abfd->sections; sec; sec = sec->next)
3860 {
252b5132 3861 asection *s;
252b5132 3862
91d6fa6a
NC
3863 d = elf_section_data (sec);
3864
252b5132 3865 i_shdrp[d->this_idx] = &d->this_hdr;
d4730f92
BS
3866 if (d->rel.idx != 0)
3867 i_shdrp[d->rel.idx] = d->rel.hdr;
3868 if (d->rela.idx != 0)
3869 i_shdrp[d->rela.idx] = d->rela.hdr;
252b5132
RH
3870
3871 /* Fill in the sh_link and sh_info fields while we're at it. */
3872
3873 /* sh_link of a reloc section is the section index of the symbol
3874 table. sh_info is the section index of the section to which
3875 the relocation entries apply. */
d4730f92 3876 if (d->rel.idx != 0)
252b5132 3877 {
12bd6957 3878 d->rel.hdr->sh_link = elf_onesymtab (abfd);
d4730f92 3879 d->rel.hdr->sh_info = d->this_idx;
9ef5d938 3880 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
252b5132 3881 }
d4730f92 3882 if (d->rela.idx != 0)
23bc299b 3883 {
12bd6957 3884 d->rela.hdr->sh_link = elf_onesymtab (abfd);
d4730f92 3885 d->rela.hdr->sh_info = d->this_idx;
9ef5d938 3886 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
23bc299b 3887 }
252b5132 3888
38ce5b11
L
3889 /* We need to set up sh_link for SHF_LINK_ORDER. */
3890 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3891 {
3892 s = elf_linked_to_section (sec);
3893 if (s)
38ce5b11 3894 {
f2876037 3895 /* elf_linked_to_section points to the input section. */
ccd2ec6a 3896 if (link_info != NULL)
38ce5b11 3897 {
f2876037 3898 /* Check discarded linkonce section. */
dbaa2011 3899 if (discarded_section (s))
38ce5b11 3900 {
ccd2ec6a 3901 asection *kept;
4eca0228 3902 _bfd_error_handler
695344c0 3903 /* xgettext:c-format */
871b3ab2
AM
3904 (_("%pB: sh_link of section `%pA' points to"
3905 " discarded section `%pA' of `%pB'"),
ccd2ec6a
L
3906 abfd, d->this_hdr.bfd_section,
3907 s, s->owner);
3908 /* Point to the kept section if it has the same
3909 size as the discarded one. */
c0f00686 3910 kept = _bfd_elf_check_kept_section (s, link_info);
ccd2ec6a 3911 if (kept == NULL)
185d09ad 3912 {
ccd2ec6a
L
3913 bfd_set_error (bfd_error_bad_value);
3914 return FALSE;
185d09ad 3915 }
ccd2ec6a 3916 s = kept;
38ce5b11 3917 }
e424ecc8 3918
ccd2ec6a
L
3919 s = s->output_section;
3920 BFD_ASSERT (s != NULL);
38ce5b11 3921 }
f2876037
L
3922 else
3923 {
3924 /* Handle objcopy. */
3925 if (s->output_section == NULL)
3926 {
4eca0228 3927 _bfd_error_handler
695344c0 3928 /* xgettext:c-format */
871b3ab2
AM
3929 (_("%pB: sh_link of section `%pA' points to"
3930 " removed section `%pA' of `%pB'"),
f2876037
L
3931 abfd, d->this_hdr.bfd_section, s, s->owner);
3932 bfd_set_error (bfd_error_bad_value);
3933 return FALSE;
3934 }
3935 s = s->output_section;
3936 }
ccd2ec6a
L
3937 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3938 }
3939 else
3940 {
3941 /* PR 290:
3942 The Intel C compiler generates SHT_IA_64_UNWIND with
3943 SHF_LINK_ORDER. But it doesn't set the sh_link or
3944 sh_info fields. Hence we could get the situation
08a40648 3945 where s is NULL. */
ccd2ec6a
L
3946 const struct elf_backend_data *bed
3947 = get_elf_backend_data (abfd);
a859124d
AM
3948 bed->link_order_error_handler
3949 /* xgettext:c-format */
3950 (_("%pB: warning: sh_link not set for section `%pA'"),
3951 abfd, sec);
38ce5b11
L
3952 }
3953 }
3954
252b5132
RH
3955 switch (d->this_hdr.sh_type)
3956 {
3957 case SHT_REL:
3958 case SHT_RELA:
3959 /* A reloc section which we are treating as a normal BFD
3960 section. sh_link is the section index of the symbol
3961 table. sh_info is the section index of the section to
3962 which the relocation entries apply. We assume that an
3963 allocated reloc section uses the dynamic symbol table.
3964 FIXME: How can we be sure? */
3965 s = bfd_get_section_by_name (abfd, ".dynsym");
3966 if (s != NULL)
3967 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3968
bce964aa 3969 s = elf_get_reloc_section (sec);
252b5132 3970 if (s != NULL)
9ef5d938
L
3971 {
3972 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3973 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3974 }
252b5132
RH
3975 break;
3976
3977 case SHT_STRTAB:
3978 /* We assume that a section named .stab*str is a stabs
3979 string section. We look for a section with the same name
3980 but without the trailing ``str'', and set its sh_link
3981 field to point to this section. */
0112cd26 3982 if (CONST_STRNEQ (sec->name, ".stab")
252b5132
RH
3983 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3984 {
3985 size_t len;
3986 char *alc;
3987
3988 len = strlen (sec->name);
a50b1753 3989 alc = (char *) bfd_malloc (len - 2);
252b5132 3990 if (alc == NULL)
b34976b6 3991 return FALSE;
d4c88bbb 3992 memcpy (alc, sec->name, len - 3);
252b5132
RH
3993 alc[len - 3] = '\0';
3994 s = bfd_get_section_by_name (abfd, alc);
3995 free (alc);
3996 if (s != NULL)
3997 {
3998 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3999
4000 /* This is a .stab section. */
34ca5531 4001 elf_section_data (s)->this_hdr.sh_entsize = 12;
252b5132
RH
4002 }
4003 }
4004 break;
4005
4006 case SHT_DYNAMIC:
4007 case SHT_DYNSYM:
4008 case SHT_GNU_verneed:
4009 case SHT_GNU_verdef:
4010 /* sh_link is the section header index of the string table
4011 used for the dynamic entries, or the symbol table, or the
4012 version strings. */
4013 s = bfd_get_section_by_name (abfd, ".dynstr");
4014 if (s != NULL)
4015 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4016 break;
4017
7f1204bb
JJ
4018 case SHT_GNU_LIBLIST:
4019 /* sh_link is the section header index of the prelink library
08a40648
AM
4020 list used for the dynamic entries, or the symbol table, or
4021 the version strings. */
7f1204bb
JJ
4022 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
4023 ? ".dynstr" : ".gnu.libstr");
4024 if (s != NULL)
4025 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4026 break;
4027
252b5132 4028 case SHT_HASH:
fdc90cb4 4029 case SHT_GNU_HASH:
252b5132
RH
4030 case SHT_GNU_versym:
4031 /* sh_link is the section header index of the symbol table
4032 this hash table or version table is for. */
4033 s = bfd_get_section_by_name (abfd, ".dynsym");
4034 if (s != NULL)
4035 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4036 break;
dbb410c3
AM
4037
4038 case SHT_GROUP:
12bd6957 4039 d->this_hdr.sh_link = elf_onesymtab (abfd);
252b5132
RH
4040 }
4041 }
4042
3e19fb8f
L
4043 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4044 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4045 debug section name from .debug_* to .zdebug_* if needed. */
4046
b34976b6 4047 return TRUE;
252b5132
RH
4048}
4049
5372391b 4050static bfd_boolean
217aa764 4051sym_is_global (bfd *abfd, asymbol *sym)
252b5132
RH
4052{
4053 /* If the backend has a special mapping, use it. */
9c5bfbb7 4054 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764
AM
4055 if (bed->elf_backend_sym_is_global)
4056 return (*bed->elf_backend_sym_is_global) (abfd, sym);
252b5132 4057
e47bf690 4058 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
e6f7f6d1
AM
4059 || bfd_is_und_section (bfd_asymbol_section (sym))
4060 || bfd_is_com_section (bfd_asymbol_section (sym)));
252b5132
RH
4061}
4062
76359541
TP
4063/* Filter global symbols of ABFD to include in the import library. All
4064 SYMCOUNT symbols of ABFD can be examined from their pointers in
4065 SYMS. Pointers of symbols to keep should be stored contiguously at
4066 the beginning of that array.
4067
4068 Returns the number of symbols to keep. */
4069
4070unsigned int
4071_bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4072 asymbol **syms, long symcount)
4073{
4074 long src_count, dst_count = 0;
4075
4076 for (src_count = 0; src_count < symcount; src_count++)
4077 {
4078 asymbol *sym = syms[src_count];
4079 char *name = (char *) bfd_asymbol_name (sym);
4080 struct bfd_link_hash_entry *h;
4081
4082 if (!sym_is_global (abfd, sym))
4083 continue;
4084
4085 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE);
5df1bc57
AM
4086 if (h == NULL)
4087 continue;
76359541
TP
4088 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4089 continue;
76359541
TP
4090 if (h->linker_def || h->ldscript_def)
4091 continue;
4092
4093 syms[dst_count++] = sym;
4094 }
4095
4096 syms[dst_count] = NULL;
4097
4098 return dst_count;
4099}
4100
5372391b 4101/* Don't output section symbols for sections that are not going to be
c6d8cab4 4102 output, that are duplicates or there is no BFD section. */
5372391b
AM
4103
4104static bfd_boolean
4105ignore_section_sym (bfd *abfd, asymbol *sym)
4106{
c6d8cab4
L
4107 elf_symbol_type *type_ptr;
4108
db0c309f
NC
4109 if (sym == NULL)
4110 return FALSE;
4111
c6d8cab4
L
4112 if ((sym->flags & BSF_SECTION_SYM) == 0)
4113 return FALSE;
4114
db0c309f
NC
4115 if (sym->section == NULL)
4116 return TRUE;
4117
c6d8cab4
L
4118 type_ptr = elf_symbol_from (abfd, sym);
4119 return ((type_ptr != NULL
4120 && type_ptr->internal_elf_sym.st_shndx != 0
4121 && bfd_is_abs_section (sym->section))
4122 || !(sym->section->owner == abfd
db0c309f
NC
4123 || (sym->section->output_section != NULL
4124 && sym->section->output_section->owner == abfd
2633a79c
AM
4125 && sym->section->output_offset == 0)
4126 || bfd_is_abs_section (sym->section)));
5372391b
AM
4127}
4128
2633a79c
AM
4129/* Map symbol from it's internal number to the external number, moving
4130 all local symbols to be at the head of the list. */
4131
b34976b6 4132static bfd_boolean
12bd6957 4133elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
252b5132 4134{
dc810e39 4135 unsigned int symcount = bfd_get_symcount (abfd);
252b5132
RH
4136 asymbol **syms = bfd_get_outsymbols (abfd);
4137 asymbol **sect_syms;
dc810e39
AM
4138 unsigned int num_locals = 0;
4139 unsigned int num_globals = 0;
4140 unsigned int num_locals2 = 0;
4141 unsigned int num_globals2 = 0;
7292b3ac 4142 unsigned int max_index = 0;
dc810e39 4143 unsigned int idx;
252b5132
RH
4144 asection *asect;
4145 asymbol **new_syms;
446f7ed5 4146 size_t amt;
252b5132
RH
4147
4148#ifdef DEBUG
4149 fprintf (stderr, "elf_map_symbols\n");
4150 fflush (stderr);
4151#endif
4152
252b5132
RH
4153 for (asect = abfd->sections; asect; asect = asect->next)
4154 {
4155 if (max_index < asect->index)
4156 max_index = asect->index;
4157 }
4158
4159 max_index++;
446f7ed5
AM
4160 amt = max_index * sizeof (asymbol *);
4161 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
252b5132 4162 if (sect_syms == NULL)
b34976b6 4163 return FALSE;
252b5132 4164 elf_section_syms (abfd) = sect_syms;
4e89ac30 4165 elf_num_section_syms (abfd) = max_index;
252b5132 4166
079e9a2f
AM
4167 /* Init sect_syms entries for any section symbols we have already
4168 decided to output. */
252b5132
RH
4169 for (idx = 0; idx < symcount; idx++)
4170 {
dc810e39 4171 asymbol *sym = syms[idx];
c044fabd 4172
252b5132 4173 if ((sym->flags & BSF_SECTION_SYM) != 0
0f0a5e58 4174 && sym->value == 0
2633a79c
AM
4175 && !ignore_section_sym (abfd, sym)
4176 && !bfd_is_abs_section (sym->section))
252b5132 4177 {
5372391b 4178 asection *sec = sym->section;
252b5132 4179
5372391b
AM
4180 if (sec->owner != abfd)
4181 sec = sec->output_section;
252b5132 4182
5372391b 4183 sect_syms[sec->index] = syms[idx];
252b5132
RH
4184 }
4185 }
4186
252b5132
RH
4187 /* Classify all of the symbols. */
4188 for (idx = 0; idx < symcount; idx++)
4189 {
2633a79c 4190 if (sym_is_global (abfd, syms[idx]))
252b5132 4191 num_globals++;
2633a79c
AM
4192 else if (!ignore_section_sym (abfd, syms[idx]))
4193 num_locals++;
252b5132 4194 }
079e9a2f 4195
5372391b 4196 /* We will be adding a section symbol for each normal BFD section. Most
079e9a2f
AM
4197 sections will already have a section symbol in outsymbols, but
4198 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4199 at least in that case. */
252b5132
RH
4200 for (asect = abfd->sections; asect; asect = asect->next)
4201 {
079e9a2f 4202 if (sect_syms[asect->index] == NULL)
252b5132 4203 {
079e9a2f 4204 if (!sym_is_global (abfd, asect->symbol))
252b5132
RH
4205 num_locals++;
4206 else
4207 num_globals++;
252b5132
RH
4208 }
4209 }
4210
4211 /* Now sort the symbols so the local symbols are first. */
446f7ed5
AM
4212 amt = (num_locals + num_globals) * sizeof (asymbol *);
4213 new_syms = (asymbol **) bfd_alloc (abfd, amt);
252b5132 4214 if (new_syms == NULL)
b34976b6 4215 return FALSE;
252b5132
RH
4216
4217 for (idx = 0; idx < symcount; idx++)
4218 {
4219 asymbol *sym = syms[idx];
dc810e39 4220 unsigned int i;
252b5132 4221
2633a79c
AM
4222 if (sym_is_global (abfd, sym))
4223 i = num_locals + num_globals2++;
4224 else if (!ignore_section_sym (abfd, sym))
252b5132
RH
4225 i = num_locals2++;
4226 else
2633a79c 4227 continue;
252b5132
RH
4228 new_syms[i] = sym;
4229 sym->udata.i = i + 1;
4230 }
4231 for (asect = abfd->sections; asect; asect = asect->next)
4232 {
079e9a2f 4233 if (sect_syms[asect->index] == NULL)
252b5132 4234 {
079e9a2f 4235 asymbol *sym = asect->symbol;
dc810e39 4236 unsigned int i;
252b5132 4237
079e9a2f 4238 sect_syms[asect->index] = sym;
252b5132
RH
4239 if (!sym_is_global (abfd, sym))
4240 i = num_locals2++;
4241 else
4242 i = num_locals + num_globals2++;
4243 new_syms[i] = sym;
4244 sym->udata.i = i + 1;
4245 }
4246 }
4247
4248 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4249
12bd6957 4250 *pnum_locals = num_locals;
b34976b6 4251 return TRUE;
252b5132
RH
4252}
4253
4254/* Align to the maximum file alignment that could be required for any
4255 ELF data structure. */
4256
268b6b39 4257static inline file_ptr
217aa764 4258align_file_position (file_ptr off, int align)
252b5132
RH
4259{
4260 return (off + align - 1) & ~(align - 1);
4261}
4262
4263/* Assign a file position to a section, optionally aligning to the
4264 required section alignment. */
4265
217aa764
AM
4266file_ptr
4267_bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4268 file_ptr offset,
4269 bfd_boolean align)
252b5132 4270{
72de5009
AM
4271 if (align && i_shdrp->sh_addralign > 1)
4272 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
252b5132
RH
4273 i_shdrp->sh_offset = offset;
4274 if (i_shdrp->bfd_section != NULL)
4275 i_shdrp->bfd_section->filepos = offset;
4276 if (i_shdrp->sh_type != SHT_NOBITS)
4277 offset += i_shdrp->sh_size;
4278 return offset;
4279}
4280
4281/* Compute the file positions we are going to put the sections at, and
4282 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4283 is not NULL, this is being called by the ELF backend linker. */
4284
b34976b6 4285bfd_boolean
217aa764
AM
4286_bfd_elf_compute_section_file_positions (bfd *abfd,
4287 struct bfd_link_info *link_info)
252b5132 4288{
9c5bfbb7 4289 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 4290 struct fake_section_arg fsargs;
b34976b6 4291 bfd_boolean failed;
ef10c3ac 4292 struct elf_strtab_hash *strtab = NULL;
252b5132 4293 Elf_Internal_Shdr *shstrtab_hdr;
3516e984 4294 bfd_boolean need_symtab;
252b5132
RH
4295
4296 if (abfd->output_has_begun)
b34976b6 4297 return TRUE;
252b5132
RH
4298
4299 /* Do any elf backend specific processing first. */
4300 if (bed->elf_backend_begin_write_processing)
4301 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4302
ed7e9d0b 4303 if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
b34976b6 4304 return FALSE;
252b5132 4305
d4730f92
BS
4306 fsargs.failed = FALSE;
4307 fsargs.link_info = link_info;
4308 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4309 if (fsargs.failed)
b34976b6 4310 return FALSE;
252b5132 4311
da9f89d4 4312 if (!assign_section_numbers (abfd, link_info))
b34976b6 4313 return FALSE;
252b5132
RH
4314
4315 /* The backend linker builds symbol table information itself. */
3516e984
L
4316 need_symtab = (link_info == NULL
4317 && (bfd_get_symcount (abfd) > 0
4318 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4319 == HAS_RELOC)));
4320 if (need_symtab)
252b5132
RH
4321 {
4322 /* Non-zero if doing a relocatable link. */
4323 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4324
4325 if (! swap_out_syms (abfd, &strtab, relocatable_p))
b34976b6 4326 return FALSE;
252b5132
RH
4327 }
4328
d4730f92 4329 failed = FALSE;
1126897b 4330 if (link_info == NULL)
dbb410c3 4331 {
1126897b 4332 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
dbb410c3 4333 if (failed)
b34976b6 4334 return FALSE;
dbb410c3
AM
4335 }
4336
252b5132 4337 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
ed7e9d0b 4338 /* sh_name was set in init_file_header. */
252b5132 4339 shstrtab_hdr->sh_type = SHT_STRTAB;
84865015 4340 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
252b5132 4341 shstrtab_hdr->sh_addr = 0;
946748d5 4342 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
252b5132
RH
4343 shstrtab_hdr->sh_entsize = 0;
4344 shstrtab_hdr->sh_link = 0;
4345 shstrtab_hdr->sh_info = 0;
3e19fb8f 4346 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
252b5132
RH
4347 shstrtab_hdr->sh_addralign = 1;
4348
c84fca4d 4349 if (!assign_file_positions_except_relocs (abfd, link_info))
b34976b6 4350 return FALSE;
252b5132 4351
3516e984 4352 if (need_symtab)
252b5132
RH
4353 {
4354 file_ptr off;
4355 Elf_Internal_Shdr *hdr;
4356
12bd6957 4357 off = elf_next_file_pos (abfd);
252b5132 4358
6a40cf0c 4359 hdr = & elf_symtab_hdr (abfd);
b34976b6 4360 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
252b5132 4361
6a40cf0c
NC
4362 if (elf_symtab_shndx_list (abfd) != NULL)
4363 {
4364 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4365 if (hdr->sh_size != 0)
4366 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4367 /* FIXME: What about other symtab_shndx sections in the list ? */
4368 }
9ad5cbcf 4369
252b5132 4370 hdr = &elf_tdata (abfd)->strtab_hdr;
b34976b6 4371 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
252b5132 4372
12bd6957 4373 elf_next_file_pos (abfd) = off;
252b5132
RH
4374
4375 /* Now that we know where the .strtab section goes, write it
08a40648 4376 out. */
252b5132 4377 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 4378 || ! _bfd_elf_strtab_emit (abfd, strtab))
b34976b6 4379 return FALSE;
ef10c3ac 4380 _bfd_elf_strtab_free (strtab);
252b5132
RH
4381 }
4382
b34976b6 4383 abfd->output_has_begun = TRUE;
252b5132 4384
b34976b6 4385 return TRUE;
252b5132
RH
4386}
4387
8ded5a0f
AM
4388/* Make an initial estimate of the size of the program header. If we
4389 get the number wrong here, we'll redo section placement. */
4390
4391static bfd_size_type
4392get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4393{
4394 size_t segs;
4395 asection *s;
2b05f1b7 4396 const struct elf_backend_data *bed;
8ded5a0f
AM
4397
4398 /* Assume we will need exactly two PT_LOAD segments: one for text
4399 and one for data. */
4400 segs = 2;
4401
4402 s = bfd_get_section_by_name (abfd, ".interp");
1b9e270b 4403 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
8ded5a0f
AM
4404 {
4405 /* If we have a loadable interpreter section, we need a
4406 PT_INTERP segment. In this case, assume we also need a
4407 PT_PHDR segment, although that may not be true for all
4408 targets. */
e9a38e0f 4409 segs += 2;
8ded5a0f
AM
4410 }
4411
4412 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4413 {
4414 /* We need a PT_DYNAMIC segment. */
4415 ++segs;
f210dcff 4416 }
08a40648 4417
ceae84aa 4418 if (info != NULL && info->relro)
f210dcff
L
4419 {
4420 /* We need a PT_GNU_RELRO segment. */
4421 ++segs;
8ded5a0f
AM
4422 }
4423
12bd6957 4424 if (elf_eh_frame_hdr (abfd))
8ded5a0f
AM
4425 {
4426 /* We need a PT_GNU_EH_FRAME segment. */
4427 ++segs;
4428 }
4429
12bd6957 4430 if (elf_stack_flags (abfd))
8ded5a0f 4431 {
2b05f1b7
L
4432 /* We need a PT_GNU_STACK segment. */
4433 ++segs;
4434 }
94b11780 4435
0a59decb
L
4436 s = bfd_get_section_by_name (abfd,
4437 NOTE_GNU_PROPERTY_SECTION_NAME);
4438 if (s != NULL && s->size != 0)
4439 {
4440 /* We need a PT_GNU_PROPERTY segment. */
4441 ++segs;
4442 }
4443
2b05f1b7
L
4444 for (s = abfd->sections; s != NULL; s = s->next)
4445 {
8ded5a0f 4446 if ((s->flags & SEC_LOAD) != 0
23e463ed 4447 && elf_section_type (s) == SHT_NOTE)
8ded5a0f 4448 {
23e463ed 4449 unsigned int alignment_power;
8ded5a0f
AM
4450 /* We need a PT_NOTE segment. */
4451 ++segs;
23e463ed
L
4452 /* Try to create just one PT_NOTE segment for all adjacent
4453 loadable SHT_NOTE sections. gABI requires that within a
4454 PT_NOTE segment (and also inside of each SHT_NOTE section)
4455 each note should have the same alignment. So we check
4456 whether the sections are correctly aligned. */
4457 alignment_power = s->alignment_power;
4458 while (s->next != NULL
4459 && s->next->alignment_power == alignment_power
4460 && (s->next->flags & SEC_LOAD) != 0
4461 && elf_section_type (s->next) == SHT_NOTE)
4462 s = s->next;
8ded5a0f
AM
4463 }
4464 }
4465
4466 for (s = abfd->sections; s != NULL; s = s->next)
4467 {
4468 if (s->flags & SEC_THREAD_LOCAL)
4469 {
4470 /* We need a PT_TLS segment. */
4471 ++segs;
4472 break;
4473 }
4474 }
4475
2b05f1b7 4476 bed = get_elf_backend_data (abfd);
a91e1603 4477
df3a023b
AM
4478 if ((abfd->flags & D_PAGED) != 0
4479 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4480 {
4481 /* Add a PT_GNU_MBIND segment for each mbind section. */
4482 unsigned int page_align_power = bfd_log2 (bed->commonpagesize);
4483 for (s = abfd->sections; s != NULL; s = s->next)
4484 if (elf_section_flags (s) & SHF_GNU_MBIND)
4485 {
4486 if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4487 {
4488 _bfd_error_handler
4489 /* xgettext:c-format */
4490 (_("%pB: GNU_MBIND section `%pA' has invalid "
4491 "sh_info field: %d"),
4492 abfd, s, elf_section_data (s)->this_hdr.sh_info);
4493 continue;
4494 }
4495 /* Align mbind section to page size. */
4496 if (s->alignment_power < page_align_power)
4497 s->alignment_power = page_align_power;
4498 segs ++;
4499 }
4500 }
4501
4502 /* Let the backend count up any program headers it might need. */
4503 if (bed->elf_backend_additional_program_headers)
8ded5a0f
AM
4504 {
4505 int a;
4506
4507 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4508 if (a == -1)
4509 abort ();
4510 segs += a;
4511 }
4512
4513 return segs * bed->s->sizeof_phdr;
4514}
4515
2ea37f1c
NC
4516/* Find the segment that contains the output_section of section. */
4517
4518Elf_Internal_Phdr *
4519_bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4520{
4521 struct elf_segment_map *m;
4522 Elf_Internal_Phdr *p;
4523
12bd6957 4524 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
2ea37f1c
NC
4525 m != NULL;
4526 m = m->next, p++)
4527 {
4528 int i;
4529
4530 for (i = m->count - 1; i >= 0; i--)
4531 if (m->sections[i] == section)
4532 return p;
4533 }
4534
4535 return NULL;
4536}
4537
252b5132
RH
4538/* Create a mapping from a set of sections to a program segment. */
4539
217aa764
AM
4540static struct elf_segment_map *
4541make_mapping (bfd *abfd,
4542 asection **sections,
4543 unsigned int from,
4544 unsigned int to,
4545 bfd_boolean phdr)
252b5132
RH
4546{
4547 struct elf_segment_map *m;
4548 unsigned int i;
4549 asection **hdrpp;
986f0783 4550 size_t amt;
252b5132 4551
00bee008
AM
4552 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4553 amt += (to - from) * sizeof (asection *);
a50b1753 4554 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
4555 if (m == NULL)
4556 return NULL;
4557 m->next = NULL;
4558 m->p_type = PT_LOAD;
4559 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4560 m->sections[i - from] = *hdrpp;
4561 m->count = to - from;
4562
4563 if (from == 0 && phdr)
4564 {
4565 /* Include the headers in the first PT_LOAD segment. */
4566 m->includes_filehdr = 1;
4567 m->includes_phdrs = 1;
4568 }
4569
4570 return m;
4571}
4572
229fcec5
MM
4573/* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4574 on failure. */
4575
4576struct elf_segment_map *
4577_bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4578{
4579 struct elf_segment_map *m;
4580
a50b1753 4581 m = (struct elf_segment_map *) bfd_zalloc (abfd,
07d6d2b8 4582 sizeof (struct elf_segment_map));
229fcec5
MM
4583 if (m == NULL)
4584 return NULL;
4585 m->next = NULL;
4586 m->p_type = PT_DYNAMIC;
4587 m->count = 1;
4588 m->sections[0] = dynsec;
08a40648 4589
229fcec5
MM
4590 return m;
4591}
4592
8ded5a0f 4593/* Possibly add or remove segments from the segment map. */
252b5132 4594
b34976b6 4595static bfd_boolean
3dea8fca
AM
4596elf_modify_segment_map (bfd *abfd,
4597 struct bfd_link_info *info,
4598 bfd_boolean remove_empty_load)
252b5132 4599{
252e386e 4600 struct elf_segment_map **m;
8ded5a0f 4601 const struct elf_backend_data *bed;
252b5132 4602
8ded5a0f
AM
4603 /* The placement algorithm assumes that non allocated sections are
4604 not in PT_LOAD segments. We ensure this here by removing such
4605 sections from the segment map. We also remove excluded
252e386e
AM
4606 sections. Finally, any PT_LOAD segment without sections is
4607 removed. */
12bd6957 4608 m = &elf_seg_map (abfd);
252e386e 4609 while (*m)
8ded5a0f
AM
4610 {
4611 unsigned int i, new_count;
252b5132 4612
252e386e 4613 for (new_count = 0, i = 0; i < (*m)->count; i++)
8ded5a0f 4614 {
252e386e
AM
4615 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4616 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4617 || (*m)->p_type != PT_LOAD))
8ded5a0f 4618 {
252e386e
AM
4619 (*m)->sections[new_count] = (*m)->sections[i];
4620 new_count++;
8ded5a0f
AM
4621 }
4622 }
252e386e 4623 (*m)->count = new_count;
252b5132 4624
1a9ccd70
NC
4625 if (remove_empty_load
4626 && (*m)->p_type == PT_LOAD
4627 && (*m)->count == 0
4628 && !(*m)->includes_phdrs)
252e386e
AM
4629 *m = (*m)->next;
4630 else
4631 m = &(*m)->next;
8ded5a0f 4632 }
252b5132 4633
8ded5a0f
AM
4634 bed = get_elf_backend_data (abfd);
4635 if (bed->elf_backend_modify_segment_map != NULL)
252b5132 4636 {
252e386e 4637 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
8ded5a0f 4638 return FALSE;
252b5132 4639 }
252b5132 4640
8ded5a0f
AM
4641 return TRUE;
4642}
252b5132 4643
dbc88fc1
AM
4644#define IS_TBSS(s) \
4645 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4646
8ded5a0f 4647/* Set up a mapping from BFD sections to program segments. */
252b5132 4648
8ded5a0f
AM
4649bfd_boolean
4650_bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
4651{
4652 unsigned int count;
4653 struct elf_segment_map *m;
4654 asection **sections = NULL;
4655 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3dea8fca 4656 bfd_boolean no_user_phdrs;
252b5132 4657
12bd6957 4658 no_user_phdrs = elf_seg_map (abfd) == NULL;
d324f6d6
RM
4659
4660 if (info != NULL)
4661 info->user_phdrs = !no_user_phdrs;
4662
3dea8fca 4663 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
252b5132 4664 {
8ded5a0f
AM
4665 asection *s;
4666 unsigned int i;
4667 struct elf_segment_map *mfirst;
4668 struct elf_segment_map **pm;
4669 asection *last_hdr;
4670 bfd_vma last_size;
00bee008 4671 unsigned int hdr_index;
8ded5a0f
AM
4672 bfd_vma maxpagesize;
4673 asection **hdrpp;
64029e93 4674 bfd_boolean phdr_in_segment;
8ded5a0f 4675 bfd_boolean writable;
2888249f 4676 bfd_boolean executable;
446f7ed5 4677 unsigned int tls_count = 0;
8ded5a0f 4678 asection *first_tls = NULL;
a91e1603 4679 asection *first_mbind = NULL;
8ded5a0f 4680 asection *dynsec, *eh_frame_hdr;
446f7ed5 4681 size_t amt;
66631823
CE
4682 bfd_vma addr_mask, wrap_to = 0; /* Bytes. */
4683 bfd_size_type phdr_size; /* Octets/bytes. */
502794d4 4684 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132 4685
8ded5a0f 4686 /* Select the allocated sections, and sort them. */
252b5132 4687
446f7ed5
AM
4688 amt = bfd_count_sections (abfd) * sizeof (asection *);
4689 sections = (asection **) bfd_malloc (amt);
8ded5a0f 4690 if (sections == NULL)
252b5132 4691 goto error_return;
252b5132 4692
8d06853e
AM
4693 /* Calculate top address, avoiding undefined behaviour of shift
4694 left operator when shift count is equal to size of type
4695 being shifted. */
4696 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4697 addr_mask = (addr_mask << 1) + 1;
4698
8ded5a0f
AM
4699 i = 0;
4700 for (s = abfd->sections; s != NULL; s = s->next)
4701 {
4702 if ((s->flags & SEC_ALLOC) != 0)
4703 {
48db3297
AM
4704 /* target_index is unused until bfd_elf_final_link
4705 starts output of section symbols. Use it to make
4706 qsort stable. */
4707 s->target_index = i;
8ded5a0f
AM
4708 sections[i] = s;
4709 ++i;
8d06853e 4710 /* A wrapping section potentially clashes with header. */
66631823
CE
4711 if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
4712 wrap_to = (s->lma + s->size / opb) & addr_mask;
8ded5a0f
AM
4713 }
4714 }
4715 BFD_ASSERT (i <= bfd_count_sections (abfd));
4716 count = i;
252b5132 4717
8ded5a0f 4718 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
252b5132 4719
64029e93
AM
4720 phdr_size = elf_program_header_size (abfd);
4721 if (phdr_size == (bfd_size_type) -1)
4722 phdr_size = get_program_header_size (abfd, info);
4723 phdr_size += bed->s->sizeof_ehdr;
502794d4
CE
4724 /* phdr_size is compared to LMA values which are in bytes. */
4725 phdr_size /= opb;
64029e93
AM
4726 maxpagesize = bed->maxpagesize;
4727 if (maxpagesize == 0)
4728 maxpagesize = 1;
4729 phdr_in_segment = info != NULL && info->load_phdrs;
4730 if (count != 0
4731 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
4732 >= (phdr_size & (maxpagesize - 1))))
4733 /* For compatibility with old scripts that may not be using
4734 SIZEOF_HEADERS, add headers when it looks like space has
4735 been left for them. */
4736 phdr_in_segment = TRUE;
252b5132 4737
64029e93 4738 /* Build the mapping. */
8ded5a0f
AM
4739 mfirst = NULL;
4740 pm = &mfirst;
252b5132 4741
8ded5a0f
AM
4742 /* If we have a .interp section, then create a PT_PHDR segment for
4743 the program headers and a PT_INTERP segment for the .interp
4744 section. */
4745 s = bfd_get_section_by_name (abfd, ".interp");
1b9e270b 4746 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
8ded5a0f
AM
4747 {
4748 amt = sizeof (struct elf_segment_map);
a50b1753 4749 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4750 if (m == NULL)
4751 goto error_return;
4752 m->next = NULL;
4753 m->p_type = PT_PHDR;
f882209d 4754 m->p_flags = PF_R;
8ded5a0f
AM
4755 m->p_flags_valid = 1;
4756 m->includes_phdrs = 1;
64029e93 4757 phdr_in_segment = TRUE;
8ded5a0f
AM
4758 *pm = m;
4759 pm = &m->next;
252b5132 4760
8ded5a0f 4761 amt = sizeof (struct elf_segment_map);
a50b1753 4762 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4763 if (m == NULL)
4764 goto error_return;
4765 m->next = NULL;
4766 m->p_type = PT_INTERP;
4767 m->count = 1;
4768 m->sections[0] = s;
4769
4770 *pm = m;
4771 pm = &m->next;
252b5132 4772 }
8ded5a0f
AM
4773
4774 /* Look through the sections. We put sections in the same program
4775 segment when the start of the second section can be placed within
4776 a few bytes of the end of the first section. */
4777 last_hdr = NULL;
4778 last_size = 0;
00bee008 4779 hdr_index = 0;
8ded5a0f 4780 writable = FALSE;
2888249f 4781 executable = FALSE;
8ded5a0f
AM
4782 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4783 if (dynsec != NULL
4784 && (dynsec->flags & SEC_LOAD) == 0)
4785 dynsec = NULL;
4786
64029e93
AM
4787 if ((abfd->flags & D_PAGED) == 0)
4788 phdr_in_segment = FALSE;
4789
8ded5a0f
AM
4790 /* Deal with -Ttext or something similar such that the first section
4791 is not adjacent to the program headers. This is an
4792 approximation, since at this point we don't know exactly how many
4793 program headers we will need. */
64029e93 4794 if (phdr_in_segment && count > 0)
252b5132 4795 {
66631823 4796 bfd_vma phdr_lma; /* Bytes. */
64029e93
AM
4797 bfd_boolean separate_phdr = FALSE;
4798
4799 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
4800 if (info != NULL
4801 && info->separate_code
4802 && (sections[0]->flags & SEC_CODE) != 0)
1a9ccd70 4803 {
64029e93
AM
4804 /* If data sections should be separate from code and
4805 thus not executable, and the first section is
4806 executable then put the file and program headers in
4807 their own PT_LOAD. */
4808 separate_phdr = TRUE;
4809 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
4810 == (sections[0]->lma & addr_mask & -maxpagesize)))
4811 {
4812 /* The file and program headers are currently on the
4813 same page as the first section. Put them on the
4814 previous page if we can. */
4815 if (phdr_lma >= maxpagesize)
4816 phdr_lma -= maxpagesize;
4817 else
4818 separate_phdr = FALSE;
4819 }
4820 }
4821 if ((sections[0]->lma & addr_mask) < phdr_lma
4822 || (sections[0]->lma & addr_mask) < phdr_size)
4823 /* If file and program headers would be placed at the end
4824 of memory then it's probably better to omit them. */
4825 phdr_in_segment = FALSE;
4826 else if (phdr_lma < wrap_to)
4827 /* If a section wraps around to where we'll be placing
4828 file and program headers, then the headers will be
4829 overwritten. */
4830 phdr_in_segment = FALSE;
4831 else if (separate_phdr)
4832 {
4833 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
4834 if (m == NULL)
4835 goto error_return;
66631823 4836 m->p_paddr = phdr_lma * opb;
64029e93
AM
4837 m->p_vaddr_offset
4838 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
4839 m->p_paddr_valid = 1;
4840 *pm = m;
4841 pm = &m->next;
4842 phdr_in_segment = FALSE;
1a9ccd70 4843 }
252b5132
RH
4844 }
4845
8ded5a0f 4846 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
252b5132 4847 {
8ded5a0f
AM
4848 asection *hdr;
4849 bfd_boolean new_segment;
4850
4851 hdr = *hdrpp;
4852
4853 /* See if this section and the last one will fit in the same
4854 segment. */
4855
4856 if (last_hdr == NULL)
4857 {
4858 /* If we don't have a segment yet, then we don't need a new
4859 one (we build the last one after this loop). */
4860 new_segment = FALSE;
4861 }
4862 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4863 {
4864 /* If this section has a different relation between the
4865 virtual address and the load address, then we need a new
4866 segment. */
4867 new_segment = TRUE;
4868 }
b5599592
AM
4869 else if (hdr->lma < last_hdr->lma + last_size
4870 || last_hdr->lma + last_size < last_hdr->lma)
4871 {
4872 /* If this section has a load address that makes it overlap
4873 the previous section, then we need a new segment. */
4874 new_segment = TRUE;
4875 }
76cb3a89
AM
4876 else if ((abfd->flags & D_PAGED) != 0
4877 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4878 == (hdr->lma & -maxpagesize)))
4879 {
4880 /* If we are demand paged then we can't map two disk
4881 pages onto the same memory page. */
4882 new_segment = FALSE;
4883 }
39948a60
NC
4884 /* In the next test we have to be careful when last_hdr->lma is close
4885 to the end of the address space. If the aligned address wraps
4886 around to the start of the address space, then there are no more
4887 pages left in memory and it is OK to assume that the current
4888 section can be included in the current segment. */
76cb3a89
AM
4889 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4890 + maxpagesize > last_hdr->lma)
4891 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4892 + maxpagesize <= hdr->lma))
8ded5a0f
AM
4893 {
4894 /* If putting this section in this segment would force us to
4895 skip a page in the segment, then we need a new segment. */
4896 new_segment = TRUE;
4897 }
4898 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
76cb3a89 4899 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
8ded5a0f 4900 {
e5654c0f
AM
4901 /* We don't want to put a loaded section after a
4902 nonloaded (ie. bss style) section in the same segment
4903 as that will force the non-loaded section to be loaded.
76cb3a89 4904 Consider .tbss sections as loaded for this purpose. */
8ded5a0f
AM
4905 new_segment = TRUE;
4906 }
4907 else if ((abfd->flags & D_PAGED) == 0)
4908 {
4909 /* If the file is not demand paged, which means that we
4910 don't require the sections to be correctly aligned in the
4911 file, then there is no other reason for a new segment. */
4912 new_segment = FALSE;
4913 }
2888249f
L
4914 else if (info != NULL
4915 && info->separate_code
4916 && executable != ((hdr->flags & SEC_CODE) != 0))
4917 {
4918 new_segment = TRUE;
4919 }
8ded5a0f 4920 else if (! writable
76cb3a89 4921 && (hdr->flags & SEC_READONLY) == 0)
8ded5a0f
AM
4922 {
4923 /* We don't want to put a writable section in a read only
76cb3a89 4924 segment. */
8ded5a0f
AM
4925 new_segment = TRUE;
4926 }
4927 else
4928 {
4929 /* Otherwise, we can use the same segment. */
4930 new_segment = FALSE;
4931 }
4932
2889e75b 4933 /* Allow interested parties a chance to override our decision. */
ceae84aa
AM
4934 if (last_hdr != NULL
4935 && info != NULL
4936 && info->callbacks->override_segment_assignment != NULL)
4937 new_segment
4938 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4939 last_hdr,
4940 new_segment);
2889e75b 4941
8ded5a0f
AM
4942 if (! new_segment)
4943 {
4944 if ((hdr->flags & SEC_READONLY) == 0)
4945 writable = TRUE;
2888249f
L
4946 if ((hdr->flags & SEC_CODE) != 0)
4947 executable = TRUE;
8ded5a0f
AM
4948 last_hdr = hdr;
4949 /* .tbss sections effectively have zero size. */
502794d4 4950 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
8ded5a0f
AM
4951 continue;
4952 }
4953
4954 /* We need a new program segment. We must create a new program
00bee008 4955 header holding all the sections from hdr_index until hdr. */
8ded5a0f 4956
00bee008 4957 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
8ded5a0f
AM
4958 if (m == NULL)
4959 goto error_return;
4960
4961 *pm = m;
4962 pm = &m->next;
4963
252b5132 4964 if ((hdr->flags & SEC_READONLY) == 0)
b34976b6 4965 writable = TRUE;
8ded5a0f
AM
4966 else
4967 writable = FALSE;
4968
2888249f
L
4969 if ((hdr->flags & SEC_CODE) == 0)
4970 executable = FALSE;
4971 else
4972 executable = TRUE;
4973
baaff79e
JJ
4974 last_hdr = hdr;
4975 /* .tbss sections effectively have zero size. */
502794d4 4976 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
00bee008 4977 hdr_index = i;
8ded5a0f 4978 phdr_in_segment = FALSE;
252b5132
RH
4979 }
4980
86b2281f
AM
4981 /* Create a final PT_LOAD program segment, but not if it's just
4982 for .tbss. */
4983 if (last_hdr != NULL
00bee008 4984 && (i - hdr_index != 1
dbc88fc1 4985 || !IS_TBSS (last_hdr)))
8ded5a0f 4986 {
00bee008 4987 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
8ded5a0f
AM
4988 if (m == NULL)
4989 goto error_return;
252b5132 4990
8ded5a0f
AM
4991 *pm = m;
4992 pm = &m->next;
4993 }
252b5132 4994
8ded5a0f
AM
4995 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4996 if (dynsec != NULL)
4997 {
4998 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4999 if (m == NULL)
5000 goto error_return;
5001 *pm = m;
5002 pm = &m->next;
5003 }
252b5132 5004
23e463ed 5005 /* For each batch of consecutive loadable SHT_NOTE sections,
1c5265b5
JJ
5006 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
5007 because if we link together nonloadable .note sections and
5008 loadable .note sections, we will generate two .note sections
23e463ed 5009 in the output file. */
8ded5a0f
AM
5010 for (s = abfd->sections; s != NULL; s = s->next)
5011 {
5012 if ((s->flags & SEC_LOAD) != 0
23e463ed 5013 && elf_section_type (s) == SHT_NOTE)
8ded5a0f 5014 {
1c5265b5 5015 asection *s2;
23e463ed 5016 unsigned int alignment_power = s->alignment_power;
91d6fa6a
NC
5017
5018 count = 1;
23e463ed
L
5019 for (s2 = s; s2->next != NULL; s2 = s2->next)
5020 {
5021 if (s2->next->alignment_power == alignment_power
5022 && (s2->next->flags & SEC_LOAD) != 0
5023 && elf_section_type (s2->next) == SHT_NOTE
66631823 5024 && align_power (s2->lma + s2->size / opb,
23e463ed
L
5025 alignment_power)
5026 == s2->next->lma)
5027 count++;
5028 else
5029 break;
5030 }
00bee008
AM
5031 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5032 amt += count * sizeof (asection *);
a50b1753 5033 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5034 if (m == NULL)
5035 goto error_return;
5036 m->next = NULL;
5037 m->p_type = PT_NOTE;
1c5265b5
JJ
5038 m->count = count;
5039 while (count > 1)
5040 {
5041 m->sections[m->count - count--] = s;
5042 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5043 s = s->next;
5044 }
5045 m->sections[m->count - 1] = s;
5046 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
8ded5a0f
AM
5047 *pm = m;
5048 pm = &m->next;
5049 }
5050 if (s->flags & SEC_THREAD_LOCAL)
5051 {
5052 if (! tls_count)
5053 first_tls = s;
5054 tls_count++;
5055 }
a91e1603
L
5056 if (first_mbind == NULL
5057 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5058 first_mbind = s;
8ded5a0f 5059 }
252b5132 5060
8ded5a0f
AM
5061 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5062 if (tls_count > 0)
5063 {
00bee008
AM
5064 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5065 amt += tls_count * sizeof (asection *);
a50b1753 5066 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5067 if (m == NULL)
5068 goto error_return;
5069 m->next = NULL;
5070 m->p_type = PT_TLS;
5071 m->count = tls_count;
5072 /* Mandated PF_R. */
5073 m->p_flags = PF_R;
5074 m->p_flags_valid = 1;
d923cae0 5075 s = first_tls;
446f7ed5 5076 for (i = 0; i < tls_count; ++i)
8ded5a0f 5077 {
d923cae0
L
5078 if ((s->flags & SEC_THREAD_LOCAL) == 0)
5079 {
5080 _bfd_error_handler
871b3ab2 5081 (_("%pB: TLS sections are not adjacent:"), abfd);
d923cae0
L
5082 s = first_tls;
5083 i = 0;
446f7ed5 5084 while (i < tls_count)
d923cae0
L
5085 {
5086 if ((s->flags & SEC_THREAD_LOCAL) != 0)
5087 {
871b3ab2 5088 _bfd_error_handler (_(" TLS: %pA"), s);
d923cae0
L
5089 i++;
5090 }
5091 else
871b3ab2 5092 _bfd_error_handler (_(" non-TLS: %pA"), s);
d923cae0
L
5093 s = s->next;
5094 }
5095 bfd_set_error (bfd_error_bad_value);
5096 goto error_return;
5097 }
5098 m->sections[i] = s;
5099 s = s->next;
8ded5a0f 5100 }
252b5132 5101
8ded5a0f
AM
5102 *pm = m;
5103 pm = &m->next;
5104 }
252b5132 5105
df3a023b
AM
5106 if (first_mbind
5107 && (abfd->flags & D_PAGED) != 0
5108 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
a91e1603
L
5109 for (s = first_mbind; s != NULL; s = s->next)
5110 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
df3a023b 5111 && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
a91e1603
L
5112 {
5113 /* Mandated PF_R. */
5114 unsigned long p_flags = PF_R;
5115 if ((s->flags & SEC_READONLY) == 0)
5116 p_flags |= PF_W;
5117 if ((s->flags & SEC_CODE) != 0)
5118 p_flags |= PF_X;
5119
5120 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5121 m = bfd_zalloc (abfd, amt);
5122 if (m == NULL)
5123 goto error_return;
5124 m->next = NULL;
5125 m->p_type = (PT_GNU_MBIND_LO
5126 + elf_section_data (s)->this_hdr.sh_info);
5127 m->count = 1;
5128 m->p_flags_valid = 1;
5129 m->sections[0] = s;
5130 m->p_flags = p_flags;
5131
5132 *pm = m;
5133 pm = &m->next;
5134 }
5135
0a59decb
L
5136 s = bfd_get_section_by_name (abfd,
5137 NOTE_GNU_PROPERTY_SECTION_NAME);
5138 if (s != NULL && s->size != 0)
5139 {
5140 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5141 m = bfd_zalloc (abfd, amt);
5142 if (m == NULL)
5143 goto error_return;
5144 m->next = NULL;
5145 m->p_type = PT_GNU_PROPERTY;
5146 m->count = 1;
5147 m->p_flags_valid = 1;
5148 m->sections[0] = s;
5149 m->p_flags = PF_R;
5150 *pm = m;
5151 pm = &m->next;
5152 }
5153
8ded5a0f
AM
5154 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5155 segment. */
12bd6957 5156 eh_frame_hdr = elf_eh_frame_hdr (abfd);
8ded5a0f
AM
5157 if (eh_frame_hdr != NULL
5158 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
252b5132 5159 {
dc810e39 5160 amt = sizeof (struct elf_segment_map);
a50b1753 5161 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
5162 if (m == NULL)
5163 goto error_return;
5164 m->next = NULL;
8ded5a0f 5165 m->p_type = PT_GNU_EH_FRAME;
252b5132 5166 m->count = 1;
8ded5a0f 5167 m->sections[0] = eh_frame_hdr->output_section;
252b5132
RH
5168
5169 *pm = m;
5170 pm = &m->next;
5171 }
13ae64f3 5172
12bd6957 5173 if (elf_stack_flags (abfd))
13ae64f3 5174 {
8ded5a0f 5175 amt = sizeof (struct elf_segment_map);
a50b1753 5176 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5177 if (m == NULL)
5178 goto error_return;
5179 m->next = NULL;
2b05f1b7 5180 m->p_type = PT_GNU_STACK;
12bd6957 5181 m->p_flags = elf_stack_flags (abfd);
04c3a755 5182 m->p_align = bed->stack_align;
8ded5a0f 5183 m->p_flags_valid = 1;
04c3a755
NS
5184 m->p_align_valid = m->p_align != 0;
5185 if (info->stacksize > 0)
5186 {
5187 m->p_size = info->stacksize;
5188 m->p_size_valid = 1;
5189 }
252b5132 5190
8ded5a0f
AM
5191 *pm = m;
5192 pm = &m->next;
5193 }
65765700 5194
ceae84aa 5195 if (info != NULL && info->relro)
8ded5a0f 5196 {
f210dcff
L
5197 for (m = mfirst; m != NULL; m = m->next)
5198 {
3832a4d8
AM
5199 if (m->p_type == PT_LOAD
5200 && m->count != 0
5201 && m->sections[0]->vma >= info->relro_start
5202 && m->sections[0]->vma < info->relro_end)
f210dcff 5203 {
3832a4d8
AM
5204 i = m->count;
5205 while (--i != (unsigned) -1)
ec2e748a
NC
5206 {
5207 if (m->sections[i]->size > 0
5208 && (m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
5209 == (SEC_LOAD | SEC_HAS_CONTENTS))
5210 break;
5211 }
3832a4d8 5212
43a8475c 5213 if (i != (unsigned) -1)
f210dcff
L
5214 break;
5215 }
be01b344 5216 }
f210dcff
L
5217
5218 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5219 if (m != NULL)
5220 {
5221 amt = sizeof (struct elf_segment_map);
a50b1753 5222 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
f210dcff
L
5223 if (m == NULL)
5224 goto error_return;
5225 m->next = NULL;
5226 m->p_type = PT_GNU_RELRO;
f210dcff
L
5227 *pm = m;
5228 pm = &m->next;
5229 }
8ded5a0f 5230 }
9ee5e499 5231
8ded5a0f 5232 free (sections);
12bd6957 5233 elf_seg_map (abfd) = mfirst;
9ee5e499
JJ
5234 }
5235
3dea8fca 5236 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
8ded5a0f 5237 return FALSE;
8c37241b 5238
12bd6957 5239 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
8ded5a0f 5240 ++count;
12bd6957 5241 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
252b5132 5242
b34976b6 5243 return TRUE;
252b5132
RH
5244
5245 error_return:
5246 if (sections != NULL)
5247 free (sections);
b34976b6 5248 return FALSE;
252b5132
RH
5249}
5250
5251/* Sort sections by address. */
5252
5253static int
217aa764 5254elf_sort_sections (const void *arg1, const void *arg2)
252b5132
RH
5255{
5256 const asection *sec1 = *(const asection **) arg1;
5257 const asection *sec2 = *(const asection **) arg2;
eecdbe52 5258 bfd_size_type size1, size2;
252b5132
RH
5259
5260 /* Sort by LMA first, since this is the address used to
5261 place the section into a segment. */
5262 if (sec1->lma < sec2->lma)
5263 return -1;
5264 else if (sec1->lma > sec2->lma)
5265 return 1;
5266
5267 /* Then sort by VMA. Normally the LMA and the VMA will be
5268 the same, and this will do nothing. */
5269 if (sec1->vma < sec2->vma)
5270 return -1;
5271 else if (sec1->vma > sec2->vma)
5272 return 1;
5273
5274 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5275
07c6e936 5276#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
252b5132
RH
5277
5278 if (TOEND (sec1))
5279 {
48db3297 5280 if (!TOEND (sec2))
252b5132
RH
5281 return 1;
5282 }
00a7cdc5 5283 else if (TOEND (sec2))
252b5132
RH
5284 return -1;
5285
5286#undef TOEND
5287
00a7cdc5
NC
5288 /* Sort by size, to put zero sized sections
5289 before others at the same address. */
252b5132 5290
eea6121a
AM
5291 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5292 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
eecdbe52
JJ
5293
5294 if (size1 < size2)
252b5132 5295 return -1;
eecdbe52 5296 if (size1 > size2)
252b5132
RH
5297 return 1;
5298
5299 return sec1->target_index - sec2->target_index;
5300}
5301
30fe1832
AM
5302/* This qsort comparison functions sorts PT_LOAD segments first and
5303 by p_paddr, for assign_file_positions_for_load_sections. */
5304
5305static int
5306elf_sort_segments (const void *arg1, const void *arg2)
5307{
5308 const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5309 const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5310
5311 if (m1->p_type != m2->p_type)
5312 {
5313 if (m1->p_type == PT_NULL)
5314 return 1;
5315 if (m2->p_type == PT_NULL)
5316 return -1;
5317 return m1->p_type < m2->p_type ? -1 : 1;
5318 }
5319 if (m1->includes_filehdr != m2->includes_filehdr)
5320 return m1->includes_filehdr ? -1 : 1;
5321 if (m1->no_sort_lma != m2->no_sort_lma)
5322 return m1->no_sort_lma ? -1 : 1;
5323 if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5324 {
4b3ecb3b 5325 bfd_vma lma1, lma2; /* Octets. */
30fe1832
AM
5326 lma1 = 0;
5327 if (m1->p_paddr_valid)
4b3ecb3b 5328 lma1 = m1->p_paddr;
30fe1832 5329 else if (m1->count != 0)
4b3ecb3b
AM
5330 {
5331 unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5332 m1->sections[0]);
5333 lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5334 }
30fe1832
AM
5335 lma2 = 0;
5336 if (m2->p_paddr_valid)
4b3ecb3b 5337 lma2 = m2->p_paddr;
30fe1832 5338 else if (m2->count != 0)
4b3ecb3b
AM
5339 {
5340 unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5341 m2->sections[0]);
5342 lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5343 }
30fe1832
AM
5344 if (lma1 != lma2)
5345 return lma1 < lma2 ? -1 : 1;
5346 }
5347 if (m1->idx != m2->idx)
5348 return m1->idx < m2->idx ? -1 : 1;
5349 return 0;
5350}
5351
340b6d91
AC
5352/* Ian Lance Taylor writes:
5353
5354 We shouldn't be using % with a negative signed number. That's just
5355 not good. We have to make sure either that the number is not
5356 negative, or that the number has an unsigned type. When the types
5357 are all the same size they wind up as unsigned. When file_ptr is a
5358 larger signed type, the arithmetic winds up as signed long long,
5359 which is wrong.
5360
5361 What we're trying to say here is something like ``increase OFF by
5362 the least amount that will cause it to be equal to the VMA modulo
5363 the page size.'' */
5364/* In other words, something like:
5365
5366 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5367 off_offset = off % bed->maxpagesize;
5368 if (vma_offset < off_offset)
5369 adjustment = vma_offset + bed->maxpagesize - off_offset;
5370 else
5371 adjustment = vma_offset - off_offset;
08a40648 5372
de194d85 5373 which can be collapsed into the expression below. */
340b6d91
AC
5374
5375static file_ptr
5376vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5377{
dc9155b2
NC
5378 /* PR binutils/16199: Handle an alignment of zero. */
5379 if (maxpagesize == 0)
5380 maxpagesize = 1;
340b6d91
AC
5381 return ((vma - off) % maxpagesize);
5382}
5383
6d33f217
L
5384static void
5385print_segment_map (const struct elf_segment_map *m)
5386{
5387 unsigned int j;
5388 const char *pt = get_segment_type (m->p_type);
5389 char buf[32];
5390
5391 if (pt == NULL)
5392 {
5393 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5394 sprintf (buf, "LOPROC+%7.7x",
5395 (unsigned int) (m->p_type - PT_LOPROC));
5396 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5397 sprintf (buf, "LOOS+%7.7x",
5398 (unsigned int) (m->p_type - PT_LOOS));
5399 else
5400 snprintf (buf, sizeof (buf), "%8.8x",
5401 (unsigned int) m->p_type);
5402 pt = buf;
5403 }
4a97a0e5 5404 fflush (stdout);
6d33f217
L
5405 fprintf (stderr, "%s:", pt);
5406 for (j = 0; j < m->count; j++)
5407 fprintf (stderr, " %s", m->sections [j]->name);
5408 putc ('\n',stderr);
4a97a0e5 5409 fflush (stderr);
6d33f217
L
5410}
5411
32812159
AM
5412static bfd_boolean
5413write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
5414{
5415 void *buf;
5416 bfd_boolean ret;
5417
5418 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5419 return FALSE;
5420 buf = bfd_zmalloc (len);
5421 if (buf == NULL)
5422 return FALSE;
5423 ret = bfd_bwrite (buf, len, abfd) == len;
5424 free (buf);
5425 return ret;
5426}
5427
252b5132
RH
5428/* Assign file positions to the sections based on the mapping from
5429 sections to segments. This function also sets up some fields in
f3520d2f 5430 the file header. */
252b5132 5431
b34976b6 5432static bfd_boolean
f3520d2f
AM
5433assign_file_positions_for_load_sections (bfd *abfd,
5434 struct bfd_link_info *link_info)
252b5132
RH
5435{
5436 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 5437 struct elf_segment_map *m;
30fe1832 5438 struct elf_segment_map *phdr_load_seg;
252b5132 5439 Elf_Internal_Phdr *phdrs;
252b5132 5440 Elf_Internal_Phdr *p;
502794d4 5441 file_ptr off; /* Octets. */
3f570048 5442 bfd_size_type maxpagesize;
30fe1832 5443 unsigned int alloc, actual;
0920dee7 5444 unsigned int i, j;
30fe1832 5445 struct elf_segment_map **sorted_seg_map;
502794d4 5446 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132 5447
e36284ab 5448 if (link_info == NULL
ceae84aa 5449 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
8ded5a0f 5450 return FALSE;
252b5132 5451
8ded5a0f 5452 alloc = 0;
12bd6957 5453 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
30fe1832 5454 m->idx = alloc++;
252b5132 5455
82f2dbf7
NC
5456 if (alloc)
5457 {
5458 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5459 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5460 }
5461 else
5462 {
5463 /* PR binutils/12467. */
5464 elf_elfheader (abfd)->e_phoff = 0;
5465 elf_elfheader (abfd)->e_phentsize = 0;
5466 }
d324f6d6 5467
8ded5a0f 5468 elf_elfheader (abfd)->e_phnum = alloc;
252b5132 5469
12bd6957 5470 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
30fe1832
AM
5471 {
5472 actual = alloc;
5473 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5474 }
8ded5a0f 5475 else
30fe1832
AM
5476 {
5477 actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5478 BFD_ASSERT (elf_program_header_size (abfd)
5479 == actual * bed->s->sizeof_phdr);
5480 BFD_ASSERT (actual >= alloc);
5481 }
252b5132
RH
5482
5483 if (alloc == 0)
f3520d2f 5484 {
12bd6957 5485 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
8ded5a0f 5486 return TRUE;
f3520d2f 5487 }
252b5132 5488
12bd6957 5489 /* We're writing the size in elf_program_header_size (abfd),
57268894
HPN
5490 see assign_file_positions_except_relocs, so make sure we have
5491 that amount allocated, with trailing space cleared.
12bd6957
AM
5492 The variable alloc contains the computed need, while
5493 elf_program_header_size (abfd) contains the size used for the
57268894
HPN
5494 layout.
5495 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5496 where the layout is forced to according to a larger size in the
5497 last iterations for the testcase ld-elf/header. */
30fe1832
AM
5498 phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
5499 + alloc * sizeof (*sorted_seg_map)));
5500 sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
f3520d2f 5501 elf_tdata (abfd)->phdr = phdrs;
252b5132 5502 if (phdrs == NULL)
b34976b6 5503 return FALSE;
252b5132 5504
30fe1832 5505 for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
252b5132 5506 {
30fe1832 5507 sorted_seg_map[j] = m;
252b5132 5508 /* If elf_segment_map is not from map_sections_to_segments, the
08a40648 5509 sections may not be correctly ordered. NOTE: sorting should
52e9b619
MS
5510 not be done to the PT_NOTE section of a corefile, which may
5511 contain several pseudo-sections artificially created by bfd.
5512 Sorting these pseudo-sections breaks things badly. */
47d9a591
AM
5513 if (m->count > 1
5514 && !(elf_elfheader (abfd)->e_type == ET_CORE
52e9b619 5515 && m->p_type == PT_NOTE))
48db3297
AM
5516 {
5517 for (i = 0; i < m->count; i++)
5518 m->sections[i]->target_index = i;
5519 qsort (m->sections, (size_t) m->count, sizeof (asection *),
5520 elf_sort_sections);
5521 }
30fe1832
AM
5522 }
5523 if (alloc > 1)
5524 qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
5525 elf_sort_segments);
5526
5527 maxpagesize = 1;
5528 if ((abfd->flags & D_PAGED) != 0)
5529 maxpagesize = bed->maxpagesize;
5530
5531 /* Sections must map to file offsets past the ELF file header. */
5532 off = bed->s->sizeof_ehdr;
5533 /* And if one of the PT_LOAD headers doesn't include the program
5534 headers then we'll be mapping program headers in the usual
5535 position after the ELF file header. */
5536 phdr_load_seg = NULL;
5537 for (j = 0; j < alloc; j++)
5538 {
5539 m = sorted_seg_map[j];
5540 if (m->p_type != PT_LOAD)
5541 break;
5542 if (m->includes_phdrs)
5543 {
5544 phdr_load_seg = m;
5545 break;
5546 }
5547 }
5548 if (phdr_load_seg == NULL)
5549 off += actual * bed->s->sizeof_phdr;
5550
5551 for (j = 0; j < alloc; j++)
5552 {
5553 asection **secpp;
502794d4 5554 bfd_vma off_adjust; /* Octets. */
30fe1832 5555 bfd_boolean no_contents;
252b5132 5556
b301b248
AM
5557 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5558 number of sections with contents contributing to both p_filesz
5559 and p_memsz, followed by a number of sections with no contents
5560 that just contribute to p_memsz. In this loop, OFF tracks next
02bf8d82 5561 available file offset for PT_LOAD and PT_NOTE segments. */
30fe1832
AM
5562 m = sorted_seg_map[j];
5563 p = phdrs + m->idx;
252b5132 5564 p->p_type = m->p_type;
28a7f3e7 5565 p->p_flags = m->p_flags;
252b5132 5566
3f570048 5567 if (m->count == 0)
502794d4 5568 p->p_vaddr = m->p_vaddr_offset * opb;
3f570048 5569 else
502794d4 5570 p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
3f570048
AM
5571
5572 if (m->p_paddr_valid)
5573 p->p_paddr = m->p_paddr;
5574 else if (m->count == 0)
5575 p->p_paddr = 0;
5576 else
502794d4 5577 p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
3f570048
AM
5578
5579 if (p->p_type == PT_LOAD
5580 && (abfd->flags & D_PAGED) != 0)
5581 {
5582 /* p_align in demand paged PT_LOAD segments effectively stores
5583 the maximum page size. When copying an executable with
5584 objcopy, we set m->p_align from the input file. Use this
5585 value for maxpagesize rather than bed->maxpagesize, which
5586 may be different. Note that we use maxpagesize for PT_TLS
5587 segment alignment later in this function, so we are relying
5588 on at least one PT_LOAD segment appearing before a PT_TLS
5589 segment. */
5590 if (m->p_align_valid)
5591 maxpagesize = m->p_align;
5592
5593 p->p_align = maxpagesize;
5594 }
3271a814
NS
5595 else if (m->p_align_valid)
5596 p->p_align = m->p_align;
e970b90a
DJ
5597 else if (m->count == 0)
5598 p->p_align = 1 << bed->s->log_file_align;
30fe1832
AM
5599
5600 if (m == phdr_load_seg)
5601 {
5602 if (!m->includes_filehdr)
5603 p->p_offset = off;
5604 off += actual * bed->s->sizeof_phdr;
5605 }
3f570048 5606
bf988460
AM
5607 no_contents = FALSE;
5608 off_adjust = 0;
252b5132 5609 if (p->p_type == PT_LOAD
b301b248 5610 && m->count > 0)
252b5132 5611 {
66631823 5612 bfd_size_type align; /* Bytes. */
a49e53ed 5613 unsigned int align_power = 0;
b301b248 5614
3271a814
NS
5615 if (m->p_align_valid)
5616 align = p->p_align;
5617 else
252b5132 5618 {
3271a814
NS
5619 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5620 {
5621 unsigned int secalign;
08a40648 5622
fd361982 5623 secalign = bfd_section_alignment (*secpp);
3271a814
NS
5624 if (secalign > align_power)
5625 align_power = secalign;
5626 }
5627 align = (bfd_size_type) 1 << align_power;
5628 if (align < maxpagesize)
5629 align = maxpagesize;
b301b248 5630 }
252b5132 5631
02bf8d82
AM
5632 for (i = 0; i < m->count; i++)
5633 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
5634 /* If we aren't making room for this section, then
5635 it must be SHT_NOBITS regardless of what we've
5636 set via struct bfd_elf_special_section. */
5637 elf_section_type (m->sections[i]) = SHT_NOBITS;
5638
bf988460 5639 /* Find out whether this segment contains any loadable
aea274d3
AM
5640 sections. */
5641 no_contents = TRUE;
5642 for (i = 0; i < m->count; i++)
5643 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
5644 {
5645 no_contents = FALSE;
5646 break;
5647 }
bf988460 5648
66631823 5649 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
a8c75b76
AM
5650
5651 /* Broken hardware and/or kernel require that files do not
5652 map the same page with different permissions on some hppa
5653 processors. */
30fe1832
AM
5654 if (j != 0
5655 && (abfd->flags & D_PAGED) != 0
a8c75b76
AM
5656 && bed->no_page_alias
5657 && (off & (maxpagesize - 1)) != 0
502794d4
CE
5658 && ((off & -maxpagesize)
5659 == ((off + off_adjust) & -maxpagesize)))
a8c75b76 5660 off_adjust += maxpagesize;
bf988460
AM
5661 off += off_adjust;
5662 if (no_contents)
5663 {
5664 /* We shouldn't need to align the segment on disk since
5665 the segment doesn't need file space, but the gABI
5666 arguably requires the alignment and glibc ld.so
5667 checks it. So to comply with the alignment
5668 requirement but not waste file space, we adjust
5669 p_offset for just this segment. (OFF_ADJUST is
5670 subtracted from OFF later.) This may put p_offset
5671 past the end of file, but that shouldn't matter. */
5672 }
5673 else
5674 off_adjust = 0;
252b5132 5675 }
b1a6d0b1
NC
5676 /* Make sure the .dynamic section is the first section in the
5677 PT_DYNAMIC segment. */
5678 else if (p->p_type == PT_DYNAMIC
5679 && m->count > 1
5680 && strcmp (m->sections[0]->name, ".dynamic") != 0)
5681 {
5682 _bfd_error_handler
871b3ab2 5683 (_("%pB: The first section in the PT_DYNAMIC segment"
63a5468a 5684 " is not the .dynamic section"),
b301b248 5685 abfd);
b1a6d0b1
NC
5686 bfd_set_error (bfd_error_bad_value);
5687 return FALSE;
5688 }
3f001e84
JK
5689 /* Set the note section type to SHT_NOTE. */
5690 else if (p->p_type == PT_NOTE)
5691 for (i = 0; i < m->count; i++)
5692 elf_section_type (m->sections[i]) = SHT_NOTE;
252b5132 5693
252b5132
RH
5694 if (m->includes_filehdr)
5695 {
bf988460 5696 if (!m->p_flags_valid)
252b5132 5697 p->p_flags |= PF_R;
252b5132
RH
5698 p->p_filesz = bed->s->sizeof_ehdr;
5699 p->p_memsz = bed->s->sizeof_ehdr;
30fe1832 5700 if (p->p_type == PT_LOAD)
252b5132 5701 {
30fe1832 5702 if (m->count > 0)
252b5132 5703 {
30fe1832
AM
5704 if (p->p_vaddr < (bfd_vma) off
5705 || (!m->p_paddr_valid
5706 && p->p_paddr < (bfd_vma) off))
5707 {
5708 _bfd_error_handler
5709 (_("%pB: not enough room for program headers,"
5710 " try linking with -N"),
5711 abfd);
5712 bfd_set_error (bfd_error_bad_value);
5713 return FALSE;
5714 }
5715 p->p_vaddr -= off;
5716 if (!m->p_paddr_valid)
5717 p->p_paddr -= off;
252b5132 5718 }
30fe1832
AM
5719 }
5720 else if (sorted_seg_map[0]->includes_filehdr)
5721 {
5722 Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
5723 p->p_vaddr = filehdr->p_vaddr;
bf988460 5724 if (!m->p_paddr_valid)
30fe1832 5725 p->p_paddr = filehdr->p_paddr;
252b5132 5726 }
252b5132
RH
5727 }
5728
5729 if (m->includes_phdrs)
5730 {
bf988460 5731 if (!m->p_flags_valid)
252b5132 5732 p->p_flags |= PF_R;
30fe1832
AM
5733 p->p_filesz += actual * bed->s->sizeof_phdr;
5734 p->p_memsz += actual * bed->s->sizeof_phdr;
f3520d2f 5735 if (!m->includes_filehdr)
252b5132 5736 {
30fe1832 5737 if (p->p_type == PT_LOAD)
252b5132 5738 {
30fe1832
AM
5739 elf_elfheader (abfd)->e_phoff = p->p_offset;
5740 if (m->count > 0)
5741 {
5742 p->p_vaddr -= off - p->p_offset;
5743 if (!m->p_paddr_valid)
5744 p->p_paddr -= off - p->p_offset;
5745 }
5746 }
5747 else if (phdr_load_seg != NULL)
5748 {
5749 Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
502794d4 5750 bfd_vma phdr_off = 0; /* Octets. */
30fe1832
AM
5751 if (phdr_load_seg->includes_filehdr)
5752 phdr_off = bed->s->sizeof_ehdr;
5753 p->p_vaddr = phdr->p_vaddr + phdr_off;
bf988460 5754 if (!m->p_paddr_valid)
30fe1832
AM
5755 p->p_paddr = phdr->p_paddr + phdr_off;
5756 p->p_offset = phdr->p_offset + phdr_off;
252b5132 5757 }
30fe1832
AM
5758 else
5759 p->p_offset = bed->s->sizeof_ehdr;
2b0bc088 5760 }
252b5132
RH
5761 }
5762
5763 if (p->p_type == PT_LOAD
5764 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5765 {
bf988460 5766 if (!m->includes_filehdr && !m->includes_phdrs)
0bc3450e
AM
5767 {
5768 p->p_offset = off;
5769 if (no_contents)
67641dd3
AM
5770 {
5771 /* Put meaningless p_offset for PT_LOAD segments
5772 without file contents somewhere within the first
5773 page, in an attempt to not point past EOF. */
5774 bfd_size_type align = maxpagesize;
5775 if (align < p->p_align)
5776 align = p->p_align;
5777 if (align < 1)
5778 align = 1;
5779 p->p_offset = off % align;
5780 }
0bc3450e 5781 }
252b5132
RH
5782 else
5783 {
502794d4 5784 file_ptr adjust; /* Octets. */
252b5132
RH
5785
5786 adjust = off - (p->p_offset + p->p_filesz);
bf988460
AM
5787 if (!no_contents)
5788 p->p_filesz += adjust;
252b5132
RH
5789 p->p_memsz += adjust;
5790 }
5791 }
5792
1ea63fd2
AM
5793 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5794 maps. Set filepos for sections in PT_LOAD segments, and in
5795 core files, for sections in PT_NOTE segments.
5796 assign_file_positions_for_non_load_sections will set filepos
5797 for other sections and update p_filesz for other segments. */
252b5132
RH
5798 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5799 {
5800 asection *sec;
252b5132 5801 bfd_size_type align;
627b32bc 5802 Elf_Internal_Shdr *this_hdr;
252b5132
RH
5803
5804 sec = *secpp;
02bf8d82 5805 this_hdr = &elf_section_data (sec)->this_hdr;
fd361982 5806 align = (bfd_size_type) 1 << bfd_section_alignment (sec);
252b5132 5807
88967714
AM
5808 if ((p->p_type == PT_LOAD
5809 || p->p_type == PT_TLS)
5810 && (this_hdr->sh_type != SHT_NOBITS
5811 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5812 && ((this_hdr->sh_flags & SHF_TLS) == 0
5813 || p->p_type == PT_TLS))))
252b5132 5814 {
502794d4
CE
5815 bfd_vma p_start = p->p_paddr; /* Octets. */
5816 bfd_vma p_end = p_start + p->p_memsz; /* Octets. */
5817 bfd_vma s_start = sec->lma * opb; /* Octets. */
5818 bfd_vma adjust = s_start - p_end; /* Octets. */
252b5132 5819
a2d1e028
L
5820 if (adjust != 0
5821 && (s_start < p_end
5822 || p_end < p_start))
252b5132 5823 {
4eca0228 5824 _bfd_error_handler
695344c0 5825 /* xgettext:c-format */
2dcf00ce 5826 (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64),
502794d4
CE
5827 abfd, sec, (uint64_t) s_start / opb,
5828 (uint64_t) p_end / opb);
88967714 5829 adjust = 0;
502794d4 5830 sec->lma = p_end / opb;
1cfb7d1e 5831 }
3ac9b6c9 5832 p->p_memsz += adjust;
1cfb7d1e 5833
d16e3d2e 5834 if (p->p_type == PT_LOAD)
88967714 5835 {
d16e3d2e 5836 if (this_hdr->sh_type != SHT_NOBITS)
32812159 5837 {
d16e3d2e 5838 off_adjust = 0;
30fe1832
AM
5839 if (p->p_filesz + adjust < p->p_memsz)
5840 {
5841 /* We have a PROGBITS section following NOBITS ones.
5842 Allocate file space for the NOBITS section(s) and
5843 zero it. */
5844 adjust = p->p_memsz - p->p_filesz;
5845 if (!write_zeros (abfd, off, adjust))
5846 return FALSE;
5847 }
d16e3d2e
AM
5848 }
5849 /* We only adjust sh_offset in SHT_NOBITS sections
5850 as would seem proper for their address when the
5851 section is first in the segment. sh_offset
5852 doesn't really have any significance for
5853 SHT_NOBITS anyway, apart from a notional position
5854 relative to other sections. Historically we
5855 didn't bother with adjusting sh_offset and some
5856 programs depend on it not being adjusted. See
5857 pr12921 and pr25662. */
5858 if (this_hdr->sh_type != SHT_NOBITS || i == 0)
5859 {
30fe1832 5860 off += adjust;
d16e3d2e
AM
5861 if (this_hdr->sh_type == SHT_NOBITS)
5862 off_adjust += adjust;
32812159 5863 }
252b5132 5864 }
d16e3d2e
AM
5865 if (this_hdr->sh_type != SHT_NOBITS)
5866 p->p_filesz += adjust;
252b5132
RH
5867 }
5868
5869 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5870 {
b301b248
AM
5871 /* The section at i == 0 is the one that actually contains
5872 everything. */
4a938328
MS
5873 if (i == 0)
5874 {
627b32bc 5875 this_hdr->sh_offset = sec->filepos = off;
6a3cd2b4
AM
5876 off += this_hdr->sh_size;
5877 p->p_filesz = this_hdr->sh_size;
b301b248
AM
5878 p->p_memsz = 0;
5879 p->p_align = 1;
252b5132 5880 }
4a938328 5881 else
252b5132 5882 {
b301b248 5883 /* The rest are fake sections that shouldn't be written. */
252b5132 5884 sec->filepos = 0;
eea6121a 5885 sec->size = 0;
b301b248
AM
5886 sec->flags = 0;
5887 continue;
252b5132 5888 }
252b5132
RH
5889 }
5890 else
5891 {
1e951488 5892 if (p->p_type == PT_LOAD)
b301b248 5893 {
1e951488
AM
5894 this_hdr->sh_offset = sec->filepos = off;
5895 if (this_hdr->sh_type != SHT_NOBITS)
5896 off += this_hdr->sh_size;
5897 }
5898 else if (this_hdr->sh_type == SHT_NOBITS
5899 && (this_hdr->sh_flags & SHF_TLS) != 0
5900 && this_hdr->sh_offset == 0)
5901 {
5902 /* This is a .tbss section that didn't get a PT_LOAD.
5903 (See _bfd_elf_map_sections_to_segments "Create a
5904 final PT_LOAD".) Set sh_offset to the value it
5905 would have if we had created a zero p_filesz and
5906 p_memsz PT_LOAD header for the section. This
5907 also makes the PT_TLS header have the same
5908 p_offset value. */
5909 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5910 off, align);
5911 this_hdr->sh_offset = sec->filepos = off + adjust;
b301b248 5912 }
252b5132 5913
02bf8d82 5914 if (this_hdr->sh_type != SHT_NOBITS)
b301b248 5915 {
6a3cd2b4 5916 p->p_filesz += this_hdr->sh_size;
02bf8d82
AM
5917 /* A load section without SHF_ALLOC is something like
5918 a note section in a PT_NOTE segment. These take
5919 file space but are not loaded into memory. */
5920 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6a3cd2b4 5921 p->p_memsz += this_hdr->sh_size;
b301b248 5922 }
6a3cd2b4 5923 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
13ae64f3 5924 {
6a3cd2b4
AM
5925 if (p->p_type == PT_TLS)
5926 p->p_memsz += this_hdr->sh_size;
5927
5928 /* .tbss is special. It doesn't contribute to p_memsz of
5929 normal segments. */
5930 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
5931 p->p_memsz += this_hdr->sh_size;
13ae64f3
JJ
5932 }
5933
b10a8ae0
L
5934 if (align > p->p_align
5935 && !m->p_align_valid
5936 && (p->p_type != PT_LOAD
5937 || (abfd->flags & D_PAGED) == 0))
252b5132
RH
5938 p->p_align = align;
5939 }
5940
bf988460 5941 if (!m->p_flags_valid)
252b5132
RH
5942 {
5943 p->p_flags |= PF_R;
02bf8d82 5944 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
252b5132 5945 p->p_flags |= PF_X;
02bf8d82 5946 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
252b5132
RH
5947 p->p_flags |= PF_W;
5948 }
5949 }
43a8475c 5950
bf988460 5951 off -= off_adjust;
0920dee7 5952
30fe1832
AM
5953 /* PR ld/20815 - Check that the program header segment, if
5954 present, will be loaded into memory. */
5955 if (p->p_type == PT_PHDR
5956 && phdr_load_seg == NULL
5957 && !(bed->elf_backend_allow_non_load_phdr != NULL
5958 && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
5959 {
5960 /* The fix for this error is usually to edit the linker script being
5961 used and set up the program headers manually. Either that or
5962 leave room for the headers at the start of the SECTIONS. */
5963 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
5964 " by LOAD segment"),
5965 abfd);
7b3c2715
AM
5966 if (link_info == NULL)
5967 return FALSE;
5968 /* Arrange for the linker to exit with an error, deleting
5969 the output file unless --noinhibit-exec is given. */
5970 link_info->callbacks->info ("%X");
30fe1832
AM
5971 }
5972
7c928300
AM
5973 /* Check that all sections are in a PT_LOAD segment.
5974 Don't check funky gdb generated core files. */
5975 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
9a83a553
AM
5976 {
5977 bfd_boolean check_vma = TRUE;
5978
5979 for (i = 1; i < m->count; i++)
5980 if (m->sections[i]->vma == m->sections[i - 1]->vma
5981 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
5982 ->this_hdr), p) != 0
5983 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
5984 ->this_hdr), p) != 0)
0920dee7 5985 {
9a83a553
AM
5986 /* Looks like we have overlays packed into the segment. */
5987 check_vma = FALSE;
5988 break;
0920dee7 5989 }
9a83a553
AM
5990
5991 for (i = 0; i < m->count; i++)
5992 {
5993 Elf_Internal_Shdr *this_hdr;
5994 asection *sec;
5995
5996 sec = m->sections[i];
5997 this_hdr = &(elf_section_data(sec)->this_hdr);
86b2281f
AM
5998 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
5999 && !ELF_TBSS_SPECIAL (this_hdr, p))
9a83a553 6000 {
4eca0228 6001 _bfd_error_handler
695344c0 6002 /* xgettext:c-format */
871b3ab2 6003 (_("%pB: section `%pA' can't be allocated in segment %d"),
9a83a553
AM
6004 abfd, sec, j);
6005 print_segment_map (m);
6006 }
6007 }
6008 }
252b5132
RH
6009 }
6010
12bd6957 6011 elf_next_file_pos (abfd) = off;
30fe1832
AM
6012
6013 if (link_info != NULL
6014 && phdr_load_seg != NULL
6015 && phdr_load_seg->includes_filehdr)
6016 {
6017 /* There is a segment that contains both the file headers and the
6018 program headers, so provide a symbol __ehdr_start pointing there.
6019 A program can use this to examine itself robustly. */
6020
6021 struct elf_link_hash_entry *hash
6022 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
6023 FALSE, FALSE, TRUE);
6024 /* If the symbol was referenced and not defined, define it. */
6025 if (hash != NULL
6026 && (hash->root.type == bfd_link_hash_new
6027 || hash->root.type == bfd_link_hash_undefined
6028 || hash->root.type == bfd_link_hash_undefweak
6029 || hash->root.type == bfd_link_hash_common))
6030 {
6031 asection *s = NULL;
66631823 6032 bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
30fe1832
AM
6033
6034 if (phdr_load_seg->count != 0)
6035 /* The segment contains sections, so use the first one. */
6036 s = phdr_load_seg->sections[0];
6037 else
6038 /* Use the first (i.e. lowest-addressed) section in any segment. */
6039 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6040 if (m->p_type == PT_LOAD && m->count != 0)
6041 {
6042 s = m->sections[0];
6043 break;
6044 }
6045
6046 if (s != NULL)
6047 {
6048 hash->root.u.def.value = filehdr_vaddr - s->vma;
6049 hash->root.u.def.section = s;
6050 }
6051 else
6052 {
6053 hash->root.u.def.value = filehdr_vaddr;
6054 hash->root.u.def.section = bfd_abs_section_ptr;
6055 }
6056
6057 hash->root.type = bfd_link_hash_defined;
6058 hash->def_regular = 1;
6059 hash->non_elf = 0;
6060 }
6061 }
6062
f3520d2f
AM
6063 return TRUE;
6064}
6065
1faa385f
NC
6066/* Determine if a bfd is a debuginfo file. Unfortunately there
6067 is no defined method for detecting such files, so we have to
6068 use heuristics instead. */
6069
6070bfd_boolean
6071is_debuginfo_file (bfd *abfd)
6072{
6073 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6074 return FALSE;
6075
6076 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6077 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6078 Elf_Internal_Shdr **headerp;
6079
6080 for (headerp = start_headers; headerp < end_headers; headerp ++)
6081 {
6082 Elf_Internal_Shdr *header = * headerp;
6083
6084 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6085 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6086 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6087 && header->sh_type != SHT_NOBITS
6088 && header->sh_type != SHT_NOTE)
6089 return FALSE;
6090 }
6091
6092 return TRUE;
6093}
6094
1ff6de03
NA
6095/* Assign file positions for the other sections, except for compressed debugging
6096 and other sections assigned in _bfd_elf_assign_file_positions_for_non_load(). */
f3520d2f
AM
6097
6098static bfd_boolean
6099assign_file_positions_for_non_load_sections (bfd *abfd,
6100 struct bfd_link_info *link_info)
6101{
6102 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6103 Elf_Internal_Shdr **i_shdrpp;
e06efbf1 6104 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
f3520d2f
AM
6105 Elf_Internal_Phdr *phdrs;
6106 Elf_Internal_Phdr *p;
6107 struct elf_segment_map *m;
f3520d2f 6108 file_ptr off;
66631823 6109 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
f3520d2f 6110
5c182d5f 6111 i_shdrpp = elf_elfsections (abfd);
e06efbf1 6112 end_hdrpp = i_shdrpp + elf_numsections (abfd);
12bd6957 6113 off = elf_next_file_pos (abfd);
e06efbf1 6114 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
5c182d5f 6115 {
5c182d5f
AM
6116 Elf_Internal_Shdr *hdr;
6117
6118 hdr = *hdrpp;
6119 if (hdr->bfd_section != NULL
252e386e
AM
6120 && (hdr->bfd_section->filepos != 0
6121 || (hdr->sh_type == SHT_NOBITS
6122 && hdr->contents == NULL)))
627b32bc 6123 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
5c182d5f
AM
6124 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6125 {
1faa385f
NC
6126 if (hdr->sh_size != 0
6127 /* PR 24717 - debuginfo files are known to be not strictly
6128 compliant with the ELF standard. In particular they often
6129 have .note.gnu.property sections that are outside of any
6130 loadable segment. This is not a problem for such files,
6131 so do not warn about them. */
6132 && ! is_debuginfo_file (abfd))
4eca0228 6133 _bfd_error_handler
695344c0 6134 /* xgettext:c-format */
871b3ab2 6135 (_("%pB: warning: allocated section `%s' not in segment"),
e8d2ba53
AM
6136 abfd,
6137 (hdr->bfd_section == NULL
6138 ? "*unknown*"
6139 : hdr->bfd_section->name));
3ba71138
L
6140 /* We don't need to page align empty sections. */
6141 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
5c182d5f
AM
6142 off += vma_page_aligned_bias (hdr->sh_addr, off,
6143 bed->maxpagesize);
6144 else
6145 off += vma_page_aligned_bias (hdr->sh_addr, off,
6146 hdr->sh_addralign);
6147 off = _bfd_elf_assign_file_position_for_section (hdr, off,
6148 FALSE);
6149 }
6150 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6151 && hdr->bfd_section == NULL)
1ff6de03
NA
6152 /* We don't know the offset of these sections yet: their size has
6153 not been decided. */
0ce398f1 6154 || (hdr->bfd_section != NULL
1ff6de03
NA
6155 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
6156 || (bfd_section_is_ctf (hdr->bfd_section)
6157 && abfd->is_linker_output)))
12bd6957 6158 || hdr == i_shdrpp[elf_onesymtab (abfd)]
6a40cf0c
NC
6159 || (elf_symtab_shndx_list (abfd) != NULL
6160 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
3e19fb8f
L
6161 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6162 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
5c182d5f
AM
6163 hdr->sh_offset = -1;
6164 else
6165 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5c182d5f 6166 }
30fe1832 6167 elf_next_file_pos (abfd) = off;
5c182d5f 6168
252b5132
RH
6169 /* Now that we have set the section file positions, we can set up
6170 the file positions for the non PT_LOAD segments. */
f3520d2f 6171 phdrs = elf_tdata (abfd)->phdr;
12bd6957 6172 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
252b5132 6173 {
129af99f 6174 if (p->p_type == PT_GNU_RELRO)
252b5132 6175 {
66631823 6176 bfd_vma start, end; /* Bytes. */
01f7e10c 6177 bfd_boolean ok;
1ea63fd2 6178
129af99f 6179 if (link_info != NULL)
8c37241b 6180 {
129af99f 6181 /* During linking the range of the RELRO segment is passed
f2731e0c
AM
6182 in link_info. Note that there may be padding between
6183 relro_start and the first RELRO section. */
6184 start = link_info->relro_start;
6185 end = link_info->relro_end;
6186 }
6187 else if (m->count != 0)
6188 {
6189 if (!m->p_size_valid)
6190 abort ();
6191 start = m->sections[0]->vma;
66631823 6192 end = start + m->p_size / opb;
f2731e0c
AM
6193 }
6194 else
6195 {
6196 start = 0;
6197 end = 0;
6198 }
6199
01f7e10c 6200 ok = FALSE;
f2731e0c
AM
6201 if (start < end)
6202 {
6203 struct elf_segment_map *lm;
6204 const Elf_Internal_Phdr *lp;
6205 unsigned int i;
6206
6207 /* Find a LOAD segment containing a section in the RELRO
6208 segment. */
12bd6957 6209 for (lm = elf_seg_map (abfd), lp = phdrs;
3146fac4
AM
6210 lm != NULL;
6211 lm = lm->next, lp++)
8c37241b
JJ
6212 {
6213 if (lp->p_type == PT_LOAD
3146fac4 6214 && lm->count != 0
dbc88fc1
AM
6215 && (lm->sections[lm->count - 1]->vma
6216 + (!IS_TBSS (lm->sections[lm->count - 1])
66631823 6217 ? lm->sections[lm->count - 1]->size / opb
dbc88fc1 6218 : 0)) > start
f2731e0c 6219 && lm->sections[0]->vma < end)
8c37241b
JJ
6220 break;
6221 }
f2731e0c 6222
01f7e10c 6223 if (lm != NULL)
129af99f 6224 {
01f7e10c
AM
6225 /* Find the section starting the RELRO segment. */
6226 for (i = 0; i < lm->count; i++)
6227 {
6228 asection *s = lm->sections[i];
6229 if (s->vma >= start
6230 && s->vma < end
6231 && s->size != 0)
6232 break;
6233 }
6234
6235 if (i < lm->count)
6236 {
502794d4
CE
6237 p->p_vaddr = lm->sections[i]->vma * opb;
6238 p->p_paddr = lm->sections[i]->lma * opb;
01f7e10c 6239 p->p_offset = lm->sections[i]->filepos;
66631823 6240 p->p_memsz = end * opb - p->p_vaddr;
01f7e10c
AM
6241 p->p_filesz = p->p_memsz;
6242
6243 /* The RELRO segment typically ends a few bytes
6244 into .got.plt but other layouts are possible.
6245 In cases where the end does not match any
6246 loaded section (for instance is in file
6247 padding), trim p_filesz back to correspond to
6248 the end of loaded section contents. */
6249 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6250 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6251
6252 /* Preserve the alignment and flags if they are
6253 valid. The gold linker generates RW/4 for
6254 the PT_GNU_RELRO section. It is better for
6255 objcopy/strip to honor these attributes
6256 otherwise gdb will choke when using separate
6257 debug files. */
6258 if (!m->p_align_valid)
6259 p->p_align = 1;
6260 if (!m->p_flags_valid)
6261 p->p_flags = PF_R;
6262 ok = TRUE;
6263 }
129af99f 6264 }
b84a33b5 6265 }
01f7e10c
AM
6266 if (link_info != NULL)
6267 BFD_ASSERT (ok);
6268 if (!ok)
6269 memset (p, 0, sizeof *p);
129af99f 6270 }
04c3a755
NS
6271 else if (p->p_type == PT_GNU_STACK)
6272 {
6273 if (m->p_size_valid)
6274 p->p_memsz = m->p_size;
6275 }
129af99f
AS
6276 else if (m->count != 0)
6277 {
e06efbf1 6278 unsigned int i;
1a9ccd70 6279
129af99f
AS
6280 if (p->p_type != PT_LOAD
6281 && (p->p_type != PT_NOTE
6282 || bfd_get_format (abfd) != bfd_core))
6283 {
1a9ccd70
NC
6284 /* A user specified segment layout may include a PHDR
6285 segment that overlaps with a LOAD segment... */
6286 if (p->p_type == PT_PHDR)
6287 {
6288 m->count = 0;
6289 continue;
6290 }
6291
c86934ce
NC
6292 if (m->includes_filehdr || m->includes_phdrs)
6293 {
b1fa9dd6 6294 /* PR 17512: file: 2195325e. */
4eca0228 6295 _bfd_error_handler
871b3ab2 6296 (_("%pB: error: non-load segment %d includes file header "
76cfced5
AM
6297 "and/or program header"),
6298 abfd, (int) (p - phdrs));
c86934ce
NC
6299 return FALSE;
6300 }
129af99f 6301
86b2281f 6302 p->p_filesz = 0;
129af99f 6303 p->p_offset = m->sections[0]->filepos;
86b2281f
AM
6304 for (i = m->count; i-- != 0;)
6305 {
6306 asection *sect = m->sections[i];
6307 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6308 if (hdr->sh_type != SHT_NOBITS)
6309 {
6310 p->p_filesz = (sect->filepos - m->sections[0]->filepos
6311 + hdr->sh_size);
6312 break;
6313 }
6314 }
129af99f
AS
6315 }
6316 }
252b5132
RH
6317 }
6318
b34976b6 6319 return TRUE;
252b5132
RH
6320}
6321
6a40cf0c
NC
6322static elf_section_list *
6323find_section_in_list (unsigned int i, elf_section_list * list)
6324{
6325 for (;list != NULL; list = list->next)
6326 if (list->ndx == i)
6327 break;
6328 return list;
6329}
6330
252b5132
RH
6331/* Work out the file positions of all the sections. This is called by
6332 _bfd_elf_compute_section_file_positions. All the section sizes and
6333 VMAs must be known before this is called.
6334
e0638f70 6335 Reloc sections come in two flavours: Those processed specially as
1ff6de03
NA
6336 "side-channel" data attached to a section to which they apply, and those that
6337 bfd doesn't process as relocations. The latter sort are stored in a normal
6338 bfd section by bfd_section_from_shdr. We don't consider the former sort
6339 here, unless they form part of the loadable image. Reloc sections not
6340 assigned here (and compressed debugging sections and CTF sections which
6341 nothing else in the file can rely upon) will be handled later by
e0638f70 6342 assign_file_positions_for_relocs.
252b5132
RH
6343
6344 We also don't set the positions of the .symtab and .strtab here. */
6345
b34976b6 6346static bfd_boolean
c84fca4d
AO
6347assign_file_positions_except_relocs (bfd *abfd,
6348 struct bfd_link_info *link_info)
252b5132 6349{
5c182d5f
AM
6350 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6351 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
9c5bfbb7 6352 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6d6c25c8 6353 unsigned int alloc;
252b5132
RH
6354
6355 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6356 && bfd_get_format (abfd) != bfd_core)
6357 {
5c182d5f
AM
6358 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6359 unsigned int num_sec = elf_numsections (abfd);
252b5132
RH
6360 Elf_Internal_Shdr **hdrpp;
6361 unsigned int i;
a485e98e 6362 file_ptr off;
252b5132
RH
6363
6364 /* Start after the ELF header. */
6365 off = i_ehdrp->e_ehsize;
6366
6367 /* We are not creating an executable, which means that we are
6368 not creating a program header, and that the actual order of
6369 the sections in the file is unimportant. */
9ad5cbcf 6370 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
252b5132
RH
6371 {
6372 Elf_Internal_Shdr *hdr;
6373
6374 hdr = *hdrpp;
e0638f70
AM
6375 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6376 && hdr->bfd_section == NULL)
1ff6de03
NA
6377 /* Do not assign offsets for these sections yet: we don't know
6378 their sizes. */
0ce398f1 6379 || (hdr->bfd_section != NULL
1ff6de03
NA
6380 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
6381 || (bfd_section_is_ctf (hdr->bfd_section)
6382 && abfd->is_linker_output)))
12bd6957 6383 || i == elf_onesymtab (abfd)
6a40cf0c
NC
6384 || (elf_symtab_shndx_list (abfd) != NULL
6385 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
3e19fb8f
L
6386 || i == elf_strtab_sec (abfd)
6387 || i == elf_shstrtab_sec (abfd))
252b5132
RH
6388 {
6389 hdr->sh_offset = -1;
252b5132 6390 }
9ad5cbcf 6391 else
b34976b6 6392 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
252b5132 6393 }
a485e98e
AM
6394
6395 elf_next_file_pos (abfd) = off;
6d6c25c8 6396 elf_program_header_size (abfd) = 0;
252b5132
RH
6397 }
6398 else
6399 {
252b5132 6400 /* Assign file positions for the loaded sections based on the
08a40648 6401 assignment of sections to segments. */
f3520d2f
AM
6402 if (!assign_file_positions_for_load_sections (abfd, link_info))
6403 return FALSE;
6404
6405 /* And for non-load sections. */
6406 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6407 return FALSE;
6d6c25c8 6408 }
f3520d2f 6409
6d6c25c8
AM
6410 if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
6411 return FALSE;
1a9ccd70 6412
6d6c25c8
AM
6413 /* Write out the program headers. */
6414 alloc = i_ehdrp->e_phnum;
6415 if (alloc != 0)
6416 {
30fe1832 6417 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
cd584857
NC
6418 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6419 return FALSE;
252b5132
RH
6420 }
6421
b34976b6 6422 return TRUE;
252b5132
RH
6423}
6424
ed7e9d0b
AM
6425bfd_boolean
6426_bfd_elf_init_file_header (bfd *abfd,
6427 struct bfd_link_info *info ATTRIBUTE_UNUSED)
252b5132 6428{
3d540e93 6429 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
2b0f7ef9 6430 struct elf_strtab_hash *shstrtab;
9c5bfbb7 6431 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
6432
6433 i_ehdrp = elf_elfheader (abfd);
252b5132 6434
2b0f7ef9 6435 shstrtab = _bfd_elf_strtab_init ();
252b5132 6436 if (shstrtab == NULL)
b34976b6 6437 return FALSE;
252b5132
RH
6438
6439 elf_shstrtab (abfd) = shstrtab;
6440
6441 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6442 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6443 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6444 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6445
6446 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6447 i_ehdrp->e_ident[EI_DATA] =
6448 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6449 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6450
252b5132
RH
6451 if ((abfd->flags & DYNAMIC) != 0)
6452 i_ehdrp->e_type = ET_DYN;
6453 else if ((abfd->flags & EXEC_P) != 0)
6454 i_ehdrp->e_type = ET_EXEC;
6455 else if (bfd_get_format (abfd) == bfd_core)
6456 i_ehdrp->e_type = ET_CORE;
6457 else
6458 i_ehdrp->e_type = ET_REL;
6459
6460 switch (bfd_get_arch (abfd))
6461 {
6462 case bfd_arch_unknown:
6463 i_ehdrp->e_machine = EM_NONE;
6464 break;
aa4f99bb
AO
6465
6466 /* There used to be a long list of cases here, each one setting
6467 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6468 in the corresponding bfd definition. To avoid duplication,
6469 the switch was removed. Machines that need special handling
6470 can generally do it in elf_backend_final_write_processing(),
6471 unless they need the information earlier than the final write.
6472 Such need can generally be supplied by replacing the tests for
6473 e_machine with the conditions used to determine it. */
252b5132 6474 default:
9c5bfbb7
AM
6475 i_ehdrp->e_machine = bed->elf_machine_code;
6476 }
aa4f99bb 6477
252b5132
RH
6478 i_ehdrp->e_version = bed->s->ev_current;
6479 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6480
c044fabd 6481 /* No program header, for now. */
252b5132
RH
6482 i_ehdrp->e_phoff = 0;
6483 i_ehdrp->e_phentsize = 0;
6484 i_ehdrp->e_phnum = 0;
6485
c044fabd 6486 /* Each bfd section is section header entry. */
252b5132
RH
6487 i_ehdrp->e_entry = bfd_get_start_address (abfd);
6488 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6489
252b5132 6490 elf_tdata (abfd)->symtab_hdr.sh_name =
b34976b6 6491 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
252b5132 6492 elf_tdata (abfd)->strtab_hdr.sh_name =
b34976b6 6493 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
252b5132 6494 elf_tdata (abfd)->shstrtab_hdr.sh_name =
b34976b6 6495 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
252b5132 6496 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
17ca87fc 6497 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
252b5132 6498 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
b34976b6 6499 return FALSE;
252b5132 6500
b34976b6 6501 return TRUE;
252b5132
RH
6502}
6503
6d6c25c8
AM
6504/* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
6505
6506 FIXME: We used to have code here to sort the PT_LOAD segments into
6507 ascending order, as per the ELF spec. But this breaks some programs,
6508 including the Linux kernel. But really either the spec should be
6509 changed or the programs updated. */
6510
6511bfd_boolean
6512_bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
6513{
6514 if (link_info != NULL && bfd_link_pie (link_info))
6515 {
6516 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
6517 unsigned int num_segments = i_ehdrp->e_phnum;
6518 struct elf_obj_tdata *tdata = elf_tdata (obfd);
6519 Elf_Internal_Phdr *segment = tdata->phdr;
6520 Elf_Internal_Phdr *end_segment = &segment[num_segments];
6521
6522 /* Find the lowest p_vaddr in PT_LOAD segments. */
6523 bfd_vma p_vaddr = (bfd_vma) -1;
6524 for (; segment < end_segment; segment++)
6525 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
6526 p_vaddr = segment->p_vaddr;
6527
6528 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6529 segments is non-zero. */
6530 if (p_vaddr)
6531 i_ehdrp->e_type = ET_EXEC;
6532 }
6533 return TRUE;
6534}
6535
252b5132 6536/* Assign file positions for all the reloc sections which are not part
a485e98e 6537 of the loadable file image, and the file position of section headers. */
252b5132 6538
0ce398f1
L
6539static bfd_boolean
6540_bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
252b5132
RH
6541{
6542 file_ptr off;
e06efbf1 6543 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
3e19fb8f 6544 Elf_Internal_Shdr *shdrp;
a485e98e
AM
6545 Elf_Internal_Ehdr *i_ehdrp;
6546 const struct elf_backend_data *bed;
252b5132 6547
12bd6957 6548 off = elf_next_file_pos (abfd);
252b5132 6549
e06efbf1
L
6550 shdrpp = elf_elfsections (abfd);
6551 end_shdrpp = shdrpp + elf_numsections (abfd);
6552 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
252b5132 6553 {
252b5132 6554 shdrp = *shdrpp;
0ce398f1
L
6555 if (shdrp->sh_offset == -1)
6556 {
3e19fb8f 6557 asection *sec = shdrp->bfd_section;
0ce398f1
L
6558 bfd_boolean is_rel = (shdrp->sh_type == SHT_REL
6559 || shdrp->sh_type == SHT_RELA);
1ff6de03 6560 bfd_boolean is_ctf = sec && bfd_section_is_ctf (sec);
0ce398f1 6561 if (is_rel
1ff6de03 6562 || is_ctf
3e19fb8f 6563 || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
0ce398f1 6564 {
1ff6de03 6565 if (!is_rel && !is_ctf)
0ce398f1 6566 {
3e19fb8f
L
6567 const char *name = sec->name;
6568 struct bfd_elf_section_data *d;
6569
0ce398f1 6570 /* Compress DWARF debug sections. */
3e19fb8f 6571 if (!bfd_compress_section (abfd, sec,
0ce398f1
L
6572 shdrp->contents))
6573 return FALSE;
3e19fb8f
L
6574
6575 if (sec->compress_status == COMPRESS_SECTION_DONE
6576 && (abfd->flags & BFD_COMPRESS_GABI) == 0)
6577 {
6578 /* If section is compressed with zlib-gnu, convert
6579 section name from .debug_* to .zdebug_*. */
6580 char *new_name
6581 = convert_debug_to_zdebug (abfd, name);
6582 if (new_name == NULL)
6583 return FALSE;
6584 name = new_name;
6585 }
dd905818 6586 /* Add section name to section name section. */
3e19fb8f
L
6587 if (shdrp->sh_name != (unsigned int) -1)
6588 abort ();
6589 shdrp->sh_name
6590 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
6591 name, FALSE);
6592 d = elf_section_data (sec);
6593
dd905818 6594 /* Add reloc section name to section name section. */
3e19fb8f
L
6595 if (d->rel.hdr
6596 && !_bfd_elf_set_reloc_sh_name (abfd,
6597 d->rel.hdr,
6598 name, FALSE))
6599 return FALSE;
6600 if (d->rela.hdr
6601 && !_bfd_elf_set_reloc_sh_name (abfd,
6602 d->rela.hdr,
91cb26da 6603 name, TRUE))
3e19fb8f
L
6604 return FALSE;
6605
0ce398f1 6606 /* Update section size and contents. */
3e19fb8f
L
6607 shdrp->sh_size = sec->size;
6608 shdrp->contents = sec->contents;
0ce398f1
L
6609 shdrp->bfd_section->contents = NULL;
6610 }
1ff6de03
NA
6611 else if (is_ctf)
6612 {
6613 /* Update section size and contents. */
6614 shdrp->sh_size = sec->size;
6615 shdrp->contents = sec->contents;
6616 }
6617
0ce398f1
L
6618 off = _bfd_elf_assign_file_position_for_section (shdrp,
6619 off,
6620 TRUE);
6621 }
6622 }
252b5132
RH
6623 }
6624
3e19fb8f
L
6625 /* Place section name section after DWARF debug sections have been
6626 compressed. */
6627 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
6628 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
6629 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
6630 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
6631
6632 /* Place the section headers. */
a485e98e
AM
6633 i_ehdrp = elf_elfheader (abfd);
6634 bed = get_elf_backend_data (abfd);
6635 off = align_file_position (off, 1 << bed->s->log_file_align);
6636 i_ehdrp->e_shoff = off;
6637 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
12bd6957 6638 elf_next_file_pos (abfd) = off;
0ce398f1
L
6639
6640 return TRUE;
252b5132
RH
6641}
6642
b34976b6 6643bfd_boolean
217aa764 6644_bfd_elf_write_object_contents (bfd *abfd)
252b5132 6645{
9c5bfbb7 6646 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 6647 Elf_Internal_Shdr **i_shdrp;
b34976b6 6648 bfd_boolean failed;
9ad5cbcf 6649 unsigned int count, num_sec;
30e8ee25 6650 struct elf_obj_tdata *t;
252b5132
RH
6651
6652 if (! abfd->output_has_begun
217aa764 6653 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
b34976b6 6654 return FALSE;
db727370
JL
6655 /* Do not rewrite ELF data when the BFD has been opened for update.
6656 abfd->output_has_begun was set to TRUE on opening, so creation of new
6657 sections, and modification of existing section sizes was restricted.
6658 This means the ELF header, program headers and section headers can't have
6659 changed.
6660 If the contents of any sections has been modified, then those changes have
6661 already been written to the BFD. */
6662 else if (abfd->direction == both_direction)
6663 {
6664 BFD_ASSERT (abfd->output_has_begun);
6665 return TRUE;
6666 }
252b5132
RH
6667
6668 i_shdrp = elf_elfsections (abfd);
252b5132 6669
b34976b6 6670 failed = FALSE;
252b5132
RH
6671 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
6672 if (failed)
b34976b6 6673 return FALSE;
252b5132 6674
0ce398f1
L
6675 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
6676 return FALSE;
252b5132 6677
c044fabd 6678 /* After writing the headers, we need to write the sections too... */
9ad5cbcf
AM
6679 num_sec = elf_numsections (abfd);
6680 for (count = 1; count < num_sec; count++)
252b5132 6681 {
3e19fb8f
L
6682 i_shdrp[count]->sh_name
6683 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
6684 i_shdrp[count]->sh_name);
252b5132 6685 if (bed->elf_backend_section_processing)
75506100
MR
6686 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
6687 return FALSE;
252b5132
RH
6688 if (i_shdrp[count]->contents)
6689 {
dc810e39
AM
6690 bfd_size_type amt = i_shdrp[count]->sh_size;
6691
252b5132 6692 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
dc810e39 6693 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
b34976b6 6694 return FALSE;
252b5132
RH
6695 }
6696 }
6697
6698 /* Write out the section header names. */
30e8ee25 6699 t = elf_tdata (abfd);
26ae6d5e 6700 if (elf_shstrtab (abfd) != NULL
30e8ee25 6701 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
08a40648 6702 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
b34976b6 6703 return FALSE;
252b5132 6704
cc364be6
AM
6705 if (!(*bed->elf_backend_final_write_processing) (abfd))
6706 return FALSE;
252b5132 6707
ff59fc36
RM
6708 if (!bed->s->write_shdrs_and_ehdr (abfd))
6709 return FALSE;
6710
6711 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
c0355132
AM
6712 if (t->o->build_id.after_write_object_contents != NULL)
6713 return (*t->o->build_id.after_write_object_contents) (abfd);
ff59fc36
RM
6714
6715 return TRUE;
252b5132
RH
6716}
6717
b34976b6 6718bfd_boolean
217aa764 6719_bfd_elf_write_corefile_contents (bfd *abfd)
252b5132 6720{
c044fabd 6721 /* Hopefully this can be done just like an object file. */
252b5132
RH
6722 return _bfd_elf_write_object_contents (abfd);
6723}
c044fabd
KH
6724
6725/* Given a section, search the header to find them. */
6726
cb33740c 6727unsigned int
198beae2 6728_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
252b5132 6729{
9c5bfbb7 6730 const struct elf_backend_data *bed;
91d6fa6a 6731 unsigned int sec_index;
252b5132 6732
9ad5cbcf
AM
6733 if (elf_section_data (asect) != NULL
6734 && elf_section_data (asect)->this_idx != 0)
6735 return elf_section_data (asect)->this_idx;
6736
6737 if (bfd_is_abs_section (asect))
91d6fa6a 6738 sec_index = SHN_ABS;
af746e92 6739 else if (bfd_is_com_section (asect))
91d6fa6a 6740 sec_index = SHN_COMMON;
af746e92 6741 else if (bfd_is_und_section (asect))
91d6fa6a 6742 sec_index = SHN_UNDEF;
af746e92 6743 else
91d6fa6a 6744 sec_index = SHN_BAD;
252b5132 6745
af746e92 6746 bed = get_elf_backend_data (abfd);
252b5132
RH
6747 if (bed->elf_backend_section_from_bfd_section)
6748 {
91d6fa6a 6749 int retval = sec_index;
9ad5cbcf 6750
af746e92
AM
6751 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6752 return retval;
252b5132
RH
6753 }
6754
91d6fa6a 6755 if (sec_index == SHN_BAD)
af746e92 6756 bfd_set_error (bfd_error_nonrepresentable_section);
252b5132 6757
91d6fa6a 6758 return sec_index;
252b5132
RH
6759}
6760
6761/* Given a BFD symbol, return the index in the ELF symbol table, or -1
6762 on error. */
6763
6764int
217aa764 6765_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
252b5132
RH
6766{
6767 asymbol *asym_ptr = *asym_ptr_ptr;
6768 int idx;
6769 flagword flags = asym_ptr->flags;
6770
6771 /* When gas creates relocations against local labels, it creates its
6772 own symbol for the section, but does put the symbol into the
6773 symbol chain, so udata is 0. When the linker is generating
6774 relocatable output, this section symbol may be for one of the
6775 input sections rather than the output section. */
6776 if (asym_ptr->udata.i == 0
6777 && (flags & BSF_SECTION_SYM)
6778 && asym_ptr->section)
6779 {
5372391b 6780 asection *sec;
252b5132
RH
6781 int indx;
6782
5372391b
AM
6783 sec = asym_ptr->section;
6784 if (sec->owner != abfd && sec->output_section != NULL)
6785 sec = sec->output_section;
6786 if (sec->owner == abfd
6787 && (indx = sec->index) < elf_num_section_syms (abfd)
4e89ac30 6788 && elf_section_syms (abfd)[indx] != NULL)
252b5132
RH
6789 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
6790 }
6791
6792 idx = asym_ptr->udata.i;
6793
6794 if (idx == 0)
6795 {
6796 /* This case can occur when using --strip-symbol on a symbol
08a40648 6797 which is used in a relocation entry. */
4eca0228 6798 _bfd_error_handler
695344c0 6799 /* xgettext:c-format */
871b3ab2 6800 (_("%pB: symbol `%s' required but not present"),
d003868e 6801 abfd, bfd_asymbol_name (asym_ptr));
252b5132
RH
6802 bfd_set_error (bfd_error_no_symbols);
6803 return -1;
6804 }
6805
6806#if DEBUG & 4
6807 {
6808 fprintf (stderr,
cd9af601
AM
6809 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6810 (long) asym_ptr, asym_ptr->name, idx, flags);
252b5132
RH
6811 fflush (stderr);
6812 }
6813#endif
6814
6815 return idx;
6816}
6817
84d1d650 6818/* Rewrite program header information. */
252b5132 6819
b34976b6 6820static bfd_boolean
84d1d650 6821rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
252b5132 6822{
b34976b6
AM
6823 Elf_Internal_Ehdr *iehdr;
6824 struct elf_segment_map *map;
6825 struct elf_segment_map *map_first;
6826 struct elf_segment_map **pointer_to_map;
6827 Elf_Internal_Phdr *segment;
6828 asection *section;
6829 unsigned int i;
6830 unsigned int num_segments;
6831 bfd_boolean phdr_included = FALSE;
5c44b38e 6832 bfd_boolean p_paddr_valid;
b34976b6
AM
6833 bfd_vma maxpagesize;
6834 struct elf_segment_map *phdr_adjust_seg = NULL;
6835 unsigned int phdr_adjust_num = 0;
9c5bfbb7 6836 const struct elf_backend_data *bed;
502794d4 6837 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
bc67d8a6 6838
caf47ea6 6839 bed = get_elf_backend_data (ibfd);
252b5132
RH
6840 iehdr = elf_elfheader (ibfd);
6841
bc67d8a6 6842 map_first = NULL;
c044fabd 6843 pointer_to_map = &map_first;
252b5132
RH
6844
6845 num_segments = elf_elfheader (ibfd)->e_phnum;
bc67d8a6
NC
6846 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
6847
6848 /* Returns the end address of the segment + 1. */
aecc8f8a
AM
6849#define SEGMENT_END(segment, start) \
6850 (start + (segment->p_memsz > segment->p_filesz \
6851 ? segment->p_memsz : segment->p_filesz))
bc67d8a6 6852
eecdbe52
JJ
6853#define SECTION_SIZE(section, segment) \
6854 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6855 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
eea6121a 6856 ? section->size : 0)
eecdbe52 6857
b34976b6 6858 /* Returns TRUE if the given section is contained within
bc67d8a6 6859 the given segment. VMA addresses are compared. */
502794d4
CE
6860#define IS_CONTAINED_BY_VMA(section, segment, opb) \
6861 (section->vma * (opb) >= segment->p_vaddr \
6862 && (section->vma * (opb) + SECTION_SIZE (section, segment) \
aecc8f8a 6863 <= (SEGMENT_END (segment, segment->p_vaddr))))
c044fabd 6864
b34976b6 6865 /* Returns TRUE if the given section is contained within
bc67d8a6 6866 the given segment. LMA addresses are compared. */
502794d4
CE
6867#define IS_CONTAINED_BY_LMA(section, segment, base, opb) \
6868 (section->lma * (opb) >= base \
6869 && (section->lma + SECTION_SIZE (section, segment) / (opb) >= section->lma) \
6870 && (section->lma * (opb) + SECTION_SIZE (section, segment) \
aecc8f8a 6871 <= SEGMENT_END (segment, base)))
252b5132 6872
0efc80c8
L
6873 /* Handle PT_NOTE segment. */
6874#define IS_NOTE(p, s) \
aecc8f8a 6875 (p->p_type == PT_NOTE \
0efc80c8 6876 && elf_section_type (s) == SHT_NOTE \
aecc8f8a 6877 && (bfd_vma) s->filepos >= p->p_offset \
cb3ff1e5 6878 && ((bfd_vma) s->filepos + s->size \
aecc8f8a 6879 <= p->p_offset + p->p_filesz))
252b5132 6880
0efc80c8
L
6881 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6882 etc. */
6883#define IS_COREFILE_NOTE(p, s) \
6884 (IS_NOTE (p, s) \
6885 && bfd_get_format (ibfd) == bfd_core \
6886 && s->vma == 0 \
6887 && s->lma == 0)
6888
252b5132
RH
6889 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6890 linker, which generates a PT_INTERP section with p_vaddr and
6891 p_memsz set to 0. */
aecc8f8a
AM
6892#define IS_SOLARIS_PT_INTERP(p, s) \
6893 (p->p_vaddr == 0 \
6894 && p->p_paddr == 0 \
6895 && p->p_memsz == 0 \
6896 && p->p_filesz > 0 \
6897 && (s->flags & SEC_HAS_CONTENTS) != 0 \
eea6121a 6898 && s->size > 0 \
aecc8f8a 6899 && (bfd_vma) s->filepos >= p->p_offset \
cb3ff1e5 6900 && ((bfd_vma) s->filepos + s->size \
aecc8f8a 6901 <= p->p_offset + p->p_filesz))
5c440b1e 6902
bc67d8a6
NC
6903 /* Decide if the given section should be included in the given segment.
6904 A section will be included if:
f5ffc919 6905 1. It is within the address space of the segment -- we use the LMA
08a40648 6906 if that is set for the segment and the VMA otherwise,
0efc80c8 6907 2. It is an allocated section or a NOTE section in a PT_NOTE
d324f6d6 6908 segment.
bc67d8a6 6909 3. There is an output section associated with it,
eecdbe52 6910 4. The section has not already been allocated to a previous segment.
2b05f1b7 6911 5. PT_GNU_STACK segments do not include any sections.
03394ac9 6912 6. PT_TLS segment includes only SHF_TLS sections.
6f79b219
JJ
6913 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6914 8. PT_DYNAMIC should not contain empty sections at the beginning
08a40648 6915 (with the possible exception of .dynamic). */
502794d4 6916#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
2b05f1b7 6917 ((((segment->p_paddr \
502794d4
CE
6918 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr, opb) \
6919 : IS_CONTAINED_BY_VMA (section, segment, opb)) \
2b05f1b7 6920 && (section->flags & SEC_ALLOC) != 0) \
0efc80c8 6921 || IS_NOTE (segment, section)) \
2b05f1b7
L
6922 && segment->p_type != PT_GNU_STACK \
6923 && (segment->p_type != PT_TLS \
6924 || (section->flags & SEC_THREAD_LOCAL)) \
6925 && (segment->p_type == PT_LOAD \
6926 || segment->p_type == PT_TLS \
6927 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6928 && (segment->p_type != PT_DYNAMIC \
6929 || SECTION_SIZE (section, segment) > 0 \
6930 || (segment->p_paddr \
502794d4
CE
6931 ? segment->p_paddr != section->lma * (opb) \
6932 : segment->p_vaddr != section->vma * (opb)) \
fd361982 6933 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
9933dc52 6934 && (segment->p_type != PT_LOAD || !section->segment_mark))
bc67d8a6 6935
9f17e2a6
L
6936/* If the output section of a section in the input segment is NULL,
6937 it is removed from the corresponding output segment. */
502794d4
CE
6938#define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
6939 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
9f17e2a6
L
6940 && section->output_section != NULL)
6941
b34976b6 6942 /* Returns TRUE iff seg1 starts after the end of seg2. */
b5f852ea
NC
6943#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6944 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6945
6946 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6947 their VMA address ranges and their LMA address ranges overlap.
6948 It is possible to have overlapping VMA ranges without overlapping LMA
6949 ranges. RedBoot images for example can have both .data and .bss mapped
6950 to the same VMA range, but with the .data section mapped to a different
6951 LMA. */
aecc8f8a 6952#define SEGMENT_OVERLAPS(seg1, seg2) \
b5f852ea 6953 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
08a40648 6954 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
b5f852ea 6955 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
08a40648 6956 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
bc67d8a6
NC
6957
6958 /* Initialise the segment mark field. */
6959 for (section = ibfd->sections; section != NULL; section = section->next)
b34976b6 6960 section->segment_mark = FALSE;
bc67d8a6 6961
5c44b38e
AM
6962 /* The Solaris linker creates program headers in which all the
6963 p_paddr fields are zero. When we try to objcopy or strip such a
6964 file, we get confused. Check for this case, and if we find it
6965 don't set the p_paddr_valid fields. */
6966 p_paddr_valid = FALSE;
6967 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6968 i < num_segments;
6969 i++, segment++)
6970 if (segment->p_paddr != 0)
6971 {
6972 p_paddr_valid = TRUE;
6973 break;
6974 }
6975
252b5132 6976 /* Scan through the segments specified in the program header
bc67d8a6 6977 of the input BFD. For this first scan we look for overlaps
9ad5cbcf 6978 in the loadable segments. These can be created by weird
aecc8f8a 6979 parameters to objcopy. Also, fix some solaris weirdness. */
bc67d8a6
NC
6980 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6981 i < num_segments;
c044fabd 6982 i++, segment++)
252b5132 6983 {
252b5132 6984 unsigned int j;
c044fabd 6985 Elf_Internal_Phdr *segment2;
252b5132 6986
aecc8f8a
AM
6987 if (segment->p_type == PT_INTERP)
6988 for (section = ibfd->sections; section; section = section->next)
6989 if (IS_SOLARIS_PT_INTERP (segment, section))
6990 {
6991 /* Mininal change so that the normal section to segment
4cc11e76 6992 assignment code will work. */
502794d4 6993 segment->p_vaddr = section->vma * opb;
aecc8f8a
AM
6994 break;
6995 }
6996
bc67d8a6 6997 if (segment->p_type != PT_LOAD)
b10a8ae0
L
6998 {
6999 /* Remove PT_GNU_RELRO segment. */
7000 if (segment->p_type == PT_GNU_RELRO)
7001 segment->p_type = PT_NULL;
7002 continue;
7003 }
c044fabd 7004
bc67d8a6 7005 /* Determine if this segment overlaps any previous segments. */
0067a569 7006 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
bc67d8a6
NC
7007 {
7008 bfd_signed_vma extra_length;
c044fabd 7009
bc67d8a6 7010 if (segment2->p_type != PT_LOAD
0067a569 7011 || !SEGMENT_OVERLAPS (segment, segment2))
bc67d8a6 7012 continue;
c044fabd 7013
bc67d8a6
NC
7014 /* Merge the two segments together. */
7015 if (segment2->p_vaddr < segment->p_vaddr)
7016 {
c044fabd 7017 /* Extend SEGMENT2 to include SEGMENT and then delete
08a40648 7018 SEGMENT. */
0067a569
AM
7019 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
7020 - SEGMENT_END (segment2, segment2->p_vaddr));
c044fabd 7021
bc67d8a6
NC
7022 if (extra_length > 0)
7023 {
0067a569 7024 segment2->p_memsz += extra_length;
bc67d8a6
NC
7025 segment2->p_filesz += extra_length;
7026 }
c044fabd 7027
bc67d8a6 7028 segment->p_type = PT_NULL;
c044fabd 7029
bc67d8a6
NC
7030 /* Since we have deleted P we must restart the outer loop. */
7031 i = 0;
7032 segment = elf_tdata (ibfd)->phdr;
7033 break;
7034 }
7035 else
7036 {
c044fabd 7037 /* Extend SEGMENT to include SEGMENT2 and then delete
08a40648 7038 SEGMENT2. */
0067a569
AM
7039 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
7040 - SEGMENT_END (segment, segment->p_vaddr));
c044fabd 7041
bc67d8a6
NC
7042 if (extra_length > 0)
7043 {
0067a569 7044 segment->p_memsz += extra_length;
bc67d8a6
NC
7045 segment->p_filesz += extra_length;
7046 }
c044fabd 7047
bc67d8a6
NC
7048 segment2->p_type = PT_NULL;
7049 }
7050 }
7051 }
c044fabd 7052
bc67d8a6
NC
7053 /* The second scan attempts to assign sections to segments. */
7054 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7055 i < num_segments;
0067a569 7056 i++, segment++)
bc67d8a6 7057 {
0067a569
AM
7058 unsigned int section_count;
7059 asection **sections;
7060 asection *output_section;
7061 unsigned int isec;
9933dc52
AM
7062 asection *matching_lma;
7063 asection *suggested_lma;
0067a569 7064 unsigned int j;
446f7ed5 7065 size_t amt;
0067a569 7066 asection *first_section;
bc67d8a6
NC
7067
7068 if (segment->p_type == PT_NULL)
7069 continue;
c044fabd 7070
9f17e2a6 7071 first_section = NULL;
bc67d8a6 7072 /* Compute how many sections might be placed into this segment. */
b5f852ea
NC
7073 for (section = ibfd->sections, section_count = 0;
7074 section != NULL;
7075 section = section->next)
9f17e2a6
L
7076 {
7077 /* Find the first section in the input segment, which may be
7078 removed from the corresponding output segment. */
502794d4 7079 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb))
9f17e2a6
L
7080 {
7081 if (first_section == NULL)
7082 first_section = section;
7083 if (section->output_section != NULL)
7084 ++section_count;
7085 }
7086 }
811072d8 7087
b5f852ea
NC
7088 /* Allocate a segment map big enough to contain
7089 all of the sections we have selected. */
00bee008 7090 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
446f7ed5 7091 amt += section_count * sizeof (asection *);
a50b1753 7092 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
bc67d8a6 7093 if (map == NULL)
b34976b6 7094 return FALSE;
252b5132
RH
7095
7096 /* Initialise the fields of the segment map. Default to
7097 using the physical address of the segment in the input BFD. */
0067a569
AM
7098 map->next = NULL;
7099 map->p_type = segment->p_type;
7100 map->p_flags = segment->p_flags;
bc67d8a6 7101 map->p_flags_valid = 1;
55d55ac7 7102
9f17e2a6
L
7103 /* If the first section in the input segment is removed, there is
7104 no need to preserve segment physical address in the corresponding
7105 output segment. */
945c025a 7106 if (!first_section || first_section->output_section != NULL)
9f17e2a6
L
7107 {
7108 map->p_paddr = segment->p_paddr;
5c44b38e 7109 map->p_paddr_valid = p_paddr_valid;
9f17e2a6 7110 }
252b5132
RH
7111
7112 /* Determine if this segment contains the ELF file header
7113 and if it contains the program headers themselves. */
bc67d8a6
NC
7114 map->includes_filehdr = (segment->p_offset == 0
7115 && segment->p_filesz >= iehdr->e_ehsize);
bc67d8a6 7116 map->includes_phdrs = 0;
252b5132 7117
0067a569 7118 if (!phdr_included || segment->p_type != PT_LOAD)
252b5132 7119 {
bc67d8a6
NC
7120 map->includes_phdrs =
7121 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7122 && (segment->p_offset + segment->p_filesz
252b5132
RH
7123 >= ((bfd_vma) iehdr->e_phoff
7124 + iehdr->e_phnum * iehdr->e_phentsize)));
c044fabd 7125
bc67d8a6 7126 if (segment->p_type == PT_LOAD && map->includes_phdrs)
b34976b6 7127 phdr_included = TRUE;
252b5132
RH
7128 }
7129
bc67d8a6 7130 if (section_count == 0)
252b5132
RH
7131 {
7132 /* Special segments, such as the PT_PHDR segment, may contain
7133 no sections, but ordinary, loadable segments should contain
1ed89aa9 7134 something. They are allowed by the ELF spec however, so only
07d6d2b8 7135 a warning is produced.
f98450c6
NC
7136 There is however the valid use case of embedded systems which
7137 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7138 flash memory with zeros. No warning is shown for that case. */
7139 if (segment->p_type == PT_LOAD
7140 && (segment->p_filesz > 0 || segment->p_memsz == 0))
7141 /* xgettext:c-format */
9793eb77
AM
7142 _bfd_error_handler
7143 (_("%pB: warning: empty loadable segment detected"
7144 " at vaddr=%#" PRIx64 ", is this intentional?"),
7145 ibfd, (uint64_t) segment->p_vaddr);
252b5132 7146
502794d4 7147 map->p_vaddr_offset = segment->p_vaddr / opb;
bc67d8a6 7148 map->count = 0;
c044fabd
KH
7149 *pointer_to_map = map;
7150 pointer_to_map = &map->next;
252b5132
RH
7151
7152 continue;
7153 }
7154
7155 /* Now scan the sections in the input BFD again and attempt
7156 to add their corresponding output sections to the segment map.
7157 The problem here is how to handle an output section which has
7158 been moved (ie had its LMA changed). There are four possibilities:
7159
7160 1. None of the sections have been moved.
7161 In this case we can continue to use the segment LMA from the
7162 input BFD.
7163
7164 2. All of the sections have been moved by the same amount.
7165 In this case we can change the segment's LMA to match the LMA
7166 of the first section.
7167
7168 3. Some of the sections have been moved, others have not.
7169 In this case those sections which have not been moved can be
7170 placed in the current segment which will have to have its size,
7171 and possibly its LMA changed, and a new segment or segments will
7172 have to be created to contain the other sections.
7173
b5f852ea 7174 4. The sections have been moved, but not by the same amount.
252b5132
RH
7175 In this case we can change the segment's LMA to match the LMA
7176 of the first section and we will have to create a new segment
7177 or segments to contain the other sections.
7178
7179 In order to save time, we allocate an array to hold the section
7180 pointers that we are interested in. As these sections get assigned
7181 to a segment, they are removed from this array. */
7182
446f7ed5
AM
7183 amt = section_count * sizeof (asection *);
7184 sections = (asection **) bfd_malloc (amt);
252b5132 7185 if (sections == NULL)
b34976b6 7186 return FALSE;
252b5132
RH
7187
7188 /* Step One: Scan for segment vs section LMA conflicts.
7189 Also add the sections to the section array allocated above.
7190 Also add the sections to the current segment. In the common
7191 case, where the sections have not been moved, this means that
7192 we have completely filled the segment, and there is nothing
7193 more to do. */
252b5132 7194 isec = 0;
9933dc52
AM
7195 matching_lma = NULL;
7196 suggested_lma = NULL;
252b5132 7197
461c4b2e 7198 for (section = first_section, j = 0;
bc67d8a6
NC
7199 section != NULL;
7200 section = section->next)
252b5132 7201 {
502794d4 7202 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb))
c0f7859b 7203 {
bc67d8a6
NC
7204 output_section = section->output_section;
7205
0067a569 7206 sections[j++] = section;
252b5132
RH
7207
7208 /* The Solaris native linker always sets p_paddr to 0.
7209 We try to catch that case here, and set it to the
5e8d7549
NC
7210 correct value. Note - some backends require that
7211 p_paddr be left as zero. */
5c44b38e 7212 if (!p_paddr_valid
4455705d 7213 && segment->p_vaddr != 0
0067a569 7214 && !bed->want_p_paddr_set_to_zero
252b5132 7215 && isec == 0
bc67d8a6 7216 && output_section->lma != 0
9933dc52
AM
7217 && (align_power (segment->p_vaddr
7218 + (map->includes_filehdr
7219 ? iehdr->e_ehsize : 0)
7220 + (map->includes_phdrs
7221 ? iehdr->e_phnum * iehdr->e_phentsize
7222 : 0),
66631823
CE
7223 output_section->alignment_power * opb)
7224 == (output_section->vma * opb)))
bc67d8a6 7225 map->p_paddr = segment->p_vaddr;
252b5132
RH
7226
7227 /* Match up the physical address of the segment with the
7228 LMA address of the output section. */
502794d4
CE
7229 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr,
7230 opb)
5e8d7549 7231 || IS_COREFILE_NOTE (segment, section)
0067a569 7232 || (bed->want_p_paddr_set_to_zero
502794d4 7233 && IS_CONTAINED_BY_VMA (output_section, segment, opb)))
252b5132 7234 {
9933dc52
AM
7235 if (matching_lma == NULL
7236 || output_section->lma < matching_lma->lma)
7237 matching_lma = output_section;
252b5132
RH
7238
7239 /* We assume that if the section fits within the segment
bc67d8a6 7240 then it does not overlap any other section within that
252b5132 7241 segment. */
0067a569
AM
7242 map->sections[isec++] = output_section;
7243 }
9933dc52
AM
7244 else if (suggested_lma == NULL)
7245 suggested_lma = output_section;
147d51c2
L
7246
7247 if (j == section_count)
7248 break;
252b5132
RH
7249 }
7250 }
7251
bc67d8a6 7252 BFD_ASSERT (j == section_count);
252b5132
RH
7253
7254 /* Step Two: Adjust the physical address of the current segment,
7255 if necessary. */
bc67d8a6 7256 if (isec == section_count)
252b5132
RH
7257 {
7258 /* All of the sections fitted within the segment as currently
7259 specified. This is the default case. Add the segment to
7260 the list of built segments and carry on to process the next
7261 program header in the input BFD. */
bc67d8a6 7262 map->count = section_count;
c044fabd
KH
7263 *pointer_to_map = map;
7264 pointer_to_map = &map->next;
08a40648 7265
5c44b38e 7266 if (p_paddr_valid
30fe1832
AM
7267 && !bed->want_p_paddr_set_to_zero)
7268 {
7269 bfd_vma hdr_size = 0;
7270 if (map->includes_filehdr)
7271 hdr_size = iehdr->e_ehsize;
7272 if (map->includes_phdrs)
7273 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7274
7275 /* Account for padding before the first section in the
7276 segment. */
502794d4
CE
7277 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7278 - matching_lma->lma);
30fe1832 7279 }
08a40648 7280
252b5132
RH
7281 free (sections);
7282 continue;
7283 }
252b5132
RH
7284 else
7285 {
9933dc52
AM
7286 /* Change the current segment's physical address to match
7287 the LMA of the first section that fitted, or if no
7288 section fitted, the first section. */
7289 if (matching_lma == NULL)
7290 matching_lma = suggested_lma;
7291
66631823 7292 map->p_paddr = matching_lma->lma * opb;
72730e0c 7293
bc67d8a6
NC
7294 /* Offset the segment physical address from the lma
7295 to allow for space taken up by elf headers. */
9933dc52 7296 if (map->includes_phdrs)
010c8431 7297 {
9933dc52
AM
7298 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7299
7300 /* iehdr->e_phnum is just an estimate of the number
7301 of program headers that we will need. Make a note
7302 here of the number we used and the segment we chose
7303 to hold these headers, so that we can adjust the
7304 offset when we know the correct value. */
7305 phdr_adjust_num = iehdr->e_phnum;
7306 phdr_adjust_seg = map;
010c8431 7307 }
252b5132 7308
9933dc52 7309 if (map->includes_filehdr)
bc67d8a6 7310 {
9933dc52
AM
7311 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7312 map->p_paddr -= iehdr->e_ehsize;
7313 /* We've subtracted off the size of headers from the
7314 first section lma, but there may have been some
7315 alignment padding before that section too. Try to
7316 account for that by adjusting the segment lma down to
7317 the same alignment. */
7318 if (segment->p_align != 0 && segment->p_align < align)
7319 align = segment->p_align;
66631823 7320 map->p_paddr &= -(align * opb);
bc67d8a6 7321 }
252b5132
RH
7322 }
7323
7324 /* Step Three: Loop over the sections again, this time assigning
caf47ea6 7325 those that fit to the current segment and removing them from the
252b5132
RH
7326 sections array; but making sure not to leave large gaps. Once all
7327 possible sections have been assigned to the current segment it is
7328 added to the list of built segments and if sections still remain
7329 to be assigned, a new segment is constructed before repeating
7330 the loop. */
7331 isec = 0;
7332 do
7333 {
bc67d8a6 7334 map->count = 0;
9933dc52 7335 suggested_lma = NULL;
252b5132
RH
7336
7337 /* Fill the current segment with sections that fit. */
bc67d8a6 7338 for (j = 0; j < section_count; j++)
252b5132 7339 {
bc67d8a6 7340 section = sections[j];
252b5132 7341
bc67d8a6 7342 if (section == NULL)
252b5132
RH
7343 continue;
7344
bc67d8a6 7345 output_section = section->output_section;
252b5132 7346
bc67d8a6 7347 BFD_ASSERT (output_section != NULL);
c044fabd 7348
502794d4
CE
7349 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr,
7350 opb)
bc67d8a6 7351 || IS_COREFILE_NOTE (segment, section))
252b5132 7352 {
bc67d8a6 7353 if (map->count == 0)
252b5132
RH
7354 {
7355 /* If the first section in a segment does not start at
bc67d8a6
NC
7356 the beginning of the segment, then something is
7357 wrong. */
9933dc52
AM
7358 if (align_power (map->p_paddr
7359 + (map->includes_filehdr
7360 ? iehdr->e_ehsize : 0)
7361 + (map->includes_phdrs
7362 ? iehdr->e_phnum * iehdr->e_phentsize
7363 : 0),
66631823
CE
7364 output_section->alignment_power * opb)
7365 != output_section->lma * opb)
9aea1e31 7366 goto sorry;
252b5132
RH
7367 }
7368 else
7369 {
0067a569 7370 asection *prev_sec;
252b5132 7371
bc67d8a6 7372 prev_sec = map->sections[map->count - 1];
252b5132
RH
7373
7374 /* If the gap between the end of the previous section
bc67d8a6
NC
7375 and the start of this section is more than
7376 maxpagesize then we need to start a new segment. */
eea6121a 7377 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
079e9a2f 7378 maxpagesize)
caf47ea6 7379 < BFD_ALIGN (output_section->lma, maxpagesize))
0067a569 7380 || (prev_sec->lma + prev_sec->size
079e9a2f 7381 > output_section->lma))
252b5132 7382 {
9933dc52
AM
7383 if (suggested_lma == NULL)
7384 suggested_lma = output_section;
252b5132
RH
7385
7386 continue;
7387 }
7388 }
7389
bc67d8a6 7390 map->sections[map->count++] = output_section;
252b5132
RH
7391 ++isec;
7392 sections[j] = NULL;
9933dc52
AM
7393 if (segment->p_type == PT_LOAD)
7394 section->segment_mark = TRUE;
0067a569 7395 }
9933dc52
AM
7396 else if (suggested_lma == NULL)
7397 suggested_lma = output_section;
252b5132
RH
7398 }
7399
beab4532
NC
7400 /* PR 23932. A corrupt input file may contain sections that cannot
7401 be assigned to any segment - because for example they have a
9984857c
NC
7402 negative size - or segments that do not contain any sections.
7403 But there are also valid reasons why a segment can be empty.
7404 So allow a count of zero. */
252b5132
RH
7405
7406 /* Add the current segment to the list of built segments. */
c044fabd
KH
7407 *pointer_to_map = map;
7408 pointer_to_map = &map->next;
252b5132 7409
bc67d8a6 7410 if (isec < section_count)
252b5132
RH
7411 {
7412 /* We still have not allocated all of the sections to
7413 segments. Create a new segment here, initialise it
7414 and carry on looping. */
00bee008 7415 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
446f7ed5 7416 amt += section_count * sizeof (asection *);
5964fc3a 7417 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
bc67d8a6 7418 if (map == NULL)
5ed6aba4
NC
7419 {
7420 free (sections);
7421 return FALSE;
7422 }
252b5132
RH
7423
7424 /* Initialise the fields of the segment map. Set the physical
7425 physical address to the LMA of the first section that has
7426 not yet been assigned. */
0067a569
AM
7427 map->next = NULL;
7428 map->p_type = segment->p_type;
7429 map->p_flags = segment->p_flags;
7430 map->p_flags_valid = 1;
66631823 7431 map->p_paddr = suggested_lma->lma * opb;
5c44b38e 7432 map->p_paddr_valid = p_paddr_valid;
bc67d8a6 7433 map->includes_filehdr = 0;
0067a569 7434 map->includes_phdrs = 0;
252b5132 7435 }
9984857c
NC
7436
7437 continue;
7438 sorry:
7439 bfd_set_error (bfd_error_sorry);
7440 free (sections);
7441 return FALSE;
252b5132 7442 }
bc67d8a6 7443 while (isec < section_count);
252b5132
RH
7444
7445 free (sections);
7446 }
7447
12bd6957 7448 elf_seg_map (obfd) = map_first;
bc67d8a6
NC
7449
7450 /* If we had to estimate the number of program headers that were
9ad5cbcf 7451 going to be needed, then check our estimate now and adjust
bc67d8a6
NC
7452 the offset if necessary. */
7453 if (phdr_adjust_seg != NULL)
7454 {
7455 unsigned int count;
c044fabd 7456
bc67d8a6 7457 for (count = 0, map = map_first; map != NULL; map = map->next)
c044fabd 7458 count++;
252b5132 7459
bc67d8a6
NC
7460 if (count > phdr_adjust_num)
7461 phdr_adjust_seg->p_paddr
7462 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
9933dc52
AM
7463
7464 for (map = map_first; map != NULL; map = map->next)
7465 if (map->p_type == PT_PHDR)
7466 {
7467 bfd_vma adjust
7468 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7469 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7470 break;
7471 }
bc67d8a6 7472 }
c044fabd 7473
bc67d8a6 7474#undef SEGMENT_END
eecdbe52 7475#undef SECTION_SIZE
bc67d8a6
NC
7476#undef IS_CONTAINED_BY_VMA
7477#undef IS_CONTAINED_BY_LMA
0efc80c8 7478#undef IS_NOTE
252b5132 7479#undef IS_COREFILE_NOTE
bc67d8a6 7480#undef IS_SOLARIS_PT_INTERP
9f17e2a6 7481#undef IS_SECTION_IN_INPUT_SEGMENT
bc67d8a6
NC
7482#undef INCLUDE_SECTION_IN_SEGMENT
7483#undef SEGMENT_AFTER_SEGMENT
7484#undef SEGMENT_OVERLAPS
b34976b6 7485 return TRUE;
252b5132
RH
7486}
7487
84d1d650
L
7488/* Copy ELF program header information. */
7489
7490static bfd_boolean
7491copy_elf_program_header (bfd *ibfd, bfd *obfd)
7492{
7493 Elf_Internal_Ehdr *iehdr;
7494 struct elf_segment_map *map;
7495 struct elf_segment_map *map_first;
7496 struct elf_segment_map **pointer_to_map;
7497 Elf_Internal_Phdr *segment;
7498 unsigned int i;
7499 unsigned int num_segments;
7500 bfd_boolean phdr_included = FALSE;
88967714 7501 bfd_boolean p_paddr_valid;
502794d4 7502 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
84d1d650
L
7503
7504 iehdr = elf_elfheader (ibfd);
7505
7506 map_first = NULL;
7507 pointer_to_map = &map_first;
7508
88967714
AM
7509 /* If all the segment p_paddr fields are zero, don't set
7510 map->p_paddr_valid. */
7511 p_paddr_valid = FALSE;
84d1d650 7512 num_segments = elf_elfheader (ibfd)->e_phnum;
88967714
AM
7513 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7514 i < num_segments;
7515 i++, segment++)
7516 if (segment->p_paddr != 0)
7517 {
7518 p_paddr_valid = TRUE;
7519 break;
7520 }
7521
84d1d650
L
7522 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7523 i < num_segments;
7524 i++, segment++)
7525 {
7526 asection *section;
7527 unsigned int section_count;
986f0783 7528 size_t amt;
84d1d650 7529 Elf_Internal_Shdr *this_hdr;
53020534 7530 asection *first_section = NULL;
a76e6f2f 7531 asection *lowest_section;
84d1d650 7532
84d1d650
L
7533 /* Compute how many sections are in this segment. */
7534 for (section = ibfd->sections, section_count = 0;
7535 section != NULL;
7536 section = section->next)
7537 {
7538 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7539 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
3271a814 7540 {
a76e6f2f
AM
7541 if (first_section == NULL)
7542 first_section = section;
3271a814
NS
7543 section_count++;
7544 }
84d1d650
L
7545 }
7546
7547 /* Allocate a segment map big enough to contain
7548 all of the sections we have selected. */
00bee008 7549 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
986f0783 7550 amt += section_count * sizeof (asection *);
a50b1753 7551 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
84d1d650
L
7552 if (map == NULL)
7553 return FALSE;
7554
7555 /* Initialize the fields of the output segment map with the
7556 input segment. */
7557 map->next = NULL;
7558 map->p_type = segment->p_type;
7559 map->p_flags = segment->p_flags;
7560 map->p_flags_valid = 1;
7561 map->p_paddr = segment->p_paddr;
88967714 7562 map->p_paddr_valid = p_paddr_valid;
3f570048
AM
7563 map->p_align = segment->p_align;
7564 map->p_align_valid = 1;
3271a814 7565 map->p_vaddr_offset = 0;
84d1d650 7566
04c3a755
NS
7567 if (map->p_type == PT_GNU_RELRO
7568 || map->p_type == PT_GNU_STACK)
b10a8ae0
L
7569 {
7570 /* The PT_GNU_RELRO segment may contain the first a few
7571 bytes in the .got.plt section even if the whole .got.plt
7572 section isn't in the PT_GNU_RELRO segment. We won't
04c3a755
NS
7573 change the size of the PT_GNU_RELRO segment.
7574 Similarly, PT_GNU_STACK size is significant on uclinux
7575 systems. */
9433b9b1 7576 map->p_size = segment->p_memsz;
b10a8ae0
L
7577 map->p_size_valid = 1;
7578 }
7579
84d1d650
L
7580 /* Determine if this segment contains the ELF file header
7581 and if it contains the program headers themselves. */
7582 map->includes_filehdr = (segment->p_offset == 0
7583 && segment->p_filesz >= iehdr->e_ehsize);
7584
7585 map->includes_phdrs = 0;
7586 if (! phdr_included || segment->p_type != PT_LOAD)
7587 {
7588 map->includes_phdrs =
7589 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7590 && (segment->p_offset + segment->p_filesz
7591 >= ((bfd_vma) iehdr->e_phoff
7592 + iehdr->e_phnum * iehdr->e_phentsize)));
7593
7594 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7595 phdr_included = TRUE;
7596 }
7597
bbefd0a9 7598 lowest_section = NULL;
84d1d650
L
7599 if (section_count != 0)
7600 {
7601 unsigned int isec = 0;
7602
53020534 7603 for (section = first_section;
84d1d650
L
7604 section != NULL;
7605 section = section->next)
7606 {
7607 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7608 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
53020534
L
7609 {
7610 map->sections[isec++] = section->output_section;
a76e6f2f
AM
7611 if ((section->flags & SEC_ALLOC) != 0)
7612 {
7613 bfd_vma seg_off;
7614
bbefd0a9
AM
7615 if (lowest_section == NULL
7616 || section->lma < lowest_section->lma)
fb8a5684
AM
7617 lowest_section = section;
7618
a76e6f2f
AM
7619 /* Section lmas are set up from PT_LOAD header
7620 p_paddr in _bfd_elf_make_section_from_shdr.
7621 If this header has a p_paddr that disagrees
7622 with the section lma, flag the p_paddr as
7623 invalid. */
7624 if ((section->flags & SEC_LOAD) != 0)
7625 seg_off = this_hdr->sh_offset - segment->p_offset;
7626 else
7627 seg_off = this_hdr->sh_addr - segment->p_vaddr;
502794d4 7628 if (section->lma * opb - segment->p_paddr != seg_off)
a76e6f2f
AM
7629 map->p_paddr_valid = FALSE;
7630 }
53020534
L
7631 if (isec == section_count)
7632 break;
7633 }
84d1d650
L
7634 }
7635 }
7636
5d695627 7637 if (section_count == 0)
502794d4 7638 map->p_vaddr_offset = segment->p_vaddr / opb;
30fe1832
AM
7639 else if (map->p_paddr_valid)
7640 {
7641 /* Account for padding before the first section in the segment. */
7642 bfd_vma hdr_size = 0;
7643 if (map->includes_filehdr)
7644 hdr_size = iehdr->e_ehsize;
7645 if (map->includes_phdrs)
7646 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7647
502794d4 7648 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
30fe1832
AM
7649 - (lowest_section ? lowest_section->lma : 0));
7650 }
a76e6f2f 7651
84d1d650
L
7652 map->count = section_count;
7653 *pointer_to_map = map;
7654 pointer_to_map = &map->next;
7655 }
7656
12bd6957 7657 elf_seg_map (obfd) = map_first;
84d1d650
L
7658 return TRUE;
7659}
7660
7661/* Copy private BFD data. This copies or rewrites ELF program header
7662 information. */
7663
7664static bfd_boolean
7665copy_private_bfd_data (bfd *ibfd, bfd *obfd)
7666{
84d1d650
L
7667 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7668 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7669 return TRUE;
7670
7671 if (elf_tdata (ibfd)->phdr == NULL)
7672 return TRUE;
7673
7674 if (ibfd->xvec == obfd->xvec)
7675 {
cb3ff1e5
NC
7676 /* Check to see if any sections in the input BFD
7677 covered by ELF program header have changed. */
d55ce4e2 7678 Elf_Internal_Phdr *segment;
84d1d650
L
7679 asection *section, *osec;
7680 unsigned int i, num_segments;
7681 Elf_Internal_Shdr *this_hdr;
147d51c2
L
7682 const struct elf_backend_data *bed;
7683
7684 bed = get_elf_backend_data (ibfd);
7685
7686 /* Regenerate the segment map if p_paddr is set to 0. */
7687 if (bed->want_p_paddr_set_to_zero)
7688 goto rewrite;
84d1d650
L
7689
7690 /* Initialize the segment mark field. */
7691 for (section = obfd->sections; section != NULL;
7692 section = section->next)
7693 section->segment_mark = FALSE;
7694
7695 num_segments = elf_elfheader (ibfd)->e_phnum;
7696 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7697 i < num_segments;
7698 i++, segment++)
7699 {
5f6999aa
NC
7700 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7701 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7702 which severly confuses things, so always regenerate the segment
7703 map in this case. */
7704 if (segment->p_paddr == 0
7705 && segment->p_memsz == 0
7706 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
cb3ff1e5 7707 goto rewrite;
5f6999aa 7708
84d1d650
L
7709 for (section = ibfd->sections;
7710 section != NULL; section = section->next)
7711 {
7712 /* We mark the output section so that we know it comes
7713 from the input BFD. */
7714 osec = section->output_section;
7715 if (osec)
7716 osec->segment_mark = TRUE;
7717
7718 /* Check if this section is covered by the segment. */
7719 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7720 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
84d1d650
L
7721 {
7722 /* FIXME: Check if its output section is changed or
7723 removed. What else do we need to check? */
7724 if (osec == NULL
7725 || section->flags != osec->flags
7726 || section->lma != osec->lma
7727 || section->vma != osec->vma
7728 || section->size != osec->size
7729 || section->rawsize != osec->rawsize
7730 || section->alignment_power != osec->alignment_power)
7731 goto rewrite;
7732 }
7733 }
7734 }
7735
cb3ff1e5 7736 /* Check to see if any output section do not come from the
84d1d650
L
7737 input BFD. */
7738 for (section = obfd->sections; section != NULL;
7739 section = section->next)
7740 {
535b785f 7741 if (!section->segment_mark)
84d1d650
L
7742 goto rewrite;
7743 else
7744 section->segment_mark = FALSE;
7745 }
7746
7747 return copy_elf_program_header (ibfd, obfd);
7748 }
7749
dc1e8a47 7750 rewrite:
f1d85785
L
7751 if (ibfd->xvec == obfd->xvec)
7752 {
7753 /* When rewriting program header, set the output maxpagesize to
7754 the maximum alignment of input PT_LOAD segments. */
7755 Elf_Internal_Phdr *segment;
7756 unsigned int i;
7757 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
7758 bfd_vma maxpagesize = 0;
7759
7760 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7761 i < num_segments;
7762 i++, segment++)
7763 if (segment->p_type == PT_LOAD
7764 && maxpagesize < segment->p_align)
c86934ce
NC
7765 {
7766 /* PR 17512: file: f17299af. */
7767 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
695344c0 7768 /* xgettext:c-format */
2dcf00ce
AM
7769 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7770 PRIx64 " is too large"),
7771 ibfd, (uint64_t) segment->p_align);
c86934ce
NC
7772 else
7773 maxpagesize = segment->p_align;
7774 }
f1d85785
L
7775
7776 if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize)
7777 bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize);
7778 }
7779
84d1d650
L
7780 return rewrite_elf_program_header (ibfd, obfd);
7781}
7782
ccd2ec6a
L
7783/* Initialize private output section information from input section. */
7784
7785bfd_boolean
7786_bfd_elf_init_private_section_data (bfd *ibfd,
7787 asection *isec,
7788 bfd *obfd,
7789 asection *osec,
7790 struct bfd_link_info *link_info)
7791
7792{
7793 Elf_Internal_Shdr *ihdr, *ohdr;
0e1862bb
L
7794 bfd_boolean final_link = (link_info != NULL
7795 && !bfd_link_relocatable (link_info));
ccd2ec6a
L
7796
7797 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7798 || obfd->xvec->flavour != bfd_target_elf_flavour)
7799 return TRUE;
7800
ba85c43e
NC
7801 BFD_ASSERT (elf_section_data (osec) != NULL);
7802
8c803a2d
AM
7803 /* If this is a known ABI section, ELF section type and flags may
7804 have been set up when OSEC was created. For normal sections we
7805 allow the user to override the type and flags other than
7806 SHF_MASKOS and SHF_MASKPROC. */
7807 if (elf_section_type (osec) == SHT_PROGBITS
7808 || elf_section_type (osec) == SHT_NOTE
7809 || elf_section_type (osec) == SHT_NOBITS)
7810 elf_section_type (osec) = SHT_NULL;
7811 /* For objcopy and relocatable link, copy the ELF section type from
7812 the input file if the BFD section flags are the same. (If they
7813 are different the user may be doing something like
7814 "objcopy --set-section-flags .text=alloc,data".) For a final
7815 link allow some flags that the linker clears to differ. */
42bb2e33 7816 if (elf_section_type (osec) == SHT_NULL
dfa7b0b8
AM
7817 && (osec->flags == isec->flags
7818 || (final_link
7819 && ((osec->flags ^ isec->flags)
0814be7d 7820 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
42bb2e33 7821 elf_section_type (osec) = elf_section_type (isec);
d270463e
L
7822
7823 /* FIXME: Is this correct for all OS/PROC specific flags? */
8c803a2d
AM
7824 elf_section_flags (osec) = (elf_section_flags (isec)
7825 & (SHF_MASKOS | SHF_MASKPROC));
ccd2ec6a 7826
a91e1603 7827 /* Copy sh_info from input for mbind section. */
df3a023b
AM
7828 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
7829 && elf_section_flags (isec) & SHF_GNU_MBIND)
a91e1603
L
7830 elf_section_data (osec)->this_hdr.sh_info
7831 = elf_section_data (isec)->this_hdr.sh_info;
7832
ccd2ec6a
L
7833 /* Set things up for objcopy and relocatable link. The output
7834 SHT_GROUP section will have its elf_next_in_group pointing back
7835 to the input group members. Ignore linker created group section.
7836 See elfNN_ia64_object_p in elfxx-ia64.c. */
7bdf4127
AB
7837 if ((link_info == NULL
7838 || !link_info->resolve_section_groups)
7839 && (elf_sec_group (isec) == NULL
7840 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
ccd2ec6a 7841 {
7bdf4127
AB
7842 if (elf_section_flags (isec) & SHF_GROUP)
7843 elf_section_flags (osec) |= SHF_GROUP;
7844 elf_next_in_group (osec) = elf_next_in_group (isec);
7845 elf_section_data (osec)->group = elf_section_data (isec)->group;
ccd2ec6a
L
7846 }
7847
7bdf4127
AB
7848 /* If not decompress, preserve SHF_COMPRESSED. */
7849 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
7850 elf_section_flags (osec) |= (elf_section_flags (isec)
7851 & SHF_COMPRESSED);
7852
ccd2ec6a
L
7853 ihdr = &elf_section_data (isec)->this_hdr;
7854
7855 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7856 don't use the output section of the linked-to section since it
7857 may be NULL at this point. */
7858 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
7859 {
7860 ohdr = &elf_section_data (osec)->this_hdr;
7861 ohdr->sh_flags |= SHF_LINK_ORDER;
7862 elf_linked_to_section (osec) = elf_linked_to_section (isec);
7863 }
7864
7865 osec->use_rela_p = isec->use_rela_p;
7866
7867 return TRUE;
7868}
7869
252b5132
RH
7870/* Copy private section information. This copies over the entsize
7871 field, and sometimes the info field. */
7872
b34976b6 7873bfd_boolean
217aa764
AM
7874_bfd_elf_copy_private_section_data (bfd *ibfd,
7875 asection *isec,
7876 bfd *obfd,
7877 asection *osec)
252b5132
RH
7878{
7879 Elf_Internal_Shdr *ihdr, *ohdr;
7880
7881 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7882 || obfd->xvec->flavour != bfd_target_elf_flavour)
b34976b6 7883 return TRUE;
252b5132 7884
252b5132
RH
7885 ihdr = &elf_section_data (isec)->this_hdr;
7886 ohdr = &elf_section_data (osec)->this_hdr;
7887
7888 ohdr->sh_entsize = ihdr->sh_entsize;
7889
7890 if (ihdr->sh_type == SHT_SYMTAB
7891 || ihdr->sh_type == SHT_DYNSYM
7892 || ihdr->sh_type == SHT_GNU_verneed
7893 || ihdr->sh_type == SHT_GNU_verdef)
7894 ohdr->sh_info = ihdr->sh_info;
7895
ccd2ec6a
L
7896 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
7897 NULL);
252b5132
RH
7898}
7899
d0bf826b
AM
7900/* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7901 necessary if we are removing either the SHT_GROUP section or any of
7902 the group member sections. DISCARDED is the value that a section's
7903 output_section has if the section will be discarded, NULL when this
7904 function is called from objcopy, bfd_abs_section_ptr when called
7905 from the linker. */
80fccad2
BW
7906
7907bfd_boolean
d0bf826b 7908_bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
80fccad2 7909{
30288845
AM
7910 asection *isec;
7911
30288845 7912 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
415f38a6 7913 if (elf_section_type (isec) == SHT_GROUP)
30288845
AM
7914 {
7915 asection *first = elf_next_in_group (isec);
7916 asection *s = first;
d0bf826b
AM
7917 bfd_size_type removed = 0;
7918
30288845
AM
7919 while (s != NULL)
7920 {
415f38a6
AM
7921 /* If this member section is being output but the
7922 SHT_GROUP section is not, then clear the group info
7923 set up by _bfd_elf_copy_private_section_data. */
d0bf826b
AM
7924 if (s->output_section != discarded
7925 && isec->output_section == discarded)
30288845
AM
7926 {
7927 elf_section_flags (s->output_section) &= ~SHF_GROUP;
7928 elf_group_name (s->output_section) = NULL;
7929 }
3349112e 7930 else
6e5e9d58
AM
7931 {
7932 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
3349112e
L
7933 if (s->output_section == discarded
7934 && isec->output_section != discarded)
7935 {
7936 /* Conversely, if the member section is not being
7937 output but the SHT_GROUP section is, then adjust
7938 its size. */
7939 removed += 4;
7940 if (elf_sec->rel.hdr != NULL
7941 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
7942 removed += 4;
7943 if (elf_sec->rela.hdr != NULL
7944 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
7945 removed += 4;
7946 }
7947 else
7948 {
7949 /* Also adjust for zero-sized relocation member
7950 section. */
7951 if (elf_sec->rel.hdr != NULL
7952 && elf_sec->rel.hdr->sh_size == 0)
7953 removed += 4;
7954 if (elf_sec->rela.hdr != NULL
7955 && elf_sec->rela.hdr->sh_size == 0)
7956 removed += 4;
7957 }
6e5e9d58 7958 }
30288845
AM
7959 s = elf_next_in_group (s);
7960 if (s == first)
7961 break;
7962 }
d0bf826b
AM
7963 if (removed != 0)
7964 {
7965 if (discarded != NULL)
7966 {
7967 /* If we've been called for ld -r, then we need to
6e5e9d58 7968 adjust the input section size. */
d0bf826b
AM
7969 if (isec->rawsize == 0)
7970 isec->rawsize = isec->size;
7971 isec->size = isec->rawsize - removed;
6e5e9d58
AM
7972 if (isec->size <= 4)
7973 {
7974 isec->size = 0;
7975 isec->flags |= SEC_EXCLUDE;
7976 }
d0bf826b
AM
7977 }
7978 else
7979 {
7980 /* Adjust the output section size when called from
7981 objcopy. */
7982 isec->output_section->size -= removed;
6e5e9d58
AM
7983 if (isec->output_section->size <= 4)
7984 {
7985 isec->output_section->size = 0;
7986 isec->output_section->flags |= SEC_EXCLUDE;
7987 }
d0bf826b
AM
7988 }
7989 }
30288845
AM
7990 }
7991
80fccad2
BW
7992 return TRUE;
7993}
7994
d0bf826b
AM
7995/* Copy private header information. */
7996
7997bfd_boolean
7998_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
7999{
8000 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8001 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8002 return TRUE;
8003
8004 /* Copy over private BFD data if it has not already been copied.
8005 This must be done here, rather than in the copy_private_bfd_data
8006 entry point, because the latter is called after the section
8007 contents have been set, which means that the program headers have
8008 already been worked out. */
12bd6957 8009 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
d0bf826b
AM
8010 {
8011 if (! copy_private_bfd_data (ibfd, obfd))
8012 return FALSE;
8013 }
8014
8015 return _bfd_elf_fixup_group_sections (ibfd, NULL);
8016}
8017
252b5132
RH
8018/* Copy private symbol information. If this symbol is in a section
8019 which we did not map into a BFD section, try to map the section
8020 index correctly. We use special macro definitions for the mapped
8021 section indices; these definitions are interpreted by the
8022 swap_out_syms function. */
8023
9ad5cbcf
AM
8024#define MAP_ONESYMTAB (SHN_HIOS + 1)
8025#define MAP_DYNSYMTAB (SHN_HIOS + 2)
8026#define MAP_STRTAB (SHN_HIOS + 3)
8027#define MAP_SHSTRTAB (SHN_HIOS + 4)
8028#define MAP_SYM_SHNDX (SHN_HIOS + 5)
252b5132 8029
b34976b6 8030bfd_boolean
217aa764
AM
8031_bfd_elf_copy_private_symbol_data (bfd *ibfd,
8032 asymbol *isymarg,
8033 bfd *obfd,
8034 asymbol *osymarg)
252b5132
RH
8035{
8036 elf_symbol_type *isym, *osym;
8037
8038 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8039 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 8040 return TRUE;
252b5132
RH
8041
8042 isym = elf_symbol_from (ibfd, isymarg);
8043 osym = elf_symbol_from (obfd, osymarg);
8044
8045 if (isym != NULL
8424d8f5 8046 && isym->internal_elf_sym.st_shndx != 0
252b5132
RH
8047 && osym != NULL
8048 && bfd_is_abs_section (isym->symbol.section))
8049 {
8050 unsigned int shndx;
8051
8052 shndx = isym->internal_elf_sym.st_shndx;
8053 if (shndx == elf_onesymtab (ibfd))
8054 shndx = MAP_ONESYMTAB;
8055 else if (shndx == elf_dynsymtab (ibfd))
8056 shndx = MAP_DYNSYMTAB;
12bd6957 8057 else if (shndx == elf_strtab_sec (ibfd))
252b5132 8058 shndx = MAP_STRTAB;
12bd6957 8059 else if (shndx == elf_shstrtab_sec (ibfd))
252b5132 8060 shndx = MAP_SHSTRTAB;
6a40cf0c 8061 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
9ad5cbcf 8062 shndx = MAP_SYM_SHNDX;
252b5132
RH
8063 osym->internal_elf_sym.st_shndx = shndx;
8064 }
8065
b34976b6 8066 return TRUE;
252b5132
RH
8067}
8068
8069/* Swap out the symbols. */
8070
b34976b6 8071static bfd_boolean
217aa764 8072swap_out_syms (bfd *abfd,
ef10c3ac 8073 struct elf_strtab_hash **sttp,
217aa764 8074 int relocatable_p)
252b5132 8075{
9c5bfbb7 8076 const struct elf_backend_data *bed;
1f4361a7 8077 unsigned int symcount;
079e9a2f 8078 asymbol **syms;
ef10c3ac 8079 struct elf_strtab_hash *stt;
079e9a2f 8080 Elf_Internal_Shdr *symtab_hdr;
9ad5cbcf 8081 Elf_Internal_Shdr *symtab_shndx_hdr;
079e9a2f 8082 Elf_Internal_Shdr *symstrtab_hdr;
ef10c3ac 8083 struct elf_sym_strtab *symstrtab;
f075ee0c
AM
8084 bfd_byte *outbound_syms;
8085 bfd_byte *outbound_shndx;
ef10c3ac
L
8086 unsigned long outbound_syms_index;
8087 unsigned long outbound_shndx_index;
1f4361a7 8088 unsigned int idx;
12bd6957 8089 unsigned int num_locals;
1f4361a7 8090 size_t amt;
174fd7f9 8091 bfd_boolean name_local_sections;
252b5132 8092
12bd6957 8093 if (!elf_map_symbols (abfd, &num_locals))
b34976b6 8094 return FALSE;
252b5132 8095
c044fabd 8096 /* Dump out the symtabs. */
ef10c3ac 8097 stt = _bfd_elf_strtab_init ();
079e9a2f 8098 if (stt == NULL)
b34976b6 8099 return FALSE;
252b5132 8100
079e9a2f
AM
8101 bed = get_elf_backend_data (abfd);
8102 symcount = bfd_get_symcount (abfd);
8103 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8104 symtab_hdr->sh_type = SHT_SYMTAB;
8105 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8106 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
12bd6957 8107 symtab_hdr->sh_info = num_locals + 1;
72de5009 8108 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
079e9a2f
AM
8109
8110 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8111 symstrtab_hdr->sh_type = SHT_STRTAB;
8112
ef10c3ac 8113 /* Allocate buffer to swap out the .strtab section. */
1f4361a7
AM
8114 if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8115 || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
ef10c3ac 8116 {
1f4361a7 8117 bfd_set_error (bfd_error_no_memory);
ef10c3ac
L
8118 _bfd_elf_strtab_free (stt);
8119 return FALSE;
8120 }
8121
1f4361a7
AM
8122 if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8123 || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
5ed6aba4 8124 {
1f4361a7
AM
8125 error_no_mem:
8126 bfd_set_error (bfd_error_no_memory);
8127 error_return:
ef10c3ac 8128 free (symstrtab);
1f4361a7 8129 _bfd_elf_strtab_free (stt);
5ed6aba4
NC
8130 return FALSE;
8131 }
217aa764 8132 symtab_hdr->contents = outbound_syms;
ef10c3ac 8133 outbound_syms_index = 0;
252b5132 8134
9ad5cbcf 8135 outbound_shndx = NULL;
ef10c3ac 8136 outbound_shndx_index = 0;
6a40cf0c
NC
8137
8138 if (elf_symtab_shndx_list (abfd))
9ad5cbcf 8139 {
6a40cf0c
NC
8140 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8141 if (symtab_shndx_hdr->sh_name != 0)
8142 {
1f4361a7
AM
8143 if (_bfd_mul_overflow (symcount + 1,
8144 sizeof (Elf_External_Sym_Shndx), &amt))
8145 goto error_no_mem;
8146 outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt);
6a40cf0c
NC
8147 if (outbound_shndx == NULL)
8148 goto error_return;
5ed6aba4 8149
6a40cf0c
NC
8150 symtab_shndx_hdr->contents = outbound_shndx;
8151 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8152 symtab_shndx_hdr->sh_size = amt;
8153 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8154 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8155 }
8156 /* FIXME: What about any other headers in the list ? */
9ad5cbcf
AM
8157 }
8158
589e6347 8159 /* Now generate the data (for "contents"). */
079e9a2f
AM
8160 {
8161 /* Fill in zeroth symbol and swap it out. */
8162 Elf_Internal_Sym sym;
8163 sym.st_name = 0;
8164 sym.st_value = 0;
8165 sym.st_size = 0;
8166 sym.st_info = 0;
8167 sym.st_other = 0;
8168 sym.st_shndx = SHN_UNDEF;
35fc36a8 8169 sym.st_target_internal = 0;
ef10c3ac
L
8170 symstrtab[0].sym = sym;
8171 symstrtab[0].dest_index = outbound_syms_index;
8172 symstrtab[0].destshndx_index = outbound_shndx_index;
8173 outbound_syms_index++;
9ad5cbcf 8174 if (outbound_shndx != NULL)
ef10c3ac 8175 outbound_shndx_index++;
079e9a2f 8176 }
252b5132 8177
174fd7f9
RS
8178 name_local_sections
8179 = (bed->elf_backend_name_local_section_symbols
8180 && bed->elf_backend_name_local_section_symbols (abfd));
8181
079e9a2f 8182 syms = bfd_get_outsymbols (abfd);
ef10c3ac 8183 for (idx = 0; idx < symcount;)
252b5132 8184 {
252b5132 8185 Elf_Internal_Sym sym;
079e9a2f
AM
8186 bfd_vma value = syms[idx]->value;
8187 elf_symbol_type *type_ptr;
8188 flagword flags = syms[idx]->flags;
8189 int type;
252b5132 8190
174fd7f9
RS
8191 if (!name_local_sections
8192 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
079e9a2f
AM
8193 {
8194 /* Local section symbols have no name. */
ef10c3ac 8195 sym.st_name = (unsigned long) -1;
079e9a2f
AM
8196 }
8197 else
8198 {
ef10c3ac
L
8199 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8200 to get the final offset for st_name. */
8201 sym.st_name
8202 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
8203 FALSE);
079e9a2f 8204 if (sym.st_name == (unsigned long) -1)
ef10c3ac 8205 goto error_return;
079e9a2f 8206 }
252b5132 8207
079e9a2f 8208 type_ptr = elf_symbol_from (abfd, syms[idx]);
252b5132 8209
079e9a2f
AM
8210 if ((flags & BSF_SECTION_SYM) == 0
8211 && bfd_is_com_section (syms[idx]->section))
8212 {
8213 /* ELF common symbols put the alignment into the `value' field,
8214 and the size into the `size' field. This is backwards from
8215 how BFD handles it, so reverse it here. */
8216 sym.st_size = value;
8217 if (type_ptr == NULL
8218 || type_ptr->internal_elf_sym.st_value == 0)
8219 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8220 else
8221 sym.st_value = type_ptr->internal_elf_sym.st_value;
8222 sym.st_shndx = _bfd_elf_section_from_bfd_section
8223 (abfd, syms[idx]->section);
8224 }
8225 else
8226 {
8227 asection *sec = syms[idx]->section;
cb33740c 8228 unsigned int shndx;
252b5132 8229
079e9a2f
AM
8230 if (sec->output_section)
8231 {
8232 value += sec->output_offset;
8233 sec = sec->output_section;
8234 }
589e6347 8235
079e9a2f
AM
8236 /* Don't add in the section vma for relocatable output. */
8237 if (! relocatable_p)
8238 value += sec->vma;
8239 sym.st_value = value;
8240 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8241
8242 if (bfd_is_abs_section (sec)
8243 && type_ptr != NULL
8244 && type_ptr->internal_elf_sym.st_shndx != 0)
8245 {
8246 /* This symbol is in a real ELF section which we did
8247 not create as a BFD section. Undo the mapping done
8248 by copy_private_symbol_data. */
8249 shndx = type_ptr->internal_elf_sym.st_shndx;
8250 switch (shndx)
8251 {
8252 case MAP_ONESYMTAB:
8253 shndx = elf_onesymtab (abfd);
8254 break;
8255 case MAP_DYNSYMTAB:
8256 shndx = elf_dynsymtab (abfd);
8257 break;
8258 case MAP_STRTAB:
12bd6957 8259 shndx = elf_strtab_sec (abfd);
079e9a2f
AM
8260 break;
8261 case MAP_SHSTRTAB:
12bd6957 8262 shndx = elf_shstrtab_sec (abfd);
079e9a2f 8263 break;
9ad5cbcf 8264 case MAP_SYM_SHNDX:
6a40cf0c
NC
8265 if (elf_symtab_shndx_list (abfd))
8266 shndx = elf_symtab_shndx_list (abfd)->ndx;
9ad5cbcf 8267 break;
00e49dff
NC
8268 case SHN_COMMON:
8269 case SHN_ABS:
15bc576a 8270 shndx = SHN_ABS;
079e9a2f 8271 break;
00e49dff
NC
8272 default:
8273 if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8274 {
8275 if (bed->symbol_section_index)
8276 shndx = bed->symbol_section_index (abfd, type_ptr);
8277 /* Otherwise just leave the index alone. */
8278 }
8279 else
8280 {
8281 if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8282 _bfd_error_handler (_("%pB: \
8283Unable to handle section index %x in ELF symbol. Using ABS instead."),
8284 abfd, shndx);
8285 shndx = SHN_ABS;
8286 }
8287 break;
079e9a2f
AM
8288 }
8289 }
8290 else
8291 {
8292 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
252b5132 8293
cb33740c 8294 if (shndx == SHN_BAD)
079e9a2f
AM
8295 {
8296 asection *sec2;
8297
8298 /* Writing this would be a hell of a lot easier if
8299 we had some decent documentation on bfd, and
8300 knew what to expect of the library, and what to
8301 demand of applications. For example, it
8302 appears that `objcopy' might not set the
8303 section of a symbol to be a section that is
8304 actually in the output file. */
8305 sec2 = bfd_get_section_by_name (abfd, sec->name);
5df1bc57
AM
8306 if (sec2 != NULL)
8307 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8308 if (shndx == SHN_BAD)
589e6347 8309 {
695344c0 8310 /* xgettext:c-format */
9793eb77
AM
8311 _bfd_error_handler
8312 (_("unable to find equivalent output section"
8313 " for symbol '%s' from section '%s'"),
8314 syms[idx]->name ? syms[idx]->name : "<Local sym>",
8315 sec->name);
811072d8 8316 bfd_set_error (bfd_error_invalid_operation);
ef10c3ac 8317 goto error_return;
589e6347 8318 }
079e9a2f
AM
8319 }
8320 }
252b5132 8321
079e9a2f
AM
8322 sym.st_shndx = shndx;
8323 }
252b5132 8324
13ae64f3
JJ
8325 if ((flags & BSF_THREAD_LOCAL) != 0)
8326 type = STT_TLS;
d8045f23
NC
8327 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8328 type = STT_GNU_IFUNC;
13ae64f3 8329 else if ((flags & BSF_FUNCTION) != 0)
079e9a2f
AM
8330 type = STT_FUNC;
8331 else if ((flags & BSF_OBJECT) != 0)
8332 type = STT_OBJECT;
d9352518
DB
8333 else if ((flags & BSF_RELC) != 0)
8334 type = STT_RELC;
8335 else if ((flags & BSF_SRELC) != 0)
8336 type = STT_SRELC;
079e9a2f
AM
8337 else
8338 type = STT_NOTYPE;
252b5132 8339
13ae64f3
JJ
8340 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8341 type = STT_TLS;
8342
589e6347 8343 /* Processor-specific types. */
079e9a2f
AM
8344 if (type_ptr != NULL
8345 && bed->elf_backend_get_symbol_type)
8346 type = ((*bed->elf_backend_get_symbol_type)
8347 (&type_ptr->internal_elf_sym, type));
252b5132 8348
079e9a2f
AM
8349 if (flags & BSF_SECTION_SYM)
8350 {
8351 if (flags & BSF_GLOBAL)
8352 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8353 else
8354 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8355 }
8356 else if (bfd_is_com_section (syms[idx]->section))
0a40daed 8357 {
b8871f35
L
8358 if (type != STT_TLS)
8359 {
8360 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8361 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8362 ? STT_COMMON : STT_OBJECT);
8363 else
8364 type = ((flags & BSF_ELF_COMMON) != 0
8365 ? STT_COMMON : STT_OBJECT);
8366 }
8367 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
0a40daed 8368 }
079e9a2f
AM
8369 else if (bfd_is_und_section (syms[idx]->section))
8370 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8371 ? STB_WEAK
8372 : STB_GLOBAL),
8373 type);
8374 else if (flags & BSF_FILE)
8375 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8376 else
8377 {
8378 int bind = STB_LOCAL;
252b5132 8379
079e9a2f
AM
8380 if (flags & BSF_LOCAL)
8381 bind = STB_LOCAL;
3e7a7d11
NC
8382 else if (flags & BSF_GNU_UNIQUE)
8383 bind = STB_GNU_UNIQUE;
079e9a2f
AM
8384 else if (flags & BSF_WEAK)
8385 bind = STB_WEAK;
8386 else if (flags & BSF_GLOBAL)
8387 bind = STB_GLOBAL;
252b5132 8388
079e9a2f
AM
8389 sym.st_info = ELF_ST_INFO (bind, type);
8390 }
252b5132 8391
079e9a2f 8392 if (type_ptr != NULL)
35fc36a8
RS
8393 {
8394 sym.st_other = type_ptr->internal_elf_sym.st_other;
8395 sym.st_target_internal
8396 = type_ptr->internal_elf_sym.st_target_internal;
8397 }
079e9a2f 8398 else
35fc36a8
RS
8399 {
8400 sym.st_other = 0;
8401 sym.st_target_internal = 0;
8402 }
252b5132 8403
ef10c3ac
L
8404 idx++;
8405 symstrtab[idx].sym = sym;
8406 symstrtab[idx].dest_index = outbound_syms_index;
8407 symstrtab[idx].destshndx_index = outbound_shndx_index;
8408
8409 outbound_syms_index++;
9ad5cbcf 8410 if (outbound_shndx != NULL)
ef10c3ac
L
8411 outbound_shndx_index++;
8412 }
8413
8414 /* Finalize the .strtab section. */
8415 _bfd_elf_strtab_finalize (stt);
8416
8417 /* Swap out the .strtab section. */
8418 for (idx = 0; idx <= symcount; idx++)
8419 {
8420 struct elf_sym_strtab *elfsym = &symstrtab[idx];
8421 if (elfsym->sym.st_name == (unsigned long) -1)
8422 elfsym->sym.st_name = 0;
8423 else
8424 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8425 elfsym->sym.st_name);
8426 bed->s->swap_symbol_out (abfd, &elfsym->sym,
8427 (outbound_syms
8428 + (elfsym->dest_index
8429 * bed->s->sizeof_sym)),
8430 (outbound_shndx
8431 + (elfsym->destshndx_index
8432 * sizeof (Elf_External_Sym_Shndx))));
079e9a2f 8433 }
ef10c3ac 8434 free (symstrtab);
252b5132 8435
079e9a2f 8436 *sttp = stt;
ef10c3ac 8437 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
079e9a2f 8438 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 8439 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
079e9a2f
AM
8440 symstrtab_hdr->sh_addr = 0;
8441 symstrtab_hdr->sh_entsize = 0;
8442 symstrtab_hdr->sh_link = 0;
8443 symstrtab_hdr->sh_info = 0;
8444 symstrtab_hdr->sh_addralign = 1;
252b5132 8445
b34976b6 8446 return TRUE;
252b5132
RH
8447}
8448
8449/* Return the number of bytes required to hold the symtab vector.
8450
8451 Note that we base it on the count plus 1, since we will null terminate
8452 the vector allocated based on this size. However, the ELF symbol table
8453 always has a dummy entry as symbol #0, so it ends up even. */
8454
8455long
217aa764 8456_bfd_elf_get_symtab_upper_bound (bfd *abfd)
252b5132 8457{
3a551c7a 8458 bfd_size_type symcount;
252b5132
RH
8459 long symtab_size;
8460 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
8461
8462 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3a551c7a
AM
8463 if (symcount >= LONG_MAX / sizeof (asymbol *))
8464 {
8465 bfd_set_error (bfd_error_file_too_big);
8466 return -1;
8467 }
b99d1833
AM
8468 symtab_size = (symcount + 1) * (sizeof (asymbol *));
8469 if (symcount > 0)
8470 symtab_size -= sizeof (asymbol *);
252b5132
RH
8471
8472 return symtab_size;
8473}
8474
8475long
217aa764 8476_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
252b5132 8477{
3a551c7a 8478 bfd_size_type symcount;
252b5132
RH
8479 long symtab_size;
8480 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
8481
8482 if (elf_dynsymtab (abfd) == 0)
8483 {
8484 bfd_set_error (bfd_error_invalid_operation);
8485 return -1;
8486 }
8487
8488 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3a551c7a
AM
8489 if (symcount >= LONG_MAX / sizeof (asymbol *))
8490 {
8491 bfd_set_error (bfd_error_file_too_big);
8492 return -1;
8493 }
b99d1833
AM
8494 symtab_size = (symcount + 1) * (sizeof (asymbol *));
8495 if (symcount > 0)
8496 symtab_size -= sizeof (asymbol *);
252b5132
RH
8497
8498 return symtab_size;
8499}
8500
8501long
217aa764
AM
8502_bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
8503 sec_ptr asect)
252b5132 8504{
242a1159 8505#if SIZEOF_LONG == SIZEOF_INT
7a6e0d89
AM
8506 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
8507 {
8508 bfd_set_error (bfd_error_file_too_big);
8509 return -1;
8510 }
242a1159 8511#endif
252b5132
RH
8512 return (asect->reloc_count + 1) * sizeof (arelent *);
8513}
8514
8515/* Canonicalize the relocs. */
8516
8517long
217aa764
AM
8518_bfd_elf_canonicalize_reloc (bfd *abfd,
8519 sec_ptr section,
8520 arelent **relptr,
8521 asymbol **symbols)
252b5132
RH
8522{
8523 arelent *tblptr;
8524 unsigned int i;
9c5bfbb7 8525 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 8526
b34976b6 8527 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
252b5132
RH
8528 return -1;
8529
8530 tblptr = section->relocation;
8531 for (i = 0; i < section->reloc_count; i++)
8532 *relptr++ = tblptr++;
8533
8534 *relptr = NULL;
8535
8536 return section->reloc_count;
8537}
8538
8539long
6cee3f79 8540_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
252b5132 8541{
9c5bfbb7 8542 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764 8543 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
252b5132
RH
8544
8545 if (symcount >= 0)
ed48ec2e 8546 abfd->symcount = symcount;
252b5132
RH
8547 return symcount;
8548}
8549
8550long
217aa764
AM
8551_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
8552 asymbol **allocation)
252b5132 8553{
9c5bfbb7 8554 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764 8555 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
1f70368c
DJ
8556
8557 if (symcount >= 0)
ed48ec2e 8558 abfd->dynsymcount = symcount;
1f70368c 8559 return symcount;
252b5132
RH
8560}
8561
8615f3f2
AM
8562/* Return the size required for the dynamic reloc entries. Any loadable
8563 section that was actually installed in the BFD, and has type SHT_REL
8564 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8565 dynamic reloc section. */
252b5132
RH
8566
8567long
217aa764 8568_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
252b5132 8569{
3a551c7a 8570 bfd_size_type count;
252b5132
RH
8571 asection *s;
8572
8573 if (elf_dynsymtab (abfd) == 0)
8574 {
8575 bfd_set_error (bfd_error_invalid_operation);
8576 return -1;
8577 }
8578
3a551c7a 8579 count = 1;
252b5132 8580 for (s = abfd->sections; s != NULL; s = s->next)
266b05cf 8581 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132
RH
8582 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8583 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
3a551c7a
AM
8584 {
8585 count += s->size / elf_section_data (s)->this_hdr.sh_entsize;
8586 if (count > LONG_MAX / sizeof (arelent *))
8587 {
8588 bfd_set_error (bfd_error_file_too_big);
8589 return -1;
8590 }
8591 }
8592 return count * sizeof (arelent *);
252b5132
RH
8593}
8594
8615f3f2
AM
8595/* Canonicalize the dynamic relocation entries. Note that we return the
8596 dynamic relocations as a single block, although they are actually
8597 associated with particular sections; the interface, which was
8598 designed for SunOS style shared libraries, expects that there is only
8599 one set of dynamic relocs. Any loadable section that was actually
8600 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8601 dynamic symbol table, is considered to be a dynamic reloc section. */
252b5132
RH
8602
8603long
217aa764
AM
8604_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
8605 arelent **storage,
8606 asymbol **syms)
252b5132 8607{
217aa764 8608 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
252b5132
RH
8609 asection *s;
8610 long ret;
8611
8612 if (elf_dynsymtab (abfd) == 0)
8613 {
8614 bfd_set_error (bfd_error_invalid_operation);
8615 return -1;
8616 }
8617
8618 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8619 ret = 0;
8620 for (s = abfd->sections; s != NULL; s = s->next)
8621 {
266b05cf 8622 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132
RH
8623 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8624 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8625 {
8626 arelent *p;
8627 long count, i;
8628
b34976b6 8629 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
252b5132 8630 return -1;
eea6121a 8631 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
252b5132
RH
8632 p = s->relocation;
8633 for (i = 0; i < count; i++)
8634 *storage++ = p++;
8635 ret += count;
8636 }
8637 }
8638
8639 *storage = NULL;
8640
8641 return ret;
8642}
8643\f
8644/* Read in the version information. */
8645
b34976b6 8646bfd_boolean
fc0e6df6 8647_bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
252b5132
RH
8648{
8649 bfd_byte *contents = NULL;
fc0e6df6 8650 unsigned int freeidx = 0;
1f4361a7 8651 size_t amt;
fc0e6df6
PB
8652
8653 if (elf_dynverref (abfd) != 0)
8654 {
8655 Elf_Internal_Shdr *hdr;
8656 Elf_External_Verneed *everneed;
8657 Elf_Internal_Verneed *iverneed;
8658 unsigned int i;
d0fb9a8d 8659 bfd_byte *contents_end;
fc0e6df6
PB
8660
8661 hdr = &elf_tdata (abfd)->dynverref_hdr;
8662
bd61e135
AM
8663 if (hdr->sh_info == 0
8664 || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
d0fb9a8d 8665 {
dc1e8a47 8666 error_return_bad_verref:
4eca0228 8667 _bfd_error_handler
871b3ab2 8668 (_("%pB: .gnu.version_r invalid entry"), abfd);
601a03ba 8669 bfd_set_error (bfd_error_bad_value);
dc1e8a47 8670 error_return_verref:
d0fb9a8d
JJ
8671 elf_tdata (abfd)->verref = NULL;
8672 elf_tdata (abfd)->cverrefs = 0;
8673 goto error_return;
8674 }
601a03ba 8675
2bb3687b
AM
8676 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
8677 goto error_return_verref;
8678 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
8679 if (contents == NULL)
d0fb9a8d 8680 goto error_return_verref;
fc0e6df6 8681
1f4361a7
AM
8682 if (_bfd_mul_overflow (hdr->sh_info, sizeof (Elf_Internal_Verneed), &amt))
8683 {
8684 bfd_set_error (bfd_error_file_too_big);
8685 goto error_return_verref;
8686 }
8687 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_alloc (abfd, amt);
601a03ba 8688 if (elf_tdata (abfd)->verref == NULL)
d0fb9a8d
JJ
8689 goto error_return_verref;
8690
8691 BFD_ASSERT (sizeof (Elf_External_Verneed)
8692 == sizeof (Elf_External_Vernaux));
8693 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
fc0e6df6
PB
8694 everneed = (Elf_External_Verneed *) contents;
8695 iverneed = elf_tdata (abfd)->verref;
8696 for (i = 0; i < hdr->sh_info; i++, iverneed++)
8697 {
8698 Elf_External_Vernaux *evernaux;
8699 Elf_Internal_Vernaux *ivernaux;
8700 unsigned int j;
8701
8702 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
8703
8704 iverneed->vn_bfd = abfd;
8705
8706 iverneed->vn_filename =
8707 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8708 iverneed->vn_file);
8709 if (iverneed->vn_filename == NULL)
601a03ba 8710 goto error_return_bad_verref;
fc0e6df6 8711
d0fb9a8d
JJ
8712 if (iverneed->vn_cnt == 0)
8713 iverneed->vn_auxptr = NULL;
8714 else
8715 {
1f4361a7
AM
8716 if (_bfd_mul_overflow (iverneed->vn_cnt,
8717 sizeof (Elf_Internal_Vernaux), &amt))
8718 {
8719 bfd_set_error (bfd_error_file_too_big);
8720 goto error_return_verref;
8721 }
a50b1753 8722 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
1f4361a7 8723 bfd_alloc (abfd, amt);
d0fb9a8d
JJ
8724 if (iverneed->vn_auxptr == NULL)
8725 goto error_return_verref;
8726 }
8727
8728 if (iverneed->vn_aux
8729 > (size_t) (contents_end - (bfd_byte *) everneed))
601a03ba 8730 goto error_return_bad_verref;
fc0e6df6
PB
8731
8732 evernaux = ((Elf_External_Vernaux *)
8733 ((bfd_byte *) everneed + iverneed->vn_aux));
8734 ivernaux = iverneed->vn_auxptr;
8735 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
8736 {
8737 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
8738
8739 ivernaux->vna_nodename =
8740 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8741 ivernaux->vna_name);
8742 if (ivernaux->vna_nodename == NULL)
601a03ba 8743 goto error_return_bad_verref;
fc0e6df6 8744
25ff461f
AM
8745 if (ivernaux->vna_other > freeidx)
8746 freeidx = ivernaux->vna_other;
8747
8748 ivernaux->vna_nextptr = NULL;
8749 if (ivernaux->vna_next == 0)
8750 {
8751 iverneed->vn_cnt = j + 1;
8752 break;
8753 }
fc0e6df6
PB
8754 if (j + 1 < iverneed->vn_cnt)
8755 ivernaux->vna_nextptr = ivernaux + 1;
fc0e6df6 8756
d0fb9a8d
JJ
8757 if (ivernaux->vna_next
8758 > (size_t) (contents_end - (bfd_byte *) evernaux))
601a03ba 8759 goto error_return_bad_verref;
d0fb9a8d 8760
fc0e6df6
PB
8761 evernaux = ((Elf_External_Vernaux *)
8762 ((bfd_byte *) evernaux + ivernaux->vna_next));
fc0e6df6
PB
8763 }
8764
25ff461f
AM
8765 iverneed->vn_nextref = NULL;
8766 if (iverneed->vn_next == 0)
8767 break;
fc0e6df6
PB
8768 if (i + 1 < hdr->sh_info)
8769 iverneed->vn_nextref = iverneed + 1;
fc0e6df6 8770
d0fb9a8d
JJ
8771 if (iverneed->vn_next
8772 > (size_t) (contents_end - (bfd_byte *) everneed))
601a03ba 8773 goto error_return_bad_verref;
d0fb9a8d 8774
fc0e6df6
PB
8775 everneed = ((Elf_External_Verneed *)
8776 ((bfd_byte *) everneed + iverneed->vn_next));
8777 }
25ff461f 8778 elf_tdata (abfd)->cverrefs = i;
fc0e6df6
PB
8779
8780 free (contents);
8781 contents = NULL;
8782 }
252b5132
RH
8783
8784 if (elf_dynverdef (abfd) != 0)
8785 {
8786 Elf_Internal_Shdr *hdr;
8787 Elf_External_Verdef *everdef;
8788 Elf_Internal_Verdef *iverdef;
f631889e
UD
8789 Elf_Internal_Verdef *iverdefarr;
8790 Elf_Internal_Verdef iverdefmem;
252b5132 8791 unsigned int i;
062e2358 8792 unsigned int maxidx;
d0fb9a8d 8793 bfd_byte *contents_end_def, *contents_end_aux;
252b5132
RH
8794
8795 hdr = &elf_tdata (abfd)->dynverdef_hdr;
8796
601a03ba
AM
8797 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
8798 {
8799 error_return_bad_verdef:
4eca0228 8800 _bfd_error_handler
871b3ab2 8801 (_("%pB: .gnu.version_d invalid entry"), abfd);
601a03ba
AM
8802 bfd_set_error (bfd_error_bad_value);
8803 error_return_verdef:
8804 elf_tdata (abfd)->verdef = NULL;
8805 elf_tdata (abfd)->cverdefs = 0;
8806 goto error_return;
8807 }
8808
2bb3687b 8809 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
601a03ba 8810 goto error_return_verdef;
2bb3687b
AM
8811 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
8812 if (contents == NULL)
601a03ba 8813 goto error_return_verdef;
d0fb9a8d
JJ
8814
8815 BFD_ASSERT (sizeof (Elf_External_Verdef)
8816 >= sizeof (Elf_External_Verdaux));
8817 contents_end_def = contents + hdr->sh_size
8818 - sizeof (Elf_External_Verdef);
8819 contents_end_aux = contents + hdr->sh_size
8820 - sizeof (Elf_External_Verdaux);
8821
f631889e
UD
8822 /* We know the number of entries in the section but not the maximum
8823 index. Therefore we have to run through all entries and find
8824 the maximum. */
252b5132 8825 everdef = (Elf_External_Verdef *) contents;
f631889e
UD
8826 maxidx = 0;
8827 for (i = 0; i < hdr->sh_info; ++i)
8828 {
8829 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8830
601a03ba
AM
8831 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
8832 goto error_return_bad_verdef;
062e2358
AM
8833 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
8834 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
f631889e 8835
25ff461f
AM
8836 if (iverdefmem.vd_next == 0)
8837 break;
8838
d0fb9a8d
JJ
8839 if (iverdefmem.vd_next
8840 > (size_t) (contents_end_def - (bfd_byte *) everdef))
601a03ba 8841 goto error_return_bad_verdef;
d0fb9a8d 8842
f631889e
UD
8843 everdef = ((Elf_External_Verdef *)
8844 ((bfd_byte *) everdef + iverdefmem.vd_next));
8845 }
8846
fc0e6df6
PB
8847 if (default_imported_symver)
8848 {
8849 if (freeidx > maxidx)
8850 maxidx = ++freeidx;
8851 else
8852 freeidx = ++maxidx;
8853 }
1f4361a7
AM
8854 if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
8855 {
8856 bfd_set_error (bfd_error_file_too_big);
8857 goto error_return_verdef;
8858 }
8859 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
f631889e 8860 if (elf_tdata (abfd)->verdef == NULL)
601a03ba 8861 goto error_return_verdef;
f631889e
UD
8862
8863 elf_tdata (abfd)->cverdefs = maxidx;
8864
8865 everdef = (Elf_External_Verdef *) contents;
8866 iverdefarr = elf_tdata (abfd)->verdef;
8867 for (i = 0; i < hdr->sh_info; i++)
252b5132
RH
8868 {
8869 Elf_External_Verdaux *everdaux;
8870 Elf_Internal_Verdaux *iverdaux;
8871 unsigned int j;
8872
f631889e
UD
8873 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8874
d0fb9a8d 8875 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
601a03ba 8876 goto error_return_bad_verdef;
d0fb9a8d 8877
f631889e 8878 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
595bce75 8879 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
252b5132
RH
8880
8881 iverdef->vd_bfd = abfd;
8882
d0fb9a8d
JJ
8883 if (iverdef->vd_cnt == 0)
8884 iverdef->vd_auxptr = NULL;
8885 else
8886 {
1f4361a7
AM
8887 if (_bfd_mul_overflow (iverdef->vd_cnt,
8888 sizeof (Elf_Internal_Verdaux), &amt))
8889 {
8890 bfd_set_error (bfd_error_file_too_big);
8891 goto error_return_verdef;
8892 }
a50b1753 8893 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
1f4361a7 8894 bfd_alloc (abfd, amt);
d0fb9a8d
JJ
8895 if (iverdef->vd_auxptr == NULL)
8896 goto error_return_verdef;
8897 }
8898
8899 if (iverdef->vd_aux
8900 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
601a03ba 8901 goto error_return_bad_verdef;
252b5132
RH
8902
8903 everdaux = ((Elf_External_Verdaux *)
8904 ((bfd_byte *) everdef + iverdef->vd_aux));
8905 iverdaux = iverdef->vd_auxptr;
8906 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
8907 {
8908 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
8909
8910 iverdaux->vda_nodename =
8911 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8912 iverdaux->vda_name);
8913 if (iverdaux->vda_nodename == NULL)
601a03ba 8914 goto error_return_bad_verdef;
252b5132 8915
25ff461f
AM
8916 iverdaux->vda_nextptr = NULL;
8917 if (iverdaux->vda_next == 0)
8918 {
8919 iverdef->vd_cnt = j + 1;
8920 break;
8921 }
252b5132
RH
8922 if (j + 1 < iverdef->vd_cnt)
8923 iverdaux->vda_nextptr = iverdaux + 1;
252b5132 8924
d0fb9a8d
JJ
8925 if (iverdaux->vda_next
8926 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
601a03ba 8927 goto error_return_bad_verdef;
d0fb9a8d 8928
252b5132
RH
8929 everdaux = ((Elf_External_Verdaux *)
8930 ((bfd_byte *) everdaux + iverdaux->vda_next));
8931 }
8932
595bce75 8933 iverdef->vd_nodename = NULL;
d0fb9a8d
JJ
8934 if (iverdef->vd_cnt)
8935 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
252b5132 8936
25ff461f
AM
8937 iverdef->vd_nextdef = NULL;
8938 if (iverdef->vd_next == 0)
8939 break;
d0fb9a8d 8940 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
252b5132 8941 iverdef->vd_nextdef = iverdef + 1;
252b5132
RH
8942
8943 everdef = ((Elf_External_Verdef *)
8944 ((bfd_byte *) everdef + iverdef->vd_next));
8945 }
8946
8947 free (contents);
8948 contents = NULL;
8949 }
fc0e6df6 8950 else if (default_imported_symver)
252b5132 8951 {
fc0e6df6
PB
8952 if (freeidx < 3)
8953 freeidx = 3;
8954 else
8955 freeidx++;
252b5132 8956
1f4361a7
AM
8957 if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
8958 {
8959 bfd_set_error (bfd_error_file_too_big);
8960 goto error_return;
8961 }
8962 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
fc0e6df6 8963 if (elf_tdata (abfd)->verdef == NULL)
252b5132
RH
8964 goto error_return;
8965
fc0e6df6
PB
8966 elf_tdata (abfd)->cverdefs = freeidx;
8967 }
252b5132 8968
fc0e6df6
PB
8969 /* Create a default version based on the soname. */
8970 if (default_imported_symver)
8971 {
8972 Elf_Internal_Verdef *iverdef;
8973 Elf_Internal_Verdaux *iverdaux;
252b5132 8974
5bb3703f 8975 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
252b5132 8976
fc0e6df6
PB
8977 iverdef->vd_version = VER_DEF_CURRENT;
8978 iverdef->vd_flags = 0;
8979 iverdef->vd_ndx = freeidx;
8980 iverdef->vd_cnt = 1;
252b5132 8981
fc0e6df6 8982 iverdef->vd_bfd = abfd;
252b5132 8983
fc0e6df6
PB
8984 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
8985 if (iverdef->vd_nodename == NULL)
d0fb9a8d 8986 goto error_return_verdef;
fc0e6df6 8987 iverdef->vd_nextdef = NULL;
601a03ba
AM
8988 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
8989 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
d0fb9a8d
JJ
8990 if (iverdef->vd_auxptr == NULL)
8991 goto error_return_verdef;
252b5132 8992
fc0e6df6
PB
8993 iverdaux = iverdef->vd_auxptr;
8994 iverdaux->vda_nodename = iverdef->vd_nodename;
252b5132
RH
8995 }
8996
b34976b6 8997 return TRUE;
252b5132
RH
8998
8999 error_return:
5ed6aba4 9000 if (contents != NULL)
252b5132 9001 free (contents);
b34976b6 9002 return FALSE;
252b5132
RH
9003}
9004\f
9005asymbol *
217aa764 9006_bfd_elf_make_empty_symbol (bfd *abfd)
252b5132
RH
9007{
9008 elf_symbol_type *newsym;
9009
7a6e0d89 9010 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
252b5132
RH
9011 if (!newsym)
9012 return NULL;
201159ec
NC
9013 newsym->symbol.the_bfd = abfd;
9014 return &newsym->symbol;
252b5132
RH
9015}
9016
9017void
217aa764
AM
9018_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9019 asymbol *symbol,
9020 symbol_info *ret)
252b5132
RH
9021{
9022 bfd_symbol_info (symbol, ret);
9023}
9024
9025/* Return whether a symbol name implies a local symbol. Most targets
9026 use this function for the is_local_label_name entry point, but some
9027 override it. */
9028
b34976b6 9029bfd_boolean
217aa764
AM
9030_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9031 const char *name)
252b5132
RH
9032{
9033 /* Normal local symbols start with ``.L''. */
9034 if (name[0] == '.' && name[1] == 'L')
b34976b6 9035 return TRUE;
252b5132
RH
9036
9037 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9038 DWARF debugging symbols starting with ``..''. */
9039 if (name[0] == '.' && name[1] == '.')
b34976b6 9040 return TRUE;
252b5132
RH
9041
9042 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9043 emitting DWARF debugging output. I suspect this is actually a
9044 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9045 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9046 underscore to be emitted on some ELF targets). For ease of use,
9047 we treat such symbols as local. */
9048 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
b34976b6 9049 return TRUE;
252b5132 9050
b1fa9dd6
NC
9051 /* Treat assembler generated fake symbols, dollar local labels and
9052 forward-backward labels (aka local labels) as locals.
9053 These labels have the form:
9054
07d6d2b8 9055 L0^A.* (fake symbols)
b1fa9dd6
NC
9056
9057 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9058
9059 Versions which start with .L will have already been matched above,
9060 so we only need to match the rest. */
9061 if (name[0] == 'L' && ISDIGIT (name[1]))
9062 {
9063 bfd_boolean ret = FALSE;
9064 const char * p;
9065 char c;
9066
9067 for (p = name + 2; (c = *p); p++)
9068 {
9069 if (c == 1 || c == 2)
9070 {
9071 if (c == 1 && p == name + 2)
9072 /* A fake symbol. */
9073 return TRUE;
9074
9075 /* FIXME: We are being paranoid here and treating symbols like
9076 L0^Bfoo as if there were non-local, on the grounds that the
9077 assembler will never generate them. But can any symbol
9078 containing an ASCII value in the range 1-31 ever be anything
9079 other than some kind of local ? */
9080 ret = TRUE;
9081 }
9082
9083 if (! ISDIGIT (c))
9084 {
9085 ret = FALSE;
9086 break;
9087 }
9088 }
9089 return ret;
9090 }
ffa54770 9091
b34976b6 9092 return FALSE;
252b5132
RH
9093}
9094
9095alent *
217aa764
AM
9096_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9097 asymbol *symbol ATTRIBUTE_UNUSED)
252b5132
RH
9098{
9099 abort ();
9100 return NULL;
9101}
9102
b34976b6 9103bfd_boolean
217aa764
AM
9104_bfd_elf_set_arch_mach (bfd *abfd,
9105 enum bfd_architecture arch,
9106 unsigned long machine)
252b5132
RH
9107{
9108 /* If this isn't the right architecture for this backend, and this
9109 isn't the generic backend, fail. */
9110 if (arch != get_elf_backend_data (abfd)->arch
9111 && arch != bfd_arch_unknown
9112 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
b34976b6 9113 return FALSE;
252b5132
RH
9114
9115 return bfd_default_set_arch_mach (abfd, arch, machine);
9116}
9117
d1fad7c6
NC
9118/* Find the nearest line to a particular section and offset,
9119 for error reporting. */
9120
b34976b6 9121bfd_boolean
217aa764 9122_bfd_elf_find_nearest_line (bfd *abfd,
217aa764 9123 asymbol **symbols,
fb167eb2 9124 asection *section,
217aa764
AM
9125 bfd_vma offset,
9126 const char **filename_ptr,
9127 const char **functionname_ptr,
fb167eb2
AM
9128 unsigned int *line_ptr,
9129 unsigned int *discriminator_ptr)
d1fad7c6 9130{
b34976b6 9131 bfd_boolean found;
d1fad7c6 9132
fb167eb2 9133 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
4e8a9624 9134 filename_ptr, functionname_ptr,
fb167eb2 9135 line_ptr, discriminator_ptr,
9defd221 9136 dwarf_debug_sections,
e7679060
AM
9137 &elf_tdata (abfd)->dwarf2_find_line_info))
9138 return TRUE;
9139
9140 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9141 filename_ptr, functionname_ptr, line_ptr))
d1fad7c6
NC
9142 {
9143 if (!*functionname_ptr)
e00e8198
AM
9144 _bfd_elf_find_function (abfd, symbols, section, offset,
9145 *filename_ptr ? NULL : filename_ptr,
9146 functionname_ptr);
b34976b6 9147 return TRUE;
d1fad7c6
NC
9148 }
9149
9150 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4e8a9624
AM
9151 &found, filename_ptr,
9152 functionname_ptr, line_ptr,
9153 &elf_tdata (abfd)->line_info))
b34976b6 9154 return FALSE;
dc43ada5 9155 if (found && (*functionname_ptr || *line_ptr))
b34976b6 9156 return TRUE;
d1fad7c6
NC
9157
9158 if (symbols == NULL)
b34976b6 9159 return FALSE;
d1fad7c6 9160
e00e8198
AM
9161 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9162 filename_ptr, functionname_ptr))
b34976b6 9163 return FALSE;
d1fad7c6 9164
252b5132 9165 *line_ptr = 0;
b34976b6 9166 return TRUE;
252b5132
RH
9167}
9168
5420f73d
L
9169/* Find the line for a symbol. */
9170
9171bfd_boolean
9172_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9173 const char **filename_ptr, unsigned int *line_ptr)
9b8d1a36 9174{
fb167eb2
AM
9175 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9176 filename_ptr, NULL, line_ptr, NULL,
9defd221 9177 dwarf_debug_sections,
fb167eb2 9178 &elf_tdata (abfd)->dwarf2_find_line_info);
5420f73d
L
9179}
9180
4ab527b0
FF
9181/* After a call to bfd_find_nearest_line, successive calls to
9182 bfd_find_inliner_info can be used to get source information about
9183 each level of function inlining that terminated at the address
9184 passed to bfd_find_nearest_line. Currently this is only supported
9185 for DWARF2 with appropriate DWARF3 extensions. */
9186
9187bfd_boolean
9188_bfd_elf_find_inliner_info (bfd *abfd,
9189 const char **filename_ptr,
9190 const char **functionname_ptr,
9191 unsigned int *line_ptr)
9192{
9193 bfd_boolean found;
9194 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9195 functionname_ptr, line_ptr,
9196 & elf_tdata (abfd)->dwarf2_find_line_info);
9197 return found;
9198}
9199
252b5132 9200int
a6b96beb 9201_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
252b5132 9202{
8ded5a0f
AM
9203 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9204 int ret = bed->s->sizeof_ehdr;
252b5132 9205
0e1862bb 9206 if (!bfd_link_relocatable (info))
8ded5a0f 9207 {
12bd6957 9208 bfd_size_type phdr_size = elf_program_header_size (abfd);
8ded5a0f 9209
62d7a5f6
AM
9210 if (phdr_size == (bfd_size_type) -1)
9211 {
9212 struct elf_segment_map *m;
9213
9214 phdr_size = 0;
12bd6957 9215 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
62d7a5f6 9216 phdr_size += bed->s->sizeof_phdr;
8ded5a0f 9217
62d7a5f6
AM
9218 if (phdr_size == 0)
9219 phdr_size = get_program_header_size (abfd, info);
9220 }
8ded5a0f 9221
12bd6957 9222 elf_program_header_size (abfd) = phdr_size;
8ded5a0f
AM
9223 ret += phdr_size;
9224 }
9225
252b5132
RH
9226 return ret;
9227}
9228
b34976b6 9229bfd_boolean
217aa764
AM
9230_bfd_elf_set_section_contents (bfd *abfd,
9231 sec_ptr section,
0f867abe 9232 const void *location,
217aa764
AM
9233 file_ptr offset,
9234 bfd_size_type count)
252b5132
RH
9235{
9236 Elf_Internal_Shdr *hdr;
1b6aeedb 9237 file_ptr pos;
252b5132
RH
9238
9239 if (! abfd->output_has_begun
217aa764 9240 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
b34976b6 9241 return FALSE;
252b5132 9242
0ce398f1
L
9243 if (!count)
9244 return TRUE;
9245
252b5132 9246 hdr = &elf_section_data (section)->this_hdr;
0ce398f1
L
9247 if (hdr->sh_offset == (file_ptr) -1)
9248 {
a0dcf297
NC
9249 unsigned char *contents;
9250
1ff6de03
NA
9251 if (bfd_section_is_ctf (section))
9252 /* Nothing to do with this section: the contents are generated
9253 later. */
9254 return TRUE;
9255
a0dcf297
NC
9256 if ((section->flags & SEC_ELF_COMPRESS) == 0)
9257 {
9258 _bfd_error_handler
9259 (_("%pB:%pA: error: attempting to write into an unallocated compressed section"),
9260 abfd, section);
9261 bfd_set_error (bfd_error_invalid_operation);
9262 return FALSE;
9263 }
9264
9265 if ((offset + count) > hdr->sh_size)
9266 {
9267 _bfd_error_handler
9268 (_("%pB:%pA: error: attempting to write over the end of the section"),
9269 abfd, section);
9270
9271 bfd_set_error (bfd_error_invalid_operation);
9272 return FALSE;
9273 }
9274
9275 contents = hdr->contents;
9276 if (contents == NULL)
9277 {
9278 _bfd_error_handler
9279 (_("%pB:%pA: error: attempting to write section into an empty buffer"),
9280 abfd, section);
9281
9282 bfd_set_error (bfd_error_invalid_operation);
9283 return FALSE;
9284 }
9285
0ce398f1
L
9286 memcpy (contents + offset, location, count);
9287 return TRUE;
9288 }
a0dcf297 9289
dc810e39
AM
9290 pos = hdr->sh_offset + offset;
9291 if (bfd_seek (abfd, pos, SEEK_SET) != 0
9292 || bfd_bwrite (location, count, abfd) != count)
b34976b6 9293 return FALSE;
252b5132 9294
b34976b6 9295 return TRUE;
252b5132
RH
9296}
9297
f3185997 9298bfd_boolean
217aa764
AM
9299_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
9300 arelent *cache_ptr ATTRIBUTE_UNUSED,
9301 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
252b5132
RH
9302{
9303 abort ();
f3185997 9304 return FALSE;
252b5132
RH
9305}
9306
252b5132
RH
9307/* Try to convert a non-ELF reloc into an ELF one. */
9308
b34976b6 9309bfd_boolean
217aa764 9310_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
252b5132 9311{
c044fabd 9312 /* Check whether we really have an ELF howto. */
252b5132
RH
9313
9314 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
9315 {
9316 bfd_reloc_code_real_type code;
9317 reloc_howto_type *howto;
9318
9319 /* Alien reloc: Try to determine its type to replace it with an
c044fabd 9320 equivalent ELF reloc. */
252b5132
RH
9321
9322 if (areloc->howto->pc_relative)
9323 {
9324 switch (areloc->howto->bitsize)
9325 {
9326 case 8:
9327 code = BFD_RELOC_8_PCREL;
9328 break;
9329 case 12:
9330 code = BFD_RELOC_12_PCREL;
9331 break;
9332 case 16:
9333 code = BFD_RELOC_16_PCREL;
9334 break;
9335 case 24:
9336 code = BFD_RELOC_24_PCREL;
9337 break;
9338 case 32:
9339 code = BFD_RELOC_32_PCREL;
9340 break;
9341 case 64:
9342 code = BFD_RELOC_64_PCREL;
9343 break;
9344 default:
9345 goto fail;
9346 }
9347
9348 howto = bfd_reloc_type_lookup (abfd, code);
9349
94698d01 9350 if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
252b5132
RH
9351 {
9352 if (howto->pcrel_offset)
9353 areloc->addend += areloc->address;
9354 else
9355 areloc->addend -= areloc->address; /* addend is unsigned!! */
9356 }
9357 }
9358 else
9359 {
9360 switch (areloc->howto->bitsize)
9361 {
9362 case 8:
9363 code = BFD_RELOC_8;
9364 break;
9365 case 14:
9366 code = BFD_RELOC_14;
9367 break;
9368 case 16:
9369 code = BFD_RELOC_16;
9370 break;
9371 case 26:
9372 code = BFD_RELOC_26;
9373 break;
9374 case 32:
9375 code = BFD_RELOC_32;
9376 break;
9377 case 64:
9378 code = BFD_RELOC_64;
9379 break;
9380 default:
9381 goto fail;
9382 }
9383
9384 howto = bfd_reloc_type_lookup (abfd, code);
9385 }
9386
9387 if (howto)
9388 areloc->howto = howto;
9389 else
9390 goto fail;
9391 }
9392
b34976b6 9393 return TRUE;
252b5132
RH
9394
9395 fail:
0aa13fee
AM
9396 /* xgettext:c-format */
9397 _bfd_error_handler (_("%pB: %s unsupported"),
9398 abfd, areloc->howto->name);
9aea1e31 9399 bfd_set_error (bfd_error_sorry);
b34976b6 9400 return FALSE;
252b5132
RH
9401}
9402
b34976b6 9403bfd_boolean
217aa764 9404_bfd_elf_close_and_cleanup (bfd *abfd)
252b5132 9405{
d9071b0c
TG
9406 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9407 if (bfd_get_format (abfd) == bfd_object && tdata != NULL)
252b5132 9408 {
c0355132 9409 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
2b0f7ef9 9410 _bfd_elf_strtab_free (elf_shstrtab (abfd));
d9071b0c 9411 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
252b5132
RH
9412 }
9413
9414 return _bfd_generic_close_and_cleanup (abfd);
9415}
9416
9417/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9418 in the relocation's offset. Thus we cannot allow any sort of sanity
9419 range-checking to interfere. There is nothing else to do in processing
9420 this reloc. */
9421
9422bfd_reloc_status_type
217aa764
AM
9423_bfd_elf_rel_vtable_reloc_fn
9424 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
fc0a2244 9425 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
217aa764
AM
9426 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
9427 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
252b5132
RH
9428{
9429 return bfd_reloc_ok;
9430}
252b5132
RH
9431\f
9432/* Elf core file support. Much of this only works on native
9433 toolchains, since we rely on knowing the
9434 machine-dependent procfs structure in order to pick
c044fabd 9435 out details about the corefile. */
252b5132
RH
9436
9437#ifdef HAVE_SYS_PROCFS_H
16231b7b
DG
9438/* Needed for new procfs interface on sparc-solaris. */
9439# define _STRUCTURED_PROC 1
252b5132
RH
9440# include <sys/procfs.h>
9441#endif
9442
261b8d08
PA
9443/* Return a PID that identifies a "thread" for threaded cores, or the
9444 PID of the main process for non-threaded cores. */
252b5132
RH
9445
9446static int
217aa764 9447elfcore_make_pid (bfd *abfd)
252b5132 9448{
261b8d08
PA
9449 int pid;
9450
228e534f 9451 pid = elf_tdata (abfd)->core->lwpid;
261b8d08 9452 if (pid == 0)
228e534f 9453 pid = elf_tdata (abfd)->core->pid;
261b8d08
PA
9454
9455 return pid;
252b5132
RH
9456}
9457
252b5132
RH
9458/* If there isn't a section called NAME, make one, using
9459 data from SECT. Note, this function will generate a
9460 reference to NAME, so you shouldn't deallocate or
c044fabd 9461 overwrite it. */
252b5132 9462
b34976b6 9463static bfd_boolean
217aa764 9464elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
252b5132 9465{
c044fabd 9466 asection *sect2;
252b5132
RH
9467
9468 if (bfd_get_section_by_name (abfd, name) != NULL)
b34976b6 9469 return TRUE;
252b5132 9470
117ed4f8 9471 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
252b5132 9472 if (sect2 == NULL)
b34976b6 9473 return FALSE;
252b5132 9474
eea6121a 9475 sect2->size = sect->size;
252b5132 9476 sect2->filepos = sect->filepos;
252b5132 9477 sect2->alignment_power = sect->alignment_power;
b34976b6 9478 return TRUE;
252b5132
RH
9479}
9480
bb0082d6
AM
9481/* Create a pseudosection containing SIZE bytes at FILEPOS. This
9482 actually creates up to two pseudosections:
9483 - For the single-threaded case, a section named NAME, unless
9484 such a section already exists.
9485 - For the multi-threaded case, a section named "NAME/PID", where
9486 PID is elfcore_make_pid (abfd).
24d3e51b 9487 Both pseudosections have identical contents. */
b34976b6 9488bfd_boolean
217aa764
AM
9489_bfd_elfcore_make_pseudosection (bfd *abfd,
9490 char *name,
9491 size_t size,
9492 ufile_ptr filepos)
bb0082d6
AM
9493{
9494 char buf[100];
9495 char *threaded_name;
d4c88bbb 9496 size_t len;
bb0082d6
AM
9497 asection *sect;
9498
9499 /* Build the section name. */
9500
9501 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
d4c88bbb 9502 len = strlen (buf) + 1;
a50b1753 9503 threaded_name = (char *) bfd_alloc (abfd, len);
bb0082d6 9504 if (threaded_name == NULL)
b34976b6 9505 return FALSE;
d4c88bbb 9506 memcpy (threaded_name, buf, len);
bb0082d6 9507
117ed4f8
AM
9508 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
9509 SEC_HAS_CONTENTS);
bb0082d6 9510 if (sect == NULL)
b34976b6 9511 return FALSE;
eea6121a 9512 sect->size = size;
bb0082d6 9513 sect->filepos = filepos;
bb0082d6
AM
9514 sect->alignment_power = 2;
9515
936e320b 9516 return elfcore_maybe_make_sect (abfd, name, sect);
bb0082d6
AM
9517}
9518
58e07198
CZ
9519static bfd_boolean
9520elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
9521 size_t offs)
9522{
9523 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9524 SEC_HAS_CONTENTS);
9525
9526 if (sect == NULL)
9527 return FALSE;
9528
9529 sect->size = note->descsz - offs;
9530 sect->filepos = note->descpos + offs;
9531 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9532
9533 return TRUE;
9534}
9535
252b5132 9536/* prstatus_t exists on:
4a938328 9537 solaris 2.5+
252b5132
RH
9538 linux 2.[01] + glibc
9539 unixware 4.2
9540*/
9541
9542#if defined (HAVE_PRSTATUS_T)
a7b97311 9543
b34976b6 9544static bfd_boolean
217aa764 9545elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 9546{
eea6121a 9547 size_t size;
7ee38065 9548 int offset;
252b5132 9549
4a938328
MS
9550 if (note->descsz == sizeof (prstatus_t))
9551 {
9552 prstatus_t prstat;
252b5132 9553
eea6121a 9554 size = sizeof (prstat.pr_reg);
7ee38065 9555 offset = offsetof (prstatus_t, pr_reg);
4a938328 9556 memcpy (&prstat, note->descdata, sizeof (prstat));
252b5132 9557
fa49d224
NC
9558 /* Do not overwrite the core signal if it
9559 has already been set by another thread. */
228e534f
AM
9560 if (elf_tdata (abfd)->core->signal == 0)
9561 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9562 if (elf_tdata (abfd)->core->pid == 0)
9563 elf_tdata (abfd)->core->pid = prstat.pr_pid;
252b5132 9564
4a938328
MS
9565 /* pr_who exists on:
9566 solaris 2.5+
9567 unixware 4.2
9568 pr_who doesn't exist on:
9569 linux 2.[01]
9570 */
252b5132 9571#if defined (HAVE_PRSTATUS_T_PR_WHO)
228e534f 9572 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
261b8d08 9573#else
228e534f 9574 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
252b5132 9575#endif
4a938328 9576 }
7ee38065 9577#if defined (HAVE_PRSTATUS32_T)
4a938328
MS
9578 else if (note->descsz == sizeof (prstatus32_t))
9579 {
9580 /* 64-bit host, 32-bit corefile */
9581 prstatus32_t prstat;
9582
eea6121a 9583 size = sizeof (prstat.pr_reg);
7ee38065 9584 offset = offsetof (prstatus32_t, pr_reg);
4a938328
MS
9585 memcpy (&prstat, note->descdata, sizeof (prstat));
9586
fa49d224
NC
9587 /* Do not overwrite the core signal if it
9588 has already been set by another thread. */
228e534f
AM
9589 if (elf_tdata (abfd)->core->signal == 0)
9590 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9591 if (elf_tdata (abfd)->core->pid == 0)
9592 elf_tdata (abfd)->core->pid = prstat.pr_pid;
4a938328
MS
9593
9594 /* pr_who exists on:
9595 solaris 2.5+
9596 unixware 4.2
9597 pr_who doesn't exist on:
9598 linux 2.[01]
9599 */
7ee38065 9600#if defined (HAVE_PRSTATUS32_T_PR_WHO)
228e534f 9601 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
261b8d08 9602#else
228e534f 9603 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
4a938328
MS
9604#endif
9605 }
7ee38065 9606#endif /* HAVE_PRSTATUS32_T */
4a938328
MS
9607 else
9608 {
9609 /* Fail - we don't know how to handle any other
9610 note size (ie. data object type). */
b34976b6 9611 return TRUE;
4a938328 9612 }
252b5132 9613
bb0082d6 9614 /* Make a ".reg/999" section and a ".reg" section. */
936e320b 9615 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 9616 size, note->descpos + offset);
252b5132
RH
9617}
9618#endif /* defined (HAVE_PRSTATUS_T) */
9619
bb0082d6 9620/* Create a pseudosection containing the exact contents of NOTE. */
b34976b6 9621static bfd_boolean
217aa764
AM
9622elfcore_make_note_pseudosection (bfd *abfd,
9623 char *name,
9624 Elf_Internal_Note *note)
252b5132 9625{
936e320b
AM
9626 return _bfd_elfcore_make_pseudosection (abfd, name,
9627 note->descsz, note->descpos);
252b5132
RH
9628}
9629
ff08c6bb
JB
9630/* There isn't a consistent prfpregset_t across platforms,
9631 but it doesn't matter, because we don't have to pick this
c044fabd
KH
9632 data structure apart. */
9633
b34976b6 9634static bfd_boolean
217aa764 9635elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
9636{
9637 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9638}
9639
ff08c6bb 9640/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
971d4640 9641 type of NT_PRXFPREG. Just include the whole note's contents
ff08c6bb 9642 literally. */
c044fabd 9643
b34976b6 9644static bfd_boolean
217aa764 9645elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
9646{
9647 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9648}
9649
4339cae0
L
9650/* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9651 with a note type of NT_X86_XSTATE. Just include the whole note's
9652 contents literally. */
9653
9654static bfd_boolean
9655elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
9656{
9657 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
9658}
9659
97753bd5
AM
9660static bfd_boolean
9661elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
9662{
9663 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
9664}
9665
89eeb0bc
LM
9666static bfd_boolean
9667elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
9668{
9669 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
9670}
97753bd5 9671
cb2366c1
EBM
9672static bfd_boolean
9673elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
9674{
9675 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
9676}
9677
9678static bfd_boolean
9679elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
9680{
9681 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
9682}
9683
9684static bfd_boolean
9685elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
9686{
9687 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
9688}
9689
9690static bfd_boolean
9691elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
9692{
9693 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
9694}
9695
9696static bfd_boolean
9697elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
9698{
9699 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
9700}
9701
9702static bfd_boolean
9703elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
9704{
9705 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
9706}
9707
9708static bfd_boolean
9709elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
9710{
9711 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
9712}
9713
9714static bfd_boolean
9715elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
9716{
9717 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
9718}
9719
9720static bfd_boolean
9721elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
9722{
9723 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
9724}
9725
9726static bfd_boolean
9727elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
9728{
9729 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
9730}
9731
9732static bfd_boolean
9733elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
9734{
9735 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
9736}
9737
9738static bfd_boolean
9739elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
9740{
9741 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
9742}
9743
9744static bfd_boolean
9745elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
9746{
9747 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
9748}
9749
0675e188
UW
9750static bfd_boolean
9751elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
9752{
9753 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
9754}
9755
d7eeb400
MS
9756static bfd_boolean
9757elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
9758{
9759 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
9760}
9761
9762static bfd_boolean
9763elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
9764{
9765 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
9766}
9767
9768static bfd_boolean
9769elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
9770{
9771 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
9772}
9773
9774static bfd_boolean
9775elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
9776{
9777 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
9778}
9779
9780static bfd_boolean
9781elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
9782{
9783 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
9784}
9785
355b81d9
UW
9786static bfd_boolean
9787elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
9788{
9789 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
9790}
9791
9792static bfd_boolean
9793elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
9794{
9795 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
9796}
9797
abb3f6cc
NC
9798static bfd_boolean
9799elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
9800{
9801 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
9802}
9803
4ef9f41a
AA
9804static bfd_boolean
9805elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
9806{
9807 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
9808}
9809
9810static bfd_boolean
9811elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
9812{
9813 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
9814}
9815
88ab90e8
AA
9816static bfd_boolean
9817elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
9818{
9819 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
9820}
9821
9822static bfd_boolean
9823elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
9824{
9825 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
9826}
9827
faa9a424
UW
9828static bfd_boolean
9829elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
9830{
9831 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
9832}
9833
652451f8
YZ
9834static bfd_boolean
9835elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
9836{
9837 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
9838}
9839
9840static bfd_boolean
9841elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
9842{
9843 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
9844}
9845
9846static bfd_boolean
9847elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
9848{
9849 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
9850}
9851
ad1cc4e4
AH
9852static bfd_boolean
9853elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
9854{
9855 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
9856}
9857
e6c3b5bf
AH
9858static bfd_boolean
9859elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
9860{
9861 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
9862}
9863
252b5132 9864#if defined (HAVE_PRPSINFO_T)
4a938328 9865typedef prpsinfo_t elfcore_psinfo_t;
7ee38065 9866#if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
9867typedef prpsinfo32_t elfcore_psinfo32_t;
9868#endif
252b5132
RH
9869#endif
9870
9871#if defined (HAVE_PSINFO_T)
4a938328 9872typedef psinfo_t elfcore_psinfo_t;
7ee38065 9873#if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
9874typedef psinfo32_t elfcore_psinfo32_t;
9875#endif
252b5132
RH
9876#endif
9877
252b5132
RH
9878/* return a malloc'ed copy of a string at START which is at
9879 most MAX bytes long, possibly without a terminating '\0'.
c044fabd 9880 the copy will always have a terminating '\0'. */
252b5132 9881
936e320b 9882char *
217aa764 9883_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
252b5132 9884{
dc810e39 9885 char *dups;
a50b1753 9886 char *end = (char *) memchr (start, '\0', max);
dc810e39 9887 size_t len;
252b5132
RH
9888
9889 if (end == NULL)
9890 len = max;
9891 else
9892 len = end - start;
9893
a50b1753 9894 dups = (char *) bfd_alloc (abfd, len + 1);
dc810e39 9895 if (dups == NULL)
252b5132
RH
9896 return NULL;
9897
dc810e39
AM
9898 memcpy (dups, start, len);
9899 dups[len] = '\0';
252b5132 9900
dc810e39 9901 return dups;
252b5132
RH
9902}
9903
bb0082d6 9904#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
b34976b6 9905static bfd_boolean
217aa764 9906elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
252b5132 9907{
4a938328
MS
9908 if (note->descsz == sizeof (elfcore_psinfo_t))
9909 {
9910 elfcore_psinfo_t psinfo;
252b5132 9911
7ee38065 9912 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 9913
335e41d4 9914#if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
228e534f 9915 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
335e41d4 9916#endif
228e534f 9917 elf_tdata (abfd)->core->program
936e320b
AM
9918 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9919 sizeof (psinfo.pr_fname));
252b5132 9920
228e534f 9921 elf_tdata (abfd)->core->command
936e320b
AM
9922 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9923 sizeof (psinfo.pr_psargs));
4a938328 9924 }
7ee38065 9925#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
4a938328
MS
9926 else if (note->descsz == sizeof (elfcore_psinfo32_t))
9927 {
9928 /* 64-bit host, 32-bit corefile */
9929 elfcore_psinfo32_t psinfo;
9930
7ee38065 9931 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 9932
335e41d4 9933#if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
228e534f 9934 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
335e41d4 9935#endif
228e534f 9936 elf_tdata (abfd)->core->program
936e320b
AM
9937 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9938 sizeof (psinfo.pr_fname));
4a938328 9939
228e534f 9940 elf_tdata (abfd)->core->command
936e320b
AM
9941 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9942 sizeof (psinfo.pr_psargs));
4a938328
MS
9943 }
9944#endif
9945
9946 else
9947 {
9948 /* Fail - we don't know how to handle any other
9949 note size (ie. data object type). */
b34976b6 9950 return TRUE;
4a938328 9951 }
252b5132
RH
9952
9953 /* Note that for some reason, a spurious space is tacked
9954 onto the end of the args in some (at least one anyway)
c044fabd 9955 implementations, so strip it off if it exists. */
252b5132
RH
9956
9957 {
228e534f 9958 char *command = elf_tdata (abfd)->core->command;
252b5132
RH
9959 int n = strlen (command);
9960
9961 if (0 < n && command[n - 1] == ' ')
9962 command[n - 1] = '\0';
9963 }
9964
b34976b6 9965 return TRUE;
252b5132
RH
9966}
9967#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9968
252b5132 9969#if defined (HAVE_PSTATUS_T)
b34976b6 9970static bfd_boolean
217aa764 9971elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 9972{
f572a39d
AM
9973 if (note->descsz == sizeof (pstatus_t)
9974#if defined (HAVE_PXSTATUS_T)
9975 || note->descsz == sizeof (pxstatus_t)
9976#endif
9977 )
4a938328
MS
9978 {
9979 pstatus_t pstat;
252b5132 9980
4a938328 9981 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 9982
228e534f 9983 elf_tdata (abfd)->core->pid = pstat.pr_pid;
4a938328 9984 }
7ee38065 9985#if defined (HAVE_PSTATUS32_T)
4a938328
MS
9986 else if (note->descsz == sizeof (pstatus32_t))
9987 {
9988 /* 64-bit host, 32-bit corefile */
9989 pstatus32_t pstat;
252b5132 9990
4a938328 9991 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 9992
228e534f 9993 elf_tdata (abfd)->core->pid = pstat.pr_pid;
4a938328
MS
9994 }
9995#endif
252b5132
RH
9996 /* Could grab some more details from the "representative"
9997 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
c044fabd 9998 NT_LWPSTATUS note, presumably. */
252b5132 9999
b34976b6 10000 return TRUE;
252b5132
RH
10001}
10002#endif /* defined (HAVE_PSTATUS_T) */
10003
252b5132 10004#if defined (HAVE_LWPSTATUS_T)
b34976b6 10005static bfd_boolean
217aa764 10006elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132
RH
10007{
10008 lwpstatus_t lwpstat;
10009 char buf[100];
c044fabd 10010 char *name;
d4c88bbb 10011 size_t len;
c044fabd 10012 asection *sect;
252b5132 10013
f572a39d
AM
10014 if (note->descsz != sizeof (lwpstat)
10015#if defined (HAVE_LWPXSTATUS_T)
10016 && note->descsz != sizeof (lwpxstatus_t)
10017#endif
10018 )
b34976b6 10019 return TRUE;
252b5132
RH
10020
10021 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10022
228e534f 10023 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
a1504221
JB
10024 /* Do not overwrite the core signal if it has already been set by
10025 another thread. */
228e534f
AM
10026 if (elf_tdata (abfd)->core->signal == 0)
10027 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
252b5132 10028
c044fabd 10029 /* Make a ".reg/999" section. */
252b5132
RH
10030
10031 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
d4c88bbb 10032 len = strlen (buf) + 1;
217aa764 10033 name = bfd_alloc (abfd, len);
252b5132 10034 if (name == NULL)
b34976b6 10035 return FALSE;
d4c88bbb 10036 memcpy (name, buf, len);
252b5132 10037
117ed4f8 10038 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 10039 if (sect == NULL)
b34976b6 10040 return FALSE;
252b5132
RH
10041
10042#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 10043 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
252b5132
RH
10044 sect->filepos = note->descpos
10045 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10046#endif
10047
10048#if defined (HAVE_LWPSTATUS_T_PR_REG)
eea6121a 10049 sect->size = sizeof (lwpstat.pr_reg);
252b5132
RH
10050 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10051#endif
10052
252b5132
RH
10053 sect->alignment_power = 2;
10054
10055 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
b34976b6 10056 return FALSE;
252b5132
RH
10057
10058 /* Make a ".reg2/999" section */
10059
10060 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
d4c88bbb 10061 len = strlen (buf) + 1;
217aa764 10062 name = bfd_alloc (abfd, len);
252b5132 10063 if (name == NULL)
b34976b6 10064 return FALSE;
d4c88bbb 10065 memcpy (name, buf, len);
252b5132 10066
117ed4f8 10067 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 10068 if (sect == NULL)
b34976b6 10069 return FALSE;
252b5132
RH
10070
10071#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 10072 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
252b5132
RH
10073 sect->filepos = note->descpos
10074 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
10075#endif
10076
10077#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
eea6121a 10078 sect->size = sizeof (lwpstat.pr_fpreg);
252b5132
RH
10079 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
10080#endif
10081
252b5132
RH
10082 sect->alignment_power = 2;
10083
936e320b 10084 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
252b5132
RH
10085}
10086#endif /* defined (HAVE_LWPSTATUS_T) */
10087
b34976b6 10088static bfd_boolean
217aa764 10089elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
16e9c715
NC
10090{
10091 char buf[30];
c044fabd 10092 char *name;
d4c88bbb 10093 size_t len;
c044fabd 10094 asection *sect;
4a6636fb
PA
10095 int type;
10096 int is_active_thread;
10097 bfd_vma base_addr;
16e9c715 10098
4a6636fb 10099 if (note->descsz < 728)
b34976b6 10100 return TRUE;
16e9c715 10101
4a6636fb
PA
10102 if (! CONST_STRNEQ (note->namedata, "win32"))
10103 return TRUE;
10104
10105 type = bfd_get_32 (abfd, note->descdata);
c044fabd 10106
4a6636fb 10107 switch (type)
16e9c715 10108 {
4a6636fb 10109 case 1 /* NOTE_INFO_PROCESS */:
228e534f 10110 /* FIXME: need to add ->core->command. */
4a6636fb 10111 /* process_info.pid */
228e534f 10112 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8);
4a6636fb 10113 /* process_info.signal */
228e534f 10114 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12);
c044fabd 10115 break;
16e9c715 10116
4a6636fb 10117 case 2 /* NOTE_INFO_THREAD */:
16e9c715 10118 /* Make a ".reg/999" section. */
4a6636fb
PA
10119 /* thread_info.tid */
10120 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
c044fabd 10121
d4c88bbb 10122 len = strlen (buf) + 1;
a50b1753 10123 name = (char *) bfd_alloc (abfd, len);
16e9c715 10124 if (name == NULL)
b34976b6 10125 return FALSE;
c044fabd 10126
d4c88bbb 10127 memcpy (name, buf, len);
16e9c715 10128
117ed4f8 10129 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
16e9c715 10130 if (sect == NULL)
b34976b6 10131 return FALSE;
c044fabd 10132
4a6636fb
PA
10133 /* sizeof (thread_info.thread_context) */
10134 sect->size = 716;
10135 /* offsetof (thread_info.thread_context) */
10136 sect->filepos = note->descpos + 12;
16e9c715
NC
10137 sect->alignment_power = 2;
10138
4a6636fb
PA
10139 /* thread_info.is_active_thread */
10140 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
10141
10142 if (is_active_thread)
16e9c715 10143 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
b34976b6 10144 return FALSE;
16e9c715
NC
10145 break;
10146
4a6636fb 10147 case 3 /* NOTE_INFO_MODULE */:
16e9c715 10148 /* Make a ".module/xxxxxxxx" section. */
4a6636fb
PA
10149 /* module_info.base_address */
10150 base_addr = bfd_get_32 (abfd, note->descdata + 4);
0af1713e 10151 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
c044fabd 10152
d4c88bbb 10153 len = strlen (buf) + 1;
a50b1753 10154 name = (char *) bfd_alloc (abfd, len);
16e9c715 10155 if (name == NULL)
b34976b6 10156 return FALSE;
c044fabd 10157
d4c88bbb 10158 memcpy (name, buf, len);
252b5132 10159
117ed4f8 10160 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
c044fabd 10161
16e9c715 10162 if (sect == NULL)
b34976b6 10163 return FALSE;
c044fabd 10164
eea6121a 10165 sect->size = note->descsz;
16e9c715 10166 sect->filepos = note->descpos;
16e9c715
NC
10167 sect->alignment_power = 2;
10168 break;
10169
10170 default:
b34976b6 10171 return TRUE;
16e9c715
NC
10172 }
10173
b34976b6 10174 return TRUE;
16e9c715 10175}
252b5132 10176
b34976b6 10177static bfd_boolean
217aa764 10178elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
252b5132 10179{
9c5bfbb7 10180 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
bb0082d6 10181
252b5132
RH
10182 switch (note->type)
10183 {
10184 default:
b34976b6 10185 return TRUE;
252b5132 10186
252b5132 10187 case NT_PRSTATUS:
bb0082d6
AM
10188 if (bed->elf_backend_grok_prstatus)
10189 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
b34976b6 10190 return TRUE;
bb0082d6 10191#if defined (HAVE_PRSTATUS_T)
252b5132 10192 return elfcore_grok_prstatus (abfd, note);
bb0082d6 10193#else
b34976b6 10194 return TRUE;
252b5132
RH
10195#endif
10196
10197#if defined (HAVE_PSTATUS_T)
10198 case NT_PSTATUS:
10199 return elfcore_grok_pstatus (abfd, note);
10200#endif
10201
10202#if defined (HAVE_LWPSTATUS_T)
10203 case NT_LWPSTATUS:
10204 return elfcore_grok_lwpstatus (abfd, note);
10205#endif
10206
10207 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
10208 return elfcore_grok_prfpreg (abfd, note);
10209
c044fabd 10210 case NT_WIN32PSTATUS:
16e9c715 10211 return elfcore_grok_win32pstatus (abfd, note);
16e9c715 10212
c044fabd 10213 case NT_PRXFPREG: /* Linux SSE extension */
e377ab71
MK
10214 if (note->namesz == 6
10215 && strcmp (note->namedata, "LINUX") == 0)
ff08c6bb
JB
10216 return elfcore_grok_prxfpreg (abfd, note);
10217 else
b34976b6 10218 return TRUE;
ff08c6bb 10219
4339cae0
L
10220 case NT_X86_XSTATE: /* Linux XSAVE extension */
10221 if (note->namesz == 6
10222 && strcmp (note->namedata, "LINUX") == 0)
10223 return elfcore_grok_xstatereg (abfd, note);
10224 else
10225 return TRUE;
10226
97753bd5
AM
10227 case NT_PPC_VMX:
10228 if (note->namesz == 6
10229 && strcmp (note->namedata, "LINUX") == 0)
10230 return elfcore_grok_ppc_vmx (abfd, note);
10231 else
10232 return TRUE;
10233
89eeb0bc
LM
10234 case NT_PPC_VSX:
10235 if (note->namesz == 6
07d6d2b8
AM
10236 && strcmp (note->namedata, "LINUX") == 0)
10237 return elfcore_grok_ppc_vsx (abfd, note);
89eeb0bc 10238 else
07d6d2b8 10239 return TRUE;
89eeb0bc 10240
cb2366c1
EBM
10241 case NT_PPC_TAR:
10242 if (note->namesz == 6
4b24dd1a
AM
10243 && strcmp (note->namedata, "LINUX") == 0)
10244 return elfcore_grok_ppc_tar (abfd, note);
cb2366c1 10245 else
4b24dd1a 10246 return TRUE;
cb2366c1
EBM
10247
10248 case NT_PPC_PPR:
10249 if (note->namesz == 6
4b24dd1a
AM
10250 && strcmp (note->namedata, "LINUX") == 0)
10251 return elfcore_grok_ppc_ppr (abfd, note);
cb2366c1 10252 else
4b24dd1a 10253 return TRUE;
cb2366c1
EBM
10254
10255 case NT_PPC_DSCR:
10256 if (note->namesz == 6
4b24dd1a
AM
10257 && strcmp (note->namedata, "LINUX") == 0)
10258 return elfcore_grok_ppc_dscr (abfd, note);
cb2366c1 10259 else
4b24dd1a 10260 return TRUE;
cb2366c1
EBM
10261
10262 case NT_PPC_EBB:
10263 if (note->namesz == 6
4b24dd1a
AM
10264 && strcmp (note->namedata, "LINUX") == 0)
10265 return elfcore_grok_ppc_ebb (abfd, note);
cb2366c1 10266 else
4b24dd1a 10267 return TRUE;
cb2366c1
EBM
10268
10269 case NT_PPC_PMU:
10270 if (note->namesz == 6
4b24dd1a
AM
10271 && strcmp (note->namedata, "LINUX") == 0)
10272 return elfcore_grok_ppc_pmu (abfd, note);
cb2366c1 10273 else
4b24dd1a 10274 return TRUE;
cb2366c1
EBM
10275
10276 case NT_PPC_TM_CGPR:
10277 if (note->namesz == 6
4b24dd1a
AM
10278 && strcmp (note->namedata, "LINUX") == 0)
10279 return elfcore_grok_ppc_tm_cgpr (abfd, note);
cb2366c1 10280 else
4b24dd1a 10281 return TRUE;
cb2366c1
EBM
10282
10283 case NT_PPC_TM_CFPR:
10284 if (note->namesz == 6
4b24dd1a
AM
10285 && strcmp (note->namedata, "LINUX") == 0)
10286 return elfcore_grok_ppc_tm_cfpr (abfd, note);
cb2366c1 10287 else
4b24dd1a 10288 return TRUE;
cb2366c1
EBM
10289
10290 case NT_PPC_TM_CVMX:
10291 if (note->namesz == 6
4b24dd1a
AM
10292 && strcmp (note->namedata, "LINUX") == 0)
10293 return elfcore_grok_ppc_tm_cvmx (abfd, note);
cb2366c1 10294 else
4b24dd1a 10295 return TRUE;
cb2366c1
EBM
10296
10297 case NT_PPC_TM_CVSX:
10298 if (note->namesz == 6
4b24dd1a
AM
10299 && strcmp (note->namedata, "LINUX") == 0)
10300 return elfcore_grok_ppc_tm_cvsx (abfd, note);
cb2366c1 10301 else
4b24dd1a 10302 return TRUE;
cb2366c1
EBM
10303
10304 case NT_PPC_TM_SPR:
10305 if (note->namesz == 6
4b24dd1a
AM
10306 && strcmp (note->namedata, "LINUX") == 0)
10307 return elfcore_grok_ppc_tm_spr (abfd, note);
cb2366c1 10308 else
4b24dd1a 10309 return TRUE;
cb2366c1
EBM
10310
10311 case NT_PPC_TM_CTAR:
10312 if (note->namesz == 6
4b24dd1a
AM
10313 && strcmp (note->namedata, "LINUX") == 0)
10314 return elfcore_grok_ppc_tm_ctar (abfd, note);
cb2366c1 10315 else
4b24dd1a 10316 return TRUE;
cb2366c1
EBM
10317
10318 case NT_PPC_TM_CPPR:
10319 if (note->namesz == 6
4b24dd1a
AM
10320 && strcmp (note->namedata, "LINUX") == 0)
10321 return elfcore_grok_ppc_tm_cppr (abfd, note);
cb2366c1 10322 else
4b24dd1a 10323 return TRUE;
cb2366c1
EBM
10324
10325 case NT_PPC_TM_CDSCR:
10326 if (note->namesz == 6
4b24dd1a
AM
10327 && strcmp (note->namedata, "LINUX") == 0)
10328 return elfcore_grok_ppc_tm_cdscr (abfd, note);
cb2366c1 10329 else
4b24dd1a 10330 return TRUE;
cb2366c1 10331
0675e188
UW
10332 case NT_S390_HIGH_GPRS:
10333 if (note->namesz == 6
07d6d2b8
AM
10334 && strcmp (note->namedata, "LINUX") == 0)
10335 return elfcore_grok_s390_high_gprs (abfd, note);
0675e188 10336 else
07d6d2b8 10337 return TRUE;
0675e188 10338
d7eeb400
MS
10339 case NT_S390_TIMER:
10340 if (note->namesz == 6
07d6d2b8
AM
10341 && strcmp (note->namedata, "LINUX") == 0)
10342 return elfcore_grok_s390_timer (abfd, note);
d7eeb400 10343 else
07d6d2b8 10344 return TRUE;
d7eeb400
MS
10345
10346 case NT_S390_TODCMP:
10347 if (note->namesz == 6
07d6d2b8
AM
10348 && strcmp (note->namedata, "LINUX") == 0)
10349 return elfcore_grok_s390_todcmp (abfd, note);
d7eeb400 10350 else
07d6d2b8 10351 return TRUE;
d7eeb400
MS
10352
10353 case NT_S390_TODPREG:
10354 if (note->namesz == 6
07d6d2b8
AM
10355 && strcmp (note->namedata, "LINUX") == 0)
10356 return elfcore_grok_s390_todpreg (abfd, note);
d7eeb400 10357 else
07d6d2b8 10358 return TRUE;
d7eeb400
MS
10359
10360 case NT_S390_CTRS:
10361 if (note->namesz == 6
07d6d2b8
AM
10362 && strcmp (note->namedata, "LINUX") == 0)
10363 return elfcore_grok_s390_ctrs (abfd, note);
d7eeb400 10364 else
07d6d2b8 10365 return TRUE;
d7eeb400
MS
10366
10367 case NT_S390_PREFIX:
10368 if (note->namesz == 6
07d6d2b8
AM
10369 && strcmp (note->namedata, "LINUX") == 0)
10370 return elfcore_grok_s390_prefix (abfd, note);
d7eeb400 10371 else
07d6d2b8 10372 return TRUE;
d7eeb400 10373
355b81d9
UW
10374 case NT_S390_LAST_BREAK:
10375 if (note->namesz == 6
07d6d2b8
AM
10376 && strcmp (note->namedata, "LINUX") == 0)
10377 return elfcore_grok_s390_last_break (abfd, note);
355b81d9 10378 else
07d6d2b8 10379 return TRUE;
355b81d9
UW
10380
10381 case NT_S390_SYSTEM_CALL:
10382 if (note->namesz == 6
07d6d2b8
AM
10383 && strcmp (note->namedata, "LINUX") == 0)
10384 return elfcore_grok_s390_system_call (abfd, note);
355b81d9 10385 else
07d6d2b8 10386 return TRUE;
355b81d9 10387
abb3f6cc
NC
10388 case NT_S390_TDB:
10389 if (note->namesz == 6
07d6d2b8
AM
10390 && strcmp (note->namedata, "LINUX") == 0)
10391 return elfcore_grok_s390_tdb (abfd, note);
abb3f6cc 10392 else
07d6d2b8 10393 return TRUE;
abb3f6cc 10394
4ef9f41a
AA
10395 case NT_S390_VXRS_LOW:
10396 if (note->namesz == 6
10397 && strcmp (note->namedata, "LINUX") == 0)
10398 return elfcore_grok_s390_vxrs_low (abfd, note);
10399 else
10400 return TRUE;
10401
10402 case NT_S390_VXRS_HIGH:
10403 if (note->namesz == 6
10404 && strcmp (note->namedata, "LINUX") == 0)
10405 return elfcore_grok_s390_vxrs_high (abfd, note);
10406 else
10407 return TRUE;
10408
88ab90e8
AA
10409 case NT_S390_GS_CB:
10410 if (note->namesz == 6
10411 && strcmp (note->namedata, "LINUX") == 0)
8fe09d74 10412 return elfcore_grok_s390_gs_cb (abfd, note);
88ab90e8
AA
10413 else
10414 return TRUE;
10415
10416 case NT_S390_GS_BC:
10417 if (note->namesz == 6
10418 && strcmp (note->namedata, "LINUX") == 0)
8fe09d74 10419 return elfcore_grok_s390_gs_bc (abfd, note);
88ab90e8
AA
10420 else
10421 return TRUE;
10422
faa9a424
UW
10423 case NT_ARM_VFP:
10424 if (note->namesz == 6
10425 && strcmp (note->namedata, "LINUX") == 0)
10426 return elfcore_grok_arm_vfp (abfd, note);
10427 else
10428 return TRUE;
10429
652451f8
YZ
10430 case NT_ARM_TLS:
10431 if (note->namesz == 6
10432 && strcmp (note->namedata, "LINUX") == 0)
10433 return elfcore_grok_aarch_tls (abfd, note);
10434 else
10435 return TRUE;
10436
10437 case NT_ARM_HW_BREAK:
10438 if (note->namesz == 6
10439 && strcmp (note->namedata, "LINUX") == 0)
10440 return elfcore_grok_aarch_hw_break (abfd, note);
10441 else
10442 return TRUE;
10443
10444 case NT_ARM_HW_WATCH:
10445 if (note->namesz == 6
10446 && strcmp (note->namedata, "LINUX") == 0)
10447 return elfcore_grok_aarch_hw_watch (abfd, note);
10448 else
10449 return TRUE;
10450
ad1cc4e4
AH
10451 case NT_ARM_SVE:
10452 if (note->namesz == 6
10453 && strcmp (note->namedata, "LINUX") == 0)
10454 return elfcore_grok_aarch_sve (abfd, note);
10455 else
10456 return TRUE;
10457
e6c3b5bf
AH
10458 case NT_ARM_PAC_MASK:
10459 if (note->namesz == 6
10460 && strcmp (note->namedata, "LINUX") == 0)
10461 return elfcore_grok_aarch_pauth (abfd, note);
10462 else
10463 return TRUE;
10464
252b5132
RH
10465 case NT_PRPSINFO:
10466 case NT_PSINFO:
bb0082d6
AM
10467 if (bed->elf_backend_grok_psinfo)
10468 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
b34976b6 10469 return TRUE;
bb0082d6 10470#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
252b5132 10471 return elfcore_grok_psinfo (abfd, note);
bb0082d6 10472#else
b34976b6 10473 return TRUE;
252b5132 10474#endif
3333a7c3
RM
10475
10476 case NT_AUXV:
58e07198 10477 return elfcore_make_auxv_note_section (abfd, note, 0);
9015683b 10478
451b7c33
TT
10479 case NT_FILE:
10480 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
10481 note);
10482
9015683b
TT
10483 case NT_SIGINFO:
10484 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
10485 note);
5b2c414d 10486
252b5132
RH
10487 }
10488}
10489
718175fa
JK
10490static bfd_boolean
10491elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
10492{
c74f7d1c 10493 struct bfd_build_id* build_id;
30e8ee25
AM
10494
10495 if (note->descsz == 0)
10496 return FALSE;
10497
c74f7d1c
JT
10498 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
10499 if (build_id == NULL)
718175fa
JK
10500 return FALSE;
10501
c74f7d1c
JT
10502 build_id->size = note->descsz;
10503 memcpy (build_id->data, note->descdata, note->descsz);
10504 abfd->build_id = build_id;
718175fa
JK
10505
10506 return TRUE;
10507}
10508
10509static bfd_boolean
10510elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
10511{
10512 switch (note->type)
10513 {
10514 default:
10515 return TRUE;
10516
46bed679
L
10517 case NT_GNU_PROPERTY_TYPE_0:
10518 return _bfd_elf_parse_gnu_properties (abfd, note);
10519
718175fa
JK
10520 case NT_GNU_BUILD_ID:
10521 return elfobj_grok_gnu_build_id (abfd, note);
10522 }
10523}
10524
e21e5835
NC
10525static bfd_boolean
10526elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
10527{
10528 struct sdt_note *cur =
7a6e0d89
AM
10529 (struct sdt_note *) bfd_alloc (abfd,
10530 sizeof (struct sdt_note) + note->descsz);
e21e5835
NC
10531
10532 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
10533 cur->size = (bfd_size_type) note->descsz;
10534 memcpy (cur->data, note->descdata, note->descsz);
10535
10536 elf_tdata (abfd)->sdt_note_head = cur;
10537
10538 return TRUE;
10539}
10540
10541static bfd_boolean
10542elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
10543{
10544 switch (note->type)
10545 {
10546 case NT_STAPSDT:
10547 return elfobj_grok_stapsdt_note_1 (abfd, note);
10548
10549 default:
10550 return TRUE;
10551 }
10552}
10553
aa1ed4a9
JB
10554static bfd_boolean
10555elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
10556{
10557 size_t offset;
10558
b5430a3c 10559 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
aa1ed4a9 10560 {
b5430a3c 10561 case ELFCLASS32:
0064d223
JB
10562 if (note->descsz < 108)
10563 return FALSE;
aa1ed4a9
JB
10564 break;
10565
b5430a3c 10566 case ELFCLASS64:
0064d223
JB
10567 if (note->descsz < 120)
10568 return FALSE;
aa1ed4a9
JB
10569 break;
10570
10571 default:
10572 return FALSE;
10573 }
10574
0064d223
JB
10575 /* Check for version 1 in pr_version. */
10576 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10577 return FALSE;
80a04378 10578
0064d223
JB
10579 offset = 4;
10580
10581 /* Skip over pr_psinfosz. */
b5430a3c 10582 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
0064d223
JB
10583 offset += 4;
10584 else
10585 {
10586 offset += 4; /* Padding before pr_psinfosz. */
10587 offset += 8;
10588 }
10589
aa1ed4a9
JB
10590 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10591 elf_tdata (abfd)->core->program
10592 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
10593 offset += 17;
10594
10595 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10596 elf_tdata (abfd)->core->command
10597 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
0064d223
JB
10598 offset += 81;
10599
10600 /* Padding before pr_pid. */
10601 offset += 2;
10602
10603 /* The pr_pid field was added in version "1a". */
10604 if (note->descsz < offset + 4)
10605 return TRUE;
10606
10607 elf_tdata (abfd)->core->pid
10608 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
aa1ed4a9
JB
10609
10610 return TRUE;
10611}
10612
10613static bfd_boolean
10614elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
10615{
10616 size_t offset;
10617 size_t size;
24d3e51b 10618 size_t min_size;
aa1ed4a9 10619
24d3e51b
NC
10620 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10621 Also compute minimum size of this note. */
b5430a3c 10622 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
aa1ed4a9 10623 {
b5430a3c 10624 case ELFCLASS32:
24d3e51b
NC
10625 offset = 4 + 4;
10626 min_size = offset + (4 * 2) + 4 + 4 + 4;
aa1ed4a9
JB
10627 break;
10628
b5430a3c 10629 case ELFCLASS64:
24d3e51b
NC
10630 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
10631 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
aa1ed4a9
JB
10632 break;
10633
10634 default:
10635 return FALSE;
10636 }
10637
24d3e51b
NC
10638 if (note->descsz < min_size)
10639 return FALSE;
10640
10641 /* Check for version 1 in pr_version. */
10642 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10643 return FALSE;
aa1ed4a9 10644
24d3e51b
NC
10645 /* Extract size of pr_reg from pr_gregsetsz. */
10646 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
b5430a3c 10647 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
24d3e51b
NC
10648 {
10649 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10650 offset += 4 * 2;
10651 }
b5430a3c 10652 else
24d3e51b
NC
10653 {
10654 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
10655 offset += 8 * 2;
10656 }
aa1ed4a9 10657
24d3e51b 10658 /* Skip over pr_osreldate. */
aa1ed4a9
JB
10659 offset += 4;
10660
24d3e51b 10661 /* Read signal from pr_cursig. */
aa1ed4a9
JB
10662 if (elf_tdata (abfd)->core->signal == 0)
10663 elf_tdata (abfd)->core->signal
10664 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10665 offset += 4;
10666
24d3e51b 10667 /* Read TID from pr_pid. */
aa1ed4a9
JB
10668 elf_tdata (abfd)->core->lwpid
10669 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10670 offset += 4;
10671
24d3e51b 10672 /* Padding before pr_reg. */
b5430a3c 10673 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
aa1ed4a9
JB
10674 offset += 4;
10675
24d3e51b
NC
10676 /* Make sure that there is enough data remaining in the note. */
10677 if ((note->descsz - offset) < size)
10678 return FALSE;
10679
aa1ed4a9
JB
10680 /* Make a ".reg/999" section and a ".reg" section. */
10681 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10682 size, note->descpos + offset);
10683}
10684
10685static bfd_boolean
10686elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
10687{
544c67cd
JB
10688 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10689
aa1ed4a9
JB
10690 switch (note->type)
10691 {
10692 case NT_PRSTATUS:
544c67cd
JB
10693 if (bed->elf_backend_grok_freebsd_prstatus)
10694 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
10695 return TRUE;
aa1ed4a9
JB
10696 return elfcore_grok_freebsd_prstatus (abfd, note);
10697
10698 case NT_FPREGSET:
10699 return elfcore_grok_prfpreg (abfd, note);
10700
10701 case NT_PRPSINFO:
10702 return elfcore_grok_freebsd_psinfo (abfd, note);
10703
10704 case NT_FREEBSD_THRMISC:
10705 if (note->namesz == 8)
10706 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
10707 else
10708 return TRUE;
10709
ddb2bbcf
JB
10710 case NT_FREEBSD_PROCSTAT_PROC:
10711 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
10712 note);
10713
10714 case NT_FREEBSD_PROCSTAT_FILES:
10715 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
10716 note);
10717
10718 case NT_FREEBSD_PROCSTAT_VMMAP:
10719 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
10720 note);
10721
3350c5f5 10722 case NT_FREEBSD_PROCSTAT_AUXV:
58e07198 10723 return elfcore_make_auxv_note_section (abfd, note, 4);
3350c5f5 10724
aa1ed4a9
JB
10725 case NT_X86_XSTATE:
10726 if (note->namesz == 8)
10727 return elfcore_grok_xstatereg (abfd, note);
10728 else
10729 return TRUE;
10730
e6f3b9c3
JB
10731 case NT_FREEBSD_PTLWPINFO:
10732 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
10733 note);
10734
6d5be5d6
JB
10735 case NT_ARM_VFP:
10736 return elfcore_grok_arm_vfp (abfd, note);
10737
aa1ed4a9
JB
10738 default:
10739 return TRUE;
10740 }
10741}
10742
b34976b6 10743static bfd_boolean
217aa764 10744elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
50b2bdb7
AM
10745{
10746 char *cp;
10747
10748 cp = strchr (note->namedata, '@');
10749 if (cp != NULL)
10750 {
d2b64500 10751 *lwpidp = atoi(cp + 1);
b34976b6 10752 return TRUE;
50b2bdb7 10753 }
b34976b6 10754 return FALSE;
50b2bdb7
AM
10755}
10756
b34976b6 10757static bfd_boolean
217aa764 10758elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7 10759{
80a04378
NC
10760 if (note->descsz <= 0x7c + 31)
10761 return FALSE;
10762
50b2bdb7 10763 /* Signal number at offset 0x08. */
228e534f 10764 elf_tdata (abfd)->core->signal
50b2bdb7
AM
10765 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10766
10767 /* Process ID at offset 0x50. */
228e534f 10768 elf_tdata (abfd)->core->pid
50b2bdb7
AM
10769 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
10770
10771 /* Command name at 0x7c (max 32 bytes, including nul). */
228e534f 10772 elf_tdata (abfd)->core->command
50b2bdb7
AM
10773 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
10774
7720ba9f
MK
10775 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
10776 note);
50b2bdb7
AM
10777}
10778
b34976b6 10779static bfd_boolean
217aa764 10780elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7
AM
10781{
10782 int lwp;
10783
10784 if (elfcore_netbsd_get_lwpid (note, &lwp))
228e534f 10785 elf_tdata (abfd)->core->lwpid = lwp;
50b2bdb7 10786
58e07198 10787 switch (note->type)
50b2bdb7 10788 {
58e07198 10789 case NT_NETBSDCORE_PROCINFO:
50b2bdb7 10790 /* NetBSD-specific core "procinfo". Note that we expect to
08a40648
AM
10791 find this note before any of the others, which is fine,
10792 since the kernel writes this note out first when it
10793 creates a core file. */
50b2bdb7 10794 return elfcore_grok_netbsd_procinfo (abfd, note);
58e07198
CZ
10795#ifdef NT_NETBSDCORE_AUXV
10796 case NT_NETBSDCORE_AUXV:
10797 /* NetBSD-specific Elf Auxiliary Vector data. */
10798 return elfcore_make_auxv_note_section (abfd, note, 4);
06d949ec
KR
10799#endif
10800#ifdef NT_NETBSDCORE_LWPSTATUS
10801 case NT_NETBSDCORE_LWPSTATUS:
10802 return elfcore_make_note_pseudosection (abfd,
10803 ".note.netbsdcore.lwpstatus",
10804 note);
58e07198
CZ
10805#endif
10806 default:
10807 break;
50b2bdb7
AM
10808 }
10809
06d949ec 10810 /* As of March 2020 there are no other machine-independent notes
b4db1224
JT
10811 defined for NetBSD core files. If the note type is less
10812 than the start of the machine-dependent note types, we don't
10813 understand it. */
47d9a591 10814
b4db1224 10815 if (note->type < NT_NETBSDCORE_FIRSTMACH)
b34976b6 10816 return TRUE;
50b2bdb7
AM
10817
10818
10819 switch (bfd_get_arch (abfd))
10820 {
08a40648
AM
10821 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10822 PT_GETFPREGS == mach+2. */
50b2bdb7 10823
015ec493 10824 case bfd_arch_aarch64:
50b2bdb7
AM
10825 case bfd_arch_alpha:
10826 case bfd_arch_sparc:
10827 switch (note->type)
08a40648
AM
10828 {
10829 case NT_NETBSDCORE_FIRSTMACH+0:
10830 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 10831
08a40648
AM
10832 case NT_NETBSDCORE_FIRSTMACH+2:
10833 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 10834
08a40648
AM
10835 default:
10836 return TRUE;
10837 }
50b2bdb7 10838
58e07198
CZ
10839 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
10840 There's also old PT___GETREGS40 == mach + 1 for old reg
10841 structure which lacks GBR. */
10842
10843 case bfd_arch_sh:
10844 switch (note->type)
10845 {
10846 case NT_NETBSDCORE_FIRSTMACH+3:
10847 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10848
10849 case NT_NETBSDCORE_FIRSTMACH+5:
10850 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10851
10852 default:
10853 return TRUE;
10854 }
10855
08a40648
AM
10856 /* On all other arch's, PT_GETREGS == mach+1 and
10857 PT_GETFPREGS == mach+3. */
50b2bdb7
AM
10858
10859 default:
10860 switch (note->type)
08a40648
AM
10861 {
10862 case NT_NETBSDCORE_FIRSTMACH+1:
10863 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 10864
08a40648
AM
10865 case NT_NETBSDCORE_FIRSTMACH+3:
10866 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 10867
08a40648
AM
10868 default:
10869 return TRUE;
10870 }
50b2bdb7
AM
10871 }
10872 /* NOTREACHED */
10873}
10874
67cc5033
MK
10875static bfd_boolean
10876elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
10877{
80a04378
NC
10878 if (note->descsz <= 0x48 + 31)
10879 return FALSE;
10880
67cc5033 10881 /* Signal number at offset 0x08. */
228e534f 10882 elf_tdata (abfd)->core->signal
67cc5033
MK
10883 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10884
10885 /* Process ID at offset 0x20. */
228e534f 10886 elf_tdata (abfd)->core->pid
67cc5033
MK
10887 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
10888
10889 /* Command name at 0x48 (max 32 bytes, including nul). */
228e534f 10890 elf_tdata (abfd)->core->command
67cc5033
MK
10891 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
10892
10893 return TRUE;
10894}
10895
10896static bfd_boolean
10897elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
10898{
10899 if (note->type == NT_OPENBSD_PROCINFO)
10900 return elfcore_grok_openbsd_procinfo (abfd, note);
10901
10902 if (note->type == NT_OPENBSD_REGS)
10903 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10904
10905 if (note->type == NT_OPENBSD_FPREGS)
10906 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10907
10908 if (note->type == NT_OPENBSD_XFPREGS)
10909 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
10910
10911 if (note->type == NT_OPENBSD_AUXV)
58e07198 10912 return elfcore_make_auxv_note_section (abfd, note, 0);
67cc5033
MK
10913
10914 if (note->type == NT_OPENBSD_WCOOKIE)
10915 {
10916 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
10917 SEC_HAS_CONTENTS);
10918
10919 if (sect == NULL)
10920 return FALSE;
10921 sect->size = note->descsz;
10922 sect->filepos = note->descpos;
10923 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10924
10925 return TRUE;
10926 }
10927
10928 return TRUE;
10929}
10930
07c6e936 10931static bfd_boolean
d3fd4074 10932elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
07c6e936
NC
10933{
10934 void *ddata = note->descdata;
10935 char buf[100];
10936 char *name;
10937 asection *sect;
f8843e87
AM
10938 short sig;
10939 unsigned flags;
07c6e936 10940
80a04378
NC
10941 if (note->descsz < 16)
10942 return FALSE;
10943
07c6e936 10944 /* nto_procfs_status 'pid' field is at offset 0. */
228e534f 10945 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
07c6e936 10946
f8843e87
AM
10947 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
10948 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
10949
10950 /* nto_procfs_status 'flags' field is at offset 8. */
10951 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
07c6e936
NC
10952
10953 /* nto_procfs_status 'what' field is at offset 14. */
f8843e87
AM
10954 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
10955 {
228e534f
AM
10956 elf_tdata (abfd)->core->signal = sig;
10957 elf_tdata (abfd)->core->lwpid = *tid;
f8843e87 10958 }
07c6e936 10959
f8843e87
AM
10960 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
10961 do not come from signals so we make sure we set the current
10962 thread just in case. */
10963 if (flags & 0x00000080)
228e534f 10964 elf_tdata (abfd)->core->lwpid = *tid;
07c6e936
NC
10965
10966 /* Make a ".qnx_core_status/%d" section. */
d3fd4074 10967 sprintf (buf, ".qnx_core_status/%ld", *tid);
07c6e936 10968
a50b1753 10969 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
07c6e936
NC
10970 if (name == NULL)
10971 return FALSE;
10972 strcpy (name, buf);
10973
117ed4f8 10974 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936
NC
10975 if (sect == NULL)
10976 return FALSE;
10977
07d6d2b8
AM
10978 sect->size = note->descsz;
10979 sect->filepos = note->descpos;
07c6e936
NC
10980 sect->alignment_power = 2;
10981
10982 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
10983}
10984
10985static bfd_boolean
d69f560c
KW
10986elfcore_grok_nto_regs (bfd *abfd,
10987 Elf_Internal_Note *note,
d3fd4074 10988 long tid,
d69f560c 10989 char *base)
07c6e936
NC
10990{
10991 char buf[100];
10992 char *name;
10993 asection *sect;
10994
d69f560c 10995 /* Make a "(base)/%d" section. */
d3fd4074 10996 sprintf (buf, "%s/%ld", base, tid);
07c6e936 10997
a50b1753 10998 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
07c6e936
NC
10999 if (name == NULL)
11000 return FALSE;
11001 strcpy (name, buf);
11002
117ed4f8 11003 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936
NC
11004 if (sect == NULL)
11005 return FALSE;
11006
07d6d2b8
AM
11007 sect->size = note->descsz;
11008 sect->filepos = note->descpos;
07c6e936
NC
11009 sect->alignment_power = 2;
11010
f8843e87 11011 /* This is the current thread. */
228e534f 11012 if (elf_tdata (abfd)->core->lwpid == tid)
d69f560c 11013 return elfcore_maybe_make_sect (abfd, base, sect);
f8843e87
AM
11014
11015 return TRUE;
07c6e936
NC
11016}
11017
11018#define BFD_QNT_CORE_INFO 7
11019#define BFD_QNT_CORE_STATUS 8
11020#define BFD_QNT_CORE_GREG 9
11021#define BFD_QNT_CORE_FPREG 10
11022
11023static bfd_boolean
217aa764 11024elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
07c6e936
NC
11025{
11026 /* Every GREG section has a STATUS section before it. Store the
811072d8 11027 tid from the previous call to pass down to the next gregs
07c6e936 11028 function. */
d3fd4074 11029 static long tid = 1;
07c6e936
NC
11030
11031 switch (note->type)
11032 {
d69f560c
KW
11033 case BFD_QNT_CORE_INFO:
11034 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
11035 case BFD_QNT_CORE_STATUS:
11036 return elfcore_grok_nto_status (abfd, note, &tid);
11037 case BFD_QNT_CORE_GREG:
11038 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
11039 case BFD_QNT_CORE_FPREG:
11040 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
11041 default:
11042 return TRUE;
07c6e936
NC
11043 }
11044}
11045
b15fa79e
AM
11046static bfd_boolean
11047elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
11048{
11049 char *name;
11050 asection *sect;
11051 size_t len;
11052
11053 /* Use note name as section name. */
11054 len = note->namesz;
a50b1753 11055 name = (char *) bfd_alloc (abfd, len);
b15fa79e
AM
11056 if (name == NULL)
11057 return FALSE;
11058 memcpy (name, note->namedata, len);
11059 name[len - 1] = '\0';
11060
11061 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11062 if (sect == NULL)
11063 return FALSE;
11064
07d6d2b8
AM
11065 sect->size = note->descsz;
11066 sect->filepos = note->descpos;
b15fa79e
AM
11067 sect->alignment_power = 1;
11068
11069 return TRUE;
11070}
11071
7c76fa91
MS
11072/* Function: elfcore_write_note
11073
47d9a591 11074 Inputs:
a39f3346 11075 buffer to hold note, and current size of buffer
7c76fa91
MS
11076 name of note
11077 type of note
11078 data for note
11079 size of data for note
11080
a39f3346
AM
11081 Writes note to end of buffer. ELF64 notes are written exactly as
11082 for ELF32, despite the current (as of 2006) ELF gabi specifying
11083 that they ought to have 8-byte namesz and descsz field, and have
11084 8-byte alignment. Other writers, eg. Linux kernel, do the same.
11085
7c76fa91 11086 Return:
a39f3346 11087 Pointer to realloc'd buffer, *BUFSIZ updated. */
7c76fa91
MS
11088
11089char *
a39f3346 11090elfcore_write_note (bfd *abfd,
217aa764 11091 char *buf,
a39f3346 11092 int *bufsiz,
217aa764 11093 const char *name,
a39f3346 11094 int type,
217aa764 11095 const void *input,
a39f3346 11096 int size)
7c76fa91
MS
11097{
11098 Elf_External_Note *xnp;
d4c88bbb 11099 size_t namesz;
d4c88bbb 11100 size_t newspace;
a39f3346 11101 char *dest;
7c76fa91 11102
d4c88bbb 11103 namesz = 0;
d4c88bbb 11104 if (name != NULL)
a39f3346 11105 namesz = strlen (name) + 1;
d4c88bbb 11106
a39f3346 11107 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
d4c88bbb 11108
a50b1753 11109 buf = (char *) realloc (buf, *bufsiz + newspace);
14b1c01e
AM
11110 if (buf == NULL)
11111 return buf;
a39f3346 11112 dest = buf + *bufsiz;
7c76fa91
MS
11113 *bufsiz += newspace;
11114 xnp = (Elf_External_Note *) dest;
11115 H_PUT_32 (abfd, namesz, xnp->namesz);
11116 H_PUT_32 (abfd, size, xnp->descsz);
11117 H_PUT_32 (abfd, type, xnp->type);
d4c88bbb
AM
11118 dest = xnp->name;
11119 if (name != NULL)
11120 {
11121 memcpy (dest, name, namesz);
11122 dest += namesz;
a39f3346 11123 while (namesz & 3)
d4c88bbb
AM
11124 {
11125 *dest++ = '\0';
a39f3346 11126 ++namesz;
d4c88bbb
AM
11127 }
11128 }
11129 memcpy (dest, input, size);
a39f3346
AM
11130 dest += size;
11131 while (size & 3)
11132 {
11133 *dest++ = '\0';
11134 ++size;
11135 }
11136 return buf;
7c76fa91
MS
11137}
11138
602f1657
AM
11139/* gcc-8 warns (*) on all the strncpy calls in this function about
11140 possible string truncation. The "truncation" is not a bug. We
11141 have an external representation of structs with fields that are not
11142 necessarily NULL terminated and corresponding internal
11143 representation fields that are one larger so that they can always
11144 be NULL terminated.
11145 gcc versions between 4.2 and 4.6 do not allow pragma control of
11146 diagnostics inside functions, giving a hard error if you try to use
11147 the finer control available with later versions.
11148 gcc prior to 4.2 warns about diagnostic push and pop.
11149 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
11150 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
11151 (*) Depending on your system header files! */
d99b4b92 11152#if GCC_VERSION >= 8000
602f1657
AM
11153# pragma GCC diagnostic push
11154# pragma GCC diagnostic ignored "-Wstringop-truncation"
d99b4b92 11155#endif
7c76fa91 11156char *
217aa764
AM
11157elfcore_write_prpsinfo (bfd *abfd,
11158 char *buf,
11159 int *bufsiz,
11160 const char *fname,
11161 const char *psargs)
7c76fa91 11162{
183e98be
AM
11163 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11164
11165 if (bed->elf_backend_write_core_note != NULL)
11166 {
11167 char *ret;
11168 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11169 NT_PRPSINFO, fname, psargs);
11170 if (ret != NULL)
11171 return ret;
11172 }
7c76fa91 11173
1f20dca5 11174#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
602f1657 11175# if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
183e98be
AM
11176 if (bed->s->elfclass == ELFCLASS32)
11177 {
602f1657 11178# if defined (HAVE_PSINFO32_T)
183e98be
AM
11179 psinfo32_t data;
11180 int note_type = NT_PSINFO;
602f1657 11181# else
183e98be
AM
11182 prpsinfo32_t data;
11183 int note_type = NT_PRPSINFO;
602f1657 11184# endif
183e98be
AM
11185
11186 memset (&data, 0, sizeof (data));
11187 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11188 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11189 return elfcore_write_note (abfd, buf, bufsiz,
1f20dca5 11190 "CORE", note_type, &data, sizeof (data));
183e98be
AM
11191 }
11192 else
602f1657 11193# endif
183e98be 11194 {
602f1657 11195# if defined (HAVE_PSINFO_T)
183e98be
AM
11196 psinfo_t data;
11197 int note_type = NT_PSINFO;
602f1657 11198# else
183e98be
AM
11199 prpsinfo_t data;
11200 int note_type = NT_PRPSINFO;
602f1657 11201# endif
7c76fa91 11202
183e98be
AM
11203 memset (&data, 0, sizeof (data));
11204 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11205 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11206 return elfcore_write_note (abfd, buf, bufsiz,
1f20dca5 11207 "CORE", note_type, &data, sizeof (data));
183e98be 11208 }
7c76fa91
MS
11209#endif /* PSINFO_T or PRPSINFO_T */
11210
1f20dca5
UW
11211 free (buf);
11212 return NULL;
11213}
d99b4b92 11214#if GCC_VERSION >= 8000
602f1657 11215# pragma GCC diagnostic pop
d99b4b92 11216#endif
1f20dca5 11217
70a38d42
SDJ
11218char *
11219elfcore_write_linux_prpsinfo32
11220 (bfd *abfd, char *buf, int *bufsiz,
11221 const struct elf_internal_linux_prpsinfo *prpsinfo)
11222{
a2f63b2e
MR
11223 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
11224 {
11225 struct elf_external_linux_prpsinfo32_ugid16 data;
11226
11227 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
11228 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11229 &data, sizeof (data));
11230 }
11231 else
11232 {
11233 struct elf_external_linux_prpsinfo32_ugid32 data;
70a38d42 11234
a2f63b2e
MR
11235 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
11236 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11237 &data, sizeof (data));
11238 }
70a38d42
SDJ
11239}
11240
11241char *
11242elfcore_write_linux_prpsinfo64
11243 (bfd *abfd, char *buf, int *bufsiz,
11244 const struct elf_internal_linux_prpsinfo *prpsinfo)
11245{
3c9a7b0d
MR
11246 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
11247 {
11248 struct elf_external_linux_prpsinfo64_ugid16 data;
11249
11250 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
11251 return elfcore_write_note (abfd, buf, bufsiz,
11252 "CORE", NT_PRPSINFO, &data, sizeof (data));
11253 }
11254 else
11255 {
11256 struct elf_external_linux_prpsinfo64_ugid32 data;
70a38d42 11257
3c9a7b0d
MR
11258 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
11259 return elfcore_write_note (abfd, buf, bufsiz,
11260 "CORE", NT_PRPSINFO, &data, sizeof (data));
11261 }
70a38d42
SDJ
11262}
11263
7c76fa91 11264char *
217aa764
AM
11265elfcore_write_prstatus (bfd *abfd,
11266 char *buf,
11267 int *bufsiz,
11268 long pid,
11269 int cursig,
11270 const void *gregs)
7c76fa91 11271{
183e98be 11272 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 11273
183e98be
AM
11274 if (bed->elf_backend_write_core_note != NULL)
11275 {
11276 char *ret;
11277 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11278 NT_PRSTATUS,
11279 pid, cursig, gregs);
11280 if (ret != NULL)
11281 return ret;
11282 }
11283
1f20dca5 11284#if defined (HAVE_PRSTATUS_T)
183e98be
AM
11285#if defined (HAVE_PRSTATUS32_T)
11286 if (bed->s->elfclass == ELFCLASS32)
11287 {
11288 prstatus32_t prstat;
11289
11290 memset (&prstat, 0, sizeof (prstat));
11291 prstat.pr_pid = pid;
11292 prstat.pr_cursig = cursig;
11293 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
1f20dca5 11294 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
183e98be
AM
11295 NT_PRSTATUS, &prstat, sizeof (prstat));
11296 }
11297 else
11298#endif
11299 {
11300 prstatus_t prstat;
11301
11302 memset (&prstat, 0, sizeof (prstat));
11303 prstat.pr_pid = pid;
11304 prstat.pr_cursig = cursig;
11305 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
1f20dca5 11306 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
183e98be
AM
11307 NT_PRSTATUS, &prstat, sizeof (prstat));
11308 }
7c76fa91
MS
11309#endif /* HAVE_PRSTATUS_T */
11310
1f20dca5
UW
11311 free (buf);
11312 return NULL;
11313}
11314
51316059
MS
11315#if defined (HAVE_LWPSTATUS_T)
11316char *
217aa764
AM
11317elfcore_write_lwpstatus (bfd *abfd,
11318 char *buf,
11319 int *bufsiz,
11320 long pid,
11321 int cursig,
11322 const void *gregs)
51316059
MS
11323{
11324 lwpstatus_t lwpstat;
183e98be 11325 const char *note_name = "CORE";
51316059
MS
11326
11327 memset (&lwpstat, 0, sizeof (lwpstat));
11328 lwpstat.pr_lwpid = pid >> 16;
11329 lwpstat.pr_cursig = cursig;
11330#if defined (HAVE_LWPSTATUS_T_PR_REG)
d1e8523e 11331 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
51316059
MS
11332#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11333#if !defined(gregs)
11334 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
11335 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
11336#else
11337 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
11338 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
11339#endif
11340#endif
47d9a591 11341 return elfcore_write_note (abfd, buf, bufsiz, note_name,
51316059
MS
11342 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
11343}
11344#endif /* HAVE_LWPSTATUS_T */
11345
7c76fa91
MS
11346#if defined (HAVE_PSTATUS_T)
11347char *
217aa764
AM
11348elfcore_write_pstatus (bfd *abfd,
11349 char *buf,
11350 int *bufsiz,
11351 long pid,
6c10990d
NC
11352 int cursig ATTRIBUTE_UNUSED,
11353 const void *gregs ATTRIBUTE_UNUSED)
7c76fa91 11354{
183e98be
AM
11355 const char *note_name = "CORE";
11356#if defined (HAVE_PSTATUS32_T)
11357 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 11358
183e98be
AM
11359 if (bed->s->elfclass == ELFCLASS32)
11360 {
11361 pstatus32_t pstat;
11362
11363 memset (&pstat, 0, sizeof (pstat));
11364 pstat.pr_pid = pid & 0xffff;
11365 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11366 NT_PSTATUS, &pstat, sizeof (pstat));
11367 return buf;
11368 }
11369 else
11370#endif
11371 {
11372 pstatus_t pstat;
11373
11374 memset (&pstat, 0, sizeof (pstat));
11375 pstat.pr_pid = pid & 0xffff;
11376 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11377 NT_PSTATUS, &pstat, sizeof (pstat));
11378 return buf;
11379 }
7c76fa91
MS
11380}
11381#endif /* HAVE_PSTATUS_T */
11382
11383char *
217aa764
AM
11384elfcore_write_prfpreg (bfd *abfd,
11385 char *buf,
11386 int *bufsiz,
11387 const void *fpregs,
11388 int size)
7c76fa91 11389{
183e98be 11390 const char *note_name = "CORE";
47d9a591 11391 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
11392 note_name, NT_FPREGSET, fpregs, size);
11393}
11394
11395char *
217aa764
AM
11396elfcore_write_prxfpreg (bfd *abfd,
11397 char *buf,
11398 int *bufsiz,
11399 const void *xfpregs,
11400 int size)
7c76fa91
MS
11401{
11402 char *note_name = "LINUX";
47d9a591 11403 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
11404 note_name, NT_PRXFPREG, xfpregs, size);
11405}
11406
4339cae0
L
11407char *
11408elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
11409 const void *xfpregs, int size)
11410{
97de3545
JB
11411 char *note_name;
11412 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
11413 note_name = "FreeBSD";
11414 else
11415 note_name = "LINUX";
4339cae0
L
11416 return elfcore_write_note (abfd, buf, bufsiz,
11417 note_name, NT_X86_XSTATE, xfpregs, size);
11418}
11419
97753bd5
AM
11420char *
11421elfcore_write_ppc_vmx (bfd *abfd,
11422 char *buf,
11423 int *bufsiz,
11424 const void *ppc_vmx,
11425 int size)
11426{
11427 char *note_name = "LINUX";
11428 return elfcore_write_note (abfd, buf, bufsiz,
11429 note_name, NT_PPC_VMX, ppc_vmx, size);
11430}
11431
89eeb0bc
LM
11432char *
11433elfcore_write_ppc_vsx (bfd *abfd,
07d6d2b8
AM
11434 char *buf,
11435 int *bufsiz,
11436 const void *ppc_vsx,
11437 int size)
89eeb0bc
LM
11438{
11439 char *note_name = "LINUX";
11440 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11441 note_name, NT_PPC_VSX, ppc_vsx, size);
89eeb0bc
LM
11442}
11443
cb2366c1
EBM
11444char *
11445elfcore_write_ppc_tar (bfd *abfd,
4b24dd1a
AM
11446 char *buf,
11447 int *bufsiz,
11448 const void *ppc_tar,
11449 int size)
cb2366c1
EBM
11450{
11451 char *note_name = "LINUX";
11452 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11453 note_name, NT_PPC_TAR, ppc_tar, size);
cb2366c1
EBM
11454}
11455
11456char *
11457elfcore_write_ppc_ppr (bfd *abfd,
4b24dd1a
AM
11458 char *buf,
11459 int *bufsiz,
11460 const void *ppc_ppr,
11461 int size)
cb2366c1
EBM
11462{
11463 char *note_name = "LINUX";
11464 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11465 note_name, NT_PPC_PPR, ppc_ppr, size);
cb2366c1
EBM
11466}
11467
11468char *
11469elfcore_write_ppc_dscr (bfd *abfd,
4b24dd1a
AM
11470 char *buf,
11471 int *bufsiz,
11472 const void *ppc_dscr,
11473 int size)
cb2366c1
EBM
11474{
11475 char *note_name = "LINUX";
11476 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11477 note_name, NT_PPC_DSCR, ppc_dscr, size);
cb2366c1
EBM
11478}
11479
11480char *
11481elfcore_write_ppc_ebb (bfd *abfd,
4b24dd1a
AM
11482 char *buf,
11483 int *bufsiz,
11484 const void *ppc_ebb,
11485 int size)
cb2366c1
EBM
11486{
11487 char *note_name = "LINUX";
11488 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11489 note_name, NT_PPC_EBB, ppc_ebb, size);
cb2366c1
EBM
11490}
11491
11492char *
11493elfcore_write_ppc_pmu (bfd *abfd,
4b24dd1a
AM
11494 char *buf,
11495 int *bufsiz,
11496 const void *ppc_pmu,
11497 int size)
cb2366c1
EBM
11498{
11499 char *note_name = "LINUX";
11500 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11501 note_name, NT_PPC_PMU, ppc_pmu, size);
cb2366c1
EBM
11502}
11503
11504char *
11505elfcore_write_ppc_tm_cgpr (bfd *abfd,
4b24dd1a
AM
11506 char *buf,
11507 int *bufsiz,
11508 const void *ppc_tm_cgpr,
11509 int size)
cb2366c1
EBM
11510{
11511 char *note_name = "LINUX";
11512 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11513 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
cb2366c1
EBM
11514}
11515
11516char *
11517elfcore_write_ppc_tm_cfpr (bfd *abfd,
4b24dd1a
AM
11518 char *buf,
11519 int *bufsiz,
11520 const void *ppc_tm_cfpr,
11521 int size)
cb2366c1
EBM
11522{
11523 char *note_name = "LINUX";
11524 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11525 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
cb2366c1
EBM
11526}
11527
11528char *
11529elfcore_write_ppc_tm_cvmx (bfd *abfd,
4b24dd1a
AM
11530 char *buf,
11531 int *bufsiz,
11532 const void *ppc_tm_cvmx,
11533 int size)
cb2366c1
EBM
11534{
11535 char *note_name = "LINUX";
11536 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11537 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
cb2366c1
EBM
11538}
11539
11540char *
11541elfcore_write_ppc_tm_cvsx (bfd *abfd,
4b24dd1a
AM
11542 char *buf,
11543 int *bufsiz,
11544 const void *ppc_tm_cvsx,
11545 int size)
cb2366c1
EBM
11546{
11547 char *note_name = "LINUX";
11548 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11549 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
cb2366c1
EBM
11550}
11551
11552char *
11553elfcore_write_ppc_tm_spr (bfd *abfd,
4b24dd1a
AM
11554 char *buf,
11555 int *bufsiz,
11556 const void *ppc_tm_spr,
11557 int size)
cb2366c1
EBM
11558{
11559 char *note_name = "LINUX";
11560 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11561 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
cb2366c1
EBM
11562}
11563
11564char *
11565elfcore_write_ppc_tm_ctar (bfd *abfd,
4b24dd1a
AM
11566 char *buf,
11567 int *bufsiz,
11568 const void *ppc_tm_ctar,
11569 int size)
cb2366c1
EBM
11570{
11571 char *note_name = "LINUX";
11572 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11573 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
cb2366c1
EBM
11574}
11575
11576char *
11577elfcore_write_ppc_tm_cppr (bfd *abfd,
4b24dd1a
AM
11578 char *buf,
11579 int *bufsiz,
11580 const void *ppc_tm_cppr,
11581 int size)
cb2366c1
EBM
11582{
11583 char *note_name = "LINUX";
11584 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11585 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
cb2366c1
EBM
11586}
11587
11588char *
11589elfcore_write_ppc_tm_cdscr (bfd *abfd,
4b24dd1a
AM
11590 char *buf,
11591 int *bufsiz,
11592 const void *ppc_tm_cdscr,
11593 int size)
cb2366c1
EBM
11594{
11595 char *note_name = "LINUX";
11596 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11597 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
cb2366c1
EBM
11598}
11599
0675e188
UW
11600static char *
11601elfcore_write_s390_high_gprs (bfd *abfd,
11602 char *buf,
11603 int *bufsiz,
11604 const void *s390_high_gprs,
11605 int size)
11606{
11607 char *note_name = "LINUX";
11608 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11609 note_name, NT_S390_HIGH_GPRS,
0675e188
UW
11610 s390_high_gprs, size);
11611}
11612
d7eeb400
MS
11613char *
11614elfcore_write_s390_timer (bfd *abfd,
07d6d2b8
AM
11615 char *buf,
11616 int *bufsiz,
11617 const void *s390_timer,
11618 int size)
d7eeb400
MS
11619{
11620 char *note_name = "LINUX";
11621 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11622 note_name, NT_S390_TIMER, s390_timer, size);
d7eeb400
MS
11623}
11624
11625char *
11626elfcore_write_s390_todcmp (bfd *abfd,
07d6d2b8
AM
11627 char *buf,
11628 int *bufsiz,
11629 const void *s390_todcmp,
11630 int size)
d7eeb400
MS
11631{
11632 char *note_name = "LINUX";
11633 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11634 note_name, NT_S390_TODCMP, s390_todcmp, size);
d7eeb400
MS
11635}
11636
11637char *
11638elfcore_write_s390_todpreg (bfd *abfd,
07d6d2b8
AM
11639 char *buf,
11640 int *bufsiz,
11641 const void *s390_todpreg,
11642 int size)
d7eeb400
MS
11643{
11644 char *note_name = "LINUX";
11645 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11646 note_name, NT_S390_TODPREG, s390_todpreg, size);
d7eeb400
MS
11647}
11648
11649char *
11650elfcore_write_s390_ctrs (bfd *abfd,
07d6d2b8
AM
11651 char *buf,
11652 int *bufsiz,
11653 const void *s390_ctrs,
11654 int size)
d7eeb400
MS
11655{
11656 char *note_name = "LINUX";
11657 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11658 note_name, NT_S390_CTRS, s390_ctrs, size);
d7eeb400
MS
11659}
11660
11661char *
11662elfcore_write_s390_prefix (bfd *abfd,
07d6d2b8
AM
11663 char *buf,
11664 int *bufsiz,
11665 const void *s390_prefix,
11666 int size)
d7eeb400
MS
11667{
11668 char *note_name = "LINUX";
11669 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11670 note_name, NT_S390_PREFIX, s390_prefix, size);
d7eeb400
MS
11671}
11672
355b81d9
UW
11673char *
11674elfcore_write_s390_last_break (bfd *abfd,
11675 char *buf,
11676 int *bufsiz,
11677 const void *s390_last_break,
11678 int size)
11679{
11680 char *note_name = "LINUX";
11681 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11682 note_name, NT_S390_LAST_BREAK,
355b81d9
UW
11683 s390_last_break, size);
11684}
11685
11686char *
11687elfcore_write_s390_system_call (bfd *abfd,
11688 char *buf,
11689 int *bufsiz,
11690 const void *s390_system_call,
11691 int size)
11692{
11693 char *note_name = "LINUX";
11694 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11695 note_name, NT_S390_SYSTEM_CALL,
355b81d9
UW
11696 s390_system_call, size);
11697}
11698
abb3f6cc
NC
11699char *
11700elfcore_write_s390_tdb (bfd *abfd,
11701 char *buf,
11702 int *bufsiz,
11703 const void *s390_tdb,
11704 int size)
11705{
11706 char *note_name = "LINUX";
11707 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11708 note_name, NT_S390_TDB, s390_tdb, size);
abb3f6cc
NC
11709}
11710
4ef9f41a
AA
11711char *
11712elfcore_write_s390_vxrs_low (bfd *abfd,
11713 char *buf,
11714 int *bufsiz,
11715 const void *s390_vxrs_low,
11716 int size)
11717{
11718 char *note_name = "LINUX";
11719 return elfcore_write_note (abfd, buf, bufsiz,
11720 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
11721}
11722
11723char *
11724elfcore_write_s390_vxrs_high (bfd *abfd,
11725 char *buf,
11726 int *bufsiz,
11727 const void *s390_vxrs_high,
11728 int size)
11729{
11730 char *note_name = "LINUX";
11731 return elfcore_write_note (abfd, buf, bufsiz,
11732 note_name, NT_S390_VXRS_HIGH,
11733 s390_vxrs_high, size);
11734}
11735
88ab90e8
AA
11736char *
11737elfcore_write_s390_gs_cb (bfd *abfd,
11738 char *buf,
11739 int *bufsiz,
11740 const void *s390_gs_cb,
11741 int size)
11742{
11743 char *note_name = "LINUX";
11744 return elfcore_write_note (abfd, buf, bufsiz,
11745 note_name, NT_S390_GS_CB,
11746 s390_gs_cb, size);
11747}
11748
11749char *
11750elfcore_write_s390_gs_bc (bfd *abfd,
11751 char *buf,
11752 int *bufsiz,
11753 const void *s390_gs_bc,
11754 int size)
11755{
11756 char *note_name = "LINUX";
11757 return elfcore_write_note (abfd, buf, bufsiz,
11758 note_name, NT_S390_GS_BC,
11759 s390_gs_bc, size);
11760}
11761
faa9a424
UW
11762char *
11763elfcore_write_arm_vfp (bfd *abfd,
11764 char *buf,
11765 int *bufsiz,
11766 const void *arm_vfp,
11767 int size)
11768{
11769 char *note_name = "LINUX";
11770 return elfcore_write_note (abfd, buf, bufsiz,
11771 note_name, NT_ARM_VFP, arm_vfp, size);
11772}
11773
652451f8
YZ
11774char *
11775elfcore_write_aarch_tls (bfd *abfd,
11776 char *buf,
11777 int *bufsiz,
11778 const void *aarch_tls,
11779 int size)
11780{
11781 char *note_name = "LINUX";
11782 return elfcore_write_note (abfd, buf, bufsiz,
11783 note_name, NT_ARM_TLS, aarch_tls, size);
11784}
11785
11786char *
11787elfcore_write_aarch_hw_break (bfd *abfd,
11788 char *buf,
11789 int *bufsiz,
11790 const void *aarch_hw_break,
11791 int size)
11792{
11793 char *note_name = "LINUX";
11794 return elfcore_write_note (abfd, buf, bufsiz,
11795 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
11796}
11797
11798char *
11799elfcore_write_aarch_hw_watch (bfd *abfd,
11800 char *buf,
11801 int *bufsiz,
11802 const void *aarch_hw_watch,
11803 int size)
11804{
11805 char *note_name = "LINUX";
11806 return elfcore_write_note (abfd, buf, bufsiz,
11807 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
11808}
11809
ad1cc4e4
AH
11810char *
11811elfcore_write_aarch_sve (bfd *abfd,
11812 char *buf,
11813 int *bufsiz,
11814 const void *aarch_sve,
11815 int size)
11816{
11817 char *note_name = "LINUX";
11818 return elfcore_write_note (abfd, buf, bufsiz,
11819 note_name, NT_ARM_SVE, aarch_sve, size);
11820}
11821
e6c3b5bf
AH
11822char *
11823elfcore_write_aarch_pauth (bfd *abfd,
11824 char *buf,
11825 int *bufsiz,
11826 const void *aarch_pauth,
11827 int size)
11828{
11829 char *note_name = "LINUX";
11830 return elfcore_write_note (abfd, buf, bufsiz,
11831 note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
11832}
11833
bb864ac1
CES
11834char *
11835elfcore_write_register_note (bfd *abfd,
11836 char *buf,
11837 int *bufsiz,
11838 const char *section,
11839 const void *data,
11840 int size)
11841{
11842 if (strcmp (section, ".reg2") == 0)
11843 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
11844 if (strcmp (section, ".reg-xfp") == 0)
11845 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
4339cae0
L
11846 if (strcmp (section, ".reg-xstate") == 0)
11847 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
bb864ac1
CES
11848 if (strcmp (section, ".reg-ppc-vmx") == 0)
11849 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
89eeb0bc
LM
11850 if (strcmp (section, ".reg-ppc-vsx") == 0)
11851 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
cb2366c1
EBM
11852 if (strcmp (section, ".reg-ppc-tar") == 0)
11853 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
11854 if (strcmp (section, ".reg-ppc-ppr") == 0)
11855 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
11856 if (strcmp (section, ".reg-ppc-dscr") == 0)
11857 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
11858 if (strcmp (section, ".reg-ppc-ebb") == 0)
11859 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
11860 if (strcmp (section, ".reg-ppc-pmu") == 0)
11861 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
11862 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
11863 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
11864 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
11865 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
11866 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
11867 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
11868 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
11869 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
11870 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
11871 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
11872 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
11873 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
11874 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
11875 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
11876 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
11877 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
0675e188
UW
11878 if (strcmp (section, ".reg-s390-high-gprs") == 0)
11879 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
d7eeb400
MS
11880 if (strcmp (section, ".reg-s390-timer") == 0)
11881 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
11882 if (strcmp (section, ".reg-s390-todcmp") == 0)
11883 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
11884 if (strcmp (section, ".reg-s390-todpreg") == 0)
11885 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
11886 if (strcmp (section, ".reg-s390-ctrs") == 0)
11887 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
11888 if (strcmp (section, ".reg-s390-prefix") == 0)
11889 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
355b81d9
UW
11890 if (strcmp (section, ".reg-s390-last-break") == 0)
11891 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
11892 if (strcmp (section, ".reg-s390-system-call") == 0)
11893 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
abb3f6cc
NC
11894 if (strcmp (section, ".reg-s390-tdb") == 0)
11895 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
4ef9f41a
AA
11896 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
11897 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
11898 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
11899 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
88ab90e8
AA
11900 if (strcmp (section, ".reg-s390-gs-cb") == 0)
11901 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
11902 if (strcmp (section, ".reg-s390-gs-bc") == 0)
11903 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
faa9a424
UW
11904 if (strcmp (section, ".reg-arm-vfp") == 0)
11905 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
652451f8
YZ
11906 if (strcmp (section, ".reg-aarch-tls") == 0)
11907 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
11908 if (strcmp (section, ".reg-aarch-hw-break") == 0)
11909 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
11910 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
11911 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
ad1cc4e4
AH
11912 if (strcmp (section, ".reg-aarch-sve") == 0)
11913 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
e6c3b5bf
AH
11914 if (strcmp (section, ".reg-aarch-pauth") == 0)
11915 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
bb864ac1
CES
11916 return NULL;
11917}
11918
b34976b6 11919static bfd_boolean
276da9b3
L
11920elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
11921 size_t align)
252b5132 11922{
c044fabd 11923 char *p;
252b5132 11924
276da9b3
L
11925 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
11926 gABI specifies that PT_NOTE alignment should be aligned to 4
11927 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
11928 align is less than 4, we use 4 byte alignment. */
11929 if (align < 4)
11930 align = 4;
ef135d43
NC
11931 if (align != 4 && align != 8)
11932 return FALSE;
276da9b3 11933
252b5132
RH
11934 p = buf;
11935 while (p < buf + size)
11936 {
c044fabd 11937 Elf_External_Note *xnp = (Elf_External_Note *) p;
252b5132
RH
11938 Elf_Internal_Note in;
11939
baea7ef1
AM
11940 if (offsetof (Elf_External_Note, name) > buf - p + size)
11941 return FALSE;
11942
dc810e39 11943 in.type = H_GET_32 (abfd, xnp->type);
252b5132 11944
dc810e39 11945 in.namesz = H_GET_32 (abfd, xnp->namesz);
252b5132 11946 in.namedata = xnp->name;
baea7ef1
AM
11947 if (in.namesz > buf - in.namedata + size)
11948 return FALSE;
252b5132 11949
dc810e39 11950 in.descsz = H_GET_32 (abfd, xnp->descsz);
276da9b3 11951 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
252b5132 11952 in.descpos = offset + (in.descdata - buf);
baea7ef1
AM
11953 if (in.descsz != 0
11954 && (in.descdata >= buf + size
11955 || in.descsz > buf - in.descdata + size))
11956 return FALSE;
252b5132 11957
718175fa 11958 switch (bfd_get_format (abfd))
07d6d2b8 11959 {
718175fa
JK
11960 default:
11961 return TRUE;
11962
11963 case bfd_core:
f64e188b 11964 {
8acbedd6 11965#define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
f64e188b 11966 struct
718175fa 11967 {
f64e188b 11968 const char * string;
8acbedd6 11969 size_t len;
f64e188b 11970 bfd_boolean (* func)(bfd *, Elf_Internal_Note *);
718175fa 11971 }
f64e188b 11972 grokers[] =
b15fa79e 11973 {
8acbedd6 11974 GROKER_ELEMENT ("", elfcore_grok_note),
aa1ed4a9 11975 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
8acbedd6
KS
11976 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
11977 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note),
11978 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
864619bb
KS
11979 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
11980 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note)
f64e188b 11981 };
8acbedd6 11982#undef GROKER_ELEMENT
f64e188b
NC
11983 int i;
11984
11985 for (i = ARRAY_SIZE (grokers); i--;)
8acbedd6
KS
11986 {
11987 if (in.namesz >= grokers[i].len
11988 && strncmp (in.namedata, grokers[i].string,
11989 grokers[i].len) == 0)
11990 {
11991 if (! grokers[i].func (abfd, & in))
11992 return FALSE;
11993 break;
11994 }
11995 }
f64e188b
NC
11996 break;
11997 }
718175fa
JK
11998
11999 case bfd_object:
12000 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
12001 {
12002 if (! elfobj_grok_gnu_note (abfd, &in))
12003 return FALSE;
12004 }
e21e5835
NC
12005 else if (in.namesz == sizeof "stapsdt"
12006 && strcmp (in.namedata, "stapsdt") == 0)
12007 {
12008 if (! elfobj_grok_stapsdt_note (abfd, &in))
12009 return FALSE;
12010 }
718175fa 12011 break;
08a40648 12012 }
252b5132 12013
276da9b3 12014 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
252b5132
RH
12015 }
12016
718175fa
JK
12017 return TRUE;
12018}
12019
864619bb 12020bfd_boolean
276da9b3
L
12021elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
12022 size_t align)
718175fa
JK
12023{
12024 char *buf;
12025
957e1fc1 12026 if (size == 0 || (size + 1) == 0)
718175fa
JK
12027 return TRUE;
12028
12029 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
12030 return FALSE;
12031
2bb3687b 12032 buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
718175fa
JK
12033 if (buf == NULL)
12034 return FALSE;
12035
f64e188b
NC
12036 /* PR 17512: file: ec08f814
12037 0-termintate the buffer so that string searches will not overflow. */
12038 buf[size] = 0;
12039
2bb3687b 12040 if (!elf_parse_notes (abfd, buf, size, offset, align))
718175fa
JK
12041 {
12042 free (buf);
12043 return FALSE;
12044 }
12045
252b5132 12046 free (buf);
b34976b6 12047 return TRUE;
252b5132 12048}
98d8431c
JB
12049\f
12050/* Providing external access to the ELF program header table. */
12051
12052/* Return an upper bound on the number of bytes required to store a
12053 copy of ABFD's program header table entries. Return -1 if an error
12054 occurs; bfd_get_error will return an appropriate code. */
c044fabd 12055
98d8431c 12056long
217aa764 12057bfd_get_elf_phdr_upper_bound (bfd *abfd)
98d8431c
JB
12058{
12059 if (abfd->xvec->flavour != bfd_target_elf_flavour)
12060 {
12061 bfd_set_error (bfd_error_wrong_format);
12062 return -1;
12063 }
12064
936e320b 12065 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
98d8431c
JB
12066}
12067
98d8431c
JB
12068/* Copy ABFD's program header table entries to *PHDRS. The entries
12069 will be stored as an array of Elf_Internal_Phdr structures, as
12070 defined in include/elf/internal.h. To find out how large the
12071 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
12072
12073 Return the number of program header table entries read, or -1 if an
12074 error occurs; bfd_get_error will return an appropriate code. */
c044fabd 12075
98d8431c 12076int
217aa764 12077bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
98d8431c
JB
12078{
12079 int num_phdrs;
12080
12081 if (abfd->xvec->flavour != bfd_target_elf_flavour)
12082 {
12083 bfd_set_error (bfd_error_wrong_format);
12084 return -1;
12085 }
12086
12087 num_phdrs = elf_elfheader (abfd)->e_phnum;
01bcaf63
TT
12088 if (num_phdrs != 0)
12089 memcpy (phdrs, elf_tdata (abfd)->phdr,
12090 num_phdrs * sizeof (Elf_Internal_Phdr));
98d8431c
JB
12091
12092 return num_phdrs;
12093}
ae4221d7 12094
db6751f2 12095enum elf_reloc_type_class
7e612e98
AM
12096_bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12097 const asection *rel_sec ATTRIBUTE_UNUSED,
12098 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
db6751f2
JJ
12099{
12100 return reloc_class_normal;
12101}
f8df10f4 12102
47d9a591 12103/* For RELA architectures, return the relocation value for a
f8df10f4
JJ
12104 relocation against a local symbol. */
12105
12106bfd_vma
217aa764
AM
12107_bfd_elf_rela_local_sym (bfd *abfd,
12108 Elf_Internal_Sym *sym,
8517fae7 12109 asection **psec,
217aa764 12110 Elf_Internal_Rela *rel)
f8df10f4 12111{
8517fae7 12112 asection *sec = *psec;
f8df10f4
JJ
12113 bfd_vma relocation;
12114
6835821b
AM
12115 relocation = (sec->output_section->vma
12116 + sec->output_offset
12117 + sym->st_value);
f8df10f4 12118 if ((sec->flags & SEC_MERGE)
c629eae0 12119 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
dbaa2011 12120 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
f8df10f4 12121 {
f8df10f4 12122 rel->r_addend =
8517fae7 12123 _bfd_merged_section_offset (abfd, psec,
65765700 12124 elf_section_data (sec)->sec_info,
753731ee
AM
12125 sym->st_value + rel->r_addend);
12126 if (sec != *psec)
12127 {
12128 /* If we have changed the section, and our original section is
12129 marked with SEC_EXCLUDE, it means that the original
12130 SEC_MERGE section has been completely subsumed in some
12131 other SEC_MERGE section. In this case, we need to leave
12132 some info around for --emit-relocs. */
12133 if ((sec->flags & SEC_EXCLUDE) != 0)
12134 sec->kept_section = *psec;
12135 sec = *psec;
12136 }
8517fae7
AM
12137 rel->r_addend -= relocation;
12138 rel->r_addend += sec->output_section->vma + sec->output_offset;
f8df10f4
JJ
12139 }
12140 return relocation;
12141}
c629eae0
JJ
12142
12143bfd_vma
217aa764
AM
12144_bfd_elf_rel_local_sym (bfd *abfd,
12145 Elf_Internal_Sym *sym,
12146 asection **psec,
12147 bfd_vma addend)
47d9a591 12148{
c629eae0
JJ
12149 asection *sec = *psec;
12150
6835821b 12151 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
c629eae0
JJ
12152 return sym->st_value + addend;
12153
12154 return _bfd_merged_section_offset (abfd, psec,
65765700 12155 elf_section_data (sec)->sec_info,
753731ee 12156 sym->st_value + addend);
c629eae0
JJ
12157}
12158
37b01f6a
DG
12159/* Adjust an address within a section. Given OFFSET within SEC, return
12160 the new offset within the section, based upon changes made to the
12161 section. Returns -1 if the offset is now invalid.
12162 The offset (in abnd out) is in target sized bytes, however big a
12163 byte may be. */
12164
c629eae0 12165bfd_vma
217aa764 12166_bfd_elf_section_offset (bfd *abfd,
92e4ec35 12167 struct bfd_link_info *info,
217aa764
AM
12168 asection *sec,
12169 bfd_vma offset)
c629eae0 12170{
68bfbfcc 12171 switch (sec->sec_info_type)
65765700 12172 {
dbaa2011 12173 case SEC_INFO_TYPE_STABS:
eea6121a
AM
12174 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
12175 offset);
dbaa2011 12176 case SEC_INFO_TYPE_EH_FRAME:
92e4ec35 12177 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
37b01f6a 12178
65765700 12179 default:
310fd250
L
12180 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
12181 {
37b01f6a 12182 /* Reverse the offset. */
310fd250
L
12183 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12184 bfd_size_type address_size = bed->s->arch_size / 8;
37b01f6a
DG
12185
12186 /* address_size and sec->size are in octets. Convert
12187 to bytes before subtracting the original offset. */
61826503 12188 offset = ((sec->size - address_size)
bb294208 12189 / bfd_octets_per_byte (abfd, sec) - offset);
310fd250 12190 }
65765700
JJ
12191 return offset;
12192 }
c629eae0 12193}
3333a7c3
RM
12194\f
12195/* Create a new BFD as if by bfd_openr. Rather than opening a file,
12196 reconstruct an ELF file by reading the segments out of remote memory
12197 based on the ELF file header at EHDR_VMA and the ELF program headers it
12198 points to. If not null, *LOADBASEP is filled in with the difference
12199 between the VMAs from which the segments were read, and the VMAs the
12200 file headers (and hence BFD's idea of each section's VMA) put them at.
12201
12202 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12203 remote memory at target address VMA into the local buffer at MYADDR; it
12204 should return zero on success or an `errno' code on failure. TEMPL must
12205 be a BFD for an ELF target with the word size and byte order found in
12206 the remote memory. */
12207
12208bfd *
217aa764
AM
12209bfd_elf_bfd_from_remote_memory
12210 (bfd *templ,
12211 bfd_vma ehdr_vma,
f0a5d95a 12212 bfd_size_type size,
217aa764 12213 bfd_vma *loadbasep,
fe78531d 12214 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
3333a7c3
RM
12215{
12216 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
5979d6b6 12217 (templ, ehdr_vma, size, loadbasep, target_read_memory);
3333a7c3 12218}
4c45e5c9
JJ
12219\f
12220long
c9727e01
AM
12221_bfd_elf_get_synthetic_symtab (bfd *abfd,
12222 long symcount ATTRIBUTE_UNUSED,
12223 asymbol **syms ATTRIBUTE_UNUSED,
8615f3f2 12224 long dynsymcount,
c9727e01
AM
12225 asymbol **dynsyms,
12226 asymbol **ret)
4c45e5c9
JJ
12227{
12228 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12229 asection *relplt;
12230 asymbol *s;
12231 const char *relplt_name;
12232 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
12233 arelent *p;
12234 long count, i, n;
12235 size_t size;
12236 Elf_Internal_Shdr *hdr;
12237 char *names;
12238 asection *plt;
12239
8615f3f2
AM
12240 *ret = NULL;
12241
90e3cdf2
JJ
12242 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
12243 return 0;
12244
8615f3f2
AM
12245 if (dynsymcount <= 0)
12246 return 0;
12247
4c45e5c9
JJ
12248 if (!bed->plt_sym_val)
12249 return 0;
12250
12251 relplt_name = bed->relplt_name;
12252 if (relplt_name == NULL)
d35fd659 12253 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
4c45e5c9
JJ
12254 relplt = bfd_get_section_by_name (abfd, relplt_name);
12255 if (relplt == NULL)
12256 return 0;
12257
12258 hdr = &elf_section_data (relplt)->this_hdr;
12259 if (hdr->sh_link != elf_dynsymtab (abfd)
12260 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
12261 return 0;
12262
12263 plt = bfd_get_section_by_name (abfd, ".plt");
12264 if (plt == NULL)
12265 return 0;
12266
12267 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
c9727e01 12268 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
4c45e5c9
JJ
12269 return -1;
12270
eea6121a 12271 count = relplt->size / hdr->sh_entsize;
4c45e5c9
JJ
12272 size = count * sizeof (asymbol);
12273 p = relplt->relocation;
cb53bf42 12274 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
041de40d
AM
12275 {
12276 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
12277 if (p->addend != 0)
12278 {
12279#ifdef BFD64
12280 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
12281#else
12282 size += sizeof ("+0x") - 1 + 8;
12283#endif
12284 }
12285 }
4c45e5c9 12286
a50b1753 12287 s = *ret = (asymbol *) bfd_malloc (size);
4c45e5c9
JJ
12288 if (s == NULL)
12289 return -1;
12290
12291 names = (char *) (s + count);
12292 p = relplt->relocation;
12293 n = 0;
cb53bf42 12294 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
4c45e5c9
JJ
12295 {
12296 size_t len;
12297 bfd_vma addr;
12298
12299 addr = bed->plt_sym_val (i, plt, p);
12300 if (addr == (bfd_vma) -1)
12301 continue;
12302
12303 *s = **p->sym_ptr_ptr;
65a7a66f
AM
12304 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
12305 we are defining a symbol, ensure one of them is set. */
12306 if ((s->flags & BSF_LOCAL) == 0)
12307 s->flags |= BSF_GLOBAL;
6ba2a415 12308 s->flags |= BSF_SYNTHETIC;
4c45e5c9
JJ
12309 s->section = plt;
12310 s->value = addr - plt->vma;
12311 s->name = names;
8f39ba8e 12312 s->udata.p = NULL;
4c45e5c9
JJ
12313 len = strlen ((*p->sym_ptr_ptr)->name);
12314 memcpy (names, (*p->sym_ptr_ptr)->name, len);
12315 names += len;
041de40d
AM
12316 if (p->addend != 0)
12317 {
1d770845 12318 char buf[30], *a;
d324f6d6 12319
041de40d
AM
12320 memcpy (names, "+0x", sizeof ("+0x") - 1);
12321 names += sizeof ("+0x") - 1;
1d770845
L
12322 bfd_sprintf_vma (abfd, buf, p->addend);
12323 for (a = buf; *a == '0'; ++a)
12324 ;
12325 len = strlen (a);
12326 memcpy (names, a, len);
12327 names += len;
041de40d 12328 }
4c45e5c9
JJ
12329 memcpy (names, "@plt", sizeof ("@plt"));
12330 names += sizeof ("@plt");
8f39ba8e 12331 ++s, ++n;
4c45e5c9
JJ
12332 }
12333
12334 return n;
12335}
3d7f7666 12336
821e6ff6
AM
12337/* It is only used by x86-64 so far.
12338 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
7eacd66b
AM
12339 but current usage would allow all of _bfd_std_section to be zero. */
12340static const asymbol lcomm_sym
12341 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
3b22753a 12342asection _bfd_elf_large_com_section
7eacd66b 12343 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
821e6ff6 12344 "LARGE_COMMON", 0, SEC_IS_COMMON);
ecca9871 12345
cc364be6
AM
12346bfd_boolean
12347_bfd_elf_final_write_processing (bfd *abfd)
06f44071
AM
12348{
12349 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
d1036acb
L
12350
12351 i_ehdrp = elf_elfheader (abfd);
12352
06f44071
AM
12353 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12354 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
d8045f23 12355
df3a023b
AM
12356 /* Set the osabi field to ELFOSABI_GNU if the binary contains
12357 SHF_GNU_MBIND sections or symbols of STT_GNU_IFUNC type or
12358 STB_GNU_UNIQUE binding. */
cc364be6
AM
12359 if (elf_tdata (abfd)->has_gnu_osabi != 0)
12360 {
12361 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12362 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
12363 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
12364 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
12365 {
12366 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
12367 _bfd_error_handler (_("GNU_MBIND section is unsupported"));
12368 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
12369 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is unsupported"));
12370 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
12371 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is unsupported"));
9aea1e31 12372 bfd_set_error (bfd_error_sorry);
cc364be6
AM
12373 return FALSE;
12374 }
12375 }
12376 return TRUE;
d1036acb 12377}
fcb93ecf
PB
12378
12379
12380/* Return TRUE for ELF symbol types that represent functions.
12381 This is the default version of this function, which is sufficient for
d8045f23 12382 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
fcb93ecf
PB
12383
12384bfd_boolean
12385_bfd_elf_is_function_type (unsigned int type)
12386{
d8045f23
NC
12387 return (type == STT_FUNC
12388 || type == STT_GNU_IFUNC);
fcb93ecf 12389}
9f296da3 12390
aef36ac1
AM
12391/* If the ELF symbol SYM might be a function in SEC, return the
12392 function size and set *CODE_OFF to the function's entry point,
12393 otherwise return zero. */
9f296da3 12394
aef36ac1
AM
12395bfd_size_type
12396_bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
12397 bfd_vma *code_off)
9f296da3 12398{
aef36ac1
AM
12399 bfd_size_type size;
12400
ff9e0f5b 12401 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
aef36ac1
AM
12402 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
12403 || sym->section != sec)
12404 return 0;
ff9e0f5b 12405
ff9e0f5b 12406 *code_off = sym->value;
aef36ac1
AM
12407 size = 0;
12408 if (!(sym->flags & BSF_SYNTHETIC))
12409 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
12410 if (size == 0)
12411 size = 1;
12412 return size;
9f296da3 12413}
a8e14f4c
NC
12414
12415/* Set to non-zero to enable some debug messages. */
12416#define DEBUG_SECONDARY_RELOCS 0
12417
12418/* An internal-to-the-bfd-library only section type
12419 used to indicate a cached secondary reloc section. */
12420#define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
12421
12422/* Create a BFD section to hold a secondary reloc section. */
12423
12424bfd_boolean
12425_bfd_elf_init_secondary_reloc_section (bfd * abfd,
12426 Elf_Internal_Shdr *hdr,
12427 const char * name,
12428 unsigned int shindex)
12429{
12430 /* We only support RELA secondary relocs. */
12431 if (hdr->sh_type != SHT_RELA)
12432 return FALSE;
12433
12434#if DEBUG_SECONDARY_RELOCS
12435 fprintf (stderr, "secondary reloc section %s encountered\n", name);
12436#endif
12437 hdr->sh_type = SHT_SECONDARY_RELOC;
12438 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
12439}
12440
12441/* Read in any secondary relocs associated with SEC. */
12442
12443bfd_boolean
12444_bfd_elf_slurp_secondary_reloc_section (bfd * abfd,
12445 asection * sec,
12446 asymbol ** symbols)
12447{
12448 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
12449 asection * relsec;
12450 bfd_boolean result = TRUE;
12451 bfd_vma (*r_sym) (bfd_vma);
12452
12453#if BFD_DEFAULT_TARGET_SIZE > 32
12454 if (bfd_arch_bits_per_address (abfd) != 32)
12455 r_sym = elf64_r_sym;
12456 else
12457#endif
12458 r_sym = elf32_r_sym;
12459
12460 /* Discover if there are any secondary reloc sections
12461 associated with SEC. */
12462 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
12463 {
12464 Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
12465
12466 if (hdr->sh_type == SHT_SECONDARY_RELOC
12467 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
12468 {
12469 bfd_byte * native_relocs;
12470 bfd_byte * native_reloc;
12471 arelent * internal_relocs;
12472 arelent * internal_reloc;
12473 unsigned int i;
12474 unsigned int entsize;
12475 unsigned int symcount;
12476 unsigned int reloc_count;
12477 size_t amt;
12478
12479 if (ebd->elf_info_to_howto == NULL)
12480 return FALSE;
12481
12482#if DEBUG_SECONDARY_RELOCS
12483 fprintf (stderr, "read secondary relocs for %s from %s\n",
12484 sec->name, relsec->name);
12485#endif
12486 entsize = hdr->sh_entsize;
12487
12488 native_relocs = bfd_malloc (hdr->sh_size);
12489 if (native_relocs == NULL)
12490 {
12491 result = FALSE;
12492 continue;
12493 }
12494
12495 reloc_count = NUM_SHDR_ENTRIES (hdr);
12496 if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
12497 {
ecbbbdba 12498 free (native_relocs);
a8e14f4c
NC
12499 bfd_set_error (bfd_error_file_too_big);
12500 result = FALSE;
12501 continue;
12502 }
12503
12504 internal_relocs = (arelent *) bfd_alloc (abfd, amt);
12505 if (internal_relocs == NULL)
12506 {
12507 free (native_relocs);
12508 result = FALSE;
12509 continue;
12510 }
12511
12512 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
12513 || (bfd_bread (native_relocs, hdr->sh_size, abfd)
12514 != hdr->sh_size))
12515 {
12516 free (native_relocs);
ecbbbdba
NC
12517 /* The internal_relocs will be freed when
12518 the memory for the bfd is released. */
a8e14f4c
NC
12519 result = FALSE;
12520 continue;
12521 }
12522
12523 symcount = bfd_get_symcount (abfd);
12524
12525 for (i = 0, internal_reloc = internal_relocs,
12526 native_reloc = native_relocs;
12527 i < reloc_count;
12528 i++, internal_reloc++, native_reloc += entsize)
12529 {
12530 bfd_boolean res;
12531 Elf_Internal_Rela rela;
12532
12533 ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
12534
12535 /* The address of an ELF reloc is section relative for an object
12536 file, and absolute for an executable file or shared library.
12537 The address of a normal BFD reloc is always section relative,
12538 and the address of a dynamic reloc is absolute.. */
12539 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
12540 internal_reloc->address = rela.r_offset;
12541 else
12542 internal_reloc->address = rela.r_offset - sec->vma;
12543
12544 if (r_sym (rela.r_info) == STN_UNDEF)
12545 {
12546 /* FIXME: This and the error case below mean that we
12547 have a symbol on relocs that is not elf_symbol_type. */
12548 internal_reloc->sym_ptr_ptr =
12549 bfd_abs_section_ptr->symbol_ptr_ptr;
12550 }
12551 else if (r_sym (rela.r_info) > symcount)
12552 {
12553 _bfd_error_handler
12554 /* xgettext:c-format */
12555 (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
12556 abfd, sec, i, (long) r_sym (rela.r_info));
12557 bfd_set_error (bfd_error_bad_value);
12558 internal_reloc->sym_ptr_ptr =
12559 bfd_abs_section_ptr->symbol_ptr_ptr;
12560 result = FALSE;
12561 }
12562 else
12563 {
12564 asymbol **ps;
12565
12566 ps = symbols + r_sym (rela.r_info) - 1;
12567
12568 internal_reloc->sym_ptr_ptr = ps;
12569 /* Make sure that this symbol is not removed by strip. */
12570 (*ps)->flags |= BSF_KEEP;
12571 }
12572
12573 internal_reloc->addend = rela.r_addend;
12574
12575 res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
12576 if (! res || internal_reloc->howto == NULL)
12577 {
12578#if DEBUG_SECONDARY_RELOCS
12579 fprintf (stderr, "there is no howto associated with reloc %lx\n",
12580 rela.r_info);
12581#endif
12582 result = FALSE;
12583 }
12584 }
12585
12586 free (native_relocs);
12587 /* Store the internal relocs. */
12588 elf_section_data (relsec)->sec_info = internal_relocs;
12589 }
12590 }
12591
12592 return result;
12593}
12594
12595/* Set the ELF section header fields of an output secondary reloc section. */
12596
12597bfd_boolean
12598_bfd_elf_copy_special_section_fields (const bfd * ibfd ATTRIBUTE_UNUSED,
12599 bfd * obfd ATTRIBUTE_UNUSED,
12600 const Elf_Internal_Shdr * isection,
12601 Elf_Internal_Shdr * osection)
12602{
12603 asection * isec;
12604 asection * osec;
12605
12606 if (isection == NULL)
12607 return FALSE;
12608
12609 if (isection->sh_type != SHT_SECONDARY_RELOC)
12610 return TRUE;
12611
12612 isec = isection->bfd_section;
12613 if (isec == NULL)
12614 return FALSE;
12615
12616 osec = osection->bfd_section;
12617 if (osec == NULL)
12618 return FALSE;
12619
12620 BFD_ASSERT (elf_section_data (osec)->sec_info == NULL);
12621 elf_section_data (osec)->sec_info = elf_section_data (isec)->sec_info;
12622 osection->sh_type = SHT_RELA;
12623 osection->sh_link = elf_onesymtab (obfd);
12624 if (osection->sh_link == 0)
12625 {
12626 /* There is no symbol table - we are hosed... */
12627 _bfd_error_handler
12628 /* xgettext:c-format */
12629 (_("%pB(%pA): link section cannot be set because the output file does not have a symbol table"),
12630 obfd, osec);
12631 bfd_set_error (bfd_error_bad_value);
12632 return FALSE;
12633 }
12634
12635 /* Find the output section that corresponds to the isection's sh_info link. */
327ef784
NC
12636 if (isection->sh_info == 0
12637 || isection->sh_info >= elf_numsections (ibfd))
12638 {
12639 _bfd_error_handler
12640 /* xgettext:c-format */
12641 (_("%pB(%pA): info section index is invalid"),
12642 obfd, osec);
12643 bfd_set_error (bfd_error_bad_value);
12644 return FALSE;
12645 }
12646
a8e14f4c
NC
12647 isection = elf_elfsections (ibfd)[isection->sh_info];
12648
327ef784
NC
12649 if (isection == NULL
12650 || isection->bfd_section == NULL
12651 || isection->bfd_section->output_section == NULL)
12652 {
12653 _bfd_error_handler
12654 /* xgettext:c-format */
12655 (_("%pB(%pA): info section index cannot be set because the section is not in the output"),
12656 obfd, osec);
12657 bfd_set_error (bfd_error_bad_value);
12658 return FALSE;
12659 }
12660
a8e14f4c
NC
12661 osection->sh_info =
12662 elf_section_data (isection->bfd_section->output_section)->this_idx;
12663
12664#if DEBUG_SECONDARY_RELOCS
12665 fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
12666 osec->name, osection->sh_link, osection->sh_info);
12667#endif
12668
12669 return TRUE;
12670}
12671
12672/* Write out a secondary reloc section. */
12673
12674bfd_boolean
12675_bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
12676{
12677 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
12678 bfd_vma addr_offset;
12679 asection * relsec;
12680 bfd_vma (*r_info) (bfd_vma, bfd_vma);
ac4bf06c
NC
12681 bfd_boolean result = TRUE;
12682
12683 if (sec == NULL)
12684 return FALSE;
a8e14f4c
NC
12685
12686#if BFD_DEFAULT_TARGET_SIZE > 32
12687 if (bfd_arch_bits_per_address (abfd) != 32)
12688 r_info = elf64_r_info;
12689 else
12690#endif
12691 r_info = elf32_r_info;
12692
a8e14f4c
NC
12693 /* The address of an ELF reloc is section relative for an object
12694 file, and absolute for an executable file or shared library.
12695 The address of a BFD reloc is always section relative. */
12696 addr_offset = 0;
12697 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
12698 addr_offset = sec->vma;
12699
12700 /* Discover if there are any secondary reloc sections
12701 associated with SEC. */
12702 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
12703 {
12704 const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
12705 Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
12706
12707 if (hdr->sh_type == SHT_RELA
12708 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
12709 {
12710 asymbol * last_sym;
12711 int last_sym_idx;
12712 unsigned int reloc_count;
12713 unsigned int idx;
12714 arelent * src_irel;
12715 bfd_byte * dst_rela;
12716
ac4bf06c
NC
12717 if (hdr->contents != NULL)
12718 {
12719 _bfd_error_handler
12720 /* xgettext:c-format */
12721 (_("%pB(%pA): error: secondary reloc section processed twice"),
12722 abfd, relsec);
12723 bfd_set_error (bfd_error_bad_value);
12724 result = FALSE;
12725 continue;
12726 }
a8e14f4c
NC
12727
12728 reloc_count = hdr->sh_size / hdr->sh_entsize;
ac4bf06c
NC
12729 if (reloc_count <= 0)
12730 {
12731 _bfd_error_handler
12732 /* xgettext:c-format */
12733 (_("%pB(%pA): error: secondary reloc section is empty!"),
12734 abfd, relsec);
12735 bfd_set_error (bfd_error_bad_value);
12736 result = FALSE;
12737 continue;
12738 }
a8e14f4c
NC
12739
12740 hdr->contents = bfd_alloc (abfd, hdr->sh_size);
12741 if (hdr->contents == NULL)
12742 continue;
12743
12744#if DEBUG_SECONDARY_RELOCS
12745 fprintf (stderr, "write %u secondary relocs for %s from %s\n",
12746 reloc_count, sec->name, relsec->name);
12747#endif
12748 last_sym = NULL;
12749 last_sym_idx = 0;
12750 dst_rela = hdr->contents;
12751 src_irel = (arelent *) esd->sec_info;
ac4bf06c
NC
12752 if (src_irel == NULL)
12753 {
12754 _bfd_error_handler
12755 /* xgettext:c-format */
12756 (_("%pB(%pA): error: internal relocs missing for secondary reloc section"),
12757 abfd, relsec);
12758 bfd_set_error (bfd_error_bad_value);
12759 result = FALSE;
12760 continue;
12761 }
a8e14f4c
NC
12762
12763 for (idx = 0; idx < reloc_count; idx++, dst_rela += hdr->sh_entsize)
12764 {
12765 Elf_Internal_Rela src_rela;
12766 arelent *ptr;
12767 asymbol *sym;
12768 int n;
12769
12770 ptr = src_irel + idx;
ac4bf06c
NC
12771 if (ptr == NULL)
12772 {
12773 _bfd_error_handler
12774 /* xgettext:c-format */
12775 (_("%pB(%pA): error: reloc table entry %u is empty"),
12776 abfd, relsec, idx);
12777 bfd_set_error (bfd_error_bad_value);
12778 result = FALSE;
12779 break;
12780 }
a8e14f4c 12781
ac4bf06c
NC
12782 if (ptr->sym_ptr_ptr == NULL)
12783 {
12784 /* FIXME: Is this an error ? */
12785 n = 0;
12786 }
a8e14f4c
NC
12787 else
12788 {
ac4bf06c
NC
12789 sym = *ptr->sym_ptr_ptr;
12790
12791 if (sym == last_sym)
12792 n = last_sym_idx;
12793 else
a8e14f4c 12794 {
ac4bf06c
NC
12795 n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
12796 if (n < 0)
12797 {
12798 _bfd_error_handler
12799 /* xgettext:c-format */
12800 (_("%pB(%pA): error: secondary reloc %u references a missing symbol"),
12801 abfd, relsec, idx);
12802 bfd_set_error (bfd_error_bad_value);
12803 result = FALSE;
12804 n = 0;
12805 }
12806
12807 last_sym = sym;
12808 last_sym_idx = n;
a8e14f4c 12809 }
a8e14f4c 12810
ac4bf06c
NC
12811 if (sym->the_bfd != NULL
12812 && sym->the_bfd->xvec != abfd->xvec
12813 && ! _bfd_elf_validate_reloc (abfd, ptr))
12814 {
12815 _bfd_error_handler
12816 /* xgettext:c-format */
12817 (_("%pB(%pA): error: secondary reloc %u references a deleted symbol"),
12818 abfd, relsec, idx);
12819 bfd_set_error (bfd_error_bad_value);
12820 result = FALSE;
12821 n = 0;
12822 }
a8e14f4c
NC
12823 }
12824
ac4bf06c 12825 src_rela.r_offset = ptr->address + addr_offset;
a8e14f4c
NC
12826 if (ptr->howto == NULL)
12827 {
ac4bf06c
NC
12828 _bfd_error_handler
12829 /* xgettext:c-format */
12830 (_("%pB(%pA): error: secondary reloc %u is of an unknown type"),
12831 abfd, relsec, idx);
12832 bfd_set_error (bfd_error_bad_value);
12833 result = FALSE;
12834 src_rela.r_info = r_info (0, 0);
a8e14f4c 12835 }
ac4bf06c
NC
12836 else
12837 src_rela.r_info = r_info (n, ptr->howto->type);
a8e14f4c
NC
12838 src_rela.r_addend = ptr->addend;
12839 ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
12840 }
12841 }
12842 }
12843
ac4bf06c 12844 return result;
a8e14f4c 12845}