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