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