Automatic date update in version.in
[deliverable/binutils-gdb.git] / bfd / elf.c
CommitLineData
252b5132 1/* ELF executable support for BFD.
340b6d91 2
82704155 3 Copyright (C) 1993-2019 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 *);
217aa764 54static bfd_boolean prep_headers (bfd *);
ef10c3ac 55static bfd_boolean swap_out_syms (bfd *, struct elf_strtab_hash **, int) ;
276da9b3
L
56static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type,
57 size_t align) ;
718175fa 58static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
276da9b3 59 file_ptr offset, size_t align);
50b2bdb7 60
252b5132
RH
61/* Swap version information in and out. The version information is
62 currently size independent. If that ever changes, this code will
63 need to move into elfcode.h. */
64
65/* Swap in a Verdef structure. */
66
67void
217aa764
AM
68_bfd_elf_swap_verdef_in (bfd *abfd,
69 const Elf_External_Verdef *src,
70 Elf_Internal_Verdef *dst)
252b5132 71{
dc810e39
AM
72 dst->vd_version = H_GET_16 (abfd, src->vd_version);
73 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
74 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
75 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
76 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
77 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
78 dst->vd_next = H_GET_32 (abfd, src->vd_next);
252b5132
RH
79}
80
81/* Swap out a Verdef structure. */
82
83void
217aa764
AM
84_bfd_elf_swap_verdef_out (bfd *abfd,
85 const Elf_Internal_Verdef *src,
86 Elf_External_Verdef *dst)
252b5132 87{
dc810e39
AM
88 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
89 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
90 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
91 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
92 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
93 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
94 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
252b5132
RH
95}
96
97/* Swap in a Verdaux structure. */
98
99void
217aa764
AM
100_bfd_elf_swap_verdaux_in (bfd *abfd,
101 const Elf_External_Verdaux *src,
102 Elf_Internal_Verdaux *dst)
252b5132 103{
dc810e39
AM
104 dst->vda_name = H_GET_32 (abfd, src->vda_name);
105 dst->vda_next = H_GET_32 (abfd, src->vda_next);
252b5132
RH
106}
107
108/* Swap out a Verdaux structure. */
109
110void
217aa764
AM
111_bfd_elf_swap_verdaux_out (bfd *abfd,
112 const Elf_Internal_Verdaux *src,
113 Elf_External_Verdaux *dst)
252b5132 114{
dc810e39
AM
115 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
116 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
252b5132
RH
117}
118
119/* Swap in a Verneed structure. */
120
121void
217aa764
AM
122_bfd_elf_swap_verneed_in (bfd *abfd,
123 const Elf_External_Verneed *src,
124 Elf_Internal_Verneed *dst)
252b5132 125{
dc810e39
AM
126 dst->vn_version = H_GET_16 (abfd, src->vn_version);
127 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
128 dst->vn_file = H_GET_32 (abfd, src->vn_file);
129 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
130 dst->vn_next = H_GET_32 (abfd, src->vn_next);
252b5132
RH
131}
132
133/* Swap out a Verneed structure. */
134
135void
217aa764
AM
136_bfd_elf_swap_verneed_out (bfd *abfd,
137 const Elf_Internal_Verneed *src,
138 Elf_External_Verneed *dst)
252b5132 139{
dc810e39
AM
140 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
141 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
142 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
143 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
144 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
252b5132
RH
145}
146
147/* Swap in a Vernaux structure. */
148
149void
217aa764
AM
150_bfd_elf_swap_vernaux_in (bfd *abfd,
151 const Elf_External_Vernaux *src,
152 Elf_Internal_Vernaux *dst)
252b5132 153{
dc810e39
AM
154 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
155 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
156 dst->vna_other = H_GET_16 (abfd, src->vna_other);
157 dst->vna_name = H_GET_32 (abfd, src->vna_name);
158 dst->vna_next = H_GET_32 (abfd, src->vna_next);
252b5132
RH
159}
160
161/* Swap out a Vernaux structure. */
162
163void
217aa764
AM
164_bfd_elf_swap_vernaux_out (bfd *abfd,
165 const Elf_Internal_Vernaux *src,
166 Elf_External_Vernaux *dst)
252b5132 167{
dc810e39
AM
168 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
169 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
170 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
171 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
172 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
252b5132
RH
173}
174
175/* Swap in a Versym structure. */
176
177void
217aa764
AM
178_bfd_elf_swap_versym_in (bfd *abfd,
179 const Elf_External_Versym *src,
180 Elf_Internal_Versym *dst)
252b5132 181{
dc810e39 182 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
252b5132
RH
183}
184
185/* Swap out a Versym structure. */
186
187void
217aa764
AM
188_bfd_elf_swap_versym_out (bfd *abfd,
189 const Elf_Internal_Versym *src,
190 Elf_External_Versym *dst)
252b5132 191{
dc810e39 192 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
252b5132
RH
193}
194
195/* Standard ELF hash function. Do not change this function; you will
196 cause invalid hash tables to be generated. */
3a99b017 197
252b5132 198unsigned long
217aa764 199bfd_elf_hash (const char *namearg)
252b5132 200{
3a99b017 201 const unsigned char *name = (const unsigned char *) namearg;
252b5132
RH
202 unsigned long h = 0;
203 unsigned long g;
204 int ch;
205
206 while ((ch = *name++) != '\0')
207 {
208 h = (h << 4) + ch;
209 if ((g = (h & 0xf0000000)) != 0)
210 {
211 h ^= g >> 24;
212 /* The ELF ABI says `h &= ~g', but this is equivalent in
213 this case and on some machines one insn instead of two. */
214 h ^= g;
215 }
216 }
32dfa85d 217 return h & 0xffffffff;
252b5132
RH
218}
219
fdc90cb4
JJ
220/* DT_GNU_HASH hash function. Do not change this function; you will
221 cause invalid hash tables to be generated. */
222
223unsigned long
224bfd_elf_gnu_hash (const char *namearg)
225{
226 const unsigned char *name = (const unsigned char *) namearg;
227 unsigned long h = 5381;
228 unsigned char ch;
229
230 while ((ch = *name++) != '\0')
231 h = (h << 5) + h + ch;
232 return h & 0xffffffff;
233}
234
0c8d6e5c
AM
235/* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
236 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
b34976b6 237bfd_boolean
0c8d6e5c 238bfd_elf_allocate_object (bfd *abfd,
0ffa91dd 239 size_t object_size,
4dfe6ac6 240 enum elf_target_id object_id)
252b5132 241{
0ffa91dd
NC
242 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
243 abfd->tdata.any = bfd_zalloc (abfd, object_size);
244 if (abfd->tdata.any == NULL)
245 return FALSE;
252b5132 246
0ffa91dd 247 elf_object_id (abfd) = object_id;
c0355132
AM
248 if (abfd->direction != read_direction)
249 {
250 struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
251 if (o == NULL)
252 return FALSE;
253 elf_tdata (abfd)->o = o;
254 elf_program_header_size (abfd) = (bfd_size_type) -1;
255 }
b34976b6 256 return TRUE;
252b5132
RH
257}
258
0ffa91dd
NC
259
260bfd_boolean
ae95ffa6 261bfd_elf_make_object (bfd *abfd)
0ffa91dd 262{
ae95ffa6 263 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
0ffa91dd 264 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
ae95ffa6 265 bed->target_id);
0ffa91dd
NC
266}
267
b34976b6 268bfd_boolean
217aa764 269bfd_elf_mkcorefile (bfd *abfd)
252b5132 270{
c044fabd 271 /* I think this can be done just like an object file. */
228e534f
AM
272 if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
273 return FALSE;
274 elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
275 return elf_tdata (abfd)->core != NULL;
252b5132
RH
276}
277
72a80a16 278static char *
217aa764 279bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
252b5132
RH
280{
281 Elf_Internal_Shdr **i_shdrp;
f075ee0c 282 bfd_byte *shstrtab = NULL;
dc810e39
AM
283 file_ptr offset;
284 bfd_size_type shstrtabsize;
252b5132
RH
285
286 i_shdrp = elf_elfsections (abfd);
74f2e02b
AM
287 if (i_shdrp == 0
288 || shindex >= elf_numsections (abfd)
289 || i_shdrp[shindex] == 0)
f075ee0c 290 return NULL;
252b5132 291
f075ee0c 292 shstrtab = i_shdrp[shindex]->contents;
252b5132
RH
293 if (shstrtab == NULL)
294 {
c044fabd 295 /* No cached one, attempt to read, and cache what we read. */
252b5132
RH
296 offset = i_shdrp[shindex]->sh_offset;
297 shstrtabsize = i_shdrp[shindex]->sh_size;
c6c60d09
JJ
298
299 /* Allocate and clear an extra byte at the end, to prevent crashes
300 in case the string table is not terminated. */
3471d59d 301 if (shstrtabsize + 1 <= 1
95a6d235 302 || shstrtabsize > bfd_get_file_size (abfd)
06614111
NC
303 || bfd_seek (abfd, offset, SEEK_SET) != 0
304 || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL)
c6c60d09
JJ
305 shstrtab = NULL;
306 else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
307 {
308 if (bfd_get_error () != bfd_error_system_call)
309 bfd_set_error (bfd_error_file_truncated);
06614111 310 bfd_release (abfd, shstrtab);
c6c60d09 311 shstrtab = NULL;
3471d59d
CC
312 /* Once we've failed to read it, make sure we don't keep
313 trying. Otherwise, we'll keep allocating space for
314 the string table over and over. */
315 i_shdrp[shindex]->sh_size = 0;
c6c60d09
JJ
316 }
317 else
318 shstrtab[shstrtabsize] = '\0';
217aa764 319 i_shdrp[shindex]->contents = shstrtab;
252b5132 320 }
f075ee0c 321 return (char *) shstrtab;
252b5132
RH
322}
323
324char *
217aa764
AM
325bfd_elf_string_from_elf_section (bfd *abfd,
326 unsigned int shindex,
327 unsigned int strindex)
252b5132
RH
328{
329 Elf_Internal_Shdr *hdr;
330
331 if (strindex == 0)
332 return "";
333
74f2e02b
AM
334 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
335 return NULL;
336
252b5132
RH
337 hdr = elf_elfsections (abfd)[shindex];
338
06614111
NC
339 if (hdr->contents == NULL)
340 {
341 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
342 {
343 /* PR 17512: file: f057ec89. */
695344c0 344 /* xgettext:c-format */
871b3ab2 345 _bfd_error_handler (_("%pB: attempt to load strings from"
63a5468a 346 " a non-string section (number %d)"),
06614111
NC
347 abfd, shindex);
348 return NULL;
349 }
b1fa9dd6 350
06614111
NC
351 if (bfd_elf_get_str_section (abfd, shindex) == NULL)
352 return NULL;
353 }
eed5def8
NC
354 else
355 {
356 /* PR 24273: The string section's contents may have already
357 been loaded elsewhere, eg because a corrupt file has the
358 string section index in the ELF header pointing at a group
359 section. So be paranoid, and test that the last byte of
360 the section is zero. */
361 if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
362 return NULL;
363 }
252b5132
RH
364
365 if (strindex >= hdr->sh_size)
366 {
1b3a8575 367 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
4eca0228 368 _bfd_error_handler
695344c0 369 /* xgettext:c-format */
2dcf00ce
AM
370 (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
371 abfd, strindex, (uint64_t) hdr->sh_size,
1b3a8575 372 (shindex == shstrndx && strindex == hdr->sh_name
252b5132 373 ? ".shstrtab"
1b3a8575 374 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
45b222d6 375 return NULL;
252b5132
RH
376 }
377
378 return ((char *) hdr->contents) + strindex;
379}
380
6cdc0ccc
AM
381/* Read and convert symbols to internal format.
382 SYMCOUNT specifies the number of symbols to read, starting from
383 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
384 are non-NULL, they are used to store the internal symbols, external
b7c368d0
NC
385 symbols, and symbol section index extensions, respectively.
386 Returns a pointer to the internal symbol buffer (malloced if necessary)
387 or NULL if there were no symbols or some kind of problem. */
6cdc0ccc
AM
388
389Elf_Internal_Sym *
217aa764
AM
390bfd_elf_get_elf_syms (bfd *ibfd,
391 Elf_Internal_Shdr *symtab_hdr,
392 size_t symcount,
393 size_t symoffset,
394 Elf_Internal_Sym *intsym_buf,
395 void *extsym_buf,
396 Elf_External_Sym_Shndx *extshndx_buf)
6cdc0ccc
AM
397{
398 Elf_Internal_Shdr *shndx_hdr;
217aa764 399 void *alloc_ext;
df622259 400 const bfd_byte *esym;
6cdc0ccc
AM
401 Elf_External_Sym_Shndx *alloc_extshndx;
402 Elf_External_Sym_Shndx *shndx;
4dd07732 403 Elf_Internal_Sym *alloc_intsym;
6cdc0ccc
AM
404 Elf_Internal_Sym *isym;
405 Elf_Internal_Sym *isymend;
9c5bfbb7 406 const struct elf_backend_data *bed;
6cdc0ccc
AM
407 size_t extsym_size;
408 bfd_size_type amt;
409 file_ptr pos;
410
e44a2c9c
AM
411 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
412 abort ();
413
6cdc0ccc
AM
414 if (symcount == 0)
415 return intsym_buf;
416
417 /* Normal syms might have section extension entries. */
418 shndx_hdr = NULL;
6a40cf0c
NC
419 if (elf_symtab_shndx_list (ibfd) != NULL)
420 {
421 elf_section_list * entry;
422 Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
423
424 /* Find an index section that is linked to this symtab section. */
425 for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
315350be
NC
426 {
427 /* PR 20063. */
428 if (entry->hdr.sh_link >= elf_numsections (ibfd))
429 continue;
430
431 if (sections[entry->hdr.sh_link] == symtab_hdr)
432 {
433 shndx_hdr = & entry->hdr;
434 break;
435 };
436 }
6a40cf0c
NC
437
438 if (shndx_hdr == NULL)
439 {
440 if (symtab_hdr == & elf_symtab_hdr (ibfd))
441 /* Not really accurate, but this was how the old code used to work. */
442 shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr;
443 /* Otherwise we do nothing. The assumption is that
444 the index table will not be needed. */
445 }
446 }
6cdc0ccc
AM
447
448 /* Read the symbols. */
449 alloc_ext = NULL;
450 alloc_extshndx = NULL;
4dd07732 451 alloc_intsym = NULL;
6cdc0ccc
AM
452 bed = get_elf_backend_data (ibfd);
453 extsym_size = bed->s->sizeof_sym;
ef53be89 454 amt = (bfd_size_type) symcount * extsym_size;
6cdc0ccc
AM
455 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
456 if (extsym_buf == NULL)
457 {
d0fb9a8d 458 alloc_ext = bfd_malloc2 (symcount, extsym_size);
6cdc0ccc
AM
459 extsym_buf = alloc_ext;
460 }
461 if (extsym_buf == NULL
462 || bfd_seek (ibfd, pos, SEEK_SET) != 0
463 || bfd_bread (extsym_buf, amt, ibfd) != amt)
464 {
465 intsym_buf = NULL;
466 goto out;
467 }
468
469 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
470 extshndx_buf = NULL;
471 else
472 {
ef53be89 473 amt = (bfd_size_type) symcount * sizeof (Elf_External_Sym_Shndx);
6cdc0ccc
AM
474 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
475 if (extshndx_buf == NULL)
476 {
a50b1753 477 alloc_extshndx = (Elf_External_Sym_Shndx *)
07d6d2b8 478 bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx));
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 {
a50b1753 492 alloc_intsym = (Elf_Internal_Sym *)
07d6d2b8 493 bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
4dd07732 494 intsym_buf = alloc_intsym;
6cdc0ccc
AM
495 if (intsym_buf == NULL)
496 goto out;
497 }
498
499 /* Convert the symbols to internal form. */
500 isymend = intsym_buf + symcount;
a50b1753 501 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
07d6d2b8 502 shndx = extshndx_buf;
6cdc0ccc
AM
503 isym < isymend;
504 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
8384fb8f
AM
505 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
506 {
507 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
695344c0 508 /* xgettext:c-format */
871b3ab2 509 _bfd_error_handler (_("%pB symbol number %lu references"
63a5468a 510 " nonexistent SHT_SYMTAB_SHNDX section"),
4eca0228 511 ibfd, (unsigned long) symoffset);
4dd07732
AM
512 if (alloc_intsym != NULL)
513 free (alloc_intsym);
8384fb8f
AM
514 intsym_buf = NULL;
515 goto out;
516 }
6cdc0ccc
AM
517
518 out:
519 if (alloc_ext != NULL)
520 free (alloc_ext);
521 if (alloc_extshndx != NULL)
522 free (alloc_extshndx);
523
524 return intsym_buf;
525}
526
5cab59f6
AM
527/* Look up a symbol name. */
528const char *
be8dd2ca
AM
529bfd_elf_sym_name (bfd *abfd,
530 Elf_Internal_Shdr *symtab_hdr,
26c61ae5
L
531 Elf_Internal_Sym *isym,
532 asection *sym_sec)
5cab59f6 533{
26c61ae5 534 const char *name;
5cab59f6 535 unsigned int iname = isym->st_name;
be8dd2ca 536 unsigned int shindex = symtab_hdr->sh_link;
26c61ae5 537
138f35cc
JJ
538 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
539 /* Check for a bogus st_shndx to avoid crashing. */
4fbb74a6 540 && isym->st_shndx < elf_numsections (abfd))
5cab59f6
AM
541 {
542 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
543 shindex = elf_elfheader (abfd)->e_shstrndx;
544 }
545
26c61ae5
L
546 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
547 if (name == NULL)
548 name = "(null)";
549 else if (sym_sec && *name == '\0')
550 name = bfd_section_name (abfd, sym_sec);
551
552 return name;
5cab59f6
AM
553}
554
dbb410c3
AM
555/* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
556 sections. The first element is the flags, the rest are section
557 pointers. */
558
559typedef union elf_internal_group {
560 Elf_Internal_Shdr *shdr;
561 unsigned int flags;
562} Elf_Internal_Group;
563
b885599b
AM
564/* Return the name of the group signature symbol. Why isn't the
565 signature just a string? */
566
567static const char *
217aa764 568group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
b885599b 569{
9dce4196 570 Elf_Internal_Shdr *hdr;
9dce4196
AM
571 unsigned char esym[sizeof (Elf64_External_Sym)];
572 Elf_External_Sym_Shndx eshndx;
573 Elf_Internal_Sym isym;
b885599b 574
13792e9d
L
575 /* First we need to ensure the symbol table is available. Make sure
576 that it is a symbol table section. */
4fbb74a6
AM
577 if (ghdr->sh_link >= elf_numsections (abfd))
578 return NULL;
13792e9d
L
579 hdr = elf_elfsections (abfd) [ghdr->sh_link];
580 if (hdr->sh_type != SHT_SYMTAB
581 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
b885599b
AM
582 return NULL;
583
9dce4196
AM
584 /* Go read the symbol. */
585 hdr = &elf_tdata (abfd)->symtab_hdr;
6cdc0ccc
AM
586 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
587 &isym, esym, &eshndx) == NULL)
b885599b 588 return NULL;
9dce4196 589
26c61ae5 590 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
b885599b
AM
591}
592
dbb410c3
AM
593/* Set next_in_group list pointer, and group name for NEWSECT. */
594
b34976b6 595static bfd_boolean
217aa764 596setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
dbb410c3
AM
597{
598 unsigned int num_group = elf_tdata (abfd)->num_group;
599
600 /* If num_group is zero, read in all SHT_GROUP sections. The count
601 is set to -1 if there are no SHT_GROUP sections. */
602 if (num_group == 0)
603 {
604 unsigned int i, shnum;
605
606 /* First count the number of groups. If we have a SHT_GROUP
607 section with just a flag word (ie. sh_size is 4), ignore it. */
9ad5cbcf 608 shnum = elf_numsections (abfd);
dbb410c3 609 num_group = 0;
08a40648 610
44534af3 611#define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
1783205a 612 ( (shdr)->sh_type == SHT_GROUP \
44534af3 613 && (shdr)->sh_size >= minsize \
1783205a
NC
614 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
615 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
08a40648 616
dbb410c3
AM
617 for (i = 0; i < shnum; i++)
618 {
619 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
1783205a 620
44534af3 621 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
dbb410c3
AM
622 num_group += 1;
623 }
624
625 if (num_group == 0)
20dbb49d
L
626 {
627 num_group = (unsigned) -1;
628 elf_tdata (abfd)->num_group = num_group;
ce497010 629 elf_tdata (abfd)->group_sect_ptr = NULL;
20dbb49d
L
630 }
631 else
dbb410c3
AM
632 {
633 /* We keep a list of elf section headers for group sections,
634 so we can find them quickly. */
20dbb49d 635 bfd_size_type amt;
d0fb9a8d 636
20dbb49d 637 elf_tdata (abfd)->num_group = num_group;
a50b1753 638 elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **)
07d6d2b8 639 bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
dbb410c3 640 if (elf_tdata (abfd)->group_sect_ptr == NULL)
b34976b6 641 return FALSE;
4bba0fb1
AM
642 memset (elf_tdata (abfd)->group_sect_ptr, 0,
643 num_group * sizeof (Elf_Internal_Shdr *));
dbb410c3 644 num_group = 0;
ce497010 645
dbb410c3
AM
646 for (i = 0; i < shnum; i++)
647 {
648 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
1783205a 649
44534af3 650 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
dbb410c3 651 {
973ffd63 652 unsigned char *src;
dbb410c3
AM
653 Elf_Internal_Group *dest;
654
07d6d2b8
AM
655 /* Make sure the group section has a BFD section
656 attached to it. */
657 if (!bfd_section_from_shdr (abfd, i))
658 return FALSE;
659
dbb410c3
AM
660 /* Add to list of sections. */
661 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
662 num_group += 1;
663
664 /* Read the raw contents. */
665 BFD_ASSERT (sizeof (*dest) >= 4);
666 amt = shdr->sh_size * sizeof (*dest) / 4;
a50b1753 667 shdr->contents = (unsigned char *)
eed5def8 668 bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4);
1783205a
NC
669 /* PR binutils/4110: Handle corrupt group headers. */
670 if (shdr->contents == NULL)
671 {
672 _bfd_error_handler
695344c0 673 /* xgettext:c-format */
871b3ab2 674 (_("%pB: corrupt size field in group section"
2dcf00ce
AM
675 " header: %#" PRIx64),
676 abfd, (uint64_t) shdr->sh_size);
1783205a 677 bfd_set_error (bfd_error_bad_value);
493a3386
NC
678 -- num_group;
679 continue;
1783205a
NC
680 }
681
682 memset (shdr->contents, 0, amt);
683
684 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
dbb410c3
AM
685 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
686 != shdr->sh_size))
493a3386
NC
687 {
688 _bfd_error_handler
695344c0 689 /* xgettext:c-format */
871b3ab2 690 (_("%pB: invalid size field in group section"
2dcf00ce
AM
691 " header: %#" PRIx64 ""),
692 abfd, (uint64_t) shdr->sh_size);
493a3386
NC
693 bfd_set_error (bfd_error_bad_value);
694 -- num_group;
63a5468a
AM
695 /* PR 17510: If the group contents are even
696 partially corrupt, do not allow any of the
697 contents to be used. */
493a3386
NC
698 memset (shdr->contents, 0, amt);
699 continue;
700 }
708d7d0d 701
dbb410c3
AM
702 /* Translate raw contents, a flag word followed by an
703 array of elf section indices all in target byte order,
704 to the flag word followed by an array of elf section
705 pointers. */
706 src = shdr->contents + shdr->sh_size;
707 dest = (Elf_Internal_Group *) (shdr->contents + amt);
06614111 708
dbb410c3
AM
709 while (1)
710 {
711 unsigned int idx;
712
713 src -= 4;
714 --dest;
715 idx = H_GET_32 (abfd, src);
716 if (src == shdr->contents)
717 {
718 dest->flags = idx;
b885599b
AM
719 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
720 shdr->bfd_section->flags
721 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
dbb410c3
AM
722 break;
723 }
4bba0fb1 724 if (idx < shnum)
bae363f1
L
725 {
726 dest->shdr = elf_elfsections (abfd)[idx];
727 /* PR binutils/23199: All sections in a
728 section group should be marked with
729 SHF_GROUP. But some tools generate
730 broken objects without SHF_GROUP. Fix
731 them up here. */
732 dest->shdr->sh_flags |= SHF_GROUP;
733 }
4bba0fb1
AM
734 if (idx >= shnum
735 || dest->shdr->sh_type == SHT_GROUP)
dbb410c3 736 {
4eca0228 737 _bfd_error_handler
4bba0fb1
AM
738 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
739 abfd, i);
740 dest->shdr = NULL;
dbb410c3 741 }
dbb410c3
AM
742 }
743 }
744 }
493a3386
NC
745
746 /* PR 17510: Corrupt binaries might contain invalid groups. */
747 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
748 {
749 elf_tdata (abfd)->num_group = num_group;
750
751 /* If all groups are invalid then fail. */
752 if (num_group == 0)
753 {
754 elf_tdata (abfd)->group_sect_ptr = NULL;
755 elf_tdata (abfd)->num_group = num_group = -1;
4eca0228 756 _bfd_error_handler
871b3ab2 757 (_("%pB: no valid group sections found"), abfd);
493a3386
NC
758 bfd_set_error (bfd_error_bad_value);
759 }
760 }
dbb410c3
AM
761 }
762 }
763
764 if (num_group != (unsigned) -1)
765 {
564e11c9
JW
766 unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
767 unsigned int j;
dbb410c3 768
564e11c9 769 for (j = 0; j < num_group; j++)
dbb410c3 770 {
564e11c9
JW
771 /* Begin search from previous found group. */
772 unsigned i = (j + search_offset) % num_group;
773
dbb410c3 774 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
ce497010 775 Elf_Internal_Group *idx;
0c54f692 776 bfd_size_type n_elt;
ce497010
NC
777
778 if (shdr == NULL)
779 continue;
780
781 idx = (Elf_Internal_Group *) shdr->contents;
0c54f692
NC
782 if (idx == NULL || shdr->sh_size < 4)
783 {
784 /* See PR 21957 for a reproducer. */
785 /* xgettext:c-format */
871b3ab2 786 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
0c54f692
NC
787 abfd, shdr->bfd_section);
788 elf_tdata (abfd)->group_sect_ptr[i] = NULL;
789 bfd_set_error (bfd_error_bad_value);
790 return FALSE;
791 }
ce497010 792 n_elt = shdr->sh_size / 4;
dbb410c3
AM
793
794 /* Look through this group's sections to see if current
795 section is a member. */
796 while (--n_elt != 0)
797 if ((++idx)->shdr == hdr)
798 {
e0e8c97f 799 asection *s = NULL;
dbb410c3
AM
800
801 /* We are a member of this group. Go looking through
802 other members to see if any others are linked via
803 next_in_group. */
804 idx = (Elf_Internal_Group *) shdr->contents;
805 n_elt = shdr->sh_size / 4;
806 while (--n_elt != 0)
4bba0fb1
AM
807 if ((++idx)->shdr != NULL
808 && (s = idx->shdr->bfd_section) != NULL
945906ff 809 && elf_next_in_group (s) != NULL)
dbb410c3
AM
810 break;
811 if (n_elt != 0)
812 {
dbb410c3
AM
813 /* Snarf the group name from other member, and
814 insert current section in circular list. */
945906ff
AM
815 elf_group_name (newsect) = elf_group_name (s);
816 elf_next_in_group (newsect) = elf_next_in_group (s);
817 elf_next_in_group (s) = newsect;
dbb410c3
AM
818 }
819 else
820 {
dbb410c3
AM
821 const char *gname;
822
b885599b
AM
823 gname = group_signature (abfd, shdr);
824 if (gname == NULL)
b34976b6 825 return FALSE;
945906ff 826 elf_group_name (newsect) = gname;
dbb410c3
AM
827
828 /* Start a circular list with one element. */
945906ff 829 elf_next_in_group (newsect) = newsect;
dbb410c3 830 }
b885599b 831
9dce4196
AM
832 /* If the group section has been created, point to the
833 new member. */
dbb410c3 834 if (shdr->bfd_section != NULL)
945906ff 835 elf_next_in_group (shdr->bfd_section) = newsect;
b885599b 836
564e11c9
JW
837 elf_tdata (abfd)->group_search_offset = i;
838 j = num_group - 1;
dbb410c3
AM
839 break;
840 }
841 }
842 }
843
945906ff 844 if (elf_group_name (newsect) == NULL)
dbb410c3 845 {
695344c0 846 /* xgettext:c-format */
871b3ab2 847 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
4eca0228 848 abfd, newsect);
493a3386 849 return FALSE;
dbb410c3 850 }
b34976b6 851 return TRUE;
dbb410c3
AM
852}
853
3d7f7666 854bfd_boolean
dd863624 855_bfd_elf_setup_sections (bfd *abfd)
3d7f7666
L
856{
857 unsigned int i;
858 unsigned int num_group = elf_tdata (abfd)->num_group;
859 bfd_boolean result = TRUE;
dd863624
L
860 asection *s;
861
862 /* Process SHF_LINK_ORDER. */
863 for (s = abfd->sections; s != NULL; s = s->next)
864 {
865 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
866 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
867 {
868 unsigned int elfsec = this_hdr->sh_link;
869 /* FIXME: The old Intel compiler and old strip/objcopy may
870 not set the sh_link or sh_info fields. Hence we could
871 get the situation where elfsec is 0. */
872 if (elfsec == 0)
873 {
4fbb74a6 874 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
dd863624
L
875 if (bed->link_order_error_handler)
876 bed->link_order_error_handler
695344c0 877 /* xgettext:c-format */
871b3ab2 878 (_("%pB: warning: sh_link not set for section `%pA'"),
dd863624
L
879 abfd, s);
880 }
881 else
882 {
91d6fa6a 883 asection *linksec = NULL;
25bbc984 884
4fbb74a6
AM
885 if (elfsec < elf_numsections (abfd))
886 {
887 this_hdr = elf_elfsections (abfd)[elfsec];
91d6fa6a 888 linksec = this_hdr->bfd_section;
4fbb74a6 889 }
25bbc984
L
890
891 /* PR 1991, 2008:
892 Some strip/objcopy may leave an incorrect value in
893 sh_link. We don't want to proceed. */
91d6fa6a 894 if (linksec == NULL)
25bbc984 895 {
4eca0228 896 _bfd_error_handler
695344c0 897 /* xgettext:c-format */
871b3ab2 898 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
c08bb8dd 899 s->owner, elfsec, s);
25bbc984
L
900 result = FALSE;
901 }
902
91d6fa6a 903 elf_linked_to_section (s) = linksec;
dd863624
L
904 }
905 }
53720c49
AM
906 else if (this_hdr->sh_type == SHT_GROUP
907 && elf_next_in_group (s) == NULL)
908 {
4eca0228 909 _bfd_error_handler
695344c0 910 /* xgettext:c-format */
871b3ab2 911 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
53720c49
AM
912 abfd, elf_section_data (s)->this_idx);
913 result = FALSE;
914 }
dd863624 915 }
3d7f7666 916
dd863624 917 /* Process section groups. */
3d7f7666
L
918 if (num_group == (unsigned) -1)
919 return result;
920
921 for (i = 0; i < num_group; i++)
922 {
923 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
4b0e8a5f
NC
924 Elf_Internal_Group *idx;
925 unsigned int n_elt;
3d7f7666 926
4b0e8a5f
NC
927 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
928 if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
929 {
4eca0228 930 _bfd_error_handler
695344c0 931 /* xgettext:c-format */
871b3ab2 932 (_("%pB: section group entry number %u is corrupt"),
4b0e8a5f
NC
933 abfd, i);
934 result = FALSE;
935 continue;
936 }
937
938 idx = (Elf_Internal_Group *) shdr->contents;
939 n_elt = shdr->sh_size / 4;
1b786873 940
3d7f7666 941 while (--n_elt != 0)
24d3e51b
NC
942 {
943 ++ idx;
944
945 if (idx->shdr == NULL)
946 continue;
947 else if (idx->shdr->bfd_section)
948 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
db4677b8
AM
949 else if (idx->shdr->sh_type != SHT_RELA
950 && idx->shdr->sh_type != SHT_REL)
24d3e51b
NC
951 {
952 /* There are some unknown sections in the group. */
953 _bfd_error_handler
954 /* xgettext:c-format */
871b3ab2 955 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
24d3e51b
NC
956 abfd,
957 idx->shdr->sh_type,
958 bfd_elf_string_from_elf_section (abfd,
959 (elf_elfheader (abfd)
960 ->e_shstrndx),
961 idx->shdr->sh_name),
962 shdr->bfd_section);
963 result = FALSE;
964 }
965 }
3d7f7666 966 }
24d3e51b 967
3d7f7666
L
968 return result;
969}
970
72adc230
AM
971bfd_boolean
972bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
973{
974 return elf_next_in_group (sec) != NULL;
975}
976
f6fe1ccd
L
977static char *
978convert_debug_to_zdebug (bfd *abfd, const char *name)
979{
980 unsigned int len = strlen (name);
981 char *new_name = bfd_alloc (abfd, len + 2);
982 if (new_name == NULL)
983 return NULL;
984 new_name[0] = '.';
985 new_name[1] = 'z';
986 memcpy (new_name + 2, name + 1, len);
987 return new_name;
988}
989
990static char *
991convert_zdebug_to_debug (bfd *abfd, const char *name)
992{
993 unsigned int len = strlen (name);
994 char *new_name = bfd_alloc (abfd, len);
995 if (new_name == NULL)
996 return NULL;
997 new_name[0] = '.';
998 memcpy (new_name + 1, name + 2, len - 1);
999 return new_name;
1000}
1001
252b5132
RH
1002/* Make a BFD section from an ELF section. We store a pointer to the
1003 BFD section in the bfd_section field of the header. */
1004
b34976b6 1005bfd_boolean
217aa764
AM
1006_bfd_elf_make_section_from_shdr (bfd *abfd,
1007 Elf_Internal_Shdr *hdr,
6dc132d9
L
1008 const char *name,
1009 int shindex)
252b5132
RH
1010{
1011 asection *newsect;
1012 flagword flags;
9c5bfbb7 1013 const struct elf_backend_data *bed;
252b5132
RH
1014
1015 if (hdr->bfd_section != NULL)
4e011fb5 1016 return TRUE;
252b5132
RH
1017
1018 newsect = bfd_make_section_anyway (abfd, name);
1019 if (newsect == NULL)
b34976b6 1020 return FALSE;
252b5132 1021
1829f4b2
AM
1022 hdr->bfd_section = newsect;
1023 elf_section_data (newsect)->this_hdr = *hdr;
6dc132d9 1024 elf_section_data (newsect)->this_idx = shindex;
1829f4b2 1025
2f89ff8d
L
1026 /* Always use the real type/flags. */
1027 elf_section_type (newsect) = hdr->sh_type;
1028 elf_section_flags (newsect) = hdr->sh_flags;
1029
252b5132
RH
1030 newsect->filepos = hdr->sh_offset;
1031
1032 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
1033 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
1034 || ! bfd_set_section_alignment (abfd, newsect,
72de5009 1035 bfd_log2 (hdr->sh_addralign)))
b34976b6 1036 return FALSE;
252b5132
RH
1037
1038 flags = SEC_NO_FLAGS;
1039 if (hdr->sh_type != SHT_NOBITS)
1040 flags |= SEC_HAS_CONTENTS;
dbb410c3 1041 if (hdr->sh_type == SHT_GROUP)
7bdf4127 1042 flags |= SEC_GROUP;
252b5132
RH
1043 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1044 {
1045 flags |= SEC_ALLOC;
1046 if (hdr->sh_type != SHT_NOBITS)
1047 flags |= SEC_LOAD;
1048 }
1049 if ((hdr->sh_flags & SHF_WRITE) == 0)
1050 flags |= SEC_READONLY;
1051 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1052 flags |= SEC_CODE;
1053 else if ((flags & SEC_LOAD) != 0)
1054 flags |= SEC_DATA;
f5fa8ca2
JJ
1055 if ((hdr->sh_flags & SHF_MERGE) != 0)
1056 {
1057 flags |= SEC_MERGE;
1058 newsect->entsize = hdr->sh_entsize;
f5fa8ca2 1059 }
84865015
NC
1060 if ((hdr->sh_flags & SHF_STRINGS) != 0)
1061 flags |= SEC_STRINGS;
dbb410c3
AM
1062 if (hdr->sh_flags & SHF_GROUP)
1063 if (!setup_group (abfd, hdr, newsect))
b34976b6 1064 return FALSE;
13ae64f3
JJ
1065 if ((hdr->sh_flags & SHF_TLS) != 0)
1066 flags |= SEC_THREAD_LOCAL;
18ae9cc1
L
1067 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1068 flags |= SEC_EXCLUDE;
252b5132 1069
3d2b39cf 1070 if ((flags & SEC_ALLOC) == 0)
7a6cc5fb 1071 {
3d2b39cf
L
1072 /* The debugging sections appear to be recognized only by name,
1073 not any sort of flag. Their SEC_ALLOC bits are cleared. */
3d2b39cf
L
1074 if (name [0] == '.')
1075 {
f073ced3
AM
1076 const char *p;
1077 int n;
1078 if (name[1] == 'd')
1079 p = ".debug", n = 6;
1080 else if (name[1] == 'g' && name[2] == 'n')
1081 p = ".gnu.linkonce.wi.", n = 17;
1082 else if (name[1] == 'g' && name[2] == 'd')
1083 p = ".gdb_index", n = 11; /* yes we really do mean 11. */
1084 else if (name[1] == 'l')
1085 p = ".line", n = 5;
1086 else if (name[1] == 's')
1087 p = ".stab", n = 5;
1088 else if (name[1] == 'z')
1089 p = ".zdebug", n = 7;
1090 else
1091 p = NULL, n = 0;
1092 if (p != NULL && strncmp (name, p, n) == 0)
3d2b39cf
L
1093 flags |= SEC_DEBUGGING;
1094 }
1095 }
252b5132
RH
1096
1097 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1098 only link a single copy of the section. This is used to support
1099 g++. g++ will emit each template expansion in its own section.
1100 The symbols will be defined as weak, so that multiple definitions
1101 are permitted. The GNU linker extension is to actually discard
1102 all but one of the sections. */
0112cd26 1103 if (CONST_STRNEQ (name, ".gnu.linkonce")
b885599b 1104 && elf_next_in_group (newsect) == NULL)
252b5132
RH
1105 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1106
fa152c49
JW
1107 bed = get_elf_backend_data (abfd);
1108 if (bed->elf_backend_section_flags)
1109 if (! bed->elf_backend_section_flags (&flags, hdr))
b34976b6 1110 return FALSE;
fa152c49 1111
252b5132 1112 if (! bfd_set_section_flags (abfd, newsect, flags))
b34976b6 1113 return FALSE;
252b5132 1114
718175fa
JK
1115 /* We do not parse the PT_NOTE segments as we are interested even in the
1116 separate debug info files which may have the segments offsets corrupted.
1117 PT_NOTEs from the core files are currently not parsed using BFD. */
1118 if (hdr->sh_type == SHT_NOTE)
1119 {
baea7ef1 1120 bfd_byte *contents;
718175fa 1121
baea7ef1 1122 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
718175fa
JK
1123 return FALSE;
1124
276da9b3
L
1125 elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1126 hdr->sh_offset, hdr->sh_addralign);
718175fa
JK
1127 free (contents);
1128 }
1129
252b5132
RH
1130 if ((flags & SEC_ALLOC) != 0)
1131 {
1132 Elf_Internal_Phdr *phdr;
6ffd7900
AM
1133 unsigned int i, nload;
1134
1135 /* Some ELF linkers produce binaries with all the program header
1136 p_paddr fields zero. If we have such a binary with more than
1137 one PT_LOAD header, then leave the section lma equal to vma
1138 so that we don't create sections with overlapping lma. */
1139 phdr = elf_tdata (abfd)->phdr;
1140 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1141 if (phdr->p_paddr != 0)
1142 break;
1143 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1144 ++nload;
1145 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1146 return TRUE;
252b5132 1147
252b5132
RH
1148 phdr = elf_tdata (abfd)->phdr;
1149 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1150 {
86b2281f
AM
1151 if (((phdr->p_type == PT_LOAD
1152 && (hdr->sh_flags & SHF_TLS) == 0)
1153 || phdr->p_type == PT_TLS)
9a83a553 1154 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
252b5132 1155 {
88967714
AM
1156 if ((flags & SEC_LOAD) == 0)
1157 newsect->lma = (phdr->p_paddr
1158 + hdr->sh_addr - phdr->p_vaddr);
1159 else
1160 /* We used to use the same adjustment for SEC_LOAD
1161 sections, but that doesn't work if the segment
1162 is packed with code from multiple VMAs.
1163 Instead we calculate the section LMA based on
1164 the segment LMA. It is assumed that the
1165 segment will contain sections with contiguous
1166 LMAs, even if the VMAs are not. */
1167 newsect->lma = (phdr->p_paddr
1168 + hdr->sh_offset - phdr->p_offset);
1169
1170 /* With contiguous segments, we can't tell from file
1171 offsets whether a section with zero size should
1172 be placed at the end of one segment or the
1173 beginning of the next. Decide based on vaddr. */
1174 if (hdr->sh_addr >= phdr->p_vaddr
1175 && (hdr->sh_addr + hdr->sh_size
1176 <= phdr->p_vaddr + phdr->p_memsz))
1177 break;
252b5132
RH
1178 }
1179 }
1180 }
1181
4a114e3e
L
1182 /* Compress/decompress DWARF debug sections with names: .debug_* and
1183 .zdebug_*, after the section flags is set. */
1184 if ((flags & SEC_DEBUGGING)
1185 && ((name[1] == 'd' && name[6] == '_')
1186 || (name[1] == 'z' && name[7] == '_')))
1187 {
1188 enum { nothing, compress, decompress } action = nothing;
151411f8 1189 int compression_header_size;
dab394de 1190 bfd_size_type uncompressed_size;
4207142d 1191 unsigned int uncompressed_align_power;
151411f8
L
1192 bfd_boolean compressed
1193 = bfd_is_section_compressed_with_header (abfd, newsect,
dab394de 1194 &compression_header_size,
4207142d
MW
1195 &uncompressed_size,
1196 &uncompressed_align_power);
151411f8 1197 if (compressed)
4a114e3e
L
1198 {
1199 /* Compressed section. Check if we should decompress. */
1200 if ((abfd->flags & BFD_DECOMPRESS))
1201 action = decompress;
1202 }
151411f8
L
1203
1204 /* Compress the uncompressed section or convert from/to .zdebug*
1205 section. Check if we should compress. */
1206 if (action == nothing)
4a114e3e 1207 {
151411f8
L
1208 if (newsect->size != 0
1209 && (abfd->flags & BFD_COMPRESS)
1210 && compression_header_size >= 0
dab394de 1211 && uncompressed_size > 0
151411f8
L
1212 && (!compressed
1213 || ((compression_header_size > 0)
1214 != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
4a114e3e 1215 action = compress;
151411f8
L
1216 else
1217 return TRUE;
4a114e3e
L
1218 }
1219
151411f8 1220 if (action == compress)
4a114e3e 1221 {
4a114e3e
L
1222 if (!bfd_init_section_compress_status (abfd, newsect))
1223 {
4eca0228 1224 _bfd_error_handler
695344c0 1225 /* xgettext:c-format */
871b3ab2 1226 (_("%pB: unable to initialize compress status for section %s"),
4a114e3e
L
1227 abfd, name);
1228 return FALSE;
1229 }
151411f8
L
1230 }
1231 else
1232 {
4a114e3e
L
1233 if (!bfd_init_section_decompress_status (abfd, newsect))
1234 {
4eca0228 1235 _bfd_error_handler
695344c0 1236 /* xgettext:c-format */
871b3ab2 1237 (_("%pB: unable to initialize decompress status for section %s"),
4a114e3e
L
1238 abfd, name);
1239 return FALSE;
1240 }
151411f8
L
1241 }
1242
f6fe1ccd 1243 if (abfd->is_linker_input)
151411f8 1244 {
f6fe1ccd
L
1245 if (name[1] == 'z'
1246 && (action == decompress
1247 || (action == compress
1248 && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
4e011fb5 1249 {
f6fe1ccd
L
1250 /* Convert section name from .zdebug_* to .debug_* so
1251 that linker will consider this section as a debug
1252 section. */
1253 char *new_name = convert_zdebug_to_debug (abfd, name);
151411f8
L
1254 if (new_name == NULL)
1255 return FALSE;
f6fe1ccd 1256 bfd_rename_section (abfd, newsect, new_name);
151411f8 1257 }
4a114e3e 1258 }
f6fe1ccd
L
1259 else
1260 /* For objdump, don't rename the section. For objcopy, delay
1261 section rename to elf_fake_sections. */
1262 newsect->flags |= SEC_ELF_RENAME;
4a114e3e
L
1263 }
1264
b34976b6 1265 return TRUE;
252b5132
RH
1266}
1267
84865015
NC
1268const char *const bfd_elf_section_type_names[] =
1269{
252b5132
RH
1270 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1271 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1272 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1273};
1274
1049f94e 1275/* ELF relocs are against symbols. If we are producing relocatable
252b5132
RH
1276 output, and the reloc is against an external symbol, and nothing
1277 has given us any additional addend, the resulting reloc will also
1278 be against the same symbol. In such a case, we don't want to
1279 change anything about the way the reloc is handled, since it will
1280 all be done at final link time. Rather than put special case code
1281 into bfd_perform_relocation, all the reloc types use this howto
1282 function. It just short circuits the reloc if producing
1049f94e 1283 relocatable output against an external symbol. */
252b5132 1284
252b5132 1285bfd_reloc_status_type
217aa764
AM
1286bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1287 arelent *reloc_entry,
1288 asymbol *symbol,
1289 void *data ATTRIBUTE_UNUSED,
1290 asection *input_section,
1291 bfd *output_bfd,
1292 char **error_message ATTRIBUTE_UNUSED)
1293{
1294 if (output_bfd != NULL
252b5132
RH
1295 && (symbol->flags & BSF_SECTION_SYM) == 0
1296 && (! reloc_entry->howto->partial_inplace
1297 || reloc_entry->addend == 0))
1298 {
1299 reloc_entry->address += input_section->output_offset;
1300 return bfd_reloc_ok;
1301 }
1302
1303 return bfd_reloc_continue;
1304}
1305\f
84865015
NC
1306/* Returns TRUE if section A matches section B.
1307 Names, addresses and links may be different, but everything else
1308 should be the same. */
1309
1310static bfd_boolean
5522f910
NC
1311section_match (const Elf_Internal_Shdr * a,
1312 const Elf_Internal_Shdr * b)
84865015 1313{
ac85e67c
AM
1314 if (a->sh_type != b->sh_type
1315 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1316 || a->sh_addralign != b->sh_addralign
1317 || a->sh_entsize != b->sh_entsize)
1318 return FALSE;
1319 if (a->sh_type == SHT_SYMTAB
1320 || a->sh_type == SHT_STRTAB)
1321 return TRUE;
1322 return a->sh_size == b->sh_size;
84865015
NC
1323}
1324
1325/* Find a section in OBFD that has the same characteristics
1326 as IHEADER. Return the index of this section or SHN_UNDEF if
1327 none can be found. Check's section HINT first, as this is likely
1328 to be the correct section. */
1329
1330static unsigned int
5cc4ca83
ST
1331find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1332 const unsigned int hint)
84865015
NC
1333{
1334 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1335 unsigned int i;
1336
a55c9876
NC
1337 BFD_ASSERT (iheader != NULL);
1338
1339 /* See PR 20922 for a reproducer of the NULL test. */
5cc4ca83
ST
1340 if (hint < elf_numsections (obfd)
1341 && oheaders[hint] != NULL
a55c9876 1342 && section_match (oheaders[hint], iheader))
84865015
NC
1343 return hint;
1344
1345 for (i = 1; i < elf_numsections (obfd); i++)
1346 {
1347 Elf_Internal_Shdr * oheader = oheaders[i];
1348
a55c9876
NC
1349 if (oheader == NULL)
1350 continue;
84865015
NC
1351 if (section_match (oheader, iheader))
1352 /* FIXME: Do we care if there is a potential for
1353 multiple matches ? */
1354 return i;
1355 }
1356
1357 return SHN_UNDEF;
1358}
1359
5522f910
NC
1360/* PR 19938: Attempt to set the ELF section header fields of an OS or
1361 Processor specific section, based upon a matching input section.
1362 Returns TRUE upon success, FALSE otherwise. */
07d6d2b8 1363
5522f910
NC
1364static bfd_boolean
1365copy_special_section_fields (const bfd *ibfd,
1366 bfd *obfd,
1367 const Elf_Internal_Shdr *iheader,
1368 Elf_Internal_Shdr *oheader,
1369 const unsigned int secnum)
1370{
1371 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1372 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1373 bfd_boolean changed = FALSE;
1374 unsigned int sh_link;
1375
1376 if (oheader->sh_type == SHT_NOBITS)
1377 {
1378 /* This is a feature for objcopy --only-keep-debug:
1379 When a section's type is changed to NOBITS, we preserve
1380 the sh_link and sh_info fields so that they can be
1381 matched up with the original.
1382
1383 Note: Strictly speaking these assignments are wrong.
1384 The sh_link and sh_info fields should point to the
1385 relevent sections in the output BFD, which may not be in
1386 the same location as they were in the input BFD. But
1387 the whole point of this action is to preserve the
1388 original values of the sh_link and sh_info fields, so
1389 that they can be matched up with the section headers in
1390 the original file. So strictly speaking we may be
1391 creating an invalid ELF file, but it is only for a file
1392 that just contains debug info and only for sections
1393 without any contents. */
1394 if (oheader->sh_link == 0)
1395 oheader->sh_link = iheader->sh_link;
1396 if (oheader->sh_info == 0)
1397 oheader->sh_info = iheader->sh_info;
1398 return TRUE;
1399 }
1400
1401 /* Allow the target a chance to decide how these fields should be set. */
1402 if (bed->elf_backend_copy_special_section_fields != NULL
1403 && bed->elf_backend_copy_special_section_fields
1404 (ibfd, obfd, iheader, oheader))
1405 return TRUE;
1406
1407 /* We have an iheader which might match oheader, and which has non-zero
1408 sh_info and/or sh_link fields. Attempt to follow those links and find
1409 the section in the output bfd which corresponds to the linked section
1410 in the input bfd. */
1411 if (iheader->sh_link != SHN_UNDEF)
1412 {
4f3ca05b
NC
1413 /* See PR 20931 for a reproducer. */
1414 if (iheader->sh_link >= elf_numsections (ibfd))
1415 {
76cfced5 1416 _bfd_error_handler
4f3ca05b 1417 /* xgettext:c-format */
9793eb77 1418 (_("%pB: invalid sh_link field (%d) in section number %d"),
4f3ca05b
NC
1419 ibfd, iheader->sh_link, secnum);
1420 return FALSE;
1421 }
1422
5522f910
NC
1423 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1424 if (sh_link != SHN_UNDEF)
1425 {
1426 oheader->sh_link = sh_link;
1427 changed = TRUE;
1428 }
1429 else
1430 /* FIXME: Should we install iheader->sh_link
1431 if we could not find a match ? */
76cfced5 1432 _bfd_error_handler
695344c0 1433 /* xgettext:c-format */
9793eb77 1434 (_("%pB: failed to find link section for section %d"), obfd, secnum);
5522f910
NC
1435 }
1436
1437 if (iheader->sh_info)
1438 {
1439 /* The sh_info field can hold arbitrary information, but if the
1440 SHF_LINK_INFO flag is set then it should be interpreted as a
1441 section index. */
1442 if (iheader->sh_flags & SHF_INFO_LINK)
1443 {
1444 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1445 iheader->sh_info);
1446 if (sh_link != SHN_UNDEF)
1447 oheader->sh_flags |= SHF_INFO_LINK;
1448 }
1449 else
1450 /* No idea what it means - just copy it. */
1451 sh_link = iheader->sh_info;
1452
1453 if (sh_link != SHN_UNDEF)
1454 {
1455 oheader->sh_info = sh_link;
1456 changed = TRUE;
1457 }
1458 else
76cfced5 1459 _bfd_error_handler
695344c0 1460 /* xgettext:c-format */
9793eb77 1461 (_("%pB: failed to find info section for section %d"), obfd, secnum);
5522f910
NC
1462 }
1463
1464 return changed;
1465}
07d6d2b8 1466
0ac4564e
L
1467/* Copy the program header and other data from one object module to
1468 another. */
252b5132 1469
b34976b6 1470bfd_boolean
217aa764 1471_bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
2d502050 1472{
5522f910
NC
1473 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1474 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1475 const struct elf_backend_data *bed;
84865015
NC
1476 unsigned int i;
1477
2d502050 1478 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
84865015 1479 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 1480 return TRUE;
2d502050 1481
57b828ef
L
1482 if (!elf_flags_init (obfd))
1483 {
1484 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1485 elf_flags_init (obfd) = TRUE;
1486 }
2d502050 1487
0ac4564e 1488 elf_gp (obfd) = elf_gp (ibfd);
57b828ef
L
1489
1490 /* Also copy the EI_OSABI field. */
1491 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1492 elf_elfheader (ibfd)->e_ident[EI_OSABI];
104d59d1 1493
5522f910
NC
1494 /* If set, copy the EI_ABIVERSION field. */
1495 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1496 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1497 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
07d6d2b8 1498
104d59d1
JM
1499 /* Copy object attributes. */
1500 _bfd_elf_copy_obj_attributes (ibfd, obfd);
63b9bbb7 1501
84865015
NC
1502 if (iheaders == NULL || oheaders == NULL)
1503 return TRUE;
63b9bbb7 1504
5522f910
NC
1505 bed = get_elf_backend_data (obfd);
1506
1507 /* Possibly copy other fields in the section header. */
84865015 1508 for (i = 1; i < elf_numsections (obfd); i++)
63b9bbb7 1509 {
84865015
NC
1510 unsigned int j;
1511 Elf_Internal_Shdr * oheader = oheaders[i];
63b9bbb7 1512
5522f910
NC
1513 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1514 because of a special case need for generating separate debug info
1515 files. See below for more details. */
84865015
NC
1516 if (oheader == NULL
1517 || (oheader->sh_type != SHT_NOBITS
5522f910
NC
1518 && oheader->sh_type < SHT_LOOS))
1519 continue;
1520
1521 /* Ignore empty sections, and sections whose
1522 fields have already been initialised. */
1523 if (oheader->sh_size == 0
84865015
NC
1524 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1525 continue;
63b9bbb7 1526
84865015 1527 /* Scan for the matching section in the input bfd.
5522f910
NC
1528 First we try for a direct mapping between the input and output sections. */
1529 for (j = 1; j < elf_numsections (ibfd); j++)
1530 {
1531 const Elf_Internal_Shdr * iheader = iheaders[j];
1532
1533 if (iheader == NULL)
1534 continue;
1535
1536 if (oheader->bfd_section != NULL
1537 && iheader->bfd_section != NULL
1538 && iheader->bfd_section->output_section != NULL
1539 && iheader->bfd_section->output_section == oheader->bfd_section)
1540 {
1541 /* We have found a connection from the input section to the
1542 output section. Attempt to copy the header fields. If
1543 this fails then do not try any further sections - there
1544 should only be a one-to-one mapping between input and output. */
1545 if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1546 j = elf_numsections (ibfd);
1547 break;
1548 }
1549 }
1550
1551 if (j < elf_numsections (ibfd))
1552 continue;
1553
1554 /* That failed. So try to deduce the corresponding input section.
84865015
NC
1555 Unfortunately we cannot compare names as the output string table
1556 is empty, so instead we check size, address and type. */
1557 for (j = 1; j < elf_numsections (ibfd); j++)
1558 {
5522f910 1559 const Elf_Internal_Shdr * iheader = iheaders[j];
84865015 1560
5522f910
NC
1561 if (iheader == NULL)
1562 continue;
1563
1564 /* Try matching fields in the input section's header.
1565 Since --only-keep-debug turns all non-debug sections into
84865015
NC
1566 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1567 input type. */
1568 if ((oheader->sh_type == SHT_NOBITS
1569 || iheader->sh_type == oheader->sh_type)
5522f910
NC
1570 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1571 == (oheader->sh_flags & ~ SHF_INFO_LINK)
84865015
NC
1572 && iheader->sh_addralign == oheader->sh_addralign
1573 && iheader->sh_entsize == oheader->sh_entsize
1574 && iheader->sh_size == oheader->sh_size
1575 && iheader->sh_addr == oheader->sh_addr
1576 && (iheader->sh_info != oheader->sh_info
1577 || iheader->sh_link != oheader->sh_link))
63b9bbb7 1578 {
5522f910
NC
1579 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1580 break;
63b9bbb7
NC
1581 }
1582 }
5522f910
NC
1583
1584 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1585 {
1586 /* Final attempt. Call the backend copy function
1587 with a NULL input section. */
1588 if (bed->elf_backend_copy_special_section_fields != NULL)
1589 bed->elf_backend_copy_special_section_fields (ibfd, obfd, NULL, oheader);
1590 }
63b9bbb7
NC
1591 }
1592
b34976b6 1593 return TRUE;
2d502050
L
1594}
1595
cedc298e
L
1596static const char *
1597get_segment_type (unsigned int p_type)
1598{
1599 const char *pt;
1600 switch (p_type)
1601 {
1602 case PT_NULL: pt = "NULL"; break;
1603 case PT_LOAD: pt = "LOAD"; break;
1604 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1605 case PT_INTERP: pt = "INTERP"; break;
1606 case PT_NOTE: pt = "NOTE"; break;
1607 case PT_SHLIB: pt = "SHLIB"; break;
1608 case PT_PHDR: pt = "PHDR"; break;
1609 case PT_TLS: pt = "TLS"; break;
1610 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
2b05f1b7 1611 case PT_GNU_STACK: pt = "STACK"; break;
cedc298e
L
1612 case PT_GNU_RELRO: pt = "RELRO"; break;
1613 default: pt = NULL; break;
1614 }
1615 return pt;
1616}
1617
f0b79d91
L
1618/* Print out the program headers. */
1619
b34976b6 1620bfd_boolean
217aa764 1621_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
252b5132 1622{
a50b1753 1623 FILE *f = (FILE *) farg;
252b5132
RH
1624 Elf_Internal_Phdr *p;
1625 asection *s;
1626 bfd_byte *dynbuf = NULL;
1627
1628 p = elf_tdata (abfd)->phdr;
1629 if (p != NULL)
1630 {
1631 unsigned int i, c;
1632
1633 fprintf (f, _("\nProgram Header:\n"));
1634 c = elf_elfheader (abfd)->e_phnum;
1635 for (i = 0; i < c; i++, p++)
1636 {
cedc298e 1637 const char *pt = get_segment_type (p->p_type);
252b5132
RH
1638 char buf[20];
1639
cedc298e 1640 if (pt == NULL)
252b5132 1641 {
cedc298e
L
1642 sprintf (buf, "0x%lx", p->p_type);
1643 pt = buf;
252b5132 1644 }
dc810e39 1645 fprintf (f, "%8s off 0x", pt);
60b89a18 1646 bfd_fprintf_vma (abfd, f, p->p_offset);
252b5132 1647 fprintf (f, " vaddr 0x");
60b89a18 1648 bfd_fprintf_vma (abfd, f, p->p_vaddr);
252b5132 1649 fprintf (f, " paddr 0x");
60b89a18 1650 bfd_fprintf_vma (abfd, f, p->p_paddr);
252b5132
RH
1651 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1652 fprintf (f, " filesz 0x");
60b89a18 1653 bfd_fprintf_vma (abfd, f, p->p_filesz);
252b5132 1654 fprintf (f, " memsz 0x");
60b89a18 1655 bfd_fprintf_vma (abfd, f, p->p_memsz);
252b5132
RH
1656 fprintf (f, " flags %c%c%c",
1657 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1658 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1659 (p->p_flags & PF_X) != 0 ? 'x' : '-');
dc810e39
AM
1660 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1661 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
252b5132
RH
1662 fprintf (f, "\n");
1663 }
1664 }
1665
1666 s = bfd_get_section_by_name (abfd, ".dynamic");
1667 if (s != NULL)
1668 {
cb33740c 1669 unsigned int elfsec;
dc810e39 1670 unsigned long shlink;
252b5132
RH
1671 bfd_byte *extdyn, *extdynend;
1672 size_t extdynsize;
217aa764 1673 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
252b5132
RH
1674
1675 fprintf (f, _("\nDynamic Section:\n"));
1676
eea6121a 1677 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
252b5132
RH
1678 goto error_return;
1679
1680 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 1681 if (elfsec == SHN_BAD)
252b5132 1682 goto error_return;
dc810e39 1683 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
252b5132
RH
1684
1685 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1686 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1687
1688 extdyn = dynbuf;
06614111
NC
1689 /* PR 17512: file: 6f427532. */
1690 if (s->size < extdynsize)
1691 goto error_return;
eea6121a 1692 extdynend = extdyn + s->size;
1036838a 1693 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
07d6d2b8 1694 Fix range check. */
1036838a 1695 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
252b5132
RH
1696 {
1697 Elf_Internal_Dyn dyn;
ad9563d6 1698 const char *name = "";
252b5132 1699 char ab[20];
b34976b6 1700 bfd_boolean stringp;
ad9563d6 1701 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 1702
217aa764 1703 (*swap_dyn_in) (abfd, extdyn, &dyn);
252b5132
RH
1704
1705 if (dyn.d_tag == DT_NULL)
1706 break;
1707
b34976b6 1708 stringp = FALSE;
252b5132
RH
1709 switch (dyn.d_tag)
1710 {
1711 default:
ad9563d6
CM
1712 if (bed->elf_backend_get_target_dtag)
1713 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1714
1715 if (!strcmp (name, ""))
1716 {
cd9af601 1717 sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag);
ad9563d6
CM
1718 name = ab;
1719 }
252b5132
RH
1720 break;
1721
b34976b6 1722 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
252b5132
RH
1723 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1724 case DT_PLTGOT: name = "PLTGOT"; break;
1725 case DT_HASH: name = "HASH"; break;
1726 case DT_STRTAB: name = "STRTAB"; break;
1727 case DT_SYMTAB: name = "SYMTAB"; break;
1728 case DT_RELA: name = "RELA"; break;
1729 case DT_RELASZ: name = "RELASZ"; break;
1730 case DT_RELAENT: name = "RELAENT"; break;
1731 case DT_STRSZ: name = "STRSZ"; break;
1732 case DT_SYMENT: name = "SYMENT"; break;
1733 case DT_INIT: name = "INIT"; break;
1734 case DT_FINI: name = "FINI"; break;
b34976b6
AM
1735 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1736 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
252b5132
RH
1737 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1738 case DT_REL: name = "REL"; break;
1739 case DT_RELSZ: name = "RELSZ"; break;
1740 case DT_RELENT: name = "RELENT"; break;
1741 case DT_PLTREL: name = "PLTREL"; break;
1742 case DT_DEBUG: name = "DEBUG"; break;
1743 case DT_TEXTREL: name = "TEXTREL"; break;
1744 case DT_JMPREL: name = "JMPREL"; break;
94558834
L
1745 case DT_BIND_NOW: name = "BIND_NOW"; break;
1746 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1747 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1748 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1749 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
b34976b6 1750 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
94558834
L
1751 case DT_FLAGS: name = "FLAGS"; break;
1752 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1753 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
d48188b9 1754 case DT_CHECKSUM: name = "CHECKSUM"; break;
94558834
L
1755 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1756 case DT_MOVEENT: name = "MOVEENT"; break;
1757 case DT_MOVESZ: name = "MOVESZ"; break;
1758 case DT_FEATURE: name = "FEATURE"; break;
1759 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1760 case DT_SYMINSZ: name = "SYMINSZ"; break;
1761 case DT_SYMINENT: name = "SYMINENT"; break;
b34976b6
AM
1762 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1763 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1764 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
94558834
L
1765 case DT_PLTPAD: name = "PLTPAD"; break;
1766 case DT_MOVETAB: name = "MOVETAB"; break;
1767 case DT_SYMINFO: name = "SYMINFO"; break;
1768 case DT_RELACOUNT: name = "RELACOUNT"; break;
1769 case DT_RELCOUNT: name = "RELCOUNT"; break;
1770 case DT_FLAGS_1: name = "FLAGS_1"; break;
252b5132
RH
1771 case DT_VERSYM: name = "VERSYM"; break;
1772 case DT_VERDEF: name = "VERDEF"; break;
1773 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1774 case DT_VERNEED: name = "VERNEED"; break;
1775 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
b34976b6 1776 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
94558834 1777 case DT_USED: name = "USED"; break;
b34976b6 1778 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
fdc90cb4 1779 case DT_GNU_HASH: name = "GNU_HASH"; break;
252b5132
RH
1780 }
1781
ad9563d6 1782 fprintf (f, " %-20s ", name);
252b5132 1783 if (! stringp)
a1f3c56e
AN
1784 {
1785 fprintf (f, "0x");
1786 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1787 }
252b5132
RH
1788 else
1789 {
1790 const char *string;
dc810e39 1791 unsigned int tagv = dyn.d_un.d_val;
252b5132 1792
dc810e39 1793 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
252b5132
RH
1794 if (string == NULL)
1795 goto error_return;
1796 fprintf (f, "%s", string);
1797 }
1798 fprintf (f, "\n");
1799 }
1800
1801 free (dynbuf);
1802 dynbuf = NULL;
1803 }
1804
1805 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1806 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1807 {
fc0e6df6 1808 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
b34976b6 1809 return FALSE;
252b5132
RH
1810 }
1811
1812 if (elf_dynverdef (abfd) != 0)
1813 {
1814 Elf_Internal_Verdef *t;
1815
1816 fprintf (f, _("\nVersion definitions:\n"));
1817 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1818 {
1819 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
d0fb9a8d
JJ
1820 t->vd_flags, t->vd_hash,
1821 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1822 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
252b5132
RH
1823 {
1824 Elf_Internal_Verdaux *a;
1825
1826 fprintf (f, "\t");
1827 for (a = t->vd_auxptr->vda_nextptr;
1828 a != NULL;
1829 a = a->vda_nextptr)
d0fb9a8d
JJ
1830 fprintf (f, "%s ",
1831 a->vda_nodename ? a->vda_nodename : "<corrupt>");
252b5132
RH
1832 fprintf (f, "\n");
1833 }
1834 }
1835 }
1836
1837 if (elf_dynverref (abfd) != 0)
1838 {
1839 Elf_Internal_Verneed *t;
1840
1841 fprintf (f, _("\nVersion References:\n"));
1842 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1843 {
1844 Elf_Internal_Vernaux *a;
1845
d0fb9a8d
JJ
1846 fprintf (f, _(" required from %s:\n"),
1847 t->vn_filename ? t->vn_filename : "<corrupt>");
252b5132
RH
1848 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1849 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
d0fb9a8d
JJ
1850 a->vna_flags, a->vna_other,
1851 a->vna_nodename ? a->vna_nodename : "<corrupt>");
252b5132
RH
1852 }
1853 }
1854
b34976b6 1855 return TRUE;
252b5132
RH
1856
1857 error_return:
1858 if (dynbuf != NULL)
1859 free (dynbuf);
b34976b6 1860 return FALSE;
252b5132
RH
1861}
1862
bb4d2ac2
L
1863/* Get version string. */
1864
1865const char *
60bb06bc
L
1866_bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
1867 bfd_boolean *hidden)
bb4d2ac2
L
1868{
1869 const char *version_string = NULL;
1870 if (elf_dynversym (abfd) != 0
1871 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1872 {
1873 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1874
1875 *hidden = (vernum & VERSYM_HIDDEN) != 0;
1876 vernum &= VERSYM_VERSION;
1877
1878 if (vernum == 0)
1879 version_string = "";
1f6f5dba
L
1880 else if (vernum == 1
1881 && (vernum > elf_tdata (abfd)->cverdefs
1882 || (elf_tdata (abfd)->verdef[0].vd_flags
1883 == VER_FLG_BASE)))
bb4d2ac2
L
1884 version_string = "Base";
1885 else if (vernum <= elf_tdata (abfd)->cverdefs)
1886 version_string =
1887 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1888 else
1889 {
1890 Elf_Internal_Verneed *t;
1891
7a815dd5 1892 version_string = _("<corrupt>");
bb4d2ac2
L
1893 for (t = elf_tdata (abfd)->verref;
1894 t != NULL;
1895 t = t->vn_nextref)
1896 {
1897 Elf_Internal_Vernaux *a;
1898
1899 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1900 {
1901 if (a->vna_other == vernum)
1902 {
1903 version_string = a->vna_nodename;
1904 break;
1905 }
1906 }
1907 }
1908 }
1909 }
1910 return version_string;
1911}
1912
252b5132
RH
1913/* Display ELF-specific fields of a symbol. */
1914
1915void
217aa764
AM
1916bfd_elf_print_symbol (bfd *abfd,
1917 void *filep,
1918 asymbol *symbol,
1919 bfd_print_symbol_type how)
252b5132 1920{
a50b1753 1921 FILE *file = (FILE *) filep;
252b5132
RH
1922 switch (how)
1923 {
1924 case bfd_print_symbol_name:
1925 fprintf (file, "%s", symbol->name);
1926 break;
1927 case bfd_print_symbol_more:
1928 fprintf (file, "elf ");
60b89a18 1929 bfd_fprintf_vma (abfd, file, symbol->value);
cd9af601 1930 fprintf (file, " %x", symbol->flags);
252b5132
RH
1931 break;
1932 case bfd_print_symbol_all:
1933 {
4e8a9624
AM
1934 const char *section_name;
1935 const char *name = NULL;
9c5bfbb7 1936 const struct elf_backend_data *bed;
7a13edea 1937 unsigned char st_other;
dbb410c3 1938 bfd_vma val;
bb4d2ac2
L
1939 const char *version_string;
1940 bfd_boolean hidden;
c044fabd 1941
252b5132 1942 section_name = symbol->section ? symbol->section->name : "(*none*)";
587ff49e
RH
1943
1944 bed = get_elf_backend_data (abfd);
1945 if (bed->elf_backend_print_symbol_all)
c044fabd 1946 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
587ff49e
RH
1947
1948 if (name == NULL)
1949 {
7ee38065 1950 name = symbol->name;
217aa764 1951 bfd_print_symbol_vandf (abfd, file, symbol);
587ff49e
RH
1952 }
1953
252b5132
RH
1954 fprintf (file, " %s\t", section_name);
1955 /* Print the "other" value for a symbol. For common symbols,
1956 we've already printed the size; now print the alignment.
1957 For other symbols, we have no specified alignment, and
1958 we've printed the address; now print the size. */
dcf6c779 1959 if (symbol->section && bfd_is_com_section (symbol->section))
dbb410c3
AM
1960 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1961 else
1962 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1963 bfd_fprintf_vma (abfd, file, val);
252b5132
RH
1964
1965 /* If we have version information, print it. */
60bb06bc
L
1966 version_string = _bfd_elf_get_symbol_version_string (abfd,
1967 symbol,
1968 &hidden);
bb4d2ac2 1969 if (version_string)
252b5132 1970 {
bb4d2ac2 1971 if (!hidden)
252b5132
RH
1972 fprintf (file, " %-11s", version_string);
1973 else
1974 {
1975 int i;
1976
1977 fprintf (file, " (%s)", version_string);
1978 for (i = 10 - strlen (version_string); i > 0; --i)
1979 putc (' ', file);
1980 }
1981 }
1982
1983 /* If the st_other field is not zero, print it. */
7a13edea 1984 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
c044fabd 1985
7a13edea
NC
1986 switch (st_other)
1987 {
1988 case 0: break;
1989 case STV_INTERNAL: fprintf (file, " .internal"); break;
1990 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1991 case STV_PROTECTED: fprintf (file, " .protected"); break;
1992 default:
1993 /* Some other non-defined flags are also present, so print
1994 everything hex. */
1995 fprintf (file, " 0x%02x", (unsigned int) st_other);
1996 }
252b5132 1997
587ff49e 1998 fprintf (file, " %s", name);
252b5132
RH
1999 }
2000 break;
2001 }
2002}
252b5132
RH
2003\f
2004/* ELF .o/exec file reading */
2005
c044fabd 2006/* Create a new bfd section from an ELF section header. */
252b5132 2007
b34976b6 2008bfd_boolean
217aa764 2009bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
252b5132 2010{
4fbb74a6
AM
2011 Elf_Internal_Shdr *hdr;
2012 Elf_Internal_Ehdr *ehdr;
2013 const struct elf_backend_data *bed;
90937f86 2014 const char *name;
bf67003b
NC
2015 bfd_boolean ret = TRUE;
2016 static bfd_boolean * sections_being_created = NULL;
5a4b0ccc 2017 static bfd * sections_being_created_abfd = NULL;
bf67003b 2018 static unsigned int nesting = 0;
252b5132 2019
4fbb74a6
AM
2020 if (shindex >= elf_numsections (abfd))
2021 return FALSE;
2022
bf67003b
NC
2023 if (++ nesting > 3)
2024 {
2025 /* PR17512: A corrupt ELF binary might contain a recursive group of
67ce483b 2026 sections, with each the string indices pointing to the next in the
bf67003b
NC
2027 loop. Detect this here, by refusing to load a section that we are
2028 already in the process of loading. We only trigger this test if
2029 we have nested at least three sections deep as normal ELF binaries
5a4b0ccc
NC
2030 can expect to recurse at least once.
2031
2032 FIXME: It would be better if this array was attached to the bfd,
2033 rather than being held in a static pointer. */
2034
2035 if (sections_being_created_abfd != abfd)
2036 sections_being_created = NULL;
bf67003b
NC
2037 if (sections_being_created == NULL)
2038 {
2039 /* FIXME: It would be more efficient to attach this array to the bfd somehow. */
2040 sections_being_created = (bfd_boolean *)
2041 bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean));
5a4b0ccc 2042 sections_being_created_abfd = abfd;
bf67003b
NC
2043 }
2044 if (sections_being_created [shindex])
2045 {
4eca0228 2046 _bfd_error_handler
871b3ab2 2047 (_("%pB: warning: loop in section dependencies detected"), abfd);
bf67003b
NC
2048 return FALSE;
2049 }
2050 sections_being_created [shindex] = TRUE;
2051 }
2052
4fbb74a6
AM
2053 hdr = elf_elfsections (abfd)[shindex];
2054 ehdr = elf_elfheader (abfd);
2055 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1b3a8575 2056 hdr->sh_name);
933d961a 2057 if (name == NULL)
bf67003b 2058 goto fail;
252b5132 2059
4fbb74a6 2060 bed = get_elf_backend_data (abfd);
252b5132
RH
2061 switch (hdr->sh_type)
2062 {
2063 case SHT_NULL:
2064 /* Inactive section. Throw it away. */
bf67003b 2065 goto success;
252b5132 2066
bf67003b
NC
2067 case SHT_PROGBITS: /* Normal section with contents. */
2068 case SHT_NOBITS: /* .bss section. */
2069 case SHT_HASH: /* .hash section. */
2070 case SHT_NOTE: /* .note section. */
25e27870
L
2071 case SHT_INIT_ARRAY: /* .init_array section. */
2072 case SHT_FINI_ARRAY: /* .fini_array section. */
2073 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
7f1204bb 2074 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
fdc90cb4 2075 case SHT_GNU_HASH: /* .gnu.hash section. */
bf67003b
NC
2076 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2077 goto success;
252b5132 2078
797fc050 2079 case SHT_DYNAMIC: /* Dynamic linking information. */
6dc132d9 2080 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b
NC
2081 goto fail;
2082
cfcac11d
NC
2083 if (hdr->sh_link > elf_numsections (abfd))
2084 {
caa83f8b 2085 /* PR 10478: Accept Solaris binaries with a sh_link
cfcac11d
NC
2086 field set to SHN_BEFORE or SHN_AFTER. */
2087 switch (bfd_get_arch (abfd))
2088 {
caa83f8b 2089 case bfd_arch_i386:
cfcac11d
NC
2090 case bfd_arch_sparc:
2091 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
2092 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
2093 break;
2094 /* Otherwise fall through. */
2095 default:
bf67003b 2096 goto fail;
cfcac11d
NC
2097 }
2098 }
2099 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
bf67003b 2100 goto fail;
cfcac11d 2101 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
797fc050
AM
2102 {
2103 Elf_Internal_Shdr *dynsymhdr;
2104
2105 /* The shared libraries distributed with hpux11 have a bogus
2106 sh_link field for the ".dynamic" section. Find the
2107 string table for the ".dynsym" section instead. */
2108 if (elf_dynsymtab (abfd) != 0)
2109 {
2110 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2111 hdr->sh_link = dynsymhdr->sh_link;
2112 }
2113 else
2114 {
2115 unsigned int i, num_sec;
2116
2117 num_sec = elf_numsections (abfd);
2118 for (i = 1; i < num_sec; i++)
2119 {
2120 dynsymhdr = elf_elfsections (abfd)[i];
2121 if (dynsymhdr->sh_type == SHT_DYNSYM)
2122 {
2123 hdr->sh_link = dynsymhdr->sh_link;
2124 break;
2125 }
2126 }
2127 }
2128 }
bf67003b 2129 goto success;
797fc050 2130
bf67003b 2131 case SHT_SYMTAB: /* A symbol table. */
252b5132 2132 if (elf_onesymtab (abfd) == shindex)
bf67003b 2133 goto success;
252b5132 2134
a50b2160 2135 if (hdr->sh_entsize != bed->s->sizeof_sym)
bf67003b
NC
2136 goto fail;
2137
3337c1e5 2138 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
eee3b786
AM
2139 {
2140 if (hdr->sh_size != 0)
bf67003b 2141 goto fail;
eee3b786
AM
2142 /* Some assemblers erroneously set sh_info to one with a
2143 zero sh_size. ld sees this as a global symbol count
2144 of (unsigned) -1. Fix it here. */
2145 hdr->sh_info = 0;
bf67003b 2146 goto success;
eee3b786 2147 }
bf67003b 2148
16ad13ec
NC
2149 /* PR 18854: A binary might contain more than one symbol table.
2150 Unusual, but possible. Warn, but continue. */
2151 if (elf_onesymtab (abfd) != 0)
2152 {
4eca0228 2153 _bfd_error_handler
695344c0 2154 /* xgettext:c-format */
871b3ab2 2155 (_("%pB: warning: multiple symbol tables detected"
63a5468a 2156 " - ignoring the table in section %u"),
16ad13ec
NC
2157 abfd, shindex);
2158 goto success;
2159 }
252b5132 2160 elf_onesymtab (abfd) = shindex;
6a40cf0c
NC
2161 elf_symtab_hdr (abfd) = *hdr;
2162 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
252b5132
RH
2163 abfd->flags |= HAS_SYMS;
2164
2165 /* Sometimes a shared object will map in the symbol table. If
08a40648
AM
2166 SHF_ALLOC is set, and this is a shared object, then we also
2167 treat this section as a BFD section. We can not base the
2168 decision purely on SHF_ALLOC, because that flag is sometimes
2169 set in a relocatable object file, which would confuse the
2170 linker. */
252b5132
RH
2171 if ((hdr->sh_flags & SHF_ALLOC) != 0
2172 && (abfd->flags & DYNAMIC) != 0
6dc132d9
L
2173 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2174 shindex))
bf67003b 2175 goto fail;
252b5132 2176
1b3a8575
AM
2177 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2178 can't read symbols without that section loaded as well. It
2179 is most likely specified by the next section header. */
6a40cf0c
NC
2180 {
2181 elf_section_list * entry;
2182 unsigned int i, num_sec;
1b3a8575 2183
6a40cf0c
NC
2184 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2185 if (entry->hdr.sh_link == shindex)
2186 goto success;
2187
2188 num_sec = elf_numsections (abfd);
2189 for (i = shindex + 1; i < num_sec; i++)
2190 {
2191 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2192
2193 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2194 && hdr2->sh_link == shindex)
2195 break;
2196 }
2197
2198 if (i == num_sec)
2199 for (i = 1; i < shindex; i++)
1b3a8575
AM
2200 {
2201 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
6a40cf0c 2202
1b3a8575
AM
2203 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2204 && hdr2->sh_link == shindex)
2205 break;
2206 }
6a40cf0c
NC
2207
2208 if (i != shindex)
2209 ret = bfd_section_from_shdr (abfd, i);
2210 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2211 goto success;
2212 }
252b5132 2213
bf67003b 2214 case SHT_DYNSYM: /* A dynamic symbol table. */
252b5132 2215 if (elf_dynsymtab (abfd) == shindex)
bf67003b 2216 goto success;
252b5132 2217
a50b2160 2218 if (hdr->sh_entsize != bed->s->sizeof_sym)
bf67003b
NC
2219 goto fail;
2220
eee3b786
AM
2221 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2222 {
2223 if (hdr->sh_size != 0)
bf67003b
NC
2224 goto fail;
2225
eee3b786
AM
2226 /* Some linkers erroneously set sh_info to one with a
2227 zero sh_size. ld sees this as a global symbol count
2228 of (unsigned) -1. Fix it here. */
2229 hdr->sh_info = 0;
bf67003b 2230 goto success;
eee3b786 2231 }
bf67003b 2232
16ad13ec
NC
2233 /* PR 18854: A binary might contain more than one dynamic symbol table.
2234 Unusual, but possible. Warn, but continue. */
2235 if (elf_dynsymtab (abfd) != 0)
2236 {
4eca0228 2237 _bfd_error_handler
695344c0 2238 /* xgettext:c-format */
871b3ab2 2239 (_("%pB: warning: multiple dynamic symbol tables detected"
63a5468a 2240 " - ignoring the table in section %u"),
16ad13ec
NC
2241 abfd, shindex);
2242 goto success;
2243 }
252b5132
RH
2244 elf_dynsymtab (abfd) = shindex;
2245 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2246 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2247 abfd->flags |= HAS_SYMS;
2248
2249 /* Besides being a symbol table, we also treat this as a regular
2250 section, so that objcopy can handle it. */
bf67003b
NC
2251 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2252 goto success;
252b5132 2253
bf67003b 2254 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
6a40cf0c
NC
2255 {
2256 elf_section_list * entry;
9ad5cbcf 2257
6a40cf0c
NC
2258 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2259 if (entry->ndx == shindex)
2260 goto success;
07d6d2b8 2261
6a40cf0c
NC
2262 entry = bfd_alloc (abfd, sizeof * entry);
2263 if (entry == NULL)
2264 goto fail;
2265 entry->ndx = shindex;
2266 entry->hdr = * hdr;
2267 entry->next = elf_symtab_shndx_list (abfd);
2268 elf_symtab_shndx_list (abfd) = entry;
2269 elf_elfsections (abfd)[shindex] = & entry->hdr;
2270 goto success;
2271 }
9ad5cbcf 2272
bf67003b 2273 case SHT_STRTAB: /* A string table. */
252b5132 2274 if (hdr->bfd_section != NULL)
bf67003b
NC
2275 goto success;
2276
252b5132
RH
2277 if (ehdr->e_shstrndx == shindex)
2278 {
2279 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2280 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
bf67003b 2281 goto success;
252b5132 2282 }
bf67003b 2283
1b3a8575
AM
2284 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2285 {
2286 symtab_strtab:
2287 elf_tdata (abfd)->strtab_hdr = *hdr;
2288 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
bf67003b 2289 goto success;
1b3a8575 2290 }
bf67003b 2291
1b3a8575
AM
2292 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2293 {
2294 dynsymtab_strtab:
2295 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2296 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2297 elf_elfsections (abfd)[shindex] = hdr;
2298 /* We also treat this as a regular section, so that objcopy
2299 can handle it. */
bf67003b
NC
2300 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2301 shindex);
2302 goto success;
1b3a8575 2303 }
252b5132 2304
1b3a8575
AM
2305 /* If the string table isn't one of the above, then treat it as a
2306 regular section. We need to scan all the headers to be sure,
2307 just in case this strtab section appeared before the above. */
2308 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2309 {
2310 unsigned int i, num_sec;
252b5132 2311
1b3a8575
AM
2312 num_sec = elf_numsections (abfd);
2313 for (i = 1; i < num_sec; i++)
2314 {
2315 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2316 if (hdr2->sh_link == shindex)
2317 {
933d961a
JJ
2318 /* Prevent endless recursion on broken objects. */
2319 if (i == shindex)
bf67003b 2320 goto fail;
1b3a8575 2321 if (! bfd_section_from_shdr (abfd, i))
bf67003b 2322 goto fail;
1b3a8575
AM
2323 if (elf_onesymtab (abfd) == i)
2324 goto symtab_strtab;
2325 if (elf_dynsymtab (abfd) == i)
2326 goto dynsymtab_strtab;
2327 }
2328 }
2329 }
bf67003b
NC
2330 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2331 goto success;
252b5132
RH
2332
2333 case SHT_REL:
2334 case SHT_RELA:
2335 /* *These* do a lot of work -- but build no sections! */
2336 {
2337 asection *target_sect;
d4730f92 2338 Elf_Internal_Shdr *hdr2, **p_hdr;
9ad5cbcf 2339 unsigned int num_sec = elf_numsections (abfd);
d4730f92 2340 struct bfd_elf_section_data *esdt;
252b5132 2341
aa2ca951
JJ
2342 if (hdr->sh_entsize
2343 != (bfd_size_type) (hdr->sh_type == SHT_REL
a50b2160 2344 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
bf67003b 2345 goto fail;
a50b2160 2346
03ae5f59 2347 /* Check for a bogus link to avoid crashing. */
4fbb74a6 2348 if (hdr->sh_link >= num_sec)
03ae5f59 2349 {
4eca0228 2350 _bfd_error_handler
695344c0 2351 /* xgettext:c-format */
871b3ab2 2352 (_("%pB: invalid link %u for reloc section %s (index %u)"),
4eca0228 2353 abfd, hdr->sh_link, name, shindex);
bf67003b
NC
2354 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2355 shindex);
2356 goto success;
03ae5f59
ILT
2357 }
2358
252b5132
RH
2359 /* For some incomprehensible reason Oracle distributes
2360 libraries for Solaris in which some of the objects have
2361 bogus sh_link fields. It would be nice if we could just
2362 reject them, but, unfortunately, some people need to use
2363 them. We scan through the section headers; if we find only
2364 one suitable symbol table, we clobber the sh_link to point
83b89087
L
2365 to it. I hope this doesn't break anything.
2366
2367 Don't do it on executable nor shared library. */
2368 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
2369 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
252b5132
RH
2370 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
2371 {
9ad5cbcf 2372 unsigned int scan;
252b5132
RH
2373 int found;
2374
2375 found = 0;
9ad5cbcf 2376 for (scan = 1; scan < num_sec; scan++)
252b5132
RH
2377 {
2378 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
2379 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
2380 {
2381 if (found != 0)
2382 {
2383 found = 0;
2384 break;
2385 }
2386 found = scan;
2387 }
2388 }
2389 if (found != 0)
2390 hdr->sh_link = found;
2391 }
2392
2393 /* Get the symbol table. */
1b3a8575
AM
2394 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2395 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
252b5132 2396 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
bf67003b 2397 goto fail;
252b5132 2398
a4bcd733
AM
2399 /* If this is an alloc section in an executable or shared
2400 library, or the reloc section does not use the main symbol
2401 table we don't treat it as a reloc section. BFD can't
2402 adequately represent such a section, so at least for now,
2403 we don't try. We just present it as a normal section. We
2404 also can't use it as a reloc section if it points to the
2405 null section, an invalid section, another reloc section, or
2406 its sh_link points to the null section. */
2407 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2408 && (hdr->sh_flags & SHF_ALLOC) != 0)
83b89087 2409 || hdr->sh_link == SHN_UNDEF
a4bcd733 2410 || hdr->sh_link != elf_onesymtab (abfd)
185ef66d 2411 || hdr->sh_info == SHN_UNDEF
185ef66d
AM
2412 || hdr->sh_info >= num_sec
2413 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2414 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
bf67003b
NC
2415 {
2416 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2417 shindex);
2418 goto success;
2419 }
252b5132
RH
2420
2421 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
bf67003b
NC
2422 goto fail;
2423
252b5132
RH
2424 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2425 if (target_sect == NULL)
bf67003b 2426 goto fail;
252b5132 2427
d4730f92
BS
2428 esdt = elf_section_data (target_sect);
2429 if (hdr->sh_type == SHT_RELA)
2430 p_hdr = &esdt->rela.hdr;
252b5132 2431 else
d4730f92
BS
2432 p_hdr = &esdt->rel.hdr;
2433
06614111
NC
2434 /* PR 17512: file: 0b4f81b7. */
2435 if (*p_hdr != NULL)
2436 goto fail;
ef53be89 2437 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
d4730f92 2438 if (hdr2 == NULL)
bf67003b 2439 goto fail;
252b5132 2440 *hdr2 = *hdr;
d4730f92 2441 *p_hdr = hdr2;
252b5132 2442 elf_elfsections (abfd)[shindex] = hdr2;
056bafd4
MR
2443 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2444 * bed->s->int_rels_per_ext_rel);
252b5132
RH
2445 target_sect->flags |= SEC_RELOC;
2446 target_sect->relocation = NULL;
2447 target_sect->rel_filepos = hdr->sh_offset;
bf572ba0
MM
2448 /* In the section to which the relocations apply, mark whether
2449 its relocations are of the REL or RELA variety. */
72730e0c 2450 if (hdr->sh_size != 0)
d4730f92
BS
2451 {
2452 if (hdr->sh_type == SHT_RELA)
2453 target_sect->use_rela_p = 1;
2454 }
252b5132 2455 abfd->flags |= HAS_RELOC;
bf67003b 2456 goto success;
252b5132 2457 }
252b5132
RH
2458
2459 case SHT_GNU_verdef:
2460 elf_dynverdef (abfd) = shindex;
2461 elf_tdata (abfd)->dynverdef_hdr = *hdr;
bf67003b
NC
2462 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2463 goto success;
252b5132
RH
2464
2465 case SHT_GNU_versym:
a50b2160 2466 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
bf67003b
NC
2467 goto fail;
2468
252b5132
RH
2469 elf_dynversym (abfd) = shindex;
2470 elf_tdata (abfd)->dynversym_hdr = *hdr;
bf67003b
NC
2471 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2472 goto success;
252b5132
RH
2473
2474 case SHT_GNU_verneed:
2475 elf_dynverref (abfd) = shindex;
2476 elf_tdata (abfd)->dynverref_hdr = *hdr;
bf67003b
NC
2477 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2478 goto success;
252b5132
RH
2479
2480 case SHT_SHLIB:
bf67003b 2481 goto success;
252b5132 2482
dbb410c3 2483 case SHT_GROUP:
44534af3 2484 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
bf67003b
NC
2485 goto fail;
2486
6dc132d9 2487 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b
NC
2488 goto fail;
2489
bf67003b 2490 goto success;
dbb410c3 2491
252b5132 2492 default:
104d59d1
JM
2493 /* Possibly an attributes section. */
2494 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2495 || hdr->sh_type == bed->obj_attrs_section_type)
2496 {
2497 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b 2498 goto fail;
104d59d1 2499 _bfd_elf_parse_attributes (abfd, hdr);
bf67003b 2500 goto success;
104d59d1
JM
2501 }
2502
252b5132 2503 /* Check for any processor-specific section types. */
3eb70a79 2504 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
bf67003b 2505 goto success;
3eb70a79
L
2506
2507 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2508 {
2509 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2510 /* FIXME: How to properly handle allocated section reserved
2511 for applications? */
4eca0228 2512 _bfd_error_handler
695344c0 2513 /* xgettext:c-format */
871b3ab2 2514 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2515 abfd, hdr->sh_type, name);
3eb70a79 2516 else
bf67003b
NC
2517 {
2518 /* Allow sections reserved for applications. */
2519 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2520 shindex);
2521 goto success;
2522 }
3eb70a79
L
2523 }
2524 else if (hdr->sh_type >= SHT_LOPROC
2525 && hdr->sh_type <= SHT_HIPROC)
2526 /* FIXME: We should handle this section. */
4eca0228 2527 _bfd_error_handler
695344c0 2528 /* xgettext:c-format */
871b3ab2 2529 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2530 abfd, hdr->sh_type, name);
3eb70a79 2531 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
ff15b240
NC
2532 {
2533 /* Unrecognised OS-specific sections. */
2534 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2535 /* SHF_OS_NONCONFORMING indicates that special knowledge is
08a40648 2536 required to correctly process the section and the file should
ff15b240 2537 be rejected with an error message. */
4eca0228 2538 _bfd_error_handler
695344c0 2539 /* xgettext:c-format */
871b3ab2 2540 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2541 abfd, hdr->sh_type, name);
ff15b240 2542 else
bf67003b
NC
2543 {
2544 /* Otherwise it should be processed. */
2545 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2546 goto success;
2547 }
ff15b240 2548 }
3eb70a79
L
2549 else
2550 /* FIXME: We should handle this section. */
4eca0228 2551 _bfd_error_handler
695344c0 2552 /* xgettext:c-format */
871b3ab2 2553 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2554 abfd, hdr->sh_type, name);
3eb70a79 2555
bf67003b 2556 goto fail;
252b5132
RH
2557 }
2558
bf67003b
NC
2559 fail:
2560 ret = FALSE;
2561 success:
e5b470e2 2562 if (sections_being_created && sections_being_created_abfd == abfd)
bf67003b
NC
2563 sections_being_created [shindex] = FALSE;
2564 if (-- nesting == 0)
5a4b0ccc
NC
2565 {
2566 sections_being_created = NULL;
2567 sections_being_created_abfd = abfd;
2568 }
bf67003b 2569 return ret;
252b5132
RH
2570}
2571
87d72d41 2572/* Return the local symbol specified by ABFD, R_SYMNDX. */
ec338859 2573
87d72d41
AM
2574Elf_Internal_Sym *
2575bfd_sym_from_r_symndx (struct sym_cache *cache,
2576 bfd *abfd,
2577 unsigned long r_symndx)
ec338859 2578{
ec338859
AM
2579 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2580
a5d1b3b5
AM
2581 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2582 {
2583 Elf_Internal_Shdr *symtab_hdr;
2584 unsigned char esym[sizeof (Elf64_External_Sym)];
2585 Elf_External_Sym_Shndx eshndx;
ec338859 2586
a5d1b3b5
AM
2587 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2588 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
87d72d41 2589 &cache->sym[ent], esym, &eshndx) == NULL)
a5d1b3b5 2590 return NULL;
9ad5cbcf 2591
a5d1b3b5
AM
2592 if (cache->abfd != abfd)
2593 {
2594 memset (cache->indx, -1, sizeof (cache->indx));
2595 cache->abfd = abfd;
2596 }
2597 cache->indx[ent] = r_symndx;
ec338859 2598 }
a5d1b3b5 2599
87d72d41 2600 return &cache->sym[ent];
ec338859
AM
2601}
2602
252b5132
RH
2603/* Given an ELF section number, retrieve the corresponding BFD
2604 section. */
2605
2606asection *
91d6fa6a 2607bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
252b5132 2608{
91d6fa6a 2609 if (sec_index >= elf_numsections (abfd))
252b5132 2610 return NULL;
91d6fa6a 2611 return elf_elfsections (abfd)[sec_index]->bfd_section;
252b5132
RH
2612}
2613
b35d266b 2614static const struct bfd_elf_special_section special_sections_b[] =
2f89ff8d 2615{
0112cd26 2616 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 2617 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2618};
2619
b35d266b 2620static const struct bfd_elf_special_section special_sections_c[] =
7f4d3958 2621{
0112cd26 2622 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2623 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2624};
2625
b35d266b 2626static const struct bfd_elf_special_section special_sections_d[] =
7f4d3958 2627{
07d6d2b8
AM
2628 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2629 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
a9a72a65
DE
2630 /* There are more DWARF sections than these, but they needn't be added here
2631 unless you have to cope with broken compilers that don't emit section
2632 attributes or you want to help the user writing assembler. */
07d6d2b8
AM
2633 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2634 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2635 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2636 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
0112cd26 2637 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
07d6d2b8
AM
2638 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2639 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2640 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2641 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2642};
2643
b35d266b 2644static const struct bfd_elf_special_section special_sections_f[] =
7f4d3958 2645{
07d6d2b8 2646 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
6f9dbcd4 2647 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8 2648 { NULL, 0 , 0, 0, 0 }
7f4d3958
L
2649};
2650
b35d266b 2651static const struct bfd_elf_special_section special_sections_g[] =
7f4d3958 2652{
0112cd26 2653 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2654 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2655 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2656 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
0112cd26
NC
2657 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2658 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
07d6d2b8
AM
2659 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2660 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2661 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2662 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2663};
2664
b35d266b 2665static const struct bfd_elf_special_section special_sections_h[] =
7f4d3958 2666{
07d6d2b8
AM
2667 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2668 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2669};
2670
b35d266b 2671static const struct bfd_elf_special_section special_sections_i[] =
7f4d3958 2672{
07d6d2b8 2673 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
6f9dbcd4 2674 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2675 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2676 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2677};
2678
b35d266b 2679static const struct bfd_elf_special_section special_sections_l[] =
7f4d3958 2680{
0112cd26 2681 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2682 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2683};
2684
b35d266b 2685static const struct bfd_elf_special_section special_sections_n[] =
7f4d3958 2686{
0112cd26 2687 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
07d6d2b8
AM
2688 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2689 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2690};
2691
b35d266b 2692static const struct bfd_elf_special_section special_sections_p[] =
7f4d3958 2693{
6f9dbcd4 2694 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2695 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2696 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2697};
2698
b35d266b 2699static const struct bfd_elf_special_section special_sections_r[] =
7f4d3958 2700{
0112cd26
NC
2701 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2702 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
07d6d2b8
AM
2703 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2704 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2705 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2706};
2707
b35d266b 2708static const struct bfd_elf_special_section special_sections_s[] =
7f4d3958 2709{
0112cd26
NC
2710 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2711 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2712 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
60ff4dc4
HPN
2713 /* See struct bfd_elf_special_section declaration for the semantics of
2714 this special case where .prefix_length != strlen (.prefix). */
2715 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
07d6d2b8 2716 { NULL, 0, 0, 0, 0 }
2f89ff8d
L
2717};
2718
b35d266b 2719static const struct bfd_elf_special_section special_sections_t[] =
7f4d3958 2720{
07d6d2b8
AM
2721 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2722 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
0112cd26 2723 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
07d6d2b8 2724 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2725};
2726
1b315056
CS
2727static const struct bfd_elf_special_section special_sections_z[] =
2728{
07d6d2b8
AM
2729 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2730 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
1b315056
CS
2731 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2732 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2733 { NULL, 0, 0, 0, 0 }
1b315056
CS
2734};
2735
e4c93b56 2736static const struct bfd_elf_special_section * const special_sections[] =
7f4d3958 2737{
7f4d3958 2738 special_sections_b, /* 'b' */
98ece1b3 2739 special_sections_c, /* 'c' */
7f4d3958
L
2740 special_sections_d, /* 'd' */
2741 NULL, /* 'e' */
2742 special_sections_f, /* 'f' */
2743 special_sections_g, /* 'g' */
2744 special_sections_h, /* 'h' */
2745 special_sections_i, /* 'i' */
2746 NULL, /* 'j' */
2747 NULL, /* 'k' */
2748 special_sections_l, /* 'l' */
2749 NULL, /* 'm' */
2750 special_sections_n, /* 'n' */
2751 NULL, /* 'o' */
2752 special_sections_p, /* 'p' */
2753 NULL, /* 'q' */
2754 special_sections_r, /* 'r' */
2755 special_sections_s, /* 's' */
2756 special_sections_t, /* 't' */
1b315056
CS
2757 NULL, /* 'u' */
2758 NULL, /* 'v' */
2759 NULL, /* 'w' */
2760 NULL, /* 'x' */
2761 NULL, /* 'y' */
2762 special_sections_z /* 'z' */
7f4d3958
L
2763};
2764
551b43fd
AM
2765const struct bfd_elf_special_section *
2766_bfd_elf_get_special_section (const char *name,
2767 const struct bfd_elf_special_section *spec,
2768 unsigned int rela)
2f89ff8d
L
2769{
2770 int i;
7f4d3958 2771 int len;
7f4d3958 2772
551b43fd 2773 len = strlen (name);
7f4d3958 2774
551b43fd 2775 for (i = 0; spec[i].prefix != NULL; i++)
7dcb9820
AM
2776 {
2777 int suffix_len;
551b43fd 2778 int prefix_len = spec[i].prefix_length;
7dcb9820
AM
2779
2780 if (len < prefix_len)
2781 continue;
551b43fd 2782 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
7dcb9820
AM
2783 continue;
2784
551b43fd 2785 suffix_len = spec[i].suffix_length;
7dcb9820
AM
2786 if (suffix_len <= 0)
2787 {
2788 if (name[prefix_len] != 0)
2789 {
2790 if (suffix_len == 0)
2791 continue;
2792 if (name[prefix_len] != '.'
2793 && (suffix_len == -2
551b43fd 2794 || (rela && spec[i].type == SHT_REL)))
7dcb9820
AM
2795 continue;
2796 }
2797 }
2798 else
2799 {
2800 if (len < prefix_len + suffix_len)
2801 continue;
2802 if (memcmp (name + len - suffix_len,
551b43fd 2803 spec[i].prefix + prefix_len,
7dcb9820
AM
2804 suffix_len) != 0)
2805 continue;
2806 }
551b43fd 2807 return &spec[i];
7dcb9820 2808 }
2f89ff8d
L
2809
2810 return NULL;
2811}
2812
7dcb9820 2813const struct bfd_elf_special_section *
29ef7005 2814_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2f89ff8d 2815{
551b43fd
AM
2816 int i;
2817 const struct bfd_elf_special_section *spec;
29ef7005 2818 const struct elf_backend_data *bed;
2f89ff8d
L
2819
2820 /* See if this is one of the special sections. */
551b43fd
AM
2821 if (sec->name == NULL)
2822 return NULL;
2f89ff8d 2823
29ef7005
L
2824 bed = get_elf_backend_data (abfd);
2825 spec = bed->special_sections;
2826 if (spec)
2827 {
2828 spec = _bfd_elf_get_special_section (sec->name,
2829 bed->special_sections,
2830 sec->use_rela_p);
2831 if (spec != NULL)
2832 return spec;
2833 }
2834
551b43fd
AM
2835 if (sec->name[0] != '.')
2836 return NULL;
2f89ff8d 2837
551b43fd 2838 i = sec->name[1] - 'b';
1b315056 2839 if (i < 0 || i > 'z' - 'b')
551b43fd
AM
2840 return NULL;
2841
2842 spec = special_sections[i];
2f89ff8d 2843
551b43fd
AM
2844 if (spec == NULL)
2845 return NULL;
2846
2847 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2f89ff8d
L
2848}
2849
b34976b6 2850bfd_boolean
217aa764 2851_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
252b5132
RH
2852{
2853 struct bfd_elf_section_data *sdata;
551b43fd 2854 const struct elf_backend_data *bed;
7dcb9820 2855 const struct bfd_elf_special_section *ssect;
252b5132 2856
f0abc2a1
AM
2857 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2858 if (sdata == NULL)
2859 {
a50b1753 2860 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
07d6d2b8 2861 sizeof (*sdata));
f0abc2a1
AM
2862 if (sdata == NULL)
2863 return FALSE;
217aa764 2864 sec->used_by_bfd = sdata;
f0abc2a1 2865 }
bf572ba0 2866
551b43fd
AM
2867 /* Indicate whether or not this section should use RELA relocations. */
2868 bed = get_elf_backend_data (abfd);
2869 sec->use_rela_p = bed->default_use_rela_p;
2870
e843e0f8
L
2871 /* When we read a file, we don't need to set ELF section type and
2872 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2873 anyway. We will set ELF section type and flags for all linker
2874 created sections. If user specifies BFD section flags, we will
2875 set ELF section type and flags based on BFD section flags in
02ecc8e9
L
2876 elf_fake_sections. Special handling for .init_array/.fini_array
2877 output sections since they may contain .ctors/.dtors input
2878 sections. We don't want _bfd_elf_init_private_section_data to
2879 copy ELF section type from .ctors/.dtors input sections. */
2880 if (abfd->direction != read_direction
3496cb2a 2881 || (sec->flags & SEC_LINKER_CREATED) != 0)
2f89ff8d 2882 {
551b43fd 2883 ssect = (*bed->get_sec_type_attr) (abfd, sec);
02ecc8e9
L
2884 if (ssect != NULL
2885 && (!sec->flags
2886 || (sec->flags & SEC_LINKER_CREATED) != 0
2887 || ssect->type == SHT_INIT_ARRAY
2888 || ssect->type == SHT_FINI_ARRAY))
a31501e9
L
2889 {
2890 elf_section_type (sec) = ssect->type;
2891 elf_section_flags (sec) = ssect->attr;
2892 }
2f89ff8d
L
2893 }
2894
f592407e 2895 return _bfd_generic_new_section_hook (abfd, sec);
252b5132
RH
2896}
2897
2898/* Create a new bfd section from an ELF program header.
2899
2900 Since program segments have no names, we generate a synthetic name
2901 of the form segment<NUM>, where NUM is generally the index in the
2902 program header table. For segments that are split (see below) we
2903 generate the names segment<NUM>a and segment<NUM>b.
2904
2905 Note that some program segments may have a file size that is different than
2906 (less than) the memory size. All this means is that at execution the
2907 system must allocate the amount of memory specified by the memory size,
2908 but only initialize it with the first "file size" bytes read from the
2909 file. This would occur for example, with program segments consisting
2910 of combined data+bss.
2911
2912 To handle the above situation, this routine generates TWO bfd sections
2913 for the single program segment. The first has the length specified by
2914 the file size of the segment, and the second has the length specified
2915 by the difference between the two sizes. In effect, the segment is split
d5191d0c 2916 into its initialized and uninitialized parts.
252b5132
RH
2917
2918 */
2919
b34976b6 2920bfd_boolean
217aa764
AM
2921_bfd_elf_make_section_from_phdr (bfd *abfd,
2922 Elf_Internal_Phdr *hdr,
91d6fa6a 2923 int hdr_index,
a50b1753 2924 const char *type_name)
252b5132
RH
2925{
2926 asection *newsect;
2927 char *name;
2928 char namebuf[64];
d4c88bbb 2929 size_t len;
252b5132
RH
2930 int split;
2931
2932 split = ((hdr->p_memsz > 0)
2933 && (hdr->p_filesz > 0)
2934 && (hdr->p_memsz > hdr->p_filesz));
d5191d0c
AM
2935
2936 if (hdr->p_filesz > 0)
252b5132 2937 {
91d6fa6a 2938 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
d5191d0c 2939 len = strlen (namebuf) + 1;
a50b1753 2940 name = (char *) bfd_alloc (abfd, len);
d5191d0c
AM
2941 if (!name)
2942 return FALSE;
2943 memcpy (name, namebuf, len);
2944 newsect = bfd_make_section (abfd, name);
2945 if (newsect == NULL)
2946 return FALSE;
2947 newsect->vma = hdr->p_vaddr;
2948 newsect->lma = hdr->p_paddr;
2949 newsect->size = hdr->p_filesz;
2950 newsect->filepos = hdr->p_offset;
2951 newsect->flags |= SEC_HAS_CONTENTS;
2952 newsect->alignment_power = bfd_log2 (hdr->p_align);
2953 if (hdr->p_type == PT_LOAD)
252b5132 2954 {
d5191d0c
AM
2955 newsect->flags |= SEC_ALLOC;
2956 newsect->flags |= SEC_LOAD;
2957 if (hdr->p_flags & PF_X)
2958 {
2959 /* FIXME: all we known is that it has execute PERMISSION,
2960 may be data. */
2961 newsect->flags |= SEC_CODE;
2962 }
2963 }
2964 if (!(hdr->p_flags & PF_W))
2965 {
2966 newsect->flags |= SEC_READONLY;
252b5132 2967 }
252b5132
RH
2968 }
2969
d5191d0c 2970 if (hdr->p_memsz > hdr->p_filesz)
252b5132 2971 {
d5191d0c
AM
2972 bfd_vma align;
2973
91d6fa6a 2974 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
d4c88bbb 2975 len = strlen (namebuf) + 1;
a50b1753 2976 name = (char *) bfd_alloc (abfd, len);
252b5132 2977 if (!name)
b34976b6 2978 return FALSE;
d4c88bbb 2979 memcpy (name, namebuf, len);
252b5132
RH
2980 newsect = bfd_make_section (abfd, name);
2981 if (newsect == NULL)
b34976b6 2982 return FALSE;
252b5132
RH
2983 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2984 newsect->lma = hdr->p_paddr + hdr->p_filesz;
eea6121a 2985 newsect->size = hdr->p_memsz - hdr->p_filesz;
d5191d0c
AM
2986 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2987 align = newsect->vma & -newsect->vma;
2988 if (align == 0 || align > hdr->p_align)
2989 align = hdr->p_align;
2990 newsect->alignment_power = bfd_log2 (align);
252b5132
RH
2991 if (hdr->p_type == PT_LOAD)
2992 {
d5191d0c
AM
2993 /* Hack for gdb. Segments that have not been modified do
2994 not have their contents written to a core file, on the
2995 assumption that a debugger can find the contents in the
2996 executable. We flag this case by setting the fake
2997 section size to zero. Note that "real" bss sections will
2998 always have their contents dumped to the core file. */
2999 if (bfd_get_format (abfd) == bfd_core)
3000 newsect->size = 0;
252b5132
RH
3001 newsect->flags |= SEC_ALLOC;
3002 if (hdr->p_flags & PF_X)
3003 newsect->flags |= SEC_CODE;
3004 }
3005 if (!(hdr->p_flags & PF_W))
3006 newsect->flags |= SEC_READONLY;
3007 }
3008
b34976b6 3009 return TRUE;
252b5132
RH
3010}
3011
b34976b6 3012bfd_boolean
91d6fa6a 3013bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
20cfcaae 3014{
9c5bfbb7 3015 const struct elf_backend_data *bed;
20cfcaae
NC
3016
3017 switch (hdr->p_type)
3018 {
3019 case PT_NULL:
91d6fa6a 3020 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
20cfcaae
NC
3021
3022 case PT_LOAD:
91d6fa6a 3023 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load");
20cfcaae
NC
3024
3025 case PT_DYNAMIC:
91d6fa6a 3026 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
20cfcaae
NC
3027
3028 case PT_INTERP:
91d6fa6a 3029 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
20cfcaae
NC
3030
3031 case PT_NOTE:
91d6fa6a 3032 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
b34976b6 3033 return FALSE;
276da9b3
L
3034 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3035 hdr->p_align))
b34976b6
AM
3036 return FALSE;
3037 return TRUE;
20cfcaae
NC
3038
3039 case PT_SHLIB:
91d6fa6a 3040 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
20cfcaae
NC
3041
3042 case PT_PHDR:
91d6fa6a 3043 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
20cfcaae 3044
811072d8 3045 case PT_GNU_EH_FRAME:
91d6fa6a 3046 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
811072d8
RM
3047 "eh_frame_hdr");
3048
2b05f1b7 3049 case PT_GNU_STACK:
91d6fa6a 3050 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
9ee5e499 3051
8c37241b 3052 case PT_GNU_RELRO:
91d6fa6a 3053 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
8c37241b 3054
20cfcaae 3055 default:
8c1acd09 3056 /* Check for any processor-specific program segment types. */
20cfcaae 3057 bed = get_elf_backend_data (abfd);
91d6fa6a 3058 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
20cfcaae
NC
3059 }
3060}
3061
d4730f92
BS
3062/* Return the REL_HDR for SEC, assuming there is only a single one, either
3063 REL or RELA. */
3064
3065Elf_Internal_Shdr *
3066_bfd_elf_single_rel_hdr (asection *sec)
3067{
3068 if (elf_section_data (sec)->rel.hdr)
3069 {
3070 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3071 return elf_section_data (sec)->rel.hdr;
3072 }
3073 else
3074 return elf_section_data (sec)->rela.hdr;
3075}
3076
3e19fb8f
L
3077static bfd_boolean
3078_bfd_elf_set_reloc_sh_name (bfd *abfd,
3079 Elf_Internal_Shdr *rel_hdr,
3080 const char *sec_name,
3081 bfd_boolean use_rela_p)
3082{
3083 char *name = (char *) bfd_alloc (abfd,
3084 sizeof ".rela" + strlen (sec_name));
3085 if (name == NULL)
3086 return FALSE;
3087
3088 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3089 rel_hdr->sh_name =
3090 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3091 FALSE);
3092 if (rel_hdr->sh_name == (unsigned int) -1)
3093 return FALSE;
3094
3095 return TRUE;
3096}
3097
d4730f92
BS
3098/* Allocate and initialize a section-header for a new reloc section,
3099 containing relocations against ASECT. It is stored in RELDATA. If
3100 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3101 relocations. */
23bc299b 3102
5d13b3b3 3103static bfd_boolean
217aa764 3104_bfd_elf_init_reloc_shdr (bfd *abfd,
d4730f92 3105 struct bfd_elf_section_reloc_data *reldata,
f6fe1ccd 3106 const char *sec_name,
3e19fb8f
L
3107 bfd_boolean use_rela_p,
3108 bfd_boolean delay_st_name_p)
23bc299b 3109{
d4730f92 3110 Elf_Internal_Shdr *rel_hdr;
9c5bfbb7 3111 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 3112
d4730f92 3113 BFD_ASSERT (reldata->hdr == NULL);
ef53be89 3114 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
d4730f92 3115 reldata->hdr = rel_hdr;
23bc299b 3116
3e19fb8f
L
3117 if (delay_st_name_p)
3118 rel_hdr->sh_name = (unsigned int) -1;
3119 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3120 use_rela_p))
b34976b6 3121 return FALSE;
23bc299b
MM
3122 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3123 rel_hdr->sh_entsize = (use_rela_p
3124 ? bed->s->sizeof_rela
3125 : bed->s->sizeof_rel);
72de5009 3126 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
28e07a05 3127 rel_hdr->sh_flags = 0;
23bc299b
MM
3128 rel_hdr->sh_addr = 0;
3129 rel_hdr->sh_size = 0;
3130 rel_hdr->sh_offset = 0;
3131
b34976b6 3132 return TRUE;
23bc299b
MM
3133}
3134
94be91de
JB
3135/* Return the default section type based on the passed in section flags. */
3136
3137int
3138bfd_elf_get_default_section_type (flagword flags)
3139{
0e41bebb 3140 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
2e76e85a 3141 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
94be91de
JB
3142 return SHT_NOBITS;
3143 return SHT_PROGBITS;
3144}
3145
d4730f92
BS
3146struct fake_section_arg
3147{
3148 struct bfd_link_info *link_info;
3149 bfd_boolean failed;
3150};
3151
252b5132
RH
3152/* Set up an ELF internal section header for a section. */
3153
252b5132 3154static void
d4730f92 3155elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
252b5132 3156{
d4730f92 3157 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
9c5bfbb7 3158 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 3159 struct bfd_elf_section_data *esd = elf_section_data (asect);
252b5132 3160 Elf_Internal_Shdr *this_hdr;
0414f35b 3161 unsigned int sh_type;
0ce398f1 3162 const char *name = asect->name;
3e19fb8f 3163 bfd_boolean delay_st_name_p = FALSE;
252b5132 3164
d4730f92 3165 if (arg->failed)
252b5132
RH
3166 {
3167 /* We already failed; just get out of the bfd_map_over_sections
08a40648 3168 loop. */
252b5132
RH
3169 return;
3170 }
3171
d4730f92 3172 this_hdr = &esd->this_hdr;
252b5132 3173
f6fe1ccd 3174 if (arg->link_info)
0ce398f1 3175 {
f6fe1ccd
L
3176 /* ld: compress DWARF debug sections with names: .debug_*. */
3177 if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
3178 && (asect->flags & SEC_DEBUGGING)
3179 && name[1] == 'd'
3180 && name[6] == '_')
3181 {
3182 /* Set SEC_ELF_COMPRESS to indicate this section should be
3183 compressed. */
3184 asect->flags |= SEC_ELF_COMPRESS;
0ce398f1 3185
dd905818 3186 /* If this section will be compressed, delay adding section
3e19fb8f
L
3187 name to section name section after it is compressed in
3188 _bfd_elf_assign_file_positions_for_non_load. */
3189 delay_st_name_p = TRUE;
f6fe1ccd
L
3190 }
3191 }
3192 else if ((asect->flags & SEC_ELF_RENAME))
3193 {
3194 /* objcopy: rename output DWARF debug section. */
3195 if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
3196 {
3197 /* When we decompress or compress with SHF_COMPRESSED,
3198 convert section name from .zdebug_* to .debug_* if
3199 needed. */
3200 if (name[1] == 'z')
3201 {
3202 char *new_name = convert_zdebug_to_debug (abfd, name);
3203 if (new_name == NULL)
3204 {
3205 arg->failed = TRUE;
3206 return;
3207 }
3208 name = new_name;
3209 }
3210 }
3211 else if (asect->compress_status == COMPRESS_SECTION_DONE)
0ce398f1 3212 {
f6fe1ccd
L
3213 /* PR binutils/18087: Compression does not always make a
3214 section smaller. So only rename the section when
3215 compression has actually taken place. If input section
3216 name is .zdebug_*, we should never compress it again. */
3217 char *new_name = convert_debug_to_zdebug (abfd, name);
0ce398f1
L
3218 if (new_name == NULL)
3219 {
3220 arg->failed = TRUE;
3221 return;
3222 }
f6fe1ccd
L
3223 BFD_ASSERT (name[1] != 'z');
3224 name = new_name;
0ce398f1
L
3225 }
3226 }
3227
3e19fb8f
L
3228 if (delay_st_name_p)
3229 this_hdr->sh_name = (unsigned int) -1;
3230 else
252b5132 3231 {
3e19fb8f
L
3232 this_hdr->sh_name
3233 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3234 name, FALSE);
3235 if (this_hdr->sh_name == (unsigned int) -1)
3236 {
3237 arg->failed = TRUE;
3238 return;
3239 }
252b5132
RH
3240 }
3241
a4d8e49b 3242 /* Don't clear sh_flags. Assembler may set additional bits. */
252b5132
RH
3243
3244 if ((asect->flags & SEC_ALLOC) != 0
3245 || asect->user_set_vma)
3246 this_hdr->sh_addr = asect->vma;
3247 else
3248 this_hdr->sh_addr = 0;
3249
3250 this_hdr->sh_offset = 0;
eea6121a 3251 this_hdr->sh_size = asect->size;
252b5132 3252 this_hdr->sh_link = 0;
c86934ce
NC
3253 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3254 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3255 {
4eca0228 3256 _bfd_error_handler
695344c0 3257 /* xgettext:c-format */
9793eb77 3258 (_("%pB: error: alignment power %d of section `%pA' is too big"),
c08bb8dd 3259 abfd, asect->alignment_power, asect);
c86934ce
NC
3260 arg->failed = TRUE;
3261 return;
3262 }
72de5009 3263 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
252b5132
RH
3264 /* The sh_entsize and sh_info fields may have been set already by
3265 copy_private_section_data. */
3266
3267 this_hdr->bfd_section = asect;
3268 this_hdr->contents = NULL;
3269
3cddba1e
L
3270 /* If the section type is unspecified, we set it based on
3271 asect->flags. */
98ece1b3
AM
3272 if ((asect->flags & SEC_GROUP) != 0)
3273 sh_type = SHT_GROUP;
98ece1b3 3274 else
94be91de 3275 sh_type = bfd_elf_get_default_section_type (asect->flags);
98ece1b3 3276
3cddba1e 3277 if (this_hdr->sh_type == SHT_NULL)
98ece1b3
AM
3278 this_hdr->sh_type = sh_type;
3279 else if (this_hdr->sh_type == SHT_NOBITS
3280 && sh_type == SHT_PROGBITS
3281 && (asect->flags & SEC_ALLOC) != 0)
3cddba1e 3282 {
98ece1b3
AM
3283 /* Warn if we are changing a NOBITS section to PROGBITS, but
3284 allow the link to proceed. This can happen when users link
3285 non-bss input sections to bss output sections, or emit data
3286 to a bss output section via a linker script. */
4eca0228 3287 _bfd_error_handler
871b3ab2 3288 (_("warning: section `%pA' type changed to PROGBITS"), asect);
98ece1b3 3289 this_hdr->sh_type = sh_type;
3cddba1e
L
3290 }
3291
2f89ff8d 3292 switch (this_hdr->sh_type)
252b5132 3293 {
2f89ff8d 3294 default:
2f89ff8d
L
3295 break;
3296
3297 case SHT_STRTAB:
2f89ff8d
L
3298 case SHT_NOTE:
3299 case SHT_NOBITS:
3300 case SHT_PROGBITS:
3301 break;
606851fb
AM
3302
3303 case SHT_INIT_ARRAY:
3304 case SHT_FINI_ARRAY:
3305 case SHT_PREINIT_ARRAY:
3306 this_hdr->sh_entsize = bed->s->arch_size / 8;
3307 break;
2f89ff8d
L
3308
3309 case SHT_HASH:
c7ac6ff8 3310 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2f89ff8d 3311 break;
5de3bf90 3312
2f89ff8d 3313 case SHT_DYNSYM:
252b5132 3314 this_hdr->sh_entsize = bed->s->sizeof_sym;
2f89ff8d
L
3315 break;
3316
3317 case SHT_DYNAMIC:
252b5132 3318 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2f89ff8d
L
3319 break;
3320
3321 case SHT_RELA:
3322 if (get_elf_backend_data (abfd)->may_use_rela_p)
3323 this_hdr->sh_entsize = bed->s->sizeof_rela;
3324 break;
3325
3326 case SHT_REL:
3327 if (get_elf_backend_data (abfd)->may_use_rel_p)
3328 this_hdr->sh_entsize = bed->s->sizeof_rel;
3329 break;
3330
3331 case SHT_GNU_versym:
252b5132 3332 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2f89ff8d
L
3333 break;
3334
3335 case SHT_GNU_verdef:
252b5132
RH
3336 this_hdr->sh_entsize = 0;
3337 /* objcopy or strip will copy over sh_info, but may not set
08a40648
AM
3338 cverdefs. The linker will set cverdefs, but sh_info will be
3339 zero. */
252b5132
RH
3340 if (this_hdr->sh_info == 0)
3341 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3342 else
3343 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3344 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2f89ff8d
L
3345 break;
3346
3347 case SHT_GNU_verneed:
252b5132
RH
3348 this_hdr->sh_entsize = 0;
3349 /* objcopy or strip will copy over sh_info, but may not set
08a40648
AM
3350 cverrefs. The linker will set cverrefs, but sh_info will be
3351 zero. */
252b5132
RH
3352 if (this_hdr->sh_info == 0)
3353 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3354 else
3355 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3356 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2f89ff8d
L
3357 break;
3358
3359 case SHT_GROUP:
1783205a 3360 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2f89ff8d 3361 break;
fdc90cb4
JJ
3362
3363 case SHT_GNU_HASH:
3364 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3365 break;
dbb410c3 3366 }
252b5132
RH
3367
3368 if ((asect->flags & SEC_ALLOC) != 0)
3369 this_hdr->sh_flags |= SHF_ALLOC;
3370 if ((asect->flags & SEC_READONLY) == 0)
3371 this_hdr->sh_flags |= SHF_WRITE;
3372 if ((asect->flags & SEC_CODE) != 0)
3373 this_hdr->sh_flags |= SHF_EXECINSTR;
f5fa8ca2
JJ
3374 if ((asect->flags & SEC_MERGE) != 0)
3375 {
3376 this_hdr->sh_flags |= SHF_MERGE;
3377 this_hdr->sh_entsize = asect->entsize;
f5fa8ca2 3378 }
84865015
NC
3379 if ((asect->flags & SEC_STRINGS) != 0)
3380 this_hdr->sh_flags |= SHF_STRINGS;
1126897b 3381 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
dbb410c3 3382 this_hdr->sh_flags |= SHF_GROUP;
13ae64f3 3383 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
704afa60
JJ
3384 {
3385 this_hdr->sh_flags |= SHF_TLS;
3a800eb9
AM
3386 if (asect->size == 0
3387 && (asect->flags & SEC_HAS_CONTENTS) == 0)
704afa60 3388 {
3a800eb9 3389 struct bfd_link_order *o = asect->map_tail.link_order;
b34976b6 3390
704afa60 3391 this_hdr->sh_size = 0;
3a800eb9
AM
3392 if (o != NULL)
3393 {
704afa60 3394 this_hdr->sh_size = o->offset + o->size;
3a800eb9
AM
3395 if (this_hdr->sh_size != 0)
3396 this_hdr->sh_type = SHT_NOBITS;
3397 }
704afa60
JJ
3398 }
3399 }
18ae9cc1
L
3400 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3401 this_hdr->sh_flags |= SHF_EXCLUDE;
252b5132 3402
d4730f92
BS
3403 /* If the section has relocs, set up a section header for the
3404 SHT_REL[A] section. If two relocation sections are required for
3405 this section, it is up to the processor-specific back-end to
3406 create the other. */
3407 if ((asect->flags & SEC_RELOC) != 0)
3408 {
3409 /* When doing a relocatable link, create both REL and RELA sections if
3410 needed. */
3411 if (arg->link_info
3412 /* Do the normal setup if we wouldn't create any sections here. */
3413 && esd->rel.count + esd->rela.count > 0
0e1862bb
L
3414 && (bfd_link_relocatable (arg->link_info)
3415 || arg->link_info->emitrelocations))
d4730f92
BS
3416 {
3417 if (esd->rel.count && esd->rel.hdr == NULL
28e07a05 3418 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
db4677b8 3419 FALSE, delay_st_name_p))
d4730f92
BS
3420 {
3421 arg->failed = TRUE;
3422 return;
3423 }
3424 if (esd->rela.count && esd->rela.hdr == NULL
28e07a05 3425 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
db4677b8 3426 TRUE, delay_st_name_p))
d4730f92
BS
3427 {
3428 arg->failed = TRUE;
3429 return;
3430 }
3431 }
3432 else if (!_bfd_elf_init_reloc_shdr (abfd,
3433 (asect->use_rela_p
3434 ? &esd->rela : &esd->rel),
f6fe1ccd 3435 name,
3e19fb8f
L
3436 asect->use_rela_p,
3437 delay_st_name_p))
db4677b8 3438 {
d4730f92 3439 arg->failed = TRUE;
db4677b8
AM
3440 return;
3441 }
d4730f92
BS
3442 }
3443
252b5132 3444 /* Check for processor-specific section types. */
0414f35b 3445 sh_type = this_hdr->sh_type;
e1fddb6b
AO
3446 if (bed->elf_backend_fake_sections
3447 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
db4677b8
AM
3448 {
3449 arg->failed = TRUE;
3450 return;
3451 }
252b5132 3452
42bb2e33 3453 if (sh_type == SHT_NOBITS && asect->size != 0)
0414f35b
AM
3454 {
3455 /* Don't change the header type from NOBITS if we are being
42bb2e33 3456 called for objcopy --only-keep-debug. */
0414f35b
AM
3457 this_hdr->sh_type = sh_type;
3458 }
252b5132
RH
3459}
3460
bcacc0f5
AM
3461/* Fill in the contents of a SHT_GROUP section. Called from
3462 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3463 when ELF targets use the generic linker, ld. Called for ld -r
3464 from bfd_elf_final_link. */
dbb410c3 3465
1126897b 3466void
217aa764 3467bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
dbb410c3 3468{
a50b1753 3469 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
9dce4196 3470 asection *elt, *first;
dbb410c3 3471 unsigned char *loc;
b34976b6 3472 bfd_boolean gas;
dbb410c3 3473
7e4111ad
L
3474 /* Ignore linker created group section. See elfNN_ia64_object_p in
3475 elfxx-ia64.c. */
3476 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
dbb410c3
AM
3477 || *failedptr)
3478 return;
3479
bcacc0f5
AM
3480 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3481 {
3482 unsigned long symindx = 0;
3483
3484 /* elf_group_id will have been set up by objcopy and the
3485 generic linker. */
3486 if (elf_group_id (sec) != NULL)
3487 symindx = elf_group_id (sec)->udata.i;
1126897b 3488
bcacc0f5
AM
3489 if (symindx == 0)
3490 {
3491 /* If called from the assembler, swap_out_syms will have set up
3492 elf_section_syms. */
3493 BFD_ASSERT (elf_section_syms (abfd) != NULL);
3494 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3495 }
3496 elf_section_data (sec)->this_hdr.sh_info = symindx;
3497 }
3498 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
1126897b 3499 {
bcacc0f5
AM
3500 /* The ELF backend linker sets sh_info to -2 when the group
3501 signature symbol is global, and thus the index can't be
3502 set until all local symbols are output. */
53720c49
AM
3503 asection *igroup;
3504 struct bfd_elf_section_data *sec_data;
3505 unsigned long symndx;
3506 unsigned long extsymoff;
bcacc0f5
AM
3507 struct elf_link_hash_entry *h;
3508
53720c49
AM
3509 /* The point of this little dance to the first SHF_GROUP section
3510 then back to the SHT_GROUP section is that this gets us to
3511 the SHT_GROUP in the input object. */
3512 igroup = elf_sec_group (elf_next_in_group (sec));
3513 sec_data = elf_section_data (igroup);
3514 symndx = sec_data->this_hdr.sh_info;
3515 extsymoff = 0;
bcacc0f5
AM
3516 if (!elf_bad_symtab (igroup->owner))
3517 {
3518 Elf_Internal_Shdr *symtab_hdr;
3519
3520 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3521 extsymoff = symtab_hdr->sh_info;
3522 }
3523 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3524 while (h->root.type == bfd_link_hash_indirect
3525 || h->root.type == bfd_link_hash_warning)
3526 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3527
3528 elf_section_data (sec)->this_hdr.sh_info = h->indx;
1126897b 3529 }
dbb410c3 3530
1126897b 3531 /* The contents won't be allocated for "ld -r" or objcopy. */
b34976b6 3532 gas = TRUE;
dbb410c3
AM
3533 if (sec->contents == NULL)
3534 {
b34976b6 3535 gas = FALSE;
a50b1753 3536 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
9dce4196
AM
3537
3538 /* Arrange for the section to be written out. */
3539 elf_section_data (sec)->this_hdr.contents = sec->contents;
dbb410c3
AM
3540 if (sec->contents == NULL)
3541 {
b34976b6 3542 *failedptr = TRUE;
dbb410c3
AM
3543 return;
3544 }
3545 }
3546
eea6121a 3547 loc = sec->contents + sec->size;
dbb410c3 3548
9dce4196
AM
3549 /* Get the pointer to the first section in the group that gas
3550 squirreled away here. objcopy arranges for this to be set to the
3551 start of the input section group. */
3552 first = elt = elf_next_in_group (sec);
dbb410c3
AM
3553
3554 /* First element is a flag word. Rest of section is elf section
3555 indices for all the sections of the group. Write them backwards
3556 just to keep the group in the same order as given in .section
3557 directives, not that it matters. */
3558 while (elt != NULL)
3559 {
9dce4196 3560 asection *s;
9dce4196 3561
9dce4196 3562 s = elt;
415f38a6
AM
3563 if (!gas)
3564 s = s->output_section;
3565 if (s != NULL
3566 && !bfd_is_abs_section (s))
01e1a5bc 3567 {
db4677b8 3568 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
28e07a05
AM
3569 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3570
3571 if (elf_sec->rel.hdr != NULL
3572 && (gas
3573 || (input_elf_sec->rel.hdr != NULL
3574 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
db4677b8 3575 {
28e07a05 3576 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
db4677b8
AM
3577 loc -= 4;
3578 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3579 }
28e07a05
AM
3580 if (elf_sec->rela.hdr != NULL
3581 && (gas
3582 || (input_elf_sec->rela.hdr != NULL
3583 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
db4677b8 3584 {
28e07a05 3585 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
db4677b8
AM
3586 loc -= 4;
3587 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3588 }
01e1a5bc 3589 loc -= 4;
db4677b8 3590 H_PUT_32 (abfd, elf_sec->this_idx, loc);
01e1a5bc 3591 }
945906ff 3592 elt = elf_next_in_group (elt);
9dce4196
AM
3593 if (elt == first)
3594 break;
dbb410c3
AM
3595 }
3596
7bdf4127
AB
3597 loc -= 4;
3598 BFD_ASSERT (loc == sec->contents);
dbb410c3 3599
9dce4196 3600 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
dbb410c3
AM
3601}
3602
bce964aa
AM
3603/* Given NAME, the name of a relocation section stripped of its
3604 .rel/.rela prefix, return the section in ABFD to which the
3605 relocations apply. */
bd53a53a
L
3606
3607asection *
bce964aa
AM
3608_bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3609{
3610 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3611 section likely apply to .got.plt or .got section. */
3612 if (get_elf_backend_data (abfd)->want_got_plt
3613 && strcmp (name, ".plt") == 0)
3614 {
3615 asection *sec;
3616
3617 name = ".got.plt";
3618 sec = bfd_get_section_by_name (abfd, name);
3619 if (sec != NULL)
3620 return sec;
3621 name = ".got";
3622 }
3623
3624 return bfd_get_section_by_name (abfd, name);
3625}
3626
3627/* Return the section to which RELOC_SEC applies. */
3628
3629static asection *
3630elf_get_reloc_section (asection *reloc_sec)
bd53a53a
L
3631{
3632 const char *name;
3633 unsigned int type;
3634 bfd *abfd;
bce964aa 3635 const struct elf_backend_data *bed;
bd53a53a
L
3636
3637 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3638 if (type != SHT_REL && type != SHT_RELA)
3639 return NULL;
3640
3641 /* We look up the section the relocs apply to by name. */
3642 name = reloc_sec->name;
bce964aa
AM
3643 if (strncmp (name, ".rel", 4) != 0)
3644 return NULL;
3645 name += 4;
3646 if (type == SHT_RELA && *name++ != 'a')
3647 return NULL;
bd53a53a 3648
bd53a53a 3649 abfd = reloc_sec->owner;
bce964aa
AM
3650 bed = get_elf_backend_data (abfd);
3651 return bed->get_reloc_section (abfd, name);
bd53a53a
L
3652}
3653
252b5132
RH
3654/* Assign all ELF section numbers. The dummy first section is handled here
3655 too. The link/info pointers for the standard section types are filled
3656 in here too, while we're at it. */
3657
b34976b6 3658static bfd_boolean
da9f89d4 3659assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
252b5132
RH
3660{
3661 struct elf_obj_tdata *t = elf_tdata (abfd);
3662 asection *sec;
3e19fb8f 3663 unsigned int section_number;
252b5132 3664 Elf_Internal_Shdr **i_shdrp;
47cc2cf5 3665 struct bfd_elf_section_data *d;
3516e984 3666 bfd_boolean need_symtab;
252b5132
RH
3667
3668 section_number = 1;
3669
2b0f7ef9
JJ
3670 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3671
da9f89d4 3672 /* SHT_GROUP sections are in relocatable files only. */
7bdf4127 3673 if (link_info == NULL || !link_info->resolve_section_groups)
252b5132 3674 {
ef53be89 3675 size_t reloc_count = 0;
14f2c699 3676
da9f89d4 3677 /* Put SHT_GROUP sections first. */
04dd1667 3678 for (sec = abfd->sections; sec != NULL; sec = sec->next)
47cc2cf5 3679 {
5daa8fe7 3680 d = elf_section_data (sec);
da9f89d4
L
3681
3682 if (d->this_hdr.sh_type == SHT_GROUP)
08a40648 3683 {
5daa8fe7 3684 if (sec->flags & SEC_LINKER_CREATED)
da9f89d4
L
3685 {
3686 /* Remove the linker created SHT_GROUP sections. */
5daa8fe7 3687 bfd_section_list_remove (abfd, sec);
da9f89d4 3688 abfd->section_count--;
da9f89d4 3689 }
08a40648 3690 else
4fbb74a6 3691 d->this_idx = section_number++;
da9f89d4 3692 }
14f2c699
L
3693
3694 /* Count relocations. */
3695 reloc_count += sec->reloc_count;
47cc2cf5 3696 }
14f2c699
L
3697
3698 /* Clear HAS_RELOC if there are no relocations. */
3699 if (reloc_count == 0)
3700 abfd->flags &= ~HAS_RELOC;
47cc2cf5
PB
3701 }
3702
3703 for (sec = abfd->sections; sec; sec = sec->next)
3704 {
3705 d = elf_section_data (sec);
3706
3707 if (d->this_hdr.sh_type != SHT_GROUP)
4fbb74a6 3708 d->this_idx = section_number++;
3e19fb8f
L
3709 if (d->this_hdr.sh_name != (unsigned int) -1)
3710 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
d4730f92 3711 if (d->rel.hdr)
2b0f7ef9 3712 {
d4730f92 3713 d->rel.idx = section_number++;
3e19fb8f
L
3714 if (d->rel.hdr->sh_name != (unsigned int) -1)
3715 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
2b0f7ef9 3716 }
d4730f92
BS
3717 else
3718 d->rel.idx = 0;
23bc299b 3719
d4730f92 3720 if (d->rela.hdr)
2b0f7ef9 3721 {
d4730f92 3722 d->rela.idx = section_number++;
3e19fb8f
L
3723 if (d->rela.hdr->sh_name != (unsigned int) -1)
3724 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
2b0f7ef9 3725 }
23bc299b 3726 else
d4730f92 3727 d->rela.idx = 0;
252b5132
RH
3728 }
3729
3516e984
L
3730 need_symtab = (bfd_get_symcount (abfd) > 0
3731 || (link_info == NULL
3732 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3733 == HAS_RELOC)));
3734 if (need_symtab)
252b5132 3735 {
12bd6957 3736 elf_onesymtab (abfd) = section_number++;
2b0f7ef9 3737 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4fbb74a6 3738 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
9ad5cbcf 3739 {
6a40cf0c
NC
3740 elf_section_list * entry;
3741
3742 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
3743
3744 entry = bfd_zalloc (abfd, sizeof * entry);
3745 entry->ndx = section_number++;
3746 elf_symtab_shndx_list (abfd) = entry;
3747 entry->hdr.sh_name
9ad5cbcf 3748 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
b34976b6 3749 ".symtab_shndx", FALSE);
6a40cf0c 3750 if (entry->hdr.sh_name == (unsigned int) -1)
b34976b6 3751 return FALSE;
9ad5cbcf 3752 }
12bd6957 3753 elf_strtab_sec (abfd) = section_number++;
2b0f7ef9 3754 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
252b5132
RH
3755 }
3756
dd905818
NC
3757 elf_shstrtab_sec (abfd) = section_number++;
3758 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3759 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3760
1c52a645
L
3761 if (section_number >= SHN_LORESERVE)
3762 {
695344c0 3763 /* xgettext:c-format */
871b3ab2 3764 _bfd_error_handler (_("%pB: too many sections: %u"),
1c52a645
L
3765 abfd, section_number);
3766 return FALSE;
3767 }
3768
9ad5cbcf 3769 elf_numsections (abfd) = section_number;
252b5132
RH
3770 elf_elfheader (abfd)->e_shnum = section_number;
3771
3772 /* Set up the list of section header pointers, in agreement with the
3773 indices. */
a50b1753 3774 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
07d6d2b8 3775 sizeof (Elf_Internal_Shdr *));
252b5132 3776 if (i_shdrp == NULL)
b34976b6 3777 return FALSE;
252b5132 3778
a50b1753 3779 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
07d6d2b8 3780 sizeof (Elf_Internal_Shdr));
252b5132
RH
3781 if (i_shdrp[0] == NULL)
3782 {
3783 bfd_release (abfd, i_shdrp);
b34976b6 3784 return FALSE;
252b5132 3785 }
252b5132
RH
3786
3787 elf_elfsections (abfd) = i_shdrp;
3788
12bd6957 3789 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3516e984 3790 if (need_symtab)
252b5132 3791 {
12bd6957 3792 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4fbb74a6 3793 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
9ad5cbcf 3794 {
6a40cf0c
NC
3795 elf_section_list * entry = elf_symtab_shndx_list (abfd);
3796 BFD_ASSERT (entry != NULL);
3797 i_shdrp[entry->ndx] = & entry->hdr;
3798 entry->hdr.sh_link = elf_onesymtab (abfd);
9ad5cbcf 3799 }
12bd6957
AM
3800 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3801 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
252b5132 3802 }
38ce5b11 3803
252b5132
RH
3804 for (sec = abfd->sections; sec; sec = sec->next)
3805 {
252b5132 3806 asection *s;
252b5132 3807
91d6fa6a
NC
3808 d = elf_section_data (sec);
3809
252b5132 3810 i_shdrp[d->this_idx] = &d->this_hdr;
d4730f92
BS
3811 if (d->rel.idx != 0)
3812 i_shdrp[d->rel.idx] = d->rel.hdr;
3813 if (d->rela.idx != 0)
3814 i_shdrp[d->rela.idx] = d->rela.hdr;
252b5132
RH
3815
3816 /* Fill in the sh_link and sh_info fields while we're at it. */
3817
3818 /* sh_link of a reloc section is the section index of the symbol
3819 table. sh_info is the section index of the section to which
3820 the relocation entries apply. */
d4730f92 3821 if (d->rel.idx != 0)
252b5132 3822 {
12bd6957 3823 d->rel.hdr->sh_link = elf_onesymtab (abfd);
d4730f92 3824 d->rel.hdr->sh_info = d->this_idx;
9ef5d938 3825 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
252b5132 3826 }
d4730f92 3827 if (d->rela.idx != 0)
23bc299b 3828 {
12bd6957 3829 d->rela.hdr->sh_link = elf_onesymtab (abfd);
d4730f92 3830 d->rela.hdr->sh_info = d->this_idx;
9ef5d938 3831 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
23bc299b 3832 }
252b5132 3833
38ce5b11
L
3834 /* We need to set up sh_link for SHF_LINK_ORDER. */
3835 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3836 {
3837 s = elf_linked_to_section (sec);
3838 if (s)
38ce5b11 3839 {
f2876037 3840 /* elf_linked_to_section points to the input section. */
ccd2ec6a 3841 if (link_info != NULL)
38ce5b11 3842 {
f2876037 3843 /* Check discarded linkonce section. */
dbaa2011 3844 if (discarded_section (s))
38ce5b11 3845 {
ccd2ec6a 3846 asection *kept;
4eca0228 3847 _bfd_error_handler
695344c0 3848 /* xgettext:c-format */
871b3ab2
AM
3849 (_("%pB: sh_link of section `%pA' points to"
3850 " discarded section `%pA' of `%pB'"),
ccd2ec6a
L
3851 abfd, d->this_hdr.bfd_section,
3852 s, s->owner);
3853 /* Point to the kept section if it has the same
3854 size as the discarded one. */
c0f00686 3855 kept = _bfd_elf_check_kept_section (s, link_info);
ccd2ec6a 3856 if (kept == NULL)
185d09ad 3857 {
ccd2ec6a
L
3858 bfd_set_error (bfd_error_bad_value);
3859 return FALSE;
185d09ad 3860 }
ccd2ec6a 3861 s = kept;
38ce5b11 3862 }
e424ecc8 3863
ccd2ec6a
L
3864 s = s->output_section;
3865 BFD_ASSERT (s != NULL);
38ce5b11 3866 }
f2876037
L
3867 else
3868 {
3869 /* Handle objcopy. */
3870 if (s->output_section == NULL)
3871 {
4eca0228 3872 _bfd_error_handler
695344c0 3873 /* xgettext:c-format */
871b3ab2
AM
3874 (_("%pB: sh_link of section `%pA' points to"
3875 " removed section `%pA' of `%pB'"),
f2876037
L
3876 abfd, d->this_hdr.bfd_section, s, s->owner);
3877 bfd_set_error (bfd_error_bad_value);
3878 return FALSE;
3879 }
3880 s = s->output_section;
3881 }
ccd2ec6a
L
3882 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3883 }
3884 else
3885 {
3886 /* PR 290:
3887 The Intel C compiler generates SHT_IA_64_UNWIND with
3888 SHF_LINK_ORDER. But it doesn't set the sh_link or
3889 sh_info fields. Hence we could get the situation
08a40648 3890 where s is NULL. */
ccd2ec6a
L
3891 const struct elf_backend_data *bed
3892 = get_elf_backend_data (abfd);
3893 if (bed->link_order_error_handler)
3894 bed->link_order_error_handler
695344c0 3895 /* xgettext:c-format */
871b3ab2 3896 (_("%pB: warning: sh_link not set for section `%pA'"),
ccd2ec6a 3897 abfd, sec);
38ce5b11
L
3898 }
3899 }
3900
252b5132
RH
3901 switch (d->this_hdr.sh_type)
3902 {
3903 case SHT_REL:
3904 case SHT_RELA:
3905 /* A reloc section which we are treating as a normal BFD
3906 section. sh_link is the section index of the symbol
3907 table. sh_info is the section index of the section to
3908 which the relocation entries apply. We assume that an
3909 allocated reloc section uses the dynamic symbol table.
3910 FIXME: How can we be sure? */
3911 s = bfd_get_section_by_name (abfd, ".dynsym");
3912 if (s != NULL)
3913 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3914
bce964aa 3915 s = elf_get_reloc_section (sec);
252b5132 3916 if (s != NULL)
9ef5d938
L
3917 {
3918 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3919 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3920 }
252b5132
RH
3921 break;
3922
3923 case SHT_STRTAB:
3924 /* We assume that a section named .stab*str is a stabs
3925 string section. We look for a section with the same name
3926 but without the trailing ``str'', and set its sh_link
3927 field to point to this section. */
0112cd26 3928 if (CONST_STRNEQ (sec->name, ".stab")
252b5132
RH
3929 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3930 {
3931 size_t len;
3932 char *alc;
3933
3934 len = strlen (sec->name);
a50b1753 3935 alc = (char *) bfd_malloc (len - 2);
252b5132 3936 if (alc == NULL)
b34976b6 3937 return FALSE;
d4c88bbb 3938 memcpy (alc, sec->name, len - 3);
252b5132
RH
3939 alc[len - 3] = '\0';
3940 s = bfd_get_section_by_name (abfd, alc);
3941 free (alc);
3942 if (s != NULL)
3943 {
3944 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3945
3946 /* This is a .stab section. */
0594c12d
AM
3947 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3948 elf_section_data (s)->this_hdr.sh_entsize
3949 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
252b5132
RH
3950 }
3951 }
3952 break;
3953
3954 case SHT_DYNAMIC:
3955 case SHT_DYNSYM:
3956 case SHT_GNU_verneed:
3957 case SHT_GNU_verdef:
3958 /* sh_link is the section header index of the string table
3959 used for the dynamic entries, or the symbol table, or the
3960 version strings. */
3961 s = bfd_get_section_by_name (abfd, ".dynstr");
3962 if (s != NULL)
3963 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3964 break;
3965
7f1204bb
JJ
3966 case SHT_GNU_LIBLIST:
3967 /* sh_link is the section header index of the prelink library
08a40648
AM
3968 list used for the dynamic entries, or the symbol table, or
3969 the version strings. */
7f1204bb
JJ
3970 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3971 ? ".dynstr" : ".gnu.libstr");
3972 if (s != NULL)
3973 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3974 break;
3975
252b5132 3976 case SHT_HASH:
fdc90cb4 3977 case SHT_GNU_HASH:
252b5132
RH
3978 case SHT_GNU_versym:
3979 /* sh_link is the section header index of the symbol table
3980 this hash table or version table is for. */
3981 s = bfd_get_section_by_name (abfd, ".dynsym");
3982 if (s != NULL)
3983 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3984 break;
dbb410c3
AM
3985
3986 case SHT_GROUP:
12bd6957 3987 d->this_hdr.sh_link = elf_onesymtab (abfd);
252b5132
RH
3988 }
3989 }
3990
3e19fb8f
L
3991 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
3992 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
3993 debug section name from .debug_* to .zdebug_* if needed. */
3994
b34976b6 3995 return TRUE;
252b5132
RH
3996}
3997
5372391b 3998static bfd_boolean
217aa764 3999sym_is_global (bfd *abfd, asymbol *sym)
252b5132
RH
4000{
4001 /* If the backend has a special mapping, use it. */
9c5bfbb7 4002 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764
AM
4003 if (bed->elf_backend_sym_is_global)
4004 return (*bed->elf_backend_sym_is_global) (abfd, sym);
252b5132 4005
e47bf690 4006 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
252b5132
RH
4007 || bfd_is_und_section (bfd_get_section (sym))
4008 || bfd_is_com_section (bfd_get_section (sym)));
4009}
4010
76359541
TP
4011/* Filter global symbols of ABFD to include in the import library. All
4012 SYMCOUNT symbols of ABFD can be examined from their pointers in
4013 SYMS. Pointers of symbols to keep should be stored contiguously at
4014 the beginning of that array.
4015
4016 Returns the number of symbols to keep. */
4017
4018unsigned int
4019_bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4020 asymbol **syms, long symcount)
4021{
4022 long src_count, dst_count = 0;
4023
4024 for (src_count = 0; src_count < symcount; src_count++)
4025 {
4026 asymbol *sym = syms[src_count];
4027 char *name = (char *) bfd_asymbol_name (sym);
4028 struct bfd_link_hash_entry *h;
4029
4030 if (!sym_is_global (abfd, sym))
4031 continue;
4032
4033 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE);
5df1bc57
AM
4034 if (h == NULL)
4035 continue;
76359541
TP
4036 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4037 continue;
76359541
TP
4038 if (h->linker_def || h->ldscript_def)
4039 continue;
4040
4041 syms[dst_count++] = sym;
4042 }
4043
4044 syms[dst_count] = NULL;
4045
4046 return dst_count;
4047}
4048
5372391b 4049/* Don't output section symbols for sections that are not going to be
c6d8cab4 4050 output, that are duplicates or there is no BFD section. */
5372391b
AM
4051
4052static bfd_boolean
4053ignore_section_sym (bfd *abfd, asymbol *sym)
4054{
c6d8cab4
L
4055 elf_symbol_type *type_ptr;
4056
db0c309f
NC
4057 if (sym == NULL)
4058 return FALSE;
4059
c6d8cab4
L
4060 if ((sym->flags & BSF_SECTION_SYM) == 0)
4061 return FALSE;
4062
db0c309f
NC
4063 if (sym->section == NULL)
4064 return TRUE;
4065
c6d8cab4
L
4066 type_ptr = elf_symbol_from (abfd, sym);
4067 return ((type_ptr != NULL
4068 && type_ptr->internal_elf_sym.st_shndx != 0
4069 && bfd_is_abs_section (sym->section))
4070 || !(sym->section->owner == abfd
db0c309f
NC
4071 || (sym->section->output_section != NULL
4072 && sym->section->output_section->owner == abfd
2633a79c
AM
4073 && sym->section->output_offset == 0)
4074 || bfd_is_abs_section (sym->section)));
5372391b
AM
4075}
4076
2633a79c
AM
4077/* Map symbol from it's internal number to the external number, moving
4078 all local symbols to be at the head of the list. */
4079
b34976b6 4080static bfd_boolean
12bd6957 4081elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
252b5132 4082{
dc810e39 4083 unsigned int symcount = bfd_get_symcount (abfd);
252b5132
RH
4084 asymbol **syms = bfd_get_outsymbols (abfd);
4085 asymbol **sect_syms;
dc810e39
AM
4086 unsigned int num_locals = 0;
4087 unsigned int num_globals = 0;
4088 unsigned int num_locals2 = 0;
4089 unsigned int num_globals2 = 0;
7292b3ac 4090 unsigned int max_index = 0;
dc810e39 4091 unsigned int idx;
252b5132
RH
4092 asection *asect;
4093 asymbol **new_syms;
252b5132
RH
4094
4095#ifdef DEBUG
4096 fprintf (stderr, "elf_map_symbols\n");
4097 fflush (stderr);
4098#endif
4099
252b5132
RH
4100 for (asect = abfd->sections; asect; asect = asect->next)
4101 {
4102 if (max_index < asect->index)
4103 max_index = asect->index;
4104 }
4105
4106 max_index++;
a50b1753 4107 sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
252b5132 4108 if (sect_syms == NULL)
b34976b6 4109 return FALSE;
252b5132 4110 elf_section_syms (abfd) = sect_syms;
4e89ac30 4111 elf_num_section_syms (abfd) = max_index;
252b5132 4112
079e9a2f
AM
4113 /* Init sect_syms entries for any section symbols we have already
4114 decided to output. */
252b5132
RH
4115 for (idx = 0; idx < symcount; idx++)
4116 {
dc810e39 4117 asymbol *sym = syms[idx];
c044fabd 4118
252b5132 4119 if ((sym->flags & BSF_SECTION_SYM) != 0
0f0a5e58 4120 && sym->value == 0
2633a79c
AM
4121 && !ignore_section_sym (abfd, sym)
4122 && !bfd_is_abs_section (sym->section))
252b5132 4123 {
5372391b 4124 asection *sec = sym->section;
252b5132 4125
5372391b
AM
4126 if (sec->owner != abfd)
4127 sec = sec->output_section;
252b5132 4128
5372391b 4129 sect_syms[sec->index] = syms[idx];
252b5132
RH
4130 }
4131 }
4132
252b5132
RH
4133 /* Classify all of the symbols. */
4134 for (idx = 0; idx < symcount; idx++)
4135 {
2633a79c 4136 if (sym_is_global (abfd, syms[idx]))
252b5132 4137 num_globals++;
2633a79c
AM
4138 else if (!ignore_section_sym (abfd, syms[idx]))
4139 num_locals++;
252b5132 4140 }
079e9a2f 4141
5372391b 4142 /* We will be adding a section symbol for each normal BFD section. Most
079e9a2f
AM
4143 sections will already have a section symbol in outsymbols, but
4144 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4145 at least in that case. */
252b5132
RH
4146 for (asect = abfd->sections; asect; asect = asect->next)
4147 {
079e9a2f 4148 if (sect_syms[asect->index] == NULL)
252b5132 4149 {
079e9a2f 4150 if (!sym_is_global (abfd, asect->symbol))
252b5132
RH
4151 num_locals++;
4152 else
4153 num_globals++;
252b5132
RH
4154 }
4155 }
4156
4157 /* Now sort the symbols so the local symbols are first. */
a50b1753 4158 new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
07d6d2b8 4159 sizeof (asymbol *));
dc810e39 4160
252b5132 4161 if (new_syms == NULL)
b34976b6 4162 return FALSE;
252b5132
RH
4163
4164 for (idx = 0; idx < symcount; idx++)
4165 {
4166 asymbol *sym = syms[idx];
dc810e39 4167 unsigned int i;
252b5132 4168
2633a79c
AM
4169 if (sym_is_global (abfd, sym))
4170 i = num_locals + num_globals2++;
4171 else if (!ignore_section_sym (abfd, sym))
252b5132
RH
4172 i = num_locals2++;
4173 else
2633a79c 4174 continue;
252b5132
RH
4175 new_syms[i] = sym;
4176 sym->udata.i = i + 1;
4177 }
4178 for (asect = abfd->sections; asect; asect = asect->next)
4179 {
079e9a2f 4180 if (sect_syms[asect->index] == NULL)
252b5132 4181 {
079e9a2f 4182 asymbol *sym = asect->symbol;
dc810e39 4183 unsigned int i;
252b5132 4184
079e9a2f 4185 sect_syms[asect->index] = sym;
252b5132
RH
4186 if (!sym_is_global (abfd, sym))
4187 i = num_locals2++;
4188 else
4189 i = num_locals + num_globals2++;
4190 new_syms[i] = sym;
4191 sym->udata.i = i + 1;
4192 }
4193 }
4194
4195 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4196
12bd6957 4197 *pnum_locals = num_locals;
b34976b6 4198 return TRUE;
252b5132
RH
4199}
4200
4201/* Align to the maximum file alignment that could be required for any
4202 ELF data structure. */
4203
268b6b39 4204static inline file_ptr
217aa764 4205align_file_position (file_ptr off, int align)
252b5132
RH
4206{
4207 return (off + align - 1) & ~(align - 1);
4208}
4209
4210/* Assign a file position to a section, optionally aligning to the
4211 required section alignment. */
4212
217aa764
AM
4213file_ptr
4214_bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4215 file_ptr offset,
4216 bfd_boolean align)
252b5132 4217{
72de5009
AM
4218 if (align && i_shdrp->sh_addralign > 1)
4219 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
252b5132
RH
4220 i_shdrp->sh_offset = offset;
4221 if (i_shdrp->bfd_section != NULL)
4222 i_shdrp->bfd_section->filepos = offset;
4223 if (i_shdrp->sh_type != SHT_NOBITS)
4224 offset += i_shdrp->sh_size;
4225 return offset;
4226}
4227
4228/* Compute the file positions we are going to put the sections at, and
4229 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4230 is not NULL, this is being called by the ELF backend linker. */
4231
b34976b6 4232bfd_boolean
217aa764
AM
4233_bfd_elf_compute_section_file_positions (bfd *abfd,
4234 struct bfd_link_info *link_info)
252b5132 4235{
9c5bfbb7 4236 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 4237 struct fake_section_arg fsargs;
b34976b6 4238 bfd_boolean failed;
ef10c3ac 4239 struct elf_strtab_hash *strtab = NULL;
252b5132 4240 Elf_Internal_Shdr *shstrtab_hdr;
3516e984 4241 bfd_boolean need_symtab;
252b5132
RH
4242
4243 if (abfd->output_has_begun)
b34976b6 4244 return TRUE;
252b5132
RH
4245
4246 /* Do any elf backend specific processing first. */
4247 if (bed->elf_backend_begin_write_processing)
4248 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4249
4250 if (! prep_headers (abfd))
b34976b6 4251 return FALSE;
252b5132 4252
e6c51ed4 4253 /* Post process the headers if necessary. */
78245035 4254 (*bed->elf_backend_post_process_headers) (abfd, link_info);
e6c51ed4 4255
d4730f92
BS
4256 fsargs.failed = FALSE;
4257 fsargs.link_info = link_info;
4258 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4259 if (fsargs.failed)
b34976b6 4260 return FALSE;
252b5132 4261
da9f89d4 4262 if (!assign_section_numbers (abfd, link_info))
b34976b6 4263 return FALSE;
252b5132
RH
4264
4265 /* The backend linker builds symbol table information itself. */
3516e984
L
4266 need_symtab = (link_info == NULL
4267 && (bfd_get_symcount (abfd) > 0
4268 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4269 == HAS_RELOC)));
4270 if (need_symtab)
252b5132
RH
4271 {
4272 /* Non-zero if doing a relocatable link. */
4273 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4274
4275 if (! swap_out_syms (abfd, &strtab, relocatable_p))
b34976b6 4276 return FALSE;
252b5132
RH
4277 }
4278
d4730f92 4279 failed = FALSE;
1126897b 4280 if (link_info == NULL)
dbb410c3 4281 {
1126897b 4282 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
dbb410c3 4283 if (failed)
b34976b6 4284 return FALSE;
dbb410c3
AM
4285 }
4286
252b5132
RH
4287 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4288 /* sh_name was set in prep_headers. */
4289 shstrtab_hdr->sh_type = SHT_STRTAB;
84865015 4290 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
252b5132 4291 shstrtab_hdr->sh_addr = 0;
946748d5 4292 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
252b5132
RH
4293 shstrtab_hdr->sh_entsize = 0;
4294 shstrtab_hdr->sh_link = 0;
4295 shstrtab_hdr->sh_info = 0;
3e19fb8f 4296 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
252b5132
RH
4297 shstrtab_hdr->sh_addralign = 1;
4298
c84fca4d 4299 if (!assign_file_positions_except_relocs (abfd, link_info))
b34976b6 4300 return FALSE;
252b5132 4301
3516e984 4302 if (need_symtab)
252b5132
RH
4303 {
4304 file_ptr off;
4305 Elf_Internal_Shdr *hdr;
4306
12bd6957 4307 off = elf_next_file_pos (abfd);
252b5132 4308
6a40cf0c 4309 hdr = & elf_symtab_hdr (abfd);
b34976b6 4310 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
252b5132 4311
6a40cf0c
NC
4312 if (elf_symtab_shndx_list (abfd) != NULL)
4313 {
4314 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4315 if (hdr->sh_size != 0)
4316 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4317 /* FIXME: What about other symtab_shndx sections in the list ? */
4318 }
9ad5cbcf 4319
252b5132 4320 hdr = &elf_tdata (abfd)->strtab_hdr;
b34976b6 4321 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
252b5132 4322
12bd6957 4323 elf_next_file_pos (abfd) = off;
252b5132
RH
4324
4325 /* Now that we know where the .strtab section goes, write it
08a40648 4326 out. */
252b5132 4327 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 4328 || ! _bfd_elf_strtab_emit (abfd, strtab))
b34976b6 4329 return FALSE;
ef10c3ac 4330 _bfd_elf_strtab_free (strtab);
252b5132
RH
4331 }
4332
b34976b6 4333 abfd->output_has_begun = TRUE;
252b5132 4334
b34976b6 4335 return TRUE;
252b5132
RH
4336}
4337
8ded5a0f
AM
4338/* Make an initial estimate of the size of the program header. If we
4339 get the number wrong here, we'll redo section placement. */
4340
4341static bfd_size_type
4342get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4343{
4344 size_t segs;
4345 asection *s;
2b05f1b7 4346 const struct elf_backend_data *bed;
8ded5a0f
AM
4347
4348 /* Assume we will need exactly two PT_LOAD segments: one for text
4349 and one for data. */
4350 segs = 2;
4351
4352 s = bfd_get_section_by_name (abfd, ".interp");
1b9e270b 4353 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
8ded5a0f
AM
4354 {
4355 /* If we have a loadable interpreter section, we need a
4356 PT_INTERP segment. In this case, assume we also need a
4357 PT_PHDR segment, although that may not be true for all
4358 targets. */
e9a38e0f 4359 segs += 2;
8ded5a0f
AM
4360 }
4361
4362 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4363 {
4364 /* We need a PT_DYNAMIC segment. */
4365 ++segs;
f210dcff 4366 }
08a40648 4367
ceae84aa 4368 if (info != NULL && info->relro)
f210dcff
L
4369 {
4370 /* We need a PT_GNU_RELRO segment. */
4371 ++segs;
8ded5a0f
AM
4372 }
4373
12bd6957 4374 if (elf_eh_frame_hdr (abfd))
8ded5a0f
AM
4375 {
4376 /* We need a PT_GNU_EH_FRAME segment. */
4377 ++segs;
4378 }
4379
12bd6957 4380 if (elf_stack_flags (abfd))
8ded5a0f 4381 {
2b05f1b7
L
4382 /* We need a PT_GNU_STACK segment. */
4383 ++segs;
4384 }
94b11780 4385
0a59decb
L
4386 s = bfd_get_section_by_name (abfd,
4387 NOTE_GNU_PROPERTY_SECTION_NAME);
4388 if (s != NULL && s->size != 0)
4389 {
4390 /* We need a PT_GNU_PROPERTY segment. */
4391 ++segs;
4392 }
4393
2b05f1b7
L
4394 for (s = abfd->sections; s != NULL; s = s->next)
4395 {
8ded5a0f 4396 if ((s->flags & SEC_LOAD) != 0
23e463ed 4397 && elf_section_type (s) == SHT_NOTE)
8ded5a0f 4398 {
23e463ed 4399 unsigned int alignment_power;
8ded5a0f
AM
4400 /* We need a PT_NOTE segment. */
4401 ++segs;
23e463ed
L
4402 /* Try to create just one PT_NOTE segment for all adjacent
4403 loadable SHT_NOTE sections. gABI requires that within a
4404 PT_NOTE segment (and also inside of each SHT_NOTE section)
4405 each note should have the same alignment. So we check
4406 whether the sections are correctly aligned. */
4407 alignment_power = s->alignment_power;
4408 while (s->next != NULL
4409 && s->next->alignment_power == alignment_power
4410 && (s->next->flags & SEC_LOAD) != 0
4411 && elf_section_type (s->next) == SHT_NOTE)
4412 s = s->next;
8ded5a0f
AM
4413 }
4414 }
4415
4416 for (s = abfd->sections; s != NULL; s = s->next)
4417 {
4418 if (s->flags & SEC_THREAD_LOCAL)
4419 {
4420 /* We need a PT_TLS segment. */
4421 ++segs;
4422 break;
4423 }
4424 }
4425
2b05f1b7 4426 bed = get_elf_backend_data (abfd);
a91e1603
L
4427
4428 if ((abfd->flags & D_PAGED) != 0)
4429 {
4430 /* Add a PT_GNU_MBIND segment for each mbind section. */
4431 unsigned int page_align_power = bfd_log2 (bed->commonpagesize);
4432 for (s = abfd->sections; s != NULL; s = s->next)
4433 if (elf_section_flags (s) & SHF_GNU_MBIND)
4434 {
4435 if (elf_section_data (s)->this_hdr.sh_info
4436 > PT_GNU_MBIND_NUM)
4437 {
4438 _bfd_error_handler
4439 /* xgettext:c-format */
871b3ab2 4440 (_("%pB: GNU_MBIN section `%pA' has invalid sh_info field: %d"),
a91e1603
L
4441 abfd, s, elf_section_data (s)->this_hdr.sh_info);
4442 continue;
4443 }
4444 /* Align mbind section to page size. */
4445 if (s->alignment_power < page_align_power)
4446 s->alignment_power = page_align_power;
4447 segs ++;
4448 }
4449 }
4450
4451 /* Let the backend count up any program headers it might need. */
4452 if (bed->elf_backend_additional_program_headers)
8ded5a0f
AM
4453 {
4454 int a;
4455
4456 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4457 if (a == -1)
4458 abort ();
4459 segs += a;
4460 }
4461
4462 return segs * bed->s->sizeof_phdr;
4463}
4464
2ea37f1c
NC
4465/* Find the segment that contains the output_section of section. */
4466
4467Elf_Internal_Phdr *
4468_bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4469{
4470 struct elf_segment_map *m;
4471 Elf_Internal_Phdr *p;
4472
12bd6957 4473 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
2ea37f1c
NC
4474 m != NULL;
4475 m = m->next, p++)
4476 {
4477 int i;
4478
4479 for (i = m->count - 1; i >= 0; i--)
4480 if (m->sections[i] == section)
4481 return p;
4482 }
4483
4484 return NULL;
4485}
4486
252b5132
RH
4487/* Create a mapping from a set of sections to a program segment. */
4488
217aa764
AM
4489static struct elf_segment_map *
4490make_mapping (bfd *abfd,
4491 asection **sections,
4492 unsigned int from,
4493 unsigned int to,
4494 bfd_boolean phdr)
252b5132
RH
4495{
4496 struct elf_segment_map *m;
4497 unsigned int i;
4498 asection **hdrpp;
dc810e39 4499 bfd_size_type amt;
252b5132 4500
00bee008
AM
4501 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4502 amt += (to - from) * sizeof (asection *);
a50b1753 4503 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
4504 if (m == NULL)
4505 return NULL;
4506 m->next = NULL;
4507 m->p_type = PT_LOAD;
4508 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4509 m->sections[i - from] = *hdrpp;
4510 m->count = to - from;
4511
4512 if (from == 0 && phdr)
4513 {
4514 /* Include the headers in the first PT_LOAD segment. */
4515 m->includes_filehdr = 1;
4516 m->includes_phdrs = 1;
4517 }
4518
4519 return m;
4520}
4521
229fcec5
MM
4522/* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4523 on failure. */
4524
4525struct elf_segment_map *
4526_bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4527{
4528 struct elf_segment_map *m;
4529
a50b1753 4530 m = (struct elf_segment_map *) bfd_zalloc (abfd,
07d6d2b8 4531 sizeof (struct elf_segment_map));
229fcec5
MM
4532 if (m == NULL)
4533 return NULL;
4534 m->next = NULL;
4535 m->p_type = PT_DYNAMIC;
4536 m->count = 1;
4537 m->sections[0] = dynsec;
08a40648 4538
229fcec5
MM
4539 return m;
4540}
4541
8ded5a0f 4542/* Possibly add or remove segments from the segment map. */
252b5132 4543
b34976b6 4544static bfd_boolean
3dea8fca
AM
4545elf_modify_segment_map (bfd *abfd,
4546 struct bfd_link_info *info,
4547 bfd_boolean remove_empty_load)
252b5132 4548{
252e386e 4549 struct elf_segment_map **m;
8ded5a0f 4550 const struct elf_backend_data *bed;
252b5132 4551
8ded5a0f
AM
4552 /* The placement algorithm assumes that non allocated sections are
4553 not in PT_LOAD segments. We ensure this here by removing such
4554 sections from the segment map. We also remove excluded
252e386e
AM
4555 sections. Finally, any PT_LOAD segment without sections is
4556 removed. */
12bd6957 4557 m = &elf_seg_map (abfd);
252e386e 4558 while (*m)
8ded5a0f
AM
4559 {
4560 unsigned int i, new_count;
252b5132 4561
252e386e 4562 for (new_count = 0, i = 0; i < (*m)->count; i++)
8ded5a0f 4563 {
252e386e
AM
4564 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4565 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4566 || (*m)->p_type != PT_LOAD))
8ded5a0f 4567 {
252e386e
AM
4568 (*m)->sections[new_count] = (*m)->sections[i];
4569 new_count++;
8ded5a0f
AM
4570 }
4571 }
252e386e 4572 (*m)->count = new_count;
252b5132 4573
1a9ccd70
NC
4574 if (remove_empty_load
4575 && (*m)->p_type == PT_LOAD
4576 && (*m)->count == 0
4577 && !(*m)->includes_phdrs)
252e386e
AM
4578 *m = (*m)->next;
4579 else
4580 m = &(*m)->next;
8ded5a0f 4581 }
252b5132 4582
8ded5a0f
AM
4583 bed = get_elf_backend_data (abfd);
4584 if (bed->elf_backend_modify_segment_map != NULL)
252b5132 4585 {
252e386e 4586 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
8ded5a0f 4587 return FALSE;
252b5132 4588 }
252b5132 4589
8ded5a0f
AM
4590 return TRUE;
4591}
252b5132 4592
dbc88fc1
AM
4593#define IS_TBSS(s) \
4594 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4595
8ded5a0f 4596/* Set up a mapping from BFD sections to program segments. */
252b5132 4597
8ded5a0f
AM
4598bfd_boolean
4599_bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
4600{
4601 unsigned int count;
4602 struct elf_segment_map *m;
4603 asection **sections = NULL;
4604 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3dea8fca 4605 bfd_boolean no_user_phdrs;
252b5132 4606
12bd6957 4607 no_user_phdrs = elf_seg_map (abfd) == NULL;
d324f6d6
RM
4608
4609 if (info != NULL)
4610 info->user_phdrs = !no_user_phdrs;
4611
3dea8fca 4612 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
252b5132 4613 {
8ded5a0f
AM
4614 asection *s;
4615 unsigned int i;
4616 struct elf_segment_map *mfirst;
4617 struct elf_segment_map **pm;
4618 asection *last_hdr;
4619 bfd_vma last_size;
00bee008 4620 unsigned int hdr_index;
8ded5a0f
AM
4621 bfd_vma maxpagesize;
4622 asection **hdrpp;
64029e93 4623 bfd_boolean phdr_in_segment;
8ded5a0f 4624 bfd_boolean writable;
2888249f 4625 bfd_boolean executable;
8ded5a0f
AM
4626 int tls_count = 0;
4627 asection *first_tls = NULL;
a91e1603 4628 asection *first_mbind = NULL;
8ded5a0f
AM
4629 asection *dynsec, *eh_frame_hdr;
4630 bfd_size_type amt;
8d06853e 4631 bfd_vma addr_mask, wrap_to = 0;
64029e93 4632 bfd_size_type phdr_size;
252b5132 4633
8ded5a0f 4634 /* Select the allocated sections, and sort them. */
252b5132 4635
a50b1753 4636 sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
07d6d2b8 4637 sizeof (asection *));
8ded5a0f 4638 if (sections == NULL)
252b5132 4639 goto error_return;
252b5132 4640
8d06853e
AM
4641 /* Calculate top address, avoiding undefined behaviour of shift
4642 left operator when shift count is equal to size of type
4643 being shifted. */
4644 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4645 addr_mask = (addr_mask << 1) + 1;
4646
8ded5a0f
AM
4647 i = 0;
4648 for (s = abfd->sections; s != NULL; s = s->next)
4649 {
4650 if ((s->flags & SEC_ALLOC) != 0)
4651 {
4652 sections[i] = s;
4653 ++i;
8d06853e
AM
4654 /* A wrapping section potentially clashes with header. */
4655 if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask))
4656 wrap_to = (s->lma + s->size) & addr_mask;
8ded5a0f
AM
4657 }
4658 }
4659 BFD_ASSERT (i <= bfd_count_sections (abfd));
4660 count = i;
252b5132 4661
8ded5a0f 4662 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
252b5132 4663
64029e93
AM
4664 phdr_size = elf_program_header_size (abfd);
4665 if (phdr_size == (bfd_size_type) -1)
4666 phdr_size = get_program_header_size (abfd, info);
4667 phdr_size += bed->s->sizeof_ehdr;
4668 maxpagesize = bed->maxpagesize;
4669 if (maxpagesize == 0)
4670 maxpagesize = 1;
4671 phdr_in_segment = info != NULL && info->load_phdrs;
4672 if (count != 0
4673 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
4674 >= (phdr_size & (maxpagesize - 1))))
4675 /* For compatibility with old scripts that may not be using
4676 SIZEOF_HEADERS, add headers when it looks like space has
4677 been left for them. */
4678 phdr_in_segment = TRUE;
252b5132 4679
64029e93 4680 /* Build the mapping. */
8ded5a0f
AM
4681 mfirst = NULL;
4682 pm = &mfirst;
252b5132 4683
8ded5a0f
AM
4684 /* If we have a .interp section, then create a PT_PHDR segment for
4685 the program headers and a PT_INTERP segment for the .interp
4686 section. */
4687 s = bfd_get_section_by_name (abfd, ".interp");
1b9e270b 4688 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
8ded5a0f
AM
4689 {
4690 amt = sizeof (struct elf_segment_map);
a50b1753 4691 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4692 if (m == NULL)
4693 goto error_return;
4694 m->next = NULL;
4695 m->p_type = PT_PHDR;
f882209d 4696 m->p_flags = PF_R;
8ded5a0f
AM
4697 m->p_flags_valid = 1;
4698 m->includes_phdrs = 1;
64029e93 4699 phdr_in_segment = TRUE;
8ded5a0f
AM
4700 *pm = m;
4701 pm = &m->next;
252b5132 4702
8ded5a0f 4703 amt = sizeof (struct elf_segment_map);
a50b1753 4704 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4705 if (m == NULL)
4706 goto error_return;
4707 m->next = NULL;
4708 m->p_type = PT_INTERP;
4709 m->count = 1;
4710 m->sections[0] = s;
4711
4712 *pm = m;
4713 pm = &m->next;
252b5132 4714 }
8ded5a0f
AM
4715
4716 /* Look through the sections. We put sections in the same program
4717 segment when the start of the second section can be placed within
4718 a few bytes of the end of the first section. */
4719 last_hdr = NULL;
4720 last_size = 0;
00bee008 4721 hdr_index = 0;
8ded5a0f 4722 writable = FALSE;
2888249f 4723 executable = FALSE;
8ded5a0f
AM
4724 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4725 if (dynsec != NULL
4726 && (dynsec->flags & SEC_LOAD) == 0)
4727 dynsec = NULL;
4728
64029e93
AM
4729 if ((abfd->flags & D_PAGED) == 0)
4730 phdr_in_segment = FALSE;
4731
8ded5a0f
AM
4732 /* Deal with -Ttext or something similar such that the first section
4733 is not adjacent to the program headers. This is an
4734 approximation, since at this point we don't know exactly how many
4735 program headers we will need. */
64029e93 4736 if (phdr_in_segment && count > 0)
252b5132 4737 {
64029e93
AM
4738 bfd_vma phdr_lma;
4739 bfd_boolean separate_phdr = FALSE;
4740
4741 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
4742 if (info != NULL
4743 && info->separate_code
4744 && (sections[0]->flags & SEC_CODE) != 0)
1a9ccd70 4745 {
64029e93
AM
4746 /* If data sections should be separate from code and
4747 thus not executable, and the first section is
4748 executable then put the file and program headers in
4749 their own PT_LOAD. */
4750 separate_phdr = TRUE;
4751 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
4752 == (sections[0]->lma & addr_mask & -maxpagesize)))
4753 {
4754 /* The file and program headers are currently on the
4755 same page as the first section. Put them on the
4756 previous page if we can. */
4757 if (phdr_lma >= maxpagesize)
4758 phdr_lma -= maxpagesize;
4759 else
4760 separate_phdr = FALSE;
4761 }
4762 }
4763 if ((sections[0]->lma & addr_mask) < phdr_lma
4764 || (sections[0]->lma & addr_mask) < phdr_size)
4765 /* If file and program headers would be placed at the end
4766 of memory then it's probably better to omit them. */
4767 phdr_in_segment = FALSE;
4768 else if (phdr_lma < wrap_to)
4769 /* If a section wraps around to where we'll be placing
4770 file and program headers, then the headers will be
4771 overwritten. */
4772 phdr_in_segment = FALSE;
4773 else if (separate_phdr)
4774 {
4775 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
4776 if (m == NULL)
4777 goto error_return;
4778 m->p_paddr = phdr_lma;
4779 m->p_vaddr_offset
4780 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
4781 m->p_paddr_valid = 1;
4782 *pm = m;
4783 pm = &m->next;
4784 phdr_in_segment = FALSE;
1a9ccd70 4785 }
252b5132
RH
4786 }
4787
8ded5a0f 4788 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
252b5132 4789 {
8ded5a0f
AM
4790 asection *hdr;
4791 bfd_boolean new_segment;
4792
4793 hdr = *hdrpp;
4794
4795 /* See if this section and the last one will fit in the same
4796 segment. */
4797
4798 if (last_hdr == NULL)
4799 {
4800 /* If we don't have a segment yet, then we don't need a new
4801 one (we build the last one after this loop). */
4802 new_segment = FALSE;
4803 }
4804 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4805 {
4806 /* If this section has a different relation between the
4807 virtual address and the load address, then we need a new
4808 segment. */
4809 new_segment = TRUE;
4810 }
b5599592
AM
4811 else if (hdr->lma < last_hdr->lma + last_size
4812 || last_hdr->lma + last_size < last_hdr->lma)
4813 {
4814 /* If this section has a load address that makes it overlap
4815 the previous section, then we need a new segment. */
4816 new_segment = TRUE;
4817 }
76cb3a89
AM
4818 else if ((abfd->flags & D_PAGED) != 0
4819 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4820 == (hdr->lma & -maxpagesize)))
4821 {
4822 /* If we are demand paged then we can't map two disk
4823 pages onto the same memory page. */
4824 new_segment = FALSE;
4825 }
39948a60
NC
4826 /* In the next test we have to be careful when last_hdr->lma is close
4827 to the end of the address space. If the aligned address wraps
4828 around to the start of the address space, then there are no more
4829 pages left in memory and it is OK to assume that the current
4830 section can be included in the current segment. */
76cb3a89
AM
4831 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4832 + maxpagesize > last_hdr->lma)
4833 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4834 + maxpagesize <= hdr->lma))
8ded5a0f
AM
4835 {
4836 /* If putting this section in this segment would force us to
4837 skip a page in the segment, then we need a new segment. */
4838 new_segment = TRUE;
4839 }
4840 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
76cb3a89 4841 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
8ded5a0f 4842 {
e5654c0f
AM
4843 /* We don't want to put a loaded section after a
4844 nonloaded (ie. bss style) section in the same segment
4845 as that will force the non-loaded section to be loaded.
76cb3a89 4846 Consider .tbss sections as loaded for this purpose. */
8ded5a0f
AM
4847 new_segment = TRUE;
4848 }
4849 else if ((abfd->flags & D_PAGED) == 0)
4850 {
4851 /* If the file is not demand paged, which means that we
4852 don't require the sections to be correctly aligned in the
4853 file, then there is no other reason for a new segment. */
4854 new_segment = FALSE;
4855 }
2888249f
L
4856 else if (info != NULL
4857 && info->separate_code
4858 && executable != ((hdr->flags & SEC_CODE) != 0))
4859 {
4860 new_segment = TRUE;
4861 }
8ded5a0f 4862 else if (! writable
76cb3a89 4863 && (hdr->flags & SEC_READONLY) == 0)
8ded5a0f
AM
4864 {
4865 /* We don't want to put a writable section in a read only
76cb3a89 4866 segment. */
8ded5a0f
AM
4867 new_segment = TRUE;
4868 }
4869 else
4870 {
4871 /* Otherwise, we can use the same segment. */
4872 new_segment = FALSE;
4873 }
4874
2889e75b 4875 /* Allow interested parties a chance to override our decision. */
ceae84aa
AM
4876 if (last_hdr != NULL
4877 && info != NULL
4878 && info->callbacks->override_segment_assignment != NULL)
4879 new_segment
4880 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4881 last_hdr,
4882 new_segment);
2889e75b 4883
8ded5a0f
AM
4884 if (! new_segment)
4885 {
4886 if ((hdr->flags & SEC_READONLY) == 0)
4887 writable = TRUE;
2888249f
L
4888 if ((hdr->flags & SEC_CODE) != 0)
4889 executable = TRUE;
8ded5a0f
AM
4890 last_hdr = hdr;
4891 /* .tbss sections effectively have zero size. */
dbc88fc1 4892 last_size = !IS_TBSS (hdr) ? hdr->size : 0;
8ded5a0f
AM
4893 continue;
4894 }
4895
4896 /* We need a new program segment. We must create a new program
00bee008 4897 header holding all the sections from hdr_index until hdr. */
8ded5a0f 4898
00bee008 4899 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
8ded5a0f
AM
4900 if (m == NULL)
4901 goto error_return;
4902
4903 *pm = m;
4904 pm = &m->next;
4905
252b5132 4906 if ((hdr->flags & SEC_READONLY) == 0)
b34976b6 4907 writable = TRUE;
8ded5a0f
AM
4908 else
4909 writable = FALSE;
4910
2888249f
L
4911 if ((hdr->flags & SEC_CODE) == 0)
4912 executable = FALSE;
4913 else
4914 executable = TRUE;
4915
baaff79e
JJ
4916 last_hdr = hdr;
4917 /* .tbss sections effectively have zero size. */
dbc88fc1 4918 last_size = !IS_TBSS (hdr) ? hdr->size : 0;
00bee008 4919 hdr_index = i;
8ded5a0f 4920 phdr_in_segment = FALSE;
252b5132
RH
4921 }
4922
86b2281f
AM
4923 /* Create a final PT_LOAD program segment, but not if it's just
4924 for .tbss. */
4925 if (last_hdr != NULL
00bee008 4926 && (i - hdr_index != 1
dbc88fc1 4927 || !IS_TBSS (last_hdr)))
8ded5a0f 4928 {
00bee008 4929 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
8ded5a0f
AM
4930 if (m == NULL)
4931 goto error_return;
252b5132 4932
8ded5a0f
AM
4933 *pm = m;
4934 pm = &m->next;
4935 }
252b5132 4936
8ded5a0f
AM
4937 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4938 if (dynsec != NULL)
4939 {
4940 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4941 if (m == NULL)
4942 goto error_return;
4943 *pm = m;
4944 pm = &m->next;
4945 }
252b5132 4946
23e463ed 4947 /* For each batch of consecutive loadable SHT_NOTE sections,
1c5265b5
JJ
4948 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4949 because if we link together nonloadable .note sections and
4950 loadable .note sections, we will generate two .note sections
23e463ed 4951 in the output file. */
8ded5a0f
AM
4952 for (s = abfd->sections; s != NULL; s = s->next)
4953 {
4954 if ((s->flags & SEC_LOAD) != 0
23e463ed 4955 && elf_section_type (s) == SHT_NOTE)
8ded5a0f 4956 {
1c5265b5 4957 asection *s2;
23e463ed 4958 unsigned int alignment_power = s->alignment_power;
91d6fa6a
NC
4959
4960 count = 1;
23e463ed
L
4961 for (s2 = s; s2->next != NULL; s2 = s2->next)
4962 {
4963 if (s2->next->alignment_power == alignment_power
4964 && (s2->next->flags & SEC_LOAD) != 0
4965 && elf_section_type (s2->next) == SHT_NOTE
4966 && align_power (s2->lma + s2->size,
4967 alignment_power)
4968 == s2->next->lma)
4969 count++;
4970 else
4971 break;
4972 }
00bee008
AM
4973 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4974 amt += count * sizeof (asection *);
a50b1753 4975 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4976 if (m == NULL)
4977 goto error_return;
4978 m->next = NULL;
4979 m->p_type = PT_NOTE;
1c5265b5
JJ
4980 m->count = count;
4981 while (count > 1)
4982 {
4983 m->sections[m->count - count--] = s;
4984 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4985 s = s->next;
4986 }
4987 m->sections[m->count - 1] = s;
4988 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
8ded5a0f
AM
4989 *pm = m;
4990 pm = &m->next;
4991 }
4992 if (s->flags & SEC_THREAD_LOCAL)
4993 {
4994 if (! tls_count)
4995 first_tls = s;
4996 tls_count++;
4997 }
a91e1603
L
4998 if (first_mbind == NULL
4999 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5000 first_mbind = s;
8ded5a0f 5001 }
252b5132 5002
8ded5a0f
AM
5003 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5004 if (tls_count > 0)
5005 {
00bee008
AM
5006 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5007 amt += tls_count * sizeof (asection *);
a50b1753 5008 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5009 if (m == NULL)
5010 goto error_return;
5011 m->next = NULL;
5012 m->p_type = PT_TLS;
5013 m->count = tls_count;
5014 /* Mandated PF_R. */
5015 m->p_flags = PF_R;
5016 m->p_flags_valid = 1;
d923cae0 5017 s = first_tls;
91d6fa6a 5018 for (i = 0; i < (unsigned int) tls_count; ++i)
8ded5a0f 5019 {
d923cae0
L
5020 if ((s->flags & SEC_THREAD_LOCAL) == 0)
5021 {
5022 _bfd_error_handler
871b3ab2 5023 (_("%pB: TLS sections are not adjacent:"), abfd);
d923cae0
L
5024 s = first_tls;
5025 i = 0;
5026 while (i < (unsigned int) tls_count)
5027 {
5028 if ((s->flags & SEC_THREAD_LOCAL) != 0)
5029 {
871b3ab2 5030 _bfd_error_handler (_(" TLS: %pA"), s);
d923cae0
L
5031 i++;
5032 }
5033 else
871b3ab2 5034 _bfd_error_handler (_(" non-TLS: %pA"), s);
d923cae0
L
5035 s = s->next;
5036 }
5037 bfd_set_error (bfd_error_bad_value);
5038 goto error_return;
5039 }
5040 m->sections[i] = s;
5041 s = s->next;
8ded5a0f 5042 }
252b5132 5043
8ded5a0f
AM
5044 *pm = m;
5045 pm = &m->next;
5046 }
252b5132 5047
a91e1603
L
5048 if (first_mbind && (abfd->flags & D_PAGED) != 0)
5049 for (s = first_mbind; s != NULL; s = s->next)
5050 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5051 && (elf_section_data (s)->this_hdr.sh_info
5052 <= PT_GNU_MBIND_NUM))
5053 {
5054 /* Mandated PF_R. */
5055 unsigned long p_flags = PF_R;
5056 if ((s->flags & SEC_READONLY) == 0)
5057 p_flags |= PF_W;
5058 if ((s->flags & SEC_CODE) != 0)
5059 p_flags |= PF_X;
5060
5061 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5062 m = bfd_zalloc (abfd, amt);
5063 if (m == NULL)
5064 goto error_return;
5065 m->next = NULL;
5066 m->p_type = (PT_GNU_MBIND_LO
5067 + elf_section_data (s)->this_hdr.sh_info);
5068 m->count = 1;
5069 m->p_flags_valid = 1;
5070 m->sections[0] = s;
5071 m->p_flags = p_flags;
5072
5073 *pm = m;
5074 pm = &m->next;
5075 }
5076
0a59decb
L
5077 s = bfd_get_section_by_name (abfd,
5078 NOTE_GNU_PROPERTY_SECTION_NAME);
5079 if (s != NULL && s->size != 0)
5080 {
5081 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5082 m = bfd_zalloc (abfd, amt);
5083 if (m == NULL)
5084 goto error_return;
5085 m->next = NULL;
5086 m->p_type = PT_GNU_PROPERTY;
5087 m->count = 1;
5088 m->p_flags_valid = 1;
5089 m->sections[0] = s;
5090 m->p_flags = PF_R;
5091 *pm = m;
5092 pm = &m->next;
5093 }
5094
8ded5a0f
AM
5095 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5096 segment. */
12bd6957 5097 eh_frame_hdr = elf_eh_frame_hdr (abfd);
8ded5a0f
AM
5098 if (eh_frame_hdr != NULL
5099 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
252b5132 5100 {
dc810e39 5101 amt = sizeof (struct elf_segment_map);
a50b1753 5102 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
5103 if (m == NULL)
5104 goto error_return;
5105 m->next = NULL;
8ded5a0f 5106 m->p_type = PT_GNU_EH_FRAME;
252b5132 5107 m->count = 1;
8ded5a0f 5108 m->sections[0] = eh_frame_hdr->output_section;
252b5132
RH
5109
5110 *pm = m;
5111 pm = &m->next;
5112 }
13ae64f3 5113
12bd6957 5114 if (elf_stack_flags (abfd))
13ae64f3 5115 {
8ded5a0f 5116 amt = sizeof (struct elf_segment_map);
a50b1753 5117 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5118 if (m == NULL)
5119 goto error_return;
5120 m->next = NULL;
2b05f1b7 5121 m->p_type = PT_GNU_STACK;
12bd6957 5122 m->p_flags = elf_stack_flags (abfd);
04c3a755 5123 m->p_align = bed->stack_align;
8ded5a0f 5124 m->p_flags_valid = 1;
04c3a755
NS
5125 m->p_align_valid = m->p_align != 0;
5126 if (info->stacksize > 0)
5127 {
5128 m->p_size = info->stacksize;
5129 m->p_size_valid = 1;
5130 }
252b5132 5131
8ded5a0f
AM
5132 *pm = m;
5133 pm = &m->next;
5134 }
65765700 5135
ceae84aa 5136 if (info != NULL && info->relro)
8ded5a0f 5137 {
f210dcff
L
5138 for (m = mfirst; m != NULL; m = m->next)
5139 {
3832a4d8
AM
5140 if (m->p_type == PT_LOAD
5141 && m->count != 0
5142 && m->sections[0]->vma >= info->relro_start
5143 && m->sections[0]->vma < info->relro_end)
f210dcff 5144 {
3832a4d8
AM
5145 i = m->count;
5146 while (--i != (unsigned) -1)
5147 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
5148 == (SEC_LOAD | SEC_HAS_CONTENTS))
5149 break;
5150
43a8475c 5151 if (i != (unsigned) -1)
f210dcff
L
5152 break;
5153 }
be01b344 5154 }
f210dcff
L
5155
5156 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5157 if (m != NULL)
5158 {
5159 amt = sizeof (struct elf_segment_map);
a50b1753 5160 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
f210dcff
L
5161 if (m == NULL)
5162 goto error_return;
5163 m->next = NULL;
5164 m->p_type = PT_GNU_RELRO;
f210dcff
L
5165 *pm = m;
5166 pm = &m->next;
5167 }
8ded5a0f 5168 }
9ee5e499 5169
8ded5a0f 5170 free (sections);
12bd6957 5171 elf_seg_map (abfd) = mfirst;
9ee5e499
JJ
5172 }
5173
3dea8fca 5174 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
8ded5a0f 5175 return FALSE;
8c37241b 5176
12bd6957 5177 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
8ded5a0f 5178 ++count;
12bd6957 5179 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
252b5132 5180
b34976b6 5181 return TRUE;
252b5132
RH
5182
5183 error_return:
5184 if (sections != NULL)
5185 free (sections);
b34976b6 5186 return FALSE;
252b5132
RH
5187}
5188
5189/* Sort sections by address. */
5190
5191static int
217aa764 5192elf_sort_sections (const void *arg1, const void *arg2)
252b5132
RH
5193{
5194 const asection *sec1 = *(const asection **) arg1;
5195 const asection *sec2 = *(const asection **) arg2;
eecdbe52 5196 bfd_size_type size1, size2;
252b5132
RH
5197
5198 /* Sort by LMA first, since this is the address used to
5199 place the section into a segment. */
5200 if (sec1->lma < sec2->lma)
5201 return -1;
5202 else if (sec1->lma > sec2->lma)
5203 return 1;
5204
5205 /* Then sort by VMA. Normally the LMA and the VMA will be
5206 the same, and this will do nothing. */
5207 if (sec1->vma < sec2->vma)
5208 return -1;
5209 else if (sec1->vma > sec2->vma)
5210 return 1;
5211
5212 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5213
07c6e936 5214#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
252b5132
RH
5215
5216 if (TOEND (sec1))
5217 {
5218 if (TOEND (sec2))
00a7cdc5 5219 {
67ce483b 5220 /* If the indices are the same, do not return 0
00a7cdc5
NC
5221 here, but continue to try the next comparison. */
5222 if (sec1->target_index - sec2->target_index != 0)
5223 return sec1->target_index - sec2->target_index;
5224 }
252b5132
RH
5225 else
5226 return 1;
5227 }
00a7cdc5 5228 else if (TOEND (sec2))
252b5132
RH
5229 return -1;
5230
5231#undef TOEND
5232
00a7cdc5
NC
5233 /* Sort by size, to put zero sized sections
5234 before others at the same address. */
252b5132 5235
eea6121a
AM
5236 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5237 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
eecdbe52
JJ
5238
5239 if (size1 < size2)
252b5132 5240 return -1;
eecdbe52 5241 if (size1 > size2)
252b5132
RH
5242 return 1;
5243
5244 return sec1->target_index - sec2->target_index;
5245}
5246
340b6d91
AC
5247/* Ian Lance Taylor writes:
5248
5249 We shouldn't be using % with a negative signed number. That's just
5250 not good. We have to make sure either that the number is not
5251 negative, or that the number has an unsigned type. When the types
5252 are all the same size they wind up as unsigned. When file_ptr is a
5253 larger signed type, the arithmetic winds up as signed long long,
5254 which is wrong.
5255
5256 What we're trying to say here is something like ``increase OFF by
5257 the least amount that will cause it to be equal to the VMA modulo
5258 the page size.'' */
5259/* In other words, something like:
5260
5261 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5262 off_offset = off % bed->maxpagesize;
5263 if (vma_offset < off_offset)
5264 adjustment = vma_offset + bed->maxpagesize - off_offset;
5265 else
5266 adjustment = vma_offset - off_offset;
08a40648 5267
de194d85 5268 which can be collapsed into the expression below. */
340b6d91
AC
5269
5270static file_ptr
5271vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5272{
dc9155b2
NC
5273 /* PR binutils/16199: Handle an alignment of zero. */
5274 if (maxpagesize == 0)
5275 maxpagesize = 1;
340b6d91
AC
5276 return ((vma - off) % maxpagesize);
5277}
5278
6d33f217
L
5279static void
5280print_segment_map (const struct elf_segment_map *m)
5281{
5282 unsigned int j;
5283 const char *pt = get_segment_type (m->p_type);
5284 char buf[32];
5285
5286 if (pt == NULL)
5287 {
5288 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5289 sprintf (buf, "LOPROC+%7.7x",
5290 (unsigned int) (m->p_type - PT_LOPROC));
5291 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5292 sprintf (buf, "LOOS+%7.7x",
5293 (unsigned int) (m->p_type - PT_LOOS));
5294 else
5295 snprintf (buf, sizeof (buf), "%8.8x",
5296 (unsigned int) m->p_type);
5297 pt = buf;
5298 }
4a97a0e5 5299 fflush (stdout);
6d33f217
L
5300 fprintf (stderr, "%s:", pt);
5301 for (j = 0; j < m->count; j++)
5302 fprintf (stderr, " %s", m->sections [j]->name);
5303 putc ('\n',stderr);
4a97a0e5 5304 fflush (stderr);
6d33f217
L
5305}
5306
32812159
AM
5307static bfd_boolean
5308write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
5309{
5310 void *buf;
5311 bfd_boolean ret;
5312
5313 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5314 return FALSE;
5315 buf = bfd_zmalloc (len);
5316 if (buf == NULL)
5317 return FALSE;
5318 ret = bfd_bwrite (buf, len, abfd) == len;
5319 free (buf);
5320 return ret;
5321}
5322
252b5132
RH
5323/* Assign file positions to the sections based on the mapping from
5324 sections to segments. This function also sets up some fields in
f3520d2f 5325 the file header. */
252b5132 5326
b34976b6 5327static bfd_boolean
f3520d2f
AM
5328assign_file_positions_for_load_sections (bfd *abfd,
5329 struct bfd_link_info *link_info)
252b5132
RH
5330{
5331 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 5332 struct elf_segment_map *m;
252b5132 5333 Elf_Internal_Phdr *phdrs;
252b5132 5334 Elf_Internal_Phdr *p;
02bf8d82 5335 file_ptr off;
3f570048 5336 bfd_size_type maxpagesize;
a8c75b76 5337 unsigned int pt_load_count = 0;
f3520d2f 5338 unsigned int alloc;
0920dee7 5339 unsigned int i, j;
2b0bc088 5340 bfd_vma header_pad = 0;
252b5132 5341
e36284ab 5342 if (link_info == NULL
ceae84aa 5343 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
8ded5a0f 5344 return FALSE;
252b5132 5345
8ded5a0f 5346 alloc = 0;
12bd6957 5347 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
2b0bc088
NC
5348 {
5349 ++alloc;
5350 if (m->header_size)
5351 header_pad = m->header_size;
5352 }
252b5132 5353
82f2dbf7
NC
5354 if (alloc)
5355 {
5356 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5357 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5358 }
5359 else
5360 {
5361 /* PR binutils/12467. */
5362 elf_elfheader (abfd)->e_phoff = 0;
5363 elf_elfheader (abfd)->e_phentsize = 0;
5364 }
d324f6d6 5365
8ded5a0f 5366 elf_elfheader (abfd)->e_phnum = alloc;
252b5132 5367
12bd6957
AM
5368 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
5369 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
8ded5a0f 5370 else
12bd6957 5371 BFD_ASSERT (elf_program_header_size (abfd)
59e0647f 5372 >= alloc * bed->s->sizeof_phdr);
252b5132
RH
5373
5374 if (alloc == 0)
f3520d2f 5375 {
12bd6957 5376 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
8ded5a0f 5377 return TRUE;
f3520d2f 5378 }
252b5132 5379
12bd6957 5380 /* We're writing the size in elf_program_header_size (abfd),
57268894
HPN
5381 see assign_file_positions_except_relocs, so make sure we have
5382 that amount allocated, with trailing space cleared.
12bd6957
AM
5383 The variable alloc contains the computed need, while
5384 elf_program_header_size (abfd) contains the size used for the
57268894
HPN
5385 layout.
5386 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5387 where the layout is forced to according to a larger size in the
5388 last iterations for the testcase ld-elf/header. */
12bd6957 5389 BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr
57268894 5390 == 0);
a50b1753
NC
5391 phdrs = (Elf_Internal_Phdr *)
5392 bfd_zalloc2 (abfd,
07d6d2b8
AM
5393 (elf_program_header_size (abfd) / bed->s->sizeof_phdr),
5394 sizeof (Elf_Internal_Phdr));
f3520d2f 5395 elf_tdata (abfd)->phdr = phdrs;
252b5132 5396 if (phdrs == NULL)
b34976b6 5397 return FALSE;
252b5132 5398
3f570048
AM
5399 maxpagesize = 1;
5400 if ((abfd->flags & D_PAGED) != 0)
5401 maxpagesize = bed->maxpagesize;
5402
252b5132
RH
5403 off = bed->s->sizeof_ehdr;
5404 off += alloc * bed->s->sizeof_phdr;
2b0bc088
NC
5405 if (header_pad < (bfd_vma) off)
5406 header_pad = 0;
5407 else
5408 header_pad -= off;
5409 off += header_pad;
252b5132 5410
12bd6957 5411 for (m = elf_seg_map (abfd), p = phdrs, j = 0;
252b5132 5412 m != NULL;
0920dee7 5413 m = m->next, p++, j++)
252b5132 5414 {
252b5132 5415 asection **secpp;
bf988460
AM
5416 bfd_vma off_adjust;
5417 bfd_boolean no_contents;
252b5132
RH
5418
5419 /* If elf_segment_map is not from map_sections_to_segments, the
08a40648 5420 sections may not be correctly ordered. NOTE: sorting should
52e9b619
MS
5421 not be done to the PT_NOTE section of a corefile, which may
5422 contain several pseudo-sections artificially created by bfd.
5423 Sorting these pseudo-sections breaks things badly. */
47d9a591
AM
5424 if (m->count > 1
5425 && !(elf_elfheader (abfd)->e_type == ET_CORE
52e9b619 5426 && m->p_type == PT_NOTE))
252b5132
RH
5427 qsort (m->sections, (size_t) m->count, sizeof (asection *),
5428 elf_sort_sections);
5429
b301b248
AM
5430 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5431 number of sections with contents contributing to both p_filesz
5432 and p_memsz, followed by a number of sections with no contents
5433 that just contribute to p_memsz. In this loop, OFF tracks next
02bf8d82 5434 available file offset for PT_LOAD and PT_NOTE segments. */
252b5132 5435 p->p_type = m->p_type;
28a7f3e7 5436 p->p_flags = m->p_flags;
252b5132 5437
3f570048 5438 if (m->count == 0)
5d695627 5439 p->p_vaddr = m->p_vaddr_offset;
3f570048 5440 else
5d695627 5441 p->p_vaddr = m->sections[0]->vma + m->p_vaddr_offset;
3f570048
AM
5442
5443 if (m->p_paddr_valid)
5444 p->p_paddr = m->p_paddr;
5445 else if (m->count == 0)
5446 p->p_paddr = 0;
5447 else
5d695627 5448 p->p_paddr = m->sections[0]->lma + m->p_vaddr_offset;
3f570048
AM
5449
5450 if (p->p_type == PT_LOAD
5451 && (abfd->flags & D_PAGED) != 0)
5452 {
5453 /* p_align in demand paged PT_LOAD segments effectively stores
5454 the maximum page size. When copying an executable with
5455 objcopy, we set m->p_align from the input file. Use this
5456 value for maxpagesize rather than bed->maxpagesize, which
5457 may be different. Note that we use maxpagesize for PT_TLS
5458 segment alignment later in this function, so we are relying
5459 on at least one PT_LOAD segment appearing before a PT_TLS
5460 segment. */
5461 if (m->p_align_valid)
5462 maxpagesize = m->p_align;
5463
5464 p->p_align = maxpagesize;
a8c75b76 5465 pt_load_count += 1;
3f570048 5466 }
3271a814
NS
5467 else if (m->p_align_valid)
5468 p->p_align = m->p_align;
e970b90a
DJ
5469 else if (m->count == 0)
5470 p->p_align = 1 << bed->s->log_file_align;
3f570048
AM
5471 else
5472 p->p_align = 0;
5473
bf988460
AM
5474 no_contents = FALSE;
5475 off_adjust = 0;
252b5132 5476 if (p->p_type == PT_LOAD
b301b248 5477 && m->count > 0)
252b5132 5478 {
b301b248 5479 bfd_size_type align;
a49e53ed 5480 unsigned int align_power = 0;
b301b248 5481
3271a814
NS
5482 if (m->p_align_valid)
5483 align = p->p_align;
5484 else
252b5132 5485 {
3271a814
NS
5486 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5487 {
5488 unsigned int secalign;
08a40648 5489
3271a814
NS
5490 secalign = bfd_get_section_alignment (abfd, *secpp);
5491 if (secalign > align_power)
5492 align_power = secalign;
5493 }
5494 align = (bfd_size_type) 1 << align_power;
5495 if (align < maxpagesize)
5496 align = maxpagesize;
b301b248 5497 }
252b5132 5498
02bf8d82
AM
5499 for (i = 0; i < m->count; i++)
5500 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
5501 /* If we aren't making room for this section, then
5502 it must be SHT_NOBITS regardless of what we've
5503 set via struct bfd_elf_special_section. */
5504 elf_section_type (m->sections[i]) = SHT_NOBITS;
5505
bf988460 5506 /* Find out whether this segment contains any loadable
aea274d3
AM
5507 sections. */
5508 no_contents = TRUE;
5509 for (i = 0; i < m->count; i++)
5510 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
5511 {
5512 no_contents = FALSE;
5513 break;
5514 }
bf988460 5515
85cfcbfb 5516 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align);
a8c75b76
AM
5517
5518 /* Broken hardware and/or kernel require that files do not
5519 map the same page with different permissions on some hppa
5520 processors. */
5521 if (pt_load_count > 1
5522 && bed->no_page_alias
5523 && (off & (maxpagesize - 1)) != 0
5524 && (off & -maxpagesize) == ((off + off_adjust) & -maxpagesize))
5525 off_adjust += maxpagesize;
bf988460
AM
5526 off += off_adjust;
5527 if (no_contents)
5528 {
5529 /* We shouldn't need to align the segment on disk since
5530 the segment doesn't need file space, but the gABI
5531 arguably requires the alignment and glibc ld.so
5532 checks it. So to comply with the alignment
5533 requirement but not waste file space, we adjust
5534 p_offset for just this segment. (OFF_ADJUST is
5535 subtracted from OFF later.) This may put p_offset
5536 past the end of file, but that shouldn't matter. */
5537 }
5538 else
5539 off_adjust = 0;
252b5132 5540 }
b1a6d0b1
NC
5541 /* Make sure the .dynamic section is the first section in the
5542 PT_DYNAMIC segment. */
5543 else if (p->p_type == PT_DYNAMIC
5544 && m->count > 1
5545 && strcmp (m->sections[0]->name, ".dynamic") != 0)
5546 {
5547 _bfd_error_handler
871b3ab2 5548 (_("%pB: The first section in the PT_DYNAMIC segment"
63a5468a 5549 " is not the .dynamic section"),
b301b248 5550 abfd);
b1a6d0b1
NC
5551 bfd_set_error (bfd_error_bad_value);
5552 return FALSE;
5553 }
3f001e84
JK
5554 /* Set the note section type to SHT_NOTE. */
5555 else if (p->p_type == PT_NOTE)
5556 for (i = 0; i < m->count; i++)
5557 elf_section_type (m->sections[i]) = SHT_NOTE;
252b5132 5558
252b5132
RH
5559 p->p_offset = 0;
5560 p->p_filesz = 0;
5561 p->p_memsz = 0;
5562
5563 if (m->includes_filehdr)
5564 {
bf988460 5565 if (!m->p_flags_valid)
252b5132 5566 p->p_flags |= PF_R;
252b5132
RH
5567 p->p_filesz = bed->s->sizeof_ehdr;
5568 p->p_memsz = bed->s->sizeof_ehdr;
5569 if (m->count > 0)
5570 {
9ab82472
L
5571 if (p->p_vaddr < (bfd_vma) off
5572 || (!m->p_paddr_valid
5573 && p->p_paddr < (bfd_vma) off))
252b5132 5574 {
4eca0228 5575 _bfd_error_handler
9793eb77 5576 (_("%pB: not enough room for program headers,"
63a5468a 5577 " try linking with -N"),
b301b248 5578 abfd);
252b5132 5579 bfd_set_error (bfd_error_bad_value);
b34976b6 5580 return FALSE;
252b5132
RH
5581 }
5582
5583 p->p_vaddr -= off;
bf988460 5584 if (!m->p_paddr_valid)
252b5132
RH
5585 p->p_paddr -= off;
5586 }
252b5132
RH
5587 }
5588
5589 if (m->includes_phdrs)
5590 {
bf988460 5591 if (!m->p_flags_valid)
252b5132
RH
5592 p->p_flags |= PF_R;
5593
f3520d2f 5594 if (!m->includes_filehdr)
252b5132
RH
5595 {
5596 p->p_offset = bed->s->sizeof_ehdr;
5597
5598 if (m->count > 0)
5599 {
252b5132 5600 p->p_vaddr -= off - p->p_offset;
bf988460 5601 if (!m->p_paddr_valid)
252b5132
RH
5602 p->p_paddr -= off - p->p_offset;
5603 }
252b5132
RH
5604 }
5605
5606 p->p_filesz += alloc * bed->s->sizeof_phdr;
5607 p->p_memsz += alloc * bed->s->sizeof_phdr;
2b0bc088
NC
5608 if (m->count)
5609 {
5610 p->p_filesz += header_pad;
5611 p->p_memsz += header_pad;
5612 }
252b5132
RH
5613 }
5614
5615 if (p->p_type == PT_LOAD
5616 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5617 {
bf988460 5618 if (!m->includes_filehdr && !m->includes_phdrs)
02bf8d82 5619 p->p_offset = off;
252b5132
RH
5620 else
5621 {
5622 file_ptr adjust;
5623
5624 adjust = off - (p->p_offset + p->p_filesz);
bf988460
AM
5625 if (!no_contents)
5626 p->p_filesz += adjust;
252b5132
RH
5627 p->p_memsz += adjust;
5628 }
5629 }
5630
1ea63fd2
AM
5631 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5632 maps. Set filepos for sections in PT_LOAD segments, and in
5633 core files, for sections in PT_NOTE segments.
5634 assign_file_positions_for_non_load_sections will set filepos
5635 for other sections and update p_filesz for other segments. */
252b5132
RH
5636 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5637 {
5638 asection *sec;
252b5132 5639 bfd_size_type align;
627b32bc 5640 Elf_Internal_Shdr *this_hdr;
252b5132
RH
5641
5642 sec = *secpp;
02bf8d82 5643 this_hdr = &elf_section_data (sec)->this_hdr;
3f570048 5644 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
252b5132 5645
88967714
AM
5646 if ((p->p_type == PT_LOAD
5647 || p->p_type == PT_TLS)
5648 && (this_hdr->sh_type != SHT_NOBITS
5649 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5650 && ((this_hdr->sh_flags & SHF_TLS) == 0
5651 || p->p_type == PT_TLS))))
252b5132 5652 {
b5599592
AM
5653 bfd_vma p_start = p->p_paddr;
5654 bfd_vma p_end = p_start + p->p_memsz;
5655 bfd_vma s_start = sec->lma;
5656 bfd_vma adjust = s_start - p_end;
252b5132 5657
a2d1e028
L
5658 if (adjust != 0
5659 && (s_start < p_end
5660 || p_end < p_start))
252b5132 5661 {
4eca0228 5662 _bfd_error_handler
695344c0 5663 /* xgettext:c-format */
2dcf00ce
AM
5664 (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64),
5665 abfd, sec, (uint64_t) s_start, (uint64_t) p_end);
88967714 5666 adjust = 0;
b5599592 5667 sec->lma = p_end;
1cfb7d1e 5668 }
3ac9b6c9 5669 p->p_memsz += adjust;
1cfb7d1e 5670
88967714
AM
5671 if (this_hdr->sh_type != SHT_NOBITS)
5672 {
32812159
AM
5673 if (p->p_filesz + adjust < p->p_memsz)
5674 {
5675 /* We have a PROGBITS section following NOBITS ones.
07d6d2b8 5676 Allocate file space for the NOBITS section(s) and
32812159
AM
5677 zero it. */
5678 adjust = p->p_memsz - p->p_filesz;
5679 if (!write_zeros (abfd, off, adjust))
5680 return FALSE;
5681 }
88967714
AM
5682 off += adjust;
5683 p->p_filesz += adjust;
252b5132 5684 }
252b5132
RH
5685 }
5686
5687 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5688 {
b301b248
AM
5689 /* The section at i == 0 is the one that actually contains
5690 everything. */
4a938328
MS
5691 if (i == 0)
5692 {
627b32bc 5693 this_hdr->sh_offset = sec->filepos = off;
6a3cd2b4
AM
5694 off += this_hdr->sh_size;
5695 p->p_filesz = this_hdr->sh_size;
b301b248
AM
5696 p->p_memsz = 0;
5697 p->p_align = 1;
252b5132 5698 }
4a938328 5699 else
252b5132 5700 {
b301b248 5701 /* The rest are fake sections that shouldn't be written. */
252b5132 5702 sec->filepos = 0;
eea6121a 5703 sec->size = 0;
b301b248
AM
5704 sec->flags = 0;
5705 continue;
252b5132 5706 }
252b5132
RH
5707 }
5708 else
5709 {
1e951488 5710 if (p->p_type == PT_LOAD)
b301b248 5711 {
1e951488
AM
5712 this_hdr->sh_offset = sec->filepos = off;
5713 if (this_hdr->sh_type != SHT_NOBITS)
5714 off += this_hdr->sh_size;
5715 }
5716 else if (this_hdr->sh_type == SHT_NOBITS
5717 && (this_hdr->sh_flags & SHF_TLS) != 0
5718 && this_hdr->sh_offset == 0)
5719 {
5720 /* This is a .tbss section that didn't get a PT_LOAD.
5721 (See _bfd_elf_map_sections_to_segments "Create a
5722 final PT_LOAD".) Set sh_offset to the value it
5723 would have if we had created a zero p_filesz and
5724 p_memsz PT_LOAD header for the section. This
5725 also makes the PT_TLS header have the same
5726 p_offset value. */
5727 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5728 off, align);
5729 this_hdr->sh_offset = sec->filepos = off + adjust;
b301b248 5730 }
252b5132 5731
02bf8d82 5732 if (this_hdr->sh_type != SHT_NOBITS)
b301b248 5733 {
6a3cd2b4 5734 p->p_filesz += this_hdr->sh_size;
02bf8d82
AM
5735 /* A load section without SHF_ALLOC is something like
5736 a note section in a PT_NOTE segment. These take
5737 file space but are not loaded into memory. */
5738 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6a3cd2b4 5739 p->p_memsz += this_hdr->sh_size;
b301b248 5740 }
6a3cd2b4 5741 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
13ae64f3 5742 {
6a3cd2b4
AM
5743 if (p->p_type == PT_TLS)
5744 p->p_memsz += this_hdr->sh_size;
5745
5746 /* .tbss is special. It doesn't contribute to p_memsz of
5747 normal segments. */
5748 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
5749 p->p_memsz += this_hdr->sh_size;
13ae64f3
JJ
5750 }
5751
b10a8ae0
L
5752 if (align > p->p_align
5753 && !m->p_align_valid
5754 && (p->p_type != PT_LOAD
5755 || (abfd->flags & D_PAGED) == 0))
252b5132
RH
5756 p->p_align = align;
5757 }
5758
bf988460 5759 if (!m->p_flags_valid)
252b5132
RH
5760 {
5761 p->p_flags |= PF_R;
02bf8d82 5762 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
252b5132 5763 p->p_flags |= PF_X;
02bf8d82 5764 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
252b5132
RH
5765 p->p_flags |= PF_W;
5766 }
5767 }
43a8475c 5768
bf988460 5769 off -= off_adjust;
0920dee7 5770
7c928300
AM
5771 /* Check that all sections are in a PT_LOAD segment.
5772 Don't check funky gdb generated core files. */
5773 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
9a83a553
AM
5774 {
5775 bfd_boolean check_vma = TRUE;
5776
5777 for (i = 1; i < m->count; i++)
5778 if (m->sections[i]->vma == m->sections[i - 1]->vma
5779 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
5780 ->this_hdr), p) != 0
5781 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
5782 ->this_hdr), p) != 0)
0920dee7 5783 {
9a83a553
AM
5784 /* Looks like we have overlays packed into the segment. */
5785 check_vma = FALSE;
5786 break;
0920dee7 5787 }
9a83a553
AM
5788
5789 for (i = 0; i < m->count; i++)
5790 {
5791 Elf_Internal_Shdr *this_hdr;
5792 asection *sec;
5793
5794 sec = m->sections[i];
5795 this_hdr = &(elf_section_data(sec)->this_hdr);
86b2281f
AM
5796 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
5797 && !ELF_TBSS_SPECIAL (this_hdr, p))
9a83a553 5798 {
4eca0228 5799 _bfd_error_handler
695344c0 5800 /* xgettext:c-format */
871b3ab2 5801 (_("%pB: section `%pA' can't be allocated in segment %d"),
9a83a553
AM
5802 abfd, sec, j);
5803 print_segment_map (m);
5804 }
5805 }
5806 }
252b5132
RH
5807 }
5808
12bd6957 5809 elf_next_file_pos (abfd) = off;
f3520d2f
AM
5810 return TRUE;
5811}
5812
5813/* Assign file positions for the other sections. */
5814
5815static bfd_boolean
5816assign_file_positions_for_non_load_sections (bfd *abfd,
5817 struct bfd_link_info *link_info)
5818{
5819 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5820 Elf_Internal_Shdr **i_shdrpp;
e06efbf1 5821 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
f3520d2f
AM
5822 Elf_Internal_Phdr *phdrs;
5823 Elf_Internal_Phdr *p;
5824 struct elf_segment_map *m;
62655c7b 5825 struct elf_segment_map *hdrs_segment;
f3520d2f
AM
5826 bfd_vma filehdr_vaddr, filehdr_paddr;
5827 bfd_vma phdrs_vaddr, phdrs_paddr;
5828 file_ptr off;
f3520d2f
AM
5829 unsigned int count;
5830
5c182d5f 5831 i_shdrpp = elf_elfsections (abfd);
e06efbf1 5832 end_hdrpp = i_shdrpp + elf_numsections (abfd);
12bd6957 5833 off = elf_next_file_pos (abfd);
e06efbf1 5834 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
5c182d5f 5835 {
5c182d5f
AM
5836 Elf_Internal_Shdr *hdr;
5837
5838 hdr = *hdrpp;
5839 if (hdr->bfd_section != NULL
252e386e
AM
5840 && (hdr->bfd_section->filepos != 0
5841 || (hdr->sh_type == SHT_NOBITS
5842 && hdr->contents == NULL)))
627b32bc 5843 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
5c182d5f
AM
5844 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
5845 {
e8d2ba53 5846 if (hdr->sh_size != 0)
4eca0228 5847 _bfd_error_handler
695344c0 5848 /* xgettext:c-format */
871b3ab2 5849 (_("%pB: warning: allocated section `%s' not in segment"),
e8d2ba53
AM
5850 abfd,
5851 (hdr->bfd_section == NULL
5852 ? "*unknown*"
5853 : hdr->bfd_section->name));
3ba71138
L
5854 /* We don't need to page align empty sections. */
5855 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
5c182d5f
AM
5856 off += vma_page_aligned_bias (hdr->sh_addr, off,
5857 bed->maxpagesize);
5858 else
5859 off += vma_page_aligned_bias (hdr->sh_addr, off,
5860 hdr->sh_addralign);
5861 off = _bfd_elf_assign_file_position_for_section (hdr, off,
5862 FALSE);
5863 }
5864 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
5865 && hdr->bfd_section == NULL)
0ce398f1
L
5866 || (hdr->bfd_section != NULL
5867 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
5868 /* Compress DWARF debug sections. */
12bd6957 5869 || hdr == i_shdrpp[elf_onesymtab (abfd)]
6a40cf0c
NC
5870 || (elf_symtab_shndx_list (abfd) != NULL
5871 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
3e19fb8f
L
5872 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
5873 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
5c182d5f
AM
5874 hdr->sh_offset = -1;
5875 else
5876 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5c182d5f
AM
5877 }
5878
252b5132
RH
5879 /* Now that we have set the section file positions, we can set up
5880 the file positions for the non PT_LOAD segments. */
f3520d2f
AM
5881 count = 0;
5882 filehdr_vaddr = 0;
5883 filehdr_paddr = 0;
5884 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
5885 phdrs_paddr = 0;
62655c7b 5886 hdrs_segment = NULL;
f3520d2f 5887 phdrs = elf_tdata (abfd)->phdr;
12bd6957 5888 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
f3520d2f
AM
5889 {
5890 ++count;
5891 if (p->p_type != PT_LOAD)
5892 continue;
5893
5894 if (m->includes_filehdr)
5895 {
5896 filehdr_vaddr = p->p_vaddr;
5897 filehdr_paddr = p->p_paddr;
5898 }
5899 if (m->includes_phdrs)
5900 {
5901 phdrs_vaddr = p->p_vaddr;
5902 phdrs_paddr = p->p_paddr;
5903 if (m->includes_filehdr)
5904 {
62655c7b 5905 hdrs_segment = m;
f3520d2f
AM
5906 phdrs_vaddr += bed->s->sizeof_ehdr;
5907 phdrs_paddr += bed->s->sizeof_ehdr;
5908 }
5909 }
5910 }
5911
62655c7b
RM
5912 if (hdrs_segment != NULL && link_info != NULL)
5913 {
5914 /* There is a segment that contains both the file headers and the
5915 program headers, so provide a symbol __ehdr_start pointing there.
5916 A program can use this to examine itself robustly. */
5917
5918 struct elf_link_hash_entry *hash
5919 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
5920 FALSE, FALSE, TRUE);
5921 /* If the symbol was referenced and not defined, define it. */
5922 if (hash != NULL
5923 && (hash->root.type == bfd_link_hash_new
5924 || hash->root.type == bfd_link_hash_undefined
5925 || hash->root.type == bfd_link_hash_undefweak
5926 || hash->root.type == bfd_link_hash_common))
5927 {
5928 asection *s = NULL;
5929 if (hdrs_segment->count != 0)
5930 /* The segment contains sections, so use the first one. */
5931 s = hdrs_segment->sections[0];
5932 else
5933 /* Use the first (i.e. lowest-addressed) section in any segment. */
12bd6957 5934 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
62655c7b
RM
5935 if (m->count != 0)
5936 {
5937 s = m->sections[0];
5938 break;
5939 }
5940
5941 if (s != NULL)
5942 {
5943 hash->root.u.def.value = filehdr_vaddr - s->vma;
5944 hash->root.u.def.section = s;
5945 }
5946 else
5947 {
5948 hash->root.u.def.value = filehdr_vaddr;
5949 hash->root.u.def.section = bfd_abs_section_ptr;
5950 }
5951
5952 hash->root.type = bfd_link_hash_defined;
5953 hash->def_regular = 1;
5954 hash->non_elf = 0;
5955 }
5956 }
5957
12bd6957 5958 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
252b5132 5959 {
129af99f 5960 if (p->p_type == PT_GNU_RELRO)
252b5132 5961 {
f2731e0c 5962 bfd_vma start, end;
01f7e10c 5963 bfd_boolean ok;
1ea63fd2 5964
129af99f 5965 if (link_info != NULL)
8c37241b 5966 {
129af99f 5967 /* During linking the range of the RELRO segment is passed
f2731e0c
AM
5968 in link_info. Note that there may be padding between
5969 relro_start and the first RELRO section. */
5970 start = link_info->relro_start;
5971 end = link_info->relro_end;
5972 }
5973 else if (m->count != 0)
5974 {
5975 if (!m->p_size_valid)
5976 abort ();
5977 start = m->sections[0]->vma;
5978 end = start + m->p_size;
5979 }
5980 else
5981 {
5982 start = 0;
5983 end = 0;
5984 }
5985
01f7e10c 5986 ok = FALSE;
f2731e0c
AM
5987 if (start < end)
5988 {
5989 struct elf_segment_map *lm;
5990 const Elf_Internal_Phdr *lp;
5991 unsigned int i;
5992
5993 /* Find a LOAD segment containing a section in the RELRO
5994 segment. */
12bd6957 5995 for (lm = elf_seg_map (abfd), lp = phdrs;
3146fac4
AM
5996 lm != NULL;
5997 lm = lm->next, lp++)
8c37241b
JJ
5998 {
5999 if (lp->p_type == PT_LOAD
3146fac4 6000 && lm->count != 0
dbc88fc1
AM
6001 && (lm->sections[lm->count - 1]->vma
6002 + (!IS_TBSS (lm->sections[lm->count - 1])
6003 ? lm->sections[lm->count - 1]->size
6004 : 0)) > start
f2731e0c 6005 && lm->sections[0]->vma < end)
8c37241b
JJ
6006 break;
6007 }
f2731e0c 6008
01f7e10c 6009 if (lm != NULL)
129af99f 6010 {
01f7e10c
AM
6011 /* Find the section starting the RELRO segment. */
6012 for (i = 0; i < lm->count; i++)
6013 {
6014 asection *s = lm->sections[i];
6015 if (s->vma >= start
6016 && s->vma < end
6017 && s->size != 0)
6018 break;
6019 }
6020
6021 if (i < lm->count)
6022 {
6023 p->p_vaddr = lm->sections[i]->vma;
6024 p->p_paddr = lm->sections[i]->lma;
6025 p->p_offset = lm->sections[i]->filepos;
6026 p->p_memsz = end - p->p_vaddr;
6027 p->p_filesz = p->p_memsz;
6028
6029 /* The RELRO segment typically ends a few bytes
6030 into .got.plt but other layouts are possible.
6031 In cases where the end does not match any
6032 loaded section (for instance is in file
6033 padding), trim p_filesz back to correspond to
6034 the end of loaded section contents. */
6035 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6036 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6037
6038 /* Preserve the alignment and flags if they are
6039 valid. The gold linker generates RW/4 for
6040 the PT_GNU_RELRO section. It is better for
6041 objcopy/strip to honor these attributes
6042 otherwise gdb will choke when using separate
6043 debug files. */
6044 if (!m->p_align_valid)
6045 p->p_align = 1;
6046 if (!m->p_flags_valid)
6047 p->p_flags = PF_R;
6048 ok = TRUE;
6049 }
129af99f 6050 }
b84a33b5 6051 }
01f7e10c
AM
6052 if (link_info != NULL)
6053 BFD_ASSERT (ok);
6054 if (!ok)
6055 memset (p, 0, sizeof *p);
129af99f 6056 }
04c3a755
NS
6057 else if (p->p_type == PT_GNU_STACK)
6058 {
6059 if (m->p_size_valid)
6060 p->p_memsz = m->p_size;
6061 }
129af99f
AS
6062 else if (m->count != 0)
6063 {
e06efbf1 6064 unsigned int i;
1a9ccd70 6065
129af99f
AS
6066 if (p->p_type != PT_LOAD
6067 && (p->p_type != PT_NOTE
6068 || bfd_get_format (abfd) != bfd_core))
6069 {
1a9ccd70
NC
6070 /* A user specified segment layout may include a PHDR
6071 segment that overlaps with a LOAD segment... */
6072 if (p->p_type == PT_PHDR)
6073 {
6074 m->count = 0;
6075 continue;
6076 }
6077
c86934ce
NC
6078 if (m->includes_filehdr || m->includes_phdrs)
6079 {
b1fa9dd6 6080 /* PR 17512: file: 2195325e. */
4eca0228 6081 _bfd_error_handler
871b3ab2 6082 (_("%pB: error: non-load segment %d includes file header "
76cfced5
AM
6083 "and/or program header"),
6084 abfd, (int) (p - phdrs));
c86934ce
NC
6085 return FALSE;
6086 }
129af99f 6087
86b2281f 6088 p->p_filesz = 0;
129af99f 6089 p->p_offset = m->sections[0]->filepos;
86b2281f
AM
6090 for (i = m->count; i-- != 0;)
6091 {
6092 asection *sect = m->sections[i];
6093 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6094 if (hdr->sh_type != SHT_NOBITS)
6095 {
6096 p->p_filesz = (sect->filepos - m->sections[0]->filepos
6097 + hdr->sh_size);
6098 break;
6099 }
6100 }
129af99f
AS
6101 }
6102 }
6103 else if (m->includes_filehdr)
6104 {
6105 p->p_vaddr = filehdr_vaddr;
6106 if (! m->p_paddr_valid)
6107 p->p_paddr = filehdr_paddr;
6108 }
6109 else if (m->includes_phdrs)
6110 {
6111 p->p_vaddr = phdrs_vaddr;
6112 if (! m->p_paddr_valid)
6113 p->p_paddr = phdrs_paddr;
252b5132
RH
6114 }
6115 }
6116
12bd6957 6117 elf_next_file_pos (abfd) = off;
252b5132 6118
b34976b6 6119 return TRUE;
252b5132
RH
6120}
6121
6a40cf0c
NC
6122static elf_section_list *
6123find_section_in_list (unsigned int i, elf_section_list * list)
6124{
6125 for (;list != NULL; list = list->next)
6126 if (list->ndx == i)
6127 break;
6128 return list;
6129}
6130
252b5132
RH
6131/* Work out the file positions of all the sections. This is called by
6132 _bfd_elf_compute_section_file_positions. All the section sizes and
6133 VMAs must be known before this is called.
6134
e0638f70
AM
6135 Reloc sections come in two flavours: Those processed specially as
6136 "side-channel" data attached to a section to which they apply, and
6137 those that bfd doesn't process as relocations. The latter sort are
6138 stored in a normal bfd section by bfd_section_from_shdr. We don't
6139 consider the former sort here, unless they form part of the loadable
6140 image. Reloc sections not assigned here will be handled later by
6141 assign_file_positions_for_relocs.
252b5132
RH
6142
6143 We also don't set the positions of the .symtab and .strtab here. */
6144
b34976b6 6145static bfd_boolean
c84fca4d
AO
6146assign_file_positions_except_relocs (bfd *abfd,
6147 struct bfd_link_info *link_info)
252b5132 6148{
5c182d5f
AM
6149 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6150 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
9c5bfbb7 6151 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
6152
6153 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6154 && bfd_get_format (abfd) != bfd_core)
6155 {
5c182d5f
AM
6156 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6157 unsigned int num_sec = elf_numsections (abfd);
252b5132
RH
6158 Elf_Internal_Shdr **hdrpp;
6159 unsigned int i;
a485e98e 6160 file_ptr off;
252b5132
RH
6161
6162 /* Start after the ELF header. */
6163 off = i_ehdrp->e_ehsize;
6164
6165 /* We are not creating an executable, which means that we are
6166 not creating a program header, and that the actual order of
6167 the sections in the file is unimportant. */
9ad5cbcf 6168 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
252b5132
RH
6169 {
6170 Elf_Internal_Shdr *hdr;
6171
6172 hdr = *hdrpp;
e0638f70
AM
6173 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6174 && hdr->bfd_section == NULL)
0ce398f1
L
6175 || (hdr->bfd_section != NULL
6176 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
6177 /* Compress DWARF debug sections. */
12bd6957 6178 || i == elf_onesymtab (abfd)
6a40cf0c
NC
6179 || (elf_symtab_shndx_list (abfd) != NULL
6180 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
3e19fb8f
L
6181 || i == elf_strtab_sec (abfd)
6182 || i == elf_shstrtab_sec (abfd))
252b5132
RH
6183 {
6184 hdr->sh_offset = -1;
252b5132 6185 }
9ad5cbcf 6186 else
b34976b6 6187 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
252b5132 6188 }
a485e98e
AM
6189
6190 elf_next_file_pos (abfd) = off;
252b5132
RH
6191 }
6192 else
6193 {
f3520d2f
AM
6194 unsigned int alloc;
6195
252b5132 6196 /* Assign file positions for the loaded sections based on the
08a40648 6197 assignment of sections to segments. */
f3520d2f
AM
6198 if (!assign_file_positions_for_load_sections (abfd, link_info))
6199 return FALSE;
6200
6201 /* And for non-load sections. */
6202 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6203 return FALSE;
6204
e36284ab
AM
6205 if (bed->elf_backend_modify_program_headers != NULL)
6206 {
6207 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
6208 return FALSE;
6209 }
6210
58e7ebac 6211 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
0e1862bb 6212 if (link_info != NULL && bfd_link_pie (link_info))
58e7ebac
L
6213 {
6214 unsigned int num_segments = elf_elfheader (abfd)->e_phnum;
6215 Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr;
6216 Elf_Internal_Phdr *end_segment = &segment[num_segments];
6217
6218 /* Find the lowest p_vaddr in PT_LOAD segments. */
6219 bfd_vma p_vaddr = (bfd_vma) -1;
6220 for (; segment < end_segment; segment++)
6221 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
6222 p_vaddr = segment->p_vaddr;
6223
6224 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6225 segments is non-zero. */
6226 if (p_vaddr)
6227 i_ehdrp->e_type = ET_EXEC;
6228 }
6229
f3520d2f 6230 /* Write out the program headers. */
6838f2be 6231 alloc = elf_elfheader (abfd)->e_phnum;
1a9ccd70
NC
6232 if (alloc == 0)
6233 return TRUE;
6234
1a9ccd70
NC
6235 /* PR ld/20815 - Check that the program header segment, if present, will
6236 be loaded into memory. FIXME: The check below is not sufficient as
6237 really all PT_LOAD segments should be checked before issuing an error
6238 message. Plus the PHDR segment does not have to be the first segment
6239 in the program header table. But this version of the check should
cd584857
NC
6240 catch all real world use cases.
6241
6242 FIXME: We used to have code here to sort the PT_LOAD segments into
6243 ascending order, as per the ELF spec. But this breaks some programs,
6244 including the Linux kernel. But really either the spec should be
07d6d2b8 6245 changed or the programs updated. */
1a9ccd70 6246 if (alloc > 1
cd584857 6247 && tdata->phdr[0].p_type == PT_PHDR
d00dd7dc
AM
6248 && (bed->elf_backend_allow_non_load_phdr == NULL
6249 || !bed->elf_backend_allow_non_load_phdr (abfd, tdata->phdr,
6250 alloc))
cd584857
NC
6251 && tdata->phdr[1].p_type == PT_LOAD
6252 && (tdata->phdr[1].p_vaddr > tdata->phdr[0].p_vaddr
6838f2be
AM
6253 || (tdata->phdr[1].p_vaddr + tdata->phdr[1].p_memsz
6254 < tdata->phdr[0].p_vaddr + tdata->phdr[0].p_memsz)))
1a9ccd70
NC
6255 {
6256 /* The fix for this error is usually to edit the linker script being
6257 used and set up the program headers manually. Either that or
6258 leave room for the headers at the start of the SECTIONS. */
6838f2be
AM
6259 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6260 " by LOAD segment"),
1a9ccd70 6261 abfd);
1a9ccd70
NC
6262 return FALSE;
6263 }
6264
f3520d2f 6265 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
cd584857
NC
6266 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6267 return FALSE;
252b5132
RH
6268 }
6269
b34976b6 6270 return TRUE;
252b5132
RH
6271}
6272
b34976b6 6273static bfd_boolean
217aa764 6274prep_headers (bfd *abfd)
252b5132 6275{
3d540e93 6276 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
2b0f7ef9 6277 struct elf_strtab_hash *shstrtab;
9c5bfbb7 6278 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
6279
6280 i_ehdrp = elf_elfheader (abfd);
252b5132 6281
2b0f7ef9 6282 shstrtab = _bfd_elf_strtab_init ();
252b5132 6283 if (shstrtab == NULL)
b34976b6 6284 return FALSE;
252b5132
RH
6285
6286 elf_shstrtab (abfd) = shstrtab;
6287
6288 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6289 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6290 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6291 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6292
6293 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6294 i_ehdrp->e_ident[EI_DATA] =
6295 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6296 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6297
252b5132
RH
6298 if ((abfd->flags & DYNAMIC) != 0)
6299 i_ehdrp->e_type = ET_DYN;
6300 else if ((abfd->flags & EXEC_P) != 0)
6301 i_ehdrp->e_type = ET_EXEC;
6302 else if (bfd_get_format (abfd) == bfd_core)
6303 i_ehdrp->e_type = ET_CORE;
6304 else
6305 i_ehdrp->e_type = ET_REL;
6306
6307 switch (bfd_get_arch (abfd))
6308 {
6309 case bfd_arch_unknown:
6310 i_ehdrp->e_machine = EM_NONE;
6311 break;
aa4f99bb
AO
6312
6313 /* There used to be a long list of cases here, each one setting
6314 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6315 in the corresponding bfd definition. To avoid duplication,
6316 the switch was removed. Machines that need special handling
6317 can generally do it in elf_backend_final_write_processing(),
6318 unless they need the information earlier than the final write.
6319 Such need can generally be supplied by replacing the tests for
6320 e_machine with the conditions used to determine it. */
252b5132 6321 default:
9c5bfbb7
AM
6322 i_ehdrp->e_machine = bed->elf_machine_code;
6323 }
aa4f99bb 6324
252b5132
RH
6325 i_ehdrp->e_version = bed->s->ev_current;
6326 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6327
c044fabd 6328 /* No program header, for now. */
252b5132
RH
6329 i_ehdrp->e_phoff = 0;
6330 i_ehdrp->e_phentsize = 0;
6331 i_ehdrp->e_phnum = 0;
6332
c044fabd 6333 /* Each bfd section is section header entry. */
252b5132
RH
6334 i_ehdrp->e_entry = bfd_get_start_address (abfd);
6335 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6336
c044fabd 6337 /* If we're building an executable, we'll need a program header table. */
252b5132 6338 if (abfd->flags & EXEC_P)
0e71e495
BE
6339 /* It all happens later. */
6340 ;
252b5132
RH
6341 else
6342 {
6343 i_ehdrp->e_phentsize = 0;
252b5132
RH
6344 i_ehdrp->e_phoff = 0;
6345 }
6346
6347 elf_tdata (abfd)->symtab_hdr.sh_name =
b34976b6 6348 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
252b5132 6349 elf_tdata (abfd)->strtab_hdr.sh_name =
b34976b6 6350 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
252b5132 6351 elf_tdata (abfd)->shstrtab_hdr.sh_name =
b34976b6 6352 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
252b5132 6353 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
17ca87fc 6354 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
252b5132 6355 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
b34976b6 6356 return FALSE;
252b5132 6357
b34976b6 6358 return TRUE;
252b5132
RH
6359}
6360
6361/* Assign file positions for all the reloc sections which are not part
a485e98e 6362 of the loadable file image, and the file position of section headers. */
252b5132 6363
0ce398f1
L
6364static bfd_boolean
6365_bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
252b5132
RH
6366{
6367 file_ptr off;
e06efbf1 6368 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
3e19fb8f 6369 Elf_Internal_Shdr *shdrp;
a485e98e
AM
6370 Elf_Internal_Ehdr *i_ehdrp;
6371 const struct elf_backend_data *bed;
252b5132 6372
12bd6957 6373 off = elf_next_file_pos (abfd);
252b5132 6374
e06efbf1
L
6375 shdrpp = elf_elfsections (abfd);
6376 end_shdrpp = shdrpp + elf_numsections (abfd);
6377 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
252b5132 6378 {
252b5132 6379 shdrp = *shdrpp;
0ce398f1
L
6380 if (shdrp->sh_offset == -1)
6381 {
3e19fb8f 6382 asection *sec = shdrp->bfd_section;
0ce398f1
L
6383 bfd_boolean is_rel = (shdrp->sh_type == SHT_REL
6384 || shdrp->sh_type == SHT_RELA);
6385 if (is_rel
3e19fb8f 6386 || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
0ce398f1
L
6387 {
6388 if (!is_rel)
6389 {
3e19fb8f
L
6390 const char *name = sec->name;
6391 struct bfd_elf_section_data *d;
6392
0ce398f1 6393 /* Compress DWARF debug sections. */
3e19fb8f 6394 if (!bfd_compress_section (abfd, sec,
0ce398f1
L
6395 shdrp->contents))
6396 return FALSE;
3e19fb8f
L
6397
6398 if (sec->compress_status == COMPRESS_SECTION_DONE
6399 && (abfd->flags & BFD_COMPRESS_GABI) == 0)
6400 {
6401 /* If section is compressed with zlib-gnu, convert
6402 section name from .debug_* to .zdebug_*. */
6403 char *new_name
6404 = convert_debug_to_zdebug (abfd, name);
6405 if (new_name == NULL)
6406 return FALSE;
6407 name = new_name;
6408 }
dd905818 6409 /* Add section name to section name section. */
3e19fb8f
L
6410 if (shdrp->sh_name != (unsigned int) -1)
6411 abort ();
6412 shdrp->sh_name
6413 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
6414 name, FALSE);
6415 d = elf_section_data (sec);
6416
dd905818 6417 /* Add reloc section name to section name section. */
3e19fb8f
L
6418 if (d->rel.hdr
6419 && !_bfd_elf_set_reloc_sh_name (abfd,
6420 d->rel.hdr,
6421 name, FALSE))
6422 return FALSE;
6423 if (d->rela.hdr
6424 && !_bfd_elf_set_reloc_sh_name (abfd,
6425 d->rela.hdr,
91cb26da 6426 name, TRUE))
3e19fb8f
L
6427 return FALSE;
6428
0ce398f1 6429 /* Update section size and contents. */
3e19fb8f
L
6430 shdrp->sh_size = sec->size;
6431 shdrp->contents = sec->contents;
0ce398f1
L
6432 shdrp->bfd_section->contents = NULL;
6433 }
6434 off = _bfd_elf_assign_file_position_for_section (shdrp,
6435 off,
6436 TRUE);
6437 }
6438 }
252b5132
RH
6439 }
6440
3e19fb8f
L
6441 /* Place section name section after DWARF debug sections have been
6442 compressed. */
6443 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
6444 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
6445 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
6446 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
6447
6448 /* Place the section headers. */
a485e98e
AM
6449 i_ehdrp = elf_elfheader (abfd);
6450 bed = get_elf_backend_data (abfd);
6451 off = align_file_position (off, 1 << bed->s->log_file_align);
6452 i_ehdrp->e_shoff = off;
6453 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
12bd6957 6454 elf_next_file_pos (abfd) = off;
0ce398f1
L
6455
6456 return TRUE;
252b5132
RH
6457}
6458
b34976b6 6459bfd_boolean
217aa764 6460_bfd_elf_write_object_contents (bfd *abfd)
252b5132 6461{
9c5bfbb7 6462 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 6463 Elf_Internal_Shdr **i_shdrp;
b34976b6 6464 bfd_boolean failed;
9ad5cbcf 6465 unsigned int count, num_sec;
30e8ee25 6466 struct elf_obj_tdata *t;
252b5132
RH
6467
6468 if (! abfd->output_has_begun
217aa764 6469 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
b34976b6 6470 return FALSE;
db727370
JL
6471 /* Do not rewrite ELF data when the BFD has been opened for update.
6472 abfd->output_has_begun was set to TRUE on opening, so creation of new
6473 sections, and modification of existing section sizes was restricted.
6474 This means the ELF header, program headers and section headers can't have
6475 changed.
6476 If the contents of any sections has been modified, then those changes have
6477 already been written to the BFD. */
6478 else if (abfd->direction == both_direction)
6479 {
6480 BFD_ASSERT (abfd->output_has_begun);
6481 return TRUE;
6482 }
252b5132
RH
6483
6484 i_shdrp = elf_elfsections (abfd);
252b5132 6485
b34976b6 6486 failed = FALSE;
252b5132
RH
6487 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
6488 if (failed)
b34976b6 6489 return FALSE;
252b5132 6490
0ce398f1
L
6491 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
6492 return FALSE;
252b5132 6493
c044fabd 6494 /* After writing the headers, we need to write the sections too... */
9ad5cbcf
AM
6495 num_sec = elf_numsections (abfd);
6496 for (count = 1; count < num_sec; count++)
252b5132 6497 {
3e19fb8f
L
6498 i_shdrp[count]->sh_name
6499 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
6500 i_shdrp[count]->sh_name);
252b5132 6501 if (bed->elf_backend_section_processing)
75506100
MR
6502 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
6503 return FALSE;
252b5132
RH
6504 if (i_shdrp[count]->contents)
6505 {
dc810e39
AM
6506 bfd_size_type amt = i_shdrp[count]->sh_size;
6507
252b5132 6508 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
dc810e39 6509 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
b34976b6 6510 return FALSE;
252b5132
RH
6511 }
6512 }
6513
6514 /* Write out the section header names. */
30e8ee25 6515 t = elf_tdata (abfd);
26ae6d5e 6516 if (elf_shstrtab (abfd) != NULL
30e8ee25 6517 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
08a40648 6518 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
b34976b6 6519 return FALSE;
252b5132
RH
6520
6521 if (bed->elf_backend_final_write_processing)
12bd6957 6522 (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd));
252b5132 6523
ff59fc36
RM
6524 if (!bed->s->write_shdrs_and_ehdr (abfd))
6525 return FALSE;
6526
6527 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
c0355132
AM
6528 if (t->o->build_id.after_write_object_contents != NULL)
6529 return (*t->o->build_id.after_write_object_contents) (abfd);
ff59fc36
RM
6530
6531 return TRUE;
252b5132
RH
6532}
6533
b34976b6 6534bfd_boolean
217aa764 6535_bfd_elf_write_corefile_contents (bfd *abfd)
252b5132 6536{
c044fabd 6537 /* Hopefully this can be done just like an object file. */
252b5132
RH
6538 return _bfd_elf_write_object_contents (abfd);
6539}
c044fabd
KH
6540
6541/* Given a section, search the header to find them. */
6542
cb33740c 6543unsigned int
198beae2 6544_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
252b5132 6545{
9c5bfbb7 6546 const struct elf_backend_data *bed;
91d6fa6a 6547 unsigned int sec_index;
252b5132 6548
9ad5cbcf
AM
6549 if (elf_section_data (asect) != NULL
6550 && elf_section_data (asect)->this_idx != 0)
6551 return elf_section_data (asect)->this_idx;
6552
6553 if (bfd_is_abs_section (asect))
91d6fa6a 6554 sec_index = SHN_ABS;
af746e92 6555 else if (bfd_is_com_section (asect))
91d6fa6a 6556 sec_index = SHN_COMMON;
af746e92 6557 else if (bfd_is_und_section (asect))
91d6fa6a 6558 sec_index = SHN_UNDEF;
af746e92 6559 else
91d6fa6a 6560 sec_index = SHN_BAD;
252b5132 6561
af746e92 6562 bed = get_elf_backend_data (abfd);
252b5132
RH
6563 if (bed->elf_backend_section_from_bfd_section)
6564 {
91d6fa6a 6565 int retval = sec_index;
9ad5cbcf 6566
af746e92
AM
6567 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6568 return retval;
252b5132
RH
6569 }
6570
91d6fa6a 6571 if (sec_index == SHN_BAD)
af746e92 6572 bfd_set_error (bfd_error_nonrepresentable_section);
252b5132 6573
91d6fa6a 6574 return sec_index;
252b5132
RH
6575}
6576
6577/* Given a BFD symbol, return the index in the ELF symbol table, or -1
6578 on error. */
6579
6580int
217aa764 6581_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
252b5132
RH
6582{
6583 asymbol *asym_ptr = *asym_ptr_ptr;
6584 int idx;
6585 flagword flags = asym_ptr->flags;
6586
6587 /* When gas creates relocations against local labels, it creates its
6588 own symbol for the section, but does put the symbol into the
6589 symbol chain, so udata is 0. When the linker is generating
6590 relocatable output, this section symbol may be for one of the
6591 input sections rather than the output section. */
6592 if (asym_ptr->udata.i == 0
6593 && (flags & BSF_SECTION_SYM)
6594 && asym_ptr->section)
6595 {
5372391b 6596 asection *sec;
252b5132
RH
6597 int indx;
6598
5372391b
AM
6599 sec = asym_ptr->section;
6600 if (sec->owner != abfd && sec->output_section != NULL)
6601 sec = sec->output_section;
6602 if (sec->owner == abfd
6603 && (indx = sec->index) < elf_num_section_syms (abfd)
4e89ac30 6604 && elf_section_syms (abfd)[indx] != NULL)
252b5132
RH
6605 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
6606 }
6607
6608 idx = asym_ptr->udata.i;
6609
6610 if (idx == 0)
6611 {
6612 /* This case can occur when using --strip-symbol on a symbol
08a40648 6613 which is used in a relocation entry. */
4eca0228 6614 _bfd_error_handler
695344c0 6615 /* xgettext:c-format */
871b3ab2 6616 (_("%pB: symbol `%s' required but not present"),
d003868e 6617 abfd, bfd_asymbol_name (asym_ptr));
252b5132
RH
6618 bfd_set_error (bfd_error_no_symbols);
6619 return -1;
6620 }
6621
6622#if DEBUG & 4
6623 {
6624 fprintf (stderr,
cd9af601
AM
6625 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6626 (long) asym_ptr, asym_ptr->name, idx, flags);
252b5132
RH
6627 fflush (stderr);
6628 }
6629#endif
6630
6631 return idx;
6632}
6633
84d1d650 6634/* Rewrite program header information. */
252b5132 6635
b34976b6 6636static bfd_boolean
84d1d650 6637rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
252b5132 6638{
b34976b6
AM
6639 Elf_Internal_Ehdr *iehdr;
6640 struct elf_segment_map *map;
6641 struct elf_segment_map *map_first;
6642 struct elf_segment_map **pointer_to_map;
6643 Elf_Internal_Phdr *segment;
6644 asection *section;
6645 unsigned int i;
6646 unsigned int num_segments;
6647 bfd_boolean phdr_included = FALSE;
5c44b38e 6648 bfd_boolean p_paddr_valid;
b34976b6
AM
6649 bfd_vma maxpagesize;
6650 struct elf_segment_map *phdr_adjust_seg = NULL;
6651 unsigned int phdr_adjust_num = 0;
9c5bfbb7 6652 const struct elf_backend_data *bed;
bc67d8a6 6653
caf47ea6 6654 bed = get_elf_backend_data (ibfd);
252b5132
RH
6655 iehdr = elf_elfheader (ibfd);
6656
bc67d8a6 6657 map_first = NULL;
c044fabd 6658 pointer_to_map = &map_first;
252b5132
RH
6659
6660 num_segments = elf_elfheader (ibfd)->e_phnum;
bc67d8a6
NC
6661 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
6662
6663 /* Returns the end address of the segment + 1. */
aecc8f8a
AM
6664#define SEGMENT_END(segment, start) \
6665 (start + (segment->p_memsz > segment->p_filesz \
6666 ? segment->p_memsz : segment->p_filesz))
bc67d8a6 6667
eecdbe52
JJ
6668#define SECTION_SIZE(section, segment) \
6669 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6670 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
eea6121a 6671 ? section->size : 0)
eecdbe52 6672
b34976b6 6673 /* Returns TRUE if the given section is contained within
bc67d8a6 6674 the given segment. VMA addresses are compared. */
aecc8f8a
AM
6675#define IS_CONTAINED_BY_VMA(section, segment) \
6676 (section->vma >= segment->p_vaddr \
eecdbe52 6677 && (section->vma + SECTION_SIZE (section, segment) \
aecc8f8a 6678 <= (SEGMENT_END (segment, segment->p_vaddr))))
c044fabd 6679
b34976b6 6680 /* Returns TRUE if the given section is contained within
bc67d8a6 6681 the given segment. LMA addresses are compared. */
aecc8f8a
AM
6682#define IS_CONTAINED_BY_LMA(section, segment, base) \
6683 (section->lma >= base \
beab4532 6684 && (section->lma + SECTION_SIZE (section, segment) >= section->lma) \
eecdbe52 6685 && (section->lma + SECTION_SIZE (section, segment) \
aecc8f8a 6686 <= SEGMENT_END (segment, base)))
252b5132 6687
0efc80c8
L
6688 /* Handle PT_NOTE segment. */
6689#define IS_NOTE(p, s) \
aecc8f8a 6690 (p->p_type == PT_NOTE \
0efc80c8 6691 && elf_section_type (s) == SHT_NOTE \
aecc8f8a 6692 && (bfd_vma) s->filepos >= p->p_offset \
cb3ff1e5 6693 && ((bfd_vma) s->filepos + s->size \
aecc8f8a 6694 <= p->p_offset + p->p_filesz))
252b5132 6695
0efc80c8
L
6696 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6697 etc. */
6698#define IS_COREFILE_NOTE(p, s) \
6699 (IS_NOTE (p, s) \
6700 && bfd_get_format (ibfd) == bfd_core \
6701 && s->vma == 0 \
6702 && s->lma == 0)
6703
252b5132
RH
6704 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6705 linker, which generates a PT_INTERP section with p_vaddr and
6706 p_memsz set to 0. */
aecc8f8a
AM
6707#define IS_SOLARIS_PT_INTERP(p, s) \
6708 (p->p_vaddr == 0 \
6709 && p->p_paddr == 0 \
6710 && p->p_memsz == 0 \
6711 && p->p_filesz > 0 \
6712 && (s->flags & SEC_HAS_CONTENTS) != 0 \
eea6121a 6713 && s->size > 0 \
aecc8f8a 6714 && (bfd_vma) s->filepos >= p->p_offset \
cb3ff1e5 6715 && ((bfd_vma) s->filepos + s->size \
aecc8f8a 6716 <= p->p_offset + p->p_filesz))
5c440b1e 6717
bc67d8a6
NC
6718 /* Decide if the given section should be included in the given segment.
6719 A section will be included if:
f5ffc919 6720 1. It is within the address space of the segment -- we use the LMA
08a40648 6721 if that is set for the segment and the VMA otherwise,
0efc80c8 6722 2. It is an allocated section or a NOTE section in a PT_NOTE
d324f6d6 6723 segment.
bc67d8a6 6724 3. There is an output section associated with it,
eecdbe52 6725 4. The section has not already been allocated to a previous segment.
2b05f1b7 6726 5. PT_GNU_STACK segments do not include any sections.
03394ac9 6727 6. PT_TLS segment includes only SHF_TLS sections.
6f79b219
JJ
6728 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6729 8. PT_DYNAMIC should not contain empty sections at the beginning
08a40648 6730 (with the possible exception of .dynamic). */
9f17e2a6 6731#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
2b05f1b7
L
6732 ((((segment->p_paddr \
6733 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
6734 : IS_CONTAINED_BY_VMA (section, segment)) \
6735 && (section->flags & SEC_ALLOC) != 0) \
0efc80c8 6736 || IS_NOTE (segment, section)) \
2b05f1b7
L
6737 && segment->p_type != PT_GNU_STACK \
6738 && (segment->p_type != PT_TLS \
6739 || (section->flags & SEC_THREAD_LOCAL)) \
6740 && (segment->p_type == PT_LOAD \
6741 || segment->p_type == PT_TLS \
6742 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6743 && (segment->p_type != PT_DYNAMIC \
6744 || SECTION_SIZE (section, segment) > 0 \
6745 || (segment->p_paddr \
6746 ? segment->p_paddr != section->lma \
6747 : segment->p_vaddr != section->vma) \
6748 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
6749 == 0)) \
9933dc52 6750 && (segment->p_type != PT_LOAD || !section->segment_mark))
bc67d8a6 6751
9f17e2a6
L
6752/* If the output section of a section in the input segment is NULL,
6753 it is removed from the corresponding output segment. */
6754#define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
6755 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
6756 && section->output_section != NULL)
6757
b34976b6 6758 /* Returns TRUE iff seg1 starts after the end of seg2. */
b5f852ea
NC
6759#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6760 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6761
6762 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6763 their VMA address ranges and their LMA address ranges overlap.
6764 It is possible to have overlapping VMA ranges without overlapping LMA
6765 ranges. RedBoot images for example can have both .data and .bss mapped
6766 to the same VMA range, but with the .data section mapped to a different
6767 LMA. */
aecc8f8a 6768#define SEGMENT_OVERLAPS(seg1, seg2) \
b5f852ea 6769 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
08a40648 6770 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
b5f852ea 6771 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
08a40648 6772 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
bc67d8a6
NC
6773
6774 /* Initialise the segment mark field. */
6775 for (section = ibfd->sections; section != NULL; section = section->next)
b34976b6 6776 section->segment_mark = FALSE;
bc67d8a6 6777
5c44b38e
AM
6778 /* The Solaris linker creates program headers in which all the
6779 p_paddr fields are zero. When we try to objcopy or strip such a
6780 file, we get confused. Check for this case, and if we find it
6781 don't set the p_paddr_valid fields. */
6782 p_paddr_valid = FALSE;
6783 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6784 i < num_segments;
6785 i++, segment++)
6786 if (segment->p_paddr != 0)
6787 {
6788 p_paddr_valid = TRUE;
6789 break;
6790 }
6791
252b5132 6792 /* Scan through the segments specified in the program header
bc67d8a6 6793 of the input BFD. For this first scan we look for overlaps
9ad5cbcf 6794 in the loadable segments. These can be created by weird
aecc8f8a 6795 parameters to objcopy. Also, fix some solaris weirdness. */
bc67d8a6
NC
6796 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6797 i < num_segments;
c044fabd 6798 i++, segment++)
252b5132 6799 {
252b5132 6800 unsigned int j;
c044fabd 6801 Elf_Internal_Phdr *segment2;
252b5132 6802
aecc8f8a
AM
6803 if (segment->p_type == PT_INTERP)
6804 for (section = ibfd->sections; section; section = section->next)
6805 if (IS_SOLARIS_PT_INTERP (segment, section))
6806 {
6807 /* Mininal change so that the normal section to segment
4cc11e76 6808 assignment code will work. */
aecc8f8a
AM
6809 segment->p_vaddr = section->vma;
6810 break;
6811 }
6812
bc67d8a6 6813 if (segment->p_type != PT_LOAD)
b10a8ae0
L
6814 {
6815 /* Remove PT_GNU_RELRO segment. */
6816 if (segment->p_type == PT_GNU_RELRO)
6817 segment->p_type = PT_NULL;
6818 continue;
6819 }
c044fabd 6820
bc67d8a6 6821 /* Determine if this segment overlaps any previous segments. */
0067a569 6822 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
bc67d8a6
NC
6823 {
6824 bfd_signed_vma extra_length;
c044fabd 6825
bc67d8a6 6826 if (segment2->p_type != PT_LOAD
0067a569 6827 || !SEGMENT_OVERLAPS (segment, segment2))
bc67d8a6 6828 continue;
c044fabd 6829
bc67d8a6
NC
6830 /* Merge the two segments together. */
6831 if (segment2->p_vaddr < segment->p_vaddr)
6832 {
c044fabd 6833 /* Extend SEGMENT2 to include SEGMENT and then delete
08a40648 6834 SEGMENT. */
0067a569
AM
6835 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
6836 - SEGMENT_END (segment2, segment2->p_vaddr));
c044fabd 6837
bc67d8a6
NC
6838 if (extra_length > 0)
6839 {
0067a569 6840 segment2->p_memsz += extra_length;
bc67d8a6
NC
6841 segment2->p_filesz += extra_length;
6842 }
c044fabd 6843
bc67d8a6 6844 segment->p_type = PT_NULL;
c044fabd 6845
bc67d8a6
NC
6846 /* Since we have deleted P we must restart the outer loop. */
6847 i = 0;
6848 segment = elf_tdata (ibfd)->phdr;
6849 break;
6850 }
6851 else
6852 {
c044fabd 6853 /* Extend SEGMENT to include SEGMENT2 and then delete
08a40648 6854 SEGMENT2. */
0067a569
AM
6855 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
6856 - SEGMENT_END (segment, segment->p_vaddr));
c044fabd 6857
bc67d8a6
NC
6858 if (extra_length > 0)
6859 {
0067a569 6860 segment->p_memsz += extra_length;
bc67d8a6
NC
6861 segment->p_filesz += extra_length;
6862 }
c044fabd 6863
bc67d8a6
NC
6864 segment2->p_type = PT_NULL;
6865 }
6866 }
6867 }
c044fabd 6868
bc67d8a6
NC
6869 /* The second scan attempts to assign sections to segments. */
6870 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6871 i < num_segments;
0067a569 6872 i++, segment++)
bc67d8a6 6873 {
0067a569
AM
6874 unsigned int section_count;
6875 asection **sections;
6876 asection *output_section;
6877 unsigned int isec;
9933dc52
AM
6878 asection *matching_lma;
6879 asection *suggested_lma;
0067a569 6880 unsigned int j;
dc810e39 6881 bfd_size_type amt;
0067a569 6882 asection *first_section;
bc67d8a6
NC
6883
6884 if (segment->p_type == PT_NULL)
6885 continue;
c044fabd 6886
9f17e2a6 6887 first_section = NULL;
bc67d8a6 6888 /* Compute how many sections might be placed into this segment. */
b5f852ea
NC
6889 for (section = ibfd->sections, section_count = 0;
6890 section != NULL;
6891 section = section->next)
9f17e2a6
L
6892 {
6893 /* Find the first section in the input segment, which may be
6894 removed from the corresponding output segment. */
6895 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
6896 {
6897 if (first_section == NULL)
6898 first_section = section;
6899 if (section->output_section != NULL)
6900 ++section_count;
6901 }
6902 }
811072d8 6903
b5f852ea
NC
6904 /* Allocate a segment map big enough to contain
6905 all of the sections we have selected. */
00bee008
AM
6906 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
6907 amt += (bfd_size_type) section_count * sizeof (asection *);
a50b1753 6908 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
bc67d8a6 6909 if (map == NULL)
b34976b6 6910 return FALSE;
252b5132
RH
6911
6912 /* Initialise the fields of the segment map. Default to
6913 using the physical address of the segment in the input BFD. */
0067a569
AM
6914 map->next = NULL;
6915 map->p_type = segment->p_type;
6916 map->p_flags = segment->p_flags;
bc67d8a6 6917 map->p_flags_valid = 1;
55d55ac7 6918
9f17e2a6
L
6919 /* If the first section in the input segment is removed, there is
6920 no need to preserve segment physical address in the corresponding
6921 output segment. */
945c025a 6922 if (!first_section || first_section->output_section != NULL)
9f17e2a6
L
6923 {
6924 map->p_paddr = segment->p_paddr;
5c44b38e 6925 map->p_paddr_valid = p_paddr_valid;
9f17e2a6 6926 }
252b5132
RH
6927
6928 /* Determine if this segment contains the ELF file header
6929 and if it contains the program headers themselves. */
bc67d8a6
NC
6930 map->includes_filehdr = (segment->p_offset == 0
6931 && segment->p_filesz >= iehdr->e_ehsize);
bc67d8a6 6932 map->includes_phdrs = 0;
252b5132 6933
0067a569 6934 if (!phdr_included || segment->p_type != PT_LOAD)
252b5132 6935 {
bc67d8a6
NC
6936 map->includes_phdrs =
6937 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
6938 && (segment->p_offset + segment->p_filesz
252b5132
RH
6939 >= ((bfd_vma) iehdr->e_phoff
6940 + iehdr->e_phnum * iehdr->e_phentsize)));
c044fabd 6941
bc67d8a6 6942 if (segment->p_type == PT_LOAD && map->includes_phdrs)
b34976b6 6943 phdr_included = TRUE;
252b5132
RH
6944 }
6945
bc67d8a6 6946 if (section_count == 0)
252b5132
RH
6947 {
6948 /* Special segments, such as the PT_PHDR segment, may contain
6949 no sections, but ordinary, loadable segments should contain
1ed89aa9 6950 something. They are allowed by the ELF spec however, so only
07d6d2b8 6951 a warning is produced.
f98450c6
NC
6952 There is however the valid use case of embedded systems which
6953 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
6954 flash memory with zeros. No warning is shown for that case. */
6955 if (segment->p_type == PT_LOAD
6956 && (segment->p_filesz > 0 || segment->p_memsz == 0))
6957 /* xgettext:c-format */
9793eb77
AM
6958 _bfd_error_handler
6959 (_("%pB: warning: empty loadable segment detected"
6960 " at vaddr=%#" PRIx64 ", is this intentional?"),
6961 ibfd, (uint64_t) segment->p_vaddr);
252b5132 6962
5d695627 6963 map->p_vaddr_offset = segment->p_vaddr;
bc67d8a6 6964 map->count = 0;
c044fabd
KH
6965 *pointer_to_map = map;
6966 pointer_to_map = &map->next;
252b5132
RH
6967
6968 continue;
6969 }
6970
6971 /* Now scan the sections in the input BFD again and attempt
6972 to add their corresponding output sections to the segment map.
6973 The problem here is how to handle an output section which has
6974 been moved (ie had its LMA changed). There are four possibilities:
6975
6976 1. None of the sections have been moved.
6977 In this case we can continue to use the segment LMA from the
6978 input BFD.
6979
6980 2. All of the sections have been moved by the same amount.
6981 In this case we can change the segment's LMA to match the LMA
6982 of the first section.
6983
6984 3. Some of the sections have been moved, others have not.
6985 In this case those sections which have not been moved can be
6986 placed in the current segment which will have to have its size,
6987 and possibly its LMA changed, and a new segment or segments will
6988 have to be created to contain the other sections.
6989
b5f852ea 6990 4. The sections have been moved, but not by the same amount.
252b5132
RH
6991 In this case we can change the segment's LMA to match the LMA
6992 of the first section and we will have to create a new segment
6993 or segments to contain the other sections.
6994
6995 In order to save time, we allocate an array to hold the section
6996 pointers that we are interested in. As these sections get assigned
6997 to a segment, they are removed from this array. */
6998
a50b1753 6999 sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
252b5132 7000 if (sections == NULL)
b34976b6 7001 return FALSE;
252b5132
RH
7002
7003 /* Step One: Scan for segment vs section LMA conflicts.
7004 Also add the sections to the section array allocated above.
7005 Also add the sections to the current segment. In the common
7006 case, where the sections have not been moved, this means that
7007 we have completely filled the segment, and there is nothing
7008 more to do. */
252b5132 7009 isec = 0;
9933dc52
AM
7010 matching_lma = NULL;
7011 suggested_lma = NULL;
252b5132 7012
461c4b2e 7013 for (section = first_section, j = 0;
bc67d8a6
NC
7014 section != NULL;
7015 section = section->next)
252b5132 7016 {
caf47ea6 7017 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
c0f7859b 7018 {
bc67d8a6
NC
7019 output_section = section->output_section;
7020
0067a569 7021 sections[j++] = section;
252b5132
RH
7022
7023 /* The Solaris native linker always sets p_paddr to 0.
7024 We try to catch that case here, and set it to the
5e8d7549
NC
7025 correct value. Note - some backends require that
7026 p_paddr be left as zero. */
5c44b38e 7027 if (!p_paddr_valid
4455705d 7028 && segment->p_vaddr != 0
0067a569 7029 && !bed->want_p_paddr_set_to_zero
252b5132 7030 && isec == 0
bc67d8a6 7031 && output_section->lma != 0
9933dc52
AM
7032 && (align_power (segment->p_vaddr
7033 + (map->includes_filehdr
7034 ? iehdr->e_ehsize : 0)
7035 + (map->includes_phdrs
7036 ? iehdr->e_phnum * iehdr->e_phentsize
7037 : 0),
7038 output_section->alignment_power)
7039 == output_section->vma))
bc67d8a6 7040 map->p_paddr = segment->p_vaddr;
252b5132
RH
7041
7042 /* Match up the physical address of the segment with the
7043 LMA address of the output section. */
bc67d8a6 7044 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5e8d7549 7045 || IS_COREFILE_NOTE (segment, section)
0067a569
AM
7046 || (bed->want_p_paddr_set_to_zero
7047 && IS_CONTAINED_BY_VMA (output_section, segment)))
252b5132 7048 {
9933dc52
AM
7049 if (matching_lma == NULL
7050 || output_section->lma < matching_lma->lma)
7051 matching_lma = output_section;
252b5132
RH
7052
7053 /* We assume that if the section fits within the segment
bc67d8a6 7054 then it does not overlap any other section within that
252b5132 7055 segment. */
0067a569
AM
7056 map->sections[isec++] = output_section;
7057 }
9933dc52
AM
7058 else if (suggested_lma == NULL)
7059 suggested_lma = output_section;
147d51c2
L
7060
7061 if (j == section_count)
7062 break;
252b5132
RH
7063 }
7064 }
7065
bc67d8a6 7066 BFD_ASSERT (j == section_count);
252b5132
RH
7067
7068 /* Step Two: Adjust the physical address of the current segment,
7069 if necessary. */
bc67d8a6 7070 if (isec == section_count)
252b5132
RH
7071 {
7072 /* All of the sections fitted within the segment as currently
7073 specified. This is the default case. Add the segment to
7074 the list of built segments and carry on to process the next
7075 program header in the input BFD. */
bc67d8a6 7076 map->count = section_count;
c044fabd
KH
7077 *pointer_to_map = map;
7078 pointer_to_map = &map->next;
08a40648 7079
5c44b38e
AM
7080 if (p_paddr_valid
7081 && !bed->want_p_paddr_set_to_zero
9933dc52 7082 && matching_lma->lma != map->p_paddr
5c44b38e
AM
7083 && !map->includes_filehdr
7084 && !map->includes_phdrs)
3271a814
NS
7085 /* There is some padding before the first section in the
7086 segment. So, we must account for that in the output
7087 segment's vma. */
5d695627 7088 map->p_vaddr_offset = map->p_paddr - matching_lma->lma;
08a40648 7089
252b5132
RH
7090 free (sections);
7091 continue;
7092 }
252b5132
RH
7093 else
7094 {
9933dc52
AM
7095 /* Change the current segment's physical address to match
7096 the LMA of the first section that fitted, or if no
7097 section fitted, the first section. */
7098 if (matching_lma == NULL)
7099 matching_lma = suggested_lma;
7100
7101 map->p_paddr = matching_lma->lma;
72730e0c 7102
bc67d8a6
NC
7103 /* Offset the segment physical address from the lma
7104 to allow for space taken up by elf headers. */
9933dc52 7105 if (map->includes_phdrs)
010c8431 7106 {
9933dc52
AM
7107 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7108
7109 /* iehdr->e_phnum is just an estimate of the number
7110 of program headers that we will need. Make a note
7111 here of the number we used and the segment we chose
7112 to hold these headers, so that we can adjust the
7113 offset when we know the correct value. */
7114 phdr_adjust_num = iehdr->e_phnum;
7115 phdr_adjust_seg = map;
010c8431 7116 }
252b5132 7117
9933dc52 7118 if (map->includes_filehdr)
bc67d8a6 7119 {
9933dc52
AM
7120 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7121 map->p_paddr -= iehdr->e_ehsize;
7122 /* We've subtracted off the size of headers from the
7123 first section lma, but there may have been some
7124 alignment padding before that section too. Try to
7125 account for that by adjusting the segment lma down to
7126 the same alignment. */
7127 if (segment->p_align != 0 && segment->p_align < align)
7128 align = segment->p_align;
7129 map->p_paddr &= -align;
bc67d8a6 7130 }
252b5132
RH
7131 }
7132
7133 /* Step Three: Loop over the sections again, this time assigning
caf47ea6 7134 those that fit to the current segment and removing them from the
252b5132
RH
7135 sections array; but making sure not to leave large gaps. Once all
7136 possible sections have been assigned to the current segment it is
7137 added to the list of built segments and if sections still remain
7138 to be assigned, a new segment is constructed before repeating
7139 the loop. */
7140 isec = 0;
7141 do
7142 {
bc67d8a6 7143 map->count = 0;
9933dc52 7144 suggested_lma = NULL;
252b5132
RH
7145
7146 /* Fill the current segment with sections that fit. */
bc67d8a6 7147 for (j = 0; j < section_count; j++)
252b5132 7148 {
bc67d8a6 7149 section = sections[j];
252b5132 7150
bc67d8a6 7151 if (section == NULL)
252b5132
RH
7152 continue;
7153
bc67d8a6 7154 output_section = section->output_section;
252b5132 7155
bc67d8a6 7156 BFD_ASSERT (output_section != NULL);
c044fabd 7157
bc67d8a6
NC
7158 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
7159 || IS_COREFILE_NOTE (segment, section))
252b5132 7160 {
bc67d8a6 7161 if (map->count == 0)
252b5132
RH
7162 {
7163 /* If the first section in a segment does not start at
bc67d8a6
NC
7164 the beginning of the segment, then something is
7165 wrong. */
9933dc52
AM
7166 if (align_power (map->p_paddr
7167 + (map->includes_filehdr
7168 ? iehdr->e_ehsize : 0)
7169 + (map->includes_phdrs
7170 ? iehdr->e_phnum * iehdr->e_phentsize
7171 : 0),
7172 output_section->alignment_power)
7173 != output_section->lma)
252b5132
RH
7174 abort ();
7175 }
7176 else
7177 {
0067a569 7178 asection *prev_sec;
252b5132 7179
bc67d8a6 7180 prev_sec = map->sections[map->count - 1];
252b5132
RH
7181
7182 /* If the gap between the end of the previous section
bc67d8a6
NC
7183 and the start of this section is more than
7184 maxpagesize then we need to start a new segment. */
eea6121a 7185 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
079e9a2f 7186 maxpagesize)
caf47ea6 7187 < BFD_ALIGN (output_section->lma, maxpagesize))
0067a569 7188 || (prev_sec->lma + prev_sec->size
079e9a2f 7189 > output_section->lma))
252b5132 7190 {
9933dc52
AM
7191 if (suggested_lma == NULL)
7192 suggested_lma = output_section;
252b5132
RH
7193
7194 continue;
7195 }
7196 }
7197
bc67d8a6 7198 map->sections[map->count++] = output_section;
252b5132
RH
7199 ++isec;
7200 sections[j] = NULL;
9933dc52
AM
7201 if (segment->p_type == PT_LOAD)
7202 section->segment_mark = TRUE;
0067a569 7203 }
9933dc52
AM
7204 else if (suggested_lma == NULL)
7205 suggested_lma = output_section;
252b5132
RH
7206 }
7207
beab4532
NC
7208 /* PR 23932. A corrupt input file may contain sections that cannot
7209 be assigned to any segment - because for example they have a
7210 negative size - or segments that do not contain any sections. */
7211 if (map->count == 0)
7212 {
7213 bfd_set_error (bfd_error_bad_value);
7214 free (sections);
7215 return FALSE;
7216 }
252b5132
RH
7217
7218 /* Add the current segment to the list of built segments. */
c044fabd
KH
7219 *pointer_to_map = map;
7220 pointer_to_map = &map->next;
252b5132 7221
bc67d8a6 7222 if (isec < section_count)
252b5132
RH
7223 {
7224 /* We still have not allocated all of the sections to
7225 segments. Create a new segment here, initialise it
7226 and carry on looping. */
00bee008
AM
7227 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7228 amt += (bfd_size_type) section_count * sizeof (asection *);
5964fc3a 7229 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
bc67d8a6 7230 if (map == NULL)
5ed6aba4
NC
7231 {
7232 free (sections);
7233 return FALSE;
7234 }
252b5132
RH
7235
7236 /* Initialise the fields of the segment map. Set the physical
7237 physical address to the LMA of the first section that has
7238 not yet been assigned. */
0067a569
AM
7239 map->next = NULL;
7240 map->p_type = segment->p_type;
7241 map->p_flags = segment->p_flags;
7242 map->p_flags_valid = 1;
9933dc52 7243 map->p_paddr = suggested_lma->lma;
5c44b38e 7244 map->p_paddr_valid = p_paddr_valid;
bc67d8a6 7245 map->includes_filehdr = 0;
0067a569 7246 map->includes_phdrs = 0;
252b5132
RH
7247 }
7248 }
bc67d8a6 7249 while (isec < section_count);
252b5132
RH
7250
7251 free (sections);
7252 }
7253
12bd6957 7254 elf_seg_map (obfd) = map_first;
bc67d8a6
NC
7255
7256 /* If we had to estimate the number of program headers that were
9ad5cbcf 7257 going to be needed, then check our estimate now and adjust
bc67d8a6
NC
7258 the offset if necessary. */
7259 if (phdr_adjust_seg != NULL)
7260 {
7261 unsigned int count;
c044fabd 7262
bc67d8a6 7263 for (count = 0, map = map_first; map != NULL; map = map->next)
c044fabd 7264 count++;
252b5132 7265
bc67d8a6
NC
7266 if (count > phdr_adjust_num)
7267 phdr_adjust_seg->p_paddr
7268 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
9933dc52
AM
7269
7270 for (map = map_first; map != NULL; map = map->next)
7271 if (map->p_type == PT_PHDR)
7272 {
7273 bfd_vma adjust
7274 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7275 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7276 break;
7277 }
bc67d8a6 7278 }
c044fabd 7279
bc67d8a6 7280#undef SEGMENT_END
eecdbe52 7281#undef SECTION_SIZE
bc67d8a6
NC
7282#undef IS_CONTAINED_BY_VMA
7283#undef IS_CONTAINED_BY_LMA
0efc80c8 7284#undef IS_NOTE
252b5132 7285#undef IS_COREFILE_NOTE
bc67d8a6 7286#undef IS_SOLARIS_PT_INTERP
9f17e2a6 7287#undef IS_SECTION_IN_INPUT_SEGMENT
bc67d8a6
NC
7288#undef INCLUDE_SECTION_IN_SEGMENT
7289#undef SEGMENT_AFTER_SEGMENT
7290#undef SEGMENT_OVERLAPS
b34976b6 7291 return TRUE;
252b5132
RH
7292}
7293
84d1d650
L
7294/* Copy ELF program header information. */
7295
7296static bfd_boolean
7297copy_elf_program_header (bfd *ibfd, bfd *obfd)
7298{
7299 Elf_Internal_Ehdr *iehdr;
7300 struct elf_segment_map *map;
7301 struct elf_segment_map *map_first;
7302 struct elf_segment_map **pointer_to_map;
7303 Elf_Internal_Phdr *segment;
7304 unsigned int i;
7305 unsigned int num_segments;
7306 bfd_boolean phdr_included = FALSE;
88967714 7307 bfd_boolean p_paddr_valid;
84d1d650
L
7308
7309 iehdr = elf_elfheader (ibfd);
7310
7311 map_first = NULL;
7312 pointer_to_map = &map_first;
7313
88967714
AM
7314 /* If all the segment p_paddr fields are zero, don't set
7315 map->p_paddr_valid. */
7316 p_paddr_valid = FALSE;
84d1d650 7317 num_segments = elf_elfheader (ibfd)->e_phnum;
88967714
AM
7318 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7319 i < num_segments;
7320 i++, segment++)
7321 if (segment->p_paddr != 0)
7322 {
7323 p_paddr_valid = TRUE;
7324 break;
7325 }
7326
84d1d650
L
7327 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7328 i < num_segments;
7329 i++, segment++)
7330 {
7331 asection *section;
7332 unsigned int section_count;
7333 bfd_size_type amt;
7334 Elf_Internal_Shdr *this_hdr;
53020534 7335 asection *first_section = NULL;
a76e6f2f 7336 asection *lowest_section;
2542e49e 7337 bfd_boolean no_contents = TRUE;
84d1d650 7338
84d1d650
L
7339 /* Compute how many sections are in this segment. */
7340 for (section = ibfd->sections, section_count = 0;
7341 section != NULL;
7342 section = section->next)
7343 {
7344 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7345 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
3271a814 7346 {
a76e6f2f
AM
7347 if (first_section == NULL)
7348 first_section = section;
2542e49e
JL
7349 if (elf_section_type (section) != SHT_NOBITS)
7350 no_contents = FALSE;
3271a814
NS
7351 section_count++;
7352 }
84d1d650
L
7353 }
7354
7355 /* Allocate a segment map big enough to contain
7356 all of the sections we have selected. */
00bee008
AM
7357 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7358 amt += (bfd_size_type) section_count * sizeof (asection *);
a50b1753 7359 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
84d1d650
L
7360 if (map == NULL)
7361 return FALSE;
7362
7363 /* Initialize the fields of the output segment map with the
7364 input segment. */
7365 map->next = NULL;
7366 map->p_type = segment->p_type;
7367 map->p_flags = segment->p_flags;
7368 map->p_flags_valid = 1;
7369 map->p_paddr = segment->p_paddr;
88967714 7370 map->p_paddr_valid = p_paddr_valid;
3f570048
AM
7371 map->p_align = segment->p_align;
7372 map->p_align_valid = 1;
3271a814 7373 map->p_vaddr_offset = 0;
84d1d650 7374
04c3a755
NS
7375 if (map->p_type == PT_GNU_RELRO
7376 || map->p_type == PT_GNU_STACK)
b10a8ae0
L
7377 {
7378 /* The PT_GNU_RELRO segment may contain the first a few
7379 bytes in the .got.plt section even if the whole .got.plt
7380 section isn't in the PT_GNU_RELRO segment. We won't
04c3a755
NS
7381 change the size of the PT_GNU_RELRO segment.
7382 Similarly, PT_GNU_STACK size is significant on uclinux
7383 systems. */
9433b9b1 7384 map->p_size = segment->p_memsz;
b10a8ae0
L
7385 map->p_size_valid = 1;
7386 }
7387
84d1d650
L
7388 /* Determine if this segment contains the ELF file header
7389 and if it contains the program headers themselves. */
7390 map->includes_filehdr = (segment->p_offset == 0
7391 && segment->p_filesz >= iehdr->e_ehsize);
7392
7393 map->includes_phdrs = 0;
7394 if (! phdr_included || segment->p_type != PT_LOAD)
7395 {
7396 map->includes_phdrs =
7397 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7398 && (segment->p_offset + segment->p_filesz
7399 >= ((bfd_vma) iehdr->e_phoff
7400 + iehdr->e_phnum * iehdr->e_phentsize)));
7401
7402 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7403 phdr_included = TRUE;
7404 }
7405
bbefd0a9 7406 lowest_section = NULL;
84d1d650
L
7407 if (section_count != 0)
7408 {
7409 unsigned int isec = 0;
7410
53020534 7411 for (section = first_section;
84d1d650
L
7412 section != NULL;
7413 section = section->next)
7414 {
7415 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7416 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
53020534
L
7417 {
7418 map->sections[isec++] = section->output_section;
a76e6f2f
AM
7419 if ((section->flags & SEC_ALLOC) != 0)
7420 {
7421 bfd_vma seg_off;
7422
bbefd0a9
AM
7423 if (lowest_section == NULL
7424 || section->lma < lowest_section->lma)
fb8a5684
AM
7425 lowest_section = section;
7426
a76e6f2f
AM
7427 /* Section lmas are set up from PT_LOAD header
7428 p_paddr in _bfd_elf_make_section_from_shdr.
7429 If this header has a p_paddr that disagrees
7430 with the section lma, flag the p_paddr as
7431 invalid. */
7432 if ((section->flags & SEC_LOAD) != 0)
7433 seg_off = this_hdr->sh_offset - segment->p_offset;
7434 else
7435 seg_off = this_hdr->sh_addr - segment->p_vaddr;
7436 if (section->lma - segment->p_paddr != seg_off)
7437 map->p_paddr_valid = FALSE;
7438 }
53020534
L
7439 if (isec == section_count)
7440 break;
7441 }
84d1d650
L
7442 }
7443 }
7444
a76e6f2f 7445 if (map->includes_filehdr && lowest_section != NULL)
2542e49e
JL
7446 {
7447 /* Try to keep the space used by the headers plus any
7448 padding fixed. If there are sections with file contents
7449 in this segment then the lowest sh_offset is the best
7450 guess. Otherwise the segment only has file contents for
7451 the headers, and p_filesz is the best guess. */
7452 if (no_contents)
7453 map->header_size = segment->p_filesz;
7454 else
7455 map->header_size = lowest_section->filepos;
7456 }
d324f6d6 7457
5d695627
AM
7458 if (section_count == 0)
7459 map->p_vaddr_offset = segment->p_vaddr;
7460 else if (!map->includes_phdrs
7461 && !map->includes_filehdr
7462 && map->p_paddr_valid)
7463 /* Account for padding before the first section. */
7464 map->p_vaddr_offset = (segment->p_paddr
7465 - (lowest_section ? lowest_section->lma : 0));
a76e6f2f 7466
84d1d650
L
7467 map->count = section_count;
7468 *pointer_to_map = map;
7469 pointer_to_map = &map->next;
7470 }
7471
12bd6957 7472 elf_seg_map (obfd) = map_first;
84d1d650
L
7473 return TRUE;
7474}
7475
7476/* Copy private BFD data. This copies or rewrites ELF program header
7477 information. */
7478
7479static bfd_boolean
7480copy_private_bfd_data (bfd *ibfd, bfd *obfd)
7481{
84d1d650
L
7482 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7483 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7484 return TRUE;
7485
7486 if (elf_tdata (ibfd)->phdr == NULL)
7487 return TRUE;
7488
7489 if (ibfd->xvec == obfd->xvec)
7490 {
cb3ff1e5
NC
7491 /* Check to see if any sections in the input BFD
7492 covered by ELF program header have changed. */
d55ce4e2 7493 Elf_Internal_Phdr *segment;
84d1d650
L
7494 asection *section, *osec;
7495 unsigned int i, num_segments;
7496 Elf_Internal_Shdr *this_hdr;
147d51c2
L
7497 const struct elf_backend_data *bed;
7498
7499 bed = get_elf_backend_data (ibfd);
7500
7501 /* Regenerate the segment map if p_paddr is set to 0. */
7502 if (bed->want_p_paddr_set_to_zero)
7503 goto rewrite;
84d1d650
L
7504
7505 /* Initialize the segment mark field. */
7506 for (section = obfd->sections; section != NULL;
7507 section = section->next)
7508 section->segment_mark = FALSE;
7509
7510 num_segments = elf_elfheader (ibfd)->e_phnum;
7511 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7512 i < num_segments;
7513 i++, segment++)
7514 {
5f6999aa
NC
7515 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7516 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7517 which severly confuses things, so always regenerate the segment
7518 map in this case. */
7519 if (segment->p_paddr == 0
7520 && segment->p_memsz == 0
7521 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
cb3ff1e5 7522 goto rewrite;
5f6999aa 7523
84d1d650
L
7524 for (section = ibfd->sections;
7525 section != NULL; section = section->next)
7526 {
7527 /* We mark the output section so that we know it comes
7528 from the input BFD. */
7529 osec = section->output_section;
7530 if (osec)
7531 osec->segment_mark = TRUE;
7532
7533 /* Check if this section is covered by the segment. */
7534 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7535 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
84d1d650
L
7536 {
7537 /* FIXME: Check if its output section is changed or
7538 removed. What else do we need to check? */
7539 if (osec == NULL
7540 || section->flags != osec->flags
7541 || section->lma != osec->lma
7542 || section->vma != osec->vma
7543 || section->size != osec->size
7544 || section->rawsize != osec->rawsize
7545 || section->alignment_power != osec->alignment_power)
7546 goto rewrite;
7547 }
7548 }
7549 }
7550
cb3ff1e5 7551 /* Check to see if any output section do not come from the
84d1d650
L
7552 input BFD. */
7553 for (section = obfd->sections; section != NULL;
7554 section = section->next)
7555 {
535b785f 7556 if (!section->segment_mark)
84d1d650
L
7557 goto rewrite;
7558 else
7559 section->segment_mark = FALSE;
7560 }
7561
7562 return copy_elf_program_header (ibfd, obfd);
7563 }
7564
7565rewrite:
f1d85785
L
7566 if (ibfd->xvec == obfd->xvec)
7567 {
7568 /* When rewriting program header, set the output maxpagesize to
7569 the maximum alignment of input PT_LOAD segments. */
7570 Elf_Internal_Phdr *segment;
7571 unsigned int i;
7572 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
7573 bfd_vma maxpagesize = 0;
7574
7575 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7576 i < num_segments;
7577 i++, segment++)
7578 if (segment->p_type == PT_LOAD
7579 && maxpagesize < segment->p_align)
c86934ce
NC
7580 {
7581 /* PR 17512: file: f17299af. */
7582 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
695344c0 7583 /* xgettext:c-format */
2dcf00ce
AM
7584 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7585 PRIx64 " is too large"),
7586 ibfd, (uint64_t) segment->p_align);
c86934ce
NC
7587 else
7588 maxpagesize = segment->p_align;
7589 }
f1d85785
L
7590
7591 if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize)
7592 bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize);
7593 }
7594
84d1d650
L
7595 return rewrite_elf_program_header (ibfd, obfd);
7596}
7597
ccd2ec6a
L
7598/* Initialize private output section information from input section. */
7599
7600bfd_boolean
7601_bfd_elf_init_private_section_data (bfd *ibfd,
7602 asection *isec,
7603 bfd *obfd,
7604 asection *osec,
7605 struct bfd_link_info *link_info)
7606
7607{
7608 Elf_Internal_Shdr *ihdr, *ohdr;
0e1862bb
L
7609 bfd_boolean final_link = (link_info != NULL
7610 && !bfd_link_relocatable (link_info));
ccd2ec6a
L
7611
7612 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7613 || obfd->xvec->flavour != bfd_target_elf_flavour)
7614 return TRUE;
7615
ba85c43e
NC
7616 BFD_ASSERT (elf_section_data (osec) != NULL);
7617
dfa7b0b8
AM
7618 /* For objcopy and relocatable link, don't copy the output ELF
7619 section type from input if the output BFD section flags have been
7620 set to something different. For a final link allow some flags
7621 that the linker clears to differ. */
42bb2e33 7622 if (elf_section_type (osec) == SHT_NULL
dfa7b0b8
AM
7623 && (osec->flags == isec->flags
7624 || (final_link
7625 && ((osec->flags ^ isec->flags)
0814be7d 7626 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
42bb2e33 7627 elf_section_type (osec) = elf_section_type (isec);
d270463e
L
7628
7629 /* FIXME: Is this correct for all OS/PROC specific flags? */
7630 elf_section_flags (osec) |= (elf_section_flags (isec)
7631 & (SHF_MASKOS | SHF_MASKPROC));
ccd2ec6a 7632
a91e1603
L
7633 /* Copy sh_info from input for mbind section. */
7634 if (elf_section_flags (isec) & SHF_GNU_MBIND)
7635 elf_section_data (osec)->this_hdr.sh_info
7636 = elf_section_data (isec)->this_hdr.sh_info;
7637
ccd2ec6a
L
7638 /* Set things up for objcopy and relocatable link. The output
7639 SHT_GROUP section will have its elf_next_in_group pointing back
7640 to the input group members. Ignore linker created group section.
7641 See elfNN_ia64_object_p in elfxx-ia64.c. */
7bdf4127
AB
7642 if ((link_info == NULL
7643 || !link_info->resolve_section_groups)
7644 && (elf_sec_group (isec) == NULL
7645 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
ccd2ec6a 7646 {
7bdf4127
AB
7647 if (elf_section_flags (isec) & SHF_GROUP)
7648 elf_section_flags (osec) |= SHF_GROUP;
7649 elf_next_in_group (osec) = elf_next_in_group (isec);
7650 elf_section_data (osec)->group = elf_section_data (isec)->group;
ccd2ec6a
L
7651 }
7652
7bdf4127
AB
7653 /* If not decompress, preserve SHF_COMPRESSED. */
7654 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
7655 elf_section_flags (osec) |= (elf_section_flags (isec)
7656 & SHF_COMPRESSED);
7657
ccd2ec6a
L
7658 ihdr = &elf_section_data (isec)->this_hdr;
7659
7660 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7661 don't use the output section of the linked-to section since it
7662 may be NULL at this point. */
7663 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
7664 {
7665 ohdr = &elf_section_data (osec)->this_hdr;
7666 ohdr->sh_flags |= SHF_LINK_ORDER;
7667 elf_linked_to_section (osec) = elf_linked_to_section (isec);
7668 }
7669
7670 osec->use_rela_p = isec->use_rela_p;
7671
7672 return TRUE;
7673}
7674
252b5132
RH
7675/* Copy private section information. This copies over the entsize
7676 field, and sometimes the info field. */
7677
b34976b6 7678bfd_boolean
217aa764
AM
7679_bfd_elf_copy_private_section_data (bfd *ibfd,
7680 asection *isec,
7681 bfd *obfd,
7682 asection *osec)
252b5132
RH
7683{
7684 Elf_Internal_Shdr *ihdr, *ohdr;
7685
7686 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7687 || obfd->xvec->flavour != bfd_target_elf_flavour)
b34976b6 7688 return TRUE;
252b5132 7689
252b5132
RH
7690 ihdr = &elf_section_data (isec)->this_hdr;
7691 ohdr = &elf_section_data (osec)->this_hdr;
7692
7693 ohdr->sh_entsize = ihdr->sh_entsize;
7694
7695 if (ihdr->sh_type == SHT_SYMTAB
7696 || ihdr->sh_type == SHT_DYNSYM
7697 || ihdr->sh_type == SHT_GNU_verneed
7698 || ihdr->sh_type == SHT_GNU_verdef)
7699 ohdr->sh_info = ihdr->sh_info;
7700
ccd2ec6a
L
7701 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
7702 NULL);
252b5132
RH
7703}
7704
d0bf826b
AM
7705/* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7706 necessary if we are removing either the SHT_GROUP section or any of
7707 the group member sections. DISCARDED is the value that a section's
7708 output_section has if the section will be discarded, NULL when this
7709 function is called from objcopy, bfd_abs_section_ptr when called
7710 from the linker. */
80fccad2
BW
7711
7712bfd_boolean
d0bf826b 7713_bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
80fccad2 7714{
30288845
AM
7715 asection *isec;
7716
30288845 7717 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
415f38a6 7718 if (elf_section_type (isec) == SHT_GROUP)
30288845
AM
7719 {
7720 asection *first = elf_next_in_group (isec);
7721 asection *s = first;
d0bf826b
AM
7722 bfd_size_type removed = 0;
7723
30288845
AM
7724 while (s != NULL)
7725 {
415f38a6
AM
7726 /* If this member section is being output but the
7727 SHT_GROUP section is not, then clear the group info
7728 set up by _bfd_elf_copy_private_section_data. */
d0bf826b
AM
7729 if (s->output_section != discarded
7730 && isec->output_section == discarded)
30288845
AM
7731 {
7732 elf_section_flags (s->output_section) &= ~SHF_GROUP;
7733 elf_group_name (s->output_section) = NULL;
7734 }
415f38a6
AM
7735 /* Conversely, if the member section is not being output
7736 but the SHT_GROUP section is, then adjust its size. */
d0bf826b
AM
7737 else if (s->output_section == discarded
7738 && isec->output_section != discarded)
6e5e9d58
AM
7739 {
7740 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
7741 removed += 4;
7742 if (elf_sec->rel.hdr != NULL
7743 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
7744 removed += 4;
7745 if (elf_sec->rela.hdr != NULL
7746 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
7747 removed += 4;
7748 }
30288845
AM
7749 s = elf_next_in_group (s);
7750 if (s == first)
7751 break;
7752 }
d0bf826b
AM
7753 if (removed != 0)
7754 {
7755 if (discarded != NULL)
7756 {
7757 /* If we've been called for ld -r, then we need to
6e5e9d58 7758 adjust the input section size. */
d0bf826b
AM
7759 if (isec->rawsize == 0)
7760 isec->rawsize = isec->size;
7761 isec->size = isec->rawsize - removed;
6e5e9d58
AM
7762 if (isec->size <= 4)
7763 {
7764 isec->size = 0;
7765 isec->flags |= SEC_EXCLUDE;
7766 }
d0bf826b
AM
7767 }
7768 else
7769 {
7770 /* Adjust the output section size when called from
7771 objcopy. */
7772 isec->output_section->size -= removed;
6e5e9d58
AM
7773 if (isec->output_section->size <= 4)
7774 {
7775 isec->output_section->size = 0;
7776 isec->output_section->flags |= SEC_EXCLUDE;
7777 }
d0bf826b
AM
7778 }
7779 }
30288845
AM
7780 }
7781
80fccad2
BW
7782 return TRUE;
7783}
7784
d0bf826b
AM
7785/* Copy private header information. */
7786
7787bfd_boolean
7788_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
7789{
7790 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7791 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7792 return TRUE;
7793
7794 /* Copy over private BFD data if it has not already been copied.
7795 This must be done here, rather than in the copy_private_bfd_data
7796 entry point, because the latter is called after the section
7797 contents have been set, which means that the program headers have
7798 already been worked out. */
12bd6957 7799 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
d0bf826b
AM
7800 {
7801 if (! copy_private_bfd_data (ibfd, obfd))
7802 return FALSE;
7803 }
7804
7805 return _bfd_elf_fixup_group_sections (ibfd, NULL);
7806}
7807
252b5132
RH
7808/* Copy private symbol information. If this symbol is in a section
7809 which we did not map into a BFD section, try to map the section
7810 index correctly. We use special macro definitions for the mapped
7811 section indices; these definitions are interpreted by the
7812 swap_out_syms function. */
7813
9ad5cbcf
AM
7814#define MAP_ONESYMTAB (SHN_HIOS + 1)
7815#define MAP_DYNSYMTAB (SHN_HIOS + 2)
7816#define MAP_STRTAB (SHN_HIOS + 3)
7817#define MAP_SHSTRTAB (SHN_HIOS + 4)
7818#define MAP_SYM_SHNDX (SHN_HIOS + 5)
252b5132 7819
b34976b6 7820bfd_boolean
217aa764
AM
7821_bfd_elf_copy_private_symbol_data (bfd *ibfd,
7822 asymbol *isymarg,
7823 bfd *obfd,
7824 asymbol *osymarg)
252b5132
RH
7825{
7826 elf_symbol_type *isym, *osym;
7827
7828 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7829 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 7830 return TRUE;
252b5132
RH
7831
7832 isym = elf_symbol_from (ibfd, isymarg);
7833 osym = elf_symbol_from (obfd, osymarg);
7834
7835 if (isym != NULL
8424d8f5 7836 && isym->internal_elf_sym.st_shndx != 0
252b5132
RH
7837 && osym != NULL
7838 && bfd_is_abs_section (isym->symbol.section))
7839 {
7840 unsigned int shndx;
7841
7842 shndx = isym->internal_elf_sym.st_shndx;
7843 if (shndx == elf_onesymtab (ibfd))
7844 shndx = MAP_ONESYMTAB;
7845 else if (shndx == elf_dynsymtab (ibfd))
7846 shndx = MAP_DYNSYMTAB;
12bd6957 7847 else if (shndx == elf_strtab_sec (ibfd))
252b5132 7848 shndx = MAP_STRTAB;
12bd6957 7849 else if (shndx == elf_shstrtab_sec (ibfd))
252b5132 7850 shndx = MAP_SHSTRTAB;
6a40cf0c 7851 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
9ad5cbcf 7852 shndx = MAP_SYM_SHNDX;
252b5132
RH
7853 osym->internal_elf_sym.st_shndx = shndx;
7854 }
7855
b34976b6 7856 return TRUE;
252b5132
RH
7857}
7858
7859/* Swap out the symbols. */
7860
b34976b6 7861static bfd_boolean
217aa764 7862swap_out_syms (bfd *abfd,
ef10c3ac 7863 struct elf_strtab_hash **sttp,
217aa764 7864 int relocatable_p)
252b5132 7865{
9c5bfbb7 7866 const struct elf_backend_data *bed;
079e9a2f
AM
7867 int symcount;
7868 asymbol **syms;
ef10c3ac 7869 struct elf_strtab_hash *stt;
079e9a2f 7870 Elf_Internal_Shdr *symtab_hdr;
9ad5cbcf 7871 Elf_Internal_Shdr *symtab_shndx_hdr;
079e9a2f 7872 Elf_Internal_Shdr *symstrtab_hdr;
ef10c3ac 7873 struct elf_sym_strtab *symstrtab;
f075ee0c
AM
7874 bfd_byte *outbound_syms;
7875 bfd_byte *outbound_shndx;
ef10c3ac
L
7876 unsigned long outbound_syms_index;
7877 unsigned long outbound_shndx_index;
079e9a2f 7878 int idx;
12bd6957 7879 unsigned int num_locals;
079e9a2f 7880 bfd_size_type amt;
174fd7f9 7881 bfd_boolean name_local_sections;
252b5132 7882
12bd6957 7883 if (!elf_map_symbols (abfd, &num_locals))
b34976b6 7884 return FALSE;
252b5132 7885
c044fabd 7886 /* Dump out the symtabs. */
ef10c3ac 7887 stt = _bfd_elf_strtab_init ();
079e9a2f 7888 if (stt == NULL)
b34976b6 7889 return FALSE;
252b5132 7890
079e9a2f
AM
7891 bed = get_elf_backend_data (abfd);
7892 symcount = bfd_get_symcount (abfd);
7893 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7894 symtab_hdr->sh_type = SHT_SYMTAB;
7895 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
7896 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
12bd6957 7897 symtab_hdr->sh_info = num_locals + 1;
72de5009 7898 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
079e9a2f
AM
7899
7900 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
7901 symstrtab_hdr->sh_type = SHT_STRTAB;
7902
ef10c3ac
L
7903 /* Allocate buffer to swap out the .strtab section. */
7904 symstrtab = (struct elf_sym_strtab *) bfd_malloc ((symcount + 1)
7905 * sizeof (*symstrtab));
7906 if (symstrtab == NULL)
7907 {
7908 _bfd_elf_strtab_free (stt);
7909 return FALSE;
7910 }
7911
a50b1753 7912 outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
07d6d2b8 7913 bed->s->sizeof_sym);
079e9a2f 7914 if (outbound_syms == NULL)
5ed6aba4 7915 {
ef10c3ac
L
7916error_return:
7917 _bfd_elf_strtab_free (stt);
7918 free (symstrtab);
5ed6aba4
NC
7919 return FALSE;
7920 }
217aa764 7921 symtab_hdr->contents = outbound_syms;
ef10c3ac 7922 outbound_syms_index = 0;
252b5132 7923
9ad5cbcf 7924 outbound_shndx = NULL;
ef10c3ac 7925 outbound_shndx_index = 0;
6a40cf0c
NC
7926
7927 if (elf_symtab_shndx_list (abfd))
9ad5cbcf 7928 {
6a40cf0c
NC
7929 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
7930 if (symtab_shndx_hdr->sh_name != 0)
7931 {
7932 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
7933 outbound_shndx = (bfd_byte *)
7934 bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
7935 if (outbound_shndx == NULL)
7936 goto error_return;
5ed6aba4 7937
6a40cf0c
NC
7938 symtab_shndx_hdr->contents = outbound_shndx;
7939 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
7940 symtab_shndx_hdr->sh_size = amt;
7941 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
7942 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
7943 }
7944 /* FIXME: What about any other headers in the list ? */
9ad5cbcf
AM
7945 }
7946
589e6347 7947 /* Now generate the data (for "contents"). */
079e9a2f
AM
7948 {
7949 /* Fill in zeroth symbol and swap it out. */
7950 Elf_Internal_Sym sym;
7951 sym.st_name = 0;
7952 sym.st_value = 0;
7953 sym.st_size = 0;
7954 sym.st_info = 0;
7955 sym.st_other = 0;
7956 sym.st_shndx = SHN_UNDEF;
35fc36a8 7957 sym.st_target_internal = 0;
ef10c3ac
L
7958 symstrtab[0].sym = sym;
7959 symstrtab[0].dest_index = outbound_syms_index;
7960 symstrtab[0].destshndx_index = outbound_shndx_index;
7961 outbound_syms_index++;
9ad5cbcf 7962 if (outbound_shndx != NULL)
ef10c3ac 7963 outbound_shndx_index++;
079e9a2f 7964 }
252b5132 7965
174fd7f9
RS
7966 name_local_sections
7967 = (bed->elf_backend_name_local_section_symbols
7968 && bed->elf_backend_name_local_section_symbols (abfd));
7969
079e9a2f 7970 syms = bfd_get_outsymbols (abfd);
ef10c3ac 7971 for (idx = 0; idx < symcount;)
252b5132 7972 {
252b5132 7973 Elf_Internal_Sym sym;
079e9a2f
AM
7974 bfd_vma value = syms[idx]->value;
7975 elf_symbol_type *type_ptr;
7976 flagword flags = syms[idx]->flags;
7977 int type;
252b5132 7978
174fd7f9
RS
7979 if (!name_local_sections
7980 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
079e9a2f
AM
7981 {
7982 /* Local section symbols have no name. */
ef10c3ac 7983 sym.st_name = (unsigned long) -1;
079e9a2f
AM
7984 }
7985 else
7986 {
ef10c3ac
L
7987 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
7988 to get the final offset for st_name. */
7989 sym.st_name
7990 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
7991 FALSE);
079e9a2f 7992 if (sym.st_name == (unsigned long) -1)
ef10c3ac 7993 goto error_return;
079e9a2f 7994 }
252b5132 7995
079e9a2f 7996 type_ptr = elf_symbol_from (abfd, syms[idx]);
252b5132 7997
079e9a2f
AM
7998 if ((flags & BSF_SECTION_SYM) == 0
7999 && bfd_is_com_section (syms[idx]->section))
8000 {
8001 /* ELF common symbols put the alignment into the `value' field,
8002 and the size into the `size' field. This is backwards from
8003 how BFD handles it, so reverse it here. */
8004 sym.st_size = value;
8005 if (type_ptr == NULL
8006 || type_ptr->internal_elf_sym.st_value == 0)
8007 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8008 else
8009 sym.st_value = type_ptr->internal_elf_sym.st_value;
8010 sym.st_shndx = _bfd_elf_section_from_bfd_section
8011 (abfd, syms[idx]->section);
8012 }
8013 else
8014 {
8015 asection *sec = syms[idx]->section;
cb33740c 8016 unsigned int shndx;
252b5132 8017
079e9a2f
AM
8018 if (sec->output_section)
8019 {
8020 value += sec->output_offset;
8021 sec = sec->output_section;
8022 }
589e6347 8023
079e9a2f
AM
8024 /* Don't add in the section vma for relocatable output. */
8025 if (! relocatable_p)
8026 value += sec->vma;
8027 sym.st_value = value;
8028 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8029
8030 if (bfd_is_abs_section (sec)
8031 && type_ptr != NULL
8032 && type_ptr->internal_elf_sym.st_shndx != 0)
8033 {
8034 /* This symbol is in a real ELF section which we did
8035 not create as a BFD section. Undo the mapping done
8036 by copy_private_symbol_data. */
8037 shndx = type_ptr->internal_elf_sym.st_shndx;
8038 switch (shndx)
8039 {
8040 case MAP_ONESYMTAB:
8041 shndx = elf_onesymtab (abfd);
8042 break;
8043 case MAP_DYNSYMTAB:
8044 shndx = elf_dynsymtab (abfd);
8045 break;
8046 case MAP_STRTAB:
12bd6957 8047 shndx = elf_strtab_sec (abfd);
079e9a2f
AM
8048 break;
8049 case MAP_SHSTRTAB:
12bd6957 8050 shndx = elf_shstrtab_sec (abfd);
079e9a2f 8051 break;
9ad5cbcf 8052 case MAP_SYM_SHNDX:
6a40cf0c
NC
8053 if (elf_symtab_shndx_list (abfd))
8054 shndx = elf_symtab_shndx_list (abfd)->ndx;
9ad5cbcf 8055 break;
079e9a2f 8056 default:
15bc576a 8057 shndx = SHN_ABS;
079e9a2f
AM
8058 break;
8059 }
8060 }
8061 else
8062 {
8063 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
252b5132 8064
cb33740c 8065 if (shndx == SHN_BAD)
079e9a2f
AM
8066 {
8067 asection *sec2;
8068
8069 /* Writing this would be a hell of a lot easier if
8070 we had some decent documentation on bfd, and
8071 knew what to expect of the library, and what to
8072 demand of applications. For example, it
8073 appears that `objcopy' might not set the
8074 section of a symbol to be a section that is
8075 actually in the output file. */
8076 sec2 = bfd_get_section_by_name (abfd, sec->name);
5df1bc57
AM
8077 if (sec2 != NULL)
8078 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8079 if (shndx == SHN_BAD)
589e6347 8080 {
695344c0 8081 /* xgettext:c-format */
9793eb77
AM
8082 _bfd_error_handler
8083 (_("unable to find equivalent output section"
8084 " for symbol '%s' from section '%s'"),
8085 syms[idx]->name ? syms[idx]->name : "<Local sym>",
8086 sec->name);
811072d8 8087 bfd_set_error (bfd_error_invalid_operation);
ef10c3ac 8088 goto error_return;
589e6347 8089 }
079e9a2f
AM
8090 }
8091 }
252b5132 8092
079e9a2f
AM
8093 sym.st_shndx = shndx;
8094 }
252b5132 8095
13ae64f3
JJ
8096 if ((flags & BSF_THREAD_LOCAL) != 0)
8097 type = STT_TLS;
d8045f23
NC
8098 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8099 type = STT_GNU_IFUNC;
13ae64f3 8100 else if ((flags & BSF_FUNCTION) != 0)
079e9a2f
AM
8101 type = STT_FUNC;
8102 else if ((flags & BSF_OBJECT) != 0)
8103 type = STT_OBJECT;
d9352518
DB
8104 else if ((flags & BSF_RELC) != 0)
8105 type = STT_RELC;
8106 else if ((flags & BSF_SRELC) != 0)
8107 type = STT_SRELC;
079e9a2f
AM
8108 else
8109 type = STT_NOTYPE;
252b5132 8110
13ae64f3
JJ
8111 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8112 type = STT_TLS;
8113
589e6347 8114 /* Processor-specific types. */
079e9a2f
AM
8115 if (type_ptr != NULL
8116 && bed->elf_backend_get_symbol_type)
8117 type = ((*bed->elf_backend_get_symbol_type)
8118 (&type_ptr->internal_elf_sym, type));
252b5132 8119
079e9a2f
AM
8120 if (flags & BSF_SECTION_SYM)
8121 {
8122 if (flags & BSF_GLOBAL)
8123 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8124 else
8125 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8126 }
8127 else if (bfd_is_com_section (syms[idx]->section))
0a40daed 8128 {
b8871f35
L
8129 if (type != STT_TLS)
8130 {
8131 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8132 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8133 ? STT_COMMON : STT_OBJECT);
8134 else
8135 type = ((flags & BSF_ELF_COMMON) != 0
8136 ? STT_COMMON : STT_OBJECT);
8137 }
8138 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
0a40daed 8139 }
079e9a2f
AM
8140 else if (bfd_is_und_section (syms[idx]->section))
8141 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8142 ? STB_WEAK
8143 : STB_GLOBAL),
8144 type);
8145 else if (flags & BSF_FILE)
8146 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8147 else
8148 {
8149 int bind = STB_LOCAL;
252b5132 8150
079e9a2f
AM
8151 if (flags & BSF_LOCAL)
8152 bind = STB_LOCAL;
3e7a7d11
NC
8153 else if (flags & BSF_GNU_UNIQUE)
8154 bind = STB_GNU_UNIQUE;
079e9a2f
AM
8155 else if (flags & BSF_WEAK)
8156 bind = STB_WEAK;
8157 else if (flags & BSF_GLOBAL)
8158 bind = STB_GLOBAL;
252b5132 8159
079e9a2f
AM
8160 sym.st_info = ELF_ST_INFO (bind, type);
8161 }
252b5132 8162
079e9a2f 8163 if (type_ptr != NULL)
35fc36a8
RS
8164 {
8165 sym.st_other = type_ptr->internal_elf_sym.st_other;
8166 sym.st_target_internal
8167 = type_ptr->internal_elf_sym.st_target_internal;
8168 }
079e9a2f 8169 else
35fc36a8
RS
8170 {
8171 sym.st_other = 0;
8172 sym.st_target_internal = 0;
8173 }
252b5132 8174
ef10c3ac
L
8175 idx++;
8176 symstrtab[idx].sym = sym;
8177 symstrtab[idx].dest_index = outbound_syms_index;
8178 symstrtab[idx].destshndx_index = outbound_shndx_index;
8179
8180 outbound_syms_index++;
9ad5cbcf 8181 if (outbound_shndx != NULL)
ef10c3ac
L
8182 outbound_shndx_index++;
8183 }
8184
8185 /* Finalize the .strtab section. */
8186 _bfd_elf_strtab_finalize (stt);
8187
8188 /* Swap out the .strtab section. */
8189 for (idx = 0; idx <= symcount; idx++)
8190 {
8191 struct elf_sym_strtab *elfsym = &symstrtab[idx];
8192 if (elfsym->sym.st_name == (unsigned long) -1)
8193 elfsym->sym.st_name = 0;
8194 else
8195 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8196 elfsym->sym.st_name);
8197 bed->s->swap_symbol_out (abfd, &elfsym->sym,
8198 (outbound_syms
8199 + (elfsym->dest_index
8200 * bed->s->sizeof_sym)),
8201 (outbound_shndx
8202 + (elfsym->destshndx_index
8203 * sizeof (Elf_External_Sym_Shndx))));
079e9a2f 8204 }
ef10c3ac 8205 free (symstrtab);
252b5132 8206
079e9a2f 8207 *sttp = stt;
ef10c3ac 8208 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
079e9a2f 8209 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 8210 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
079e9a2f
AM
8211 symstrtab_hdr->sh_addr = 0;
8212 symstrtab_hdr->sh_entsize = 0;
8213 symstrtab_hdr->sh_link = 0;
8214 symstrtab_hdr->sh_info = 0;
8215 symstrtab_hdr->sh_addralign = 1;
252b5132 8216
b34976b6 8217 return TRUE;
252b5132
RH
8218}
8219
8220/* Return the number of bytes required to hold the symtab vector.
8221
8222 Note that we base it on the count plus 1, since we will null terminate
8223 the vector allocated based on this size. However, the ELF symbol table
8224 always has a dummy entry as symbol #0, so it ends up even. */
8225
8226long
217aa764 8227_bfd_elf_get_symtab_upper_bound (bfd *abfd)
252b5132 8228{
3a551c7a 8229 bfd_size_type symcount;
252b5132
RH
8230 long symtab_size;
8231 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
8232
8233 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3a551c7a
AM
8234 if (symcount >= LONG_MAX / sizeof (asymbol *))
8235 {
8236 bfd_set_error (bfd_error_file_too_big);
8237 return -1;
8238 }
b99d1833
AM
8239 symtab_size = (symcount + 1) * (sizeof (asymbol *));
8240 if (symcount > 0)
8241 symtab_size -= sizeof (asymbol *);
252b5132
RH
8242
8243 return symtab_size;
8244}
8245
8246long
217aa764 8247_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
252b5132 8248{
3a551c7a 8249 bfd_size_type symcount;
252b5132
RH
8250 long symtab_size;
8251 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
8252
8253 if (elf_dynsymtab (abfd) == 0)
8254 {
8255 bfd_set_error (bfd_error_invalid_operation);
8256 return -1;
8257 }
8258
8259 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3a551c7a
AM
8260 if (symcount >= LONG_MAX / sizeof (asymbol *))
8261 {
8262 bfd_set_error (bfd_error_file_too_big);
8263 return -1;
8264 }
b99d1833
AM
8265 symtab_size = (symcount + 1) * (sizeof (asymbol *));
8266 if (symcount > 0)
8267 symtab_size -= sizeof (asymbol *);
252b5132
RH
8268
8269 return symtab_size;
8270}
8271
8272long
217aa764
AM
8273_bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
8274 sec_ptr asect)
252b5132
RH
8275{
8276 return (asect->reloc_count + 1) * sizeof (arelent *);
8277}
8278
8279/* Canonicalize the relocs. */
8280
8281long
217aa764
AM
8282_bfd_elf_canonicalize_reloc (bfd *abfd,
8283 sec_ptr section,
8284 arelent **relptr,
8285 asymbol **symbols)
252b5132
RH
8286{
8287 arelent *tblptr;
8288 unsigned int i;
9c5bfbb7 8289 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 8290
b34976b6 8291 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
252b5132
RH
8292 return -1;
8293
8294 tblptr = section->relocation;
8295 for (i = 0; i < section->reloc_count; i++)
8296 *relptr++ = tblptr++;
8297
8298 *relptr = NULL;
8299
8300 return section->reloc_count;
8301}
8302
8303long
6cee3f79 8304_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
252b5132 8305{
9c5bfbb7 8306 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764 8307 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
252b5132
RH
8308
8309 if (symcount >= 0)
8310 bfd_get_symcount (abfd) = symcount;
8311 return symcount;
8312}
8313
8314long
217aa764
AM
8315_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
8316 asymbol **allocation)
252b5132 8317{
9c5bfbb7 8318 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764 8319 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
1f70368c
DJ
8320
8321 if (symcount >= 0)
8322 bfd_get_dynamic_symcount (abfd) = symcount;
8323 return symcount;
252b5132
RH
8324}
8325
8615f3f2
AM
8326/* Return the size required for the dynamic reloc entries. Any loadable
8327 section that was actually installed in the BFD, and has type SHT_REL
8328 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8329 dynamic reloc section. */
252b5132
RH
8330
8331long
217aa764 8332_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
252b5132 8333{
3a551c7a 8334 bfd_size_type count;
252b5132
RH
8335 asection *s;
8336
8337 if (elf_dynsymtab (abfd) == 0)
8338 {
8339 bfd_set_error (bfd_error_invalid_operation);
8340 return -1;
8341 }
8342
3a551c7a 8343 count = 1;
252b5132 8344 for (s = abfd->sections; s != NULL; s = s->next)
266b05cf 8345 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132
RH
8346 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8347 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
3a551c7a
AM
8348 {
8349 count += s->size / elf_section_data (s)->this_hdr.sh_entsize;
8350 if (count > LONG_MAX / sizeof (arelent *))
8351 {
8352 bfd_set_error (bfd_error_file_too_big);
8353 return -1;
8354 }
8355 }
8356 return count * sizeof (arelent *);
252b5132
RH
8357}
8358
8615f3f2
AM
8359/* Canonicalize the dynamic relocation entries. Note that we return the
8360 dynamic relocations as a single block, although they are actually
8361 associated with particular sections; the interface, which was
8362 designed for SunOS style shared libraries, expects that there is only
8363 one set of dynamic relocs. Any loadable section that was actually
8364 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8365 dynamic symbol table, is considered to be a dynamic reloc section. */
252b5132
RH
8366
8367long
217aa764
AM
8368_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
8369 arelent **storage,
8370 asymbol **syms)
252b5132 8371{
217aa764 8372 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
252b5132
RH
8373 asection *s;
8374 long ret;
8375
8376 if (elf_dynsymtab (abfd) == 0)
8377 {
8378 bfd_set_error (bfd_error_invalid_operation);
8379 return -1;
8380 }
8381
8382 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8383 ret = 0;
8384 for (s = abfd->sections; s != NULL; s = s->next)
8385 {
266b05cf 8386 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132
RH
8387 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8388 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8389 {
8390 arelent *p;
8391 long count, i;
8392
b34976b6 8393 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
252b5132 8394 return -1;
eea6121a 8395 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
252b5132
RH
8396 p = s->relocation;
8397 for (i = 0; i < count; i++)
8398 *storage++ = p++;
8399 ret += count;
8400 }
8401 }
8402
8403 *storage = NULL;
8404
8405 return ret;
8406}
8407\f
8408/* Read in the version information. */
8409
b34976b6 8410bfd_boolean
fc0e6df6 8411_bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
252b5132
RH
8412{
8413 bfd_byte *contents = NULL;
fc0e6df6
PB
8414 unsigned int freeidx = 0;
8415
8416 if (elf_dynverref (abfd) != 0)
8417 {
8418 Elf_Internal_Shdr *hdr;
8419 Elf_External_Verneed *everneed;
8420 Elf_Internal_Verneed *iverneed;
8421 unsigned int i;
d0fb9a8d 8422 bfd_byte *contents_end;
fc0e6df6
PB
8423
8424 hdr = &elf_tdata (abfd)->dynverref_hdr;
8425
bd61e135
AM
8426 if (hdr->sh_info == 0
8427 || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
d0fb9a8d 8428 {
601a03ba 8429error_return_bad_verref:
4eca0228 8430 _bfd_error_handler
871b3ab2 8431 (_("%pB: .gnu.version_r invalid entry"), abfd);
601a03ba 8432 bfd_set_error (bfd_error_bad_value);
d0fb9a8d
JJ
8433error_return_verref:
8434 elf_tdata (abfd)->verref = NULL;
8435 elf_tdata (abfd)->cverrefs = 0;
8436 goto error_return;
8437 }
601a03ba
AM
8438
8439 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
8440 if (contents == NULL)
8441 goto error_return_verref;
8442
fc0e6df6
PB
8443 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
8444 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
d0fb9a8d 8445 goto error_return_verref;
fc0e6df6 8446
601a03ba 8447 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
bd61e135 8448 bfd_alloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
601a03ba
AM
8449
8450 if (elf_tdata (abfd)->verref == NULL)
d0fb9a8d
JJ
8451 goto error_return_verref;
8452
8453 BFD_ASSERT (sizeof (Elf_External_Verneed)
8454 == sizeof (Elf_External_Vernaux));
8455 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
fc0e6df6
PB
8456 everneed = (Elf_External_Verneed *) contents;
8457 iverneed = elf_tdata (abfd)->verref;
8458 for (i = 0; i < hdr->sh_info; i++, iverneed++)
8459 {
8460 Elf_External_Vernaux *evernaux;
8461 Elf_Internal_Vernaux *ivernaux;
8462 unsigned int j;
8463
8464 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
8465
8466 iverneed->vn_bfd = abfd;
8467
8468 iverneed->vn_filename =
8469 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8470 iverneed->vn_file);
8471 if (iverneed->vn_filename == NULL)
601a03ba 8472 goto error_return_bad_verref;
fc0e6df6 8473
d0fb9a8d
JJ
8474 if (iverneed->vn_cnt == 0)
8475 iverneed->vn_auxptr = NULL;
8476 else
8477 {
a50b1753 8478 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
07d6d2b8
AM
8479 bfd_alloc2 (abfd, iverneed->vn_cnt,
8480 sizeof (Elf_Internal_Vernaux));
d0fb9a8d
JJ
8481 if (iverneed->vn_auxptr == NULL)
8482 goto error_return_verref;
8483 }
8484
8485 if (iverneed->vn_aux
8486 > (size_t) (contents_end - (bfd_byte *) everneed))
601a03ba 8487 goto error_return_bad_verref;
fc0e6df6
PB
8488
8489 evernaux = ((Elf_External_Vernaux *)
8490 ((bfd_byte *) everneed + iverneed->vn_aux));
8491 ivernaux = iverneed->vn_auxptr;
8492 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
8493 {
8494 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
8495
8496 ivernaux->vna_nodename =
8497 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8498 ivernaux->vna_name);
8499 if (ivernaux->vna_nodename == NULL)
601a03ba 8500 goto error_return_bad_verref;
fc0e6df6 8501
25ff461f
AM
8502 if (ivernaux->vna_other > freeidx)
8503 freeidx = ivernaux->vna_other;
8504
8505 ivernaux->vna_nextptr = NULL;
8506 if (ivernaux->vna_next == 0)
8507 {
8508 iverneed->vn_cnt = j + 1;
8509 break;
8510 }
fc0e6df6
PB
8511 if (j + 1 < iverneed->vn_cnt)
8512 ivernaux->vna_nextptr = ivernaux + 1;
fc0e6df6 8513
d0fb9a8d
JJ
8514 if (ivernaux->vna_next
8515 > (size_t) (contents_end - (bfd_byte *) evernaux))
601a03ba 8516 goto error_return_bad_verref;
d0fb9a8d 8517
fc0e6df6
PB
8518 evernaux = ((Elf_External_Vernaux *)
8519 ((bfd_byte *) evernaux + ivernaux->vna_next));
fc0e6df6
PB
8520 }
8521
25ff461f
AM
8522 iverneed->vn_nextref = NULL;
8523 if (iverneed->vn_next == 0)
8524 break;
fc0e6df6
PB
8525 if (i + 1 < hdr->sh_info)
8526 iverneed->vn_nextref = iverneed + 1;
fc0e6df6 8527
d0fb9a8d
JJ
8528 if (iverneed->vn_next
8529 > (size_t) (contents_end - (bfd_byte *) everneed))
601a03ba 8530 goto error_return_bad_verref;
d0fb9a8d 8531
fc0e6df6
PB
8532 everneed = ((Elf_External_Verneed *)
8533 ((bfd_byte *) everneed + iverneed->vn_next));
8534 }
25ff461f 8535 elf_tdata (abfd)->cverrefs = i;
fc0e6df6
PB
8536
8537 free (contents);
8538 contents = NULL;
8539 }
252b5132
RH
8540
8541 if (elf_dynverdef (abfd) != 0)
8542 {
8543 Elf_Internal_Shdr *hdr;
8544 Elf_External_Verdef *everdef;
8545 Elf_Internal_Verdef *iverdef;
f631889e
UD
8546 Elf_Internal_Verdef *iverdefarr;
8547 Elf_Internal_Verdef iverdefmem;
252b5132 8548 unsigned int i;
062e2358 8549 unsigned int maxidx;
d0fb9a8d 8550 bfd_byte *contents_end_def, *contents_end_aux;
252b5132
RH
8551
8552 hdr = &elf_tdata (abfd)->dynverdef_hdr;
8553
601a03ba
AM
8554 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
8555 {
8556 error_return_bad_verdef:
4eca0228 8557 _bfd_error_handler
871b3ab2 8558 (_("%pB: .gnu.version_d invalid entry"), abfd);
601a03ba
AM
8559 bfd_set_error (bfd_error_bad_value);
8560 error_return_verdef:
8561 elf_tdata (abfd)->verdef = NULL;
8562 elf_tdata (abfd)->cverdefs = 0;
8563 goto error_return;
8564 }
8565
a50b1753 8566 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
252b5132 8567 if (contents == NULL)
601a03ba 8568 goto error_return_verdef;
252b5132 8569 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
217aa764 8570 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
601a03ba 8571 goto error_return_verdef;
d0fb9a8d
JJ
8572
8573 BFD_ASSERT (sizeof (Elf_External_Verdef)
8574 >= sizeof (Elf_External_Verdaux));
8575 contents_end_def = contents + hdr->sh_size
8576 - sizeof (Elf_External_Verdef);
8577 contents_end_aux = contents + hdr->sh_size
8578 - sizeof (Elf_External_Verdaux);
8579
f631889e
UD
8580 /* We know the number of entries in the section but not the maximum
8581 index. Therefore we have to run through all entries and find
8582 the maximum. */
252b5132 8583 everdef = (Elf_External_Verdef *) contents;
f631889e
UD
8584 maxidx = 0;
8585 for (i = 0; i < hdr->sh_info; ++i)
8586 {
8587 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8588
601a03ba
AM
8589 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
8590 goto error_return_bad_verdef;
062e2358
AM
8591 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
8592 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
f631889e 8593
25ff461f
AM
8594 if (iverdefmem.vd_next == 0)
8595 break;
8596
d0fb9a8d
JJ
8597 if (iverdefmem.vd_next
8598 > (size_t) (contents_end_def - (bfd_byte *) everdef))
601a03ba 8599 goto error_return_bad_verdef;
d0fb9a8d 8600
f631889e
UD
8601 everdef = ((Elf_External_Verdef *)
8602 ((bfd_byte *) everdef + iverdefmem.vd_next));
8603 }
8604
fc0e6df6
PB
8605 if (default_imported_symver)
8606 {
8607 if (freeidx > maxidx)
8608 maxidx = ++freeidx;
8609 else
8610 freeidx = ++maxidx;
8611 }
201159ec 8612
601a03ba
AM
8613 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
8614 bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
f631889e 8615 if (elf_tdata (abfd)->verdef == NULL)
601a03ba 8616 goto error_return_verdef;
f631889e
UD
8617
8618 elf_tdata (abfd)->cverdefs = maxidx;
8619
8620 everdef = (Elf_External_Verdef *) contents;
8621 iverdefarr = elf_tdata (abfd)->verdef;
8622 for (i = 0; i < hdr->sh_info; i++)
252b5132
RH
8623 {
8624 Elf_External_Verdaux *everdaux;
8625 Elf_Internal_Verdaux *iverdaux;
8626 unsigned int j;
8627
f631889e
UD
8628 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8629
d0fb9a8d 8630 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
601a03ba 8631 goto error_return_bad_verdef;
d0fb9a8d 8632
f631889e 8633 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
595bce75 8634 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
252b5132
RH
8635
8636 iverdef->vd_bfd = abfd;
8637
d0fb9a8d
JJ
8638 if (iverdef->vd_cnt == 0)
8639 iverdef->vd_auxptr = NULL;
8640 else
8641 {
a50b1753 8642 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
07d6d2b8
AM
8643 bfd_alloc2 (abfd, iverdef->vd_cnt,
8644 sizeof (Elf_Internal_Verdaux));
d0fb9a8d
JJ
8645 if (iverdef->vd_auxptr == NULL)
8646 goto error_return_verdef;
8647 }
8648
8649 if (iverdef->vd_aux
8650 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
601a03ba 8651 goto error_return_bad_verdef;
252b5132
RH
8652
8653 everdaux = ((Elf_External_Verdaux *)
8654 ((bfd_byte *) everdef + iverdef->vd_aux));
8655 iverdaux = iverdef->vd_auxptr;
8656 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
8657 {
8658 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
8659
8660 iverdaux->vda_nodename =
8661 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8662 iverdaux->vda_name);
8663 if (iverdaux->vda_nodename == NULL)
601a03ba 8664 goto error_return_bad_verdef;
252b5132 8665
25ff461f
AM
8666 iverdaux->vda_nextptr = NULL;
8667 if (iverdaux->vda_next == 0)
8668 {
8669 iverdef->vd_cnt = j + 1;
8670 break;
8671 }
252b5132
RH
8672 if (j + 1 < iverdef->vd_cnt)
8673 iverdaux->vda_nextptr = iverdaux + 1;
252b5132 8674
d0fb9a8d
JJ
8675 if (iverdaux->vda_next
8676 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
601a03ba 8677 goto error_return_bad_verdef;
d0fb9a8d 8678
252b5132
RH
8679 everdaux = ((Elf_External_Verdaux *)
8680 ((bfd_byte *) everdaux + iverdaux->vda_next));
8681 }
8682
595bce75 8683 iverdef->vd_nodename = NULL;
d0fb9a8d
JJ
8684 if (iverdef->vd_cnt)
8685 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
252b5132 8686
25ff461f
AM
8687 iverdef->vd_nextdef = NULL;
8688 if (iverdef->vd_next == 0)
8689 break;
d0fb9a8d 8690 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
252b5132 8691 iverdef->vd_nextdef = iverdef + 1;
252b5132
RH
8692
8693 everdef = ((Elf_External_Verdef *)
8694 ((bfd_byte *) everdef + iverdef->vd_next));
8695 }
8696
8697 free (contents);
8698 contents = NULL;
8699 }
fc0e6df6 8700 else if (default_imported_symver)
252b5132 8701 {
fc0e6df6
PB
8702 if (freeidx < 3)
8703 freeidx = 3;
8704 else
8705 freeidx++;
252b5132 8706
a50b1753 8707 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
07d6d2b8 8708 bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
fc0e6df6 8709 if (elf_tdata (abfd)->verdef == NULL)
252b5132
RH
8710 goto error_return;
8711
fc0e6df6
PB
8712 elf_tdata (abfd)->cverdefs = freeidx;
8713 }
252b5132 8714
fc0e6df6
PB
8715 /* Create a default version based on the soname. */
8716 if (default_imported_symver)
8717 {
8718 Elf_Internal_Verdef *iverdef;
8719 Elf_Internal_Verdaux *iverdaux;
252b5132 8720
5bb3703f 8721 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
252b5132 8722
fc0e6df6
PB
8723 iverdef->vd_version = VER_DEF_CURRENT;
8724 iverdef->vd_flags = 0;
8725 iverdef->vd_ndx = freeidx;
8726 iverdef->vd_cnt = 1;
252b5132 8727
fc0e6df6 8728 iverdef->vd_bfd = abfd;
252b5132 8729
fc0e6df6
PB
8730 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
8731 if (iverdef->vd_nodename == NULL)
d0fb9a8d 8732 goto error_return_verdef;
fc0e6df6 8733 iverdef->vd_nextdef = NULL;
601a03ba
AM
8734 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
8735 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
d0fb9a8d
JJ
8736 if (iverdef->vd_auxptr == NULL)
8737 goto error_return_verdef;
252b5132 8738
fc0e6df6
PB
8739 iverdaux = iverdef->vd_auxptr;
8740 iverdaux->vda_nodename = iverdef->vd_nodename;
252b5132
RH
8741 }
8742
b34976b6 8743 return TRUE;
252b5132
RH
8744
8745 error_return:
5ed6aba4 8746 if (contents != NULL)
252b5132 8747 free (contents);
b34976b6 8748 return FALSE;
252b5132
RH
8749}
8750\f
8751asymbol *
217aa764 8752_bfd_elf_make_empty_symbol (bfd *abfd)
252b5132
RH
8753{
8754 elf_symbol_type *newsym;
8755
201159ec 8756 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym);
252b5132
RH
8757 if (!newsym)
8758 return NULL;
201159ec
NC
8759 newsym->symbol.the_bfd = abfd;
8760 return &newsym->symbol;
252b5132
RH
8761}
8762
8763void
217aa764
AM
8764_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
8765 asymbol *symbol,
8766 symbol_info *ret)
252b5132
RH
8767{
8768 bfd_symbol_info (symbol, ret);
8769}
8770
8771/* Return whether a symbol name implies a local symbol. Most targets
8772 use this function for the is_local_label_name entry point, but some
8773 override it. */
8774
b34976b6 8775bfd_boolean
217aa764
AM
8776_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
8777 const char *name)
252b5132
RH
8778{
8779 /* Normal local symbols start with ``.L''. */
8780 if (name[0] == '.' && name[1] == 'L')
b34976b6 8781 return TRUE;
252b5132
RH
8782
8783 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8784 DWARF debugging symbols starting with ``..''. */
8785 if (name[0] == '.' && name[1] == '.')
b34976b6 8786 return TRUE;
252b5132
RH
8787
8788 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8789 emitting DWARF debugging output. I suspect this is actually a
8790 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8791 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8792 underscore to be emitted on some ELF targets). For ease of use,
8793 we treat such symbols as local. */
8794 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
b34976b6 8795 return TRUE;
252b5132 8796
b1fa9dd6
NC
8797 /* Treat assembler generated fake symbols, dollar local labels and
8798 forward-backward labels (aka local labels) as locals.
8799 These labels have the form:
8800
07d6d2b8 8801 L0^A.* (fake symbols)
b1fa9dd6
NC
8802
8803 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
8804
8805 Versions which start with .L will have already been matched above,
8806 so we only need to match the rest. */
8807 if (name[0] == 'L' && ISDIGIT (name[1]))
8808 {
8809 bfd_boolean ret = FALSE;
8810 const char * p;
8811 char c;
8812
8813 for (p = name + 2; (c = *p); p++)
8814 {
8815 if (c == 1 || c == 2)
8816 {
8817 if (c == 1 && p == name + 2)
8818 /* A fake symbol. */
8819 return TRUE;
8820
8821 /* FIXME: We are being paranoid here and treating symbols like
8822 L0^Bfoo as if there were non-local, on the grounds that the
8823 assembler will never generate them. But can any symbol
8824 containing an ASCII value in the range 1-31 ever be anything
8825 other than some kind of local ? */
8826 ret = TRUE;
8827 }
8828
8829 if (! ISDIGIT (c))
8830 {
8831 ret = FALSE;
8832 break;
8833 }
8834 }
8835 return ret;
8836 }
ffa54770 8837
b34976b6 8838 return FALSE;
252b5132
RH
8839}
8840
8841alent *
217aa764
AM
8842_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
8843 asymbol *symbol ATTRIBUTE_UNUSED)
252b5132
RH
8844{
8845 abort ();
8846 return NULL;
8847}
8848
b34976b6 8849bfd_boolean
217aa764
AM
8850_bfd_elf_set_arch_mach (bfd *abfd,
8851 enum bfd_architecture arch,
8852 unsigned long machine)
252b5132
RH
8853{
8854 /* If this isn't the right architecture for this backend, and this
8855 isn't the generic backend, fail. */
8856 if (arch != get_elf_backend_data (abfd)->arch
8857 && arch != bfd_arch_unknown
8858 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
b34976b6 8859 return FALSE;
252b5132
RH
8860
8861 return bfd_default_set_arch_mach (abfd, arch, machine);
8862}
8863
d1fad7c6
NC
8864/* Find the nearest line to a particular section and offset,
8865 for error reporting. */
8866
b34976b6 8867bfd_boolean
217aa764 8868_bfd_elf_find_nearest_line (bfd *abfd,
217aa764 8869 asymbol **symbols,
fb167eb2 8870 asection *section,
217aa764
AM
8871 bfd_vma offset,
8872 const char **filename_ptr,
8873 const char **functionname_ptr,
fb167eb2
AM
8874 unsigned int *line_ptr,
8875 unsigned int *discriminator_ptr)
d1fad7c6 8876{
b34976b6 8877 bfd_boolean found;
d1fad7c6 8878
fb167eb2 8879 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
4e8a9624 8880 filename_ptr, functionname_ptr,
fb167eb2
AM
8881 line_ptr, discriminator_ptr,
8882 dwarf_debug_sections, 0,
e00e8198
AM
8883 &elf_tdata (abfd)->dwarf2_find_line_info)
8884 || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
8885 filename_ptr, functionname_ptr,
8886 line_ptr))
d1fad7c6
NC
8887 {
8888 if (!*functionname_ptr)
e00e8198
AM
8889 _bfd_elf_find_function (abfd, symbols, section, offset,
8890 *filename_ptr ? NULL : filename_ptr,
8891 functionname_ptr);
b34976b6 8892 return TRUE;
d1fad7c6
NC
8893 }
8894
8895 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4e8a9624
AM
8896 &found, filename_ptr,
8897 functionname_ptr, line_ptr,
8898 &elf_tdata (abfd)->line_info))
b34976b6 8899 return FALSE;
dc43ada5 8900 if (found && (*functionname_ptr || *line_ptr))
b34976b6 8901 return TRUE;
d1fad7c6
NC
8902
8903 if (symbols == NULL)
b34976b6 8904 return FALSE;
d1fad7c6 8905
e00e8198
AM
8906 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
8907 filename_ptr, functionname_ptr))
b34976b6 8908 return FALSE;
d1fad7c6 8909
252b5132 8910 *line_ptr = 0;
b34976b6 8911 return TRUE;
252b5132
RH
8912}
8913
5420f73d
L
8914/* Find the line for a symbol. */
8915
8916bfd_boolean
8917_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
8918 const char **filename_ptr, unsigned int *line_ptr)
9b8d1a36 8919{
fb167eb2
AM
8920 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
8921 filename_ptr, NULL, line_ptr, NULL,
8922 dwarf_debug_sections, 0,
8923 &elf_tdata (abfd)->dwarf2_find_line_info);
5420f73d
L
8924}
8925
4ab527b0
FF
8926/* After a call to bfd_find_nearest_line, successive calls to
8927 bfd_find_inliner_info can be used to get source information about
8928 each level of function inlining that terminated at the address
8929 passed to bfd_find_nearest_line. Currently this is only supported
8930 for DWARF2 with appropriate DWARF3 extensions. */
8931
8932bfd_boolean
8933_bfd_elf_find_inliner_info (bfd *abfd,
8934 const char **filename_ptr,
8935 const char **functionname_ptr,
8936 unsigned int *line_ptr)
8937{
8938 bfd_boolean found;
8939 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
8940 functionname_ptr, line_ptr,
8941 & elf_tdata (abfd)->dwarf2_find_line_info);
8942 return found;
8943}
8944
252b5132 8945int
a6b96beb 8946_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
252b5132 8947{
8ded5a0f
AM
8948 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8949 int ret = bed->s->sizeof_ehdr;
252b5132 8950
0e1862bb 8951 if (!bfd_link_relocatable (info))
8ded5a0f 8952 {
12bd6957 8953 bfd_size_type phdr_size = elf_program_header_size (abfd);
8ded5a0f 8954
62d7a5f6
AM
8955 if (phdr_size == (bfd_size_type) -1)
8956 {
8957 struct elf_segment_map *m;
8958
8959 phdr_size = 0;
12bd6957 8960 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
62d7a5f6 8961 phdr_size += bed->s->sizeof_phdr;
8ded5a0f 8962
62d7a5f6
AM
8963 if (phdr_size == 0)
8964 phdr_size = get_program_header_size (abfd, info);
8965 }
8ded5a0f 8966
12bd6957 8967 elf_program_header_size (abfd) = phdr_size;
8ded5a0f
AM
8968 ret += phdr_size;
8969 }
8970
252b5132
RH
8971 return ret;
8972}
8973
b34976b6 8974bfd_boolean
217aa764
AM
8975_bfd_elf_set_section_contents (bfd *abfd,
8976 sec_ptr section,
0f867abe 8977 const void *location,
217aa764
AM
8978 file_ptr offset,
8979 bfd_size_type count)
252b5132
RH
8980{
8981 Elf_Internal_Shdr *hdr;
1b6aeedb 8982 file_ptr pos;
252b5132
RH
8983
8984 if (! abfd->output_has_begun
217aa764 8985 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
b34976b6 8986 return FALSE;
252b5132 8987
0ce398f1
L
8988 if (!count)
8989 return TRUE;
8990
252b5132 8991 hdr = &elf_section_data (section)->this_hdr;
0ce398f1
L
8992 if (hdr->sh_offset == (file_ptr) -1)
8993 {
8994 /* We must compress this section. Write output to the buffer. */
8995 unsigned char *contents = hdr->contents;
8996 if ((offset + count) > hdr->sh_size
8997 || (section->flags & SEC_ELF_COMPRESS) == 0
8998 || contents == NULL)
8999 abort ();
9000 memcpy (contents + offset, location, count);
9001 return TRUE;
9002 }
dc810e39
AM
9003 pos = hdr->sh_offset + offset;
9004 if (bfd_seek (abfd, pos, SEEK_SET) != 0
9005 || bfd_bwrite (location, count, abfd) != count)
b34976b6 9006 return FALSE;
252b5132 9007
b34976b6 9008 return TRUE;
252b5132
RH
9009}
9010
f3185997 9011bfd_boolean
217aa764
AM
9012_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
9013 arelent *cache_ptr ATTRIBUTE_UNUSED,
9014 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
252b5132
RH
9015{
9016 abort ();
f3185997 9017 return FALSE;
252b5132
RH
9018}
9019
252b5132
RH
9020/* Try to convert a non-ELF reloc into an ELF one. */
9021
b34976b6 9022bfd_boolean
217aa764 9023_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
252b5132 9024{
c044fabd 9025 /* Check whether we really have an ELF howto. */
252b5132
RH
9026
9027 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
9028 {
9029 bfd_reloc_code_real_type code;
9030 reloc_howto_type *howto;
9031
9032 /* Alien reloc: Try to determine its type to replace it with an
c044fabd 9033 equivalent ELF reloc. */
252b5132
RH
9034
9035 if (areloc->howto->pc_relative)
9036 {
9037 switch (areloc->howto->bitsize)
9038 {
9039 case 8:
9040 code = BFD_RELOC_8_PCREL;
9041 break;
9042 case 12:
9043 code = BFD_RELOC_12_PCREL;
9044 break;
9045 case 16:
9046 code = BFD_RELOC_16_PCREL;
9047 break;
9048 case 24:
9049 code = BFD_RELOC_24_PCREL;
9050 break;
9051 case 32:
9052 code = BFD_RELOC_32_PCREL;
9053 break;
9054 case 64:
9055 code = BFD_RELOC_64_PCREL;
9056 break;
9057 default:
9058 goto fail;
9059 }
9060
9061 howto = bfd_reloc_type_lookup (abfd, code);
9062
9063 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
9064 {
9065 if (howto->pcrel_offset)
9066 areloc->addend += areloc->address;
9067 else
9068 areloc->addend -= areloc->address; /* addend is unsigned!! */
9069 }
9070 }
9071 else
9072 {
9073 switch (areloc->howto->bitsize)
9074 {
9075 case 8:
9076 code = BFD_RELOC_8;
9077 break;
9078 case 14:
9079 code = BFD_RELOC_14;
9080 break;
9081 case 16:
9082 code = BFD_RELOC_16;
9083 break;
9084 case 26:
9085 code = BFD_RELOC_26;
9086 break;
9087 case 32:
9088 code = BFD_RELOC_32;
9089 break;
9090 case 64:
9091 code = BFD_RELOC_64;
9092 break;
9093 default:
9094 goto fail;
9095 }
9096
9097 howto = bfd_reloc_type_lookup (abfd, code);
9098 }
9099
9100 if (howto)
9101 areloc->howto = howto;
9102 else
9103 goto fail;
9104 }
9105
b34976b6 9106 return TRUE;
252b5132
RH
9107
9108 fail:
0aa13fee
AM
9109 /* xgettext:c-format */
9110 _bfd_error_handler (_("%pB: %s unsupported"),
9111 abfd, areloc->howto->name);
252b5132 9112 bfd_set_error (bfd_error_bad_value);
b34976b6 9113 return FALSE;
252b5132
RH
9114}
9115
b34976b6 9116bfd_boolean
217aa764 9117_bfd_elf_close_and_cleanup (bfd *abfd)
252b5132 9118{
d9071b0c
TG
9119 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9120 if (bfd_get_format (abfd) == bfd_object && tdata != NULL)
252b5132 9121 {
c0355132 9122 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
2b0f7ef9 9123 _bfd_elf_strtab_free (elf_shstrtab (abfd));
d9071b0c 9124 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
252b5132
RH
9125 }
9126
9127 return _bfd_generic_close_and_cleanup (abfd);
9128}
9129
9130/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9131 in the relocation's offset. Thus we cannot allow any sort of sanity
9132 range-checking to interfere. There is nothing else to do in processing
9133 this reloc. */
9134
9135bfd_reloc_status_type
217aa764
AM
9136_bfd_elf_rel_vtable_reloc_fn
9137 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
fc0a2244 9138 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
217aa764
AM
9139 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
9140 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
252b5132
RH
9141{
9142 return bfd_reloc_ok;
9143}
252b5132
RH
9144\f
9145/* Elf core file support. Much of this only works on native
9146 toolchains, since we rely on knowing the
9147 machine-dependent procfs structure in order to pick
c044fabd 9148 out details about the corefile. */
252b5132
RH
9149
9150#ifdef HAVE_SYS_PROCFS_H
16231b7b
DG
9151/* Needed for new procfs interface on sparc-solaris. */
9152# define _STRUCTURED_PROC 1
252b5132
RH
9153# include <sys/procfs.h>
9154#endif
9155
261b8d08
PA
9156/* Return a PID that identifies a "thread" for threaded cores, or the
9157 PID of the main process for non-threaded cores. */
252b5132
RH
9158
9159static int
217aa764 9160elfcore_make_pid (bfd *abfd)
252b5132 9161{
261b8d08
PA
9162 int pid;
9163
228e534f 9164 pid = elf_tdata (abfd)->core->lwpid;
261b8d08 9165 if (pid == 0)
228e534f 9166 pid = elf_tdata (abfd)->core->pid;
261b8d08
PA
9167
9168 return pid;
252b5132
RH
9169}
9170
252b5132
RH
9171/* If there isn't a section called NAME, make one, using
9172 data from SECT. Note, this function will generate a
9173 reference to NAME, so you shouldn't deallocate or
c044fabd 9174 overwrite it. */
252b5132 9175
b34976b6 9176static bfd_boolean
217aa764 9177elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
252b5132 9178{
c044fabd 9179 asection *sect2;
252b5132
RH
9180
9181 if (bfd_get_section_by_name (abfd, name) != NULL)
b34976b6 9182 return TRUE;
252b5132 9183
117ed4f8 9184 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
252b5132 9185 if (sect2 == NULL)
b34976b6 9186 return FALSE;
252b5132 9187
eea6121a 9188 sect2->size = sect->size;
252b5132 9189 sect2->filepos = sect->filepos;
252b5132 9190 sect2->alignment_power = sect->alignment_power;
b34976b6 9191 return TRUE;
252b5132
RH
9192}
9193
bb0082d6
AM
9194/* Create a pseudosection containing SIZE bytes at FILEPOS. This
9195 actually creates up to two pseudosections:
9196 - For the single-threaded case, a section named NAME, unless
9197 such a section already exists.
9198 - For the multi-threaded case, a section named "NAME/PID", where
9199 PID is elfcore_make_pid (abfd).
24d3e51b 9200 Both pseudosections have identical contents. */
b34976b6 9201bfd_boolean
217aa764
AM
9202_bfd_elfcore_make_pseudosection (bfd *abfd,
9203 char *name,
9204 size_t size,
9205 ufile_ptr filepos)
bb0082d6
AM
9206{
9207 char buf[100];
9208 char *threaded_name;
d4c88bbb 9209 size_t len;
bb0082d6
AM
9210 asection *sect;
9211
9212 /* Build the section name. */
9213
9214 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
d4c88bbb 9215 len = strlen (buf) + 1;
a50b1753 9216 threaded_name = (char *) bfd_alloc (abfd, len);
bb0082d6 9217 if (threaded_name == NULL)
b34976b6 9218 return FALSE;
d4c88bbb 9219 memcpy (threaded_name, buf, len);
bb0082d6 9220
117ed4f8
AM
9221 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
9222 SEC_HAS_CONTENTS);
bb0082d6 9223 if (sect == NULL)
b34976b6 9224 return FALSE;
eea6121a 9225 sect->size = size;
bb0082d6 9226 sect->filepos = filepos;
bb0082d6
AM
9227 sect->alignment_power = 2;
9228
936e320b 9229 return elfcore_maybe_make_sect (abfd, name, sect);
bb0082d6
AM
9230}
9231
252b5132 9232/* prstatus_t exists on:
4a938328 9233 solaris 2.5+
252b5132
RH
9234 linux 2.[01] + glibc
9235 unixware 4.2
9236*/
9237
9238#if defined (HAVE_PRSTATUS_T)
a7b97311 9239
b34976b6 9240static bfd_boolean
217aa764 9241elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 9242{
eea6121a 9243 size_t size;
7ee38065 9244 int offset;
252b5132 9245
4a938328
MS
9246 if (note->descsz == sizeof (prstatus_t))
9247 {
9248 prstatus_t prstat;
252b5132 9249
eea6121a 9250 size = sizeof (prstat.pr_reg);
7ee38065 9251 offset = offsetof (prstatus_t, pr_reg);
4a938328 9252 memcpy (&prstat, note->descdata, sizeof (prstat));
252b5132 9253
fa49d224
NC
9254 /* Do not overwrite the core signal if it
9255 has already been set by another thread. */
228e534f
AM
9256 if (elf_tdata (abfd)->core->signal == 0)
9257 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9258 if (elf_tdata (abfd)->core->pid == 0)
9259 elf_tdata (abfd)->core->pid = prstat.pr_pid;
252b5132 9260
4a938328
MS
9261 /* pr_who exists on:
9262 solaris 2.5+
9263 unixware 4.2
9264 pr_who doesn't exist on:
9265 linux 2.[01]
9266 */
252b5132 9267#if defined (HAVE_PRSTATUS_T_PR_WHO)
228e534f 9268 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
261b8d08 9269#else
228e534f 9270 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
252b5132 9271#endif
4a938328 9272 }
7ee38065 9273#if defined (HAVE_PRSTATUS32_T)
4a938328
MS
9274 else if (note->descsz == sizeof (prstatus32_t))
9275 {
9276 /* 64-bit host, 32-bit corefile */
9277 prstatus32_t prstat;
9278
eea6121a 9279 size = sizeof (prstat.pr_reg);
7ee38065 9280 offset = offsetof (prstatus32_t, pr_reg);
4a938328
MS
9281 memcpy (&prstat, note->descdata, sizeof (prstat));
9282
fa49d224
NC
9283 /* Do not overwrite the core signal if it
9284 has already been set by another thread. */
228e534f
AM
9285 if (elf_tdata (abfd)->core->signal == 0)
9286 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9287 if (elf_tdata (abfd)->core->pid == 0)
9288 elf_tdata (abfd)->core->pid = prstat.pr_pid;
4a938328
MS
9289
9290 /* pr_who exists on:
9291 solaris 2.5+
9292 unixware 4.2
9293 pr_who doesn't exist on:
9294 linux 2.[01]
9295 */
7ee38065 9296#if defined (HAVE_PRSTATUS32_T_PR_WHO)
228e534f 9297 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
261b8d08 9298#else
228e534f 9299 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
4a938328
MS
9300#endif
9301 }
7ee38065 9302#endif /* HAVE_PRSTATUS32_T */
4a938328
MS
9303 else
9304 {
9305 /* Fail - we don't know how to handle any other
9306 note size (ie. data object type). */
b34976b6 9307 return TRUE;
4a938328 9308 }
252b5132 9309
bb0082d6 9310 /* Make a ".reg/999" section and a ".reg" section. */
936e320b 9311 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 9312 size, note->descpos + offset);
252b5132
RH
9313}
9314#endif /* defined (HAVE_PRSTATUS_T) */
9315
bb0082d6 9316/* Create a pseudosection containing the exact contents of NOTE. */
b34976b6 9317static bfd_boolean
217aa764
AM
9318elfcore_make_note_pseudosection (bfd *abfd,
9319 char *name,
9320 Elf_Internal_Note *note)
252b5132 9321{
936e320b
AM
9322 return _bfd_elfcore_make_pseudosection (abfd, name,
9323 note->descsz, note->descpos);
252b5132
RH
9324}
9325
ff08c6bb
JB
9326/* There isn't a consistent prfpregset_t across platforms,
9327 but it doesn't matter, because we don't have to pick this
c044fabd
KH
9328 data structure apart. */
9329
b34976b6 9330static bfd_boolean
217aa764 9331elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
9332{
9333 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9334}
9335
ff08c6bb 9336/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
971d4640 9337 type of NT_PRXFPREG. Just include the whole note's contents
ff08c6bb 9338 literally. */
c044fabd 9339
b34976b6 9340static bfd_boolean
217aa764 9341elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
9342{
9343 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9344}
9345
4339cae0
L
9346/* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9347 with a note type of NT_X86_XSTATE. Just include the whole note's
9348 contents literally. */
9349
9350static bfd_boolean
9351elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
9352{
9353 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
9354}
9355
97753bd5
AM
9356static bfd_boolean
9357elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
9358{
9359 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
9360}
9361
89eeb0bc
LM
9362static bfd_boolean
9363elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
9364{
9365 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
9366}
97753bd5 9367
cb2366c1
EBM
9368static bfd_boolean
9369elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
9370{
9371 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
9372}
9373
9374static bfd_boolean
9375elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
9376{
9377 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
9378}
9379
9380static bfd_boolean
9381elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
9382{
9383 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
9384}
9385
9386static bfd_boolean
9387elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
9388{
9389 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
9390}
9391
9392static bfd_boolean
9393elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
9394{
9395 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
9396}
9397
9398static bfd_boolean
9399elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
9400{
9401 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
9402}
9403
9404static bfd_boolean
9405elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
9406{
9407 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
9408}
9409
9410static bfd_boolean
9411elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
9412{
9413 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
9414}
9415
9416static bfd_boolean
9417elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
9418{
9419 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
9420}
9421
9422static bfd_boolean
9423elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
9424{
9425 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
9426}
9427
9428static bfd_boolean
9429elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
9430{
9431 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
9432}
9433
9434static bfd_boolean
9435elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
9436{
9437 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
9438}
9439
9440static bfd_boolean
9441elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
9442{
9443 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
9444}
9445
0675e188
UW
9446static bfd_boolean
9447elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
9448{
9449 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
9450}
9451
d7eeb400
MS
9452static bfd_boolean
9453elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
9454{
9455 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
9456}
9457
9458static bfd_boolean
9459elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
9460{
9461 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
9462}
9463
9464static bfd_boolean
9465elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
9466{
9467 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
9468}
9469
9470static bfd_boolean
9471elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
9472{
9473 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
9474}
9475
9476static bfd_boolean
9477elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
9478{
9479 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
9480}
9481
355b81d9
UW
9482static bfd_boolean
9483elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
9484{
9485 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
9486}
9487
9488static bfd_boolean
9489elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
9490{
9491 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
9492}
9493
abb3f6cc
NC
9494static bfd_boolean
9495elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
9496{
9497 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
9498}
9499
4ef9f41a
AA
9500static bfd_boolean
9501elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
9502{
9503 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
9504}
9505
9506static bfd_boolean
9507elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
9508{
9509 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
9510}
9511
88ab90e8
AA
9512static bfd_boolean
9513elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
9514{
9515 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
9516}
9517
9518static bfd_boolean
9519elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
9520{
9521 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
9522}
9523
faa9a424
UW
9524static bfd_boolean
9525elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
9526{
9527 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
9528}
9529
652451f8
YZ
9530static bfd_boolean
9531elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
9532{
9533 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
9534}
9535
9536static bfd_boolean
9537elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
9538{
9539 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
9540}
9541
9542static bfd_boolean
9543elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
9544{
9545 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
9546}
9547
ad1cc4e4
AH
9548static bfd_boolean
9549elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
9550{
9551 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
9552}
9553
e6c3b5bf
AH
9554static bfd_boolean
9555elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
9556{
9557 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
9558}
9559
252b5132 9560#if defined (HAVE_PRPSINFO_T)
4a938328 9561typedef prpsinfo_t elfcore_psinfo_t;
7ee38065 9562#if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
9563typedef prpsinfo32_t elfcore_psinfo32_t;
9564#endif
252b5132
RH
9565#endif
9566
9567#if defined (HAVE_PSINFO_T)
4a938328 9568typedef psinfo_t elfcore_psinfo_t;
7ee38065 9569#if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
9570typedef psinfo32_t elfcore_psinfo32_t;
9571#endif
252b5132
RH
9572#endif
9573
252b5132
RH
9574/* return a malloc'ed copy of a string at START which is at
9575 most MAX bytes long, possibly without a terminating '\0'.
c044fabd 9576 the copy will always have a terminating '\0'. */
252b5132 9577
936e320b 9578char *
217aa764 9579_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
252b5132 9580{
dc810e39 9581 char *dups;
a50b1753 9582 char *end = (char *) memchr (start, '\0', max);
dc810e39 9583 size_t len;
252b5132
RH
9584
9585 if (end == NULL)
9586 len = max;
9587 else
9588 len = end - start;
9589
a50b1753 9590 dups = (char *) bfd_alloc (abfd, len + 1);
dc810e39 9591 if (dups == NULL)
252b5132
RH
9592 return NULL;
9593
dc810e39
AM
9594 memcpy (dups, start, len);
9595 dups[len] = '\0';
252b5132 9596
dc810e39 9597 return dups;
252b5132
RH
9598}
9599
bb0082d6 9600#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
b34976b6 9601static bfd_boolean
217aa764 9602elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
252b5132 9603{
4a938328
MS
9604 if (note->descsz == sizeof (elfcore_psinfo_t))
9605 {
9606 elfcore_psinfo_t psinfo;
252b5132 9607
7ee38065 9608 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 9609
335e41d4 9610#if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
228e534f 9611 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
335e41d4 9612#endif
228e534f 9613 elf_tdata (abfd)->core->program
936e320b
AM
9614 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9615 sizeof (psinfo.pr_fname));
252b5132 9616
228e534f 9617 elf_tdata (abfd)->core->command
936e320b
AM
9618 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9619 sizeof (psinfo.pr_psargs));
4a938328 9620 }
7ee38065 9621#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
4a938328
MS
9622 else if (note->descsz == sizeof (elfcore_psinfo32_t))
9623 {
9624 /* 64-bit host, 32-bit corefile */
9625 elfcore_psinfo32_t psinfo;
9626
7ee38065 9627 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 9628
335e41d4 9629#if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
228e534f 9630 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
335e41d4 9631#endif
228e534f 9632 elf_tdata (abfd)->core->program
936e320b
AM
9633 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9634 sizeof (psinfo.pr_fname));
4a938328 9635
228e534f 9636 elf_tdata (abfd)->core->command
936e320b
AM
9637 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9638 sizeof (psinfo.pr_psargs));
4a938328
MS
9639 }
9640#endif
9641
9642 else
9643 {
9644 /* Fail - we don't know how to handle any other
9645 note size (ie. data object type). */
b34976b6 9646 return TRUE;
4a938328 9647 }
252b5132
RH
9648
9649 /* Note that for some reason, a spurious space is tacked
9650 onto the end of the args in some (at least one anyway)
c044fabd 9651 implementations, so strip it off if it exists. */
252b5132
RH
9652
9653 {
228e534f 9654 char *command = elf_tdata (abfd)->core->command;
252b5132
RH
9655 int n = strlen (command);
9656
9657 if (0 < n && command[n - 1] == ' ')
9658 command[n - 1] = '\0';
9659 }
9660
b34976b6 9661 return TRUE;
252b5132
RH
9662}
9663#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9664
252b5132 9665#if defined (HAVE_PSTATUS_T)
b34976b6 9666static bfd_boolean
217aa764 9667elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 9668{
f572a39d
AM
9669 if (note->descsz == sizeof (pstatus_t)
9670#if defined (HAVE_PXSTATUS_T)
9671 || note->descsz == sizeof (pxstatus_t)
9672#endif
9673 )
4a938328
MS
9674 {
9675 pstatus_t pstat;
252b5132 9676
4a938328 9677 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 9678
228e534f 9679 elf_tdata (abfd)->core->pid = pstat.pr_pid;
4a938328 9680 }
7ee38065 9681#if defined (HAVE_PSTATUS32_T)
4a938328
MS
9682 else if (note->descsz == sizeof (pstatus32_t))
9683 {
9684 /* 64-bit host, 32-bit corefile */
9685 pstatus32_t pstat;
252b5132 9686
4a938328 9687 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 9688
228e534f 9689 elf_tdata (abfd)->core->pid = pstat.pr_pid;
4a938328
MS
9690 }
9691#endif
252b5132
RH
9692 /* Could grab some more details from the "representative"
9693 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
c044fabd 9694 NT_LWPSTATUS note, presumably. */
252b5132 9695
b34976b6 9696 return TRUE;
252b5132
RH
9697}
9698#endif /* defined (HAVE_PSTATUS_T) */
9699
252b5132 9700#if defined (HAVE_LWPSTATUS_T)
b34976b6 9701static bfd_boolean
217aa764 9702elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132
RH
9703{
9704 lwpstatus_t lwpstat;
9705 char buf[100];
c044fabd 9706 char *name;
d4c88bbb 9707 size_t len;
c044fabd 9708 asection *sect;
252b5132 9709
f572a39d
AM
9710 if (note->descsz != sizeof (lwpstat)
9711#if defined (HAVE_LWPXSTATUS_T)
9712 && note->descsz != sizeof (lwpxstatus_t)
9713#endif
9714 )
b34976b6 9715 return TRUE;
252b5132
RH
9716
9717 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
9718
228e534f 9719 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
a1504221
JB
9720 /* Do not overwrite the core signal if it has already been set by
9721 another thread. */
228e534f
AM
9722 if (elf_tdata (abfd)->core->signal == 0)
9723 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
252b5132 9724
c044fabd 9725 /* Make a ".reg/999" section. */
252b5132
RH
9726
9727 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
d4c88bbb 9728 len = strlen (buf) + 1;
217aa764 9729 name = bfd_alloc (abfd, len);
252b5132 9730 if (name == NULL)
b34976b6 9731 return FALSE;
d4c88bbb 9732 memcpy (name, buf, len);
252b5132 9733
117ed4f8 9734 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 9735 if (sect == NULL)
b34976b6 9736 return FALSE;
252b5132
RH
9737
9738#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 9739 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
252b5132
RH
9740 sect->filepos = note->descpos
9741 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
9742#endif
9743
9744#if defined (HAVE_LWPSTATUS_T_PR_REG)
eea6121a 9745 sect->size = sizeof (lwpstat.pr_reg);
252b5132
RH
9746 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
9747#endif
9748
252b5132
RH
9749 sect->alignment_power = 2;
9750
9751 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
b34976b6 9752 return FALSE;
252b5132
RH
9753
9754 /* Make a ".reg2/999" section */
9755
9756 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
d4c88bbb 9757 len = strlen (buf) + 1;
217aa764 9758 name = bfd_alloc (abfd, len);
252b5132 9759 if (name == NULL)
b34976b6 9760 return FALSE;
d4c88bbb 9761 memcpy (name, buf, len);
252b5132 9762
117ed4f8 9763 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 9764 if (sect == NULL)
b34976b6 9765 return FALSE;
252b5132
RH
9766
9767#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 9768 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
252b5132
RH
9769 sect->filepos = note->descpos
9770 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
9771#endif
9772
9773#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
eea6121a 9774 sect->size = sizeof (lwpstat.pr_fpreg);
252b5132
RH
9775 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
9776#endif
9777
252b5132
RH
9778 sect->alignment_power = 2;
9779
936e320b 9780 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
252b5132
RH
9781}
9782#endif /* defined (HAVE_LWPSTATUS_T) */
9783
b34976b6 9784static bfd_boolean
217aa764 9785elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
16e9c715
NC
9786{
9787 char buf[30];
c044fabd 9788 char *name;
d4c88bbb 9789 size_t len;
c044fabd 9790 asection *sect;
4a6636fb
PA
9791 int type;
9792 int is_active_thread;
9793 bfd_vma base_addr;
16e9c715 9794
4a6636fb 9795 if (note->descsz < 728)
b34976b6 9796 return TRUE;
16e9c715 9797
4a6636fb
PA
9798 if (! CONST_STRNEQ (note->namedata, "win32"))
9799 return TRUE;
9800
9801 type = bfd_get_32 (abfd, note->descdata);
c044fabd 9802
4a6636fb 9803 switch (type)
16e9c715 9804 {
4a6636fb 9805 case 1 /* NOTE_INFO_PROCESS */:
228e534f 9806 /* FIXME: need to add ->core->command. */
4a6636fb 9807 /* process_info.pid */
228e534f 9808 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8);
4a6636fb 9809 /* process_info.signal */
228e534f 9810 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12);
c044fabd 9811 break;
16e9c715 9812
4a6636fb 9813 case 2 /* NOTE_INFO_THREAD */:
16e9c715 9814 /* Make a ".reg/999" section. */
4a6636fb
PA
9815 /* thread_info.tid */
9816 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
c044fabd 9817
d4c88bbb 9818 len = strlen (buf) + 1;
a50b1753 9819 name = (char *) bfd_alloc (abfd, len);
16e9c715 9820 if (name == NULL)
b34976b6 9821 return FALSE;
c044fabd 9822
d4c88bbb 9823 memcpy (name, buf, len);
16e9c715 9824
117ed4f8 9825 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
16e9c715 9826 if (sect == NULL)
b34976b6 9827 return FALSE;
c044fabd 9828
4a6636fb
PA
9829 /* sizeof (thread_info.thread_context) */
9830 sect->size = 716;
9831 /* offsetof (thread_info.thread_context) */
9832 sect->filepos = note->descpos + 12;
16e9c715
NC
9833 sect->alignment_power = 2;
9834
4a6636fb
PA
9835 /* thread_info.is_active_thread */
9836 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
9837
9838 if (is_active_thread)
16e9c715 9839 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
b34976b6 9840 return FALSE;
16e9c715
NC
9841 break;
9842
4a6636fb 9843 case 3 /* NOTE_INFO_MODULE */:
16e9c715 9844 /* Make a ".module/xxxxxxxx" section. */
4a6636fb
PA
9845 /* module_info.base_address */
9846 base_addr = bfd_get_32 (abfd, note->descdata + 4);
0af1713e 9847 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
c044fabd 9848
d4c88bbb 9849 len = strlen (buf) + 1;
a50b1753 9850 name = (char *) bfd_alloc (abfd, len);
16e9c715 9851 if (name == NULL)
b34976b6 9852 return FALSE;
c044fabd 9853
d4c88bbb 9854 memcpy (name, buf, len);
252b5132 9855
117ed4f8 9856 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
c044fabd 9857
16e9c715 9858 if (sect == NULL)
b34976b6 9859 return FALSE;
c044fabd 9860
eea6121a 9861 sect->size = note->descsz;
16e9c715 9862 sect->filepos = note->descpos;
16e9c715
NC
9863 sect->alignment_power = 2;
9864 break;
9865
9866 default:
b34976b6 9867 return TRUE;
16e9c715
NC
9868 }
9869
b34976b6 9870 return TRUE;
16e9c715 9871}
252b5132 9872
b34976b6 9873static bfd_boolean
217aa764 9874elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
252b5132 9875{
9c5bfbb7 9876 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
bb0082d6 9877
252b5132
RH
9878 switch (note->type)
9879 {
9880 default:
b34976b6 9881 return TRUE;
252b5132 9882
252b5132 9883 case NT_PRSTATUS:
bb0082d6
AM
9884 if (bed->elf_backend_grok_prstatus)
9885 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
b34976b6 9886 return TRUE;
bb0082d6 9887#if defined (HAVE_PRSTATUS_T)
252b5132 9888 return elfcore_grok_prstatus (abfd, note);
bb0082d6 9889#else
b34976b6 9890 return TRUE;
252b5132
RH
9891#endif
9892
9893#if defined (HAVE_PSTATUS_T)
9894 case NT_PSTATUS:
9895 return elfcore_grok_pstatus (abfd, note);
9896#endif
9897
9898#if defined (HAVE_LWPSTATUS_T)
9899 case NT_LWPSTATUS:
9900 return elfcore_grok_lwpstatus (abfd, note);
9901#endif
9902
9903 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
9904 return elfcore_grok_prfpreg (abfd, note);
9905
c044fabd 9906 case NT_WIN32PSTATUS:
16e9c715 9907 return elfcore_grok_win32pstatus (abfd, note);
16e9c715 9908
c044fabd 9909 case NT_PRXFPREG: /* Linux SSE extension */
e377ab71
MK
9910 if (note->namesz == 6
9911 && strcmp (note->namedata, "LINUX") == 0)
ff08c6bb
JB
9912 return elfcore_grok_prxfpreg (abfd, note);
9913 else
b34976b6 9914 return TRUE;
ff08c6bb 9915
4339cae0
L
9916 case NT_X86_XSTATE: /* Linux XSAVE extension */
9917 if (note->namesz == 6
9918 && strcmp (note->namedata, "LINUX") == 0)
9919 return elfcore_grok_xstatereg (abfd, note);
9920 else
9921 return TRUE;
9922
97753bd5
AM
9923 case NT_PPC_VMX:
9924 if (note->namesz == 6
9925 && strcmp (note->namedata, "LINUX") == 0)
9926 return elfcore_grok_ppc_vmx (abfd, note);
9927 else
9928 return TRUE;
9929
89eeb0bc
LM
9930 case NT_PPC_VSX:
9931 if (note->namesz == 6
07d6d2b8
AM
9932 && strcmp (note->namedata, "LINUX") == 0)
9933 return elfcore_grok_ppc_vsx (abfd, note);
89eeb0bc 9934 else
07d6d2b8 9935 return TRUE;
89eeb0bc 9936
cb2366c1
EBM
9937 case NT_PPC_TAR:
9938 if (note->namesz == 6
9939 && strcmp (note->namedata, "LINUX") == 0)
9940 return elfcore_grok_ppc_tar (abfd, note);
9941 else
9942 return TRUE;
9943
9944 case NT_PPC_PPR:
9945 if (note->namesz == 6
9946 && strcmp (note->namedata, "LINUX") == 0)
9947 return elfcore_grok_ppc_ppr (abfd, note);
9948 else
9949 return TRUE;
9950
9951 case NT_PPC_DSCR:
9952 if (note->namesz == 6
9953 && strcmp (note->namedata, "LINUX") == 0)
9954 return elfcore_grok_ppc_dscr (abfd, note);
9955 else
9956 return TRUE;
9957
9958 case NT_PPC_EBB:
9959 if (note->namesz == 6
9960 && strcmp (note->namedata, "LINUX") == 0)
9961 return elfcore_grok_ppc_ebb (abfd, note);
9962 else
9963 return TRUE;
9964
9965 case NT_PPC_PMU:
9966 if (note->namesz == 6
9967 && strcmp (note->namedata, "LINUX") == 0)
9968 return elfcore_grok_ppc_pmu (abfd, note);
9969 else
9970 return TRUE;
9971
9972 case NT_PPC_TM_CGPR:
9973 if (note->namesz == 6
9974 && strcmp (note->namedata, "LINUX") == 0)
9975 return elfcore_grok_ppc_tm_cgpr (abfd, note);
9976 else
9977 return TRUE;
9978
9979 case NT_PPC_TM_CFPR:
9980 if (note->namesz == 6
9981 && strcmp (note->namedata, "LINUX") == 0)
9982 return elfcore_grok_ppc_tm_cfpr (abfd, note);
9983 else
9984 return TRUE;
9985
9986 case NT_PPC_TM_CVMX:
9987 if (note->namesz == 6
9988 && strcmp (note->namedata, "LINUX") == 0)
9989 return elfcore_grok_ppc_tm_cvmx (abfd, note);
9990 else
9991 return TRUE;
9992
9993 case NT_PPC_TM_CVSX:
9994 if (note->namesz == 6
9995 && strcmp (note->namedata, "LINUX") == 0)
9996 return elfcore_grok_ppc_tm_cvsx (abfd, note);
9997 else
9998 return TRUE;
9999
10000 case NT_PPC_TM_SPR:
10001 if (note->namesz == 6
10002 && strcmp (note->namedata, "LINUX") == 0)
10003 return elfcore_grok_ppc_tm_spr (abfd, note);
10004 else
10005 return TRUE;
10006
10007 case NT_PPC_TM_CTAR:
10008 if (note->namesz == 6
10009 && strcmp (note->namedata, "LINUX") == 0)
10010 return elfcore_grok_ppc_tm_ctar (abfd, note);
10011 else
10012 return TRUE;
10013
10014 case NT_PPC_TM_CPPR:
10015 if (note->namesz == 6
10016 && strcmp (note->namedata, "LINUX") == 0)
10017 return elfcore_grok_ppc_tm_cppr (abfd, note);
10018 else
10019 return TRUE;
10020
10021 case NT_PPC_TM_CDSCR:
10022 if (note->namesz == 6
10023 && strcmp (note->namedata, "LINUX") == 0)
10024 return elfcore_grok_ppc_tm_cdscr (abfd, note);
10025 else
10026 return TRUE;
10027
0675e188
UW
10028 case NT_S390_HIGH_GPRS:
10029 if (note->namesz == 6
07d6d2b8
AM
10030 && strcmp (note->namedata, "LINUX") == 0)
10031 return elfcore_grok_s390_high_gprs (abfd, note);
0675e188 10032 else
07d6d2b8 10033 return TRUE;
0675e188 10034
d7eeb400
MS
10035 case NT_S390_TIMER:
10036 if (note->namesz == 6
07d6d2b8
AM
10037 && strcmp (note->namedata, "LINUX") == 0)
10038 return elfcore_grok_s390_timer (abfd, note);
d7eeb400 10039 else
07d6d2b8 10040 return TRUE;
d7eeb400
MS
10041
10042 case NT_S390_TODCMP:
10043 if (note->namesz == 6
07d6d2b8
AM
10044 && strcmp (note->namedata, "LINUX") == 0)
10045 return elfcore_grok_s390_todcmp (abfd, note);
d7eeb400 10046 else
07d6d2b8 10047 return TRUE;
d7eeb400
MS
10048
10049 case NT_S390_TODPREG:
10050 if (note->namesz == 6
07d6d2b8
AM
10051 && strcmp (note->namedata, "LINUX") == 0)
10052 return elfcore_grok_s390_todpreg (abfd, note);
d7eeb400 10053 else
07d6d2b8 10054 return TRUE;
d7eeb400
MS
10055
10056 case NT_S390_CTRS:
10057 if (note->namesz == 6
07d6d2b8
AM
10058 && strcmp (note->namedata, "LINUX") == 0)
10059 return elfcore_grok_s390_ctrs (abfd, note);
d7eeb400 10060 else
07d6d2b8 10061 return TRUE;
d7eeb400
MS
10062
10063 case NT_S390_PREFIX:
10064 if (note->namesz == 6
07d6d2b8
AM
10065 && strcmp (note->namedata, "LINUX") == 0)
10066 return elfcore_grok_s390_prefix (abfd, note);
d7eeb400 10067 else
07d6d2b8 10068 return TRUE;
d7eeb400 10069
355b81d9
UW
10070 case NT_S390_LAST_BREAK:
10071 if (note->namesz == 6
07d6d2b8
AM
10072 && strcmp (note->namedata, "LINUX") == 0)
10073 return elfcore_grok_s390_last_break (abfd, note);
355b81d9 10074 else
07d6d2b8 10075 return TRUE;
355b81d9
UW
10076
10077 case NT_S390_SYSTEM_CALL:
10078 if (note->namesz == 6
07d6d2b8
AM
10079 && strcmp (note->namedata, "LINUX") == 0)
10080 return elfcore_grok_s390_system_call (abfd, note);
355b81d9 10081 else
07d6d2b8 10082 return TRUE;
355b81d9 10083
abb3f6cc
NC
10084 case NT_S390_TDB:
10085 if (note->namesz == 6
07d6d2b8
AM
10086 && strcmp (note->namedata, "LINUX") == 0)
10087 return elfcore_grok_s390_tdb (abfd, note);
abb3f6cc 10088 else
07d6d2b8 10089 return TRUE;
abb3f6cc 10090
4ef9f41a
AA
10091 case NT_S390_VXRS_LOW:
10092 if (note->namesz == 6
10093 && strcmp (note->namedata, "LINUX") == 0)
10094 return elfcore_grok_s390_vxrs_low (abfd, note);
10095 else
10096 return TRUE;
10097
10098 case NT_S390_VXRS_HIGH:
10099 if (note->namesz == 6
10100 && strcmp (note->namedata, "LINUX") == 0)
10101 return elfcore_grok_s390_vxrs_high (abfd, note);
10102 else
10103 return TRUE;
10104
88ab90e8
AA
10105 case NT_S390_GS_CB:
10106 if (note->namesz == 6
10107 && strcmp (note->namedata, "LINUX") == 0)
8fe09d74 10108 return elfcore_grok_s390_gs_cb (abfd, note);
88ab90e8
AA
10109 else
10110 return TRUE;
10111
10112 case NT_S390_GS_BC:
10113 if (note->namesz == 6
10114 && strcmp (note->namedata, "LINUX") == 0)
8fe09d74 10115 return elfcore_grok_s390_gs_bc (abfd, note);
88ab90e8
AA
10116 else
10117 return TRUE;
10118
faa9a424
UW
10119 case NT_ARM_VFP:
10120 if (note->namesz == 6
10121 && strcmp (note->namedata, "LINUX") == 0)
10122 return elfcore_grok_arm_vfp (abfd, note);
10123 else
10124 return TRUE;
10125
652451f8
YZ
10126 case NT_ARM_TLS:
10127 if (note->namesz == 6
10128 && strcmp (note->namedata, "LINUX") == 0)
10129 return elfcore_grok_aarch_tls (abfd, note);
10130 else
10131 return TRUE;
10132
10133 case NT_ARM_HW_BREAK:
10134 if (note->namesz == 6
10135 && strcmp (note->namedata, "LINUX") == 0)
10136 return elfcore_grok_aarch_hw_break (abfd, note);
10137 else
10138 return TRUE;
10139
10140 case NT_ARM_HW_WATCH:
10141 if (note->namesz == 6
10142 && strcmp (note->namedata, "LINUX") == 0)
10143 return elfcore_grok_aarch_hw_watch (abfd, note);
10144 else
10145 return TRUE;
10146
ad1cc4e4
AH
10147 case NT_ARM_SVE:
10148 if (note->namesz == 6
10149 && strcmp (note->namedata, "LINUX") == 0)
10150 return elfcore_grok_aarch_sve (abfd, note);
10151 else
10152 return TRUE;
10153
e6c3b5bf
AH
10154 case NT_ARM_PAC_MASK:
10155 if (note->namesz == 6
10156 && strcmp (note->namedata, "LINUX") == 0)
10157 return elfcore_grok_aarch_pauth (abfd, note);
10158 else
10159 return TRUE;
10160
252b5132
RH
10161 case NT_PRPSINFO:
10162 case NT_PSINFO:
bb0082d6
AM
10163 if (bed->elf_backend_grok_psinfo)
10164 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
b34976b6 10165 return TRUE;
bb0082d6 10166#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
252b5132 10167 return elfcore_grok_psinfo (abfd, note);
bb0082d6 10168#else
b34976b6 10169 return TRUE;
252b5132 10170#endif
3333a7c3
RM
10171
10172 case NT_AUXV:
10173 {
117ed4f8
AM
10174 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10175 SEC_HAS_CONTENTS);
3333a7c3
RM
10176
10177 if (sect == NULL)
10178 return FALSE;
eea6121a 10179 sect->size = note->descsz;
3333a7c3 10180 sect->filepos = note->descpos;
3333a7c3
RM
10181 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10182
10183 return TRUE;
10184 }
9015683b 10185
451b7c33
TT
10186 case NT_FILE:
10187 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
10188 note);
10189
9015683b
TT
10190 case NT_SIGINFO:
10191 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
10192 note);
5b2c414d 10193
252b5132
RH
10194 }
10195}
10196
718175fa
JK
10197static bfd_boolean
10198elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
10199{
c74f7d1c 10200 struct bfd_build_id* build_id;
30e8ee25
AM
10201
10202 if (note->descsz == 0)
10203 return FALSE;
10204
c74f7d1c
JT
10205 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
10206 if (build_id == NULL)
718175fa
JK
10207 return FALSE;
10208
c74f7d1c
JT
10209 build_id->size = note->descsz;
10210 memcpy (build_id->data, note->descdata, note->descsz);
10211 abfd->build_id = build_id;
718175fa
JK
10212
10213 return TRUE;
10214}
10215
10216static bfd_boolean
10217elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
10218{
10219 switch (note->type)
10220 {
10221 default:
10222 return TRUE;
10223
46bed679
L
10224 case NT_GNU_PROPERTY_TYPE_0:
10225 return _bfd_elf_parse_gnu_properties (abfd, note);
10226
718175fa
JK
10227 case NT_GNU_BUILD_ID:
10228 return elfobj_grok_gnu_build_id (abfd, note);
10229 }
10230}
10231
e21e5835
NC
10232static bfd_boolean
10233elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
10234{
10235 struct sdt_note *cur =
10236 (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note)
10237 + note->descsz);
10238
10239 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
10240 cur->size = (bfd_size_type) note->descsz;
10241 memcpy (cur->data, note->descdata, note->descsz);
10242
10243 elf_tdata (abfd)->sdt_note_head = cur;
10244
10245 return TRUE;
10246}
10247
10248static bfd_boolean
10249elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
10250{
10251 switch (note->type)
10252 {
10253 case NT_STAPSDT:
10254 return elfobj_grok_stapsdt_note_1 (abfd, note);
10255
10256 default:
10257 return TRUE;
10258 }
10259}
10260
aa1ed4a9
JB
10261static bfd_boolean
10262elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
10263{
10264 size_t offset;
10265
b5430a3c 10266 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
aa1ed4a9 10267 {
b5430a3c 10268 case ELFCLASS32:
0064d223
JB
10269 if (note->descsz < 108)
10270 return FALSE;
aa1ed4a9
JB
10271 break;
10272
b5430a3c 10273 case ELFCLASS64:
0064d223
JB
10274 if (note->descsz < 120)
10275 return FALSE;
aa1ed4a9
JB
10276 break;
10277
10278 default:
10279 return FALSE;
10280 }
10281
0064d223
JB
10282 /* Check for version 1 in pr_version. */
10283 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10284 return FALSE;
80a04378 10285
0064d223
JB
10286 offset = 4;
10287
10288 /* Skip over pr_psinfosz. */
b5430a3c 10289 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
0064d223
JB
10290 offset += 4;
10291 else
10292 {
10293 offset += 4; /* Padding before pr_psinfosz. */
10294 offset += 8;
10295 }
10296
aa1ed4a9
JB
10297 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10298 elf_tdata (abfd)->core->program
10299 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
10300 offset += 17;
10301
10302 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10303 elf_tdata (abfd)->core->command
10304 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
0064d223
JB
10305 offset += 81;
10306
10307 /* Padding before pr_pid. */
10308 offset += 2;
10309
10310 /* The pr_pid field was added in version "1a". */
10311 if (note->descsz < offset + 4)
10312 return TRUE;
10313
10314 elf_tdata (abfd)->core->pid
10315 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
aa1ed4a9
JB
10316
10317 return TRUE;
10318}
10319
10320static bfd_boolean
10321elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
10322{
10323 size_t offset;
10324 size_t size;
24d3e51b 10325 size_t min_size;
aa1ed4a9 10326
24d3e51b
NC
10327 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10328 Also compute minimum size of this note. */
b5430a3c 10329 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
aa1ed4a9 10330 {
b5430a3c 10331 case ELFCLASS32:
24d3e51b
NC
10332 offset = 4 + 4;
10333 min_size = offset + (4 * 2) + 4 + 4 + 4;
aa1ed4a9
JB
10334 break;
10335
b5430a3c 10336 case ELFCLASS64:
24d3e51b
NC
10337 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
10338 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
aa1ed4a9
JB
10339 break;
10340
10341 default:
10342 return FALSE;
10343 }
10344
24d3e51b
NC
10345 if (note->descsz < min_size)
10346 return FALSE;
10347
10348 /* Check for version 1 in pr_version. */
10349 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10350 return FALSE;
aa1ed4a9 10351
24d3e51b
NC
10352 /* Extract size of pr_reg from pr_gregsetsz. */
10353 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
b5430a3c 10354 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
24d3e51b
NC
10355 {
10356 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10357 offset += 4 * 2;
10358 }
b5430a3c 10359 else
24d3e51b
NC
10360 {
10361 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
10362 offset += 8 * 2;
10363 }
aa1ed4a9 10364
24d3e51b 10365 /* Skip over pr_osreldate. */
aa1ed4a9
JB
10366 offset += 4;
10367
24d3e51b 10368 /* Read signal from pr_cursig. */
aa1ed4a9
JB
10369 if (elf_tdata (abfd)->core->signal == 0)
10370 elf_tdata (abfd)->core->signal
10371 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10372 offset += 4;
10373
24d3e51b 10374 /* Read TID from pr_pid. */
aa1ed4a9
JB
10375 elf_tdata (abfd)->core->lwpid
10376 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10377 offset += 4;
10378
24d3e51b 10379 /* Padding before pr_reg. */
b5430a3c 10380 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
aa1ed4a9
JB
10381 offset += 4;
10382
24d3e51b
NC
10383 /* Make sure that there is enough data remaining in the note. */
10384 if ((note->descsz - offset) < size)
10385 return FALSE;
10386
aa1ed4a9
JB
10387 /* Make a ".reg/999" section and a ".reg" section. */
10388 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10389 size, note->descpos + offset);
10390}
10391
10392static bfd_boolean
10393elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
10394{
544c67cd
JB
10395 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10396
aa1ed4a9
JB
10397 switch (note->type)
10398 {
10399 case NT_PRSTATUS:
544c67cd
JB
10400 if (bed->elf_backend_grok_freebsd_prstatus)
10401 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
10402 return TRUE;
aa1ed4a9
JB
10403 return elfcore_grok_freebsd_prstatus (abfd, note);
10404
10405 case NT_FPREGSET:
10406 return elfcore_grok_prfpreg (abfd, note);
10407
10408 case NT_PRPSINFO:
10409 return elfcore_grok_freebsd_psinfo (abfd, note);
10410
10411 case NT_FREEBSD_THRMISC:
10412 if (note->namesz == 8)
10413 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
10414 else
10415 return TRUE;
10416
ddb2bbcf
JB
10417 case NT_FREEBSD_PROCSTAT_PROC:
10418 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
10419 note);
10420
10421 case NT_FREEBSD_PROCSTAT_FILES:
10422 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
10423 note);
10424
10425 case NT_FREEBSD_PROCSTAT_VMMAP:
10426 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
10427 note);
10428
3350c5f5
JB
10429 case NT_FREEBSD_PROCSTAT_AUXV:
10430 {
10431 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10432 SEC_HAS_CONTENTS);
10433
10434 if (sect == NULL)
10435 return FALSE;
10436 sect->size = note->descsz - 4;
10437 sect->filepos = note->descpos + 4;
10438 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10439
10440 return TRUE;
10441 }
10442
aa1ed4a9
JB
10443 case NT_X86_XSTATE:
10444 if (note->namesz == 8)
10445 return elfcore_grok_xstatereg (abfd, note);
10446 else
10447 return TRUE;
10448
e6f3b9c3
JB
10449 case NT_FREEBSD_PTLWPINFO:
10450 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
10451 note);
10452
6d5be5d6
JB
10453 case NT_ARM_VFP:
10454 return elfcore_grok_arm_vfp (abfd, note);
10455
aa1ed4a9
JB
10456 default:
10457 return TRUE;
10458 }
10459}
10460
b34976b6 10461static bfd_boolean
217aa764 10462elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
50b2bdb7
AM
10463{
10464 char *cp;
10465
10466 cp = strchr (note->namedata, '@');
10467 if (cp != NULL)
10468 {
d2b64500 10469 *lwpidp = atoi(cp + 1);
b34976b6 10470 return TRUE;
50b2bdb7 10471 }
b34976b6 10472 return FALSE;
50b2bdb7
AM
10473}
10474
b34976b6 10475static bfd_boolean
217aa764 10476elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7 10477{
80a04378
NC
10478 if (note->descsz <= 0x7c + 31)
10479 return FALSE;
10480
50b2bdb7 10481 /* Signal number at offset 0x08. */
228e534f 10482 elf_tdata (abfd)->core->signal
50b2bdb7
AM
10483 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10484
10485 /* Process ID at offset 0x50. */
228e534f 10486 elf_tdata (abfd)->core->pid
50b2bdb7
AM
10487 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
10488
10489 /* Command name at 0x7c (max 32 bytes, including nul). */
228e534f 10490 elf_tdata (abfd)->core->command
50b2bdb7
AM
10491 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
10492
7720ba9f
MK
10493 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
10494 note);
50b2bdb7
AM
10495}
10496
b34976b6 10497static bfd_boolean
217aa764 10498elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7
AM
10499{
10500 int lwp;
10501
10502 if (elfcore_netbsd_get_lwpid (note, &lwp))
228e534f 10503 elf_tdata (abfd)->core->lwpid = lwp;
50b2bdb7 10504
b4db1224 10505 if (note->type == NT_NETBSDCORE_PROCINFO)
50b2bdb7
AM
10506 {
10507 /* NetBSD-specific core "procinfo". Note that we expect to
08a40648
AM
10508 find this note before any of the others, which is fine,
10509 since the kernel writes this note out first when it
10510 creates a core file. */
47d9a591 10511
50b2bdb7
AM
10512 return elfcore_grok_netbsd_procinfo (abfd, note);
10513 }
10514
b4db1224
JT
10515 /* As of Jan 2002 there are no other machine-independent notes
10516 defined for NetBSD core files. If the note type is less
10517 than the start of the machine-dependent note types, we don't
10518 understand it. */
47d9a591 10519
b4db1224 10520 if (note->type < NT_NETBSDCORE_FIRSTMACH)
b34976b6 10521 return TRUE;
50b2bdb7
AM
10522
10523
10524 switch (bfd_get_arch (abfd))
10525 {
08a40648
AM
10526 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10527 PT_GETFPREGS == mach+2. */
50b2bdb7
AM
10528
10529 case bfd_arch_alpha:
10530 case bfd_arch_sparc:
10531 switch (note->type)
08a40648
AM
10532 {
10533 case NT_NETBSDCORE_FIRSTMACH+0:
10534 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 10535
08a40648
AM
10536 case NT_NETBSDCORE_FIRSTMACH+2:
10537 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 10538
08a40648
AM
10539 default:
10540 return TRUE;
10541 }
50b2bdb7 10542
08a40648
AM
10543 /* On all other arch's, PT_GETREGS == mach+1 and
10544 PT_GETFPREGS == mach+3. */
50b2bdb7
AM
10545
10546 default:
10547 switch (note->type)
08a40648
AM
10548 {
10549 case NT_NETBSDCORE_FIRSTMACH+1:
10550 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 10551
08a40648
AM
10552 case NT_NETBSDCORE_FIRSTMACH+3:
10553 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 10554
08a40648
AM
10555 default:
10556 return TRUE;
10557 }
50b2bdb7
AM
10558 }
10559 /* NOTREACHED */
10560}
10561
67cc5033
MK
10562static bfd_boolean
10563elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
10564{
80a04378
NC
10565 if (note->descsz <= 0x48 + 31)
10566 return FALSE;
10567
67cc5033 10568 /* Signal number at offset 0x08. */
228e534f 10569 elf_tdata (abfd)->core->signal
67cc5033
MK
10570 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10571
10572 /* Process ID at offset 0x20. */
228e534f 10573 elf_tdata (abfd)->core->pid
67cc5033
MK
10574 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
10575
10576 /* Command name at 0x48 (max 32 bytes, including nul). */
228e534f 10577 elf_tdata (abfd)->core->command
67cc5033
MK
10578 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
10579
10580 return TRUE;
10581}
10582
10583static bfd_boolean
10584elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
10585{
10586 if (note->type == NT_OPENBSD_PROCINFO)
10587 return elfcore_grok_openbsd_procinfo (abfd, note);
10588
10589 if (note->type == NT_OPENBSD_REGS)
10590 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10591
10592 if (note->type == NT_OPENBSD_FPREGS)
10593 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10594
10595 if (note->type == NT_OPENBSD_XFPREGS)
10596 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
10597
10598 if (note->type == NT_OPENBSD_AUXV)
10599 {
10600 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10601 SEC_HAS_CONTENTS);
10602
10603 if (sect == NULL)
10604 return FALSE;
10605 sect->size = note->descsz;
10606 sect->filepos = note->descpos;
10607 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10608
10609 return TRUE;
10610 }
10611
10612 if (note->type == NT_OPENBSD_WCOOKIE)
10613 {
10614 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
10615 SEC_HAS_CONTENTS);
10616
10617 if (sect == NULL)
10618 return FALSE;
10619 sect->size = note->descsz;
10620 sect->filepos = note->descpos;
10621 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10622
10623 return TRUE;
10624 }
10625
10626 return TRUE;
10627}
10628
07c6e936 10629static bfd_boolean
d3fd4074 10630elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
07c6e936
NC
10631{
10632 void *ddata = note->descdata;
10633 char buf[100];
10634 char *name;
10635 asection *sect;
f8843e87
AM
10636 short sig;
10637 unsigned flags;
07c6e936 10638
80a04378
NC
10639 if (note->descsz < 16)
10640 return FALSE;
10641
07c6e936 10642 /* nto_procfs_status 'pid' field is at offset 0. */
228e534f 10643 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
07c6e936 10644
f8843e87
AM
10645 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
10646 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
10647
10648 /* nto_procfs_status 'flags' field is at offset 8. */
10649 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
07c6e936
NC
10650
10651 /* nto_procfs_status 'what' field is at offset 14. */
f8843e87
AM
10652 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
10653 {
228e534f
AM
10654 elf_tdata (abfd)->core->signal = sig;
10655 elf_tdata (abfd)->core->lwpid = *tid;
f8843e87 10656 }
07c6e936 10657
f8843e87
AM
10658 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
10659 do not come from signals so we make sure we set the current
10660 thread just in case. */
10661 if (flags & 0x00000080)
228e534f 10662 elf_tdata (abfd)->core->lwpid = *tid;
07c6e936
NC
10663
10664 /* Make a ".qnx_core_status/%d" section. */
d3fd4074 10665 sprintf (buf, ".qnx_core_status/%ld", *tid);
07c6e936 10666
a50b1753 10667 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
07c6e936
NC
10668 if (name == NULL)
10669 return FALSE;
10670 strcpy (name, buf);
10671
117ed4f8 10672 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936
NC
10673 if (sect == NULL)
10674 return FALSE;
10675
07d6d2b8
AM
10676 sect->size = note->descsz;
10677 sect->filepos = note->descpos;
07c6e936
NC
10678 sect->alignment_power = 2;
10679
10680 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
10681}
10682
10683static bfd_boolean
d69f560c
KW
10684elfcore_grok_nto_regs (bfd *abfd,
10685 Elf_Internal_Note *note,
d3fd4074 10686 long tid,
d69f560c 10687 char *base)
07c6e936
NC
10688{
10689 char buf[100];
10690 char *name;
10691 asection *sect;
10692
d69f560c 10693 /* Make a "(base)/%d" section. */
d3fd4074 10694 sprintf (buf, "%s/%ld", base, tid);
07c6e936 10695
a50b1753 10696 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
07c6e936
NC
10697 if (name == NULL)
10698 return FALSE;
10699 strcpy (name, buf);
10700
117ed4f8 10701 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936
NC
10702 if (sect == NULL)
10703 return FALSE;
10704
07d6d2b8
AM
10705 sect->size = note->descsz;
10706 sect->filepos = note->descpos;
07c6e936
NC
10707 sect->alignment_power = 2;
10708
f8843e87 10709 /* This is the current thread. */
228e534f 10710 if (elf_tdata (abfd)->core->lwpid == tid)
d69f560c 10711 return elfcore_maybe_make_sect (abfd, base, sect);
f8843e87
AM
10712
10713 return TRUE;
07c6e936
NC
10714}
10715
10716#define BFD_QNT_CORE_INFO 7
10717#define BFD_QNT_CORE_STATUS 8
10718#define BFD_QNT_CORE_GREG 9
10719#define BFD_QNT_CORE_FPREG 10
10720
10721static bfd_boolean
217aa764 10722elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
07c6e936
NC
10723{
10724 /* Every GREG section has a STATUS section before it. Store the
811072d8 10725 tid from the previous call to pass down to the next gregs
07c6e936 10726 function. */
d3fd4074 10727 static long tid = 1;
07c6e936
NC
10728
10729 switch (note->type)
10730 {
d69f560c
KW
10731 case BFD_QNT_CORE_INFO:
10732 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
10733 case BFD_QNT_CORE_STATUS:
10734 return elfcore_grok_nto_status (abfd, note, &tid);
10735 case BFD_QNT_CORE_GREG:
10736 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
10737 case BFD_QNT_CORE_FPREG:
10738 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
10739 default:
10740 return TRUE;
07c6e936
NC
10741 }
10742}
10743
b15fa79e
AM
10744static bfd_boolean
10745elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
10746{
10747 char *name;
10748 asection *sect;
10749 size_t len;
10750
10751 /* Use note name as section name. */
10752 len = note->namesz;
a50b1753 10753 name = (char *) bfd_alloc (abfd, len);
b15fa79e
AM
10754 if (name == NULL)
10755 return FALSE;
10756 memcpy (name, note->namedata, len);
10757 name[len - 1] = '\0';
10758
10759 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10760 if (sect == NULL)
10761 return FALSE;
10762
07d6d2b8
AM
10763 sect->size = note->descsz;
10764 sect->filepos = note->descpos;
b15fa79e
AM
10765 sect->alignment_power = 1;
10766
10767 return TRUE;
10768}
10769
7c76fa91
MS
10770/* Function: elfcore_write_note
10771
47d9a591 10772 Inputs:
a39f3346 10773 buffer to hold note, and current size of buffer
7c76fa91
MS
10774 name of note
10775 type of note
10776 data for note
10777 size of data for note
10778
a39f3346
AM
10779 Writes note to end of buffer. ELF64 notes are written exactly as
10780 for ELF32, despite the current (as of 2006) ELF gabi specifying
10781 that they ought to have 8-byte namesz and descsz field, and have
10782 8-byte alignment. Other writers, eg. Linux kernel, do the same.
10783
7c76fa91 10784 Return:
a39f3346 10785 Pointer to realloc'd buffer, *BUFSIZ updated. */
7c76fa91
MS
10786
10787char *
a39f3346 10788elfcore_write_note (bfd *abfd,
217aa764 10789 char *buf,
a39f3346 10790 int *bufsiz,
217aa764 10791 const char *name,
a39f3346 10792 int type,
217aa764 10793 const void *input,
a39f3346 10794 int size)
7c76fa91
MS
10795{
10796 Elf_External_Note *xnp;
d4c88bbb 10797 size_t namesz;
d4c88bbb 10798 size_t newspace;
a39f3346 10799 char *dest;
7c76fa91 10800
d4c88bbb 10801 namesz = 0;
d4c88bbb 10802 if (name != NULL)
a39f3346 10803 namesz = strlen (name) + 1;
d4c88bbb 10804
a39f3346 10805 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
d4c88bbb 10806
a50b1753 10807 buf = (char *) realloc (buf, *bufsiz + newspace);
14b1c01e
AM
10808 if (buf == NULL)
10809 return buf;
a39f3346 10810 dest = buf + *bufsiz;
7c76fa91
MS
10811 *bufsiz += newspace;
10812 xnp = (Elf_External_Note *) dest;
10813 H_PUT_32 (abfd, namesz, xnp->namesz);
10814 H_PUT_32 (abfd, size, xnp->descsz);
10815 H_PUT_32 (abfd, type, xnp->type);
d4c88bbb
AM
10816 dest = xnp->name;
10817 if (name != NULL)
10818 {
10819 memcpy (dest, name, namesz);
10820 dest += namesz;
a39f3346 10821 while (namesz & 3)
d4c88bbb
AM
10822 {
10823 *dest++ = '\0';
a39f3346 10824 ++namesz;
d4c88bbb
AM
10825 }
10826 }
10827 memcpy (dest, input, size);
a39f3346
AM
10828 dest += size;
10829 while (size & 3)
10830 {
10831 *dest++ = '\0';
10832 ++size;
10833 }
10834 return buf;
7c76fa91
MS
10835}
10836
602f1657
AM
10837/* gcc-8 warns (*) on all the strncpy calls in this function about
10838 possible string truncation. The "truncation" is not a bug. We
10839 have an external representation of structs with fields that are not
10840 necessarily NULL terminated and corresponding internal
10841 representation fields that are one larger so that they can always
10842 be NULL terminated.
10843 gcc versions between 4.2 and 4.6 do not allow pragma control of
10844 diagnostics inside functions, giving a hard error if you try to use
10845 the finer control available with later versions.
10846 gcc prior to 4.2 warns about diagnostic push and pop.
10847 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
10848 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
10849 (*) Depending on your system header files! */
d99b4b92 10850#if GCC_VERSION >= 8000
602f1657
AM
10851# pragma GCC diagnostic push
10852# pragma GCC diagnostic ignored "-Wstringop-truncation"
d99b4b92 10853#endif
7c76fa91 10854char *
217aa764
AM
10855elfcore_write_prpsinfo (bfd *abfd,
10856 char *buf,
10857 int *bufsiz,
10858 const char *fname,
10859 const char *psargs)
7c76fa91 10860{
183e98be
AM
10861 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10862
10863 if (bed->elf_backend_write_core_note != NULL)
10864 {
10865 char *ret;
10866 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
10867 NT_PRPSINFO, fname, psargs);
10868 if (ret != NULL)
10869 return ret;
10870 }
7c76fa91 10871
1f20dca5 10872#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
602f1657 10873# if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
183e98be
AM
10874 if (bed->s->elfclass == ELFCLASS32)
10875 {
602f1657 10876# if defined (HAVE_PSINFO32_T)
183e98be
AM
10877 psinfo32_t data;
10878 int note_type = NT_PSINFO;
602f1657 10879# else
183e98be
AM
10880 prpsinfo32_t data;
10881 int note_type = NT_PRPSINFO;
602f1657 10882# endif
183e98be
AM
10883
10884 memset (&data, 0, sizeof (data));
10885 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
10886 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
10887 return elfcore_write_note (abfd, buf, bufsiz,
1f20dca5 10888 "CORE", note_type, &data, sizeof (data));
183e98be
AM
10889 }
10890 else
602f1657 10891# endif
183e98be 10892 {
602f1657 10893# if defined (HAVE_PSINFO_T)
183e98be
AM
10894 psinfo_t data;
10895 int note_type = NT_PSINFO;
602f1657 10896# else
183e98be
AM
10897 prpsinfo_t data;
10898 int note_type = NT_PRPSINFO;
602f1657 10899# endif
7c76fa91 10900
183e98be
AM
10901 memset (&data, 0, sizeof (data));
10902 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
10903 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
10904 return elfcore_write_note (abfd, buf, bufsiz,
1f20dca5 10905 "CORE", note_type, &data, sizeof (data));
183e98be 10906 }
7c76fa91
MS
10907#endif /* PSINFO_T or PRPSINFO_T */
10908
1f20dca5
UW
10909 free (buf);
10910 return NULL;
10911}
d99b4b92 10912#if GCC_VERSION >= 8000
602f1657 10913# pragma GCC diagnostic pop
d99b4b92 10914#endif
1f20dca5 10915
70a38d42
SDJ
10916char *
10917elfcore_write_linux_prpsinfo32
10918 (bfd *abfd, char *buf, int *bufsiz,
10919 const struct elf_internal_linux_prpsinfo *prpsinfo)
10920{
a2f63b2e
MR
10921 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
10922 {
10923 struct elf_external_linux_prpsinfo32_ugid16 data;
10924
10925 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
10926 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
10927 &data, sizeof (data));
10928 }
10929 else
10930 {
10931 struct elf_external_linux_prpsinfo32_ugid32 data;
70a38d42 10932
a2f63b2e
MR
10933 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
10934 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
10935 &data, sizeof (data));
10936 }
70a38d42
SDJ
10937}
10938
10939char *
10940elfcore_write_linux_prpsinfo64
10941 (bfd *abfd, char *buf, int *bufsiz,
10942 const struct elf_internal_linux_prpsinfo *prpsinfo)
10943{
3c9a7b0d
MR
10944 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
10945 {
10946 struct elf_external_linux_prpsinfo64_ugid16 data;
10947
10948 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
10949 return elfcore_write_note (abfd, buf, bufsiz,
10950 "CORE", NT_PRPSINFO, &data, sizeof (data));
10951 }
10952 else
10953 {
10954 struct elf_external_linux_prpsinfo64_ugid32 data;
70a38d42 10955
3c9a7b0d
MR
10956 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
10957 return elfcore_write_note (abfd, buf, bufsiz,
10958 "CORE", NT_PRPSINFO, &data, sizeof (data));
10959 }
70a38d42
SDJ
10960}
10961
7c76fa91 10962char *
217aa764
AM
10963elfcore_write_prstatus (bfd *abfd,
10964 char *buf,
10965 int *bufsiz,
10966 long pid,
10967 int cursig,
10968 const void *gregs)
7c76fa91 10969{
183e98be 10970 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 10971
183e98be
AM
10972 if (bed->elf_backend_write_core_note != NULL)
10973 {
10974 char *ret;
10975 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
10976 NT_PRSTATUS,
10977 pid, cursig, gregs);
10978 if (ret != NULL)
10979 return ret;
10980 }
10981
1f20dca5 10982#if defined (HAVE_PRSTATUS_T)
183e98be
AM
10983#if defined (HAVE_PRSTATUS32_T)
10984 if (bed->s->elfclass == ELFCLASS32)
10985 {
10986 prstatus32_t prstat;
10987
10988 memset (&prstat, 0, sizeof (prstat));
10989 prstat.pr_pid = pid;
10990 prstat.pr_cursig = cursig;
10991 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
1f20dca5 10992 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
183e98be
AM
10993 NT_PRSTATUS, &prstat, sizeof (prstat));
10994 }
10995 else
10996#endif
10997 {
10998 prstatus_t prstat;
10999
11000 memset (&prstat, 0, sizeof (prstat));
11001 prstat.pr_pid = pid;
11002 prstat.pr_cursig = cursig;
11003 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
1f20dca5 11004 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
183e98be
AM
11005 NT_PRSTATUS, &prstat, sizeof (prstat));
11006 }
7c76fa91
MS
11007#endif /* HAVE_PRSTATUS_T */
11008
1f20dca5
UW
11009 free (buf);
11010 return NULL;
11011}
11012
51316059
MS
11013#if defined (HAVE_LWPSTATUS_T)
11014char *
217aa764
AM
11015elfcore_write_lwpstatus (bfd *abfd,
11016 char *buf,
11017 int *bufsiz,
11018 long pid,
11019 int cursig,
11020 const void *gregs)
51316059
MS
11021{
11022 lwpstatus_t lwpstat;
183e98be 11023 const char *note_name = "CORE";
51316059
MS
11024
11025 memset (&lwpstat, 0, sizeof (lwpstat));
11026 lwpstat.pr_lwpid = pid >> 16;
11027 lwpstat.pr_cursig = cursig;
11028#if defined (HAVE_LWPSTATUS_T_PR_REG)
d1e8523e 11029 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
51316059
MS
11030#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11031#if !defined(gregs)
11032 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
11033 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
11034#else
11035 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
11036 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
11037#endif
11038#endif
47d9a591 11039 return elfcore_write_note (abfd, buf, bufsiz, note_name,
51316059
MS
11040 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
11041}
11042#endif /* HAVE_LWPSTATUS_T */
11043
7c76fa91
MS
11044#if defined (HAVE_PSTATUS_T)
11045char *
217aa764
AM
11046elfcore_write_pstatus (bfd *abfd,
11047 char *buf,
11048 int *bufsiz,
11049 long pid,
6c10990d
NC
11050 int cursig ATTRIBUTE_UNUSED,
11051 const void *gregs ATTRIBUTE_UNUSED)
7c76fa91 11052{
183e98be
AM
11053 const char *note_name = "CORE";
11054#if defined (HAVE_PSTATUS32_T)
11055 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 11056
183e98be
AM
11057 if (bed->s->elfclass == ELFCLASS32)
11058 {
11059 pstatus32_t pstat;
11060
11061 memset (&pstat, 0, sizeof (pstat));
11062 pstat.pr_pid = pid & 0xffff;
11063 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11064 NT_PSTATUS, &pstat, sizeof (pstat));
11065 return buf;
11066 }
11067 else
11068#endif
11069 {
11070 pstatus_t pstat;
11071
11072 memset (&pstat, 0, sizeof (pstat));
11073 pstat.pr_pid = pid & 0xffff;
11074 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11075 NT_PSTATUS, &pstat, sizeof (pstat));
11076 return buf;
11077 }
7c76fa91
MS
11078}
11079#endif /* HAVE_PSTATUS_T */
11080
11081char *
217aa764
AM
11082elfcore_write_prfpreg (bfd *abfd,
11083 char *buf,
11084 int *bufsiz,
11085 const void *fpregs,
11086 int size)
7c76fa91 11087{
183e98be 11088 const char *note_name = "CORE";
47d9a591 11089 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
11090 note_name, NT_FPREGSET, fpregs, size);
11091}
11092
11093char *
217aa764
AM
11094elfcore_write_prxfpreg (bfd *abfd,
11095 char *buf,
11096 int *bufsiz,
11097 const void *xfpregs,
11098 int size)
7c76fa91
MS
11099{
11100 char *note_name = "LINUX";
47d9a591 11101 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
11102 note_name, NT_PRXFPREG, xfpregs, size);
11103}
11104
4339cae0
L
11105char *
11106elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
11107 const void *xfpregs, int size)
11108{
97de3545
JB
11109 char *note_name;
11110 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
11111 note_name = "FreeBSD";
11112 else
11113 note_name = "LINUX";
4339cae0
L
11114 return elfcore_write_note (abfd, buf, bufsiz,
11115 note_name, NT_X86_XSTATE, xfpregs, size);
11116}
11117
97753bd5
AM
11118char *
11119elfcore_write_ppc_vmx (bfd *abfd,
11120 char *buf,
11121 int *bufsiz,
11122 const void *ppc_vmx,
11123 int size)
11124{
11125 char *note_name = "LINUX";
11126 return elfcore_write_note (abfd, buf, bufsiz,
11127 note_name, NT_PPC_VMX, ppc_vmx, size);
11128}
11129
89eeb0bc
LM
11130char *
11131elfcore_write_ppc_vsx (bfd *abfd,
07d6d2b8
AM
11132 char *buf,
11133 int *bufsiz,
11134 const void *ppc_vsx,
11135 int size)
89eeb0bc
LM
11136{
11137 char *note_name = "LINUX";
11138 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11139 note_name, NT_PPC_VSX, ppc_vsx, size);
89eeb0bc
LM
11140}
11141
cb2366c1
EBM
11142char *
11143elfcore_write_ppc_tar (bfd *abfd,
11144 char *buf,
11145 int *bufsiz,
11146 const void *ppc_tar,
11147 int size)
11148{
11149 char *note_name = "LINUX";
11150 return elfcore_write_note (abfd, buf, bufsiz,
11151 note_name, NT_PPC_TAR, ppc_tar, size);
11152}
11153
11154char *
11155elfcore_write_ppc_ppr (bfd *abfd,
11156 char *buf,
11157 int *bufsiz,
11158 const void *ppc_ppr,
11159 int size)
11160{
11161 char *note_name = "LINUX";
11162 return elfcore_write_note (abfd, buf, bufsiz,
11163 note_name, NT_PPC_PPR, ppc_ppr, size);
11164}
11165
11166char *
11167elfcore_write_ppc_dscr (bfd *abfd,
11168 char *buf,
11169 int *bufsiz,
11170 const void *ppc_dscr,
11171 int size)
11172{
11173 char *note_name = "LINUX";
11174 return elfcore_write_note (abfd, buf, bufsiz,
11175 note_name, NT_PPC_DSCR, ppc_dscr, size);
11176}
11177
11178char *
11179elfcore_write_ppc_ebb (bfd *abfd,
11180 char *buf,
11181 int *bufsiz,
11182 const void *ppc_ebb,
11183 int size)
11184{
11185 char *note_name = "LINUX";
11186 return elfcore_write_note (abfd, buf, bufsiz,
11187 note_name, NT_PPC_EBB, ppc_ebb, size);
11188}
11189
11190char *
11191elfcore_write_ppc_pmu (bfd *abfd,
11192 char *buf,
11193 int *bufsiz,
11194 const void *ppc_pmu,
11195 int size)
11196{
11197 char *note_name = "LINUX";
11198 return elfcore_write_note (abfd, buf, bufsiz,
11199 note_name, NT_PPC_PMU, ppc_pmu, size);
11200}
11201
11202char *
11203elfcore_write_ppc_tm_cgpr (bfd *abfd,
11204 char *buf,
11205 int *bufsiz,
11206 const void *ppc_tm_cgpr,
11207 int size)
11208{
11209 char *note_name = "LINUX";
11210 return elfcore_write_note (abfd, buf, bufsiz,
11211 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
11212}
11213
11214char *
11215elfcore_write_ppc_tm_cfpr (bfd *abfd,
11216 char *buf,
11217 int *bufsiz,
11218 const void *ppc_tm_cfpr,
11219 int size)
11220{
11221 char *note_name = "LINUX";
11222 return elfcore_write_note (abfd, buf, bufsiz,
11223 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
11224}
11225
11226char *
11227elfcore_write_ppc_tm_cvmx (bfd *abfd,
11228 char *buf,
11229 int *bufsiz,
11230 const void *ppc_tm_cvmx,
11231 int size)
11232{
11233 char *note_name = "LINUX";
11234 return elfcore_write_note (abfd, buf, bufsiz,
11235 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
11236}
11237
11238char *
11239elfcore_write_ppc_tm_cvsx (bfd *abfd,
11240 char *buf,
11241 int *bufsiz,
11242 const void *ppc_tm_cvsx,
11243 int size)
11244{
11245 char *note_name = "LINUX";
11246 return elfcore_write_note (abfd, buf, bufsiz,
11247 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
11248}
11249
11250char *
11251elfcore_write_ppc_tm_spr (bfd *abfd,
11252 char *buf,
11253 int *bufsiz,
11254 const void *ppc_tm_spr,
11255 int size)
11256{
11257 char *note_name = "LINUX";
11258 return elfcore_write_note (abfd, buf, bufsiz,
11259 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
11260}
11261
11262char *
11263elfcore_write_ppc_tm_ctar (bfd *abfd,
11264 char *buf,
11265 int *bufsiz,
11266 const void *ppc_tm_ctar,
11267 int size)
11268{
11269 char *note_name = "LINUX";
11270 return elfcore_write_note (abfd, buf, bufsiz,
11271 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
11272}
11273
11274char *
11275elfcore_write_ppc_tm_cppr (bfd *abfd,
11276 char *buf,
11277 int *bufsiz,
11278 const void *ppc_tm_cppr,
11279 int size)
11280{
11281 char *note_name = "LINUX";
11282 return elfcore_write_note (abfd, buf, bufsiz,
11283 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
11284}
11285
11286char *
11287elfcore_write_ppc_tm_cdscr (bfd *abfd,
11288 char *buf,
11289 int *bufsiz,
11290 const void *ppc_tm_cdscr,
11291 int size)
11292{
11293 char *note_name = "LINUX";
11294 return elfcore_write_note (abfd, buf, bufsiz,
11295 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
11296}
11297
0675e188
UW
11298static char *
11299elfcore_write_s390_high_gprs (bfd *abfd,
11300 char *buf,
11301 int *bufsiz,
11302 const void *s390_high_gprs,
11303 int size)
11304{
11305 char *note_name = "LINUX";
11306 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11307 note_name, NT_S390_HIGH_GPRS,
0675e188
UW
11308 s390_high_gprs, size);
11309}
11310
d7eeb400
MS
11311char *
11312elfcore_write_s390_timer (bfd *abfd,
07d6d2b8
AM
11313 char *buf,
11314 int *bufsiz,
11315 const void *s390_timer,
11316 int size)
d7eeb400
MS
11317{
11318 char *note_name = "LINUX";
11319 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11320 note_name, NT_S390_TIMER, s390_timer, size);
d7eeb400
MS
11321}
11322
11323char *
11324elfcore_write_s390_todcmp (bfd *abfd,
07d6d2b8
AM
11325 char *buf,
11326 int *bufsiz,
11327 const void *s390_todcmp,
11328 int size)
d7eeb400
MS
11329{
11330 char *note_name = "LINUX";
11331 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11332 note_name, NT_S390_TODCMP, s390_todcmp, size);
d7eeb400
MS
11333}
11334
11335char *
11336elfcore_write_s390_todpreg (bfd *abfd,
07d6d2b8
AM
11337 char *buf,
11338 int *bufsiz,
11339 const void *s390_todpreg,
11340 int size)
d7eeb400
MS
11341{
11342 char *note_name = "LINUX";
11343 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11344 note_name, NT_S390_TODPREG, s390_todpreg, size);
d7eeb400
MS
11345}
11346
11347char *
11348elfcore_write_s390_ctrs (bfd *abfd,
07d6d2b8
AM
11349 char *buf,
11350 int *bufsiz,
11351 const void *s390_ctrs,
11352 int size)
d7eeb400
MS
11353{
11354 char *note_name = "LINUX";
11355 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11356 note_name, NT_S390_CTRS, s390_ctrs, size);
d7eeb400
MS
11357}
11358
11359char *
11360elfcore_write_s390_prefix (bfd *abfd,
07d6d2b8
AM
11361 char *buf,
11362 int *bufsiz,
11363 const void *s390_prefix,
11364 int size)
d7eeb400
MS
11365{
11366 char *note_name = "LINUX";
11367 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11368 note_name, NT_S390_PREFIX, s390_prefix, size);
d7eeb400
MS
11369}
11370
355b81d9
UW
11371char *
11372elfcore_write_s390_last_break (bfd *abfd,
11373 char *buf,
11374 int *bufsiz,
11375 const void *s390_last_break,
11376 int size)
11377{
11378 char *note_name = "LINUX";
11379 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11380 note_name, NT_S390_LAST_BREAK,
355b81d9
UW
11381 s390_last_break, size);
11382}
11383
11384char *
11385elfcore_write_s390_system_call (bfd *abfd,
11386 char *buf,
11387 int *bufsiz,
11388 const void *s390_system_call,
11389 int size)
11390{
11391 char *note_name = "LINUX";
11392 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11393 note_name, NT_S390_SYSTEM_CALL,
355b81d9
UW
11394 s390_system_call, size);
11395}
11396
abb3f6cc
NC
11397char *
11398elfcore_write_s390_tdb (bfd *abfd,
11399 char *buf,
11400 int *bufsiz,
11401 const void *s390_tdb,
11402 int size)
11403{
11404 char *note_name = "LINUX";
11405 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11406 note_name, NT_S390_TDB, s390_tdb, size);
abb3f6cc
NC
11407}
11408
4ef9f41a
AA
11409char *
11410elfcore_write_s390_vxrs_low (bfd *abfd,
11411 char *buf,
11412 int *bufsiz,
11413 const void *s390_vxrs_low,
11414 int size)
11415{
11416 char *note_name = "LINUX";
11417 return elfcore_write_note (abfd, buf, bufsiz,
11418 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
11419}
11420
11421char *
11422elfcore_write_s390_vxrs_high (bfd *abfd,
11423 char *buf,
11424 int *bufsiz,
11425 const void *s390_vxrs_high,
11426 int size)
11427{
11428 char *note_name = "LINUX";
11429 return elfcore_write_note (abfd, buf, bufsiz,
11430 note_name, NT_S390_VXRS_HIGH,
11431 s390_vxrs_high, size);
11432}
11433
88ab90e8
AA
11434char *
11435elfcore_write_s390_gs_cb (bfd *abfd,
11436 char *buf,
11437 int *bufsiz,
11438 const void *s390_gs_cb,
11439 int size)
11440{
11441 char *note_name = "LINUX";
11442 return elfcore_write_note (abfd, buf, bufsiz,
11443 note_name, NT_S390_GS_CB,
11444 s390_gs_cb, size);
11445}
11446
11447char *
11448elfcore_write_s390_gs_bc (bfd *abfd,
11449 char *buf,
11450 int *bufsiz,
11451 const void *s390_gs_bc,
11452 int size)
11453{
11454 char *note_name = "LINUX";
11455 return elfcore_write_note (abfd, buf, bufsiz,
11456 note_name, NT_S390_GS_BC,
11457 s390_gs_bc, size);
11458}
11459
faa9a424
UW
11460char *
11461elfcore_write_arm_vfp (bfd *abfd,
11462 char *buf,
11463 int *bufsiz,
11464 const void *arm_vfp,
11465 int size)
11466{
11467 char *note_name = "LINUX";
11468 return elfcore_write_note (abfd, buf, bufsiz,
11469 note_name, NT_ARM_VFP, arm_vfp, size);
11470}
11471
652451f8
YZ
11472char *
11473elfcore_write_aarch_tls (bfd *abfd,
11474 char *buf,
11475 int *bufsiz,
11476 const void *aarch_tls,
11477 int size)
11478{
11479 char *note_name = "LINUX";
11480 return elfcore_write_note (abfd, buf, bufsiz,
11481 note_name, NT_ARM_TLS, aarch_tls, size);
11482}
11483
11484char *
11485elfcore_write_aarch_hw_break (bfd *abfd,
11486 char *buf,
11487 int *bufsiz,
11488 const void *aarch_hw_break,
11489 int size)
11490{
11491 char *note_name = "LINUX";
11492 return elfcore_write_note (abfd, buf, bufsiz,
11493 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
11494}
11495
11496char *
11497elfcore_write_aarch_hw_watch (bfd *abfd,
11498 char *buf,
11499 int *bufsiz,
11500 const void *aarch_hw_watch,
11501 int size)
11502{
11503 char *note_name = "LINUX";
11504 return elfcore_write_note (abfd, buf, bufsiz,
11505 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
11506}
11507
ad1cc4e4
AH
11508char *
11509elfcore_write_aarch_sve (bfd *abfd,
11510 char *buf,
11511 int *bufsiz,
11512 const void *aarch_sve,
11513 int size)
11514{
11515 char *note_name = "LINUX";
11516 return elfcore_write_note (abfd, buf, bufsiz,
11517 note_name, NT_ARM_SVE, aarch_sve, size);
11518}
11519
e6c3b5bf
AH
11520char *
11521elfcore_write_aarch_pauth (bfd *abfd,
11522 char *buf,
11523 int *bufsiz,
11524 const void *aarch_pauth,
11525 int size)
11526{
11527 char *note_name = "LINUX";
11528 return elfcore_write_note (abfd, buf, bufsiz,
11529 note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
11530}
11531
bb864ac1
CES
11532char *
11533elfcore_write_register_note (bfd *abfd,
11534 char *buf,
11535 int *bufsiz,
11536 const char *section,
11537 const void *data,
11538 int size)
11539{
11540 if (strcmp (section, ".reg2") == 0)
11541 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
11542 if (strcmp (section, ".reg-xfp") == 0)
11543 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
4339cae0
L
11544 if (strcmp (section, ".reg-xstate") == 0)
11545 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
bb864ac1
CES
11546 if (strcmp (section, ".reg-ppc-vmx") == 0)
11547 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
89eeb0bc
LM
11548 if (strcmp (section, ".reg-ppc-vsx") == 0)
11549 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
cb2366c1
EBM
11550 if (strcmp (section, ".reg-ppc-tar") == 0)
11551 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
11552 if (strcmp (section, ".reg-ppc-ppr") == 0)
11553 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
11554 if (strcmp (section, ".reg-ppc-dscr") == 0)
11555 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
11556 if (strcmp (section, ".reg-ppc-ebb") == 0)
11557 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
11558 if (strcmp (section, ".reg-ppc-pmu") == 0)
11559 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
11560 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
11561 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
11562 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
11563 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
11564 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
11565 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
11566 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
11567 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
11568 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
11569 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
11570 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
11571 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
11572 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
11573 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
11574 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
11575 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
0675e188
UW
11576 if (strcmp (section, ".reg-s390-high-gprs") == 0)
11577 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
d7eeb400
MS
11578 if (strcmp (section, ".reg-s390-timer") == 0)
11579 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
11580 if (strcmp (section, ".reg-s390-todcmp") == 0)
11581 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
11582 if (strcmp (section, ".reg-s390-todpreg") == 0)
11583 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
11584 if (strcmp (section, ".reg-s390-ctrs") == 0)
11585 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
11586 if (strcmp (section, ".reg-s390-prefix") == 0)
11587 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
355b81d9
UW
11588 if (strcmp (section, ".reg-s390-last-break") == 0)
11589 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
11590 if (strcmp (section, ".reg-s390-system-call") == 0)
11591 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
abb3f6cc
NC
11592 if (strcmp (section, ".reg-s390-tdb") == 0)
11593 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
4ef9f41a
AA
11594 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
11595 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
11596 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
11597 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
88ab90e8
AA
11598 if (strcmp (section, ".reg-s390-gs-cb") == 0)
11599 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
11600 if (strcmp (section, ".reg-s390-gs-bc") == 0)
11601 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
faa9a424
UW
11602 if (strcmp (section, ".reg-arm-vfp") == 0)
11603 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
652451f8
YZ
11604 if (strcmp (section, ".reg-aarch-tls") == 0)
11605 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
11606 if (strcmp (section, ".reg-aarch-hw-break") == 0)
11607 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
11608 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
11609 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
ad1cc4e4
AH
11610 if (strcmp (section, ".reg-aarch-sve") == 0)
11611 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
e6c3b5bf
AH
11612 if (strcmp (section, ".reg-aarch-pauth") == 0)
11613 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
bb864ac1
CES
11614 return NULL;
11615}
11616
b34976b6 11617static bfd_boolean
276da9b3
L
11618elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
11619 size_t align)
252b5132 11620{
c044fabd 11621 char *p;
252b5132 11622
276da9b3
L
11623 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
11624 gABI specifies that PT_NOTE alignment should be aligned to 4
11625 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
11626 align is less than 4, we use 4 byte alignment. */
11627 if (align < 4)
11628 align = 4;
ef135d43
NC
11629 if (align != 4 && align != 8)
11630 return FALSE;
276da9b3 11631
252b5132
RH
11632 p = buf;
11633 while (p < buf + size)
11634 {
c044fabd 11635 Elf_External_Note *xnp = (Elf_External_Note *) p;
252b5132
RH
11636 Elf_Internal_Note in;
11637
baea7ef1
AM
11638 if (offsetof (Elf_External_Note, name) > buf - p + size)
11639 return FALSE;
11640
dc810e39 11641 in.type = H_GET_32 (abfd, xnp->type);
252b5132 11642
dc810e39 11643 in.namesz = H_GET_32 (abfd, xnp->namesz);
252b5132 11644 in.namedata = xnp->name;
baea7ef1
AM
11645 if (in.namesz > buf - in.namedata + size)
11646 return FALSE;
252b5132 11647
dc810e39 11648 in.descsz = H_GET_32 (abfd, xnp->descsz);
276da9b3 11649 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
252b5132 11650 in.descpos = offset + (in.descdata - buf);
baea7ef1
AM
11651 if (in.descsz != 0
11652 && (in.descdata >= buf + size
11653 || in.descsz > buf - in.descdata + size))
11654 return FALSE;
252b5132 11655
718175fa 11656 switch (bfd_get_format (abfd))
07d6d2b8 11657 {
718175fa
JK
11658 default:
11659 return TRUE;
11660
11661 case bfd_core:
f64e188b 11662 {
8acbedd6 11663#define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
f64e188b 11664 struct
718175fa 11665 {
f64e188b 11666 const char * string;
8acbedd6 11667 size_t len;
f64e188b 11668 bfd_boolean (* func)(bfd *, Elf_Internal_Note *);
718175fa 11669 }
f64e188b 11670 grokers[] =
b15fa79e 11671 {
8acbedd6 11672 GROKER_ELEMENT ("", elfcore_grok_note),
aa1ed4a9 11673 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
8acbedd6
KS
11674 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
11675 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note),
11676 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
11677 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note)
f64e188b 11678 };
8acbedd6 11679#undef GROKER_ELEMENT
f64e188b
NC
11680 int i;
11681
11682 for (i = ARRAY_SIZE (grokers); i--;)
8acbedd6
KS
11683 {
11684 if (in.namesz >= grokers[i].len
11685 && strncmp (in.namedata, grokers[i].string,
11686 grokers[i].len) == 0)
11687 {
11688 if (! grokers[i].func (abfd, & in))
11689 return FALSE;
11690 break;
11691 }
11692 }
f64e188b
NC
11693 break;
11694 }
718175fa
JK
11695
11696 case bfd_object:
11697 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
11698 {
11699 if (! elfobj_grok_gnu_note (abfd, &in))
11700 return FALSE;
11701 }
e21e5835
NC
11702 else if (in.namesz == sizeof "stapsdt"
11703 && strcmp (in.namedata, "stapsdt") == 0)
11704 {
11705 if (! elfobj_grok_stapsdt_note (abfd, &in))
11706 return FALSE;
11707 }
718175fa 11708 break;
08a40648 11709 }
252b5132 11710
276da9b3 11711 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
252b5132
RH
11712 }
11713
718175fa
JK
11714 return TRUE;
11715}
11716
11717static bfd_boolean
276da9b3
L
11718elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
11719 size_t align)
718175fa
JK
11720{
11721 char *buf;
11722
957e1fc1 11723 if (size == 0 || (size + 1) == 0)
718175fa
JK
11724 return TRUE;
11725
11726 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
11727 return FALSE;
11728
f64e188b 11729 buf = (char *) bfd_malloc (size + 1);
718175fa
JK
11730 if (buf == NULL)
11731 return FALSE;
11732
f64e188b
NC
11733 /* PR 17512: file: ec08f814
11734 0-termintate the buffer so that string searches will not overflow. */
11735 buf[size] = 0;
11736
718175fa 11737 if (bfd_bread (buf, size, abfd) != size
276da9b3 11738 || !elf_parse_notes (abfd, buf, size, offset, align))
718175fa
JK
11739 {
11740 free (buf);
11741 return FALSE;
11742 }
11743
252b5132 11744 free (buf);
b34976b6 11745 return TRUE;
252b5132 11746}
98d8431c
JB
11747\f
11748/* Providing external access to the ELF program header table. */
11749
11750/* Return an upper bound on the number of bytes required to store a
11751 copy of ABFD's program header table entries. Return -1 if an error
11752 occurs; bfd_get_error will return an appropriate code. */
c044fabd 11753
98d8431c 11754long
217aa764 11755bfd_get_elf_phdr_upper_bound (bfd *abfd)
98d8431c
JB
11756{
11757 if (abfd->xvec->flavour != bfd_target_elf_flavour)
11758 {
11759 bfd_set_error (bfd_error_wrong_format);
11760 return -1;
11761 }
11762
936e320b 11763 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
98d8431c
JB
11764}
11765
98d8431c
JB
11766/* Copy ABFD's program header table entries to *PHDRS. The entries
11767 will be stored as an array of Elf_Internal_Phdr structures, as
11768 defined in include/elf/internal.h. To find out how large the
11769 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
11770
11771 Return the number of program header table entries read, or -1 if an
11772 error occurs; bfd_get_error will return an appropriate code. */
c044fabd 11773
98d8431c 11774int
217aa764 11775bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
98d8431c
JB
11776{
11777 int num_phdrs;
11778
11779 if (abfd->xvec->flavour != bfd_target_elf_flavour)
11780 {
11781 bfd_set_error (bfd_error_wrong_format);
11782 return -1;
11783 }
11784
11785 num_phdrs = elf_elfheader (abfd)->e_phnum;
01bcaf63
TT
11786 if (num_phdrs != 0)
11787 memcpy (phdrs, elf_tdata (abfd)->phdr,
11788 num_phdrs * sizeof (Elf_Internal_Phdr));
98d8431c
JB
11789
11790 return num_phdrs;
11791}
ae4221d7 11792
db6751f2 11793enum elf_reloc_type_class
7e612e98
AM
11794_bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
11795 const asection *rel_sec ATTRIBUTE_UNUSED,
11796 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
db6751f2
JJ
11797{
11798 return reloc_class_normal;
11799}
f8df10f4 11800
47d9a591 11801/* For RELA architectures, return the relocation value for a
f8df10f4
JJ
11802 relocation against a local symbol. */
11803
11804bfd_vma
217aa764
AM
11805_bfd_elf_rela_local_sym (bfd *abfd,
11806 Elf_Internal_Sym *sym,
8517fae7 11807 asection **psec,
217aa764 11808 Elf_Internal_Rela *rel)
f8df10f4 11809{
8517fae7 11810 asection *sec = *psec;
f8df10f4
JJ
11811 bfd_vma relocation;
11812
11813 relocation = (sec->output_section->vma
11814 + sec->output_offset
11815 + sym->st_value);
11816 if ((sec->flags & SEC_MERGE)
c629eae0 11817 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
dbaa2011 11818 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
f8df10f4 11819 {
f8df10f4 11820 rel->r_addend =
8517fae7 11821 _bfd_merged_section_offset (abfd, psec,
65765700 11822 elf_section_data (sec)->sec_info,
753731ee
AM
11823 sym->st_value + rel->r_addend);
11824 if (sec != *psec)
11825 {
11826 /* If we have changed the section, and our original section is
11827 marked with SEC_EXCLUDE, it means that the original
11828 SEC_MERGE section has been completely subsumed in some
11829 other SEC_MERGE section. In this case, we need to leave
11830 some info around for --emit-relocs. */
11831 if ((sec->flags & SEC_EXCLUDE) != 0)
11832 sec->kept_section = *psec;
11833 sec = *psec;
11834 }
8517fae7
AM
11835 rel->r_addend -= relocation;
11836 rel->r_addend += sec->output_section->vma + sec->output_offset;
f8df10f4
JJ
11837 }
11838 return relocation;
11839}
c629eae0
JJ
11840
11841bfd_vma
217aa764
AM
11842_bfd_elf_rel_local_sym (bfd *abfd,
11843 Elf_Internal_Sym *sym,
11844 asection **psec,
11845 bfd_vma addend)
47d9a591 11846{
c629eae0
JJ
11847 asection *sec = *psec;
11848
dbaa2011 11849 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
c629eae0
JJ
11850 return sym->st_value + addend;
11851
11852 return _bfd_merged_section_offset (abfd, psec,
65765700 11853 elf_section_data (sec)->sec_info,
753731ee 11854 sym->st_value + addend);
c629eae0
JJ
11855}
11856
37b01f6a
DG
11857/* Adjust an address within a section. Given OFFSET within SEC, return
11858 the new offset within the section, based upon changes made to the
11859 section. Returns -1 if the offset is now invalid.
11860 The offset (in abnd out) is in target sized bytes, however big a
11861 byte may be. */
11862
c629eae0 11863bfd_vma
217aa764 11864_bfd_elf_section_offset (bfd *abfd,
92e4ec35 11865 struct bfd_link_info *info,
217aa764
AM
11866 asection *sec,
11867 bfd_vma offset)
c629eae0 11868{
68bfbfcc 11869 switch (sec->sec_info_type)
65765700 11870 {
dbaa2011 11871 case SEC_INFO_TYPE_STABS:
eea6121a
AM
11872 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
11873 offset);
dbaa2011 11874 case SEC_INFO_TYPE_EH_FRAME:
92e4ec35 11875 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
37b01f6a 11876
65765700 11877 default:
310fd250
L
11878 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
11879 {
37b01f6a 11880 /* Reverse the offset. */
310fd250
L
11881 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11882 bfd_size_type address_size = bed->s->arch_size / 8;
37b01f6a
DG
11883
11884 /* address_size and sec->size are in octets. Convert
11885 to bytes before subtracting the original offset. */
11886 offset = (sec->size - address_size) / bfd_octets_per_byte (abfd) - offset;
310fd250 11887 }
65765700
JJ
11888 return offset;
11889 }
c629eae0 11890}
3333a7c3
RM
11891\f
11892/* Create a new BFD as if by bfd_openr. Rather than opening a file,
11893 reconstruct an ELF file by reading the segments out of remote memory
11894 based on the ELF file header at EHDR_VMA and the ELF program headers it
11895 points to. If not null, *LOADBASEP is filled in with the difference
11896 between the VMAs from which the segments were read, and the VMAs the
11897 file headers (and hence BFD's idea of each section's VMA) put them at.
11898
11899 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
11900 remote memory at target address VMA into the local buffer at MYADDR; it
11901 should return zero on success or an `errno' code on failure. TEMPL must
11902 be a BFD for an ELF target with the word size and byte order found in
11903 the remote memory. */
11904
11905bfd *
217aa764
AM
11906bfd_elf_bfd_from_remote_memory
11907 (bfd *templ,
11908 bfd_vma ehdr_vma,
f0a5d95a 11909 bfd_size_type size,
217aa764 11910 bfd_vma *loadbasep,
fe78531d 11911 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
3333a7c3
RM
11912{
11913 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
5979d6b6 11914 (templ, ehdr_vma, size, loadbasep, target_read_memory);
3333a7c3 11915}
4c45e5c9
JJ
11916\f
11917long
c9727e01
AM
11918_bfd_elf_get_synthetic_symtab (bfd *abfd,
11919 long symcount ATTRIBUTE_UNUSED,
11920 asymbol **syms ATTRIBUTE_UNUSED,
8615f3f2 11921 long dynsymcount,
c9727e01
AM
11922 asymbol **dynsyms,
11923 asymbol **ret)
4c45e5c9
JJ
11924{
11925 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11926 asection *relplt;
11927 asymbol *s;
11928 const char *relplt_name;
11929 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
11930 arelent *p;
11931 long count, i, n;
11932 size_t size;
11933 Elf_Internal_Shdr *hdr;
11934 char *names;
11935 asection *plt;
11936
8615f3f2
AM
11937 *ret = NULL;
11938
90e3cdf2
JJ
11939 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
11940 return 0;
11941
8615f3f2
AM
11942 if (dynsymcount <= 0)
11943 return 0;
11944
4c45e5c9
JJ
11945 if (!bed->plt_sym_val)
11946 return 0;
11947
11948 relplt_name = bed->relplt_name;
11949 if (relplt_name == NULL)
d35fd659 11950 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
4c45e5c9
JJ
11951 relplt = bfd_get_section_by_name (abfd, relplt_name);
11952 if (relplt == NULL)
11953 return 0;
11954
11955 hdr = &elf_section_data (relplt)->this_hdr;
11956 if (hdr->sh_link != elf_dynsymtab (abfd)
11957 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
11958 return 0;
11959
11960 plt = bfd_get_section_by_name (abfd, ".plt");
11961 if (plt == NULL)
11962 return 0;
11963
11964 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
c9727e01 11965 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
4c45e5c9
JJ
11966 return -1;
11967
eea6121a 11968 count = relplt->size / hdr->sh_entsize;
4c45e5c9
JJ
11969 size = count * sizeof (asymbol);
11970 p = relplt->relocation;
cb53bf42 11971 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
041de40d
AM
11972 {
11973 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
11974 if (p->addend != 0)
11975 {
11976#ifdef BFD64
11977 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
11978#else
11979 size += sizeof ("+0x") - 1 + 8;
11980#endif
11981 }
11982 }
4c45e5c9 11983
a50b1753 11984 s = *ret = (asymbol *) bfd_malloc (size);
4c45e5c9
JJ
11985 if (s == NULL)
11986 return -1;
11987
11988 names = (char *) (s + count);
11989 p = relplt->relocation;
11990 n = 0;
cb53bf42 11991 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
4c45e5c9
JJ
11992 {
11993 size_t len;
11994 bfd_vma addr;
11995
11996 addr = bed->plt_sym_val (i, plt, p);
11997 if (addr == (bfd_vma) -1)
11998 continue;
11999
12000 *s = **p->sym_ptr_ptr;
65a7a66f
AM
12001 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
12002 we are defining a symbol, ensure one of them is set. */
12003 if ((s->flags & BSF_LOCAL) == 0)
12004 s->flags |= BSF_GLOBAL;
6ba2a415 12005 s->flags |= BSF_SYNTHETIC;
4c45e5c9
JJ
12006 s->section = plt;
12007 s->value = addr - plt->vma;
12008 s->name = names;
8f39ba8e 12009 s->udata.p = NULL;
4c45e5c9
JJ
12010 len = strlen ((*p->sym_ptr_ptr)->name);
12011 memcpy (names, (*p->sym_ptr_ptr)->name, len);
12012 names += len;
041de40d
AM
12013 if (p->addend != 0)
12014 {
1d770845 12015 char buf[30], *a;
d324f6d6 12016
041de40d
AM
12017 memcpy (names, "+0x", sizeof ("+0x") - 1);
12018 names += sizeof ("+0x") - 1;
1d770845
L
12019 bfd_sprintf_vma (abfd, buf, p->addend);
12020 for (a = buf; *a == '0'; ++a)
12021 ;
12022 len = strlen (a);
12023 memcpy (names, a, len);
12024 names += len;
041de40d 12025 }
4c45e5c9
JJ
12026 memcpy (names, "@plt", sizeof ("@plt"));
12027 names += sizeof ("@plt");
8f39ba8e 12028 ++s, ++n;
4c45e5c9
JJ
12029 }
12030
12031 return n;
12032}
3d7f7666 12033
821e6ff6
AM
12034/* It is only used by x86-64 so far.
12035 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
7eacd66b
AM
12036 but current usage would allow all of _bfd_std_section to be zero. */
12037static const asymbol lcomm_sym
12038 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
3b22753a 12039asection _bfd_elf_large_com_section
7eacd66b 12040 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
821e6ff6 12041 "LARGE_COMMON", 0, SEC_IS_COMMON);
ecca9871 12042
d1036acb 12043void
78245035
L
12044_bfd_elf_post_process_headers (bfd * abfd,
12045 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
d1036acb
L
12046{
12047 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
12048
12049 i_ehdrp = elf_elfheader (abfd);
12050
12051 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
d8045f23
NC
12052
12053 /* To make things simpler for the loader on Linux systems we set the
9c55345c 12054 osabi field to ELFOSABI_GNU if the binary contains symbols of
f64b2e8d 12055 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
d8045f23 12056 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE
f64b2e8d 12057 && elf_tdata (abfd)->has_gnu_symbols)
9c55345c 12058 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
d1036acb 12059}
fcb93ecf
PB
12060
12061
12062/* Return TRUE for ELF symbol types that represent functions.
12063 This is the default version of this function, which is sufficient for
d8045f23 12064 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
fcb93ecf
PB
12065
12066bfd_boolean
12067_bfd_elf_is_function_type (unsigned int type)
12068{
d8045f23
NC
12069 return (type == STT_FUNC
12070 || type == STT_GNU_IFUNC);
fcb93ecf 12071}
9f296da3 12072
aef36ac1
AM
12073/* If the ELF symbol SYM might be a function in SEC, return the
12074 function size and set *CODE_OFF to the function's entry point,
12075 otherwise return zero. */
9f296da3 12076
aef36ac1
AM
12077bfd_size_type
12078_bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
12079 bfd_vma *code_off)
9f296da3 12080{
aef36ac1
AM
12081 bfd_size_type size;
12082
ff9e0f5b 12083 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
aef36ac1
AM
12084 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
12085 || sym->section != sec)
12086 return 0;
ff9e0f5b 12087
ff9e0f5b 12088 *code_off = sym->value;
aef36ac1
AM
12089 size = 0;
12090 if (!(sym->flags & BSF_SYNTHETIC))
12091 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
12092 if (size == 0)
12093 size = 1;
12094 return size;
9f296da3 12095}
This page took 2.180583 seconds and 4 git commands to generate.