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