asan: readelf: wild read in get_num_dynamic_syms
[deliverable/binutils-gdb.git] / bfd / elf.c
CommitLineData
252b5132 1/* ELF executable support for BFD.
340b6d91 2
b3adc24a 3 Copyright (C) 1993-2020 Free Software Foundation, Inc.
252b5132 4
5e8d7549 5 This file is part of BFD, the Binary File Descriptor library.
252b5132 6
5e8d7549
NC
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
cd123cb7 9 the Free Software Foundation; either version 3 of the License, or
5e8d7549 10 (at your option) any later version.
252b5132 11
5e8d7549
NC
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
252b5132 16
5e8d7549 17 You should have received a copy of the GNU General Public License
b34976b6 18 along with this program; if not, write to the Free Software
cd123cb7
NC
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21
252b5132 22
1b74d094
BW
23/*
24SECTION
252b5132
RH
25 ELF backends
26
27 BFD support for ELF formats is being worked on.
28 Currently, the best supported back ends are for sparc and i386
29 (running svr4 or Solaris 2).
30
31 Documentation of the internals of the support code still needs
32 to be written. The code is changing quickly enough that we
661a3fd4 33 haven't bothered yet. */
252b5132 34
7ee38065
MS
35/* For sparc64-cross-sparc32. */
36#define _SYSCALL32
252b5132 37#include "sysdep.h"
3a551c7a 38#include <limits.h>
3db64b00 39#include "bfd.h"
252b5132
RH
40#include "bfdlink.h"
41#include "libbfd.h"
42#define ARCH_SIZE 0
43#include "elf-bfd.h"
e0e8c97f 44#include "libiberty.h"
ff59fc36 45#include "safe-ctype.h"
de64ce13 46#include "elf-linux-core.h"
252b5132 47
8bc7f138
L
48#ifdef CORE_HEADER
49#include CORE_HEADER
50#endif
51
217aa764 52static int elf_sort_sections (const void *, const void *);
c84fca4d 53static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
ef10c3ac 54static bfd_boolean swap_out_syms (bfd *, struct elf_strtab_hash **, int) ;
718175fa 55static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
276da9b3 56 file_ptr offset, size_t align);
50b2bdb7 57
252b5132
RH
58/* Swap version information in and out. The version information is
59 currently size independent. If that ever changes, this code will
60 need to move into elfcode.h. */
61
62/* Swap in a Verdef structure. */
63
64void
217aa764
AM
65_bfd_elf_swap_verdef_in (bfd *abfd,
66 const Elf_External_Verdef *src,
67 Elf_Internal_Verdef *dst)
252b5132 68{
dc810e39
AM
69 dst->vd_version = H_GET_16 (abfd, src->vd_version);
70 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
71 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
72 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
73 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
74 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
75 dst->vd_next = H_GET_32 (abfd, src->vd_next);
252b5132
RH
76}
77
78/* Swap out a Verdef structure. */
79
80void
217aa764
AM
81_bfd_elf_swap_verdef_out (bfd *abfd,
82 const Elf_Internal_Verdef *src,
83 Elf_External_Verdef *dst)
252b5132 84{
dc810e39
AM
85 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
86 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
87 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
88 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
89 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
90 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
91 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
252b5132
RH
92}
93
94/* Swap in a Verdaux structure. */
95
96void
217aa764
AM
97_bfd_elf_swap_verdaux_in (bfd *abfd,
98 const Elf_External_Verdaux *src,
99 Elf_Internal_Verdaux *dst)
252b5132 100{
dc810e39
AM
101 dst->vda_name = H_GET_32 (abfd, src->vda_name);
102 dst->vda_next = H_GET_32 (abfd, src->vda_next);
252b5132
RH
103}
104
105/* Swap out a Verdaux structure. */
106
107void
217aa764
AM
108_bfd_elf_swap_verdaux_out (bfd *abfd,
109 const Elf_Internal_Verdaux *src,
110 Elf_External_Verdaux *dst)
252b5132 111{
dc810e39
AM
112 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
113 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
252b5132
RH
114}
115
116/* Swap in a Verneed structure. */
117
118void
217aa764
AM
119_bfd_elf_swap_verneed_in (bfd *abfd,
120 const Elf_External_Verneed *src,
121 Elf_Internal_Verneed *dst)
252b5132 122{
dc810e39
AM
123 dst->vn_version = H_GET_16 (abfd, src->vn_version);
124 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
125 dst->vn_file = H_GET_32 (abfd, src->vn_file);
126 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
127 dst->vn_next = H_GET_32 (abfd, src->vn_next);
252b5132
RH
128}
129
130/* Swap out a Verneed structure. */
131
132void
217aa764
AM
133_bfd_elf_swap_verneed_out (bfd *abfd,
134 const Elf_Internal_Verneed *src,
135 Elf_External_Verneed *dst)
252b5132 136{
dc810e39
AM
137 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
138 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
139 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
140 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
141 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
252b5132
RH
142}
143
144/* Swap in a Vernaux structure. */
145
146void
217aa764
AM
147_bfd_elf_swap_vernaux_in (bfd *abfd,
148 const Elf_External_Vernaux *src,
149 Elf_Internal_Vernaux *dst)
252b5132 150{
dc810e39
AM
151 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
152 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
153 dst->vna_other = H_GET_16 (abfd, src->vna_other);
154 dst->vna_name = H_GET_32 (abfd, src->vna_name);
155 dst->vna_next = H_GET_32 (abfd, src->vna_next);
252b5132
RH
156}
157
158/* Swap out a Vernaux structure. */
159
160void
217aa764
AM
161_bfd_elf_swap_vernaux_out (bfd *abfd,
162 const Elf_Internal_Vernaux *src,
163 Elf_External_Vernaux *dst)
252b5132 164{
dc810e39
AM
165 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
166 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
167 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
168 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
169 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
252b5132
RH
170}
171
172/* Swap in a Versym structure. */
173
174void
217aa764
AM
175_bfd_elf_swap_versym_in (bfd *abfd,
176 const Elf_External_Versym *src,
177 Elf_Internal_Versym *dst)
252b5132 178{
dc810e39 179 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
252b5132
RH
180}
181
182/* Swap out a Versym structure. */
183
184void
217aa764
AM
185_bfd_elf_swap_versym_out (bfd *abfd,
186 const Elf_Internal_Versym *src,
187 Elf_External_Versym *dst)
252b5132 188{
dc810e39 189 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
252b5132
RH
190}
191
192/* Standard ELF hash function. Do not change this function; you will
193 cause invalid hash tables to be generated. */
3a99b017 194
252b5132 195unsigned long
217aa764 196bfd_elf_hash (const char *namearg)
252b5132 197{
3a99b017 198 const unsigned char *name = (const unsigned char *) namearg;
252b5132
RH
199 unsigned long h = 0;
200 unsigned long g;
201 int ch;
202
203 while ((ch = *name++) != '\0')
204 {
205 h = (h << 4) + ch;
206 if ((g = (h & 0xf0000000)) != 0)
207 {
208 h ^= g >> 24;
209 /* The ELF ABI says `h &= ~g', but this is equivalent in
210 this case and on some machines one insn instead of two. */
211 h ^= g;
212 }
213 }
32dfa85d 214 return h & 0xffffffff;
252b5132
RH
215}
216
fdc90cb4
JJ
217/* DT_GNU_HASH hash function. Do not change this function; you will
218 cause invalid hash tables to be generated. */
219
220unsigned long
221bfd_elf_gnu_hash (const char *namearg)
222{
223 const unsigned char *name = (const unsigned char *) namearg;
224 unsigned long h = 5381;
225 unsigned char ch;
226
227 while ((ch = *name++) != '\0')
228 h = (h << 5) + h + ch;
229 return h & 0xffffffff;
230}
231
0c8d6e5c
AM
232/* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
233 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
b34976b6 234bfd_boolean
0c8d6e5c 235bfd_elf_allocate_object (bfd *abfd,
0ffa91dd 236 size_t object_size,
4dfe6ac6 237 enum elf_target_id object_id)
252b5132 238{
0ffa91dd
NC
239 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
240 abfd->tdata.any = bfd_zalloc (abfd, object_size);
241 if (abfd->tdata.any == NULL)
242 return FALSE;
252b5132 243
0ffa91dd 244 elf_object_id (abfd) = object_id;
c0355132
AM
245 if (abfd->direction != read_direction)
246 {
247 struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
248 if (o == NULL)
249 return FALSE;
250 elf_tdata (abfd)->o = o;
251 elf_program_header_size (abfd) = (bfd_size_type) -1;
252 }
b34976b6 253 return TRUE;
252b5132
RH
254}
255
0ffa91dd
NC
256
257bfd_boolean
ae95ffa6 258bfd_elf_make_object (bfd *abfd)
0ffa91dd 259{
ae95ffa6 260 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
0ffa91dd 261 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
ae95ffa6 262 bed->target_id);
0ffa91dd
NC
263}
264
b34976b6 265bfd_boolean
217aa764 266bfd_elf_mkcorefile (bfd *abfd)
252b5132 267{
c044fabd 268 /* I think this can be done just like an object file. */
228e534f
AM
269 if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
270 return FALSE;
271 elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
272 return elf_tdata (abfd)->core != NULL;
252b5132
RH
273}
274
6d5944fc 275char *
217aa764 276bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
252b5132
RH
277{
278 Elf_Internal_Shdr **i_shdrp;
f075ee0c 279 bfd_byte *shstrtab = NULL;
dc810e39
AM
280 file_ptr offset;
281 bfd_size_type shstrtabsize;
252b5132
RH
282
283 i_shdrp = elf_elfsections (abfd);
74f2e02b
AM
284 if (i_shdrp == 0
285 || shindex >= elf_numsections (abfd)
286 || i_shdrp[shindex] == 0)
f075ee0c 287 return NULL;
252b5132 288
f075ee0c 289 shstrtab = i_shdrp[shindex]->contents;
252b5132
RH
290 if (shstrtab == NULL)
291 {
c044fabd 292 /* No cached one, attempt to read, and cache what we read. */
252b5132
RH
293 offset = i_shdrp[shindex]->sh_offset;
294 shstrtabsize = i_shdrp[shindex]->sh_size;
c6c60d09
JJ
295
296 /* Allocate and clear an extra byte at the end, to prevent crashes
297 in case the string table is not terminated. */
3471d59d 298 if (shstrtabsize + 1 <= 1
06614111 299 || bfd_seek (abfd, offset, SEEK_SET) != 0
2bb3687b
AM
300 || (shstrtab = _bfd_alloc_and_read (abfd, shstrtabsize + 1,
301 shstrtabsize)) == NULL)
302 {
3471d59d
CC
303 /* Once we've failed to read it, make sure we don't keep
304 trying. Otherwise, we'll keep allocating space for
305 the string table over and over. */
306 i_shdrp[shindex]->sh_size = 0;
c6c60d09
JJ
307 }
308 else
309 shstrtab[shstrtabsize] = '\0';
217aa764 310 i_shdrp[shindex]->contents = shstrtab;
252b5132 311 }
f075ee0c 312 return (char *) shstrtab;
252b5132
RH
313}
314
315char *
217aa764
AM
316bfd_elf_string_from_elf_section (bfd *abfd,
317 unsigned int shindex,
318 unsigned int strindex)
252b5132
RH
319{
320 Elf_Internal_Shdr *hdr;
321
322 if (strindex == 0)
323 return "";
324
74f2e02b
AM
325 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
326 return NULL;
327
252b5132
RH
328 hdr = elf_elfsections (abfd)[shindex];
329
06614111
NC
330 if (hdr->contents == NULL)
331 {
332 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
333 {
334 /* PR 17512: file: f057ec89. */
695344c0 335 /* xgettext:c-format */
871b3ab2 336 _bfd_error_handler (_("%pB: attempt to load strings from"
63a5468a 337 " a non-string section (number %d)"),
06614111
NC
338 abfd, shindex);
339 return NULL;
340 }
b1fa9dd6 341
06614111
NC
342 if (bfd_elf_get_str_section (abfd, shindex) == NULL)
343 return NULL;
344 }
eed5def8
NC
345 else
346 {
347 /* PR 24273: The string section's contents may have already
348 been loaded elsewhere, eg because a corrupt file has the
349 string section index in the ELF header pointing at a group
350 section. So be paranoid, and test that the last byte of
351 the section is zero. */
352 if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
353 return NULL;
354 }
252b5132
RH
355
356 if (strindex >= hdr->sh_size)
357 {
1b3a8575 358 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
4eca0228 359 _bfd_error_handler
695344c0 360 /* xgettext:c-format */
2dcf00ce
AM
361 (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
362 abfd, strindex, (uint64_t) hdr->sh_size,
1b3a8575 363 (shindex == shstrndx && strindex == hdr->sh_name
252b5132 364 ? ".shstrtab"
1b3a8575 365 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
45b222d6 366 return NULL;
252b5132
RH
367 }
368
369 return ((char *) hdr->contents) + strindex;
370}
371
6cdc0ccc
AM
372/* Read and convert symbols to internal format.
373 SYMCOUNT specifies the number of symbols to read, starting from
374 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
375 are non-NULL, they are used to store the internal symbols, external
b7c368d0
NC
376 symbols, and symbol section index extensions, respectively.
377 Returns a pointer to the internal symbol buffer (malloced if necessary)
378 or NULL if there were no symbols or some kind of problem. */
6cdc0ccc
AM
379
380Elf_Internal_Sym *
217aa764
AM
381bfd_elf_get_elf_syms (bfd *ibfd,
382 Elf_Internal_Shdr *symtab_hdr,
383 size_t symcount,
384 size_t symoffset,
385 Elf_Internal_Sym *intsym_buf,
386 void *extsym_buf,
387 Elf_External_Sym_Shndx *extshndx_buf)
6cdc0ccc
AM
388{
389 Elf_Internal_Shdr *shndx_hdr;
217aa764 390 void *alloc_ext;
df622259 391 const bfd_byte *esym;
6cdc0ccc
AM
392 Elf_External_Sym_Shndx *alloc_extshndx;
393 Elf_External_Sym_Shndx *shndx;
4dd07732 394 Elf_Internal_Sym *alloc_intsym;
6cdc0ccc
AM
395 Elf_Internal_Sym *isym;
396 Elf_Internal_Sym *isymend;
9c5bfbb7 397 const struct elf_backend_data *bed;
6cdc0ccc 398 size_t extsym_size;
1f4361a7 399 size_t amt;
6cdc0ccc
AM
400 file_ptr pos;
401
e44a2c9c
AM
402 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
403 abort ();
404
6cdc0ccc
AM
405 if (symcount == 0)
406 return intsym_buf;
407
408 /* Normal syms might have section extension entries. */
409 shndx_hdr = NULL;
6a40cf0c
NC
410 if (elf_symtab_shndx_list (ibfd) != NULL)
411 {
412 elf_section_list * entry;
413 Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
414
415 /* Find an index section that is linked to this symtab section. */
416 for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
315350be
NC
417 {
418 /* PR 20063. */
419 if (entry->hdr.sh_link >= elf_numsections (ibfd))
420 continue;
421
422 if (sections[entry->hdr.sh_link] == symtab_hdr)
423 {
424 shndx_hdr = & entry->hdr;
425 break;
426 };
427 }
6a40cf0c
NC
428
429 if (shndx_hdr == NULL)
430 {
431 if (symtab_hdr == & elf_symtab_hdr (ibfd))
432 /* Not really accurate, but this was how the old code used to work. */
433 shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr;
434 /* Otherwise we do nothing. The assumption is that
435 the index table will not be needed. */
436 }
437 }
6cdc0ccc
AM
438
439 /* Read the symbols. */
440 alloc_ext = NULL;
441 alloc_extshndx = NULL;
4dd07732 442 alloc_intsym = NULL;
6cdc0ccc
AM
443 bed = get_elf_backend_data (ibfd);
444 extsym_size = bed->s->sizeof_sym;
1f4361a7
AM
445 if (_bfd_mul_overflow (symcount, extsym_size, &amt))
446 {
447 bfd_set_error (bfd_error_file_too_big);
448 intsym_buf = NULL;
449 goto out;
450 }
6cdc0ccc
AM
451 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
452 if (extsym_buf == NULL)
453 {
1f4361a7 454 alloc_ext = bfd_malloc (amt);
6cdc0ccc
AM
455 extsym_buf = alloc_ext;
456 }
457 if (extsym_buf == NULL
458 || bfd_seek (ibfd, pos, SEEK_SET) != 0
459 || bfd_bread (extsym_buf, amt, ibfd) != amt)
460 {
461 intsym_buf = NULL;
462 goto out;
463 }
464
465 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
466 extshndx_buf = NULL;
467 else
468 {
1f4361a7
AM
469 if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
470 {
471 bfd_set_error (bfd_error_file_too_big);
472 intsym_buf = NULL;
473 goto out;
474 }
6cdc0ccc
AM
475 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
476 if (extshndx_buf == NULL)
477 {
1f4361a7 478 alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
6cdc0ccc
AM
479 extshndx_buf = alloc_extshndx;
480 }
481 if (extshndx_buf == NULL
482 || bfd_seek (ibfd, pos, SEEK_SET) != 0
483 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
484 {
485 intsym_buf = NULL;
486 goto out;
487 }
488 }
489
490 if (intsym_buf == NULL)
491 {
1f4361a7
AM
492 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
493 {
494 bfd_set_error (bfd_error_file_too_big);
495 goto out;
496 }
497 alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
4dd07732 498 intsym_buf = alloc_intsym;
6cdc0ccc
AM
499 if (intsym_buf == NULL)
500 goto out;
501 }
502
503 /* Convert the symbols to internal form. */
504 isymend = intsym_buf + symcount;
a50b1753 505 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
07d6d2b8 506 shndx = extshndx_buf;
6cdc0ccc
AM
507 isym < isymend;
508 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
8384fb8f
AM
509 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
510 {
511 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
695344c0 512 /* xgettext:c-format */
871b3ab2 513 _bfd_error_handler (_("%pB symbol number %lu references"
63a5468a 514 " nonexistent SHT_SYMTAB_SHNDX section"),
4eca0228 515 ibfd, (unsigned long) symoffset);
4dd07732
AM
516 if (alloc_intsym != NULL)
517 free (alloc_intsym);
8384fb8f
AM
518 intsym_buf = NULL;
519 goto out;
520 }
6cdc0ccc
AM
521
522 out:
523 if (alloc_ext != NULL)
524 free (alloc_ext);
525 if (alloc_extshndx != NULL)
526 free (alloc_extshndx);
527
528 return intsym_buf;
529}
530
5cab59f6
AM
531/* Look up a symbol name. */
532const char *
be8dd2ca
AM
533bfd_elf_sym_name (bfd *abfd,
534 Elf_Internal_Shdr *symtab_hdr,
26c61ae5
L
535 Elf_Internal_Sym *isym,
536 asection *sym_sec)
5cab59f6 537{
26c61ae5 538 const char *name;
5cab59f6 539 unsigned int iname = isym->st_name;
be8dd2ca 540 unsigned int shindex = symtab_hdr->sh_link;
26c61ae5 541
138f35cc
JJ
542 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
543 /* Check for a bogus st_shndx to avoid crashing. */
4fbb74a6 544 && isym->st_shndx < elf_numsections (abfd))
5cab59f6
AM
545 {
546 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
547 shindex = elf_elfheader (abfd)->e_shstrndx;
548 }
549
26c61ae5
L
550 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
551 if (name == NULL)
552 name = "(null)";
553 else if (sym_sec && *name == '\0')
fd361982 554 name = bfd_section_name (sym_sec);
26c61ae5
L
555
556 return name;
5cab59f6
AM
557}
558
dbb410c3
AM
559/* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
560 sections. The first element is the flags, the rest are section
561 pointers. */
562
563typedef union elf_internal_group {
564 Elf_Internal_Shdr *shdr;
565 unsigned int flags;
566} Elf_Internal_Group;
567
b885599b
AM
568/* Return the name of the group signature symbol. Why isn't the
569 signature just a string? */
570
571static const char *
217aa764 572group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
b885599b 573{
9dce4196 574 Elf_Internal_Shdr *hdr;
9dce4196
AM
575 unsigned char esym[sizeof (Elf64_External_Sym)];
576 Elf_External_Sym_Shndx eshndx;
577 Elf_Internal_Sym isym;
b885599b 578
13792e9d
L
579 /* First we need to ensure the symbol table is available. Make sure
580 that it is a symbol table section. */
4fbb74a6
AM
581 if (ghdr->sh_link >= elf_numsections (abfd))
582 return NULL;
13792e9d
L
583 hdr = elf_elfsections (abfd) [ghdr->sh_link];
584 if (hdr->sh_type != SHT_SYMTAB
585 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
b885599b
AM
586 return NULL;
587
9dce4196
AM
588 /* Go read the symbol. */
589 hdr = &elf_tdata (abfd)->symtab_hdr;
6cdc0ccc
AM
590 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
591 &isym, esym, &eshndx) == NULL)
b885599b 592 return NULL;
9dce4196 593
26c61ae5 594 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
b885599b
AM
595}
596
dbb410c3
AM
597/* Set next_in_group list pointer, and group name for NEWSECT. */
598
b34976b6 599static bfd_boolean
217aa764 600setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
dbb410c3
AM
601{
602 unsigned int num_group = elf_tdata (abfd)->num_group;
603
604 /* If num_group is zero, read in all SHT_GROUP sections. The count
605 is set to -1 if there are no SHT_GROUP sections. */
606 if (num_group == 0)
607 {
608 unsigned int i, shnum;
609
610 /* First count the number of groups. If we have a SHT_GROUP
611 section with just a flag word (ie. sh_size is 4), ignore it. */
9ad5cbcf 612 shnum = elf_numsections (abfd);
dbb410c3 613 num_group = 0;
08a40648 614
44534af3 615#define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
1783205a 616 ( (shdr)->sh_type == SHT_GROUP \
44534af3 617 && (shdr)->sh_size >= minsize \
1783205a
NC
618 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
619 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
08a40648 620
dbb410c3
AM
621 for (i = 0; i < shnum; i++)
622 {
623 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
1783205a 624
44534af3 625 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
dbb410c3
AM
626 num_group += 1;
627 }
628
629 if (num_group == 0)
20dbb49d
L
630 {
631 num_group = (unsigned) -1;
632 elf_tdata (abfd)->num_group = num_group;
ce497010 633 elf_tdata (abfd)->group_sect_ptr = NULL;
20dbb49d
L
634 }
635 else
dbb410c3
AM
636 {
637 /* We keep a list of elf section headers for group sections,
638 so we can find them quickly. */
1f4361a7 639 size_t amt;
d0fb9a8d 640
20dbb49d 641 elf_tdata (abfd)->num_group = num_group;
1f4361a7
AM
642 amt = num_group * sizeof (Elf_Internal_Shdr *);
643 elf_tdata (abfd)->group_sect_ptr
644 = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
dbb410c3 645 if (elf_tdata (abfd)->group_sect_ptr == NULL)
b34976b6 646 return FALSE;
dbb410c3 647 num_group = 0;
ce497010 648
dbb410c3
AM
649 for (i = 0; i < shnum; i++)
650 {
651 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
1783205a 652
44534af3 653 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
dbb410c3 654 {
973ffd63 655 unsigned char *src;
dbb410c3
AM
656 Elf_Internal_Group *dest;
657
07d6d2b8
AM
658 /* Make sure the group section has a BFD section
659 attached to it. */
660 if (!bfd_section_from_shdr (abfd, i))
661 return FALSE;
662
dbb410c3
AM
663 /* Add to list of sections. */
664 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
665 num_group += 1;
666
667 /* Read the raw contents. */
1f4361a7
AM
668 BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0);
669 shdr->contents = NULL;
670 if (_bfd_mul_overflow (shdr->sh_size,
671 sizeof (*dest) / 4, &amt)
1f4361a7 672 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
2bb3687b
AM
673 || !(shdr->contents
674 = _bfd_alloc_and_read (abfd, amt, shdr->sh_size)))
493a3386
NC
675 {
676 _bfd_error_handler
695344c0 677 /* xgettext:c-format */
871b3ab2 678 (_("%pB: invalid size field in group section"
2dcf00ce
AM
679 " header: %#" PRIx64 ""),
680 abfd, (uint64_t) shdr->sh_size);
493a3386
NC
681 bfd_set_error (bfd_error_bad_value);
682 -- num_group;
493a3386
NC
683 continue;
684 }
708d7d0d 685
dbb410c3
AM
686 /* Translate raw contents, a flag word followed by an
687 array of elf section indices all in target byte order,
688 to the flag word followed by an array of elf section
689 pointers. */
690 src = shdr->contents + shdr->sh_size;
691 dest = (Elf_Internal_Group *) (shdr->contents + amt);
06614111 692
dbb410c3
AM
693 while (1)
694 {
695 unsigned int idx;
696
697 src -= 4;
698 --dest;
699 idx = H_GET_32 (abfd, src);
700 if (src == shdr->contents)
701 {
327301a4 702 dest->shdr = NULL;
dbb410c3 703 dest->flags = idx;
b885599b
AM
704 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
705 shdr->bfd_section->flags
706 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
dbb410c3
AM
707 break;
708 }
4bba0fb1 709 if (idx < shnum)
bae363f1
L
710 {
711 dest->shdr = elf_elfsections (abfd)[idx];
712 /* PR binutils/23199: All sections in a
713 section group should be marked with
714 SHF_GROUP. But some tools generate
715 broken objects without SHF_GROUP. Fix
716 them up here. */
717 dest->shdr->sh_flags |= SHF_GROUP;
718 }
4bba0fb1
AM
719 if (idx >= shnum
720 || dest->shdr->sh_type == SHT_GROUP)
dbb410c3 721 {
4eca0228 722 _bfd_error_handler
4bba0fb1
AM
723 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
724 abfd, i);
725 dest->shdr = NULL;
dbb410c3 726 }
dbb410c3
AM
727 }
728 }
729 }
493a3386
NC
730
731 /* PR 17510: Corrupt binaries might contain invalid groups. */
732 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
733 {
734 elf_tdata (abfd)->num_group = num_group;
735
736 /* If all groups are invalid then fail. */
737 if (num_group == 0)
738 {
739 elf_tdata (abfd)->group_sect_ptr = NULL;
740 elf_tdata (abfd)->num_group = num_group = -1;
4eca0228 741 _bfd_error_handler
871b3ab2 742 (_("%pB: no valid group sections found"), abfd);
493a3386
NC
743 bfd_set_error (bfd_error_bad_value);
744 }
745 }
dbb410c3
AM
746 }
747 }
748
749 if (num_group != (unsigned) -1)
750 {
564e11c9
JW
751 unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
752 unsigned int j;
dbb410c3 753
564e11c9 754 for (j = 0; j < num_group; j++)
dbb410c3 755 {
564e11c9
JW
756 /* Begin search from previous found group. */
757 unsigned i = (j + search_offset) % num_group;
758
dbb410c3 759 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
ce497010 760 Elf_Internal_Group *idx;
0c54f692 761 bfd_size_type n_elt;
ce497010
NC
762
763 if (shdr == NULL)
764 continue;
765
766 idx = (Elf_Internal_Group *) shdr->contents;
0c54f692
NC
767 if (idx == NULL || shdr->sh_size < 4)
768 {
769 /* See PR 21957 for a reproducer. */
770 /* xgettext:c-format */
871b3ab2 771 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
0c54f692
NC
772 abfd, shdr->bfd_section);
773 elf_tdata (abfd)->group_sect_ptr[i] = NULL;
774 bfd_set_error (bfd_error_bad_value);
775 return FALSE;
776 }
ce497010 777 n_elt = shdr->sh_size / 4;
dbb410c3
AM
778
779 /* Look through this group's sections to see if current
780 section is a member. */
781 while (--n_elt != 0)
782 if ((++idx)->shdr == hdr)
783 {
e0e8c97f 784 asection *s = NULL;
dbb410c3
AM
785
786 /* We are a member of this group. Go looking through
787 other members to see if any others are linked via
788 next_in_group. */
789 idx = (Elf_Internal_Group *) shdr->contents;
790 n_elt = shdr->sh_size / 4;
791 while (--n_elt != 0)
4bba0fb1
AM
792 if ((++idx)->shdr != NULL
793 && (s = idx->shdr->bfd_section) != NULL
945906ff 794 && elf_next_in_group (s) != NULL)
dbb410c3
AM
795 break;
796 if (n_elt != 0)
797 {
dbb410c3
AM
798 /* Snarf the group name from other member, and
799 insert current section in circular list. */
945906ff
AM
800 elf_group_name (newsect) = elf_group_name (s);
801 elf_next_in_group (newsect) = elf_next_in_group (s);
802 elf_next_in_group (s) = newsect;
dbb410c3
AM
803 }
804 else
805 {
dbb410c3
AM
806 const char *gname;
807
b885599b
AM
808 gname = group_signature (abfd, shdr);
809 if (gname == NULL)
b34976b6 810 return FALSE;
945906ff 811 elf_group_name (newsect) = gname;
dbb410c3
AM
812
813 /* Start a circular list with one element. */
945906ff 814 elf_next_in_group (newsect) = newsect;
dbb410c3 815 }
b885599b 816
9dce4196
AM
817 /* If the group section has been created, point to the
818 new member. */
dbb410c3 819 if (shdr->bfd_section != NULL)
945906ff 820 elf_next_in_group (shdr->bfd_section) = newsect;
b885599b 821
564e11c9
JW
822 elf_tdata (abfd)->group_search_offset = i;
823 j = num_group - 1;
dbb410c3
AM
824 break;
825 }
826 }
827 }
828
945906ff 829 if (elf_group_name (newsect) == NULL)
dbb410c3 830 {
695344c0 831 /* xgettext:c-format */
871b3ab2 832 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
4eca0228 833 abfd, newsect);
493a3386 834 return FALSE;
dbb410c3 835 }
b34976b6 836 return TRUE;
dbb410c3
AM
837}
838
3d7f7666 839bfd_boolean
dd863624 840_bfd_elf_setup_sections (bfd *abfd)
3d7f7666
L
841{
842 unsigned int i;
843 unsigned int num_group = elf_tdata (abfd)->num_group;
844 bfd_boolean result = TRUE;
dd863624
L
845 asection *s;
846
847 /* Process SHF_LINK_ORDER. */
848 for (s = abfd->sections; s != NULL; s = s->next)
849 {
850 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
851 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
852 {
853 unsigned int elfsec = this_hdr->sh_link;
854 /* FIXME: The old Intel compiler and old strip/objcopy may
855 not set the sh_link or sh_info fields. Hence we could
856 get the situation where elfsec is 0. */
857 if (elfsec == 0)
858 {
4fbb74a6 859 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
a859124d
AM
860 bed->link_order_error_handler
861 /* xgettext:c-format */
862 (_("%pB: warning: sh_link not set for section `%pA'"),
863 abfd, s);
dd863624
L
864 }
865 else
866 {
91d6fa6a 867 asection *linksec = NULL;
25bbc984 868
4fbb74a6
AM
869 if (elfsec < elf_numsections (abfd))
870 {
871 this_hdr = elf_elfsections (abfd)[elfsec];
91d6fa6a 872 linksec = this_hdr->bfd_section;
4fbb74a6 873 }
25bbc984
L
874
875 /* PR 1991, 2008:
876 Some strip/objcopy may leave an incorrect value in
877 sh_link. We don't want to proceed. */
91d6fa6a 878 if (linksec == NULL)
25bbc984 879 {
4eca0228 880 _bfd_error_handler
695344c0 881 /* xgettext:c-format */
871b3ab2 882 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
c08bb8dd 883 s->owner, elfsec, s);
25bbc984
L
884 result = FALSE;
885 }
886
91d6fa6a 887 elf_linked_to_section (s) = linksec;
dd863624
L
888 }
889 }
53720c49
AM
890 else if (this_hdr->sh_type == SHT_GROUP
891 && elf_next_in_group (s) == NULL)
892 {
4eca0228 893 _bfd_error_handler
695344c0 894 /* xgettext:c-format */
871b3ab2 895 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
53720c49
AM
896 abfd, elf_section_data (s)->this_idx);
897 result = FALSE;
898 }
dd863624 899 }
3d7f7666 900
dd863624 901 /* Process section groups. */
3d7f7666
L
902 if (num_group == (unsigned) -1)
903 return result;
904
905 for (i = 0; i < num_group; i++)
906 {
907 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
4b0e8a5f
NC
908 Elf_Internal_Group *idx;
909 unsigned int n_elt;
3d7f7666 910
4b0e8a5f
NC
911 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
912 if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
913 {
4eca0228 914 _bfd_error_handler
695344c0 915 /* xgettext:c-format */
871b3ab2 916 (_("%pB: section group entry number %u is corrupt"),
4b0e8a5f
NC
917 abfd, i);
918 result = FALSE;
919 continue;
920 }
921
922 idx = (Elf_Internal_Group *) shdr->contents;
923 n_elt = shdr->sh_size / 4;
1b786873 924
3d7f7666 925 while (--n_elt != 0)
24d3e51b
NC
926 {
927 ++ idx;
928
929 if (idx->shdr == NULL)
930 continue;
931 else if (idx->shdr->bfd_section)
932 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
db4677b8
AM
933 else if (idx->shdr->sh_type != SHT_RELA
934 && idx->shdr->sh_type != SHT_REL)
24d3e51b
NC
935 {
936 /* There are some unknown sections in the group. */
937 _bfd_error_handler
938 /* xgettext:c-format */
871b3ab2 939 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
24d3e51b
NC
940 abfd,
941 idx->shdr->sh_type,
942 bfd_elf_string_from_elf_section (abfd,
943 (elf_elfheader (abfd)
944 ->e_shstrndx),
945 idx->shdr->sh_name),
946 shdr->bfd_section);
947 result = FALSE;
948 }
949 }
3d7f7666 950 }
24d3e51b 951
3d7f7666
L
952 return result;
953}
954
72adc230
AM
955bfd_boolean
956bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
957{
958 return elf_next_in_group (sec) != NULL;
959}
960
cb7f4b29
AM
961const char *
962bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
963{
964 if (elf_sec_group (sec) != NULL)
965 return elf_group_name (sec);
966 return NULL;
967}
968
f6fe1ccd
L
969static char *
970convert_debug_to_zdebug (bfd *abfd, const char *name)
971{
972 unsigned int len = strlen (name);
973 char *new_name = bfd_alloc (abfd, len + 2);
974 if (new_name == NULL)
975 return NULL;
976 new_name[0] = '.';
977 new_name[1] = 'z';
978 memcpy (new_name + 2, name + 1, len);
979 return new_name;
980}
981
982static char *
983convert_zdebug_to_debug (bfd *abfd, const char *name)
984{
985 unsigned int len = strlen (name);
986 char *new_name = bfd_alloc (abfd, len);
987 if (new_name == NULL)
988 return NULL;
989 new_name[0] = '.';
990 memcpy (new_name + 1, name + 2, len - 1);
991 return new_name;
992}
993
cc5277b1
ML
994/* This a copy of lto_section defined in GCC (lto-streamer.h). */
995
996struct lto_section
997{
998 int16_t major_version;
999 int16_t minor_version;
1000 unsigned char slim_object;
1001
1002 /* Flags is a private field that is not defined publicly. */
1003 uint16_t flags;
1004};
1005
252b5132
RH
1006/* Make a BFD section from an ELF section. We store a pointer to the
1007 BFD section in the bfd_section field of the header. */
1008
b34976b6 1009bfd_boolean
217aa764
AM
1010_bfd_elf_make_section_from_shdr (bfd *abfd,
1011 Elf_Internal_Shdr *hdr,
6dc132d9
L
1012 const char *name,
1013 int shindex)
252b5132
RH
1014{
1015 asection *newsect;
1016 flagword flags;
9c5bfbb7 1017 const struct elf_backend_data *bed;
502794d4 1018 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132
RH
1019
1020 if (hdr->bfd_section != NULL)
4e011fb5 1021 return TRUE;
252b5132
RH
1022
1023 newsect = bfd_make_section_anyway (abfd, name);
1024 if (newsect == NULL)
b34976b6 1025 return FALSE;
252b5132 1026
1829f4b2
AM
1027 hdr->bfd_section = newsect;
1028 elf_section_data (newsect)->this_hdr = *hdr;
6dc132d9 1029 elf_section_data (newsect)->this_idx = shindex;
1829f4b2 1030
2f89ff8d
L
1031 /* Always use the real type/flags. */
1032 elf_section_type (newsect) = hdr->sh_type;
1033 elf_section_flags (newsect) = hdr->sh_flags;
1034
252b5132
RH
1035 newsect->filepos = hdr->sh_offset;
1036
252b5132
RH
1037 flags = SEC_NO_FLAGS;
1038 if (hdr->sh_type != SHT_NOBITS)
1039 flags |= SEC_HAS_CONTENTS;
dbb410c3 1040 if (hdr->sh_type == SHT_GROUP)
7bdf4127 1041 flags |= SEC_GROUP;
252b5132
RH
1042 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1043 {
1044 flags |= SEC_ALLOC;
1045 if (hdr->sh_type != SHT_NOBITS)
1046 flags |= SEC_LOAD;
1047 }
1048 if ((hdr->sh_flags & SHF_WRITE) == 0)
1049 flags |= SEC_READONLY;
1050 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1051 flags |= SEC_CODE;
1052 else if ((flags & SEC_LOAD) != 0)
1053 flags |= SEC_DATA;
f5fa8ca2
JJ
1054 if ((hdr->sh_flags & SHF_MERGE) != 0)
1055 {
1056 flags |= SEC_MERGE;
1057 newsect->entsize = hdr->sh_entsize;
f5fa8ca2 1058 }
84865015
NC
1059 if ((hdr->sh_flags & SHF_STRINGS) != 0)
1060 flags |= SEC_STRINGS;
dbb410c3
AM
1061 if (hdr->sh_flags & SHF_GROUP)
1062 if (!setup_group (abfd, hdr, newsect))
b34976b6 1063 return FALSE;
13ae64f3
JJ
1064 if ((hdr->sh_flags & SHF_TLS) != 0)
1065 flags |= SEC_THREAD_LOCAL;
18ae9cc1
L
1066 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1067 flags |= SEC_EXCLUDE;
252b5132 1068
df3a023b
AM
1069 switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
1070 {
1071 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1072 but binutils as of 2019-07-23 did not set the EI_OSABI header
1073 byte. */
1074 case ELFOSABI_NONE:
1075 case ELFOSABI_GNU:
1076 case ELFOSABI_FREEBSD:
1077 if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
1078 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1079 break;
1080 }
1081
3d2b39cf 1082 if ((flags & SEC_ALLOC) == 0)
7a6cc5fb 1083 {
3d2b39cf
L
1084 /* The debugging sections appear to be recognized only by name,
1085 not any sort of flag. Their SEC_ALLOC bits are cleared. */
3d2b39cf
L
1086 if (name [0] == '.')
1087 {
bb294208
AM
1088 if (strncmp (name, ".debug", 6) == 0
1089 || strncmp (name, ".gnu.linkonce.wi.", 17) == 0
1090 || strncmp (name, ".zdebug", 7) == 0)
1091 flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
1092 else if (strncmp (name, GNU_BUILD_ATTRS_SECTION_NAME, 21) == 0
1093 || strncmp (name, ".note.gnu", 9) == 0)
502794d4
CE
1094 {
1095 flags |= SEC_ELF_OCTETS;
1096 opb = 1;
1097 }
bb294208
AM
1098 else if (strncmp (name, ".line", 5) == 0
1099 || strncmp (name, ".stab", 5) == 0
1100 || strcmp (name, ".gdb_index") == 0)
3d2b39cf
L
1101 flags |= SEC_DEBUGGING;
1102 }
1103 }
252b5132 1104
502794d4
CE
1105 if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
1106 || !bfd_set_section_size (newsect, hdr->sh_size)
1107 || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign)))
1108 return FALSE;
1109
252b5132
RH
1110 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1111 only link a single copy of the section. This is used to support
1112 g++. g++ will emit each template expansion in its own section.
1113 The symbols will be defined as weak, so that multiple definitions
1114 are permitted. The GNU linker extension is to actually discard
1115 all but one of the sections. */
0112cd26 1116 if (CONST_STRNEQ (name, ".gnu.linkonce")
b885599b 1117 && elf_next_in_group (newsect) == NULL)
252b5132
RH
1118 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1119
8c803a2d
AM
1120 if (!bfd_set_section_flags (newsect, flags))
1121 return FALSE;
1122
fa152c49
JW
1123 bed = get_elf_backend_data (abfd);
1124 if (bed->elf_backend_section_flags)
8c803a2d 1125 if (!bed->elf_backend_section_flags (hdr))
b34976b6 1126 return FALSE;
fa152c49 1127
718175fa
JK
1128 /* We do not parse the PT_NOTE segments as we are interested even in the
1129 separate debug info files which may have the segments offsets corrupted.
1130 PT_NOTEs from the core files are currently not parsed using BFD. */
1131 if (hdr->sh_type == SHT_NOTE)
1132 {
baea7ef1 1133 bfd_byte *contents;
718175fa 1134
baea7ef1 1135 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
718175fa
JK
1136 return FALSE;
1137
276da9b3
L
1138 elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1139 hdr->sh_offset, hdr->sh_addralign);
718175fa
JK
1140 free (contents);
1141 }
1142
8c803a2d 1143 if ((newsect->flags & SEC_ALLOC) != 0)
252b5132
RH
1144 {
1145 Elf_Internal_Phdr *phdr;
6ffd7900
AM
1146 unsigned int i, nload;
1147
1148 /* Some ELF linkers produce binaries with all the program header
1149 p_paddr fields zero. If we have such a binary with more than
1150 one PT_LOAD header, then leave the section lma equal to vma
1151 so that we don't create sections with overlapping lma. */
1152 phdr = elf_tdata (abfd)->phdr;
1153 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1154 if (phdr->p_paddr != 0)
1155 break;
1156 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1157 ++nload;
1158 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1159 return TRUE;
252b5132 1160
252b5132
RH
1161 phdr = elf_tdata (abfd)->phdr;
1162 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1163 {
86b2281f
AM
1164 if (((phdr->p_type == PT_LOAD
1165 && (hdr->sh_flags & SHF_TLS) == 0)
1166 || phdr->p_type == PT_TLS)
9a83a553 1167 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
252b5132 1168 {
8c803a2d 1169 if ((newsect->flags & SEC_LOAD) == 0)
88967714 1170 newsect->lma = (phdr->p_paddr
502794d4 1171 + hdr->sh_addr - phdr->p_vaddr) / opb;
88967714
AM
1172 else
1173 /* We used to use the same adjustment for SEC_LOAD
1174 sections, but that doesn't work if the segment
1175 is packed with code from multiple VMAs.
1176 Instead we calculate the section LMA based on
1177 the segment LMA. It is assumed that the
1178 segment will contain sections with contiguous
1179 LMAs, even if the VMAs are not. */
1180 newsect->lma = (phdr->p_paddr
502794d4 1181 + hdr->sh_offset - phdr->p_offset) / opb;
88967714
AM
1182
1183 /* With contiguous segments, we can't tell from file
1184 offsets whether a section with zero size should
1185 be placed at the end of one segment or the
1186 beginning of the next. Decide based on vaddr. */
1187 if (hdr->sh_addr >= phdr->p_vaddr
1188 && (hdr->sh_addr + hdr->sh_size
1189 <= phdr->p_vaddr + phdr->p_memsz))
1190 break;
252b5132
RH
1191 }
1192 }
1193 }
1194
4a114e3e
L
1195 /* Compress/decompress DWARF debug sections with names: .debug_* and
1196 .zdebug_*, after the section flags is set. */
8c803a2d 1197 if ((newsect->flags & SEC_DEBUGGING)
4a114e3e
L
1198 && ((name[1] == 'd' && name[6] == '_')
1199 || (name[1] == 'z' && name[7] == '_')))
1200 {
1201 enum { nothing, compress, decompress } action = nothing;
151411f8 1202 int compression_header_size;
dab394de 1203 bfd_size_type uncompressed_size;
4207142d 1204 unsigned int uncompressed_align_power;
151411f8
L
1205 bfd_boolean compressed
1206 = bfd_is_section_compressed_with_header (abfd, newsect,
dab394de 1207 &compression_header_size,
4207142d
MW
1208 &uncompressed_size,
1209 &uncompressed_align_power);
151411f8 1210 if (compressed)
4a114e3e
L
1211 {
1212 /* Compressed section. Check if we should decompress. */
1213 if ((abfd->flags & BFD_DECOMPRESS))
1214 action = decompress;
1215 }
151411f8
L
1216
1217 /* Compress the uncompressed section or convert from/to .zdebug*
1218 section. Check if we should compress. */
1219 if (action == nothing)
4a114e3e 1220 {
151411f8
L
1221 if (newsect->size != 0
1222 && (abfd->flags & BFD_COMPRESS)
1223 && compression_header_size >= 0
dab394de 1224 && uncompressed_size > 0
151411f8
L
1225 && (!compressed
1226 || ((compression_header_size > 0)
1227 != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
4a114e3e 1228 action = compress;
151411f8
L
1229 else
1230 return TRUE;
4a114e3e
L
1231 }
1232
151411f8 1233 if (action == compress)
4a114e3e 1234 {
4a114e3e
L
1235 if (!bfd_init_section_compress_status (abfd, newsect))
1236 {
4eca0228 1237 _bfd_error_handler
695344c0 1238 /* xgettext:c-format */
871b3ab2 1239 (_("%pB: unable to initialize compress status for section %s"),
4a114e3e
L
1240 abfd, name);
1241 return FALSE;
1242 }
151411f8
L
1243 }
1244 else
1245 {
4a114e3e
L
1246 if (!bfd_init_section_decompress_status (abfd, newsect))
1247 {
4eca0228 1248 _bfd_error_handler
695344c0 1249 /* xgettext:c-format */
871b3ab2 1250 (_("%pB: unable to initialize decompress status for section %s"),
4a114e3e
L
1251 abfd, name);
1252 return FALSE;
1253 }
151411f8
L
1254 }
1255
f6fe1ccd 1256 if (abfd->is_linker_input)
151411f8 1257 {
f6fe1ccd
L
1258 if (name[1] == 'z'
1259 && (action == decompress
1260 || (action == compress
1261 && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
4e011fb5 1262 {
f6fe1ccd
L
1263 /* Convert section name from .zdebug_* to .debug_* so
1264 that linker will consider this section as a debug
1265 section. */
1266 char *new_name = convert_zdebug_to_debug (abfd, name);
151411f8
L
1267 if (new_name == NULL)
1268 return FALSE;
fd361982 1269 bfd_rename_section (newsect, new_name);
151411f8 1270 }
4a114e3e 1271 }
f6fe1ccd
L
1272 else
1273 /* For objdump, don't rename the section. For objcopy, delay
1274 section rename to elf_fake_sections. */
1275 newsect->flags |= SEC_ELF_RENAME;
4a114e3e
L
1276 }
1277
cc5277b1
ML
1278 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1279 section. */
1280 const char *lto_section_name = ".gnu.lto_.lto.";
1281 if (strncmp (name, lto_section_name, strlen (lto_section_name)) == 0)
1282 {
1283 struct lto_section lsection;
1284 if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
1285 sizeof (struct lto_section)))
1286 abfd->lto_slim_object = lsection.slim_object;
1287 }
1288
b34976b6 1289 return TRUE;
252b5132
RH
1290}
1291
84865015
NC
1292const char *const bfd_elf_section_type_names[] =
1293{
252b5132
RH
1294 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1295 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1296 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1297};
1298
1049f94e 1299/* ELF relocs are against symbols. If we are producing relocatable
252b5132
RH
1300 output, and the reloc is against an external symbol, and nothing
1301 has given us any additional addend, the resulting reloc will also
1302 be against the same symbol. In such a case, we don't want to
1303 change anything about the way the reloc is handled, since it will
1304 all be done at final link time. Rather than put special case code
1305 into bfd_perform_relocation, all the reloc types use this howto
1306 function. It just short circuits the reloc if producing
1049f94e 1307 relocatable output against an external symbol. */
252b5132 1308
252b5132 1309bfd_reloc_status_type
217aa764
AM
1310bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1311 arelent *reloc_entry,
1312 asymbol *symbol,
1313 void *data ATTRIBUTE_UNUSED,
1314 asection *input_section,
1315 bfd *output_bfd,
1316 char **error_message ATTRIBUTE_UNUSED)
1317{
1318 if (output_bfd != NULL
252b5132
RH
1319 && (symbol->flags & BSF_SECTION_SYM) == 0
1320 && (! reloc_entry->howto->partial_inplace
1321 || reloc_entry->addend == 0))
1322 {
1323 reloc_entry->address += input_section->output_offset;
1324 return bfd_reloc_ok;
1325 }
1326
1327 return bfd_reloc_continue;
1328}
1329\f
84865015
NC
1330/* Returns TRUE if section A matches section B.
1331 Names, addresses and links may be different, but everything else
1332 should be the same. */
1333
1334static bfd_boolean
5522f910
NC
1335section_match (const Elf_Internal_Shdr * a,
1336 const Elf_Internal_Shdr * b)
84865015 1337{
ac85e67c
AM
1338 if (a->sh_type != b->sh_type
1339 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1340 || a->sh_addralign != b->sh_addralign
1341 || a->sh_entsize != b->sh_entsize)
1342 return FALSE;
1343 if (a->sh_type == SHT_SYMTAB
1344 || a->sh_type == SHT_STRTAB)
1345 return TRUE;
1346 return a->sh_size == b->sh_size;
84865015
NC
1347}
1348
1349/* Find a section in OBFD that has the same characteristics
1350 as IHEADER. Return the index of this section or SHN_UNDEF if
1351 none can be found. Check's section HINT first, as this is likely
1352 to be the correct section. */
1353
1354static unsigned int
5cc4ca83
ST
1355find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1356 const unsigned int hint)
84865015
NC
1357{
1358 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1359 unsigned int i;
1360
a55c9876
NC
1361 BFD_ASSERT (iheader != NULL);
1362
1363 /* See PR 20922 for a reproducer of the NULL test. */
5cc4ca83
ST
1364 if (hint < elf_numsections (obfd)
1365 && oheaders[hint] != NULL
a55c9876 1366 && section_match (oheaders[hint], iheader))
84865015
NC
1367 return hint;
1368
1369 for (i = 1; i < elf_numsections (obfd); i++)
1370 {
1371 Elf_Internal_Shdr * oheader = oheaders[i];
1372
a55c9876
NC
1373 if (oheader == NULL)
1374 continue;
84865015
NC
1375 if (section_match (oheader, iheader))
1376 /* FIXME: Do we care if there is a potential for
1377 multiple matches ? */
1378 return i;
1379 }
1380
1381 return SHN_UNDEF;
1382}
1383
5522f910
NC
1384/* PR 19938: Attempt to set the ELF section header fields of an OS or
1385 Processor specific section, based upon a matching input section.
1386 Returns TRUE upon success, FALSE otherwise. */
07d6d2b8 1387
5522f910
NC
1388static bfd_boolean
1389copy_special_section_fields (const bfd *ibfd,
1390 bfd *obfd,
1391 const Elf_Internal_Shdr *iheader,
1392 Elf_Internal_Shdr *oheader,
1393 const unsigned int secnum)
1394{
1395 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1396 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1397 bfd_boolean changed = FALSE;
1398 unsigned int sh_link;
1399
1400 if (oheader->sh_type == SHT_NOBITS)
1401 {
1402 /* This is a feature for objcopy --only-keep-debug:
1403 When a section's type is changed to NOBITS, we preserve
1404 the sh_link and sh_info fields so that they can be
1405 matched up with the original.
1406
1407 Note: Strictly speaking these assignments are wrong.
1408 The sh_link and sh_info fields should point to the
1409 relevent sections in the output BFD, which may not be in
1410 the same location as they were in the input BFD. But
1411 the whole point of this action is to preserve the
1412 original values of the sh_link and sh_info fields, so
1413 that they can be matched up with the section headers in
1414 the original file. So strictly speaking we may be
1415 creating an invalid ELF file, but it is only for a file
1416 that just contains debug info and only for sections
1417 without any contents. */
1418 if (oheader->sh_link == 0)
1419 oheader->sh_link = iheader->sh_link;
1420 if (oheader->sh_info == 0)
1421 oheader->sh_info = iheader->sh_info;
1422 return TRUE;
1423 }
1424
1425 /* Allow the target a chance to decide how these fields should be set. */
a859124d
AM
1426 if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1427 iheader, oheader))
5522f910
NC
1428 return TRUE;
1429
1430 /* We have an iheader which might match oheader, and which has non-zero
1431 sh_info and/or sh_link fields. Attempt to follow those links and find
1432 the section in the output bfd which corresponds to the linked section
1433 in the input bfd. */
1434 if (iheader->sh_link != SHN_UNDEF)
1435 {
4f3ca05b
NC
1436 /* See PR 20931 for a reproducer. */
1437 if (iheader->sh_link >= elf_numsections (ibfd))
1438 {
76cfced5 1439 _bfd_error_handler
4f3ca05b 1440 /* xgettext:c-format */
9793eb77 1441 (_("%pB: invalid sh_link field (%d) in section number %d"),
4f3ca05b
NC
1442 ibfd, iheader->sh_link, secnum);
1443 return FALSE;
1444 }
1445
5522f910
NC
1446 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1447 if (sh_link != SHN_UNDEF)
1448 {
1449 oheader->sh_link = sh_link;
1450 changed = TRUE;
1451 }
1452 else
1453 /* FIXME: Should we install iheader->sh_link
1454 if we could not find a match ? */
76cfced5 1455 _bfd_error_handler
695344c0 1456 /* xgettext:c-format */
9793eb77 1457 (_("%pB: failed to find link section for section %d"), obfd, secnum);
5522f910
NC
1458 }
1459
1460 if (iheader->sh_info)
1461 {
1462 /* The sh_info field can hold arbitrary information, but if the
1463 SHF_LINK_INFO flag is set then it should be interpreted as a
1464 section index. */
1465 if (iheader->sh_flags & SHF_INFO_LINK)
1466 {
1467 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1468 iheader->sh_info);
1469 if (sh_link != SHN_UNDEF)
1470 oheader->sh_flags |= SHF_INFO_LINK;
1471 }
1472 else
1473 /* No idea what it means - just copy it. */
1474 sh_link = iheader->sh_info;
1475
1476 if (sh_link != SHN_UNDEF)
1477 {
1478 oheader->sh_info = sh_link;
1479 changed = TRUE;
1480 }
1481 else
76cfced5 1482 _bfd_error_handler
695344c0 1483 /* xgettext:c-format */
9793eb77 1484 (_("%pB: failed to find info section for section %d"), obfd, secnum);
5522f910
NC
1485 }
1486
1487 return changed;
1488}
07d6d2b8 1489
0ac4564e
L
1490/* Copy the program header and other data from one object module to
1491 another. */
252b5132 1492
b34976b6 1493bfd_boolean
217aa764 1494_bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
2d502050 1495{
5522f910
NC
1496 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1497 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1498 const struct elf_backend_data *bed;
84865015
NC
1499 unsigned int i;
1500
2d502050 1501 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
84865015 1502 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 1503 return TRUE;
2d502050 1504
57b828ef
L
1505 if (!elf_flags_init (obfd))
1506 {
1507 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1508 elf_flags_init (obfd) = TRUE;
1509 }
2d502050 1510
0ac4564e 1511 elf_gp (obfd) = elf_gp (ibfd);
57b828ef
L
1512
1513 /* Also copy the EI_OSABI field. */
1514 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1515 elf_elfheader (ibfd)->e_ident[EI_OSABI];
104d59d1 1516
5522f910
NC
1517 /* If set, copy the EI_ABIVERSION field. */
1518 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1519 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1520 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
07d6d2b8 1521
104d59d1
JM
1522 /* Copy object attributes. */
1523 _bfd_elf_copy_obj_attributes (ibfd, obfd);
63b9bbb7 1524
84865015
NC
1525 if (iheaders == NULL || oheaders == NULL)
1526 return TRUE;
63b9bbb7 1527
5522f910
NC
1528 bed = get_elf_backend_data (obfd);
1529
1530 /* Possibly copy other fields in the section header. */
84865015 1531 for (i = 1; i < elf_numsections (obfd); i++)
63b9bbb7 1532 {
84865015
NC
1533 unsigned int j;
1534 Elf_Internal_Shdr * oheader = oheaders[i];
63b9bbb7 1535
5522f910
NC
1536 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1537 because of a special case need for generating separate debug info
1538 files. See below for more details. */
84865015
NC
1539 if (oheader == NULL
1540 || (oheader->sh_type != SHT_NOBITS
5522f910
NC
1541 && oheader->sh_type < SHT_LOOS))
1542 continue;
1543
1544 /* Ignore empty sections, and sections whose
1545 fields have already been initialised. */
1546 if (oheader->sh_size == 0
84865015
NC
1547 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1548 continue;
63b9bbb7 1549
84865015 1550 /* Scan for the matching section in the input bfd.
5522f910
NC
1551 First we try for a direct mapping between the input and output sections. */
1552 for (j = 1; j < elf_numsections (ibfd); j++)
1553 {
1554 const Elf_Internal_Shdr * iheader = iheaders[j];
1555
1556 if (iheader == NULL)
1557 continue;
1558
1559 if (oheader->bfd_section != NULL
1560 && iheader->bfd_section != NULL
1561 && iheader->bfd_section->output_section != NULL
1562 && iheader->bfd_section->output_section == oheader->bfd_section)
1563 {
1564 /* We have found a connection from the input section to the
1565 output section. Attempt to copy the header fields. If
1566 this fails then do not try any further sections - there
1567 should only be a one-to-one mapping between input and output. */
1568 if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1569 j = elf_numsections (ibfd);
1570 break;
1571 }
1572 }
1573
1574 if (j < elf_numsections (ibfd))
1575 continue;
1576
1577 /* That failed. So try to deduce the corresponding input section.
84865015
NC
1578 Unfortunately we cannot compare names as the output string table
1579 is empty, so instead we check size, address and type. */
1580 for (j = 1; j < elf_numsections (ibfd); j++)
1581 {
5522f910 1582 const Elf_Internal_Shdr * iheader = iheaders[j];
84865015 1583
5522f910
NC
1584 if (iheader == NULL)
1585 continue;
1586
1587 /* Try matching fields in the input section's header.
1588 Since --only-keep-debug turns all non-debug sections into
84865015
NC
1589 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1590 input type. */
1591 if ((oheader->sh_type == SHT_NOBITS
1592 || iheader->sh_type == oheader->sh_type)
5522f910
NC
1593 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1594 == (oheader->sh_flags & ~ SHF_INFO_LINK)
84865015
NC
1595 && iheader->sh_addralign == oheader->sh_addralign
1596 && iheader->sh_entsize == oheader->sh_entsize
1597 && iheader->sh_size == oheader->sh_size
1598 && iheader->sh_addr == oheader->sh_addr
1599 && (iheader->sh_info != oheader->sh_info
1600 || iheader->sh_link != oheader->sh_link))
63b9bbb7 1601 {
5522f910
NC
1602 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1603 break;
63b9bbb7
NC
1604 }
1605 }
5522f910
NC
1606
1607 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1608 {
1609 /* Final attempt. Call the backend copy function
1610 with a NULL input section. */
a859124d
AM
1611 (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1612 NULL, oheader);
5522f910 1613 }
63b9bbb7
NC
1614 }
1615
b34976b6 1616 return TRUE;
2d502050
L
1617}
1618
cedc298e
L
1619static const char *
1620get_segment_type (unsigned int p_type)
1621{
1622 const char *pt;
1623 switch (p_type)
1624 {
1625 case PT_NULL: pt = "NULL"; break;
1626 case PT_LOAD: pt = "LOAD"; break;
1627 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1628 case PT_INTERP: pt = "INTERP"; break;
1629 case PT_NOTE: pt = "NOTE"; break;
1630 case PT_SHLIB: pt = "SHLIB"; break;
1631 case PT_PHDR: pt = "PHDR"; break;
1632 case PT_TLS: pt = "TLS"; break;
1633 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
2b05f1b7 1634 case PT_GNU_STACK: pt = "STACK"; break;
cedc298e
L
1635 case PT_GNU_RELRO: pt = "RELRO"; break;
1636 default: pt = NULL; break;
1637 }
1638 return pt;
1639}
1640
f0b79d91
L
1641/* Print out the program headers. */
1642
b34976b6 1643bfd_boolean
217aa764 1644_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
252b5132 1645{
a50b1753 1646 FILE *f = (FILE *) farg;
252b5132
RH
1647 Elf_Internal_Phdr *p;
1648 asection *s;
1649 bfd_byte *dynbuf = NULL;
1650
1651 p = elf_tdata (abfd)->phdr;
1652 if (p != NULL)
1653 {
1654 unsigned int i, c;
1655
1656 fprintf (f, _("\nProgram Header:\n"));
1657 c = elf_elfheader (abfd)->e_phnum;
1658 for (i = 0; i < c; i++, p++)
1659 {
cedc298e 1660 const char *pt = get_segment_type (p->p_type);
252b5132
RH
1661 char buf[20];
1662
cedc298e 1663 if (pt == NULL)
252b5132 1664 {
cedc298e
L
1665 sprintf (buf, "0x%lx", p->p_type);
1666 pt = buf;
252b5132 1667 }
dc810e39 1668 fprintf (f, "%8s off 0x", pt);
60b89a18 1669 bfd_fprintf_vma (abfd, f, p->p_offset);
252b5132 1670 fprintf (f, " vaddr 0x");
60b89a18 1671 bfd_fprintf_vma (abfd, f, p->p_vaddr);
252b5132 1672 fprintf (f, " paddr 0x");
60b89a18 1673 bfd_fprintf_vma (abfd, f, p->p_paddr);
252b5132
RH
1674 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1675 fprintf (f, " filesz 0x");
60b89a18 1676 bfd_fprintf_vma (abfd, f, p->p_filesz);
252b5132 1677 fprintf (f, " memsz 0x");
60b89a18 1678 bfd_fprintf_vma (abfd, f, p->p_memsz);
252b5132
RH
1679 fprintf (f, " flags %c%c%c",
1680 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1681 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1682 (p->p_flags & PF_X) != 0 ? 'x' : '-');
dc810e39
AM
1683 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1684 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
252b5132
RH
1685 fprintf (f, "\n");
1686 }
1687 }
1688
1689 s = bfd_get_section_by_name (abfd, ".dynamic");
1690 if (s != NULL)
1691 {
cb33740c 1692 unsigned int elfsec;
dc810e39 1693 unsigned long shlink;
252b5132
RH
1694 bfd_byte *extdyn, *extdynend;
1695 size_t extdynsize;
217aa764 1696 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
252b5132
RH
1697
1698 fprintf (f, _("\nDynamic Section:\n"));
1699
eea6121a 1700 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
252b5132
RH
1701 goto error_return;
1702
1703 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 1704 if (elfsec == SHN_BAD)
252b5132 1705 goto error_return;
dc810e39 1706 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
252b5132
RH
1707
1708 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1709 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1710
1711 extdyn = dynbuf;
06614111
NC
1712 /* PR 17512: file: 6f427532. */
1713 if (s->size < extdynsize)
1714 goto error_return;
eea6121a 1715 extdynend = extdyn + s->size;
1036838a 1716 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
07d6d2b8 1717 Fix range check. */
1036838a 1718 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
252b5132
RH
1719 {
1720 Elf_Internal_Dyn dyn;
ad9563d6 1721 const char *name = "";
252b5132 1722 char ab[20];
b34976b6 1723 bfd_boolean stringp;
ad9563d6 1724 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 1725
217aa764 1726 (*swap_dyn_in) (abfd, extdyn, &dyn);
252b5132
RH
1727
1728 if (dyn.d_tag == DT_NULL)
1729 break;
1730
b34976b6 1731 stringp = FALSE;
252b5132
RH
1732 switch (dyn.d_tag)
1733 {
1734 default:
ad9563d6
CM
1735 if (bed->elf_backend_get_target_dtag)
1736 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1737
1738 if (!strcmp (name, ""))
1739 {
cd9af601 1740 sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag);
ad9563d6
CM
1741 name = ab;
1742 }
252b5132
RH
1743 break;
1744
b34976b6 1745 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
252b5132
RH
1746 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1747 case DT_PLTGOT: name = "PLTGOT"; break;
1748 case DT_HASH: name = "HASH"; break;
1749 case DT_STRTAB: name = "STRTAB"; break;
1750 case DT_SYMTAB: name = "SYMTAB"; break;
1751 case DT_RELA: name = "RELA"; break;
1752 case DT_RELASZ: name = "RELASZ"; break;
1753 case DT_RELAENT: name = "RELAENT"; break;
1754 case DT_STRSZ: name = "STRSZ"; break;
1755 case DT_SYMENT: name = "SYMENT"; break;
1756 case DT_INIT: name = "INIT"; break;
1757 case DT_FINI: name = "FINI"; break;
b34976b6
AM
1758 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1759 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
252b5132
RH
1760 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1761 case DT_REL: name = "REL"; break;
1762 case DT_RELSZ: name = "RELSZ"; break;
1763 case DT_RELENT: name = "RELENT"; break;
1764 case DT_PLTREL: name = "PLTREL"; break;
1765 case DT_DEBUG: name = "DEBUG"; break;
1766 case DT_TEXTREL: name = "TEXTREL"; break;
1767 case DT_JMPREL: name = "JMPREL"; break;
94558834
L
1768 case DT_BIND_NOW: name = "BIND_NOW"; break;
1769 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1770 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1771 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1772 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
b34976b6 1773 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
94558834
L
1774 case DT_FLAGS: name = "FLAGS"; break;
1775 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1776 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
d48188b9 1777 case DT_CHECKSUM: name = "CHECKSUM"; break;
94558834
L
1778 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1779 case DT_MOVEENT: name = "MOVEENT"; break;
1780 case DT_MOVESZ: name = "MOVESZ"; break;
1781 case DT_FEATURE: name = "FEATURE"; break;
1782 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1783 case DT_SYMINSZ: name = "SYMINSZ"; break;
1784 case DT_SYMINENT: name = "SYMINENT"; break;
b34976b6
AM
1785 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1786 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1787 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
94558834
L
1788 case DT_PLTPAD: name = "PLTPAD"; break;
1789 case DT_MOVETAB: name = "MOVETAB"; break;
1790 case DT_SYMINFO: name = "SYMINFO"; break;
1791 case DT_RELACOUNT: name = "RELACOUNT"; break;
1792 case DT_RELCOUNT: name = "RELCOUNT"; break;
1793 case DT_FLAGS_1: name = "FLAGS_1"; break;
252b5132
RH
1794 case DT_VERSYM: name = "VERSYM"; break;
1795 case DT_VERDEF: name = "VERDEF"; break;
1796 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1797 case DT_VERNEED: name = "VERNEED"; break;
1798 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
b34976b6 1799 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
94558834 1800 case DT_USED: name = "USED"; break;
b34976b6 1801 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
fdc90cb4 1802 case DT_GNU_HASH: name = "GNU_HASH"; break;
252b5132
RH
1803 }
1804
ad9563d6 1805 fprintf (f, " %-20s ", name);
252b5132 1806 if (! stringp)
a1f3c56e
AN
1807 {
1808 fprintf (f, "0x");
1809 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1810 }
252b5132
RH
1811 else
1812 {
1813 const char *string;
dc810e39 1814 unsigned int tagv = dyn.d_un.d_val;
252b5132 1815
dc810e39 1816 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
252b5132
RH
1817 if (string == NULL)
1818 goto error_return;
1819 fprintf (f, "%s", string);
1820 }
1821 fprintf (f, "\n");
1822 }
1823
1824 free (dynbuf);
1825 dynbuf = NULL;
1826 }
1827
1828 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1829 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1830 {
fc0e6df6 1831 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
b34976b6 1832 return FALSE;
252b5132
RH
1833 }
1834
1835 if (elf_dynverdef (abfd) != 0)
1836 {
1837 Elf_Internal_Verdef *t;
1838
1839 fprintf (f, _("\nVersion definitions:\n"));
1840 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1841 {
1842 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
d0fb9a8d
JJ
1843 t->vd_flags, t->vd_hash,
1844 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1845 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
252b5132
RH
1846 {
1847 Elf_Internal_Verdaux *a;
1848
1849 fprintf (f, "\t");
1850 for (a = t->vd_auxptr->vda_nextptr;
1851 a != NULL;
1852 a = a->vda_nextptr)
d0fb9a8d
JJ
1853 fprintf (f, "%s ",
1854 a->vda_nodename ? a->vda_nodename : "<corrupt>");
252b5132
RH
1855 fprintf (f, "\n");
1856 }
1857 }
1858 }
1859
1860 if (elf_dynverref (abfd) != 0)
1861 {
1862 Elf_Internal_Verneed *t;
1863
1864 fprintf (f, _("\nVersion References:\n"));
1865 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1866 {
1867 Elf_Internal_Vernaux *a;
1868
d0fb9a8d
JJ
1869 fprintf (f, _(" required from %s:\n"),
1870 t->vn_filename ? t->vn_filename : "<corrupt>");
252b5132
RH
1871 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1872 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
d0fb9a8d
JJ
1873 a->vna_flags, a->vna_other,
1874 a->vna_nodename ? a->vna_nodename : "<corrupt>");
252b5132
RH
1875 }
1876 }
1877
b34976b6 1878 return TRUE;
252b5132
RH
1879
1880 error_return:
1881 if (dynbuf != NULL)
1882 free (dynbuf);
b34976b6 1883 return FALSE;
252b5132
RH
1884}
1885
7e6e972f
L
1886/* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
1887 and return symbol version for symbol version itself. */
bb4d2ac2
L
1888
1889const char *
1081065c
L
1890_bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
1891 bfd_boolean base_p,
1892 bfd_boolean *hidden)
bb4d2ac2
L
1893{
1894 const char *version_string = NULL;
1895 if (elf_dynversym (abfd) != 0
1896 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1897 {
1898 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1899
1900 *hidden = (vernum & VERSYM_HIDDEN) != 0;
1901 vernum &= VERSYM_VERSION;
1902
1903 if (vernum == 0)
1904 version_string = "";
1f6f5dba
L
1905 else if (vernum == 1
1906 && (vernum > elf_tdata (abfd)->cverdefs
1907 || (elf_tdata (abfd)->verdef[0].vd_flags
1908 == VER_FLG_BASE)))
7e6e972f 1909 version_string = base_p ? "Base" : "";
bb4d2ac2 1910 else if (vernum <= elf_tdata (abfd)->cverdefs)
7e6e972f
L
1911 {
1912 const char *nodename
1913 = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
8d55d10a
AM
1914 version_string = "";
1915 if (base_p
1916 || nodename == NULL
1917 || symbol->name == NULL
1918 || strcmp (symbol->name, nodename) != 0)
1919 version_string = nodename;
7e6e972f 1920 }
bb4d2ac2
L
1921 else
1922 {
1923 Elf_Internal_Verneed *t;
1924
7a815dd5 1925 version_string = _("<corrupt>");
bb4d2ac2
L
1926 for (t = elf_tdata (abfd)->verref;
1927 t != NULL;
1928 t = t->vn_nextref)
1929 {
1930 Elf_Internal_Vernaux *a;
1931
1932 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1933 {
1934 if (a->vna_other == vernum)
1935 {
1936 version_string = a->vna_nodename;
1937 break;
1938 }
1939 }
1940 }
1941 }
1942 }
1943 return version_string;
1944}
1945
252b5132
RH
1946/* Display ELF-specific fields of a symbol. */
1947
1948void
217aa764
AM
1949bfd_elf_print_symbol (bfd *abfd,
1950 void *filep,
1951 asymbol *symbol,
1952 bfd_print_symbol_type how)
252b5132 1953{
a50b1753 1954 FILE *file = (FILE *) filep;
252b5132
RH
1955 switch (how)
1956 {
1957 case bfd_print_symbol_name:
1958 fprintf (file, "%s", symbol->name);
1959 break;
1960 case bfd_print_symbol_more:
1961 fprintf (file, "elf ");
60b89a18 1962 bfd_fprintf_vma (abfd, file, symbol->value);
cd9af601 1963 fprintf (file, " %x", symbol->flags);
252b5132
RH
1964 break;
1965 case bfd_print_symbol_all:
1966 {
4e8a9624
AM
1967 const char *section_name;
1968 const char *name = NULL;
9c5bfbb7 1969 const struct elf_backend_data *bed;
7a13edea 1970 unsigned char st_other;
dbb410c3 1971 bfd_vma val;
bb4d2ac2
L
1972 const char *version_string;
1973 bfd_boolean hidden;
c044fabd 1974
252b5132 1975 section_name = symbol->section ? symbol->section->name : "(*none*)";
587ff49e
RH
1976
1977 bed = get_elf_backend_data (abfd);
1978 if (bed->elf_backend_print_symbol_all)
c044fabd 1979 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
587ff49e
RH
1980
1981 if (name == NULL)
1982 {
7ee38065 1983 name = symbol->name;
217aa764 1984 bfd_print_symbol_vandf (abfd, file, symbol);
587ff49e
RH
1985 }
1986
252b5132
RH
1987 fprintf (file, " %s\t", section_name);
1988 /* Print the "other" value for a symbol. For common symbols,
1989 we've already printed the size; now print the alignment.
1990 For other symbols, we have no specified alignment, and
1991 we've printed the address; now print the size. */
dcf6c779 1992 if (symbol->section && bfd_is_com_section (symbol->section))
dbb410c3
AM
1993 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1994 else
1995 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1996 bfd_fprintf_vma (abfd, file, val);
252b5132
RH
1997
1998 /* If we have version information, print it. */
60bb06bc
L
1999 version_string = _bfd_elf_get_symbol_version_string (abfd,
2000 symbol,
1081065c 2001 TRUE,
60bb06bc 2002 &hidden);
bb4d2ac2 2003 if (version_string)
252b5132 2004 {
bb4d2ac2 2005 if (!hidden)
252b5132
RH
2006 fprintf (file, " %-11s", version_string);
2007 else
2008 {
2009 int i;
2010
2011 fprintf (file, " (%s)", version_string);
2012 for (i = 10 - strlen (version_string); i > 0; --i)
2013 putc (' ', file);
2014 }
2015 }
2016
2017 /* If the st_other field is not zero, print it. */
7a13edea 2018 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
c044fabd 2019
7a13edea
NC
2020 switch (st_other)
2021 {
2022 case 0: break;
2023 case STV_INTERNAL: fprintf (file, " .internal"); break;
2024 case STV_HIDDEN: fprintf (file, " .hidden"); break;
2025 case STV_PROTECTED: fprintf (file, " .protected"); break;
2026 default:
2027 /* Some other non-defined flags are also present, so print
2028 everything hex. */
2029 fprintf (file, " 0x%02x", (unsigned int) st_other);
2030 }
252b5132 2031
587ff49e 2032 fprintf (file, " %s", name);
252b5132
RH
2033 }
2034 break;
2035 }
2036}
252b5132
RH
2037\f
2038/* ELF .o/exec file reading */
2039
c044fabd 2040/* Create a new bfd section from an ELF section header. */
252b5132 2041
b34976b6 2042bfd_boolean
217aa764 2043bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
252b5132 2044{
4fbb74a6
AM
2045 Elf_Internal_Shdr *hdr;
2046 Elf_Internal_Ehdr *ehdr;
2047 const struct elf_backend_data *bed;
90937f86 2048 const char *name;
bf67003b
NC
2049 bfd_boolean ret = TRUE;
2050 static bfd_boolean * sections_being_created = NULL;
5a4b0ccc 2051 static bfd * sections_being_created_abfd = NULL;
bf67003b 2052 static unsigned int nesting = 0;
252b5132 2053
4fbb74a6
AM
2054 if (shindex >= elf_numsections (abfd))
2055 return FALSE;
2056
bf67003b
NC
2057 if (++ nesting > 3)
2058 {
2059 /* PR17512: A corrupt ELF binary might contain a recursive group of
67ce483b 2060 sections, with each the string indices pointing to the next in the
bf67003b
NC
2061 loop. Detect this here, by refusing to load a section that we are
2062 already in the process of loading. We only trigger this test if
2063 we have nested at least three sections deep as normal ELF binaries
5a4b0ccc
NC
2064 can expect to recurse at least once.
2065
2066 FIXME: It would be better if this array was attached to the bfd,
2067 rather than being held in a static pointer. */
2068
2069 if (sections_being_created_abfd != abfd)
2070 sections_being_created = NULL;
bf67003b
NC
2071 if (sections_being_created == NULL)
2072 {
446f7ed5 2073 size_t amt = elf_numsections (abfd) * sizeof (bfd_boolean);
ed02cdb5
NC
2074
2075 /* PR 26005: Do not use bfd_zalloc here as the memory might
2076 be released before the bfd has been fully scanned. */
2077 sections_being_created = (bfd_boolean *) bfd_malloc (amt);
96d3b80f
AM
2078 if (sections_being_created == NULL)
2079 return FALSE;
6fd1d259 2080 memset (sections_being_created, FALSE, amt);
5a4b0ccc 2081 sections_being_created_abfd = abfd;
bf67003b
NC
2082 }
2083 if (sections_being_created [shindex])
2084 {
4eca0228 2085 _bfd_error_handler
871b3ab2 2086 (_("%pB: warning: loop in section dependencies detected"), abfd);
bf67003b
NC
2087 return FALSE;
2088 }
2089 sections_being_created [shindex] = TRUE;
2090 }
2091
4fbb74a6
AM
2092 hdr = elf_elfsections (abfd)[shindex];
2093 ehdr = elf_elfheader (abfd);
2094 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1b3a8575 2095 hdr->sh_name);
933d961a 2096 if (name == NULL)
bf67003b 2097 goto fail;
252b5132 2098
4fbb74a6 2099 bed = get_elf_backend_data (abfd);
252b5132
RH
2100 switch (hdr->sh_type)
2101 {
2102 case SHT_NULL:
2103 /* Inactive section. Throw it away. */
bf67003b 2104 goto success;
252b5132 2105
bf67003b
NC
2106 case SHT_PROGBITS: /* Normal section with contents. */
2107 case SHT_NOBITS: /* .bss section. */
2108 case SHT_HASH: /* .hash section. */
2109 case SHT_NOTE: /* .note section. */
25e27870
L
2110 case SHT_INIT_ARRAY: /* .init_array section. */
2111 case SHT_FINI_ARRAY: /* .fini_array section. */
2112 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
7f1204bb 2113 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
fdc90cb4 2114 case SHT_GNU_HASH: /* .gnu.hash section. */
bf67003b
NC
2115 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2116 goto success;
252b5132 2117
797fc050 2118 case SHT_DYNAMIC: /* Dynamic linking information. */
6dc132d9 2119 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b
NC
2120 goto fail;
2121
cfcac11d
NC
2122 if (hdr->sh_link > elf_numsections (abfd))
2123 {
caa83f8b 2124 /* PR 10478: Accept Solaris binaries with a sh_link
cfcac11d
NC
2125 field set to SHN_BEFORE or SHN_AFTER. */
2126 switch (bfd_get_arch (abfd))
2127 {
caa83f8b 2128 case bfd_arch_i386:
cfcac11d
NC
2129 case bfd_arch_sparc:
2130 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
2131 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
2132 break;
2133 /* Otherwise fall through. */
2134 default:
bf67003b 2135 goto fail;
cfcac11d
NC
2136 }
2137 }
2138 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
bf67003b 2139 goto fail;
cfcac11d 2140 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
797fc050
AM
2141 {
2142 Elf_Internal_Shdr *dynsymhdr;
2143
2144 /* The shared libraries distributed with hpux11 have a bogus
2145 sh_link field for the ".dynamic" section. Find the
2146 string table for the ".dynsym" section instead. */
2147 if (elf_dynsymtab (abfd) != 0)
2148 {
2149 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2150 hdr->sh_link = dynsymhdr->sh_link;
2151 }
2152 else
2153 {
2154 unsigned int i, num_sec;
2155
2156 num_sec = elf_numsections (abfd);
2157 for (i = 1; i < num_sec; i++)
2158 {
2159 dynsymhdr = elf_elfsections (abfd)[i];
2160 if (dynsymhdr->sh_type == SHT_DYNSYM)
2161 {
2162 hdr->sh_link = dynsymhdr->sh_link;
2163 break;
2164 }
2165 }
2166 }
2167 }
bf67003b 2168 goto success;
797fc050 2169
bf67003b 2170 case SHT_SYMTAB: /* A symbol table. */
252b5132 2171 if (elf_onesymtab (abfd) == shindex)
bf67003b 2172 goto success;
252b5132 2173
a50b2160 2174 if (hdr->sh_entsize != bed->s->sizeof_sym)
bf67003b
NC
2175 goto fail;
2176
3337c1e5 2177 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
eee3b786
AM
2178 {
2179 if (hdr->sh_size != 0)
bf67003b 2180 goto fail;
eee3b786
AM
2181 /* Some assemblers erroneously set sh_info to one with a
2182 zero sh_size. ld sees this as a global symbol count
2183 of (unsigned) -1. Fix it here. */
2184 hdr->sh_info = 0;
bf67003b 2185 goto success;
eee3b786 2186 }
bf67003b 2187
16ad13ec
NC
2188 /* PR 18854: A binary might contain more than one symbol table.
2189 Unusual, but possible. Warn, but continue. */
2190 if (elf_onesymtab (abfd) != 0)
2191 {
4eca0228 2192 _bfd_error_handler
695344c0 2193 /* xgettext:c-format */
871b3ab2 2194 (_("%pB: warning: multiple symbol tables detected"
63a5468a 2195 " - ignoring the table in section %u"),
16ad13ec
NC
2196 abfd, shindex);
2197 goto success;
2198 }
252b5132 2199 elf_onesymtab (abfd) = shindex;
6a40cf0c
NC
2200 elf_symtab_hdr (abfd) = *hdr;
2201 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
252b5132
RH
2202 abfd->flags |= HAS_SYMS;
2203
2204 /* Sometimes a shared object will map in the symbol table. If
08a40648
AM
2205 SHF_ALLOC is set, and this is a shared object, then we also
2206 treat this section as a BFD section. We can not base the
2207 decision purely on SHF_ALLOC, because that flag is sometimes
2208 set in a relocatable object file, which would confuse the
2209 linker. */
252b5132
RH
2210 if ((hdr->sh_flags & SHF_ALLOC) != 0
2211 && (abfd->flags & DYNAMIC) != 0
6dc132d9
L
2212 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2213 shindex))
bf67003b 2214 goto fail;
252b5132 2215
1b3a8575
AM
2216 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2217 can't read symbols without that section loaded as well. It
2218 is most likely specified by the next section header. */
6a40cf0c
NC
2219 {
2220 elf_section_list * entry;
2221 unsigned int i, num_sec;
1b3a8575 2222
6a40cf0c
NC
2223 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2224 if (entry->hdr.sh_link == shindex)
2225 goto success;
2226
2227 num_sec = elf_numsections (abfd);
2228 for (i = shindex + 1; i < num_sec; i++)
2229 {
2230 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2231
2232 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2233 && hdr2->sh_link == shindex)
2234 break;
2235 }
2236
2237 if (i == num_sec)
2238 for (i = 1; i < shindex; i++)
1b3a8575
AM
2239 {
2240 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
6a40cf0c 2241
1b3a8575
AM
2242 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2243 && hdr2->sh_link == shindex)
2244 break;
2245 }
6a40cf0c
NC
2246
2247 if (i != shindex)
2248 ret = bfd_section_from_shdr (abfd, i);
2249 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2250 goto success;
2251 }
252b5132 2252
bf67003b 2253 case SHT_DYNSYM: /* A dynamic symbol table. */
252b5132 2254 if (elf_dynsymtab (abfd) == shindex)
bf67003b 2255 goto success;
252b5132 2256
a50b2160 2257 if (hdr->sh_entsize != bed->s->sizeof_sym)
bf67003b
NC
2258 goto fail;
2259
eee3b786
AM
2260 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2261 {
2262 if (hdr->sh_size != 0)
bf67003b
NC
2263 goto fail;
2264
eee3b786
AM
2265 /* Some linkers erroneously set sh_info to one with a
2266 zero sh_size. ld sees this as a global symbol count
2267 of (unsigned) -1. Fix it here. */
2268 hdr->sh_info = 0;
bf67003b 2269 goto success;
eee3b786 2270 }
bf67003b 2271
16ad13ec
NC
2272 /* PR 18854: A binary might contain more than one dynamic symbol table.
2273 Unusual, but possible. Warn, but continue. */
2274 if (elf_dynsymtab (abfd) != 0)
2275 {
4eca0228 2276 _bfd_error_handler
695344c0 2277 /* xgettext:c-format */
871b3ab2 2278 (_("%pB: warning: multiple dynamic symbol tables detected"
63a5468a 2279 " - ignoring the table in section %u"),
16ad13ec
NC
2280 abfd, shindex);
2281 goto success;
2282 }
252b5132
RH
2283 elf_dynsymtab (abfd) = shindex;
2284 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2285 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2286 abfd->flags |= HAS_SYMS;
2287
2288 /* Besides being a symbol table, we also treat this as a regular
2289 section, so that objcopy can handle it. */
bf67003b
NC
2290 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2291 goto success;
252b5132 2292
bf67003b 2293 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
6a40cf0c
NC
2294 {
2295 elf_section_list * entry;
9ad5cbcf 2296
6a40cf0c
NC
2297 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2298 if (entry->ndx == shindex)
2299 goto success;
07d6d2b8 2300
7a6e0d89 2301 entry = bfd_alloc (abfd, sizeof (*entry));
6a40cf0c
NC
2302 if (entry == NULL)
2303 goto fail;
2304 entry->ndx = shindex;
2305 entry->hdr = * hdr;
2306 entry->next = elf_symtab_shndx_list (abfd);
2307 elf_symtab_shndx_list (abfd) = entry;
2308 elf_elfsections (abfd)[shindex] = & entry->hdr;
2309 goto success;
2310 }
9ad5cbcf 2311
bf67003b 2312 case SHT_STRTAB: /* A string table. */
252b5132 2313 if (hdr->bfd_section != NULL)
bf67003b
NC
2314 goto success;
2315
252b5132
RH
2316 if (ehdr->e_shstrndx == shindex)
2317 {
2318 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2319 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
bf67003b 2320 goto success;
252b5132 2321 }
bf67003b 2322
1b3a8575
AM
2323 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2324 {
2325 symtab_strtab:
2326 elf_tdata (abfd)->strtab_hdr = *hdr;
2327 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
bf67003b 2328 goto success;
1b3a8575 2329 }
bf67003b 2330
1b3a8575
AM
2331 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2332 {
2333 dynsymtab_strtab:
2334 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2335 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2336 elf_elfsections (abfd)[shindex] = hdr;
2337 /* We also treat this as a regular section, so that objcopy
2338 can handle it. */
bf67003b
NC
2339 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2340 shindex);
2341 goto success;
1b3a8575 2342 }
252b5132 2343
1b3a8575
AM
2344 /* If the string table isn't one of the above, then treat it as a
2345 regular section. We need to scan all the headers to be sure,
2346 just in case this strtab section appeared before the above. */
2347 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2348 {
2349 unsigned int i, num_sec;
252b5132 2350
1b3a8575
AM
2351 num_sec = elf_numsections (abfd);
2352 for (i = 1; i < num_sec; i++)
2353 {
2354 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2355 if (hdr2->sh_link == shindex)
2356 {
933d961a
JJ
2357 /* Prevent endless recursion on broken objects. */
2358 if (i == shindex)
bf67003b 2359 goto fail;
1b3a8575 2360 if (! bfd_section_from_shdr (abfd, i))
bf67003b 2361 goto fail;
1b3a8575
AM
2362 if (elf_onesymtab (abfd) == i)
2363 goto symtab_strtab;
2364 if (elf_dynsymtab (abfd) == i)
2365 goto dynsymtab_strtab;
2366 }
2367 }
2368 }
bf67003b
NC
2369 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2370 goto success;
252b5132
RH
2371
2372 case SHT_REL:
2373 case SHT_RELA:
2374 /* *These* do a lot of work -- but build no sections! */
2375 {
2376 asection *target_sect;
d4730f92 2377 Elf_Internal_Shdr *hdr2, **p_hdr;
9ad5cbcf 2378 unsigned int num_sec = elf_numsections (abfd);
d4730f92 2379 struct bfd_elf_section_data *esdt;
252b5132 2380
aa2ca951
JJ
2381 if (hdr->sh_entsize
2382 != (bfd_size_type) (hdr->sh_type == SHT_REL
a50b2160 2383 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
bf67003b 2384 goto fail;
a50b2160 2385
03ae5f59 2386 /* Check for a bogus link to avoid crashing. */
4fbb74a6 2387 if (hdr->sh_link >= num_sec)
03ae5f59 2388 {
4eca0228 2389 _bfd_error_handler
695344c0 2390 /* xgettext:c-format */
871b3ab2 2391 (_("%pB: invalid link %u for reloc section %s (index %u)"),
4eca0228 2392 abfd, hdr->sh_link, name, shindex);
bf67003b
NC
2393 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2394 shindex);
2395 goto success;
03ae5f59
ILT
2396 }
2397
252b5132
RH
2398 /* For some incomprehensible reason Oracle distributes
2399 libraries for Solaris in which some of the objects have
2400 bogus sh_link fields. It would be nice if we could just
2401 reject them, but, unfortunately, some people need to use
2402 them. We scan through the section headers; if we find only
2403 one suitable symbol table, we clobber the sh_link to point
83b89087
L
2404 to it. I hope this doesn't break anything.
2405
2406 Don't do it on executable nor shared library. */
2407 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
2408 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
252b5132
RH
2409 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
2410 {
9ad5cbcf 2411 unsigned int scan;
252b5132
RH
2412 int found;
2413
2414 found = 0;
9ad5cbcf 2415 for (scan = 1; scan < num_sec; scan++)
252b5132
RH
2416 {
2417 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
2418 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
2419 {
2420 if (found != 0)
2421 {
2422 found = 0;
2423 break;
2424 }
2425 found = scan;
2426 }
2427 }
2428 if (found != 0)
2429 hdr->sh_link = found;
2430 }
2431
2432 /* Get the symbol table. */
1b3a8575
AM
2433 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2434 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
252b5132 2435 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
bf67003b 2436 goto fail;
252b5132 2437
a4bcd733
AM
2438 /* If this is an alloc section in an executable or shared
2439 library, or the reloc section does not use the main symbol
2440 table we don't treat it as a reloc section. BFD can't
2441 adequately represent such a section, so at least for now,
2442 we don't try. We just present it as a normal section. We
2443 also can't use it as a reloc section if it points to the
2444 null section, an invalid section, another reloc section, or
2445 its sh_link points to the null section. */
2446 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2447 && (hdr->sh_flags & SHF_ALLOC) != 0)
83b89087 2448 || hdr->sh_link == SHN_UNDEF
a4bcd733 2449 || hdr->sh_link != elf_onesymtab (abfd)
185ef66d 2450 || hdr->sh_info == SHN_UNDEF
185ef66d
AM
2451 || hdr->sh_info >= num_sec
2452 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2453 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
bf67003b
NC
2454 {
2455 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2456 shindex);
2457 goto success;
2458 }
252b5132
RH
2459
2460 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
bf67003b
NC
2461 goto fail;
2462
252b5132
RH
2463 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2464 if (target_sect == NULL)
bf67003b 2465 goto fail;
252b5132 2466
d4730f92
BS
2467 esdt = elf_section_data (target_sect);
2468 if (hdr->sh_type == SHT_RELA)
2469 p_hdr = &esdt->rela.hdr;
252b5132 2470 else
d4730f92
BS
2471 p_hdr = &esdt->rel.hdr;
2472
a7ba3896
NC
2473 /* PR 17512: file: 0b4f81b7.
2474 Also see PR 24456, for a file which deliberately has two reloc
2475 sections. */
06614111 2476 if (*p_hdr != NULL)
a7ba3896 2477 {
a859124d 2478 if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
a8e14f4c
NC
2479 {
2480 _bfd_error_handler
2481 /* xgettext:c-format */
a859124d
AM
2482 (_("%pB: warning: secondary relocation section '%s' "
2483 "for section %pA found - ignoring"),
a8e14f4c
NC
2484 abfd, name, target_sect);
2485 }
a7ba3896
NC
2486 goto success;
2487 }
a8e14f4c 2488
ef53be89 2489 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
d4730f92 2490 if (hdr2 == NULL)
bf67003b 2491 goto fail;
252b5132 2492 *hdr2 = *hdr;
d4730f92 2493 *p_hdr = hdr2;
252b5132 2494 elf_elfsections (abfd)[shindex] = hdr2;
056bafd4
MR
2495 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2496 * bed->s->int_rels_per_ext_rel);
252b5132
RH
2497 target_sect->flags |= SEC_RELOC;
2498 target_sect->relocation = NULL;
2499 target_sect->rel_filepos = hdr->sh_offset;
bf572ba0
MM
2500 /* In the section to which the relocations apply, mark whether
2501 its relocations are of the REL or RELA variety. */
72730e0c 2502 if (hdr->sh_size != 0)
d4730f92
BS
2503 {
2504 if (hdr->sh_type == SHT_RELA)
2505 target_sect->use_rela_p = 1;
2506 }
252b5132 2507 abfd->flags |= HAS_RELOC;
bf67003b 2508 goto success;
252b5132 2509 }
252b5132
RH
2510
2511 case SHT_GNU_verdef:
2512 elf_dynverdef (abfd) = shindex;
2513 elf_tdata (abfd)->dynverdef_hdr = *hdr;
bf67003b
NC
2514 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2515 goto success;
252b5132
RH
2516
2517 case SHT_GNU_versym:
a50b2160 2518 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
bf67003b
NC
2519 goto fail;
2520
252b5132
RH
2521 elf_dynversym (abfd) = shindex;
2522 elf_tdata (abfd)->dynversym_hdr = *hdr;
bf67003b
NC
2523 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2524 goto success;
252b5132
RH
2525
2526 case SHT_GNU_verneed:
2527 elf_dynverref (abfd) = shindex;
2528 elf_tdata (abfd)->dynverref_hdr = *hdr;
bf67003b
NC
2529 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2530 goto success;
252b5132
RH
2531
2532 case SHT_SHLIB:
bf67003b 2533 goto success;
252b5132 2534
dbb410c3 2535 case SHT_GROUP:
44534af3 2536 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
bf67003b
NC
2537 goto fail;
2538
6dc132d9 2539 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b
NC
2540 goto fail;
2541
bf67003b 2542 goto success;
dbb410c3 2543
252b5132 2544 default:
104d59d1
JM
2545 /* Possibly an attributes section. */
2546 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2547 || hdr->sh_type == bed->obj_attrs_section_type)
2548 {
2549 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b 2550 goto fail;
104d59d1 2551 _bfd_elf_parse_attributes (abfd, hdr);
bf67003b 2552 goto success;
104d59d1
JM
2553 }
2554
252b5132 2555 /* Check for any processor-specific section types. */
3eb70a79 2556 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
bf67003b 2557 goto success;
3eb70a79
L
2558
2559 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2560 {
2561 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2562 /* FIXME: How to properly handle allocated section reserved
2563 for applications? */
4eca0228 2564 _bfd_error_handler
695344c0 2565 /* xgettext:c-format */
871b3ab2 2566 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2567 abfd, hdr->sh_type, name);
3eb70a79 2568 else
bf67003b
NC
2569 {
2570 /* Allow sections reserved for applications. */
2571 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2572 shindex);
2573 goto success;
2574 }
3eb70a79
L
2575 }
2576 else if (hdr->sh_type >= SHT_LOPROC
2577 && hdr->sh_type <= SHT_HIPROC)
2578 /* FIXME: We should handle this section. */
4eca0228 2579 _bfd_error_handler
695344c0 2580 /* xgettext:c-format */
871b3ab2 2581 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2582 abfd, hdr->sh_type, name);
3eb70a79 2583 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
ff15b240
NC
2584 {
2585 /* Unrecognised OS-specific sections. */
2586 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2587 /* SHF_OS_NONCONFORMING indicates that special knowledge is
08a40648 2588 required to correctly process the section and the file should
ff15b240 2589 be rejected with an error message. */
4eca0228 2590 _bfd_error_handler
695344c0 2591 /* xgettext:c-format */
871b3ab2 2592 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2593 abfd, hdr->sh_type, name);
ff15b240 2594 else
bf67003b
NC
2595 {
2596 /* Otherwise it should be processed. */
2597 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2598 goto success;
2599 }
ff15b240 2600 }
3eb70a79
L
2601 else
2602 /* FIXME: We should handle this section. */
4eca0228 2603 _bfd_error_handler
695344c0 2604 /* xgettext:c-format */
871b3ab2 2605 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2606 abfd, hdr->sh_type, name);
3eb70a79 2607
bf67003b 2608 goto fail;
252b5132
RH
2609 }
2610
bf67003b
NC
2611 fail:
2612 ret = FALSE;
2613 success:
e5b470e2 2614 if (sections_being_created && sections_being_created_abfd == abfd)
bf67003b
NC
2615 sections_being_created [shindex] = FALSE;
2616 if (-- nesting == 0)
5a4b0ccc 2617 {
ed02cdb5 2618 free (sections_being_created);
5a4b0ccc 2619 sections_being_created = NULL;
ed02cdb5 2620 sections_being_created_abfd = NULL;
5a4b0ccc 2621 }
bf67003b 2622 return ret;
252b5132
RH
2623}
2624
87d72d41 2625/* Return the local symbol specified by ABFD, R_SYMNDX. */
ec338859 2626
87d72d41
AM
2627Elf_Internal_Sym *
2628bfd_sym_from_r_symndx (struct sym_cache *cache,
2629 bfd *abfd,
2630 unsigned long r_symndx)
ec338859 2631{
ec338859
AM
2632 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2633
a5d1b3b5
AM
2634 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2635 {
2636 Elf_Internal_Shdr *symtab_hdr;
2637 unsigned char esym[sizeof (Elf64_External_Sym)];
2638 Elf_External_Sym_Shndx eshndx;
ec338859 2639
a5d1b3b5
AM
2640 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2641 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
87d72d41 2642 &cache->sym[ent], esym, &eshndx) == NULL)
a5d1b3b5 2643 return NULL;
9ad5cbcf 2644
a5d1b3b5
AM
2645 if (cache->abfd != abfd)
2646 {
2647 memset (cache->indx, -1, sizeof (cache->indx));
2648 cache->abfd = abfd;
2649 }
2650 cache->indx[ent] = r_symndx;
ec338859 2651 }
a5d1b3b5 2652
87d72d41 2653 return &cache->sym[ent];
ec338859
AM
2654}
2655
252b5132
RH
2656/* Given an ELF section number, retrieve the corresponding BFD
2657 section. */
2658
2659asection *
91d6fa6a 2660bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
252b5132 2661{
91d6fa6a 2662 if (sec_index >= elf_numsections (abfd))
252b5132 2663 return NULL;
91d6fa6a 2664 return elf_elfsections (abfd)[sec_index]->bfd_section;
252b5132
RH
2665}
2666
b35d266b 2667static const struct bfd_elf_special_section special_sections_b[] =
2f89ff8d 2668{
0112cd26 2669 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 2670 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2671};
2672
b35d266b 2673static const struct bfd_elf_special_section special_sections_c[] =
7f4d3958 2674{
0112cd26 2675 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
1ff6de03 2676 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2677 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2678};
2679
b35d266b 2680static const struct bfd_elf_special_section special_sections_d[] =
7f4d3958 2681{
07d6d2b8
AM
2682 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2683 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
a9a72a65
DE
2684 /* There are more DWARF sections than these, but they needn't be added here
2685 unless you have to cope with broken compilers that don't emit section
2686 attributes or you want to help the user writing assembler. */
07d6d2b8
AM
2687 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2688 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2689 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2690 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
0112cd26 2691 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
07d6d2b8
AM
2692 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2693 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2694 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2695 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2696};
2697
b35d266b 2698static const struct bfd_elf_special_section special_sections_f[] =
7f4d3958 2699{
07d6d2b8 2700 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
6f9dbcd4 2701 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8 2702 { NULL, 0 , 0, 0, 0 }
7f4d3958
L
2703};
2704
b35d266b 2705static const struct bfd_elf_special_section special_sections_g[] =
7f4d3958 2706{
0112cd26 2707 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2708 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2709 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2710 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
0112cd26
NC
2711 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2712 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
07d6d2b8
AM
2713 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2714 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2715 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2716 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2717};
2718
b35d266b 2719static const struct bfd_elf_special_section special_sections_h[] =
7f4d3958 2720{
07d6d2b8
AM
2721 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2722 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2723};
2724
b35d266b 2725static const struct bfd_elf_special_section special_sections_i[] =
7f4d3958 2726{
07d6d2b8 2727 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
6f9dbcd4 2728 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2729 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2730 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2731};
2732
b35d266b 2733static const struct bfd_elf_special_section special_sections_l[] =
7f4d3958 2734{
0112cd26 2735 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2736 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2737};
2738
b35d266b 2739static const struct bfd_elf_special_section special_sections_n[] =
7f4d3958 2740{
0112cd26 2741 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
07d6d2b8
AM
2742 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2743 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2744};
2745
b35d266b 2746static const struct bfd_elf_special_section special_sections_p[] =
7f4d3958 2747{
6f9dbcd4 2748 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2749 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2750 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2751};
2752
b35d266b 2753static const struct bfd_elf_special_section special_sections_r[] =
7f4d3958 2754{
0112cd26
NC
2755 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2756 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
07d6d2b8
AM
2757 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2758 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2759 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2760};
2761
b35d266b 2762static const struct bfd_elf_special_section special_sections_s[] =
7f4d3958 2763{
0112cd26
NC
2764 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2765 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2766 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
60ff4dc4
HPN
2767 /* See struct bfd_elf_special_section declaration for the semantics of
2768 this special case where .prefix_length != strlen (.prefix). */
2769 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
07d6d2b8 2770 { NULL, 0, 0, 0, 0 }
2f89ff8d
L
2771};
2772
b35d266b 2773static const struct bfd_elf_special_section special_sections_t[] =
7f4d3958 2774{
07d6d2b8
AM
2775 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2776 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
0112cd26 2777 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
07d6d2b8 2778 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2779};
2780
1b315056
CS
2781static const struct bfd_elf_special_section special_sections_z[] =
2782{
07d6d2b8
AM
2783 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2784 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
1b315056
CS
2785 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2786 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2787 { NULL, 0, 0, 0, 0 }
1b315056
CS
2788};
2789
e4c93b56 2790static const struct bfd_elf_special_section * const special_sections[] =
7f4d3958 2791{
7f4d3958 2792 special_sections_b, /* 'b' */
98ece1b3 2793 special_sections_c, /* 'c' */
7f4d3958
L
2794 special_sections_d, /* 'd' */
2795 NULL, /* 'e' */
2796 special_sections_f, /* 'f' */
2797 special_sections_g, /* 'g' */
2798 special_sections_h, /* 'h' */
2799 special_sections_i, /* 'i' */
2800 NULL, /* 'j' */
2801 NULL, /* 'k' */
2802 special_sections_l, /* 'l' */
2803 NULL, /* 'm' */
2804 special_sections_n, /* 'n' */
2805 NULL, /* 'o' */
2806 special_sections_p, /* 'p' */
2807 NULL, /* 'q' */
2808 special_sections_r, /* 'r' */
2809 special_sections_s, /* 's' */
2810 special_sections_t, /* 't' */
1b315056
CS
2811 NULL, /* 'u' */
2812 NULL, /* 'v' */
2813 NULL, /* 'w' */
2814 NULL, /* 'x' */
2815 NULL, /* 'y' */
2816 special_sections_z /* 'z' */
7f4d3958
L
2817};
2818
551b43fd
AM
2819const struct bfd_elf_special_section *
2820_bfd_elf_get_special_section (const char *name,
2821 const struct bfd_elf_special_section *spec,
2822 unsigned int rela)
2f89ff8d
L
2823{
2824 int i;
7f4d3958 2825 int len;
7f4d3958 2826
551b43fd 2827 len = strlen (name);
7f4d3958 2828
551b43fd 2829 for (i = 0; spec[i].prefix != NULL; i++)
7dcb9820
AM
2830 {
2831 int suffix_len;
551b43fd 2832 int prefix_len = spec[i].prefix_length;
7dcb9820
AM
2833
2834 if (len < prefix_len)
2835 continue;
551b43fd 2836 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
7dcb9820
AM
2837 continue;
2838
551b43fd 2839 suffix_len = spec[i].suffix_length;
7dcb9820
AM
2840 if (suffix_len <= 0)
2841 {
2842 if (name[prefix_len] != 0)
2843 {
2844 if (suffix_len == 0)
2845 continue;
2846 if (name[prefix_len] != '.'
2847 && (suffix_len == -2
551b43fd 2848 || (rela && spec[i].type == SHT_REL)))
7dcb9820
AM
2849 continue;
2850 }
2851 }
2852 else
2853 {
2854 if (len < prefix_len + suffix_len)
2855 continue;
2856 if (memcmp (name + len - suffix_len,
551b43fd 2857 spec[i].prefix + prefix_len,
7dcb9820
AM
2858 suffix_len) != 0)
2859 continue;
2860 }
551b43fd 2861 return &spec[i];
7dcb9820 2862 }
2f89ff8d
L
2863
2864 return NULL;
2865}
2866
7dcb9820 2867const struct bfd_elf_special_section *
29ef7005 2868_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2f89ff8d 2869{
551b43fd
AM
2870 int i;
2871 const struct bfd_elf_special_section *spec;
29ef7005 2872 const struct elf_backend_data *bed;
2f89ff8d
L
2873
2874 /* See if this is one of the special sections. */
551b43fd
AM
2875 if (sec->name == NULL)
2876 return NULL;
2f89ff8d 2877
29ef7005
L
2878 bed = get_elf_backend_data (abfd);
2879 spec = bed->special_sections;
2880 if (spec)
2881 {
2882 spec = _bfd_elf_get_special_section (sec->name,
2883 bed->special_sections,
2884 sec->use_rela_p);
2885 if (spec != NULL)
2886 return spec;
2887 }
2888
551b43fd
AM
2889 if (sec->name[0] != '.')
2890 return NULL;
2f89ff8d 2891
551b43fd 2892 i = sec->name[1] - 'b';
1b315056 2893 if (i < 0 || i > 'z' - 'b')
551b43fd
AM
2894 return NULL;
2895
2896 spec = special_sections[i];
2f89ff8d 2897
551b43fd
AM
2898 if (spec == NULL)
2899 return NULL;
2900
2901 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2f89ff8d
L
2902}
2903
b34976b6 2904bfd_boolean
217aa764 2905_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
252b5132
RH
2906{
2907 struct bfd_elf_section_data *sdata;
551b43fd 2908 const struct elf_backend_data *bed;
7dcb9820 2909 const struct bfd_elf_special_section *ssect;
252b5132 2910
f0abc2a1
AM
2911 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2912 if (sdata == NULL)
2913 {
a50b1753 2914 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
07d6d2b8 2915 sizeof (*sdata));
f0abc2a1
AM
2916 if (sdata == NULL)
2917 return FALSE;
217aa764 2918 sec->used_by_bfd = sdata;
f0abc2a1 2919 }
bf572ba0 2920
551b43fd
AM
2921 /* Indicate whether or not this section should use RELA relocations. */
2922 bed = get_elf_backend_data (abfd);
2923 sec->use_rela_p = bed->default_use_rela_p;
2924
8c803a2d
AM
2925 /* Set up ELF section type and flags for newly created sections, if
2926 there is an ABI mandated section. */
2927 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2928 if (ssect != NULL)
2f89ff8d 2929 {
8c803a2d
AM
2930 elf_section_type (sec) = ssect->type;
2931 elf_section_flags (sec) = ssect->attr;
2f89ff8d
L
2932 }
2933
f592407e 2934 return _bfd_generic_new_section_hook (abfd, sec);
252b5132
RH
2935}
2936
2937/* Create a new bfd section from an ELF program header.
2938
2939 Since program segments have no names, we generate a synthetic name
2940 of the form segment<NUM>, where NUM is generally the index in the
2941 program header table. For segments that are split (see below) we
2942 generate the names segment<NUM>a and segment<NUM>b.
2943
2944 Note that some program segments may have a file size that is different than
2945 (less than) the memory size. All this means is that at execution the
2946 system must allocate the amount of memory specified by the memory size,
2947 but only initialize it with the first "file size" bytes read from the
2948 file. This would occur for example, with program segments consisting
2949 of combined data+bss.
2950
2951 To handle the above situation, this routine generates TWO bfd sections
2952 for the single program segment. The first has the length specified by
2953 the file size of the segment, and the second has the length specified
2954 by the difference between the two sizes. In effect, the segment is split
d5191d0c 2955 into its initialized and uninitialized parts.
252b5132
RH
2956
2957 */
2958
b34976b6 2959bfd_boolean
217aa764
AM
2960_bfd_elf_make_section_from_phdr (bfd *abfd,
2961 Elf_Internal_Phdr *hdr,
91d6fa6a 2962 int hdr_index,
a50b1753 2963 const char *type_name)
252b5132
RH
2964{
2965 asection *newsect;
2966 char *name;
2967 char namebuf[64];
d4c88bbb 2968 size_t len;
252b5132 2969 int split;
502794d4 2970 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132
RH
2971
2972 split = ((hdr->p_memsz > 0)
2973 && (hdr->p_filesz > 0)
2974 && (hdr->p_memsz > hdr->p_filesz));
d5191d0c
AM
2975
2976 if (hdr->p_filesz > 0)
252b5132 2977 {
91d6fa6a 2978 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
d5191d0c 2979 len = strlen (namebuf) + 1;
a50b1753 2980 name = (char *) bfd_alloc (abfd, len);
d5191d0c
AM
2981 if (!name)
2982 return FALSE;
2983 memcpy (name, namebuf, len);
2984 newsect = bfd_make_section (abfd, name);
2985 if (newsect == NULL)
2986 return FALSE;
502794d4
CE
2987 newsect->vma = hdr->p_vaddr / opb;
2988 newsect->lma = hdr->p_paddr / opb;
d5191d0c
AM
2989 newsect->size = hdr->p_filesz;
2990 newsect->filepos = hdr->p_offset;
2991 newsect->flags |= SEC_HAS_CONTENTS;
2992 newsect->alignment_power = bfd_log2 (hdr->p_align);
2993 if (hdr->p_type == PT_LOAD)
252b5132 2994 {
d5191d0c
AM
2995 newsect->flags |= SEC_ALLOC;
2996 newsect->flags |= SEC_LOAD;
2997 if (hdr->p_flags & PF_X)
2998 {
2999 /* FIXME: all we known is that it has execute PERMISSION,
3000 may be data. */
3001 newsect->flags |= SEC_CODE;
3002 }
3003 }
3004 if (!(hdr->p_flags & PF_W))
3005 {
3006 newsect->flags |= SEC_READONLY;
252b5132 3007 }
252b5132
RH
3008 }
3009
d5191d0c 3010 if (hdr->p_memsz > hdr->p_filesz)
252b5132 3011 {
d5191d0c
AM
3012 bfd_vma align;
3013
91d6fa6a 3014 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
d4c88bbb 3015 len = strlen (namebuf) + 1;
a50b1753 3016 name = (char *) bfd_alloc (abfd, len);
252b5132 3017 if (!name)
b34976b6 3018 return FALSE;
d4c88bbb 3019 memcpy (name, namebuf, len);
252b5132
RH
3020 newsect = bfd_make_section (abfd, name);
3021 if (newsect == NULL)
b34976b6 3022 return FALSE;
502794d4
CE
3023 newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
3024 newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
eea6121a 3025 newsect->size = hdr->p_memsz - hdr->p_filesz;
d5191d0c
AM
3026 newsect->filepos = hdr->p_offset + hdr->p_filesz;
3027 align = newsect->vma & -newsect->vma;
3028 if (align == 0 || align > hdr->p_align)
3029 align = hdr->p_align;
3030 newsect->alignment_power = bfd_log2 (align);
252b5132
RH
3031 if (hdr->p_type == PT_LOAD)
3032 {
d5191d0c
AM
3033 /* Hack for gdb. Segments that have not been modified do
3034 not have their contents written to a core file, on the
3035 assumption that a debugger can find the contents in the
3036 executable. We flag this case by setting the fake
3037 section size to zero. Note that "real" bss sections will
3038 always have their contents dumped to the core file. */
3039 if (bfd_get_format (abfd) == bfd_core)
3040 newsect->size = 0;
252b5132
RH
3041 newsect->flags |= SEC_ALLOC;
3042 if (hdr->p_flags & PF_X)
3043 newsect->flags |= SEC_CODE;
3044 }
3045 if (!(hdr->p_flags & PF_W))
3046 newsect->flags |= SEC_READONLY;
3047 }
3048
b34976b6 3049 return TRUE;
252b5132
RH
3050}
3051
864619bb
KS
3052static bfd_boolean
3053_bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3054{
3055 /* The return value is ignored. Build-ids are considered optional. */
3056 if (templ->xvec->flavour == bfd_target_elf_flavour)
3057 return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3058 (templ, offset);
3059 return FALSE;
3060}
3061
b34976b6 3062bfd_boolean
91d6fa6a 3063bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
20cfcaae 3064{
9c5bfbb7 3065 const struct elf_backend_data *bed;
20cfcaae
NC
3066
3067 switch (hdr->p_type)
3068 {
3069 case PT_NULL:
91d6fa6a 3070 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
20cfcaae
NC
3071
3072 case PT_LOAD:
864619bb
KS
3073 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3074 return FALSE;
3075 if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3076 _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3077 return TRUE;
20cfcaae
NC
3078
3079 case PT_DYNAMIC:
91d6fa6a 3080 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
20cfcaae
NC
3081
3082 case PT_INTERP:
91d6fa6a 3083 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
20cfcaae
NC
3084
3085 case PT_NOTE:
91d6fa6a 3086 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
b34976b6 3087 return FALSE;
276da9b3
L
3088 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3089 hdr->p_align))
b34976b6
AM
3090 return FALSE;
3091 return TRUE;
20cfcaae
NC
3092
3093 case PT_SHLIB:
91d6fa6a 3094 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
20cfcaae
NC
3095
3096 case PT_PHDR:
91d6fa6a 3097 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
20cfcaae 3098
811072d8 3099 case PT_GNU_EH_FRAME:
91d6fa6a 3100 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
811072d8
RM
3101 "eh_frame_hdr");
3102
2b05f1b7 3103 case PT_GNU_STACK:
91d6fa6a 3104 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
9ee5e499 3105
8c37241b 3106 case PT_GNU_RELRO:
91d6fa6a 3107 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
8c37241b 3108
20cfcaae 3109 default:
8c1acd09 3110 /* Check for any processor-specific program segment types. */
20cfcaae 3111 bed = get_elf_backend_data (abfd);
91d6fa6a 3112 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
20cfcaae
NC
3113 }
3114}
3115
d4730f92
BS
3116/* Return the REL_HDR for SEC, assuming there is only a single one, either
3117 REL or RELA. */
3118
3119Elf_Internal_Shdr *
3120_bfd_elf_single_rel_hdr (asection *sec)
3121{
3122 if (elf_section_data (sec)->rel.hdr)
3123 {
3124 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3125 return elf_section_data (sec)->rel.hdr;
3126 }
3127 else
3128 return elf_section_data (sec)->rela.hdr;
3129}
3130
3e19fb8f
L
3131static bfd_boolean
3132_bfd_elf_set_reloc_sh_name (bfd *abfd,
3133 Elf_Internal_Shdr *rel_hdr,
3134 const char *sec_name,
3135 bfd_boolean use_rela_p)
3136{
3137 char *name = (char *) bfd_alloc (abfd,
3138 sizeof ".rela" + strlen (sec_name));
3139 if (name == NULL)
3140 return FALSE;
3141
3142 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3143 rel_hdr->sh_name =
3144 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3145 FALSE);
3146 if (rel_hdr->sh_name == (unsigned int) -1)
3147 return FALSE;
3148
3149 return TRUE;
3150}
3151
d4730f92
BS
3152/* Allocate and initialize a section-header for a new reloc section,
3153 containing relocations against ASECT. It is stored in RELDATA. If
3154 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3155 relocations. */
23bc299b 3156
5d13b3b3 3157static bfd_boolean
217aa764 3158_bfd_elf_init_reloc_shdr (bfd *abfd,
d4730f92 3159 struct bfd_elf_section_reloc_data *reldata,
f6fe1ccd 3160 const char *sec_name,
3e19fb8f
L
3161 bfd_boolean use_rela_p,
3162 bfd_boolean delay_st_name_p)
23bc299b 3163{
d4730f92 3164 Elf_Internal_Shdr *rel_hdr;
9c5bfbb7 3165 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 3166
d4730f92 3167 BFD_ASSERT (reldata->hdr == NULL);
ef53be89 3168 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
d4730f92 3169 reldata->hdr = rel_hdr;
23bc299b 3170
3e19fb8f
L
3171 if (delay_st_name_p)
3172 rel_hdr->sh_name = (unsigned int) -1;
3173 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3174 use_rela_p))
b34976b6 3175 return FALSE;
23bc299b
MM
3176 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3177 rel_hdr->sh_entsize = (use_rela_p
3178 ? bed->s->sizeof_rela
3179 : bed->s->sizeof_rel);
72de5009 3180 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
28e07a05 3181 rel_hdr->sh_flags = 0;
23bc299b
MM
3182 rel_hdr->sh_addr = 0;
3183 rel_hdr->sh_size = 0;
3184 rel_hdr->sh_offset = 0;
3185
b34976b6 3186 return TRUE;
23bc299b
MM
3187}
3188
94be91de
JB
3189/* Return the default section type based on the passed in section flags. */
3190
3191int
3192bfd_elf_get_default_section_type (flagword flags)
3193{
0e41bebb 3194 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
2e76e85a 3195 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
94be91de
JB
3196 return SHT_NOBITS;
3197 return SHT_PROGBITS;
3198}
3199
d4730f92
BS
3200struct fake_section_arg
3201{
3202 struct bfd_link_info *link_info;
3203 bfd_boolean failed;
3204};
3205
252b5132
RH
3206/* Set up an ELF internal section header for a section. */
3207
252b5132 3208static void
d4730f92 3209elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
252b5132 3210{
d4730f92 3211 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
9c5bfbb7 3212 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 3213 struct bfd_elf_section_data *esd = elf_section_data (asect);
252b5132 3214 Elf_Internal_Shdr *this_hdr;
0414f35b 3215 unsigned int sh_type;
0ce398f1 3216 const char *name = asect->name;
3e19fb8f 3217 bfd_boolean delay_st_name_p = FALSE;
233bf4f8 3218 bfd_vma mask;
252b5132 3219
d4730f92 3220 if (arg->failed)
252b5132
RH
3221 {
3222 /* We already failed; just get out of the bfd_map_over_sections
08a40648 3223 loop. */
252b5132
RH
3224 return;
3225 }
3226
d4730f92 3227 this_hdr = &esd->this_hdr;
252b5132 3228
f6fe1ccd 3229 if (arg->link_info)
0ce398f1 3230 {
f6fe1ccd
L
3231 /* ld: compress DWARF debug sections with names: .debug_*. */
3232 if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
3233 && (asect->flags & SEC_DEBUGGING)
3234 && name[1] == 'd'
3235 && name[6] == '_')
3236 {
3237 /* Set SEC_ELF_COMPRESS to indicate this section should be
3238 compressed. */
3239 asect->flags |= SEC_ELF_COMPRESS;
dd905818 3240 /* If this section will be compressed, delay adding section
3e19fb8f
L
3241 name to section name section after it is compressed in
3242 _bfd_elf_assign_file_positions_for_non_load. */
3243 delay_st_name_p = TRUE;
f6fe1ccd
L
3244 }
3245 }
3246 else if ((asect->flags & SEC_ELF_RENAME))
3247 {
3248 /* objcopy: rename output DWARF debug section. */
3249 if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
3250 {
3251 /* When we decompress or compress with SHF_COMPRESSED,
3252 convert section name from .zdebug_* to .debug_* if
3253 needed. */
3254 if (name[1] == 'z')
3255 {
3256 char *new_name = convert_zdebug_to_debug (abfd, name);
3257 if (new_name == NULL)
3258 {
3259 arg->failed = TRUE;
3260 return;
3261 }
3262 name = new_name;
3263 }
3264 }
3265 else if (asect->compress_status == COMPRESS_SECTION_DONE)
0ce398f1 3266 {
f6fe1ccd
L
3267 /* PR binutils/18087: Compression does not always make a
3268 section smaller. So only rename the section when
3269 compression has actually taken place. If input section
3270 name is .zdebug_*, we should never compress it again. */
3271 char *new_name = convert_debug_to_zdebug (abfd, name);
0ce398f1
L
3272 if (new_name == NULL)
3273 {
3274 arg->failed = TRUE;
3275 return;
3276 }
f6fe1ccd
L
3277 BFD_ASSERT (name[1] != 'z');
3278 name = new_name;
0ce398f1
L
3279 }
3280 }
3281
3e19fb8f
L
3282 if (delay_st_name_p)
3283 this_hdr->sh_name = (unsigned int) -1;
3284 else
252b5132 3285 {
3e19fb8f
L
3286 this_hdr->sh_name
3287 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3288 name, FALSE);
3289 if (this_hdr->sh_name == (unsigned int) -1)
3290 {
3291 arg->failed = TRUE;
3292 return;
3293 }
252b5132
RH
3294 }
3295
a4d8e49b 3296 /* Don't clear sh_flags. Assembler may set additional bits. */
252b5132
RH
3297
3298 if ((asect->flags & SEC_ALLOC) != 0
3299 || asect->user_set_vma)
502794d4 3300 this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
252b5132
RH
3301 else
3302 this_hdr->sh_addr = 0;
3303
3304 this_hdr->sh_offset = 0;
eea6121a 3305 this_hdr->sh_size = asect->size;
252b5132 3306 this_hdr->sh_link = 0;
c86934ce
NC
3307 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3308 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3309 {
4eca0228 3310 _bfd_error_handler
695344c0 3311 /* xgettext:c-format */
9793eb77 3312 (_("%pB: error: alignment power %d of section `%pA' is too big"),
c08bb8dd 3313 abfd, asect->alignment_power, asect);
c86934ce
NC
3314 arg->failed = TRUE;
3315 return;
3316 }
233bf4f8
AM
3317 /* Set sh_addralign to the highest power of two given by alignment
3318 consistent with the section VMA. Linker scripts can force VMA. */
3319 mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3320 this_hdr->sh_addralign = mask & -mask;
252b5132
RH
3321 /* The sh_entsize and sh_info fields may have been set already by
3322 copy_private_section_data. */
3323
3324 this_hdr->bfd_section = asect;
3325 this_hdr->contents = NULL;
3326
3cddba1e
L
3327 /* If the section type is unspecified, we set it based on
3328 asect->flags. */
98ece1b3
AM
3329 if ((asect->flags & SEC_GROUP) != 0)
3330 sh_type = SHT_GROUP;
98ece1b3 3331 else
94be91de 3332 sh_type = bfd_elf_get_default_section_type (asect->flags);
98ece1b3 3333
3cddba1e 3334 if (this_hdr->sh_type == SHT_NULL)
98ece1b3
AM
3335 this_hdr->sh_type = sh_type;
3336 else if (this_hdr->sh_type == SHT_NOBITS
3337 && sh_type == SHT_PROGBITS
3338 && (asect->flags & SEC_ALLOC) != 0)
3cddba1e 3339 {
98ece1b3
AM
3340 /* Warn if we are changing a NOBITS section to PROGBITS, but
3341 allow the link to proceed. This can happen when users link
3342 non-bss input sections to bss output sections, or emit data
3343 to a bss output section via a linker script. */
4eca0228 3344 _bfd_error_handler
871b3ab2 3345 (_("warning: section `%pA' type changed to PROGBITS"), asect);
98ece1b3 3346 this_hdr->sh_type = sh_type;
3cddba1e
L
3347 }
3348
2f89ff8d 3349 switch (this_hdr->sh_type)
252b5132 3350 {
2f89ff8d 3351 default:
2f89ff8d
L
3352 break;
3353
3354 case SHT_STRTAB:
2f89ff8d
L
3355 case SHT_NOTE:
3356 case SHT_NOBITS:
3357 case SHT_PROGBITS:
3358 break;
606851fb
AM
3359
3360 case SHT_INIT_ARRAY:
3361 case SHT_FINI_ARRAY:
3362 case SHT_PREINIT_ARRAY:
3363 this_hdr->sh_entsize = bed->s->arch_size / 8;
3364 break;
2f89ff8d
L
3365
3366 case SHT_HASH:
c7ac6ff8 3367 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2f89ff8d 3368 break;
5de3bf90 3369
2f89ff8d 3370 case SHT_DYNSYM:
252b5132 3371 this_hdr->sh_entsize = bed->s->sizeof_sym;
2f89ff8d
L
3372 break;
3373
3374 case SHT_DYNAMIC:
252b5132 3375 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2f89ff8d
L
3376 break;
3377
3378 case SHT_RELA:
3379 if (get_elf_backend_data (abfd)->may_use_rela_p)
3380 this_hdr->sh_entsize = bed->s->sizeof_rela;
3381 break;
3382
3383 case SHT_REL:
3384 if (get_elf_backend_data (abfd)->may_use_rel_p)
3385 this_hdr->sh_entsize = bed->s->sizeof_rel;
3386 break;
3387
3388 case SHT_GNU_versym:
252b5132 3389 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2f89ff8d
L
3390 break;
3391
3392 case SHT_GNU_verdef:
252b5132
RH
3393 this_hdr->sh_entsize = 0;
3394 /* objcopy or strip will copy over sh_info, but may not set
08a40648
AM
3395 cverdefs. The linker will set cverdefs, but sh_info will be
3396 zero. */
252b5132
RH
3397 if (this_hdr->sh_info == 0)
3398 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3399 else
3400 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3401 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2f89ff8d
L
3402 break;
3403
3404 case SHT_GNU_verneed:
252b5132
RH
3405 this_hdr->sh_entsize = 0;
3406 /* objcopy or strip will copy over sh_info, but may not set
08a40648
AM
3407 cverrefs. The linker will set cverrefs, but sh_info will be
3408 zero. */
252b5132
RH
3409 if (this_hdr->sh_info == 0)
3410 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3411 else
3412 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3413 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2f89ff8d
L
3414 break;
3415
3416 case SHT_GROUP:
1783205a 3417 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2f89ff8d 3418 break;
fdc90cb4
JJ
3419
3420 case SHT_GNU_HASH:
3421 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3422 break;
dbb410c3 3423 }
252b5132
RH
3424
3425 if ((asect->flags & SEC_ALLOC) != 0)
3426 this_hdr->sh_flags |= SHF_ALLOC;
3427 if ((asect->flags & SEC_READONLY) == 0)
3428 this_hdr->sh_flags |= SHF_WRITE;
3429 if ((asect->flags & SEC_CODE) != 0)
3430 this_hdr->sh_flags |= SHF_EXECINSTR;
f5fa8ca2
JJ
3431 if ((asect->flags & SEC_MERGE) != 0)
3432 {
3433 this_hdr->sh_flags |= SHF_MERGE;
3434 this_hdr->sh_entsize = asect->entsize;
f5fa8ca2 3435 }
84865015
NC
3436 if ((asect->flags & SEC_STRINGS) != 0)
3437 this_hdr->sh_flags |= SHF_STRINGS;
1126897b 3438 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
dbb410c3 3439 this_hdr->sh_flags |= SHF_GROUP;
13ae64f3 3440 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
704afa60
JJ
3441 {
3442 this_hdr->sh_flags |= SHF_TLS;
3a800eb9
AM
3443 if (asect->size == 0
3444 && (asect->flags & SEC_HAS_CONTENTS) == 0)
704afa60 3445 {
3a800eb9 3446 struct bfd_link_order *o = asect->map_tail.link_order;
b34976b6 3447
704afa60 3448 this_hdr->sh_size = 0;
3a800eb9
AM
3449 if (o != NULL)
3450 {
704afa60 3451 this_hdr->sh_size = o->offset + o->size;
3a800eb9
AM
3452 if (this_hdr->sh_size != 0)
3453 this_hdr->sh_type = SHT_NOBITS;
3454 }
704afa60
JJ
3455 }
3456 }
18ae9cc1
L
3457 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3458 this_hdr->sh_flags |= SHF_EXCLUDE;
252b5132 3459
d4730f92
BS
3460 /* If the section has relocs, set up a section header for the
3461 SHT_REL[A] section. If two relocation sections are required for
3462 this section, it is up to the processor-specific back-end to
3463 create the other. */
3464 if ((asect->flags & SEC_RELOC) != 0)
3465 {
3466 /* When doing a relocatable link, create both REL and RELA sections if
3467 needed. */
3468 if (arg->link_info
3469 /* Do the normal setup if we wouldn't create any sections here. */
3470 && esd->rel.count + esd->rela.count > 0
0e1862bb
L
3471 && (bfd_link_relocatable (arg->link_info)
3472 || arg->link_info->emitrelocations))
d4730f92
BS
3473 {
3474 if (esd->rel.count && esd->rel.hdr == NULL
28e07a05 3475 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
db4677b8 3476 FALSE, delay_st_name_p))
d4730f92
BS
3477 {
3478 arg->failed = TRUE;
3479 return;
3480 }
3481 if (esd->rela.count && esd->rela.hdr == NULL
28e07a05 3482 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
db4677b8 3483 TRUE, delay_st_name_p))
d4730f92
BS
3484 {
3485 arg->failed = TRUE;
3486 return;
3487 }
3488 }
3489 else if (!_bfd_elf_init_reloc_shdr (abfd,
3490 (asect->use_rela_p
3491 ? &esd->rela : &esd->rel),
f6fe1ccd 3492 name,
3e19fb8f
L
3493 asect->use_rela_p,
3494 delay_st_name_p))
db4677b8 3495 {
d4730f92 3496 arg->failed = TRUE;
db4677b8
AM
3497 return;
3498 }
d4730f92
BS
3499 }
3500
252b5132 3501 /* Check for processor-specific section types. */
0414f35b 3502 sh_type = this_hdr->sh_type;
e1fddb6b
AO
3503 if (bed->elf_backend_fake_sections
3504 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
db4677b8
AM
3505 {
3506 arg->failed = TRUE;
3507 return;
3508 }
252b5132 3509
42bb2e33 3510 if (sh_type == SHT_NOBITS && asect->size != 0)
0414f35b
AM
3511 {
3512 /* Don't change the header type from NOBITS if we are being
42bb2e33 3513 called for objcopy --only-keep-debug. */
0414f35b
AM
3514 this_hdr->sh_type = sh_type;
3515 }
252b5132
RH
3516}
3517
bcacc0f5
AM
3518/* Fill in the contents of a SHT_GROUP section. Called from
3519 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3520 when ELF targets use the generic linker, ld. Called for ld -r
3521 from bfd_elf_final_link. */
dbb410c3 3522
1126897b 3523void
217aa764 3524bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
dbb410c3 3525{
a50b1753 3526 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
9dce4196 3527 asection *elt, *first;
dbb410c3 3528 unsigned char *loc;
b34976b6 3529 bfd_boolean gas;
dbb410c3 3530
7e4111ad
L
3531 /* Ignore linker created group section. See elfNN_ia64_object_p in
3532 elfxx-ia64.c. */
ce5aecf8
AM
3533 if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3534 || sec->size == 0
dbb410c3
AM
3535 || *failedptr)
3536 return;
3537
bcacc0f5
AM
3538 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3539 {
3540 unsigned long symindx = 0;
3541
3542 /* elf_group_id will have been set up by objcopy and the
3543 generic linker. */
3544 if (elf_group_id (sec) != NULL)
3545 symindx = elf_group_id (sec)->udata.i;
1126897b 3546
bcacc0f5
AM
3547 if (symindx == 0)
3548 {
3549 /* If called from the assembler, swap_out_syms will have set up
6a541707
NC
3550 elf_section_syms.
3551 PR 25699: A corrupt input file could contain bogus group info. */
3552 if (elf_section_syms (abfd) == NULL)
3553 {
3554 *failedptr = TRUE;
3555 return;
3556 }
bcacc0f5
AM
3557 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3558 }
3559 elf_section_data (sec)->this_hdr.sh_info = symindx;
3560 }
3561 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
1126897b 3562 {
bcacc0f5
AM
3563 /* The ELF backend linker sets sh_info to -2 when the group
3564 signature symbol is global, and thus the index can't be
3565 set until all local symbols are output. */
53720c49
AM
3566 asection *igroup;
3567 struct bfd_elf_section_data *sec_data;
3568 unsigned long symndx;
3569 unsigned long extsymoff;
bcacc0f5
AM
3570 struct elf_link_hash_entry *h;
3571
53720c49
AM
3572 /* The point of this little dance to the first SHF_GROUP section
3573 then back to the SHT_GROUP section is that this gets us to
3574 the SHT_GROUP in the input object. */
3575 igroup = elf_sec_group (elf_next_in_group (sec));
3576 sec_data = elf_section_data (igroup);
3577 symndx = sec_data->this_hdr.sh_info;
3578 extsymoff = 0;
bcacc0f5
AM
3579 if (!elf_bad_symtab (igroup->owner))
3580 {
3581 Elf_Internal_Shdr *symtab_hdr;
3582
3583 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3584 extsymoff = symtab_hdr->sh_info;
3585 }
3586 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3587 while (h->root.type == bfd_link_hash_indirect
3588 || h->root.type == bfd_link_hash_warning)
3589 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3590
3591 elf_section_data (sec)->this_hdr.sh_info = h->indx;
1126897b 3592 }
dbb410c3 3593
1126897b 3594 /* The contents won't be allocated for "ld -r" or objcopy. */
b34976b6 3595 gas = TRUE;
dbb410c3
AM
3596 if (sec->contents == NULL)
3597 {
b34976b6 3598 gas = FALSE;
a50b1753 3599 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
9dce4196
AM
3600
3601 /* Arrange for the section to be written out. */
3602 elf_section_data (sec)->this_hdr.contents = sec->contents;
dbb410c3
AM
3603 if (sec->contents == NULL)
3604 {
b34976b6 3605 *failedptr = TRUE;
dbb410c3
AM
3606 return;
3607 }
3608 }
3609
eea6121a 3610 loc = sec->contents + sec->size;
dbb410c3 3611
9dce4196
AM
3612 /* Get the pointer to the first section in the group that gas
3613 squirreled away here. objcopy arranges for this to be set to the
3614 start of the input section group. */
3615 first = elt = elf_next_in_group (sec);
dbb410c3
AM
3616
3617 /* First element is a flag word. Rest of section is elf section
3618 indices for all the sections of the group. Write them backwards
3619 just to keep the group in the same order as given in .section
3620 directives, not that it matters. */
3621 while (elt != NULL)
3622 {
9dce4196 3623 asection *s;
9dce4196 3624
9dce4196 3625 s = elt;
415f38a6
AM
3626 if (!gas)
3627 s = s->output_section;
3628 if (s != NULL
3629 && !bfd_is_abs_section (s))
01e1a5bc 3630 {
db4677b8 3631 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
28e07a05
AM
3632 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3633
3634 if (elf_sec->rel.hdr != NULL
3635 && (gas
3636 || (input_elf_sec->rel.hdr != NULL
3637 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
db4677b8 3638 {
28e07a05 3639 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
db4677b8
AM
3640 loc -= 4;
3641 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3642 }
28e07a05
AM
3643 if (elf_sec->rela.hdr != NULL
3644 && (gas
3645 || (input_elf_sec->rela.hdr != NULL
3646 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
db4677b8 3647 {
28e07a05 3648 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
db4677b8
AM
3649 loc -= 4;
3650 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3651 }
01e1a5bc 3652 loc -= 4;
db4677b8 3653 H_PUT_32 (abfd, elf_sec->this_idx, loc);
01e1a5bc 3654 }
945906ff 3655 elt = elf_next_in_group (elt);
9dce4196
AM
3656 if (elt == first)
3657 break;
dbb410c3
AM
3658 }
3659
7bdf4127
AB
3660 loc -= 4;
3661 BFD_ASSERT (loc == sec->contents);
dbb410c3 3662
9dce4196 3663 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
dbb410c3
AM
3664}
3665
bce964aa
AM
3666/* Given NAME, the name of a relocation section stripped of its
3667 .rel/.rela prefix, return the section in ABFD to which the
3668 relocations apply. */
bd53a53a
L
3669
3670asection *
bce964aa
AM
3671_bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3672{
3673 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3674 section likely apply to .got.plt or .got section. */
3675 if (get_elf_backend_data (abfd)->want_got_plt
3676 && strcmp (name, ".plt") == 0)
3677 {
3678 asection *sec;
3679
3680 name = ".got.plt";
3681 sec = bfd_get_section_by_name (abfd, name);
3682 if (sec != NULL)
3683 return sec;
3684 name = ".got";
3685 }
3686
3687 return bfd_get_section_by_name (abfd, name);
3688}
3689
3690/* Return the section to which RELOC_SEC applies. */
3691
3692static asection *
3693elf_get_reloc_section (asection *reloc_sec)
bd53a53a
L
3694{
3695 const char *name;
3696 unsigned int type;
3697 bfd *abfd;
bce964aa 3698 const struct elf_backend_data *bed;
bd53a53a
L
3699
3700 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3701 if (type != SHT_REL && type != SHT_RELA)
3702 return NULL;
3703
3704 /* We look up the section the relocs apply to by name. */
3705 name = reloc_sec->name;
bce964aa
AM
3706 if (strncmp (name, ".rel", 4) != 0)
3707 return NULL;
3708 name += 4;
3709 if (type == SHT_RELA && *name++ != 'a')
3710 return NULL;
bd53a53a 3711
bd53a53a 3712 abfd = reloc_sec->owner;
bce964aa
AM
3713 bed = get_elf_backend_data (abfd);
3714 return bed->get_reloc_section (abfd, name);
bd53a53a
L
3715}
3716
252b5132
RH
3717/* Assign all ELF section numbers. The dummy first section is handled here
3718 too. The link/info pointers for the standard section types are filled
3719 in here too, while we're at it. */
3720
b34976b6 3721static bfd_boolean
da9f89d4 3722assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
252b5132
RH
3723{
3724 struct elf_obj_tdata *t = elf_tdata (abfd);
3725 asection *sec;
3e19fb8f 3726 unsigned int section_number;
252b5132 3727 Elf_Internal_Shdr **i_shdrp;
47cc2cf5 3728 struct bfd_elf_section_data *d;
3516e984 3729 bfd_boolean need_symtab;
446f7ed5 3730 size_t amt;
252b5132
RH
3731
3732 section_number = 1;
3733
2b0f7ef9
JJ
3734 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3735
da9f89d4 3736 /* SHT_GROUP sections are in relocatable files only. */
7bdf4127 3737 if (link_info == NULL || !link_info->resolve_section_groups)
252b5132 3738 {
ef53be89 3739 size_t reloc_count = 0;
14f2c699 3740
da9f89d4 3741 /* Put SHT_GROUP sections first. */
04dd1667 3742 for (sec = abfd->sections; sec != NULL; sec = sec->next)
47cc2cf5 3743 {
5daa8fe7 3744 d = elf_section_data (sec);
da9f89d4
L
3745
3746 if (d->this_hdr.sh_type == SHT_GROUP)
08a40648 3747 {
5daa8fe7 3748 if (sec->flags & SEC_LINKER_CREATED)
da9f89d4
L
3749 {
3750 /* Remove the linker created SHT_GROUP sections. */
5daa8fe7 3751 bfd_section_list_remove (abfd, sec);
da9f89d4 3752 abfd->section_count--;
da9f89d4 3753 }
08a40648 3754 else
4fbb74a6 3755 d->this_idx = section_number++;
da9f89d4 3756 }
14f2c699
L
3757
3758 /* Count relocations. */
3759 reloc_count += sec->reloc_count;
47cc2cf5 3760 }
14f2c699
L
3761
3762 /* Clear HAS_RELOC if there are no relocations. */
3763 if (reloc_count == 0)
3764 abfd->flags &= ~HAS_RELOC;
47cc2cf5
PB
3765 }
3766
3767 for (sec = abfd->sections; sec; sec = sec->next)
3768 {
3769 d = elf_section_data (sec);
3770
3771 if (d->this_hdr.sh_type != SHT_GROUP)
4fbb74a6 3772 d->this_idx = section_number++;
3e19fb8f
L
3773 if (d->this_hdr.sh_name != (unsigned int) -1)
3774 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
d4730f92 3775 if (d->rel.hdr)
2b0f7ef9 3776 {
d4730f92 3777 d->rel.idx = section_number++;
3e19fb8f
L
3778 if (d->rel.hdr->sh_name != (unsigned int) -1)
3779 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
2b0f7ef9 3780 }
d4730f92
BS
3781 else
3782 d->rel.idx = 0;
23bc299b 3783
d4730f92 3784 if (d->rela.hdr)
2b0f7ef9 3785 {
d4730f92 3786 d->rela.idx = section_number++;
3e19fb8f
L
3787 if (d->rela.hdr->sh_name != (unsigned int) -1)
3788 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
2b0f7ef9 3789 }
23bc299b 3790 else
d4730f92 3791 d->rela.idx = 0;
252b5132
RH
3792 }
3793
3516e984
L
3794 need_symtab = (bfd_get_symcount (abfd) > 0
3795 || (link_info == NULL
3796 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3797 == HAS_RELOC)));
3798 if (need_symtab)
252b5132 3799 {
12bd6957 3800 elf_onesymtab (abfd) = section_number++;
2b0f7ef9 3801 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4fbb74a6 3802 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
9ad5cbcf 3803 {
7a6e0d89 3804 elf_section_list *entry;
6a40cf0c
NC
3805
3806 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
3807
7a6e0d89 3808 entry = bfd_zalloc (abfd, sizeof (*entry));
6a40cf0c
NC
3809 entry->ndx = section_number++;
3810 elf_symtab_shndx_list (abfd) = entry;
3811 entry->hdr.sh_name
9ad5cbcf 3812 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
b34976b6 3813 ".symtab_shndx", FALSE);
6a40cf0c 3814 if (entry->hdr.sh_name == (unsigned int) -1)
b34976b6 3815 return FALSE;
9ad5cbcf 3816 }
12bd6957 3817 elf_strtab_sec (abfd) = section_number++;
2b0f7ef9 3818 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
252b5132
RH
3819 }
3820
dd905818
NC
3821 elf_shstrtab_sec (abfd) = section_number++;
3822 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3823 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3824
1c52a645
L
3825 if (section_number >= SHN_LORESERVE)
3826 {
695344c0 3827 /* xgettext:c-format */
871b3ab2 3828 _bfd_error_handler (_("%pB: too many sections: %u"),
1c52a645
L
3829 abfd, section_number);
3830 return FALSE;
3831 }
3832
9ad5cbcf 3833 elf_numsections (abfd) = section_number;
252b5132
RH
3834 elf_elfheader (abfd)->e_shnum = section_number;
3835
3836 /* Set up the list of section header pointers, in agreement with the
3837 indices. */
446f7ed5
AM
3838 amt = section_number * sizeof (Elf_Internal_Shdr *);
3839 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
252b5132 3840 if (i_shdrp == NULL)
b34976b6 3841 return FALSE;
252b5132 3842
a50b1753 3843 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
07d6d2b8 3844 sizeof (Elf_Internal_Shdr));
252b5132
RH
3845 if (i_shdrp[0] == NULL)
3846 {
3847 bfd_release (abfd, i_shdrp);
b34976b6 3848 return FALSE;
252b5132 3849 }
252b5132
RH
3850
3851 elf_elfsections (abfd) = i_shdrp;
3852
12bd6957 3853 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3516e984 3854 if (need_symtab)
252b5132 3855 {
12bd6957 3856 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4fbb74a6 3857 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
9ad5cbcf 3858 {
6a40cf0c
NC
3859 elf_section_list * entry = elf_symtab_shndx_list (abfd);
3860 BFD_ASSERT (entry != NULL);
3861 i_shdrp[entry->ndx] = & entry->hdr;
3862 entry->hdr.sh_link = elf_onesymtab (abfd);
9ad5cbcf 3863 }
12bd6957
AM
3864 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3865 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
252b5132 3866 }
38ce5b11 3867
252b5132
RH
3868 for (sec = abfd->sections; sec; sec = sec->next)
3869 {
252b5132 3870 asection *s;
252b5132 3871
91d6fa6a
NC
3872 d = elf_section_data (sec);
3873
252b5132 3874 i_shdrp[d->this_idx] = &d->this_hdr;
d4730f92
BS
3875 if (d->rel.idx != 0)
3876 i_shdrp[d->rel.idx] = d->rel.hdr;
3877 if (d->rela.idx != 0)
3878 i_shdrp[d->rela.idx] = d->rela.hdr;
252b5132
RH
3879
3880 /* Fill in the sh_link and sh_info fields while we're at it. */
3881
3882 /* sh_link of a reloc section is the section index of the symbol
3883 table. sh_info is the section index of the section to which
3884 the relocation entries apply. */
d4730f92 3885 if (d->rel.idx != 0)
252b5132 3886 {
12bd6957 3887 d->rel.hdr->sh_link = elf_onesymtab (abfd);
d4730f92 3888 d->rel.hdr->sh_info = d->this_idx;
9ef5d938 3889 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
252b5132 3890 }
d4730f92 3891 if (d->rela.idx != 0)
23bc299b 3892 {
12bd6957 3893 d->rela.hdr->sh_link = elf_onesymtab (abfd);
d4730f92 3894 d->rela.hdr->sh_info = d->this_idx;
9ef5d938 3895 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
23bc299b 3896 }
252b5132 3897
38ce5b11
L
3898 /* We need to set up sh_link for SHF_LINK_ORDER. */
3899 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3900 {
3901 s = elf_linked_to_section (sec);
3902 if (s)
38ce5b11 3903 {
f2876037 3904 /* elf_linked_to_section points to the input section. */
ccd2ec6a 3905 if (link_info != NULL)
38ce5b11 3906 {
f2876037 3907 /* Check discarded linkonce section. */
dbaa2011 3908 if (discarded_section (s))
38ce5b11 3909 {
ccd2ec6a 3910 asection *kept;
4eca0228 3911 _bfd_error_handler
695344c0 3912 /* xgettext:c-format */
871b3ab2
AM
3913 (_("%pB: sh_link of section `%pA' points to"
3914 " discarded section `%pA' of `%pB'"),
ccd2ec6a
L
3915 abfd, d->this_hdr.bfd_section,
3916 s, s->owner);
3917 /* Point to the kept section if it has the same
3918 size as the discarded one. */
c0f00686 3919 kept = _bfd_elf_check_kept_section (s, link_info);
ccd2ec6a 3920 if (kept == NULL)
185d09ad 3921 {
ccd2ec6a
L
3922 bfd_set_error (bfd_error_bad_value);
3923 return FALSE;
185d09ad 3924 }
ccd2ec6a 3925 s = kept;
38ce5b11 3926 }
e424ecc8 3927
ccd2ec6a
L
3928 s = s->output_section;
3929 BFD_ASSERT (s != NULL);
38ce5b11 3930 }
f2876037
L
3931 else
3932 {
3933 /* Handle objcopy. */
3934 if (s->output_section == NULL)
3935 {
4eca0228 3936 _bfd_error_handler
695344c0 3937 /* xgettext:c-format */
871b3ab2
AM
3938 (_("%pB: sh_link of section `%pA' points to"
3939 " removed section `%pA' of `%pB'"),
f2876037
L
3940 abfd, d->this_hdr.bfd_section, s, s->owner);
3941 bfd_set_error (bfd_error_bad_value);
3942 return FALSE;
3943 }
3944 s = s->output_section;
3945 }
ccd2ec6a
L
3946 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3947 }
3948 else
3949 {
3950 /* PR 290:
3951 The Intel C compiler generates SHT_IA_64_UNWIND with
3952 SHF_LINK_ORDER. But it doesn't set the sh_link or
3953 sh_info fields. Hence we could get the situation
08a40648 3954 where s is NULL. */
ccd2ec6a
L
3955 const struct elf_backend_data *bed
3956 = get_elf_backend_data (abfd);
a859124d
AM
3957 bed->link_order_error_handler
3958 /* xgettext:c-format */
3959 (_("%pB: warning: sh_link not set for section `%pA'"),
3960 abfd, sec);
38ce5b11
L
3961 }
3962 }
3963
252b5132
RH
3964 switch (d->this_hdr.sh_type)
3965 {
3966 case SHT_REL:
3967 case SHT_RELA:
3968 /* A reloc section which we are treating as a normal BFD
3969 section. sh_link is the section index of the symbol
3970 table. sh_info is the section index of the section to
3971 which the relocation entries apply. We assume that an
3972 allocated reloc section uses the dynamic symbol table.
3973 FIXME: How can we be sure? */
3974 s = bfd_get_section_by_name (abfd, ".dynsym");
3975 if (s != NULL)
3976 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3977
bce964aa 3978 s = elf_get_reloc_section (sec);
252b5132 3979 if (s != NULL)
9ef5d938
L
3980 {
3981 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3982 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3983 }
252b5132
RH
3984 break;
3985
3986 case SHT_STRTAB:
3987 /* We assume that a section named .stab*str is a stabs
3988 string section. We look for a section with the same name
3989 but without the trailing ``str'', and set its sh_link
3990 field to point to this section. */
0112cd26 3991 if (CONST_STRNEQ (sec->name, ".stab")
252b5132
RH
3992 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3993 {
3994 size_t len;
3995 char *alc;
3996
3997 len = strlen (sec->name);
a50b1753 3998 alc = (char *) bfd_malloc (len - 2);
252b5132 3999 if (alc == NULL)
b34976b6 4000 return FALSE;
d4c88bbb 4001 memcpy (alc, sec->name, len - 3);
252b5132
RH
4002 alc[len - 3] = '\0';
4003 s = bfd_get_section_by_name (abfd, alc);
4004 free (alc);
4005 if (s != NULL)
4006 {
4007 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
4008
4009 /* This is a .stab section. */
34ca5531 4010 elf_section_data (s)->this_hdr.sh_entsize = 12;
252b5132
RH
4011 }
4012 }
4013 break;
4014
4015 case SHT_DYNAMIC:
4016 case SHT_DYNSYM:
4017 case SHT_GNU_verneed:
4018 case SHT_GNU_verdef:
4019 /* sh_link is the section header index of the string table
4020 used for the dynamic entries, or the symbol table, or the
4021 version strings. */
4022 s = bfd_get_section_by_name (abfd, ".dynstr");
4023 if (s != NULL)
4024 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4025 break;
4026
7f1204bb
JJ
4027 case SHT_GNU_LIBLIST:
4028 /* sh_link is the section header index of the prelink library
08a40648
AM
4029 list used for the dynamic entries, or the symbol table, or
4030 the version strings. */
7f1204bb
JJ
4031 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
4032 ? ".dynstr" : ".gnu.libstr");
4033 if (s != NULL)
4034 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4035 break;
4036
252b5132 4037 case SHT_HASH:
fdc90cb4 4038 case SHT_GNU_HASH:
252b5132
RH
4039 case SHT_GNU_versym:
4040 /* sh_link is the section header index of the symbol table
4041 this hash table or version table is for. */
4042 s = bfd_get_section_by_name (abfd, ".dynsym");
4043 if (s != NULL)
4044 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4045 break;
dbb410c3
AM
4046
4047 case SHT_GROUP:
12bd6957 4048 d->this_hdr.sh_link = elf_onesymtab (abfd);
252b5132
RH
4049 }
4050 }
4051
3e19fb8f
L
4052 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4053 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4054 debug section name from .debug_* to .zdebug_* if needed. */
4055
b34976b6 4056 return TRUE;
252b5132
RH
4057}
4058
5372391b 4059static bfd_boolean
217aa764 4060sym_is_global (bfd *abfd, asymbol *sym)
252b5132
RH
4061{
4062 /* If the backend has a special mapping, use it. */
9c5bfbb7 4063 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764
AM
4064 if (bed->elf_backend_sym_is_global)
4065 return (*bed->elf_backend_sym_is_global) (abfd, sym);
252b5132 4066
e47bf690 4067 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
e6f7f6d1
AM
4068 || bfd_is_und_section (bfd_asymbol_section (sym))
4069 || bfd_is_com_section (bfd_asymbol_section (sym)));
252b5132
RH
4070}
4071
76359541
TP
4072/* Filter global symbols of ABFD to include in the import library. All
4073 SYMCOUNT symbols of ABFD can be examined from their pointers in
4074 SYMS. Pointers of symbols to keep should be stored contiguously at
4075 the beginning of that array.
4076
4077 Returns the number of symbols to keep. */
4078
4079unsigned int
4080_bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4081 asymbol **syms, long symcount)
4082{
4083 long src_count, dst_count = 0;
4084
4085 for (src_count = 0; src_count < symcount; src_count++)
4086 {
4087 asymbol *sym = syms[src_count];
4088 char *name = (char *) bfd_asymbol_name (sym);
4089 struct bfd_link_hash_entry *h;
4090
4091 if (!sym_is_global (abfd, sym))
4092 continue;
4093
4094 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE);
5df1bc57
AM
4095 if (h == NULL)
4096 continue;
76359541
TP
4097 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4098 continue;
76359541
TP
4099 if (h->linker_def || h->ldscript_def)
4100 continue;
4101
4102 syms[dst_count++] = sym;
4103 }
4104
4105 syms[dst_count] = NULL;
4106
4107 return dst_count;
4108}
4109
5372391b 4110/* Don't output section symbols for sections that are not going to be
c6d8cab4 4111 output, that are duplicates or there is no BFD section. */
5372391b
AM
4112
4113static bfd_boolean
4114ignore_section_sym (bfd *abfd, asymbol *sym)
4115{
c6d8cab4
L
4116 elf_symbol_type *type_ptr;
4117
db0c309f
NC
4118 if (sym == NULL)
4119 return FALSE;
4120
c6d8cab4
L
4121 if ((sym->flags & BSF_SECTION_SYM) == 0)
4122 return FALSE;
4123
db0c309f
NC
4124 if (sym->section == NULL)
4125 return TRUE;
4126
c6d8cab4
L
4127 type_ptr = elf_symbol_from (abfd, sym);
4128 return ((type_ptr != NULL
4129 && type_ptr->internal_elf_sym.st_shndx != 0
4130 && bfd_is_abs_section (sym->section))
4131 || !(sym->section->owner == abfd
db0c309f
NC
4132 || (sym->section->output_section != NULL
4133 && sym->section->output_section->owner == abfd
2633a79c
AM
4134 && sym->section->output_offset == 0)
4135 || bfd_is_abs_section (sym->section)));
5372391b
AM
4136}
4137
2633a79c
AM
4138/* Map symbol from it's internal number to the external number, moving
4139 all local symbols to be at the head of the list. */
4140
b34976b6 4141static bfd_boolean
12bd6957 4142elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
252b5132 4143{
dc810e39 4144 unsigned int symcount = bfd_get_symcount (abfd);
252b5132
RH
4145 asymbol **syms = bfd_get_outsymbols (abfd);
4146 asymbol **sect_syms;
dc810e39
AM
4147 unsigned int num_locals = 0;
4148 unsigned int num_globals = 0;
4149 unsigned int num_locals2 = 0;
4150 unsigned int num_globals2 = 0;
7292b3ac 4151 unsigned int max_index = 0;
dc810e39 4152 unsigned int idx;
252b5132
RH
4153 asection *asect;
4154 asymbol **new_syms;
446f7ed5 4155 size_t amt;
252b5132
RH
4156
4157#ifdef DEBUG
4158 fprintf (stderr, "elf_map_symbols\n");
4159 fflush (stderr);
4160#endif
4161
252b5132
RH
4162 for (asect = abfd->sections; asect; asect = asect->next)
4163 {
4164 if (max_index < asect->index)
4165 max_index = asect->index;
4166 }
4167
4168 max_index++;
446f7ed5
AM
4169 amt = max_index * sizeof (asymbol *);
4170 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
252b5132 4171 if (sect_syms == NULL)
b34976b6 4172 return FALSE;
252b5132 4173 elf_section_syms (abfd) = sect_syms;
4e89ac30 4174 elf_num_section_syms (abfd) = max_index;
252b5132 4175
079e9a2f
AM
4176 /* Init sect_syms entries for any section symbols we have already
4177 decided to output. */
252b5132
RH
4178 for (idx = 0; idx < symcount; idx++)
4179 {
dc810e39 4180 asymbol *sym = syms[idx];
c044fabd 4181
252b5132 4182 if ((sym->flags & BSF_SECTION_SYM) != 0
0f0a5e58 4183 && sym->value == 0
2633a79c
AM
4184 && !ignore_section_sym (abfd, sym)
4185 && !bfd_is_abs_section (sym->section))
252b5132 4186 {
5372391b 4187 asection *sec = sym->section;
252b5132 4188
5372391b
AM
4189 if (sec->owner != abfd)
4190 sec = sec->output_section;
252b5132 4191
5372391b 4192 sect_syms[sec->index] = syms[idx];
252b5132
RH
4193 }
4194 }
4195
252b5132
RH
4196 /* Classify all of the symbols. */
4197 for (idx = 0; idx < symcount; idx++)
4198 {
2633a79c 4199 if (sym_is_global (abfd, syms[idx]))
252b5132 4200 num_globals++;
2633a79c
AM
4201 else if (!ignore_section_sym (abfd, syms[idx]))
4202 num_locals++;
252b5132 4203 }
079e9a2f 4204
5372391b 4205 /* We will be adding a section symbol for each normal BFD section. Most
079e9a2f
AM
4206 sections will already have a section symbol in outsymbols, but
4207 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4208 at least in that case. */
252b5132
RH
4209 for (asect = abfd->sections; asect; asect = asect->next)
4210 {
079e9a2f 4211 if (sect_syms[asect->index] == NULL)
252b5132 4212 {
079e9a2f 4213 if (!sym_is_global (abfd, asect->symbol))
252b5132
RH
4214 num_locals++;
4215 else
4216 num_globals++;
252b5132
RH
4217 }
4218 }
4219
4220 /* Now sort the symbols so the local symbols are first. */
446f7ed5
AM
4221 amt = (num_locals + num_globals) * sizeof (asymbol *);
4222 new_syms = (asymbol **) bfd_alloc (abfd, amt);
252b5132 4223 if (new_syms == NULL)
b34976b6 4224 return FALSE;
252b5132
RH
4225
4226 for (idx = 0; idx < symcount; idx++)
4227 {
4228 asymbol *sym = syms[idx];
dc810e39 4229 unsigned int i;
252b5132 4230
2633a79c
AM
4231 if (sym_is_global (abfd, sym))
4232 i = num_locals + num_globals2++;
4233 else if (!ignore_section_sym (abfd, sym))
252b5132
RH
4234 i = num_locals2++;
4235 else
2633a79c 4236 continue;
252b5132
RH
4237 new_syms[i] = sym;
4238 sym->udata.i = i + 1;
4239 }
4240 for (asect = abfd->sections; asect; asect = asect->next)
4241 {
079e9a2f 4242 if (sect_syms[asect->index] == NULL)
252b5132 4243 {
079e9a2f 4244 asymbol *sym = asect->symbol;
dc810e39 4245 unsigned int i;
252b5132 4246
079e9a2f 4247 sect_syms[asect->index] = sym;
252b5132
RH
4248 if (!sym_is_global (abfd, sym))
4249 i = num_locals2++;
4250 else
4251 i = num_locals + num_globals2++;
4252 new_syms[i] = sym;
4253 sym->udata.i = i + 1;
4254 }
4255 }
4256
4257 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4258
12bd6957 4259 *pnum_locals = num_locals;
b34976b6 4260 return TRUE;
252b5132
RH
4261}
4262
4263/* Align to the maximum file alignment that could be required for any
4264 ELF data structure. */
4265
268b6b39 4266static inline file_ptr
217aa764 4267align_file_position (file_ptr off, int align)
252b5132
RH
4268{
4269 return (off + align - 1) & ~(align - 1);
4270}
4271
4272/* Assign a file position to a section, optionally aligning to the
4273 required section alignment. */
4274
217aa764
AM
4275file_ptr
4276_bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4277 file_ptr offset,
4278 bfd_boolean align)
252b5132 4279{
72de5009
AM
4280 if (align && i_shdrp->sh_addralign > 1)
4281 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
252b5132
RH
4282 i_shdrp->sh_offset = offset;
4283 if (i_shdrp->bfd_section != NULL)
4284 i_shdrp->bfd_section->filepos = offset;
4285 if (i_shdrp->sh_type != SHT_NOBITS)
4286 offset += i_shdrp->sh_size;
4287 return offset;
4288}
4289
4290/* Compute the file positions we are going to put the sections at, and
4291 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4292 is not NULL, this is being called by the ELF backend linker. */
4293
b34976b6 4294bfd_boolean
217aa764
AM
4295_bfd_elf_compute_section_file_positions (bfd *abfd,
4296 struct bfd_link_info *link_info)
252b5132 4297{
9c5bfbb7 4298 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 4299 struct fake_section_arg fsargs;
b34976b6 4300 bfd_boolean failed;
ef10c3ac 4301 struct elf_strtab_hash *strtab = NULL;
252b5132 4302 Elf_Internal_Shdr *shstrtab_hdr;
3516e984 4303 bfd_boolean need_symtab;
252b5132
RH
4304
4305 if (abfd->output_has_begun)
b34976b6 4306 return TRUE;
252b5132
RH
4307
4308 /* Do any elf backend specific processing first. */
4309 if (bed->elf_backend_begin_write_processing)
4310 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4311
ed7e9d0b 4312 if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
b34976b6 4313 return FALSE;
252b5132 4314
d4730f92
BS
4315 fsargs.failed = FALSE;
4316 fsargs.link_info = link_info;
4317 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4318 if (fsargs.failed)
b34976b6 4319 return FALSE;
252b5132 4320
da9f89d4 4321 if (!assign_section_numbers (abfd, link_info))
b34976b6 4322 return FALSE;
252b5132
RH
4323
4324 /* The backend linker builds symbol table information itself. */
3516e984
L
4325 need_symtab = (link_info == NULL
4326 && (bfd_get_symcount (abfd) > 0
4327 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4328 == HAS_RELOC)));
4329 if (need_symtab)
252b5132
RH
4330 {
4331 /* Non-zero if doing a relocatable link. */
4332 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4333
4334 if (! swap_out_syms (abfd, &strtab, relocatable_p))
b34976b6 4335 return FALSE;
252b5132
RH
4336 }
4337
d4730f92 4338 failed = FALSE;
1126897b 4339 if (link_info == NULL)
dbb410c3 4340 {
1126897b 4341 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
dbb410c3 4342 if (failed)
b34976b6 4343 return FALSE;
dbb410c3
AM
4344 }
4345
252b5132 4346 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
ed7e9d0b 4347 /* sh_name was set in init_file_header. */
252b5132 4348 shstrtab_hdr->sh_type = SHT_STRTAB;
84865015 4349 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
252b5132 4350 shstrtab_hdr->sh_addr = 0;
946748d5 4351 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
252b5132
RH
4352 shstrtab_hdr->sh_entsize = 0;
4353 shstrtab_hdr->sh_link = 0;
4354 shstrtab_hdr->sh_info = 0;
3e19fb8f 4355 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
252b5132
RH
4356 shstrtab_hdr->sh_addralign = 1;
4357
c84fca4d 4358 if (!assign_file_positions_except_relocs (abfd, link_info))
b34976b6 4359 return FALSE;
252b5132 4360
3516e984 4361 if (need_symtab)
252b5132
RH
4362 {
4363 file_ptr off;
4364 Elf_Internal_Shdr *hdr;
4365
12bd6957 4366 off = elf_next_file_pos (abfd);
252b5132 4367
6a40cf0c 4368 hdr = & elf_symtab_hdr (abfd);
b34976b6 4369 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
252b5132 4370
6a40cf0c
NC
4371 if (elf_symtab_shndx_list (abfd) != NULL)
4372 {
4373 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4374 if (hdr->sh_size != 0)
4375 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4376 /* FIXME: What about other symtab_shndx sections in the list ? */
4377 }
9ad5cbcf 4378
252b5132 4379 hdr = &elf_tdata (abfd)->strtab_hdr;
b34976b6 4380 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
252b5132 4381
12bd6957 4382 elf_next_file_pos (abfd) = off;
252b5132
RH
4383
4384 /* Now that we know where the .strtab section goes, write it
08a40648 4385 out. */
252b5132 4386 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 4387 || ! _bfd_elf_strtab_emit (abfd, strtab))
b34976b6 4388 return FALSE;
ef10c3ac 4389 _bfd_elf_strtab_free (strtab);
252b5132
RH
4390 }
4391
b34976b6 4392 abfd->output_has_begun = TRUE;
252b5132 4393
b34976b6 4394 return TRUE;
252b5132
RH
4395}
4396
8ded5a0f
AM
4397/* Make an initial estimate of the size of the program header. If we
4398 get the number wrong here, we'll redo section placement. */
4399
4400static bfd_size_type
4401get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4402{
4403 size_t segs;
4404 asection *s;
2b05f1b7 4405 const struct elf_backend_data *bed;
8ded5a0f
AM
4406
4407 /* Assume we will need exactly two PT_LOAD segments: one for text
4408 and one for data. */
4409 segs = 2;
4410
4411 s = bfd_get_section_by_name (abfd, ".interp");
1b9e270b 4412 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
8ded5a0f
AM
4413 {
4414 /* If we have a loadable interpreter section, we need a
4415 PT_INTERP segment. In this case, assume we also need a
4416 PT_PHDR segment, although that may not be true for all
4417 targets. */
e9a38e0f 4418 segs += 2;
8ded5a0f
AM
4419 }
4420
4421 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4422 {
4423 /* We need a PT_DYNAMIC segment. */
4424 ++segs;
f210dcff 4425 }
08a40648 4426
ceae84aa 4427 if (info != NULL && info->relro)
f210dcff
L
4428 {
4429 /* We need a PT_GNU_RELRO segment. */
4430 ++segs;
8ded5a0f
AM
4431 }
4432
12bd6957 4433 if (elf_eh_frame_hdr (abfd))
8ded5a0f
AM
4434 {
4435 /* We need a PT_GNU_EH_FRAME segment. */
4436 ++segs;
4437 }
4438
12bd6957 4439 if (elf_stack_flags (abfd))
8ded5a0f 4440 {
2b05f1b7
L
4441 /* We need a PT_GNU_STACK segment. */
4442 ++segs;
4443 }
94b11780 4444
0a59decb
L
4445 s = bfd_get_section_by_name (abfd,
4446 NOTE_GNU_PROPERTY_SECTION_NAME);
4447 if (s != NULL && s->size != 0)
4448 {
4449 /* We need a PT_GNU_PROPERTY segment. */
4450 ++segs;
4451 }
4452
2b05f1b7
L
4453 for (s = abfd->sections; s != NULL; s = s->next)
4454 {
8ded5a0f 4455 if ((s->flags & SEC_LOAD) != 0
23e463ed 4456 && elf_section_type (s) == SHT_NOTE)
8ded5a0f 4457 {
23e463ed 4458 unsigned int alignment_power;
8ded5a0f
AM
4459 /* We need a PT_NOTE segment. */
4460 ++segs;
23e463ed
L
4461 /* Try to create just one PT_NOTE segment for all adjacent
4462 loadable SHT_NOTE sections. gABI requires that within a
4463 PT_NOTE segment (and also inside of each SHT_NOTE section)
4464 each note should have the same alignment. So we check
4465 whether the sections are correctly aligned. */
4466 alignment_power = s->alignment_power;
4467 while (s->next != NULL
4468 && s->next->alignment_power == alignment_power
4469 && (s->next->flags & SEC_LOAD) != 0
4470 && elf_section_type (s->next) == SHT_NOTE)
4471 s = s->next;
8ded5a0f
AM
4472 }
4473 }
4474
4475 for (s = abfd->sections; s != NULL; s = s->next)
4476 {
4477 if (s->flags & SEC_THREAD_LOCAL)
4478 {
4479 /* We need a PT_TLS segment. */
4480 ++segs;
4481 break;
4482 }
4483 }
4484
2b05f1b7 4485 bed = get_elf_backend_data (abfd);
a91e1603 4486
df3a023b
AM
4487 if ((abfd->flags & D_PAGED) != 0
4488 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4489 {
4490 /* Add a PT_GNU_MBIND segment for each mbind section. */
4491 unsigned int page_align_power = bfd_log2 (bed->commonpagesize);
4492 for (s = abfd->sections; s != NULL; s = s->next)
4493 if (elf_section_flags (s) & SHF_GNU_MBIND)
4494 {
4495 if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4496 {
4497 _bfd_error_handler
4498 /* xgettext:c-format */
4499 (_("%pB: GNU_MBIND section `%pA' has invalid "
4500 "sh_info field: %d"),
4501 abfd, s, elf_section_data (s)->this_hdr.sh_info);
4502 continue;
4503 }
4504 /* Align mbind section to page size. */
4505 if (s->alignment_power < page_align_power)
4506 s->alignment_power = page_align_power;
4507 segs ++;
4508 }
4509 }
4510
4511 /* Let the backend count up any program headers it might need. */
4512 if (bed->elf_backend_additional_program_headers)
8ded5a0f
AM
4513 {
4514 int a;
4515
4516 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4517 if (a == -1)
4518 abort ();
4519 segs += a;
4520 }
4521
4522 return segs * bed->s->sizeof_phdr;
4523}
4524
2ea37f1c
NC
4525/* Find the segment that contains the output_section of section. */
4526
4527Elf_Internal_Phdr *
4528_bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4529{
4530 struct elf_segment_map *m;
4531 Elf_Internal_Phdr *p;
4532
12bd6957 4533 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
2ea37f1c
NC
4534 m != NULL;
4535 m = m->next, p++)
4536 {
4537 int i;
4538
4539 for (i = m->count - 1; i >= 0; i--)
4540 if (m->sections[i] == section)
4541 return p;
4542 }
4543
4544 return NULL;
4545}
4546
252b5132
RH
4547/* Create a mapping from a set of sections to a program segment. */
4548
217aa764
AM
4549static struct elf_segment_map *
4550make_mapping (bfd *abfd,
4551 asection **sections,
4552 unsigned int from,
4553 unsigned int to,
4554 bfd_boolean phdr)
252b5132
RH
4555{
4556 struct elf_segment_map *m;
4557 unsigned int i;
4558 asection **hdrpp;
986f0783 4559 size_t amt;
252b5132 4560
00bee008
AM
4561 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4562 amt += (to - from) * sizeof (asection *);
a50b1753 4563 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
4564 if (m == NULL)
4565 return NULL;
4566 m->next = NULL;
4567 m->p_type = PT_LOAD;
4568 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4569 m->sections[i - from] = *hdrpp;
4570 m->count = to - from;
4571
4572 if (from == 0 && phdr)
4573 {
4574 /* Include the headers in the first PT_LOAD segment. */
4575 m->includes_filehdr = 1;
4576 m->includes_phdrs = 1;
4577 }
4578
4579 return m;
4580}
4581
229fcec5
MM
4582/* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4583 on failure. */
4584
4585struct elf_segment_map *
4586_bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4587{
4588 struct elf_segment_map *m;
4589
a50b1753 4590 m = (struct elf_segment_map *) bfd_zalloc (abfd,
07d6d2b8 4591 sizeof (struct elf_segment_map));
229fcec5
MM
4592 if (m == NULL)
4593 return NULL;
4594 m->next = NULL;
4595 m->p_type = PT_DYNAMIC;
4596 m->count = 1;
4597 m->sections[0] = dynsec;
08a40648 4598
229fcec5
MM
4599 return m;
4600}
4601
8ded5a0f 4602/* Possibly add or remove segments from the segment map. */
252b5132 4603
b34976b6 4604static bfd_boolean
3dea8fca
AM
4605elf_modify_segment_map (bfd *abfd,
4606 struct bfd_link_info *info,
4607 bfd_boolean remove_empty_load)
252b5132 4608{
252e386e 4609 struct elf_segment_map **m;
8ded5a0f 4610 const struct elf_backend_data *bed;
252b5132 4611
8ded5a0f
AM
4612 /* The placement algorithm assumes that non allocated sections are
4613 not in PT_LOAD segments. We ensure this here by removing such
4614 sections from the segment map. We also remove excluded
252e386e
AM
4615 sections. Finally, any PT_LOAD segment without sections is
4616 removed. */
12bd6957 4617 m = &elf_seg_map (abfd);
252e386e 4618 while (*m)
8ded5a0f
AM
4619 {
4620 unsigned int i, new_count;
252b5132 4621
252e386e 4622 for (new_count = 0, i = 0; i < (*m)->count; i++)
8ded5a0f 4623 {
252e386e
AM
4624 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4625 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4626 || (*m)->p_type != PT_LOAD))
8ded5a0f 4627 {
252e386e
AM
4628 (*m)->sections[new_count] = (*m)->sections[i];
4629 new_count++;
8ded5a0f
AM
4630 }
4631 }
252e386e 4632 (*m)->count = new_count;
252b5132 4633
1a9ccd70
NC
4634 if (remove_empty_load
4635 && (*m)->p_type == PT_LOAD
4636 && (*m)->count == 0
4637 && !(*m)->includes_phdrs)
252e386e
AM
4638 *m = (*m)->next;
4639 else
4640 m = &(*m)->next;
8ded5a0f 4641 }
252b5132 4642
8ded5a0f
AM
4643 bed = get_elf_backend_data (abfd);
4644 if (bed->elf_backend_modify_segment_map != NULL)
252b5132 4645 {
252e386e 4646 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
8ded5a0f 4647 return FALSE;
252b5132 4648 }
252b5132 4649
8ded5a0f
AM
4650 return TRUE;
4651}
252b5132 4652
dbc88fc1
AM
4653#define IS_TBSS(s) \
4654 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4655
8ded5a0f 4656/* Set up a mapping from BFD sections to program segments. */
252b5132 4657
8ded5a0f
AM
4658bfd_boolean
4659_bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
4660{
4661 unsigned int count;
4662 struct elf_segment_map *m;
4663 asection **sections = NULL;
4664 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3dea8fca 4665 bfd_boolean no_user_phdrs;
252b5132 4666
12bd6957 4667 no_user_phdrs = elf_seg_map (abfd) == NULL;
d324f6d6
RM
4668
4669 if (info != NULL)
4670 info->user_phdrs = !no_user_phdrs;
4671
3dea8fca 4672 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
252b5132 4673 {
8ded5a0f
AM
4674 asection *s;
4675 unsigned int i;
4676 struct elf_segment_map *mfirst;
4677 struct elf_segment_map **pm;
4678 asection *last_hdr;
4679 bfd_vma last_size;
00bee008 4680 unsigned int hdr_index;
8ded5a0f
AM
4681 bfd_vma maxpagesize;
4682 asection **hdrpp;
64029e93 4683 bfd_boolean phdr_in_segment;
8ded5a0f 4684 bfd_boolean writable;
2888249f 4685 bfd_boolean executable;
446f7ed5 4686 unsigned int tls_count = 0;
8ded5a0f 4687 asection *first_tls = NULL;
a91e1603 4688 asection *first_mbind = NULL;
8ded5a0f 4689 asection *dynsec, *eh_frame_hdr;
446f7ed5 4690 size_t amt;
66631823
CE
4691 bfd_vma addr_mask, wrap_to = 0; /* Bytes. */
4692 bfd_size_type phdr_size; /* Octets/bytes. */
502794d4 4693 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132 4694
8ded5a0f 4695 /* Select the allocated sections, and sort them. */
252b5132 4696
446f7ed5
AM
4697 amt = bfd_count_sections (abfd) * sizeof (asection *);
4698 sections = (asection **) bfd_malloc (amt);
8ded5a0f 4699 if (sections == NULL)
252b5132 4700 goto error_return;
252b5132 4701
8d06853e
AM
4702 /* Calculate top address, avoiding undefined behaviour of shift
4703 left operator when shift count is equal to size of type
4704 being shifted. */
4705 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4706 addr_mask = (addr_mask << 1) + 1;
4707
8ded5a0f
AM
4708 i = 0;
4709 for (s = abfd->sections; s != NULL; s = s->next)
4710 {
4711 if ((s->flags & SEC_ALLOC) != 0)
4712 {
48db3297
AM
4713 /* target_index is unused until bfd_elf_final_link
4714 starts output of section symbols. Use it to make
4715 qsort stable. */
4716 s->target_index = i;
8ded5a0f
AM
4717 sections[i] = s;
4718 ++i;
8d06853e 4719 /* A wrapping section potentially clashes with header. */
66631823
CE
4720 if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
4721 wrap_to = (s->lma + s->size / opb) & addr_mask;
8ded5a0f
AM
4722 }
4723 }
4724 BFD_ASSERT (i <= bfd_count_sections (abfd));
4725 count = i;
252b5132 4726
8ded5a0f 4727 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
252b5132 4728
64029e93
AM
4729 phdr_size = elf_program_header_size (abfd);
4730 if (phdr_size == (bfd_size_type) -1)
4731 phdr_size = get_program_header_size (abfd, info);
4732 phdr_size += bed->s->sizeof_ehdr;
502794d4
CE
4733 /* phdr_size is compared to LMA values which are in bytes. */
4734 phdr_size /= opb;
64029e93
AM
4735 maxpagesize = bed->maxpagesize;
4736 if (maxpagesize == 0)
4737 maxpagesize = 1;
4738 phdr_in_segment = info != NULL && info->load_phdrs;
4739 if (count != 0
4740 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
4741 >= (phdr_size & (maxpagesize - 1))))
4742 /* For compatibility with old scripts that may not be using
4743 SIZEOF_HEADERS, add headers when it looks like space has
4744 been left for them. */
4745 phdr_in_segment = TRUE;
252b5132 4746
64029e93 4747 /* Build the mapping. */
8ded5a0f
AM
4748 mfirst = NULL;
4749 pm = &mfirst;
252b5132 4750
8ded5a0f
AM
4751 /* If we have a .interp section, then create a PT_PHDR segment for
4752 the program headers and a PT_INTERP segment for the .interp
4753 section. */
4754 s = bfd_get_section_by_name (abfd, ".interp");
1b9e270b 4755 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
8ded5a0f
AM
4756 {
4757 amt = sizeof (struct elf_segment_map);
a50b1753 4758 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4759 if (m == NULL)
4760 goto error_return;
4761 m->next = NULL;
4762 m->p_type = PT_PHDR;
f882209d 4763 m->p_flags = PF_R;
8ded5a0f
AM
4764 m->p_flags_valid = 1;
4765 m->includes_phdrs = 1;
64029e93 4766 phdr_in_segment = TRUE;
8ded5a0f
AM
4767 *pm = m;
4768 pm = &m->next;
252b5132 4769
8ded5a0f 4770 amt = sizeof (struct elf_segment_map);
a50b1753 4771 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4772 if (m == NULL)
4773 goto error_return;
4774 m->next = NULL;
4775 m->p_type = PT_INTERP;
4776 m->count = 1;
4777 m->sections[0] = s;
4778
4779 *pm = m;
4780 pm = &m->next;
252b5132 4781 }
8ded5a0f
AM
4782
4783 /* Look through the sections. We put sections in the same program
4784 segment when the start of the second section can be placed within
4785 a few bytes of the end of the first section. */
4786 last_hdr = NULL;
4787 last_size = 0;
00bee008 4788 hdr_index = 0;
8ded5a0f 4789 writable = FALSE;
2888249f 4790 executable = FALSE;
8ded5a0f
AM
4791 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4792 if (dynsec != NULL
4793 && (dynsec->flags & SEC_LOAD) == 0)
4794 dynsec = NULL;
4795
64029e93
AM
4796 if ((abfd->flags & D_PAGED) == 0)
4797 phdr_in_segment = FALSE;
4798
8ded5a0f
AM
4799 /* Deal with -Ttext or something similar such that the first section
4800 is not adjacent to the program headers. This is an
4801 approximation, since at this point we don't know exactly how many
4802 program headers we will need. */
64029e93 4803 if (phdr_in_segment && count > 0)
252b5132 4804 {
66631823 4805 bfd_vma phdr_lma; /* Bytes. */
64029e93
AM
4806 bfd_boolean separate_phdr = FALSE;
4807
4808 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
4809 if (info != NULL
4810 && info->separate_code
4811 && (sections[0]->flags & SEC_CODE) != 0)
1a9ccd70 4812 {
64029e93
AM
4813 /* If data sections should be separate from code and
4814 thus not executable, and the first section is
4815 executable then put the file and program headers in
4816 their own PT_LOAD. */
4817 separate_phdr = TRUE;
4818 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
4819 == (sections[0]->lma & addr_mask & -maxpagesize)))
4820 {
4821 /* The file and program headers are currently on the
4822 same page as the first section. Put them on the
4823 previous page if we can. */
4824 if (phdr_lma >= maxpagesize)
4825 phdr_lma -= maxpagesize;
4826 else
4827 separate_phdr = FALSE;
4828 }
4829 }
4830 if ((sections[0]->lma & addr_mask) < phdr_lma
4831 || (sections[0]->lma & addr_mask) < phdr_size)
4832 /* If file and program headers would be placed at the end
4833 of memory then it's probably better to omit them. */
4834 phdr_in_segment = FALSE;
4835 else if (phdr_lma < wrap_to)
4836 /* If a section wraps around to where we'll be placing
4837 file and program headers, then the headers will be
4838 overwritten. */
4839 phdr_in_segment = FALSE;
4840 else if (separate_phdr)
4841 {
4842 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
4843 if (m == NULL)
4844 goto error_return;
66631823 4845 m->p_paddr = phdr_lma * opb;
64029e93
AM
4846 m->p_vaddr_offset
4847 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
4848 m->p_paddr_valid = 1;
4849 *pm = m;
4850 pm = &m->next;
4851 phdr_in_segment = FALSE;
1a9ccd70 4852 }
252b5132
RH
4853 }
4854
8ded5a0f 4855 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
252b5132 4856 {
8ded5a0f
AM
4857 asection *hdr;
4858 bfd_boolean new_segment;
4859
4860 hdr = *hdrpp;
4861
4862 /* See if this section and the last one will fit in the same
4863 segment. */
4864
4865 if (last_hdr == NULL)
4866 {
4867 /* If we don't have a segment yet, then we don't need a new
4868 one (we build the last one after this loop). */
4869 new_segment = FALSE;
4870 }
4871 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4872 {
4873 /* If this section has a different relation between the
4874 virtual address and the load address, then we need a new
4875 segment. */
4876 new_segment = TRUE;
4877 }
b5599592
AM
4878 else if (hdr->lma < last_hdr->lma + last_size
4879 || last_hdr->lma + last_size < last_hdr->lma)
4880 {
4881 /* If this section has a load address that makes it overlap
4882 the previous section, then we need a new segment. */
4883 new_segment = TRUE;
4884 }
76cb3a89
AM
4885 else if ((abfd->flags & D_PAGED) != 0
4886 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4887 == (hdr->lma & -maxpagesize)))
4888 {
4889 /* If we are demand paged then we can't map two disk
4890 pages onto the same memory page. */
4891 new_segment = FALSE;
4892 }
39948a60
NC
4893 /* In the next test we have to be careful when last_hdr->lma is close
4894 to the end of the address space. If the aligned address wraps
4895 around to the start of the address space, then there are no more
4896 pages left in memory and it is OK to assume that the current
4897 section can be included in the current segment. */
76cb3a89
AM
4898 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4899 + maxpagesize > last_hdr->lma)
4900 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4901 + maxpagesize <= hdr->lma))
8ded5a0f
AM
4902 {
4903 /* If putting this section in this segment would force us to
4904 skip a page in the segment, then we need a new segment. */
4905 new_segment = TRUE;
4906 }
4907 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
76cb3a89 4908 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
8ded5a0f 4909 {
e5654c0f
AM
4910 /* We don't want to put a loaded section after a
4911 nonloaded (ie. bss style) section in the same segment
4912 as that will force the non-loaded section to be loaded.
76cb3a89 4913 Consider .tbss sections as loaded for this purpose. */
8ded5a0f
AM
4914 new_segment = TRUE;
4915 }
4916 else if ((abfd->flags & D_PAGED) == 0)
4917 {
4918 /* If the file is not demand paged, which means that we
4919 don't require the sections to be correctly aligned in the
4920 file, then there is no other reason for a new segment. */
4921 new_segment = FALSE;
4922 }
2888249f
L
4923 else if (info != NULL
4924 && info->separate_code
4925 && executable != ((hdr->flags & SEC_CODE) != 0))
4926 {
4927 new_segment = TRUE;
4928 }
8ded5a0f 4929 else if (! writable
76cb3a89 4930 && (hdr->flags & SEC_READONLY) == 0)
8ded5a0f
AM
4931 {
4932 /* We don't want to put a writable section in a read only
76cb3a89 4933 segment. */
8ded5a0f
AM
4934 new_segment = TRUE;
4935 }
4936 else
4937 {
4938 /* Otherwise, we can use the same segment. */
4939 new_segment = FALSE;
4940 }
4941
2889e75b 4942 /* Allow interested parties a chance to override our decision. */
ceae84aa
AM
4943 if (last_hdr != NULL
4944 && info != NULL
4945 && info->callbacks->override_segment_assignment != NULL)
4946 new_segment
4947 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4948 last_hdr,
4949 new_segment);
2889e75b 4950
8ded5a0f
AM
4951 if (! new_segment)
4952 {
4953 if ((hdr->flags & SEC_READONLY) == 0)
4954 writable = TRUE;
2888249f
L
4955 if ((hdr->flags & SEC_CODE) != 0)
4956 executable = TRUE;
8ded5a0f
AM
4957 last_hdr = hdr;
4958 /* .tbss sections effectively have zero size. */
502794d4 4959 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
8ded5a0f
AM
4960 continue;
4961 }
4962
4963 /* We need a new program segment. We must create a new program
00bee008 4964 header holding all the sections from hdr_index until hdr. */
8ded5a0f 4965
00bee008 4966 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
8ded5a0f
AM
4967 if (m == NULL)
4968 goto error_return;
4969
4970 *pm = m;
4971 pm = &m->next;
4972
252b5132 4973 if ((hdr->flags & SEC_READONLY) == 0)
b34976b6 4974 writable = TRUE;
8ded5a0f
AM
4975 else
4976 writable = FALSE;
4977
2888249f
L
4978 if ((hdr->flags & SEC_CODE) == 0)
4979 executable = FALSE;
4980 else
4981 executable = TRUE;
4982
baaff79e
JJ
4983 last_hdr = hdr;
4984 /* .tbss sections effectively have zero size. */
502794d4 4985 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
00bee008 4986 hdr_index = i;
8ded5a0f 4987 phdr_in_segment = FALSE;
252b5132
RH
4988 }
4989
86b2281f
AM
4990 /* Create a final PT_LOAD program segment, but not if it's just
4991 for .tbss. */
4992 if (last_hdr != NULL
00bee008 4993 && (i - hdr_index != 1
dbc88fc1 4994 || !IS_TBSS (last_hdr)))
8ded5a0f 4995 {
00bee008 4996 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
8ded5a0f
AM
4997 if (m == NULL)
4998 goto error_return;
252b5132 4999
8ded5a0f
AM
5000 *pm = m;
5001 pm = &m->next;
5002 }
252b5132 5003
8ded5a0f
AM
5004 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
5005 if (dynsec != NULL)
5006 {
5007 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
5008 if (m == NULL)
5009 goto error_return;
5010 *pm = m;
5011 pm = &m->next;
5012 }
252b5132 5013
23e463ed 5014 /* For each batch of consecutive loadable SHT_NOTE sections,
1c5265b5
JJ
5015 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
5016 because if we link together nonloadable .note sections and
5017 loadable .note sections, we will generate two .note sections
23e463ed 5018 in the output file. */
8ded5a0f
AM
5019 for (s = abfd->sections; s != NULL; s = s->next)
5020 {
5021 if ((s->flags & SEC_LOAD) != 0
23e463ed 5022 && elf_section_type (s) == SHT_NOTE)
8ded5a0f 5023 {
1c5265b5 5024 asection *s2;
23e463ed 5025 unsigned int alignment_power = s->alignment_power;
91d6fa6a
NC
5026
5027 count = 1;
23e463ed
L
5028 for (s2 = s; s2->next != NULL; s2 = s2->next)
5029 {
5030 if (s2->next->alignment_power == alignment_power
5031 && (s2->next->flags & SEC_LOAD) != 0
5032 && elf_section_type (s2->next) == SHT_NOTE
66631823 5033 && align_power (s2->lma + s2->size / opb,
23e463ed
L
5034 alignment_power)
5035 == s2->next->lma)
5036 count++;
5037 else
5038 break;
5039 }
00bee008
AM
5040 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5041 amt += count * sizeof (asection *);
a50b1753 5042 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5043 if (m == NULL)
5044 goto error_return;
5045 m->next = NULL;
5046 m->p_type = PT_NOTE;
1c5265b5
JJ
5047 m->count = count;
5048 while (count > 1)
5049 {
5050 m->sections[m->count - count--] = s;
5051 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5052 s = s->next;
5053 }
5054 m->sections[m->count - 1] = s;
5055 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
8ded5a0f
AM
5056 *pm = m;
5057 pm = &m->next;
5058 }
5059 if (s->flags & SEC_THREAD_LOCAL)
5060 {
5061 if (! tls_count)
5062 first_tls = s;
5063 tls_count++;
5064 }
a91e1603
L
5065 if (first_mbind == NULL
5066 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5067 first_mbind = s;
8ded5a0f 5068 }
252b5132 5069
8ded5a0f
AM
5070 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5071 if (tls_count > 0)
5072 {
00bee008
AM
5073 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5074 amt += tls_count * sizeof (asection *);
a50b1753 5075 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5076 if (m == NULL)
5077 goto error_return;
5078 m->next = NULL;
5079 m->p_type = PT_TLS;
5080 m->count = tls_count;
5081 /* Mandated PF_R. */
5082 m->p_flags = PF_R;
5083 m->p_flags_valid = 1;
d923cae0 5084 s = first_tls;
446f7ed5 5085 for (i = 0; i < tls_count; ++i)
8ded5a0f 5086 {
d923cae0
L
5087 if ((s->flags & SEC_THREAD_LOCAL) == 0)
5088 {
5089 _bfd_error_handler
871b3ab2 5090 (_("%pB: TLS sections are not adjacent:"), abfd);
d923cae0
L
5091 s = first_tls;
5092 i = 0;
446f7ed5 5093 while (i < tls_count)
d923cae0
L
5094 {
5095 if ((s->flags & SEC_THREAD_LOCAL) != 0)
5096 {
871b3ab2 5097 _bfd_error_handler (_(" TLS: %pA"), s);
d923cae0
L
5098 i++;
5099 }
5100 else
871b3ab2 5101 _bfd_error_handler (_(" non-TLS: %pA"), s);
d923cae0
L
5102 s = s->next;
5103 }
5104 bfd_set_error (bfd_error_bad_value);
5105 goto error_return;
5106 }
5107 m->sections[i] = s;
5108 s = s->next;
8ded5a0f 5109 }
252b5132 5110
8ded5a0f
AM
5111 *pm = m;
5112 pm = &m->next;
5113 }
252b5132 5114
df3a023b
AM
5115 if (first_mbind
5116 && (abfd->flags & D_PAGED) != 0
5117 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
a91e1603
L
5118 for (s = first_mbind; s != NULL; s = s->next)
5119 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
df3a023b 5120 && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
a91e1603
L
5121 {
5122 /* Mandated PF_R. */
5123 unsigned long p_flags = PF_R;
5124 if ((s->flags & SEC_READONLY) == 0)
5125 p_flags |= PF_W;
5126 if ((s->flags & SEC_CODE) != 0)
5127 p_flags |= PF_X;
5128
5129 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5130 m = bfd_zalloc (abfd, amt);
5131 if (m == NULL)
5132 goto error_return;
5133 m->next = NULL;
5134 m->p_type = (PT_GNU_MBIND_LO
5135 + elf_section_data (s)->this_hdr.sh_info);
5136 m->count = 1;
5137 m->p_flags_valid = 1;
5138 m->sections[0] = s;
5139 m->p_flags = p_flags;
5140
5141 *pm = m;
5142 pm = &m->next;
5143 }
5144
0a59decb
L
5145 s = bfd_get_section_by_name (abfd,
5146 NOTE_GNU_PROPERTY_SECTION_NAME);
5147 if (s != NULL && s->size != 0)
5148 {
5149 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5150 m = bfd_zalloc (abfd, amt);
5151 if (m == NULL)
5152 goto error_return;
5153 m->next = NULL;
5154 m->p_type = PT_GNU_PROPERTY;
5155 m->count = 1;
5156 m->p_flags_valid = 1;
5157 m->sections[0] = s;
5158 m->p_flags = PF_R;
5159 *pm = m;
5160 pm = &m->next;
5161 }
5162
8ded5a0f
AM
5163 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5164 segment. */
12bd6957 5165 eh_frame_hdr = elf_eh_frame_hdr (abfd);
8ded5a0f
AM
5166 if (eh_frame_hdr != NULL
5167 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
252b5132 5168 {
dc810e39 5169 amt = sizeof (struct elf_segment_map);
a50b1753 5170 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
5171 if (m == NULL)
5172 goto error_return;
5173 m->next = NULL;
8ded5a0f 5174 m->p_type = PT_GNU_EH_FRAME;
252b5132 5175 m->count = 1;
8ded5a0f 5176 m->sections[0] = eh_frame_hdr->output_section;
252b5132
RH
5177
5178 *pm = m;
5179 pm = &m->next;
5180 }
13ae64f3 5181
12bd6957 5182 if (elf_stack_flags (abfd))
13ae64f3 5183 {
8ded5a0f 5184 amt = sizeof (struct elf_segment_map);
a50b1753 5185 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5186 if (m == NULL)
5187 goto error_return;
5188 m->next = NULL;
2b05f1b7 5189 m->p_type = PT_GNU_STACK;
12bd6957 5190 m->p_flags = elf_stack_flags (abfd);
04c3a755 5191 m->p_align = bed->stack_align;
8ded5a0f 5192 m->p_flags_valid = 1;
04c3a755
NS
5193 m->p_align_valid = m->p_align != 0;
5194 if (info->stacksize > 0)
5195 {
5196 m->p_size = info->stacksize;
5197 m->p_size_valid = 1;
5198 }
252b5132 5199
8ded5a0f
AM
5200 *pm = m;
5201 pm = &m->next;
5202 }
65765700 5203
ceae84aa 5204 if (info != NULL && info->relro)
8ded5a0f 5205 {
f210dcff
L
5206 for (m = mfirst; m != NULL; m = m->next)
5207 {
3832a4d8
AM
5208 if (m->p_type == PT_LOAD
5209 && m->count != 0
5210 && m->sections[0]->vma >= info->relro_start
5211 && m->sections[0]->vma < info->relro_end)
f210dcff 5212 {
3832a4d8
AM
5213 i = m->count;
5214 while (--i != (unsigned) -1)
ec2e748a
NC
5215 {
5216 if (m->sections[i]->size > 0
5217 && (m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
5218 == (SEC_LOAD | SEC_HAS_CONTENTS))
5219 break;
5220 }
3832a4d8 5221
43a8475c 5222 if (i != (unsigned) -1)
f210dcff
L
5223 break;
5224 }
be01b344 5225 }
f210dcff
L
5226
5227 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5228 if (m != NULL)
5229 {
5230 amt = sizeof (struct elf_segment_map);
a50b1753 5231 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
f210dcff
L
5232 if (m == NULL)
5233 goto error_return;
5234 m->next = NULL;
5235 m->p_type = PT_GNU_RELRO;
f210dcff
L
5236 *pm = m;
5237 pm = &m->next;
5238 }
8ded5a0f 5239 }
9ee5e499 5240
8ded5a0f 5241 free (sections);
12bd6957 5242 elf_seg_map (abfd) = mfirst;
9ee5e499
JJ
5243 }
5244
3dea8fca 5245 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
8ded5a0f 5246 return FALSE;
8c37241b 5247
12bd6957 5248 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
8ded5a0f 5249 ++count;
12bd6957 5250 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
252b5132 5251
b34976b6 5252 return TRUE;
252b5132
RH
5253
5254 error_return:
5255 if (sections != NULL)
5256 free (sections);
b34976b6 5257 return FALSE;
252b5132
RH
5258}
5259
5260/* Sort sections by address. */
5261
5262static int
217aa764 5263elf_sort_sections (const void *arg1, const void *arg2)
252b5132
RH
5264{
5265 const asection *sec1 = *(const asection **) arg1;
5266 const asection *sec2 = *(const asection **) arg2;
eecdbe52 5267 bfd_size_type size1, size2;
252b5132
RH
5268
5269 /* Sort by LMA first, since this is the address used to
5270 place the section into a segment. */
5271 if (sec1->lma < sec2->lma)
5272 return -1;
5273 else if (sec1->lma > sec2->lma)
5274 return 1;
5275
5276 /* Then sort by VMA. Normally the LMA and the VMA will be
5277 the same, and this will do nothing. */
5278 if (sec1->vma < sec2->vma)
5279 return -1;
5280 else if (sec1->vma > sec2->vma)
5281 return 1;
5282
5283 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5284
07c6e936 5285#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
252b5132
RH
5286
5287 if (TOEND (sec1))
5288 {
48db3297 5289 if (!TOEND (sec2))
252b5132
RH
5290 return 1;
5291 }
00a7cdc5 5292 else if (TOEND (sec2))
252b5132
RH
5293 return -1;
5294
5295#undef TOEND
5296
00a7cdc5
NC
5297 /* Sort by size, to put zero sized sections
5298 before others at the same address. */
252b5132 5299
eea6121a
AM
5300 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5301 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
eecdbe52
JJ
5302
5303 if (size1 < size2)
252b5132 5304 return -1;
eecdbe52 5305 if (size1 > size2)
252b5132
RH
5306 return 1;
5307
5308 return sec1->target_index - sec2->target_index;
5309}
5310
30fe1832
AM
5311/* This qsort comparison functions sorts PT_LOAD segments first and
5312 by p_paddr, for assign_file_positions_for_load_sections. */
5313
5314static int
5315elf_sort_segments (const void *arg1, const void *arg2)
5316{
5317 const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5318 const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5319
5320 if (m1->p_type != m2->p_type)
5321 {
5322 if (m1->p_type == PT_NULL)
5323 return 1;
5324 if (m2->p_type == PT_NULL)
5325 return -1;
5326 return m1->p_type < m2->p_type ? -1 : 1;
5327 }
5328 if (m1->includes_filehdr != m2->includes_filehdr)
5329 return m1->includes_filehdr ? -1 : 1;
5330 if (m1->no_sort_lma != m2->no_sort_lma)
5331 return m1->no_sort_lma ? -1 : 1;
5332 if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5333 {
4b3ecb3b 5334 bfd_vma lma1, lma2; /* Octets. */
30fe1832
AM
5335 lma1 = 0;
5336 if (m1->p_paddr_valid)
4b3ecb3b 5337 lma1 = m1->p_paddr;
30fe1832 5338 else if (m1->count != 0)
4b3ecb3b
AM
5339 {
5340 unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5341 m1->sections[0]);
5342 lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5343 }
30fe1832
AM
5344 lma2 = 0;
5345 if (m2->p_paddr_valid)
4b3ecb3b 5346 lma2 = m2->p_paddr;
30fe1832 5347 else if (m2->count != 0)
4b3ecb3b
AM
5348 {
5349 unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5350 m2->sections[0]);
5351 lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5352 }
30fe1832
AM
5353 if (lma1 != lma2)
5354 return lma1 < lma2 ? -1 : 1;
5355 }
5356 if (m1->idx != m2->idx)
5357 return m1->idx < m2->idx ? -1 : 1;
5358 return 0;
5359}
5360
340b6d91
AC
5361/* Ian Lance Taylor writes:
5362
5363 We shouldn't be using % with a negative signed number. That's just
5364 not good. We have to make sure either that the number is not
5365 negative, or that the number has an unsigned type. When the types
5366 are all the same size they wind up as unsigned. When file_ptr is a
5367 larger signed type, the arithmetic winds up as signed long long,
5368 which is wrong.
5369
5370 What we're trying to say here is something like ``increase OFF by
5371 the least amount that will cause it to be equal to the VMA modulo
5372 the page size.'' */
5373/* In other words, something like:
5374
5375 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5376 off_offset = off % bed->maxpagesize;
5377 if (vma_offset < off_offset)
5378 adjustment = vma_offset + bed->maxpagesize - off_offset;
5379 else
5380 adjustment = vma_offset - off_offset;
08a40648 5381
de194d85 5382 which can be collapsed into the expression below. */
340b6d91
AC
5383
5384static file_ptr
5385vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5386{
dc9155b2
NC
5387 /* PR binutils/16199: Handle an alignment of zero. */
5388 if (maxpagesize == 0)
5389 maxpagesize = 1;
340b6d91
AC
5390 return ((vma - off) % maxpagesize);
5391}
5392
6d33f217
L
5393static void
5394print_segment_map (const struct elf_segment_map *m)
5395{
5396 unsigned int j;
5397 const char *pt = get_segment_type (m->p_type);
5398 char buf[32];
5399
5400 if (pt == NULL)
5401 {
5402 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5403 sprintf (buf, "LOPROC+%7.7x",
5404 (unsigned int) (m->p_type - PT_LOPROC));
5405 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5406 sprintf (buf, "LOOS+%7.7x",
5407 (unsigned int) (m->p_type - PT_LOOS));
5408 else
5409 snprintf (buf, sizeof (buf), "%8.8x",
5410 (unsigned int) m->p_type);
5411 pt = buf;
5412 }
4a97a0e5 5413 fflush (stdout);
6d33f217
L
5414 fprintf (stderr, "%s:", pt);
5415 for (j = 0; j < m->count; j++)
5416 fprintf (stderr, " %s", m->sections [j]->name);
5417 putc ('\n',stderr);
4a97a0e5 5418 fflush (stderr);
6d33f217
L
5419}
5420
32812159
AM
5421static bfd_boolean
5422write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
5423{
5424 void *buf;
5425 bfd_boolean ret;
5426
5427 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5428 return FALSE;
5429 buf = bfd_zmalloc (len);
5430 if (buf == NULL)
5431 return FALSE;
5432 ret = bfd_bwrite (buf, len, abfd) == len;
5433 free (buf);
5434 return ret;
5435}
5436
252b5132
RH
5437/* Assign file positions to the sections based on the mapping from
5438 sections to segments. This function also sets up some fields in
f3520d2f 5439 the file header. */
252b5132 5440
b34976b6 5441static bfd_boolean
f3520d2f
AM
5442assign_file_positions_for_load_sections (bfd *abfd,
5443 struct bfd_link_info *link_info)
252b5132
RH
5444{
5445 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 5446 struct elf_segment_map *m;
30fe1832 5447 struct elf_segment_map *phdr_load_seg;
252b5132 5448 Elf_Internal_Phdr *phdrs;
252b5132 5449 Elf_Internal_Phdr *p;
502794d4 5450 file_ptr off; /* Octets. */
3f570048 5451 bfd_size_type maxpagesize;
30fe1832 5452 unsigned int alloc, actual;
0920dee7 5453 unsigned int i, j;
30fe1832 5454 struct elf_segment_map **sorted_seg_map;
502794d4 5455 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132 5456
e36284ab 5457 if (link_info == NULL
ceae84aa 5458 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
8ded5a0f 5459 return FALSE;
252b5132 5460
8ded5a0f 5461 alloc = 0;
12bd6957 5462 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
30fe1832 5463 m->idx = alloc++;
252b5132 5464
82f2dbf7
NC
5465 if (alloc)
5466 {
5467 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5468 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5469 }
5470 else
5471 {
5472 /* PR binutils/12467. */
5473 elf_elfheader (abfd)->e_phoff = 0;
5474 elf_elfheader (abfd)->e_phentsize = 0;
5475 }
d324f6d6 5476
8ded5a0f 5477 elf_elfheader (abfd)->e_phnum = alloc;
252b5132 5478
12bd6957 5479 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
30fe1832
AM
5480 {
5481 actual = alloc;
5482 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5483 }
8ded5a0f 5484 else
30fe1832
AM
5485 {
5486 actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5487 BFD_ASSERT (elf_program_header_size (abfd)
5488 == actual * bed->s->sizeof_phdr);
5489 BFD_ASSERT (actual >= alloc);
5490 }
252b5132
RH
5491
5492 if (alloc == 0)
f3520d2f 5493 {
12bd6957 5494 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
8ded5a0f 5495 return TRUE;
f3520d2f 5496 }
252b5132 5497
12bd6957 5498 /* We're writing the size in elf_program_header_size (abfd),
57268894
HPN
5499 see assign_file_positions_except_relocs, so make sure we have
5500 that amount allocated, with trailing space cleared.
12bd6957
AM
5501 The variable alloc contains the computed need, while
5502 elf_program_header_size (abfd) contains the size used for the
57268894
HPN
5503 layout.
5504 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5505 where the layout is forced to according to a larger size in the
5506 last iterations for the testcase ld-elf/header. */
30fe1832
AM
5507 phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
5508 + alloc * sizeof (*sorted_seg_map)));
5509 sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
f3520d2f 5510 elf_tdata (abfd)->phdr = phdrs;
252b5132 5511 if (phdrs == NULL)
b34976b6 5512 return FALSE;
252b5132 5513
30fe1832 5514 for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
252b5132 5515 {
30fe1832 5516 sorted_seg_map[j] = m;
252b5132 5517 /* If elf_segment_map is not from map_sections_to_segments, the
08a40648 5518 sections may not be correctly ordered. NOTE: sorting should
52e9b619
MS
5519 not be done to the PT_NOTE section of a corefile, which may
5520 contain several pseudo-sections artificially created by bfd.
5521 Sorting these pseudo-sections breaks things badly. */
47d9a591
AM
5522 if (m->count > 1
5523 && !(elf_elfheader (abfd)->e_type == ET_CORE
52e9b619 5524 && m->p_type == PT_NOTE))
48db3297
AM
5525 {
5526 for (i = 0; i < m->count; i++)
5527 m->sections[i]->target_index = i;
5528 qsort (m->sections, (size_t) m->count, sizeof (asection *),
5529 elf_sort_sections);
5530 }
30fe1832
AM
5531 }
5532 if (alloc > 1)
5533 qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
5534 elf_sort_segments);
5535
5536 maxpagesize = 1;
5537 if ((abfd->flags & D_PAGED) != 0)
5538 maxpagesize = bed->maxpagesize;
5539
5540 /* Sections must map to file offsets past the ELF file header. */
5541 off = bed->s->sizeof_ehdr;
5542 /* And if one of the PT_LOAD headers doesn't include the program
5543 headers then we'll be mapping program headers in the usual
5544 position after the ELF file header. */
5545 phdr_load_seg = NULL;
5546 for (j = 0; j < alloc; j++)
5547 {
5548 m = sorted_seg_map[j];
5549 if (m->p_type != PT_LOAD)
5550 break;
5551 if (m->includes_phdrs)
5552 {
5553 phdr_load_seg = m;
5554 break;
5555 }
5556 }
5557 if (phdr_load_seg == NULL)
5558 off += actual * bed->s->sizeof_phdr;
5559
5560 for (j = 0; j < alloc; j++)
5561 {
5562 asection **secpp;
502794d4 5563 bfd_vma off_adjust; /* Octets. */
30fe1832 5564 bfd_boolean no_contents;
252b5132 5565
b301b248
AM
5566 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5567 number of sections with contents contributing to both p_filesz
5568 and p_memsz, followed by a number of sections with no contents
5569 that just contribute to p_memsz. In this loop, OFF tracks next
02bf8d82 5570 available file offset for PT_LOAD and PT_NOTE segments. */
30fe1832
AM
5571 m = sorted_seg_map[j];
5572 p = phdrs + m->idx;
252b5132 5573 p->p_type = m->p_type;
28a7f3e7 5574 p->p_flags = m->p_flags;
252b5132 5575
3f570048 5576 if (m->count == 0)
502794d4 5577 p->p_vaddr = m->p_vaddr_offset * opb;
3f570048 5578 else
502794d4 5579 p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
3f570048
AM
5580
5581 if (m->p_paddr_valid)
5582 p->p_paddr = m->p_paddr;
5583 else if (m->count == 0)
5584 p->p_paddr = 0;
5585 else
502794d4 5586 p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
3f570048
AM
5587
5588 if (p->p_type == PT_LOAD
5589 && (abfd->flags & D_PAGED) != 0)
5590 {
5591 /* p_align in demand paged PT_LOAD segments effectively stores
5592 the maximum page size. When copying an executable with
5593 objcopy, we set m->p_align from the input file. Use this
5594 value for maxpagesize rather than bed->maxpagesize, which
5595 may be different. Note that we use maxpagesize for PT_TLS
5596 segment alignment later in this function, so we are relying
5597 on at least one PT_LOAD segment appearing before a PT_TLS
5598 segment. */
5599 if (m->p_align_valid)
5600 maxpagesize = m->p_align;
5601
5602 p->p_align = maxpagesize;
5603 }
3271a814
NS
5604 else if (m->p_align_valid)
5605 p->p_align = m->p_align;
e970b90a
DJ
5606 else if (m->count == 0)
5607 p->p_align = 1 << bed->s->log_file_align;
30fe1832
AM
5608
5609 if (m == phdr_load_seg)
5610 {
5611 if (!m->includes_filehdr)
5612 p->p_offset = off;
5613 off += actual * bed->s->sizeof_phdr;
5614 }
3f570048 5615
bf988460
AM
5616 no_contents = FALSE;
5617 off_adjust = 0;
252b5132 5618 if (p->p_type == PT_LOAD
b301b248 5619 && m->count > 0)
252b5132 5620 {
66631823 5621 bfd_size_type align; /* Bytes. */
a49e53ed 5622 unsigned int align_power = 0;
b301b248 5623
3271a814
NS
5624 if (m->p_align_valid)
5625 align = p->p_align;
5626 else
252b5132 5627 {
3271a814
NS
5628 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5629 {
5630 unsigned int secalign;
08a40648 5631
fd361982 5632 secalign = bfd_section_alignment (*secpp);
3271a814
NS
5633 if (secalign > align_power)
5634 align_power = secalign;
5635 }
5636 align = (bfd_size_type) 1 << align_power;
5637 if (align < maxpagesize)
5638 align = maxpagesize;
b301b248 5639 }
252b5132 5640
02bf8d82
AM
5641 for (i = 0; i < m->count; i++)
5642 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
5643 /* If we aren't making room for this section, then
5644 it must be SHT_NOBITS regardless of what we've
5645 set via struct bfd_elf_special_section. */
5646 elf_section_type (m->sections[i]) = SHT_NOBITS;
5647
bf988460 5648 /* Find out whether this segment contains any loadable
aea274d3
AM
5649 sections. */
5650 no_contents = TRUE;
5651 for (i = 0; i < m->count; i++)
5652 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
5653 {
5654 no_contents = FALSE;
5655 break;
5656 }
bf988460 5657
66631823 5658 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
a8c75b76
AM
5659
5660 /* Broken hardware and/or kernel require that files do not
5661 map the same page with different permissions on some hppa
5662 processors. */
30fe1832
AM
5663 if (j != 0
5664 && (abfd->flags & D_PAGED) != 0
a8c75b76
AM
5665 && bed->no_page_alias
5666 && (off & (maxpagesize - 1)) != 0
502794d4
CE
5667 && ((off & -maxpagesize)
5668 == ((off + off_adjust) & -maxpagesize)))
a8c75b76 5669 off_adjust += maxpagesize;
bf988460
AM
5670 off += off_adjust;
5671 if (no_contents)
5672 {
5673 /* We shouldn't need to align the segment on disk since
5674 the segment doesn't need file space, but the gABI
5675 arguably requires the alignment and glibc ld.so
5676 checks it. So to comply with the alignment
5677 requirement but not waste file space, we adjust
5678 p_offset for just this segment. (OFF_ADJUST is
5679 subtracted from OFF later.) This may put p_offset
5680 past the end of file, but that shouldn't matter. */
5681 }
5682 else
5683 off_adjust = 0;
252b5132 5684 }
b1a6d0b1
NC
5685 /* Make sure the .dynamic section is the first section in the
5686 PT_DYNAMIC segment. */
5687 else if (p->p_type == PT_DYNAMIC
5688 && m->count > 1
5689 && strcmp (m->sections[0]->name, ".dynamic") != 0)
5690 {
5691 _bfd_error_handler
871b3ab2 5692 (_("%pB: The first section in the PT_DYNAMIC segment"
63a5468a 5693 " is not the .dynamic section"),
b301b248 5694 abfd);
b1a6d0b1
NC
5695 bfd_set_error (bfd_error_bad_value);
5696 return FALSE;
5697 }
3f001e84
JK
5698 /* Set the note section type to SHT_NOTE. */
5699 else if (p->p_type == PT_NOTE)
5700 for (i = 0; i < m->count; i++)
5701 elf_section_type (m->sections[i]) = SHT_NOTE;
252b5132 5702
252b5132
RH
5703 if (m->includes_filehdr)
5704 {
bf988460 5705 if (!m->p_flags_valid)
252b5132 5706 p->p_flags |= PF_R;
252b5132
RH
5707 p->p_filesz = bed->s->sizeof_ehdr;
5708 p->p_memsz = bed->s->sizeof_ehdr;
30fe1832 5709 if (p->p_type == PT_LOAD)
252b5132 5710 {
30fe1832 5711 if (m->count > 0)
252b5132 5712 {
30fe1832
AM
5713 if (p->p_vaddr < (bfd_vma) off
5714 || (!m->p_paddr_valid
5715 && p->p_paddr < (bfd_vma) off))
5716 {
5717 _bfd_error_handler
5718 (_("%pB: not enough room for program headers,"
5719 " try linking with -N"),
5720 abfd);
5721 bfd_set_error (bfd_error_bad_value);
5722 return FALSE;
5723 }
5724 p->p_vaddr -= off;
5725 if (!m->p_paddr_valid)
5726 p->p_paddr -= off;
252b5132 5727 }
30fe1832
AM
5728 }
5729 else if (sorted_seg_map[0]->includes_filehdr)
5730 {
5731 Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
5732 p->p_vaddr = filehdr->p_vaddr;
bf988460 5733 if (!m->p_paddr_valid)
30fe1832 5734 p->p_paddr = filehdr->p_paddr;
252b5132 5735 }
252b5132
RH
5736 }
5737
5738 if (m->includes_phdrs)
5739 {
bf988460 5740 if (!m->p_flags_valid)
252b5132 5741 p->p_flags |= PF_R;
30fe1832
AM
5742 p->p_filesz += actual * bed->s->sizeof_phdr;
5743 p->p_memsz += actual * bed->s->sizeof_phdr;
f3520d2f 5744 if (!m->includes_filehdr)
252b5132 5745 {
30fe1832 5746 if (p->p_type == PT_LOAD)
252b5132 5747 {
30fe1832
AM
5748 elf_elfheader (abfd)->e_phoff = p->p_offset;
5749 if (m->count > 0)
5750 {
5751 p->p_vaddr -= off - p->p_offset;
5752 if (!m->p_paddr_valid)
5753 p->p_paddr -= off - p->p_offset;
5754 }
5755 }
5756 else if (phdr_load_seg != NULL)
5757 {
5758 Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
502794d4 5759 bfd_vma phdr_off = 0; /* Octets. */
30fe1832
AM
5760 if (phdr_load_seg->includes_filehdr)
5761 phdr_off = bed->s->sizeof_ehdr;
5762 p->p_vaddr = phdr->p_vaddr + phdr_off;
bf988460 5763 if (!m->p_paddr_valid)
30fe1832
AM
5764 p->p_paddr = phdr->p_paddr + phdr_off;
5765 p->p_offset = phdr->p_offset + phdr_off;
252b5132 5766 }
30fe1832
AM
5767 else
5768 p->p_offset = bed->s->sizeof_ehdr;
2b0bc088 5769 }
252b5132
RH
5770 }
5771
5772 if (p->p_type == PT_LOAD
5773 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5774 {
bf988460 5775 if (!m->includes_filehdr && !m->includes_phdrs)
0bc3450e
AM
5776 {
5777 p->p_offset = off;
5778 if (no_contents)
67641dd3
AM
5779 {
5780 /* Put meaningless p_offset for PT_LOAD segments
5781 without file contents somewhere within the first
5782 page, in an attempt to not point past EOF. */
5783 bfd_size_type align = maxpagesize;
5784 if (align < p->p_align)
5785 align = p->p_align;
5786 if (align < 1)
5787 align = 1;
5788 p->p_offset = off % align;
5789 }
0bc3450e 5790 }
252b5132
RH
5791 else
5792 {
502794d4 5793 file_ptr adjust; /* Octets. */
252b5132
RH
5794
5795 adjust = off - (p->p_offset + p->p_filesz);
bf988460
AM
5796 if (!no_contents)
5797 p->p_filesz += adjust;
252b5132
RH
5798 p->p_memsz += adjust;
5799 }
5800 }
5801
1ea63fd2
AM
5802 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5803 maps. Set filepos for sections in PT_LOAD segments, and in
5804 core files, for sections in PT_NOTE segments.
5805 assign_file_positions_for_non_load_sections will set filepos
5806 for other sections and update p_filesz for other segments. */
252b5132
RH
5807 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5808 {
5809 asection *sec;
252b5132 5810 bfd_size_type align;
627b32bc 5811 Elf_Internal_Shdr *this_hdr;
252b5132
RH
5812
5813 sec = *secpp;
02bf8d82 5814 this_hdr = &elf_section_data (sec)->this_hdr;
fd361982 5815 align = (bfd_size_type) 1 << bfd_section_alignment (sec);
252b5132 5816
88967714
AM
5817 if ((p->p_type == PT_LOAD
5818 || p->p_type == PT_TLS)
5819 && (this_hdr->sh_type != SHT_NOBITS
5820 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5821 && ((this_hdr->sh_flags & SHF_TLS) == 0
5822 || p->p_type == PT_TLS))))
252b5132 5823 {
502794d4
CE
5824 bfd_vma p_start = p->p_paddr; /* Octets. */
5825 bfd_vma p_end = p_start + p->p_memsz; /* Octets. */
5826 bfd_vma s_start = sec->lma * opb; /* Octets. */
5827 bfd_vma adjust = s_start - p_end; /* Octets. */
252b5132 5828
a2d1e028
L
5829 if (adjust != 0
5830 && (s_start < p_end
5831 || p_end < p_start))
252b5132 5832 {
4eca0228 5833 _bfd_error_handler
695344c0 5834 /* xgettext:c-format */
2dcf00ce 5835 (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64),
502794d4
CE
5836 abfd, sec, (uint64_t) s_start / opb,
5837 (uint64_t) p_end / opb);
88967714 5838 adjust = 0;
502794d4 5839 sec->lma = p_end / opb;
1cfb7d1e 5840 }
3ac9b6c9 5841 p->p_memsz += adjust;
1cfb7d1e 5842
d16e3d2e 5843 if (p->p_type == PT_LOAD)
88967714 5844 {
d16e3d2e 5845 if (this_hdr->sh_type != SHT_NOBITS)
32812159 5846 {
d16e3d2e 5847 off_adjust = 0;
30fe1832
AM
5848 if (p->p_filesz + adjust < p->p_memsz)
5849 {
5850 /* We have a PROGBITS section following NOBITS ones.
5851 Allocate file space for the NOBITS section(s) and
5852 zero it. */
5853 adjust = p->p_memsz - p->p_filesz;
5854 if (!write_zeros (abfd, off, adjust))
5855 return FALSE;
5856 }
d16e3d2e
AM
5857 }
5858 /* We only adjust sh_offset in SHT_NOBITS sections
5859 as would seem proper for their address when the
5860 section is first in the segment. sh_offset
5861 doesn't really have any significance for
5862 SHT_NOBITS anyway, apart from a notional position
5863 relative to other sections. Historically we
5864 didn't bother with adjusting sh_offset and some
5865 programs depend on it not being adjusted. See
5866 pr12921 and pr25662. */
5867 if (this_hdr->sh_type != SHT_NOBITS || i == 0)
5868 {
30fe1832 5869 off += adjust;
d16e3d2e
AM
5870 if (this_hdr->sh_type == SHT_NOBITS)
5871 off_adjust += adjust;
32812159 5872 }
252b5132 5873 }
d16e3d2e
AM
5874 if (this_hdr->sh_type != SHT_NOBITS)
5875 p->p_filesz += adjust;
252b5132
RH
5876 }
5877
5878 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5879 {
b301b248
AM
5880 /* The section at i == 0 is the one that actually contains
5881 everything. */
4a938328
MS
5882 if (i == 0)
5883 {
627b32bc 5884 this_hdr->sh_offset = sec->filepos = off;
6a3cd2b4
AM
5885 off += this_hdr->sh_size;
5886 p->p_filesz = this_hdr->sh_size;
b301b248
AM
5887 p->p_memsz = 0;
5888 p->p_align = 1;
252b5132 5889 }
4a938328 5890 else
252b5132 5891 {
b301b248 5892 /* The rest are fake sections that shouldn't be written. */
252b5132 5893 sec->filepos = 0;
eea6121a 5894 sec->size = 0;
b301b248
AM
5895 sec->flags = 0;
5896 continue;
252b5132 5897 }
252b5132
RH
5898 }
5899 else
5900 {
1e951488 5901 if (p->p_type == PT_LOAD)
b301b248 5902 {
1e951488
AM
5903 this_hdr->sh_offset = sec->filepos = off;
5904 if (this_hdr->sh_type != SHT_NOBITS)
5905 off += this_hdr->sh_size;
5906 }
5907 else if (this_hdr->sh_type == SHT_NOBITS
5908 && (this_hdr->sh_flags & SHF_TLS) != 0
5909 && this_hdr->sh_offset == 0)
5910 {
5911 /* This is a .tbss section that didn't get a PT_LOAD.
5912 (See _bfd_elf_map_sections_to_segments "Create a
5913 final PT_LOAD".) Set sh_offset to the value it
5914 would have if we had created a zero p_filesz and
5915 p_memsz PT_LOAD header for the section. This
5916 also makes the PT_TLS header have the same
5917 p_offset value. */
5918 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5919 off, align);
5920 this_hdr->sh_offset = sec->filepos = off + adjust;
b301b248 5921 }
252b5132 5922
02bf8d82 5923 if (this_hdr->sh_type != SHT_NOBITS)
b301b248 5924 {
6a3cd2b4 5925 p->p_filesz += this_hdr->sh_size;
02bf8d82
AM
5926 /* A load section without SHF_ALLOC is something like
5927 a note section in a PT_NOTE segment. These take
5928 file space but are not loaded into memory. */
5929 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6a3cd2b4 5930 p->p_memsz += this_hdr->sh_size;
b301b248 5931 }
6a3cd2b4 5932 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
13ae64f3 5933 {
6a3cd2b4
AM
5934 if (p->p_type == PT_TLS)
5935 p->p_memsz += this_hdr->sh_size;
5936
5937 /* .tbss is special. It doesn't contribute to p_memsz of
5938 normal segments. */
5939 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
5940 p->p_memsz += this_hdr->sh_size;
13ae64f3
JJ
5941 }
5942
b10a8ae0
L
5943 if (align > p->p_align
5944 && !m->p_align_valid
5945 && (p->p_type != PT_LOAD
5946 || (abfd->flags & D_PAGED) == 0))
252b5132
RH
5947 p->p_align = align;
5948 }
5949
bf988460 5950 if (!m->p_flags_valid)
252b5132
RH
5951 {
5952 p->p_flags |= PF_R;
02bf8d82 5953 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
252b5132 5954 p->p_flags |= PF_X;
02bf8d82 5955 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
252b5132
RH
5956 p->p_flags |= PF_W;
5957 }
5958 }
43a8475c 5959
bf988460 5960 off -= off_adjust;
0920dee7 5961
30fe1832
AM
5962 /* PR ld/20815 - Check that the program header segment, if
5963 present, will be loaded into memory. */
5964 if (p->p_type == PT_PHDR
5965 && phdr_load_seg == NULL
5966 && !(bed->elf_backend_allow_non_load_phdr != NULL
5967 && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
5968 {
5969 /* The fix for this error is usually to edit the linker script being
5970 used and set up the program headers manually. Either that or
5971 leave room for the headers at the start of the SECTIONS. */
5972 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
5973 " by LOAD segment"),
5974 abfd);
7b3c2715
AM
5975 if (link_info == NULL)
5976 return FALSE;
5977 /* Arrange for the linker to exit with an error, deleting
5978 the output file unless --noinhibit-exec is given. */
5979 link_info->callbacks->info ("%X");
30fe1832
AM
5980 }
5981
7c928300
AM
5982 /* Check that all sections are in a PT_LOAD segment.
5983 Don't check funky gdb generated core files. */
5984 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
9a83a553
AM
5985 {
5986 bfd_boolean check_vma = TRUE;
5987
5988 for (i = 1; i < m->count; i++)
5989 if (m->sections[i]->vma == m->sections[i - 1]->vma
5990 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
5991 ->this_hdr), p) != 0
5992 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
5993 ->this_hdr), p) != 0)
0920dee7 5994 {
9a83a553
AM
5995 /* Looks like we have overlays packed into the segment. */
5996 check_vma = FALSE;
5997 break;
0920dee7 5998 }
9a83a553
AM
5999
6000 for (i = 0; i < m->count; i++)
6001 {
6002 Elf_Internal_Shdr *this_hdr;
6003 asection *sec;
6004
6005 sec = m->sections[i];
6006 this_hdr = &(elf_section_data(sec)->this_hdr);
86b2281f
AM
6007 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
6008 && !ELF_TBSS_SPECIAL (this_hdr, p))
9a83a553 6009 {
4eca0228 6010 _bfd_error_handler
695344c0 6011 /* xgettext:c-format */
871b3ab2 6012 (_("%pB: section `%pA' can't be allocated in segment %d"),
9a83a553
AM
6013 abfd, sec, j);
6014 print_segment_map (m);
6015 }
6016 }
6017 }
252b5132
RH
6018 }
6019
12bd6957 6020 elf_next_file_pos (abfd) = off;
30fe1832
AM
6021
6022 if (link_info != NULL
6023 && phdr_load_seg != NULL
6024 && phdr_load_seg->includes_filehdr)
6025 {
6026 /* There is a segment that contains both the file headers and the
6027 program headers, so provide a symbol __ehdr_start pointing there.
6028 A program can use this to examine itself robustly. */
6029
6030 struct elf_link_hash_entry *hash
6031 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
6032 FALSE, FALSE, TRUE);
6033 /* If the symbol was referenced and not defined, define it. */
6034 if (hash != NULL
6035 && (hash->root.type == bfd_link_hash_new
6036 || hash->root.type == bfd_link_hash_undefined
6037 || hash->root.type == bfd_link_hash_undefweak
6038 || hash->root.type == bfd_link_hash_common))
6039 {
6040 asection *s = NULL;
66631823 6041 bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
30fe1832
AM
6042
6043 if (phdr_load_seg->count != 0)
6044 /* The segment contains sections, so use the first one. */
6045 s = phdr_load_seg->sections[0];
6046 else
6047 /* Use the first (i.e. lowest-addressed) section in any segment. */
6048 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6049 if (m->p_type == PT_LOAD && m->count != 0)
6050 {
6051 s = m->sections[0];
6052 break;
6053 }
6054
6055 if (s != NULL)
6056 {
6057 hash->root.u.def.value = filehdr_vaddr - s->vma;
6058 hash->root.u.def.section = s;
6059 }
6060 else
6061 {
6062 hash->root.u.def.value = filehdr_vaddr;
6063 hash->root.u.def.section = bfd_abs_section_ptr;
6064 }
6065
6066 hash->root.type = bfd_link_hash_defined;
6067 hash->def_regular = 1;
6068 hash->non_elf = 0;
6069 }
6070 }
6071
f3520d2f
AM
6072 return TRUE;
6073}
6074
1faa385f
NC
6075/* Determine if a bfd is a debuginfo file. Unfortunately there
6076 is no defined method for detecting such files, so we have to
6077 use heuristics instead. */
6078
6079bfd_boolean
6080is_debuginfo_file (bfd *abfd)
6081{
6082 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6083 return FALSE;
6084
6085 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6086 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6087 Elf_Internal_Shdr **headerp;
6088
6089 for (headerp = start_headers; headerp < end_headers; headerp ++)
6090 {
6091 Elf_Internal_Shdr *header = * headerp;
6092
6093 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6094 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6095 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6096 && header->sh_type != SHT_NOBITS
6097 && header->sh_type != SHT_NOTE)
6098 return FALSE;
6099 }
6100
6101 return TRUE;
6102}
6103
1ff6de03
NA
6104/* Assign file positions for the other sections, except for compressed debugging
6105 and other sections assigned in _bfd_elf_assign_file_positions_for_non_load(). */
f3520d2f
AM
6106
6107static bfd_boolean
6108assign_file_positions_for_non_load_sections (bfd *abfd,
6109 struct bfd_link_info *link_info)
6110{
6111 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6112 Elf_Internal_Shdr **i_shdrpp;
e06efbf1 6113 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
f3520d2f
AM
6114 Elf_Internal_Phdr *phdrs;
6115 Elf_Internal_Phdr *p;
6116 struct elf_segment_map *m;
f3520d2f 6117 file_ptr off;
66631823 6118 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
f3520d2f 6119
5c182d5f 6120 i_shdrpp = elf_elfsections (abfd);
e06efbf1 6121 end_hdrpp = i_shdrpp + elf_numsections (abfd);
12bd6957 6122 off = elf_next_file_pos (abfd);
e06efbf1 6123 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
5c182d5f 6124 {
5c182d5f
AM
6125 Elf_Internal_Shdr *hdr;
6126
6127 hdr = *hdrpp;
6128 if (hdr->bfd_section != NULL
252e386e
AM
6129 && (hdr->bfd_section->filepos != 0
6130 || (hdr->sh_type == SHT_NOBITS
6131 && hdr->contents == NULL)))
627b32bc 6132 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
5c182d5f
AM
6133 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6134 {
1faa385f
NC
6135 if (hdr->sh_size != 0
6136 /* PR 24717 - debuginfo files are known to be not strictly
6137 compliant with the ELF standard. In particular they often
6138 have .note.gnu.property sections that are outside of any
6139 loadable segment. This is not a problem for such files,
6140 so do not warn about them. */
6141 && ! is_debuginfo_file (abfd))
4eca0228 6142 _bfd_error_handler
695344c0 6143 /* xgettext:c-format */
871b3ab2 6144 (_("%pB: warning: allocated section `%s' not in segment"),
e8d2ba53
AM
6145 abfd,
6146 (hdr->bfd_section == NULL
6147 ? "*unknown*"
6148 : hdr->bfd_section->name));
3ba71138
L
6149 /* We don't need to page align empty sections. */
6150 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
5c182d5f
AM
6151 off += vma_page_aligned_bias (hdr->sh_addr, off,
6152 bed->maxpagesize);
6153 else
6154 off += vma_page_aligned_bias (hdr->sh_addr, off,
6155 hdr->sh_addralign);
6156 off = _bfd_elf_assign_file_position_for_section (hdr, off,
6157 FALSE);
6158 }
6159 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6160 && hdr->bfd_section == NULL)
1ff6de03
NA
6161 /* We don't know the offset of these sections yet: their size has
6162 not been decided. */
0ce398f1 6163 || (hdr->bfd_section != NULL
1ff6de03
NA
6164 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
6165 || (bfd_section_is_ctf (hdr->bfd_section)
6166 && abfd->is_linker_output)))
12bd6957 6167 || hdr == i_shdrpp[elf_onesymtab (abfd)]
6a40cf0c
NC
6168 || (elf_symtab_shndx_list (abfd) != NULL
6169 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
3e19fb8f
L
6170 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6171 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
5c182d5f
AM
6172 hdr->sh_offset = -1;
6173 else
6174 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5c182d5f 6175 }
30fe1832 6176 elf_next_file_pos (abfd) = off;
5c182d5f 6177
252b5132
RH
6178 /* Now that we have set the section file positions, we can set up
6179 the file positions for the non PT_LOAD segments. */
f3520d2f 6180 phdrs = elf_tdata (abfd)->phdr;
12bd6957 6181 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
252b5132 6182 {
129af99f 6183 if (p->p_type == PT_GNU_RELRO)
252b5132 6184 {
66631823 6185 bfd_vma start, end; /* Bytes. */
01f7e10c 6186 bfd_boolean ok;
1ea63fd2 6187
129af99f 6188 if (link_info != NULL)
8c37241b 6189 {
129af99f 6190 /* During linking the range of the RELRO segment is passed
f2731e0c
AM
6191 in link_info. Note that there may be padding between
6192 relro_start and the first RELRO section. */
6193 start = link_info->relro_start;
6194 end = link_info->relro_end;
6195 }
6196 else if (m->count != 0)
6197 {
6198 if (!m->p_size_valid)
6199 abort ();
6200 start = m->sections[0]->vma;
66631823 6201 end = start + m->p_size / opb;
f2731e0c
AM
6202 }
6203 else
6204 {
6205 start = 0;
6206 end = 0;
6207 }
6208
01f7e10c 6209 ok = FALSE;
f2731e0c
AM
6210 if (start < end)
6211 {
6212 struct elf_segment_map *lm;
6213 const Elf_Internal_Phdr *lp;
6214 unsigned int i;
6215
6216 /* Find a LOAD segment containing a section in the RELRO
6217 segment. */
12bd6957 6218 for (lm = elf_seg_map (abfd), lp = phdrs;
3146fac4
AM
6219 lm != NULL;
6220 lm = lm->next, lp++)
8c37241b
JJ
6221 {
6222 if (lp->p_type == PT_LOAD
3146fac4 6223 && lm->count != 0
dbc88fc1
AM
6224 && (lm->sections[lm->count - 1]->vma
6225 + (!IS_TBSS (lm->sections[lm->count - 1])
66631823 6226 ? lm->sections[lm->count - 1]->size / opb
dbc88fc1 6227 : 0)) > start
f2731e0c 6228 && lm->sections[0]->vma < end)
8c37241b
JJ
6229 break;
6230 }
f2731e0c 6231
01f7e10c 6232 if (lm != NULL)
129af99f 6233 {
01f7e10c
AM
6234 /* Find the section starting the RELRO segment. */
6235 for (i = 0; i < lm->count; i++)
6236 {
6237 asection *s = lm->sections[i];
6238 if (s->vma >= start
6239 && s->vma < end
6240 && s->size != 0)
6241 break;
6242 }
6243
6244 if (i < lm->count)
6245 {
502794d4
CE
6246 p->p_vaddr = lm->sections[i]->vma * opb;
6247 p->p_paddr = lm->sections[i]->lma * opb;
01f7e10c 6248 p->p_offset = lm->sections[i]->filepos;
66631823 6249 p->p_memsz = end * opb - p->p_vaddr;
01f7e10c
AM
6250 p->p_filesz = p->p_memsz;
6251
6252 /* The RELRO segment typically ends a few bytes
6253 into .got.plt but other layouts are possible.
6254 In cases where the end does not match any
6255 loaded section (for instance is in file
6256 padding), trim p_filesz back to correspond to
6257 the end of loaded section contents. */
6258 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6259 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6260
6261 /* Preserve the alignment and flags if they are
6262 valid. The gold linker generates RW/4 for
6263 the PT_GNU_RELRO section. It is better for
6264 objcopy/strip to honor these attributes
6265 otherwise gdb will choke when using separate
6266 debug files. */
6267 if (!m->p_align_valid)
6268 p->p_align = 1;
6269 if (!m->p_flags_valid)
6270 p->p_flags = PF_R;
6271 ok = TRUE;
6272 }
129af99f 6273 }
b84a33b5 6274 }
01f7e10c
AM
6275 if (link_info != NULL)
6276 BFD_ASSERT (ok);
6277 if (!ok)
6278 memset (p, 0, sizeof *p);
129af99f 6279 }
04c3a755
NS
6280 else if (p->p_type == PT_GNU_STACK)
6281 {
6282 if (m->p_size_valid)
6283 p->p_memsz = m->p_size;
6284 }
129af99f
AS
6285 else if (m->count != 0)
6286 {
e06efbf1 6287 unsigned int i;
1a9ccd70 6288
129af99f
AS
6289 if (p->p_type != PT_LOAD
6290 && (p->p_type != PT_NOTE
6291 || bfd_get_format (abfd) != bfd_core))
6292 {
1a9ccd70
NC
6293 /* A user specified segment layout may include a PHDR
6294 segment that overlaps with a LOAD segment... */
6295 if (p->p_type == PT_PHDR)
6296 {
6297 m->count = 0;
6298 continue;
6299 }
6300
c86934ce
NC
6301 if (m->includes_filehdr || m->includes_phdrs)
6302 {
b1fa9dd6 6303 /* PR 17512: file: 2195325e. */
4eca0228 6304 _bfd_error_handler
871b3ab2 6305 (_("%pB: error: non-load segment %d includes file header "
76cfced5
AM
6306 "and/or program header"),
6307 abfd, (int) (p - phdrs));
c86934ce
NC
6308 return FALSE;
6309 }
129af99f 6310
86b2281f 6311 p->p_filesz = 0;
129af99f 6312 p->p_offset = m->sections[0]->filepos;
86b2281f
AM
6313 for (i = m->count; i-- != 0;)
6314 {
6315 asection *sect = m->sections[i];
6316 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6317 if (hdr->sh_type != SHT_NOBITS)
6318 {
6319 p->p_filesz = (sect->filepos - m->sections[0]->filepos
6320 + hdr->sh_size);
6321 break;
6322 }
6323 }
129af99f
AS
6324 }
6325 }
252b5132
RH
6326 }
6327
b34976b6 6328 return TRUE;
252b5132
RH
6329}
6330
6a40cf0c
NC
6331static elf_section_list *
6332find_section_in_list (unsigned int i, elf_section_list * list)
6333{
6334 for (;list != NULL; list = list->next)
6335 if (list->ndx == i)
6336 break;
6337 return list;
6338}
6339
252b5132
RH
6340/* Work out the file positions of all the sections. This is called by
6341 _bfd_elf_compute_section_file_positions. All the section sizes and
6342 VMAs must be known before this is called.
6343
e0638f70 6344 Reloc sections come in two flavours: Those processed specially as
1ff6de03
NA
6345 "side-channel" data attached to a section to which they apply, and those that
6346 bfd doesn't process as relocations. The latter sort are stored in a normal
6347 bfd section by bfd_section_from_shdr. We don't consider the former sort
6348 here, unless they form part of the loadable image. Reloc sections not
6349 assigned here (and compressed debugging sections and CTF sections which
6350 nothing else in the file can rely upon) will be handled later by
e0638f70 6351 assign_file_positions_for_relocs.
252b5132
RH
6352
6353 We also don't set the positions of the .symtab and .strtab here. */
6354
b34976b6 6355static bfd_boolean
c84fca4d
AO
6356assign_file_positions_except_relocs (bfd *abfd,
6357 struct bfd_link_info *link_info)
252b5132 6358{
5c182d5f
AM
6359 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6360 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
9c5bfbb7 6361 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6d6c25c8 6362 unsigned int alloc;
252b5132
RH
6363
6364 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6365 && bfd_get_format (abfd) != bfd_core)
6366 {
5c182d5f
AM
6367 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6368 unsigned int num_sec = elf_numsections (abfd);
252b5132
RH
6369 Elf_Internal_Shdr **hdrpp;
6370 unsigned int i;
a485e98e 6371 file_ptr off;
252b5132
RH
6372
6373 /* Start after the ELF header. */
6374 off = i_ehdrp->e_ehsize;
6375
6376 /* We are not creating an executable, which means that we are
6377 not creating a program header, and that the actual order of
6378 the sections in the file is unimportant. */
9ad5cbcf 6379 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
252b5132
RH
6380 {
6381 Elf_Internal_Shdr *hdr;
6382
6383 hdr = *hdrpp;
e0638f70
AM
6384 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6385 && hdr->bfd_section == NULL)
1ff6de03
NA
6386 /* Do not assign offsets for these sections yet: we don't know
6387 their sizes. */
0ce398f1 6388 || (hdr->bfd_section != NULL
1ff6de03
NA
6389 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
6390 || (bfd_section_is_ctf (hdr->bfd_section)
6391 && abfd->is_linker_output)))
12bd6957 6392 || i == elf_onesymtab (abfd)
6a40cf0c
NC
6393 || (elf_symtab_shndx_list (abfd) != NULL
6394 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
3e19fb8f
L
6395 || i == elf_strtab_sec (abfd)
6396 || i == elf_shstrtab_sec (abfd))
252b5132
RH
6397 {
6398 hdr->sh_offset = -1;
252b5132 6399 }
9ad5cbcf 6400 else
b34976b6 6401 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
252b5132 6402 }
a485e98e
AM
6403
6404 elf_next_file_pos (abfd) = off;
6d6c25c8 6405 elf_program_header_size (abfd) = 0;
252b5132
RH
6406 }
6407 else
6408 {
252b5132 6409 /* Assign file positions for the loaded sections based on the
08a40648 6410 assignment of sections to segments. */
f3520d2f
AM
6411 if (!assign_file_positions_for_load_sections (abfd, link_info))
6412 return FALSE;
6413
6414 /* And for non-load sections. */
6415 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6416 return FALSE;
6d6c25c8 6417 }
f3520d2f 6418
6d6c25c8
AM
6419 if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
6420 return FALSE;
1a9ccd70 6421
6d6c25c8
AM
6422 /* Write out the program headers. */
6423 alloc = i_ehdrp->e_phnum;
6424 if (alloc != 0)
6425 {
30fe1832 6426 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
cd584857
NC
6427 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6428 return FALSE;
252b5132
RH
6429 }
6430
b34976b6 6431 return TRUE;
252b5132
RH
6432}
6433
ed7e9d0b
AM
6434bfd_boolean
6435_bfd_elf_init_file_header (bfd *abfd,
6436 struct bfd_link_info *info ATTRIBUTE_UNUSED)
252b5132 6437{
3d540e93 6438 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
2b0f7ef9 6439 struct elf_strtab_hash *shstrtab;
9c5bfbb7 6440 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
6441
6442 i_ehdrp = elf_elfheader (abfd);
252b5132 6443
2b0f7ef9 6444 shstrtab = _bfd_elf_strtab_init ();
252b5132 6445 if (shstrtab == NULL)
b34976b6 6446 return FALSE;
252b5132
RH
6447
6448 elf_shstrtab (abfd) = shstrtab;
6449
6450 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6451 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6452 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6453 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6454
6455 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6456 i_ehdrp->e_ident[EI_DATA] =
6457 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6458 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6459
252b5132
RH
6460 if ((abfd->flags & DYNAMIC) != 0)
6461 i_ehdrp->e_type = ET_DYN;
6462 else if ((abfd->flags & EXEC_P) != 0)
6463 i_ehdrp->e_type = ET_EXEC;
6464 else if (bfd_get_format (abfd) == bfd_core)
6465 i_ehdrp->e_type = ET_CORE;
6466 else
6467 i_ehdrp->e_type = ET_REL;
6468
6469 switch (bfd_get_arch (abfd))
6470 {
6471 case bfd_arch_unknown:
6472 i_ehdrp->e_machine = EM_NONE;
6473 break;
aa4f99bb
AO
6474
6475 /* There used to be a long list of cases here, each one setting
6476 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6477 in the corresponding bfd definition. To avoid duplication,
6478 the switch was removed. Machines that need special handling
6479 can generally do it in elf_backend_final_write_processing(),
6480 unless they need the information earlier than the final write.
6481 Such need can generally be supplied by replacing the tests for
6482 e_machine with the conditions used to determine it. */
252b5132 6483 default:
9c5bfbb7
AM
6484 i_ehdrp->e_machine = bed->elf_machine_code;
6485 }
aa4f99bb 6486
252b5132
RH
6487 i_ehdrp->e_version = bed->s->ev_current;
6488 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6489
c044fabd 6490 /* No program header, for now. */
252b5132
RH
6491 i_ehdrp->e_phoff = 0;
6492 i_ehdrp->e_phentsize = 0;
6493 i_ehdrp->e_phnum = 0;
6494
c044fabd 6495 /* Each bfd section is section header entry. */
252b5132
RH
6496 i_ehdrp->e_entry = bfd_get_start_address (abfd);
6497 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6498
252b5132 6499 elf_tdata (abfd)->symtab_hdr.sh_name =
b34976b6 6500 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
252b5132 6501 elf_tdata (abfd)->strtab_hdr.sh_name =
b34976b6 6502 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
252b5132 6503 elf_tdata (abfd)->shstrtab_hdr.sh_name =
b34976b6 6504 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
252b5132 6505 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
17ca87fc 6506 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
252b5132 6507 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
b34976b6 6508 return FALSE;
252b5132 6509
b34976b6 6510 return TRUE;
252b5132
RH
6511}
6512
6d6c25c8
AM
6513/* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
6514
6515 FIXME: We used to have code here to sort the PT_LOAD segments into
6516 ascending order, as per the ELF spec. But this breaks some programs,
6517 including the Linux kernel. But really either the spec should be
6518 changed or the programs updated. */
6519
6520bfd_boolean
6521_bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
6522{
6523 if (link_info != NULL && bfd_link_pie (link_info))
6524 {
6525 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
6526 unsigned int num_segments = i_ehdrp->e_phnum;
6527 struct elf_obj_tdata *tdata = elf_tdata (obfd);
6528 Elf_Internal_Phdr *segment = tdata->phdr;
6529 Elf_Internal_Phdr *end_segment = &segment[num_segments];
6530
6531 /* Find the lowest p_vaddr in PT_LOAD segments. */
6532 bfd_vma p_vaddr = (bfd_vma) -1;
6533 for (; segment < end_segment; segment++)
6534 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
6535 p_vaddr = segment->p_vaddr;
6536
6537 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6538 segments is non-zero. */
6539 if (p_vaddr)
6540 i_ehdrp->e_type = ET_EXEC;
6541 }
6542 return TRUE;
6543}
6544
252b5132 6545/* Assign file positions for all the reloc sections which are not part
a485e98e 6546 of the loadable file image, and the file position of section headers. */
252b5132 6547
0ce398f1
L
6548static bfd_boolean
6549_bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
252b5132
RH
6550{
6551 file_ptr off;
e06efbf1 6552 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
3e19fb8f 6553 Elf_Internal_Shdr *shdrp;
a485e98e
AM
6554 Elf_Internal_Ehdr *i_ehdrp;
6555 const struct elf_backend_data *bed;
252b5132 6556
12bd6957 6557 off = elf_next_file_pos (abfd);
252b5132 6558
e06efbf1
L
6559 shdrpp = elf_elfsections (abfd);
6560 end_shdrpp = shdrpp + elf_numsections (abfd);
6561 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
252b5132 6562 {
252b5132 6563 shdrp = *shdrpp;
0ce398f1
L
6564 if (shdrp->sh_offset == -1)
6565 {
3e19fb8f 6566 asection *sec = shdrp->bfd_section;
0ce398f1
L
6567 bfd_boolean is_rel = (shdrp->sh_type == SHT_REL
6568 || shdrp->sh_type == SHT_RELA);
1ff6de03 6569 bfd_boolean is_ctf = sec && bfd_section_is_ctf (sec);
0ce398f1 6570 if (is_rel
1ff6de03 6571 || is_ctf
3e19fb8f 6572 || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
0ce398f1 6573 {
1ff6de03 6574 if (!is_rel && !is_ctf)
0ce398f1 6575 {
3e19fb8f
L
6576 const char *name = sec->name;
6577 struct bfd_elf_section_data *d;
6578
0ce398f1 6579 /* Compress DWARF debug sections. */
3e19fb8f 6580 if (!bfd_compress_section (abfd, sec,
0ce398f1
L
6581 shdrp->contents))
6582 return FALSE;
3e19fb8f
L
6583
6584 if (sec->compress_status == COMPRESS_SECTION_DONE
6585 && (abfd->flags & BFD_COMPRESS_GABI) == 0)
6586 {
6587 /* If section is compressed with zlib-gnu, convert
6588 section name from .debug_* to .zdebug_*. */
6589 char *new_name
6590 = convert_debug_to_zdebug (abfd, name);
6591 if (new_name == NULL)
6592 return FALSE;
6593 name = new_name;
6594 }
dd905818 6595 /* Add section name to section name section. */
3e19fb8f
L
6596 if (shdrp->sh_name != (unsigned int) -1)
6597 abort ();
6598 shdrp->sh_name
6599 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
6600 name, FALSE);
6601 d = elf_section_data (sec);
6602
dd905818 6603 /* Add reloc section name to section name section. */
3e19fb8f
L
6604 if (d->rel.hdr
6605 && !_bfd_elf_set_reloc_sh_name (abfd,
6606 d->rel.hdr,
6607 name, FALSE))
6608 return FALSE;
6609 if (d->rela.hdr
6610 && !_bfd_elf_set_reloc_sh_name (abfd,
6611 d->rela.hdr,
91cb26da 6612 name, TRUE))
3e19fb8f
L
6613 return FALSE;
6614
0ce398f1 6615 /* Update section size and contents. */
3e19fb8f
L
6616 shdrp->sh_size = sec->size;
6617 shdrp->contents = sec->contents;
0ce398f1
L
6618 shdrp->bfd_section->contents = NULL;
6619 }
1ff6de03
NA
6620 else if (is_ctf)
6621 {
6622 /* Update section size and contents. */
6623 shdrp->sh_size = sec->size;
6624 shdrp->contents = sec->contents;
6625 }
6626
0ce398f1
L
6627 off = _bfd_elf_assign_file_position_for_section (shdrp,
6628 off,
6629 TRUE);
6630 }
6631 }
252b5132
RH
6632 }
6633
3e19fb8f
L
6634 /* Place section name section after DWARF debug sections have been
6635 compressed. */
6636 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
6637 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
6638 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
6639 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
6640
6641 /* Place the section headers. */
a485e98e
AM
6642 i_ehdrp = elf_elfheader (abfd);
6643 bed = get_elf_backend_data (abfd);
6644 off = align_file_position (off, 1 << bed->s->log_file_align);
6645 i_ehdrp->e_shoff = off;
6646 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
12bd6957 6647 elf_next_file_pos (abfd) = off;
0ce398f1
L
6648
6649 return TRUE;
252b5132
RH
6650}
6651
b34976b6 6652bfd_boolean
217aa764 6653_bfd_elf_write_object_contents (bfd *abfd)
252b5132 6654{
9c5bfbb7 6655 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 6656 Elf_Internal_Shdr **i_shdrp;
b34976b6 6657 bfd_boolean failed;
9ad5cbcf 6658 unsigned int count, num_sec;
30e8ee25 6659 struct elf_obj_tdata *t;
252b5132
RH
6660
6661 if (! abfd->output_has_begun
217aa764 6662 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
b34976b6 6663 return FALSE;
db727370
JL
6664 /* Do not rewrite ELF data when the BFD has been opened for update.
6665 abfd->output_has_begun was set to TRUE on opening, so creation of new
6666 sections, and modification of existing section sizes was restricted.
6667 This means the ELF header, program headers and section headers can't have
6668 changed.
6669 If the contents of any sections has been modified, then those changes have
6670 already been written to the BFD. */
6671 else if (abfd->direction == both_direction)
6672 {
6673 BFD_ASSERT (abfd->output_has_begun);
6674 return TRUE;
6675 }
252b5132
RH
6676
6677 i_shdrp = elf_elfsections (abfd);
252b5132 6678
b34976b6 6679 failed = FALSE;
252b5132
RH
6680 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
6681 if (failed)
b34976b6 6682 return FALSE;
252b5132 6683
0ce398f1
L
6684 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
6685 return FALSE;
252b5132 6686
c044fabd 6687 /* After writing the headers, we need to write the sections too... */
9ad5cbcf
AM
6688 num_sec = elf_numsections (abfd);
6689 for (count = 1; count < num_sec; count++)
252b5132 6690 {
3e19fb8f
L
6691 i_shdrp[count]->sh_name
6692 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
6693 i_shdrp[count]->sh_name);
252b5132 6694 if (bed->elf_backend_section_processing)
75506100
MR
6695 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
6696 return FALSE;
252b5132
RH
6697 if (i_shdrp[count]->contents)
6698 {
dc810e39
AM
6699 bfd_size_type amt = i_shdrp[count]->sh_size;
6700
252b5132 6701 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
dc810e39 6702 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
b34976b6 6703 return FALSE;
252b5132
RH
6704 }
6705 }
6706
6707 /* Write out the section header names. */
30e8ee25 6708 t = elf_tdata (abfd);
26ae6d5e 6709 if (elf_shstrtab (abfd) != NULL
30e8ee25 6710 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
08a40648 6711 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
b34976b6 6712 return FALSE;
252b5132 6713
cc364be6
AM
6714 if (!(*bed->elf_backend_final_write_processing) (abfd))
6715 return FALSE;
252b5132 6716
ff59fc36
RM
6717 if (!bed->s->write_shdrs_and_ehdr (abfd))
6718 return FALSE;
6719
6720 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
c0355132
AM
6721 if (t->o->build_id.after_write_object_contents != NULL)
6722 return (*t->o->build_id.after_write_object_contents) (abfd);
ff59fc36
RM
6723
6724 return TRUE;
252b5132
RH
6725}
6726
b34976b6 6727bfd_boolean
217aa764 6728_bfd_elf_write_corefile_contents (bfd *abfd)
252b5132 6729{
c044fabd 6730 /* Hopefully this can be done just like an object file. */
252b5132
RH
6731 return _bfd_elf_write_object_contents (abfd);
6732}
c044fabd
KH
6733
6734/* Given a section, search the header to find them. */
6735
cb33740c 6736unsigned int
198beae2 6737_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
252b5132 6738{
9c5bfbb7 6739 const struct elf_backend_data *bed;
91d6fa6a 6740 unsigned int sec_index;
252b5132 6741
9ad5cbcf
AM
6742 if (elf_section_data (asect) != NULL
6743 && elf_section_data (asect)->this_idx != 0)
6744 return elf_section_data (asect)->this_idx;
6745
6746 if (bfd_is_abs_section (asect))
91d6fa6a 6747 sec_index = SHN_ABS;
af746e92 6748 else if (bfd_is_com_section (asect))
91d6fa6a 6749 sec_index = SHN_COMMON;
af746e92 6750 else if (bfd_is_und_section (asect))
91d6fa6a 6751 sec_index = SHN_UNDEF;
af746e92 6752 else
91d6fa6a 6753 sec_index = SHN_BAD;
252b5132 6754
af746e92 6755 bed = get_elf_backend_data (abfd);
252b5132
RH
6756 if (bed->elf_backend_section_from_bfd_section)
6757 {
91d6fa6a 6758 int retval = sec_index;
9ad5cbcf 6759
af746e92
AM
6760 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6761 return retval;
252b5132
RH
6762 }
6763
91d6fa6a 6764 if (sec_index == SHN_BAD)
af746e92 6765 bfd_set_error (bfd_error_nonrepresentable_section);
252b5132 6766
91d6fa6a 6767 return sec_index;
252b5132
RH
6768}
6769
6770/* Given a BFD symbol, return the index in the ELF symbol table, or -1
6771 on error. */
6772
6773int
217aa764 6774_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
252b5132
RH
6775{
6776 asymbol *asym_ptr = *asym_ptr_ptr;
6777 int idx;
6778 flagword flags = asym_ptr->flags;
6779
6780 /* When gas creates relocations against local labels, it creates its
6781 own symbol for the section, but does put the symbol into the
6782 symbol chain, so udata is 0. When the linker is generating
6783 relocatable output, this section symbol may be for one of the
6784 input sections rather than the output section. */
6785 if (asym_ptr->udata.i == 0
6786 && (flags & BSF_SECTION_SYM)
6787 && asym_ptr->section)
6788 {
5372391b 6789 asection *sec;
252b5132
RH
6790 int indx;
6791
5372391b
AM
6792 sec = asym_ptr->section;
6793 if (sec->owner != abfd && sec->output_section != NULL)
6794 sec = sec->output_section;
6795 if (sec->owner == abfd
6796 && (indx = sec->index) < elf_num_section_syms (abfd)
4e89ac30 6797 && elf_section_syms (abfd)[indx] != NULL)
252b5132
RH
6798 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
6799 }
6800
6801 idx = asym_ptr->udata.i;
6802
6803 if (idx == 0)
6804 {
6805 /* This case can occur when using --strip-symbol on a symbol
08a40648 6806 which is used in a relocation entry. */
4eca0228 6807 _bfd_error_handler
695344c0 6808 /* xgettext:c-format */
871b3ab2 6809 (_("%pB: symbol `%s' required but not present"),
d003868e 6810 abfd, bfd_asymbol_name (asym_ptr));
252b5132
RH
6811 bfd_set_error (bfd_error_no_symbols);
6812 return -1;
6813 }
6814
6815#if DEBUG & 4
6816 {
6817 fprintf (stderr,
cd9af601
AM
6818 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6819 (long) asym_ptr, asym_ptr->name, idx, flags);
252b5132
RH
6820 fflush (stderr);
6821 }
6822#endif
6823
6824 return idx;
6825}
6826
84d1d650 6827/* Rewrite program header information. */
252b5132 6828
b34976b6 6829static bfd_boolean
84d1d650 6830rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
252b5132 6831{
b34976b6
AM
6832 Elf_Internal_Ehdr *iehdr;
6833 struct elf_segment_map *map;
6834 struct elf_segment_map *map_first;
6835 struct elf_segment_map **pointer_to_map;
6836 Elf_Internal_Phdr *segment;
6837 asection *section;
6838 unsigned int i;
6839 unsigned int num_segments;
6840 bfd_boolean phdr_included = FALSE;
5c44b38e 6841 bfd_boolean p_paddr_valid;
b34976b6
AM
6842 bfd_vma maxpagesize;
6843 struct elf_segment_map *phdr_adjust_seg = NULL;
6844 unsigned int phdr_adjust_num = 0;
9c5bfbb7 6845 const struct elf_backend_data *bed;
502794d4 6846 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
bc67d8a6 6847
caf47ea6 6848 bed = get_elf_backend_data (ibfd);
252b5132
RH
6849 iehdr = elf_elfheader (ibfd);
6850
bc67d8a6 6851 map_first = NULL;
c044fabd 6852 pointer_to_map = &map_first;
252b5132
RH
6853
6854 num_segments = elf_elfheader (ibfd)->e_phnum;
bc67d8a6
NC
6855 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
6856
6857 /* Returns the end address of the segment + 1. */
aecc8f8a
AM
6858#define SEGMENT_END(segment, start) \
6859 (start + (segment->p_memsz > segment->p_filesz \
6860 ? segment->p_memsz : segment->p_filesz))
bc67d8a6 6861
eecdbe52
JJ
6862#define SECTION_SIZE(section, segment) \
6863 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6864 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
eea6121a 6865 ? section->size : 0)
eecdbe52 6866
b34976b6 6867 /* Returns TRUE if the given section is contained within
bc67d8a6 6868 the given segment. VMA addresses are compared. */
502794d4
CE
6869#define IS_CONTAINED_BY_VMA(section, segment, opb) \
6870 (section->vma * (opb) >= segment->p_vaddr \
6871 && (section->vma * (opb) + SECTION_SIZE (section, segment) \
aecc8f8a 6872 <= (SEGMENT_END (segment, segment->p_vaddr))))
c044fabd 6873
b34976b6 6874 /* Returns TRUE if the given section is contained within
bc67d8a6 6875 the given segment. LMA addresses are compared. */
502794d4
CE
6876#define IS_CONTAINED_BY_LMA(section, segment, base, opb) \
6877 (section->lma * (opb) >= base \
6878 && (section->lma + SECTION_SIZE (section, segment) / (opb) >= section->lma) \
6879 && (section->lma * (opb) + SECTION_SIZE (section, segment) \
aecc8f8a 6880 <= SEGMENT_END (segment, base)))
252b5132 6881
0efc80c8
L
6882 /* Handle PT_NOTE segment. */
6883#define IS_NOTE(p, s) \
aecc8f8a 6884 (p->p_type == PT_NOTE \
0efc80c8 6885 && elf_section_type (s) == SHT_NOTE \
aecc8f8a 6886 && (bfd_vma) s->filepos >= p->p_offset \
cb3ff1e5 6887 && ((bfd_vma) s->filepos + s->size \
aecc8f8a 6888 <= p->p_offset + p->p_filesz))
252b5132 6889
0efc80c8
L
6890 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6891 etc. */
6892#define IS_COREFILE_NOTE(p, s) \
6893 (IS_NOTE (p, s) \
6894 && bfd_get_format (ibfd) == bfd_core \
6895 && s->vma == 0 \
6896 && s->lma == 0)
6897
252b5132
RH
6898 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6899 linker, which generates a PT_INTERP section with p_vaddr and
6900 p_memsz set to 0. */
aecc8f8a
AM
6901#define IS_SOLARIS_PT_INTERP(p, s) \
6902 (p->p_vaddr == 0 \
6903 && p->p_paddr == 0 \
6904 && p->p_memsz == 0 \
6905 && p->p_filesz > 0 \
6906 && (s->flags & SEC_HAS_CONTENTS) != 0 \
eea6121a 6907 && s->size > 0 \
aecc8f8a 6908 && (bfd_vma) s->filepos >= p->p_offset \
cb3ff1e5 6909 && ((bfd_vma) s->filepos + s->size \
aecc8f8a 6910 <= p->p_offset + p->p_filesz))
5c440b1e 6911
bc67d8a6
NC
6912 /* Decide if the given section should be included in the given segment.
6913 A section will be included if:
f5ffc919 6914 1. It is within the address space of the segment -- we use the LMA
08a40648 6915 if that is set for the segment and the VMA otherwise,
0efc80c8 6916 2. It is an allocated section or a NOTE section in a PT_NOTE
d324f6d6 6917 segment.
bc67d8a6 6918 3. There is an output section associated with it,
eecdbe52 6919 4. The section has not already been allocated to a previous segment.
2b05f1b7 6920 5. PT_GNU_STACK segments do not include any sections.
03394ac9 6921 6. PT_TLS segment includes only SHF_TLS sections.
6f79b219
JJ
6922 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6923 8. PT_DYNAMIC should not contain empty sections at the beginning
08a40648 6924 (with the possible exception of .dynamic). */
502794d4 6925#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
2b05f1b7 6926 ((((segment->p_paddr \
502794d4
CE
6927 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr, opb) \
6928 : IS_CONTAINED_BY_VMA (section, segment, opb)) \
2b05f1b7 6929 && (section->flags & SEC_ALLOC) != 0) \
0efc80c8 6930 || IS_NOTE (segment, section)) \
2b05f1b7
L
6931 && segment->p_type != PT_GNU_STACK \
6932 && (segment->p_type != PT_TLS \
6933 || (section->flags & SEC_THREAD_LOCAL)) \
6934 && (segment->p_type == PT_LOAD \
6935 || segment->p_type == PT_TLS \
6936 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6937 && (segment->p_type != PT_DYNAMIC \
6938 || SECTION_SIZE (section, segment) > 0 \
6939 || (segment->p_paddr \
502794d4
CE
6940 ? segment->p_paddr != section->lma * (opb) \
6941 : segment->p_vaddr != section->vma * (opb)) \
fd361982 6942 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
9933dc52 6943 && (segment->p_type != PT_LOAD || !section->segment_mark))
bc67d8a6 6944
9f17e2a6
L
6945/* If the output section of a section in the input segment is NULL,
6946 it is removed from the corresponding output segment. */
502794d4
CE
6947#define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
6948 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
9f17e2a6
L
6949 && section->output_section != NULL)
6950
b34976b6 6951 /* Returns TRUE iff seg1 starts after the end of seg2. */
b5f852ea
NC
6952#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6953 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6954
6955 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6956 their VMA address ranges and their LMA address ranges overlap.
6957 It is possible to have overlapping VMA ranges without overlapping LMA
6958 ranges. RedBoot images for example can have both .data and .bss mapped
6959 to the same VMA range, but with the .data section mapped to a different
6960 LMA. */
aecc8f8a 6961#define SEGMENT_OVERLAPS(seg1, seg2) \
b5f852ea 6962 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
08a40648 6963 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
b5f852ea 6964 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
08a40648 6965 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
bc67d8a6
NC
6966
6967 /* Initialise the segment mark field. */
6968 for (section = ibfd->sections; section != NULL; section = section->next)
b34976b6 6969 section->segment_mark = FALSE;
bc67d8a6 6970
5c44b38e
AM
6971 /* The Solaris linker creates program headers in which all the
6972 p_paddr fields are zero. When we try to objcopy or strip such a
6973 file, we get confused. Check for this case, and if we find it
6974 don't set the p_paddr_valid fields. */
6975 p_paddr_valid = FALSE;
6976 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6977 i < num_segments;
6978 i++, segment++)
6979 if (segment->p_paddr != 0)
6980 {
6981 p_paddr_valid = TRUE;
6982 break;
6983 }
6984
252b5132 6985 /* Scan through the segments specified in the program header
bc67d8a6 6986 of the input BFD. For this first scan we look for overlaps
9ad5cbcf 6987 in the loadable segments. These can be created by weird
aecc8f8a 6988 parameters to objcopy. Also, fix some solaris weirdness. */
bc67d8a6
NC
6989 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6990 i < num_segments;
c044fabd 6991 i++, segment++)
252b5132 6992 {
252b5132 6993 unsigned int j;
c044fabd 6994 Elf_Internal_Phdr *segment2;
252b5132 6995
aecc8f8a
AM
6996 if (segment->p_type == PT_INTERP)
6997 for (section = ibfd->sections; section; section = section->next)
6998 if (IS_SOLARIS_PT_INTERP (segment, section))
6999 {
7000 /* Mininal change so that the normal section to segment
4cc11e76 7001 assignment code will work. */
502794d4 7002 segment->p_vaddr = section->vma * opb;
aecc8f8a
AM
7003 break;
7004 }
7005
bc67d8a6 7006 if (segment->p_type != PT_LOAD)
b10a8ae0
L
7007 {
7008 /* Remove PT_GNU_RELRO segment. */
7009 if (segment->p_type == PT_GNU_RELRO)
7010 segment->p_type = PT_NULL;
7011 continue;
7012 }
c044fabd 7013
bc67d8a6 7014 /* Determine if this segment overlaps any previous segments. */
0067a569 7015 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
bc67d8a6
NC
7016 {
7017 bfd_signed_vma extra_length;
c044fabd 7018
bc67d8a6 7019 if (segment2->p_type != PT_LOAD
0067a569 7020 || !SEGMENT_OVERLAPS (segment, segment2))
bc67d8a6 7021 continue;
c044fabd 7022
bc67d8a6
NC
7023 /* Merge the two segments together. */
7024 if (segment2->p_vaddr < segment->p_vaddr)
7025 {
c044fabd 7026 /* Extend SEGMENT2 to include SEGMENT and then delete
08a40648 7027 SEGMENT. */
0067a569
AM
7028 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
7029 - SEGMENT_END (segment2, segment2->p_vaddr));
c044fabd 7030
bc67d8a6
NC
7031 if (extra_length > 0)
7032 {
0067a569 7033 segment2->p_memsz += extra_length;
bc67d8a6
NC
7034 segment2->p_filesz += extra_length;
7035 }
c044fabd 7036
bc67d8a6 7037 segment->p_type = PT_NULL;
c044fabd 7038
bc67d8a6
NC
7039 /* Since we have deleted P we must restart the outer loop. */
7040 i = 0;
7041 segment = elf_tdata (ibfd)->phdr;
7042 break;
7043 }
7044 else
7045 {
c044fabd 7046 /* Extend SEGMENT to include SEGMENT2 and then delete
08a40648 7047 SEGMENT2. */
0067a569
AM
7048 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
7049 - SEGMENT_END (segment, segment->p_vaddr));
c044fabd 7050
bc67d8a6
NC
7051 if (extra_length > 0)
7052 {
0067a569 7053 segment->p_memsz += extra_length;
bc67d8a6
NC
7054 segment->p_filesz += extra_length;
7055 }
c044fabd 7056
bc67d8a6
NC
7057 segment2->p_type = PT_NULL;
7058 }
7059 }
7060 }
c044fabd 7061
bc67d8a6
NC
7062 /* The second scan attempts to assign sections to segments. */
7063 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7064 i < num_segments;
0067a569 7065 i++, segment++)
bc67d8a6 7066 {
0067a569
AM
7067 unsigned int section_count;
7068 asection **sections;
7069 asection *output_section;
7070 unsigned int isec;
9933dc52
AM
7071 asection *matching_lma;
7072 asection *suggested_lma;
0067a569 7073 unsigned int j;
446f7ed5 7074 size_t amt;
0067a569 7075 asection *first_section;
bc67d8a6
NC
7076
7077 if (segment->p_type == PT_NULL)
7078 continue;
c044fabd 7079
9f17e2a6 7080 first_section = NULL;
bc67d8a6 7081 /* Compute how many sections might be placed into this segment. */
b5f852ea
NC
7082 for (section = ibfd->sections, section_count = 0;
7083 section != NULL;
7084 section = section->next)
9f17e2a6
L
7085 {
7086 /* Find the first section in the input segment, which may be
7087 removed from the corresponding output segment. */
502794d4 7088 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb))
9f17e2a6
L
7089 {
7090 if (first_section == NULL)
7091 first_section = section;
7092 if (section->output_section != NULL)
7093 ++section_count;
7094 }
7095 }
811072d8 7096
b5f852ea
NC
7097 /* Allocate a segment map big enough to contain
7098 all of the sections we have selected. */
00bee008 7099 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
446f7ed5 7100 amt += section_count * sizeof (asection *);
a50b1753 7101 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
bc67d8a6 7102 if (map == NULL)
b34976b6 7103 return FALSE;
252b5132
RH
7104
7105 /* Initialise the fields of the segment map. Default to
7106 using the physical address of the segment in the input BFD. */
0067a569
AM
7107 map->next = NULL;
7108 map->p_type = segment->p_type;
7109 map->p_flags = segment->p_flags;
bc67d8a6 7110 map->p_flags_valid = 1;
55d55ac7 7111
9f17e2a6
L
7112 /* If the first section in the input segment is removed, there is
7113 no need to preserve segment physical address in the corresponding
7114 output segment. */
945c025a 7115 if (!first_section || first_section->output_section != NULL)
9f17e2a6
L
7116 {
7117 map->p_paddr = segment->p_paddr;
5c44b38e 7118 map->p_paddr_valid = p_paddr_valid;
9f17e2a6 7119 }
252b5132
RH
7120
7121 /* Determine if this segment contains the ELF file header
7122 and if it contains the program headers themselves. */
bc67d8a6
NC
7123 map->includes_filehdr = (segment->p_offset == 0
7124 && segment->p_filesz >= iehdr->e_ehsize);
bc67d8a6 7125 map->includes_phdrs = 0;
252b5132 7126
0067a569 7127 if (!phdr_included || segment->p_type != PT_LOAD)
252b5132 7128 {
bc67d8a6
NC
7129 map->includes_phdrs =
7130 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7131 && (segment->p_offset + segment->p_filesz
252b5132
RH
7132 >= ((bfd_vma) iehdr->e_phoff
7133 + iehdr->e_phnum * iehdr->e_phentsize)));
c044fabd 7134
bc67d8a6 7135 if (segment->p_type == PT_LOAD && map->includes_phdrs)
b34976b6 7136 phdr_included = TRUE;
252b5132
RH
7137 }
7138
bc67d8a6 7139 if (section_count == 0)
252b5132
RH
7140 {
7141 /* Special segments, such as the PT_PHDR segment, may contain
7142 no sections, but ordinary, loadable segments should contain
1ed89aa9 7143 something. They are allowed by the ELF spec however, so only
07d6d2b8 7144 a warning is produced.
f98450c6
NC
7145 There is however the valid use case of embedded systems which
7146 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7147 flash memory with zeros. No warning is shown for that case. */
7148 if (segment->p_type == PT_LOAD
7149 && (segment->p_filesz > 0 || segment->p_memsz == 0))
7150 /* xgettext:c-format */
9793eb77
AM
7151 _bfd_error_handler
7152 (_("%pB: warning: empty loadable segment detected"
7153 " at vaddr=%#" PRIx64 ", is this intentional?"),
7154 ibfd, (uint64_t) segment->p_vaddr);
252b5132 7155
502794d4 7156 map->p_vaddr_offset = segment->p_vaddr / opb;
bc67d8a6 7157 map->count = 0;
c044fabd
KH
7158 *pointer_to_map = map;
7159 pointer_to_map = &map->next;
252b5132
RH
7160
7161 continue;
7162 }
7163
7164 /* Now scan the sections in the input BFD again and attempt
7165 to add their corresponding output sections to the segment map.
7166 The problem here is how to handle an output section which has
7167 been moved (ie had its LMA changed). There are four possibilities:
7168
7169 1. None of the sections have been moved.
7170 In this case we can continue to use the segment LMA from the
7171 input BFD.
7172
7173 2. All of the sections have been moved by the same amount.
7174 In this case we can change the segment's LMA to match the LMA
7175 of the first section.
7176
7177 3. Some of the sections have been moved, others have not.
7178 In this case those sections which have not been moved can be
7179 placed in the current segment which will have to have its size,
7180 and possibly its LMA changed, and a new segment or segments will
7181 have to be created to contain the other sections.
7182
b5f852ea 7183 4. The sections have been moved, but not by the same amount.
252b5132
RH
7184 In this case we can change the segment's LMA to match the LMA
7185 of the first section and we will have to create a new segment
7186 or segments to contain the other sections.
7187
7188 In order to save time, we allocate an array to hold the section
7189 pointers that we are interested in. As these sections get assigned
7190 to a segment, they are removed from this array. */
7191
446f7ed5
AM
7192 amt = section_count * sizeof (asection *);
7193 sections = (asection **) bfd_malloc (amt);
252b5132 7194 if (sections == NULL)
b34976b6 7195 return FALSE;
252b5132
RH
7196
7197 /* Step One: Scan for segment vs section LMA conflicts.
7198 Also add the sections to the section array allocated above.
7199 Also add the sections to the current segment. In the common
7200 case, where the sections have not been moved, this means that
7201 we have completely filled the segment, and there is nothing
7202 more to do. */
252b5132 7203 isec = 0;
9933dc52
AM
7204 matching_lma = NULL;
7205 suggested_lma = NULL;
252b5132 7206
461c4b2e 7207 for (section = first_section, j = 0;
bc67d8a6
NC
7208 section != NULL;
7209 section = section->next)
252b5132 7210 {
502794d4 7211 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb))
c0f7859b 7212 {
bc67d8a6
NC
7213 output_section = section->output_section;
7214
0067a569 7215 sections[j++] = section;
252b5132
RH
7216
7217 /* The Solaris native linker always sets p_paddr to 0.
7218 We try to catch that case here, and set it to the
5e8d7549
NC
7219 correct value. Note - some backends require that
7220 p_paddr be left as zero. */
5c44b38e 7221 if (!p_paddr_valid
4455705d 7222 && segment->p_vaddr != 0
0067a569 7223 && !bed->want_p_paddr_set_to_zero
252b5132 7224 && isec == 0
bc67d8a6 7225 && output_section->lma != 0
9933dc52
AM
7226 && (align_power (segment->p_vaddr
7227 + (map->includes_filehdr
7228 ? iehdr->e_ehsize : 0)
7229 + (map->includes_phdrs
7230 ? iehdr->e_phnum * iehdr->e_phentsize
7231 : 0),
66631823
CE
7232 output_section->alignment_power * opb)
7233 == (output_section->vma * opb)))
bc67d8a6 7234 map->p_paddr = segment->p_vaddr;
252b5132
RH
7235
7236 /* Match up the physical address of the segment with the
7237 LMA address of the output section. */
502794d4
CE
7238 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr,
7239 opb)
5e8d7549 7240 || IS_COREFILE_NOTE (segment, section)
0067a569 7241 || (bed->want_p_paddr_set_to_zero
502794d4 7242 && IS_CONTAINED_BY_VMA (output_section, segment, opb)))
252b5132 7243 {
9933dc52
AM
7244 if (matching_lma == NULL
7245 || output_section->lma < matching_lma->lma)
7246 matching_lma = output_section;
252b5132
RH
7247
7248 /* We assume that if the section fits within the segment
bc67d8a6 7249 then it does not overlap any other section within that
252b5132 7250 segment. */
0067a569
AM
7251 map->sections[isec++] = output_section;
7252 }
9933dc52
AM
7253 else if (suggested_lma == NULL)
7254 suggested_lma = output_section;
147d51c2
L
7255
7256 if (j == section_count)
7257 break;
252b5132
RH
7258 }
7259 }
7260
bc67d8a6 7261 BFD_ASSERT (j == section_count);
252b5132
RH
7262
7263 /* Step Two: Adjust the physical address of the current segment,
7264 if necessary. */
bc67d8a6 7265 if (isec == section_count)
252b5132
RH
7266 {
7267 /* All of the sections fitted within the segment as currently
7268 specified. This is the default case. Add the segment to
7269 the list of built segments and carry on to process the next
7270 program header in the input BFD. */
bc67d8a6 7271 map->count = section_count;
c044fabd
KH
7272 *pointer_to_map = map;
7273 pointer_to_map = &map->next;
08a40648 7274
5c44b38e 7275 if (p_paddr_valid
30fe1832
AM
7276 && !bed->want_p_paddr_set_to_zero)
7277 {
7278 bfd_vma hdr_size = 0;
7279 if (map->includes_filehdr)
7280 hdr_size = iehdr->e_ehsize;
7281 if (map->includes_phdrs)
7282 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7283
7284 /* Account for padding before the first section in the
7285 segment. */
502794d4
CE
7286 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7287 - matching_lma->lma);
30fe1832 7288 }
08a40648 7289
252b5132
RH
7290 free (sections);
7291 continue;
7292 }
252b5132
RH
7293 else
7294 {
9933dc52
AM
7295 /* Change the current segment's physical address to match
7296 the LMA of the first section that fitted, or if no
7297 section fitted, the first section. */
7298 if (matching_lma == NULL)
7299 matching_lma = suggested_lma;
7300
66631823 7301 map->p_paddr = matching_lma->lma * opb;
72730e0c 7302
bc67d8a6
NC
7303 /* Offset the segment physical address from the lma
7304 to allow for space taken up by elf headers. */
9933dc52 7305 if (map->includes_phdrs)
010c8431 7306 {
9933dc52
AM
7307 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7308
7309 /* iehdr->e_phnum is just an estimate of the number
7310 of program headers that we will need. Make a note
7311 here of the number we used and the segment we chose
7312 to hold these headers, so that we can adjust the
7313 offset when we know the correct value. */
7314 phdr_adjust_num = iehdr->e_phnum;
7315 phdr_adjust_seg = map;
010c8431 7316 }
252b5132 7317
9933dc52 7318 if (map->includes_filehdr)
bc67d8a6 7319 {
9933dc52
AM
7320 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7321 map->p_paddr -= iehdr->e_ehsize;
7322 /* We've subtracted off the size of headers from the
7323 first section lma, but there may have been some
7324 alignment padding before that section too. Try to
7325 account for that by adjusting the segment lma down to
7326 the same alignment. */
7327 if (segment->p_align != 0 && segment->p_align < align)
7328 align = segment->p_align;
66631823 7329 map->p_paddr &= -(align * opb);
bc67d8a6 7330 }
252b5132
RH
7331 }
7332
7333 /* Step Three: Loop over the sections again, this time assigning
caf47ea6 7334 those that fit to the current segment and removing them from the
252b5132
RH
7335 sections array; but making sure not to leave large gaps. Once all
7336 possible sections have been assigned to the current segment it is
7337 added to the list of built segments and if sections still remain
7338 to be assigned, a new segment is constructed before repeating
7339 the loop. */
7340 isec = 0;
7341 do
7342 {
bc67d8a6 7343 map->count = 0;
9933dc52 7344 suggested_lma = NULL;
252b5132
RH
7345
7346 /* Fill the current segment with sections that fit. */
bc67d8a6 7347 for (j = 0; j < section_count; j++)
252b5132 7348 {
bc67d8a6 7349 section = sections[j];
252b5132 7350
bc67d8a6 7351 if (section == NULL)
252b5132
RH
7352 continue;
7353
bc67d8a6 7354 output_section = section->output_section;
252b5132 7355
bc67d8a6 7356 BFD_ASSERT (output_section != NULL);
c044fabd 7357
502794d4
CE
7358 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr,
7359 opb)
bc67d8a6 7360 || IS_COREFILE_NOTE (segment, section))
252b5132 7361 {
bc67d8a6 7362 if (map->count == 0)
252b5132
RH
7363 {
7364 /* If the first section in a segment does not start at
bc67d8a6
NC
7365 the beginning of the segment, then something is
7366 wrong. */
9933dc52
AM
7367 if (align_power (map->p_paddr
7368 + (map->includes_filehdr
7369 ? iehdr->e_ehsize : 0)
7370 + (map->includes_phdrs
7371 ? iehdr->e_phnum * iehdr->e_phentsize
7372 : 0),
66631823
CE
7373 output_section->alignment_power * opb)
7374 != output_section->lma * opb)
9aea1e31 7375 goto sorry;
252b5132
RH
7376 }
7377 else
7378 {
0067a569 7379 asection *prev_sec;
252b5132 7380
bc67d8a6 7381 prev_sec = map->sections[map->count - 1];
252b5132
RH
7382
7383 /* If the gap between the end of the previous section
bc67d8a6
NC
7384 and the start of this section is more than
7385 maxpagesize then we need to start a new segment. */
eea6121a 7386 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
079e9a2f 7387 maxpagesize)
caf47ea6 7388 < BFD_ALIGN (output_section->lma, maxpagesize))
0067a569 7389 || (prev_sec->lma + prev_sec->size
079e9a2f 7390 > output_section->lma))
252b5132 7391 {
9933dc52
AM
7392 if (suggested_lma == NULL)
7393 suggested_lma = output_section;
252b5132
RH
7394
7395 continue;
7396 }
7397 }
7398
bc67d8a6 7399 map->sections[map->count++] = output_section;
252b5132
RH
7400 ++isec;
7401 sections[j] = NULL;
9933dc52
AM
7402 if (segment->p_type == PT_LOAD)
7403 section->segment_mark = TRUE;
0067a569 7404 }
9933dc52
AM
7405 else if (suggested_lma == NULL)
7406 suggested_lma = output_section;
252b5132
RH
7407 }
7408
beab4532
NC
7409 /* PR 23932. A corrupt input file may contain sections that cannot
7410 be assigned to any segment - because for example they have a
9984857c
NC
7411 negative size - or segments that do not contain any sections.
7412 But there are also valid reasons why a segment can be empty.
7413 So allow a count of zero. */
252b5132
RH
7414
7415 /* Add the current segment to the list of built segments. */
c044fabd
KH
7416 *pointer_to_map = map;
7417 pointer_to_map = &map->next;
252b5132 7418
bc67d8a6 7419 if (isec < section_count)
252b5132
RH
7420 {
7421 /* We still have not allocated all of the sections to
7422 segments. Create a new segment here, initialise it
7423 and carry on looping. */
00bee008 7424 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
446f7ed5 7425 amt += section_count * sizeof (asection *);
5964fc3a 7426 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
bc67d8a6 7427 if (map == NULL)
5ed6aba4
NC
7428 {
7429 free (sections);
7430 return FALSE;
7431 }
252b5132
RH
7432
7433 /* Initialise the fields of the segment map. Set the physical
7434 physical address to the LMA of the first section that has
7435 not yet been assigned. */
0067a569
AM
7436 map->next = NULL;
7437 map->p_type = segment->p_type;
7438 map->p_flags = segment->p_flags;
7439 map->p_flags_valid = 1;
66631823 7440 map->p_paddr = suggested_lma->lma * opb;
5c44b38e 7441 map->p_paddr_valid = p_paddr_valid;
bc67d8a6 7442 map->includes_filehdr = 0;
0067a569 7443 map->includes_phdrs = 0;
252b5132 7444 }
9984857c
NC
7445
7446 continue;
7447 sorry:
7448 bfd_set_error (bfd_error_sorry);
7449 free (sections);
7450 return FALSE;
252b5132 7451 }
bc67d8a6 7452 while (isec < section_count);
252b5132
RH
7453
7454 free (sections);
7455 }
7456
12bd6957 7457 elf_seg_map (obfd) = map_first;
bc67d8a6
NC
7458
7459 /* If we had to estimate the number of program headers that were
9ad5cbcf 7460 going to be needed, then check our estimate now and adjust
bc67d8a6
NC
7461 the offset if necessary. */
7462 if (phdr_adjust_seg != NULL)
7463 {
7464 unsigned int count;
c044fabd 7465
bc67d8a6 7466 for (count = 0, map = map_first; map != NULL; map = map->next)
c044fabd 7467 count++;
252b5132 7468
bc67d8a6
NC
7469 if (count > phdr_adjust_num)
7470 phdr_adjust_seg->p_paddr
7471 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
9933dc52
AM
7472
7473 for (map = map_first; map != NULL; map = map->next)
7474 if (map->p_type == PT_PHDR)
7475 {
7476 bfd_vma adjust
7477 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7478 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7479 break;
7480 }
bc67d8a6 7481 }
c044fabd 7482
bc67d8a6 7483#undef SEGMENT_END
eecdbe52 7484#undef SECTION_SIZE
bc67d8a6
NC
7485#undef IS_CONTAINED_BY_VMA
7486#undef IS_CONTAINED_BY_LMA
0efc80c8 7487#undef IS_NOTE
252b5132 7488#undef IS_COREFILE_NOTE
bc67d8a6 7489#undef IS_SOLARIS_PT_INTERP
9f17e2a6 7490#undef IS_SECTION_IN_INPUT_SEGMENT
bc67d8a6
NC
7491#undef INCLUDE_SECTION_IN_SEGMENT
7492#undef SEGMENT_AFTER_SEGMENT
7493#undef SEGMENT_OVERLAPS
b34976b6 7494 return TRUE;
252b5132
RH
7495}
7496
84d1d650
L
7497/* Copy ELF program header information. */
7498
7499static bfd_boolean
7500copy_elf_program_header (bfd *ibfd, bfd *obfd)
7501{
7502 Elf_Internal_Ehdr *iehdr;
7503 struct elf_segment_map *map;
7504 struct elf_segment_map *map_first;
7505 struct elf_segment_map **pointer_to_map;
7506 Elf_Internal_Phdr *segment;
7507 unsigned int i;
7508 unsigned int num_segments;
7509 bfd_boolean phdr_included = FALSE;
88967714 7510 bfd_boolean p_paddr_valid;
502794d4 7511 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
84d1d650
L
7512
7513 iehdr = elf_elfheader (ibfd);
7514
7515 map_first = NULL;
7516 pointer_to_map = &map_first;
7517
88967714
AM
7518 /* If all the segment p_paddr fields are zero, don't set
7519 map->p_paddr_valid. */
7520 p_paddr_valid = FALSE;
84d1d650 7521 num_segments = elf_elfheader (ibfd)->e_phnum;
88967714
AM
7522 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7523 i < num_segments;
7524 i++, segment++)
7525 if (segment->p_paddr != 0)
7526 {
7527 p_paddr_valid = TRUE;
7528 break;
7529 }
7530
84d1d650
L
7531 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7532 i < num_segments;
7533 i++, segment++)
7534 {
7535 asection *section;
7536 unsigned int section_count;
986f0783 7537 size_t amt;
84d1d650 7538 Elf_Internal_Shdr *this_hdr;
53020534 7539 asection *first_section = NULL;
a76e6f2f 7540 asection *lowest_section;
84d1d650 7541
84d1d650
L
7542 /* Compute how many sections are in this segment. */
7543 for (section = ibfd->sections, section_count = 0;
7544 section != NULL;
7545 section = section->next)
7546 {
7547 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7548 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
3271a814 7549 {
a76e6f2f
AM
7550 if (first_section == NULL)
7551 first_section = section;
3271a814
NS
7552 section_count++;
7553 }
84d1d650
L
7554 }
7555
7556 /* Allocate a segment map big enough to contain
7557 all of the sections we have selected. */
00bee008 7558 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
986f0783 7559 amt += section_count * sizeof (asection *);
a50b1753 7560 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
84d1d650
L
7561 if (map == NULL)
7562 return FALSE;
7563
7564 /* Initialize the fields of the output segment map with the
7565 input segment. */
7566 map->next = NULL;
7567 map->p_type = segment->p_type;
7568 map->p_flags = segment->p_flags;
7569 map->p_flags_valid = 1;
7570 map->p_paddr = segment->p_paddr;
88967714 7571 map->p_paddr_valid = p_paddr_valid;
3f570048
AM
7572 map->p_align = segment->p_align;
7573 map->p_align_valid = 1;
3271a814 7574 map->p_vaddr_offset = 0;
84d1d650 7575
04c3a755
NS
7576 if (map->p_type == PT_GNU_RELRO
7577 || map->p_type == PT_GNU_STACK)
b10a8ae0
L
7578 {
7579 /* The PT_GNU_RELRO segment may contain the first a few
7580 bytes in the .got.plt section even if the whole .got.plt
7581 section isn't in the PT_GNU_RELRO segment. We won't
04c3a755
NS
7582 change the size of the PT_GNU_RELRO segment.
7583 Similarly, PT_GNU_STACK size is significant on uclinux
7584 systems. */
9433b9b1 7585 map->p_size = segment->p_memsz;
b10a8ae0
L
7586 map->p_size_valid = 1;
7587 }
7588
84d1d650
L
7589 /* Determine if this segment contains the ELF file header
7590 and if it contains the program headers themselves. */
7591 map->includes_filehdr = (segment->p_offset == 0
7592 && segment->p_filesz >= iehdr->e_ehsize);
7593
7594 map->includes_phdrs = 0;
7595 if (! phdr_included || segment->p_type != PT_LOAD)
7596 {
7597 map->includes_phdrs =
7598 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7599 && (segment->p_offset + segment->p_filesz
7600 >= ((bfd_vma) iehdr->e_phoff
7601 + iehdr->e_phnum * iehdr->e_phentsize)));
7602
7603 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7604 phdr_included = TRUE;
7605 }
7606
bbefd0a9 7607 lowest_section = NULL;
84d1d650
L
7608 if (section_count != 0)
7609 {
7610 unsigned int isec = 0;
7611
53020534 7612 for (section = first_section;
84d1d650
L
7613 section != NULL;
7614 section = section->next)
7615 {
7616 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7617 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
53020534
L
7618 {
7619 map->sections[isec++] = section->output_section;
a76e6f2f
AM
7620 if ((section->flags & SEC_ALLOC) != 0)
7621 {
7622 bfd_vma seg_off;
7623
bbefd0a9
AM
7624 if (lowest_section == NULL
7625 || section->lma < lowest_section->lma)
fb8a5684
AM
7626 lowest_section = section;
7627
a76e6f2f
AM
7628 /* Section lmas are set up from PT_LOAD header
7629 p_paddr in _bfd_elf_make_section_from_shdr.
7630 If this header has a p_paddr that disagrees
7631 with the section lma, flag the p_paddr as
7632 invalid. */
7633 if ((section->flags & SEC_LOAD) != 0)
7634 seg_off = this_hdr->sh_offset - segment->p_offset;
7635 else
7636 seg_off = this_hdr->sh_addr - segment->p_vaddr;
502794d4 7637 if (section->lma * opb - segment->p_paddr != seg_off)
a76e6f2f
AM
7638 map->p_paddr_valid = FALSE;
7639 }
53020534
L
7640 if (isec == section_count)
7641 break;
7642 }
84d1d650
L
7643 }
7644 }
7645
5d695627 7646 if (section_count == 0)
502794d4 7647 map->p_vaddr_offset = segment->p_vaddr / opb;
30fe1832
AM
7648 else if (map->p_paddr_valid)
7649 {
7650 /* Account for padding before the first section in the segment. */
7651 bfd_vma hdr_size = 0;
7652 if (map->includes_filehdr)
7653 hdr_size = iehdr->e_ehsize;
7654 if (map->includes_phdrs)
7655 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7656
502794d4 7657 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
30fe1832
AM
7658 - (lowest_section ? lowest_section->lma : 0));
7659 }
a76e6f2f 7660
84d1d650
L
7661 map->count = section_count;
7662 *pointer_to_map = map;
7663 pointer_to_map = &map->next;
7664 }
7665
12bd6957 7666 elf_seg_map (obfd) = map_first;
84d1d650
L
7667 return TRUE;
7668}
7669
7670/* Copy private BFD data. This copies or rewrites ELF program header
7671 information. */
7672
7673static bfd_boolean
7674copy_private_bfd_data (bfd *ibfd, bfd *obfd)
7675{
84d1d650
L
7676 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7677 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7678 return TRUE;
7679
7680 if (elf_tdata (ibfd)->phdr == NULL)
7681 return TRUE;
7682
7683 if (ibfd->xvec == obfd->xvec)
7684 {
cb3ff1e5
NC
7685 /* Check to see if any sections in the input BFD
7686 covered by ELF program header have changed. */
d55ce4e2 7687 Elf_Internal_Phdr *segment;
84d1d650
L
7688 asection *section, *osec;
7689 unsigned int i, num_segments;
7690 Elf_Internal_Shdr *this_hdr;
147d51c2
L
7691 const struct elf_backend_data *bed;
7692
7693 bed = get_elf_backend_data (ibfd);
7694
7695 /* Regenerate the segment map if p_paddr is set to 0. */
7696 if (bed->want_p_paddr_set_to_zero)
7697 goto rewrite;
84d1d650
L
7698
7699 /* Initialize the segment mark field. */
7700 for (section = obfd->sections; section != NULL;
7701 section = section->next)
7702 section->segment_mark = FALSE;
7703
7704 num_segments = elf_elfheader (ibfd)->e_phnum;
7705 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7706 i < num_segments;
7707 i++, segment++)
7708 {
5f6999aa
NC
7709 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7710 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7711 which severly confuses things, so always regenerate the segment
7712 map in this case. */
7713 if (segment->p_paddr == 0
7714 && segment->p_memsz == 0
7715 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
cb3ff1e5 7716 goto rewrite;
5f6999aa 7717
84d1d650
L
7718 for (section = ibfd->sections;
7719 section != NULL; section = section->next)
7720 {
7721 /* We mark the output section so that we know it comes
7722 from the input BFD. */
7723 osec = section->output_section;
7724 if (osec)
7725 osec->segment_mark = TRUE;
7726
7727 /* Check if this section is covered by the segment. */
7728 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7729 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
84d1d650
L
7730 {
7731 /* FIXME: Check if its output section is changed or
7732 removed. What else do we need to check? */
7733 if (osec == NULL
7734 || section->flags != osec->flags
7735 || section->lma != osec->lma
7736 || section->vma != osec->vma
7737 || section->size != osec->size
7738 || section->rawsize != osec->rawsize
7739 || section->alignment_power != osec->alignment_power)
7740 goto rewrite;
7741 }
7742 }
7743 }
7744
cb3ff1e5 7745 /* Check to see if any output section do not come from the
84d1d650
L
7746 input BFD. */
7747 for (section = obfd->sections; section != NULL;
7748 section = section->next)
7749 {
535b785f 7750 if (!section->segment_mark)
84d1d650
L
7751 goto rewrite;
7752 else
7753 section->segment_mark = FALSE;
7754 }
7755
7756 return copy_elf_program_header (ibfd, obfd);
7757 }
7758
dc1e8a47 7759 rewrite:
f1d85785
L
7760 if (ibfd->xvec == obfd->xvec)
7761 {
7762 /* When rewriting program header, set the output maxpagesize to
7763 the maximum alignment of input PT_LOAD segments. */
7764 Elf_Internal_Phdr *segment;
7765 unsigned int i;
7766 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
7767 bfd_vma maxpagesize = 0;
7768
7769 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7770 i < num_segments;
7771 i++, segment++)
7772 if (segment->p_type == PT_LOAD
7773 && maxpagesize < segment->p_align)
c86934ce
NC
7774 {
7775 /* PR 17512: file: f17299af. */
7776 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
695344c0 7777 /* xgettext:c-format */
2dcf00ce
AM
7778 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7779 PRIx64 " is too large"),
7780 ibfd, (uint64_t) segment->p_align);
c86934ce
NC
7781 else
7782 maxpagesize = segment->p_align;
7783 }
f1d85785
L
7784
7785 if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize)
7786 bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize);
7787 }
7788
84d1d650
L
7789 return rewrite_elf_program_header (ibfd, obfd);
7790}
7791
ccd2ec6a
L
7792/* Initialize private output section information from input section. */
7793
7794bfd_boolean
7795_bfd_elf_init_private_section_data (bfd *ibfd,
7796 asection *isec,
7797 bfd *obfd,
7798 asection *osec,
7799 struct bfd_link_info *link_info)
7800
7801{
7802 Elf_Internal_Shdr *ihdr, *ohdr;
0e1862bb
L
7803 bfd_boolean final_link = (link_info != NULL
7804 && !bfd_link_relocatable (link_info));
ccd2ec6a
L
7805
7806 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7807 || obfd->xvec->flavour != bfd_target_elf_flavour)
7808 return TRUE;
7809
ba85c43e
NC
7810 BFD_ASSERT (elf_section_data (osec) != NULL);
7811
8c803a2d
AM
7812 /* If this is a known ABI section, ELF section type and flags may
7813 have been set up when OSEC was created. For normal sections we
7814 allow the user to override the type and flags other than
7815 SHF_MASKOS and SHF_MASKPROC. */
7816 if (elf_section_type (osec) == SHT_PROGBITS
7817 || elf_section_type (osec) == SHT_NOTE
7818 || elf_section_type (osec) == SHT_NOBITS)
7819 elf_section_type (osec) = SHT_NULL;
7820 /* For objcopy and relocatable link, copy the ELF section type from
7821 the input file if the BFD section flags are the same. (If they
7822 are different the user may be doing something like
7823 "objcopy --set-section-flags .text=alloc,data".) For a final
7824 link allow some flags that the linker clears to differ. */
42bb2e33 7825 if (elf_section_type (osec) == SHT_NULL
dfa7b0b8
AM
7826 && (osec->flags == isec->flags
7827 || (final_link
7828 && ((osec->flags ^ isec->flags)
0814be7d 7829 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
42bb2e33 7830 elf_section_type (osec) = elf_section_type (isec);
d270463e
L
7831
7832 /* FIXME: Is this correct for all OS/PROC specific flags? */
8c803a2d
AM
7833 elf_section_flags (osec) = (elf_section_flags (isec)
7834 & (SHF_MASKOS | SHF_MASKPROC));
ccd2ec6a 7835
a91e1603 7836 /* Copy sh_info from input for mbind section. */
df3a023b
AM
7837 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
7838 && elf_section_flags (isec) & SHF_GNU_MBIND)
a91e1603
L
7839 elf_section_data (osec)->this_hdr.sh_info
7840 = elf_section_data (isec)->this_hdr.sh_info;
7841
ccd2ec6a
L
7842 /* Set things up for objcopy and relocatable link. The output
7843 SHT_GROUP section will have its elf_next_in_group pointing back
7844 to the input group members. Ignore linker created group section.
7845 See elfNN_ia64_object_p in elfxx-ia64.c. */
7bdf4127
AB
7846 if ((link_info == NULL
7847 || !link_info->resolve_section_groups)
7848 && (elf_sec_group (isec) == NULL
7849 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
ccd2ec6a 7850 {
7bdf4127
AB
7851 if (elf_section_flags (isec) & SHF_GROUP)
7852 elf_section_flags (osec) |= SHF_GROUP;
7853 elf_next_in_group (osec) = elf_next_in_group (isec);
7854 elf_section_data (osec)->group = elf_section_data (isec)->group;
ccd2ec6a
L
7855 }
7856
7bdf4127
AB
7857 /* If not decompress, preserve SHF_COMPRESSED. */
7858 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
7859 elf_section_flags (osec) |= (elf_section_flags (isec)
7860 & SHF_COMPRESSED);
7861
ccd2ec6a
L
7862 ihdr = &elf_section_data (isec)->this_hdr;
7863
7864 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7865 don't use the output section of the linked-to section since it
7866 may be NULL at this point. */
7867 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
7868 {
7869 ohdr = &elf_section_data (osec)->this_hdr;
7870 ohdr->sh_flags |= SHF_LINK_ORDER;
7871 elf_linked_to_section (osec) = elf_linked_to_section (isec);
7872 }
7873
7874 osec->use_rela_p = isec->use_rela_p;
7875
7876 return TRUE;
7877}
7878
252b5132
RH
7879/* Copy private section information. This copies over the entsize
7880 field, and sometimes the info field. */
7881
b34976b6 7882bfd_boolean
217aa764
AM
7883_bfd_elf_copy_private_section_data (bfd *ibfd,
7884 asection *isec,
7885 bfd *obfd,
7886 asection *osec)
252b5132
RH
7887{
7888 Elf_Internal_Shdr *ihdr, *ohdr;
7889
7890 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7891 || obfd->xvec->flavour != bfd_target_elf_flavour)
b34976b6 7892 return TRUE;
252b5132 7893
252b5132
RH
7894 ihdr = &elf_section_data (isec)->this_hdr;
7895 ohdr = &elf_section_data (osec)->this_hdr;
7896
7897 ohdr->sh_entsize = ihdr->sh_entsize;
7898
7899 if (ihdr->sh_type == SHT_SYMTAB
7900 || ihdr->sh_type == SHT_DYNSYM
7901 || ihdr->sh_type == SHT_GNU_verneed
7902 || ihdr->sh_type == SHT_GNU_verdef)
7903 ohdr->sh_info = ihdr->sh_info;
7904
ccd2ec6a
L
7905 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
7906 NULL);
252b5132
RH
7907}
7908
d0bf826b
AM
7909/* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7910 necessary if we are removing either the SHT_GROUP section or any of
7911 the group member sections. DISCARDED is the value that a section's
7912 output_section has if the section will be discarded, NULL when this
7913 function is called from objcopy, bfd_abs_section_ptr when called
7914 from the linker. */
80fccad2
BW
7915
7916bfd_boolean
d0bf826b 7917_bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
80fccad2 7918{
30288845
AM
7919 asection *isec;
7920
30288845 7921 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
415f38a6 7922 if (elf_section_type (isec) == SHT_GROUP)
30288845
AM
7923 {
7924 asection *first = elf_next_in_group (isec);
7925 asection *s = first;
d0bf826b
AM
7926 bfd_size_type removed = 0;
7927
30288845
AM
7928 while (s != NULL)
7929 {
415f38a6
AM
7930 /* If this member section is being output but the
7931 SHT_GROUP section is not, then clear the group info
7932 set up by _bfd_elf_copy_private_section_data. */
d0bf826b
AM
7933 if (s->output_section != discarded
7934 && isec->output_section == discarded)
30288845
AM
7935 {
7936 elf_section_flags (s->output_section) &= ~SHF_GROUP;
7937 elf_group_name (s->output_section) = NULL;
7938 }
3349112e 7939 else
6e5e9d58
AM
7940 {
7941 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
3349112e
L
7942 if (s->output_section == discarded
7943 && isec->output_section != discarded)
7944 {
7945 /* Conversely, if the member section is not being
7946 output but the SHT_GROUP section is, then adjust
7947 its size. */
7948 removed += 4;
7949 if (elf_sec->rel.hdr != NULL
7950 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
7951 removed += 4;
7952 if (elf_sec->rela.hdr != NULL
7953 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
7954 removed += 4;
7955 }
7956 else
7957 {
7958 /* Also adjust for zero-sized relocation member
7959 section. */
7960 if (elf_sec->rel.hdr != NULL
7961 && elf_sec->rel.hdr->sh_size == 0)
7962 removed += 4;
7963 if (elf_sec->rela.hdr != NULL
7964 && elf_sec->rela.hdr->sh_size == 0)
7965 removed += 4;
7966 }
6e5e9d58 7967 }
30288845
AM
7968 s = elf_next_in_group (s);
7969 if (s == first)
7970 break;
7971 }
d0bf826b
AM
7972 if (removed != 0)
7973 {
7974 if (discarded != NULL)
7975 {
7976 /* If we've been called for ld -r, then we need to
6e5e9d58 7977 adjust the input section size. */
d0bf826b
AM
7978 if (isec->rawsize == 0)
7979 isec->rawsize = isec->size;
7980 isec->size = isec->rawsize - removed;
6e5e9d58
AM
7981 if (isec->size <= 4)
7982 {
7983 isec->size = 0;
7984 isec->flags |= SEC_EXCLUDE;
7985 }
d0bf826b
AM
7986 }
7987 else
7988 {
7989 /* Adjust the output section size when called from
7990 objcopy. */
7991 isec->output_section->size -= removed;
6e5e9d58
AM
7992 if (isec->output_section->size <= 4)
7993 {
7994 isec->output_section->size = 0;
7995 isec->output_section->flags |= SEC_EXCLUDE;
7996 }
d0bf826b
AM
7997 }
7998 }
30288845
AM
7999 }
8000
80fccad2
BW
8001 return TRUE;
8002}
8003
d0bf826b
AM
8004/* Copy private header information. */
8005
8006bfd_boolean
8007_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8008{
8009 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8010 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8011 return TRUE;
8012
8013 /* Copy over private BFD data if it has not already been copied.
8014 This must be done here, rather than in the copy_private_bfd_data
8015 entry point, because the latter is called after the section
8016 contents have been set, which means that the program headers have
8017 already been worked out. */
12bd6957 8018 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
d0bf826b
AM
8019 {
8020 if (! copy_private_bfd_data (ibfd, obfd))
8021 return FALSE;
8022 }
8023
8024 return _bfd_elf_fixup_group_sections (ibfd, NULL);
8025}
8026
252b5132
RH
8027/* Copy private symbol information. If this symbol is in a section
8028 which we did not map into a BFD section, try to map the section
8029 index correctly. We use special macro definitions for the mapped
8030 section indices; these definitions are interpreted by the
8031 swap_out_syms function. */
8032
9ad5cbcf
AM
8033#define MAP_ONESYMTAB (SHN_HIOS + 1)
8034#define MAP_DYNSYMTAB (SHN_HIOS + 2)
8035#define MAP_STRTAB (SHN_HIOS + 3)
8036#define MAP_SHSTRTAB (SHN_HIOS + 4)
8037#define MAP_SYM_SHNDX (SHN_HIOS + 5)
252b5132 8038
b34976b6 8039bfd_boolean
217aa764
AM
8040_bfd_elf_copy_private_symbol_data (bfd *ibfd,
8041 asymbol *isymarg,
8042 bfd *obfd,
8043 asymbol *osymarg)
252b5132
RH
8044{
8045 elf_symbol_type *isym, *osym;
8046
8047 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8048 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 8049 return TRUE;
252b5132
RH
8050
8051 isym = elf_symbol_from (ibfd, isymarg);
8052 osym = elf_symbol_from (obfd, osymarg);
8053
8054 if (isym != NULL
8424d8f5 8055 && isym->internal_elf_sym.st_shndx != 0
252b5132
RH
8056 && osym != NULL
8057 && bfd_is_abs_section (isym->symbol.section))
8058 {
8059 unsigned int shndx;
8060
8061 shndx = isym->internal_elf_sym.st_shndx;
8062 if (shndx == elf_onesymtab (ibfd))
8063 shndx = MAP_ONESYMTAB;
8064 else if (shndx == elf_dynsymtab (ibfd))
8065 shndx = MAP_DYNSYMTAB;
12bd6957 8066 else if (shndx == elf_strtab_sec (ibfd))
252b5132 8067 shndx = MAP_STRTAB;
12bd6957 8068 else if (shndx == elf_shstrtab_sec (ibfd))
252b5132 8069 shndx = MAP_SHSTRTAB;
6a40cf0c 8070 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
9ad5cbcf 8071 shndx = MAP_SYM_SHNDX;
252b5132
RH
8072 osym->internal_elf_sym.st_shndx = shndx;
8073 }
8074
b34976b6 8075 return TRUE;
252b5132
RH
8076}
8077
8078/* Swap out the symbols. */
8079
b34976b6 8080static bfd_boolean
217aa764 8081swap_out_syms (bfd *abfd,
ef10c3ac 8082 struct elf_strtab_hash **sttp,
217aa764 8083 int relocatable_p)
252b5132 8084{
9c5bfbb7 8085 const struct elf_backend_data *bed;
1f4361a7 8086 unsigned int symcount;
079e9a2f 8087 asymbol **syms;
ef10c3ac 8088 struct elf_strtab_hash *stt;
079e9a2f 8089 Elf_Internal_Shdr *symtab_hdr;
9ad5cbcf 8090 Elf_Internal_Shdr *symtab_shndx_hdr;
079e9a2f 8091 Elf_Internal_Shdr *symstrtab_hdr;
ef10c3ac 8092 struct elf_sym_strtab *symstrtab;
f075ee0c
AM
8093 bfd_byte *outbound_syms;
8094 bfd_byte *outbound_shndx;
ef10c3ac
L
8095 unsigned long outbound_syms_index;
8096 unsigned long outbound_shndx_index;
1f4361a7 8097 unsigned int idx;
12bd6957 8098 unsigned int num_locals;
1f4361a7 8099 size_t amt;
174fd7f9 8100 bfd_boolean name_local_sections;
252b5132 8101
12bd6957 8102 if (!elf_map_symbols (abfd, &num_locals))
b34976b6 8103 return FALSE;
252b5132 8104
c044fabd 8105 /* Dump out the symtabs. */
ef10c3ac 8106 stt = _bfd_elf_strtab_init ();
079e9a2f 8107 if (stt == NULL)
b34976b6 8108 return FALSE;
252b5132 8109
079e9a2f
AM
8110 bed = get_elf_backend_data (abfd);
8111 symcount = bfd_get_symcount (abfd);
8112 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8113 symtab_hdr->sh_type = SHT_SYMTAB;
8114 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8115 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
12bd6957 8116 symtab_hdr->sh_info = num_locals + 1;
72de5009 8117 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
079e9a2f
AM
8118
8119 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8120 symstrtab_hdr->sh_type = SHT_STRTAB;
8121
ef10c3ac 8122 /* Allocate buffer to swap out the .strtab section. */
1f4361a7
AM
8123 if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8124 || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
ef10c3ac 8125 {
1f4361a7 8126 bfd_set_error (bfd_error_no_memory);
ef10c3ac
L
8127 _bfd_elf_strtab_free (stt);
8128 return FALSE;
8129 }
8130
1f4361a7
AM
8131 if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8132 || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
5ed6aba4 8133 {
1f4361a7
AM
8134 error_no_mem:
8135 bfd_set_error (bfd_error_no_memory);
8136 error_return:
ef10c3ac 8137 free (symstrtab);
1f4361a7 8138 _bfd_elf_strtab_free (stt);
5ed6aba4
NC
8139 return FALSE;
8140 }
217aa764 8141 symtab_hdr->contents = outbound_syms;
ef10c3ac 8142 outbound_syms_index = 0;
252b5132 8143
9ad5cbcf 8144 outbound_shndx = NULL;
ef10c3ac 8145 outbound_shndx_index = 0;
6a40cf0c
NC
8146
8147 if (elf_symtab_shndx_list (abfd))
9ad5cbcf 8148 {
6a40cf0c
NC
8149 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8150 if (symtab_shndx_hdr->sh_name != 0)
8151 {
1f4361a7
AM
8152 if (_bfd_mul_overflow (symcount + 1,
8153 sizeof (Elf_External_Sym_Shndx), &amt))
8154 goto error_no_mem;
8155 outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt);
6a40cf0c
NC
8156 if (outbound_shndx == NULL)
8157 goto error_return;
5ed6aba4 8158
6a40cf0c
NC
8159 symtab_shndx_hdr->contents = outbound_shndx;
8160 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8161 symtab_shndx_hdr->sh_size = amt;
8162 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8163 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8164 }
8165 /* FIXME: What about any other headers in the list ? */
9ad5cbcf
AM
8166 }
8167
589e6347 8168 /* Now generate the data (for "contents"). */
079e9a2f
AM
8169 {
8170 /* Fill in zeroth symbol and swap it out. */
8171 Elf_Internal_Sym sym;
8172 sym.st_name = 0;
8173 sym.st_value = 0;
8174 sym.st_size = 0;
8175 sym.st_info = 0;
8176 sym.st_other = 0;
8177 sym.st_shndx = SHN_UNDEF;
35fc36a8 8178 sym.st_target_internal = 0;
ef10c3ac
L
8179 symstrtab[0].sym = sym;
8180 symstrtab[0].dest_index = outbound_syms_index;
8181 symstrtab[0].destshndx_index = outbound_shndx_index;
8182 outbound_syms_index++;
9ad5cbcf 8183 if (outbound_shndx != NULL)
ef10c3ac 8184 outbound_shndx_index++;
079e9a2f 8185 }
252b5132 8186
174fd7f9
RS
8187 name_local_sections
8188 = (bed->elf_backend_name_local_section_symbols
8189 && bed->elf_backend_name_local_section_symbols (abfd));
8190
079e9a2f 8191 syms = bfd_get_outsymbols (abfd);
ef10c3ac 8192 for (idx = 0; idx < symcount;)
252b5132 8193 {
252b5132 8194 Elf_Internal_Sym sym;
079e9a2f
AM
8195 bfd_vma value = syms[idx]->value;
8196 elf_symbol_type *type_ptr;
8197 flagword flags = syms[idx]->flags;
8198 int type;
252b5132 8199
174fd7f9
RS
8200 if (!name_local_sections
8201 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
079e9a2f
AM
8202 {
8203 /* Local section symbols have no name. */
ef10c3ac 8204 sym.st_name = (unsigned long) -1;
079e9a2f
AM
8205 }
8206 else
8207 {
ef10c3ac
L
8208 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8209 to get the final offset for st_name. */
8210 sym.st_name
8211 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
8212 FALSE);
079e9a2f 8213 if (sym.st_name == (unsigned long) -1)
ef10c3ac 8214 goto error_return;
079e9a2f 8215 }
252b5132 8216
079e9a2f 8217 type_ptr = elf_symbol_from (abfd, syms[idx]);
252b5132 8218
079e9a2f
AM
8219 if ((flags & BSF_SECTION_SYM) == 0
8220 && bfd_is_com_section (syms[idx]->section))
8221 {
8222 /* ELF common symbols put the alignment into the `value' field,
8223 and the size into the `size' field. This is backwards from
8224 how BFD handles it, so reverse it here. */
8225 sym.st_size = value;
8226 if (type_ptr == NULL
8227 || type_ptr->internal_elf_sym.st_value == 0)
8228 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8229 else
8230 sym.st_value = type_ptr->internal_elf_sym.st_value;
8231 sym.st_shndx = _bfd_elf_section_from_bfd_section
8232 (abfd, syms[idx]->section);
8233 }
8234 else
8235 {
8236 asection *sec = syms[idx]->section;
cb33740c 8237 unsigned int shndx;
252b5132 8238
079e9a2f
AM
8239 if (sec->output_section)
8240 {
8241 value += sec->output_offset;
8242 sec = sec->output_section;
8243 }
589e6347 8244
079e9a2f
AM
8245 /* Don't add in the section vma for relocatable output. */
8246 if (! relocatable_p)
8247 value += sec->vma;
8248 sym.st_value = value;
8249 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8250
8251 if (bfd_is_abs_section (sec)
8252 && type_ptr != NULL
8253 && type_ptr->internal_elf_sym.st_shndx != 0)
8254 {
8255 /* This symbol is in a real ELF section which we did
8256 not create as a BFD section. Undo the mapping done
8257 by copy_private_symbol_data. */
8258 shndx = type_ptr->internal_elf_sym.st_shndx;
8259 switch (shndx)
8260 {
8261 case MAP_ONESYMTAB:
8262 shndx = elf_onesymtab (abfd);
8263 break;
8264 case MAP_DYNSYMTAB:
8265 shndx = elf_dynsymtab (abfd);
8266 break;
8267 case MAP_STRTAB:
12bd6957 8268 shndx = elf_strtab_sec (abfd);
079e9a2f
AM
8269 break;
8270 case MAP_SHSTRTAB:
12bd6957 8271 shndx = elf_shstrtab_sec (abfd);
079e9a2f 8272 break;
9ad5cbcf 8273 case MAP_SYM_SHNDX:
6a40cf0c
NC
8274 if (elf_symtab_shndx_list (abfd))
8275 shndx = elf_symtab_shndx_list (abfd)->ndx;
9ad5cbcf 8276 break;
00e49dff
NC
8277 case SHN_COMMON:
8278 case SHN_ABS:
15bc576a 8279 shndx = SHN_ABS;
079e9a2f 8280 break;
00e49dff
NC
8281 default:
8282 if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8283 {
8284 if (bed->symbol_section_index)
8285 shndx = bed->symbol_section_index (abfd, type_ptr);
8286 /* Otherwise just leave the index alone. */
8287 }
8288 else
8289 {
8290 if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8291 _bfd_error_handler (_("%pB: \
8292Unable to handle section index %x in ELF symbol. Using ABS instead."),
8293 abfd, shndx);
8294 shndx = SHN_ABS;
8295 }
8296 break;
079e9a2f
AM
8297 }
8298 }
8299 else
8300 {
8301 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
252b5132 8302
cb33740c 8303 if (shndx == SHN_BAD)
079e9a2f
AM
8304 {
8305 asection *sec2;
8306
8307 /* Writing this would be a hell of a lot easier if
8308 we had some decent documentation on bfd, and
8309 knew what to expect of the library, and what to
8310 demand of applications. For example, it
8311 appears that `objcopy' might not set the
8312 section of a symbol to be a section that is
8313 actually in the output file. */
8314 sec2 = bfd_get_section_by_name (abfd, sec->name);
5df1bc57
AM
8315 if (sec2 != NULL)
8316 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8317 if (shndx == SHN_BAD)
589e6347 8318 {
695344c0 8319 /* xgettext:c-format */
9793eb77
AM
8320 _bfd_error_handler
8321 (_("unable to find equivalent output section"
8322 " for symbol '%s' from section '%s'"),
8323 syms[idx]->name ? syms[idx]->name : "<Local sym>",
8324 sec->name);
811072d8 8325 bfd_set_error (bfd_error_invalid_operation);
ef10c3ac 8326 goto error_return;
589e6347 8327 }
079e9a2f
AM
8328 }
8329 }
252b5132 8330
079e9a2f
AM
8331 sym.st_shndx = shndx;
8332 }
252b5132 8333
13ae64f3
JJ
8334 if ((flags & BSF_THREAD_LOCAL) != 0)
8335 type = STT_TLS;
d8045f23
NC
8336 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8337 type = STT_GNU_IFUNC;
13ae64f3 8338 else if ((flags & BSF_FUNCTION) != 0)
079e9a2f
AM
8339 type = STT_FUNC;
8340 else if ((flags & BSF_OBJECT) != 0)
8341 type = STT_OBJECT;
d9352518
DB
8342 else if ((flags & BSF_RELC) != 0)
8343 type = STT_RELC;
8344 else if ((flags & BSF_SRELC) != 0)
8345 type = STT_SRELC;
079e9a2f
AM
8346 else
8347 type = STT_NOTYPE;
252b5132 8348
13ae64f3
JJ
8349 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8350 type = STT_TLS;
8351
589e6347 8352 /* Processor-specific types. */
079e9a2f
AM
8353 if (type_ptr != NULL
8354 && bed->elf_backend_get_symbol_type)
8355 type = ((*bed->elf_backend_get_symbol_type)
8356 (&type_ptr->internal_elf_sym, type));
252b5132 8357
079e9a2f
AM
8358 if (flags & BSF_SECTION_SYM)
8359 {
8360 if (flags & BSF_GLOBAL)
8361 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8362 else
8363 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8364 }
8365 else if (bfd_is_com_section (syms[idx]->section))
0a40daed 8366 {
b8871f35
L
8367 if (type != STT_TLS)
8368 {
8369 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8370 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8371 ? STT_COMMON : STT_OBJECT);
8372 else
8373 type = ((flags & BSF_ELF_COMMON) != 0
8374 ? STT_COMMON : STT_OBJECT);
8375 }
8376 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
0a40daed 8377 }
079e9a2f
AM
8378 else if (bfd_is_und_section (syms[idx]->section))
8379 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8380 ? STB_WEAK
8381 : STB_GLOBAL),
8382 type);
8383 else if (flags & BSF_FILE)
8384 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8385 else
8386 {
8387 int bind = STB_LOCAL;
252b5132 8388
079e9a2f
AM
8389 if (flags & BSF_LOCAL)
8390 bind = STB_LOCAL;
3e7a7d11
NC
8391 else if (flags & BSF_GNU_UNIQUE)
8392 bind = STB_GNU_UNIQUE;
079e9a2f
AM
8393 else if (flags & BSF_WEAK)
8394 bind = STB_WEAK;
8395 else if (flags & BSF_GLOBAL)
8396 bind = STB_GLOBAL;
252b5132 8397
079e9a2f
AM
8398 sym.st_info = ELF_ST_INFO (bind, type);
8399 }
252b5132 8400
079e9a2f 8401 if (type_ptr != NULL)
35fc36a8
RS
8402 {
8403 sym.st_other = type_ptr->internal_elf_sym.st_other;
8404 sym.st_target_internal
8405 = type_ptr->internal_elf_sym.st_target_internal;
8406 }
079e9a2f 8407 else
35fc36a8
RS
8408 {
8409 sym.st_other = 0;
8410 sym.st_target_internal = 0;
8411 }
252b5132 8412
ef10c3ac
L
8413 idx++;
8414 symstrtab[idx].sym = sym;
8415 symstrtab[idx].dest_index = outbound_syms_index;
8416 symstrtab[idx].destshndx_index = outbound_shndx_index;
8417
8418 outbound_syms_index++;
9ad5cbcf 8419 if (outbound_shndx != NULL)
ef10c3ac
L
8420 outbound_shndx_index++;
8421 }
8422
8423 /* Finalize the .strtab section. */
8424 _bfd_elf_strtab_finalize (stt);
8425
8426 /* Swap out the .strtab section. */
8427 for (idx = 0; idx <= symcount; idx++)
8428 {
8429 struct elf_sym_strtab *elfsym = &symstrtab[idx];
8430 if (elfsym->sym.st_name == (unsigned long) -1)
8431 elfsym->sym.st_name = 0;
8432 else
8433 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8434 elfsym->sym.st_name);
8435 bed->s->swap_symbol_out (abfd, &elfsym->sym,
8436 (outbound_syms
8437 + (elfsym->dest_index
8438 * bed->s->sizeof_sym)),
8439 (outbound_shndx
8440 + (elfsym->destshndx_index
8441 * sizeof (Elf_External_Sym_Shndx))));
079e9a2f 8442 }
ef10c3ac 8443 free (symstrtab);
252b5132 8444
079e9a2f 8445 *sttp = stt;
ef10c3ac 8446 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
079e9a2f 8447 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 8448 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
079e9a2f
AM
8449 symstrtab_hdr->sh_addr = 0;
8450 symstrtab_hdr->sh_entsize = 0;
8451 symstrtab_hdr->sh_link = 0;
8452 symstrtab_hdr->sh_info = 0;
8453 symstrtab_hdr->sh_addralign = 1;
252b5132 8454
b34976b6 8455 return TRUE;
252b5132
RH
8456}
8457
8458/* Return the number of bytes required to hold the symtab vector.
8459
8460 Note that we base it on the count plus 1, since we will null terminate
8461 the vector allocated based on this size. However, the ELF symbol table
8462 always has a dummy entry as symbol #0, so it ends up even. */
8463
8464long
217aa764 8465_bfd_elf_get_symtab_upper_bound (bfd *abfd)
252b5132 8466{
3a551c7a 8467 bfd_size_type symcount;
252b5132
RH
8468 long symtab_size;
8469 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
8470
8471 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3a551c7a
AM
8472 if (symcount >= LONG_MAX / sizeof (asymbol *))
8473 {
8474 bfd_set_error (bfd_error_file_too_big);
8475 return -1;
8476 }
b99d1833
AM
8477 symtab_size = (symcount + 1) * (sizeof (asymbol *));
8478 if (symcount > 0)
8479 symtab_size -= sizeof (asymbol *);
252b5132
RH
8480
8481 return symtab_size;
8482}
8483
8484long
217aa764 8485_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
252b5132 8486{
3a551c7a 8487 bfd_size_type symcount;
252b5132
RH
8488 long symtab_size;
8489 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
8490
8491 if (elf_dynsymtab (abfd) == 0)
8492 {
8493 bfd_set_error (bfd_error_invalid_operation);
8494 return -1;
8495 }
8496
8497 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3a551c7a
AM
8498 if (symcount >= LONG_MAX / sizeof (asymbol *))
8499 {
8500 bfd_set_error (bfd_error_file_too_big);
8501 return -1;
8502 }
b99d1833
AM
8503 symtab_size = (symcount + 1) * (sizeof (asymbol *));
8504 if (symcount > 0)
8505 symtab_size -= sizeof (asymbol *);
252b5132
RH
8506
8507 return symtab_size;
8508}
8509
8510long
3c568b8a 8511_bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
252b5132 8512{
3c568b8a
AM
8513 if (asect->reloc_count != 0)
8514 {
8515 /* Sanity check reloc section size. */
8516 struct bfd_elf_section_data *d = elf_section_data (asect);
8517 Elf_Internal_Shdr *rel_hdr = &d->this_hdr;
8518 bfd_size_type ext_rel_size = rel_hdr->sh_size;
8519 ufile_ptr filesize = bfd_get_file_size (abfd);
8520
8521 if (filesize != 0 && ext_rel_size > filesize)
8522 {
8523 bfd_set_error (bfd_error_file_truncated);
8524 return -1;
8525 }
8526 }
8527
242a1159 8528#if SIZEOF_LONG == SIZEOF_INT
7a6e0d89
AM
8529 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
8530 {
8531 bfd_set_error (bfd_error_file_too_big);
8532 return -1;
8533 }
242a1159 8534#endif
252b5132
RH
8535 return (asect->reloc_count + 1) * sizeof (arelent *);
8536}
8537
8538/* Canonicalize the relocs. */
8539
8540long
217aa764
AM
8541_bfd_elf_canonicalize_reloc (bfd *abfd,
8542 sec_ptr section,
8543 arelent **relptr,
8544 asymbol **symbols)
252b5132
RH
8545{
8546 arelent *tblptr;
8547 unsigned int i;
9c5bfbb7 8548 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 8549
b34976b6 8550 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
252b5132
RH
8551 return -1;
8552
8553 tblptr = section->relocation;
8554 for (i = 0; i < section->reloc_count; i++)
8555 *relptr++ = tblptr++;
8556
8557 *relptr = NULL;
8558
8559 return section->reloc_count;
8560}
8561
8562long
6cee3f79 8563_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
252b5132 8564{
9c5bfbb7 8565 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764 8566 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
252b5132
RH
8567
8568 if (symcount >= 0)
ed48ec2e 8569 abfd->symcount = symcount;
252b5132
RH
8570 return symcount;
8571}
8572
8573long
217aa764
AM
8574_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
8575 asymbol **allocation)
252b5132 8576{
9c5bfbb7 8577 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764 8578 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
1f70368c
DJ
8579
8580 if (symcount >= 0)
ed48ec2e 8581 abfd->dynsymcount = symcount;
1f70368c 8582 return symcount;
252b5132
RH
8583}
8584
8615f3f2
AM
8585/* Return the size required for the dynamic reloc entries. Any loadable
8586 section that was actually installed in the BFD, and has type SHT_REL
8587 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8588 dynamic reloc section. */
252b5132
RH
8589
8590long
217aa764 8591_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
252b5132 8592{
3c568b8a 8593 bfd_size_type count, ext_rel_size;
252b5132
RH
8594 asection *s;
8595
8596 if (elf_dynsymtab (abfd) == 0)
8597 {
8598 bfd_set_error (bfd_error_invalid_operation);
8599 return -1;
8600 }
8601
3a551c7a 8602 count = 1;
3c568b8a 8603 ext_rel_size = 0;
252b5132 8604 for (s = abfd->sections; s != NULL; s = s->next)
266b05cf 8605 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132
RH
8606 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8607 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
3a551c7a 8608 {
3c568b8a
AM
8609 ext_rel_size += s->size;
8610 if (ext_rel_size < s->size)
8611 {
8612 bfd_set_error (bfd_error_file_truncated);
8613 return -1;
8614 }
3a551c7a
AM
8615 count += s->size / elf_section_data (s)->this_hdr.sh_entsize;
8616 if (count > LONG_MAX / sizeof (arelent *))
8617 {
8618 bfd_set_error (bfd_error_file_too_big);
8619 return -1;
8620 }
8621 }
3c568b8a
AM
8622 if (count > 1)
8623 {
8624 /* Sanity check reloc section sizes. */
8625 ufile_ptr filesize = bfd_get_file_size (abfd);
8626 if (filesize != 0 && ext_rel_size > filesize)
8627 {
8628 bfd_set_error (bfd_error_file_truncated);
8629 return -1;
8630 }
8631 }
3a551c7a 8632 return count * sizeof (arelent *);
252b5132
RH
8633}
8634
8615f3f2
AM
8635/* Canonicalize the dynamic relocation entries. Note that we return the
8636 dynamic relocations as a single block, although they are actually
8637 associated with particular sections; the interface, which was
8638 designed for SunOS style shared libraries, expects that there is only
8639 one set of dynamic relocs. Any loadable section that was actually
8640 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8641 dynamic symbol table, is considered to be a dynamic reloc section. */
252b5132
RH
8642
8643long
217aa764
AM
8644_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
8645 arelent **storage,
8646 asymbol **syms)
252b5132 8647{
217aa764 8648 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
252b5132
RH
8649 asection *s;
8650 long ret;
8651
8652 if (elf_dynsymtab (abfd) == 0)
8653 {
8654 bfd_set_error (bfd_error_invalid_operation);
8655 return -1;
8656 }
8657
8658 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8659 ret = 0;
8660 for (s = abfd->sections; s != NULL; s = s->next)
8661 {
266b05cf 8662 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132
RH
8663 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8664 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8665 {
8666 arelent *p;
8667 long count, i;
8668
b34976b6 8669 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
252b5132 8670 return -1;
eea6121a 8671 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
252b5132
RH
8672 p = s->relocation;
8673 for (i = 0; i < count; i++)
8674 *storage++ = p++;
8675 ret += count;
8676 }
8677 }
8678
8679 *storage = NULL;
8680
8681 return ret;
8682}
8683\f
8684/* Read in the version information. */
8685
b34976b6 8686bfd_boolean
fc0e6df6 8687_bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
252b5132
RH
8688{
8689 bfd_byte *contents = NULL;
fc0e6df6 8690 unsigned int freeidx = 0;
1f4361a7 8691 size_t amt;
fc0e6df6
PB
8692
8693 if (elf_dynverref (abfd) != 0)
8694 {
8695 Elf_Internal_Shdr *hdr;
8696 Elf_External_Verneed *everneed;
8697 Elf_Internal_Verneed *iverneed;
8698 unsigned int i;
d0fb9a8d 8699 bfd_byte *contents_end;
fc0e6df6
PB
8700
8701 hdr = &elf_tdata (abfd)->dynverref_hdr;
8702
bd61e135
AM
8703 if (hdr->sh_info == 0
8704 || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
d0fb9a8d 8705 {
dc1e8a47 8706 error_return_bad_verref:
4eca0228 8707 _bfd_error_handler
871b3ab2 8708 (_("%pB: .gnu.version_r invalid entry"), abfd);
601a03ba 8709 bfd_set_error (bfd_error_bad_value);
dc1e8a47 8710 error_return_verref:
d0fb9a8d
JJ
8711 elf_tdata (abfd)->verref = NULL;
8712 elf_tdata (abfd)->cverrefs = 0;
8713 goto error_return;
8714 }
601a03ba 8715
2bb3687b
AM
8716 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
8717 goto error_return_verref;
8718 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
8719 if (contents == NULL)
d0fb9a8d 8720 goto error_return_verref;
fc0e6df6 8721
1f4361a7
AM
8722 if (_bfd_mul_overflow (hdr->sh_info, sizeof (Elf_Internal_Verneed), &amt))
8723 {
8724 bfd_set_error (bfd_error_file_too_big);
8725 goto error_return_verref;
8726 }
8727 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_alloc (abfd, amt);
601a03ba 8728 if (elf_tdata (abfd)->verref == NULL)
d0fb9a8d
JJ
8729 goto error_return_verref;
8730
8731 BFD_ASSERT (sizeof (Elf_External_Verneed)
8732 == sizeof (Elf_External_Vernaux));
8733 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
fc0e6df6
PB
8734 everneed = (Elf_External_Verneed *) contents;
8735 iverneed = elf_tdata (abfd)->verref;
8736 for (i = 0; i < hdr->sh_info; i++, iverneed++)
8737 {
8738 Elf_External_Vernaux *evernaux;
8739 Elf_Internal_Vernaux *ivernaux;
8740 unsigned int j;
8741
8742 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
8743
8744 iverneed->vn_bfd = abfd;
8745
8746 iverneed->vn_filename =
8747 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8748 iverneed->vn_file);
8749 if (iverneed->vn_filename == NULL)
601a03ba 8750 goto error_return_bad_verref;
fc0e6df6 8751
d0fb9a8d
JJ
8752 if (iverneed->vn_cnt == 0)
8753 iverneed->vn_auxptr = NULL;
8754 else
8755 {
1f4361a7
AM
8756 if (_bfd_mul_overflow (iverneed->vn_cnt,
8757 sizeof (Elf_Internal_Vernaux), &amt))
8758 {
8759 bfd_set_error (bfd_error_file_too_big);
8760 goto error_return_verref;
8761 }
a50b1753 8762 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
1f4361a7 8763 bfd_alloc (abfd, amt);
d0fb9a8d
JJ
8764 if (iverneed->vn_auxptr == NULL)
8765 goto error_return_verref;
8766 }
8767
8768 if (iverneed->vn_aux
8769 > (size_t) (contents_end - (bfd_byte *) everneed))
601a03ba 8770 goto error_return_bad_verref;
fc0e6df6
PB
8771
8772 evernaux = ((Elf_External_Vernaux *)
8773 ((bfd_byte *) everneed + iverneed->vn_aux));
8774 ivernaux = iverneed->vn_auxptr;
8775 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
8776 {
8777 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
8778
8779 ivernaux->vna_nodename =
8780 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8781 ivernaux->vna_name);
8782 if (ivernaux->vna_nodename == NULL)
601a03ba 8783 goto error_return_bad_verref;
fc0e6df6 8784
25ff461f
AM
8785 if (ivernaux->vna_other > freeidx)
8786 freeidx = ivernaux->vna_other;
8787
8788 ivernaux->vna_nextptr = NULL;
8789 if (ivernaux->vna_next == 0)
8790 {
8791 iverneed->vn_cnt = j + 1;
8792 break;
8793 }
fc0e6df6
PB
8794 if (j + 1 < iverneed->vn_cnt)
8795 ivernaux->vna_nextptr = ivernaux + 1;
fc0e6df6 8796
d0fb9a8d
JJ
8797 if (ivernaux->vna_next
8798 > (size_t) (contents_end - (bfd_byte *) evernaux))
601a03ba 8799 goto error_return_bad_verref;
d0fb9a8d 8800
fc0e6df6
PB
8801 evernaux = ((Elf_External_Vernaux *)
8802 ((bfd_byte *) evernaux + ivernaux->vna_next));
fc0e6df6
PB
8803 }
8804
25ff461f
AM
8805 iverneed->vn_nextref = NULL;
8806 if (iverneed->vn_next == 0)
8807 break;
fc0e6df6
PB
8808 if (i + 1 < hdr->sh_info)
8809 iverneed->vn_nextref = iverneed + 1;
fc0e6df6 8810
d0fb9a8d
JJ
8811 if (iverneed->vn_next
8812 > (size_t) (contents_end - (bfd_byte *) everneed))
601a03ba 8813 goto error_return_bad_verref;
d0fb9a8d 8814
fc0e6df6
PB
8815 everneed = ((Elf_External_Verneed *)
8816 ((bfd_byte *) everneed + iverneed->vn_next));
8817 }
25ff461f 8818 elf_tdata (abfd)->cverrefs = i;
fc0e6df6
PB
8819
8820 free (contents);
8821 contents = NULL;
8822 }
252b5132
RH
8823
8824 if (elf_dynverdef (abfd) != 0)
8825 {
8826 Elf_Internal_Shdr *hdr;
8827 Elf_External_Verdef *everdef;
8828 Elf_Internal_Verdef *iverdef;
f631889e
UD
8829 Elf_Internal_Verdef *iverdefarr;
8830 Elf_Internal_Verdef iverdefmem;
252b5132 8831 unsigned int i;
062e2358 8832 unsigned int maxidx;
d0fb9a8d 8833 bfd_byte *contents_end_def, *contents_end_aux;
252b5132
RH
8834
8835 hdr = &elf_tdata (abfd)->dynverdef_hdr;
8836
601a03ba
AM
8837 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
8838 {
8839 error_return_bad_verdef:
4eca0228 8840 _bfd_error_handler
871b3ab2 8841 (_("%pB: .gnu.version_d invalid entry"), abfd);
601a03ba
AM
8842 bfd_set_error (bfd_error_bad_value);
8843 error_return_verdef:
8844 elf_tdata (abfd)->verdef = NULL;
8845 elf_tdata (abfd)->cverdefs = 0;
8846 goto error_return;
8847 }
8848
2bb3687b 8849 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
601a03ba 8850 goto error_return_verdef;
2bb3687b
AM
8851 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
8852 if (contents == NULL)
601a03ba 8853 goto error_return_verdef;
d0fb9a8d
JJ
8854
8855 BFD_ASSERT (sizeof (Elf_External_Verdef)
8856 >= sizeof (Elf_External_Verdaux));
8857 contents_end_def = contents + hdr->sh_size
8858 - sizeof (Elf_External_Verdef);
8859 contents_end_aux = contents + hdr->sh_size
8860 - sizeof (Elf_External_Verdaux);
8861
f631889e
UD
8862 /* We know the number of entries in the section but not the maximum
8863 index. Therefore we have to run through all entries and find
8864 the maximum. */
252b5132 8865 everdef = (Elf_External_Verdef *) contents;
f631889e
UD
8866 maxidx = 0;
8867 for (i = 0; i < hdr->sh_info; ++i)
8868 {
8869 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8870
601a03ba
AM
8871 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
8872 goto error_return_bad_verdef;
062e2358
AM
8873 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
8874 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
f631889e 8875
25ff461f
AM
8876 if (iverdefmem.vd_next == 0)
8877 break;
8878
d0fb9a8d
JJ
8879 if (iverdefmem.vd_next
8880 > (size_t) (contents_end_def - (bfd_byte *) everdef))
601a03ba 8881 goto error_return_bad_verdef;
d0fb9a8d 8882
f631889e
UD
8883 everdef = ((Elf_External_Verdef *)
8884 ((bfd_byte *) everdef + iverdefmem.vd_next));
8885 }
8886
fc0e6df6
PB
8887 if (default_imported_symver)
8888 {
8889 if (freeidx > maxidx)
8890 maxidx = ++freeidx;
8891 else
8892 freeidx = ++maxidx;
8893 }
1f4361a7
AM
8894 if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
8895 {
8896 bfd_set_error (bfd_error_file_too_big);
8897 goto error_return_verdef;
8898 }
8899 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
f631889e 8900 if (elf_tdata (abfd)->verdef == NULL)
601a03ba 8901 goto error_return_verdef;
f631889e
UD
8902
8903 elf_tdata (abfd)->cverdefs = maxidx;
8904
8905 everdef = (Elf_External_Verdef *) contents;
8906 iverdefarr = elf_tdata (abfd)->verdef;
8907 for (i = 0; i < hdr->sh_info; i++)
252b5132
RH
8908 {
8909 Elf_External_Verdaux *everdaux;
8910 Elf_Internal_Verdaux *iverdaux;
8911 unsigned int j;
8912
f631889e
UD
8913 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8914
d0fb9a8d 8915 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
601a03ba 8916 goto error_return_bad_verdef;
d0fb9a8d 8917
f631889e 8918 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
595bce75 8919 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
252b5132
RH
8920
8921 iverdef->vd_bfd = abfd;
8922
d0fb9a8d
JJ
8923 if (iverdef->vd_cnt == 0)
8924 iverdef->vd_auxptr = NULL;
8925 else
8926 {
1f4361a7
AM
8927 if (_bfd_mul_overflow (iverdef->vd_cnt,
8928 sizeof (Elf_Internal_Verdaux), &amt))
8929 {
8930 bfd_set_error (bfd_error_file_too_big);
8931 goto error_return_verdef;
8932 }
a50b1753 8933 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
1f4361a7 8934 bfd_alloc (abfd, amt);
d0fb9a8d
JJ
8935 if (iverdef->vd_auxptr == NULL)
8936 goto error_return_verdef;
8937 }
8938
8939 if (iverdef->vd_aux
8940 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
601a03ba 8941 goto error_return_bad_verdef;
252b5132
RH
8942
8943 everdaux = ((Elf_External_Verdaux *)
8944 ((bfd_byte *) everdef + iverdef->vd_aux));
8945 iverdaux = iverdef->vd_auxptr;
8946 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
8947 {
8948 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
8949
8950 iverdaux->vda_nodename =
8951 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8952 iverdaux->vda_name);
8953 if (iverdaux->vda_nodename == NULL)
601a03ba 8954 goto error_return_bad_verdef;
252b5132 8955
25ff461f
AM
8956 iverdaux->vda_nextptr = NULL;
8957 if (iverdaux->vda_next == 0)
8958 {
8959 iverdef->vd_cnt = j + 1;
8960 break;
8961 }
252b5132
RH
8962 if (j + 1 < iverdef->vd_cnt)
8963 iverdaux->vda_nextptr = iverdaux + 1;
252b5132 8964
d0fb9a8d
JJ
8965 if (iverdaux->vda_next
8966 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
601a03ba 8967 goto error_return_bad_verdef;
d0fb9a8d 8968
252b5132
RH
8969 everdaux = ((Elf_External_Verdaux *)
8970 ((bfd_byte *) everdaux + iverdaux->vda_next));
8971 }
8972
595bce75 8973 iverdef->vd_nodename = NULL;
d0fb9a8d
JJ
8974 if (iverdef->vd_cnt)
8975 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
252b5132 8976
25ff461f
AM
8977 iverdef->vd_nextdef = NULL;
8978 if (iverdef->vd_next == 0)
8979 break;
d0fb9a8d 8980 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
252b5132 8981 iverdef->vd_nextdef = iverdef + 1;
252b5132
RH
8982
8983 everdef = ((Elf_External_Verdef *)
8984 ((bfd_byte *) everdef + iverdef->vd_next));
8985 }
8986
8987 free (contents);
8988 contents = NULL;
8989 }
fc0e6df6 8990 else if (default_imported_symver)
252b5132 8991 {
fc0e6df6
PB
8992 if (freeidx < 3)
8993 freeidx = 3;
8994 else
8995 freeidx++;
252b5132 8996
1f4361a7
AM
8997 if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
8998 {
8999 bfd_set_error (bfd_error_file_too_big);
9000 goto error_return;
9001 }
9002 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
fc0e6df6 9003 if (elf_tdata (abfd)->verdef == NULL)
252b5132
RH
9004 goto error_return;
9005
fc0e6df6
PB
9006 elf_tdata (abfd)->cverdefs = freeidx;
9007 }
252b5132 9008
fc0e6df6
PB
9009 /* Create a default version based on the soname. */
9010 if (default_imported_symver)
9011 {
9012 Elf_Internal_Verdef *iverdef;
9013 Elf_Internal_Verdaux *iverdaux;
252b5132 9014
5bb3703f 9015 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
252b5132 9016
fc0e6df6
PB
9017 iverdef->vd_version = VER_DEF_CURRENT;
9018 iverdef->vd_flags = 0;
9019 iverdef->vd_ndx = freeidx;
9020 iverdef->vd_cnt = 1;
252b5132 9021
fc0e6df6 9022 iverdef->vd_bfd = abfd;
252b5132 9023
fc0e6df6
PB
9024 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9025 if (iverdef->vd_nodename == NULL)
d0fb9a8d 9026 goto error_return_verdef;
fc0e6df6 9027 iverdef->vd_nextdef = NULL;
601a03ba
AM
9028 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
9029 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
d0fb9a8d
JJ
9030 if (iverdef->vd_auxptr == NULL)
9031 goto error_return_verdef;
252b5132 9032
fc0e6df6
PB
9033 iverdaux = iverdef->vd_auxptr;
9034 iverdaux->vda_nodename = iverdef->vd_nodename;
252b5132
RH
9035 }
9036
b34976b6 9037 return TRUE;
252b5132
RH
9038
9039 error_return:
5ed6aba4 9040 if (contents != NULL)
252b5132 9041 free (contents);
b34976b6 9042 return FALSE;
252b5132
RH
9043}
9044\f
9045asymbol *
217aa764 9046_bfd_elf_make_empty_symbol (bfd *abfd)
252b5132
RH
9047{
9048 elf_symbol_type *newsym;
9049
7a6e0d89 9050 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
252b5132
RH
9051 if (!newsym)
9052 return NULL;
201159ec
NC
9053 newsym->symbol.the_bfd = abfd;
9054 return &newsym->symbol;
252b5132
RH
9055}
9056
9057void
217aa764
AM
9058_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9059 asymbol *symbol,
9060 symbol_info *ret)
252b5132
RH
9061{
9062 bfd_symbol_info (symbol, ret);
9063}
9064
9065/* Return whether a symbol name implies a local symbol. Most targets
9066 use this function for the is_local_label_name entry point, but some
9067 override it. */
9068
b34976b6 9069bfd_boolean
217aa764
AM
9070_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9071 const char *name)
252b5132
RH
9072{
9073 /* Normal local symbols start with ``.L''. */
9074 if (name[0] == '.' && name[1] == 'L')
b34976b6 9075 return TRUE;
252b5132
RH
9076
9077 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9078 DWARF debugging symbols starting with ``..''. */
9079 if (name[0] == '.' && name[1] == '.')
b34976b6 9080 return TRUE;
252b5132
RH
9081
9082 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9083 emitting DWARF debugging output. I suspect this is actually a
9084 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9085 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9086 underscore to be emitted on some ELF targets). For ease of use,
9087 we treat such symbols as local. */
9088 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
b34976b6 9089 return TRUE;
252b5132 9090
b1fa9dd6
NC
9091 /* Treat assembler generated fake symbols, dollar local labels and
9092 forward-backward labels (aka local labels) as locals.
9093 These labels have the form:
9094
07d6d2b8 9095 L0^A.* (fake symbols)
b1fa9dd6
NC
9096
9097 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9098
9099 Versions which start with .L will have already been matched above,
9100 so we only need to match the rest. */
9101 if (name[0] == 'L' && ISDIGIT (name[1]))
9102 {
9103 bfd_boolean ret = FALSE;
9104 const char * p;
9105 char c;
9106
9107 for (p = name + 2; (c = *p); p++)
9108 {
9109 if (c == 1 || c == 2)
9110 {
9111 if (c == 1 && p == name + 2)
9112 /* A fake symbol. */
9113 return TRUE;
9114
9115 /* FIXME: We are being paranoid here and treating symbols like
9116 L0^Bfoo as if there were non-local, on the grounds that the
9117 assembler will never generate them. But can any symbol
9118 containing an ASCII value in the range 1-31 ever be anything
9119 other than some kind of local ? */
9120 ret = TRUE;
9121 }
9122
9123 if (! ISDIGIT (c))
9124 {
9125 ret = FALSE;
9126 break;
9127 }
9128 }
9129 return ret;
9130 }
ffa54770 9131
b34976b6 9132 return FALSE;
252b5132
RH
9133}
9134
9135alent *
217aa764
AM
9136_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9137 asymbol *symbol ATTRIBUTE_UNUSED)
252b5132
RH
9138{
9139 abort ();
9140 return NULL;
9141}
9142
b34976b6 9143bfd_boolean
217aa764
AM
9144_bfd_elf_set_arch_mach (bfd *abfd,
9145 enum bfd_architecture arch,
9146 unsigned long machine)
252b5132
RH
9147{
9148 /* If this isn't the right architecture for this backend, and this
9149 isn't the generic backend, fail. */
9150 if (arch != get_elf_backend_data (abfd)->arch
9151 && arch != bfd_arch_unknown
9152 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
b34976b6 9153 return FALSE;
252b5132
RH
9154
9155 return bfd_default_set_arch_mach (abfd, arch, machine);
9156}
9157
d1fad7c6
NC
9158/* Find the nearest line to a particular section and offset,
9159 for error reporting. */
9160
b34976b6 9161bfd_boolean
217aa764 9162_bfd_elf_find_nearest_line (bfd *abfd,
217aa764 9163 asymbol **symbols,
fb167eb2 9164 asection *section,
217aa764
AM
9165 bfd_vma offset,
9166 const char **filename_ptr,
9167 const char **functionname_ptr,
fb167eb2
AM
9168 unsigned int *line_ptr,
9169 unsigned int *discriminator_ptr)
d1fad7c6 9170{
b34976b6 9171 bfd_boolean found;
d1fad7c6 9172
fb167eb2 9173 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
4e8a9624 9174 filename_ptr, functionname_ptr,
fb167eb2 9175 line_ptr, discriminator_ptr,
9defd221 9176 dwarf_debug_sections,
e7679060
AM
9177 &elf_tdata (abfd)->dwarf2_find_line_info))
9178 return TRUE;
9179
9180 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9181 filename_ptr, functionname_ptr, line_ptr))
d1fad7c6
NC
9182 {
9183 if (!*functionname_ptr)
e00e8198
AM
9184 _bfd_elf_find_function (abfd, symbols, section, offset,
9185 *filename_ptr ? NULL : filename_ptr,
9186 functionname_ptr);
b34976b6 9187 return TRUE;
d1fad7c6
NC
9188 }
9189
9190 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4e8a9624
AM
9191 &found, filename_ptr,
9192 functionname_ptr, line_ptr,
9193 &elf_tdata (abfd)->line_info))
b34976b6 9194 return FALSE;
dc43ada5 9195 if (found && (*functionname_ptr || *line_ptr))
b34976b6 9196 return TRUE;
d1fad7c6
NC
9197
9198 if (symbols == NULL)
b34976b6 9199 return FALSE;
d1fad7c6 9200
e00e8198
AM
9201 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9202 filename_ptr, functionname_ptr))
b34976b6 9203 return FALSE;
d1fad7c6 9204
252b5132 9205 *line_ptr = 0;
b34976b6 9206 return TRUE;
252b5132
RH
9207}
9208
5420f73d
L
9209/* Find the line for a symbol. */
9210
9211bfd_boolean
9212_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9213 const char **filename_ptr, unsigned int *line_ptr)
9b8d1a36 9214{
fb167eb2
AM
9215 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9216 filename_ptr, NULL, line_ptr, NULL,
9defd221 9217 dwarf_debug_sections,
fb167eb2 9218 &elf_tdata (abfd)->dwarf2_find_line_info);
5420f73d
L
9219}
9220
4ab527b0
FF
9221/* After a call to bfd_find_nearest_line, successive calls to
9222 bfd_find_inliner_info can be used to get source information about
9223 each level of function inlining that terminated at the address
9224 passed to bfd_find_nearest_line. Currently this is only supported
9225 for DWARF2 with appropriate DWARF3 extensions. */
9226
9227bfd_boolean
9228_bfd_elf_find_inliner_info (bfd *abfd,
9229 const char **filename_ptr,
9230 const char **functionname_ptr,
9231 unsigned int *line_ptr)
9232{
9233 bfd_boolean found;
9234 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9235 functionname_ptr, line_ptr,
9236 & elf_tdata (abfd)->dwarf2_find_line_info);
9237 return found;
9238}
9239
252b5132 9240int
a6b96beb 9241_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
252b5132 9242{
8ded5a0f
AM
9243 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9244 int ret = bed->s->sizeof_ehdr;
252b5132 9245
0e1862bb 9246 if (!bfd_link_relocatable (info))
8ded5a0f 9247 {
12bd6957 9248 bfd_size_type phdr_size = elf_program_header_size (abfd);
8ded5a0f 9249
62d7a5f6
AM
9250 if (phdr_size == (bfd_size_type) -1)
9251 {
9252 struct elf_segment_map *m;
9253
9254 phdr_size = 0;
12bd6957 9255 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
62d7a5f6 9256 phdr_size += bed->s->sizeof_phdr;
8ded5a0f 9257
62d7a5f6
AM
9258 if (phdr_size == 0)
9259 phdr_size = get_program_header_size (abfd, info);
9260 }
8ded5a0f 9261
12bd6957 9262 elf_program_header_size (abfd) = phdr_size;
8ded5a0f
AM
9263 ret += phdr_size;
9264 }
9265
252b5132
RH
9266 return ret;
9267}
9268
b34976b6 9269bfd_boolean
217aa764
AM
9270_bfd_elf_set_section_contents (bfd *abfd,
9271 sec_ptr section,
0f867abe 9272 const void *location,
217aa764
AM
9273 file_ptr offset,
9274 bfd_size_type count)
252b5132
RH
9275{
9276 Elf_Internal_Shdr *hdr;
1b6aeedb 9277 file_ptr pos;
252b5132
RH
9278
9279 if (! abfd->output_has_begun
217aa764 9280 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
b34976b6 9281 return FALSE;
252b5132 9282
0ce398f1
L
9283 if (!count)
9284 return TRUE;
9285
252b5132 9286 hdr = &elf_section_data (section)->this_hdr;
0ce398f1
L
9287 if (hdr->sh_offset == (file_ptr) -1)
9288 {
a0dcf297
NC
9289 unsigned char *contents;
9290
1ff6de03
NA
9291 if (bfd_section_is_ctf (section))
9292 /* Nothing to do with this section: the contents are generated
9293 later. */
9294 return TRUE;
9295
a0dcf297
NC
9296 if ((section->flags & SEC_ELF_COMPRESS) == 0)
9297 {
9298 _bfd_error_handler
9299 (_("%pB:%pA: error: attempting to write into an unallocated compressed section"),
9300 abfd, section);
9301 bfd_set_error (bfd_error_invalid_operation);
9302 return FALSE;
9303 }
9304
9305 if ((offset + count) > hdr->sh_size)
9306 {
9307 _bfd_error_handler
9308 (_("%pB:%pA: error: attempting to write over the end of the section"),
9309 abfd, section);
9310
9311 bfd_set_error (bfd_error_invalid_operation);
9312 return FALSE;
9313 }
9314
9315 contents = hdr->contents;
9316 if (contents == NULL)
9317 {
9318 _bfd_error_handler
9319 (_("%pB:%pA: error: attempting to write section into an empty buffer"),
9320 abfd, section);
9321
9322 bfd_set_error (bfd_error_invalid_operation);
9323 return FALSE;
9324 }
9325
0ce398f1
L
9326 memcpy (contents + offset, location, count);
9327 return TRUE;
9328 }
a0dcf297 9329
dc810e39
AM
9330 pos = hdr->sh_offset + offset;
9331 if (bfd_seek (abfd, pos, SEEK_SET) != 0
9332 || bfd_bwrite (location, count, abfd) != count)
b34976b6 9333 return FALSE;
252b5132 9334
b34976b6 9335 return TRUE;
252b5132
RH
9336}
9337
f3185997 9338bfd_boolean
217aa764
AM
9339_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
9340 arelent *cache_ptr ATTRIBUTE_UNUSED,
9341 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
252b5132
RH
9342{
9343 abort ();
f3185997 9344 return FALSE;
252b5132
RH
9345}
9346
252b5132
RH
9347/* Try to convert a non-ELF reloc into an ELF one. */
9348
b34976b6 9349bfd_boolean
217aa764 9350_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
252b5132 9351{
c044fabd 9352 /* Check whether we really have an ELF howto. */
252b5132
RH
9353
9354 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
9355 {
9356 bfd_reloc_code_real_type code;
9357 reloc_howto_type *howto;
9358
9359 /* Alien reloc: Try to determine its type to replace it with an
c044fabd 9360 equivalent ELF reloc. */
252b5132
RH
9361
9362 if (areloc->howto->pc_relative)
9363 {
9364 switch (areloc->howto->bitsize)
9365 {
9366 case 8:
9367 code = BFD_RELOC_8_PCREL;
9368 break;
9369 case 12:
9370 code = BFD_RELOC_12_PCREL;
9371 break;
9372 case 16:
9373 code = BFD_RELOC_16_PCREL;
9374 break;
9375 case 24:
9376 code = BFD_RELOC_24_PCREL;
9377 break;
9378 case 32:
9379 code = BFD_RELOC_32_PCREL;
9380 break;
9381 case 64:
9382 code = BFD_RELOC_64_PCREL;
9383 break;
9384 default:
9385 goto fail;
9386 }
9387
9388 howto = bfd_reloc_type_lookup (abfd, code);
9389
94698d01 9390 if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
252b5132
RH
9391 {
9392 if (howto->pcrel_offset)
9393 areloc->addend += areloc->address;
9394 else
9395 areloc->addend -= areloc->address; /* addend is unsigned!! */
9396 }
9397 }
9398 else
9399 {
9400 switch (areloc->howto->bitsize)
9401 {
9402 case 8:
9403 code = BFD_RELOC_8;
9404 break;
9405 case 14:
9406 code = BFD_RELOC_14;
9407 break;
9408 case 16:
9409 code = BFD_RELOC_16;
9410 break;
9411 case 26:
9412 code = BFD_RELOC_26;
9413 break;
9414 case 32:
9415 code = BFD_RELOC_32;
9416 break;
9417 case 64:
9418 code = BFD_RELOC_64;
9419 break;
9420 default:
9421 goto fail;
9422 }
9423
9424 howto = bfd_reloc_type_lookup (abfd, code);
9425 }
9426
9427 if (howto)
9428 areloc->howto = howto;
9429 else
9430 goto fail;
9431 }
9432
b34976b6 9433 return TRUE;
252b5132
RH
9434
9435 fail:
0aa13fee
AM
9436 /* xgettext:c-format */
9437 _bfd_error_handler (_("%pB: %s unsupported"),
9438 abfd, areloc->howto->name);
9aea1e31 9439 bfd_set_error (bfd_error_sorry);
b34976b6 9440 return FALSE;
252b5132
RH
9441}
9442
b34976b6 9443bfd_boolean
217aa764 9444_bfd_elf_close_and_cleanup (bfd *abfd)
252b5132 9445{
d9071b0c
TG
9446 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9447 if (bfd_get_format (abfd) == bfd_object && tdata != NULL)
252b5132 9448 {
c0355132 9449 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
2b0f7ef9 9450 _bfd_elf_strtab_free (elf_shstrtab (abfd));
d9071b0c 9451 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
252b5132
RH
9452 }
9453
9454 return _bfd_generic_close_and_cleanup (abfd);
9455}
9456
9457/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9458 in the relocation's offset. Thus we cannot allow any sort of sanity
9459 range-checking to interfere. There is nothing else to do in processing
9460 this reloc. */
9461
9462bfd_reloc_status_type
217aa764
AM
9463_bfd_elf_rel_vtable_reloc_fn
9464 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
fc0a2244 9465 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
217aa764
AM
9466 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
9467 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
252b5132
RH
9468{
9469 return bfd_reloc_ok;
9470}
252b5132
RH
9471\f
9472/* Elf core file support. Much of this only works on native
9473 toolchains, since we rely on knowing the
9474 machine-dependent procfs structure in order to pick
c044fabd 9475 out details about the corefile. */
252b5132
RH
9476
9477#ifdef HAVE_SYS_PROCFS_H
16231b7b
DG
9478/* Needed for new procfs interface on sparc-solaris. */
9479# define _STRUCTURED_PROC 1
252b5132
RH
9480# include <sys/procfs.h>
9481#endif
9482
261b8d08
PA
9483/* Return a PID that identifies a "thread" for threaded cores, or the
9484 PID of the main process for non-threaded cores. */
252b5132
RH
9485
9486static int
217aa764 9487elfcore_make_pid (bfd *abfd)
252b5132 9488{
261b8d08
PA
9489 int pid;
9490
228e534f 9491 pid = elf_tdata (abfd)->core->lwpid;
261b8d08 9492 if (pid == 0)
228e534f 9493 pid = elf_tdata (abfd)->core->pid;
261b8d08
PA
9494
9495 return pid;
252b5132
RH
9496}
9497
252b5132
RH
9498/* If there isn't a section called NAME, make one, using
9499 data from SECT. Note, this function will generate a
9500 reference to NAME, so you shouldn't deallocate or
c044fabd 9501 overwrite it. */
252b5132 9502
b34976b6 9503static bfd_boolean
217aa764 9504elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
252b5132 9505{
c044fabd 9506 asection *sect2;
252b5132
RH
9507
9508 if (bfd_get_section_by_name (abfd, name) != NULL)
b34976b6 9509 return TRUE;
252b5132 9510
117ed4f8 9511 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
252b5132 9512 if (sect2 == NULL)
b34976b6 9513 return FALSE;
252b5132 9514
eea6121a 9515 sect2->size = sect->size;
252b5132 9516 sect2->filepos = sect->filepos;
252b5132 9517 sect2->alignment_power = sect->alignment_power;
b34976b6 9518 return TRUE;
252b5132
RH
9519}
9520
bb0082d6
AM
9521/* Create a pseudosection containing SIZE bytes at FILEPOS. This
9522 actually creates up to two pseudosections:
9523 - For the single-threaded case, a section named NAME, unless
9524 such a section already exists.
9525 - For the multi-threaded case, a section named "NAME/PID", where
9526 PID is elfcore_make_pid (abfd).
24d3e51b 9527 Both pseudosections have identical contents. */
b34976b6 9528bfd_boolean
217aa764
AM
9529_bfd_elfcore_make_pseudosection (bfd *abfd,
9530 char *name,
9531 size_t size,
9532 ufile_ptr filepos)
bb0082d6
AM
9533{
9534 char buf[100];
9535 char *threaded_name;
d4c88bbb 9536 size_t len;
bb0082d6
AM
9537 asection *sect;
9538
9539 /* Build the section name. */
9540
9541 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
d4c88bbb 9542 len = strlen (buf) + 1;
a50b1753 9543 threaded_name = (char *) bfd_alloc (abfd, len);
bb0082d6 9544 if (threaded_name == NULL)
b34976b6 9545 return FALSE;
d4c88bbb 9546 memcpy (threaded_name, buf, len);
bb0082d6 9547
117ed4f8
AM
9548 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
9549 SEC_HAS_CONTENTS);
bb0082d6 9550 if (sect == NULL)
b34976b6 9551 return FALSE;
eea6121a 9552 sect->size = size;
bb0082d6 9553 sect->filepos = filepos;
bb0082d6
AM
9554 sect->alignment_power = 2;
9555
936e320b 9556 return elfcore_maybe_make_sect (abfd, name, sect);
bb0082d6
AM
9557}
9558
58e07198
CZ
9559static bfd_boolean
9560elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
9561 size_t offs)
9562{
9563 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9564 SEC_HAS_CONTENTS);
9565
9566 if (sect == NULL)
9567 return FALSE;
9568
9569 sect->size = note->descsz - offs;
9570 sect->filepos = note->descpos + offs;
9571 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9572
9573 return TRUE;
9574}
9575
252b5132 9576/* prstatus_t exists on:
4a938328 9577 solaris 2.5+
252b5132
RH
9578 linux 2.[01] + glibc
9579 unixware 4.2
9580*/
9581
9582#if defined (HAVE_PRSTATUS_T)
a7b97311 9583
b34976b6 9584static bfd_boolean
217aa764 9585elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 9586{
eea6121a 9587 size_t size;
7ee38065 9588 int offset;
252b5132 9589
4a938328
MS
9590 if (note->descsz == sizeof (prstatus_t))
9591 {
9592 prstatus_t prstat;
252b5132 9593
eea6121a 9594 size = sizeof (prstat.pr_reg);
7ee38065 9595 offset = offsetof (prstatus_t, pr_reg);
4a938328 9596 memcpy (&prstat, note->descdata, sizeof (prstat));
252b5132 9597
fa49d224
NC
9598 /* Do not overwrite the core signal if it
9599 has already been set by another thread. */
228e534f
AM
9600 if (elf_tdata (abfd)->core->signal == 0)
9601 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9602 if (elf_tdata (abfd)->core->pid == 0)
9603 elf_tdata (abfd)->core->pid = prstat.pr_pid;
252b5132 9604
4a938328
MS
9605 /* pr_who exists on:
9606 solaris 2.5+
9607 unixware 4.2
9608 pr_who doesn't exist on:
9609 linux 2.[01]
9610 */
252b5132 9611#if defined (HAVE_PRSTATUS_T_PR_WHO)
228e534f 9612 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
261b8d08 9613#else
228e534f 9614 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
252b5132 9615#endif
4a938328 9616 }
7ee38065 9617#if defined (HAVE_PRSTATUS32_T)
4a938328
MS
9618 else if (note->descsz == sizeof (prstatus32_t))
9619 {
9620 /* 64-bit host, 32-bit corefile */
9621 prstatus32_t prstat;
9622
eea6121a 9623 size = sizeof (prstat.pr_reg);
7ee38065 9624 offset = offsetof (prstatus32_t, pr_reg);
4a938328
MS
9625 memcpy (&prstat, note->descdata, sizeof (prstat));
9626
fa49d224
NC
9627 /* Do not overwrite the core signal if it
9628 has already been set by another thread. */
228e534f
AM
9629 if (elf_tdata (abfd)->core->signal == 0)
9630 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9631 if (elf_tdata (abfd)->core->pid == 0)
9632 elf_tdata (abfd)->core->pid = prstat.pr_pid;
4a938328
MS
9633
9634 /* pr_who exists on:
9635 solaris 2.5+
9636 unixware 4.2
9637 pr_who doesn't exist on:
9638 linux 2.[01]
9639 */
7ee38065 9640#if defined (HAVE_PRSTATUS32_T_PR_WHO)
228e534f 9641 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
261b8d08 9642#else
228e534f 9643 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
4a938328
MS
9644#endif
9645 }
7ee38065 9646#endif /* HAVE_PRSTATUS32_T */
4a938328
MS
9647 else
9648 {
9649 /* Fail - we don't know how to handle any other
9650 note size (ie. data object type). */
b34976b6 9651 return TRUE;
4a938328 9652 }
252b5132 9653
bb0082d6 9654 /* Make a ".reg/999" section and a ".reg" section. */
936e320b 9655 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 9656 size, note->descpos + offset);
252b5132
RH
9657}
9658#endif /* defined (HAVE_PRSTATUS_T) */
9659
bb0082d6 9660/* Create a pseudosection containing the exact contents of NOTE. */
b34976b6 9661static bfd_boolean
217aa764
AM
9662elfcore_make_note_pseudosection (bfd *abfd,
9663 char *name,
9664 Elf_Internal_Note *note)
252b5132 9665{
936e320b
AM
9666 return _bfd_elfcore_make_pseudosection (abfd, name,
9667 note->descsz, note->descpos);
252b5132
RH
9668}
9669
ff08c6bb
JB
9670/* There isn't a consistent prfpregset_t across platforms,
9671 but it doesn't matter, because we don't have to pick this
c044fabd
KH
9672 data structure apart. */
9673
b34976b6 9674static bfd_boolean
217aa764 9675elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
9676{
9677 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9678}
9679
ff08c6bb 9680/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
971d4640 9681 type of NT_PRXFPREG. Just include the whole note's contents
ff08c6bb 9682 literally. */
c044fabd 9683
b34976b6 9684static bfd_boolean
217aa764 9685elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
9686{
9687 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9688}
9689
4339cae0
L
9690/* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9691 with a note type of NT_X86_XSTATE. Just include the whole note's
9692 contents literally. */
9693
9694static bfd_boolean
9695elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
9696{
9697 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
9698}
9699
97753bd5
AM
9700static bfd_boolean
9701elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
9702{
9703 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
9704}
9705
89eeb0bc
LM
9706static bfd_boolean
9707elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
9708{
9709 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
9710}
97753bd5 9711
cb2366c1
EBM
9712static bfd_boolean
9713elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
9714{
9715 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
9716}
9717
9718static bfd_boolean
9719elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
9720{
9721 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
9722}
9723
9724static bfd_boolean
9725elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
9726{
9727 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
9728}
9729
9730static bfd_boolean
9731elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
9732{
9733 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
9734}
9735
9736static bfd_boolean
9737elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
9738{
9739 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
9740}
9741
9742static bfd_boolean
9743elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
9744{
9745 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
9746}
9747
9748static bfd_boolean
9749elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
9750{
9751 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
9752}
9753
9754static bfd_boolean
9755elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
9756{
9757 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
9758}
9759
9760static bfd_boolean
9761elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
9762{
9763 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
9764}
9765
9766static bfd_boolean
9767elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
9768{
9769 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
9770}
9771
9772static bfd_boolean
9773elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
9774{
9775 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
9776}
9777
9778static bfd_boolean
9779elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
9780{
9781 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
9782}
9783
9784static bfd_boolean
9785elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
9786{
9787 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
9788}
9789
0675e188
UW
9790static bfd_boolean
9791elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
9792{
9793 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
9794}
9795
d7eeb400
MS
9796static bfd_boolean
9797elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
9798{
9799 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
9800}
9801
9802static bfd_boolean
9803elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
9804{
9805 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
9806}
9807
9808static bfd_boolean
9809elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
9810{
9811 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
9812}
9813
9814static bfd_boolean
9815elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
9816{
9817 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
9818}
9819
9820static bfd_boolean
9821elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
9822{
9823 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
9824}
9825
355b81d9
UW
9826static bfd_boolean
9827elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
9828{
9829 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
9830}
9831
9832static bfd_boolean
9833elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
9834{
9835 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
9836}
9837
abb3f6cc
NC
9838static bfd_boolean
9839elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
9840{
9841 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
9842}
9843
4ef9f41a
AA
9844static bfd_boolean
9845elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
9846{
9847 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
9848}
9849
9850static bfd_boolean
9851elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
9852{
9853 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
9854}
9855
88ab90e8
AA
9856static bfd_boolean
9857elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
9858{
9859 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
9860}
9861
9862static bfd_boolean
9863elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
9864{
9865 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
9866}
9867
faa9a424
UW
9868static bfd_boolean
9869elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
9870{
9871 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
9872}
9873
652451f8
YZ
9874static bfd_boolean
9875elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
9876{
9877 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
9878}
9879
9880static bfd_boolean
9881elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
9882{
9883 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
9884}
9885
9886static bfd_boolean
9887elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
9888{
9889 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
9890}
9891
ad1cc4e4
AH
9892static bfd_boolean
9893elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
9894{
9895 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
9896}
9897
e6c3b5bf
AH
9898static bfd_boolean
9899elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
9900{
9901 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
9902}
9903
27456742
AK
9904static bfd_boolean
9905elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
9906{
9907 return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
9908}
9909
252b5132 9910#if defined (HAVE_PRPSINFO_T)
4a938328 9911typedef prpsinfo_t elfcore_psinfo_t;
7ee38065 9912#if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
9913typedef prpsinfo32_t elfcore_psinfo32_t;
9914#endif
252b5132
RH
9915#endif
9916
9917#if defined (HAVE_PSINFO_T)
4a938328 9918typedef psinfo_t elfcore_psinfo_t;
7ee38065 9919#if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
9920typedef psinfo32_t elfcore_psinfo32_t;
9921#endif
252b5132
RH
9922#endif
9923
252b5132
RH
9924/* return a malloc'ed copy of a string at START which is at
9925 most MAX bytes long, possibly without a terminating '\0'.
c044fabd 9926 the copy will always have a terminating '\0'. */
252b5132 9927
936e320b 9928char *
217aa764 9929_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
252b5132 9930{
dc810e39 9931 char *dups;
a50b1753 9932 char *end = (char *) memchr (start, '\0', max);
dc810e39 9933 size_t len;
252b5132
RH
9934
9935 if (end == NULL)
9936 len = max;
9937 else
9938 len = end - start;
9939
a50b1753 9940 dups = (char *) bfd_alloc (abfd, len + 1);
dc810e39 9941 if (dups == NULL)
252b5132
RH
9942 return NULL;
9943
dc810e39
AM
9944 memcpy (dups, start, len);
9945 dups[len] = '\0';
252b5132 9946
dc810e39 9947 return dups;
252b5132
RH
9948}
9949
bb0082d6 9950#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
b34976b6 9951static bfd_boolean
217aa764 9952elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
252b5132 9953{
4a938328
MS
9954 if (note->descsz == sizeof (elfcore_psinfo_t))
9955 {
9956 elfcore_psinfo_t psinfo;
252b5132 9957
7ee38065 9958 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 9959
335e41d4 9960#if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
228e534f 9961 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
335e41d4 9962#endif
228e534f 9963 elf_tdata (abfd)->core->program
936e320b
AM
9964 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9965 sizeof (psinfo.pr_fname));
252b5132 9966
228e534f 9967 elf_tdata (abfd)->core->command
936e320b
AM
9968 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9969 sizeof (psinfo.pr_psargs));
4a938328 9970 }
7ee38065 9971#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
4a938328
MS
9972 else if (note->descsz == sizeof (elfcore_psinfo32_t))
9973 {
9974 /* 64-bit host, 32-bit corefile */
9975 elfcore_psinfo32_t psinfo;
9976
7ee38065 9977 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 9978
335e41d4 9979#if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
228e534f 9980 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
335e41d4 9981#endif
228e534f 9982 elf_tdata (abfd)->core->program
936e320b
AM
9983 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9984 sizeof (psinfo.pr_fname));
4a938328 9985
228e534f 9986 elf_tdata (abfd)->core->command
936e320b
AM
9987 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9988 sizeof (psinfo.pr_psargs));
4a938328
MS
9989 }
9990#endif
9991
9992 else
9993 {
9994 /* Fail - we don't know how to handle any other
9995 note size (ie. data object type). */
b34976b6 9996 return TRUE;
4a938328 9997 }
252b5132
RH
9998
9999 /* Note that for some reason, a spurious space is tacked
10000 onto the end of the args in some (at least one anyway)
c044fabd 10001 implementations, so strip it off if it exists. */
252b5132
RH
10002
10003 {
228e534f 10004 char *command = elf_tdata (abfd)->core->command;
252b5132
RH
10005 int n = strlen (command);
10006
10007 if (0 < n && command[n - 1] == ' ')
10008 command[n - 1] = '\0';
10009 }
10010
b34976b6 10011 return TRUE;
252b5132
RH
10012}
10013#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10014
252b5132 10015#if defined (HAVE_PSTATUS_T)
b34976b6 10016static bfd_boolean
217aa764 10017elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 10018{
f572a39d
AM
10019 if (note->descsz == sizeof (pstatus_t)
10020#if defined (HAVE_PXSTATUS_T)
10021 || note->descsz == sizeof (pxstatus_t)
10022#endif
10023 )
4a938328
MS
10024 {
10025 pstatus_t pstat;
252b5132 10026
4a938328 10027 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 10028
228e534f 10029 elf_tdata (abfd)->core->pid = pstat.pr_pid;
4a938328 10030 }
7ee38065 10031#if defined (HAVE_PSTATUS32_T)
4a938328
MS
10032 else if (note->descsz == sizeof (pstatus32_t))
10033 {
10034 /* 64-bit host, 32-bit corefile */
10035 pstatus32_t pstat;
252b5132 10036
4a938328 10037 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 10038
228e534f 10039 elf_tdata (abfd)->core->pid = pstat.pr_pid;
4a938328
MS
10040 }
10041#endif
252b5132
RH
10042 /* Could grab some more details from the "representative"
10043 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
c044fabd 10044 NT_LWPSTATUS note, presumably. */
252b5132 10045
b34976b6 10046 return TRUE;
252b5132
RH
10047}
10048#endif /* defined (HAVE_PSTATUS_T) */
10049
252b5132 10050#if defined (HAVE_LWPSTATUS_T)
b34976b6 10051static bfd_boolean
217aa764 10052elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132
RH
10053{
10054 lwpstatus_t lwpstat;
10055 char buf[100];
c044fabd 10056 char *name;
d4c88bbb 10057 size_t len;
c044fabd 10058 asection *sect;
252b5132 10059
f572a39d
AM
10060 if (note->descsz != sizeof (lwpstat)
10061#if defined (HAVE_LWPXSTATUS_T)
10062 && note->descsz != sizeof (lwpxstatus_t)
10063#endif
10064 )
b34976b6 10065 return TRUE;
252b5132
RH
10066
10067 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10068
228e534f 10069 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
a1504221
JB
10070 /* Do not overwrite the core signal if it has already been set by
10071 another thread. */
228e534f
AM
10072 if (elf_tdata (abfd)->core->signal == 0)
10073 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
252b5132 10074
c044fabd 10075 /* Make a ".reg/999" section. */
252b5132
RH
10076
10077 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
d4c88bbb 10078 len = strlen (buf) + 1;
217aa764 10079 name = bfd_alloc (abfd, len);
252b5132 10080 if (name == NULL)
b34976b6 10081 return FALSE;
d4c88bbb 10082 memcpy (name, buf, len);
252b5132 10083
117ed4f8 10084 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 10085 if (sect == NULL)
b34976b6 10086 return FALSE;
252b5132
RH
10087
10088#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 10089 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
252b5132
RH
10090 sect->filepos = note->descpos
10091 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10092#endif
10093
10094#if defined (HAVE_LWPSTATUS_T_PR_REG)
eea6121a 10095 sect->size = sizeof (lwpstat.pr_reg);
252b5132
RH
10096 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10097#endif
10098
252b5132
RH
10099 sect->alignment_power = 2;
10100
10101 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
b34976b6 10102 return FALSE;
252b5132
RH
10103
10104 /* Make a ".reg2/999" section */
10105
10106 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
d4c88bbb 10107 len = strlen (buf) + 1;
217aa764 10108 name = bfd_alloc (abfd, len);
252b5132 10109 if (name == NULL)
b34976b6 10110 return FALSE;
d4c88bbb 10111 memcpy (name, buf, len);
252b5132 10112
117ed4f8 10113 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 10114 if (sect == NULL)
b34976b6 10115 return FALSE;
252b5132
RH
10116
10117#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 10118 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
252b5132
RH
10119 sect->filepos = note->descpos
10120 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
10121#endif
10122
10123#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
eea6121a 10124 sect->size = sizeof (lwpstat.pr_fpreg);
252b5132
RH
10125 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
10126#endif
10127
252b5132
RH
10128 sect->alignment_power = 2;
10129
936e320b 10130 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
252b5132
RH
10131}
10132#endif /* defined (HAVE_LWPSTATUS_T) */
10133
b34976b6 10134static bfd_boolean
217aa764 10135elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
16e9c715
NC
10136{
10137 char buf[30];
c044fabd 10138 char *name;
d4c88bbb 10139 size_t len;
c044fabd 10140 asection *sect;
4a6636fb
PA
10141 int type;
10142 int is_active_thread;
10143 bfd_vma base_addr;
16e9c715 10144
4a6636fb 10145 if (note->descsz < 728)
b34976b6 10146 return TRUE;
16e9c715 10147
4a6636fb
PA
10148 if (! CONST_STRNEQ (note->namedata, "win32"))
10149 return TRUE;
10150
10151 type = bfd_get_32 (abfd, note->descdata);
c044fabd 10152
4a6636fb 10153 switch (type)
16e9c715 10154 {
4a6636fb 10155 case 1 /* NOTE_INFO_PROCESS */:
228e534f 10156 /* FIXME: need to add ->core->command. */
4a6636fb 10157 /* process_info.pid */
228e534f 10158 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8);
4a6636fb 10159 /* process_info.signal */
228e534f 10160 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12);
c044fabd 10161 break;
16e9c715 10162
4a6636fb 10163 case 2 /* NOTE_INFO_THREAD */:
16e9c715 10164 /* Make a ".reg/999" section. */
4a6636fb
PA
10165 /* thread_info.tid */
10166 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
c044fabd 10167
d4c88bbb 10168 len = strlen (buf) + 1;
a50b1753 10169 name = (char *) bfd_alloc (abfd, len);
16e9c715 10170 if (name == NULL)
b34976b6 10171 return FALSE;
c044fabd 10172
d4c88bbb 10173 memcpy (name, buf, len);
16e9c715 10174
117ed4f8 10175 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
16e9c715 10176 if (sect == NULL)
b34976b6 10177 return FALSE;
c044fabd 10178
4a6636fb
PA
10179 /* sizeof (thread_info.thread_context) */
10180 sect->size = 716;
10181 /* offsetof (thread_info.thread_context) */
10182 sect->filepos = note->descpos + 12;
16e9c715
NC
10183 sect->alignment_power = 2;
10184
4a6636fb
PA
10185 /* thread_info.is_active_thread */
10186 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
10187
10188 if (is_active_thread)
16e9c715 10189 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
b34976b6 10190 return FALSE;
16e9c715
NC
10191 break;
10192
4a6636fb 10193 case 3 /* NOTE_INFO_MODULE */:
16e9c715 10194 /* Make a ".module/xxxxxxxx" section. */
4a6636fb
PA
10195 /* module_info.base_address */
10196 base_addr = bfd_get_32 (abfd, note->descdata + 4);
0af1713e 10197 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
c044fabd 10198
d4c88bbb 10199 len = strlen (buf) + 1;
a50b1753 10200 name = (char *) bfd_alloc (abfd, len);
16e9c715 10201 if (name == NULL)
b34976b6 10202 return FALSE;
c044fabd 10203
d4c88bbb 10204 memcpy (name, buf, len);
252b5132 10205
117ed4f8 10206 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
c044fabd 10207
16e9c715 10208 if (sect == NULL)
b34976b6 10209 return FALSE;
c044fabd 10210
eea6121a 10211 sect->size = note->descsz;
16e9c715 10212 sect->filepos = note->descpos;
16e9c715
NC
10213 sect->alignment_power = 2;
10214 break;
10215
10216 default:
b34976b6 10217 return TRUE;
16e9c715
NC
10218 }
10219
b34976b6 10220 return TRUE;
16e9c715 10221}
252b5132 10222
b34976b6 10223static bfd_boolean
217aa764 10224elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
252b5132 10225{
9c5bfbb7 10226 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
bb0082d6 10227
252b5132
RH
10228 switch (note->type)
10229 {
10230 default:
b34976b6 10231 return TRUE;
252b5132 10232
252b5132 10233 case NT_PRSTATUS:
bb0082d6
AM
10234 if (bed->elf_backend_grok_prstatus)
10235 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
b34976b6 10236 return TRUE;
bb0082d6 10237#if defined (HAVE_PRSTATUS_T)
252b5132 10238 return elfcore_grok_prstatus (abfd, note);
bb0082d6 10239#else
b34976b6 10240 return TRUE;
252b5132
RH
10241#endif
10242
10243#if defined (HAVE_PSTATUS_T)
10244 case NT_PSTATUS:
10245 return elfcore_grok_pstatus (abfd, note);
10246#endif
10247
10248#if defined (HAVE_LWPSTATUS_T)
10249 case NT_LWPSTATUS:
10250 return elfcore_grok_lwpstatus (abfd, note);
10251#endif
10252
10253 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
10254 return elfcore_grok_prfpreg (abfd, note);
10255
c044fabd 10256 case NT_WIN32PSTATUS:
16e9c715 10257 return elfcore_grok_win32pstatus (abfd, note);
16e9c715 10258
c044fabd 10259 case NT_PRXFPREG: /* Linux SSE extension */
e377ab71
MK
10260 if (note->namesz == 6
10261 && strcmp (note->namedata, "LINUX") == 0)
ff08c6bb
JB
10262 return elfcore_grok_prxfpreg (abfd, note);
10263 else
b34976b6 10264 return TRUE;
ff08c6bb 10265
4339cae0
L
10266 case NT_X86_XSTATE: /* Linux XSAVE extension */
10267 if (note->namesz == 6
10268 && strcmp (note->namedata, "LINUX") == 0)
10269 return elfcore_grok_xstatereg (abfd, note);
10270 else
10271 return TRUE;
10272
97753bd5
AM
10273 case NT_PPC_VMX:
10274 if (note->namesz == 6
10275 && strcmp (note->namedata, "LINUX") == 0)
10276 return elfcore_grok_ppc_vmx (abfd, note);
10277 else
10278 return TRUE;
10279
89eeb0bc
LM
10280 case NT_PPC_VSX:
10281 if (note->namesz == 6
07d6d2b8
AM
10282 && strcmp (note->namedata, "LINUX") == 0)
10283 return elfcore_grok_ppc_vsx (abfd, note);
89eeb0bc 10284 else
07d6d2b8 10285 return TRUE;
89eeb0bc 10286
cb2366c1
EBM
10287 case NT_PPC_TAR:
10288 if (note->namesz == 6
4b24dd1a
AM
10289 && strcmp (note->namedata, "LINUX") == 0)
10290 return elfcore_grok_ppc_tar (abfd, note);
cb2366c1 10291 else
4b24dd1a 10292 return TRUE;
cb2366c1
EBM
10293
10294 case NT_PPC_PPR:
10295 if (note->namesz == 6
4b24dd1a
AM
10296 && strcmp (note->namedata, "LINUX") == 0)
10297 return elfcore_grok_ppc_ppr (abfd, note);
cb2366c1 10298 else
4b24dd1a 10299 return TRUE;
cb2366c1
EBM
10300
10301 case NT_PPC_DSCR:
10302 if (note->namesz == 6
4b24dd1a
AM
10303 && strcmp (note->namedata, "LINUX") == 0)
10304 return elfcore_grok_ppc_dscr (abfd, note);
cb2366c1 10305 else
4b24dd1a 10306 return TRUE;
cb2366c1
EBM
10307
10308 case NT_PPC_EBB:
10309 if (note->namesz == 6
4b24dd1a
AM
10310 && strcmp (note->namedata, "LINUX") == 0)
10311 return elfcore_grok_ppc_ebb (abfd, note);
cb2366c1 10312 else
4b24dd1a 10313 return TRUE;
cb2366c1
EBM
10314
10315 case NT_PPC_PMU:
10316 if (note->namesz == 6
4b24dd1a
AM
10317 && strcmp (note->namedata, "LINUX") == 0)
10318 return elfcore_grok_ppc_pmu (abfd, note);
cb2366c1 10319 else
4b24dd1a 10320 return TRUE;
cb2366c1
EBM
10321
10322 case NT_PPC_TM_CGPR:
10323 if (note->namesz == 6
4b24dd1a
AM
10324 && strcmp (note->namedata, "LINUX") == 0)
10325 return elfcore_grok_ppc_tm_cgpr (abfd, note);
cb2366c1 10326 else
4b24dd1a 10327 return TRUE;
cb2366c1
EBM
10328
10329 case NT_PPC_TM_CFPR:
10330 if (note->namesz == 6
4b24dd1a
AM
10331 && strcmp (note->namedata, "LINUX") == 0)
10332 return elfcore_grok_ppc_tm_cfpr (abfd, note);
cb2366c1 10333 else
4b24dd1a 10334 return TRUE;
cb2366c1
EBM
10335
10336 case NT_PPC_TM_CVMX:
10337 if (note->namesz == 6
4b24dd1a
AM
10338 && strcmp (note->namedata, "LINUX") == 0)
10339 return elfcore_grok_ppc_tm_cvmx (abfd, note);
cb2366c1 10340 else
4b24dd1a 10341 return TRUE;
cb2366c1
EBM
10342
10343 case NT_PPC_TM_CVSX:
10344 if (note->namesz == 6
4b24dd1a
AM
10345 && strcmp (note->namedata, "LINUX") == 0)
10346 return elfcore_grok_ppc_tm_cvsx (abfd, note);
cb2366c1 10347 else
4b24dd1a 10348 return TRUE;
cb2366c1
EBM
10349
10350 case NT_PPC_TM_SPR:
10351 if (note->namesz == 6
4b24dd1a
AM
10352 && strcmp (note->namedata, "LINUX") == 0)
10353 return elfcore_grok_ppc_tm_spr (abfd, note);
cb2366c1 10354 else
4b24dd1a 10355 return TRUE;
cb2366c1
EBM
10356
10357 case NT_PPC_TM_CTAR:
10358 if (note->namesz == 6
4b24dd1a
AM
10359 && strcmp (note->namedata, "LINUX") == 0)
10360 return elfcore_grok_ppc_tm_ctar (abfd, note);
cb2366c1 10361 else
4b24dd1a 10362 return TRUE;
cb2366c1
EBM
10363
10364 case NT_PPC_TM_CPPR:
10365 if (note->namesz == 6
4b24dd1a
AM
10366 && strcmp (note->namedata, "LINUX") == 0)
10367 return elfcore_grok_ppc_tm_cppr (abfd, note);
cb2366c1 10368 else
4b24dd1a 10369 return TRUE;
cb2366c1
EBM
10370
10371 case NT_PPC_TM_CDSCR:
10372 if (note->namesz == 6
4b24dd1a
AM
10373 && strcmp (note->namedata, "LINUX") == 0)
10374 return elfcore_grok_ppc_tm_cdscr (abfd, note);
cb2366c1 10375 else
4b24dd1a 10376 return TRUE;
cb2366c1 10377
0675e188
UW
10378 case NT_S390_HIGH_GPRS:
10379 if (note->namesz == 6
07d6d2b8
AM
10380 && strcmp (note->namedata, "LINUX") == 0)
10381 return elfcore_grok_s390_high_gprs (abfd, note);
0675e188 10382 else
07d6d2b8 10383 return TRUE;
0675e188 10384
d7eeb400
MS
10385 case NT_S390_TIMER:
10386 if (note->namesz == 6
07d6d2b8
AM
10387 && strcmp (note->namedata, "LINUX") == 0)
10388 return elfcore_grok_s390_timer (abfd, note);
d7eeb400 10389 else
07d6d2b8 10390 return TRUE;
d7eeb400
MS
10391
10392 case NT_S390_TODCMP:
10393 if (note->namesz == 6
07d6d2b8
AM
10394 && strcmp (note->namedata, "LINUX") == 0)
10395 return elfcore_grok_s390_todcmp (abfd, note);
d7eeb400 10396 else
07d6d2b8 10397 return TRUE;
d7eeb400
MS
10398
10399 case NT_S390_TODPREG:
10400 if (note->namesz == 6
07d6d2b8
AM
10401 && strcmp (note->namedata, "LINUX") == 0)
10402 return elfcore_grok_s390_todpreg (abfd, note);
d7eeb400 10403 else
07d6d2b8 10404 return TRUE;
d7eeb400
MS
10405
10406 case NT_S390_CTRS:
10407 if (note->namesz == 6
07d6d2b8
AM
10408 && strcmp (note->namedata, "LINUX") == 0)
10409 return elfcore_grok_s390_ctrs (abfd, note);
d7eeb400 10410 else
07d6d2b8 10411 return TRUE;
d7eeb400
MS
10412
10413 case NT_S390_PREFIX:
10414 if (note->namesz == 6
07d6d2b8
AM
10415 && strcmp (note->namedata, "LINUX") == 0)
10416 return elfcore_grok_s390_prefix (abfd, note);
d7eeb400 10417 else
07d6d2b8 10418 return TRUE;
d7eeb400 10419
355b81d9
UW
10420 case NT_S390_LAST_BREAK:
10421 if (note->namesz == 6
07d6d2b8
AM
10422 && strcmp (note->namedata, "LINUX") == 0)
10423 return elfcore_grok_s390_last_break (abfd, note);
355b81d9 10424 else
07d6d2b8 10425 return TRUE;
355b81d9
UW
10426
10427 case NT_S390_SYSTEM_CALL:
10428 if (note->namesz == 6
07d6d2b8
AM
10429 && strcmp (note->namedata, "LINUX") == 0)
10430 return elfcore_grok_s390_system_call (abfd, note);
355b81d9 10431 else
07d6d2b8 10432 return TRUE;
355b81d9 10433
abb3f6cc
NC
10434 case NT_S390_TDB:
10435 if (note->namesz == 6
07d6d2b8
AM
10436 && strcmp (note->namedata, "LINUX") == 0)
10437 return elfcore_grok_s390_tdb (abfd, note);
abb3f6cc 10438 else
07d6d2b8 10439 return TRUE;
abb3f6cc 10440
4ef9f41a
AA
10441 case NT_S390_VXRS_LOW:
10442 if (note->namesz == 6
10443 && strcmp (note->namedata, "LINUX") == 0)
10444 return elfcore_grok_s390_vxrs_low (abfd, note);
10445 else
10446 return TRUE;
10447
10448 case NT_S390_VXRS_HIGH:
10449 if (note->namesz == 6
10450 && strcmp (note->namedata, "LINUX") == 0)
10451 return elfcore_grok_s390_vxrs_high (abfd, note);
10452 else
10453 return TRUE;
10454
88ab90e8
AA
10455 case NT_S390_GS_CB:
10456 if (note->namesz == 6
10457 && strcmp (note->namedata, "LINUX") == 0)
8fe09d74 10458 return elfcore_grok_s390_gs_cb (abfd, note);
88ab90e8
AA
10459 else
10460 return TRUE;
10461
10462 case NT_S390_GS_BC:
10463 if (note->namesz == 6
10464 && strcmp (note->namedata, "LINUX") == 0)
8fe09d74 10465 return elfcore_grok_s390_gs_bc (abfd, note);
88ab90e8
AA
10466 else
10467 return TRUE;
10468
27456742
AK
10469 case NT_ARC_V2:
10470 if (note->namesz == 6
10471 && strcmp (note->namedata, "LINUX") == 0)
10472 return elfcore_grok_arc_v2 (abfd, note);
10473 else
10474 return TRUE;
10475
faa9a424
UW
10476 case NT_ARM_VFP:
10477 if (note->namesz == 6
10478 && strcmp (note->namedata, "LINUX") == 0)
10479 return elfcore_grok_arm_vfp (abfd, note);
10480 else
10481 return TRUE;
10482
652451f8
YZ
10483 case NT_ARM_TLS:
10484 if (note->namesz == 6
10485 && strcmp (note->namedata, "LINUX") == 0)
10486 return elfcore_grok_aarch_tls (abfd, note);
10487 else
10488 return TRUE;
10489
10490 case NT_ARM_HW_BREAK:
10491 if (note->namesz == 6
10492 && strcmp (note->namedata, "LINUX") == 0)
10493 return elfcore_grok_aarch_hw_break (abfd, note);
10494 else
10495 return TRUE;
10496
10497 case NT_ARM_HW_WATCH:
10498 if (note->namesz == 6
10499 && strcmp (note->namedata, "LINUX") == 0)
10500 return elfcore_grok_aarch_hw_watch (abfd, note);
10501 else
10502 return TRUE;
10503
ad1cc4e4
AH
10504 case NT_ARM_SVE:
10505 if (note->namesz == 6
10506 && strcmp (note->namedata, "LINUX") == 0)
10507 return elfcore_grok_aarch_sve (abfd, note);
10508 else
10509 return TRUE;
10510
e6c3b5bf
AH
10511 case NT_ARM_PAC_MASK:
10512 if (note->namesz == 6
10513 && strcmp (note->namedata, "LINUX") == 0)
10514 return elfcore_grok_aarch_pauth (abfd, note);
10515 else
10516 return TRUE;
10517
252b5132
RH
10518 case NT_PRPSINFO:
10519 case NT_PSINFO:
bb0082d6
AM
10520 if (bed->elf_backend_grok_psinfo)
10521 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
b34976b6 10522 return TRUE;
bb0082d6 10523#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
252b5132 10524 return elfcore_grok_psinfo (abfd, note);
bb0082d6 10525#else
b34976b6 10526 return TRUE;
252b5132 10527#endif
3333a7c3
RM
10528
10529 case NT_AUXV:
58e07198 10530 return elfcore_make_auxv_note_section (abfd, note, 0);
9015683b 10531
451b7c33
TT
10532 case NT_FILE:
10533 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
10534 note);
10535
9015683b
TT
10536 case NT_SIGINFO:
10537 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
10538 note);
5b2c414d 10539
252b5132
RH
10540 }
10541}
10542
718175fa
JK
10543static bfd_boolean
10544elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
10545{
c74f7d1c 10546 struct bfd_build_id* build_id;
30e8ee25
AM
10547
10548 if (note->descsz == 0)
10549 return FALSE;
10550
c74f7d1c
JT
10551 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
10552 if (build_id == NULL)
718175fa
JK
10553 return FALSE;
10554
c74f7d1c
JT
10555 build_id->size = note->descsz;
10556 memcpy (build_id->data, note->descdata, note->descsz);
10557 abfd->build_id = build_id;
718175fa
JK
10558
10559 return TRUE;
10560}
10561
10562static bfd_boolean
10563elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
10564{
10565 switch (note->type)
10566 {
10567 default:
10568 return TRUE;
10569
46bed679
L
10570 case NT_GNU_PROPERTY_TYPE_0:
10571 return _bfd_elf_parse_gnu_properties (abfd, note);
10572
718175fa
JK
10573 case NT_GNU_BUILD_ID:
10574 return elfobj_grok_gnu_build_id (abfd, note);
10575 }
10576}
10577
e21e5835
NC
10578static bfd_boolean
10579elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
10580{
10581 struct sdt_note *cur =
7a6e0d89
AM
10582 (struct sdt_note *) bfd_alloc (abfd,
10583 sizeof (struct sdt_note) + note->descsz);
e21e5835
NC
10584
10585 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
10586 cur->size = (bfd_size_type) note->descsz;
10587 memcpy (cur->data, note->descdata, note->descsz);
10588
10589 elf_tdata (abfd)->sdt_note_head = cur;
10590
10591 return TRUE;
10592}
10593
10594static bfd_boolean
10595elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
10596{
10597 switch (note->type)
10598 {
10599 case NT_STAPSDT:
10600 return elfobj_grok_stapsdt_note_1 (abfd, note);
10601
10602 default:
10603 return TRUE;
10604 }
10605}
10606
aa1ed4a9
JB
10607static bfd_boolean
10608elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
10609{
10610 size_t offset;
10611
b5430a3c 10612 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
aa1ed4a9 10613 {
b5430a3c 10614 case ELFCLASS32:
0064d223
JB
10615 if (note->descsz < 108)
10616 return FALSE;
aa1ed4a9
JB
10617 break;
10618
b5430a3c 10619 case ELFCLASS64:
0064d223
JB
10620 if (note->descsz < 120)
10621 return FALSE;
aa1ed4a9
JB
10622 break;
10623
10624 default:
10625 return FALSE;
10626 }
10627
0064d223
JB
10628 /* Check for version 1 in pr_version. */
10629 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10630 return FALSE;
80a04378 10631
0064d223
JB
10632 offset = 4;
10633
10634 /* Skip over pr_psinfosz. */
b5430a3c 10635 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
0064d223
JB
10636 offset += 4;
10637 else
10638 {
10639 offset += 4; /* Padding before pr_psinfosz. */
10640 offset += 8;
10641 }
10642
aa1ed4a9
JB
10643 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10644 elf_tdata (abfd)->core->program
10645 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
10646 offset += 17;
10647
10648 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10649 elf_tdata (abfd)->core->command
10650 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
0064d223
JB
10651 offset += 81;
10652
10653 /* Padding before pr_pid. */
10654 offset += 2;
10655
10656 /* The pr_pid field was added in version "1a". */
10657 if (note->descsz < offset + 4)
10658 return TRUE;
10659
10660 elf_tdata (abfd)->core->pid
10661 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
aa1ed4a9
JB
10662
10663 return TRUE;
10664}
10665
10666static bfd_boolean
10667elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
10668{
10669 size_t offset;
10670 size_t size;
24d3e51b 10671 size_t min_size;
aa1ed4a9 10672
24d3e51b
NC
10673 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10674 Also compute minimum size of this note. */
b5430a3c 10675 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
aa1ed4a9 10676 {
b5430a3c 10677 case ELFCLASS32:
24d3e51b
NC
10678 offset = 4 + 4;
10679 min_size = offset + (4 * 2) + 4 + 4 + 4;
aa1ed4a9
JB
10680 break;
10681
b5430a3c 10682 case ELFCLASS64:
24d3e51b
NC
10683 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
10684 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
aa1ed4a9
JB
10685 break;
10686
10687 default:
10688 return FALSE;
10689 }
10690
24d3e51b
NC
10691 if (note->descsz < min_size)
10692 return FALSE;
10693
10694 /* Check for version 1 in pr_version. */
10695 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10696 return FALSE;
aa1ed4a9 10697
24d3e51b
NC
10698 /* Extract size of pr_reg from pr_gregsetsz. */
10699 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
b5430a3c 10700 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
24d3e51b
NC
10701 {
10702 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10703 offset += 4 * 2;
10704 }
b5430a3c 10705 else
24d3e51b
NC
10706 {
10707 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
10708 offset += 8 * 2;
10709 }
aa1ed4a9 10710
24d3e51b 10711 /* Skip over pr_osreldate. */
aa1ed4a9
JB
10712 offset += 4;
10713
24d3e51b 10714 /* Read signal from pr_cursig. */
aa1ed4a9
JB
10715 if (elf_tdata (abfd)->core->signal == 0)
10716 elf_tdata (abfd)->core->signal
10717 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10718 offset += 4;
10719
24d3e51b 10720 /* Read TID from pr_pid. */
aa1ed4a9
JB
10721 elf_tdata (abfd)->core->lwpid
10722 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10723 offset += 4;
10724
24d3e51b 10725 /* Padding before pr_reg. */
b5430a3c 10726 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
aa1ed4a9
JB
10727 offset += 4;
10728
24d3e51b
NC
10729 /* Make sure that there is enough data remaining in the note. */
10730 if ((note->descsz - offset) < size)
10731 return FALSE;
10732
aa1ed4a9
JB
10733 /* Make a ".reg/999" section and a ".reg" section. */
10734 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10735 size, note->descpos + offset);
10736}
10737
10738static bfd_boolean
10739elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
10740{
544c67cd
JB
10741 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10742
aa1ed4a9
JB
10743 switch (note->type)
10744 {
10745 case NT_PRSTATUS:
544c67cd
JB
10746 if (bed->elf_backend_grok_freebsd_prstatus)
10747 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
10748 return TRUE;
aa1ed4a9
JB
10749 return elfcore_grok_freebsd_prstatus (abfd, note);
10750
10751 case NT_FPREGSET:
10752 return elfcore_grok_prfpreg (abfd, note);
10753
10754 case NT_PRPSINFO:
10755 return elfcore_grok_freebsd_psinfo (abfd, note);
10756
10757 case NT_FREEBSD_THRMISC:
10758 if (note->namesz == 8)
10759 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
10760 else
10761 return TRUE;
10762
ddb2bbcf
JB
10763 case NT_FREEBSD_PROCSTAT_PROC:
10764 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
10765 note);
10766
10767 case NT_FREEBSD_PROCSTAT_FILES:
10768 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
10769 note);
10770
10771 case NT_FREEBSD_PROCSTAT_VMMAP:
10772 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
10773 note);
10774
3350c5f5 10775 case NT_FREEBSD_PROCSTAT_AUXV:
58e07198 10776 return elfcore_make_auxv_note_section (abfd, note, 4);
3350c5f5 10777
aa1ed4a9
JB
10778 case NT_X86_XSTATE:
10779 if (note->namesz == 8)
10780 return elfcore_grok_xstatereg (abfd, note);
10781 else
10782 return TRUE;
10783
e6f3b9c3
JB
10784 case NT_FREEBSD_PTLWPINFO:
10785 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
10786 note);
10787
6d5be5d6
JB
10788 case NT_ARM_VFP:
10789 return elfcore_grok_arm_vfp (abfd, note);
10790
aa1ed4a9
JB
10791 default:
10792 return TRUE;
10793 }
10794}
10795
b34976b6 10796static bfd_boolean
217aa764 10797elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
50b2bdb7
AM
10798{
10799 char *cp;
10800
10801 cp = strchr (note->namedata, '@');
10802 if (cp != NULL)
10803 {
d2b64500 10804 *lwpidp = atoi(cp + 1);
b34976b6 10805 return TRUE;
50b2bdb7 10806 }
b34976b6 10807 return FALSE;
50b2bdb7
AM
10808}
10809
b34976b6 10810static bfd_boolean
217aa764 10811elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7 10812{
80a04378
NC
10813 if (note->descsz <= 0x7c + 31)
10814 return FALSE;
10815
50b2bdb7 10816 /* Signal number at offset 0x08. */
228e534f 10817 elf_tdata (abfd)->core->signal
50b2bdb7
AM
10818 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10819
10820 /* Process ID at offset 0x50. */
228e534f 10821 elf_tdata (abfd)->core->pid
50b2bdb7
AM
10822 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
10823
10824 /* Command name at 0x7c (max 32 bytes, including nul). */
228e534f 10825 elf_tdata (abfd)->core->command
50b2bdb7
AM
10826 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
10827
7720ba9f
MK
10828 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
10829 note);
50b2bdb7
AM
10830}
10831
b34976b6 10832static bfd_boolean
217aa764 10833elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7
AM
10834{
10835 int lwp;
10836
10837 if (elfcore_netbsd_get_lwpid (note, &lwp))
228e534f 10838 elf_tdata (abfd)->core->lwpid = lwp;
50b2bdb7 10839
58e07198 10840 switch (note->type)
50b2bdb7 10841 {
58e07198 10842 case NT_NETBSDCORE_PROCINFO:
50b2bdb7 10843 /* NetBSD-specific core "procinfo". Note that we expect to
08a40648
AM
10844 find this note before any of the others, which is fine,
10845 since the kernel writes this note out first when it
10846 creates a core file. */
50b2bdb7 10847 return elfcore_grok_netbsd_procinfo (abfd, note);
58e07198
CZ
10848#ifdef NT_NETBSDCORE_AUXV
10849 case NT_NETBSDCORE_AUXV:
10850 /* NetBSD-specific Elf Auxiliary Vector data. */
10851 return elfcore_make_auxv_note_section (abfd, note, 4);
06d949ec
KR
10852#endif
10853#ifdef NT_NETBSDCORE_LWPSTATUS
10854 case NT_NETBSDCORE_LWPSTATUS:
10855 return elfcore_make_note_pseudosection (abfd,
10856 ".note.netbsdcore.lwpstatus",
10857 note);
58e07198
CZ
10858#endif
10859 default:
10860 break;
50b2bdb7
AM
10861 }
10862
06d949ec 10863 /* As of March 2020 there are no other machine-independent notes
b4db1224
JT
10864 defined for NetBSD core files. If the note type is less
10865 than the start of the machine-dependent note types, we don't
10866 understand it. */
47d9a591 10867
b4db1224 10868 if (note->type < NT_NETBSDCORE_FIRSTMACH)
b34976b6 10869 return TRUE;
50b2bdb7
AM
10870
10871
10872 switch (bfd_get_arch (abfd))
10873 {
08a40648
AM
10874 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10875 PT_GETFPREGS == mach+2. */
50b2bdb7 10876
015ec493 10877 case bfd_arch_aarch64:
50b2bdb7
AM
10878 case bfd_arch_alpha:
10879 case bfd_arch_sparc:
10880 switch (note->type)
08a40648
AM
10881 {
10882 case NT_NETBSDCORE_FIRSTMACH+0:
10883 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 10884
08a40648
AM
10885 case NT_NETBSDCORE_FIRSTMACH+2:
10886 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 10887
08a40648
AM
10888 default:
10889 return TRUE;
10890 }
50b2bdb7 10891
58e07198
CZ
10892 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
10893 There's also old PT___GETREGS40 == mach + 1 for old reg
10894 structure which lacks GBR. */
10895
10896 case bfd_arch_sh:
10897 switch (note->type)
10898 {
10899 case NT_NETBSDCORE_FIRSTMACH+3:
10900 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10901
10902 case NT_NETBSDCORE_FIRSTMACH+5:
10903 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10904
10905 default:
10906 return TRUE;
10907 }
10908
08a40648
AM
10909 /* On all other arch's, PT_GETREGS == mach+1 and
10910 PT_GETFPREGS == mach+3. */
50b2bdb7
AM
10911
10912 default:
10913 switch (note->type)
08a40648
AM
10914 {
10915 case NT_NETBSDCORE_FIRSTMACH+1:
10916 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 10917
08a40648
AM
10918 case NT_NETBSDCORE_FIRSTMACH+3:
10919 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 10920
08a40648
AM
10921 default:
10922 return TRUE;
10923 }
50b2bdb7
AM
10924 }
10925 /* NOTREACHED */
10926}
10927
67cc5033
MK
10928static bfd_boolean
10929elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
10930{
80a04378
NC
10931 if (note->descsz <= 0x48 + 31)
10932 return FALSE;
10933
67cc5033 10934 /* Signal number at offset 0x08. */
228e534f 10935 elf_tdata (abfd)->core->signal
67cc5033
MK
10936 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10937
10938 /* Process ID at offset 0x20. */
228e534f 10939 elf_tdata (abfd)->core->pid
67cc5033
MK
10940 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
10941
10942 /* Command name at 0x48 (max 32 bytes, including nul). */
228e534f 10943 elf_tdata (abfd)->core->command
67cc5033
MK
10944 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
10945
10946 return TRUE;
10947}
10948
10949static bfd_boolean
10950elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
10951{
10952 if (note->type == NT_OPENBSD_PROCINFO)
10953 return elfcore_grok_openbsd_procinfo (abfd, note);
10954
10955 if (note->type == NT_OPENBSD_REGS)
10956 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10957
10958 if (note->type == NT_OPENBSD_FPREGS)
10959 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10960
10961 if (note->type == NT_OPENBSD_XFPREGS)
10962 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
10963
10964 if (note->type == NT_OPENBSD_AUXV)
58e07198 10965 return elfcore_make_auxv_note_section (abfd, note, 0);
67cc5033
MK
10966
10967 if (note->type == NT_OPENBSD_WCOOKIE)
10968 {
10969 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
10970 SEC_HAS_CONTENTS);
10971
10972 if (sect == NULL)
10973 return FALSE;
10974 sect->size = note->descsz;
10975 sect->filepos = note->descpos;
10976 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10977
10978 return TRUE;
10979 }
10980
10981 return TRUE;
10982}
10983
07c6e936 10984static bfd_boolean
d3fd4074 10985elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
07c6e936
NC
10986{
10987 void *ddata = note->descdata;
10988 char buf[100];
10989 char *name;
10990 asection *sect;
f8843e87
AM
10991 short sig;
10992 unsigned flags;
07c6e936 10993
80a04378
NC
10994 if (note->descsz < 16)
10995 return FALSE;
10996
07c6e936 10997 /* nto_procfs_status 'pid' field is at offset 0. */
228e534f 10998 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
07c6e936 10999
f8843e87
AM
11000 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
11001 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
11002
11003 /* nto_procfs_status 'flags' field is at offset 8. */
11004 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
07c6e936
NC
11005
11006 /* nto_procfs_status 'what' field is at offset 14. */
f8843e87
AM
11007 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
11008 {
228e534f
AM
11009 elf_tdata (abfd)->core->signal = sig;
11010 elf_tdata (abfd)->core->lwpid = *tid;
f8843e87 11011 }
07c6e936 11012
f8843e87
AM
11013 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
11014 do not come from signals so we make sure we set the current
11015 thread just in case. */
11016 if (flags & 0x00000080)
228e534f 11017 elf_tdata (abfd)->core->lwpid = *tid;
07c6e936
NC
11018
11019 /* Make a ".qnx_core_status/%d" section. */
d3fd4074 11020 sprintf (buf, ".qnx_core_status/%ld", *tid);
07c6e936 11021
a50b1753 11022 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
07c6e936
NC
11023 if (name == NULL)
11024 return FALSE;
11025 strcpy (name, buf);
11026
117ed4f8 11027 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936
NC
11028 if (sect == NULL)
11029 return FALSE;
11030
07d6d2b8
AM
11031 sect->size = note->descsz;
11032 sect->filepos = note->descpos;
07c6e936
NC
11033 sect->alignment_power = 2;
11034
11035 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
11036}
11037
11038static bfd_boolean
d69f560c
KW
11039elfcore_grok_nto_regs (bfd *abfd,
11040 Elf_Internal_Note *note,
d3fd4074 11041 long tid,
d69f560c 11042 char *base)
07c6e936
NC
11043{
11044 char buf[100];
11045 char *name;
11046 asection *sect;
11047
d69f560c 11048 /* Make a "(base)/%d" section. */
d3fd4074 11049 sprintf (buf, "%s/%ld", base, tid);
07c6e936 11050
a50b1753 11051 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
07c6e936
NC
11052 if (name == NULL)
11053 return FALSE;
11054 strcpy (name, buf);
11055
117ed4f8 11056 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936
NC
11057 if (sect == NULL)
11058 return FALSE;
11059
07d6d2b8
AM
11060 sect->size = note->descsz;
11061 sect->filepos = note->descpos;
07c6e936
NC
11062 sect->alignment_power = 2;
11063
f8843e87 11064 /* This is the current thread. */
228e534f 11065 if (elf_tdata (abfd)->core->lwpid == tid)
d69f560c 11066 return elfcore_maybe_make_sect (abfd, base, sect);
f8843e87
AM
11067
11068 return TRUE;
07c6e936
NC
11069}
11070
11071#define BFD_QNT_CORE_INFO 7
11072#define BFD_QNT_CORE_STATUS 8
11073#define BFD_QNT_CORE_GREG 9
11074#define BFD_QNT_CORE_FPREG 10
11075
11076static bfd_boolean
217aa764 11077elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
07c6e936
NC
11078{
11079 /* Every GREG section has a STATUS section before it. Store the
811072d8 11080 tid from the previous call to pass down to the next gregs
07c6e936 11081 function. */
d3fd4074 11082 static long tid = 1;
07c6e936
NC
11083
11084 switch (note->type)
11085 {
d69f560c
KW
11086 case BFD_QNT_CORE_INFO:
11087 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
11088 case BFD_QNT_CORE_STATUS:
11089 return elfcore_grok_nto_status (abfd, note, &tid);
11090 case BFD_QNT_CORE_GREG:
11091 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
11092 case BFD_QNT_CORE_FPREG:
11093 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
11094 default:
11095 return TRUE;
07c6e936
NC
11096 }
11097}
11098
b15fa79e
AM
11099static bfd_boolean
11100elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
11101{
11102 char *name;
11103 asection *sect;
11104 size_t len;
11105
11106 /* Use note name as section name. */
11107 len = note->namesz;
a50b1753 11108 name = (char *) bfd_alloc (abfd, len);
b15fa79e
AM
11109 if (name == NULL)
11110 return FALSE;
11111 memcpy (name, note->namedata, len);
11112 name[len - 1] = '\0';
11113
11114 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11115 if (sect == NULL)
11116 return FALSE;
11117
07d6d2b8
AM
11118 sect->size = note->descsz;
11119 sect->filepos = note->descpos;
b15fa79e
AM
11120 sect->alignment_power = 1;
11121
11122 return TRUE;
11123}
11124
7c76fa91
MS
11125/* Function: elfcore_write_note
11126
47d9a591 11127 Inputs:
a39f3346 11128 buffer to hold note, and current size of buffer
7c76fa91
MS
11129 name of note
11130 type of note
11131 data for note
11132 size of data for note
11133
a39f3346
AM
11134 Writes note to end of buffer. ELF64 notes are written exactly as
11135 for ELF32, despite the current (as of 2006) ELF gabi specifying
11136 that they ought to have 8-byte namesz and descsz field, and have
11137 8-byte alignment. Other writers, eg. Linux kernel, do the same.
11138
7c76fa91 11139 Return:
a39f3346 11140 Pointer to realloc'd buffer, *BUFSIZ updated. */
7c76fa91
MS
11141
11142char *
a39f3346 11143elfcore_write_note (bfd *abfd,
217aa764 11144 char *buf,
a39f3346 11145 int *bufsiz,
217aa764 11146 const char *name,
a39f3346 11147 int type,
217aa764 11148 const void *input,
a39f3346 11149 int size)
7c76fa91
MS
11150{
11151 Elf_External_Note *xnp;
d4c88bbb 11152 size_t namesz;
d4c88bbb 11153 size_t newspace;
a39f3346 11154 char *dest;
7c76fa91 11155
d4c88bbb 11156 namesz = 0;
d4c88bbb 11157 if (name != NULL)
a39f3346 11158 namesz = strlen (name) + 1;
d4c88bbb 11159
a39f3346 11160 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
d4c88bbb 11161
a50b1753 11162 buf = (char *) realloc (buf, *bufsiz + newspace);
14b1c01e
AM
11163 if (buf == NULL)
11164 return buf;
a39f3346 11165 dest = buf + *bufsiz;
7c76fa91
MS
11166 *bufsiz += newspace;
11167 xnp = (Elf_External_Note *) dest;
11168 H_PUT_32 (abfd, namesz, xnp->namesz);
11169 H_PUT_32 (abfd, size, xnp->descsz);
11170 H_PUT_32 (abfd, type, xnp->type);
d4c88bbb
AM
11171 dest = xnp->name;
11172 if (name != NULL)
11173 {
11174 memcpy (dest, name, namesz);
11175 dest += namesz;
a39f3346 11176 while (namesz & 3)
d4c88bbb
AM
11177 {
11178 *dest++ = '\0';
a39f3346 11179 ++namesz;
d4c88bbb
AM
11180 }
11181 }
11182 memcpy (dest, input, size);
a39f3346
AM
11183 dest += size;
11184 while (size & 3)
11185 {
11186 *dest++ = '\0';
11187 ++size;
11188 }
11189 return buf;
7c76fa91
MS
11190}
11191
602f1657
AM
11192/* gcc-8 warns (*) on all the strncpy calls in this function about
11193 possible string truncation. The "truncation" is not a bug. We
11194 have an external representation of structs with fields that are not
11195 necessarily NULL terminated and corresponding internal
11196 representation fields that are one larger so that they can always
11197 be NULL terminated.
11198 gcc versions between 4.2 and 4.6 do not allow pragma control of
11199 diagnostics inside functions, giving a hard error if you try to use
11200 the finer control available with later versions.
11201 gcc prior to 4.2 warns about diagnostic push and pop.
11202 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
11203 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
11204 (*) Depending on your system header files! */
d99b4b92 11205#if GCC_VERSION >= 8000
602f1657
AM
11206# pragma GCC diagnostic push
11207# pragma GCC diagnostic ignored "-Wstringop-truncation"
d99b4b92 11208#endif
7c76fa91 11209char *
217aa764
AM
11210elfcore_write_prpsinfo (bfd *abfd,
11211 char *buf,
11212 int *bufsiz,
11213 const char *fname,
11214 const char *psargs)
7c76fa91 11215{
183e98be
AM
11216 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11217
11218 if (bed->elf_backend_write_core_note != NULL)
11219 {
11220 char *ret;
11221 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11222 NT_PRPSINFO, fname, psargs);
11223 if (ret != NULL)
11224 return ret;
11225 }
7c76fa91 11226
1f20dca5 11227#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
602f1657 11228# if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
183e98be
AM
11229 if (bed->s->elfclass == ELFCLASS32)
11230 {
602f1657 11231# if defined (HAVE_PSINFO32_T)
183e98be
AM
11232 psinfo32_t data;
11233 int note_type = NT_PSINFO;
602f1657 11234# else
183e98be
AM
11235 prpsinfo32_t data;
11236 int note_type = NT_PRPSINFO;
602f1657 11237# endif
183e98be
AM
11238
11239 memset (&data, 0, sizeof (data));
11240 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11241 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11242 return elfcore_write_note (abfd, buf, bufsiz,
1f20dca5 11243 "CORE", note_type, &data, sizeof (data));
183e98be
AM
11244 }
11245 else
602f1657 11246# endif
183e98be 11247 {
602f1657 11248# if defined (HAVE_PSINFO_T)
183e98be
AM
11249 psinfo_t data;
11250 int note_type = NT_PSINFO;
602f1657 11251# else
183e98be
AM
11252 prpsinfo_t data;
11253 int note_type = NT_PRPSINFO;
602f1657 11254# endif
7c76fa91 11255
183e98be
AM
11256 memset (&data, 0, sizeof (data));
11257 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11258 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11259 return elfcore_write_note (abfd, buf, bufsiz,
1f20dca5 11260 "CORE", note_type, &data, sizeof (data));
183e98be 11261 }
7c76fa91
MS
11262#endif /* PSINFO_T or PRPSINFO_T */
11263
1f20dca5
UW
11264 free (buf);
11265 return NULL;
11266}
d99b4b92 11267#if GCC_VERSION >= 8000
602f1657 11268# pragma GCC diagnostic pop
d99b4b92 11269#endif
1f20dca5 11270
70a38d42
SDJ
11271char *
11272elfcore_write_linux_prpsinfo32
11273 (bfd *abfd, char *buf, int *bufsiz,
11274 const struct elf_internal_linux_prpsinfo *prpsinfo)
11275{
a2f63b2e
MR
11276 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
11277 {
11278 struct elf_external_linux_prpsinfo32_ugid16 data;
11279
11280 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
11281 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11282 &data, sizeof (data));
11283 }
11284 else
11285 {
11286 struct elf_external_linux_prpsinfo32_ugid32 data;
70a38d42 11287
a2f63b2e
MR
11288 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
11289 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11290 &data, sizeof (data));
11291 }
70a38d42
SDJ
11292}
11293
11294char *
11295elfcore_write_linux_prpsinfo64
11296 (bfd *abfd, char *buf, int *bufsiz,
11297 const struct elf_internal_linux_prpsinfo *prpsinfo)
11298{
3c9a7b0d
MR
11299 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
11300 {
11301 struct elf_external_linux_prpsinfo64_ugid16 data;
11302
11303 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
11304 return elfcore_write_note (abfd, buf, bufsiz,
11305 "CORE", NT_PRPSINFO, &data, sizeof (data));
11306 }
11307 else
11308 {
11309 struct elf_external_linux_prpsinfo64_ugid32 data;
70a38d42 11310
3c9a7b0d
MR
11311 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
11312 return elfcore_write_note (abfd, buf, bufsiz,
11313 "CORE", NT_PRPSINFO, &data, sizeof (data));
11314 }
70a38d42
SDJ
11315}
11316
7c76fa91 11317char *
217aa764
AM
11318elfcore_write_prstatus (bfd *abfd,
11319 char *buf,
11320 int *bufsiz,
11321 long pid,
11322 int cursig,
11323 const void *gregs)
7c76fa91 11324{
183e98be 11325 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 11326
183e98be
AM
11327 if (bed->elf_backend_write_core_note != NULL)
11328 {
11329 char *ret;
11330 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11331 NT_PRSTATUS,
11332 pid, cursig, gregs);
11333 if (ret != NULL)
11334 return ret;
11335 }
11336
1f20dca5 11337#if defined (HAVE_PRSTATUS_T)
183e98be
AM
11338#if defined (HAVE_PRSTATUS32_T)
11339 if (bed->s->elfclass == ELFCLASS32)
11340 {
11341 prstatus32_t prstat;
11342
11343 memset (&prstat, 0, sizeof (prstat));
11344 prstat.pr_pid = pid;
11345 prstat.pr_cursig = cursig;
11346 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
1f20dca5 11347 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
183e98be
AM
11348 NT_PRSTATUS, &prstat, sizeof (prstat));
11349 }
11350 else
11351#endif
11352 {
11353 prstatus_t prstat;
11354
11355 memset (&prstat, 0, sizeof (prstat));
11356 prstat.pr_pid = pid;
11357 prstat.pr_cursig = cursig;
11358 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
1f20dca5 11359 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
183e98be
AM
11360 NT_PRSTATUS, &prstat, sizeof (prstat));
11361 }
7c76fa91
MS
11362#endif /* HAVE_PRSTATUS_T */
11363
1f20dca5
UW
11364 free (buf);
11365 return NULL;
11366}
11367
51316059
MS
11368#if defined (HAVE_LWPSTATUS_T)
11369char *
217aa764
AM
11370elfcore_write_lwpstatus (bfd *abfd,
11371 char *buf,
11372 int *bufsiz,
11373 long pid,
11374 int cursig,
11375 const void *gregs)
51316059
MS
11376{
11377 lwpstatus_t lwpstat;
183e98be 11378 const char *note_name = "CORE";
51316059
MS
11379
11380 memset (&lwpstat, 0, sizeof (lwpstat));
11381 lwpstat.pr_lwpid = pid >> 16;
11382 lwpstat.pr_cursig = cursig;
11383#if defined (HAVE_LWPSTATUS_T_PR_REG)
d1e8523e 11384 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
51316059
MS
11385#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11386#if !defined(gregs)
11387 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
11388 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
11389#else
11390 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
11391 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
11392#endif
11393#endif
47d9a591 11394 return elfcore_write_note (abfd, buf, bufsiz, note_name,
51316059
MS
11395 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
11396}
11397#endif /* HAVE_LWPSTATUS_T */
11398
7c76fa91
MS
11399#if defined (HAVE_PSTATUS_T)
11400char *
217aa764
AM
11401elfcore_write_pstatus (bfd *abfd,
11402 char *buf,
11403 int *bufsiz,
11404 long pid,
6c10990d
NC
11405 int cursig ATTRIBUTE_UNUSED,
11406 const void *gregs ATTRIBUTE_UNUSED)
7c76fa91 11407{
183e98be
AM
11408 const char *note_name = "CORE";
11409#if defined (HAVE_PSTATUS32_T)
11410 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 11411
183e98be
AM
11412 if (bed->s->elfclass == ELFCLASS32)
11413 {
11414 pstatus32_t pstat;
11415
11416 memset (&pstat, 0, sizeof (pstat));
11417 pstat.pr_pid = pid & 0xffff;
11418 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11419 NT_PSTATUS, &pstat, sizeof (pstat));
11420 return buf;
11421 }
11422 else
11423#endif
11424 {
11425 pstatus_t pstat;
11426
11427 memset (&pstat, 0, sizeof (pstat));
11428 pstat.pr_pid = pid & 0xffff;
11429 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11430 NT_PSTATUS, &pstat, sizeof (pstat));
11431 return buf;
11432 }
7c76fa91
MS
11433}
11434#endif /* HAVE_PSTATUS_T */
11435
11436char *
217aa764
AM
11437elfcore_write_prfpreg (bfd *abfd,
11438 char *buf,
11439 int *bufsiz,
11440 const void *fpregs,
11441 int size)
7c76fa91 11442{
183e98be 11443 const char *note_name = "CORE";
47d9a591 11444 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
11445 note_name, NT_FPREGSET, fpregs, size);
11446}
11447
11448char *
217aa764
AM
11449elfcore_write_prxfpreg (bfd *abfd,
11450 char *buf,
11451 int *bufsiz,
11452 const void *xfpregs,
11453 int size)
7c76fa91
MS
11454{
11455 char *note_name = "LINUX";
47d9a591 11456 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
11457 note_name, NT_PRXFPREG, xfpregs, size);
11458}
11459
4339cae0
L
11460char *
11461elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
11462 const void *xfpregs, int size)
11463{
97de3545
JB
11464 char *note_name;
11465 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
11466 note_name = "FreeBSD";
11467 else
11468 note_name = "LINUX";
4339cae0
L
11469 return elfcore_write_note (abfd, buf, bufsiz,
11470 note_name, NT_X86_XSTATE, xfpregs, size);
11471}
11472
97753bd5
AM
11473char *
11474elfcore_write_ppc_vmx (bfd *abfd,
11475 char *buf,
11476 int *bufsiz,
11477 const void *ppc_vmx,
11478 int size)
11479{
11480 char *note_name = "LINUX";
11481 return elfcore_write_note (abfd, buf, bufsiz,
11482 note_name, NT_PPC_VMX, ppc_vmx, size);
11483}
11484
89eeb0bc
LM
11485char *
11486elfcore_write_ppc_vsx (bfd *abfd,
07d6d2b8
AM
11487 char *buf,
11488 int *bufsiz,
11489 const void *ppc_vsx,
11490 int size)
89eeb0bc
LM
11491{
11492 char *note_name = "LINUX";
11493 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11494 note_name, NT_PPC_VSX, ppc_vsx, size);
89eeb0bc
LM
11495}
11496
cb2366c1
EBM
11497char *
11498elfcore_write_ppc_tar (bfd *abfd,
4b24dd1a
AM
11499 char *buf,
11500 int *bufsiz,
11501 const void *ppc_tar,
11502 int size)
cb2366c1
EBM
11503{
11504 char *note_name = "LINUX";
11505 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11506 note_name, NT_PPC_TAR, ppc_tar, size);
cb2366c1
EBM
11507}
11508
11509char *
11510elfcore_write_ppc_ppr (bfd *abfd,
4b24dd1a
AM
11511 char *buf,
11512 int *bufsiz,
11513 const void *ppc_ppr,
11514 int size)
cb2366c1
EBM
11515{
11516 char *note_name = "LINUX";
11517 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11518 note_name, NT_PPC_PPR, ppc_ppr, size);
cb2366c1
EBM
11519}
11520
11521char *
11522elfcore_write_ppc_dscr (bfd *abfd,
4b24dd1a
AM
11523 char *buf,
11524 int *bufsiz,
11525 const void *ppc_dscr,
11526 int size)
cb2366c1
EBM
11527{
11528 char *note_name = "LINUX";
11529 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11530 note_name, NT_PPC_DSCR, ppc_dscr, size);
cb2366c1
EBM
11531}
11532
11533char *
11534elfcore_write_ppc_ebb (bfd *abfd,
4b24dd1a
AM
11535 char *buf,
11536 int *bufsiz,
11537 const void *ppc_ebb,
11538 int size)
cb2366c1
EBM
11539{
11540 char *note_name = "LINUX";
11541 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11542 note_name, NT_PPC_EBB, ppc_ebb, size);
cb2366c1
EBM
11543}
11544
11545char *
11546elfcore_write_ppc_pmu (bfd *abfd,
4b24dd1a
AM
11547 char *buf,
11548 int *bufsiz,
11549 const void *ppc_pmu,
11550 int size)
cb2366c1
EBM
11551{
11552 char *note_name = "LINUX";
11553 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11554 note_name, NT_PPC_PMU, ppc_pmu, size);
cb2366c1
EBM
11555}
11556
11557char *
11558elfcore_write_ppc_tm_cgpr (bfd *abfd,
4b24dd1a
AM
11559 char *buf,
11560 int *bufsiz,
11561 const void *ppc_tm_cgpr,
11562 int size)
cb2366c1
EBM
11563{
11564 char *note_name = "LINUX";
11565 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11566 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
cb2366c1
EBM
11567}
11568
11569char *
11570elfcore_write_ppc_tm_cfpr (bfd *abfd,
4b24dd1a
AM
11571 char *buf,
11572 int *bufsiz,
11573 const void *ppc_tm_cfpr,
11574 int size)
cb2366c1
EBM
11575{
11576 char *note_name = "LINUX";
11577 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11578 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
cb2366c1
EBM
11579}
11580
11581char *
11582elfcore_write_ppc_tm_cvmx (bfd *abfd,
4b24dd1a
AM
11583 char *buf,
11584 int *bufsiz,
11585 const void *ppc_tm_cvmx,
11586 int size)
cb2366c1
EBM
11587{
11588 char *note_name = "LINUX";
11589 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11590 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
cb2366c1
EBM
11591}
11592
11593char *
11594elfcore_write_ppc_tm_cvsx (bfd *abfd,
4b24dd1a
AM
11595 char *buf,
11596 int *bufsiz,
11597 const void *ppc_tm_cvsx,
11598 int size)
cb2366c1
EBM
11599{
11600 char *note_name = "LINUX";
11601 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11602 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
cb2366c1
EBM
11603}
11604
11605char *
11606elfcore_write_ppc_tm_spr (bfd *abfd,
4b24dd1a
AM
11607 char *buf,
11608 int *bufsiz,
11609 const void *ppc_tm_spr,
11610 int size)
cb2366c1
EBM
11611{
11612 char *note_name = "LINUX";
11613 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11614 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
cb2366c1
EBM
11615}
11616
11617char *
11618elfcore_write_ppc_tm_ctar (bfd *abfd,
4b24dd1a
AM
11619 char *buf,
11620 int *bufsiz,
11621 const void *ppc_tm_ctar,
11622 int size)
cb2366c1
EBM
11623{
11624 char *note_name = "LINUX";
11625 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11626 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
cb2366c1
EBM
11627}
11628
11629char *
11630elfcore_write_ppc_tm_cppr (bfd *abfd,
4b24dd1a
AM
11631 char *buf,
11632 int *bufsiz,
11633 const void *ppc_tm_cppr,
11634 int size)
cb2366c1
EBM
11635{
11636 char *note_name = "LINUX";
11637 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11638 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
cb2366c1
EBM
11639}
11640
11641char *
11642elfcore_write_ppc_tm_cdscr (bfd *abfd,
4b24dd1a
AM
11643 char *buf,
11644 int *bufsiz,
11645 const void *ppc_tm_cdscr,
11646 int size)
cb2366c1
EBM
11647{
11648 char *note_name = "LINUX";
11649 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11650 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
cb2366c1
EBM
11651}
11652
0675e188
UW
11653static char *
11654elfcore_write_s390_high_gprs (bfd *abfd,
11655 char *buf,
11656 int *bufsiz,
11657 const void *s390_high_gprs,
11658 int size)
11659{
11660 char *note_name = "LINUX";
11661 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11662 note_name, NT_S390_HIGH_GPRS,
0675e188
UW
11663 s390_high_gprs, size);
11664}
11665
d7eeb400
MS
11666char *
11667elfcore_write_s390_timer (bfd *abfd,
07d6d2b8
AM
11668 char *buf,
11669 int *bufsiz,
11670 const void *s390_timer,
11671 int size)
d7eeb400
MS
11672{
11673 char *note_name = "LINUX";
11674 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11675 note_name, NT_S390_TIMER, s390_timer, size);
d7eeb400
MS
11676}
11677
11678char *
11679elfcore_write_s390_todcmp (bfd *abfd,
07d6d2b8
AM
11680 char *buf,
11681 int *bufsiz,
11682 const void *s390_todcmp,
11683 int size)
d7eeb400
MS
11684{
11685 char *note_name = "LINUX";
11686 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11687 note_name, NT_S390_TODCMP, s390_todcmp, size);
d7eeb400
MS
11688}
11689
11690char *
11691elfcore_write_s390_todpreg (bfd *abfd,
07d6d2b8
AM
11692 char *buf,
11693 int *bufsiz,
11694 const void *s390_todpreg,
11695 int size)
d7eeb400
MS
11696{
11697 char *note_name = "LINUX";
11698 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11699 note_name, NT_S390_TODPREG, s390_todpreg, size);
d7eeb400
MS
11700}
11701
11702char *
11703elfcore_write_s390_ctrs (bfd *abfd,
07d6d2b8
AM
11704 char *buf,
11705 int *bufsiz,
11706 const void *s390_ctrs,
11707 int size)
d7eeb400
MS
11708{
11709 char *note_name = "LINUX";
11710 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11711 note_name, NT_S390_CTRS, s390_ctrs, size);
d7eeb400
MS
11712}
11713
11714char *
11715elfcore_write_s390_prefix (bfd *abfd,
07d6d2b8
AM
11716 char *buf,
11717 int *bufsiz,
11718 const void *s390_prefix,
11719 int size)
d7eeb400
MS
11720{
11721 char *note_name = "LINUX";
11722 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11723 note_name, NT_S390_PREFIX, s390_prefix, size);
d7eeb400
MS
11724}
11725
355b81d9
UW
11726char *
11727elfcore_write_s390_last_break (bfd *abfd,
11728 char *buf,
11729 int *bufsiz,
11730 const void *s390_last_break,
11731 int size)
11732{
11733 char *note_name = "LINUX";
11734 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11735 note_name, NT_S390_LAST_BREAK,
355b81d9
UW
11736 s390_last_break, size);
11737}
11738
11739char *
11740elfcore_write_s390_system_call (bfd *abfd,
11741 char *buf,
11742 int *bufsiz,
11743 const void *s390_system_call,
11744 int size)
11745{
11746 char *note_name = "LINUX";
11747 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11748 note_name, NT_S390_SYSTEM_CALL,
355b81d9
UW
11749 s390_system_call, size);
11750}
11751
abb3f6cc
NC
11752char *
11753elfcore_write_s390_tdb (bfd *abfd,
11754 char *buf,
11755 int *bufsiz,
11756 const void *s390_tdb,
11757 int size)
11758{
11759 char *note_name = "LINUX";
11760 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11761 note_name, NT_S390_TDB, s390_tdb, size);
abb3f6cc
NC
11762}
11763
4ef9f41a
AA
11764char *
11765elfcore_write_s390_vxrs_low (bfd *abfd,
11766 char *buf,
11767 int *bufsiz,
11768 const void *s390_vxrs_low,
11769 int size)
11770{
11771 char *note_name = "LINUX";
11772 return elfcore_write_note (abfd, buf, bufsiz,
11773 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
11774}
11775
11776char *
11777elfcore_write_s390_vxrs_high (bfd *abfd,
11778 char *buf,
11779 int *bufsiz,
11780 const void *s390_vxrs_high,
11781 int size)
11782{
11783 char *note_name = "LINUX";
11784 return elfcore_write_note (abfd, buf, bufsiz,
11785 note_name, NT_S390_VXRS_HIGH,
11786 s390_vxrs_high, size);
11787}
11788
88ab90e8
AA
11789char *
11790elfcore_write_s390_gs_cb (bfd *abfd,
11791 char *buf,
11792 int *bufsiz,
11793 const void *s390_gs_cb,
11794 int size)
11795{
11796 char *note_name = "LINUX";
11797 return elfcore_write_note (abfd, buf, bufsiz,
11798 note_name, NT_S390_GS_CB,
11799 s390_gs_cb, size);
11800}
11801
11802char *
11803elfcore_write_s390_gs_bc (bfd *abfd,
11804 char *buf,
11805 int *bufsiz,
11806 const void *s390_gs_bc,
11807 int size)
11808{
11809 char *note_name = "LINUX";
11810 return elfcore_write_note (abfd, buf, bufsiz,
11811 note_name, NT_S390_GS_BC,
11812 s390_gs_bc, size);
11813}
11814
faa9a424
UW
11815char *
11816elfcore_write_arm_vfp (bfd *abfd,
11817 char *buf,
11818 int *bufsiz,
11819 const void *arm_vfp,
11820 int size)
11821{
11822 char *note_name = "LINUX";
11823 return elfcore_write_note (abfd, buf, bufsiz,
11824 note_name, NT_ARM_VFP, arm_vfp, size);
11825}
11826
652451f8
YZ
11827char *
11828elfcore_write_aarch_tls (bfd *abfd,
11829 char *buf,
11830 int *bufsiz,
11831 const void *aarch_tls,
11832 int size)
11833{
11834 char *note_name = "LINUX";
11835 return elfcore_write_note (abfd, buf, bufsiz,
11836 note_name, NT_ARM_TLS, aarch_tls, size);
11837}
11838
11839char *
11840elfcore_write_aarch_hw_break (bfd *abfd,
11841 char *buf,
11842 int *bufsiz,
11843 const void *aarch_hw_break,
11844 int size)
11845{
11846 char *note_name = "LINUX";
11847 return elfcore_write_note (abfd, buf, bufsiz,
11848 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
11849}
11850
11851char *
11852elfcore_write_aarch_hw_watch (bfd *abfd,
11853 char *buf,
11854 int *bufsiz,
11855 const void *aarch_hw_watch,
11856 int size)
11857{
11858 char *note_name = "LINUX";
11859 return elfcore_write_note (abfd, buf, bufsiz,
11860 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
11861}
11862
ad1cc4e4
AH
11863char *
11864elfcore_write_aarch_sve (bfd *abfd,
11865 char *buf,
11866 int *bufsiz,
11867 const void *aarch_sve,
11868 int size)
11869{
11870 char *note_name = "LINUX";
11871 return elfcore_write_note (abfd, buf, bufsiz,
11872 note_name, NT_ARM_SVE, aarch_sve, size);
11873}
11874
e6c3b5bf
AH
11875char *
11876elfcore_write_aarch_pauth (bfd *abfd,
11877 char *buf,
11878 int *bufsiz,
11879 const void *aarch_pauth,
11880 int size)
11881{
11882 char *note_name = "LINUX";
11883 return elfcore_write_note (abfd, buf, bufsiz,
11884 note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
11885}
11886
27456742
AK
11887char *
11888elfcore_write_arc_v2 (bfd *abfd,
11889 char *buf,
11890 int *bufsiz,
11891 const void *arc_v2,
11892 int size)
11893{
11894 char *note_name = "LINUX";
11895 return elfcore_write_note (abfd, buf, bufsiz,
11896 note_name, NT_ARC_V2, arc_v2, size);
11897}
11898
bb864ac1
CES
11899char *
11900elfcore_write_register_note (bfd *abfd,
11901 char *buf,
11902 int *bufsiz,
11903 const char *section,
11904 const void *data,
11905 int size)
11906{
11907 if (strcmp (section, ".reg2") == 0)
11908 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
11909 if (strcmp (section, ".reg-xfp") == 0)
11910 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
4339cae0
L
11911 if (strcmp (section, ".reg-xstate") == 0)
11912 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
bb864ac1
CES
11913 if (strcmp (section, ".reg-ppc-vmx") == 0)
11914 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
89eeb0bc
LM
11915 if (strcmp (section, ".reg-ppc-vsx") == 0)
11916 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
cb2366c1
EBM
11917 if (strcmp (section, ".reg-ppc-tar") == 0)
11918 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
11919 if (strcmp (section, ".reg-ppc-ppr") == 0)
11920 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
11921 if (strcmp (section, ".reg-ppc-dscr") == 0)
11922 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
11923 if (strcmp (section, ".reg-ppc-ebb") == 0)
11924 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
11925 if (strcmp (section, ".reg-ppc-pmu") == 0)
11926 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
11927 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
11928 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
11929 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
11930 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
11931 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
11932 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
11933 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
11934 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
11935 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
11936 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
11937 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
11938 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
11939 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
11940 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
11941 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
11942 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
0675e188
UW
11943 if (strcmp (section, ".reg-s390-high-gprs") == 0)
11944 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
d7eeb400
MS
11945 if (strcmp (section, ".reg-s390-timer") == 0)
11946 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
11947 if (strcmp (section, ".reg-s390-todcmp") == 0)
11948 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
11949 if (strcmp (section, ".reg-s390-todpreg") == 0)
11950 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
11951 if (strcmp (section, ".reg-s390-ctrs") == 0)
11952 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
11953 if (strcmp (section, ".reg-s390-prefix") == 0)
11954 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
355b81d9
UW
11955 if (strcmp (section, ".reg-s390-last-break") == 0)
11956 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
11957 if (strcmp (section, ".reg-s390-system-call") == 0)
11958 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
abb3f6cc
NC
11959 if (strcmp (section, ".reg-s390-tdb") == 0)
11960 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
4ef9f41a
AA
11961 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
11962 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
11963 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
11964 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
88ab90e8
AA
11965 if (strcmp (section, ".reg-s390-gs-cb") == 0)
11966 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
11967 if (strcmp (section, ".reg-s390-gs-bc") == 0)
11968 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
faa9a424
UW
11969 if (strcmp (section, ".reg-arm-vfp") == 0)
11970 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
652451f8
YZ
11971 if (strcmp (section, ".reg-aarch-tls") == 0)
11972 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
11973 if (strcmp (section, ".reg-aarch-hw-break") == 0)
11974 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
11975 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
11976 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
ad1cc4e4
AH
11977 if (strcmp (section, ".reg-aarch-sve") == 0)
11978 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
e6c3b5bf
AH
11979 if (strcmp (section, ".reg-aarch-pauth") == 0)
11980 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
27456742
AK
11981 if (strcmp (section, ".reg-arc-v2") == 0)
11982 return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
bb864ac1
CES
11983 return NULL;
11984}
11985
b34976b6 11986static bfd_boolean
276da9b3
L
11987elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
11988 size_t align)
252b5132 11989{
c044fabd 11990 char *p;
252b5132 11991
276da9b3
L
11992 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
11993 gABI specifies that PT_NOTE alignment should be aligned to 4
11994 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
11995 align is less than 4, we use 4 byte alignment. */
11996 if (align < 4)
11997 align = 4;
ef135d43
NC
11998 if (align != 4 && align != 8)
11999 return FALSE;
276da9b3 12000
252b5132
RH
12001 p = buf;
12002 while (p < buf + size)
12003 {
c044fabd 12004 Elf_External_Note *xnp = (Elf_External_Note *) p;
252b5132
RH
12005 Elf_Internal_Note in;
12006
baea7ef1
AM
12007 if (offsetof (Elf_External_Note, name) > buf - p + size)
12008 return FALSE;
12009
dc810e39 12010 in.type = H_GET_32 (abfd, xnp->type);
252b5132 12011
dc810e39 12012 in.namesz = H_GET_32 (abfd, xnp->namesz);
252b5132 12013 in.namedata = xnp->name;
baea7ef1
AM
12014 if (in.namesz > buf - in.namedata + size)
12015 return FALSE;
252b5132 12016
dc810e39 12017 in.descsz = H_GET_32 (abfd, xnp->descsz);
276da9b3 12018 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
252b5132 12019 in.descpos = offset + (in.descdata - buf);
baea7ef1
AM
12020 if (in.descsz != 0
12021 && (in.descdata >= buf + size
12022 || in.descsz > buf - in.descdata + size))
12023 return FALSE;
252b5132 12024
718175fa 12025 switch (bfd_get_format (abfd))
07d6d2b8 12026 {
718175fa
JK
12027 default:
12028 return TRUE;
12029
12030 case bfd_core:
f64e188b 12031 {
8acbedd6 12032#define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
f64e188b 12033 struct
718175fa 12034 {
f64e188b 12035 const char * string;
8acbedd6 12036 size_t len;
f64e188b 12037 bfd_boolean (* func)(bfd *, Elf_Internal_Note *);
718175fa 12038 }
f64e188b 12039 grokers[] =
b15fa79e 12040 {
8acbedd6 12041 GROKER_ELEMENT ("", elfcore_grok_note),
aa1ed4a9 12042 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
8acbedd6
KS
12043 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
12044 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note),
12045 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
864619bb
KS
12046 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
12047 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note)
f64e188b 12048 };
8acbedd6 12049#undef GROKER_ELEMENT
f64e188b
NC
12050 int i;
12051
12052 for (i = ARRAY_SIZE (grokers); i--;)
8acbedd6
KS
12053 {
12054 if (in.namesz >= grokers[i].len
12055 && strncmp (in.namedata, grokers[i].string,
12056 grokers[i].len) == 0)
12057 {
12058 if (! grokers[i].func (abfd, & in))
12059 return FALSE;
12060 break;
12061 }
12062 }
f64e188b
NC
12063 break;
12064 }
718175fa
JK
12065
12066 case bfd_object:
12067 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
12068 {
12069 if (! elfobj_grok_gnu_note (abfd, &in))
12070 return FALSE;
12071 }
e21e5835
NC
12072 else if (in.namesz == sizeof "stapsdt"
12073 && strcmp (in.namedata, "stapsdt") == 0)
12074 {
12075 if (! elfobj_grok_stapsdt_note (abfd, &in))
12076 return FALSE;
12077 }
718175fa 12078 break;
08a40648 12079 }
252b5132 12080
276da9b3 12081 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
252b5132
RH
12082 }
12083
718175fa
JK
12084 return TRUE;
12085}
12086
864619bb 12087bfd_boolean
276da9b3
L
12088elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
12089 size_t align)
718175fa
JK
12090{
12091 char *buf;
12092
957e1fc1 12093 if (size == 0 || (size + 1) == 0)
718175fa
JK
12094 return TRUE;
12095
12096 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
12097 return FALSE;
12098
2bb3687b 12099 buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
718175fa
JK
12100 if (buf == NULL)
12101 return FALSE;
12102
f64e188b
NC
12103 /* PR 17512: file: ec08f814
12104 0-termintate the buffer so that string searches will not overflow. */
12105 buf[size] = 0;
12106
2bb3687b 12107 if (!elf_parse_notes (abfd, buf, size, offset, align))
718175fa
JK
12108 {
12109 free (buf);
12110 return FALSE;
12111 }
12112
252b5132 12113 free (buf);
b34976b6 12114 return TRUE;
252b5132 12115}
98d8431c
JB
12116\f
12117/* Providing external access to the ELF program header table. */
12118
12119/* Return an upper bound on the number of bytes required to store a
12120 copy of ABFD's program header table entries. Return -1 if an error
12121 occurs; bfd_get_error will return an appropriate code. */
c044fabd 12122
98d8431c 12123long
217aa764 12124bfd_get_elf_phdr_upper_bound (bfd *abfd)
98d8431c
JB
12125{
12126 if (abfd->xvec->flavour != bfd_target_elf_flavour)
12127 {
12128 bfd_set_error (bfd_error_wrong_format);
12129 return -1;
12130 }
12131
936e320b 12132 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
98d8431c
JB
12133}
12134
98d8431c
JB
12135/* Copy ABFD's program header table entries to *PHDRS. The entries
12136 will be stored as an array of Elf_Internal_Phdr structures, as
12137 defined in include/elf/internal.h. To find out how large the
12138 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
12139
12140 Return the number of program header table entries read, or -1 if an
12141 error occurs; bfd_get_error will return an appropriate code. */
c044fabd 12142
98d8431c 12143int
217aa764 12144bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
98d8431c
JB
12145{
12146 int num_phdrs;
12147
12148 if (abfd->xvec->flavour != bfd_target_elf_flavour)
12149 {
12150 bfd_set_error (bfd_error_wrong_format);
12151 return -1;
12152 }
12153
12154 num_phdrs = elf_elfheader (abfd)->e_phnum;
01bcaf63
TT
12155 if (num_phdrs != 0)
12156 memcpy (phdrs, elf_tdata (abfd)->phdr,
12157 num_phdrs * sizeof (Elf_Internal_Phdr));
98d8431c
JB
12158
12159 return num_phdrs;
12160}
ae4221d7 12161
db6751f2 12162enum elf_reloc_type_class
7e612e98
AM
12163_bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12164 const asection *rel_sec ATTRIBUTE_UNUSED,
12165 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
db6751f2
JJ
12166{
12167 return reloc_class_normal;
12168}
f8df10f4 12169
47d9a591 12170/* For RELA architectures, return the relocation value for a
f8df10f4
JJ
12171 relocation against a local symbol. */
12172
12173bfd_vma
217aa764
AM
12174_bfd_elf_rela_local_sym (bfd *abfd,
12175 Elf_Internal_Sym *sym,
8517fae7 12176 asection **psec,
217aa764 12177 Elf_Internal_Rela *rel)
f8df10f4 12178{
8517fae7 12179 asection *sec = *psec;
f8df10f4
JJ
12180 bfd_vma relocation;
12181
6835821b
AM
12182 relocation = (sec->output_section->vma
12183 + sec->output_offset
12184 + sym->st_value);
f8df10f4 12185 if ((sec->flags & SEC_MERGE)
c629eae0 12186 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
dbaa2011 12187 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
f8df10f4 12188 {
f8df10f4 12189 rel->r_addend =
8517fae7 12190 _bfd_merged_section_offset (abfd, psec,
65765700 12191 elf_section_data (sec)->sec_info,
753731ee
AM
12192 sym->st_value + rel->r_addend);
12193 if (sec != *psec)
12194 {
12195 /* If we have changed the section, and our original section is
12196 marked with SEC_EXCLUDE, it means that the original
12197 SEC_MERGE section has been completely subsumed in some
12198 other SEC_MERGE section. In this case, we need to leave
12199 some info around for --emit-relocs. */
12200 if ((sec->flags & SEC_EXCLUDE) != 0)
12201 sec->kept_section = *psec;
12202 sec = *psec;
12203 }
8517fae7
AM
12204 rel->r_addend -= relocation;
12205 rel->r_addend += sec->output_section->vma + sec->output_offset;
f8df10f4
JJ
12206 }
12207 return relocation;
12208}
c629eae0
JJ
12209
12210bfd_vma
217aa764
AM
12211_bfd_elf_rel_local_sym (bfd *abfd,
12212 Elf_Internal_Sym *sym,
12213 asection **psec,
12214 bfd_vma addend)
47d9a591 12215{
c629eae0
JJ
12216 asection *sec = *psec;
12217
6835821b 12218 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
c629eae0
JJ
12219 return sym->st_value + addend;
12220
12221 return _bfd_merged_section_offset (abfd, psec,
65765700 12222 elf_section_data (sec)->sec_info,
753731ee 12223 sym->st_value + addend);
c629eae0
JJ
12224}
12225
37b01f6a
DG
12226/* Adjust an address within a section. Given OFFSET within SEC, return
12227 the new offset within the section, based upon changes made to the
12228 section. Returns -1 if the offset is now invalid.
12229 The offset (in abnd out) is in target sized bytes, however big a
12230 byte may be. */
12231
c629eae0 12232bfd_vma
217aa764 12233_bfd_elf_section_offset (bfd *abfd,
92e4ec35 12234 struct bfd_link_info *info,
217aa764
AM
12235 asection *sec,
12236 bfd_vma offset)
c629eae0 12237{
68bfbfcc 12238 switch (sec->sec_info_type)
65765700 12239 {
dbaa2011 12240 case SEC_INFO_TYPE_STABS:
eea6121a
AM
12241 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
12242 offset);
dbaa2011 12243 case SEC_INFO_TYPE_EH_FRAME:
92e4ec35 12244 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
37b01f6a 12245
65765700 12246 default:
310fd250
L
12247 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
12248 {
37b01f6a 12249 /* Reverse the offset. */
310fd250
L
12250 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12251 bfd_size_type address_size = bed->s->arch_size / 8;
37b01f6a
DG
12252
12253 /* address_size and sec->size are in octets. Convert
12254 to bytes before subtracting the original offset. */
61826503 12255 offset = ((sec->size - address_size)
bb294208 12256 / bfd_octets_per_byte (abfd, sec) - offset);
310fd250 12257 }
65765700
JJ
12258 return offset;
12259 }
c629eae0 12260}
3333a7c3
RM
12261\f
12262/* Create a new BFD as if by bfd_openr. Rather than opening a file,
12263 reconstruct an ELF file by reading the segments out of remote memory
12264 based on the ELF file header at EHDR_VMA and the ELF program headers it
12265 points to. If not null, *LOADBASEP is filled in with the difference
12266 between the VMAs from which the segments were read, and the VMAs the
12267 file headers (and hence BFD's idea of each section's VMA) put them at.
12268
12269 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12270 remote memory at target address VMA into the local buffer at MYADDR; it
12271 should return zero on success or an `errno' code on failure. TEMPL must
12272 be a BFD for an ELF target with the word size and byte order found in
12273 the remote memory. */
12274
12275bfd *
217aa764
AM
12276bfd_elf_bfd_from_remote_memory
12277 (bfd *templ,
12278 bfd_vma ehdr_vma,
f0a5d95a 12279 bfd_size_type size,
217aa764 12280 bfd_vma *loadbasep,
fe78531d 12281 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
3333a7c3
RM
12282{
12283 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
5979d6b6 12284 (templ, ehdr_vma, size, loadbasep, target_read_memory);
3333a7c3 12285}
4c45e5c9
JJ
12286\f
12287long
c9727e01
AM
12288_bfd_elf_get_synthetic_symtab (bfd *abfd,
12289 long symcount ATTRIBUTE_UNUSED,
12290 asymbol **syms ATTRIBUTE_UNUSED,
8615f3f2 12291 long dynsymcount,
c9727e01
AM
12292 asymbol **dynsyms,
12293 asymbol **ret)
4c45e5c9
JJ
12294{
12295 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12296 asection *relplt;
12297 asymbol *s;
12298 const char *relplt_name;
12299 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
12300 arelent *p;
12301 long count, i, n;
12302 size_t size;
12303 Elf_Internal_Shdr *hdr;
12304 char *names;
12305 asection *plt;
12306
8615f3f2
AM
12307 *ret = NULL;
12308
90e3cdf2
JJ
12309 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
12310 return 0;
12311
8615f3f2
AM
12312 if (dynsymcount <= 0)
12313 return 0;
12314
4c45e5c9
JJ
12315 if (!bed->plt_sym_val)
12316 return 0;
12317
12318 relplt_name = bed->relplt_name;
12319 if (relplt_name == NULL)
d35fd659 12320 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
4c45e5c9
JJ
12321 relplt = bfd_get_section_by_name (abfd, relplt_name);
12322 if (relplt == NULL)
12323 return 0;
12324
12325 hdr = &elf_section_data (relplt)->this_hdr;
12326 if (hdr->sh_link != elf_dynsymtab (abfd)
12327 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
12328 return 0;
12329
12330 plt = bfd_get_section_by_name (abfd, ".plt");
12331 if (plt == NULL)
12332 return 0;
12333
12334 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
c9727e01 12335 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
4c45e5c9
JJ
12336 return -1;
12337
eea6121a 12338 count = relplt->size / hdr->sh_entsize;
4c45e5c9
JJ
12339 size = count * sizeof (asymbol);
12340 p = relplt->relocation;
cb53bf42 12341 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
041de40d
AM
12342 {
12343 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
12344 if (p->addend != 0)
12345 {
12346#ifdef BFD64
12347 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
12348#else
12349 size += sizeof ("+0x") - 1 + 8;
12350#endif
12351 }
12352 }
4c45e5c9 12353
a50b1753 12354 s = *ret = (asymbol *) bfd_malloc (size);
4c45e5c9
JJ
12355 if (s == NULL)
12356 return -1;
12357
12358 names = (char *) (s + count);
12359 p = relplt->relocation;
12360 n = 0;
cb53bf42 12361 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
4c45e5c9
JJ
12362 {
12363 size_t len;
12364 bfd_vma addr;
12365
12366 addr = bed->plt_sym_val (i, plt, p);
12367 if (addr == (bfd_vma) -1)
12368 continue;
12369
12370 *s = **p->sym_ptr_ptr;
65a7a66f
AM
12371 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
12372 we are defining a symbol, ensure one of them is set. */
12373 if ((s->flags & BSF_LOCAL) == 0)
12374 s->flags |= BSF_GLOBAL;
6ba2a415 12375 s->flags |= BSF_SYNTHETIC;
4c45e5c9
JJ
12376 s->section = plt;
12377 s->value = addr - plt->vma;
12378 s->name = names;
8f39ba8e 12379 s->udata.p = NULL;
4c45e5c9
JJ
12380 len = strlen ((*p->sym_ptr_ptr)->name);
12381 memcpy (names, (*p->sym_ptr_ptr)->name, len);
12382 names += len;
041de40d
AM
12383 if (p->addend != 0)
12384 {
1d770845 12385 char buf[30], *a;
d324f6d6 12386
041de40d
AM
12387 memcpy (names, "+0x", sizeof ("+0x") - 1);
12388 names += sizeof ("+0x") - 1;
1d770845
L
12389 bfd_sprintf_vma (abfd, buf, p->addend);
12390 for (a = buf; *a == '0'; ++a)
12391 ;
12392 len = strlen (a);
12393 memcpy (names, a, len);
12394 names += len;
041de40d 12395 }
4c45e5c9
JJ
12396 memcpy (names, "@plt", sizeof ("@plt"));
12397 names += sizeof ("@plt");
8f39ba8e 12398 ++s, ++n;
4c45e5c9
JJ
12399 }
12400
12401 return n;
12402}
3d7f7666 12403
821e6ff6
AM
12404/* It is only used by x86-64 so far.
12405 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
7eacd66b
AM
12406 but current usage would allow all of _bfd_std_section to be zero. */
12407static const asymbol lcomm_sym
12408 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
3b22753a 12409asection _bfd_elf_large_com_section
7eacd66b 12410 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
821e6ff6 12411 "LARGE_COMMON", 0, SEC_IS_COMMON);
ecca9871 12412
cc364be6
AM
12413bfd_boolean
12414_bfd_elf_final_write_processing (bfd *abfd)
06f44071
AM
12415{
12416 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
d1036acb
L
12417
12418 i_ehdrp = elf_elfheader (abfd);
12419
06f44071
AM
12420 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12421 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
d8045f23 12422
df3a023b
AM
12423 /* Set the osabi field to ELFOSABI_GNU if the binary contains
12424 SHF_GNU_MBIND sections or symbols of STT_GNU_IFUNC type or
12425 STB_GNU_UNIQUE binding. */
cc364be6
AM
12426 if (elf_tdata (abfd)->has_gnu_osabi != 0)
12427 {
12428 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12429 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
12430 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
12431 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
12432 {
12433 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
12434 _bfd_error_handler (_("GNU_MBIND section is unsupported"));
12435 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
12436 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is unsupported"));
12437 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
12438 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is unsupported"));
9aea1e31 12439 bfd_set_error (bfd_error_sorry);
cc364be6
AM
12440 return FALSE;
12441 }
12442 }
12443 return TRUE;
d1036acb 12444}
fcb93ecf
PB
12445
12446
12447/* Return TRUE for ELF symbol types that represent functions.
12448 This is the default version of this function, which is sufficient for
d8045f23 12449 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
fcb93ecf
PB
12450
12451bfd_boolean
12452_bfd_elf_is_function_type (unsigned int type)
12453{
d8045f23
NC
12454 return (type == STT_FUNC
12455 || type == STT_GNU_IFUNC);
fcb93ecf 12456}
9f296da3 12457
aef36ac1
AM
12458/* If the ELF symbol SYM might be a function in SEC, return the
12459 function size and set *CODE_OFF to the function's entry point,
12460 otherwise return zero. */
9f296da3 12461
aef36ac1
AM
12462bfd_size_type
12463_bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
12464 bfd_vma *code_off)
9f296da3 12465{
aef36ac1
AM
12466 bfd_size_type size;
12467
ff9e0f5b 12468 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
aef36ac1
AM
12469 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
12470 || sym->section != sec)
12471 return 0;
ff9e0f5b 12472
ff9e0f5b 12473 *code_off = sym->value;
aef36ac1
AM
12474 size = 0;
12475 if (!(sym->flags & BSF_SYNTHETIC))
12476 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
12477 if (size == 0)
12478 size = 1;
12479 return size;
9f296da3 12480}
a8e14f4c
NC
12481
12482/* Set to non-zero to enable some debug messages. */
12483#define DEBUG_SECONDARY_RELOCS 0
12484
12485/* An internal-to-the-bfd-library only section type
12486 used to indicate a cached secondary reloc section. */
12487#define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
12488
12489/* Create a BFD section to hold a secondary reloc section. */
12490
12491bfd_boolean
12492_bfd_elf_init_secondary_reloc_section (bfd * abfd,
12493 Elf_Internal_Shdr *hdr,
12494 const char * name,
12495 unsigned int shindex)
12496{
12497 /* We only support RELA secondary relocs. */
12498 if (hdr->sh_type != SHT_RELA)
12499 return FALSE;
12500
12501#if DEBUG_SECONDARY_RELOCS
12502 fprintf (stderr, "secondary reloc section %s encountered\n", name);
12503#endif
12504 hdr->sh_type = SHT_SECONDARY_RELOC;
12505 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
12506}
12507
12508/* Read in any secondary relocs associated with SEC. */
12509
12510bfd_boolean
12511_bfd_elf_slurp_secondary_reloc_section (bfd * abfd,
12512 asection * sec,
12513 asymbol ** symbols)
12514{
12515 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
12516 asection * relsec;
12517 bfd_boolean result = TRUE;
12518 bfd_vma (*r_sym) (bfd_vma);
12519
12520#if BFD_DEFAULT_TARGET_SIZE > 32
12521 if (bfd_arch_bits_per_address (abfd) != 32)
12522 r_sym = elf64_r_sym;
12523 else
12524#endif
12525 r_sym = elf32_r_sym;
12526
12527 /* Discover if there are any secondary reloc sections
12528 associated with SEC. */
12529 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
12530 {
12531 Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
12532
12533 if (hdr->sh_type == SHT_SECONDARY_RELOC
12534 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
12535 {
12536 bfd_byte * native_relocs;
12537 bfd_byte * native_reloc;
12538 arelent * internal_relocs;
12539 arelent * internal_reloc;
12540 unsigned int i;
12541 unsigned int entsize;
12542 unsigned int symcount;
12543 unsigned int reloc_count;
12544 size_t amt;
12545
12546 if (ebd->elf_info_to_howto == NULL)
12547 return FALSE;
12548
12549#if DEBUG_SECONDARY_RELOCS
12550 fprintf (stderr, "read secondary relocs for %s from %s\n",
12551 sec->name, relsec->name);
12552#endif
12553 entsize = hdr->sh_entsize;
12554
12555 native_relocs = bfd_malloc (hdr->sh_size);
12556 if (native_relocs == NULL)
12557 {
12558 result = FALSE;
12559 continue;
12560 }
12561
12562 reloc_count = NUM_SHDR_ENTRIES (hdr);
12563 if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
12564 {
ecbbbdba 12565 free (native_relocs);
a8e14f4c
NC
12566 bfd_set_error (bfd_error_file_too_big);
12567 result = FALSE;
12568 continue;
12569 }
12570
12571 internal_relocs = (arelent *) bfd_alloc (abfd, amt);
12572 if (internal_relocs == NULL)
12573 {
12574 free (native_relocs);
12575 result = FALSE;
12576 continue;
12577 }
12578
12579 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
12580 || (bfd_bread (native_relocs, hdr->sh_size, abfd)
12581 != hdr->sh_size))
12582 {
12583 free (native_relocs);
ecbbbdba
NC
12584 /* The internal_relocs will be freed when
12585 the memory for the bfd is released. */
a8e14f4c
NC
12586 result = FALSE;
12587 continue;
12588 }
12589
12590 symcount = bfd_get_symcount (abfd);
12591
12592 for (i = 0, internal_reloc = internal_relocs,
12593 native_reloc = native_relocs;
12594 i < reloc_count;
12595 i++, internal_reloc++, native_reloc += entsize)
12596 {
12597 bfd_boolean res;
12598 Elf_Internal_Rela rela;
12599
12600 ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
12601
12602 /* The address of an ELF reloc is section relative for an object
12603 file, and absolute for an executable file or shared library.
12604 The address of a normal BFD reloc is always section relative,
12605 and the address of a dynamic reloc is absolute.. */
12606 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
12607 internal_reloc->address = rela.r_offset;
12608 else
12609 internal_reloc->address = rela.r_offset - sec->vma;
12610
12611 if (r_sym (rela.r_info) == STN_UNDEF)
12612 {
12613 /* FIXME: This and the error case below mean that we
12614 have a symbol on relocs that is not elf_symbol_type. */
12615 internal_reloc->sym_ptr_ptr =
12616 bfd_abs_section_ptr->symbol_ptr_ptr;
12617 }
12618 else if (r_sym (rela.r_info) > symcount)
12619 {
12620 _bfd_error_handler
12621 /* xgettext:c-format */
12622 (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
12623 abfd, sec, i, (long) r_sym (rela.r_info));
12624 bfd_set_error (bfd_error_bad_value);
12625 internal_reloc->sym_ptr_ptr =
12626 bfd_abs_section_ptr->symbol_ptr_ptr;
12627 result = FALSE;
12628 }
12629 else
12630 {
12631 asymbol **ps;
12632
12633 ps = symbols + r_sym (rela.r_info) - 1;
12634
12635 internal_reloc->sym_ptr_ptr = ps;
12636 /* Make sure that this symbol is not removed by strip. */
12637 (*ps)->flags |= BSF_KEEP;
12638 }
12639
12640 internal_reloc->addend = rela.r_addend;
12641
12642 res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
12643 if (! res || internal_reloc->howto == NULL)
12644 {
12645#if DEBUG_SECONDARY_RELOCS
12646 fprintf (stderr, "there is no howto associated with reloc %lx\n",
12647 rela.r_info);
12648#endif
12649 result = FALSE;
12650 }
12651 }
12652
12653 free (native_relocs);
12654 /* Store the internal relocs. */
12655 elf_section_data (relsec)->sec_info = internal_relocs;
12656 }
12657 }
12658
12659 return result;
12660}
12661
12662/* Set the ELF section header fields of an output secondary reloc section. */
12663
12664bfd_boolean
12665_bfd_elf_copy_special_section_fields (const bfd * ibfd ATTRIBUTE_UNUSED,
12666 bfd * obfd ATTRIBUTE_UNUSED,
12667 const Elf_Internal_Shdr * isection,
12668 Elf_Internal_Shdr * osection)
12669{
12670 asection * isec;
12671 asection * osec;
12672
12673 if (isection == NULL)
12674 return FALSE;
12675
12676 if (isection->sh_type != SHT_SECONDARY_RELOC)
12677 return TRUE;
12678
12679 isec = isection->bfd_section;
12680 if (isec == NULL)
12681 return FALSE;
12682
12683 osec = osection->bfd_section;
12684 if (osec == NULL)
12685 return FALSE;
12686
12687 BFD_ASSERT (elf_section_data (osec)->sec_info == NULL);
12688 elf_section_data (osec)->sec_info = elf_section_data (isec)->sec_info;
12689 osection->sh_type = SHT_RELA;
12690 osection->sh_link = elf_onesymtab (obfd);
12691 if (osection->sh_link == 0)
12692 {
12693 /* There is no symbol table - we are hosed... */
12694 _bfd_error_handler
12695 /* xgettext:c-format */
12696 (_("%pB(%pA): link section cannot be set because the output file does not have a symbol table"),
12697 obfd, osec);
12698 bfd_set_error (bfd_error_bad_value);
12699 return FALSE;
12700 }
12701
12702 /* Find the output section that corresponds to the isection's sh_info link. */
327ef784
NC
12703 if (isection->sh_info == 0
12704 || isection->sh_info >= elf_numsections (ibfd))
12705 {
12706 _bfd_error_handler
12707 /* xgettext:c-format */
12708 (_("%pB(%pA): info section index is invalid"),
12709 obfd, osec);
12710 bfd_set_error (bfd_error_bad_value);
12711 return FALSE;
12712 }
12713
a8e14f4c
NC
12714 isection = elf_elfsections (ibfd)[isection->sh_info];
12715
327ef784
NC
12716 if (isection == NULL
12717 || isection->bfd_section == NULL
12718 || isection->bfd_section->output_section == NULL)
12719 {
12720 _bfd_error_handler
12721 /* xgettext:c-format */
12722 (_("%pB(%pA): info section index cannot be set because the section is not in the output"),
12723 obfd, osec);
12724 bfd_set_error (bfd_error_bad_value);
12725 return FALSE;
12726 }
12727
a8e14f4c
NC
12728 osection->sh_info =
12729 elf_section_data (isection->bfd_section->output_section)->this_idx;
12730
12731#if DEBUG_SECONDARY_RELOCS
12732 fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
12733 osec->name, osection->sh_link, osection->sh_info);
12734#endif
12735
12736 return TRUE;
12737}
12738
12739/* Write out a secondary reloc section. */
12740
12741bfd_boolean
12742_bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
12743{
12744 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
12745 bfd_vma addr_offset;
12746 asection * relsec;
12747 bfd_vma (*r_info) (bfd_vma, bfd_vma);
ac4bf06c
NC
12748 bfd_boolean result = TRUE;
12749
12750 if (sec == NULL)
12751 return FALSE;
a8e14f4c
NC
12752
12753#if BFD_DEFAULT_TARGET_SIZE > 32
12754 if (bfd_arch_bits_per_address (abfd) != 32)
12755 r_info = elf64_r_info;
12756 else
12757#endif
12758 r_info = elf32_r_info;
12759
a8e14f4c
NC
12760 /* The address of an ELF reloc is section relative for an object
12761 file, and absolute for an executable file or shared library.
12762 The address of a BFD reloc is always section relative. */
12763 addr_offset = 0;
12764 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
12765 addr_offset = sec->vma;
12766
12767 /* Discover if there are any secondary reloc sections
12768 associated with SEC. */
12769 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
12770 {
12771 const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
12772 Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
12773
12774 if (hdr->sh_type == SHT_RELA
12775 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
12776 {
12777 asymbol * last_sym;
12778 int last_sym_idx;
12779 unsigned int reloc_count;
12780 unsigned int idx;
12781 arelent * src_irel;
12782 bfd_byte * dst_rela;
12783
ac4bf06c
NC
12784 if (hdr->contents != NULL)
12785 {
12786 _bfd_error_handler
12787 /* xgettext:c-format */
12788 (_("%pB(%pA): error: secondary reloc section processed twice"),
12789 abfd, relsec);
12790 bfd_set_error (bfd_error_bad_value);
12791 result = FALSE;
12792 continue;
12793 }
a8e14f4c
NC
12794
12795 reloc_count = hdr->sh_size / hdr->sh_entsize;
ac4bf06c
NC
12796 if (reloc_count <= 0)
12797 {
12798 _bfd_error_handler
12799 /* xgettext:c-format */
12800 (_("%pB(%pA): error: secondary reloc section is empty!"),
12801 abfd, relsec);
12802 bfd_set_error (bfd_error_bad_value);
12803 result = FALSE;
12804 continue;
12805 }
a8e14f4c
NC
12806
12807 hdr->contents = bfd_alloc (abfd, hdr->sh_size);
12808 if (hdr->contents == NULL)
12809 continue;
12810
12811#if DEBUG_SECONDARY_RELOCS
12812 fprintf (stderr, "write %u secondary relocs for %s from %s\n",
12813 reloc_count, sec->name, relsec->name);
12814#endif
12815 last_sym = NULL;
12816 last_sym_idx = 0;
12817 dst_rela = hdr->contents;
12818 src_irel = (arelent *) esd->sec_info;
ac4bf06c
NC
12819 if (src_irel == NULL)
12820 {
12821 _bfd_error_handler
12822 /* xgettext:c-format */
12823 (_("%pB(%pA): error: internal relocs missing for secondary reloc section"),
12824 abfd, relsec);
12825 bfd_set_error (bfd_error_bad_value);
12826 result = FALSE;
12827 continue;
12828 }
a8e14f4c
NC
12829
12830 for (idx = 0; idx < reloc_count; idx++, dst_rela += hdr->sh_entsize)
12831 {
12832 Elf_Internal_Rela src_rela;
12833 arelent *ptr;
12834 asymbol *sym;
12835 int n;
12836
12837 ptr = src_irel + idx;
ac4bf06c
NC
12838 if (ptr == NULL)
12839 {
12840 _bfd_error_handler
12841 /* xgettext:c-format */
12842 (_("%pB(%pA): error: reloc table entry %u is empty"),
12843 abfd, relsec, idx);
12844 bfd_set_error (bfd_error_bad_value);
12845 result = FALSE;
12846 break;
12847 }
a8e14f4c 12848
ac4bf06c
NC
12849 if (ptr->sym_ptr_ptr == NULL)
12850 {
12851 /* FIXME: Is this an error ? */
12852 n = 0;
12853 }
a8e14f4c
NC
12854 else
12855 {
ac4bf06c
NC
12856 sym = *ptr->sym_ptr_ptr;
12857
12858 if (sym == last_sym)
12859 n = last_sym_idx;
12860 else
a8e14f4c 12861 {
ac4bf06c
NC
12862 n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
12863 if (n < 0)
12864 {
12865 _bfd_error_handler
12866 /* xgettext:c-format */
12867 (_("%pB(%pA): error: secondary reloc %u references a missing symbol"),
12868 abfd, relsec, idx);
12869 bfd_set_error (bfd_error_bad_value);
12870 result = FALSE;
12871 n = 0;
12872 }
12873
12874 last_sym = sym;
12875 last_sym_idx = n;
a8e14f4c 12876 }
a8e14f4c 12877
ac4bf06c
NC
12878 if (sym->the_bfd != NULL
12879 && sym->the_bfd->xvec != abfd->xvec
12880 && ! _bfd_elf_validate_reloc (abfd, ptr))
12881 {
12882 _bfd_error_handler
12883 /* xgettext:c-format */
12884 (_("%pB(%pA): error: secondary reloc %u references a deleted symbol"),
12885 abfd, relsec, idx);
12886 bfd_set_error (bfd_error_bad_value);
12887 result = FALSE;
12888 n = 0;
12889 }
a8e14f4c
NC
12890 }
12891
ac4bf06c 12892 src_rela.r_offset = ptr->address + addr_offset;
a8e14f4c
NC
12893 if (ptr->howto == NULL)
12894 {
ac4bf06c
NC
12895 _bfd_error_handler
12896 /* xgettext:c-format */
12897 (_("%pB(%pA): error: secondary reloc %u is of an unknown type"),
12898 abfd, relsec, idx);
12899 bfd_set_error (bfd_error_bad_value);
12900 result = FALSE;
12901 src_rela.r_info = r_info (0, 0);
a8e14f4c 12902 }
ac4bf06c
NC
12903 else
12904 src_rela.r_info = r_info (n, ptr->howto->type);
a8e14f4c
NC
12905 src_rela.r_addend = ptr->addend;
12906 ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
12907 }
12908 }
12909 }
12910
ac4bf06c 12911 return result;
a8e14f4c 12912}
This page took 2.355896 seconds and 4 git commands to generate.