]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf.c
gdb/ChangeLog: fix typo in an entry from 2020-11-15 (gmp-utils.c)
[thirdparty/binutils-gdb.git] / bfd / elf.c
CommitLineData
252b5132 1/* ELF executable support for BFD.
340b6d91 2
b3adc24a 3 Copyright (C) 1993-2020 Free Software Foundation, Inc.
252b5132 4
5e8d7549 5 This file is part of BFD, the Binary File Descriptor library.
252b5132 6
5e8d7549
NC
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
cd123cb7 9 the Free Software Foundation; either version 3 of the License, or
5e8d7549 10 (at your option) any later version.
252b5132 11
5e8d7549
NC
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
252b5132 16
5e8d7549 17 You should have received a copy of the GNU General Public License
b34976b6 18 along with this program; if not, write to the Free Software
cd123cb7
NC
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21
252b5132 22
1b74d094
BW
23/*
24SECTION
252b5132
RH
25 ELF backends
26
27 BFD support for ELF formats is being worked on.
28 Currently, the best supported back ends are for sparc and i386
29 (running svr4 or Solaris 2).
30
31 Documentation of the internals of the support code still needs
32 to be written. The code is changing quickly enough that we
661a3fd4 33 haven't bothered yet. */
252b5132 34
7ee38065
MS
35/* For sparc64-cross-sparc32. */
36#define _SYSCALL32
252b5132 37#include "sysdep.h"
3a551c7a 38#include <limits.h>
3db64b00 39#include "bfd.h"
252b5132
RH
40#include "bfdlink.h"
41#include "libbfd.h"
42#define ARCH_SIZE 0
43#include "elf-bfd.h"
e0e8c97f 44#include "libiberty.h"
ff59fc36 45#include "safe-ctype.h"
de64ce13 46#include "elf-linux-core.h"
252b5132 47
8bc7f138
L
48#ifdef CORE_HEADER
49#include CORE_HEADER
50#endif
51
217aa764 52static int elf_sort_sections (const void *, const void *);
c84fca4d 53static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
ef10c3ac 54static bfd_boolean swap_out_syms (bfd *, struct elf_strtab_hash **, int) ;
718175fa 55static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
276da9b3 56 file_ptr offset, size_t align);
50b2bdb7 57
252b5132
RH
58/* Swap version information in and out. The version information is
59 currently size independent. If that ever changes, this code will
60 need to move into elfcode.h. */
61
62/* Swap in a Verdef structure. */
63
64void
217aa764
AM
65_bfd_elf_swap_verdef_in (bfd *abfd,
66 const Elf_External_Verdef *src,
67 Elf_Internal_Verdef *dst)
252b5132 68{
dc810e39
AM
69 dst->vd_version = H_GET_16 (abfd, src->vd_version);
70 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
71 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
72 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
73 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
74 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
75 dst->vd_next = H_GET_32 (abfd, src->vd_next);
252b5132
RH
76}
77
78/* Swap out a Verdef structure. */
79
80void
217aa764
AM
81_bfd_elf_swap_verdef_out (bfd *abfd,
82 const Elf_Internal_Verdef *src,
83 Elf_External_Verdef *dst)
252b5132 84{
dc810e39
AM
85 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
86 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
87 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
88 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
89 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
90 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
91 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
252b5132
RH
92}
93
94/* Swap in a Verdaux structure. */
95
96void
217aa764
AM
97_bfd_elf_swap_verdaux_in (bfd *abfd,
98 const Elf_External_Verdaux *src,
99 Elf_Internal_Verdaux *dst)
252b5132 100{
dc810e39
AM
101 dst->vda_name = H_GET_32 (abfd, src->vda_name);
102 dst->vda_next = H_GET_32 (abfd, src->vda_next);
252b5132
RH
103}
104
105/* Swap out a Verdaux structure. */
106
107void
217aa764
AM
108_bfd_elf_swap_verdaux_out (bfd *abfd,
109 const Elf_Internal_Verdaux *src,
110 Elf_External_Verdaux *dst)
252b5132 111{
dc810e39
AM
112 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
113 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
252b5132
RH
114}
115
116/* Swap in a Verneed structure. */
117
118void
217aa764
AM
119_bfd_elf_swap_verneed_in (bfd *abfd,
120 const Elf_External_Verneed *src,
121 Elf_Internal_Verneed *dst)
252b5132 122{
dc810e39
AM
123 dst->vn_version = H_GET_16 (abfd, src->vn_version);
124 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
125 dst->vn_file = H_GET_32 (abfd, src->vn_file);
126 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
127 dst->vn_next = H_GET_32 (abfd, src->vn_next);
252b5132
RH
128}
129
130/* Swap out a Verneed structure. */
131
132void
217aa764
AM
133_bfd_elf_swap_verneed_out (bfd *abfd,
134 const Elf_Internal_Verneed *src,
135 Elf_External_Verneed *dst)
252b5132 136{
dc810e39
AM
137 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
138 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
139 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
140 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
141 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
252b5132
RH
142}
143
144/* Swap in a Vernaux structure. */
145
146void
217aa764
AM
147_bfd_elf_swap_vernaux_in (bfd *abfd,
148 const Elf_External_Vernaux *src,
149 Elf_Internal_Vernaux *dst)
252b5132 150{
dc810e39
AM
151 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
152 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
153 dst->vna_other = H_GET_16 (abfd, src->vna_other);
154 dst->vna_name = H_GET_32 (abfd, src->vna_name);
155 dst->vna_next = H_GET_32 (abfd, src->vna_next);
252b5132
RH
156}
157
158/* Swap out a Vernaux structure. */
159
160void
217aa764
AM
161_bfd_elf_swap_vernaux_out (bfd *abfd,
162 const Elf_Internal_Vernaux *src,
163 Elf_External_Vernaux *dst)
252b5132 164{
dc810e39
AM
165 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
166 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
167 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
168 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
169 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
252b5132
RH
170}
171
172/* Swap in a Versym structure. */
173
174void
217aa764
AM
175_bfd_elf_swap_versym_in (bfd *abfd,
176 const Elf_External_Versym *src,
177 Elf_Internal_Versym *dst)
252b5132 178{
dc810e39 179 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
252b5132
RH
180}
181
182/* Swap out a Versym structure. */
183
184void
217aa764
AM
185_bfd_elf_swap_versym_out (bfd *abfd,
186 const Elf_Internal_Versym *src,
187 Elf_External_Versym *dst)
252b5132 188{
dc810e39 189 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
252b5132
RH
190}
191
192/* Standard ELF hash function. Do not change this function; you will
193 cause invalid hash tables to be generated. */
3a99b017 194
252b5132 195unsigned long
217aa764 196bfd_elf_hash (const char *namearg)
252b5132 197{
3a99b017 198 const unsigned char *name = (const unsigned char *) namearg;
252b5132
RH
199 unsigned long h = 0;
200 unsigned long g;
201 int ch;
202
203 while ((ch = *name++) != '\0')
204 {
205 h = (h << 4) + ch;
206 if ((g = (h & 0xf0000000)) != 0)
207 {
208 h ^= g >> 24;
209 /* The ELF ABI says `h &= ~g', but this is equivalent in
210 this case and on some machines one insn instead of two. */
211 h ^= g;
212 }
213 }
32dfa85d 214 return h & 0xffffffff;
252b5132
RH
215}
216
fdc90cb4
JJ
217/* DT_GNU_HASH hash function. Do not change this function; you will
218 cause invalid hash tables to be generated. */
219
220unsigned long
221bfd_elf_gnu_hash (const char *namearg)
222{
223 const unsigned char *name = (const unsigned char *) namearg;
224 unsigned long h = 5381;
225 unsigned char ch;
226
227 while ((ch = *name++) != '\0')
228 h = (h << 5) + h + ch;
229 return h & 0xffffffff;
230}
231
0c8d6e5c
AM
232/* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
233 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
b34976b6 234bfd_boolean
0c8d6e5c 235bfd_elf_allocate_object (bfd *abfd,
0ffa91dd 236 size_t object_size,
4dfe6ac6 237 enum elf_target_id object_id)
252b5132 238{
0ffa91dd
NC
239 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
240 abfd->tdata.any = bfd_zalloc (abfd, object_size);
241 if (abfd->tdata.any == NULL)
242 return FALSE;
252b5132 243
0ffa91dd 244 elf_object_id (abfd) = object_id;
c0355132
AM
245 if (abfd->direction != read_direction)
246 {
247 struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
248 if (o == NULL)
249 return FALSE;
250 elf_tdata (abfd)->o = o;
251 elf_program_header_size (abfd) = (bfd_size_type) -1;
252 }
b34976b6 253 return TRUE;
252b5132
RH
254}
255
0ffa91dd
NC
256
257bfd_boolean
ae95ffa6 258bfd_elf_make_object (bfd *abfd)
0ffa91dd 259{
ae95ffa6 260 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
0ffa91dd 261 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
ae95ffa6 262 bed->target_id);
0ffa91dd
NC
263}
264
b34976b6 265bfd_boolean
217aa764 266bfd_elf_mkcorefile (bfd *abfd)
252b5132 267{
c044fabd 268 /* I think this can be done just like an object file. */
228e534f
AM
269 if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
270 return FALSE;
271 elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
272 return elf_tdata (abfd)->core != NULL;
252b5132
RH
273}
274
6d5944fc 275char *
217aa764 276bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
252b5132
RH
277{
278 Elf_Internal_Shdr **i_shdrp;
f075ee0c 279 bfd_byte *shstrtab = NULL;
dc810e39
AM
280 file_ptr offset;
281 bfd_size_type shstrtabsize;
252b5132
RH
282
283 i_shdrp = elf_elfsections (abfd);
74f2e02b
AM
284 if (i_shdrp == 0
285 || shindex >= elf_numsections (abfd)
286 || i_shdrp[shindex] == 0)
f075ee0c 287 return NULL;
252b5132 288
f075ee0c 289 shstrtab = i_shdrp[shindex]->contents;
252b5132
RH
290 if (shstrtab == NULL)
291 {
c044fabd 292 /* No cached one, attempt to read, and cache what we read. */
252b5132
RH
293 offset = i_shdrp[shindex]->sh_offset;
294 shstrtabsize = i_shdrp[shindex]->sh_size;
c6c60d09
JJ
295
296 /* Allocate and clear an extra byte at the end, to prevent crashes
297 in case the string table is not terminated. */
3471d59d 298 if (shstrtabsize + 1 <= 1
06614111 299 || bfd_seek (abfd, offset, SEEK_SET) != 0
2bb3687b
AM
300 || (shstrtab = _bfd_alloc_and_read (abfd, shstrtabsize + 1,
301 shstrtabsize)) == NULL)
302 {
3471d59d
CC
303 /* Once we've failed to read it, make sure we don't keep
304 trying. Otherwise, we'll keep allocating space for
305 the string table over and over. */
306 i_shdrp[shindex]->sh_size = 0;
c6c60d09
JJ
307 }
308 else
309 shstrtab[shstrtabsize] = '\0';
217aa764 310 i_shdrp[shindex]->contents = shstrtab;
252b5132 311 }
f075ee0c 312 return (char *) shstrtab;
252b5132
RH
313}
314
315char *
217aa764
AM
316bfd_elf_string_from_elf_section (bfd *abfd,
317 unsigned int shindex,
318 unsigned int strindex)
252b5132
RH
319{
320 Elf_Internal_Shdr *hdr;
321
322 if (strindex == 0)
323 return "";
324
74f2e02b
AM
325 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
326 return NULL;
327
252b5132
RH
328 hdr = elf_elfsections (abfd)[shindex];
329
06614111
NC
330 if (hdr->contents == NULL)
331 {
332 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
333 {
334 /* PR 17512: file: f057ec89. */
695344c0 335 /* xgettext:c-format */
871b3ab2 336 _bfd_error_handler (_("%pB: attempt to load strings from"
63a5468a 337 " a non-string section (number %d)"),
06614111
NC
338 abfd, shindex);
339 return NULL;
340 }
b1fa9dd6 341
06614111
NC
342 if (bfd_elf_get_str_section (abfd, shindex) == NULL)
343 return NULL;
344 }
eed5def8
NC
345 else
346 {
347 /* PR 24273: The string section's contents may have already
348 been loaded elsewhere, eg because a corrupt file has the
349 string section index in the ELF header pointing at a group
350 section. So be paranoid, and test that the last byte of
351 the section is zero. */
352 if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
353 return NULL;
354 }
252b5132
RH
355
356 if (strindex >= hdr->sh_size)
357 {
1b3a8575 358 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
4eca0228 359 _bfd_error_handler
695344c0 360 /* xgettext:c-format */
2dcf00ce
AM
361 (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
362 abfd, strindex, (uint64_t) hdr->sh_size,
1b3a8575 363 (shindex == shstrndx && strindex == hdr->sh_name
252b5132 364 ? ".shstrtab"
1b3a8575 365 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
45b222d6 366 return NULL;
252b5132
RH
367 }
368
369 return ((char *) hdr->contents) + strindex;
370}
371
6cdc0ccc
AM
372/* Read and convert symbols to internal format.
373 SYMCOUNT specifies the number of symbols to read, starting from
374 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
375 are non-NULL, they are used to store the internal symbols, external
b7c368d0
NC
376 symbols, and symbol section index extensions, respectively.
377 Returns a pointer to the internal symbol buffer (malloced if necessary)
378 or NULL if there were no symbols or some kind of problem. */
6cdc0ccc
AM
379
380Elf_Internal_Sym *
217aa764
AM
381bfd_elf_get_elf_syms (bfd *ibfd,
382 Elf_Internal_Shdr *symtab_hdr,
383 size_t symcount,
384 size_t symoffset,
385 Elf_Internal_Sym *intsym_buf,
386 void *extsym_buf,
387 Elf_External_Sym_Shndx *extshndx_buf)
6cdc0ccc
AM
388{
389 Elf_Internal_Shdr *shndx_hdr;
217aa764 390 void *alloc_ext;
df622259 391 const bfd_byte *esym;
6cdc0ccc
AM
392 Elf_External_Sym_Shndx *alloc_extshndx;
393 Elf_External_Sym_Shndx *shndx;
4dd07732 394 Elf_Internal_Sym *alloc_intsym;
6cdc0ccc
AM
395 Elf_Internal_Sym *isym;
396 Elf_Internal_Sym *isymend;
9c5bfbb7 397 const struct elf_backend_data *bed;
6cdc0ccc 398 size_t extsym_size;
1f4361a7 399 size_t amt;
6cdc0ccc
AM
400 file_ptr pos;
401
e44a2c9c
AM
402 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
403 abort ();
404
6cdc0ccc
AM
405 if (symcount == 0)
406 return intsym_buf;
407
408 /* Normal syms might have section extension entries. */
409 shndx_hdr = NULL;
6a40cf0c
NC
410 if (elf_symtab_shndx_list (ibfd) != NULL)
411 {
412 elf_section_list * entry;
413 Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
414
415 /* Find an index section that is linked to this symtab section. */
416 for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
315350be
NC
417 {
418 /* PR 20063. */
419 if (entry->hdr.sh_link >= elf_numsections (ibfd))
420 continue;
421
422 if (sections[entry->hdr.sh_link] == symtab_hdr)
423 {
424 shndx_hdr = & entry->hdr;
425 break;
426 };
427 }
6a40cf0c
NC
428
429 if (shndx_hdr == NULL)
430 {
431 if (symtab_hdr == & elf_symtab_hdr (ibfd))
432 /* Not really accurate, but this was how the old code used to work. */
433 shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr;
434 /* Otherwise we do nothing. The assumption is that
435 the index table will not be needed. */
436 }
437 }
6cdc0ccc
AM
438
439 /* Read the symbols. */
440 alloc_ext = NULL;
441 alloc_extshndx = NULL;
4dd07732 442 alloc_intsym = NULL;
6cdc0ccc
AM
443 bed = get_elf_backend_data (ibfd);
444 extsym_size = bed->s->sizeof_sym;
1f4361a7
AM
445 if (_bfd_mul_overflow (symcount, extsym_size, &amt))
446 {
447 bfd_set_error (bfd_error_file_too_big);
448 intsym_buf = NULL;
449 goto out;
450 }
6cdc0ccc
AM
451 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
452 if (extsym_buf == NULL)
453 {
1f4361a7 454 alloc_ext = bfd_malloc (amt);
6cdc0ccc
AM
455 extsym_buf = alloc_ext;
456 }
457 if (extsym_buf == NULL
458 || bfd_seek (ibfd, pos, SEEK_SET) != 0
459 || bfd_bread (extsym_buf, amt, ibfd) != amt)
460 {
461 intsym_buf = NULL;
462 goto out;
463 }
464
465 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
466 extshndx_buf = NULL;
467 else
468 {
1f4361a7
AM
469 if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
470 {
471 bfd_set_error (bfd_error_file_too_big);
472 intsym_buf = NULL;
473 goto out;
474 }
6cdc0ccc
AM
475 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
476 if (extshndx_buf == NULL)
477 {
1f4361a7 478 alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
6cdc0ccc
AM
479 extshndx_buf = alloc_extshndx;
480 }
481 if (extshndx_buf == NULL
482 || bfd_seek (ibfd, pos, SEEK_SET) != 0
483 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
484 {
485 intsym_buf = NULL;
486 goto out;
487 }
488 }
489
490 if (intsym_buf == NULL)
491 {
1f4361a7
AM
492 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
493 {
494 bfd_set_error (bfd_error_file_too_big);
495 goto out;
496 }
497 alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
4dd07732 498 intsym_buf = alloc_intsym;
6cdc0ccc
AM
499 if (intsym_buf == NULL)
500 goto out;
501 }
502
503 /* Convert the symbols to internal form. */
504 isymend = intsym_buf + symcount;
a50b1753 505 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
07d6d2b8 506 shndx = extshndx_buf;
6cdc0ccc
AM
507 isym < isymend;
508 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
8384fb8f
AM
509 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
510 {
511 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
695344c0 512 /* xgettext:c-format */
871b3ab2 513 _bfd_error_handler (_("%pB symbol number %lu references"
63a5468a 514 " nonexistent SHT_SYMTAB_SHNDX section"),
4eca0228 515 ibfd, (unsigned long) symoffset);
c9594989 516 free (alloc_intsym);
8384fb8f
AM
517 intsym_buf = NULL;
518 goto out;
519 }
6cdc0ccc
AM
520
521 out:
c9594989
AM
522 free (alloc_ext);
523 free (alloc_extshndx);
6cdc0ccc
AM
524
525 return intsym_buf;
526}
527
5cab59f6
AM
528/* Look up a symbol name. */
529const char *
be8dd2ca
AM
530bfd_elf_sym_name (bfd *abfd,
531 Elf_Internal_Shdr *symtab_hdr,
26c61ae5
L
532 Elf_Internal_Sym *isym,
533 asection *sym_sec)
5cab59f6 534{
26c61ae5 535 const char *name;
5cab59f6 536 unsigned int iname = isym->st_name;
be8dd2ca 537 unsigned int shindex = symtab_hdr->sh_link;
26c61ae5 538
138f35cc
JJ
539 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
540 /* Check for a bogus st_shndx to avoid crashing. */
4fbb74a6 541 && isym->st_shndx < elf_numsections (abfd))
5cab59f6
AM
542 {
543 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
544 shindex = elf_elfheader (abfd)->e_shstrndx;
545 }
546
26c61ae5
L
547 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
548 if (name == NULL)
549 name = "(null)";
550 else if (sym_sec && *name == '\0')
fd361982 551 name = bfd_section_name (sym_sec);
26c61ae5
L
552
553 return name;
5cab59f6
AM
554}
555
dbb410c3
AM
556/* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
557 sections. The first element is the flags, the rest are section
558 pointers. */
559
560typedef union elf_internal_group {
561 Elf_Internal_Shdr *shdr;
562 unsigned int flags;
563} Elf_Internal_Group;
564
b885599b
AM
565/* Return the name of the group signature symbol. Why isn't the
566 signature just a string? */
567
568static const char *
217aa764 569group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
b885599b 570{
9dce4196 571 Elf_Internal_Shdr *hdr;
9dce4196
AM
572 unsigned char esym[sizeof (Elf64_External_Sym)];
573 Elf_External_Sym_Shndx eshndx;
574 Elf_Internal_Sym isym;
b885599b 575
13792e9d
L
576 /* First we need to ensure the symbol table is available. Make sure
577 that it is a symbol table section. */
4fbb74a6
AM
578 if (ghdr->sh_link >= elf_numsections (abfd))
579 return NULL;
13792e9d
L
580 hdr = elf_elfsections (abfd) [ghdr->sh_link];
581 if (hdr->sh_type != SHT_SYMTAB
582 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
b885599b
AM
583 return NULL;
584
9dce4196
AM
585 /* Go read the symbol. */
586 hdr = &elf_tdata (abfd)->symtab_hdr;
6cdc0ccc
AM
587 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
588 &isym, esym, &eshndx) == NULL)
b885599b 589 return NULL;
9dce4196 590
26c61ae5 591 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
b885599b
AM
592}
593
dbb410c3
AM
594/* Set next_in_group list pointer, and group name for NEWSECT. */
595
b34976b6 596static bfd_boolean
217aa764 597setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
dbb410c3
AM
598{
599 unsigned int num_group = elf_tdata (abfd)->num_group;
600
601 /* If num_group is zero, read in all SHT_GROUP sections. The count
602 is set to -1 if there are no SHT_GROUP sections. */
603 if (num_group == 0)
604 {
605 unsigned int i, shnum;
606
607 /* First count the number of groups. If we have a SHT_GROUP
608 section with just a flag word (ie. sh_size is 4), ignore it. */
9ad5cbcf 609 shnum = elf_numsections (abfd);
dbb410c3 610 num_group = 0;
08a40648 611
44534af3 612#define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
1783205a 613 ( (shdr)->sh_type == SHT_GROUP \
44534af3 614 && (shdr)->sh_size >= minsize \
1783205a
NC
615 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
616 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
08a40648 617
dbb410c3
AM
618 for (i = 0; i < shnum; i++)
619 {
620 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
1783205a 621
44534af3 622 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
dbb410c3
AM
623 num_group += 1;
624 }
625
626 if (num_group == 0)
20dbb49d
L
627 {
628 num_group = (unsigned) -1;
629 elf_tdata (abfd)->num_group = num_group;
ce497010 630 elf_tdata (abfd)->group_sect_ptr = NULL;
20dbb49d
L
631 }
632 else
dbb410c3
AM
633 {
634 /* We keep a list of elf section headers for group sections,
635 so we can find them quickly. */
1f4361a7 636 size_t amt;
d0fb9a8d 637
20dbb49d 638 elf_tdata (abfd)->num_group = num_group;
1f4361a7
AM
639 amt = num_group * sizeof (Elf_Internal_Shdr *);
640 elf_tdata (abfd)->group_sect_ptr
641 = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
dbb410c3 642 if (elf_tdata (abfd)->group_sect_ptr == NULL)
b34976b6 643 return FALSE;
dbb410c3 644 num_group = 0;
ce497010 645
dbb410c3
AM
646 for (i = 0; i < shnum; i++)
647 {
648 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
1783205a 649
44534af3 650 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
dbb410c3 651 {
973ffd63 652 unsigned char *src;
dbb410c3
AM
653 Elf_Internal_Group *dest;
654
07d6d2b8
AM
655 /* Make sure the group section has a BFD section
656 attached to it. */
657 if (!bfd_section_from_shdr (abfd, i))
658 return FALSE;
659
dbb410c3
AM
660 /* Add to list of sections. */
661 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
662 num_group += 1;
663
664 /* Read the raw contents. */
1f4361a7
AM
665 BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0);
666 shdr->contents = NULL;
667 if (_bfd_mul_overflow (shdr->sh_size,
668 sizeof (*dest) / 4, &amt)
1f4361a7 669 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
2bb3687b
AM
670 || !(shdr->contents
671 = _bfd_alloc_and_read (abfd, amt, shdr->sh_size)))
493a3386
NC
672 {
673 _bfd_error_handler
695344c0 674 /* xgettext:c-format */
871b3ab2 675 (_("%pB: invalid size field in group section"
2dcf00ce
AM
676 " header: %#" PRIx64 ""),
677 abfd, (uint64_t) shdr->sh_size);
493a3386
NC
678 bfd_set_error (bfd_error_bad_value);
679 -- num_group;
493a3386
NC
680 continue;
681 }
708d7d0d 682
dbb410c3
AM
683 /* Translate raw contents, a flag word followed by an
684 array of elf section indices all in target byte order,
685 to the flag word followed by an array of elf section
686 pointers. */
687 src = shdr->contents + shdr->sh_size;
688 dest = (Elf_Internal_Group *) (shdr->contents + amt);
06614111 689
dbb410c3
AM
690 while (1)
691 {
692 unsigned int idx;
693
694 src -= 4;
695 --dest;
696 idx = H_GET_32 (abfd, src);
697 if (src == shdr->contents)
698 {
327301a4 699 dest->shdr = NULL;
dbb410c3 700 dest->flags = idx;
b885599b
AM
701 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
702 shdr->bfd_section->flags
703 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
dbb410c3
AM
704 break;
705 }
4bba0fb1 706 if (idx < shnum)
bae363f1
L
707 {
708 dest->shdr = elf_elfsections (abfd)[idx];
709 /* PR binutils/23199: All sections in a
710 section group should be marked with
711 SHF_GROUP. But some tools generate
712 broken objects without SHF_GROUP. Fix
713 them up here. */
714 dest->shdr->sh_flags |= SHF_GROUP;
715 }
4bba0fb1
AM
716 if (idx >= shnum
717 || dest->shdr->sh_type == SHT_GROUP)
dbb410c3 718 {
4eca0228 719 _bfd_error_handler
4bba0fb1
AM
720 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
721 abfd, i);
722 dest->shdr = NULL;
dbb410c3 723 }
dbb410c3
AM
724 }
725 }
726 }
493a3386
NC
727
728 /* PR 17510: Corrupt binaries might contain invalid groups. */
729 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
730 {
731 elf_tdata (abfd)->num_group = num_group;
732
733 /* If all groups are invalid then fail. */
734 if (num_group == 0)
735 {
736 elf_tdata (abfd)->group_sect_ptr = NULL;
737 elf_tdata (abfd)->num_group = num_group = -1;
4eca0228 738 _bfd_error_handler
871b3ab2 739 (_("%pB: no valid group sections found"), abfd);
493a3386
NC
740 bfd_set_error (bfd_error_bad_value);
741 }
742 }
dbb410c3
AM
743 }
744 }
745
746 if (num_group != (unsigned) -1)
747 {
564e11c9
JW
748 unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
749 unsigned int j;
dbb410c3 750
564e11c9 751 for (j = 0; j < num_group; j++)
dbb410c3 752 {
564e11c9
JW
753 /* Begin search from previous found group. */
754 unsigned i = (j + search_offset) % num_group;
755
dbb410c3 756 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
ce497010 757 Elf_Internal_Group *idx;
0c54f692 758 bfd_size_type n_elt;
ce497010
NC
759
760 if (shdr == NULL)
761 continue;
762
763 idx = (Elf_Internal_Group *) shdr->contents;
0c54f692
NC
764 if (idx == NULL || shdr->sh_size < 4)
765 {
766 /* See PR 21957 for a reproducer. */
767 /* xgettext:c-format */
871b3ab2 768 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
0c54f692
NC
769 abfd, shdr->bfd_section);
770 elf_tdata (abfd)->group_sect_ptr[i] = NULL;
771 bfd_set_error (bfd_error_bad_value);
772 return FALSE;
773 }
ce497010 774 n_elt = shdr->sh_size / 4;
dbb410c3
AM
775
776 /* Look through this group's sections to see if current
777 section is a member. */
778 while (--n_elt != 0)
779 if ((++idx)->shdr == hdr)
780 {
e0e8c97f 781 asection *s = NULL;
dbb410c3
AM
782
783 /* We are a member of this group. Go looking through
784 other members to see if any others are linked via
785 next_in_group. */
786 idx = (Elf_Internal_Group *) shdr->contents;
787 n_elt = shdr->sh_size / 4;
788 while (--n_elt != 0)
4bba0fb1
AM
789 if ((++idx)->shdr != NULL
790 && (s = idx->shdr->bfd_section) != NULL
945906ff 791 && elf_next_in_group (s) != NULL)
dbb410c3
AM
792 break;
793 if (n_elt != 0)
794 {
dbb410c3
AM
795 /* Snarf the group name from other member, and
796 insert current section in circular list. */
945906ff
AM
797 elf_group_name (newsect) = elf_group_name (s);
798 elf_next_in_group (newsect) = elf_next_in_group (s);
799 elf_next_in_group (s) = newsect;
dbb410c3
AM
800 }
801 else
802 {
dbb410c3
AM
803 const char *gname;
804
b885599b
AM
805 gname = group_signature (abfd, shdr);
806 if (gname == NULL)
b34976b6 807 return FALSE;
945906ff 808 elf_group_name (newsect) = gname;
dbb410c3
AM
809
810 /* Start a circular list with one element. */
945906ff 811 elf_next_in_group (newsect) = newsect;
dbb410c3 812 }
b885599b 813
9dce4196
AM
814 /* If the group section has been created, point to the
815 new member. */
dbb410c3 816 if (shdr->bfd_section != NULL)
945906ff 817 elf_next_in_group (shdr->bfd_section) = newsect;
b885599b 818
564e11c9
JW
819 elf_tdata (abfd)->group_search_offset = i;
820 j = num_group - 1;
dbb410c3
AM
821 break;
822 }
823 }
824 }
825
945906ff 826 if (elf_group_name (newsect) == NULL)
dbb410c3 827 {
695344c0 828 /* xgettext:c-format */
871b3ab2 829 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
4eca0228 830 abfd, newsect);
493a3386 831 return FALSE;
dbb410c3 832 }
b34976b6 833 return TRUE;
dbb410c3
AM
834}
835
3d7f7666 836bfd_boolean
dd863624 837_bfd_elf_setup_sections (bfd *abfd)
3d7f7666
L
838{
839 unsigned int i;
840 unsigned int num_group = elf_tdata (abfd)->num_group;
841 bfd_boolean result = TRUE;
dd863624
L
842 asection *s;
843
844 /* Process SHF_LINK_ORDER. */
845 for (s = abfd->sections; s != NULL; s = s->next)
846 {
847 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
848 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
849 {
850 unsigned int elfsec = this_hdr->sh_link;
b71702f1
NC
851 /* An sh_link value of 0 is now allowed. It indicates that linked
852 to section has already been discarded, but that the current
853 section has been retained for some other reason. This linking
854 section is still a candidate for later garbage collection
855 however. */
dd863624
L
856 if (elfsec == 0)
857 {
b71702f1 858 elf_linked_to_section (s) = NULL;
dd863624
L
859 }
860 else
861 {
91d6fa6a 862 asection *linksec = NULL;
25bbc984 863
4fbb74a6
AM
864 if (elfsec < elf_numsections (abfd))
865 {
866 this_hdr = elf_elfsections (abfd)[elfsec];
91d6fa6a 867 linksec = this_hdr->bfd_section;
4fbb74a6 868 }
25bbc984
L
869
870 /* PR 1991, 2008:
871 Some strip/objcopy may leave an incorrect value in
872 sh_link. We don't want to proceed. */
91d6fa6a 873 if (linksec == NULL)
25bbc984 874 {
4eca0228 875 _bfd_error_handler
695344c0 876 /* xgettext:c-format */
871b3ab2 877 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
c08bb8dd 878 s->owner, elfsec, s);
25bbc984
L
879 result = FALSE;
880 }
881
91d6fa6a 882 elf_linked_to_section (s) = linksec;
dd863624
L
883 }
884 }
53720c49
AM
885 else if (this_hdr->sh_type == SHT_GROUP
886 && elf_next_in_group (s) == NULL)
887 {
4eca0228 888 _bfd_error_handler
695344c0 889 /* xgettext:c-format */
871b3ab2 890 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
53720c49
AM
891 abfd, elf_section_data (s)->this_idx);
892 result = FALSE;
893 }
dd863624 894 }
3d7f7666 895
dd863624 896 /* Process section groups. */
3d7f7666
L
897 if (num_group == (unsigned) -1)
898 return result;
899
900 for (i = 0; i < num_group; i++)
901 {
902 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
4b0e8a5f
NC
903 Elf_Internal_Group *idx;
904 unsigned int n_elt;
3d7f7666 905
4b0e8a5f
NC
906 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
907 if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
908 {
4eca0228 909 _bfd_error_handler
695344c0 910 /* xgettext:c-format */
871b3ab2 911 (_("%pB: section group entry number %u is corrupt"),
4b0e8a5f
NC
912 abfd, i);
913 result = FALSE;
914 continue;
915 }
916
917 idx = (Elf_Internal_Group *) shdr->contents;
918 n_elt = shdr->sh_size / 4;
1b786873 919
3d7f7666 920 while (--n_elt != 0)
24d3e51b
NC
921 {
922 ++ idx;
923
924 if (idx->shdr == NULL)
925 continue;
926 else if (idx->shdr->bfd_section)
927 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
db4677b8
AM
928 else if (idx->shdr->sh_type != SHT_RELA
929 && idx->shdr->sh_type != SHT_REL)
24d3e51b
NC
930 {
931 /* There are some unknown sections in the group. */
932 _bfd_error_handler
933 /* xgettext:c-format */
871b3ab2 934 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
24d3e51b
NC
935 abfd,
936 idx->shdr->sh_type,
937 bfd_elf_string_from_elf_section (abfd,
938 (elf_elfheader (abfd)
939 ->e_shstrndx),
940 idx->shdr->sh_name),
941 shdr->bfd_section);
942 result = FALSE;
943 }
944 }
3d7f7666 945 }
24d3e51b 946
3d7f7666
L
947 return result;
948}
949
72adc230
AM
950bfd_boolean
951bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
952{
953 return elf_next_in_group (sec) != NULL;
954}
955
cb7f4b29
AM
956const char *
957bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
958{
959 if (elf_sec_group (sec) != NULL)
960 return elf_group_name (sec);
961 return NULL;
962}
963
f6fe1ccd
L
964static char *
965convert_debug_to_zdebug (bfd *abfd, const char *name)
966{
967 unsigned int len = strlen (name);
968 char *new_name = bfd_alloc (abfd, len + 2);
969 if (new_name == NULL)
970 return NULL;
971 new_name[0] = '.';
972 new_name[1] = 'z';
973 memcpy (new_name + 2, name + 1, len);
974 return new_name;
975}
976
977static char *
978convert_zdebug_to_debug (bfd *abfd, const char *name)
979{
980 unsigned int len = strlen (name);
981 char *new_name = bfd_alloc (abfd, len);
982 if (new_name == NULL)
983 return NULL;
984 new_name[0] = '.';
985 memcpy (new_name + 1, name + 2, len - 1);
986 return new_name;
987}
988
cc5277b1
ML
989/* This a copy of lto_section defined in GCC (lto-streamer.h). */
990
991struct lto_section
992{
993 int16_t major_version;
994 int16_t minor_version;
995 unsigned char slim_object;
996
997 /* Flags is a private field that is not defined publicly. */
998 uint16_t flags;
999};
1000
252b5132
RH
1001/* Make a BFD section from an ELF section. We store a pointer to the
1002 BFD section in the bfd_section field of the header. */
1003
b34976b6 1004bfd_boolean
217aa764
AM
1005_bfd_elf_make_section_from_shdr (bfd *abfd,
1006 Elf_Internal_Shdr *hdr,
6dc132d9
L
1007 const char *name,
1008 int shindex)
252b5132
RH
1009{
1010 asection *newsect;
1011 flagword flags;
9c5bfbb7 1012 const struct elf_backend_data *bed;
502794d4 1013 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132
RH
1014
1015 if (hdr->bfd_section != NULL)
4e011fb5 1016 return TRUE;
252b5132
RH
1017
1018 newsect = bfd_make_section_anyway (abfd, name);
1019 if (newsect == NULL)
b34976b6 1020 return FALSE;
252b5132 1021
1829f4b2
AM
1022 hdr->bfd_section = newsect;
1023 elf_section_data (newsect)->this_hdr = *hdr;
6dc132d9 1024 elf_section_data (newsect)->this_idx = shindex;
1829f4b2 1025
2f89ff8d
L
1026 /* Always use the real type/flags. */
1027 elf_section_type (newsect) = hdr->sh_type;
1028 elf_section_flags (newsect) = hdr->sh_flags;
1029
252b5132
RH
1030 newsect->filepos = hdr->sh_offset;
1031
252b5132
RH
1032 flags = SEC_NO_FLAGS;
1033 if (hdr->sh_type != SHT_NOBITS)
1034 flags |= SEC_HAS_CONTENTS;
dbb410c3 1035 if (hdr->sh_type == SHT_GROUP)
7bdf4127 1036 flags |= SEC_GROUP;
252b5132
RH
1037 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1038 {
1039 flags |= SEC_ALLOC;
1040 if (hdr->sh_type != SHT_NOBITS)
1041 flags |= SEC_LOAD;
1042 }
1043 if ((hdr->sh_flags & SHF_WRITE) == 0)
1044 flags |= SEC_READONLY;
1045 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1046 flags |= SEC_CODE;
1047 else if ((flags & SEC_LOAD) != 0)
1048 flags |= SEC_DATA;
f5fa8ca2
JJ
1049 if ((hdr->sh_flags & SHF_MERGE) != 0)
1050 {
1051 flags |= SEC_MERGE;
1052 newsect->entsize = hdr->sh_entsize;
f5fa8ca2 1053 }
84865015
NC
1054 if ((hdr->sh_flags & SHF_STRINGS) != 0)
1055 flags |= SEC_STRINGS;
dbb410c3
AM
1056 if (hdr->sh_flags & SHF_GROUP)
1057 if (!setup_group (abfd, hdr, newsect))
b34976b6 1058 return FALSE;
13ae64f3
JJ
1059 if ((hdr->sh_flags & SHF_TLS) != 0)
1060 flags |= SEC_THREAD_LOCAL;
18ae9cc1
L
1061 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1062 flags |= SEC_EXCLUDE;
252b5132 1063
df3a023b
AM
1064 switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
1065 {
1066 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1067 but binutils as of 2019-07-23 did not set the EI_OSABI header
1068 byte. */
1069 case ELFOSABI_NONE:
1070 case ELFOSABI_GNU:
1071 case ELFOSABI_FREEBSD:
1072 if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
1073 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1074 break;
1075 }
1076
3d2b39cf 1077 if ((flags & SEC_ALLOC) == 0)
7a6cc5fb 1078 {
3d2b39cf
L
1079 /* The debugging sections appear to be recognized only by name,
1080 not any sort of flag. Their SEC_ALLOC bits are cleared. */
3d2b39cf
L
1081 if (name [0] == '.')
1082 {
bb294208
AM
1083 if (strncmp (name, ".debug", 6) == 0
1084 || strncmp (name, ".gnu.linkonce.wi.", 17) == 0
1085 || strncmp (name, ".zdebug", 7) == 0)
1086 flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
1087 else if (strncmp (name, GNU_BUILD_ATTRS_SECTION_NAME, 21) == 0
1088 || strncmp (name, ".note.gnu", 9) == 0)
502794d4
CE
1089 {
1090 flags |= SEC_ELF_OCTETS;
1091 opb = 1;
1092 }
bb294208
AM
1093 else if (strncmp (name, ".line", 5) == 0
1094 || strncmp (name, ".stab", 5) == 0
1095 || strcmp (name, ".gdb_index") == 0)
3d2b39cf
L
1096 flags |= SEC_DEBUGGING;
1097 }
1098 }
252b5132 1099
502794d4
CE
1100 if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
1101 || !bfd_set_section_size (newsect, hdr->sh_size)
1102 || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign)))
1103 return FALSE;
1104
252b5132
RH
1105 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1106 only link a single copy of the section. This is used to support
1107 g++. g++ will emit each template expansion in its own section.
1108 The symbols will be defined as weak, so that multiple definitions
1109 are permitted. The GNU linker extension is to actually discard
1110 all but one of the sections. */
0112cd26 1111 if (CONST_STRNEQ (name, ".gnu.linkonce")
b885599b 1112 && elf_next_in_group (newsect) == NULL)
252b5132
RH
1113 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1114
8c803a2d
AM
1115 if (!bfd_set_section_flags (newsect, flags))
1116 return FALSE;
1117
fa152c49
JW
1118 bed = get_elf_backend_data (abfd);
1119 if (bed->elf_backend_section_flags)
8c803a2d 1120 if (!bed->elf_backend_section_flags (hdr))
b34976b6 1121 return FALSE;
fa152c49 1122
718175fa
JK
1123 /* We do not parse the PT_NOTE segments as we are interested even in the
1124 separate debug info files which may have the segments offsets corrupted.
1125 PT_NOTEs from the core files are currently not parsed using BFD. */
1126 if (hdr->sh_type == SHT_NOTE)
1127 {
baea7ef1 1128 bfd_byte *contents;
718175fa 1129
baea7ef1 1130 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
718175fa
JK
1131 return FALSE;
1132
276da9b3
L
1133 elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1134 hdr->sh_offset, hdr->sh_addralign);
718175fa
JK
1135 free (contents);
1136 }
1137
8c803a2d 1138 if ((newsect->flags & SEC_ALLOC) != 0)
252b5132
RH
1139 {
1140 Elf_Internal_Phdr *phdr;
6ffd7900
AM
1141 unsigned int i, nload;
1142
1143 /* Some ELF linkers produce binaries with all the program header
1144 p_paddr fields zero. If we have such a binary with more than
1145 one PT_LOAD header, then leave the section lma equal to vma
1146 so that we don't create sections with overlapping lma. */
1147 phdr = elf_tdata (abfd)->phdr;
1148 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1149 if (phdr->p_paddr != 0)
1150 break;
1151 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1152 ++nload;
1153 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1154 return TRUE;
252b5132 1155
252b5132
RH
1156 phdr = elf_tdata (abfd)->phdr;
1157 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1158 {
86b2281f
AM
1159 if (((phdr->p_type == PT_LOAD
1160 && (hdr->sh_flags & SHF_TLS) == 0)
1161 || phdr->p_type == PT_TLS)
9a83a553 1162 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
252b5132 1163 {
8c803a2d 1164 if ((newsect->flags & SEC_LOAD) == 0)
88967714 1165 newsect->lma = (phdr->p_paddr
502794d4 1166 + hdr->sh_addr - phdr->p_vaddr) / opb;
88967714
AM
1167 else
1168 /* We used to use the same adjustment for SEC_LOAD
1169 sections, but that doesn't work if the segment
1170 is packed with code from multiple VMAs.
1171 Instead we calculate the section LMA based on
1172 the segment LMA. It is assumed that the
1173 segment will contain sections with contiguous
1174 LMAs, even if the VMAs are not. */
1175 newsect->lma = (phdr->p_paddr
502794d4 1176 + hdr->sh_offset - phdr->p_offset) / opb;
88967714
AM
1177
1178 /* With contiguous segments, we can't tell from file
1179 offsets whether a section with zero size should
1180 be placed at the end of one segment or the
1181 beginning of the next. Decide based on vaddr. */
1182 if (hdr->sh_addr >= phdr->p_vaddr
1183 && (hdr->sh_addr + hdr->sh_size
1184 <= phdr->p_vaddr + phdr->p_memsz))
1185 break;
252b5132
RH
1186 }
1187 }
1188 }
1189
4a114e3e
L
1190 /* Compress/decompress DWARF debug sections with names: .debug_* and
1191 .zdebug_*, after the section flags is set. */
8c803a2d 1192 if ((newsect->flags & SEC_DEBUGGING)
4a114e3e
L
1193 && ((name[1] == 'd' && name[6] == '_')
1194 || (name[1] == 'z' && name[7] == '_')))
1195 {
1196 enum { nothing, compress, decompress } action = nothing;
151411f8 1197 int compression_header_size;
dab394de 1198 bfd_size_type uncompressed_size;
4207142d 1199 unsigned int uncompressed_align_power;
151411f8
L
1200 bfd_boolean compressed
1201 = bfd_is_section_compressed_with_header (abfd, newsect,
dab394de 1202 &compression_header_size,
4207142d
MW
1203 &uncompressed_size,
1204 &uncompressed_align_power);
151411f8 1205 if (compressed)
4a114e3e
L
1206 {
1207 /* Compressed section. Check if we should decompress. */
1208 if ((abfd->flags & BFD_DECOMPRESS))
1209 action = decompress;
1210 }
151411f8
L
1211
1212 /* Compress the uncompressed section or convert from/to .zdebug*
1213 section. Check if we should compress. */
1214 if (action == nothing)
4a114e3e 1215 {
151411f8
L
1216 if (newsect->size != 0
1217 && (abfd->flags & BFD_COMPRESS)
1218 && compression_header_size >= 0
dab394de 1219 && uncompressed_size > 0
151411f8
L
1220 && (!compressed
1221 || ((compression_header_size > 0)
1222 != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
4a114e3e 1223 action = compress;
151411f8
L
1224 else
1225 return TRUE;
4a114e3e
L
1226 }
1227
151411f8 1228 if (action == compress)
4a114e3e 1229 {
4a114e3e
L
1230 if (!bfd_init_section_compress_status (abfd, newsect))
1231 {
4eca0228 1232 _bfd_error_handler
695344c0 1233 /* xgettext:c-format */
871b3ab2 1234 (_("%pB: unable to initialize compress status for section %s"),
4a114e3e
L
1235 abfd, name);
1236 return FALSE;
1237 }
151411f8
L
1238 }
1239 else
1240 {
4a114e3e
L
1241 if (!bfd_init_section_decompress_status (abfd, newsect))
1242 {
4eca0228 1243 _bfd_error_handler
695344c0 1244 /* xgettext:c-format */
871b3ab2 1245 (_("%pB: unable to initialize decompress status for section %s"),
4a114e3e
L
1246 abfd, name);
1247 return FALSE;
1248 }
151411f8
L
1249 }
1250
f6fe1ccd 1251 if (abfd->is_linker_input)
151411f8 1252 {
f6fe1ccd
L
1253 if (name[1] == 'z'
1254 && (action == decompress
1255 || (action == compress
1256 && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
4e011fb5 1257 {
f6fe1ccd
L
1258 /* Convert section name from .zdebug_* to .debug_* so
1259 that linker will consider this section as a debug
1260 section. */
1261 char *new_name = convert_zdebug_to_debug (abfd, name);
151411f8
L
1262 if (new_name == NULL)
1263 return FALSE;
fd361982 1264 bfd_rename_section (newsect, new_name);
151411f8 1265 }
4a114e3e 1266 }
f6fe1ccd
L
1267 else
1268 /* For objdump, don't rename the section. For objcopy, delay
1269 section rename to elf_fake_sections. */
1270 newsect->flags |= SEC_ELF_RENAME;
4a114e3e
L
1271 }
1272
cc5277b1
ML
1273 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1274 section. */
1275 const char *lto_section_name = ".gnu.lto_.lto.";
1276 if (strncmp (name, lto_section_name, strlen (lto_section_name)) == 0)
1277 {
1278 struct lto_section lsection;
1279 if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
1280 sizeof (struct lto_section)))
1281 abfd->lto_slim_object = lsection.slim_object;
1282 }
1283
b34976b6 1284 return TRUE;
252b5132
RH
1285}
1286
84865015
NC
1287const char *const bfd_elf_section_type_names[] =
1288{
252b5132
RH
1289 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1290 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1291 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1292};
1293
1049f94e 1294/* ELF relocs are against symbols. If we are producing relocatable
252b5132
RH
1295 output, and the reloc is against an external symbol, and nothing
1296 has given us any additional addend, the resulting reloc will also
1297 be against the same symbol. In such a case, we don't want to
1298 change anything about the way the reloc is handled, since it will
1299 all be done at final link time. Rather than put special case code
1300 into bfd_perform_relocation, all the reloc types use this howto
1301 function. It just short circuits the reloc if producing
1049f94e 1302 relocatable output against an external symbol. */
252b5132 1303
252b5132 1304bfd_reloc_status_type
217aa764
AM
1305bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1306 arelent *reloc_entry,
1307 asymbol *symbol,
1308 void *data ATTRIBUTE_UNUSED,
1309 asection *input_section,
1310 bfd *output_bfd,
1311 char **error_message ATTRIBUTE_UNUSED)
1312{
1313 if (output_bfd != NULL
252b5132
RH
1314 && (symbol->flags & BSF_SECTION_SYM) == 0
1315 && (! reloc_entry->howto->partial_inplace
1316 || reloc_entry->addend == 0))
1317 {
1318 reloc_entry->address += input_section->output_offset;
1319 return bfd_reloc_ok;
1320 }
1321
1322 return bfd_reloc_continue;
1323}
1324\f
84865015
NC
1325/* Returns TRUE if section A matches section B.
1326 Names, addresses and links may be different, but everything else
1327 should be the same. */
1328
1329static bfd_boolean
5522f910
NC
1330section_match (const Elf_Internal_Shdr * a,
1331 const Elf_Internal_Shdr * b)
84865015 1332{
ac85e67c
AM
1333 if (a->sh_type != b->sh_type
1334 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1335 || a->sh_addralign != b->sh_addralign
1336 || a->sh_entsize != b->sh_entsize)
1337 return FALSE;
1338 if (a->sh_type == SHT_SYMTAB
1339 || a->sh_type == SHT_STRTAB)
1340 return TRUE;
1341 return a->sh_size == b->sh_size;
84865015
NC
1342}
1343
1344/* Find a section in OBFD that has the same characteristics
1345 as IHEADER. Return the index of this section or SHN_UNDEF if
1346 none can be found. Check's section HINT first, as this is likely
1347 to be the correct section. */
1348
1349static unsigned int
5cc4ca83
ST
1350find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1351 const unsigned int hint)
84865015
NC
1352{
1353 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1354 unsigned int i;
1355
a55c9876
NC
1356 BFD_ASSERT (iheader != NULL);
1357
1358 /* See PR 20922 for a reproducer of the NULL test. */
5cc4ca83
ST
1359 if (hint < elf_numsections (obfd)
1360 && oheaders[hint] != NULL
a55c9876 1361 && section_match (oheaders[hint], iheader))
84865015
NC
1362 return hint;
1363
1364 for (i = 1; i < elf_numsections (obfd); i++)
1365 {
1366 Elf_Internal_Shdr * oheader = oheaders[i];
1367
a55c9876
NC
1368 if (oheader == NULL)
1369 continue;
84865015
NC
1370 if (section_match (oheader, iheader))
1371 /* FIXME: Do we care if there is a potential for
1372 multiple matches ? */
1373 return i;
1374 }
1375
1376 return SHN_UNDEF;
1377}
1378
5522f910
NC
1379/* PR 19938: Attempt to set the ELF section header fields of an OS or
1380 Processor specific section, based upon a matching input section.
1381 Returns TRUE upon success, FALSE otherwise. */
07d6d2b8 1382
5522f910
NC
1383static bfd_boolean
1384copy_special_section_fields (const bfd *ibfd,
1385 bfd *obfd,
1386 const Elf_Internal_Shdr *iheader,
1387 Elf_Internal_Shdr *oheader,
1388 const unsigned int secnum)
1389{
1390 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1391 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1392 bfd_boolean changed = FALSE;
1393 unsigned int sh_link;
1394
1395 if (oheader->sh_type == SHT_NOBITS)
1396 {
1397 /* This is a feature for objcopy --only-keep-debug:
1398 When a section's type is changed to NOBITS, we preserve
1399 the sh_link and sh_info fields so that they can be
1400 matched up with the original.
1401
1402 Note: Strictly speaking these assignments are wrong.
1403 The sh_link and sh_info fields should point to the
1404 relevent sections in the output BFD, which may not be in
1405 the same location as they were in the input BFD. But
1406 the whole point of this action is to preserve the
1407 original values of the sh_link and sh_info fields, so
1408 that they can be matched up with the section headers in
1409 the original file. So strictly speaking we may be
1410 creating an invalid ELF file, but it is only for a file
1411 that just contains debug info and only for sections
1412 without any contents. */
1413 if (oheader->sh_link == 0)
1414 oheader->sh_link = iheader->sh_link;
1415 if (oheader->sh_info == 0)
1416 oheader->sh_info = iheader->sh_info;
1417 return TRUE;
1418 }
1419
1420 /* Allow the target a chance to decide how these fields should be set. */
a859124d
AM
1421 if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1422 iheader, oheader))
5522f910
NC
1423 return TRUE;
1424
1425 /* We have an iheader which might match oheader, and which has non-zero
1426 sh_info and/or sh_link fields. Attempt to follow those links and find
1427 the section in the output bfd which corresponds to the linked section
1428 in the input bfd. */
1429 if (iheader->sh_link != SHN_UNDEF)
1430 {
4f3ca05b
NC
1431 /* See PR 20931 for a reproducer. */
1432 if (iheader->sh_link >= elf_numsections (ibfd))
1433 {
76cfced5 1434 _bfd_error_handler
4f3ca05b 1435 /* xgettext:c-format */
9793eb77 1436 (_("%pB: invalid sh_link field (%d) in section number %d"),
4f3ca05b
NC
1437 ibfd, iheader->sh_link, secnum);
1438 return FALSE;
1439 }
1440
5522f910
NC
1441 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1442 if (sh_link != SHN_UNDEF)
1443 {
1444 oheader->sh_link = sh_link;
1445 changed = TRUE;
1446 }
1447 else
1448 /* FIXME: Should we install iheader->sh_link
1449 if we could not find a match ? */
76cfced5 1450 _bfd_error_handler
695344c0 1451 /* xgettext:c-format */
9793eb77 1452 (_("%pB: failed to find link section for section %d"), obfd, secnum);
5522f910
NC
1453 }
1454
1455 if (iheader->sh_info)
1456 {
1457 /* The sh_info field can hold arbitrary information, but if the
1458 SHF_LINK_INFO flag is set then it should be interpreted as a
1459 section index. */
1460 if (iheader->sh_flags & SHF_INFO_LINK)
1461 {
1462 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1463 iheader->sh_info);
1464 if (sh_link != SHN_UNDEF)
1465 oheader->sh_flags |= SHF_INFO_LINK;
1466 }
1467 else
1468 /* No idea what it means - just copy it. */
1469 sh_link = iheader->sh_info;
1470
1471 if (sh_link != SHN_UNDEF)
1472 {
1473 oheader->sh_info = sh_link;
1474 changed = TRUE;
1475 }
1476 else
76cfced5 1477 _bfd_error_handler
695344c0 1478 /* xgettext:c-format */
9793eb77 1479 (_("%pB: failed to find info section for section %d"), obfd, secnum);
5522f910
NC
1480 }
1481
1482 return changed;
1483}
07d6d2b8 1484
0ac4564e
L
1485/* Copy the program header and other data from one object module to
1486 another. */
252b5132 1487
b34976b6 1488bfd_boolean
217aa764 1489_bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
2d502050 1490{
5522f910
NC
1491 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1492 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1493 const struct elf_backend_data *bed;
84865015
NC
1494 unsigned int i;
1495
2d502050 1496 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
84865015 1497 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 1498 return TRUE;
2d502050 1499
57b828ef
L
1500 if (!elf_flags_init (obfd))
1501 {
1502 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1503 elf_flags_init (obfd) = TRUE;
1504 }
2d502050 1505
0ac4564e 1506 elf_gp (obfd) = elf_gp (ibfd);
57b828ef
L
1507
1508 /* Also copy the EI_OSABI field. */
1509 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1510 elf_elfheader (ibfd)->e_ident[EI_OSABI];
104d59d1 1511
5522f910
NC
1512 /* If set, copy the EI_ABIVERSION field. */
1513 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1514 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1515 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
07d6d2b8 1516
104d59d1
JM
1517 /* Copy object attributes. */
1518 _bfd_elf_copy_obj_attributes (ibfd, obfd);
63b9bbb7 1519
84865015
NC
1520 if (iheaders == NULL || oheaders == NULL)
1521 return TRUE;
63b9bbb7 1522
5522f910
NC
1523 bed = get_elf_backend_data (obfd);
1524
1525 /* Possibly copy other fields in the section header. */
84865015 1526 for (i = 1; i < elf_numsections (obfd); i++)
63b9bbb7 1527 {
84865015
NC
1528 unsigned int j;
1529 Elf_Internal_Shdr * oheader = oheaders[i];
63b9bbb7 1530
5522f910
NC
1531 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1532 because of a special case need for generating separate debug info
1533 files. See below for more details. */
84865015
NC
1534 if (oheader == NULL
1535 || (oheader->sh_type != SHT_NOBITS
5522f910
NC
1536 && oheader->sh_type < SHT_LOOS))
1537 continue;
1538
1539 /* Ignore empty sections, and sections whose
1540 fields have already been initialised. */
1541 if (oheader->sh_size == 0
84865015
NC
1542 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1543 continue;
63b9bbb7 1544
84865015 1545 /* Scan for the matching section in the input bfd.
5522f910
NC
1546 First we try for a direct mapping between the input and output sections. */
1547 for (j = 1; j < elf_numsections (ibfd); j++)
1548 {
1549 const Elf_Internal_Shdr * iheader = iheaders[j];
1550
1551 if (iheader == NULL)
1552 continue;
1553
1554 if (oheader->bfd_section != NULL
1555 && iheader->bfd_section != NULL
1556 && iheader->bfd_section->output_section != NULL
1557 && iheader->bfd_section->output_section == oheader->bfd_section)
1558 {
1559 /* We have found a connection from the input section to the
1560 output section. Attempt to copy the header fields. If
1561 this fails then do not try any further sections - there
1562 should only be a one-to-one mapping between input and output. */
1563 if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1564 j = elf_numsections (ibfd);
1565 break;
1566 }
1567 }
1568
1569 if (j < elf_numsections (ibfd))
1570 continue;
1571
1572 /* That failed. So try to deduce the corresponding input section.
84865015
NC
1573 Unfortunately we cannot compare names as the output string table
1574 is empty, so instead we check size, address and type. */
1575 for (j = 1; j < elf_numsections (ibfd); j++)
1576 {
5522f910 1577 const Elf_Internal_Shdr * iheader = iheaders[j];
84865015 1578
5522f910
NC
1579 if (iheader == NULL)
1580 continue;
1581
1582 /* Try matching fields in the input section's header.
1583 Since --only-keep-debug turns all non-debug sections into
84865015
NC
1584 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1585 input type. */
1586 if ((oheader->sh_type == SHT_NOBITS
1587 || iheader->sh_type == oheader->sh_type)
5522f910
NC
1588 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1589 == (oheader->sh_flags & ~ SHF_INFO_LINK)
84865015
NC
1590 && iheader->sh_addralign == oheader->sh_addralign
1591 && iheader->sh_entsize == oheader->sh_entsize
1592 && iheader->sh_size == oheader->sh_size
1593 && iheader->sh_addr == oheader->sh_addr
1594 && (iheader->sh_info != oheader->sh_info
1595 || iheader->sh_link != oheader->sh_link))
63b9bbb7 1596 {
5522f910
NC
1597 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1598 break;
63b9bbb7
NC
1599 }
1600 }
5522f910
NC
1601
1602 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1603 {
1604 /* Final attempt. Call the backend copy function
1605 with a NULL input section. */
a859124d
AM
1606 (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1607 NULL, oheader);
5522f910 1608 }
63b9bbb7
NC
1609 }
1610
b34976b6 1611 return TRUE;
2d502050
L
1612}
1613
cedc298e
L
1614static const char *
1615get_segment_type (unsigned int p_type)
1616{
1617 const char *pt;
1618 switch (p_type)
1619 {
1620 case PT_NULL: pt = "NULL"; break;
1621 case PT_LOAD: pt = "LOAD"; break;
1622 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1623 case PT_INTERP: pt = "INTERP"; break;
1624 case PT_NOTE: pt = "NOTE"; break;
1625 case PT_SHLIB: pt = "SHLIB"; break;
1626 case PT_PHDR: pt = "PHDR"; break;
1627 case PT_TLS: pt = "TLS"; break;
1628 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
2b05f1b7 1629 case PT_GNU_STACK: pt = "STACK"; break;
cedc298e
L
1630 case PT_GNU_RELRO: pt = "RELRO"; break;
1631 default: pt = NULL; break;
1632 }
1633 return pt;
1634}
1635
f0b79d91
L
1636/* Print out the program headers. */
1637
b34976b6 1638bfd_boolean
217aa764 1639_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
252b5132 1640{
a50b1753 1641 FILE *f = (FILE *) farg;
252b5132
RH
1642 Elf_Internal_Phdr *p;
1643 asection *s;
1644 bfd_byte *dynbuf = NULL;
1645
1646 p = elf_tdata (abfd)->phdr;
1647 if (p != NULL)
1648 {
1649 unsigned int i, c;
1650
1651 fprintf (f, _("\nProgram Header:\n"));
1652 c = elf_elfheader (abfd)->e_phnum;
1653 for (i = 0; i < c; i++, p++)
1654 {
cedc298e 1655 const char *pt = get_segment_type (p->p_type);
252b5132
RH
1656 char buf[20];
1657
cedc298e 1658 if (pt == NULL)
252b5132 1659 {
cedc298e
L
1660 sprintf (buf, "0x%lx", p->p_type);
1661 pt = buf;
252b5132 1662 }
dc810e39 1663 fprintf (f, "%8s off 0x", pt);
60b89a18 1664 bfd_fprintf_vma (abfd, f, p->p_offset);
252b5132 1665 fprintf (f, " vaddr 0x");
60b89a18 1666 bfd_fprintf_vma (abfd, f, p->p_vaddr);
252b5132 1667 fprintf (f, " paddr 0x");
60b89a18 1668 bfd_fprintf_vma (abfd, f, p->p_paddr);
252b5132
RH
1669 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1670 fprintf (f, " filesz 0x");
60b89a18 1671 bfd_fprintf_vma (abfd, f, p->p_filesz);
252b5132 1672 fprintf (f, " memsz 0x");
60b89a18 1673 bfd_fprintf_vma (abfd, f, p->p_memsz);
252b5132
RH
1674 fprintf (f, " flags %c%c%c",
1675 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1676 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1677 (p->p_flags & PF_X) != 0 ? 'x' : '-');
dc810e39
AM
1678 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1679 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
252b5132
RH
1680 fprintf (f, "\n");
1681 }
1682 }
1683
1684 s = bfd_get_section_by_name (abfd, ".dynamic");
1685 if (s != NULL)
1686 {
cb33740c 1687 unsigned int elfsec;
dc810e39 1688 unsigned long shlink;
252b5132
RH
1689 bfd_byte *extdyn, *extdynend;
1690 size_t extdynsize;
217aa764 1691 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
252b5132
RH
1692
1693 fprintf (f, _("\nDynamic Section:\n"));
1694
eea6121a 1695 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
252b5132
RH
1696 goto error_return;
1697
1698 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 1699 if (elfsec == SHN_BAD)
252b5132 1700 goto error_return;
dc810e39 1701 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
252b5132
RH
1702
1703 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1704 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1705
1706 extdyn = dynbuf;
06614111
NC
1707 /* PR 17512: file: 6f427532. */
1708 if (s->size < extdynsize)
1709 goto error_return;
eea6121a 1710 extdynend = extdyn + s->size;
1036838a 1711 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
07d6d2b8 1712 Fix range check. */
1036838a 1713 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
252b5132
RH
1714 {
1715 Elf_Internal_Dyn dyn;
ad9563d6 1716 const char *name = "";
252b5132 1717 char ab[20];
b34976b6 1718 bfd_boolean stringp;
ad9563d6 1719 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 1720
217aa764 1721 (*swap_dyn_in) (abfd, extdyn, &dyn);
252b5132
RH
1722
1723 if (dyn.d_tag == DT_NULL)
1724 break;
1725
b34976b6 1726 stringp = FALSE;
252b5132
RH
1727 switch (dyn.d_tag)
1728 {
1729 default:
ad9563d6
CM
1730 if (bed->elf_backend_get_target_dtag)
1731 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1732
1733 if (!strcmp (name, ""))
1734 {
cd9af601 1735 sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag);
ad9563d6
CM
1736 name = ab;
1737 }
252b5132
RH
1738 break;
1739
b34976b6 1740 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
252b5132
RH
1741 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1742 case DT_PLTGOT: name = "PLTGOT"; break;
1743 case DT_HASH: name = "HASH"; break;
1744 case DT_STRTAB: name = "STRTAB"; break;
1745 case DT_SYMTAB: name = "SYMTAB"; break;
1746 case DT_RELA: name = "RELA"; break;
1747 case DT_RELASZ: name = "RELASZ"; break;
1748 case DT_RELAENT: name = "RELAENT"; break;
1749 case DT_STRSZ: name = "STRSZ"; break;
1750 case DT_SYMENT: name = "SYMENT"; break;
1751 case DT_INIT: name = "INIT"; break;
1752 case DT_FINI: name = "FINI"; break;
b34976b6
AM
1753 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1754 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
252b5132
RH
1755 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1756 case DT_REL: name = "REL"; break;
1757 case DT_RELSZ: name = "RELSZ"; break;
1758 case DT_RELENT: name = "RELENT"; break;
1759 case DT_PLTREL: name = "PLTREL"; break;
1760 case DT_DEBUG: name = "DEBUG"; break;
1761 case DT_TEXTREL: name = "TEXTREL"; break;
1762 case DT_JMPREL: name = "JMPREL"; break;
94558834
L
1763 case DT_BIND_NOW: name = "BIND_NOW"; break;
1764 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1765 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1766 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1767 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
b34976b6 1768 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
94558834
L
1769 case DT_FLAGS: name = "FLAGS"; break;
1770 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1771 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
d48188b9 1772 case DT_CHECKSUM: name = "CHECKSUM"; break;
94558834
L
1773 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1774 case DT_MOVEENT: name = "MOVEENT"; break;
1775 case DT_MOVESZ: name = "MOVESZ"; break;
1776 case DT_FEATURE: name = "FEATURE"; break;
1777 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1778 case DT_SYMINSZ: name = "SYMINSZ"; break;
1779 case DT_SYMINENT: name = "SYMINENT"; break;
b34976b6
AM
1780 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1781 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1782 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
94558834
L
1783 case DT_PLTPAD: name = "PLTPAD"; break;
1784 case DT_MOVETAB: name = "MOVETAB"; break;
1785 case DT_SYMINFO: name = "SYMINFO"; break;
1786 case DT_RELACOUNT: name = "RELACOUNT"; break;
1787 case DT_RELCOUNT: name = "RELCOUNT"; break;
1788 case DT_FLAGS_1: name = "FLAGS_1"; break;
252b5132
RH
1789 case DT_VERSYM: name = "VERSYM"; break;
1790 case DT_VERDEF: name = "VERDEF"; break;
1791 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1792 case DT_VERNEED: name = "VERNEED"; break;
1793 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
b34976b6 1794 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
94558834 1795 case DT_USED: name = "USED"; break;
b34976b6 1796 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
fdc90cb4 1797 case DT_GNU_HASH: name = "GNU_HASH"; break;
252b5132
RH
1798 }
1799
ad9563d6 1800 fprintf (f, " %-20s ", name);
252b5132 1801 if (! stringp)
a1f3c56e
AN
1802 {
1803 fprintf (f, "0x");
1804 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1805 }
252b5132
RH
1806 else
1807 {
1808 const char *string;
dc810e39 1809 unsigned int tagv = dyn.d_un.d_val;
252b5132 1810
dc810e39 1811 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
252b5132
RH
1812 if (string == NULL)
1813 goto error_return;
1814 fprintf (f, "%s", string);
1815 }
1816 fprintf (f, "\n");
1817 }
1818
1819 free (dynbuf);
1820 dynbuf = NULL;
1821 }
1822
1823 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1824 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1825 {
fc0e6df6 1826 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
b34976b6 1827 return FALSE;
252b5132
RH
1828 }
1829
1830 if (elf_dynverdef (abfd) != 0)
1831 {
1832 Elf_Internal_Verdef *t;
1833
1834 fprintf (f, _("\nVersion definitions:\n"));
1835 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1836 {
1837 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
d0fb9a8d
JJ
1838 t->vd_flags, t->vd_hash,
1839 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1840 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
252b5132
RH
1841 {
1842 Elf_Internal_Verdaux *a;
1843
1844 fprintf (f, "\t");
1845 for (a = t->vd_auxptr->vda_nextptr;
1846 a != NULL;
1847 a = a->vda_nextptr)
d0fb9a8d
JJ
1848 fprintf (f, "%s ",
1849 a->vda_nodename ? a->vda_nodename : "<corrupt>");
252b5132
RH
1850 fprintf (f, "\n");
1851 }
1852 }
1853 }
1854
1855 if (elf_dynverref (abfd) != 0)
1856 {
1857 Elf_Internal_Verneed *t;
1858
1859 fprintf (f, _("\nVersion References:\n"));
1860 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1861 {
1862 Elf_Internal_Vernaux *a;
1863
d0fb9a8d
JJ
1864 fprintf (f, _(" required from %s:\n"),
1865 t->vn_filename ? t->vn_filename : "<corrupt>");
252b5132
RH
1866 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1867 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
d0fb9a8d
JJ
1868 a->vna_flags, a->vna_other,
1869 a->vna_nodename ? a->vna_nodename : "<corrupt>");
252b5132
RH
1870 }
1871 }
1872
b34976b6 1873 return TRUE;
252b5132
RH
1874
1875 error_return:
c9594989 1876 free (dynbuf);
b34976b6 1877 return FALSE;
252b5132
RH
1878}
1879
7e6e972f
L
1880/* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
1881 and return symbol version for symbol version itself. */
bb4d2ac2
L
1882
1883const char *
1081065c
L
1884_bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
1885 bfd_boolean base_p,
1886 bfd_boolean *hidden)
bb4d2ac2
L
1887{
1888 const char *version_string = NULL;
1889 if (elf_dynversym (abfd) != 0
1890 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1891 {
1892 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1893
1894 *hidden = (vernum & VERSYM_HIDDEN) != 0;
1895 vernum &= VERSYM_VERSION;
1896
1897 if (vernum == 0)
1898 version_string = "";
1f6f5dba
L
1899 else if (vernum == 1
1900 && (vernum > elf_tdata (abfd)->cverdefs
1901 || (elf_tdata (abfd)->verdef[0].vd_flags
1902 == VER_FLG_BASE)))
7e6e972f 1903 version_string = base_p ? "Base" : "";
bb4d2ac2 1904 else if (vernum <= elf_tdata (abfd)->cverdefs)
7e6e972f
L
1905 {
1906 const char *nodename
1907 = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
8d55d10a
AM
1908 version_string = "";
1909 if (base_p
1910 || nodename == NULL
1911 || symbol->name == NULL
1912 || strcmp (symbol->name, nodename) != 0)
1913 version_string = nodename;
7e6e972f 1914 }
bb4d2ac2
L
1915 else
1916 {
1917 Elf_Internal_Verneed *t;
1918
7a815dd5 1919 version_string = _("<corrupt>");
bb4d2ac2
L
1920 for (t = elf_tdata (abfd)->verref;
1921 t != NULL;
1922 t = t->vn_nextref)
1923 {
1924 Elf_Internal_Vernaux *a;
1925
1926 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1927 {
1928 if (a->vna_other == vernum)
1929 {
1930 version_string = a->vna_nodename;
1931 break;
1932 }
1933 }
1934 }
1935 }
1936 }
1937 return version_string;
1938}
1939
252b5132
RH
1940/* Display ELF-specific fields of a symbol. */
1941
1942void
217aa764
AM
1943bfd_elf_print_symbol (bfd *abfd,
1944 void *filep,
1945 asymbol *symbol,
1946 bfd_print_symbol_type how)
252b5132 1947{
a50b1753 1948 FILE *file = (FILE *) filep;
252b5132
RH
1949 switch (how)
1950 {
1951 case bfd_print_symbol_name:
1952 fprintf (file, "%s", symbol->name);
1953 break;
1954 case bfd_print_symbol_more:
1955 fprintf (file, "elf ");
60b89a18 1956 bfd_fprintf_vma (abfd, file, symbol->value);
cd9af601 1957 fprintf (file, " %x", symbol->flags);
252b5132
RH
1958 break;
1959 case bfd_print_symbol_all:
1960 {
4e8a9624
AM
1961 const char *section_name;
1962 const char *name = NULL;
9c5bfbb7 1963 const struct elf_backend_data *bed;
7a13edea 1964 unsigned char st_other;
dbb410c3 1965 bfd_vma val;
bb4d2ac2
L
1966 const char *version_string;
1967 bfd_boolean hidden;
c044fabd 1968
252b5132 1969 section_name = symbol->section ? symbol->section->name : "(*none*)";
587ff49e
RH
1970
1971 bed = get_elf_backend_data (abfd);
1972 if (bed->elf_backend_print_symbol_all)
c044fabd 1973 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
587ff49e
RH
1974
1975 if (name == NULL)
1976 {
7ee38065 1977 name = symbol->name;
217aa764 1978 bfd_print_symbol_vandf (abfd, file, symbol);
587ff49e
RH
1979 }
1980
252b5132
RH
1981 fprintf (file, " %s\t", section_name);
1982 /* Print the "other" value for a symbol. For common symbols,
1983 we've already printed the size; now print the alignment.
1984 For other symbols, we have no specified alignment, and
1985 we've printed the address; now print the size. */
dcf6c779 1986 if (symbol->section && bfd_is_com_section (symbol->section))
dbb410c3
AM
1987 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1988 else
1989 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1990 bfd_fprintf_vma (abfd, file, val);
252b5132
RH
1991
1992 /* If we have version information, print it. */
60bb06bc
L
1993 version_string = _bfd_elf_get_symbol_version_string (abfd,
1994 symbol,
1081065c 1995 TRUE,
60bb06bc 1996 &hidden);
bb4d2ac2 1997 if (version_string)
252b5132 1998 {
bb4d2ac2 1999 if (!hidden)
252b5132
RH
2000 fprintf (file, " %-11s", version_string);
2001 else
2002 {
2003 int i;
2004
2005 fprintf (file, " (%s)", version_string);
2006 for (i = 10 - strlen (version_string); i > 0; --i)
2007 putc (' ', file);
2008 }
2009 }
2010
2011 /* If the st_other field is not zero, print it. */
7a13edea 2012 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
c044fabd 2013
7a13edea
NC
2014 switch (st_other)
2015 {
2016 case 0: break;
2017 case STV_INTERNAL: fprintf (file, " .internal"); break;
2018 case STV_HIDDEN: fprintf (file, " .hidden"); break;
2019 case STV_PROTECTED: fprintf (file, " .protected"); break;
2020 default:
2021 /* Some other non-defined flags are also present, so print
2022 everything hex. */
2023 fprintf (file, " 0x%02x", (unsigned int) st_other);
2024 }
252b5132 2025
587ff49e 2026 fprintf (file, " %s", name);
252b5132
RH
2027 }
2028 break;
2029 }
2030}
252b5132
RH
2031\f
2032/* ELF .o/exec file reading */
2033
c044fabd 2034/* Create a new bfd section from an ELF section header. */
252b5132 2035
b34976b6 2036bfd_boolean
217aa764 2037bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
252b5132 2038{
4fbb74a6
AM
2039 Elf_Internal_Shdr *hdr;
2040 Elf_Internal_Ehdr *ehdr;
2041 const struct elf_backend_data *bed;
90937f86 2042 const char *name;
bf67003b
NC
2043 bfd_boolean ret = TRUE;
2044 static bfd_boolean * sections_being_created = NULL;
5a4b0ccc 2045 static bfd * sections_being_created_abfd = NULL;
bf67003b 2046 static unsigned int nesting = 0;
252b5132 2047
4fbb74a6
AM
2048 if (shindex >= elf_numsections (abfd))
2049 return FALSE;
2050
bf67003b
NC
2051 if (++ nesting > 3)
2052 {
2053 /* PR17512: A corrupt ELF binary might contain a recursive group of
67ce483b 2054 sections, with each the string indices pointing to the next in the
bf67003b
NC
2055 loop. Detect this here, by refusing to load a section that we are
2056 already in the process of loading. We only trigger this test if
2057 we have nested at least three sections deep as normal ELF binaries
5a4b0ccc
NC
2058 can expect to recurse at least once.
2059
2060 FIXME: It would be better if this array was attached to the bfd,
2061 rather than being held in a static pointer. */
2062
2063 if (sections_being_created_abfd != abfd)
706d7ce9
AM
2064 {
2065 free (sections_being_created);
2066 sections_being_created = NULL;
2067 }
bf67003b
NC
2068 if (sections_being_created == NULL)
2069 {
446f7ed5 2070 size_t amt = elf_numsections (abfd) * sizeof (bfd_boolean);
ed02cdb5
NC
2071
2072 /* PR 26005: Do not use bfd_zalloc here as the memory might
2073 be released before the bfd has been fully scanned. */
706d7ce9 2074 sections_being_created = (bfd_boolean *) bfd_zmalloc (amt);
96d3b80f
AM
2075 if (sections_being_created == NULL)
2076 return FALSE;
5a4b0ccc 2077 sections_being_created_abfd = abfd;
bf67003b
NC
2078 }
2079 if (sections_being_created [shindex])
2080 {
4eca0228 2081 _bfd_error_handler
871b3ab2 2082 (_("%pB: warning: loop in section dependencies detected"), abfd);
bf67003b
NC
2083 return FALSE;
2084 }
2085 sections_being_created [shindex] = TRUE;
2086 }
2087
4fbb74a6
AM
2088 hdr = elf_elfsections (abfd)[shindex];
2089 ehdr = elf_elfheader (abfd);
2090 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1b3a8575 2091 hdr->sh_name);
933d961a 2092 if (name == NULL)
bf67003b 2093 goto fail;
252b5132 2094
4fbb74a6 2095 bed = get_elf_backend_data (abfd);
252b5132
RH
2096 switch (hdr->sh_type)
2097 {
2098 case SHT_NULL:
2099 /* Inactive section. Throw it away. */
bf67003b 2100 goto success;
252b5132 2101
bf67003b
NC
2102 case SHT_PROGBITS: /* Normal section with contents. */
2103 case SHT_NOBITS: /* .bss section. */
2104 case SHT_HASH: /* .hash section. */
2105 case SHT_NOTE: /* .note section. */
25e27870
L
2106 case SHT_INIT_ARRAY: /* .init_array section. */
2107 case SHT_FINI_ARRAY: /* .fini_array section. */
2108 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
7f1204bb 2109 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
fdc90cb4 2110 case SHT_GNU_HASH: /* .gnu.hash section. */
bf67003b
NC
2111 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2112 goto success;
252b5132 2113
797fc050 2114 case SHT_DYNAMIC: /* Dynamic linking information. */
6dc132d9 2115 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b
NC
2116 goto fail;
2117
cfcac11d
NC
2118 if (hdr->sh_link > elf_numsections (abfd))
2119 {
caa83f8b 2120 /* PR 10478: Accept Solaris binaries with a sh_link
cfcac11d
NC
2121 field set to SHN_BEFORE or SHN_AFTER. */
2122 switch (bfd_get_arch (abfd))
2123 {
caa83f8b 2124 case bfd_arch_i386:
cfcac11d
NC
2125 case bfd_arch_sparc:
2126 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
2127 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
2128 break;
2129 /* Otherwise fall through. */
2130 default:
bf67003b 2131 goto fail;
cfcac11d
NC
2132 }
2133 }
2134 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
bf67003b 2135 goto fail;
cfcac11d 2136 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
797fc050
AM
2137 {
2138 Elf_Internal_Shdr *dynsymhdr;
2139
2140 /* The shared libraries distributed with hpux11 have a bogus
2141 sh_link field for the ".dynamic" section. Find the
2142 string table for the ".dynsym" section instead. */
2143 if (elf_dynsymtab (abfd) != 0)
2144 {
2145 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2146 hdr->sh_link = dynsymhdr->sh_link;
2147 }
2148 else
2149 {
2150 unsigned int i, num_sec;
2151
2152 num_sec = elf_numsections (abfd);
2153 for (i = 1; i < num_sec; i++)
2154 {
2155 dynsymhdr = elf_elfsections (abfd)[i];
2156 if (dynsymhdr->sh_type == SHT_DYNSYM)
2157 {
2158 hdr->sh_link = dynsymhdr->sh_link;
2159 break;
2160 }
2161 }
2162 }
2163 }
bf67003b 2164 goto success;
797fc050 2165
bf67003b 2166 case SHT_SYMTAB: /* A symbol table. */
252b5132 2167 if (elf_onesymtab (abfd) == shindex)
bf67003b 2168 goto success;
252b5132 2169
a50b2160 2170 if (hdr->sh_entsize != bed->s->sizeof_sym)
bf67003b
NC
2171 goto fail;
2172
3337c1e5 2173 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
eee3b786
AM
2174 {
2175 if (hdr->sh_size != 0)
bf67003b 2176 goto fail;
eee3b786
AM
2177 /* Some assemblers erroneously set sh_info to one with a
2178 zero sh_size. ld sees this as a global symbol count
2179 of (unsigned) -1. Fix it here. */
2180 hdr->sh_info = 0;
bf67003b 2181 goto success;
eee3b786 2182 }
bf67003b 2183
16ad13ec
NC
2184 /* PR 18854: A binary might contain more than one symbol table.
2185 Unusual, but possible. Warn, but continue. */
2186 if (elf_onesymtab (abfd) != 0)
2187 {
4eca0228 2188 _bfd_error_handler
695344c0 2189 /* xgettext:c-format */
871b3ab2 2190 (_("%pB: warning: multiple symbol tables detected"
63a5468a 2191 " - ignoring the table in section %u"),
16ad13ec
NC
2192 abfd, shindex);
2193 goto success;
2194 }
252b5132 2195 elf_onesymtab (abfd) = shindex;
6a40cf0c
NC
2196 elf_symtab_hdr (abfd) = *hdr;
2197 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
252b5132
RH
2198 abfd->flags |= HAS_SYMS;
2199
2200 /* Sometimes a shared object will map in the symbol table. If
08a40648
AM
2201 SHF_ALLOC is set, and this is a shared object, then we also
2202 treat this section as a BFD section. We can not base the
2203 decision purely on SHF_ALLOC, because that flag is sometimes
2204 set in a relocatable object file, which would confuse the
2205 linker. */
252b5132
RH
2206 if ((hdr->sh_flags & SHF_ALLOC) != 0
2207 && (abfd->flags & DYNAMIC) != 0
6dc132d9
L
2208 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2209 shindex))
bf67003b 2210 goto fail;
252b5132 2211
1b3a8575
AM
2212 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2213 can't read symbols without that section loaded as well. It
2214 is most likely specified by the next section header. */
6a40cf0c
NC
2215 {
2216 elf_section_list * entry;
2217 unsigned int i, num_sec;
1b3a8575 2218
6a40cf0c
NC
2219 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2220 if (entry->hdr.sh_link == shindex)
2221 goto success;
2222
2223 num_sec = elf_numsections (abfd);
2224 for (i = shindex + 1; i < num_sec; i++)
2225 {
2226 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2227
2228 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2229 && hdr2->sh_link == shindex)
2230 break;
2231 }
2232
2233 if (i == num_sec)
2234 for (i = 1; i < shindex; i++)
1b3a8575
AM
2235 {
2236 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
6a40cf0c 2237
1b3a8575
AM
2238 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2239 && hdr2->sh_link == shindex)
2240 break;
2241 }
6a40cf0c
NC
2242
2243 if (i != shindex)
2244 ret = bfd_section_from_shdr (abfd, i);
2245 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2246 goto success;
2247 }
252b5132 2248
bf67003b 2249 case SHT_DYNSYM: /* A dynamic symbol table. */
252b5132 2250 if (elf_dynsymtab (abfd) == shindex)
bf67003b 2251 goto success;
252b5132 2252
a50b2160 2253 if (hdr->sh_entsize != bed->s->sizeof_sym)
bf67003b
NC
2254 goto fail;
2255
eee3b786
AM
2256 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2257 {
2258 if (hdr->sh_size != 0)
bf67003b
NC
2259 goto fail;
2260
eee3b786
AM
2261 /* Some linkers erroneously set sh_info to one with a
2262 zero sh_size. ld sees this as a global symbol count
2263 of (unsigned) -1. Fix it here. */
2264 hdr->sh_info = 0;
bf67003b 2265 goto success;
eee3b786 2266 }
bf67003b 2267
16ad13ec
NC
2268 /* PR 18854: A binary might contain more than one dynamic symbol table.
2269 Unusual, but possible. Warn, but continue. */
2270 if (elf_dynsymtab (abfd) != 0)
2271 {
4eca0228 2272 _bfd_error_handler
695344c0 2273 /* xgettext:c-format */
871b3ab2 2274 (_("%pB: warning: multiple dynamic symbol tables detected"
63a5468a 2275 " - ignoring the table in section %u"),
16ad13ec
NC
2276 abfd, shindex);
2277 goto success;
2278 }
252b5132
RH
2279 elf_dynsymtab (abfd) = shindex;
2280 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2281 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2282 abfd->flags |= HAS_SYMS;
2283
2284 /* Besides being a symbol table, we also treat this as a regular
2285 section, so that objcopy can handle it. */
bf67003b
NC
2286 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2287 goto success;
252b5132 2288
bf67003b 2289 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
6a40cf0c
NC
2290 {
2291 elf_section_list * entry;
9ad5cbcf 2292
6a40cf0c
NC
2293 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2294 if (entry->ndx == shindex)
2295 goto success;
07d6d2b8 2296
7a6e0d89 2297 entry = bfd_alloc (abfd, sizeof (*entry));
6a40cf0c
NC
2298 if (entry == NULL)
2299 goto fail;
2300 entry->ndx = shindex;
2301 entry->hdr = * hdr;
2302 entry->next = elf_symtab_shndx_list (abfd);
2303 elf_symtab_shndx_list (abfd) = entry;
2304 elf_elfsections (abfd)[shindex] = & entry->hdr;
2305 goto success;
2306 }
9ad5cbcf 2307
bf67003b 2308 case SHT_STRTAB: /* A string table. */
252b5132 2309 if (hdr->bfd_section != NULL)
bf67003b
NC
2310 goto success;
2311
252b5132
RH
2312 if (ehdr->e_shstrndx == shindex)
2313 {
2314 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2315 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
bf67003b 2316 goto success;
252b5132 2317 }
bf67003b 2318
1b3a8575
AM
2319 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2320 {
2321 symtab_strtab:
2322 elf_tdata (abfd)->strtab_hdr = *hdr;
2323 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
bf67003b 2324 goto success;
1b3a8575 2325 }
bf67003b 2326
1b3a8575
AM
2327 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2328 {
2329 dynsymtab_strtab:
2330 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2331 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2332 elf_elfsections (abfd)[shindex] = hdr;
2333 /* We also treat this as a regular section, so that objcopy
2334 can handle it. */
bf67003b
NC
2335 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2336 shindex);
2337 goto success;
1b3a8575 2338 }
252b5132 2339
1b3a8575
AM
2340 /* If the string table isn't one of the above, then treat it as a
2341 regular section. We need to scan all the headers to be sure,
2342 just in case this strtab section appeared before the above. */
2343 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2344 {
2345 unsigned int i, num_sec;
252b5132 2346
1b3a8575
AM
2347 num_sec = elf_numsections (abfd);
2348 for (i = 1; i < num_sec; i++)
2349 {
2350 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2351 if (hdr2->sh_link == shindex)
2352 {
933d961a
JJ
2353 /* Prevent endless recursion on broken objects. */
2354 if (i == shindex)
bf67003b 2355 goto fail;
1b3a8575 2356 if (! bfd_section_from_shdr (abfd, i))
bf67003b 2357 goto fail;
1b3a8575
AM
2358 if (elf_onesymtab (abfd) == i)
2359 goto symtab_strtab;
2360 if (elf_dynsymtab (abfd) == i)
2361 goto dynsymtab_strtab;
2362 }
2363 }
2364 }
bf67003b
NC
2365 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2366 goto success;
252b5132
RH
2367
2368 case SHT_REL:
2369 case SHT_RELA:
2370 /* *These* do a lot of work -- but build no sections! */
2371 {
2372 asection *target_sect;
d4730f92 2373 Elf_Internal_Shdr *hdr2, **p_hdr;
9ad5cbcf 2374 unsigned int num_sec = elf_numsections (abfd);
d4730f92 2375 struct bfd_elf_section_data *esdt;
252b5132 2376
aa2ca951
JJ
2377 if (hdr->sh_entsize
2378 != (bfd_size_type) (hdr->sh_type == SHT_REL
a50b2160 2379 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
bf67003b 2380 goto fail;
a50b2160 2381
03ae5f59 2382 /* Check for a bogus link to avoid crashing. */
4fbb74a6 2383 if (hdr->sh_link >= num_sec)
03ae5f59 2384 {
4eca0228 2385 _bfd_error_handler
695344c0 2386 /* xgettext:c-format */
871b3ab2 2387 (_("%pB: invalid link %u for reloc section %s (index %u)"),
4eca0228 2388 abfd, hdr->sh_link, name, shindex);
bf67003b
NC
2389 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2390 shindex);
2391 goto success;
03ae5f59
ILT
2392 }
2393
252b5132
RH
2394 /* For some incomprehensible reason Oracle distributes
2395 libraries for Solaris in which some of the objects have
2396 bogus sh_link fields. It would be nice if we could just
2397 reject them, but, unfortunately, some people need to use
2398 them. We scan through the section headers; if we find only
2399 one suitable symbol table, we clobber the sh_link to point
83b89087
L
2400 to it. I hope this doesn't break anything.
2401
2402 Don't do it on executable nor shared library. */
2403 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
2404 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
252b5132
RH
2405 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
2406 {
9ad5cbcf 2407 unsigned int scan;
252b5132
RH
2408 int found;
2409
2410 found = 0;
9ad5cbcf 2411 for (scan = 1; scan < num_sec; scan++)
252b5132
RH
2412 {
2413 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
2414 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
2415 {
2416 if (found != 0)
2417 {
2418 found = 0;
2419 break;
2420 }
2421 found = scan;
2422 }
2423 }
2424 if (found != 0)
2425 hdr->sh_link = found;
2426 }
2427
2428 /* Get the symbol table. */
1b3a8575
AM
2429 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2430 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
252b5132 2431 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
bf67003b 2432 goto fail;
252b5132 2433
a4bcd733
AM
2434 /* If this is an alloc section in an executable or shared
2435 library, or the reloc section does not use the main symbol
2436 table we don't treat it as a reloc section. BFD can't
2437 adequately represent such a section, so at least for now,
2438 we don't try. We just present it as a normal section. We
2439 also can't use it as a reloc section if it points to the
2440 null section, an invalid section, another reloc section, or
2441 its sh_link points to the null section. */
2442 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2443 && (hdr->sh_flags & SHF_ALLOC) != 0)
83b89087 2444 || hdr->sh_link == SHN_UNDEF
a4bcd733 2445 || hdr->sh_link != elf_onesymtab (abfd)
185ef66d 2446 || hdr->sh_info == SHN_UNDEF
185ef66d
AM
2447 || hdr->sh_info >= num_sec
2448 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2449 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
bf67003b
NC
2450 {
2451 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2452 shindex);
2453 goto success;
2454 }
252b5132
RH
2455
2456 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
bf67003b
NC
2457 goto fail;
2458
252b5132
RH
2459 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2460 if (target_sect == NULL)
bf67003b 2461 goto fail;
252b5132 2462
d4730f92
BS
2463 esdt = elf_section_data (target_sect);
2464 if (hdr->sh_type == SHT_RELA)
2465 p_hdr = &esdt->rela.hdr;
252b5132 2466 else
d4730f92
BS
2467 p_hdr = &esdt->rel.hdr;
2468
a7ba3896
NC
2469 /* PR 17512: file: 0b4f81b7.
2470 Also see PR 24456, for a file which deliberately has two reloc
2471 sections. */
06614111 2472 if (*p_hdr != NULL)
a7ba3896 2473 {
a859124d 2474 if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
a8e14f4c
NC
2475 {
2476 _bfd_error_handler
2477 /* xgettext:c-format */
a859124d
AM
2478 (_("%pB: warning: secondary relocation section '%s' "
2479 "for section %pA found - ignoring"),
a8e14f4c
NC
2480 abfd, name, target_sect);
2481 }
a7ba3896
NC
2482 goto success;
2483 }
a8e14f4c 2484
ef53be89 2485 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
d4730f92 2486 if (hdr2 == NULL)
bf67003b 2487 goto fail;
252b5132 2488 *hdr2 = *hdr;
d4730f92 2489 *p_hdr = hdr2;
252b5132 2490 elf_elfsections (abfd)[shindex] = hdr2;
056bafd4
MR
2491 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2492 * bed->s->int_rels_per_ext_rel);
252b5132
RH
2493 target_sect->flags |= SEC_RELOC;
2494 target_sect->relocation = NULL;
2495 target_sect->rel_filepos = hdr->sh_offset;
bf572ba0
MM
2496 /* In the section to which the relocations apply, mark whether
2497 its relocations are of the REL or RELA variety. */
72730e0c 2498 if (hdr->sh_size != 0)
d4730f92
BS
2499 {
2500 if (hdr->sh_type == SHT_RELA)
2501 target_sect->use_rela_p = 1;
2502 }
252b5132 2503 abfd->flags |= HAS_RELOC;
bf67003b 2504 goto success;
252b5132 2505 }
252b5132
RH
2506
2507 case SHT_GNU_verdef:
2508 elf_dynverdef (abfd) = shindex;
2509 elf_tdata (abfd)->dynverdef_hdr = *hdr;
bf67003b
NC
2510 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2511 goto success;
252b5132
RH
2512
2513 case SHT_GNU_versym:
a50b2160 2514 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
bf67003b
NC
2515 goto fail;
2516
252b5132
RH
2517 elf_dynversym (abfd) = shindex;
2518 elf_tdata (abfd)->dynversym_hdr = *hdr;
bf67003b
NC
2519 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2520 goto success;
252b5132
RH
2521
2522 case SHT_GNU_verneed:
2523 elf_dynverref (abfd) = shindex;
2524 elf_tdata (abfd)->dynverref_hdr = *hdr;
bf67003b
NC
2525 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2526 goto success;
252b5132
RH
2527
2528 case SHT_SHLIB:
bf67003b 2529 goto success;
252b5132 2530
dbb410c3 2531 case SHT_GROUP:
44534af3 2532 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
bf67003b
NC
2533 goto fail;
2534
6dc132d9 2535 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b
NC
2536 goto fail;
2537
bf67003b 2538 goto success;
dbb410c3 2539
252b5132 2540 default:
104d59d1
JM
2541 /* Possibly an attributes section. */
2542 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2543 || hdr->sh_type == bed->obj_attrs_section_type)
2544 {
2545 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b 2546 goto fail;
104d59d1 2547 _bfd_elf_parse_attributes (abfd, hdr);
bf67003b 2548 goto success;
104d59d1
JM
2549 }
2550
252b5132 2551 /* Check for any processor-specific section types. */
3eb70a79 2552 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
bf67003b 2553 goto success;
3eb70a79
L
2554
2555 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2556 {
2557 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2558 /* FIXME: How to properly handle allocated section reserved
2559 for applications? */
4eca0228 2560 _bfd_error_handler
695344c0 2561 /* xgettext:c-format */
871b3ab2 2562 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2563 abfd, hdr->sh_type, name);
3eb70a79 2564 else
bf67003b
NC
2565 {
2566 /* Allow sections reserved for applications. */
2567 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2568 shindex);
2569 goto success;
2570 }
3eb70a79
L
2571 }
2572 else if (hdr->sh_type >= SHT_LOPROC
2573 && hdr->sh_type <= SHT_HIPROC)
2574 /* FIXME: We should handle this section. */
4eca0228 2575 _bfd_error_handler
695344c0 2576 /* xgettext:c-format */
871b3ab2 2577 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2578 abfd, hdr->sh_type, name);
3eb70a79 2579 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
ff15b240
NC
2580 {
2581 /* Unrecognised OS-specific sections. */
2582 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2583 /* SHF_OS_NONCONFORMING indicates that special knowledge is
08a40648 2584 required to correctly process the section and the file should
ff15b240 2585 be rejected with an error message. */
4eca0228 2586 _bfd_error_handler
695344c0 2587 /* xgettext:c-format */
871b3ab2 2588 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2589 abfd, hdr->sh_type, name);
ff15b240 2590 else
bf67003b
NC
2591 {
2592 /* Otherwise it should be processed. */
2593 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2594 goto success;
2595 }
ff15b240 2596 }
3eb70a79
L
2597 else
2598 /* FIXME: We should handle this section. */
4eca0228 2599 _bfd_error_handler
695344c0 2600 /* xgettext:c-format */
871b3ab2 2601 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2602 abfd, hdr->sh_type, name);
3eb70a79 2603
bf67003b 2604 goto fail;
252b5132
RH
2605 }
2606
bf67003b
NC
2607 fail:
2608 ret = FALSE;
2609 success:
e5b470e2 2610 if (sections_being_created && sections_being_created_abfd == abfd)
bf67003b
NC
2611 sections_being_created [shindex] = FALSE;
2612 if (-- nesting == 0)
5a4b0ccc 2613 {
ed02cdb5 2614 free (sections_being_created);
5a4b0ccc 2615 sections_being_created = NULL;
ed02cdb5 2616 sections_being_created_abfd = NULL;
5a4b0ccc 2617 }
bf67003b 2618 return ret;
252b5132
RH
2619}
2620
87d72d41 2621/* Return the local symbol specified by ABFD, R_SYMNDX. */
ec338859 2622
87d72d41
AM
2623Elf_Internal_Sym *
2624bfd_sym_from_r_symndx (struct sym_cache *cache,
2625 bfd *abfd,
2626 unsigned long r_symndx)
ec338859 2627{
ec338859
AM
2628 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2629
a5d1b3b5
AM
2630 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2631 {
2632 Elf_Internal_Shdr *symtab_hdr;
2633 unsigned char esym[sizeof (Elf64_External_Sym)];
2634 Elf_External_Sym_Shndx eshndx;
ec338859 2635
a5d1b3b5
AM
2636 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2637 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
87d72d41 2638 &cache->sym[ent], esym, &eshndx) == NULL)
a5d1b3b5 2639 return NULL;
9ad5cbcf 2640
a5d1b3b5
AM
2641 if (cache->abfd != abfd)
2642 {
2643 memset (cache->indx, -1, sizeof (cache->indx));
2644 cache->abfd = abfd;
2645 }
2646 cache->indx[ent] = r_symndx;
ec338859 2647 }
a5d1b3b5 2648
87d72d41 2649 return &cache->sym[ent];
ec338859
AM
2650}
2651
252b5132
RH
2652/* Given an ELF section number, retrieve the corresponding BFD
2653 section. */
2654
2655asection *
91d6fa6a 2656bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
252b5132 2657{
91d6fa6a 2658 if (sec_index >= elf_numsections (abfd))
252b5132 2659 return NULL;
91d6fa6a 2660 return elf_elfsections (abfd)[sec_index]->bfd_section;
252b5132
RH
2661}
2662
b35d266b 2663static const struct bfd_elf_special_section special_sections_b[] =
2f89ff8d 2664{
0112cd26 2665 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 2666 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2667};
2668
b35d266b 2669static const struct bfd_elf_special_section special_sections_c[] =
7f4d3958 2670{
0112cd26 2671 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
1ff6de03 2672 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2673 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2674};
2675
b35d266b 2676static const struct bfd_elf_special_section special_sections_d[] =
7f4d3958 2677{
07d6d2b8
AM
2678 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2679 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
a9a72a65
DE
2680 /* There are more DWARF sections than these, but they needn't be added here
2681 unless you have to cope with broken compilers that don't emit section
2682 attributes or you want to help the user writing assembler. */
07d6d2b8
AM
2683 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2684 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2685 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2686 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
0112cd26 2687 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
07d6d2b8
AM
2688 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2689 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2690 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2691 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2692};
2693
b35d266b 2694static const struct bfd_elf_special_section special_sections_f[] =
7f4d3958 2695{
07d6d2b8 2696 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
6f9dbcd4 2697 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8 2698 { NULL, 0 , 0, 0, 0 }
7f4d3958
L
2699};
2700
b35d266b 2701static const struct bfd_elf_special_section special_sections_g[] =
7f4d3958 2702{
0112cd26 2703 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2704 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2705 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2706 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
0112cd26
NC
2707 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2708 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
07d6d2b8
AM
2709 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2710 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2711 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2712 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2713};
2714
b35d266b 2715static const struct bfd_elf_special_section special_sections_h[] =
7f4d3958 2716{
07d6d2b8
AM
2717 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2718 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2719};
2720
b35d266b 2721static const struct bfd_elf_special_section special_sections_i[] =
7f4d3958 2722{
07d6d2b8 2723 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
6f9dbcd4 2724 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2725 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2726 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2727};
2728
b35d266b 2729static const struct bfd_elf_special_section special_sections_l[] =
7f4d3958 2730{
0112cd26 2731 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2732 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2733};
2734
b35d266b 2735static const struct bfd_elf_special_section special_sections_n[] =
7f4d3958 2736{
0112cd26 2737 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
07d6d2b8
AM
2738 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2739 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2740};
2741
b35d266b 2742static const struct bfd_elf_special_section special_sections_p[] =
7f4d3958 2743{
6f9dbcd4 2744 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2745 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2746 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2747};
2748
b35d266b 2749static const struct bfd_elf_special_section special_sections_r[] =
7f4d3958 2750{
0112cd26
NC
2751 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2752 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
07d6d2b8
AM
2753 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2754 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2755 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2756};
2757
b35d266b 2758static const struct bfd_elf_special_section special_sections_s[] =
7f4d3958 2759{
0112cd26
NC
2760 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2761 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2762 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
60ff4dc4
HPN
2763 /* See struct bfd_elf_special_section declaration for the semantics of
2764 this special case where .prefix_length != strlen (.prefix). */
2765 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
07d6d2b8 2766 { NULL, 0, 0, 0, 0 }
2f89ff8d
L
2767};
2768
b35d266b 2769static const struct bfd_elf_special_section special_sections_t[] =
7f4d3958 2770{
07d6d2b8
AM
2771 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2772 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
0112cd26 2773 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
07d6d2b8 2774 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2775};
2776
1b315056
CS
2777static const struct bfd_elf_special_section special_sections_z[] =
2778{
07d6d2b8
AM
2779 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2780 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
1b315056
CS
2781 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2782 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2783 { NULL, 0, 0, 0, 0 }
1b315056
CS
2784};
2785
e4c93b56 2786static const struct bfd_elf_special_section * const special_sections[] =
7f4d3958 2787{
7f4d3958 2788 special_sections_b, /* 'b' */
98ece1b3 2789 special_sections_c, /* 'c' */
7f4d3958
L
2790 special_sections_d, /* 'd' */
2791 NULL, /* 'e' */
2792 special_sections_f, /* 'f' */
2793 special_sections_g, /* 'g' */
2794 special_sections_h, /* 'h' */
2795 special_sections_i, /* 'i' */
2796 NULL, /* 'j' */
2797 NULL, /* 'k' */
2798 special_sections_l, /* 'l' */
2799 NULL, /* 'm' */
2800 special_sections_n, /* 'n' */
2801 NULL, /* 'o' */
2802 special_sections_p, /* 'p' */
2803 NULL, /* 'q' */
2804 special_sections_r, /* 'r' */
2805 special_sections_s, /* 's' */
2806 special_sections_t, /* 't' */
1b315056
CS
2807 NULL, /* 'u' */
2808 NULL, /* 'v' */
2809 NULL, /* 'w' */
2810 NULL, /* 'x' */
2811 NULL, /* 'y' */
2812 special_sections_z /* 'z' */
7f4d3958
L
2813};
2814
551b43fd
AM
2815const struct bfd_elf_special_section *
2816_bfd_elf_get_special_section (const char *name,
2817 const struct bfd_elf_special_section *spec,
2818 unsigned int rela)
2f89ff8d
L
2819{
2820 int i;
7f4d3958 2821 int len;
7f4d3958 2822
551b43fd 2823 len = strlen (name);
7f4d3958 2824
551b43fd 2825 for (i = 0; spec[i].prefix != NULL; i++)
7dcb9820
AM
2826 {
2827 int suffix_len;
551b43fd 2828 int prefix_len = spec[i].prefix_length;
7dcb9820
AM
2829
2830 if (len < prefix_len)
2831 continue;
551b43fd 2832 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
7dcb9820
AM
2833 continue;
2834
551b43fd 2835 suffix_len = spec[i].suffix_length;
7dcb9820
AM
2836 if (suffix_len <= 0)
2837 {
2838 if (name[prefix_len] != 0)
2839 {
2840 if (suffix_len == 0)
2841 continue;
2842 if (name[prefix_len] != '.'
2843 && (suffix_len == -2
551b43fd 2844 || (rela && spec[i].type == SHT_REL)))
7dcb9820
AM
2845 continue;
2846 }
2847 }
2848 else
2849 {
2850 if (len < prefix_len + suffix_len)
2851 continue;
2852 if (memcmp (name + len - suffix_len,
551b43fd 2853 spec[i].prefix + prefix_len,
7dcb9820
AM
2854 suffix_len) != 0)
2855 continue;
2856 }
551b43fd 2857 return &spec[i];
7dcb9820 2858 }
2f89ff8d
L
2859
2860 return NULL;
2861}
2862
7dcb9820 2863const struct bfd_elf_special_section *
29ef7005 2864_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2f89ff8d 2865{
551b43fd
AM
2866 int i;
2867 const struct bfd_elf_special_section *spec;
29ef7005 2868 const struct elf_backend_data *bed;
2f89ff8d
L
2869
2870 /* See if this is one of the special sections. */
551b43fd
AM
2871 if (sec->name == NULL)
2872 return NULL;
2f89ff8d 2873
29ef7005
L
2874 bed = get_elf_backend_data (abfd);
2875 spec = bed->special_sections;
2876 if (spec)
2877 {
2878 spec = _bfd_elf_get_special_section (sec->name,
2879 bed->special_sections,
2880 sec->use_rela_p);
2881 if (spec != NULL)
2882 return spec;
2883 }
2884
551b43fd
AM
2885 if (sec->name[0] != '.')
2886 return NULL;
2f89ff8d 2887
551b43fd 2888 i = sec->name[1] - 'b';
1b315056 2889 if (i < 0 || i > 'z' - 'b')
551b43fd
AM
2890 return NULL;
2891
2892 spec = special_sections[i];
2f89ff8d 2893
551b43fd
AM
2894 if (spec == NULL)
2895 return NULL;
2896
2897 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2f89ff8d
L
2898}
2899
b34976b6 2900bfd_boolean
217aa764 2901_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
252b5132
RH
2902{
2903 struct bfd_elf_section_data *sdata;
551b43fd 2904 const struct elf_backend_data *bed;
7dcb9820 2905 const struct bfd_elf_special_section *ssect;
252b5132 2906
f0abc2a1
AM
2907 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2908 if (sdata == NULL)
2909 {
a50b1753 2910 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
07d6d2b8 2911 sizeof (*sdata));
f0abc2a1
AM
2912 if (sdata == NULL)
2913 return FALSE;
217aa764 2914 sec->used_by_bfd = sdata;
f0abc2a1 2915 }
bf572ba0 2916
551b43fd
AM
2917 /* Indicate whether or not this section should use RELA relocations. */
2918 bed = get_elf_backend_data (abfd);
2919 sec->use_rela_p = bed->default_use_rela_p;
2920
8c803a2d
AM
2921 /* Set up ELF section type and flags for newly created sections, if
2922 there is an ABI mandated section. */
2923 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2924 if (ssect != NULL)
2f89ff8d 2925 {
8c803a2d
AM
2926 elf_section_type (sec) = ssect->type;
2927 elf_section_flags (sec) = ssect->attr;
2f89ff8d
L
2928 }
2929
f592407e 2930 return _bfd_generic_new_section_hook (abfd, sec);
252b5132
RH
2931}
2932
2933/* Create a new bfd section from an ELF program header.
2934
2935 Since program segments have no names, we generate a synthetic name
2936 of the form segment<NUM>, where NUM is generally the index in the
2937 program header table. For segments that are split (see below) we
2938 generate the names segment<NUM>a and segment<NUM>b.
2939
2940 Note that some program segments may have a file size that is different than
2941 (less than) the memory size. All this means is that at execution the
2942 system must allocate the amount of memory specified by the memory size,
2943 but only initialize it with the first "file size" bytes read from the
2944 file. This would occur for example, with program segments consisting
2945 of combined data+bss.
2946
2947 To handle the above situation, this routine generates TWO bfd sections
2948 for the single program segment. The first has the length specified by
2949 the file size of the segment, and the second has the length specified
2950 by the difference between the two sizes. In effect, the segment is split
d5191d0c 2951 into its initialized and uninitialized parts.
252b5132
RH
2952
2953 */
2954
b34976b6 2955bfd_boolean
217aa764
AM
2956_bfd_elf_make_section_from_phdr (bfd *abfd,
2957 Elf_Internal_Phdr *hdr,
91d6fa6a 2958 int hdr_index,
a50b1753 2959 const char *type_name)
252b5132
RH
2960{
2961 asection *newsect;
2962 char *name;
2963 char namebuf[64];
d4c88bbb 2964 size_t len;
252b5132 2965 int split;
502794d4 2966 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132
RH
2967
2968 split = ((hdr->p_memsz > 0)
2969 && (hdr->p_filesz > 0)
2970 && (hdr->p_memsz > hdr->p_filesz));
d5191d0c
AM
2971
2972 if (hdr->p_filesz > 0)
252b5132 2973 {
91d6fa6a 2974 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
d5191d0c 2975 len = strlen (namebuf) + 1;
a50b1753 2976 name = (char *) bfd_alloc (abfd, len);
d5191d0c
AM
2977 if (!name)
2978 return FALSE;
2979 memcpy (name, namebuf, len);
2980 newsect = bfd_make_section (abfd, name);
2981 if (newsect == NULL)
2982 return FALSE;
502794d4
CE
2983 newsect->vma = hdr->p_vaddr / opb;
2984 newsect->lma = hdr->p_paddr / opb;
d5191d0c
AM
2985 newsect->size = hdr->p_filesz;
2986 newsect->filepos = hdr->p_offset;
2987 newsect->flags |= SEC_HAS_CONTENTS;
2988 newsect->alignment_power = bfd_log2 (hdr->p_align);
2989 if (hdr->p_type == PT_LOAD)
252b5132 2990 {
d5191d0c
AM
2991 newsect->flags |= SEC_ALLOC;
2992 newsect->flags |= SEC_LOAD;
2993 if (hdr->p_flags & PF_X)
2994 {
2995 /* FIXME: all we known is that it has execute PERMISSION,
2996 may be data. */
2997 newsect->flags |= SEC_CODE;
2998 }
2999 }
3000 if (!(hdr->p_flags & PF_W))
3001 {
3002 newsect->flags |= SEC_READONLY;
252b5132 3003 }
252b5132
RH
3004 }
3005
d5191d0c 3006 if (hdr->p_memsz > hdr->p_filesz)
252b5132 3007 {
d5191d0c
AM
3008 bfd_vma align;
3009
91d6fa6a 3010 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
d4c88bbb 3011 len = strlen (namebuf) + 1;
a50b1753 3012 name = (char *) bfd_alloc (abfd, len);
252b5132 3013 if (!name)
b34976b6 3014 return FALSE;
d4c88bbb 3015 memcpy (name, namebuf, len);
252b5132
RH
3016 newsect = bfd_make_section (abfd, name);
3017 if (newsect == NULL)
b34976b6 3018 return FALSE;
502794d4
CE
3019 newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
3020 newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
eea6121a 3021 newsect->size = hdr->p_memsz - hdr->p_filesz;
d5191d0c
AM
3022 newsect->filepos = hdr->p_offset + hdr->p_filesz;
3023 align = newsect->vma & -newsect->vma;
3024 if (align == 0 || align > hdr->p_align)
3025 align = hdr->p_align;
3026 newsect->alignment_power = bfd_log2 (align);
252b5132
RH
3027 if (hdr->p_type == PT_LOAD)
3028 {
3029 newsect->flags |= SEC_ALLOC;
3030 if (hdr->p_flags & PF_X)
3031 newsect->flags |= SEC_CODE;
3032 }
3033 if (!(hdr->p_flags & PF_W))
3034 newsect->flags |= SEC_READONLY;
3035 }
3036
b34976b6 3037 return TRUE;
252b5132
RH
3038}
3039
864619bb
KS
3040static bfd_boolean
3041_bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3042{
3043 /* The return value is ignored. Build-ids are considered optional. */
3044 if (templ->xvec->flavour == bfd_target_elf_flavour)
3045 return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3046 (templ, offset);
3047 return FALSE;
3048}
3049
b34976b6 3050bfd_boolean
91d6fa6a 3051bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
20cfcaae 3052{
9c5bfbb7 3053 const struct elf_backend_data *bed;
20cfcaae
NC
3054
3055 switch (hdr->p_type)
3056 {
3057 case PT_NULL:
91d6fa6a 3058 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
20cfcaae
NC
3059
3060 case PT_LOAD:
864619bb
KS
3061 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3062 return FALSE;
3063 if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3064 _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3065 return TRUE;
20cfcaae
NC
3066
3067 case PT_DYNAMIC:
91d6fa6a 3068 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
20cfcaae
NC
3069
3070 case PT_INTERP:
91d6fa6a 3071 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
20cfcaae
NC
3072
3073 case PT_NOTE:
91d6fa6a 3074 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
b34976b6 3075 return FALSE;
276da9b3
L
3076 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3077 hdr->p_align))
b34976b6
AM
3078 return FALSE;
3079 return TRUE;
20cfcaae
NC
3080
3081 case PT_SHLIB:
91d6fa6a 3082 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
20cfcaae
NC
3083
3084 case PT_PHDR:
91d6fa6a 3085 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
20cfcaae 3086
811072d8 3087 case PT_GNU_EH_FRAME:
91d6fa6a 3088 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
811072d8
RM
3089 "eh_frame_hdr");
3090
2b05f1b7 3091 case PT_GNU_STACK:
91d6fa6a 3092 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
9ee5e499 3093
8c37241b 3094 case PT_GNU_RELRO:
91d6fa6a 3095 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
8c37241b 3096
20cfcaae 3097 default:
8c1acd09 3098 /* Check for any processor-specific program segment types. */
20cfcaae 3099 bed = get_elf_backend_data (abfd);
91d6fa6a 3100 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
20cfcaae
NC
3101 }
3102}
3103
d4730f92
BS
3104/* Return the REL_HDR for SEC, assuming there is only a single one, either
3105 REL or RELA. */
3106
3107Elf_Internal_Shdr *
3108_bfd_elf_single_rel_hdr (asection *sec)
3109{
3110 if (elf_section_data (sec)->rel.hdr)
3111 {
3112 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3113 return elf_section_data (sec)->rel.hdr;
3114 }
3115 else
3116 return elf_section_data (sec)->rela.hdr;
3117}
3118
3e19fb8f
L
3119static bfd_boolean
3120_bfd_elf_set_reloc_sh_name (bfd *abfd,
3121 Elf_Internal_Shdr *rel_hdr,
3122 const char *sec_name,
3123 bfd_boolean use_rela_p)
3124{
3125 char *name = (char *) bfd_alloc (abfd,
3126 sizeof ".rela" + strlen (sec_name));
3127 if (name == NULL)
3128 return FALSE;
3129
3130 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3131 rel_hdr->sh_name =
3132 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3133 FALSE);
3134 if (rel_hdr->sh_name == (unsigned int) -1)
3135 return FALSE;
3136
3137 return TRUE;
3138}
3139
d4730f92
BS
3140/* Allocate and initialize a section-header for a new reloc section,
3141 containing relocations against ASECT. It is stored in RELDATA. If
3142 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3143 relocations. */
23bc299b 3144
5d13b3b3 3145static bfd_boolean
217aa764 3146_bfd_elf_init_reloc_shdr (bfd *abfd,
d4730f92 3147 struct bfd_elf_section_reloc_data *reldata,
f6fe1ccd 3148 const char *sec_name,
3e19fb8f
L
3149 bfd_boolean use_rela_p,
3150 bfd_boolean delay_st_name_p)
23bc299b 3151{
d4730f92 3152 Elf_Internal_Shdr *rel_hdr;
9c5bfbb7 3153 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 3154
d4730f92 3155 BFD_ASSERT (reldata->hdr == NULL);
ef53be89 3156 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
d4730f92 3157 reldata->hdr = rel_hdr;
23bc299b 3158
3e19fb8f
L
3159 if (delay_st_name_p)
3160 rel_hdr->sh_name = (unsigned int) -1;
3161 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3162 use_rela_p))
b34976b6 3163 return FALSE;
23bc299b
MM
3164 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3165 rel_hdr->sh_entsize = (use_rela_p
3166 ? bed->s->sizeof_rela
3167 : bed->s->sizeof_rel);
72de5009 3168 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
28e07a05 3169 rel_hdr->sh_flags = 0;
23bc299b
MM
3170 rel_hdr->sh_addr = 0;
3171 rel_hdr->sh_size = 0;
3172 rel_hdr->sh_offset = 0;
3173
b34976b6 3174 return TRUE;
23bc299b
MM
3175}
3176
94be91de
JB
3177/* Return the default section type based on the passed in section flags. */
3178
3179int
3180bfd_elf_get_default_section_type (flagword flags)
3181{
0e41bebb 3182 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
2e76e85a 3183 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
94be91de
JB
3184 return SHT_NOBITS;
3185 return SHT_PROGBITS;
3186}
3187
d4730f92
BS
3188struct fake_section_arg
3189{
3190 struct bfd_link_info *link_info;
3191 bfd_boolean failed;
3192};
3193
252b5132
RH
3194/* Set up an ELF internal section header for a section. */
3195
252b5132 3196static void
d4730f92 3197elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
252b5132 3198{
d4730f92 3199 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
9c5bfbb7 3200 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 3201 struct bfd_elf_section_data *esd = elf_section_data (asect);
252b5132 3202 Elf_Internal_Shdr *this_hdr;
0414f35b 3203 unsigned int sh_type;
0ce398f1 3204 const char *name = asect->name;
3e19fb8f 3205 bfd_boolean delay_st_name_p = FALSE;
233bf4f8 3206 bfd_vma mask;
252b5132 3207
d4730f92 3208 if (arg->failed)
252b5132
RH
3209 {
3210 /* We already failed; just get out of the bfd_map_over_sections
08a40648 3211 loop. */
252b5132
RH
3212 return;
3213 }
3214
d4730f92 3215 this_hdr = &esd->this_hdr;
252b5132 3216
f6fe1ccd 3217 if (arg->link_info)
0ce398f1 3218 {
f6fe1ccd
L
3219 /* ld: compress DWARF debug sections with names: .debug_*. */
3220 if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
3221 && (asect->flags & SEC_DEBUGGING)
3222 && name[1] == 'd'
3223 && name[6] == '_')
3224 {
3225 /* Set SEC_ELF_COMPRESS to indicate this section should be
3226 compressed. */
3227 asect->flags |= SEC_ELF_COMPRESS;
dd905818 3228 /* If this section will be compressed, delay adding section
3e19fb8f
L
3229 name to section name section after it is compressed in
3230 _bfd_elf_assign_file_positions_for_non_load. */
3231 delay_st_name_p = TRUE;
f6fe1ccd
L
3232 }
3233 }
3234 else if ((asect->flags & SEC_ELF_RENAME))
3235 {
3236 /* objcopy: rename output DWARF debug section. */
3237 if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
3238 {
3239 /* When we decompress or compress with SHF_COMPRESSED,
3240 convert section name from .zdebug_* to .debug_* if
3241 needed. */
3242 if (name[1] == 'z')
3243 {
3244 char *new_name = convert_zdebug_to_debug (abfd, name);
3245 if (new_name == NULL)
3246 {
3247 arg->failed = TRUE;
3248 return;
3249 }
3250 name = new_name;
3251 }
3252 }
3253 else if (asect->compress_status == COMPRESS_SECTION_DONE)
0ce398f1 3254 {
f6fe1ccd
L
3255 /* PR binutils/18087: Compression does not always make a
3256 section smaller. So only rename the section when
3257 compression has actually taken place. If input section
3258 name is .zdebug_*, we should never compress it again. */
3259 char *new_name = convert_debug_to_zdebug (abfd, name);
0ce398f1
L
3260 if (new_name == NULL)
3261 {
3262 arg->failed = TRUE;
3263 return;
3264 }
f6fe1ccd
L
3265 BFD_ASSERT (name[1] != 'z');
3266 name = new_name;
0ce398f1
L
3267 }
3268 }
3269
3e19fb8f
L
3270 if (delay_st_name_p)
3271 this_hdr->sh_name = (unsigned int) -1;
3272 else
252b5132 3273 {
3e19fb8f
L
3274 this_hdr->sh_name
3275 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3276 name, FALSE);
3277 if (this_hdr->sh_name == (unsigned int) -1)
3278 {
3279 arg->failed = TRUE;
3280 return;
3281 }
252b5132
RH
3282 }
3283
a4d8e49b 3284 /* Don't clear sh_flags. Assembler may set additional bits. */
252b5132
RH
3285
3286 if ((asect->flags & SEC_ALLOC) != 0
3287 || asect->user_set_vma)
502794d4 3288 this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
252b5132
RH
3289 else
3290 this_hdr->sh_addr = 0;
3291
3292 this_hdr->sh_offset = 0;
eea6121a 3293 this_hdr->sh_size = asect->size;
252b5132 3294 this_hdr->sh_link = 0;
c86934ce
NC
3295 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3296 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3297 {
4eca0228 3298 _bfd_error_handler
695344c0 3299 /* xgettext:c-format */
9793eb77 3300 (_("%pB: error: alignment power %d of section `%pA' is too big"),
c08bb8dd 3301 abfd, asect->alignment_power, asect);
c86934ce
NC
3302 arg->failed = TRUE;
3303 return;
3304 }
233bf4f8
AM
3305 /* Set sh_addralign to the highest power of two given by alignment
3306 consistent with the section VMA. Linker scripts can force VMA. */
3307 mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3308 this_hdr->sh_addralign = mask & -mask;
252b5132
RH
3309 /* The sh_entsize and sh_info fields may have been set already by
3310 copy_private_section_data. */
3311
3312 this_hdr->bfd_section = asect;
3313 this_hdr->contents = NULL;
3314
3cddba1e
L
3315 /* If the section type is unspecified, we set it based on
3316 asect->flags. */
98ece1b3
AM
3317 if ((asect->flags & SEC_GROUP) != 0)
3318 sh_type = SHT_GROUP;
98ece1b3 3319 else
94be91de 3320 sh_type = bfd_elf_get_default_section_type (asect->flags);
98ece1b3 3321
3cddba1e 3322 if (this_hdr->sh_type == SHT_NULL)
98ece1b3
AM
3323 this_hdr->sh_type = sh_type;
3324 else if (this_hdr->sh_type == SHT_NOBITS
3325 && sh_type == SHT_PROGBITS
3326 && (asect->flags & SEC_ALLOC) != 0)
3cddba1e 3327 {
98ece1b3
AM
3328 /* Warn if we are changing a NOBITS section to PROGBITS, but
3329 allow the link to proceed. This can happen when users link
3330 non-bss input sections to bss output sections, or emit data
3331 to a bss output section via a linker script. */
4eca0228 3332 _bfd_error_handler
871b3ab2 3333 (_("warning: section `%pA' type changed to PROGBITS"), asect);
98ece1b3 3334 this_hdr->sh_type = sh_type;
3cddba1e
L
3335 }
3336
2f89ff8d 3337 switch (this_hdr->sh_type)
252b5132 3338 {
2f89ff8d 3339 default:
2f89ff8d
L
3340 break;
3341
3342 case SHT_STRTAB:
2f89ff8d
L
3343 case SHT_NOTE:
3344 case SHT_NOBITS:
3345 case SHT_PROGBITS:
3346 break;
606851fb
AM
3347
3348 case SHT_INIT_ARRAY:
3349 case SHT_FINI_ARRAY:
3350 case SHT_PREINIT_ARRAY:
3351 this_hdr->sh_entsize = bed->s->arch_size / 8;
3352 break;
2f89ff8d
L
3353
3354 case SHT_HASH:
c7ac6ff8 3355 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2f89ff8d 3356 break;
5de3bf90 3357
2f89ff8d 3358 case SHT_DYNSYM:
252b5132 3359 this_hdr->sh_entsize = bed->s->sizeof_sym;
2f89ff8d
L
3360 break;
3361
3362 case SHT_DYNAMIC:
252b5132 3363 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2f89ff8d
L
3364 break;
3365
3366 case SHT_RELA:
3367 if (get_elf_backend_data (abfd)->may_use_rela_p)
3368 this_hdr->sh_entsize = bed->s->sizeof_rela;
3369 break;
3370
3371 case SHT_REL:
3372 if (get_elf_backend_data (abfd)->may_use_rel_p)
3373 this_hdr->sh_entsize = bed->s->sizeof_rel;
3374 break;
3375
3376 case SHT_GNU_versym:
252b5132 3377 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2f89ff8d
L
3378 break;
3379
3380 case SHT_GNU_verdef:
252b5132
RH
3381 this_hdr->sh_entsize = 0;
3382 /* objcopy or strip will copy over sh_info, but may not set
08a40648
AM
3383 cverdefs. The linker will set cverdefs, but sh_info will be
3384 zero. */
252b5132
RH
3385 if (this_hdr->sh_info == 0)
3386 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3387 else
3388 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3389 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2f89ff8d
L
3390 break;
3391
3392 case SHT_GNU_verneed:
252b5132
RH
3393 this_hdr->sh_entsize = 0;
3394 /* objcopy or strip will copy over sh_info, but may not set
08a40648
AM
3395 cverrefs. The linker will set cverrefs, but sh_info will be
3396 zero. */
252b5132
RH
3397 if (this_hdr->sh_info == 0)
3398 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3399 else
3400 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3401 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2f89ff8d
L
3402 break;
3403
3404 case SHT_GROUP:
1783205a 3405 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2f89ff8d 3406 break;
fdc90cb4
JJ
3407
3408 case SHT_GNU_HASH:
3409 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3410 break;
dbb410c3 3411 }
252b5132
RH
3412
3413 if ((asect->flags & SEC_ALLOC) != 0)
3414 this_hdr->sh_flags |= SHF_ALLOC;
3415 if ((asect->flags & SEC_READONLY) == 0)
3416 this_hdr->sh_flags |= SHF_WRITE;
3417 if ((asect->flags & SEC_CODE) != 0)
3418 this_hdr->sh_flags |= SHF_EXECINSTR;
f5fa8ca2
JJ
3419 if ((asect->flags & SEC_MERGE) != 0)
3420 {
3421 this_hdr->sh_flags |= SHF_MERGE;
3422 this_hdr->sh_entsize = asect->entsize;
f5fa8ca2 3423 }
84865015
NC
3424 if ((asect->flags & SEC_STRINGS) != 0)
3425 this_hdr->sh_flags |= SHF_STRINGS;
1126897b 3426 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
dbb410c3 3427 this_hdr->sh_flags |= SHF_GROUP;
13ae64f3 3428 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
704afa60
JJ
3429 {
3430 this_hdr->sh_flags |= SHF_TLS;
3a800eb9
AM
3431 if (asect->size == 0
3432 && (asect->flags & SEC_HAS_CONTENTS) == 0)
704afa60 3433 {
3a800eb9 3434 struct bfd_link_order *o = asect->map_tail.link_order;
b34976b6 3435
704afa60 3436 this_hdr->sh_size = 0;
3a800eb9
AM
3437 if (o != NULL)
3438 {
704afa60 3439 this_hdr->sh_size = o->offset + o->size;
3a800eb9
AM
3440 if (this_hdr->sh_size != 0)
3441 this_hdr->sh_type = SHT_NOBITS;
3442 }
704afa60
JJ
3443 }
3444 }
18ae9cc1
L
3445 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3446 this_hdr->sh_flags |= SHF_EXCLUDE;
252b5132 3447
d4730f92
BS
3448 /* If the section has relocs, set up a section header for the
3449 SHT_REL[A] section. If two relocation sections are required for
3450 this section, it is up to the processor-specific back-end to
3451 create the other. */
3452 if ((asect->flags & SEC_RELOC) != 0)
3453 {
3454 /* When doing a relocatable link, create both REL and RELA sections if
3455 needed. */
3456 if (arg->link_info
3457 /* Do the normal setup if we wouldn't create any sections here. */
3458 && esd->rel.count + esd->rela.count > 0
0e1862bb
L
3459 && (bfd_link_relocatable (arg->link_info)
3460 || arg->link_info->emitrelocations))
d4730f92
BS
3461 {
3462 if (esd->rel.count && esd->rel.hdr == NULL
28e07a05 3463 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
db4677b8 3464 FALSE, delay_st_name_p))
d4730f92
BS
3465 {
3466 arg->failed = TRUE;
3467 return;
3468 }
3469 if (esd->rela.count && esd->rela.hdr == NULL
28e07a05 3470 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
db4677b8 3471 TRUE, delay_st_name_p))
d4730f92
BS
3472 {
3473 arg->failed = TRUE;
3474 return;
3475 }
3476 }
3477 else if (!_bfd_elf_init_reloc_shdr (abfd,
3478 (asect->use_rela_p
3479 ? &esd->rela : &esd->rel),
f6fe1ccd 3480 name,
3e19fb8f
L
3481 asect->use_rela_p,
3482 delay_st_name_p))
db4677b8 3483 {
d4730f92 3484 arg->failed = TRUE;
db4677b8
AM
3485 return;
3486 }
d4730f92
BS
3487 }
3488
252b5132 3489 /* Check for processor-specific section types. */
0414f35b 3490 sh_type = this_hdr->sh_type;
e1fddb6b
AO
3491 if (bed->elf_backend_fake_sections
3492 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
db4677b8
AM
3493 {
3494 arg->failed = TRUE;
3495 return;
3496 }
252b5132 3497
42bb2e33 3498 if (sh_type == SHT_NOBITS && asect->size != 0)
0414f35b
AM
3499 {
3500 /* Don't change the header type from NOBITS if we are being
42bb2e33 3501 called for objcopy --only-keep-debug. */
0414f35b
AM
3502 this_hdr->sh_type = sh_type;
3503 }
252b5132
RH
3504}
3505
bcacc0f5
AM
3506/* Fill in the contents of a SHT_GROUP section. Called from
3507 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3508 when ELF targets use the generic linker, ld. Called for ld -r
3509 from bfd_elf_final_link. */
dbb410c3 3510
1126897b 3511void
217aa764 3512bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
dbb410c3 3513{
a50b1753 3514 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
9dce4196 3515 asection *elt, *first;
dbb410c3 3516 unsigned char *loc;
b34976b6 3517 bfd_boolean gas;
dbb410c3 3518
7e4111ad
L
3519 /* Ignore linker created group section. See elfNN_ia64_object_p in
3520 elfxx-ia64.c. */
ce5aecf8
AM
3521 if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3522 || sec->size == 0
dbb410c3
AM
3523 || *failedptr)
3524 return;
3525
bcacc0f5
AM
3526 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3527 {
3528 unsigned long symindx = 0;
3529
3530 /* elf_group_id will have been set up by objcopy and the
3531 generic linker. */
3532 if (elf_group_id (sec) != NULL)
3533 symindx = elf_group_id (sec)->udata.i;
1126897b 3534
bcacc0f5
AM
3535 if (symindx == 0)
3536 {
3537 /* If called from the assembler, swap_out_syms will have set up
6a541707
NC
3538 elf_section_syms.
3539 PR 25699: A corrupt input file could contain bogus group info. */
3540 if (elf_section_syms (abfd) == NULL)
3541 {
3542 *failedptr = TRUE;
3543 return;
3544 }
bcacc0f5
AM
3545 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3546 }
3547 elf_section_data (sec)->this_hdr.sh_info = symindx;
3548 }
3549 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
1126897b 3550 {
bcacc0f5
AM
3551 /* The ELF backend linker sets sh_info to -2 when the group
3552 signature symbol is global, and thus the index can't be
3553 set until all local symbols are output. */
53720c49
AM
3554 asection *igroup;
3555 struct bfd_elf_section_data *sec_data;
3556 unsigned long symndx;
3557 unsigned long extsymoff;
bcacc0f5
AM
3558 struct elf_link_hash_entry *h;
3559
53720c49
AM
3560 /* The point of this little dance to the first SHF_GROUP section
3561 then back to the SHT_GROUP section is that this gets us to
3562 the SHT_GROUP in the input object. */
3563 igroup = elf_sec_group (elf_next_in_group (sec));
3564 sec_data = elf_section_data (igroup);
3565 symndx = sec_data->this_hdr.sh_info;
3566 extsymoff = 0;
bcacc0f5
AM
3567 if (!elf_bad_symtab (igroup->owner))
3568 {
3569 Elf_Internal_Shdr *symtab_hdr;
3570
3571 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3572 extsymoff = symtab_hdr->sh_info;
3573 }
3574 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3575 while (h->root.type == bfd_link_hash_indirect
3576 || h->root.type == bfd_link_hash_warning)
3577 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3578
3579 elf_section_data (sec)->this_hdr.sh_info = h->indx;
1126897b 3580 }
dbb410c3 3581
1126897b 3582 /* The contents won't be allocated for "ld -r" or objcopy. */
b34976b6 3583 gas = TRUE;
dbb410c3
AM
3584 if (sec->contents == NULL)
3585 {
b34976b6 3586 gas = FALSE;
a50b1753 3587 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
9dce4196
AM
3588
3589 /* Arrange for the section to be written out. */
3590 elf_section_data (sec)->this_hdr.contents = sec->contents;
dbb410c3
AM
3591 if (sec->contents == NULL)
3592 {
b34976b6 3593 *failedptr = TRUE;
dbb410c3
AM
3594 return;
3595 }
3596 }
3597
eea6121a 3598 loc = sec->contents + sec->size;
dbb410c3 3599
9dce4196
AM
3600 /* Get the pointer to the first section in the group that gas
3601 squirreled away here. objcopy arranges for this to be set to the
3602 start of the input section group. */
3603 first = elt = elf_next_in_group (sec);
dbb410c3
AM
3604
3605 /* First element is a flag word. Rest of section is elf section
3606 indices for all the sections of the group. Write them backwards
3607 just to keep the group in the same order as given in .section
3608 directives, not that it matters. */
3609 while (elt != NULL)
3610 {
9dce4196 3611 asection *s;
9dce4196 3612
9dce4196 3613 s = elt;
415f38a6
AM
3614 if (!gas)
3615 s = s->output_section;
3616 if (s != NULL
3617 && !bfd_is_abs_section (s))
01e1a5bc 3618 {
db4677b8 3619 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
28e07a05
AM
3620 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3621
3622 if (elf_sec->rel.hdr != NULL
3623 && (gas
3624 || (input_elf_sec->rel.hdr != NULL
3625 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
db4677b8 3626 {
28e07a05 3627 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
db4677b8
AM
3628 loc -= 4;
3629 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3630 }
28e07a05
AM
3631 if (elf_sec->rela.hdr != NULL
3632 && (gas
3633 || (input_elf_sec->rela.hdr != NULL
3634 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
db4677b8 3635 {
28e07a05 3636 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
db4677b8
AM
3637 loc -= 4;
3638 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3639 }
01e1a5bc 3640 loc -= 4;
db4677b8 3641 H_PUT_32 (abfd, elf_sec->this_idx, loc);
01e1a5bc 3642 }
945906ff 3643 elt = elf_next_in_group (elt);
9dce4196
AM
3644 if (elt == first)
3645 break;
dbb410c3
AM
3646 }
3647
7bdf4127
AB
3648 loc -= 4;
3649 BFD_ASSERT (loc == sec->contents);
dbb410c3 3650
9dce4196 3651 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
dbb410c3
AM
3652}
3653
bce964aa
AM
3654/* Given NAME, the name of a relocation section stripped of its
3655 .rel/.rela prefix, return the section in ABFD to which the
3656 relocations apply. */
bd53a53a
L
3657
3658asection *
bce964aa
AM
3659_bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3660{
3661 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3662 section likely apply to .got.plt or .got section. */
3663 if (get_elf_backend_data (abfd)->want_got_plt
3664 && strcmp (name, ".plt") == 0)
3665 {
3666 asection *sec;
3667
3668 name = ".got.plt";
3669 sec = bfd_get_section_by_name (abfd, name);
3670 if (sec != NULL)
3671 return sec;
3672 name = ".got";
3673 }
3674
3675 return bfd_get_section_by_name (abfd, name);
3676}
3677
3678/* Return the section to which RELOC_SEC applies. */
3679
3680static asection *
3681elf_get_reloc_section (asection *reloc_sec)
bd53a53a
L
3682{
3683 const char *name;
3684 unsigned int type;
3685 bfd *abfd;
bce964aa 3686 const struct elf_backend_data *bed;
bd53a53a
L
3687
3688 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3689 if (type != SHT_REL && type != SHT_RELA)
3690 return NULL;
3691
3692 /* We look up the section the relocs apply to by name. */
3693 name = reloc_sec->name;
bce964aa
AM
3694 if (strncmp (name, ".rel", 4) != 0)
3695 return NULL;
3696 name += 4;
3697 if (type == SHT_RELA && *name++ != 'a')
3698 return NULL;
bd53a53a 3699
bd53a53a 3700 abfd = reloc_sec->owner;
bce964aa
AM
3701 bed = get_elf_backend_data (abfd);
3702 return bed->get_reloc_section (abfd, name);
bd53a53a
L
3703}
3704
252b5132
RH
3705/* Assign all ELF section numbers. The dummy first section is handled here
3706 too. The link/info pointers for the standard section types are filled
67411cbf
AM
3707 in here too, while we're at it. LINK_INFO will be 0 when arriving
3708 here for objcopy, and when using the generic ELF linker. */
252b5132 3709
b34976b6 3710static bfd_boolean
da9f89d4 3711assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
252b5132
RH
3712{
3713 struct elf_obj_tdata *t = elf_tdata (abfd);
3714 asection *sec;
3e19fb8f 3715 unsigned int section_number;
252b5132 3716 Elf_Internal_Shdr **i_shdrp;
47cc2cf5 3717 struct bfd_elf_section_data *d;
3516e984 3718 bfd_boolean need_symtab;
446f7ed5 3719 size_t amt;
252b5132
RH
3720
3721 section_number = 1;
3722
2b0f7ef9
JJ
3723 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3724
da9f89d4 3725 /* SHT_GROUP sections are in relocatable files only. */
7bdf4127 3726 if (link_info == NULL || !link_info->resolve_section_groups)
252b5132 3727 {
ef53be89 3728 size_t reloc_count = 0;
14f2c699 3729
da9f89d4 3730 /* Put SHT_GROUP sections first. */
04dd1667 3731 for (sec = abfd->sections; sec != NULL; sec = sec->next)
47cc2cf5 3732 {
5daa8fe7 3733 d = elf_section_data (sec);
da9f89d4
L
3734
3735 if (d->this_hdr.sh_type == SHT_GROUP)
08a40648 3736 {
5daa8fe7 3737 if (sec->flags & SEC_LINKER_CREATED)
da9f89d4
L
3738 {
3739 /* Remove the linker created SHT_GROUP sections. */
5daa8fe7 3740 bfd_section_list_remove (abfd, sec);
da9f89d4 3741 abfd->section_count--;
da9f89d4 3742 }
08a40648 3743 else
4fbb74a6 3744 d->this_idx = section_number++;
da9f89d4 3745 }
14f2c699
L
3746
3747 /* Count relocations. */
3748 reloc_count += sec->reloc_count;
47cc2cf5 3749 }
14f2c699
L
3750
3751 /* Clear HAS_RELOC if there are no relocations. */
3752 if (reloc_count == 0)
3753 abfd->flags &= ~HAS_RELOC;
47cc2cf5
PB
3754 }
3755
3756 for (sec = abfd->sections; sec; sec = sec->next)
3757 {
3758 d = elf_section_data (sec);
3759
3760 if (d->this_hdr.sh_type != SHT_GROUP)
4fbb74a6 3761 d->this_idx = section_number++;
3e19fb8f
L
3762 if (d->this_hdr.sh_name != (unsigned int) -1)
3763 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
d4730f92 3764 if (d->rel.hdr)
2b0f7ef9 3765 {
d4730f92 3766 d->rel.idx = section_number++;
3e19fb8f
L
3767 if (d->rel.hdr->sh_name != (unsigned int) -1)
3768 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
2b0f7ef9 3769 }
d4730f92
BS
3770 else
3771 d->rel.idx = 0;
23bc299b 3772
d4730f92 3773 if (d->rela.hdr)
2b0f7ef9 3774 {
d4730f92 3775 d->rela.idx = section_number++;
3e19fb8f
L
3776 if (d->rela.hdr->sh_name != (unsigned int) -1)
3777 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
2b0f7ef9 3778 }
23bc299b 3779 else
d4730f92 3780 d->rela.idx = 0;
252b5132
RH
3781 }
3782
3516e984
L
3783 need_symtab = (bfd_get_symcount (abfd) > 0
3784 || (link_info == NULL
3785 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3786 == HAS_RELOC)));
3787 if (need_symtab)
252b5132 3788 {
12bd6957 3789 elf_onesymtab (abfd) = section_number++;
2b0f7ef9 3790 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4fbb74a6 3791 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
9ad5cbcf 3792 {
7a6e0d89 3793 elf_section_list *entry;
6a40cf0c
NC
3794
3795 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
3796
7a6e0d89 3797 entry = bfd_zalloc (abfd, sizeof (*entry));
6a40cf0c
NC
3798 entry->ndx = section_number++;
3799 elf_symtab_shndx_list (abfd) = entry;
3800 entry->hdr.sh_name
9ad5cbcf 3801 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
b34976b6 3802 ".symtab_shndx", FALSE);
6a40cf0c 3803 if (entry->hdr.sh_name == (unsigned int) -1)
b34976b6 3804 return FALSE;
9ad5cbcf 3805 }
12bd6957 3806 elf_strtab_sec (abfd) = section_number++;
2b0f7ef9 3807 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
252b5132
RH
3808 }
3809
dd905818
NC
3810 elf_shstrtab_sec (abfd) = section_number++;
3811 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3812 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3813
1c52a645
L
3814 if (section_number >= SHN_LORESERVE)
3815 {
695344c0 3816 /* xgettext:c-format */
871b3ab2 3817 _bfd_error_handler (_("%pB: too many sections: %u"),
1c52a645
L
3818 abfd, section_number);
3819 return FALSE;
3820 }
3821
9ad5cbcf 3822 elf_numsections (abfd) = section_number;
252b5132
RH
3823 elf_elfheader (abfd)->e_shnum = section_number;
3824
3825 /* Set up the list of section header pointers, in agreement with the
3826 indices. */
446f7ed5
AM
3827 amt = section_number * sizeof (Elf_Internal_Shdr *);
3828 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
252b5132 3829 if (i_shdrp == NULL)
b34976b6 3830 return FALSE;
252b5132 3831
a50b1753 3832 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
07d6d2b8 3833 sizeof (Elf_Internal_Shdr));
252b5132
RH
3834 if (i_shdrp[0] == NULL)
3835 {
3836 bfd_release (abfd, i_shdrp);
b34976b6 3837 return FALSE;
252b5132 3838 }
252b5132
RH
3839
3840 elf_elfsections (abfd) = i_shdrp;
3841
12bd6957 3842 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3516e984 3843 if (need_symtab)
252b5132 3844 {
12bd6957 3845 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4fbb74a6 3846 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
9ad5cbcf 3847 {
6a40cf0c
NC
3848 elf_section_list * entry = elf_symtab_shndx_list (abfd);
3849 BFD_ASSERT (entry != NULL);
3850 i_shdrp[entry->ndx] = & entry->hdr;
3851 entry->hdr.sh_link = elf_onesymtab (abfd);
9ad5cbcf 3852 }
12bd6957
AM
3853 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3854 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
252b5132 3855 }
38ce5b11 3856
252b5132
RH
3857 for (sec = abfd->sections; sec; sec = sec->next)
3858 {
252b5132 3859 asection *s;
252b5132 3860
91d6fa6a
NC
3861 d = elf_section_data (sec);
3862
252b5132 3863 i_shdrp[d->this_idx] = &d->this_hdr;
d4730f92
BS
3864 if (d->rel.idx != 0)
3865 i_shdrp[d->rel.idx] = d->rel.hdr;
3866 if (d->rela.idx != 0)
3867 i_shdrp[d->rela.idx] = d->rela.hdr;
252b5132
RH
3868
3869 /* Fill in the sh_link and sh_info fields while we're at it. */
3870
3871 /* sh_link of a reloc section is the section index of the symbol
3872 table. sh_info is the section index of the section to which
3873 the relocation entries apply. */
d4730f92 3874 if (d->rel.idx != 0)
252b5132 3875 {
12bd6957 3876 d->rel.hdr->sh_link = elf_onesymtab (abfd);
d4730f92 3877 d->rel.hdr->sh_info = d->this_idx;
9ef5d938 3878 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
252b5132 3879 }
d4730f92 3880 if (d->rela.idx != 0)
23bc299b 3881 {
12bd6957 3882 d->rela.hdr->sh_link = elf_onesymtab (abfd);
d4730f92 3883 d->rela.hdr->sh_info = d->this_idx;
9ef5d938 3884 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
23bc299b 3885 }
252b5132 3886
38ce5b11
L
3887 /* We need to set up sh_link for SHF_LINK_ORDER. */
3888 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3889 {
3890 s = elf_linked_to_section (sec);
b71702f1
NC
3891 /* We can now have a NULL linked section pointer.
3892 This happens when the sh_link field is 0, which is done
3893 when a linked to section is discarded but the linking
3894 section has been retained for some reason. */
38ce5b11 3895 if (s)
38ce5b11 3896 {
67411cbf
AM
3897 /* Check discarded linkonce section. */
3898 if (discarded_section (s))
38ce5b11 3899 {
67411cbf
AM
3900 asection *kept;
3901 _bfd_error_handler
3902 /* xgettext:c-format */
3903 (_("%pB: sh_link of section `%pA' points to"
3904 " discarded section `%pA' of `%pB'"),
3905 abfd, d->this_hdr.bfd_section, s, s->owner);
3906 /* Point to the kept section if it has the same
3907 size as the discarded one. */
3908 kept = _bfd_elf_check_kept_section (s, link_info);
3909 if (kept == NULL)
f2876037 3910 {
f2876037
L
3911 bfd_set_error (bfd_error_bad_value);
3912 return FALSE;
3913 }
67411cbf
AM
3914 s = kept;
3915 }
3916 /* Handle objcopy. */
3917 else if (s->output_section == NULL)
3918 {
3919 _bfd_error_handler
3920 /* xgettext:c-format */
3921 (_("%pB: sh_link of section `%pA' points to"
3922 " removed section `%pA' of `%pB'"),
3923 abfd, d->this_hdr.bfd_section, s, s->owner);
3924 bfd_set_error (bfd_error_bad_value);
3925 return FALSE;
f2876037 3926 }
67411cbf 3927 s = s->output_section;
ccd2ec6a
L
3928 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3929 }
38ce5b11
L
3930 }
3931
252b5132
RH
3932 switch (d->this_hdr.sh_type)
3933 {
3934 case SHT_REL:
3935 case SHT_RELA:
3936 /* A reloc section which we are treating as a normal BFD
3937 section. sh_link is the section index of the symbol
3938 table. sh_info is the section index of the section to
3939 which the relocation entries apply. We assume that an
3940 allocated reloc section uses the dynamic symbol table.
3941 FIXME: How can we be sure? */
3942 s = bfd_get_section_by_name (abfd, ".dynsym");
3943 if (s != NULL)
3944 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3945
bce964aa 3946 s = elf_get_reloc_section (sec);
252b5132 3947 if (s != NULL)
9ef5d938
L
3948 {
3949 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3950 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3951 }
252b5132
RH
3952 break;
3953
3954 case SHT_STRTAB:
3955 /* We assume that a section named .stab*str is a stabs
3956 string section. We look for a section with the same name
3957 but without the trailing ``str'', and set its sh_link
3958 field to point to this section. */
0112cd26 3959 if (CONST_STRNEQ (sec->name, ".stab")
252b5132
RH
3960 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3961 {
3962 size_t len;
3963 char *alc;
3964
3965 len = strlen (sec->name);
a50b1753 3966 alc = (char *) bfd_malloc (len - 2);
252b5132 3967 if (alc == NULL)
b34976b6 3968 return FALSE;
d4c88bbb 3969 memcpy (alc, sec->name, len - 3);
252b5132
RH
3970 alc[len - 3] = '\0';
3971 s = bfd_get_section_by_name (abfd, alc);
3972 free (alc);
3973 if (s != NULL)
3974 {
3975 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3976
3977 /* This is a .stab section. */
34ca5531 3978 elf_section_data (s)->this_hdr.sh_entsize = 12;
252b5132
RH
3979 }
3980 }
3981 break;
3982
3983 case SHT_DYNAMIC:
3984 case SHT_DYNSYM:
3985 case SHT_GNU_verneed:
3986 case SHT_GNU_verdef:
3987 /* sh_link is the section header index of the string table
3988 used for the dynamic entries, or the symbol table, or the
3989 version strings. */
3990 s = bfd_get_section_by_name (abfd, ".dynstr");
3991 if (s != NULL)
3992 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3993 break;
3994
7f1204bb
JJ
3995 case SHT_GNU_LIBLIST:
3996 /* sh_link is the section header index of the prelink library
08a40648
AM
3997 list used for the dynamic entries, or the symbol table, or
3998 the version strings. */
7f1204bb
JJ
3999 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
4000 ? ".dynstr" : ".gnu.libstr");
4001 if (s != NULL)
4002 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4003 break;
4004
252b5132 4005 case SHT_HASH:
fdc90cb4 4006 case SHT_GNU_HASH:
252b5132
RH
4007 case SHT_GNU_versym:
4008 /* sh_link is the section header index of the symbol table
4009 this hash table or version table is for. */
4010 s = bfd_get_section_by_name (abfd, ".dynsym");
4011 if (s != NULL)
4012 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4013 break;
dbb410c3
AM
4014
4015 case SHT_GROUP:
12bd6957 4016 d->this_hdr.sh_link = elf_onesymtab (abfd);
252b5132
RH
4017 }
4018 }
4019
3e19fb8f
L
4020 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4021 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4022 debug section name from .debug_* to .zdebug_* if needed. */
4023
b34976b6 4024 return TRUE;
252b5132
RH
4025}
4026
5372391b 4027static bfd_boolean
217aa764 4028sym_is_global (bfd *abfd, asymbol *sym)
252b5132
RH
4029{
4030 /* If the backend has a special mapping, use it. */
9c5bfbb7 4031 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764
AM
4032 if (bed->elf_backend_sym_is_global)
4033 return (*bed->elf_backend_sym_is_global) (abfd, sym);
252b5132 4034
e47bf690 4035 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
e6f7f6d1
AM
4036 || bfd_is_und_section (bfd_asymbol_section (sym))
4037 || bfd_is_com_section (bfd_asymbol_section (sym)));
252b5132
RH
4038}
4039
76359541
TP
4040/* Filter global symbols of ABFD to include in the import library. All
4041 SYMCOUNT symbols of ABFD can be examined from their pointers in
4042 SYMS. Pointers of symbols to keep should be stored contiguously at
4043 the beginning of that array.
4044
4045 Returns the number of symbols to keep. */
4046
4047unsigned int
4048_bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4049 asymbol **syms, long symcount)
4050{
4051 long src_count, dst_count = 0;
4052
4053 for (src_count = 0; src_count < symcount; src_count++)
4054 {
4055 asymbol *sym = syms[src_count];
4056 char *name = (char *) bfd_asymbol_name (sym);
4057 struct bfd_link_hash_entry *h;
4058
4059 if (!sym_is_global (abfd, sym))
4060 continue;
4061
4062 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE);
5df1bc57
AM
4063 if (h == NULL)
4064 continue;
76359541
TP
4065 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4066 continue;
76359541
TP
4067 if (h->linker_def || h->ldscript_def)
4068 continue;
4069
4070 syms[dst_count++] = sym;
4071 }
4072
4073 syms[dst_count] = NULL;
4074
4075 return dst_count;
4076}
4077
5372391b 4078/* Don't output section symbols for sections that are not going to be
c6d8cab4 4079 output, that are duplicates or there is no BFD section. */
5372391b
AM
4080
4081static bfd_boolean
4082ignore_section_sym (bfd *abfd, asymbol *sym)
4083{
c6d8cab4
L
4084 elf_symbol_type *type_ptr;
4085
db0c309f
NC
4086 if (sym == NULL)
4087 return FALSE;
4088
c6d8cab4
L
4089 if ((sym->flags & BSF_SECTION_SYM) == 0)
4090 return FALSE;
4091
db0c309f
NC
4092 if (sym->section == NULL)
4093 return TRUE;
4094
c1229f84 4095 type_ptr = elf_symbol_from (sym);
c6d8cab4
L
4096 return ((type_ptr != NULL
4097 && type_ptr->internal_elf_sym.st_shndx != 0
4098 && bfd_is_abs_section (sym->section))
4099 || !(sym->section->owner == abfd
db0c309f
NC
4100 || (sym->section->output_section != NULL
4101 && sym->section->output_section->owner == abfd
2633a79c
AM
4102 && sym->section->output_offset == 0)
4103 || bfd_is_abs_section (sym->section)));
5372391b
AM
4104}
4105
2633a79c
AM
4106/* Map symbol from it's internal number to the external number, moving
4107 all local symbols to be at the head of the list. */
4108
b34976b6 4109static bfd_boolean
12bd6957 4110elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
252b5132 4111{
dc810e39 4112 unsigned int symcount = bfd_get_symcount (abfd);
252b5132
RH
4113 asymbol **syms = bfd_get_outsymbols (abfd);
4114 asymbol **sect_syms;
dc810e39
AM
4115 unsigned int num_locals = 0;
4116 unsigned int num_globals = 0;
4117 unsigned int num_locals2 = 0;
4118 unsigned int num_globals2 = 0;
7292b3ac 4119 unsigned int max_index = 0;
dc810e39 4120 unsigned int idx;
252b5132
RH
4121 asection *asect;
4122 asymbol **new_syms;
446f7ed5 4123 size_t amt;
252b5132
RH
4124
4125#ifdef DEBUG
4126 fprintf (stderr, "elf_map_symbols\n");
4127 fflush (stderr);
4128#endif
4129
252b5132
RH
4130 for (asect = abfd->sections; asect; asect = asect->next)
4131 {
4132 if (max_index < asect->index)
4133 max_index = asect->index;
4134 }
4135
4136 max_index++;
446f7ed5
AM
4137 amt = max_index * sizeof (asymbol *);
4138 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
252b5132 4139 if (sect_syms == NULL)
b34976b6 4140 return FALSE;
252b5132 4141 elf_section_syms (abfd) = sect_syms;
4e89ac30 4142 elf_num_section_syms (abfd) = max_index;
252b5132 4143
079e9a2f
AM
4144 /* Init sect_syms entries for any section symbols we have already
4145 decided to output. */
252b5132
RH
4146 for (idx = 0; idx < symcount; idx++)
4147 {
dc810e39 4148 asymbol *sym = syms[idx];
c044fabd 4149
252b5132 4150 if ((sym->flags & BSF_SECTION_SYM) != 0
0f0a5e58 4151 && sym->value == 0
2633a79c
AM
4152 && !ignore_section_sym (abfd, sym)
4153 && !bfd_is_abs_section (sym->section))
252b5132 4154 {
5372391b 4155 asection *sec = sym->section;
252b5132 4156
5372391b
AM
4157 if (sec->owner != abfd)
4158 sec = sec->output_section;
252b5132 4159
5372391b 4160 sect_syms[sec->index] = syms[idx];
252b5132
RH
4161 }
4162 }
4163
252b5132
RH
4164 /* Classify all of the symbols. */
4165 for (idx = 0; idx < symcount; idx++)
4166 {
2633a79c 4167 if (sym_is_global (abfd, syms[idx]))
252b5132 4168 num_globals++;
2633a79c
AM
4169 else if (!ignore_section_sym (abfd, syms[idx]))
4170 num_locals++;
252b5132 4171 }
079e9a2f 4172
5372391b 4173 /* We will be adding a section symbol for each normal BFD section. Most
079e9a2f
AM
4174 sections will already have a section symbol in outsymbols, but
4175 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4176 at least in that case. */
252b5132
RH
4177 for (asect = abfd->sections; asect; asect = asect->next)
4178 {
079e9a2f 4179 if (sect_syms[asect->index] == NULL)
252b5132 4180 {
079e9a2f 4181 if (!sym_is_global (abfd, asect->symbol))
252b5132
RH
4182 num_locals++;
4183 else
4184 num_globals++;
252b5132
RH
4185 }
4186 }
4187
4188 /* Now sort the symbols so the local symbols are first. */
446f7ed5
AM
4189 amt = (num_locals + num_globals) * sizeof (asymbol *);
4190 new_syms = (asymbol **) bfd_alloc (abfd, amt);
252b5132 4191 if (new_syms == NULL)
b34976b6 4192 return FALSE;
252b5132
RH
4193
4194 for (idx = 0; idx < symcount; idx++)
4195 {
4196 asymbol *sym = syms[idx];
dc810e39 4197 unsigned int i;
252b5132 4198
2633a79c
AM
4199 if (sym_is_global (abfd, sym))
4200 i = num_locals + num_globals2++;
4201 else if (!ignore_section_sym (abfd, sym))
252b5132
RH
4202 i = num_locals2++;
4203 else
2633a79c 4204 continue;
252b5132
RH
4205 new_syms[i] = sym;
4206 sym->udata.i = i + 1;
4207 }
4208 for (asect = abfd->sections; asect; asect = asect->next)
4209 {
079e9a2f 4210 if (sect_syms[asect->index] == NULL)
252b5132 4211 {
079e9a2f 4212 asymbol *sym = asect->symbol;
dc810e39 4213 unsigned int i;
252b5132 4214
079e9a2f 4215 sect_syms[asect->index] = sym;
252b5132
RH
4216 if (!sym_is_global (abfd, sym))
4217 i = num_locals2++;
4218 else
4219 i = num_locals + num_globals2++;
4220 new_syms[i] = sym;
4221 sym->udata.i = i + 1;
4222 }
4223 }
4224
4225 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4226
12bd6957 4227 *pnum_locals = num_locals;
b34976b6 4228 return TRUE;
252b5132
RH
4229}
4230
4231/* Align to the maximum file alignment that could be required for any
4232 ELF data structure. */
4233
268b6b39 4234static inline file_ptr
217aa764 4235align_file_position (file_ptr off, int align)
252b5132
RH
4236{
4237 return (off + align - 1) & ~(align - 1);
4238}
4239
4240/* Assign a file position to a section, optionally aligning to the
4241 required section alignment. */
4242
217aa764
AM
4243file_ptr
4244_bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4245 file_ptr offset,
4246 bfd_boolean align)
252b5132 4247{
72de5009
AM
4248 if (align && i_shdrp->sh_addralign > 1)
4249 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
252b5132
RH
4250 i_shdrp->sh_offset = offset;
4251 if (i_shdrp->bfd_section != NULL)
4252 i_shdrp->bfd_section->filepos = offset;
4253 if (i_shdrp->sh_type != SHT_NOBITS)
4254 offset += i_shdrp->sh_size;
4255 return offset;
4256}
4257
4258/* Compute the file positions we are going to put the sections at, and
4259 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4260 is not NULL, this is being called by the ELF backend linker. */
4261
b34976b6 4262bfd_boolean
217aa764
AM
4263_bfd_elf_compute_section_file_positions (bfd *abfd,
4264 struct bfd_link_info *link_info)
252b5132 4265{
9c5bfbb7 4266 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 4267 struct fake_section_arg fsargs;
b34976b6 4268 bfd_boolean failed;
ef10c3ac 4269 struct elf_strtab_hash *strtab = NULL;
252b5132 4270 Elf_Internal_Shdr *shstrtab_hdr;
3516e984 4271 bfd_boolean need_symtab;
252b5132
RH
4272
4273 if (abfd->output_has_begun)
b34976b6 4274 return TRUE;
252b5132
RH
4275
4276 /* Do any elf backend specific processing first. */
4277 if (bed->elf_backend_begin_write_processing)
4278 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4279
ed7e9d0b 4280 if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
b34976b6 4281 return FALSE;
252b5132 4282
d4730f92
BS
4283 fsargs.failed = FALSE;
4284 fsargs.link_info = link_info;
4285 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4286 if (fsargs.failed)
b34976b6 4287 return FALSE;
252b5132 4288
da9f89d4 4289 if (!assign_section_numbers (abfd, link_info))
b34976b6 4290 return FALSE;
252b5132
RH
4291
4292 /* The backend linker builds symbol table information itself. */
3516e984
L
4293 need_symtab = (link_info == NULL
4294 && (bfd_get_symcount (abfd) > 0
4295 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4296 == HAS_RELOC)));
4297 if (need_symtab)
252b5132
RH
4298 {
4299 /* Non-zero if doing a relocatable link. */
4300 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4301
4302 if (! swap_out_syms (abfd, &strtab, relocatable_p))
b34976b6 4303 return FALSE;
252b5132
RH
4304 }
4305
d4730f92 4306 failed = FALSE;
1126897b 4307 if (link_info == NULL)
dbb410c3 4308 {
1126897b 4309 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
dbb410c3 4310 if (failed)
b34976b6 4311 return FALSE;
dbb410c3
AM
4312 }
4313
252b5132 4314 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
ed7e9d0b 4315 /* sh_name was set in init_file_header. */
252b5132 4316 shstrtab_hdr->sh_type = SHT_STRTAB;
84865015 4317 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
252b5132 4318 shstrtab_hdr->sh_addr = 0;
946748d5 4319 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
252b5132
RH
4320 shstrtab_hdr->sh_entsize = 0;
4321 shstrtab_hdr->sh_link = 0;
4322 shstrtab_hdr->sh_info = 0;
3e19fb8f 4323 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
252b5132
RH
4324 shstrtab_hdr->sh_addralign = 1;
4325
c84fca4d 4326 if (!assign_file_positions_except_relocs (abfd, link_info))
b34976b6 4327 return FALSE;
252b5132 4328
3516e984 4329 if (need_symtab)
252b5132
RH
4330 {
4331 file_ptr off;
4332 Elf_Internal_Shdr *hdr;
4333
12bd6957 4334 off = elf_next_file_pos (abfd);
252b5132 4335
6a40cf0c 4336 hdr = & elf_symtab_hdr (abfd);
b34976b6 4337 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
252b5132 4338
6a40cf0c
NC
4339 if (elf_symtab_shndx_list (abfd) != NULL)
4340 {
4341 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4342 if (hdr->sh_size != 0)
4343 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4344 /* FIXME: What about other symtab_shndx sections in the list ? */
4345 }
9ad5cbcf 4346
252b5132 4347 hdr = &elf_tdata (abfd)->strtab_hdr;
b34976b6 4348 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
252b5132 4349
12bd6957 4350 elf_next_file_pos (abfd) = off;
252b5132
RH
4351
4352 /* Now that we know where the .strtab section goes, write it
08a40648 4353 out. */
252b5132 4354 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 4355 || ! _bfd_elf_strtab_emit (abfd, strtab))
b34976b6 4356 return FALSE;
ef10c3ac 4357 _bfd_elf_strtab_free (strtab);
252b5132
RH
4358 }
4359
b34976b6 4360 abfd->output_has_begun = TRUE;
252b5132 4361
b34976b6 4362 return TRUE;
252b5132
RH
4363}
4364
8ded5a0f
AM
4365/* Make an initial estimate of the size of the program header. If we
4366 get the number wrong here, we'll redo section placement. */
4367
4368static bfd_size_type
4369get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4370{
4371 size_t segs;
4372 asection *s;
2b05f1b7 4373 const struct elf_backend_data *bed;
8ded5a0f
AM
4374
4375 /* Assume we will need exactly two PT_LOAD segments: one for text
4376 and one for data. */
4377 segs = 2;
4378
4379 s = bfd_get_section_by_name (abfd, ".interp");
1b9e270b 4380 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
8ded5a0f
AM
4381 {
4382 /* If we have a loadable interpreter section, we need a
4383 PT_INTERP segment. In this case, assume we also need a
4384 PT_PHDR segment, although that may not be true for all
4385 targets. */
e9a38e0f 4386 segs += 2;
8ded5a0f
AM
4387 }
4388
4389 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4390 {
4391 /* We need a PT_DYNAMIC segment. */
4392 ++segs;
f210dcff 4393 }
08a40648 4394
ceae84aa 4395 if (info != NULL && info->relro)
f210dcff
L
4396 {
4397 /* We need a PT_GNU_RELRO segment. */
4398 ++segs;
8ded5a0f
AM
4399 }
4400
12bd6957 4401 if (elf_eh_frame_hdr (abfd))
8ded5a0f
AM
4402 {
4403 /* We need a PT_GNU_EH_FRAME segment. */
4404 ++segs;
4405 }
4406
12bd6957 4407 if (elf_stack_flags (abfd))
8ded5a0f 4408 {
2b05f1b7
L
4409 /* We need a PT_GNU_STACK segment. */
4410 ++segs;
4411 }
94b11780 4412
0a59decb
L
4413 s = bfd_get_section_by_name (abfd,
4414 NOTE_GNU_PROPERTY_SECTION_NAME);
4415 if (s != NULL && s->size != 0)
4416 {
4417 /* We need a PT_GNU_PROPERTY segment. */
4418 ++segs;
4419 }
4420
2b05f1b7
L
4421 for (s = abfd->sections; s != NULL; s = s->next)
4422 {
8ded5a0f 4423 if ((s->flags & SEC_LOAD) != 0
23e463ed 4424 && elf_section_type (s) == SHT_NOTE)
8ded5a0f 4425 {
23e463ed 4426 unsigned int alignment_power;
8ded5a0f
AM
4427 /* We need a PT_NOTE segment. */
4428 ++segs;
23e463ed
L
4429 /* Try to create just one PT_NOTE segment for all adjacent
4430 loadable SHT_NOTE sections. gABI requires that within a
4431 PT_NOTE segment (and also inside of each SHT_NOTE section)
4432 each note should have the same alignment. So we check
4433 whether the sections are correctly aligned. */
4434 alignment_power = s->alignment_power;
4435 while (s->next != NULL
4436 && s->next->alignment_power == alignment_power
4437 && (s->next->flags & SEC_LOAD) != 0
4438 && elf_section_type (s->next) == SHT_NOTE)
4439 s = s->next;
8ded5a0f
AM
4440 }
4441 }
4442
4443 for (s = abfd->sections; s != NULL; s = s->next)
4444 {
4445 if (s->flags & SEC_THREAD_LOCAL)
4446 {
4447 /* We need a PT_TLS segment. */
4448 ++segs;
4449 break;
4450 }
4451 }
4452
2b05f1b7 4453 bed = get_elf_backend_data (abfd);
a91e1603 4454
df3a023b
AM
4455 if ((abfd->flags & D_PAGED) != 0
4456 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4457 {
4458 /* Add a PT_GNU_MBIND segment for each mbind section. */
4459 unsigned int page_align_power = bfd_log2 (bed->commonpagesize);
4460 for (s = abfd->sections; s != NULL; s = s->next)
4461 if (elf_section_flags (s) & SHF_GNU_MBIND)
4462 {
4463 if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4464 {
4465 _bfd_error_handler
4466 /* xgettext:c-format */
4467 (_("%pB: GNU_MBIND section `%pA' has invalid "
4468 "sh_info field: %d"),
4469 abfd, s, elf_section_data (s)->this_hdr.sh_info);
4470 continue;
4471 }
4472 /* Align mbind section to page size. */
4473 if (s->alignment_power < page_align_power)
4474 s->alignment_power = page_align_power;
4475 segs ++;
4476 }
4477 }
4478
4479 /* Let the backend count up any program headers it might need. */
4480 if (bed->elf_backend_additional_program_headers)
8ded5a0f
AM
4481 {
4482 int a;
4483
4484 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4485 if (a == -1)
4486 abort ();
4487 segs += a;
4488 }
4489
4490 return segs * bed->s->sizeof_phdr;
4491}
4492
2ea37f1c
NC
4493/* Find the segment that contains the output_section of section. */
4494
4495Elf_Internal_Phdr *
4496_bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4497{
4498 struct elf_segment_map *m;
4499 Elf_Internal_Phdr *p;
4500
12bd6957 4501 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
2ea37f1c
NC
4502 m != NULL;
4503 m = m->next, p++)
4504 {
4505 int i;
4506
4507 for (i = m->count - 1; i >= 0; i--)
4508 if (m->sections[i] == section)
4509 return p;
4510 }
4511
4512 return NULL;
4513}
4514
252b5132
RH
4515/* Create a mapping from a set of sections to a program segment. */
4516
217aa764
AM
4517static struct elf_segment_map *
4518make_mapping (bfd *abfd,
4519 asection **sections,
4520 unsigned int from,
4521 unsigned int to,
4522 bfd_boolean phdr)
252b5132
RH
4523{
4524 struct elf_segment_map *m;
4525 unsigned int i;
4526 asection **hdrpp;
986f0783 4527 size_t amt;
252b5132 4528
00bee008
AM
4529 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4530 amt += (to - from) * sizeof (asection *);
a50b1753 4531 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
4532 if (m == NULL)
4533 return NULL;
4534 m->next = NULL;
4535 m->p_type = PT_LOAD;
4536 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4537 m->sections[i - from] = *hdrpp;
4538 m->count = to - from;
4539
4540 if (from == 0 && phdr)
4541 {
4542 /* Include the headers in the first PT_LOAD segment. */
4543 m->includes_filehdr = 1;
4544 m->includes_phdrs = 1;
4545 }
4546
4547 return m;
4548}
4549
229fcec5
MM
4550/* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4551 on failure. */
4552
4553struct elf_segment_map *
4554_bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4555{
4556 struct elf_segment_map *m;
4557
a50b1753 4558 m = (struct elf_segment_map *) bfd_zalloc (abfd,
07d6d2b8 4559 sizeof (struct elf_segment_map));
229fcec5
MM
4560 if (m == NULL)
4561 return NULL;
4562 m->next = NULL;
4563 m->p_type = PT_DYNAMIC;
4564 m->count = 1;
4565 m->sections[0] = dynsec;
08a40648 4566
229fcec5
MM
4567 return m;
4568}
4569
8ded5a0f 4570/* Possibly add or remove segments from the segment map. */
252b5132 4571
b34976b6 4572static bfd_boolean
3dea8fca
AM
4573elf_modify_segment_map (bfd *abfd,
4574 struct bfd_link_info *info,
4575 bfd_boolean remove_empty_load)
252b5132 4576{
252e386e 4577 struct elf_segment_map **m;
8ded5a0f 4578 const struct elf_backend_data *bed;
252b5132 4579
8ded5a0f
AM
4580 /* The placement algorithm assumes that non allocated sections are
4581 not in PT_LOAD segments. We ensure this here by removing such
4582 sections from the segment map. We also remove excluded
252e386e
AM
4583 sections. Finally, any PT_LOAD segment without sections is
4584 removed. */
12bd6957 4585 m = &elf_seg_map (abfd);
252e386e 4586 while (*m)
8ded5a0f
AM
4587 {
4588 unsigned int i, new_count;
252b5132 4589
252e386e 4590 for (new_count = 0, i = 0; i < (*m)->count; i++)
8ded5a0f 4591 {
252e386e
AM
4592 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4593 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4594 || (*m)->p_type != PT_LOAD))
8ded5a0f 4595 {
252e386e
AM
4596 (*m)->sections[new_count] = (*m)->sections[i];
4597 new_count++;
8ded5a0f
AM
4598 }
4599 }
252e386e 4600 (*m)->count = new_count;
252b5132 4601
1a9ccd70
NC
4602 if (remove_empty_load
4603 && (*m)->p_type == PT_LOAD
4604 && (*m)->count == 0
4605 && !(*m)->includes_phdrs)
252e386e
AM
4606 *m = (*m)->next;
4607 else
4608 m = &(*m)->next;
8ded5a0f 4609 }
252b5132 4610
8ded5a0f
AM
4611 bed = get_elf_backend_data (abfd);
4612 if (bed->elf_backend_modify_segment_map != NULL)
252b5132 4613 {
252e386e 4614 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
8ded5a0f 4615 return FALSE;
252b5132 4616 }
252b5132 4617
8ded5a0f
AM
4618 return TRUE;
4619}
252b5132 4620
dbc88fc1
AM
4621#define IS_TBSS(s) \
4622 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4623
8ded5a0f 4624/* Set up a mapping from BFD sections to program segments. */
252b5132 4625
8ded5a0f
AM
4626bfd_boolean
4627_bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
4628{
4629 unsigned int count;
4630 struct elf_segment_map *m;
4631 asection **sections = NULL;
4632 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3dea8fca 4633 bfd_boolean no_user_phdrs;
252b5132 4634
12bd6957 4635 no_user_phdrs = elf_seg_map (abfd) == NULL;
d324f6d6
RM
4636
4637 if (info != NULL)
4638 info->user_phdrs = !no_user_phdrs;
4639
3dea8fca 4640 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
252b5132 4641 {
8ded5a0f
AM
4642 asection *s;
4643 unsigned int i;
4644 struct elf_segment_map *mfirst;
4645 struct elf_segment_map **pm;
4646 asection *last_hdr;
4647 bfd_vma last_size;
00bee008 4648 unsigned int hdr_index;
8ded5a0f
AM
4649 bfd_vma maxpagesize;
4650 asection **hdrpp;
64029e93 4651 bfd_boolean phdr_in_segment;
8ded5a0f 4652 bfd_boolean writable;
2888249f 4653 bfd_boolean executable;
446f7ed5 4654 unsigned int tls_count = 0;
8ded5a0f 4655 asection *first_tls = NULL;
a91e1603 4656 asection *first_mbind = NULL;
8ded5a0f 4657 asection *dynsec, *eh_frame_hdr;
446f7ed5 4658 size_t amt;
66631823
CE
4659 bfd_vma addr_mask, wrap_to = 0; /* Bytes. */
4660 bfd_size_type phdr_size; /* Octets/bytes. */
502794d4 4661 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132 4662
8ded5a0f 4663 /* Select the allocated sections, and sort them. */
252b5132 4664
446f7ed5
AM
4665 amt = bfd_count_sections (abfd) * sizeof (asection *);
4666 sections = (asection **) bfd_malloc (amt);
8ded5a0f 4667 if (sections == NULL)
252b5132 4668 goto error_return;
252b5132 4669
8d06853e
AM
4670 /* Calculate top address, avoiding undefined behaviour of shift
4671 left operator when shift count is equal to size of type
4672 being shifted. */
4673 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4674 addr_mask = (addr_mask << 1) + 1;
4675
8ded5a0f
AM
4676 i = 0;
4677 for (s = abfd->sections; s != NULL; s = s->next)
4678 {
4679 if ((s->flags & SEC_ALLOC) != 0)
4680 {
48db3297
AM
4681 /* target_index is unused until bfd_elf_final_link
4682 starts output of section symbols. Use it to make
4683 qsort stable. */
4684 s->target_index = i;
8ded5a0f
AM
4685 sections[i] = s;
4686 ++i;
8d06853e 4687 /* A wrapping section potentially clashes with header. */
66631823
CE
4688 if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
4689 wrap_to = (s->lma + s->size / opb) & addr_mask;
8ded5a0f
AM
4690 }
4691 }
4692 BFD_ASSERT (i <= bfd_count_sections (abfd));
4693 count = i;
252b5132 4694
8ded5a0f 4695 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
252b5132 4696
64029e93
AM
4697 phdr_size = elf_program_header_size (abfd);
4698 if (phdr_size == (bfd_size_type) -1)
4699 phdr_size = get_program_header_size (abfd, info);
4700 phdr_size += bed->s->sizeof_ehdr;
502794d4
CE
4701 /* phdr_size is compared to LMA values which are in bytes. */
4702 phdr_size /= opb;
64029e93
AM
4703 maxpagesize = bed->maxpagesize;
4704 if (maxpagesize == 0)
4705 maxpagesize = 1;
4706 phdr_in_segment = info != NULL && info->load_phdrs;
4707 if (count != 0
4708 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
4709 >= (phdr_size & (maxpagesize - 1))))
4710 /* For compatibility with old scripts that may not be using
4711 SIZEOF_HEADERS, add headers when it looks like space has
4712 been left for them. */
4713 phdr_in_segment = TRUE;
252b5132 4714
64029e93 4715 /* Build the mapping. */
8ded5a0f
AM
4716 mfirst = NULL;
4717 pm = &mfirst;
252b5132 4718
8ded5a0f
AM
4719 /* If we have a .interp section, then create a PT_PHDR segment for
4720 the program headers and a PT_INTERP segment for the .interp
4721 section. */
4722 s = bfd_get_section_by_name (abfd, ".interp");
1b9e270b 4723 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
8ded5a0f
AM
4724 {
4725 amt = sizeof (struct elf_segment_map);
a50b1753 4726 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4727 if (m == NULL)
4728 goto error_return;
4729 m->next = NULL;
4730 m->p_type = PT_PHDR;
f882209d 4731 m->p_flags = PF_R;
8ded5a0f
AM
4732 m->p_flags_valid = 1;
4733 m->includes_phdrs = 1;
64029e93 4734 phdr_in_segment = TRUE;
8ded5a0f
AM
4735 *pm = m;
4736 pm = &m->next;
252b5132 4737
8ded5a0f 4738 amt = sizeof (struct elf_segment_map);
a50b1753 4739 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4740 if (m == NULL)
4741 goto error_return;
4742 m->next = NULL;
4743 m->p_type = PT_INTERP;
4744 m->count = 1;
4745 m->sections[0] = s;
4746
4747 *pm = m;
4748 pm = &m->next;
252b5132 4749 }
8ded5a0f
AM
4750
4751 /* Look through the sections. We put sections in the same program
4752 segment when the start of the second section can be placed within
4753 a few bytes of the end of the first section. */
4754 last_hdr = NULL;
4755 last_size = 0;
00bee008 4756 hdr_index = 0;
8ded5a0f 4757 writable = FALSE;
2888249f 4758 executable = FALSE;
8ded5a0f
AM
4759 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4760 if (dynsec != NULL
4761 && (dynsec->flags & SEC_LOAD) == 0)
4762 dynsec = NULL;
4763
64029e93
AM
4764 if ((abfd->flags & D_PAGED) == 0)
4765 phdr_in_segment = FALSE;
4766
8ded5a0f
AM
4767 /* Deal with -Ttext or something similar such that the first section
4768 is not adjacent to the program headers. This is an
4769 approximation, since at this point we don't know exactly how many
4770 program headers we will need. */
64029e93 4771 if (phdr_in_segment && count > 0)
252b5132 4772 {
66631823 4773 bfd_vma phdr_lma; /* Bytes. */
64029e93
AM
4774 bfd_boolean separate_phdr = FALSE;
4775
4776 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
4777 if (info != NULL
4778 && info->separate_code
4779 && (sections[0]->flags & SEC_CODE) != 0)
1a9ccd70 4780 {
64029e93
AM
4781 /* If data sections should be separate from code and
4782 thus not executable, and the first section is
4783 executable then put the file and program headers in
4784 their own PT_LOAD. */
4785 separate_phdr = TRUE;
4786 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
4787 == (sections[0]->lma & addr_mask & -maxpagesize)))
4788 {
4789 /* The file and program headers are currently on the
4790 same page as the first section. Put them on the
4791 previous page if we can. */
4792 if (phdr_lma >= maxpagesize)
4793 phdr_lma -= maxpagesize;
4794 else
4795 separate_phdr = FALSE;
4796 }
4797 }
4798 if ((sections[0]->lma & addr_mask) < phdr_lma
4799 || (sections[0]->lma & addr_mask) < phdr_size)
4800 /* If file and program headers would be placed at the end
4801 of memory then it's probably better to omit them. */
4802 phdr_in_segment = FALSE;
4803 else if (phdr_lma < wrap_to)
4804 /* If a section wraps around to where we'll be placing
4805 file and program headers, then the headers will be
4806 overwritten. */
4807 phdr_in_segment = FALSE;
4808 else if (separate_phdr)
4809 {
4810 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
4811 if (m == NULL)
4812 goto error_return;
66631823 4813 m->p_paddr = phdr_lma * opb;
64029e93
AM
4814 m->p_vaddr_offset
4815 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
4816 m->p_paddr_valid = 1;
4817 *pm = m;
4818 pm = &m->next;
4819 phdr_in_segment = FALSE;
1a9ccd70 4820 }
252b5132
RH
4821 }
4822
8ded5a0f 4823 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
252b5132 4824 {
8ded5a0f
AM
4825 asection *hdr;
4826 bfd_boolean new_segment;
4827
4828 hdr = *hdrpp;
4829
4830 /* See if this section and the last one will fit in the same
4831 segment. */
4832
4833 if (last_hdr == NULL)
4834 {
4835 /* If we don't have a segment yet, then we don't need a new
4836 one (we build the last one after this loop). */
4837 new_segment = FALSE;
4838 }
4839 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4840 {
4841 /* If this section has a different relation between the
4842 virtual address and the load address, then we need a new
4843 segment. */
4844 new_segment = TRUE;
4845 }
b5599592
AM
4846 else if (hdr->lma < last_hdr->lma + last_size
4847 || last_hdr->lma + last_size < last_hdr->lma)
4848 {
4849 /* If this section has a load address that makes it overlap
4850 the previous section, then we need a new segment. */
4851 new_segment = TRUE;
4852 }
76cb3a89
AM
4853 else if ((abfd->flags & D_PAGED) != 0
4854 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4855 == (hdr->lma & -maxpagesize)))
4856 {
4857 /* If we are demand paged then we can't map two disk
4858 pages onto the same memory page. */
4859 new_segment = FALSE;
4860 }
39948a60
NC
4861 /* In the next test we have to be careful when last_hdr->lma is close
4862 to the end of the address space. If the aligned address wraps
4863 around to the start of the address space, then there are no more
4864 pages left in memory and it is OK to assume that the current
4865 section can be included in the current segment. */
76cb3a89
AM
4866 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4867 + maxpagesize > last_hdr->lma)
4868 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4869 + maxpagesize <= hdr->lma))
8ded5a0f
AM
4870 {
4871 /* If putting this section in this segment would force us to
4872 skip a page in the segment, then we need a new segment. */
4873 new_segment = TRUE;
4874 }
4875 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
76cb3a89 4876 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
8ded5a0f 4877 {
e5654c0f
AM
4878 /* We don't want to put a loaded section after a
4879 nonloaded (ie. bss style) section in the same segment
4880 as that will force the non-loaded section to be loaded.
76cb3a89 4881 Consider .tbss sections as loaded for this purpose. */
8ded5a0f
AM
4882 new_segment = TRUE;
4883 }
4884 else if ((abfd->flags & D_PAGED) == 0)
4885 {
4886 /* If the file is not demand paged, which means that we
4887 don't require the sections to be correctly aligned in the
4888 file, then there is no other reason for a new segment. */
4889 new_segment = FALSE;
4890 }
2888249f
L
4891 else if (info != NULL
4892 && info->separate_code
4893 && executable != ((hdr->flags & SEC_CODE) != 0))
4894 {
4895 new_segment = TRUE;
4896 }
8ded5a0f 4897 else if (! writable
76cb3a89 4898 && (hdr->flags & SEC_READONLY) == 0)
8ded5a0f
AM
4899 {
4900 /* We don't want to put a writable section in a read only
76cb3a89 4901 segment. */
8ded5a0f
AM
4902 new_segment = TRUE;
4903 }
4904 else
4905 {
4906 /* Otherwise, we can use the same segment. */
4907 new_segment = FALSE;
4908 }
4909
2889e75b 4910 /* Allow interested parties a chance to override our decision. */
ceae84aa
AM
4911 if (last_hdr != NULL
4912 && info != NULL
4913 && info->callbacks->override_segment_assignment != NULL)
4914 new_segment
4915 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4916 last_hdr,
4917 new_segment);
2889e75b 4918
8ded5a0f
AM
4919 if (! new_segment)
4920 {
4921 if ((hdr->flags & SEC_READONLY) == 0)
4922 writable = TRUE;
2888249f
L
4923 if ((hdr->flags & SEC_CODE) != 0)
4924 executable = TRUE;
8ded5a0f
AM
4925 last_hdr = hdr;
4926 /* .tbss sections effectively have zero size. */
502794d4 4927 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
8ded5a0f
AM
4928 continue;
4929 }
4930
4931 /* We need a new program segment. We must create a new program
00bee008 4932 header holding all the sections from hdr_index until hdr. */
8ded5a0f 4933
00bee008 4934 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
8ded5a0f
AM
4935 if (m == NULL)
4936 goto error_return;
4937
4938 *pm = m;
4939 pm = &m->next;
4940
252b5132 4941 if ((hdr->flags & SEC_READONLY) == 0)
b34976b6 4942 writable = TRUE;
8ded5a0f
AM
4943 else
4944 writable = FALSE;
4945
2888249f
L
4946 if ((hdr->flags & SEC_CODE) == 0)
4947 executable = FALSE;
4948 else
4949 executable = TRUE;
4950
baaff79e
JJ
4951 last_hdr = hdr;
4952 /* .tbss sections effectively have zero size. */
502794d4 4953 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
00bee008 4954 hdr_index = i;
8ded5a0f 4955 phdr_in_segment = FALSE;
252b5132
RH
4956 }
4957
86b2281f
AM
4958 /* Create a final PT_LOAD program segment, but not if it's just
4959 for .tbss. */
4960 if (last_hdr != NULL
00bee008 4961 && (i - hdr_index != 1
dbc88fc1 4962 || !IS_TBSS (last_hdr)))
8ded5a0f 4963 {
00bee008 4964 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
8ded5a0f
AM
4965 if (m == NULL)
4966 goto error_return;
252b5132 4967
8ded5a0f
AM
4968 *pm = m;
4969 pm = &m->next;
4970 }
252b5132 4971
8ded5a0f
AM
4972 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4973 if (dynsec != NULL)
4974 {
4975 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4976 if (m == NULL)
4977 goto error_return;
4978 *pm = m;
4979 pm = &m->next;
4980 }
252b5132 4981
23e463ed 4982 /* For each batch of consecutive loadable SHT_NOTE sections,
1c5265b5
JJ
4983 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4984 because if we link together nonloadable .note sections and
4985 loadable .note sections, we will generate two .note sections
23e463ed 4986 in the output file. */
8ded5a0f
AM
4987 for (s = abfd->sections; s != NULL; s = s->next)
4988 {
4989 if ((s->flags & SEC_LOAD) != 0
23e463ed 4990 && elf_section_type (s) == SHT_NOTE)
8ded5a0f 4991 {
1c5265b5 4992 asection *s2;
23e463ed 4993 unsigned int alignment_power = s->alignment_power;
91d6fa6a
NC
4994
4995 count = 1;
23e463ed
L
4996 for (s2 = s; s2->next != NULL; s2 = s2->next)
4997 {
4998 if (s2->next->alignment_power == alignment_power
4999 && (s2->next->flags & SEC_LOAD) != 0
5000 && elf_section_type (s2->next) == SHT_NOTE
66631823 5001 && align_power (s2->lma + s2->size / opb,
23e463ed
L
5002 alignment_power)
5003 == s2->next->lma)
5004 count++;
5005 else
5006 break;
5007 }
00bee008
AM
5008 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5009 amt += count * sizeof (asection *);
a50b1753 5010 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5011 if (m == NULL)
5012 goto error_return;
5013 m->next = NULL;
5014 m->p_type = PT_NOTE;
1c5265b5
JJ
5015 m->count = count;
5016 while (count > 1)
5017 {
5018 m->sections[m->count - count--] = s;
5019 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5020 s = s->next;
5021 }
5022 m->sections[m->count - 1] = s;
5023 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
8ded5a0f
AM
5024 *pm = m;
5025 pm = &m->next;
5026 }
5027 if (s->flags & SEC_THREAD_LOCAL)
5028 {
5029 if (! tls_count)
5030 first_tls = s;
5031 tls_count++;
5032 }
a91e1603
L
5033 if (first_mbind == NULL
5034 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5035 first_mbind = s;
8ded5a0f 5036 }
252b5132 5037
8ded5a0f
AM
5038 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5039 if (tls_count > 0)
5040 {
00bee008
AM
5041 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5042 amt += tls_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_TLS;
5048 m->count = tls_count;
5049 /* Mandated PF_R. */
5050 m->p_flags = PF_R;
5051 m->p_flags_valid = 1;
d923cae0 5052 s = first_tls;
446f7ed5 5053 for (i = 0; i < tls_count; ++i)
8ded5a0f 5054 {
d923cae0
L
5055 if ((s->flags & SEC_THREAD_LOCAL) == 0)
5056 {
5057 _bfd_error_handler
871b3ab2 5058 (_("%pB: TLS sections are not adjacent:"), abfd);
d923cae0
L
5059 s = first_tls;
5060 i = 0;
446f7ed5 5061 while (i < tls_count)
d923cae0
L
5062 {
5063 if ((s->flags & SEC_THREAD_LOCAL) != 0)
5064 {
871b3ab2 5065 _bfd_error_handler (_(" TLS: %pA"), s);
d923cae0
L
5066 i++;
5067 }
5068 else
871b3ab2 5069 _bfd_error_handler (_(" non-TLS: %pA"), s);
d923cae0
L
5070 s = s->next;
5071 }
5072 bfd_set_error (bfd_error_bad_value);
5073 goto error_return;
5074 }
5075 m->sections[i] = s;
5076 s = s->next;
8ded5a0f 5077 }
252b5132 5078
8ded5a0f
AM
5079 *pm = m;
5080 pm = &m->next;
5081 }
252b5132 5082
df3a023b
AM
5083 if (first_mbind
5084 && (abfd->flags & D_PAGED) != 0
5085 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
a91e1603
L
5086 for (s = first_mbind; s != NULL; s = s->next)
5087 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
df3a023b 5088 && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
a91e1603
L
5089 {
5090 /* Mandated PF_R. */
5091 unsigned long p_flags = PF_R;
5092 if ((s->flags & SEC_READONLY) == 0)
5093 p_flags |= PF_W;
5094 if ((s->flags & SEC_CODE) != 0)
5095 p_flags |= PF_X;
5096
5097 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5098 m = bfd_zalloc (abfd, amt);
5099 if (m == NULL)
5100 goto error_return;
5101 m->next = NULL;
5102 m->p_type = (PT_GNU_MBIND_LO
5103 + elf_section_data (s)->this_hdr.sh_info);
5104 m->count = 1;
5105 m->p_flags_valid = 1;
5106 m->sections[0] = s;
5107 m->p_flags = p_flags;
5108
5109 *pm = m;
5110 pm = &m->next;
5111 }
5112
0a59decb
L
5113 s = bfd_get_section_by_name (abfd,
5114 NOTE_GNU_PROPERTY_SECTION_NAME);
5115 if (s != NULL && s->size != 0)
5116 {
5117 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5118 m = bfd_zalloc (abfd, amt);
5119 if (m == NULL)
5120 goto error_return;
5121 m->next = NULL;
5122 m->p_type = PT_GNU_PROPERTY;
5123 m->count = 1;
5124 m->p_flags_valid = 1;
5125 m->sections[0] = s;
5126 m->p_flags = PF_R;
5127 *pm = m;
5128 pm = &m->next;
5129 }
5130
8ded5a0f
AM
5131 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5132 segment. */
12bd6957 5133 eh_frame_hdr = elf_eh_frame_hdr (abfd);
8ded5a0f
AM
5134 if (eh_frame_hdr != NULL
5135 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
252b5132 5136 {
dc810e39 5137 amt = sizeof (struct elf_segment_map);
a50b1753 5138 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
5139 if (m == NULL)
5140 goto error_return;
5141 m->next = NULL;
8ded5a0f 5142 m->p_type = PT_GNU_EH_FRAME;
252b5132 5143 m->count = 1;
8ded5a0f 5144 m->sections[0] = eh_frame_hdr->output_section;
252b5132
RH
5145
5146 *pm = m;
5147 pm = &m->next;
5148 }
13ae64f3 5149
12bd6957 5150 if (elf_stack_flags (abfd))
13ae64f3 5151 {
8ded5a0f 5152 amt = sizeof (struct elf_segment_map);
a50b1753 5153 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5154 if (m == NULL)
5155 goto error_return;
5156 m->next = NULL;
2b05f1b7 5157 m->p_type = PT_GNU_STACK;
12bd6957 5158 m->p_flags = elf_stack_flags (abfd);
04c3a755 5159 m->p_align = bed->stack_align;
8ded5a0f 5160 m->p_flags_valid = 1;
04c3a755
NS
5161 m->p_align_valid = m->p_align != 0;
5162 if (info->stacksize > 0)
5163 {
5164 m->p_size = info->stacksize;
5165 m->p_size_valid = 1;
5166 }
252b5132 5167
8ded5a0f
AM
5168 *pm = m;
5169 pm = &m->next;
5170 }
65765700 5171
ceae84aa 5172 if (info != NULL && info->relro)
8ded5a0f 5173 {
f210dcff
L
5174 for (m = mfirst; m != NULL; m = m->next)
5175 {
3832a4d8
AM
5176 if (m->p_type == PT_LOAD
5177 && m->count != 0
5178 && m->sections[0]->vma >= info->relro_start
5179 && m->sections[0]->vma < info->relro_end)
f210dcff 5180 {
3832a4d8
AM
5181 i = m->count;
5182 while (--i != (unsigned) -1)
ec2e748a
NC
5183 {
5184 if (m->sections[i]->size > 0
5185 && (m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
5186 == (SEC_LOAD | SEC_HAS_CONTENTS))
5187 break;
5188 }
3832a4d8 5189
43a8475c 5190 if (i != (unsigned) -1)
f210dcff
L
5191 break;
5192 }
be01b344 5193 }
f210dcff
L
5194
5195 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5196 if (m != NULL)
5197 {
5198 amt = sizeof (struct elf_segment_map);
a50b1753 5199 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
f210dcff
L
5200 if (m == NULL)
5201 goto error_return;
5202 m->next = NULL;
5203 m->p_type = PT_GNU_RELRO;
f210dcff
L
5204 *pm = m;
5205 pm = &m->next;
5206 }
8ded5a0f 5207 }
9ee5e499 5208
8ded5a0f 5209 free (sections);
12bd6957 5210 elf_seg_map (abfd) = mfirst;
9ee5e499
JJ
5211 }
5212
3dea8fca 5213 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
8ded5a0f 5214 return FALSE;
8c37241b 5215
12bd6957 5216 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
8ded5a0f 5217 ++count;
12bd6957 5218 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
252b5132 5219
b34976b6 5220 return TRUE;
252b5132
RH
5221
5222 error_return:
c9594989 5223 free (sections);
b34976b6 5224 return FALSE;
252b5132
RH
5225}
5226
5227/* Sort sections by address. */
5228
5229static int
217aa764 5230elf_sort_sections (const void *arg1, const void *arg2)
252b5132
RH
5231{
5232 const asection *sec1 = *(const asection **) arg1;
5233 const asection *sec2 = *(const asection **) arg2;
eecdbe52 5234 bfd_size_type size1, size2;
252b5132
RH
5235
5236 /* Sort by LMA first, since this is the address used to
5237 place the section into a segment. */
5238 if (sec1->lma < sec2->lma)
5239 return -1;
5240 else if (sec1->lma > sec2->lma)
5241 return 1;
5242
5243 /* Then sort by VMA. Normally the LMA and the VMA will be
5244 the same, and this will do nothing. */
5245 if (sec1->vma < sec2->vma)
5246 return -1;
5247 else if (sec1->vma > sec2->vma)
5248 return 1;
5249
5250 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5251
07c6e936 5252#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
252b5132
RH
5253
5254 if (TOEND (sec1))
5255 {
48db3297 5256 if (!TOEND (sec2))
252b5132
RH
5257 return 1;
5258 }
00a7cdc5 5259 else if (TOEND (sec2))
252b5132
RH
5260 return -1;
5261
5262#undef TOEND
5263
00a7cdc5
NC
5264 /* Sort by size, to put zero sized sections
5265 before others at the same address. */
252b5132 5266
eea6121a
AM
5267 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5268 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
eecdbe52
JJ
5269
5270 if (size1 < size2)
252b5132 5271 return -1;
eecdbe52 5272 if (size1 > size2)
252b5132
RH
5273 return 1;
5274
5275 return sec1->target_index - sec2->target_index;
5276}
5277
30fe1832
AM
5278/* This qsort comparison functions sorts PT_LOAD segments first and
5279 by p_paddr, for assign_file_positions_for_load_sections. */
5280
5281static int
5282elf_sort_segments (const void *arg1, const void *arg2)
5283{
5284 const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5285 const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5286
5287 if (m1->p_type != m2->p_type)
5288 {
5289 if (m1->p_type == PT_NULL)
5290 return 1;
5291 if (m2->p_type == PT_NULL)
5292 return -1;
5293 return m1->p_type < m2->p_type ? -1 : 1;
5294 }
5295 if (m1->includes_filehdr != m2->includes_filehdr)
5296 return m1->includes_filehdr ? -1 : 1;
5297 if (m1->no_sort_lma != m2->no_sort_lma)
5298 return m1->no_sort_lma ? -1 : 1;
5299 if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5300 {
4b3ecb3b 5301 bfd_vma lma1, lma2; /* Octets. */
30fe1832
AM
5302 lma1 = 0;
5303 if (m1->p_paddr_valid)
4b3ecb3b 5304 lma1 = m1->p_paddr;
30fe1832 5305 else if (m1->count != 0)
4b3ecb3b
AM
5306 {
5307 unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5308 m1->sections[0]);
5309 lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5310 }
30fe1832
AM
5311 lma2 = 0;
5312 if (m2->p_paddr_valid)
4b3ecb3b 5313 lma2 = m2->p_paddr;
30fe1832 5314 else if (m2->count != 0)
4b3ecb3b
AM
5315 {
5316 unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5317 m2->sections[0]);
5318 lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5319 }
30fe1832
AM
5320 if (lma1 != lma2)
5321 return lma1 < lma2 ? -1 : 1;
5322 }
5323 if (m1->idx != m2->idx)
5324 return m1->idx < m2->idx ? -1 : 1;
5325 return 0;
5326}
5327
340b6d91
AC
5328/* Ian Lance Taylor writes:
5329
5330 We shouldn't be using % with a negative signed number. That's just
5331 not good. We have to make sure either that the number is not
5332 negative, or that the number has an unsigned type. When the types
5333 are all the same size they wind up as unsigned. When file_ptr is a
5334 larger signed type, the arithmetic winds up as signed long long,
5335 which is wrong.
5336
5337 What we're trying to say here is something like ``increase OFF by
5338 the least amount that will cause it to be equal to the VMA modulo
5339 the page size.'' */
5340/* In other words, something like:
5341
5342 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5343 off_offset = off % bed->maxpagesize;
5344 if (vma_offset < off_offset)
5345 adjustment = vma_offset + bed->maxpagesize - off_offset;
5346 else
5347 adjustment = vma_offset - off_offset;
08a40648 5348
de194d85 5349 which can be collapsed into the expression below. */
340b6d91
AC
5350
5351static file_ptr
5352vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5353{
dc9155b2
NC
5354 /* PR binutils/16199: Handle an alignment of zero. */
5355 if (maxpagesize == 0)
5356 maxpagesize = 1;
340b6d91
AC
5357 return ((vma - off) % maxpagesize);
5358}
5359
6d33f217
L
5360static void
5361print_segment_map (const struct elf_segment_map *m)
5362{
5363 unsigned int j;
5364 const char *pt = get_segment_type (m->p_type);
5365 char buf[32];
5366
5367 if (pt == NULL)
5368 {
5369 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5370 sprintf (buf, "LOPROC+%7.7x",
5371 (unsigned int) (m->p_type - PT_LOPROC));
5372 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5373 sprintf (buf, "LOOS+%7.7x",
5374 (unsigned int) (m->p_type - PT_LOOS));
5375 else
5376 snprintf (buf, sizeof (buf), "%8.8x",
5377 (unsigned int) m->p_type);
5378 pt = buf;
5379 }
4a97a0e5 5380 fflush (stdout);
6d33f217
L
5381 fprintf (stderr, "%s:", pt);
5382 for (j = 0; j < m->count; j++)
5383 fprintf (stderr, " %s", m->sections [j]->name);
5384 putc ('\n',stderr);
4a97a0e5 5385 fflush (stderr);
6d33f217
L
5386}
5387
32812159
AM
5388static bfd_boolean
5389write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
5390{
5391 void *buf;
5392 bfd_boolean ret;
5393
5394 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5395 return FALSE;
5396 buf = bfd_zmalloc (len);
5397 if (buf == NULL)
5398 return FALSE;
5399 ret = bfd_bwrite (buf, len, abfd) == len;
5400 free (buf);
5401 return ret;
5402}
5403
252b5132
RH
5404/* Assign file positions to the sections based on the mapping from
5405 sections to segments. This function also sets up some fields in
f3520d2f 5406 the file header. */
252b5132 5407
b34976b6 5408static bfd_boolean
f3520d2f
AM
5409assign_file_positions_for_load_sections (bfd *abfd,
5410 struct bfd_link_info *link_info)
252b5132
RH
5411{
5412 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 5413 struct elf_segment_map *m;
30fe1832 5414 struct elf_segment_map *phdr_load_seg;
252b5132 5415 Elf_Internal_Phdr *phdrs;
252b5132 5416 Elf_Internal_Phdr *p;
502794d4 5417 file_ptr off; /* Octets. */
3f570048 5418 bfd_size_type maxpagesize;
30fe1832 5419 unsigned int alloc, actual;
0920dee7 5420 unsigned int i, j;
30fe1832 5421 struct elf_segment_map **sorted_seg_map;
502794d4 5422 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132 5423
e36284ab 5424 if (link_info == NULL
ceae84aa 5425 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
8ded5a0f 5426 return FALSE;
252b5132 5427
8ded5a0f 5428 alloc = 0;
12bd6957 5429 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
30fe1832 5430 m->idx = alloc++;
252b5132 5431
82f2dbf7
NC
5432 if (alloc)
5433 {
5434 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5435 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5436 }
5437 else
5438 {
5439 /* PR binutils/12467. */
5440 elf_elfheader (abfd)->e_phoff = 0;
5441 elf_elfheader (abfd)->e_phentsize = 0;
5442 }
d324f6d6 5443
8ded5a0f 5444 elf_elfheader (abfd)->e_phnum = alloc;
252b5132 5445
12bd6957 5446 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
30fe1832
AM
5447 {
5448 actual = alloc;
5449 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5450 }
8ded5a0f 5451 else
30fe1832
AM
5452 {
5453 actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5454 BFD_ASSERT (elf_program_header_size (abfd)
5455 == actual * bed->s->sizeof_phdr);
5456 BFD_ASSERT (actual >= alloc);
5457 }
252b5132
RH
5458
5459 if (alloc == 0)
f3520d2f 5460 {
12bd6957 5461 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
8ded5a0f 5462 return TRUE;
f3520d2f 5463 }
252b5132 5464
12bd6957 5465 /* We're writing the size in elf_program_header_size (abfd),
57268894
HPN
5466 see assign_file_positions_except_relocs, so make sure we have
5467 that amount allocated, with trailing space cleared.
12bd6957
AM
5468 The variable alloc contains the computed need, while
5469 elf_program_header_size (abfd) contains the size used for the
57268894
HPN
5470 layout.
5471 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5472 where the layout is forced to according to a larger size in the
5473 last iterations for the testcase ld-elf/header. */
30fe1832
AM
5474 phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
5475 + alloc * sizeof (*sorted_seg_map)));
5476 sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
f3520d2f 5477 elf_tdata (abfd)->phdr = phdrs;
252b5132 5478 if (phdrs == NULL)
b34976b6 5479 return FALSE;
252b5132 5480
30fe1832 5481 for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
252b5132 5482 {
30fe1832 5483 sorted_seg_map[j] = m;
252b5132 5484 /* If elf_segment_map is not from map_sections_to_segments, the
08a40648 5485 sections may not be correctly ordered. NOTE: sorting should
52e9b619
MS
5486 not be done to the PT_NOTE section of a corefile, which may
5487 contain several pseudo-sections artificially created by bfd.
5488 Sorting these pseudo-sections breaks things badly. */
47d9a591
AM
5489 if (m->count > 1
5490 && !(elf_elfheader (abfd)->e_type == ET_CORE
52e9b619 5491 && m->p_type == PT_NOTE))
48db3297
AM
5492 {
5493 for (i = 0; i < m->count; i++)
5494 m->sections[i]->target_index = i;
5495 qsort (m->sections, (size_t) m->count, sizeof (asection *),
5496 elf_sort_sections);
5497 }
30fe1832
AM
5498 }
5499 if (alloc > 1)
5500 qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
5501 elf_sort_segments);
5502
5503 maxpagesize = 1;
5504 if ((abfd->flags & D_PAGED) != 0)
5505 maxpagesize = bed->maxpagesize;
5506
5507 /* Sections must map to file offsets past the ELF file header. */
5508 off = bed->s->sizeof_ehdr;
5509 /* And if one of the PT_LOAD headers doesn't include the program
5510 headers then we'll be mapping program headers in the usual
5511 position after the ELF file header. */
5512 phdr_load_seg = NULL;
5513 for (j = 0; j < alloc; j++)
5514 {
5515 m = sorted_seg_map[j];
5516 if (m->p_type != PT_LOAD)
5517 break;
5518 if (m->includes_phdrs)
5519 {
5520 phdr_load_seg = m;
5521 break;
5522 }
5523 }
5524 if (phdr_load_seg == NULL)
5525 off += actual * bed->s->sizeof_phdr;
5526
5527 for (j = 0; j < alloc; j++)
5528 {
5529 asection **secpp;
502794d4 5530 bfd_vma off_adjust; /* Octets. */
30fe1832 5531 bfd_boolean no_contents;
252b5132 5532
b301b248
AM
5533 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5534 number of sections with contents contributing to both p_filesz
5535 and p_memsz, followed by a number of sections with no contents
5536 that just contribute to p_memsz. In this loop, OFF tracks next
02bf8d82 5537 available file offset for PT_LOAD and PT_NOTE segments. */
30fe1832
AM
5538 m = sorted_seg_map[j];
5539 p = phdrs + m->idx;
252b5132 5540 p->p_type = m->p_type;
28a7f3e7 5541 p->p_flags = m->p_flags;
252b5132 5542
3f570048 5543 if (m->count == 0)
502794d4 5544 p->p_vaddr = m->p_vaddr_offset * opb;
3f570048 5545 else
502794d4 5546 p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
3f570048
AM
5547
5548 if (m->p_paddr_valid)
5549 p->p_paddr = m->p_paddr;
5550 else if (m->count == 0)
5551 p->p_paddr = 0;
5552 else
502794d4 5553 p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
3f570048
AM
5554
5555 if (p->p_type == PT_LOAD
5556 && (abfd->flags & D_PAGED) != 0)
5557 {
5558 /* p_align in demand paged PT_LOAD segments effectively stores
5559 the maximum page size. When copying an executable with
5560 objcopy, we set m->p_align from the input file. Use this
5561 value for maxpagesize rather than bed->maxpagesize, which
5562 may be different. Note that we use maxpagesize for PT_TLS
5563 segment alignment later in this function, so we are relying
5564 on at least one PT_LOAD segment appearing before a PT_TLS
5565 segment. */
5566 if (m->p_align_valid)
5567 maxpagesize = m->p_align;
5568
5569 p->p_align = maxpagesize;
5570 }
3271a814
NS
5571 else if (m->p_align_valid)
5572 p->p_align = m->p_align;
e970b90a
DJ
5573 else if (m->count == 0)
5574 p->p_align = 1 << bed->s->log_file_align;
30fe1832
AM
5575
5576 if (m == phdr_load_seg)
5577 {
5578 if (!m->includes_filehdr)
5579 p->p_offset = off;
5580 off += actual * bed->s->sizeof_phdr;
5581 }
3f570048 5582
bf988460
AM
5583 no_contents = FALSE;
5584 off_adjust = 0;
252b5132 5585 if (p->p_type == PT_LOAD
b301b248 5586 && m->count > 0)
252b5132 5587 {
66631823 5588 bfd_size_type align; /* Bytes. */
a49e53ed 5589 unsigned int align_power = 0;
b301b248 5590
3271a814
NS
5591 if (m->p_align_valid)
5592 align = p->p_align;
5593 else
252b5132 5594 {
3271a814
NS
5595 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5596 {
5597 unsigned int secalign;
08a40648 5598
fd361982 5599 secalign = bfd_section_alignment (*secpp);
3271a814
NS
5600 if (secalign > align_power)
5601 align_power = secalign;
5602 }
5603 align = (bfd_size_type) 1 << align_power;
5604 if (align < maxpagesize)
5605 align = maxpagesize;
b301b248 5606 }
252b5132 5607
02bf8d82
AM
5608 for (i = 0; i < m->count; i++)
5609 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
5610 /* If we aren't making room for this section, then
5611 it must be SHT_NOBITS regardless of what we've
5612 set via struct bfd_elf_special_section. */
5613 elf_section_type (m->sections[i]) = SHT_NOBITS;
5614
bf988460 5615 /* Find out whether this segment contains any loadable
aea274d3
AM
5616 sections. */
5617 no_contents = TRUE;
5618 for (i = 0; i < m->count; i++)
5619 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
5620 {
5621 no_contents = FALSE;
5622 break;
5623 }
bf988460 5624
66631823 5625 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
a8c75b76
AM
5626
5627 /* Broken hardware and/or kernel require that files do not
5628 map the same page with different permissions on some hppa
5629 processors. */
30fe1832
AM
5630 if (j != 0
5631 && (abfd->flags & D_PAGED) != 0
a8c75b76
AM
5632 && bed->no_page_alias
5633 && (off & (maxpagesize - 1)) != 0
502794d4
CE
5634 && ((off & -maxpagesize)
5635 == ((off + off_adjust) & -maxpagesize)))
a8c75b76 5636 off_adjust += maxpagesize;
bf988460
AM
5637 off += off_adjust;
5638 if (no_contents)
5639 {
5640 /* We shouldn't need to align the segment on disk since
5641 the segment doesn't need file space, but the gABI
5642 arguably requires the alignment and glibc ld.so
5643 checks it. So to comply with the alignment
5644 requirement but not waste file space, we adjust
5645 p_offset for just this segment. (OFF_ADJUST is
5646 subtracted from OFF later.) This may put p_offset
5647 past the end of file, but that shouldn't matter. */
5648 }
5649 else
5650 off_adjust = 0;
252b5132 5651 }
b1a6d0b1
NC
5652 /* Make sure the .dynamic section is the first section in the
5653 PT_DYNAMIC segment. */
5654 else if (p->p_type == PT_DYNAMIC
5655 && m->count > 1
5656 && strcmp (m->sections[0]->name, ".dynamic") != 0)
5657 {
5658 _bfd_error_handler
871b3ab2 5659 (_("%pB: The first section in the PT_DYNAMIC segment"
63a5468a 5660 " is not the .dynamic section"),
b301b248 5661 abfd);
b1a6d0b1
NC
5662 bfd_set_error (bfd_error_bad_value);
5663 return FALSE;
5664 }
3f001e84
JK
5665 /* Set the note section type to SHT_NOTE. */
5666 else if (p->p_type == PT_NOTE)
5667 for (i = 0; i < m->count; i++)
5668 elf_section_type (m->sections[i]) = SHT_NOTE;
252b5132 5669
252b5132
RH
5670 if (m->includes_filehdr)
5671 {
bf988460 5672 if (!m->p_flags_valid)
252b5132 5673 p->p_flags |= PF_R;
252b5132
RH
5674 p->p_filesz = bed->s->sizeof_ehdr;
5675 p->p_memsz = bed->s->sizeof_ehdr;
30fe1832 5676 if (p->p_type == PT_LOAD)
252b5132 5677 {
30fe1832 5678 if (m->count > 0)
252b5132 5679 {
30fe1832
AM
5680 if (p->p_vaddr < (bfd_vma) off
5681 || (!m->p_paddr_valid
5682 && p->p_paddr < (bfd_vma) off))
5683 {
5684 _bfd_error_handler
5685 (_("%pB: not enough room for program headers,"
5686 " try linking with -N"),
5687 abfd);
5688 bfd_set_error (bfd_error_bad_value);
5689 return FALSE;
5690 }
5691 p->p_vaddr -= off;
5692 if (!m->p_paddr_valid)
5693 p->p_paddr -= off;
252b5132 5694 }
30fe1832
AM
5695 }
5696 else if (sorted_seg_map[0]->includes_filehdr)
5697 {
5698 Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
5699 p->p_vaddr = filehdr->p_vaddr;
bf988460 5700 if (!m->p_paddr_valid)
30fe1832 5701 p->p_paddr = filehdr->p_paddr;
252b5132 5702 }
252b5132
RH
5703 }
5704
5705 if (m->includes_phdrs)
5706 {
bf988460 5707 if (!m->p_flags_valid)
252b5132 5708 p->p_flags |= PF_R;
30fe1832
AM
5709 p->p_filesz += actual * bed->s->sizeof_phdr;
5710 p->p_memsz += actual * bed->s->sizeof_phdr;
f3520d2f 5711 if (!m->includes_filehdr)
252b5132 5712 {
30fe1832 5713 if (p->p_type == PT_LOAD)
252b5132 5714 {
30fe1832
AM
5715 elf_elfheader (abfd)->e_phoff = p->p_offset;
5716 if (m->count > 0)
5717 {
5718 p->p_vaddr -= off - p->p_offset;
5719 if (!m->p_paddr_valid)
5720 p->p_paddr -= off - p->p_offset;
5721 }
5722 }
5723 else if (phdr_load_seg != NULL)
5724 {
5725 Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
502794d4 5726 bfd_vma phdr_off = 0; /* Octets. */
30fe1832
AM
5727 if (phdr_load_seg->includes_filehdr)
5728 phdr_off = bed->s->sizeof_ehdr;
5729 p->p_vaddr = phdr->p_vaddr + phdr_off;
bf988460 5730 if (!m->p_paddr_valid)
30fe1832
AM
5731 p->p_paddr = phdr->p_paddr + phdr_off;
5732 p->p_offset = phdr->p_offset + phdr_off;
252b5132 5733 }
30fe1832
AM
5734 else
5735 p->p_offset = bed->s->sizeof_ehdr;
2b0bc088 5736 }
252b5132
RH
5737 }
5738
5739 if (p->p_type == PT_LOAD
5740 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5741 {
bf988460 5742 if (!m->includes_filehdr && !m->includes_phdrs)
0bc3450e
AM
5743 {
5744 p->p_offset = off;
5745 if (no_contents)
67641dd3
AM
5746 {
5747 /* Put meaningless p_offset for PT_LOAD segments
5748 without file contents somewhere within the first
5749 page, in an attempt to not point past EOF. */
5750 bfd_size_type align = maxpagesize;
5751 if (align < p->p_align)
5752 align = p->p_align;
5753 if (align < 1)
5754 align = 1;
5755 p->p_offset = off % align;
5756 }
0bc3450e 5757 }
252b5132
RH
5758 else
5759 {
502794d4 5760 file_ptr adjust; /* Octets. */
252b5132
RH
5761
5762 adjust = off - (p->p_offset + p->p_filesz);
bf988460
AM
5763 if (!no_contents)
5764 p->p_filesz += adjust;
252b5132
RH
5765 p->p_memsz += adjust;
5766 }
5767 }
5768
1ea63fd2
AM
5769 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5770 maps. Set filepos for sections in PT_LOAD segments, and in
5771 core files, for sections in PT_NOTE segments.
5772 assign_file_positions_for_non_load_sections will set filepos
5773 for other sections and update p_filesz for other segments. */
252b5132
RH
5774 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5775 {
5776 asection *sec;
252b5132 5777 bfd_size_type align;
627b32bc 5778 Elf_Internal_Shdr *this_hdr;
252b5132
RH
5779
5780 sec = *secpp;
02bf8d82 5781 this_hdr = &elf_section_data (sec)->this_hdr;
fd361982 5782 align = (bfd_size_type) 1 << bfd_section_alignment (sec);
252b5132 5783
88967714
AM
5784 if ((p->p_type == PT_LOAD
5785 || p->p_type == PT_TLS)
5786 && (this_hdr->sh_type != SHT_NOBITS
5787 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5788 && ((this_hdr->sh_flags & SHF_TLS) == 0
5789 || p->p_type == PT_TLS))))
252b5132 5790 {
502794d4
CE
5791 bfd_vma p_start = p->p_paddr; /* Octets. */
5792 bfd_vma p_end = p_start + p->p_memsz; /* Octets. */
5793 bfd_vma s_start = sec->lma * opb; /* Octets. */
5794 bfd_vma adjust = s_start - p_end; /* Octets. */
252b5132 5795
a2d1e028
L
5796 if (adjust != 0
5797 && (s_start < p_end
5798 || p_end < p_start))
252b5132 5799 {
4eca0228 5800 _bfd_error_handler
695344c0 5801 /* xgettext:c-format */
2dcf00ce 5802 (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64),
502794d4
CE
5803 abfd, sec, (uint64_t) s_start / opb,
5804 (uint64_t) p_end / opb);
88967714 5805 adjust = 0;
502794d4 5806 sec->lma = p_end / opb;
1cfb7d1e 5807 }
3ac9b6c9 5808 p->p_memsz += adjust;
1cfb7d1e 5809
d16e3d2e 5810 if (p->p_type == PT_LOAD)
88967714 5811 {
d16e3d2e 5812 if (this_hdr->sh_type != SHT_NOBITS)
32812159 5813 {
d16e3d2e 5814 off_adjust = 0;
30fe1832
AM
5815 if (p->p_filesz + adjust < p->p_memsz)
5816 {
5817 /* We have a PROGBITS section following NOBITS ones.
5818 Allocate file space for the NOBITS section(s) and
5819 zero it. */
5820 adjust = p->p_memsz - p->p_filesz;
5821 if (!write_zeros (abfd, off, adjust))
5822 return FALSE;
5823 }
d16e3d2e
AM
5824 }
5825 /* We only adjust sh_offset in SHT_NOBITS sections
5826 as would seem proper for their address when the
5827 section is first in the segment. sh_offset
5828 doesn't really have any significance for
5829 SHT_NOBITS anyway, apart from a notional position
5830 relative to other sections. Historically we
5831 didn't bother with adjusting sh_offset and some
5832 programs depend on it not being adjusted. See
5833 pr12921 and pr25662. */
5834 if (this_hdr->sh_type != SHT_NOBITS || i == 0)
5835 {
30fe1832 5836 off += adjust;
d16e3d2e
AM
5837 if (this_hdr->sh_type == SHT_NOBITS)
5838 off_adjust += adjust;
32812159 5839 }
252b5132 5840 }
d16e3d2e
AM
5841 if (this_hdr->sh_type != SHT_NOBITS)
5842 p->p_filesz += adjust;
252b5132
RH
5843 }
5844
5845 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5846 {
b301b248
AM
5847 /* The section at i == 0 is the one that actually contains
5848 everything. */
4a938328
MS
5849 if (i == 0)
5850 {
627b32bc 5851 this_hdr->sh_offset = sec->filepos = off;
6a3cd2b4
AM
5852 off += this_hdr->sh_size;
5853 p->p_filesz = this_hdr->sh_size;
b301b248
AM
5854 p->p_memsz = 0;
5855 p->p_align = 1;
252b5132 5856 }
4a938328 5857 else
252b5132 5858 {
b301b248 5859 /* The rest are fake sections that shouldn't be written. */
252b5132 5860 sec->filepos = 0;
eea6121a 5861 sec->size = 0;
b301b248
AM
5862 sec->flags = 0;
5863 continue;
252b5132 5864 }
252b5132
RH
5865 }
5866 else
5867 {
1e951488 5868 if (p->p_type == PT_LOAD)
b301b248 5869 {
1e951488
AM
5870 this_hdr->sh_offset = sec->filepos = off;
5871 if (this_hdr->sh_type != SHT_NOBITS)
5872 off += this_hdr->sh_size;
5873 }
5874 else if (this_hdr->sh_type == SHT_NOBITS
5875 && (this_hdr->sh_flags & SHF_TLS) != 0
5876 && this_hdr->sh_offset == 0)
5877 {
5878 /* This is a .tbss section that didn't get a PT_LOAD.
5879 (See _bfd_elf_map_sections_to_segments "Create a
5880 final PT_LOAD".) Set sh_offset to the value it
5881 would have if we had created a zero p_filesz and
5882 p_memsz PT_LOAD header for the section. This
5883 also makes the PT_TLS header have the same
5884 p_offset value. */
5885 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5886 off, align);
5887 this_hdr->sh_offset = sec->filepos = off + adjust;
b301b248 5888 }
252b5132 5889
02bf8d82 5890 if (this_hdr->sh_type != SHT_NOBITS)
b301b248 5891 {
6a3cd2b4 5892 p->p_filesz += this_hdr->sh_size;
02bf8d82
AM
5893 /* A load section without SHF_ALLOC is something like
5894 a note section in a PT_NOTE segment. These take
5895 file space but are not loaded into memory. */
5896 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6a3cd2b4 5897 p->p_memsz += this_hdr->sh_size;
b301b248 5898 }
6a3cd2b4 5899 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
13ae64f3 5900 {
6a3cd2b4
AM
5901 if (p->p_type == PT_TLS)
5902 p->p_memsz += this_hdr->sh_size;
5903
5904 /* .tbss is special. It doesn't contribute to p_memsz of
5905 normal segments. */
5906 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
5907 p->p_memsz += this_hdr->sh_size;
13ae64f3
JJ
5908 }
5909
b10a8ae0
L
5910 if (align > p->p_align
5911 && !m->p_align_valid
5912 && (p->p_type != PT_LOAD
5913 || (abfd->flags & D_PAGED) == 0))
252b5132
RH
5914 p->p_align = align;
5915 }
5916
bf988460 5917 if (!m->p_flags_valid)
252b5132
RH
5918 {
5919 p->p_flags |= PF_R;
02bf8d82 5920 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
252b5132 5921 p->p_flags |= PF_X;
02bf8d82 5922 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
252b5132
RH
5923 p->p_flags |= PF_W;
5924 }
5925 }
43a8475c 5926
bf988460 5927 off -= off_adjust;
0920dee7 5928
30fe1832
AM
5929 /* PR ld/20815 - Check that the program header segment, if
5930 present, will be loaded into memory. */
5931 if (p->p_type == PT_PHDR
5932 && phdr_load_seg == NULL
5933 && !(bed->elf_backend_allow_non_load_phdr != NULL
5934 && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
5935 {
5936 /* The fix for this error is usually to edit the linker script being
5937 used and set up the program headers manually. Either that or
5938 leave room for the headers at the start of the SECTIONS. */
5939 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
5940 " by LOAD segment"),
5941 abfd);
7b3c2715
AM
5942 if (link_info == NULL)
5943 return FALSE;
5944 /* Arrange for the linker to exit with an error, deleting
5945 the output file unless --noinhibit-exec is given. */
5946 link_info->callbacks->info ("%X");
30fe1832
AM
5947 }
5948
7c928300
AM
5949 /* Check that all sections are in a PT_LOAD segment.
5950 Don't check funky gdb generated core files. */
5951 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
9a83a553
AM
5952 {
5953 bfd_boolean check_vma = TRUE;
5954
5955 for (i = 1; i < m->count; i++)
5956 if (m->sections[i]->vma == m->sections[i - 1]->vma
5957 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
5958 ->this_hdr), p) != 0
5959 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
5960 ->this_hdr), p) != 0)
0920dee7 5961 {
9a83a553
AM
5962 /* Looks like we have overlays packed into the segment. */
5963 check_vma = FALSE;
5964 break;
0920dee7 5965 }
9a83a553
AM
5966
5967 for (i = 0; i < m->count; i++)
5968 {
5969 Elf_Internal_Shdr *this_hdr;
5970 asection *sec;
5971
5972 sec = m->sections[i];
5973 this_hdr = &(elf_section_data(sec)->this_hdr);
86b2281f
AM
5974 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
5975 && !ELF_TBSS_SPECIAL (this_hdr, p))
9a83a553 5976 {
4eca0228 5977 _bfd_error_handler
695344c0 5978 /* xgettext:c-format */
871b3ab2 5979 (_("%pB: section `%pA' can't be allocated in segment %d"),
9a83a553
AM
5980 abfd, sec, j);
5981 print_segment_map (m);
5982 }
5983 }
5984 }
252b5132
RH
5985 }
5986
12bd6957 5987 elf_next_file_pos (abfd) = off;
30fe1832
AM
5988
5989 if (link_info != NULL
5990 && phdr_load_seg != NULL
5991 && phdr_load_seg->includes_filehdr)
5992 {
5993 /* There is a segment that contains both the file headers and the
5994 program headers, so provide a symbol __ehdr_start pointing there.
5995 A program can use this to examine itself robustly. */
5996
5997 struct elf_link_hash_entry *hash
5998 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
5999 FALSE, FALSE, TRUE);
6000 /* If the symbol was referenced and not defined, define it. */
6001 if (hash != NULL
6002 && (hash->root.type == bfd_link_hash_new
6003 || hash->root.type == bfd_link_hash_undefined
6004 || hash->root.type == bfd_link_hash_undefweak
6005 || hash->root.type == bfd_link_hash_common))
6006 {
6007 asection *s = NULL;
66631823 6008 bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
30fe1832
AM
6009
6010 if (phdr_load_seg->count != 0)
6011 /* The segment contains sections, so use the first one. */
6012 s = phdr_load_seg->sections[0];
6013 else
6014 /* Use the first (i.e. lowest-addressed) section in any segment. */
6015 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6016 if (m->p_type == PT_LOAD && m->count != 0)
6017 {
6018 s = m->sections[0];
6019 break;
6020 }
6021
6022 if (s != NULL)
6023 {
6024 hash->root.u.def.value = filehdr_vaddr - s->vma;
6025 hash->root.u.def.section = s;
6026 }
6027 else
6028 {
6029 hash->root.u.def.value = filehdr_vaddr;
6030 hash->root.u.def.section = bfd_abs_section_ptr;
6031 }
6032
6033 hash->root.type = bfd_link_hash_defined;
6034 hash->def_regular = 1;
6035 hash->non_elf = 0;
6036 }
6037 }
6038
f3520d2f
AM
6039 return TRUE;
6040}
6041
1faa385f
NC
6042/* Determine if a bfd is a debuginfo file. Unfortunately there
6043 is no defined method for detecting such files, so we have to
6044 use heuristics instead. */
6045
6046bfd_boolean
6047is_debuginfo_file (bfd *abfd)
6048{
6049 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6050 return FALSE;
6051
6052 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6053 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6054 Elf_Internal_Shdr **headerp;
6055
6056 for (headerp = start_headers; headerp < end_headers; headerp ++)
6057 {
6058 Elf_Internal_Shdr *header = * headerp;
6059
6060 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6061 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6062 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6063 && header->sh_type != SHT_NOBITS
6064 && header->sh_type != SHT_NOTE)
6065 return FALSE;
6066 }
6067
6068 return TRUE;
6069}
6070
1ff6de03
NA
6071/* Assign file positions for the other sections, except for compressed debugging
6072 and other sections assigned in _bfd_elf_assign_file_positions_for_non_load(). */
f3520d2f
AM
6073
6074static bfd_boolean
6075assign_file_positions_for_non_load_sections (bfd *abfd,
6076 struct bfd_link_info *link_info)
6077{
6078 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6079 Elf_Internal_Shdr **i_shdrpp;
e06efbf1 6080 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
f3520d2f
AM
6081 Elf_Internal_Phdr *phdrs;
6082 Elf_Internal_Phdr *p;
6083 struct elf_segment_map *m;
f3520d2f 6084 file_ptr off;
66631823 6085 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
f3520d2f 6086
5c182d5f 6087 i_shdrpp = elf_elfsections (abfd);
e06efbf1 6088 end_hdrpp = i_shdrpp + elf_numsections (abfd);
12bd6957 6089 off = elf_next_file_pos (abfd);
e06efbf1 6090 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
5c182d5f 6091 {
5c182d5f
AM
6092 Elf_Internal_Shdr *hdr;
6093
6094 hdr = *hdrpp;
6095 if (hdr->bfd_section != NULL
252e386e
AM
6096 && (hdr->bfd_section->filepos != 0
6097 || (hdr->sh_type == SHT_NOBITS
6098 && hdr->contents == NULL)))
627b32bc 6099 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
5c182d5f
AM
6100 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6101 {
1faa385f
NC
6102 if (hdr->sh_size != 0
6103 /* PR 24717 - debuginfo files are known to be not strictly
6104 compliant with the ELF standard. In particular they often
6105 have .note.gnu.property sections that are outside of any
6106 loadable segment. This is not a problem for such files,
6107 so do not warn about them. */
6108 && ! is_debuginfo_file (abfd))
4eca0228 6109 _bfd_error_handler
695344c0 6110 /* xgettext:c-format */
871b3ab2 6111 (_("%pB: warning: allocated section `%s' not in segment"),
e8d2ba53
AM
6112 abfd,
6113 (hdr->bfd_section == NULL
6114 ? "*unknown*"
6115 : hdr->bfd_section->name));
3ba71138
L
6116 /* We don't need to page align empty sections. */
6117 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
5c182d5f
AM
6118 off += vma_page_aligned_bias (hdr->sh_addr, off,
6119 bed->maxpagesize);
6120 else
6121 off += vma_page_aligned_bias (hdr->sh_addr, off,
6122 hdr->sh_addralign);
6123 off = _bfd_elf_assign_file_position_for_section (hdr, off,
6124 FALSE);
6125 }
6126 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6127 && hdr->bfd_section == NULL)
1ff6de03
NA
6128 /* We don't know the offset of these sections yet: their size has
6129 not been decided. */
0ce398f1 6130 || (hdr->bfd_section != NULL
1ff6de03
NA
6131 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
6132 || (bfd_section_is_ctf (hdr->bfd_section)
6133 && abfd->is_linker_output)))
12bd6957 6134 || hdr == i_shdrpp[elf_onesymtab (abfd)]
6a40cf0c
NC
6135 || (elf_symtab_shndx_list (abfd) != NULL
6136 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
3e19fb8f
L
6137 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6138 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
5c182d5f
AM
6139 hdr->sh_offset = -1;
6140 else
6141 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5c182d5f 6142 }
30fe1832 6143 elf_next_file_pos (abfd) = off;
5c182d5f 6144
252b5132
RH
6145 /* Now that we have set the section file positions, we can set up
6146 the file positions for the non PT_LOAD segments. */
f3520d2f 6147 phdrs = elf_tdata (abfd)->phdr;
12bd6957 6148 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
252b5132 6149 {
129af99f 6150 if (p->p_type == PT_GNU_RELRO)
252b5132 6151 {
66631823 6152 bfd_vma start, end; /* Bytes. */
01f7e10c 6153 bfd_boolean ok;
1ea63fd2 6154
129af99f 6155 if (link_info != NULL)
8c37241b 6156 {
129af99f 6157 /* During linking the range of the RELRO segment is passed
f2731e0c
AM
6158 in link_info. Note that there may be padding between
6159 relro_start and the first RELRO section. */
6160 start = link_info->relro_start;
6161 end = link_info->relro_end;
6162 }
6163 else if (m->count != 0)
6164 {
6165 if (!m->p_size_valid)
6166 abort ();
6167 start = m->sections[0]->vma;
66631823 6168 end = start + m->p_size / opb;
f2731e0c
AM
6169 }
6170 else
6171 {
6172 start = 0;
6173 end = 0;
6174 }
6175
01f7e10c 6176 ok = FALSE;
f2731e0c
AM
6177 if (start < end)
6178 {
6179 struct elf_segment_map *lm;
6180 const Elf_Internal_Phdr *lp;
6181 unsigned int i;
6182
6183 /* Find a LOAD segment containing a section in the RELRO
6184 segment. */
12bd6957 6185 for (lm = elf_seg_map (abfd), lp = phdrs;
3146fac4
AM
6186 lm != NULL;
6187 lm = lm->next, lp++)
8c37241b
JJ
6188 {
6189 if (lp->p_type == PT_LOAD
3146fac4 6190 && lm->count != 0
dbc88fc1
AM
6191 && (lm->sections[lm->count - 1]->vma
6192 + (!IS_TBSS (lm->sections[lm->count - 1])
66631823 6193 ? lm->sections[lm->count - 1]->size / opb
dbc88fc1 6194 : 0)) > start
f2731e0c 6195 && lm->sections[0]->vma < end)
8c37241b
JJ
6196 break;
6197 }
f2731e0c 6198
01f7e10c 6199 if (lm != NULL)
129af99f 6200 {
01f7e10c
AM
6201 /* Find the section starting the RELRO segment. */
6202 for (i = 0; i < lm->count; i++)
6203 {
6204 asection *s = lm->sections[i];
6205 if (s->vma >= start
6206 && s->vma < end
6207 && s->size != 0)
6208 break;
6209 }
6210
6211 if (i < lm->count)
6212 {
502794d4
CE
6213 p->p_vaddr = lm->sections[i]->vma * opb;
6214 p->p_paddr = lm->sections[i]->lma * opb;
01f7e10c 6215 p->p_offset = lm->sections[i]->filepos;
66631823 6216 p->p_memsz = end * opb - p->p_vaddr;
01f7e10c
AM
6217 p->p_filesz = p->p_memsz;
6218
6219 /* The RELRO segment typically ends a few bytes
6220 into .got.plt but other layouts are possible.
6221 In cases where the end does not match any
6222 loaded section (for instance is in file
6223 padding), trim p_filesz back to correspond to
6224 the end of loaded section contents. */
6225 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6226 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6227
6228 /* Preserve the alignment and flags if they are
6229 valid. The gold linker generates RW/4 for
6230 the PT_GNU_RELRO section. It is better for
6231 objcopy/strip to honor these attributes
6232 otherwise gdb will choke when using separate
6233 debug files. */
6234 if (!m->p_align_valid)
6235 p->p_align = 1;
6236 if (!m->p_flags_valid)
6237 p->p_flags = PF_R;
6238 ok = TRUE;
6239 }
129af99f 6240 }
b84a33b5 6241 }
01f7e10c
AM
6242 if (link_info != NULL)
6243 BFD_ASSERT (ok);
6244 if (!ok)
6245 memset (p, 0, sizeof *p);
129af99f 6246 }
04c3a755
NS
6247 else if (p->p_type == PT_GNU_STACK)
6248 {
6249 if (m->p_size_valid)
6250 p->p_memsz = m->p_size;
6251 }
129af99f
AS
6252 else if (m->count != 0)
6253 {
e06efbf1 6254 unsigned int i;
1a9ccd70 6255
129af99f
AS
6256 if (p->p_type != PT_LOAD
6257 && (p->p_type != PT_NOTE
6258 || bfd_get_format (abfd) != bfd_core))
6259 {
1a9ccd70
NC
6260 /* A user specified segment layout may include a PHDR
6261 segment that overlaps with a LOAD segment... */
6262 if (p->p_type == PT_PHDR)
6263 {
6264 m->count = 0;
6265 continue;
6266 }
6267
c86934ce
NC
6268 if (m->includes_filehdr || m->includes_phdrs)
6269 {
b1fa9dd6 6270 /* PR 17512: file: 2195325e. */
4eca0228 6271 _bfd_error_handler
871b3ab2 6272 (_("%pB: error: non-load segment %d includes file header "
76cfced5
AM
6273 "and/or program header"),
6274 abfd, (int) (p - phdrs));
c86934ce
NC
6275 return FALSE;
6276 }
129af99f 6277
86b2281f 6278 p->p_filesz = 0;
129af99f 6279 p->p_offset = m->sections[0]->filepos;
86b2281f
AM
6280 for (i = m->count; i-- != 0;)
6281 {
6282 asection *sect = m->sections[i];
6283 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6284 if (hdr->sh_type != SHT_NOBITS)
6285 {
6286 p->p_filesz = (sect->filepos - m->sections[0]->filepos
6287 + hdr->sh_size);
6288 break;
6289 }
6290 }
129af99f
AS
6291 }
6292 }
252b5132
RH
6293 }
6294
b34976b6 6295 return TRUE;
252b5132
RH
6296}
6297
6a40cf0c
NC
6298static elf_section_list *
6299find_section_in_list (unsigned int i, elf_section_list * list)
6300{
6301 for (;list != NULL; list = list->next)
6302 if (list->ndx == i)
6303 break;
6304 return list;
6305}
6306
252b5132
RH
6307/* Work out the file positions of all the sections. This is called by
6308 _bfd_elf_compute_section_file_positions. All the section sizes and
6309 VMAs must be known before this is called.
6310
e0638f70 6311 Reloc sections come in two flavours: Those processed specially as
1ff6de03
NA
6312 "side-channel" data attached to a section to which they apply, and those that
6313 bfd doesn't process as relocations. The latter sort are stored in a normal
6314 bfd section by bfd_section_from_shdr. We don't consider the former sort
6315 here, unless they form part of the loadable image. Reloc sections not
6316 assigned here (and compressed debugging sections and CTF sections which
6317 nothing else in the file can rely upon) will be handled later by
e0638f70 6318 assign_file_positions_for_relocs.
252b5132
RH
6319
6320 We also don't set the positions of the .symtab and .strtab here. */
6321
b34976b6 6322static bfd_boolean
c84fca4d
AO
6323assign_file_positions_except_relocs (bfd *abfd,
6324 struct bfd_link_info *link_info)
252b5132 6325{
5c182d5f
AM
6326 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6327 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
9c5bfbb7 6328 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6d6c25c8 6329 unsigned int alloc;
252b5132
RH
6330
6331 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6332 && bfd_get_format (abfd) != bfd_core)
6333 {
5c182d5f
AM
6334 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6335 unsigned int num_sec = elf_numsections (abfd);
252b5132
RH
6336 Elf_Internal_Shdr **hdrpp;
6337 unsigned int i;
a485e98e 6338 file_ptr off;
252b5132
RH
6339
6340 /* Start after the ELF header. */
6341 off = i_ehdrp->e_ehsize;
6342
6343 /* We are not creating an executable, which means that we are
6344 not creating a program header, and that the actual order of
6345 the sections in the file is unimportant. */
9ad5cbcf 6346 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
252b5132
RH
6347 {
6348 Elf_Internal_Shdr *hdr;
6349
6350 hdr = *hdrpp;
e0638f70
AM
6351 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6352 && hdr->bfd_section == NULL)
1ff6de03
NA
6353 /* Do not assign offsets for these sections yet: we don't know
6354 their sizes. */
0ce398f1 6355 || (hdr->bfd_section != NULL
1ff6de03
NA
6356 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
6357 || (bfd_section_is_ctf (hdr->bfd_section)
6358 && abfd->is_linker_output)))
12bd6957 6359 || i == elf_onesymtab (abfd)
6a40cf0c
NC
6360 || (elf_symtab_shndx_list (abfd) != NULL
6361 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
3e19fb8f
L
6362 || i == elf_strtab_sec (abfd)
6363 || i == elf_shstrtab_sec (abfd))
252b5132
RH
6364 {
6365 hdr->sh_offset = -1;
252b5132 6366 }
9ad5cbcf 6367 else
b34976b6 6368 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
252b5132 6369 }
a485e98e
AM
6370
6371 elf_next_file_pos (abfd) = off;
6d6c25c8 6372 elf_program_header_size (abfd) = 0;
252b5132
RH
6373 }
6374 else
6375 {
252b5132 6376 /* Assign file positions for the loaded sections based on the
08a40648 6377 assignment of sections to segments. */
f3520d2f
AM
6378 if (!assign_file_positions_for_load_sections (abfd, link_info))
6379 return FALSE;
6380
6381 /* And for non-load sections. */
6382 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6383 return FALSE;
6d6c25c8 6384 }
f3520d2f 6385
6d6c25c8
AM
6386 if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
6387 return FALSE;
1a9ccd70 6388
6d6c25c8
AM
6389 /* Write out the program headers. */
6390 alloc = i_ehdrp->e_phnum;
6391 if (alloc != 0)
6392 {
30fe1832 6393 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
cd584857
NC
6394 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6395 return FALSE;
252b5132
RH
6396 }
6397
b34976b6 6398 return TRUE;
252b5132
RH
6399}
6400
ed7e9d0b
AM
6401bfd_boolean
6402_bfd_elf_init_file_header (bfd *abfd,
6403 struct bfd_link_info *info ATTRIBUTE_UNUSED)
252b5132 6404{
3d540e93 6405 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
2b0f7ef9 6406 struct elf_strtab_hash *shstrtab;
9c5bfbb7 6407 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
6408
6409 i_ehdrp = elf_elfheader (abfd);
252b5132 6410
2b0f7ef9 6411 shstrtab = _bfd_elf_strtab_init ();
252b5132 6412 if (shstrtab == NULL)
b34976b6 6413 return FALSE;
252b5132
RH
6414
6415 elf_shstrtab (abfd) = shstrtab;
6416
6417 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6418 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6419 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6420 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6421
6422 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6423 i_ehdrp->e_ident[EI_DATA] =
6424 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6425 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6426
252b5132
RH
6427 if ((abfd->flags & DYNAMIC) != 0)
6428 i_ehdrp->e_type = ET_DYN;
6429 else if ((abfd->flags & EXEC_P) != 0)
6430 i_ehdrp->e_type = ET_EXEC;
6431 else if (bfd_get_format (abfd) == bfd_core)
6432 i_ehdrp->e_type = ET_CORE;
6433 else
6434 i_ehdrp->e_type = ET_REL;
6435
6436 switch (bfd_get_arch (abfd))
6437 {
6438 case bfd_arch_unknown:
6439 i_ehdrp->e_machine = EM_NONE;
6440 break;
aa4f99bb
AO
6441
6442 /* There used to be a long list of cases here, each one setting
6443 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6444 in the corresponding bfd definition. To avoid duplication,
6445 the switch was removed. Machines that need special handling
6446 can generally do it in elf_backend_final_write_processing(),
6447 unless they need the information earlier than the final write.
6448 Such need can generally be supplied by replacing the tests for
6449 e_machine with the conditions used to determine it. */
252b5132 6450 default:
9c5bfbb7
AM
6451 i_ehdrp->e_machine = bed->elf_machine_code;
6452 }
aa4f99bb 6453
252b5132
RH
6454 i_ehdrp->e_version = bed->s->ev_current;
6455 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6456
c044fabd 6457 /* No program header, for now. */
252b5132
RH
6458 i_ehdrp->e_phoff = 0;
6459 i_ehdrp->e_phentsize = 0;
6460 i_ehdrp->e_phnum = 0;
6461
c044fabd 6462 /* Each bfd section is section header entry. */
252b5132
RH
6463 i_ehdrp->e_entry = bfd_get_start_address (abfd);
6464 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6465
252b5132 6466 elf_tdata (abfd)->symtab_hdr.sh_name =
b34976b6 6467 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
252b5132 6468 elf_tdata (abfd)->strtab_hdr.sh_name =
b34976b6 6469 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
252b5132 6470 elf_tdata (abfd)->shstrtab_hdr.sh_name =
b34976b6 6471 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
252b5132 6472 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
17ca87fc 6473 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
252b5132 6474 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
b34976b6 6475 return FALSE;
252b5132 6476
b34976b6 6477 return TRUE;
252b5132
RH
6478}
6479
6d6c25c8
AM
6480/* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
6481
6482 FIXME: We used to have code here to sort the PT_LOAD segments into
6483 ascending order, as per the ELF spec. But this breaks some programs,
6484 including the Linux kernel. But really either the spec should be
6485 changed or the programs updated. */
6486
6487bfd_boolean
6488_bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
6489{
6490 if (link_info != NULL && bfd_link_pie (link_info))
6491 {
6492 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
6493 unsigned int num_segments = i_ehdrp->e_phnum;
6494 struct elf_obj_tdata *tdata = elf_tdata (obfd);
6495 Elf_Internal_Phdr *segment = tdata->phdr;
6496 Elf_Internal_Phdr *end_segment = &segment[num_segments];
6497
6498 /* Find the lowest p_vaddr in PT_LOAD segments. */
6499 bfd_vma p_vaddr = (bfd_vma) -1;
6500 for (; segment < end_segment; segment++)
6501 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
6502 p_vaddr = segment->p_vaddr;
6503
6504 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6505 segments is non-zero. */
6506 if (p_vaddr)
6507 i_ehdrp->e_type = ET_EXEC;
6508 }
6509 return TRUE;
6510}
6511
252b5132 6512/* Assign file positions for all the reloc sections which are not part
a485e98e 6513 of the loadable file image, and the file position of section headers. */
252b5132 6514
0ce398f1
L
6515static bfd_boolean
6516_bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
252b5132
RH
6517{
6518 file_ptr off;
e06efbf1 6519 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
3e19fb8f 6520 Elf_Internal_Shdr *shdrp;
a485e98e
AM
6521 Elf_Internal_Ehdr *i_ehdrp;
6522 const struct elf_backend_data *bed;
252b5132 6523
12bd6957 6524 off = elf_next_file_pos (abfd);
252b5132 6525
e06efbf1
L
6526 shdrpp = elf_elfsections (abfd);
6527 end_shdrpp = shdrpp + elf_numsections (abfd);
6528 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
252b5132 6529 {
252b5132 6530 shdrp = *shdrpp;
0ce398f1
L
6531 if (shdrp->sh_offset == -1)
6532 {
3e19fb8f 6533 asection *sec = shdrp->bfd_section;
0ce398f1
L
6534 bfd_boolean is_rel = (shdrp->sh_type == SHT_REL
6535 || shdrp->sh_type == SHT_RELA);
1ff6de03 6536 bfd_boolean is_ctf = sec && bfd_section_is_ctf (sec);
0ce398f1 6537 if (is_rel
1ff6de03 6538 || is_ctf
3e19fb8f 6539 || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
0ce398f1 6540 {
1ff6de03 6541 if (!is_rel && !is_ctf)
0ce398f1 6542 {
3e19fb8f
L
6543 const char *name = sec->name;
6544 struct bfd_elf_section_data *d;
6545
0ce398f1 6546 /* Compress DWARF debug sections. */
3e19fb8f 6547 if (!bfd_compress_section (abfd, sec,
0ce398f1
L
6548 shdrp->contents))
6549 return FALSE;
3e19fb8f
L
6550
6551 if (sec->compress_status == COMPRESS_SECTION_DONE
6552 && (abfd->flags & BFD_COMPRESS_GABI) == 0)
6553 {
6554 /* If section is compressed with zlib-gnu, convert
6555 section name from .debug_* to .zdebug_*. */
6556 char *new_name
6557 = convert_debug_to_zdebug (abfd, name);
6558 if (new_name == NULL)
6559 return FALSE;
6560 name = new_name;
6561 }
dd905818 6562 /* Add section name to section name section. */
3e19fb8f
L
6563 if (shdrp->sh_name != (unsigned int) -1)
6564 abort ();
6565 shdrp->sh_name
6566 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
6567 name, FALSE);
6568 d = elf_section_data (sec);
6569
dd905818 6570 /* Add reloc section name to section name section. */
3e19fb8f
L
6571 if (d->rel.hdr
6572 && !_bfd_elf_set_reloc_sh_name (abfd,
6573 d->rel.hdr,
6574 name, FALSE))
6575 return FALSE;
6576 if (d->rela.hdr
6577 && !_bfd_elf_set_reloc_sh_name (abfd,
6578 d->rela.hdr,
91cb26da 6579 name, TRUE))
3e19fb8f
L
6580 return FALSE;
6581
0ce398f1 6582 /* Update section size and contents. */
3e19fb8f
L
6583 shdrp->sh_size = sec->size;
6584 shdrp->contents = sec->contents;
0ce398f1
L
6585 shdrp->bfd_section->contents = NULL;
6586 }
1ff6de03
NA
6587 else if (is_ctf)
6588 {
6589 /* Update section size and contents. */
6590 shdrp->sh_size = sec->size;
6591 shdrp->contents = sec->contents;
6592 }
6593
0ce398f1
L
6594 off = _bfd_elf_assign_file_position_for_section (shdrp,
6595 off,
6596 TRUE);
6597 }
6598 }
252b5132
RH
6599 }
6600
3e19fb8f
L
6601 /* Place section name section after DWARF debug sections have been
6602 compressed. */
6603 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
6604 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
6605 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
6606 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
6607
6608 /* Place the section headers. */
a485e98e
AM
6609 i_ehdrp = elf_elfheader (abfd);
6610 bed = get_elf_backend_data (abfd);
6611 off = align_file_position (off, 1 << bed->s->log_file_align);
6612 i_ehdrp->e_shoff = off;
6613 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
12bd6957 6614 elf_next_file_pos (abfd) = off;
0ce398f1
L
6615
6616 return TRUE;
252b5132
RH
6617}
6618
b34976b6 6619bfd_boolean
217aa764 6620_bfd_elf_write_object_contents (bfd *abfd)
252b5132 6621{
9c5bfbb7 6622 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 6623 Elf_Internal_Shdr **i_shdrp;
b34976b6 6624 bfd_boolean failed;
9ad5cbcf 6625 unsigned int count, num_sec;
30e8ee25 6626 struct elf_obj_tdata *t;
252b5132
RH
6627
6628 if (! abfd->output_has_begun
217aa764 6629 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
b34976b6 6630 return FALSE;
db727370
JL
6631 /* Do not rewrite ELF data when the BFD has been opened for update.
6632 abfd->output_has_begun was set to TRUE on opening, so creation of new
6633 sections, and modification of existing section sizes was restricted.
6634 This means the ELF header, program headers and section headers can't have
6635 changed.
6636 If the contents of any sections has been modified, then those changes have
6637 already been written to the BFD. */
6638 else if (abfd->direction == both_direction)
6639 {
6640 BFD_ASSERT (abfd->output_has_begun);
6641 return TRUE;
6642 }
252b5132
RH
6643
6644 i_shdrp = elf_elfsections (abfd);
252b5132 6645
b34976b6 6646 failed = FALSE;
252b5132
RH
6647 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
6648 if (failed)
b34976b6 6649 return FALSE;
252b5132 6650
0ce398f1
L
6651 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
6652 return FALSE;
252b5132 6653
c044fabd 6654 /* After writing the headers, we need to write the sections too... */
9ad5cbcf
AM
6655 num_sec = elf_numsections (abfd);
6656 for (count = 1; count < num_sec; count++)
252b5132 6657 {
3e19fb8f
L
6658 i_shdrp[count]->sh_name
6659 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
6660 i_shdrp[count]->sh_name);
252b5132 6661 if (bed->elf_backend_section_processing)
75506100
MR
6662 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
6663 return FALSE;
252b5132
RH
6664 if (i_shdrp[count]->contents)
6665 {
dc810e39
AM
6666 bfd_size_type amt = i_shdrp[count]->sh_size;
6667
252b5132 6668 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
dc810e39 6669 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
b34976b6 6670 return FALSE;
252b5132
RH
6671 }
6672 }
6673
6674 /* Write out the section header names. */
30e8ee25 6675 t = elf_tdata (abfd);
26ae6d5e 6676 if (elf_shstrtab (abfd) != NULL
30e8ee25 6677 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
08a40648 6678 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
b34976b6 6679 return FALSE;
252b5132 6680
cc364be6
AM
6681 if (!(*bed->elf_backend_final_write_processing) (abfd))
6682 return FALSE;
252b5132 6683
ff59fc36
RM
6684 if (!bed->s->write_shdrs_and_ehdr (abfd))
6685 return FALSE;
6686
6687 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
c0355132
AM
6688 if (t->o->build_id.after_write_object_contents != NULL)
6689 return (*t->o->build_id.after_write_object_contents) (abfd);
ff59fc36
RM
6690
6691 return TRUE;
252b5132
RH
6692}
6693
b34976b6 6694bfd_boolean
217aa764 6695_bfd_elf_write_corefile_contents (bfd *abfd)
252b5132 6696{
c044fabd 6697 /* Hopefully this can be done just like an object file. */
252b5132
RH
6698 return _bfd_elf_write_object_contents (abfd);
6699}
c044fabd
KH
6700
6701/* Given a section, search the header to find them. */
6702
cb33740c 6703unsigned int
198beae2 6704_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
252b5132 6705{
9c5bfbb7 6706 const struct elf_backend_data *bed;
91d6fa6a 6707 unsigned int sec_index;
252b5132 6708
9ad5cbcf
AM
6709 if (elf_section_data (asect) != NULL
6710 && elf_section_data (asect)->this_idx != 0)
6711 return elf_section_data (asect)->this_idx;
6712
6713 if (bfd_is_abs_section (asect))
91d6fa6a 6714 sec_index = SHN_ABS;
af746e92 6715 else if (bfd_is_com_section (asect))
91d6fa6a 6716 sec_index = SHN_COMMON;
af746e92 6717 else if (bfd_is_und_section (asect))
91d6fa6a 6718 sec_index = SHN_UNDEF;
af746e92 6719 else
91d6fa6a 6720 sec_index = SHN_BAD;
252b5132 6721
af746e92 6722 bed = get_elf_backend_data (abfd);
252b5132
RH
6723 if (bed->elf_backend_section_from_bfd_section)
6724 {
91d6fa6a 6725 int retval = sec_index;
9ad5cbcf 6726
af746e92
AM
6727 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6728 return retval;
252b5132
RH
6729 }
6730
91d6fa6a 6731 if (sec_index == SHN_BAD)
af746e92 6732 bfd_set_error (bfd_error_nonrepresentable_section);
252b5132 6733
91d6fa6a 6734 return sec_index;
252b5132
RH
6735}
6736
6737/* Given a BFD symbol, return the index in the ELF symbol table, or -1
6738 on error. */
6739
6740int
217aa764 6741_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
252b5132
RH
6742{
6743 asymbol *asym_ptr = *asym_ptr_ptr;
6744 int idx;
6745 flagword flags = asym_ptr->flags;
6746
6747 /* When gas creates relocations against local labels, it creates its
6748 own symbol for the section, but does put the symbol into the
6749 symbol chain, so udata is 0. When the linker is generating
6750 relocatable output, this section symbol may be for one of the
6751 input sections rather than the output section. */
6752 if (asym_ptr->udata.i == 0
6753 && (flags & BSF_SECTION_SYM)
6754 && asym_ptr->section)
6755 {
5372391b 6756 asection *sec;
252b5132
RH
6757 int indx;
6758
5372391b
AM
6759 sec = asym_ptr->section;
6760 if (sec->owner != abfd && sec->output_section != NULL)
6761 sec = sec->output_section;
6762 if (sec->owner == abfd
6763 && (indx = sec->index) < elf_num_section_syms (abfd)
4e89ac30 6764 && elf_section_syms (abfd)[indx] != NULL)
252b5132
RH
6765 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
6766 }
6767
6768 idx = asym_ptr->udata.i;
6769
6770 if (idx == 0)
6771 {
6772 /* This case can occur when using --strip-symbol on a symbol
08a40648 6773 which is used in a relocation entry. */
4eca0228 6774 _bfd_error_handler
695344c0 6775 /* xgettext:c-format */
871b3ab2 6776 (_("%pB: symbol `%s' required but not present"),
d003868e 6777 abfd, bfd_asymbol_name (asym_ptr));
252b5132
RH
6778 bfd_set_error (bfd_error_no_symbols);
6779 return -1;
6780 }
6781
6782#if DEBUG & 4
6783 {
6784 fprintf (stderr,
cd9af601
AM
6785 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6786 (long) asym_ptr, asym_ptr->name, idx, flags);
252b5132
RH
6787 fflush (stderr);
6788 }
6789#endif
6790
6791 return idx;
6792}
6793
84d1d650 6794/* Rewrite program header information. */
252b5132 6795
b34976b6 6796static bfd_boolean
84d1d650 6797rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
252b5132 6798{
b34976b6
AM
6799 Elf_Internal_Ehdr *iehdr;
6800 struct elf_segment_map *map;
6801 struct elf_segment_map *map_first;
6802 struct elf_segment_map **pointer_to_map;
6803 Elf_Internal_Phdr *segment;
6804 asection *section;
6805 unsigned int i;
6806 unsigned int num_segments;
6807 bfd_boolean phdr_included = FALSE;
5c44b38e 6808 bfd_boolean p_paddr_valid;
b34976b6
AM
6809 bfd_vma maxpagesize;
6810 struct elf_segment_map *phdr_adjust_seg = NULL;
6811 unsigned int phdr_adjust_num = 0;
9c5bfbb7 6812 const struct elf_backend_data *bed;
502794d4 6813 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
bc67d8a6 6814
caf47ea6 6815 bed = get_elf_backend_data (ibfd);
252b5132
RH
6816 iehdr = elf_elfheader (ibfd);
6817
bc67d8a6 6818 map_first = NULL;
c044fabd 6819 pointer_to_map = &map_first;
252b5132
RH
6820
6821 num_segments = elf_elfheader (ibfd)->e_phnum;
bc67d8a6
NC
6822 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
6823
6824 /* Returns the end address of the segment + 1. */
aecc8f8a
AM
6825#define SEGMENT_END(segment, start) \
6826 (start + (segment->p_memsz > segment->p_filesz \
6827 ? segment->p_memsz : segment->p_filesz))
bc67d8a6 6828
eecdbe52
JJ
6829#define SECTION_SIZE(section, segment) \
6830 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6831 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
eea6121a 6832 ? section->size : 0)
eecdbe52 6833
b34976b6 6834 /* Returns TRUE if the given section is contained within
bc67d8a6 6835 the given segment. VMA addresses are compared. */
502794d4
CE
6836#define IS_CONTAINED_BY_VMA(section, segment, opb) \
6837 (section->vma * (opb) >= segment->p_vaddr \
6838 && (section->vma * (opb) + SECTION_SIZE (section, segment) \
aecc8f8a 6839 <= (SEGMENT_END (segment, segment->p_vaddr))))
c044fabd 6840
b34976b6 6841 /* Returns TRUE if the given section is contained within
bc67d8a6 6842 the given segment. LMA addresses are compared. */
502794d4
CE
6843#define IS_CONTAINED_BY_LMA(section, segment, base, opb) \
6844 (section->lma * (opb) >= base \
6845 && (section->lma + SECTION_SIZE (section, segment) / (opb) >= section->lma) \
6846 && (section->lma * (opb) + SECTION_SIZE (section, segment) \
aecc8f8a 6847 <= SEGMENT_END (segment, base)))
252b5132 6848
0efc80c8
L
6849 /* Handle PT_NOTE segment. */
6850#define IS_NOTE(p, s) \
aecc8f8a 6851 (p->p_type == PT_NOTE \
0efc80c8 6852 && elf_section_type (s) == SHT_NOTE \
aecc8f8a 6853 && (bfd_vma) s->filepos >= p->p_offset \
cb3ff1e5 6854 && ((bfd_vma) s->filepos + s->size \
aecc8f8a 6855 <= p->p_offset + p->p_filesz))
252b5132 6856
0efc80c8
L
6857 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6858 etc. */
6859#define IS_COREFILE_NOTE(p, s) \
6860 (IS_NOTE (p, s) \
6861 && bfd_get_format (ibfd) == bfd_core \
6862 && s->vma == 0 \
6863 && s->lma == 0)
6864
252b5132
RH
6865 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6866 linker, which generates a PT_INTERP section with p_vaddr and
6867 p_memsz set to 0. */
aecc8f8a
AM
6868#define IS_SOLARIS_PT_INTERP(p, s) \
6869 (p->p_vaddr == 0 \
6870 && p->p_paddr == 0 \
6871 && p->p_memsz == 0 \
6872 && p->p_filesz > 0 \
6873 && (s->flags & SEC_HAS_CONTENTS) != 0 \
eea6121a 6874 && s->size > 0 \
aecc8f8a 6875 && (bfd_vma) s->filepos >= p->p_offset \
cb3ff1e5 6876 && ((bfd_vma) s->filepos + s->size \
aecc8f8a 6877 <= p->p_offset + p->p_filesz))
5c440b1e 6878
bc67d8a6
NC
6879 /* Decide if the given section should be included in the given segment.
6880 A section will be included if:
f5ffc919 6881 1. It is within the address space of the segment -- we use the LMA
08a40648 6882 if that is set for the segment and the VMA otherwise,
0efc80c8 6883 2. It is an allocated section or a NOTE section in a PT_NOTE
d324f6d6 6884 segment.
bc67d8a6 6885 3. There is an output section associated with it,
eecdbe52 6886 4. The section has not already been allocated to a previous segment.
2b05f1b7 6887 5. PT_GNU_STACK segments do not include any sections.
03394ac9 6888 6. PT_TLS segment includes only SHF_TLS sections.
6f79b219
JJ
6889 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6890 8. PT_DYNAMIC should not contain empty sections at the beginning
08a40648 6891 (with the possible exception of .dynamic). */
502794d4 6892#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
2b05f1b7 6893 ((((segment->p_paddr \
502794d4
CE
6894 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr, opb) \
6895 : IS_CONTAINED_BY_VMA (section, segment, opb)) \
2b05f1b7 6896 && (section->flags & SEC_ALLOC) != 0) \
0efc80c8 6897 || IS_NOTE (segment, section)) \
2b05f1b7
L
6898 && segment->p_type != PT_GNU_STACK \
6899 && (segment->p_type != PT_TLS \
6900 || (section->flags & SEC_THREAD_LOCAL)) \
6901 && (segment->p_type == PT_LOAD \
6902 || segment->p_type == PT_TLS \
6903 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6904 && (segment->p_type != PT_DYNAMIC \
6905 || SECTION_SIZE (section, segment) > 0 \
6906 || (segment->p_paddr \
502794d4
CE
6907 ? segment->p_paddr != section->lma * (opb) \
6908 : segment->p_vaddr != section->vma * (opb)) \
fd361982 6909 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
9933dc52 6910 && (segment->p_type != PT_LOAD || !section->segment_mark))
bc67d8a6 6911
9f17e2a6
L
6912/* If the output section of a section in the input segment is NULL,
6913 it is removed from the corresponding output segment. */
502794d4
CE
6914#define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
6915 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
9f17e2a6
L
6916 && section->output_section != NULL)
6917
b34976b6 6918 /* Returns TRUE iff seg1 starts after the end of seg2. */
b5f852ea
NC
6919#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6920 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6921
6922 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6923 their VMA address ranges and their LMA address ranges overlap.
6924 It is possible to have overlapping VMA ranges without overlapping LMA
6925 ranges. RedBoot images for example can have both .data and .bss mapped
6926 to the same VMA range, but with the .data section mapped to a different
6927 LMA. */
aecc8f8a 6928#define SEGMENT_OVERLAPS(seg1, seg2) \
b5f852ea 6929 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
08a40648 6930 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
b5f852ea 6931 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
08a40648 6932 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
bc67d8a6
NC
6933
6934 /* Initialise the segment mark field. */
6935 for (section = ibfd->sections; section != NULL; section = section->next)
b34976b6 6936 section->segment_mark = FALSE;
bc67d8a6 6937
5c44b38e
AM
6938 /* The Solaris linker creates program headers in which all the
6939 p_paddr fields are zero. When we try to objcopy or strip such a
6940 file, we get confused. Check for this case, and if we find it
6941 don't set the p_paddr_valid fields. */
6942 p_paddr_valid = FALSE;
6943 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6944 i < num_segments;
6945 i++, segment++)
6946 if (segment->p_paddr != 0)
6947 {
6948 p_paddr_valid = TRUE;
6949 break;
6950 }
6951
252b5132 6952 /* Scan through the segments specified in the program header
bc67d8a6 6953 of the input BFD. For this first scan we look for overlaps
9ad5cbcf 6954 in the loadable segments. These can be created by weird
aecc8f8a 6955 parameters to objcopy. Also, fix some solaris weirdness. */
bc67d8a6
NC
6956 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6957 i < num_segments;
c044fabd 6958 i++, segment++)
252b5132 6959 {
252b5132 6960 unsigned int j;
c044fabd 6961 Elf_Internal_Phdr *segment2;
252b5132 6962
aecc8f8a
AM
6963 if (segment->p_type == PT_INTERP)
6964 for (section = ibfd->sections; section; section = section->next)
6965 if (IS_SOLARIS_PT_INTERP (segment, section))
6966 {
6967 /* Mininal change so that the normal section to segment
4cc11e76 6968 assignment code will work. */
502794d4 6969 segment->p_vaddr = section->vma * opb;
aecc8f8a
AM
6970 break;
6971 }
6972
bc67d8a6 6973 if (segment->p_type != PT_LOAD)
b10a8ae0
L
6974 {
6975 /* Remove PT_GNU_RELRO segment. */
6976 if (segment->p_type == PT_GNU_RELRO)
6977 segment->p_type = PT_NULL;
6978 continue;
6979 }
c044fabd 6980
bc67d8a6 6981 /* Determine if this segment overlaps any previous segments. */
0067a569 6982 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
bc67d8a6
NC
6983 {
6984 bfd_signed_vma extra_length;
c044fabd 6985
bc67d8a6 6986 if (segment2->p_type != PT_LOAD
0067a569 6987 || !SEGMENT_OVERLAPS (segment, segment2))
bc67d8a6 6988 continue;
c044fabd 6989
bc67d8a6
NC
6990 /* Merge the two segments together. */
6991 if (segment2->p_vaddr < segment->p_vaddr)
6992 {
c044fabd 6993 /* Extend SEGMENT2 to include SEGMENT and then delete
08a40648 6994 SEGMENT. */
0067a569
AM
6995 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
6996 - SEGMENT_END (segment2, segment2->p_vaddr));
c044fabd 6997
bc67d8a6
NC
6998 if (extra_length > 0)
6999 {
0067a569 7000 segment2->p_memsz += extra_length;
bc67d8a6
NC
7001 segment2->p_filesz += extra_length;
7002 }
c044fabd 7003
bc67d8a6 7004 segment->p_type = PT_NULL;
c044fabd 7005
bc67d8a6
NC
7006 /* Since we have deleted P we must restart the outer loop. */
7007 i = 0;
7008 segment = elf_tdata (ibfd)->phdr;
7009 break;
7010 }
7011 else
7012 {
c044fabd 7013 /* Extend SEGMENT to include SEGMENT2 and then delete
08a40648 7014 SEGMENT2. */
0067a569
AM
7015 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
7016 - SEGMENT_END (segment, segment->p_vaddr));
c044fabd 7017
bc67d8a6
NC
7018 if (extra_length > 0)
7019 {
0067a569 7020 segment->p_memsz += extra_length;
bc67d8a6
NC
7021 segment->p_filesz += extra_length;
7022 }
c044fabd 7023
bc67d8a6
NC
7024 segment2->p_type = PT_NULL;
7025 }
7026 }
7027 }
c044fabd 7028
bc67d8a6
NC
7029 /* The second scan attempts to assign sections to segments. */
7030 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7031 i < num_segments;
0067a569 7032 i++, segment++)
bc67d8a6 7033 {
0067a569
AM
7034 unsigned int section_count;
7035 asection **sections;
7036 asection *output_section;
7037 unsigned int isec;
9933dc52
AM
7038 asection *matching_lma;
7039 asection *suggested_lma;
0067a569 7040 unsigned int j;
446f7ed5 7041 size_t amt;
0067a569 7042 asection *first_section;
bc67d8a6
NC
7043
7044 if (segment->p_type == PT_NULL)
7045 continue;
c044fabd 7046
9f17e2a6 7047 first_section = NULL;
bc67d8a6 7048 /* Compute how many sections might be placed into this segment. */
b5f852ea
NC
7049 for (section = ibfd->sections, section_count = 0;
7050 section != NULL;
7051 section = section->next)
9f17e2a6
L
7052 {
7053 /* Find the first section in the input segment, which may be
7054 removed from the corresponding output segment. */
502794d4 7055 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb))
9f17e2a6
L
7056 {
7057 if (first_section == NULL)
7058 first_section = section;
7059 if (section->output_section != NULL)
7060 ++section_count;
7061 }
7062 }
811072d8 7063
b5f852ea
NC
7064 /* Allocate a segment map big enough to contain
7065 all of the sections we have selected. */
00bee008 7066 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
446f7ed5 7067 amt += section_count * sizeof (asection *);
a50b1753 7068 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
bc67d8a6 7069 if (map == NULL)
b34976b6 7070 return FALSE;
252b5132
RH
7071
7072 /* Initialise the fields of the segment map. Default to
7073 using the physical address of the segment in the input BFD. */
0067a569
AM
7074 map->next = NULL;
7075 map->p_type = segment->p_type;
7076 map->p_flags = segment->p_flags;
bc67d8a6 7077 map->p_flags_valid = 1;
55d55ac7 7078
9f17e2a6
L
7079 /* If the first section in the input segment is removed, there is
7080 no need to preserve segment physical address in the corresponding
7081 output segment. */
945c025a 7082 if (!first_section || first_section->output_section != NULL)
9f17e2a6
L
7083 {
7084 map->p_paddr = segment->p_paddr;
5c44b38e 7085 map->p_paddr_valid = p_paddr_valid;
9f17e2a6 7086 }
252b5132
RH
7087
7088 /* Determine if this segment contains the ELF file header
7089 and if it contains the program headers themselves. */
bc67d8a6
NC
7090 map->includes_filehdr = (segment->p_offset == 0
7091 && segment->p_filesz >= iehdr->e_ehsize);
bc67d8a6 7092 map->includes_phdrs = 0;
252b5132 7093
0067a569 7094 if (!phdr_included || segment->p_type != PT_LOAD)
252b5132 7095 {
bc67d8a6
NC
7096 map->includes_phdrs =
7097 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7098 && (segment->p_offset + segment->p_filesz
252b5132
RH
7099 >= ((bfd_vma) iehdr->e_phoff
7100 + iehdr->e_phnum * iehdr->e_phentsize)));
c044fabd 7101
bc67d8a6 7102 if (segment->p_type == PT_LOAD && map->includes_phdrs)
b34976b6 7103 phdr_included = TRUE;
252b5132
RH
7104 }
7105
bc67d8a6 7106 if (section_count == 0)
252b5132
RH
7107 {
7108 /* Special segments, such as the PT_PHDR segment, may contain
7109 no sections, but ordinary, loadable segments should contain
1ed89aa9 7110 something. They are allowed by the ELF spec however, so only
07d6d2b8 7111 a warning is produced.
f98450c6
NC
7112 There is however the valid use case of embedded systems which
7113 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7114 flash memory with zeros. No warning is shown for that case. */
7115 if (segment->p_type == PT_LOAD
7116 && (segment->p_filesz > 0 || segment->p_memsz == 0))
7117 /* xgettext:c-format */
9793eb77
AM
7118 _bfd_error_handler
7119 (_("%pB: warning: empty loadable segment detected"
7120 " at vaddr=%#" PRIx64 ", is this intentional?"),
7121 ibfd, (uint64_t) segment->p_vaddr);
252b5132 7122
502794d4 7123 map->p_vaddr_offset = segment->p_vaddr / opb;
bc67d8a6 7124 map->count = 0;
c044fabd
KH
7125 *pointer_to_map = map;
7126 pointer_to_map = &map->next;
252b5132
RH
7127
7128 continue;
7129 }
7130
7131 /* Now scan the sections in the input BFD again and attempt
7132 to add their corresponding output sections to the segment map.
7133 The problem here is how to handle an output section which has
7134 been moved (ie had its LMA changed). There are four possibilities:
7135
7136 1. None of the sections have been moved.
7137 In this case we can continue to use the segment LMA from the
7138 input BFD.
7139
7140 2. All of the sections have been moved by the same amount.
7141 In this case we can change the segment's LMA to match the LMA
7142 of the first section.
7143
7144 3. Some of the sections have been moved, others have not.
7145 In this case those sections which have not been moved can be
7146 placed in the current segment which will have to have its size,
7147 and possibly its LMA changed, and a new segment or segments will
7148 have to be created to contain the other sections.
7149
b5f852ea 7150 4. The sections have been moved, but not by the same amount.
252b5132
RH
7151 In this case we can change the segment's LMA to match the LMA
7152 of the first section and we will have to create a new segment
7153 or segments to contain the other sections.
7154
7155 In order to save time, we allocate an array to hold the section
7156 pointers that we are interested in. As these sections get assigned
7157 to a segment, they are removed from this array. */
7158
446f7ed5
AM
7159 amt = section_count * sizeof (asection *);
7160 sections = (asection **) bfd_malloc (amt);
252b5132 7161 if (sections == NULL)
b34976b6 7162 return FALSE;
252b5132
RH
7163
7164 /* Step One: Scan for segment vs section LMA conflicts.
7165 Also add the sections to the section array allocated above.
7166 Also add the sections to the current segment. In the common
7167 case, where the sections have not been moved, this means that
7168 we have completely filled the segment, and there is nothing
7169 more to do. */
252b5132 7170 isec = 0;
9933dc52
AM
7171 matching_lma = NULL;
7172 suggested_lma = NULL;
252b5132 7173
461c4b2e 7174 for (section = first_section, j = 0;
bc67d8a6
NC
7175 section != NULL;
7176 section = section->next)
252b5132 7177 {
502794d4 7178 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb))
c0f7859b 7179 {
bc67d8a6
NC
7180 output_section = section->output_section;
7181
0067a569 7182 sections[j++] = section;
252b5132
RH
7183
7184 /* The Solaris native linker always sets p_paddr to 0.
7185 We try to catch that case here, and set it to the
5e8d7549
NC
7186 correct value. Note - some backends require that
7187 p_paddr be left as zero. */
5c44b38e 7188 if (!p_paddr_valid
4455705d 7189 && segment->p_vaddr != 0
0067a569 7190 && !bed->want_p_paddr_set_to_zero
252b5132 7191 && isec == 0
bc67d8a6 7192 && output_section->lma != 0
9933dc52
AM
7193 && (align_power (segment->p_vaddr
7194 + (map->includes_filehdr
7195 ? iehdr->e_ehsize : 0)
7196 + (map->includes_phdrs
7197 ? iehdr->e_phnum * iehdr->e_phentsize
7198 : 0),
66631823
CE
7199 output_section->alignment_power * opb)
7200 == (output_section->vma * opb)))
bc67d8a6 7201 map->p_paddr = segment->p_vaddr;
252b5132
RH
7202
7203 /* Match up the physical address of the segment with the
7204 LMA address of the output section. */
502794d4
CE
7205 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr,
7206 opb)
5e8d7549 7207 || IS_COREFILE_NOTE (segment, section)
0067a569 7208 || (bed->want_p_paddr_set_to_zero
502794d4 7209 && IS_CONTAINED_BY_VMA (output_section, segment, opb)))
252b5132 7210 {
9933dc52
AM
7211 if (matching_lma == NULL
7212 || output_section->lma < matching_lma->lma)
7213 matching_lma = output_section;
252b5132
RH
7214
7215 /* We assume that if the section fits within the segment
bc67d8a6 7216 then it does not overlap any other section within that
252b5132 7217 segment. */
0067a569
AM
7218 map->sections[isec++] = output_section;
7219 }
9933dc52
AM
7220 else if (suggested_lma == NULL)
7221 suggested_lma = output_section;
147d51c2
L
7222
7223 if (j == section_count)
7224 break;
252b5132
RH
7225 }
7226 }
7227
bc67d8a6 7228 BFD_ASSERT (j == section_count);
252b5132
RH
7229
7230 /* Step Two: Adjust the physical address of the current segment,
7231 if necessary. */
bc67d8a6 7232 if (isec == section_count)
252b5132
RH
7233 {
7234 /* All of the sections fitted within the segment as currently
7235 specified. This is the default case. Add the segment to
7236 the list of built segments and carry on to process the next
7237 program header in the input BFD. */
bc67d8a6 7238 map->count = section_count;
c044fabd
KH
7239 *pointer_to_map = map;
7240 pointer_to_map = &map->next;
08a40648 7241
5c44b38e 7242 if (p_paddr_valid
30fe1832
AM
7243 && !bed->want_p_paddr_set_to_zero)
7244 {
7245 bfd_vma hdr_size = 0;
7246 if (map->includes_filehdr)
7247 hdr_size = iehdr->e_ehsize;
7248 if (map->includes_phdrs)
7249 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7250
7251 /* Account for padding before the first section in the
7252 segment. */
502794d4
CE
7253 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7254 - matching_lma->lma);
30fe1832 7255 }
08a40648 7256
252b5132
RH
7257 free (sections);
7258 continue;
7259 }
252b5132
RH
7260 else
7261 {
9933dc52
AM
7262 /* Change the current segment's physical address to match
7263 the LMA of the first section that fitted, or if no
7264 section fitted, the first section. */
7265 if (matching_lma == NULL)
7266 matching_lma = suggested_lma;
7267
66631823 7268 map->p_paddr = matching_lma->lma * opb;
72730e0c 7269
bc67d8a6
NC
7270 /* Offset the segment physical address from the lma
7271 to allow for space taken up by elf headers. */
9933dc52 7272 if (map->includes_phdrs)
010c8431 7273 {
9933dc52
AM
7274 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7275
7276 /* iehdr->e_phnum is just an estimate of the number
7277 of program headers that we will need. Make a note
7278 here of the number we used and the segment we chose
7279 to hold these headers, so that we can adjust the
7280 offset when we know the correct value. */
7281 phdr_adjust_num = iehdr->e_phnum;
7282 phdr_adjust_seg = map;
010c8431 7283 }
252b5132 7284
9933dc52 7285 if (map->includes_filehdr)
bc67d8a6 7286 {
9933dc52
AM
7287 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7288 map->p_paddr -= iehdr->e_ehsize;
7289 /* We've subtracted off the size of headers from the
7290 first section lma, but there may have been some
7291 alignment padding before that section too. Try to
7292 account for that by adjusting the segment lma down to
7293 the same alignment. */
7294 if (segment->p_align != 0 && segment->p_align < align)
7295 align = segment->p_align;
66631823 7296 map->p_paddr &= -(align * opb);
bc67d8a6 7297 }
252b5132
RH
7298 }
7299
7300 /* Step Three: Loop over the sections again, this time assigning
caf47ea6 7301 those that fit to the current segment and removing them from the
252b5132
RH
7302 sections array; but making sure not to leave large gaps. Once all
7303 possible sections have been assigned to the current segment it is
7304 added to the list of built segments and if sections still remain
7305 to be assigned, a new segment is constructed before repeating
7306 the loop. */
7307 isec = 0;
7308 do
7309 {
bc67d8a6 7310 map->count = 0;
9933dc52 7311 suggested_lma = NULL;
252b5132
RH
7312
7313 /* Fill the current segment with sections that fit. */
bc67d8a6 7314 for (j = 0; j < section_count; j++)
252b5132 7315 {
bc67d8a6 7316 section = sections[j];
252b5132 7317
bc67d8a6 7318 if (section == NULL)
252b5132
RH
7319 continue;
7320
bc67d8a6 7321 output_section = section->output_section;
252b5132 7322
bc67d8a6 7323 BFD_ASSERT (output_section != NULL);
c044fabd 7324
502794d4
CE
7325 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr,
7326 opb)
bc67d8a6 7327 || IS_COREFILE_NOTE (segment, section))
252b5132 7328 {
bc67d8a6 7329 if (map->count == 0)
252b5132
RH
7330 {
7331 /* If the first section in a segment does not start at
bc67d8a6
NC
7332 the beginning of the segment, then something is
7333 wrong. */
9933dc52
AM
7334 if (align_power (map->p_paddr
7335 + (map->includes_filehdr
7336 ? iehdr->e_ehsize : 0)
7337 + (map->includes_phdrs
7338 ? iehdr->e_phnum * iehdr->e_phentsize
7339 : 0),
66631823
CE
7340 output_section->alignment_power * opb)
7341 != output_section->lma * opb)
9aea1e31 7342 goto sorry;
252b5132
RH
7343 }
7344 else
7345 {
0067a569 7346 asection *prev_sec;
252b5132 7347
bc67d8a6 7348 prev_sec = map->sections[map->count - 1];
252b5132
RH
7349
7350 /* If the gap between the end of the previous section
bc67d8a6
NC
7351 and the start of this section is more than
7352 maxpagesize then we need to start a new segment. */
eea6121a 7353 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
079e9a2f 7354 maxpagesize)
caf47ea6 7355 < BFD_ALIGN (output_section->lma, maxpagesize))
0067a569 7356 || (prev_sec->lma + prev_sec->size
079e9a2f 7357 > output_section->lma))
252b5132 7358 {
9933dc52
AM
7359 if (suggested_lma == NULL)
7360 suggested_lma = output_section;
252b5132
RH
7361
7362 continue;
7363 }
7364 }
7365
bc67d8a6 7366 map->sections[map->count++] = output_section;
252b5132
RH
7367 ++isec;
7368 sections[j] = NULL;
9933dc52
AM
7369 if (segment->p_type == PT_LOAD)
7370 section->segment_mark = TRUE;
0067a569 7371 }
9933dc52
AM
7372 else if (suggested_lma == NULL)
7373 suggested_lma = output_section;
252b5132
RH
7374 }
7375
beab4532
NC
7376 /* PR 23932. A corrupt input file may contain sections that cannot
7377 be assigned to any segment - because for example they have a
9984857c
NC
7378 negative size - or segments that do not contain any sections.
7379 But there are also valid reasons why a segment can be empty.
7380 So allow a count of zero. */
252b5132
RH
7381
7382 /* Add the current segment to the list of built segments. */
c044fabd
KH
7383 *pointer_to_map = map;
7384 pointer_to_map = &map->next;
252b5132 7385
bc67d8a6 7386 if (isec < section_count)
252b5132
RH
7387 {
7388 /* We still have not allocated all of the sections to
7389 segments. Create a new segment here, initialise it
7390 and carry on looping. */
00bee008 7391 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
446f7ed5 7392 amt += section_count * sizeof (asection *);
5964fc3a 7393 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
bc67d8a6 7394 if (map == NULL)
5ed6aba4
NC
7395 {
7396 free (sections);
7397 return FALSE;
7398 }
252b5132
RH
7399
7400 /* Initialise the fields of the segment map. Set the physical
7401 physical address to the LMA of the first section that has
7402 not yet been assigned. */
0067a569
AM
7403 map->next = NULL;
7404 map->p_type = segment->p_type;
7405 map->p_flags = segment->p_flags;
7406 map->p_flags_valid = 1;
66631823 7407 map->p_paddr = suggested_lma->lma * opb;
5c44b38e 7408 map->p_paddr_valid = p_paddr_valid;
bc67d8a6 7409 map->includes_filehdr = 0;
0067a569 7410 map->includes_phdrs = 0;
252b5132 7411 }
9984857c
NC
7412
7413 continue;
7414 sorry:
7415 bfd_set_error (bfd_error_sorry);
7416 free (sections);
7417 return FALSE;
252b5132 7418 }
bc67d8a6 7419 while (isec < section_count);
252b5132
RH
7420
7421 free (sections);
7422 }
7423
12bd6957 7424 elf_seg_map (obfd) = map_first;
bc67d8a6
NC
7425
7426 /* If we had to estimate the number of program headers that were
9ad5cbcf 7427 going to be needed, then check our estimate now and adjust
bc67d8a6
NC
7428 the offset if necessary. */
7429 if (phdr_adjust_seg != NULL)
7430 {
7431 unsigned int count;
c044fabd 7432
bc67d8a6 7433 for (count = 0, map = map_first; map != NULL; map = map->next)
c044fabd 7434 count++;
252b5132 7435
bc67d8a6
NC
7436 if (count > phdr_adjust_num)
7437 phdr_adjust_seg->p_paddr
7438 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
9933dc52
AM
7439
7440 for (map = map_first; map != NULL; map = map->next)
7441 if (map->p_type == PT_PHDR)
7442 {
7443 bfd_vma adjust
7444 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7445 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7446 break;
7447 }
bc67d8a6 7448 }
c044fabd 7449
bc67d8a6 7450#undef SEGMENT_END
eecdbe52 7451#undef SECTION_SIZE
bc67d8a6
NC
7452#undef IS_CONTAINED_BY_VMA
7453#undef IS_CONTAINED_BY_LMA
0efc80c8 7454#undef IS_NOTE
252b5132 7455#undef IS_COREFILE_NOTE
bc67d8a6 7456#undef IS_SOLARIS_PT_INTERP
9f17e2a6 7457#undef IS_SECTION_IN_INPUT_SEGMENT
bc67d8a6
NC
7458#undef INCLUDE_SECTION_IN_SEGMENT
7459#undef SEGMENT_AFTER_SEGMENT
7460#undef SEGMENT_OVERLAPS
b34976b6 7461 return TRUE;
252b5132
RH
7462}
7463
84d1d650
L
7464/* Copy ELF program header information. */
7465
7466static bfd_boolean
7467copy_elf_program_header (bfd *ibfd, bfd *obfd)
7468{
7469 Elf_Internal_Ehdr *iehdr;
7470 struct elf_segment_map *map;
7471 struct elf_segment_map *map_first;
7472 struct elf_segment_map **pointer_to_map;
7473 Elf_Internal_Phdr *segment;
7474 unsigned int i;
7475 unsigned int num_segments;
7476 bfd_boolean phdr_included = FALSE;
88967714 7477 bfd_boolean p_paddr_valid;
502794d4 7478 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
84d1d650
L
7479
7480 iehdr = elf_elfheader (ibfd);
7481
7482 map_first = NULL;
7483 pointer_to_map = &map_first;
7484
88967714
AM
7485 /* If all the segment p_paddr fields are zero, don't set
7486 map->p_paddr_valid. */
7487 p_paddr_valid = FALSE;
84d1d650 7488 num_segments = elf_elfheader (ibfd)->e_phnum;
88967714
AM
7489 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7490 i < num_segments;
7491 i++, segment++)
7492 if (segment->p_paddr != 0)
7493 {
7494 p_paddr_valid = TRUE;
7495 break;
7496 }
7497
84d1d650
L
7498 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7499 i < num_segments;
7500 i++, segment++)
7501 {
7502 asection *section;
7503 unsigned int section_count;
986f0783 7504 size_t amt;
84d1d650 7505 Elf_Internal_Shdr *this_hdr;
53020534 7506 asection *first_section = NULL;
a76e6f2f 7507 asection *lowest_section;
84d1d650 7508
84d1d650
L
7509 /* Compute how many sections are in this segment. */
7510 for (section = ibfd->sections, section_count = 0;
7511 section != NULL;
7512 section = section->next)
7513 {
7514 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7515 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
3271a814 7516 {
a76e6f2f
AM
7517 if (first_section == NULL)
7518 first_section = section;
3271a814
NS
7519 section_count++;
7520 }
84d1d650
L
7521 }
7522
7523 /* Allocate a segment map big enough to contain
7524 all of the sections we have selected. */
00bee008 7525 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
986f0783 7526 amt += section_count * sizeof (asection *);
a50b1753 7527 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
84d1d650
L
7528 if (map == NULL)
7529 return FALSE;
7530
7531 /* Initialize the fields of the output segment map with the
7532 input segment. */
7533 map->next = NULL;
7534 map->p_type = segment->p_type;
7535 map->p_flags = segment->p_flags;
7536 map->p_flags_valid = 1;
7537 map->p_paddr = segment->p_paddr;
88967714 7538 map->p_paddr_valid = p_paddr_valid;
3f570048
AM
7539 map->p_align = segment->p_align;
7540 map->p_align_valid = 1;
3271a814 7541 map->p_vaddr_offset = 0;
84d1d650 7542
04c3a755
NS
7543 if (map->p_type == PT_GNU_RELRO
7544 || map->p_type == PT_GNU_STACK)
b10a8ae0
L
7545 {
7546 /* The PT_GNU_RELRO segment may contain the first a few
7547 bytes in the .got.plt section even if the whole .got.plt
7548 section isn't in the PT_GNU_RELRO segment. We won't
04c3a755
NS
7549 change the size of the PT_GNU_RELRO segment.
7550 Similarly, PT_GNU_STACK size is significant on uclinux
7551 systems. */
9433b9b1 7552 map->p_size = segment->p_memsz;
b10a8ae0
L
7553 map->p_size_valid = 1;
7554 }
7555
84d1d650
L
7556 /* Determine if this segment contains the ELF file header
7557 and if it contains the program headers themselves. */
7558 map->includes_filehdr = (segment->p_offset == 0
7559 && segment->p_filesz >= iehdr->e_ehsize);
7560
7561 map->includes_phdrs = 0;
7562 if (! phdr_included || segment->p_type != PT_LOAD)
7563 {
7564 map->includes_phdrs =
7565 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7566 && (segment->p_offset + segment->p_filesz
7567 >= ((bfd_vma) iehdr->e_phoff
7568 + iehdr->e_phnum * iehdr->e_phentsize)));
7569
7570 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7571 phdr_included = TRUE;
7572 }
7573
bbefd0a9 7574 lowest_section = NULL;
84d1d650
L
7575 if (section_count != 0)
7576 {
7577 unsigned int isec = 0;
7578
53020534 7579 for (section = first_section;
84d1d650
L
7580 section != NULL;
7581 section = section->next)
7582 {
7583 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7584 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
53020534
L
7585 {
7586 map->sections[isec++] = section->output_section;
a76e6f2f
AM
7587 if ((section->flags & SEC_ALLOC) != 0)
7588 {
7589 bfd_vma seg_off;
7590
bbefd0a9
AM
7591 if (lowest_section == NULL
7592 || section->lma < lowest_section->lma)
fb8a5684
AM
7593 lowest_section = section;
7594
a76e6f2f
AM
7595 /* Section lmas are set up from PT_LOAD header
7596 p_paddr in _bfd_elf_make_section_from_shdr.
7597 If this header has a p_paddr that disagrees
7598 with the section lma, flag the p_paddr as
7599 invalid. */
7600 if ((section->flags & SEC_LOAD) != 0)
7601 seg_off = this_hdr->sh_offset - segment->p_offset;
7602 else
7603 seg_off = this_hdr->sh_addr - segment->p_vaddr;
502794d4 7604 if (section->lma * opb - segment->p_paddr != seg_off)
a76e6f2f
AM
7605 map->p_paddr_valid = FALSE;
7606 }
53020534
L
7607 if (isec == section_count)
7608 break;
7609 }
84d1d650
L
7610 }
7611 }
7612
5d695627 7613 if (section_count == 0)
502794d4 7614 map->p_vaddr_offset = segment->p_vaddr / opb;
30fe1832
AM
7615 else if (map->p_paddr_valid)
7616 {
7617 /* Account for padding before the first section in the segment. */
7618 bfd_vma hdr_size = 0;
7619 if (map->includes_filehdr)
7620 hdr_size = iehdr->e_ehsize;
7621 if (map->includes_phdrs)
7622 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7623
502794d4 7624 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
30fe1832
AM
7625 - (lowest_section ? lowest_section->lma : 0));
7626 }
a76e6f2f 7627
84d1d650
L
7628 map->count = section_count;
7629 *pointer_to_map = map;
7630 pointer_to_map = &map->next;
7631 }
7632
12bd6957 7633 elf_seg_map (obfd) = map_first;
84d1d650
L
7634 return TRUE;
7635}
7636
7637/* Copy private BFD data. This copies or rewrites ELF program header
7638 information. */
7639
7640static bfd_boolean
7641copy_private_bfd_data (bfd *ibfd, bfd *obfd)
7642{
84d1d650
L
7643 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7644 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7645 return TRUE;
7646
7647 if (elf_tdata (ibfd)->phdr == NULL)
7648 return TRUE;
7649
7650 if (ibfd->xvec == obfd->xvec)
7651 {
cb3ff1e5
NC
7652 /* Check to see if any sections in the input BFD
7653 covered by ELF program header have changed. */
d55ce4e2 7654 Elf_Internal_Phdr *segment;
84d1d650
L
7655 asection *section, *osec;
7656 unsigned int i, num_segments;
7657 Elf_Internal_Shdr *this_hdr;
147d51c2
L
7658 const struct elf_backend_data *bed;
7659
7660 bed = get_elf_backend_data (ibfd);
7661
7662 /* Regenerate the segment map if p_paddr is set to 0. */
7663 if (bed->want_p_paddr_set_to_zero)
7664 goto rewrite;
84d1d650
L
7665
7666 /* Initialize the segment mark field. */
7667 for (section = obfd->sections; section != NULL;
7668 section = section->next)
7669 section->segment_mark = FALSE;
7670
7671 num_segments = elf_elfheader (ibfd)->e_phnum;
7672 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7673 i < num_segments;
7674 i++, segment++)
7675 {
5f6999aa
NC
7676 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7677 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7678 which severly confuses things, so always regenerate the segment
7679 map in this case. */
7680 if (segment->p_paddr == 0
7681 && segment->p_memsz == 0
7682 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
cb3ff1e5 7683 goto rewrite;
5f6999aa 7684
84d1d650
L
7685 for (section = ibfd->sections;
7686 section != NULL; section = section->next)
7687 {
7688 /* We mark the output section so that we know it comes
7689 from the input BFD. */
7690 osec = section->output_section;
7691 if (osec)
7692 osec->segment_mark = TRUE;
7693
7694 /* Check if this section is covered by the segment. */
7695 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7696 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
84d1d650
L
7697 {
7698 /* FIXME: Check if its output section is changed or
7699 removed. What else do we need to check? */
7700 if (osec == NULL
7701 || section->flags != osec->flags
7702 || section->lma != osec->lma
7703 || section->vma != osec->vma
7704 || section->size != osec->size
7705 || section->rawsize != osec->rawsize
7706 || section->alignment_power != osec->alignment_power)
7707 goto rewrite;
7708 }
7709 }
7710 }
7711
cb3ff1e5 7712 /* Check to see if any output section do not come from the
84d1d650
L
7713 input BFD. */
7714 for (section = obfd->sections; section != NULL;
7715 section = section->next)
7716 {
535b785f 7717 if (!section->segment_mark)
84d1d650
L
7718 goto rewrite;
7719 else
7720 section->segment_mark = FALSE;
7721 }
7722
7723 return copy_elf_program_header (ibfd, obfd);
7724 }
7725
dc1e8a47 7726 rewrite:
f1d85785
L
7727 if (ibfd->xvec == obfd->xvec)
7728 {
7729 /* When rewriting program header, set the output maxpagesize to
7730 the maximum alignment of input PT_LOAD segments. */
7731 Elf_Internal_Phdr *segment;
7732 unsigned int i;
7733 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
7734 bfd_vma maxpagesize = 0;
7735
7736 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7737 i < num_segments;
7738 i++, segment++)
7739 if (segment->p_type == PT_LOAD
7740 && maxpagesize < segment->p_align)
c86934ce
NC
7741 {
7742 /* PR 17512: file: f17299af. */
7743 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
695344c0 7744 /* xgettext:c-format */
2dcf00ce
AM
7745 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7746 PRIx64 " is too large"),
7747 ibfd, (uint64_t) segment->p_align);
c86934ce
NC
7748 else
7749 maxpagesize = segment->p_align;
7750 }
f1d85785
L
7751
7752 if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize)
7753 bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize);
7754 }
7755
84d1d650
L
7756 return rewrite_elf_program_header (ibfd, obfd);
7757}
7758
ccd2ec6a
L
7759/* Initialize private output section information from input section. */
7760
7761bfd_boolean
7762_bfd_elf_init_private_section_data (bfd *ibfd,
7763 asection *isec,
7764 bfd *obfd,
7765 asection *osec,
7766 struct bfd_link_info *link_info)
7767
7768{
7769 Elf_Internal_Shdr *ihdr, *ohdr;
0e1862bb
L
7770 bfd_boolean final_link = (link_info != NULL
7771 && !bfd_link_relocatable (link_info));
ccd2ec6a
L
7772
7773 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7774 || obfd->xvec->flavour != bfd_target_elf_flavour)
7775 return TRUE;
7776
ba85c43e
NC
7777 BFD_ASSERT (elf_section_data (osec) != NULL);
7778
8c803a2d
AM
7779 /* If this is a known ABI section, ELF section type and flags may
7780 have been set up when OSEC was created. For normal sections we
7781 allow the user to override the type and flags other than
7782 SHF_MASKOS and SHF_MASKPROC. */
7783 if (elf_section_type (osec) == SHT_PROGBITS
7784 || elf_section_type (osec) == SHT_NOTE
7785 || elf_section_type (osec) == SHT_NOBITS)
7786 elf_section_type (osec) = SHT_NULL;
7787 /* For objcopy and relocatable link, copy the ELF section type from
7788 the input file if the BFD section flags are the same. (If they
7789 are different the user may be doing something like
7790 "objcopy --set-section-flags .text=alloc,data".) For a final
7791 link allow some flags that the linker clears to differ. */
42bb2e33 7792 if (elf_section_type (osec) == SHT_NULL
dfa7b0b8
AM
7793 && (osec->flags == isec->flags
7794 || (final_link
7795 && ((osec->flags ^ isec->flags)
0814be7d 7796 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
42bb2e33 7797 elf_section_type (osec) = elf_section_type (isec);
d270463e
L
7798
7799 /* FIXME: Is this correct for all OS/PROC specific flags? */
8c803a2d
AM
7800 elf_section_flags (osec) = (elf_section_flags (isec)
7801 & (SHF_MASKOS | SHF_MASKPROC));
ccd2ec6a 7802
a91e1603 7803 /* Copy sh_info from input for mbind section. */
df3a023b
AM
7804 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
7805 && elf_section_flags (isec) & SHF_GNU_MBIND)
a91e1603
L
7806 elf_section_data (osec)->this_hdr.sh_info
7807 = elf_section_data (isec)->this_hdr.sh_info;
7808
ccd2ec6a
L
7809 /* Set things up for objcopy and relocatable link. The output
7810 SHT_GROUP section will have its elf_next_in_group pointing back
7811 to the input group members. Ignore linker created group section.
7812 See elfNN_ia64_object_p in elfxx-ia64.c. */
7bdf4127
AB
7813 if ((link_info == NULL
7814 || !link_info->resolve_section_groups)
7815 && (elf_sec_group (isec) == NULL
7816 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
ccd2ec6a 7817 {
7bdf4127
AB
7818 if (elf_section_flags (isec) & SHF_GROUP)
7819 elf_section_flags (osec) |= SHF_GROUP;
7820 elf_next_in_group (osec) = elf_next_in_group (isec);
7821 elf_section_data (osec)->group = elf_section_data (isec)->group;
ccd2ec6a
L
7822 }
7823
7bdf4127
AB
7824 /* If not decompress, preserve SHF_COMPRESSED. */
7825 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
7826 elf_section_flags (osec) |= (elf_section_flags (isec)
7827 & SHF_COMPRESSED);
7828
ccd2ec6a
L
7829 ihdr = &elf_section_data (isec)->this_hdr;
7830
7831 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7832 don't use the output section of the linked-to section since it
7833 may be NULL at this point. */
7834 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
7835 {
7836 ohdr = &elf_section_data (osec)->this_hdr;
7837 ohdr->sh_flags |= SHF_LINK_ORDER;
7838 elf_linked_to_section (osec) = elf_linked_to_section (isec);
7839 }
7840
7841 osec->use_rela_p = isec->use_rela_p;
7842
7843 return TRUE;
7844}
7845
252b5132
RH
7846/* Copy private section information. This copies over the entsize
7847 field, and sometimes the info field. */
7848
b34976b6 7849bfd_boolean
217aa764
AM
7850_bfd_elf_copy_private_section_data (bfd *ibfd,
7851 asection *isec,
7852 bfd *obfd,
7853 asection *osec)
252b5132
RH
7854{
7855 Elf_Internal_Shdr *ihdr, *ohdr;
7856
7857 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7858 || obfd->xvec->flavour != bfd_target_elf_flavour)
b34976b6 7859 return TRUE;
252b5132 7860
252b5132
RH
7861 ihdr = &elf_section_data (isec)->this_hdr;
7862 ohdr = &elf_section_data (osec)->this_hdr;
7863
7864 ohdr->sh_entsize = ihdr->sh_entsize;
7865
7866 if (ihdr->sh_type == SHT_SYMTAB
7867 || ihdr->sh_type == SHT_DYNSYM
7868 || ihdr->sh_type == SHT_GNU_verneed
7869 || ihdr->sh_type == SHT_GNU_verdef)
7870 ohdr->sh_info = ihdr->sh_info;
7871
ccd2ec6a
L
7872 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
7873 NULL);
252b5132
RH
7874}
7875
d0bf826b
AM
7876/* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7877 necessary if we are removing either the SHT_GROUP section or any of
7878 the group member sections. DISCARDED is the value that a section's
7879 output_section has if the section will be discarded, NULL when this
7880 function is called from objcopy, bfd_abs_section_ptr when called
7881 from the linker. */
80fccad2
BW
7882
7883bfd_boolean
d0bf826b 7884_bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
80fccad2 7885{
30288845
AM
7886 asection *isec;
7887
30288845 7888 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
415f38a6 7889 if (elf_section_type (isec) == SHT_GROUP)
30288845
AM
7890 {
7891 asection *first = elf_next_in_group (isec);
7892 asection *s = first;
d0bf826b
AM
7893 bfd_size_type removed = 0;
7894
30288845
AM
7895 while (s != NULL)
7896 {
415f38a6
AM
7897 /* If this member section is being output but the
7898 SHT_GROUP section is not, then clear the group info
7899 set up by _bfd_elf_copy_private_section_data. */
d0bf826b
AM
7900 if (s->output_section != discarded
7901 && isec->output_section == discarded)
30288845
AM
7902 {
7903 elf_section_flags (s->output_section) &= ~SHF_GROUP;
7904 elf_group_name (s->output_section) = NULL;
7905 }
3349112e 7906 else
6e5e9d58
AM
7907 {
7908 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
3349112e
L
7909 if (s->output_section == discarded
7910 && isec->output_section != discarded)
7911 {
7912 /* Conversely, if the member section is not being
7913 output but the SHT_GROUP section is, then adjust
7914 its size. */
7915 removed += 4;
7916 if (elf_sec->rel.hdr != NULL
7917 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
7918 removed += 4;
7919 if (elf_sec->rela.hdr != NULL
7920 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
7921 removed += 4;
7922 }
7923 else
7924 {
7925 /* Also adjust for zero-sized relocation member
7926 section. */
7927 if (elf_sec->rel.hdr != NULL
7928 && elf_sec->rel.hdr->sh_size == 0)
7929 removed += 4;
7930 if (elf_sec->rela.hdr != NULL
7931 && elf_sec->rela.hdr->sh_size == 0)
7932 removed += 4;
7933 }
6e5e9d58 7934 }
30288845
AM
7935 s = elf_next_in_group (s);
7936 if (s == first)
7937 break;
7938 }
d0bf826b
AM
7939 if (removed != 0)
7940 {
7941 if (discarded != NULL)
7942 {
7943 /* If we've been called for ld -r, then we need to
6e5e9d58 7944 adjust the input section size. */
d0bf826b
AM
7945 if (isec->rawsize == 0)
7946 isec->rawsize = isec->size;
7947 isec->size = isec->rawsize - removed;
6e5e9d58
AM
7948 if (isec->size <= 4)
7949 {
7950 isec->size = 0;
7951 isec->flags |= SEC_EXCLUDE;
7952 }
d0bf826b
AM
7953 }
7954 else
7955 {
7956 /* Adjust the output section size when called from
7957 objcopy. */
7958 isec->output_section->size -= removed;
6e5e9d58
AM
7959 if (isec->output_section->size <= 4)
7960 {
7961 isec->output_section->size = 0;
7962 isec->output_section->flags |= SEC_EXCLUDE;
7963 }
d0bf826b
AM
7964 }
7965 }
30288845
AM
7966 }
7967
80fccad2
BW
7968 return TRUE;
7969}
7970
d0bf826b
AM
7971/* Copy private header information. */
7972
7973bfd_boolean
7974_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
7975{
7976 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7977 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7978 return TRUE;
7979
7980 /* Copy over private BFD data if it has not already been copied.
7981 This must be done here, rather than in the copy_private_bfd_data
7982 entry point, because the latter is called after the section
7983 contents have been set, which means that the program headers have
7984 already been worked out. */
12bd6957 7985 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
d0bf826b
AM
7986 {
7987 if (! copy_private_bfd_data (ibfd, obfd))
7988 return FALSE;
7989 }
7990
7991 return _bfd_elf_fixup_group_sections (ibfd, NULL);
7992}
7993
252b5132
RH
7994/* Copy private symbol information. If this symbol is in a section
7995 which we did not map into a BFD section, try to map the section
7996 index correctly. We use special macro definitions for the mapped
7997 section indices; these definitions are interpreted by the
7998 swap_out_syms function. */
7999
9ad5cbcf
AM
8000#define MAP_ONESYMTAB (SHN_HIOS + 1)
8001#define MAP_DYNSYMTAB (SHN_HIOS + 2)
8002#define MAP_STRTAB (SHN_HIOS + 3)
8003#define MAP_SHSTRTAB (SHN_HIOS + 4)
8004#define MAP_SYM_SHNDX (SHN_HIOS + 5)
252b5132 8005
b34976b6 8006bfd_boolean
217aa764
AM
8007_bfd_elf_copy_private_symbol_data (bfd *ibfd,
8008 asymbol *isymarg,
8009 bfd *obfd,
8010 asymbol *osymarg)
252b5132
RH
8011{
8012 elf_symbol_type *isym, *osym;
8013
8014 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8015 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 8016 return TRUE;
252b5132 8017
c1229f84
AM
8018 isym = elf_symbol_from (isymarg);
8019 osym = elf_symbol_from (osymarg);
252b5132
RH
8020
8021 if (isym != NULL
8424d8f5 8022 && isym->internal_elf_sym.st_shndx != 0
252b5132
RH
8023 && osym != NULL
8024 && bfd_is_abs_section (isym->symbol.section))
8025 {
8026 unsigned int shndx;
8027
8028 shndx = isym->internal_elf_sym.st_shndx;
8029 if (shndx == elf_onesymtab (ibfd))
8030 shndx = MAP_ONESYMTAB;
8031 else if (shndx == elf_dynsymtab (ibfd))
8032 shndx = MAP_DYNSYMTAB;
12bd6957 8033 else if (shndx == elf_strtab_sec (ibfd))
252b5132 8034 shndx = MAP_STRTAB;
12bd6957 8035 else if (shndx == elf_shstrtab_sec (ibfd))
252b5132 8036 shndx = MAP_SHSTRTAB;
6a40cf0c 8037 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
9ad5cbcf 8038 shndx = MAP_SYM_SHNDX;
252b5132
RH
8039 osym->internal_elf_sym.st_shndx = shndx;
8040 }
8041
b34976b6 8042 return TRUE;
252b5132
RH
8043}
8044
8045/* Swap out the symbols. */
8046
b34976b6 8047static bfd_boolean
217aa764 8048swap_out_syms (bfd *abfd,
ef10c3ac 8049 struct elf_strtab_hash **sttp,
217aa764 8050 int relocatable_p)
252b5132 8051{
9c5bfbb7 8052 const struct elf_backend_data *bed;
1f4361a7 8053 unsigned int symcount;
079e9a2f 8054 asymbol **syms;
ef10c3ac 8055 struct elf_strtab_hash *stt;
079e9a2f 8056 Elf_Internal_Shdr *symtab_hdr;
9ad5cbcf 8057 Elf_Internal_Shdr *symtab_shndx_hdr;
079e9a2f 8058 Elf_Internal_Shdr *symstrtab_hdr;
ef10c3ac 8059 struct elf_sym_strtab *symstrtab;
f075ee0c
AM
8060 bfd_byte *outbound_syms;
8061 bfd_byte *outbound_shndx;
ef10c3ac
L
8062 unsigned long outbound_syms_index;
8063 unsigned long outbound_shndx_index;
1f4361a7 8064 unsigned int idx;
12bd6957 8065 unsigned int num_locals;
1f4361a7 8066 size_t amt;
174fd7f9 8067 bfd_boolean name_local_sections;
252b5132 8068
12bd6957 8069 if (!elf_map_symbols (abfd, &num_locals))
b34976b6 8070 return FALSE;
252b5132 8071
c044fabd 8072 /* Dump out the symtabs. */
ef10c3ac 8073 stt = _bfd_elf_strtab_init ();
079e9a2f 8074 if (stt == NULL)
b34976b6 8075 return FALSE;
252b5132 8076
079e9a2f
AM
8077 bed = get_elf_backend_data (abfd);
8078 symcount = bfd_get_symcount (abfd);
8079 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8080 symtab_hdr->sh_type = SHT_SYMTAB;
8081 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8082 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
12bd6957 8083 symtab_hdr->sh_info = num_locals + 1;
72de5009 8084 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
079e9a2f
AM
8085
8086 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8087 symstrtab_hdr->sh_type = SHT_STRTAB;
8088
ef10c3ac 8089 /* Allocate buffer to swap out the .strtab section. */
1f4361a7
AM
8090 if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8091 || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
ef10c3ac 8092 {
1f4361a7 8093 bfd_set_error (bfd_error_no_memory);
ef10c3ac
L
8094 _bfd_elf_strtab_free (stt);
8095 return FALSE;
8096 }
8097
1f4361a7
AM
8098 if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8099 || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
5ed6aba4 8100 {
1f4361a7
AM
8101 error_no_mem:
8102 bfd_set_error (bfd_error_no_memory);
8103 error_return:
ef10c3ac 8104 free (symstrtab);
1f4361a7 8105 _bfd_elf_strtab_free (stt);
5ed6aba4
NC
8106 return FALSE;
8107 }
217aa764 8108 symtab_hdr->contents = outbound_syms;
ef10c3ac 8109 outbound_syms_index = 0;
252b5132 8110
9ad5cbcf 8111 outbound_shndx = NULL;
ef10c3ac 8112 outbound_shndx_index = 0;
6a40cf0c
NC
8113
8114 if (elf_symtab_shndx_list (abfd))
9ad5cbcf 8115 {
6a40cf0c
NC
8116 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8117 if (symtab_shndx_hdr->sh_name != 0)
8118 {
1f4361a7
AM
8119 if (_bfd_mul_overflow (symcount + 1,
8120 sizeof (Elf_External_Sym_Shndx), &amt))
8121 goto error_no_mem;
8122 outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt);
6a40cf0c
NC
8123 if (outbound_shndx == NULL)
8124 goto error_return;
5ed6aba4 8125
6a40cf0c
NC
8126 symtab_shndx_hdr->contents = outbound_shndx;
8127 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8128 symtab_shndx_hdr->sh_size = amt;
8129 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8130 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8131 }
8132 /* FIXME: What about any other headers in the list ? */
9ad5cbcf
AM
8133 }
8134
589e6347 8135 /* Now generate the data (for "contents"). */
079e9a2f
AM
8136 {
8137 /* Fill in zeroth symbol and swap it out. */
8138 Elf_Internal_Sym sym;
8139 sym.st_name = 0;
8140 sym.st_value = 0;
8141 sym.st_size = 0;
8142 sym.st_info = 0;
8143 sym.st_other = 0;
8144 sym.st_shndx = SHN_UNDEF;
35fc36a8 8145 sym.st_target_internal = 0;
ef10c3ac
L
8146 symstrtab[0].sym = sym;
8147 symstrtab[0].dest_index = outbound_syms_index;
8148 symstrtab[0].destshndx_index = outbound_shndx_index;
8149 outbound_syms_index++;
9ad5cbcf 8150 if (outbound_shndx != NULL)
ef10c3ac 8151 outbound_shndx_index++;
079e9a2f 8152 }
252b5132 8153
174fd7f9
RS
8154 name_local_sections
8155 = (bed->elf_backend_name_local_section_symbols
8156 && bed->elf_backend_name_local_section_symbols (abfd));
8157
079e9a2f 8158 syms = bfd_get_outsymbols (abfd);
ef10c3ac 8159 for (idx = 0; idx < symcount;)
252b5132 8160 {
252b5132 8161 Elf_Internal_Sym sym;
079e9a2f
AM
8162 bfd_vma value = syms[idx]->value;
8163 elf_symbol_type *type_ptr;
8164 flagword flags = syms[idx]->flags;
8165 int type;
252b5132 8166
174fd7f9
RS
8167 if (!name_local_sections
8168 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
079e9a2f
AM
8169 {
8170 /* Local section symbols have no name. */
ef10c3ac 8171 sym.st_name = (unsigned long) -1;
079e9a2f
AM
8172 }
8173 else
8174 {
ef10c3ac
L
8175 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8176 to get the final offset for st_name. */
8177 sym.st_name
8178 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
8179 FALSE);
079e9a2f 8180 if (sym.st_name == (unsigned long) -1)
ef10c3ac 8181 goto error_return;
079e9a2f 8182 }
252b5132 8183
c1229f84 8184 type_ptr = elf_symbol_from (syms[idx]);
252b5132 8185
079e9a2f
AM
8186 if ((flags & BSF_SECTION_SYM) == 0
8187 && bfd_is_com_section (syms[idx]->section))
8188 {
8189 /* ELF common symbols put the alignment into the `value' field,
8190 and the size into the `size' field. This is backwards from
8191 how BFD handles it, so reverse it here. */
8192 sym.st_size = value;
8193 if (type_ptr == NULL
8194 || type_ptr->internal_elf_sym.st_value == 0)
8195 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8196 else
8197 sym.st_value = type_ptr->internal_elf_sym.st_value;
8198 sym.st_shndx = _bfd_elf_section_from_bfd_section
8199 (abfd, syms[idx]->section);
8200 }
8201 else
8202 {
8203 asection *sec = syms[idx]->section;
cb33740c 8204 unsigned int shndx;
252b5132 8205
079e9a2f
AM
8206 if (sec->output_section)
8207 {
8208 value += sec->output_offset;
8209 sec = sec->output_section;
8210 }
589e6347 8211
079e9a2f
AM
8212 /* Don't add in the section vma for relocatable output. */
8213 if (! relocatable_p)
8214 value += sec->vma;
8215 sym.st_value = value;
8216 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8217
8218 if (bfd_is_abs_section (sec)
8219 && type_ptr != NULL
8220 && type_ptr->internal_elf_sym.st_shndx != 0)
8221 {
8222 /* This symbol is in a real ELF section which we did
8223 not create as a BFD section. Undo the mapping done
8224 by copy_private_symbol_data. */
8225 shndx = type_ptr->internal_elf_sym.st_shndx;
8226 switch (shndx)
8227 {
8228 case MAP_ONESYMTAB:
8229 shndx = elf_onesymtab (abfd);
8230 break;
8231 case MAP_DYNSYMTAB:
8232 shndx = elf_dynsymtab (abfd);
8233 break;
8234 case MAP_STRTAB:
12bd6957 8235 shndx = elf_strtab_sec (abfd);
079e9a2f
AM
8236 break;
8237 case MAP_SHSTRTAB:
12bd6957 8238 shndx = elf_shstrtab_sec (abfd);
079e9a2f 8239 break;
9ad5cbcf 8240 case MAP_SYM_SHNDX:
6a40cf0c
NC
8241 if (elf_symtab_shndx_list (abfd))
8242 shndx = elf_symtab_shndx_list (abfd)->ndx;
9ad5cbcf 8243 break;
00e49dff
NC
8244 case SHN_COMMON:
8245 case SHN_ABS:
15bc576a 8246 shndx = SHN_ABS;
079e9a2f 8247 break;
00e49dff
NC
8248 default:
8249 if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8250 {
8251 if (bed->symbol_section_index)
8252 shndx = bed->symbol_section_index (abfd, type_ptr);
8253 /* Otherwise just leave the index alone. */
8254 }
8255 else
8256 {
8257 if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8258 _bfd_error_handler (_("%pB: \
8259Unable to handle section index %x in ELF symbol. Using ABS instead."),
8260 abfd, shndx);
8261 shndx = SHN_ABS;
8262 }
8263 break;
079e9a2f
AM
8264 }
8265 }
8266 else
8267 {
8268 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
252b5132 8269
cb33740c 8270 if (shndx == SHN_BAD)
079e9a2f
AM
8271 {
8272 asection *sec2;
8273
8274 /* Writing this would be a hell of a lot easier if
8275 we had some decent documentation on bfd, and
8276 knew what to expect of the library, and what to
8277 demand of applications. For example, it
8278 appears that `objcopy' might not set the
8279 section of a symbol to be a section that is
8280 actually in the output file. */
8281 sec2 = bfd_get_section_by_name (abfd, sec->name);
5df1bc57
AM
8282 if (sec2 != NULL)
8283 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8284 if (shndx == SHN_BAD)
589e6347 8285 {
695344c0 8286 /* xgettext:c-format */
9793eb77
AM
8287 _bfd_error_handler
8288 (_("unable to find equivalent output section"
8289 " for symbol '%s' from section '%s'"),
8290 syms[idx]->name ? syms[idx]->name : "<Local sym>",
8291 sec->name);
811072d8 8292 bfd_set_error (bfd_error_invalid_operation);
ef10c3ac 8293 goto error_return;
589e6347 8294 }
079e9a2f
AM
8295 }
8296 }
252b5132 8297
079e9a2f
AM
8298 sym.st_shndx = shndx;
8299 }
252b5132 8300
13ae64f3
JJ
8301 if ((flags & BSF_THREAD_LOCAL) != 0)
8302 type = STT_TLS;
d8045f23
NC
8303 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8304 type = STT_GNU_IFUNC;
13ae64f3 8305 else if ((flags & BSF_FUNCTION) != 0)
079e9a2f
AM
8306 type = STT_FUNC;
8307 else if ((flags & BSF_OBJECT) != 0)
8308 type = STT_OBJECT;
d9352518
DB
8309 else if ((flags & BSF_RELC) != 0)
8310 type = STT_RELC;
8311 else if ((flags & BSF_SRELC) != 0)
8312 type = STT_SRELC;
079e9a2f
AM
8313 else
8314 type = STT_NOTYPE;
252b5132 8315
13ae64f3
JJ
8316 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8317 type = STT_TLS;
8318
589e6347 8319 /* Processor-specific types. */
079e9a2f
AM
8320 if (type_ptr != NULL
8321 && bed->elf_backend_get_symbol_type)
8322 type = ((*bed->elf_backend_get_symbol_type)
8323 (&type_ptr->internal_elf_sym, type));
252b5132 8324
079e9a2f
AM
8325 if (flags & BSF_SECTION_SYM)
8326 {
8327 if (flags & BSF_GLOBAL)
8328 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8329 else
8330 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8331 }
8332 else if (bfd_is_com_section (syms[idx]->section))
0a40daed 8333 {
b8871f35
L
8334 if (type != STT_TLS)
8335 {
8336 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8337 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8338 ? STT_COMMON : STT_OBJECT);
8339 else
8340 type = ((flags & BSF_ELF_COMMON) != 0
8341 ? STT_COMMON : STT_OBJECT);
8342 }
8343 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
0a40daed 8344 }
079e9a2f
AM
8345 else if (bfd_is_und_section (syms[idx]->section))
8346 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8347 ? STB_WEAK
8348 : STB_GLOBAL),
8349 type);
8350 else if (flags & BSF_FILE)
8351 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8352 else
8353 {
8354 int bind = STB_LOCAL;
252b5132 8355
079e9a2f
AM
8356 if (flags & BSF_LOCAL)
8357 bind = STB_LOCAL;
3e7a7d11
NC
8358 else if (flags & BSF_GNU_UNIQUE)
8359 bind = STB_GNU_UNIQUE;
079e9a2f
AM
8360 else if (flags & BSF_WEAK)
8361 bind = STB_WEAK;
8362 else if (flags & BSF_GLOBAL)
8363 bind = STB_GLOBAL;
252b5132 8364
079e9a2f
AM
8365 sym.st_info = ELF_ST_INFO (bind, type);
8366 }
252b5132 8367
079e9a2f 8368 if (type_ptr != NULL)
35fc36a8
RS
8369 {
8370 sym.st_other = type_ptr->internal_elf_sym.st_other;
8371 sym.st_target_internal
8372 = type_ptr->internal_elf_sym.st_target_internal;
8373 }
079e9a2f 8374 else
35fc36a8
RS
8375 {
8376 sym.st_other = 0;
8377 sym.st_target_internal = 0;
8378 }
252b5132 8379
ef10c3ac
L
8380 idx++;
8381 symstrtab[idx].sym = sym;
8382 symstrtab[idx].dest_index = outbound_syms_index;
8383 symstrtab[idx].destshndx_index = outbound_shndx_index;
8384
8385 outbound_syms_index++;
9ad5cbcf 8386 if (outbound_shndx != NULL)
ef10c3ac
L
8387 outbound_shndx_index++;
8388 }
8389
8390 /* Finalize the .strtab section. */
8391 _bfd_elf_strtab_finalize (stt);
8392
8393 /* Swap out the .strtab section. */
8394 for (idx = 0; idx <= symcount; idx++)
8395 {
8396 struct elf_sym_strtab *elfsym = &symstrtab[idx];
8397 if (elfsym->sym.st_name == (unsigned long) -1)
8398 elfsym->sym.st_name = 0;
8399 else
8400 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8401 elfsym->sym.st_name);
8402 bed->s->swap_symbol_out (abfd, &elfsym->sym,
8403 (outbound_syms
8404 + (elfsym->dest_index
8405 * bed->s->sizeof_sym)),
8406 (outbound_shndx
8407 + (elfsym->destshndx_index
8408 * sizeof (Elf_External_Sym_Shndx))));
079e9a2f 8409 }
ef10c3ac 8410 free (symstrtab);
252b5132 8411
079e9a2f 8412 *sttp = stt;
ef10c3ac 8413 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
079e9a2f 8414 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 8415 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
079e9a2f
AM
8416 symstrtab_hdr->sh_addr = 0;
8417 symstrtab_hdr->sh_entsize = 0;
8418 symstrtab_hdr->sh_link = 0;
8419 symstrtab_hdr->sh_info = 0;
8420 symstrtab_hdr->sh_addralign = 1;
252b5132 8421
b34976b6 8422 return TRUE;
252b5132
RH
8423}
8424
8425/* Return the number of bytes required to hold the symtab vector.
8426
8427 Note that we base it on the count plus 1, since we will null terminate
8428 the vector allocated based on this size. However, the ELF symbol table
8429 always has a dummy entry as symbol #0, so it ends up even. */
8430
8431long
217aa764 8432_bfd_elf_get_symtab_upper_bound (bfd *abfd)
252b5132 8433{
3a551c7a 8434 bfd_size_type symcount;
252b5132
RH
8435 long symtab_size;
8436 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
8437
8438 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
b5f386d5 8439 if (symcount > LONG_MAX / sizeof (asymbol *))
3a551c7a
AM
8440 {
8441 bfd_set_error (bfd_error_file_too_big);
8442 return -1;
8443 }
b5f386d5
AM
8444 symtab_size = symcount * (sizeof (asymbol *));
8445 if (symcount == 0)
8446 symtab_size = sizeof (asymbol *);
8447 else if (!bfd_write_p (abfd))
8448 {
8449 ufile_ptr filesize = bfd_get_file_size (abfd);
8450
8451 if (filesize != 0 && (unsigned long) symtab_size > filesize)
8452 {
8453 bfd_set_error (bfd_error_file_truncated);
8454 return -1;
8455 }
8456 }
252b5132
RH
8457
8458 return symtab_size;
8459}
8460
8461long
217aa764 8462_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
252b5132 8463{
3a551c7a 8464 bfd_size_type symcount;
252b5132
RH
8465 long symtab_size;
8466 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
8467
8468 if (elf_dynsymtab (abfd) == 0)
8469 {
8470 bfd_set_error (bfd_error_invalid_operation);
8471 return -1;
8472 }
8473
8474 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
b5f386d5 8475 if (symcount > LONG_MAX / sizeof (asymbol *))
3a551c7a
AM
8476 {
8477 bfd_set_error (bfd_error_file_too_big);
8478 return -1;
8479 }
b5f386d5
AM
8480 symtab_size = symcount * (sizeof (asymbol *));
8481 if (symcount == 0)
8482 symtab_size = sizeof (asymbol *);
8483 else if (!bfd_write_p (abfd))
8484 {
8485 ufile_ptr filesize = bfd_get_file_size (abfd);
8486
8487 if (filesize != 0 && (unsigned long) symtab_size > filesize)
8488 {
8489 bfd_set_error (bfd_error_file_truncated);
8490 return -1;
8491 }
8492 }
252b5132
RH
8493
8494 return symtab_size;
8495}
8496
8497long
3c568b8a 8498_bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
252b5132 8499{
b5f386d5 8500 if (asect->reloc_count != 0 && !bfd_write_p (abfd))
3c568b8a
AM
8501 {
8502 /* Sanity check reloc section size. */
8503 struct bfd_elf_section_data *d = elf_section_data (asect);
8504 Elf_Internal_Shdr *rel_hdr = &d->this_hdr;
8505 bfd_size_type ext_rel_size = rel_hdr->sh_size;
8506 ufile_ptr filesize = bfd_get_file_size (abfd);
8507
8508 if (filesize != 0 && ext_rel_size > filesize)
8509 {
8510 bfd_set_error (bfd_error_file_truncated);
8511 return -1;
8512 }
8513 }
8514
242a1159 8515#if SIZEOF_LONG == SIZEOF_INT
7a6e0d89
AM
8516 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
8517 {
8518 bfd_set_error (bfd_error_file_too_big);
8519 return -1;
8520 }
242a1159 8521#endif
252b5132
RH
8522 return (asect->reloc_count + 1) * sizeof (arelent *);
8523}
8524
8525/* Canonicalize the relocs. */
8526
8527long
217aa764
AM
8528_bfd_elf_canonicalize_reloc (bfd *abfd,
8529 sec_ptr section,
8530 arelent **relptr,
8531 asymbol **symbols)
252b5132
RH
8532{
8533 arelent *tblptr;
8534 unsigned int i;
9c5bfbb7 8535 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 8536
b34976b6 8537 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
252b5132
RH
8538 return -1;
8539
8540 tblptr = section->relocation;
8541 for (i = 0; i < section->reloc_count; i++)
8542 *relptr++ = tblptr++;
8543
8544 *relptr = NULL;
8545
8546 return section->reloc_count;
8547}
8548
8549long
6cee3f79 8550_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
252b5132 8551{
9c5bfbb7 8552 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764 8553 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
252b5132
RH
8554
8555 if (symcount >= 0)
ed48ec2e 8556 abfd->symcount = symcount;
252b5132
RH
8557 return symcount;
8558}
8559
8560long
217aa764
AM
8561_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
8562 asymbol **allocation)
252b5132 8563{
9c5bfbb7 8564 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764 8565 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
1f70368c
DJ
8566
8567 if (symcount >= 0)
ed48ec2e 8568 abfd->dynsymcount = symcount;
1f70368c 8569 return symcount;
252b5132
RH
8570}
8571
8615f3f2
AM
8572/* Return the size required for the dynamic reloc entries. Any loadable
8573 section that was actually installed in the BFD, and has type SHT_REL
8574 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8575 dynamic reloc section. */
252b5132
RH
8576
8577long
217aa764 8578_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
252b5132 8579{
3c568b8a 8580 bfd_size_type count, ext_rel_size;
252b5132
RH
8581 asection *s;
8582
8583 if (elf_dynsymtab (abfd) == 0)
8584 {
8585 bfd_set_error (bfd_error_invalid_operation);
8586 return -1;
8587 }
8588
3a551c7a 8589 count = 1;
3c568b8a 8590 ext_rel_size = 0;
252b5132 8591 for (s = abfd->sections; s != NULL; s = s->next)
266b05cf 8592 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132
RH
8593 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8594 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
3a551c7a 8595 {
3c568b8a
AM
8596 ext_rel_size += s->size;
8597 if (ext_rel_size < s->size)
8598 {
8599 bfd_set_error (bfd_error_file_truncated);
8600 return -1;
8601 }
3a551c7a
AM
8602 count += s->size / elf_section_data (s)->this_hdr.sh_entsize;
8603 if (count > LONG_MAX / sizeof (arelent *))
8604 {
8605 bfd_set_error (bfd_error_file_too_big);
8606 return -1;
8607 }
8608 }
b5f386d5 8609 if (count > 1 && !bfd_write_p (abfd))
3c568b8a
AM
8610 {
8611 /* Sanity check reloc section sizes. */
8612 ufile_ptr filesize = bfd_get_file_size (abfd);
8613 if (filesize != 0 && ext_rel_size > filesize)
8614 {
8615 bfd_set_error (bfd_error_file_truncated);
8616 return -1;
8617 }
8618 }
3a551c7a 8619 return count * sizeof (arelent *);
252b5132
RH
8620}
8621
8615f3f2
AM
8622/* Canonicalize the dynamic relocation entries. Note that we return the
8623 dynamic relocations as a single block, although they are actually
8624 associated with particular sections; the interface, which was
8625 designed for SunOS style shared libraries, expects that there is only
8626 one set of dynamic relocs. Any loadable section that was actually
8627 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8628 dynamic symbol table, is considered to be a dynamic reloc section. */
252b5132
RH
8629
8630long
217aa764
AM
8631_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
8632 arelent **storage,
8633 asymbol **syms)
252b5132 8634{
217aa764 8635 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
252b5132
RH
8636 asection *s;
8637 long ret;
8638
8639 if (elf_dynsymtab (abfd) == 0)
8640 {
8641 bfd_set_error (bfd_error_invalid_operation);
8642 return -1;
8643 }
8644
8645 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8646 ret = 0;
8647 for (s = abfd->sections; s != NULL; s = s->next)
8648 {
266b05cf 8649 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132
RH
8650 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8651 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8652 {
8653 arelent *p;
8654 long count, i;
8655
b34976b6 8656 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
252b5132 8657 return -1;
eea6121a 8658 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
252b5132
RH
8659 p = s->relocation;
8660 for (i = 0; i < count; i++)
8661 *storage++ = p++;
8662 ret += count;
8663 }
8664 }
8665
8666 *storage = NULL;
8667
8668 return ret;
8669}
8670\f
8671/* Read in the version information. */
8672
b34976b6 8673bfd_boolean
fc0e6df6 8674_bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
252b5132
RH
8675{
8676 bfd_byte *contents = NULL;
fc0e6df6 8677 unsigned int freeidx = 0;
1f4361a7 8678 size_t amt;
fc0e6df6
PB
8679
8680 if (elf_dynverref (abfd) != 0)
8681 {
8682 Elf_Internal_Shdr *hdr;
8683 Elf_External_Verneed *everneed;
8684 Elf_Internal_Verneed *iverneed;
8685 unsigned int i;
d0fb9a8d 8686 bfd_byte *contents_end;
fc0e6df6
PB
8687
8688 hdr = &elf_tdata (abfd)->dynverref_hdr;
8689
bd61e135
AM
8690 if (hdr->sh_info == 0
8691 || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
d0fb9a8d 8692 {
dc1e8a47 8693 error_return_bad_verref:
4eca0228 8694 _bfd_error_handler
871b3ab2 8695 (_("%pB: .gnu.version_r invalid entry"), abfd);
601a03ba 8696 bfd_set_error (bfd_error_bad_value);
dc1e8a47 8697 error_return_verref:
d0fb9a8d
JJ
8698 elf_tdata (abfd)->verref = NULL;
8699 elf_tdata (abfd)->cverrefs = 0;
8700 goto error_return;
8701 }
601a03ba 8702
2bb3687b
AM
8703 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
8704 goto error_return_verref;
8705 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
8706 if (contents == NULL)
d0fb9a8d 8707 goto error_return_verref;
fc0e6df6 8708
1f4361a7
AM
8709 if (_bfd_mul_overflow (hdr->sh_info, sizeof (Elf_Internal_Verneed), &amt))
8710 {
8711 bfd_set_error (bfd_error_file_too_big);
8712 goto error_return_verref;
8713 }
8714 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_alloc (abfd, amt);
601a03ba 8715 if (elf_tdata (abfd)->verref == NULL)
d0fb9a8d
JJ
8716 goto error_return_verref;
8717
8718 BFD_ASSERT (sizeof (Elf_External_Verneed)
8719 == sizeof (Elf_External_Vernaux));
8720 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
fc0e6df6
PB
8721 everneed = (Elf_External_Verneed *) contents;
8722 iverneed = elf_tdata (abfd)->verref;
8723 for (i = 0; i < hdr->sh_info; i++, iverneed++)
8724 {
8725 Elf_External_Vernaux *evernaux;
8726 Elf_Internal_Vernaux *ivernaux;
8727 unsigned int j;
8728
8729 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
8730
8731 iverneed->vn_bfd = abfd;
8732
8733 iverneed->vn_filename =
8734 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8735 iverneed->vn_file);
8736 if (iverneed->vn_filename == NULL)
601a03ba 8737 goto error_return_bad_verref;
fc0e6df6 8738
d0fb9a8d
JJ
8739 if (iverneed->vn_cnt == 0)
8740 iverneed->vn_auxptr = NULL;
8741 else
8742 {
1f4361a7
AM
8743 if (_bfd_mul_overflow (iverneed->vn_cnt,
8744 sizeof (Elf_Internal_Vernaux), &amt))
8745 {
8746 bfd_set_error (bfd_error_file_too_big);
8747 goto error_return_verref;
8748 }
a50b1753 8749 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
1f4361a7 8750 bfd_alloc (abfd, amt);
d0fb9a8d
JJ
8751 if (iverneed->vn_auxptr == NULL)
8752 goto error_return_verref;
8753 }
8754
8755 if (iverneed->vn_aux
8756 > (size_t) (contents_end - (bfd_byte *) everneed))
601a03ba 8757 goto error_return_bad_verref;
fc0e6df6
PB
8758
8759 evernaux = ((Elf_External_Vernaux *)
8760 ((bfd_byte *) everneed + iverneed->vn_aux));
8761 ivernaux = iverneed->vn_auxptr;
8762 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
8763 {
8764 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
8765
8766 ivernaux->vna_nodename =
8767 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8768 ivernaux->vna_name);
8769 if (ivernaux->vna_nodename == NULL)
601a03ba 8770 goto error_return_bad_verref;
fc0e6df6 8771
25ff461f
AM
8772 if (ivernaux->vna_other > freeidx)
8773 freeidx = ivernaux->vna_other;
8774
8775 ivernaux->vna_nextptr = NULL;
8776 if (ivernaux->vna_next == 0)
8777 {
8778 iverneed->vn_cnt = j + 1;
8779 break;
8780 }
fc0e6df6
PB
8781 if (j + 1 < iverneed->vn_cnt)
8782 ivernaux->vna_nextptr = ivernaux + 1;
fc0e6df6 8783
d0fb9a8d
JJ
8784 if (ivernaux->vna_next
8785 > (size_t) (contents_end - (bfd_byte *) evernaux))
601a03ba 8786 goto error_return_bad_verref;
d0fb9a8d 8787
fc0e6df6
PB
8788 evernaux = ((Elf_External_Vernaux *)
8789 ((bfd_byte *) evernaux + ivernaux->vna_next));
fc0e6df6
PB
8790 }
8791
25ff461f
AM
8792 iverneed->vn_nextref = NULL;
8793 if (iverneed->vn_next == 0)
8794 break;
fc0e6df6
PB
8795 if (i + 1 < hdr->sh_info)
8796 iverneed->vn_nextref = iverneed + 1;
fc0e6df6 8797
d0fb9a8d
JJ
8798 if (iverneed->vn_next
8799 > (size_t) (contents_end - (bfd_byte *) everneed))
601a03ba 8800 goto error_return_bad_verref;
d0fb9a8d 8801
fc0e6df6
PB
8802 everneed = ((Elf_External_Verneed *)
8803 ((bfd_byte *) everneed + iverneed->vn_next));
8804 }
25ff461f 8805 elf_tdata (abfd)->cverrefs = i;
fc0e6df6
PB
8806
8807 free (contents);
8808 contents = NULL;
8809 }
252b5132
RH
8810
8811 if (elf_dynverdef (abfd) != 0)
8812 {
8813 Elf_Internal_Shdr *hdr;
8814 Elf_External_Verdef *everdef;
8815 Elf_Internal_Verdef *iverdef;
f631889e
UD
8816 Elf_Internal_Verdef *iverdefarr;
8817 Elf_Internal_Verdef iverdefmem;
252b5132 8818 unsigned int i;
062e2358 8819 unsigned int maxidx;
d0fb9a8d 8820 bfd_byte *contents_end_def, *contents_end_aux;
252b5132
RH
8821
8822 hdr = &elf_tdata (abfd)->dynverdef_hdr;
8823
601a03ba
AM
8824 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
8825 {
8826 error_return_bad_verdef:
4eca0228 8827 _bfd_error_handler
871b3ab2 8828 (_("%pB: .gnu.version_d invalid entry"), abfd);
601a03ba
AM
8829 bfd_set_error (bfd_error_bad_value);
8830 error_return_verdef:
8831 elf_tdata (abfd)->verdef = NULL;
8832 elf_tdata (abfd)->cverdefs = 0;
8833 goto error_return;
8834 }
8835
2bb3687b 8836 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
601a03ba 8837 goto error_return_verdef;
2bb3687b
AM
8838 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
8839 if (contents == NULL)
601a03ba 8840 goto error_return_verdef;
d0fb9a8d
JJ
8841
8842 BFD_ASSERT (sizeof (Elf_External_Verdef)
8843 >= sizeof (Elf_External_Verdaux));
8844 contents_end_def = contents + hdr->sh_size
8845 - sizeof (Elf_External_Verdef);
8846 contents_end_aux = contents + hdr->sh_size
8847 - sizeof (Elf_External_Verdaux);
8848
f631889e
UD
8849 /* We know the number of entries in the section but not the maximum
8850 index. Therefore we have to run through all entries and find
8851 the maximum. */
252b5132 8852 everdef = (Elf_External_Verdef *) contents;
f631889e
UD
8853 maxidx = 0;
8854 for (i = 0; i < hdr->sh_info; ++i)
8855 {
8856 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8857
601a03ba
AM
8858 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
8859 goto error_return_bad_verdef;
062e2358
AM
8860 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
8861 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
f631889e 8862
25ff461f
AM
8863 if (iverdefmem.vd_next == 0)
8864 break;
8865
d0fb9a8d
JJ
8866 if (iverdefmem.vd_next
8867 > (size_t) (contents_end_def - (bfd_byte *) everdef))
601a03ba 8868 goto error_return_bad_verdef;
d0fb9a8d 8869
f631889e
UD
8870 everdef = ((Elf_External_Verdef *)
8871 ((bfd_byte *) everdef + iverdefmem.vd_next));
8872 }
8873
fc0e6df6
PB
8874 if (default_imported_symver)
8875 {
8876 if (freeidx > maxidx)
8877 maxidx = ++freeidx;
8878 else
8879 freeidx = ++maxidx;
8880 }
1f4361a7
AM
8881 if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
8882 {
8883 bfd_set_error (bfd_error_file_too_big);
8884 goto error_return_verdef;
8885 }
8886 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
f631889e 8887 if (elf_tdata (abfd)->verdef == NULL)
601a03ba 8888 goto error_return_verdef;
f631889e
UD
8889
8890 elf_tdata (abfd)->cverdefs = maxidx;
8891
8892 everdef = (Elf_External_Verdef *) contents;
8893 iverdefarr = elf_tdata (abfd)->verdef;
8894 for (i = 0; i < hdr->sh_info; i++)
252b5132
RH
8895 {
8896 Elf_External_Verdaux *everdaux;
8897 Elf_Internal_Verdaux *iverdaux;
8898 unsigned int j;
8899
f631889e
UD
8900 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8901
d0fb9a8d 8902 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
601a03ba 8903 goto error_return_bad_verdef;
d0fb9a8d 8904
f631889e 8905 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
595bce75 8906 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
252b5132
RH
8907
8908 iverdef->vd_bfd = abfd;
8909
d0fb9a8d
JJ
8910 if (iverdef->vd_cnt == 0)
8911 iverdef->vd_auxptr = NULL;
8912 else
8913 {
1f4361a7
AM
8914 if (_bfd_mul_overflow (iverdef->vd_cnt,
8915 sizeof (Elf_Internal_Verdaux), &amt))
8916 {
8917 bfd_set_error (bfd_error_file_too_big);
8918 goto error_return_verdef;
8919 }
a50b1753 8920 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
1f4361a7 8921 bfd_alloc (abfd, amt);
d0fb9a8d
JJ
8922 if (iverdef->vd_auxptr == NULL)
8923 goto error_return_verdef;
8924 }
8925
8926 if (iverdef->vd_aux
8927 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
601a03ba 8928 goto error_return_bad_verdef;
252b5132
RH
8929
8930 everdaux = ((Elf_External_Verdaux *)
8931 ((bfd_byte *) everdef + iverdef->vd_aux));
8932 iverdaux = iverdef->vd_auxptr;
8933 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
8934 {
8935 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
8936
8937 iverdaux->vda_nodename =
8938 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8939 iverdaux->vda_name);
8940 if (iverdaux->vda_nodename == NULL)
601a03ba 8941 goto error_return_bad_verdef;
252b5132 8942
25ff461f
AM
8943 iverdaux->vda_nextptr = NULL;
8944 if (iverdaux->vda_next == 0)
8945 {
8946 iverdef->vd_cnt = j + 1;
8947 break;
8948 }
252b5132
RH
8949 if (j + 1 < iverdef->vd_cnt)
8950 iverdaux->vda_nextptr = iverdaux + 1;
252b5132 8951
d0fb9a8d
JJ
8952 if (iverdaux->vda_next
8953 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
601a03ba 8954 goto error_return_bad_verdef;
d0fb9a8d 8955
252b5132
RH
8956 everdaux = ((Elf_External_Verdaux *)
8957 ((bfd_byte *) everdaux + iverdaux->vda_next));
8958 }
8959
595bce75 8960 iverdef->vd_nodename = NULL;
d0fb9a8d
JJ
8961 if (iverdef->vd_cnt)
8962 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
252b5132 8963
25ff461f
AM
8964 iverdef->vd_nextdef = NULL;
8965 if (iverdef->vd_next == 0)
8966 break;
d0fb9a8d 8967 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
252b5132 8968 iverdef->vd_nextdef = iverdef + 1;
252b5132
RH
8969
8970 everdef = ((Elf_External_Verdef *)
8971 ((bfd_byte *) everdef + iverdef->vd_next));
8972 }
8973
8974 free (contents);
8975 contents = NULL;
8976 }
fc0e6df6 8977 else if (default_imported_symver)
252b5132 8978 {
fc0e6df6
PB
8979 if (freeidx < 3)
8980 freeidx = 3;
8981 else
8982 freeidx++;
252b5132 8983
1f4361a7
AM
8984 if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
8985 {
8986 bfd_set_error (bfd_error_file_too_big);
8987 goto error_return;
8988 }
8989 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
fc0e6df6 8990 if (elf_tdata (abfd)->verdef == NULL)
252b5132
RH
8991 goto error_return;
8992
fc0e6df6
PB
8993 elf_tdata (abfd)->cverdefs = freeidx;
8994 }
252b5132 8995
fc0e6df6
PB
8996 /* Create a default version based on the soname. */
8997 if (default_imported_symver)
8998 {
8999 Elf_Internal_Verdef *iverdef;
9000 Elf_Internal_Verdaux *iverdaux;
252b5132 9001
5bb3703f 9002 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
252b5132 9003
fc0e6df6
PB
9004 iverdef->vd_version = VER_DEF_CURRENT;
9005 iverdef->vd_flags = 0;
9006 iverdef->vd_ndx = freeidx;
9007 iverdef->vd_cnt = 1;
252b5132 9008
fc0e6df6 9009 iverdef->vd_bfd = abfd;
252b5132 9010
fc0e6df6
PB
9011 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9012 if (iverdef->vd_nodename == NULL)
d0fb9a8d 9013 goto error_return_verdef;
fc0e6df6 9014 iverdef->vd_nextdef = NULL;
601a03ba
AM
9015 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
9016 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
d0fb9a8d
JJ
9017 if (iverdef->vd_auxptr == NULL)
9018 goto error_return_verdef;
252b5132 9019
fc0e6df6
PB
9020 iverdaux = iverdef->vd_auxptr;
9021 iverdaux->vda_nodename = iverdef->vd_nodename;
252b5132
RH
9022 }
9023
b34976b6 9024 return TRUE;
252b5132
RH
9025
9026 error_return:
c9594989 9027 free (contents);
b34976b6 9028 return FALSE;
252b5132
RH
9029}
9030\f
9031asymbol *
217aa764 9032_bfd_elf_make_empty_symbol (bfd *abfd)
252b5132
RH
9033{
9034 elf_symbol_type *newsym;
9035
7a6e0d89 9036 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
252b5132
RH
9037 if (!newsym)
9038 return NULL;
201159ec
NC
9039 newsym->symbol.the_bfd = abfd;
9040 return &newsym->symbol;
252b5132
RH
9041}
9042
9043void
217aa764
AM
9044_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9045 asymbol *symbol,
9046 symbol_info *ret)
252b5132
RH
9047{
9048 bfd_symbol_info (symbol, ret);
9049}
9050
9051/* Return whether a symbol name implies a local symbol. Most targets
9052 use this function for the is_local_label_name entry point, but some
9053 override it. */
9054
b34976b6 9055bfd_boolean
217aa764
AM
9056_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9057 const char *name)
252b5132
RH
9058{
9059 /* Normal local symbols start with ``.L''. */
9060 if (name[0] == '.' && name[1] == 'L')
b34976b6 9061 return TRUE;
252b5132
RH
9062
9063 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9064 DWARF debugging symbols starting with ``..''. */
9065 if (name[0] == '.' && name[1] == '.')
b34976b6 9066 return TRUE;
252b5132
RH
9067
9068 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9069 emitting DWARF debugging output. I suspect this is actually a
9070 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9071 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9072 underscore to be emitted on some ELF targets). For ease of use,
9073 we treat such symbols as local. */
9074 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
b34976b6 9075 return TRUE;
252b5132 9076
b1fa9dd6
NC
9077 /* Treat assembler generated fake symbols, dollar local labels and
9078 forward-backward labels (aka local labels) as locals.
9079 These labels have the form:
9080
07d6d2b8 9081 L0^A.* (fake symbols)
b1fa9dd6
NC
9082
9083 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9084
9085 Versions which start with .L will have already been matched above,
9086 so we only need to match the rest. */
9087 if (name[0] == 'L' && ISDIGIT (name[1]))
9088 {
9089 bfd_boolean ret = FALSE;
9090 const char * p;
9091 char c;
9092
9093 for (p = name + 2; (c = *p); p++)
9094 {
9095 if (c == 1 || c == 2)
9096 {
9097 if (c == 1 && p == name + 2)
9098 /* A fake symbol. */
9099 return TRUE;
9100
9101 /* FIXME: We are being paranoid here and treating symbols like
9102 L0^Bfoo as if there were non-local, on the grounds that the
9103 assembler will never generate them. But can any symbol
9104 containing an ASCII value in the range 1-31 ever be anything
9105 other than some kind of local ? */
9106 ret = TRUE;
9107 }
9108
9109 if (! ISDIGIT (c))
9110 {
9111 ret = FALSE;
9112 break;
9113 }
9114 }
9115 return ret;
9116 }
ffa54770 9117
b34976b6 9118 return FALSE;
252b5132
RH
9119}
9120
9121alent *
217aa764
AM
9122_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9123 asymbol *symbol ATTRIBUTE_UNUSED)
252b5132
RH
9124{
9125 abort ();
9126 return NULL;
9127}
9128
b34976b6 9129bfd_boolean
217aa764
AM
9130_bfd_elf_set_arch_mach (bfd *abfd,
9131 enum bfd_architecture arch,
9132 unsigned long machine)
252b5132
RH
9133{
9134 /* If this isn't the right architecture for this backend, and this
9135 isn't the generic backend, fail. */
9136 if (arch != get_elf_backend_data (abfd)->arch
9137 && arch != bfd_arch_unknown
9138 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
b34976b6 9139 return FALSE;
252b5132
RH
9140
9141 return bfd_default_set_arch_mach (abfd, arch, machine);
9142}
9143
d1fad7c6
NC
9144/* Find the nearest line to a particular section and offset,
9145 for error reporting. */
9146
b34976b6 9147bfd_boolean
217aa764 9148_bfd_elf_find_nearest_line (bfd *abfd,
217aa764 9149 asymbol **symbols,
fb167eb2 9150 asection *section,
217aa764
AM
9151 bfd_vma offset,
9152 const char **filename_ptr,
9153 const char **functionname_ptr,
fb167eb2
AM
9154 unsigned int *line_ptr,
9155 unsigned int *discriminator_ptr)
d1fad7c6 9156{
b34976b6 9157 bfd_boolean found;
d1fad7c6 9158
fb167eb2 9159 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
4e8a9624 9160 filename_ptr, functionname_ptr,
fb167eb2 9161 line_ptr, discriminator_ptr,
9defd221 9162 dwarf_debug_sections,
e7679060
AM
9163 &elf_tdata (abfd)->dwarf2_find_line_info))
9164 return TRUE;
9165
9166 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9167 filename_ptr, functionname_ptr, line_ptr))
d1fad7c6
NC
9168 {
9169 if (!*functionname_ptr)
e00e8198
AM
9170 _bfd_elf_find_function (abfd, symbols, section, offset,
9171 *filename_ptr ? NULL : filename_ptr,
9172 functionname_ptr);
b34976b6 9173 return TRUE;
d1fad7c6
NC
9174 }
9175
9176 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4e8a9624
AM
9177 &found, filename_ptr,
9178 functionname_ptr, line_ptr,
9179 &elf_tdata (abfd)->line_info))
b34976b6 9180 return FALSE;
dc43ada5 9181 if (found && (*functionname_ptr || *line_ptr))
b34976b6 9182 return TRUE;
d1fad7c6
NC
9183
9184 if (symbols == NULL)
b34976b6 9185 return FALSE;
d1fad7c6 9186
e00e8198
AM
9187 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9188 filename_ptr, functionname_ptr))
b34976b6 9189 return FALSE;
d1fad7c6 9190
252b5132 9191 *line_ptr = 0;
b34976b6 9192 return TRUE;
252b5132
RH
9193}
9194
5420f73d
L
9195/* Find the line for a symbol. */
9196
9197bfd_boolean
9198_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9199 const char **filename_ptr, unsigned int *line_ptr)
9b8d1a36 9200{
fb167eb2
AM
9201 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9202 filename_ptr, NULL, line_ptr, NULL,
9defd221 9203 dwarf_debug_sections,
fb167eb2 9204 &elf_tdata (abfd)->dwarf2_find_line_info);
5420f73d
L
9205}
9206
4ab527b0
FF
9207/* After a call to bfd_find_nearest_line, successive calls to
9208 bfd_find_inliner_info can be used to get source information about
9209 each level of function inlining that terminated at the address
9210 passed to bfd_find_nearest_line. Currently this is only supported
9211 for DWARF2 with appropriate DWARF3 extensions. */
9212
9213bfd_boolean
9214_bfd_elf_find_inliner_info (bfd *abfd,
9215 const char **filename_ptr,
9216 const char **functionname_ptr,
9217 unsigned int *line_ptr)
9218{
9219 bfd_boolean found;
9220 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9221 functionname_ptr, line_ptr,
9222 & elf_tdata (abfd)->dwarf2_find_line_info);
9223 return found;
9224}
9225
252b5132 9226int
a6b96beb 9227_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
252b5132 9228{
8ded5a0f
AM
9229 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9230 int ret = bed->s->sizeof_ehdr;
252b5132 9231
0e1862bb 9232 if (!bfd_link_relocatable (info))
8ded5a0f 9233 {
12bd6957 9234 bfd_size_type phdr_size = elf_program_header_size (abfd);
8ded5a0f 9235
62d7a5f6
AM
9236 if (phdr_size == (bfd_size_type) -1)
9237 {
9238 struct elf_segment_map *m;
9239
9240 phdr_size = 0;
12bd6957 9241 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
62d7a5f6 9242 phdr_size += bed->s->sizeof_phdr;
8ded5a0f 9243
62d7a5f6
AM
9244 if (phdr_size == 0)
9245 phdr_size = get_program_header_size (abfd, info);
9246 }
8ded5a0f 9247
12bd6957 9248 elf_program_header_size (abfd) = phdr_size;
8ded5a0f
AM
9249 ret += phdr_size;
9250 }
9251
252b5132
RH
9252 return ret;
9253}
9254
b34976b6 9255bfd_boolean
217aa764
AM
9256_bfd_elf_set_section_contents (bfd *abfd,
9257 sec_ptr section,
0f867abe 9258 const void *location,
217aa764
AM
9259 file_ptr offset,
9260 bfd_size_type count)
252b5132
RH
9261{
9262 Elf_Internal_Shdr *hdr;
1b6aeedb 9263 file_ptr pos;
252b5132
RH
9264
9265 if (! abfd->output_has_begun
217aa764 9266 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
b34976b6 9267 return FALSE;
252b5132 9268
0ce398f1
L
9269 if (!count)
9270 return TRUE;
9271
252b5132 9272 hdr = &elf_section_data (section)->this_hdr;
0ce398f1
L
9273 if (hdr->sh_offset == (file_ptr) -1)
9274 {
a0dcf297
NC
9275 unsigned char *contents;
9276
1ff6de03
NA
9277 if (bfd_section_is_ctf (section))
9278 /* Nothing to do with this section: the contents are generated
9279 later. */
9280 return TRUE;
9281
a0dcf297
NC
9282 if ((section->flags & SEC_ELF_COMPRESS) == 0)
9283 {
9284 _bfd_error_handler
9285 (_("%pB:%pA: error: attempting to write into an unallocated compressed section"),
9286 abfd, section);
9287 bfd_set_error (bfd_error_invalid_operation);
9288 return FALSE;
9289 }
9290
9291 if ((offset + count) > hdr->sh_size)
9292 {
9293 _bfd_error_handler
9294 (_("%pB:%pA: error: attempting to write over the end of the section"),
9295 abfd, section);
9296
9297 bfd_set_error (bfd_error_invalid_operation);
9298 return FALSE;
9299 }
9300
9301 contents = hdr->contents;
9302 if (contents == NULL)
9303 {
9304 _bfd_error_handler
9305 (_("%pB:%pA: error: attempting to write section into an empty buffer"),
9306 abfd, section);
9307
9308 bfd_set_error (bfd_error_invalid_operation);
9309 return FALSE;
9310 }
9311
0ce398f1
L
9312 memcpy (contents + offset, location, count);
9313 return TRUE;
9314 }
a0dcf297 9315
dc810e39
AM
9316 pos = hdr->sh_offset + offset;
9317 if (bfd_seek (abfd, pos, SEEK_SET) != 0
9318 || bfd_bwrite (location, count, abfd) != count)
b34976b6 9319 return FALSE;
252b5132 9320
b34976b6 9321 return TRUE;
252b5132
RH
9322}
9323
f3185997 9324bfd_boolean
217aa764
AM
9325_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
9326 arelent *cache_ptr ATTRIBUTE_UNUSED,
9327 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
252b5132
RH
9328{
9329 abort ();
f3185997 9330 return FALSE;
252b5132
RH
9331}
9332
252b5132
RH
9333/* Try to convert a non-ELF reloc into an ELF one. */
9334
b34976b6 9335bfd_boolean
217aa764 9336_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
252b5132 9337{
c044fabd 9338 /* Check whether we really have an ELF howto. */
252b5132
RH
9339
9340 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
9341 {
9342 bfd_reloc_code_real_type code;
9343 reloc_howto_type *howto;
9344
9345 /* Alien reloc: Try to determine its type to replace it with an
c044fabd 9346 equivalent ELF reloc. */
252b5132
RH
9347
9348 if (areloc->howto->pc_relative)
9349 {
9350 switch (areloc->howto->bitsize)
9351 {
9352 case 8:
9353 code = BFD_RELOC_8_PCREL;
9354 break;
9355 case 12:
9356 code = BFD_RELOC_12_PCREL;
9357 break;
9358 case 16:
9359 code = BFD_RELOC_16_PCREL;
9360 break;
9361 case 24:
9362 code = BFD_RELOC_24_PCREL;
9363 break;
9364 case 32:
9365 code = BFD_RELOC_32_PCREL;
9366 break;
9367 case 64:
9368 code = BFD_RELOC_64_PCREL;
9369 break;
9370 default:
9371 goto fail;
9372 }
9373
9374 howto = bfd_reloc_type_lookup (abfd, code);
9375
94698d01 9376 if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
252b5132
RH
9377 {
9378 if (howto->pcrel_offset)
9379 areloc->addend += areloc->address;
9380 else
9381 areloc->addend -= areloc->address; /* addend is unsigned!! */
9382 }
9383 }
9384 else
9385 {
9386 switch (areloc->howto->bitsize)
9387 {
9388 case 8:
9389 code = BFD_RELOC_8;
9390 break;
9391 case 14:
9392 code = BFD_RELOC_14;
9393 break;
9394 case 16:
9395 code = BFD_RELOC_16;
9396 break;
9397 case 26:
9398 code = BFD_RELOC_26;
9399 break;
9400 case 32:
9401 code = BFD_RELOC_32;
9402 break;
9403 case 64:
9404 code = BFD_RELOC_64;
9405 break;
9406 default:
9407 goto fail;
9408 }
9409
9410 howto = bfd_reloc_type_lookup (abfd, code);
9411 }
9412
9413 if (howto)
9414 areloc->howto = howto;
9415 else
9416 goto fail;
9417 }
9418
b34976b6 9419 return TRUE;
252b5132
RH
9420
9421 fail:
0aa13fee
AM
9422 /* xgettext:c-format */
9423 _bfd_error_handler (_("%pB: %s unsupported"),
9424 abfd, areloc->howto->name);
9aea1e31 9425 bfd_set_error (bfd_error_sorry);
b34976b6 9426 return FALSE;
252b5132
RH
9427}
9428
b34976b6 9429bfd_boolean
217aa764 9430_bfd_elf_close_and_cleanup (bfd *abfd)
252b5132 9431{
d9071b0c 9432 struct elf_obj_tdata *tdata = elf_tdata (abfd);
0ed18fa1
AM
9433 if (tdata != NULL
9434 && (bfd_get_format (abfd) == bfd_object
9435 || bfd_get_format (abfd) == bfd_core))
252b5132 9436 {
c0355132 9437 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
2b0f7ef9 9438 _bfd_elf_strtab_free (elf_shstrtab (abfd));
d9071b0c 9439 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
252b5132
RH
9440 }
9441
9442 return _bfd_generic_close_and_cleanup (abfd);
9443}
9444
9445/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9446 in the relocation's offset. Thus we cannot allow any sort of sanity
9447 range-checking to interfere. There is nothing else to do in processing
9448 this reloc. */
9449
9450bfd_reloc_status_type
217aa764
AM
9451_bfd_elf_rel_vtable_reloc_fn
9452 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
fc0a2244 9453 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
217aa764
AM
9454 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
9455 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
252b5132
RH
9456{
9457 return bfd_reloc_ok;
9458}
252b5132
RH
9459\f
9460/* Elf core file support. Much of this only works on native
9461 toolchains, since we rely on knowing the
9462 machine-dependent procfs structure in order to pick
c044fabd 9463 out details about the corefile. */
252b5132
RH
9464
9465#ifdef HAVE_SYS_PROCFS_H
9466# include <sys/procfs.h>
9467#endif
9468
261b8d08
PA
9469/* Return a PID that identifies a "thread" for threaded cores, or the
9470 PID of the main process for non-threaded cores. */
252b5132
RH
9471
9472static int
217aa764 9473elfcore_make_pid (bfd *abfd)
252b5132 9474{
261b8d08
PA
9475 int pid;
9476
228e534f 9477 pid = elf_tdata (abfd)->core->lwpid;
261b8d08 9478 if (pid == 0)
228e534f 9479 pid = elf_tdata (abfd)->core->pid;
261b8d08
PA
9480
9481 return pid;
252b5132
RH
9482}
9483
252b5132
RH
9484/* If there isn't a section called NAME, make one, using
9485 data from SECT. Note, this function will generate a
9486 reference to NAME, so you shouldn't deallocate or
c044fabd 9487 overwrite it. */
252b5132 9488
b34976b6 9489static bfd_boolean
217aa764 9490elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
252b5132 9491{
c044fabd 9492 asection *sect2;
252b5132
RH
9493
9494 if (bfd_get_section_by_name (abfd, name) != NULL)
b34976b6 9495 return TRUE;
252b5132 9496
117ed4f8 9497 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
252b5132 9498 if (sect2 == NULL)
b34976b6 9499 return FALSE;
252b5132 9500
eea6121a 9501 sect2->size = sect->size;
252b5132 9502 sect2->filepos = sect->filepos;
252b5132 9503 sect2->alignment_power = sect->alignment_power;
b34976b6 9504 return TRUE;
252b5132
RH
9505}
9506
bb0082d6
AM
9507/* Create a pseudosection containing SIZE bytes at FILEPOS. This
9508 actually creates up to two pseudosections:
9509 - For the single-threaded case, a section named NAME, unless
9510 such a section already exists.
9511 - For the multi-threaded case, a section named "NAME/PID", where
9512 PID is elfcore_make_pid (abfd).
24d3e51b 9513 Both pseudosections have identical contents. */
b34976b6 9514bfd_boolean
217aa764
AM
9515_bfd_elfcore_make_pseudosection (bfd *abfd,
9516 char *name,
9517 size_t size,
9518 ufile_ptr filepos)
bb0082d6
AM
9519{
9520 char buf[100];
9521 char *threaded_name;
d4c88bbb 9522 size_t len;
bb0082d6
AM
9523 asection *sect;
9524
9525 /* Build the section name. */
9526
9527 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
d4c88bbb 9528 len = strlen (buf) + 1;
a50b1753 9529 threaded_name = (char *) bfd_alloc (abfd, len);
bb0082d6 9530 if (threaded_name == NULL)
b34976b6 9531 return FALSE;
d4c88bbb 9532 memcpy (threaded_name, buf, len);
bb0082d6 9533
117ed4f8
AM
9534 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
9535 SEC_HAS_CONTENTS);
bb0082d6 9536 if (sect == NULL)
b34976b6 9537 return FALSE;
eea6121a 9538 sect->size = size;
bb0082d6 9539 sect->filepos = filepos;
bb0082d6
AM
9540 sect->alignment_power = 2;
9541
936e320b 9542 return elfcore_maybe_make_sect (abfd, name, sect);
bb0082d6
AM
9543}
9544
58e07198
CZ
9545static bfd_boolean
9546elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
9547 size_t offs)
9548{
9549 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9550 SEC_HAS_CONTENTS);
9551
9552 if (sect == NULL)
9553 return FALSE;
9554
9555 sect->size = note->descsz - offs;
9556 sect->filepos = note->descpos + offs;
9557 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9558
9559 return TRUE;
9560}
9561
252b5132 9562/* prstatus_t exists on:
4a938328 9563 solaris 2.5+
252b5132
RH
9564 linux 2.[01] + glibc
9565 unixware 4.2
9566*/
9567
9568#if defined (HAVE_PRSTATUS_T)
a7b97311 9569
b34976b6 9570static bfd_boolean
217aa764 9571elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 9572{
eea6121a 9573 size_t size;
7ee38065 9574 int offset;
252b5132 9575
4a938328
MS
9576 if (note->descsz == sizeof (prstatus_t))
9577 {
9578 prstatus_t prstat;
252b5132 9579
eea6121a 9580 size = sizeof (prstat.pr_reg);
7ee38065 9581 offset = offsetof (prstatus_t, pr_reg);
4a938328 9582 memcpy (&prstat, note->descdata, sizeof (prstat));
252b5132 9583
fa49d224
NC
9584 /* Do not overwrite the core signal if it
9585 has already been set by another thread. */
228e534f
AM
9586 if (elf_tdata (abfd)->core->signal == 0)
9587 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9588 if (elf_tdata (abfd)->core->pid == 0)
9589 elf_tdata (abfd)->core->pid = prstat.pr_pid;
252b5132 9590
4a938328
MS
9591 /* pr_who exists on:
9592 solaris 2.5+
9593 unixware 4.2
9594 pr_who doesn't exist on:
9595 linux 2.[01]
9596 */
252b5132 9597#if defined (HAVE_PRSTATUS_T_PR_WHO)
228e534f 9598 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
261b8d08 9599#else
228e534f 9600 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
252b5132 9601#endif
4a938328 9602 }
7ee38065 9603#if defined (HAVE_PRSTATUS32_T)
4a938328
MS
9604 else if (note->descsz == sizeof (prstatus32_t))
9605 {
9606 /* 64-bit host, 32-bit corefile */
9607 prstatus32_t prstat;
9608
eea6121a 9609 size = sizeof (prstat.pr_reg);
7ee38065 9610 offset = offsetof (prstatus32_t, pr_reg);
4a938328
MS
9611 memcpy (&prstat, note->descdata, sizeof (prstat));
9612
fa49d224
NC
9613 /* Do not overwrite the core signal if it
9614 has already been set by another thread. */
228e534f
AM
9615 if (elf_tdata (abfd)->core->signal == 0)
9616 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9617 if (elf_tdata (abfd)->core->pid == 0)
9618 elf_tdata (abfd)->core->pid = prstat.pr_pid;
4a938328
MS
9619
9620 /* pr_who exists on:
9621 solaris 2.5+
9622 unixware 4.2
9623 pr_who doesn't exist on:
9624 linux 2.[01]
9625 */
7ee38065 9626#if defined (HAVE_PRSTATUS32_T_PR_WHO)
228e534f 9627 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
261b8d08 9628#else
228e534f 9629 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
4a938328
MS
9630#endif
9631 }
7ee38065 9632#endif /* HAVE_PRSTATUS32_T */
4a938328
MS
9633 else
9634 {
9635 /* Fail - we don't know how to handle any other
9636 note size (ie. data object type). */
b34976b6 9637 return TRUE;
4a938328 9638 }
252b5132 9639
bb0082d6 9640 /* Make a ".reg/999" section and a ".reg" section. */
936e320b 9641 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 9642 size, note->descpos + offset);
252b5132
RH
9643}
9644#endif /* defined (HAVE_PRSTATUS_T) */
9645
bb0082d6 9646/* Create a pseudosection containing the exact contents of NOTE. */
b34976b6 9647static bfd_boolean
217aa764
AM
9648elfcore_make_note_pseudosection (bfd *abfd,
9649 char *name,
9650 Elf_Internal_Note *note)
252b5132 9651{
936e320b
AM
9652 return _bfd_elfcore_make_pseudosection (abfd, name,
9653 note->descsz, note->descpos);
252b5132
RH
9654}
9655
ff08c6bb
JB
9656/* There isn't a consistent prfpregset_t across platforms,
9657 but it doesn't matter, because we don't have to pick this
c044fabd
KH
9658 data structure apart. */
9659
b34976b6 9660static bfd_boolean
217aa764 9661elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
9662{
9663 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9664}
9665
ff08c6bb 9666/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
971d4640 9667 type of NT_PRXFPREG. Just include the whole note's contents
ff08c6bb 9668 literally. */
c044fabd 9669
b34976b6 9670static bfd_boolean
217aa764 9671elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
9672{
9673 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9674}
9675
4339cae0
L
9676/* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9677 with a note type of NT_X86_XSTATE. Just include the whole note's
9678 contents literally. */
9679
9680static bfd_boolean
9681elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
9682{
9683 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
9684}
9685
97753bd5
AM
9686static bfd_boolean
9687elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
9688{
9689 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
9690}
9691
89eeb0bc
LM
9692static bfd_boolean
9693elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
9694{
9695 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
9696}
97753bd5 9697
cb2366c1
EBM
9698static bfd_boolean
9699elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
9700{
9701 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
9702}
9703
9704static bfd_boolean
9705elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
9706{
9707 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
9708}
9709
9710static bfd_boolean
9711elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
9712{
9713 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
9714}
9715
9716static bfd_boolean
9717elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
9718{
9719 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
9720}
9721
9722static bfd_boolean
9723elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
9724{
9725 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
9726}
9727
9728static bfd_boolean
9729elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
9730{
9731 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
9732}
9733
9734static bfd_boolean
9735elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
9736{
9737 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
9738}
9739
9740static bfd_boolean
9741elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
9742{
9743 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
9744}
9745
9746static bfd_boolean
9747elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
9748{
9749 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
9750}
9751
9752static bfd_boolean
9753elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
9754{
9755 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
9756}
9757
9758static bfd_boolean
9759elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
9760{
9761 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
9762}
9763
9764static bfd_boolean
9765elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
9766{
9767 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
9768}
9769
9770static bfd_boolean
9771elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
9772{
9773 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
9774}
9775
0675e188
UW
9776static bfd_boolean
9777elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
9778{
9779 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
9780}
9781
d7eeb400
MS
9782static bfd_boolean
9783elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
9784{
9785 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
9786}
9787
9788static bfd_boolean
9789elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
9790{
9791 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
9792}
9793
9794static bfd_boolean
9795elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
9796{
9797 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
9798}
9799
9800static bfd_boolean
9801elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
9802{
9803 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
9804}
9805
9806static bfd_boolean
9807elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
9808{
9809 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
9810}
9811
355b81d9
UW
9812static bfd_boolean
9813elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
9814{
9815 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
9816}
9817
9818static bfd_boolean
9819elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
9820{
9821 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
9822}
9823
abb3f6cc
NC
9824static bfd_boolean
9825elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
9826{
9827 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
9828}
9829
4ef9f41a
AA
9830static bfd_boolean
9831elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
9832{
9833 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
9834}
9835
9836static bfd_boolean
9837elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
9838{
9839 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
9840}
9841
88ab90e8
AA
9842static bfd_boolean
9843elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
9844{
9845 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
9846}
9847
9848static bfd_boolean
9849elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
9850{
9851 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
9852}
9853
faa9a424
UW
9854static bfd_boolean
9855elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
9856{
9857 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
9858}
9859
652451f8
YZ
9860static bfd_boolean
9861elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
9862{
9863 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
9864}
9865
9866static bfd_boolean
9867elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
9868{
9869 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
9870}
9871
9872static bfd_boolean
9873elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
9874{
9875 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
9876}
9877
ad1cc4e4
AH
9878static bfd_boolean
9879elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
9880{
9881 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
9882}
9883
e6c3b5bf
AH
9884static bfd_boolean
9885elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
9886{
9887 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
9888}
9889
27456742
AK
9890static bfd_boolean
9891elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
9892{
9893 return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
9894}
9895
252b5132 9896#if defined (HAVE_PRPSINFO_T)
4a938328 9897typedef prpsinfo_t elfcore_psinfo_t;
7ee38065 9898#if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
9899typedef prpsinfo32_t elfcore_psinfo32_t;
9900#endif
252b5132
RH
9901#endif
9902
9903#if defined (HAVE_PSINFO_T)
4a938328 9904typedef psinfo_t elfcore_psinfo_t;
7ee38065 9905#if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
9906typedef psinfo32_t elfcore_psinfo32_t;
9907#endif
252b5132
RH
9908#endif
9909
252b5132
RH
9910/* return a malloc'ed copy of a string at START which is at
9911 most MAX bytes long, possibly without a terminating '\0'.
c044fabd 9912 the copy will always have a terminating '\0'. */
252b5132 9913
936e320b 9914char *
217aa764 9915_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
252b5132 9916{
dc810e39 9917 char *dups;
a50b1753 9918 char *end = (char *) memchr (start, '\0', max);
dc810e39 9919 size_t len;
252b5132
RH
9920
9921 if (end == NULL)
9922 len = max;
9923 else
9924 len = end - start;
9925
a50b1753 9926 dups = (char *) bfd_alloc (abfd, len + 1);
dc810e39 9927 if (dups == NULL)
252b5132
RH
9928 return NULL;
9929
dc810e39
AM
9930 memcpy (dups, start, len);
9931 dups[len] = '\0';
252b5132 9932
dc810e39 9933 return dups;
252b5132
RH
9934}
9935
bb0082d6 9936#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
b34976b6 9937static bfd_boolean
217aa764 9938elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
252b5132 9939{
4a938328
MS
9940 if (note->descsz == sizeof (elfcore_psinfo_t))
9941 {
9942 elfcore_psinfo_t psinfo;
252b5132 9943
7ee38065 9944 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 9945
335e41d4 9946#if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
228e534f 9947 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
335e41d4 9948#endif
228e534f 9949 elf_tdata (abfd)->core->program
936e320b
AM
9950 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9951 sizeof (psinfo.pr_fname));
252b5132 9952
228e534f 9953 elf_tdata (abfd)->core->command
936e320b
AM
9954 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9955 sizeof (psinfo.pr_psargs));
4a938328 9956 }
7ee38065 9957#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
4a938328
MS
9958 else if (note->descsz == sizeof (elfcore_psinfo32_t))
9959 {
9960 /* 64-bit host, 32-bit corefile */
9961 elfcore_psinfo32_t psinfo;
9962
7ee38065 9963 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 9964
335e41d4 9965#if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
228e534f 9966 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
335e41d4 9967#endif
228e534f 9968 elf_tdata (abfd)->core->program
936e320b
AM
9969 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9970 sizeof (psinfo.pr_fname));
4a938328 9971
228e534f 9972 elf_tdata (abfd)->core->command
936e320b
AM
9973 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9974 sizeof (psinfo.pr_psargs));
4a938328
MS
9975 }
9976#endif
9977
9978 else
9979 {
9980 /* Fail - we don't know how to handle any other
9981 note size (ie. data object type). */
b34976b6 9982 return TRUE;
4a938328 9983 }
252b5132
RH
9984
9985 /* Note that for some reason, a spurious space is tacked
9986 onto the end of the args in some (at least one anyway)
c044fabd 9987 implementations, so strip it off if it exists. */
252b5132
RH
9988
9989 {
228e534f 9990 char *command = elf_tdata (abfd)->core->command;
252b5132
RH
9991 int n = strlen (command);
9992
9993 if (0 < n && command[n - 1] == ' ')
9994 command[n - 1] = '\0';
9995 }
9996
b34976b6 9997 return TRUE;
252b5132
RH
9998}
9999#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10000
252b5132 10001#if defined (HAVE_PSTATUS_T)
b34976b6 10002static bfd_boolean
217aa764 10003elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 10004{
f572a39d
AM
10005 if (note->descsz == sizeof (pstatus_t)
10006#if defined (HAVE_PXSTATUS_T)
10007 || note->descsz == sizeof (pxstatus_t)
10008#endif
10009 )
4a938328
MS
10010 {
10011 pstatus_t pstat;
252b5132 10012
4a938328 10013 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 10014
228e534f 10015 elf_tdata (abfd)->core->pid = pstat.pr_pid;
4a938328 10016 }
7ee38065 10017#if defined (HAVE_PSTATUS32_T)
4a938328
MS
10018 else if (note->descsz == sizeof (pstatus32_t))
10019 {
10020 /* 64-bit host, 32-bit corefile */
10021 pstatus32_t pstat;
252b5132 10022
4a938328 10023 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 10024
228e534f 10025 elf_tdata (abfd)->core->pid = pstat.pr_pid;
4a938328
MS
10026 }
10027#endif
252b5132
RH
10028 /* Could grab some more details from the "representative"
10029 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
c044fabd 10030 NT_LWPSTATUS note, presumably. */
252b5132 10031
b34976b6 10032 return TRUE;
252b5132
RH
10033}
10034#endif /* defined (HAVE_PSTATUS_T) */
10035
252b5132 10036#if defined (HAVE_LWPSTATUS_T)
b34976b6 10037static bfd_boolean
217aa764 10038elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132
RH
10039{
10040 lwpstatus_t lwpstat;
10041 char buf[100];
c044fabd 10042 char *name;
d4c88bbb 10043 size_t len;
c044fabd 10044 asection *sect;
252b5132 10045
f572a39d
AM
10046 if (note->descsz != sizeof (lwpstat)
10047#if defined (HAVE_LWPXSTATUS_T)
10048 && note->descsz != sizeof (lwpxstatus_t)
10049#endif
10050 )
b34976b6 10051 return TRUE;
252b5132
RH
10052
10053 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10054
228e534f 10055 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
a1504221
JB
10056 /* Do not overwrite the core signal if it has already been set by
10057 another thread. */
228e534f
AM
10058 if (elf_tdata (abfd)->core->signal == 0)
10059 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
252b5132 10060
c044fabd 10061 /* Make a ".reg/999" section. */
252b5132
RH
10062
10063 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
d4c88bbb 10064 len = strlen (buf) + 1;
217aa764 10065 name = bfd_alloc (abfd, len);
252b5132 10066 if (name == NULL)
b34976b6 10067 return FALSE;
d4c88bbb 10068 memcpy (name, buf, len);
252b5132 10069
117ed4f8 10070 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 10071 if (sect == NULL)
b34976b6 10072 return FALSE;
252b5132
RH
10073
10074#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 10075 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
252b5132
RH
10076 sect->filepos = note->descpos
10077 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10078#endif
10079
10080#if defined (HAVE_LWPSTATUS_T_PR_REG)
eea6121a 10081 sect->size = sizeof (lwpstat.pr_reg);
252b5132
RH
10082 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10083#endif
10084
252b5132
RH
10085 sect->alignment_power = 2;
10086
10087 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
b34976b6 10088 return FALSE;
252b5132
RH
10089
10090 /* Make a ".reg2/999" section */
10091
10092 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
d4c88bbb 10093 len = strlen (buf) + 1;
217aa764 10094 name = bfd_alloc (abfd, len);
252b5132 10095 if (name == NULL)
b34976b6 10096 return FALSE;
d4c88bbb 10097 memcpy (name, buf, len);
252b5132 10098
117ed4f8 10099 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 10100 if (sect == NULL)
b34976b6 10101 return FALSE;
252b5132
RH
10102
10103#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 10104 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
252b5132
RH
10105 sect->filepos = note->descpos
10106 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
10107#endif
10108
10109#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
eea6121a 10110 sect->size = sizeof (lwpstat.pr_fpreg);
252b5132
RH
10111 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
10112#endif
10113
252b5132
RH
10114 sect->alignment_power = 2;
10115
936e320b 10116 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
252b5132
RH
10117}
10118#endif /* defined (HAVE_LWPSTATUS_T) */
10119
8fbac78b
JT
10120/* These constants, and the structure offsets used below, are defined by
10121 Cygwin's core_dump.h */
10122#define NOTE_INFO_PROCESS 1
10123#define NOTE_INFO_THREAD 2
10124#define NOTE_INFO_MODULE 3
d61f3d03 10125#define NOTE_INFO_MODULE64 4
8fbac78b 10126
b34976b6 10127static bfd_boolean
217aa764 10128elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
16e9c715
NC
10129{
10130 char buf[30];
c044fabd 10131 char *name;
d4c88bbb 10132 size_t len;
3cdad084 10133 unsigned int name_size;
c044fabd 10134 asection *sect;
2fef9373 10135 unsigned int type;
4a6636fb
PA
10136 int is_active_thread;
10137 bfd_vma base_addr;
16e9c715 10138
04ec0fa2 10139 if (note->descsz < 4)
b34976b6 10140 return TRUE;
16e9c715 10141
4a6636fb
PA
10142 if (! CONST_STRNEQ (note->namedata, "win32"))
10143 return TRUE;
10144
10145 type = bfd_get_32 (abfd, note->descdata);
c044fabd 10146
404ec933
JT
10147 struct {
10148 const char *type_name;
10149 unsigned long min_size;
10150 } size_check[] =
10151 {
10152 { "NOTE_INFO_PROCESS", 12 },
10153 { "NOTE_INFO_THREAD", 12 },
10154 { "NOTE_INFO_MODULE", 12 },
10155 { "NOTE_INFO_MODULE64", 16 },
10156 };
10157
10158 if (type > (sizeof(size_check)/sizeof(size_check[0])))
10159 return TRUE;
10160
10161 if (note->descsz < size_check[type - 1].min_size)
10162 {
10163 _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes is too small"),
10164 abfd, size_check[type - 1].type_name, note->descsz);
10165 return TRUE;
10166 }
10167
4a6636fb 10168 switch (type)
16e9c715 10169 {
8fbac78b 10170 case NOTE_INFO_PROCESS:
228e534f 10171 /* FIXME: need to add ->core->command. */
ff2084b9 10172 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
ff2084b9 10173 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
c044fabd 10174 break;
16e9c715 10175
8fbac78b 10176 case NOTE_INFO_THREAD:
ff2084b9
JT
10177 /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
10178 structure. */
4a6636fb 10179 /* thread_info.tid */
ff2084b9 10180 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
c044fabd 10181
d4c88bbb 10182 len = strlen (buf) + 1;
a50b1753 10183 name = (char *) bfd_alloc (abfd, len);
16e9c715 10184 if (name == NULL)
b34976b6 10185 return FALSE;
c044fabd 10186
d4c88bbb 10187 memcpy (name, buf, len);
16e9c715 10188
117ed4f8 10189 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
16e9c715 10190 if (sect == NULL)
b34976b6 10191 return FALSE;
c044fabd 10192
4a6636fb 10193 /* sizeof (thread_info.thread_context) */
03c29a6f 10194 sect->size = note->descsz - 12;
4a6636fb
PA
10195 /* offsetof (thread_info.thread_context) */
10196 sect->filepos = note->descpos + 12;
16e9c715
NC
10197 sect->alignment_power = 2;
10198
4a6636fb
PA
10199 /* thread_info.is_active_thread */
10200 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
10201
10202 if (is_active_thread)
16e9c715 10203 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
b34976b6 10204 return FALSE;
16e9c715
NC
10205 break;
10206
8fbac78b 10207 case NOTE_INFO_MODULE:
d61f3d03 10208 case NOTE_INFO_MODULE64:
16e9c715 10209 /* Make a ".module/xxxxxxxx" section. */
d61f3d03
JT
10210 if (type == NOTE_INFO_MODULE)
10211 {
d61f3d03
JT
10212 /* module_info.base_address */
10213 base_addr = bfd_get_32 (abfd, note->descdata + 4);
10214 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
10215 /* module_info.module_name_size */
10216 name_size = bfd_get_32 (abfd, note->descdata + 8);
10217 }
10218 else /* NOTE_INFO_MODULE64 */
10219 {
d61f3d03
JT
10220 /* module_info.base_address */
10221 base_addr = bfd_get_64 (abfd, note->descdata + 4);
10222 sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
10223 /* module_info.module_name_size */
10224 name_size = bfd_get_32 (abfd, note->descdata + 12);
10225 }
c044fabd 10226
d4c88bbb 10227 len = strlen (buf) + 1;
a50b1753 10228 name = (char *) bfd_alloc (abfd, len);
16e9c715 10229 if (name == NULL)
b34976b6 10230 return FALSE;
c044fabd 10231
d4c88bbb 10232 memcpy (name, buf, len);
252b5132 10233
117ed4f8 10234 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
c044fabd 10235
16e9c715 10236 if (sect == NULL)
b34976b6 10237 return FALSE;
c044fabd 10238
04ec0fa2 10239 if (note->descsz < 12 + name_size)
404ec933 10240 {
3cdad084 10241 _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu is too small to contain a name of size %u"),
404ec933
JT
10242 abfd, note->descsz, name_size);
10243 return TRUE;
10244 }
04ec0fa2 10245
eea6121a 10246 sect->size = note->descsz;
16e9c715 10247 sect->filepos = note->descpos;
16e9c715
NC
10248 sect->alignment_power = 2;
10249 break;
10250
10251 default:
b34976b6 10252 return TRUE;
16e9c715
NC
10253 }
10254
b34976b6 10255 return TRUE;
16e9c715 10256}
252b5132 10257
b34976b6 10258static bfd_boolean
217aa764 10259elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
252b5132 10260{
9c5bfbb7 10261 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
bb0082d6 10262
252b5132
RH
10263 switch (note->type)
10264 {
10265 default:
b34976b6 10266 return TRUE;
252b5132 10267
252b5132 10268 case NT_PRSTATUS:
bb0082d6
AM
10269 if (bed->elf_backend_grok_prstatus)
10270 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
b34976b6 10271 return TRUE;
bb0082d6 10272#if defined (HAVE_PRSTATUS_T)
252b5132 10273 return elfcore_grok_prstatus (abfd, note);
bb0082d6 10274#else
b34976b6 10275 return TRUE;
252b5132
RH
10276#endif
10277
10278#if defined (HAVE_PSTATUS_T)
10279 case NT_PSTATUS:
10280 return elfcore_grok_pstatus (abfd, note);
10281#endif
10282
10283#if defined (HAVE_LWPSTATUS_T)
10284 case NT_LWPSTATUS:
10285 return elfcore_grok_lwpstatus (abfd, note);
10286#endif
10287
10288 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
10289 return elfcore_grok_prfpreg (abfd, note);
10290
c044fabd 10291 case NT_WIN32PSTATUS:
16e9c715 10292 return elfcore_grok_win32pstatus (abfd, note);
16e9c715 10293
c044fabd 10294 case NT_PRXFPREG: /* Linux SSE extension */
e377ab71
MK
10295 if (note->namesz == 6
10296 && strcmp (note->namedata, "LINUX") == 0)
ff08c6bb
JB
10297 return elfcore_grok_prxfpreg (abfd, note);
10298 else
b34976b6 10299 return TRUE;
ff08c6bb 10300
4339cae0
L
10301 case NT_X86_XSTATE: /* Linux XSAVE extension */
10302 if (note->namesz == 6
10303 && strcmp (note->namedata, "LINUX") == 0)
10304 return elfcore_grok_xstatereg (abfd, note);
10305 else
10306 return TRUE;
10307
97753bd5
AM
10308 case NT_PPC_VMX:
10309 if (note->namesz == 6
10310 && strcmp (note->namedata, "LINUX") == 0)
10311 return elfcore_grok_ppc_vmx (abfd, note);
10312 else
10313 return TRUE;
10314
89eeb0bc
LM
10315 case NT_PPC_VSX:
10316 if (note->namesz == 6
07d6d2b8
AM
10317 && strcmp (note->namedata, "LINUX") == 0)
10318 return elfcore_grok_ppc_vsx (abfd, note);
89eeb0bc 10319 else
07d6d2b8 10320 return TRUE;
89eeb0bc 10321
cb2366c1
EBM
10322 case NT_PPC_TAR:
10323 if (note->namesz == 6
4b24dd1a
AM
10324 && strcmp (note->namedata, "LINUX") == 0)
10325 return elfcore_grok_ppc_tar (abfd, note);
cb2366c1 10326 else
4b24dd1a 10327 return TRUE;
cb2366c1
EBM
10328
10329 case NT_PPC_PPR:
10330 if (note->namesz == 6
4b24dd1a
AM
10331 && strcmp (note->namedata, "LINUX") == 0)
10332 return elfcore_grok_ppc_ppr (abfd, note);
cb2366c1 10333 else
4b24dd1a 10334 return TRUE;
cb2366c1
EBM
10335
10336 case NT_PPC_DSCR:
10337 if (note->namesz == 6
4b24dd1a
AM
10338 && strcmp (note->namedata, "LINUX") == 0)
10339 return elfcore_grok_ppc_dscr (abfd, note);
cb2366c1 10340 else
4b24dd1a 10341 return TRUE;
cb2366c1
EBM
10342
10343 case NT_PPC_EBB:
10344 if (note->namesz == 6
4b24dd1a
AM
10345 && strcmp (note->namedata, "LINUX") == 0)
10346 return elfcore_grok_ppc_ebb (abfd, note);
cb2366c1 10347 else
4b24dd1a 10348 return TRUE;
cb2366c1
EBM
10349
10350 case NT_PPC_PMU:
10351 if (note->namesz == 6
4b24dd1a
AM
10352 && strcmp (note->namedata, "LINUX") == 0)
10353 return elfcore_grok_ppc_pmu (abfd, note);
cb2366c1 10354 else
4b24dd1a 10355 return TRUE;
cb2366c1
EBM
10356
10357 case NT_PPC_TM_CGPR:
10358 if (note->namesz == 6
4b24dd1a
AM
10359 && strcmp (note->namedata, "LINUX") == 0)
10360 return elfcore_grok_ppc_tm_cgpr (abfd, note);
cb2366c1 10361 else
4b24dd1a 10362 return TRUE;
cb2366c1
EBM
10363
10364 case NT_PPC_TM_CFPR:
10365 if (note->namesz == 6
4b24dd1a
AM
10366 && strcmp (note->namedata, "LINUX") == 0)
10367 return elfcore_grok_ppc_tm_cfpr (abfd, note);
cb2366c1 10368 else
4b24dd1a 10369 return TRUE;
cb2366c1
EBM
10370
10371 case NT_PPC_TM_CVMX:
10372 if (note->namesz == 6
4b24dd1a
AM
10373 && strcmp (note->namedata, "LINUX") == 0)
10374 return elfcore_grok_ppc_tm_cvmx (abfd, note);
cb2366c1 10375 else
4b24dd1a 10376 return TRUE;
cb2366c1
EBM
10377
10378 case NT_PPC_TM_CVSX:
10379 if (note->namesz == 6
4b24dd1a
AM
10380 && strcmp (note->namedata, "LINUX") == 0)
10381 return elfcore_grok_ppc_tm_cvsx (abfd, note);
cb2366c1 10382 else
4b24dd1a 10383 return TRUE;
cb2366c1
EBM
10384
10385 case NT_PPC_TM_SPR:
10386 if (note->namesz == 6
4b24dd1a
AM
10387 && strcmp (note->namedata, "LINUX") == 0)
10388 return elfcore_grok_ppc_tm_spr (abfd, note);
cb2366c1 10389 else
4b24dd1a 10390 return TRUE;
cb2366c1
EBM
10391
10392 case NT_PPC_TM_CTAR:
10393 if (note->namesz == 6
4b24dd1a
AM
10394 && strcmp (note->namedata, "LINUX") == 0)
10395 return elfcore_grok_ppc_tm_ctar (abfd, note);
cb2366c1 10396 else
4b24dd1a 10397 return TRUE;
cb2366c1
EBM
10398
10399 case NT_PPC_TM_CPPR:
10400 if (note->namesz == 6
4b24dd1a
AM
10401 && strcmp (note->namedata, "LINUX") == 0)
10402 return elfcore_grok_ppc_tm_cppr (abfd, note);
cb2366c1 10403 else
4b24dd1a 10404 return TRUE;
cb2366c1
EBM
10405
10406 case NT_PPC_TM_CDSCR:
10407 if (note->namesz == 6
4b24dd1a
AM
10408 && strcmp (note->namedata, "LINUX") == 0)
10409 return elfcore_grok_ppc_tm_cdscr (abfd, note);
cb2366c1 10410 else
4b24dd1a 10411 return TRUE;
cb2366c1 10412
0675e188
UW
10413 case NT_S390_HIGH_GPRS:
10414 if (note->namesz == 6
07d6d2b8
AM
10415 && strcmp (note->namedata, "LINUX") == 0)
10416 return elfcore_grok_s390_high_gprs (abfd, note);
0675e188 10417 else
07d6d2b8 10418 return TRUE;
0675e188 10419
d7eeb400
MS
10420 case NT_S390_TIMER:
10421 if (note->namesz == 6
07d6d2b8
AM
10422 && strcmp (note->namedata, "LINUX") == 0)
10423 return elfcore_grok_s390_timer (abfd, note);
d7eeb400 10424 else
07d6d2b8 10425 return TRUE;
d7eeb400
MS
10426
10427 case NT_S390_TODCMP:
10428 if (note->namesz == 6
07d6d2b8
AM
10429 && strcmp (note->namedata, "LINUX") == 0)
10430 return elfcore_grok_s390_todcmp (abfd, note);
d7eeb400 10431 else
07d6d2b8 10432 return TRUE;
d7eeb400
MS
10433
10434 case NT_S390_TODPREG:
10435 if (note->namesz == 6
07d6d2b8
AM
10436 && strcmp (note->namedata, "LINUX") == 0)
10437 return elfcore_grok_s390_todpreg (abfd, note);
d7eeb400 10438 else
07d6d2b8 10439 return TRUE;
d7eeb400
MS
10440
10441 case NT_S390_CTRS:
10442 if (note->namesz == 6
07d6d2b8
AM
10443 && strcmp (note->namedata, "LINUX") == 0)
10444 return elfcore_grok_s390_ctrs (abfd, note);
d7eeb400 10445 else
07d6d2b8 10446 return TRUE;
d7eeb400
MS
10447
10448 case NT_S390_PREFIX:
10449 if (note->namesz == 6
07d6d2b8
AM
10450 && strcmp (note->namedata, "LINUX") == 0)
10451 return elfcore_grok_s390_prefix (abfd, note);
d7eeb400 10452 else
07d6d2b8 10453 return TRUE;
d7eeb400 10454
355b81d9
UW
10455 case NT_S390_LAST_BREAK:
10456 if (note->namesz == 6
07d6d2b8
AM
10457 && strcmp (note->namedata, "LINUX") == 0)
10458 return elfcore_grok_s390_last_break (abfd, note);
355b81d9 10459 else
07d6d2b8 10460 return TRUE;
355b81d9
UW
10461
10462 case NT_S390_SYSTEM_CALL:
10463 if (note->namesz == 6
07d6d2b8
AM
10464 && strcmp (note->namedata, "LINUX") == 0)
10465 return elfcore_grok_s390_system_call (abfd, note);
355b81d9 10466 else
07d6d2b8 10467 return TRUE;
355b81d9 10468
abb3f6cc
NC
10469 case NT_S390_TDB:
10470 if (note->namesz == 6
07d6d2b8
AM
10471 && strcmp (note->namedata, "LINUX") == 0)
10472 return elfcore_grok_s390_tdb (abfd, note);
abb3f6cc 10473 else
07d6d2b8 10474 return TRUE;
abb3f6cc 10475
4ef9f41a
AA
10476 case NT_S390_VXRS_LOW:
10477 if (note->namesz == 6
10478 && strcmp (note->namedata, "LINUX") == 0)
10479 return elfcore_grok_s390_vxrs_low (abfd, note);
10480 else
10481 return TRUE;
10482
10483 case NT_S390_VXRS_HIGH:
10484 if (note->namesz == 6
10485 && strcmp (note->namedata, "LINUX") == 0)
10486 return elfcore_grok_s390_vxrs_high (abfd, note);
10487 else
10488 return TRUE;
10489
88ab90e8
AA
10490 case NT_S390_GS_CB:
10491 if (note->namesz == 6
10492 && strcmp (note->namedata, "LINUX") == 0)
8fe09d74 10493 return elfcore_grok_s390_gs_cb (abfd, note);
88ab90e8
AA
10494 else
10495 return TRUE;
10496
10497 case NT_S390_GS_BC:
10498 if (note->namesz == 6
10499 && strcmp (note->namedata, "LINUX") == 0)
8fe09d74 10500 return elfcore_grok_s390_gs_bc (abfd, note);
88ab90e8
AA
10501 else
10502 return TRUE;
10503
27456742
AK
10504 case NT_ARC_V2:
10505 if (note->namesz == 6
10506 && strcmp (note->namedata, "LINUX") == 0)
10507 return elfcore_grok_arc_v2 (abfd, note);
10508 else
10509 return TRUE;
10510
faa9a424
UW
10511 case NT_ARM_VFP:
10512 if (note->namesz == 6
10513 && strcmp (note->namedata, "LINUX") == 0)
10514 return elfcore_grok_arm_vfp (abfd, note);
10515 else
10516 return TRUE;
10517
652451f8
YZ
10518 case NT_ARM_TLS:
10519 if (note->namesz == 6
10520 && strcmp (note->namedata, "LINUX") == 0)
10521 return elfcore_grok_aarch_tls (abfd, note);
10522 else
10523 return TRUE;
10524
10525 case NT_ARM_HW_BREAK:
10526 if (note->namesz == 6
10527 && strcmp (note->namedata, "LINUX") == 0)
10528 return elfcore_grok_aarch_hw_break (abfd, note);
10529 else
10530 return TRUE;
10531
10532 case NT_ARM_HW_WATCH:
10533 if (note->namesz == 6
10534 && strcmp (note->namedata, "LINUX") == 0)
10535 return elfcore_grok_aarch_hw_watch (abfd, note);
10536 else
10537 return TRUE;
10538
ad1cc4e4
AH
10539 case NT_ARM_SVE:
10540 if (note->namesz == 6
10541 && strcmp (note->namedata, "LINUX") == 0)
10542 return elfcore_grok_aarch_sve (abfd, note);
10543 else
10544 return TRUE;
10545
e6c3b5bf
AH
10546 case NT_ARM_PAC_MASK:
10547 if (note->namesz == 6
10548 && strcmp (note->namedata, "LINUX") == 0)
10549 return elfcore_grok_aarch_pauth (abfd, note);
10550 else
10551 return TRUE;
10552
252b5132
RH
10553 case NT_PRPSINFO:
10554 case NT_PSINFO:
bb0082d6
AM
10555 if (bed->elf_backend_grok_psinfo)
10556 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
b34976b6 10557 return TRUE;
bb0082d6 10558#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
252b5132 10559 return elfcore_grok_psinfo (abfd, note);
bb0082d6 10560#else
b34976b6 10561 return TRUE;
252b5132 10562#endif
3333a7c3
RM
10563
10564 case NT_AUXV:
58e07198 10565 return elfcore_make_auxv_note_section (abfd, note, 0);
9015683b 10566
451b7c33
TT
10567 case NT_FILE:
10568 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
10569 note);
10570
9015683b
TT
10571 case NT_SIGINFO:
10572 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
10573 note);
5b2c414d 10574
252b5132
RH
10575 }
10576}
10577
718175fa
JK
10578static bfd_boolean
10579elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
10580{
c74f7d1c 10581 struct bfd_build_id* build_id;
30e8ee25
AM
10582
10583 if (note->descsz == 0)
10584 return FALSE;
10585
c74f7d1c
JT
10586 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
10587 if (build_id == NULL)
718175fa
JK
10588 return FALSE;
10589
c74f7d1c
JT
10590 build_id->size = note->descsz;
10591 memcpy (build_id->data, note->descdata, note->descsz);
10592 abfd->build_id = build_id;
718175fa
JK
10593
10594 return TRUE;
10595}
10596
10597static bfd_boolean
10598elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
10599{
10600 switch (note->type)
10601 {
10602 default:
10603 return TRUE;
10604
46bed679
L
10605 case NT_GNU_PROPERTY_TYPE_0:
10606 return _bfd_elf_parse_gnu_properties (abfd, note);
10607
718175fa
JK
10608 case NT_GNU_BUILD_ID:
10609 return elfobj_grok_gnu_build_id (abfd, note);
10610 }
10611}
10612
e21e5835
NC
10613static bfd_boolean
10614elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
10615{
10616 struct sdt_note *cur =
7a6e0d89
AM
10617 (struct sdt_note *) bfd_alloc (abfd,
10618 sizeof (struct sdt_note) + note->descsz);
e21e5835
NC
10619
10620 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
10621 cur->size = (bfd_size_type) note->descsz;
10622 memcpy (cur->data, note->descdata, note->descsz);
10623
10624 elf_tdata (abfd)->sdt_note_head = cur;
10625
10626 return TRUE;
10627}
10628
10629static bfd_boolean
10630elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
10631{
10632 switch (note->type)
10633 {
10634 case NT_STAPSDT:
10635 return elfobj_grok_stapsdt_note_1 (abfd, note);
10636
10637 default:
10638 return TRUE;
10639 }
10640}
10641
aa1ed4a9
JB
10642static bfd_boolean
10643elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
10644{
10645 size_t offset;
10646
b5430a3c 10647 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
aa1ed4a9 10648 {
b5430a3c 10649 case ELFCLASS32:
0064d223
JB
10650 if (note->descsz < 108)
10651 return FALSE;
aa1ed4a9
JB
10652 break;
10653
b5430a3c 10654 case ELFCLASS64:
0064d223
JB
10655 if (note->descsz < 120)
10656 return FALSE;
aa1ed4a9
JB
10657 break;
10658
10659 default:
10660 return FALSE;
10661 }
10662
0064d223
JB
10663 /* Check for version 1 in pr_version. */
10664 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10665 return FALSE;
80a04378 10666
0064d223
JB
10667 offset = 4;
10668
10669 /* Skip over pr_psinfosz. */
b5430a3c 10670 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
0064d223
JB
10671 offset += 4;
10672 else
10673 {
10674 offset += 4; /* Padding before pr_psinfosz. */
10675 offset += 8;
10676 }
10677
aa1ed4a9
JB
10678 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10679 elf_tdata (abfd)->core->program
10680 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
10681 offset += 17;
10682
10683 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10684 elf_tdata (abfd)->core->command
10685 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
0064d223
JB
10686 offset += 81;
10687
10688 /* Padding before pr_pid. */
10689 offset += 2;
10690
10691 /* The pr_pid field was added in version "1a". */
10692 if (note->descsz < offset + 4)
10693 return TRUE;
10694
10695 elf_tdata (abfd)->core->pid
10696 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
aa1ed4a9
JB
10697
10698 return TRUE;
10699}
10700
10701static bfd_boolean
10702elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
10703{
10704 size_t offset;
10705 size_t size;
24d3e51b 10706 size_t min_size;
aa1ed4a9 10707
24d3e51b
NC
10708 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10709 Also compute minimum size of this note. */
b5430a3c 10710 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
aa1ed4a9 10711 {
b5430a3c 10712 case ELFCLASS32:
24d3e51b
NC
10713 offset = 4 + 4;
10714 min_size = offset + (4 * 2) + 4 + 4 + 4;
aa1ed4a9
JB
10715 break;
10716
b5430a3c 10717 case ELFCLASS64:
24d3e51b
NC
10718 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
10719 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
aa1ed4a9
JB
10720 break;
10721
10722 default:
10723 return FALSE;
10724 }
10725
24d3e51b
NC
10726 if (note->descsz < min_size)
10727 return FALSE;
10728
10729 /* Check for version 1 in pr_version. */
10730 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10731 return FALSE;
aa1ed4a9 10732
24d3e51b
NC
10733 /* Extract size of pr_reg from pr_gregsetsz. */
10734 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
b5430a3c 10735 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
24d3e51b
NC
10736 {
10737 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10738 offset += 4 * 2;
10739 }
b5430a3c 10740 else
24d3e51b
NC
10741 {
10742 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
10743 offset += 8 * 2;
10744 }
aa1ed4a9 10745
24d3e51b 10746 /* Skip over pr_osreldate. */
aa1ed4a9
JB
10747 offset += 4;
10748
24d3e51b 10749 /* Read signal from pr_cursig. */
aa1ed4a9
JB
10750 if (elf_tdata (abfd)->core->signal == 0)
10751 elf_tdata (abfd)->core->signal
10752 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10753 offset += 4;
10754
24d3e51b 10755 /* Read TID from pr_pid. */
aa1ed4a9
JB
10756 elf_tdata (abfd)->core->lwpid
10757 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10758 offset += 4;
10759
24d3e51b 10760 /* Padding before pr_reg. */
b5430a3c 10761 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
aa1ed4a9
JB
10762 offset += 4;
10763
24d3e51b
NC
10764 /* Make sure that there is enough data remaining in the note. */
10765 if ((note->descsz - offset) < size)
10766 return FALSE;
10767
aa1ed4a9
JB
10768 /* Make a ".reg/999" section and a ".reg" section. */
10769 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10770 size, note->descpos + offset);
10771}
10772
10773static bfd_boolean
10774elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
10775{
544c67cd
JB
10776 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10777
aa1ed4a9
JB
10778 switch (note->type)
10779 {
10780 case NT_PRSTATUS:
544c67cd
JB
10781 if (bed->elf_backend_grok_freebsd_prstatus)
10782 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
10783 return TRUE;
aa1ed4a9
JB
10784 return elfcore_grok_freebsd_prstatus (abfd, note);
10785
10786 case NT_FPREGSET:
10787 return elfcore_grok_prfpreg (abfd, note);
10788
10789 case NT_PRPSINFO:
10790 return elfcore_grok_freebsd_psinfo (abfd, note);
10791
10792 case NT_FREEBSD_THRMISC:
10793 if (note->namesz == 8)
10794 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
10795 else
10796 return TRUE;
10797
ddb2bbcf
JB
10798 case NT_FREEBSD_PROCSTAT_PROC:
10799 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
10800 note);
10801
10802 case NT_FREEBSD_PROCSTAT_FILES:
10803 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
10804 note);
10805
10806 case NT_FREEBSD_PROCSTAT_VMMAP:
10807 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
10808 note);
10809
3350c5f5 10810 case NT_FREEBSD_PROCSTAT_AUXV:
58e07198 10811 return elfcore_make_auxv_note_section (abfd, note, 4);
3350c5f5 10812
aa1ed4a9
JB
10813 case NT_X86_XSTATE:
10814 if (note->namesz == 8)
10815 return elfcore_grok_xstatereg (abfd, note);
10816 else
10817 return TRUE;
10818
e6f3b9c3
JB
10819 case NT_FREEBSD_PTLWPINFO:
10820 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
10821 note);
10822
6d5be5d6
JB
10823 case NT_ARM_VFP:
10824 return elfcore_grok_arm_vfp (abfd, note);
10825
aa1ed4a9
JB
10826 default:
10827 return TRUE;
10828 }
10829}
10830
b34976b6 10831static bfd_boolean
217aa764 10832elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
50b2bdb7
AM
10833{
10834 char *cp;
10835
10836 cp = strchr (note->namedata, '@');
10837 if (cp != NULL)
10838 {
d2b64500 10839 *lwpidp = atoi(cp + 1);
b34976b6 10840 return TRUE;
50b2bdb7 10841 }
b34976b6 10842 return FALSE;
50b2bdb7
AM
10843}
10844
b34976b6 10845static bfd_boolean
217aa764 10846elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7 10847{
80a04378
NC
10848 if (note->descsz <= 0x7c + 31)
10849 return FALSE;
10850
50b2bdb7 10851 /* Signal number at offset 0x08. */
228e534f 10852 elf_tdata (abfd)->core->signal
50b2bdb7
AM
10853 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10854
10855 /* Process ID at offset 0x50. */
228e534f 10856 elf_tdata (abfd)->core->pid
50b2bdb7
AM
10857 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
10858
10859 /* Command name at 0x7c (max 32 bytes, including nul). */
228e534f 10860 elf_tdata (abfd)->core->command
50b2bdb7
AM
10861 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
10862
7720ba9f
MK
10863 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
10864 note);
50b2bdb7
AM
10865}
10866
b34976b6 10867static bfd_boolean
217aa764 10868elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7
AM
10869{
10870 int lwp;
10871
10872 if (elfcore_netbsd_get_lwpid (note, &lwp))
228e534f 10873 elf_tdata (abfd)->core->lwpid = lwp;
50b2bdb7 10874
58e07198 10875 switch (note->type)
50b2bdb7 10876 {
58e07198 10877 case NT_NETBSDCORE_PROCINFO:
50b2bdb7 10878 /* NetBSD-specific core "procinfo". Note that we expect to
08a40648
AM
10879 find this note before any of the others, which is fine,
10880 since the kernel writes this note out first when it
10881 creates a core file. */
50b2bdb7 10882 return elfcore_grok_netbsd_procinfo (abfd, note);
58e07198
CZ
10883#ifdef NT_NETBSDCORE_AUXV
10884 case NT_NETBSDCORE_AUXV:
10885 /* NetBSD-specific Elf Auxiliary Vector data. */
10886 return elfcore_make_auxv_note_section (abfd, note, 4);
06d949ec
KR
10887#endif
10888#ifdef NT_NETBSDCORE_LWPSTATUS
10889 case NT_NETBSDCORE_LWPSTATUS:
10890 return elfcore_make_note_pseudosection (abfd,
10891 ".note.netbsdcore.lwpstatus",
10892 note);
58e07198
CZ
10893#endif
10894 default:
10895 break;
50b2bdb7
AM
10896 }
10897
06d949ec 10898 /* As of March 2020 there are no other machine-independent notes
b4db1224
JT
10899 defined for NetBSD core files. If the note type is less
10900 than the start of the machine-dependent note types, we don't
10901 understand it. */
47d9a591 10902
b4db1224 10903 if (note->type < NT_NETBSDCORE_FIRSTMACH)
b34976b6 10904 return TRUE;
50b2bdb7
AM
10905
10906
10907 switch (bfd_get_arch (abfd))
10908 {
08a40648
AM
10909 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10910 PT_GETFPREGS == mach+2. */
50b2bdb7 10911
015ec493 10912 case bfd_arch_aarch64:
50b2bdb7
AM
10913 case bfd_arch_alpha:
10914 case bfd_arch_sparc:
10915 switch (note->type)
08a40648
AM
10916 {
10917 case NT_NETBSDCORE_FIRSTMACH+0:
10918 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 10919
08a40648
AM
10920 case NT_NETBSDCORE_FIRSTMACH+2:
10921 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 10922
08a40648
AM
10923 default:
10924 return TRUE;
10925 }
50b2bdb7 10926
58e07198
CZ
10927 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
10928 There's also old PT___GETREGS40 == mach + 1 for old reg
10929 structure which lacks GBR. */
10930
10931 case bfd_arch_sh:
10932 switch (note->type)
10933 {
10934 case NT_NETBSDCORE_FIRSTMACH+3:
10935 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10936
10937 case NT_NETBSDCORE_FIRSTMACH+5:
10938 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10939
10940 default:
10941 return TRUE;
10942 }
10943
08a40648
AM
10944 /* On all other arch's, PT_GETREGS == mach+1 and
10945 PT_GETFPREGS == mach+3. */
50b2bdb7
AM
10946
10947 default:
10948 switch (note->type)
08a40648
AM
10949 {
10950 case NT_NETBSDCORE_FIRSTMACH+1:
10951 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 10952
08a40648
AM
10953 case NT_NETBSDCORE_FIRSTMACH+3:
10954 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 10955
08a40648
AM
10956 default:
10957 return TRUE;
10958 }
50b2bdb7
AM
10959 }
10960 /* NOTREACHED */
10961}
10962
67cc5033
MK
10963static bfd_boolean
10964elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
10965{
80a04378
NC
10966 if (note->descsz <= 0x48 + 31)
10967 return FALSE;
10968
67cc5033 10969 /* Signal number at offset 0x08. */
228e534f 10970 elf_tdata (abfd)->core->signal
67cc5033
MK
10971 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10972
10973 /* Process ID at offset 0x20. */
228e534f 10974 elf_tdata (abfd)->core->pid
67cc5033
MK
10975 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
10976
10977 /* Command name at 0x48 (max 32 bytes, including nul). */
228e534f 10978 elf_tdata (abfd)->core->command
67cc5033
MK
10979 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
10980
10981 return TRUE;
10982}
10983
10984static bfd_boolean
10985elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
10986{
10987 if (note->type == NT_OPENBSD_PROCINFO)
10988 return elfcore_grok_openbsd_procinfo (abfd, note);
10989
10990 if (note->type == NT_OPENBSD_REGS)
10991 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10992
10993 if (note->type == NT_OPENBSD_FPREGS)
10994 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10995
10996 if (note->type == NT_OPENBSD_XFPREGS)
10997 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
10998
10999 if (note->type == NT_OPENBSD_AUXV)
58e07198 11000 return elfcore_make_auxv_note_section (abfd, note, 0);
67cc5033
MK
11001
11002 if (note->type == NT_OPENBSD_WCOOKIE)
11003 {
11004 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
11005 SEC_HAS_CONTENTS);
11006
11007 if (sect == NULL)
11008 return FALSE;
11009 sect->size = note->descsz;
11010 sect->filepos = note->descpos;
11011 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
11012
11013 return TRUE;
11014 }
11015
11016 return TRUE;
11017}
11018
07c6e936 11019static bfd_boolean
d3fd4074 11020elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
07c6e936
NC
11021{
11022 void *ddata = note->descdata;
11023 char buf[100];
11024 char *name;
11025 asection *sect;
f8843e87
AM
11026 short sig;
11027 unsigned flags;
07c6e936 11028
80a04378
NC
11029 if (note->descsz < 16)
11030 return FALSE;
11031
07c6e936 11032 /* nto_procfs_status 'pid' field is at offset 0. */
228e534f 11033 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
07c6e936 11034
f8843e87
AM
11035 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
11036 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
11037
11038 /* nto_procfs_status 'flags' field is at offset 8. */
11039 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
07c6e936
NC
11040
11041 /* nto_procfs_status 'what' field is at offset 14. */
f8843e87
AM
11042 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
11043 {
228e534f
AM
11044 elf_tdata (abfd)->core->signal = sig;
11045 elf_tdata (abfd)->core->lwpid = *tid;
f8843e87 11046 }
07c6e936 11047
f8843e87
AM
11048 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
11049 do not come from signals so we make sure we set the current
11050 thread just in case. */
11051 if (flags & 0x00000080)
228e534f 11052 elf_tdata (abfd)->core->lwpid = *tid;
07c6e936
NC
11053
11054 /* Make a ".qnx_core_status/%d" section. */
d3fd4074 11055 sprintf (buf, ".qnx_core_status/%ld", *tid);
07c6e936 11056
a50b1753 11057 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
07c6e936
NC
11058 if (name == NULL)
11059 return FALSE;
11060 strcpy (name, buf);
11061
117ed4f8 11062 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936
NC
11063 if (sect == NULL)
11064 return FALSE;
11065
07d6d2b8
AM
11066 sect->size = note->descsz;
11067 sect->filepos = note->descpos;
07c6e936
NC
11068 sect->alignment_power = 2;
11069
11070 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
11071}
11072
11073static bfd_boolean
d69f560c
KW
11074elfcore_grok_nto_regs (bfd *abfd,
11075 Elf_Internal_Note *note,
d3fd4074 11076 long tid,
d69f560c 11077 char *base)
07c6e936
NC
11078{
11079 char buf[100];
11080 char *name;
11081 asection *sect;
11082
d69f560c 11083 /* Make a "(base)/%d" section. */
d3fd4074 11084 sprintf (buf, "%s/%ld", base, tid);
07c6e936 11085
a50b1753 11086 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
07c6e936
NC
11087 if (name == NULL)
11088 return FALSE;
11089 strcpy (name, buf);
11090
117ed4f8 11091 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936
NC
11092 if (sect == NULL)
11093 return FALSE;
11094
07d6d2b8
AM
11095 sect->size = note->descsz;
11096 sect->filepos = note->descpos;
07c6e936
NC
11097 sect->alignment_power = 2;
11098
f8843e87 11099 /* This is the current thread. */
228e534f 11100 if (elf_tdata (abfd)->core->lwpid == tid)
d69f560c 11101 return elfcore_maybe_make_sect (abfd, base, sect);
f8843e87
AM
11102
11103 return TRUE;
07c6e936
NC
11104}
11105
11106#define BFD_QNT_CORE_INFO 7
11107#define BFD_QNT_CORE_STATUS 8
11108#define BFD_QNT_CORE_GREG 9
11109#define BFD_QNT_CORE_FPREG 10
11110
11111static bfd_boolean
217aa764 11112elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
07c6e936
NC
11113{
11114 /* Every GREG section has a STATUS section before it. Store the
811072d8 11115 tid from the previous call to pass down to the next gregs
07c6e936 11116 function. */
d3fd4074 11117 static long tid = 1;
07c6e936
NC
11118
11119 switch (note->type)
11120 {
d69f560c
KW
11121 case BFD_QNT_CORE_INFO:
11122 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
11123 case BFD_QNT_CORE_STATUS:
11124 return elfcore_grok_nto_status (abfd, note, &tid);
11125 case BFD_QNT_CORE_GREG:
11126 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
11127 case BFD_QNT_CORE_FPREG:
11128 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
11129 default:
11130 return TRUE;
07c6e936
NC
11131 }
11132}
11133
b15fa79e
AM
11134static bfd_boolean
11135elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
11136{
11137 char *name;
11138 asection *sect;
11139 size_t len;
11140
11141 /* Use note name as section name. */
11142 len = note->namesz;
a50b1753 11143 name = (char *) bfd_alloc (abfd, len);
b15fa79e
AM
11144 if (name == NULL)
11145 return FALSE;
11146 memcpy (name, note->namedata, len);
11147 name[len - 1] = '\0';
11148
11149 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11150 if (sect == NULL)
11151 return FALSE;
11152
07d6d2b8
AM
11153 sect->size = note->descsz;
11154 sect->filepos = note->descpos;
b15fa79e
AM
11155 sect->alignment_power = 1;
11156
11157 return TRUE;
11158}
11159
7c76fa91
MS
11160/* Function: elfcore_write_note
11161
47d9a591 11162 Inputs:
a39f3346 11163 buffer to hold note, and current size of buffer
7c76fa91
MS
11164 name of note
11165 type of note
11166 data for note
11167 size of data for note
11168
a39f3346
AM
11169 Writes note to end of buffer. ELF64 notes are written exactly as
11170 for ELF32, despite the current (as of 2006) ELF gabi specifying
11171 that they ought to have 8-byte namesz and descsz field, and have
11172 8-byte alignment. Other writers, eg. Linux kernel, do the same.
11173
7c76fa91 11174 Return:
a39f3346 11175 Pointer to realloc'd buffer, *BUFSIZ updated. */
7c76fa91
MS
11176
11177char *
a39f3346 11178elfcore_write_note (bfd *abfd,
217aa764 11179 char *buf,
a39f3346 11180 int *bufsiz,
217aa764 11181 const char *name,
a39f3346 11182 int type,
217aa764 11183 const void *input,
a39f3346 11184 int size)
7c76fa91
MS
11185{
11186 Elf_External_Note *xnp;
d4c88bbb 11187 size_t namesz;
d4c88bbb 11188 size_t newspace;
a39f3346 11189 char *dest;
7c76fa91 11190
d4c88bbb 11191 namesz = 0;
d4c88bbb 11192 if (name != NULL)
a39f3346 11193 namesz = strlen (name) + 1;
d4c88bbb 11194
a39f3346 11195 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
d4c88bbb 11196
a50b1753 11197 buf = (char *) realloc (buf, *bufsiz + newspace);
14b1c01e
AM
11198 if (buf == NULL)
11199 return buf;
a39f3346 11200 dest = buf + *bufsiz;
7c76fa91
MS
11201 *bufsiz += newspace;
11202 xnp = (Elf_External_Note *) dest;
11203 H_PUT_32 (abfd, namesz, xnp->namesz);
11204 H_PUT_32 (abfd, size, xnp->descsz);
11205 H_PUT_32 (abfd, type, xnp->type);
d4c88bbb
AM
11206 dest = xnp->name;
11207 if (name != NULL)
11208 {
11209 memcpy (dest, name, namesz);
11210 dest += namesz;
a39f3346 11211 while (namesz & 3)
d4c88bbb
AM
11212 {
11213 *dest++ = '\0';
a39f3346 11214 ++namesz;
d4c88bbb
AM
11215 }
11216 }
11217 memcpy (dest, input, size);
a39f3346
AM
11218 dest += size;
11219 while (size & 3)
11220 {
11221 *dest++ = '\0';
11222 ++size;
11223 }
11224 return buf;
7c76fa91
MS
11225}
11226
602f1657
AM
11227/* gcc-8 warns (*) on all the strncpy calls in this function about
11228 possible string truncation. The "truncation" is not a bug. We
11229 have an external representation of structs with fields that are not
11230 necessarily NULL terminated and corresponding internal
11231 representation fields that are one larger so that they can always
11232 be NULL terminated.
11233 gcc versions between 4.2 and 4.6 do not allow pragma control of
11234 diagnostics inside functions, giving a hard error if you try to use
11235 the finer control available with later versions.
11236 gcc prior to 4.2 warns about diagnostic push and pop.
11237 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
11238 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
11239 (*) Depending on your system header files! */
d99b4b92 11240#if GCC_VERSION >= 8000
602f1657
AM
11241# pragma GCC diagnostic push
11242# pragma GCC diagnostic ignored "-Wstringop-truncation"
d99b4b92 11243#endif
7c76fa91 11244char *
217aa764
AM
11245elfcore_write_prpsinfo (bfd *abfd,
11246 char *buf,
11247 int *bufsiz,
11248 const char *fname,
11249 const char *psargs)
7c76fa91 11250{
183e98be
AM
11251 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11252
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_PRPSINFO, fname, psargs);
11258 if (ret != NULL)
11259 return ret;
11260 }
7c76fa91 11261
1f20dca5 11262#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
602f1657 11263# if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
183e98be
AM
11264 if (bed->s->elfclass == ELFCLASS32)
11265 {
602f1657 11266# if defined (HAVE_PSINFO32_T)
183e98be
AM
11267 psinfo32_t data;
11268 int note_type = NT_PSINFO;
602f1657 11269# else
183e98be
AM
11270 prpsinfo32_t data;
11271 int note_type = NT_PRPSINFO;
602f1657 11272# endif
183e98be
AM
11273
11274 memset (&data, 0, sizeof (data));
11275 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11276 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11277 return elfcore_write_note (abfd, buf, bufsiz,
1f20dca5 11278 "CORE", note_type, &data, sizeof (data));
183e98be
AM
11279 }
11280 else
602f1657 11281# endif
183e98be 11282 {
602f1657 11283# if defined (HAVE_PSINFO_T)
183e98be
AM
11284 psinfo_t data;
11285 int note_type = NT_PSINFO;
602f1657 11286# else
183e98be
AM
11287 prpsinfo_t data;
11288 int note_type = NT_PRPSINFO;
602f1657 11289# endif
7c76fa91 11290
183e98be
AM
11291 memset (&data, 0, sizeof (data));
11292 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11293 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11294 return elfcore_write_note (abfd, buf, bufsiz,
1f20dca5 11295 "CORE", note_type, &data, sizeof (data));
183e98be 11296 }
7c76fa91
MS
11297#endif /* PSINFO_T or PRPSINFO_T */
11298
1f20dca5
UW
11299 free (buf);
11300 return NULL;
11301}
d99b4b92 11302#if GCC_VERSION >= 8000
602f1657 11303# pragma GCC diagnostic pop
d99b4b92 11304#endif
1f20dca5 11305
70a38d42
SDJ
11306char *
11307elfcore_write_linux_prpsinfo32
11308 (bfd *abfd, char *buf, int *bufsiz,
11309 const struct elf_internal_linux_prpsinfo *prpsinfo)
11310{
a2f63b2e
MR
11311 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
11312 {
11313 struct elf_external_linux_prpsinfo32_ugid16 data;
11314
11315 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
11316 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11317 &data, sizeof (data));
11318 }
11319 else
11320 {
11321 struct elf_external_linux_prpsinfo32_ugid32 data;
70a38d42 11322
a2f63b2e
MR
11323 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
11324 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11325 &data, sizeof (data));
11326 }
70a38d42
SDJ
11327}
11328
11329char *
11330elfcore_write_linux_prpsinfo64
11331 (bfd *abfd, char *buf, int *bufsiz,
11332 const struct elf_internal_linux_prpsinfo *prpsinfo)
11333{
3c9a7b0d
MR
11334 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
11335 {
11336 struct elf_external_linux_prpsinfo64_ugid16 data;
11337
11338 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
11339 return elfcore_write_note (abfd, buf, bufsiz,
11340 "CORE", NT_PRPSINFO, &data, sizeof (data));
11341 }
11342 else
11343 {
11344 struct elf_external_linux_prpsinfo64_ugid32 data;
70a38d42 11345
3c9a7b0d
MR
11346 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
11347 return elfcore_write_note (abfd, buf, bufsiz,
11348 "CORE", NT_PRPSINFO, &data, sizeof (data));
11349 }
70a38d42
SDJ
11350}
11351
7c76fa91 11352char *
217aa764
AM
11353elfcore_write_prstatus (bfd *abfd,
11354 char *buf,
11355 int *bufsiz,
11356 long pid,
11357 int cursig,
11358 const void *gregs)
7c76fa91 11359{
183e98be 11360 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 11361
183e98be
AM
11362 if (bed->elf_backend_write_core_note != NULL)
11363 {
11364 char *ret;
11365 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11366 NT_PRSTATUS,
11367 pid, cursig, gregs);
11368 if (ret != NULL)
11369 return ret;
11370 }
11371
1f20dca5 11372#if defined (HAVE_PRSTATUS_T)
183e98be
AM
11373#if defined (HAVE_PRSTATUS32_T)
11374 if (bed->s->elfclass == ELFCLASS32)
11375 {
11376 prstatus32_t prstat;
11377
11378 memset (&prstat, 0, sizeof (prstat));
11379 prstat.pr_pid = pid;
11380 prstat.pr_cursig = cursig;
11381 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
1f20dca5 11382 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
183e98be
AM
11383 NT_PRSTATUS, &prstat, sizeof (prstat));
11384 }
11385 else
11386#endif
11387 {
11388 prstatus_t prstat;
11389
11390 memset (&prstat, 0, sizeof (prstat));
11391 prstat.pr_pid = pid;
11392 prstat.pr_cursig = cursig;
11393 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
1f20dca5 11394 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
183e98be
AM
11395 NT_PRSTATUS, &prstat, sizeof (prstat));
11396 }
7c76fa91
MS
11397#endif /* HAVE_PRSTATUS_T */
11398
1f20dca5
UW
11399 free (buf);
11400 return NULL;
11401}
11402
51316059
MS
11403#if defined (HAVE_LWPSTATUS_T)
11404char *
217aa764
AM
11405elfcore_write_lwpstatus (bfd *abfd,
11406 char *buf,
11407 int *bufsiz,
11408 long pid,
11409 int cursig,
11410 const void *gregs)
51316059
MS
11411{
11412 lwpstatus_t lwpstat;
183e98be 11413 const char *note_name = "CORE";
51316059
MS
11414
11415 memset (&lwpstat, 0, sizeof (lwpstat));
11416 lwpstat.pr_lwpid = pid >> 16;
11417 lwpstat.pr_cursig = cursig;
11418#if defined (HAVE_LWPSTATUS_T_PR_REG)
d1e8523e 11419 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
51316059
MS
11420#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11421#if !defined(gregs)
11422 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
11423 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
11424#else
11425 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
11426 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
11427#endif
11428#endif
47d9a591 11429 return elfcore_write_note (abfd, buf, bufsiz, note_name,
51316059
MS
11430 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
11431}
11432#endif /* HAVE_LWPSTATUS_T */
11433
7c76fa91
MS
11434#if defined (HAVE_PSTATUS_T)
11435char *
217aa764
AM
11436elfcore_write_pstatus (bfd *abfd,
11437 char *buf,
11438 int *bufsiz,
11439 long pid,
6c10990d
NC
11440 int cursig ATTRIBUTE_UNUSED,
11441 const void *gregs ATTRIBUTE_UNUSED)
7c76fa91 11442{
183e98be
AM
11443 const char *note_name = "CORE";
11444#if defined (HAVE_PSTATUS32_T)
11445 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 11446
183e98be
AM
11447 if (bed->s->elfclass == ELFCLASS32)
11448 {
11449 pstatus32_t pstat;
11450
11451 memset (&pstat, 0, sizeof (pstat));
11452 pstat.pr_pid = pid & 0xffff;
11453 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11454 NT_PSTATUS, &pstat, sizeof (pstat));
11455 return buf;
11456 }
11457 else
11458#endif
11459 {
11460 pstatus_t pstat;
11461
11462 memset (&pstat, 0, sizeof (pstat));
11463 pstat.pr_pid = pid & 0xffff;
11464 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11465 NT_PSTATUS, &pstat, sizeof (pstat));
11466 return buf;
11467 }
7c76fa91
MS
11468}
11469#endif /* HAVE_PSTATUS_T */
11470
11471char *
217aa764
AM
11472elfcore_write_prfpreg (bfd *abfd,
11473 char *buf,
11474 int *bufsiz,
11475 const void *fpregs,
11476 int size)
7c76fa91 11477{
183e98be 11478 const char *note_name = "CORE";
47d9a591 11479 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
11480 note_name, NT_FPREGSET, fpregs, size);
11481}
11482
11483char *
217aa764
AM
11484elfcore_write_prxfpreg (bfd *abfd,
11485 char *buf,
11486 int *bufsiz,
11487 const void *xfpregs,
11488 int size)
7c76fa91
MS
11489{
11490 char *note_name = "LINUX";
47d9a591 11491 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
11492 note_name, NT_PRXFPREG, xfpregs, size);
11493}
11494
4339cae0
L
11495char *
11496elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
11497 const void *xfpregs, int size)
11498{
97de3545
JB
11499 char *note_name;
11500 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
11501 note_name = "FreeBSD";
11502 else
11503 note_name = "LINUX";
4339cae0
L
11504 return elfcore_write_note (abfd, buf, bufsiz,
11505 note_name, NT_X86_XSTATE, xfpregs, size);
11506}
11507
97753bd5
AM
11508char *
11509elfcore_write_ppc_vmx (bfd *abfd,
11510 char *buf,
11511 int *bufsiz,
11512 const void *ppc_vmx,
11513 int size)
11514{
11515 char *note_name = "LINUX";
11516 return elfcore_write_note (abfd, buf, bufsiz,
11517 note_name, NT_PPC_VMX, ppc_vmx, size);
11518}
11519
89eeb0bc
LM
11520char *
11521elfcore_write_ppc_vsx (bfd *abfd,
07d6d2b8
AM
11522 char *buf,
11523 int *bufsiz,
11524 const void *ppc_vsx,
11525 int size)
89eeb0bc
LM
11526{
11527 char *note_name = "LINUX";
11528 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11529 note_name, NT_PPC_VSX, ppc_vsx, size);
89eeb0bc
LM
11530}
11531
cb2366c1
EBM
11532char *
11533elfcore_write_ppc_tar (bfd *abfd,
4b24dd1a
AM
11534 char *buf,
11535 int *bufsiz,
11536 const void *ppc_tar,
11537 int size)
cb2366c1
EBM
11538{
11539 char *note_name = "LINUX";
11540 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11541 note_name, NT_PPC_TAR, ppc_tar, size);
cb2366c1
EBM
11542}
11543
11544char *
11545elfcore_write_ppc_ppr (bfd *abfd,
4b24dd1a
AM
11546 char *buf,
11547 int *bufsiz,
11548 const void *ppc_ppr,
11549 int size)
cb2366c1
EBM
11550{
11551 char *note_name = "LINUX";
11552 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11553 note_name, NT_PPC_PPR, ppc_ppr, size);
cb2366c1
EBM
11554}
11555
11556char *
11557elfcore_write_ppc_dscr (bfd *abfd,
4b24dd1a
AM
11558 char *buf,
11559 int *bufsiz,
11560 const void *ppc_dscr,
11561 int size)
cb2366c1
EBM
11562{
11563 char *note_name = "LINUX";
11564 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11565 note_name, NT_PPC_DSCR, ppc_dscr, size);
cb2366c1
EBM
11566}
11567
11568char *
11569elfcore_write_ppc_ebb (bfd *abfd,
4b24dd1a
AM
11570 char *buf,
11571 int *bufsiz,
11572 const void *ppc_ebb,
11573 int size)
cb2366c1
EBM
11574{
11575 char *note_name = "LINUX";
11576 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11577 note_name, NT_PPC_EBB, ppc_ebb, size);
cb2366c1
EBM
11578}
11579
11580char *
11581elfcore_write_ppc_pmu (bfd *abfd,
4b24dd1a
AM
11582 char *buf,
11583 int *bufsiz,
11584 const void *ppc_pmu,
11585 int size)
cb2366c1
EBM
11586{
11587 char *note_name = "LINUX";
11588 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11589 note_name, NT_PPC_PMU, ppc_pmu, size);
cb2366c1
EBM
11590}
11591
11592char *
11593elfcore_write_ppc_tm_cgpr (bfd *abfd,
4b24dd1a
AM
11594 char *buf,
11595 int *bufsiz,
11596 const void *ppc_tm_cgpr,
11597 int size)
cb2366c1
EBM
11598{
11599 char *note_name = "LINUX";
11600 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11601 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
cb2366c1
EBM
11602}
11603
11604char *
11605elfcore_write_ppc_tm_cfpr (bfd *abfd,
4b24dd1a
AM
11606 char *buf,
11607 int *bufsiz,
11608 const void *ppc_tm_cfpr,
11609 int size)
cb2366c1
EBM
11610{
11611 char *note_name = "LINUX";
11612 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11613 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
cb2366c1
EBM
11614}
11615
11616char *
11617elfcore_write_ppc_tm_cvmx (bfd *abfd,
4b24dd1a
AM
11618 char *buf,
11619 int *bufsiz,
11620 const void *ppc_tm_cvmx,
11621 int size)
cb2366c1
EBM
11622{
11623 char *note_name = "LINUX";
11624 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11625 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
cb2366c1
EBM
11626}
11627
11628char *
11629elfcore_write_ppc_tm_cvsx (bfd *abfd,
4b24dd1a
AM
11630 char *buf,
11631 int *bufsiz,
11632 const void *ppc_tm_cvsx,
11633 int size)
cb2366c1
EBM
11634{
11635 char *note_name = "LINUX";
11636 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11637 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
cb2366c1
EBM
11638}
11639
11640char *
11641elfcore_write_ppc_tm_spr (bfd *abfd,
4b24dd1a
AM
11642 char *buf,
11643 int *bufsiz,
11644 const void *ppc_tm_spr,
11645 int size)
cb2366c1
EBM
11646{
11647 char *note_name = "LINUX";
11648 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11649 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
cb2366c1
EBM
11650}
11651
11652char *
11653elfcore_write_ppc_tm_ctar (bfd *abfd,
4b24dd1a
AM
11654 char *buf,
11655 int *bufsiz,
11656 const void *ppc_tm_ctar,
11657 int size)
cb2366c1
EBM
11658{
11659 char *note_name = "LINUX";
11660 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11661 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
cb2366c1
EBM
11662}
11663
11664char *
11665elfcore_write_ppc_tm_cppr (bfd *abfd,
4b24dd1a
AM
11666 char *buf,
11667 int *bufsiz,
11668 const void *ppc_tm_cppr,
11669 int size)
cb2366c1
EBM
11670{
11671 char *note_name = "LINUX";
11672 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11673 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
cb2366c1
EBM
11674}
11675
11676char *
11677elfcore_write_ppc_tm_cdscr (bfd *abfd,
4b24dd1a
AM
11678 char *buf,
11679 int *bufsiz,
11680 const void *ppc_tm_cdscr,
11681 int size)
cb2366c1
EBM
11682{
11683 char *note_name = "LINUX";
11684 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11685 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
cb2366c1
EBM
11686}
11687
0675e188
UW
11688static char *
11689elfcore_write_s390_high_gprs (bfd *abfd,
11690 char *buf,
11691 int *bufsiz,
11692 const void *s390_high_gprs,
11693 int size)
11694{
11695 char *note_name = "LINUX";
11696 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11697 note_name, NT_S390_HIGH_GPRS,
0675e188
UW
11698 s390_high_gprs, size);
11699}
11700
d7eeb400
MS
11701char *
11702elfcore_write_s390_timer (bfd *abfd,
07d6d2b8
AM
11703 char *buf,
11704 int *bufsiz,
11705 const void *s390_timer,
11706 int size)
d7eeb400
MS
11707{
11708 char *note_name = "LINUX";
11709 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11710 note_name, NT_S390_TIMER, s390_timer, size);
d7eeb400
MS
11711}
11712
11713char *
11714elfcore_write_s390_todcmp (bfd *abfd,
07d6d2b8
AM
11715 char *buf,
11716 int *bufsiz,
11717 const void *s390_todcmp,
11718 int size)
d7eeb400
MS
11719{
11720 char *note_name = "LINUX";
11721 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11722 note_name, NT_S390_TODCMP, s390_todcmp, size);
d7eeb400
MS
11723}
11724
11725char *
11726elfcore_write_s390_todpreg (bfd *abfd,
07d6d2b8
AM
11727 char *buf,
11728 int *bufsiz,
11729 const void *s390_todpreg,
11730 int size)
d7eeb400
MS
11731{
11732 char *note_name = "LINUX";
11733 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11734 note_name, NT_S390_TODPREG, s390_todpreg, size);
d7eeb400
MS
11735}
11736
11737char *
11738elfcore_write_s390_ctrs (bfd *abfd,
07d6d2b8
AM
11739 char *buf,
11740 int *bufsiz,
11741 const void *s390_ctrs,
11742 int size)
d7eeb400
MS
11743{
11744 char *note_name = "LINUX";
11745 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11746 note_name, NT_S390_CTRS, s390_ctrs, size);
d7eeb400
MS
11747}
11748
11749char *
11750elfcore_write_s390_prefix (bfd *abfd,
07d6d2b8
AM
11751 char *buf,
11752 int *bufsiz,
11753 const void *s390_prefix,
11754 int size)
d7eeb400
MS
11755{
11756 char *note_name = "LINUX";
11757 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11758 note_name, NT_S390_PREFIX, s390_prefix, size);
d7eeb400
MS
11759}
11760
355b81d9
UW
11761char *
11762elfcore_write_s390_last_break (bfd *abfd,
11763 char *buf,
11764 int *bufsiz,
11765 const void *s390_last_break,
11766 int size)
11767{
11768 char *note_name = "LINUX";
11769 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11770 note_name, NT_S390_LAST_BREAK,
355b81d9
UW
11771 s390_last_break, size);
11772}
11773
11774char *
11775elfcore_write_s390_system_call (bfd *abfd,
11776 char *buf,
11777 int *bufsiz,
11778 const void *s390_system_call,
11779 int size)
11780{
11781 char *note_name = "LINUX";
11782 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11783 note_name, NT_S390_SYSTEM_CALL,
355b81d9
UW
11784 s390_system_call, size);
11785}
11786
abb3f6cc
NC
11787char *
11788elfcore_write_s390_tdb (bfd *abfd,
11789 char *buf,
11790 int *bufsiz,
11791 const void *s390_tdb,
11792 int size)
11793{
11794 char *note_name = "LINUX";
11795 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11796 note_name, NT_S390_TDB, s390_tdb, size);
abb3f6cc
NC
11797}
11798
4ef9f41a
AA
11799char *
11800elfcore_write_s390_vxrs_low (bfd *abfd,
11801 char *buf,
11802 int *bufsiz,
11803 const void *s390_vxrs_low,
11804 int size)
11805{
11806 char *note_name = "LINUX";
11807 return elfcore_write_note (abfd, buf, bufsiz,
11808 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
11809}
11810
11811char *
11812elfcore_write_s390_vxrs_high (bfd *abfd,
11813 char *buf,
11814 int *bufsiz,
11815 const void *s390_vxrs_high,
11816 int size)
11817{
11818 char *note_name = "LINUX";
11819 return elfcore_write_note (abfd, buf, bufsiz,
11820 note_name, NT_S390_VXRS_HIGH,
11821 s390_vxrs_high, size);
11822}
11823
88ab90e8
AA
11824char *
11825elfcore_write_s390_gs_cb (bfd *abfd,
11826 char *buf,
11827 int *bufsiz,
11828 const void *s390_gs_cb,
11829 int size)
11830{
11831 char *note_name = "LINUX";
11832 return elfcore_write_note (abfd, buf, bufsiz,
11833 note_name, NT_S390_GS_CB,
11834 s390_gs_cb, size);
11835}
11836
11837char *
11838elfcore_write_s390_gs_bc (bfd *abfd,
11839 char *buf,
11840 int *bufsiz,
11841 const void *s390_gs_bc,
11842 int size)
11843{
11844 char *note_name = "LINUX";
11845 return elfcore_write_note (abfd, buf, bufsiz,
11846 note_name, NT_S390_GS_BC,
11847 s390_gs_bc, size);
11848}
11849
faa9a424
UW
11850char *
11851elfcore_write_arm_vfp (bfd *abfd,
11852 char *buf,
11853 int *bufsiz,
11854 const void *arm_vfp,
11855 int size)
11856{
11857 char *note_name = "LINUX";
11858 return elfcore_write_note (abfd, buf, bufsiz,
11859 note_name, NT_ARM_VFP, arm_vfp, size);
11860}
11861
652451f8
YZ
11862char *
11863elfcore_write_aarch_tls (bfd *abfd,
11864 char *buf,
11865 int *bufsiz,
11866 const void *aarch_tls,
11867 int size)
11868{
11869 char *note_name = "LINUX";
11870 return elfcore_write_note (abfd, buf, bufsiz,
11871 note_name, NT_ARM_TLS, aarch_tls, size);
11872}
11873
11874char *
11875elfcore_write_aarch_hw_break (bfd *abfd,
11876 char *buf,
11877 int *bufsiz,
11878 const void *aarch_hw_break,
11879 int size)
11880{
11881 char *note_name = "LINUX";
11882 return elfcore_write_note (abfd, buf, bufsiz,
11883 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
11884}
11885
11886char *
11887elfcore_write_aarch_hw_watch (bfd *abfd,
11888 char *buf,
11889 int *bufsiz,
11890 const void *aarch_hw_watch,
11891 int size)
11892{
11893 char *note_name = "LINUX";
11894 return elfcore_write_note (abfd, buf, bufsiz,
11895 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
11896}
11897
ad1cc4e4
AH
11898char *
11899elfcore_write_aarch_sve (bfd *abfd,
11900 char *buf,
11901 int *bufsiz,
11902 const void *aarch_sve,
11903 int size)
11904{
11905 char *note_name = "LINUX";
11906 return elfcore_write_note (abfd, buf, bufsiz,
11907 note_name, NT_ARM_SVE, aarch_sve, size);
11908}
11909
e6c3b5bf
AH
11910char *
11911elfcore_write_aarch_pauth (bfd *abfd,
11912 char *buf,
11913 int *bufsiz,
11914 const void *aarch_pauth,
11915 int size)
11916{
11917 char *note_name = "LINUX";
11918 return elfcore_write_note (abfd, buf, bufsiz,
11919 note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
11920}
11921
27456742
AK
11922char *
11923elfcore_write_arc_v2 (bfd *abfd,
11924 char *buf,
11925 int *bufsiz,
11926 const void *arc_v2,
11927 int size)
11928{
11929 char *note_name = "LINUX";
11930 return elfcore_write_note (abfd, buf, bufsiz,
11931 note_name, NT_ARC_V2, arc_v2, size);
11932}
11933
bb864ac1
CES
11934char *
11935elfcore_write_register_note (bfd *abfd,
11936 char *buf,
11937 int *bufsiz,
11938 const char *section,
11939 const void *data,
11940 int size)
11941{
11942 if (strcmp (section, ".reg2") == 0)
11943 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
11944 if (strcmp (section, ".reg-xfp") == 0)
11945 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
4339cae0
L
11946 if (strcmp (section, ".reg-xstate") == 0)
11947 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
bb864ac1
CES
11948 if (strcmp (section, ".reg-ppc-vmx") == 0)
11949 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
89eeb0bc
LM
11950 if (strcmp (section, ".reg-ppc-vsx") == 0)
11951 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
cb2366c1
EBM
11952 if (strcmp (section, ".reg-ppc-tar") == 0)
11953 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
11954 if (strcmp (section, ".reg-ppc-ppr") == 0)
11955 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
11956 if (strcmp (section, ".reg-ppc-dscr") == 0)
11957 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
11958 if (strcmp (section, ".reg-ppc-ebb") == 0)
11959 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
11960 if (strcmp (section, ".reg-ppc-pmu") == 0)
11961 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
11962 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
11963 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
11964 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
11965 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
11966 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
11967 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
11968 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
11969 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
11970 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
11971 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
11972 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
11973 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
11974 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
11975 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
11976 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
11977 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
0675e188
UW
11978 if (strcmp (section, ".reg-s390-high-gprs") == 0)
11979 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
d7eeb400
MS
11980 if (strcmp (section, ".reg-s390-timer") == 0)
11981 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
11982 if (strcmp (section, ".reg-s390-todcmp") == 0)
11983 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
11984 if (strcmp (section, ".reg-s390-todpreg") == 0)
11985 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
11986 if (strcmp (section, ".reg-s390-ctrs") == 0)
11987 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
11988 if (strcmp (section, ".reg-s390-prefix") == 0)
11989 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
355b81d9
UW
11990 if (strcmp (section, ".reg-s390-last-break") == 0)
11991 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
11992 if (strcmp (section, ".reg-s390-system-call") == 0)
11993 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
abb3f6cc
NC
11994 if (strcmp (section, ".reg-s390-tdb") == 0)
11995 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
4ef9f41a
AA
11996 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
11997 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
11998 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
11999 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
88ab90e8
AA
12000 if (strcmp (section, ".reg-s390-gs-cb") == 0)
12001 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
12002 if (strcmp (section, ".reg-s390-gs-bc") == 0)
12003 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
faa9a424
UW
12004 if (strcmp (section, ".reg-arm-vfp") == 0)
12005 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
652451f8
YZ
12006 if (strcmp (section, ".reg-aarch-tls") == 0)
12007 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
12008 if (strcmp (section, ".reg-aarch-hw-break") == 0)
12009 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
12010 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
12011 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
ad1cc4e4
AH
12012 if (strcmp (section, ".reg-aarch-sve") == 0)
12013 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
e6c3b5bf
AH
12014 if (strcmp (section, ".reg-aarch-pauth") == 0)
12015 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
27456742
AK
12016 if (strcmp (section, ".reg-arc-v2") == 0)
12017 return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
bb864ac1
CES
12018 return NULL;
12019}
12020
b34976b6 12021static bfd_boolean
276da9b3
L
12022elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
12023 size_t align)
252b5132 12024{
c044fabd 12025 char *p;
252b5132 12026
276da9b3
L
12027 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
12028 gABI specifies that PT_NOTE alignment should be aligned to 4
12029 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
12030 align is less than 4, we use 4 byte alignment. */
12031 if (align < 4)
12032 align = 4;
ef135d43
NC
12033 if (align != 4 && align != 8)
12034 return FALSE;
276da9b3 12035
252b5132
RH
12036 p = buf;
12037 while (p < buf + size)
12038 {
c044fabd 12039 Elf_External_Note *xnp = (Elf_External_Note *) p;
252b5132
RH
12040 Elf_Internal_Note in;
12041
baea7ef1
AM
12042 if (offsetof (Elf_External_Note, name) > buf - p + size)
12043 return FALSE;
12044
dc810e39 12045 in.type = H_GET_32 (abfd, xnp->type);
252b5132 12046
dc810e39 12047 in.namesz = H_GET_32 (abfd, xnp->namesz);
252b5132 12048 in.namedata = xnp->name;
baea7ef1
AM
12049 if (in.namesz > buf - in.namedata + size)
12050 return FALSE;
252b5132 12051
dc810e39 12052 in.descsz = H_GET_32 (abfd, xnp->descsz);
276da9b3 12053 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
252b5132 12054 in.descpos = offset + (in.descdata - buf);
baea7ef1
AM
12055 if (in.descsz != 0
12056 && (in.descdata >= buf + size
12057 || in.descsz > buf - in.descdata + size))
12058 return FALSE;
252b5132 12059
718175fa 12060 switch (bfd_get_format (abfd))
07d6d2b8 12061 {
718175fa
JK
12062 default:
12063 return TRUE;
12064
12065 case bfd_core:
f64e188b 12066 {
8acbedd6 12067#define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
f64e188b 12068 struct
718175fa 12069 {
f64e188b 12070 const char * string;
8acbedd6 12071 size_t len;
f64e188b 12072 bfd_boolean (* func)(bfd *, Elf_Internal_Note *);
718175fa 12073 }
f64e188b 12074 grokers[] =
b15fa79e 12075 {
8acbedd6 12076 GROKER_ELEMENT ("", elfcore_grok_note),
aa1ed4a9 12077 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
8acbedd6
KS
12078 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
12079 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note),
12080 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
864619bb
KS
12081 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
12082 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note)
f64e188b 12083 };
8acbedd6 12084#undef GROKER_ELEMENT
f64e188b
NC
12085 int i;
12086
12087 for (i = ARRAY_SIZE (grokers); i--;)
8acbedd6
KS
12088 {
12089 if (in.namesz >= grokers[i].len
12090 && strncmp (in.namedata, grokers[i].string,
12091 grokers[i].len) == 0)
12092 {
12093 if (! grokers[i].func (abfd, & in))
12094 return FALSE;
12095 break;
12096 }
12097 }
f64e188b
NC
12098 break;
12099 }
718175fa
JK
12100
12101 case bfd_object:
12102 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
12103 {
12104 if (! elfobj_grok_gnu_note (abfd, &in))
12105 return FALSE;
12106 }
e21e5835
NC
12107 else if (in.namesz == sizeof "stapsdt"
12108 && strcmp (in.namedata, "stapsdt") == 0)
12109 {
12110 if (! elfobj_grok_stapsdt_note (abfd, &in))
12111 return FALSE;
12112 }
718175fa 12113 break;
08a40648 12114 }
252b5132 12115
276da9b3 12116 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
252b5132
RH
12117 }
12118
718175fa
JK
12119 return TRUE;
12120}
12121
864619bb 12122bfd_boolean
276da9b3
L
12123elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
12124 size_t align)
718175fa
JK
12125{
12126 char *buf;
12127
957e1fc1 12128 if (size == 0 || (size + 1) == 0)
718175fa
JK
12129 return TRUE;
12130
12131 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
12132 return FALSE;
12133
2bb3687b 12134 buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
718175fa
JK
12135 if (buf == NULL)
12136 return FALSE;
12137
f64e188b
NC
12138 /* PR 17512: file: ec08f814
12139 0-termintate the buffer so that string searches will not overflow. */
12140 buf[size] = 0;
12141
2bb3687b 12142 if (!elf_parse_notes (abfd, buf, size, offset, align))
718175fa
JK
12143 {
12144 free (buf);
12145 return FALSE;
12146 }
12147
252b5132 12148 free (buf);
b34976b6 12149 return TRUE;
252b5132 12150}
98d8431c
JB
12151\f
12152/* Providing external access to the ELF program header table. */
12153
12154/* Return an upper bound on the number of bytes required to store a
12155 copy of ABFD's program header table entries. Return -1 if an error
12156 occurs; bfd_get_error will return an appropriate code. */
c044fabd 12157
98d8431c 12158long
217aa764 12159bfd_get_elf_phdr_upper_bound (bfd *abfd)
98d8431c
JB
12160{
12161 if (abfd->xvec->flavour != bfd_target_elf_flavour)
12162 {
12163 bfd_set_error (bfd_error_wrong_format);
12164 return -1;
12165 }
12166
936e320b 12167 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
98d8431c
JB
12168}
12169
98d8431c
JB
12170/* Copy ABFD's program header table entries to *PHDRS. The entries
12171 will be stored as an array of Elf_Internal_Phdr structures, as
12172 defined in include/elf/internal.h. To find out how large the
12173 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
12174
12175 Return the number of program header table entries read, or -1 if an
12176 error occurs; bfd_get_error will return an appropriate code. */
c044fabd 12177
98d8431c 12178int
217aa764 12179bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
98d8431c
JB
12180{
12181 int num_phdrs;
12182
12183 if (abfd->xvec->flavour != bfd_target_elf_flavour)
12184 {
12185 bfd_set_error (bfd_error_wrong_format);
12186 return -1;
12187 }
12188
12189 num_phdrs = elf_elfheader (abfd)->e_phnum;
01bcaf63
TT
12190 if (num_phdrs != 0)
12191 memcpy (phdrs, elf_tdata (abfd)->phdr,
12192 num_phdrs * sizeof (Elf_Internal_Phdr));
98d8431c
JB
12193
12194 return num_phdrs;
12195}
ae4221d7 12196
db6751f2 12197enum elf_reloc_type_class
7e612e98
AM
12198_bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12199 const asection *rel_sec ATTRIBUTE_UNUSED,
12200 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
db6751f2
JJ
12201{
12202 return reloc_class_normal;
12203}
f8df10f4 12204
47d9a591 12205/* For RELA architectures, return the relocation value for a
f8df10f4
JJ
12206 relocation against a local symbol. */
12207
12208bfd_vma
217aa764
AM
12209_bfd_elf_rela_local_sym (bfd *abfd,
12210 Elf_Internal_Sym *sym,
8517fae7 12211 asection **psec,
217aa764 12212 Elf_Internal_Rela *rel)
f8df10f4 12213{
8517fae7 12214 asection *sec = *psec;
f8df10f4
JJ
12215 bfd_vma relocation;
12216
6835821b
AM
12217 relocation = (sec->output_section->vma
12218 + sec->output_offset
12219 + sym->st_value);
f8df10f4 12220 if ((sec->flags & SEC_MERGE)
c629eae0 12221 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
dbaa2011 12222 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
f8df10f4 12223 {
f8df10f4 12224 rel->r_addend =
8517fae7 12225 _bfd_merged_section_offset (abfd, psec,
65765700 12226 elf_section_data (sec)->sec_info,
753731ee
AM
12227 sym->st_value + rel->r_addend);
12228 if (sec != *psec)
12229 {
12230 /* If we have changed the section, and our original section is
12231 marked with SEC_EXCLUDE, it means that the original
12232 SEC_MERGE section has been completely subsumed in some
12233 other SEC_MERGE section. In this case, we need to leave
12234 some info around for --emit-relocs. */
12235 if ((sec->flags & SEC_EXCLUDE) != 0)
12236 sec->kept_section = *psec;
12237 sec = *psec;
12238 }
8517fae7
AM
12239 rel->r_addend -= relocation;
12240 rel->r_addend += sec->output_section->vma + sec->output_offset;
f8df10f4
JJ
12241 }
12242 return relocation;
12243}
c629eae0
JJ
12244
12245bfd_vma
217aa764
AM
12246_bfd_elf_rel_local_sym (bfd *abfd,
12247 Elf_Internal_Sym *sym,
12248 asection **psec,
12249 bfd_vma addend)
47d9a591 12250{
c629eae0
JJ
12251 asection *sec = *psec;
12252
6835821b 12253 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
c629eae0
JJ
12254 return sym->st_value + addend;
12255
12256 return _bfd_merged_section_offset (abfd, psec,
65765700 12257 elf_section_data (sec)->sec_info,
753731ee 12258 sym->st_value + addend);
c629eae0
JJ
12259}
12260
37b01f6a
DG
12261/* Adjust an address within a section. Given OFFSET within SEC, return
12262 the new offset within the section, based upon changes made to the
12263 section. Returns -1 if the offset is now invalid.
12264 The offset (in abnd out) is in target sized bytes, however big a
12265 byte may be. */
12266
c629eae0 12267bfd_vma
217aa764 12268_bfd_elf_section_offset (bfd *abfd,
92e4ec35 12269 struct bfd_link_info *info,
217aa764
AM
12270 asection *sec,
12271 bfd_vma offset)
c629eae0 12272{
68bfbfcc 12273 switch (sec->sec_info_type)
65765700 12274 {
dbaa2011 12275 case SEC_INFO_TYPE_STABS:
eea6121a
AM
12276 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
12277 offset);
dbaa2011 12278 case SEC_INFO_TYPE_EH_FRAME:
92e4ec35 12279 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
37b01f6a 12280
65765700 12281 default:
310fd250
L
12282 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
12283 {
37b01f6a 12284 /* Reverse the offset. */
310fd250
L
12285 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12286 bfd_size_type address_size = bed->s->arch_size / 8;
37b01f6a
DG
12287
12288 /* address_size and sec->size are in octets. Convert
12289 to bytes before subtracting the original offset. */
61826503 12290 offset = ((sec->size - address_size)
bb294208 12291 / bfd_octets_per_byte (abfd, sec) - offset);
310fd250 12292 }
65765700
JJ
12293 return offset;
12294 }
c629eae0 12295}
3333a7c3
RM
12296\f
12297/* Create a new BFD as if by bfd_openr. Rather than opening a file,
12298 reconstruct an ELF file by reading the segments out of remote memory
12299 based on the ELF file header at EHDR_VMA and the ELF program headers it
12300 points to. If not null, *LOADBASEP is filled in with the difference
12301 between the VMAs from which the segments were read, and the VMAs the
12302 file headers (and hence BFD's idea of each section's VMA) put them at.
12303
12304 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12305 remote memory at target address VMA into the local buffer at MYADDR; it
12306 should return zero on success or an `errno' code on failure. TEMPL must
12307 be a BFD for an ELF target with the word size and byte order found in
12308 the remote memory. */
12309
12310bfd *
217aa764
AM
12311bfd_elf_bfd_from_remote_memory
12312 (bfd *templ,
12313 bfd_vma ehdr_vma,
f0a5d95a 12314 bfd_size_type size,
217aa764 12315 bfd_vma *loadbasep,
fe78531d 12316 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
3333a7c3
RM
12317{
12318 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
5979d6b6 12319 (templ, ehdr_vma, size, loadbasep, target_read_memory);
3333a7c3 12320}
4c45e5c9
JJ
12321\f
12322long
c9727e01
AM
12323_bfd_elf_get_synthetic_symtab (bfd *abfd,
12324 long symcount ATTRIBUTE_UNUSED,
12325 asymbol **syms ATTRIBUTE_UNUSED,
8615f3f2 12326 long dynsymcount,
c9727e01
AM
12327 asymbol **dynsyms,
12328 asymbol **ret)
4c45e5c9
JJ
12329{
12330 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12331 asection *relplt;
12332 asymbol *s;
12333 const char *relplt_name;
12334 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
12335 arelent *p;
12336 long count, i, n;
12337 size_t size;
12338 Elf_Internal_Shdr *hdr;
12339 char *names;
12340 asection *plt;
12341
8615f3f2
AM
12342 *ret = NULL;
12343
90e3cdf2
JJ
12344 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
12345 return 0;
12346
8615f3f2
AM
12347 if (dynsymcount <= 0)
12348 return 0;
12349
4c45e5c9
JJ
12350 if (!bed->plt_sym_val)
12351 return 0;
12352
12353 relplt_name = bed->relplt_name;
12354 if (relplt_name == NULL)
d35fd659 12355 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
4c45e5c9
JJ
12356 relplt = bfd_get_section_by_name (abfd, relplt_name);
12357 if (relplt == NULL)
12358 return 0;
12359
12360 hdr = &elf_section_data (relplt)->this_hdr;
12361 if (hdr->sh_link != elf_dynsymtab (abfd)
12362 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
12363 return 0;
12364
12365 plt = bfd_get_section_by_name (abfd, ".plt");
12366 if (plt == NULL)
12367 return 0;
12368
12369 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
c9727e01 12370 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
4c45e5c9
JJ
12371 return -1;
12372
eea6121a 12373 count = relplt->size / hdr->sh_entsize;
4c45e5c9
JJ
12374 size = count * sizeof (asymbol);
12375 p = relplt->relocation;
cb53bf42 12376 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
041de40d
AM
12377 {
12378 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
12379 if (p->addend != 0)
12380 {
12381#ifdef BFD64
12382 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
12383#else
12384 size += sizeof ("+0x") - 1 + 8;
12385#endif
12386 }
12387 }
4c45e5c9 12388
a50b1753 12389 s = *ret = (asymbol *) bfd_malloc (size);
4c45e5c9
JJ
12390 if (s == NULL)
12391 return -1;
12392
12393 names = (char *) (s + count);
12394 p = relplt->relocation;
12395 n = 0;
cb53bf42 12396 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
4c45e5c9
JJ
12397 {
12398 size_t len;
12399 bfd_vma addr;
12400
12401 addr = bed->plt_sym_val (i, plt, p);
12402 if (addr == (bfd_vma) -1)
12403 continue;
12404
12405 *s = **p->sym_ptr_ptr;
65a7a66f
AM
12406 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
12407 we are defining a symbol, ensure one of them is set. */
12408 if ((s->flags & BSF_LOCAL) == 0)
12409 s->flags |= BSF_GLOBAL;
6ba2a415 12410 s->flags |= BSF_SYNTHETIC;
4c45e5c9
JJ
12411 s->section = plt;
12412 s->value = addr - plt->vma;
12413 s->name = names;
8f39ba8e 12414 s->udata.p = NULL;
4c45e5c9
JJ
12415 len = strlen ((*p->sym_ptr_ptr)->name);
12416 memcpy (names, (*p->sym_ptr_ptr)->name, len);
12417 names += len;
041de40d
AM
12418 if (p->addend != 0)
12419 {
1d770845 12420 char buf[30], *a;
d324f6d6 12421
041de40d
AM
12422 memcpy (names, "+0x", sizeof ("+0x") - 1);
12423 names += sizeof ("+0x") - 1;
1d770845
L
12424 bfd_sprintf_vma (abfd, buf, p->addend);
12425 for (a = buf; *a == '0'; ++a)
12426 ;
12427 len = strlen (a);
12428 memcpy (names, a, len);
12429 names += len;
041de40d 12430 }
4c45e5c9
JJ
12431 memcpy (names, "@plt", sizeof ("@plt"));
12432 names += sizeof ("@plt");
8f39ba8e 12433 ++s, ++n;
4c45e5c9
JJ
12434 }
12435
12436 return n;
12437}
3d7f7666 12438
821e6ff6
AM
12439/* It is only used by x86-64 so far.
12440 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
7eacd66b
AM
12441 but current usage would allow all of _bfd_std_section to be zero. */
12442static const asymbol lcomm_sym
12443 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
3b22753a 12444asection _bfd_elf_large_com_section
7eacd66b 12445 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
821e6ff6 12446 "LARGE_COMMON", 0, SEC_IS_COMMON);
ecca9871 12447
cc364be6
AM
12448bfd_boolean
12449_bfd_elf_final_write_processing (bfd *abfd)
06f44071
AM
12450{
12451 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
d1036acb
L
12452
12453 i_ehdrp = elf_elfheader (abfd);
12454
06f44071
AM
12455 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12456 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
d8045f23 12457
df3a023b
AM
12458 /* Set the osabi field to ELFOSABI_GNU if the binary contains
12459 SHF_GNU_MBIND sections or symbols of STT_GNU_IFUNC type or
12460 STB_GNU_UNIQUE binding. */
cc364be6
AM
12461 if (elf_tdata (abfd)->has_gnu_osabi != 0)
12462 {
12463 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12464 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
12465 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
12466 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
12467 {
12468 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
12469 _bfd_error_handler (_("GNU_MBIND section is unsupported"));
12470 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
12471 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is unsupported"));
12472 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
12473 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is unsupported"));
9aea1e31 12474 bfd_set_error (bfd_error_sorry);
cc364be6
AM
12475 return FALSE;
12476 }
12477 }
12478 return TRUE;
d1036acb 12479}
fcb93ecf
PB
12480
12481
12482/* Return TRUE for ELF symbol types that represent functions.
12483 This is the default version of this function, which is sufficient for
d8045f23 12484 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
fcb93ecf
PB
12485
12486bfd_boolean
12487_bfd_elf_is_function_type (unsigned int type)
12488{
d8045f23
NC
12489 return (type == STT_FUNC
12490 || type == STT_GNU_IFUNC);
fcb93ecf 12491}
9f296da3 12492
aef36ac1
AM
12493/* If the ELF symbol SYM might be a function in SEC, return the
12494 function size and set *CODE_OFF to the function's entry point,
12495 otherwise return zero. */
9f296da3 12496
aef36ac1
AM
12497bfd_size_type
12498_bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
12499 bfd_vma *code_off)
9f296da3 12500{
aef36ac1
AM
12501 bfd_size_type size;
12502
ff9e0f5b 12503 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
aef36ac1
AM
12504 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
12505 || sym->section != sec)
12506 return 0;
ff9e0f5b 12507
ff9e0f5b 12508 *code_off = sym->value;
aef36ac1
AM
12509 size = 0;
12510 if (!(sym->flags & BSF_SYNTHETIC))
12511 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
12512 if (size == 0)
12513 size = 1;
12514 return size;
9f296da3 12515}
a8e14f4c
NC
12516
12517/* Set to non-zero to enable some debug messages. */
12518#define DEBUG_SECONDARY_RELOCS 0
12519
12520/* An internal-to-the-bfd-library only section type
12521 used to indicate a cached secondary reloc section. */
12522#define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
12523
12524/* Create a BFD section to hold a secondary reloc section. */
12525
12526bfd_boolean
12527_bfd_elf_init_secondary_reloc_section (bfd * abfd,
12528 Elf_Internal_Shdr *hdr,
12529 const char * name,
12530 unsigned int shindex)
12531{
12532 /* We only support RELA secondary relocs. */
12533 if (hdr->sh_type != SHT_RELA)
12534 return FALSE;
12535
12536#if DEBUG_SECONDARY_RELOCS
12537 fprintf (stderr, "secondary reloc section %s encountered\n", name);
12538#endif
12539 hdr->sh_type = SHT_SECONDARY_RELOC;
12540 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
12541}
12542
12543/* Read in any secondary relocs associated with SEC. */
12544
12545bfd_boolean
12546_bfd_elf_slurp_secondary_reloc_section (bfd * abfd,
12547 asection * sec,
12548 asymbol ** symbols)
12549{
12550 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
12551 asection * relsec;
12552 bfd_boolean result = TRUE;
12553 bfd_vma (*r_sym) (bfd_vma);
12554
12555#if BFD_DEFAULT_TARGET_SIZE > 32
12556 if (bfd_arch_bits_per_address (abfd) != 32)
12557 r_sym = elf64_r_sym;
12558 else
12559#endif
12560 r_sym = elf32_r_sym;
12561
12562 /* Discover if there are any secondary reloc sections
12563 associated with SEC. */
12564 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
12565 {
12566 Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
12567
12568 if (hdr->sh_type == SHT_SECONDARY_RELOC
8642dafa
AM
12569 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
12570 && (hdr->sh_entsize == ebd->s->sizeof_rel
12571 || hdr->sh_entsize == ebd->s->sizeof_rela))
a8e14f4c
NC
12572 {
12573 bfd_byte * native_relocs;
12574 bfd_byte * native_reloc;
12575 arelent * internal_relocs;
12576 arelent * internal_reloc;
12577 unsigned int i;
12578 unsigned int entsize;
12579 unsigned int symcount;
12580 unsigned int reloc_count;
12581 size_t amt;
12582
12583 if (ebd->elf_info_to_howto == NULL)
12584 return FALSE;
12585
12586#if DEBUG_SECONDARY_RELOCS
12587 fprintf (stderr, "read secondary relocs for %s from %s\n",
12588 sec->name, relsec->name);
12589#endif
12590 entsize = hdr->sh_entsize;
12591
12592 native_relocs = bfd_malloc (hdr->sh_size);
12593 if (native_relocs == NULL)
12594 {
12595 result = FALSE;
12596 continue;
12597 }
12598
12599 reloc_count = NUM_SHDR_ENTRIES (hdr);
12600 if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
12601 {
ecbbbdba 12602 free (native_relocs);
a8e14f4c
NC
12603 bfd_set_error (bfd_error_file_too_big);
12604 result = FALSE;
12605 continue;
12606 }
12607
12608 internal_relocs = (arelent *) bfd_alloc (abfd, amt);
12609 if (internal_relocs == NULL)
12610 {
12611 free (native_relocs);
12612 result = FALSE;
12613 continue;
12614 }
12615
12616 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
12617 || (bfd_bread (native_relocs, hdr->sh_size, abfd)
12618 != hdr->sh_size))
12619 {
12620 free (native_relocs);
ecbbbdba
NC
12621 /* The internal_relocs will be freed when
12622 the memory for the bfd is released. */
a8e14f4c
NC
12623 result = FALSE;
12624 continue;
12625 }
12626
12627 symcount = bfd_get_symcount (abfd);
12628
12629 for (i = 0, internal_reloc = internal_relocs,
12630 native_reloc = native_relocs;
12631 i < reloc_count;
12632 i++, internal_reloc++, native_reloc += entsize)
12633 {
12634 bfd_boolean res;
12635 Elf_Internal_Rela rela;
12636
8ee54925
NC
12637 if (entsize == ebd->s->sizeof_rel)
12638 ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
12639 else /* entsize == ebd->s->sizeof_rela */
12640 ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
a8e14f4c
NC
12641
12642 /* The address of an ELF reloc is section relative for an object
12643 file, and absolute for an executable file or shared library.
12644 The address of a normal BFD reloc is always section relative,
12645 and the address of a dynamic reloc is absolute.. */
12646 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
12647 internal_reloc->address = rela.r_offset;
12648 else
12649 internal_reloc->address = rela.r_offset - sec->vma;
12650
12651 if (r_sym (rela.r_info) == STN_UNDEF)
12652 {
12653 /* FIXME: This and the error case below mean that we
12654 have a symbol on relocs that is not elf_symbol_type. */
12655 internal_reloc->sym_ptr_ptr =
12656 bfd_abs_section_ptr->symbol_ptr_ptr;
12657 }
12658 else if (r_sym (rela.r_info) > symcount)
12659 {
12660 _bfd_error_handler
12661 /* xgettext:c-format */
12662 (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
12663 abfd, sec, i, (long) r_sym (rela.r_info));
12664 bfd_set_error (bfd_error_bad_value);
12665 internal_reloc->sym_ptr_ptr =
12666 bfd_abs_section_ptr->symbol_ptr_ptr;
12667 result = FALSE;
12668 }
12669 else
12670 {
12671 asymbol **ps;
12672
12673 ps = symbols + r_sym (rela.r_info) - 1;
12674
12675 internal_reloc->sym_ptr_ptr = ps;
12676 /* Make sure that this symbol is not removed by strip. */
12677 (*ps)->flags |= BSF_KEEP;
12678 }
12679
12680 internal_reloc->addend = rela.r_addend;
12681
12682 res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
12683 if (! res || internal_reloc->howto == NULL)
12684 {
12685#if DEBUG_SECONDARY_RELOCS
12686 fprintf (stderr, "there is no howto associated with reloc %lx\n",
12687 rela.r_info);
12688#endif
12689 result = FALSE;
12690 }
12691 }
12692
12693 free (native_relocs);
12694 /* Store the internal relocs. */
12695 elf_section_data (relsec)->sec_info = internal_relocs;
12696 }
12697 }
12698
12699 return result;
12700}
12701
12702/* Set the ELF section header fields of an output secondary reloc section. */
12703
12704bfd_boolean
12705_bfd_elf_copy_special_section_fields (const bfd * ibfd ATTRIBUTE_UNUSED,
12706 bfd * obfd ATTRIBUTE_UNUSED,
12707 const Elf_Internal_Shdr * isection,
12708 Elf_Internal_Shdr * osection)
12709{
12710 asection * isec;
12711 asection * osec;
44466e45 12712 struct bfd_elf_section_data * esd;
a8e14f4c
NC
12713
12714 if (isection == NULL)
12715 return FALSE;
12716
12717 if (isection->sh_type != SHT_SECONDARY_RELOC)
12718 return TRUE;
12719
12720 isec = isection->bfd_section;
12721 if (isec == NULL)
12722 return FALSE;
12723
12724 osec = osection->bfd_section;
12725 if (osec == NULL)
12726 return FALSE;
12727
44466e45
NC
12728 esd = elf_section_data (osec);
12729 BFD_ASSERT (esd->sec_info == NULL);
12730 esd->sec_info = elf_section_data (isec)->sec_info;
a8e14f4c
NC
12731 osection->sh_type = SHT_RELA;
12732 osection->sh_link = elf_onesymtab (obfd);
12733 if (osection->sh_link == 0)
12734 {
12735 /* There is no symbol table - we are hosed... */
12736 _bfd_error_handler
12737 /* xgettext:c-format */
12738 (_("%pB(%pA): link section cannot be set because the output file does not have a symbol table"),
12739 obfd, osec);
12740 bfd_set_error (bfd_error_bad_value);
12741 return FALSE;
12742 }
12743
12744 /* Find the output section that corresponds to the isection's sh_info link. */
327ef784
NC
12745 if (isection->sh_info == 0
12746 || isection->sh_info >= elf_numsections (ibfd))
12747 {
12748 _bfd_error_handler
12749 /* xgettext:c-format */
12750 (_("%pB(%pA): info section index is invalid"),
12751 obfd, osec);
12752 bfd_set_error (bfd_error_bad_value);
12753 return FALSE;
12754 }
12755
a8e14f4c
NC
12756 isection = elf_elfsections (ibfd)[isection->sh_info];
12757
327ef784
NC
12758 if (isection == NULL
12759 || isection->bfd_section == NULL
12760 || isection->bfd_section->output_section == NULL)
12761 {
12762 _bfd_error_handler
12763 /* xgettext:c-format */
12764 (_("%pB(%pA): info section index cannot be set because the section is not in the output"),
12765 obfd, osec);
12766 bfd_set_error (bfd_error_bad_value);
12767 return FALSE;
12768 }
12769
44466e45
NC
12770 esd = elf_section_data (isection->bfd_section->output_section);
12771 BFD_ASSERT (esd != NULL);
12772 osection->sh_info = esd->this_idx;
12773 esd->has_secondary_relocs = TRUE;
a8e14f4c
NC
12774#if DEBUG_SECONDARY_RELOCS
12775 fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
12776 osec->name, osection->sh_link, osection->sh_info);
44466e45
NC
12777 fprintf (stderr, "mark section %s as having secondary relocs\n",
12778 bfd_section_name (isection->bfd_section->output_section));
a8e14f4c
NC
12779#endif
12780
12781 return TRUE;
12782}
12783
44466e45
NC
12784/* Write out a secondary reloc section.
12785
12786 FIXME: Currently this function can result in a serious performance penalty
12787 for files with secondary relocs and lots of sections. The proper way to
12788 fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
12789 relocs together and then to have this function just walk that chain. */
a8e14f4c
NC
12790
12791bfd_boolean
12792_bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
12793{
12794 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
12795 bfd_vma addr_offset;
12796 asection * relsec;
12797 bfd_vma (*r_info) (bfd_vma, bfd_vma);
ac4bf06c
NC
12798 bfd_boolean result = TRUE;
12799
12800 if (sec == NULL)
12801 return FALSE;
a8e14f4c
NC
12802
12803#if BFD_DEFAULT_TARGET_SIZE > 32
12804 if (bfd_arch_bits_per_address (abfd) != 32)
12805 r_info = elf64_r_info;
12806 else
12807#endif
12808 r_info = elf32_r_info;
12809
a8e14f4c
NC
12810 /* The address of an ELF reloc is section relative for an object
12811 file, and absolute for an executable file or shared library.
12812 The address of a BFD reloc is always section relative. */
12813 addr_offset = 0;
12814 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
12815 addr_offset = sec->vma;
12816
12817 /* Discover if there are any secondary reloc sections
12818 associated with SEC. */
12819 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
12820 {
12821 const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
12822 Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
12823
12824 if (hdr->sh_type == SHT_RELA
12825 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
12826 {
12827 asymbol * last_sym;
12828 int last_sym_idx;
12829 unsigned int reloc_count;
12830 unsigned int idx;
8ee54925 12831 unsigned int entsize;
a8e14f4c
NC
12832 arelent * src_irel;
12833 bfd_byte * dst_rela;
12834
ac4bf06c
NC
12835 if (hdr->contents != NULL)
12836 {
12837 _bfd_error_handler
12838 /* xgettext:c-format */
12839 (_("%pB(%pA): error: secondary reloc section processed twice"),
12840 abfd, relsec);
12841 bfd_set_error (bfd_error_bad_value);
12842 result = FALSE;
12843 continue;
12844 }
a8e14f4c 12845
8ee54925
NC
12846 entsize = hdr->sh_entsize;
12847 if (entsize == 0)
ac267c75
NC
12848 {
12849 _bfd_error_handler
12850 /* xgettext:c-format */
12851 (_("%pB(%pA): error: secondary reloc section has zero sized entries"),
12852 abfd, relsec);
12853 bfd_set_error (bfd_error_bad_value);
12854 result = FALSE;
12855 continue;
12856 }
8ee54925
NC
12857 else if (entsize != ebd->s->sizeof_rel
12858 && entsize != ebd->s->sizeof_rela)
12859 {
12860 _bfd_error_handler
12861 /* xgettext:c-format */
12862 (_("%pB(%pA): error: secondary reloc section has non-standard sized entries"),
12863 abfd, relsec);
12864 bfd_set_error (bfd_error_bad_value);
12865 result = FALSE;
12866 continue;
12867 }
ac267c75 12868
8ee54925 12869 reloc_count = hdr->sh_size / entsize;
ac4bf06c
NC
12870 if (reloc_count <= 0)
12871 {
12872 _bfd_error_handler
12873 /* xgettext:c-format */
12874 (_("%pB(%pA): error: secondary reloc section is empty!"),
12875 abfd, relsec);
12876 bfd_set_error (bfd_error_bad_value);
12877 result = FALSE;
12878 continue;
12879 }
a8e14f4c
NC
12880
12881 hdr->contents = bfd_alloc (abfd, hdr->sh_size);
12882 if (hdr->contents == NULL)
12883 continue;
12884
12885#if DEBUG_SECONDARY_RELOCS
12886 fprintf (stderr, "write %u secondary relocs for %s from %s\n",
12887 reloc_count, sec->name, relsec->name);
12888#endif
12889 last_sym = NULL;
12890 last_sym_idx = 0;
12891 dst_rela = hdr->contents;
12892 src_irel = (arelent *) esd->sec_info;
ac4bf06c
NC
12893 if (src_irel == NULL)
12894 {
12895 _bfd_error_handler
12896 /* xgettext:c-format */
12897 (_("%pB(%pA): error: internal relocs missing for secondary reloc section"),
12898 abfd, relsec);
12899 bfd_set_error (bfd_error_bad_value);
12900 result = FALSE;
12901 continue;
12902 }
a8e14f4c 12903
8ee54925 12904 for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
a8e14f4c
NC
12905 {
12906 Elf_Internal_Rela src_rela;
12907 arelent *ptr;
12908 asymbol *sym;
12909 int n;
12910
12911 ptr = src_irel + idx;
ac4bf06c
NC
12912 if (ptr == NULL)
12913 {
12914 _bfd_error_handler
12915 /* xgettext:c-format */
12916 (_("%pB(%pA): error: reloc table entry %u is empty"),
12917 abfd, relsec, idx);
12918 bfd_set_error (bfd_error_bad_value);
12919 result = FALSE;
12920 break;
12921 }
a8e14f4c 12922
ac4bf06c
NC
12923 if (ptr->sym_ptr_ptr == NULL)
12924 {
12925 /* FIXME: Is this an error ? */
12926 n = 0;
12927 }
a8e14f4c
NC
12928 else
12929 {
ac4bf06c
NC
12930 sym = *ptr->sym_ptr_ptr;
12931
12932 if (sym == last_sym)
12933 n = last_sym_idx;
12934 else
a8e14f4c 12935 {
ac4bf06c
NC
12936 n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
12937 if (n < 0)
12938 {
12939 _bfd_error_handler
12940 /* xgettext:c-format */
12941 (_("%pB(%pA): error: secondary reloc %u references a missing symbol"),
12942 abfd, relsec, idx);
12943 bfd_set_error (bfd_error_bad_value);
12944 result = FALSE;
12945 n = 0;
12946 }
12947
12948 last_sym = sym;
12949 last_sym_idx = n;
a8e14f4c 12950 }
a8e14f4c 12951
ac4bf06c
NC
12952 if (sym->the_bfd != NULL
12953 && sym->the_bfd->xvec != abfd->xvec
12954 && ! _bfd_elf_validate_reloc (abfd, ptr))
12955 {
12956 _bfd_error_handler
12957 /* xgettext:c-format */
12958 (_("%pB(%pA): error: secondary reloc %u references a deleted symbol"),
12959 abfd, relsec, idx);
12960 bfd_set_error (bfd_error_bad_value);
12961 result = FALSE;
12962 n = 0;
12963 }
a8e14f4c
NC
12964 }
12965
ac4bf06c 12966 src_rela.r_offset = ptr->address + addr_offset;
a8e14f4c
NC
12967 if (ptr->howto == NULL)
12968 {
ac4bf06c
NC
12969 _bfd_error_handler
12970 /* xgettext:c-format */
12971 (_("%pB(%pA): error: secondary reloc %u is of an unknown type"),
12972 abfd, relsec, idx);
12973 bfd_set_error (bfd_error_bad_value);
12974 result = FALSE;
12975 src_rela.r_info = r_info (0, 0);
a8e14f4c 12976 }
ac4bf06c
NC
12977 else
12978 src_rela.r_info = r_info (n, ptr->howto->type);
a8e14f4c 12979 src_rela.r_addend = ptr->addend;
8ee54925
NC
12980
12981 if (entsize == ebd->s->sizeof_rel)
12982 ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
12983 else /* entsize == ebd->s->sizeof_rela */
12984 ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
a8e14f4c
NC
12985 }
12986 }
12987 }
12988
ac4bf06c 12989 return result;
a8e14f4c 12990}