Automatic date update in version.in
[deliverable/binutils-gdb.git] / bfd / elf.c
CommitLineData
252b5132 1/* ELF executable support for BFD.
340b6d91 2
250d07de 3 Copyright (C) 1993-2021 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 *);
3d16b64e
NA
54static bfd_boolean swap_out_syms (bfd *, struct elf_strtab_hash **, int,
55 struct bfd_link_info *);
718175fa 56static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
276da9b3 57 file_ptr offset, size_t align);
50b2bdb7 58
252b5132
RH
59/* Swap version information in and out. The version information is
60 currently size independent. If that ever changes, this code will
61 need to move into elfcode.h. */
62
63/* Swap in a Verdef structure. */
64
65void
217aa764
AM
66_bfd_elf_swap_verdef_in (bfd *abfd,
67 const Elf_External_Verdef *src,
68 Elf_Internal_Verdef *dst)
252b5132 69{
dc810e39
AM
70 dst->vd_version = H_GET_16 (abfd, src->vd_version);
71 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
72 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
73 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
74 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
75 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
76 dst->vd_next = H_GET_32 (abfd, src->vd_next);
252b5132
RH
77}
78
79/* Swap out a Verdef structure. */
80
81void
217aa764
AM
82_bfd_elf_swap_verdef_out (bfd *abfd,
83 const Elf_Internal_Verdef *src,
84 Elf_External_Verdef *dst)
252b5132 85{
dc810e39
AM
86 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
87 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
88 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
89 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
90 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
91 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
92 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
252b5132
RH
93}
94
95/* Swap in a Verdaux structure. */
96
97void
217aa764
AM
98_bfd_elf_swap_verdaux_in (bfd *abfd,
99 const Elf_External_Verdaux *src,
100 Elf_Internal_Verdaux *dst)
252b5132 101{
dc810e39
AM
102 dst->vda_name = H_GET_32 (abfd, src->vda_name);
103 dst->vda_next = H_GET_32 (abfd, src->vda_next);
252b5132
RH
104}
105
106/* Swap out a Verdaux structure. */
107
108void
217aa764
AM
109_bfd_elf_swap_verdaux_out (bfd *abfd,
110 const Elf_Internal_Verdaux *src,
111 Elf_External_Verdaux *dst)
252b5132 112{
dc810e39
AM
113 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
114 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
252b5132
RH
115}
116
117/* Swap in a Verneed structure. */
118
119void
217aa764
AM
120_bfd_elf_swap_verneed_in (bfd *abfd,
121 const Elf_External_Verneed *src,
122 Elf_Internal_Verneed *dst)
252b5132 123{
dc810e39
AM
124 dst->vn_version = H_GET_16 (abfd, src->vn_version);
125 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
126 dst->vn_file = H_GET_32 (abfd, src->vn_file);
127 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
128 dst->vn_next = H_GET_32 (abfd, src->vn_next);
252b5132
RH
129}
130
131/* Swap out a Verneed structure. */
132
133void
217aa764
AM
134_bfd_elf_swap_verneed_out (bfd *abfd,
135 const Elf_Internal_Verneed *src,
136 Elf_External_Verneed *dst)
252b5132 137{
dc810e39
AM
138 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
139 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
140 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
141 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
142 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
252b5132
RH
143}
144
145/* Swap in a Vernaux structure. */
146
147void
217aa764
AM
148_bfd_elf_swap_vernaux_in (bfd *abfd,
149 const Elf_External_Vernaux *src,
150 Elf_Internal_Vernaux *dst)
252b5132 151{
dc810e39
AM
152 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
153 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
154 dst->vna_other = H_GET_16 (abfd, src->vna_other);
155 dst->vna_name = H_GET_32 (abfd, src->vna_name);
156 dst->vna_next = H_GET_32 (abfd, src->vna_next);
252b5132
RH
157}
158
159/* Swap out a Vernaux structure. */
160
161void
217aa764
AM
162_bfd_elf_swap_vernaux_out (bfd *abfd,
163 const Elf_Internal_Vernaux *src,
164 Elf_External_Vernaux *dst)
252b5132 165{
dc810e39
AM
166 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
167 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
168 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
169 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
170 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
252b5132
RH
171}
172
173/* Swap in a Versym structure. */
174
175void
217aa764
AM
176_bfd_elf_swap_versym_in (bfd *abfd,
177 const Elf_External_Versym *src,
178 Elf_Internal_Versym *dst)
252b5132 179{
dc810e39 180 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
252b5132
RH
181}
182
183/* Swap out a Versym structure. */
184
185void
217aa764
AM
186_bfd_elf_swap_versym_out (bfd *abfd,
187 const Elf_Internal_Versym *src,
188 Elf_External_Versym *dst)
252b5132 189{
dc810e39 190 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
252b5132
RH
191}
192
193/* Standard ELF hash function. Do not change this function; you will
194 cause invalid hash tables to be generated. */
3a99b017 195
252b5132 196unsigned long
217aa764 197bfd_elf_hash (const char *namearg)
252b5132 198{
3a99b017 199 const unsigned char *name = (const unsigned char *) namearg;
252b5132
RH
200 unsigned long h = 0;
201 unsigned long g;
202 int ch;
203
204 while ((ch = *name++) != '\0')
205 {
206 h = (h << 4) + ch;
207 if ((g = (h & 0xf0000000)) != 0)
208 {
209 h ^= g >> 24;
210 /* The ELF ABI says `h &= ~g', but this is equivalent in
211 this case and on some machines one insn instead of two. */
212 h ^= g;
213 }
214 }
32dfa85d 215 return h & 0xffffffff;
252b5132
RH
216}
217
fdc90cb4
JJ
218/* DT_GNU_HASH hash function. Do not change this function; you will
219 cause invalid hash tables to be generated. */
220
221unsigned long
222bfd_elf_gnu_hash (const char *namearg)
223{
224 const unsigned char *name = (const unsigned char *) namearg;
225 unsigned long h = 5381;
226 unsigned char ch;
227
228 while ((ch = *name++) != '\0')
229 h = (h << 5) + h + ch;
230 return h & 0xffffffff;
231}
232
0c8d6e5c
AM
233/* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
234 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
b34976b6 235bfd_boolean
0c8d6e5c 236bfd_elf_allocate_object (bfd *abfd,
0ffa91dd 237 size_t object_size,
4dfe6ac6 238 enum elf_target_id object_id)
252b5132 239{
0ffa91dd
NC
240 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
241 abfd->tdata.any = bfd_zalloc (abfd, object_size);
242 if (abfd->tdata.any == NULL)
243 return FALSE;
252b5132 244
0ffa91dd 245 elf_object_id (abfd) = object_id;
c0355132
AM
246 if (abfd->direction != read_direction)
247 {
248 struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
249 if (o == NULL)
250 return FALSE;
251 elf_tdata (abfd)->o = o;
252 elf_program_header_size (abfd) = (bfd_size_type) -1;
253 }
b34976b6 254 return TRUE;
252b5132
RH
255}
256
0ffa91dd
NC
257
258bfd_boolean
ae95ffa6 259bfd_elf_make_object (bfd *abfd)
0ffa91dd 260{
ae95ffa6 261 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
0ffa91dd 262 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
ae95ffa6 263 bed->target_id);
0ffa91dd
NC
264}
265
b34976b6 266bfd_boolean
217aa764 267bfd_elf_mkcorefile (bfd *abfd)
252b5132 268{
c044fabd 269 /* I think this can be done just like an object file. */
228e534f
AM
270 if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
271 return FALSE;
272 elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
273 return elf_tdata (abfd)->core != NULL;
252b5132
RH
274}
275
6d5944fc 276char *
217aa764 277bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
252b5132
RH
278{
279 Elf_Internal_Shdr **i_shdrp;
f075ee0c 280 bfd_byte *shstrtab = NULL;
dc810e39
AM
281 file_ptr offset;
282 bfd_size_type shstrtabsize;
252b5132
RH
283
284 i_shdrp = elf_elfsections (abfd);
74f2e02b
AM
285 if (i_shdrp == 0
286 || shindex >= elf_numsections (abfd)
287 || i_shdrp[shindex] == 0)
f075ee0c 288 return NULL;
252b5132 289
f075ee0c 290 shstrtab = i_shdrp[shindex]->contents;
252b5132
RH
291 if (shstrtab == NULL)
292 {
c044fabd 293 /* No cached one, attempt to read, and cache what we read. */
252b5132
RH
294 offset = i_shdrp[shindex]->sh_offset;
295 shstrtabsize = i_shdrp[shindex]->sh_size;
c6c60d09
JJ
296
297 /* Allocate and clear an extra byte at the end, to prevent crashes
298 in case the string table is not terminated. */
3471d59d 299 if (shstrtabsize + 1 <= 1
06614111 300 || bfd_seek (abfd, offset, SEEK_SET) != 0
2bb3687b
AM
301 || (shstrtab = _bfd_alloc_and_read (abfd, shstrtabsize + 1,
302 shstrtabsize)) == NULL)
303 {
3471d59d
CC
304 /* Once we've failed to read it, make sure we don't keep
305 trying. Otherwise, we'll keep allocating space for
306 the string table over and over. */
307 i_shdrp[shindex]->sh_size = 0;
c6c60d09
JJ
308 }
309 else
310 shstrtab[shstrtabsize] = '\0';
217aa764 311 i_shdrp[shindex]->contents = shstrtab;
252b5132 312 }
f075ee0c 313 return (char *) shstrtab;
252b5132
RH
314}
315
316char *
217aa764
AM
317bfd_elf_string_from_elf_section (bfd *abfd,
318 unsigned int shindex,
319 unsigned int strindex)
252b5132
RH
320{
321 Elf_Internal_Shdr *hdr;
322
323 if (strindex == 0)
324 return "";
325
74f2e02b
AM
326 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
327 return NULL;
328
252b5132
RH
329 hdr = elf_elfsections (abfd)[shindex];
330
06614111
NC
331 if (hdr->contents == NULL)
332 {
333 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
334 {
335 /* PR 17512: file: f057ec89. */
695344c0 336 /* xgettext:c-format */
871b3ab2 337 _bfd_error_handler (_("%pB: attempt to load strings from"
63a5468a 338 " a non-string section (number %d)"),
06614111
NC
339 abfd, shindex);
340 return NULL;
341 }
b1fa9dd6 342
06614111
NC
343 if (bfd_elf_get_str_section (abfd, shindex) == NULL)
344 return NULL;
345 }
eed5def8
NC
346 else
347 {
348 /* PR 24273: The string section's contents may have already
349 been loaded elsewhere, eg because a corrupt file has the
350 string section index in the ELF header pointing at a group
351 section. So be paranoid, and test that the last byte of
352 the section is zero. */
353 if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
354 return NULL;
355 }
252b5132
RH
356
357 if (strindex >= hdr->sh_size)
358 {
1b3a8575 359 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
4eca0228 360 _bfd_error_handler
695344c0 361 /* xgettext:c-format */
2dcf00ce
AM
362 (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
363 abfd, strindex, (uint64_t) hdr->sh_size,
1b3a8575 364 (shindex == shstrndx && strindex == hdr->sh_name
252b5132 365 ? ".shstrtab"
1b3a8575 366 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
45b222d6 367 return NULL;
252b5132
RH
368 }
369
370 return ((char *) hdr->contents) + strindex;
371}
372
6cdc0ccc
AM
373/* Read and convert symbols to internal format.
374 SYMCOUNT specifies the number of symbols to read, starting from
375 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
376 are non-NULL, they are used to store the internal symbols, external
b7c368d0
NC
377 symbols, and symbol section index extensions, respectively.
378 Returns a pointer to the internal symbol buffer (malloced if necessary)
379 or NULL if there were no symbols or some kind of problem. */
6cdc0ccc
AM
380
381Elf_Internal_Sym *
217aa764
AM
382bfd_elf_get_elf_syms (bfd *ibfd,
383 Elf_Internal_Shdr *symtab_hdr,
384 size_t symcount,
385 size_t symoffset,
386 Elf_Internal_Sym *intsym_buf,
387 void *extsym_buf,
388 Elf_External_Sym_Shndx *extshndx_buf)
6cdc0ccc
AM
389{
390 Elf_Internal_Shdr *shndx_hdr;
217aa764 391 void *alloc_ext;
df622259 392 const bfd_byte *esym;
6cdc0ccc
AM
393 Elf_External_Sym_Shndx *alloc_extshndx;
394 Elf_External_Sym_Shndx *shndx;
4dd07732 395 Elf_Internal_Sym *alloc_intsym;
6cdc0ccc
AM
396 Elf_Internal_Sym *isym;
397 Elf_Internal_Sym *isymend;
9c5bfbb7 398 const struct elf_backend_data *bed;
6cdc0ccc 399 size_t extsym_size;
1f4361a7 400 size_t amt;
6cdc0ccc
AM
401 file_ptr pos;
402
e44a2c9c
AM
403 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
404 abort ();
405
6cdc0ccc
AM
406 if (symcount == 0)
407 return intsym_buf;
408
409 /* Normal syms might have section extension entries. */
410 shndx_hdr = NULL;
6a40cf0c
NC
411 if (elf_symtab_shndx_list (ibfd) != NULL)
412 {
413 elf_section_list * entry;
414 Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
415
416 /* Find an index section that is linked to this symtab section. */
417 for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
315350be
NC
418 {
419 /* PR 20063. */
420 if (entry->hdr.sh_link >= elf_numsections (ibfd))
421 continue;
422
423 if (sections[entry->hdr.sh_link] == symtab_hdr)
424 {
425 shndx_hdr = & entry->hdr;
426 break;
427 };
428 }
6a40cf0c
NC
429
430 if (shndx_hdr == NULL)
431 {
432 if (symtab_hdr == & elf_symtab_hdr (ibfd))
433 /* Not really accurate, but this was how the old code used to work. */
434 shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr;
435 /* Otherwise we do nothing. The assumption is that
436 the index table will not be needed. */
437 }
438 }
6cdc0ccc
AM
439
440 /* Read the symbols. */
441 alloc_ext = NULL;
442 alloc_extshndx = NULL;
4dd07732 443 alloc_intsym = NULL;
6cdc0ccc
AM
444 bed = get_elf_backend_data (ibfd);
445 extsym_size = bed->s->sizeof_sym;
1f4361a7
AM
446 if (_bfd_mul_overflow (symcount, extsym_size, &amt))
447 {
448 bfd_set_error (bfd_error_file_too_big);
449 intsym_buf = NULL;
450 goto out;
451 }
6cdc0ccc
AM
452 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
453 if (extsym_buf == NULL)
454 {
1f4361a7 455 alloc_ext = bfd_malloc (amt);
6cdc0ccc
AM
456 extsym_buf = alloc_ext;
457 }
458 if (extsym_buf == NULL
459 || bfd_seek (ibfd, pos, SEEK_SET) != 0
460 || bfd_bread (extsym_buf, amt, ibfd) != amt)
461 {
462 intsym_buf = NULL;
463 goto out;
464 }
465
466 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
467 extshndx_buf = NULL;
468 else
469 {
1f4361a7
AM
470 if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
471 {
472 bfd_set_error (bfd_error_file_too_big);
473 intsym_buf = NULL;
474 goto out;
475 }
6cdc0ccc
AM
476 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
477 if (extshndx_buf == NULL)
478 {
1f4361a7 479 alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
6cdc0ccc
AM
480 extshndx_buf = alloc_extshndx;
481 }
482 if (extshndx_buf == NULL
483 || bfd_seek (ibfd, pos, SEEK_SET) != 0
484 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
485 {
486 intsym_buf = NULL;
487 goto out;
488 }
489 }
490
491 if (intsym_buf == NULL)
492 {
1f4361a7
AM
493 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
494 {
495 bfd_set_error (bfd_error_file_too_big);
496 goto out;
497 }
498 alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
4dd07732 499 intsym_buf = alloc_intsym;
6cdc0ccc
AM
500 if (intsym_buf == NULL)
501 goto out;
502 }
503
504 /* Convert the symbols to internal form. */
505 isymend = intsym_buf + symcount;
a50b1753 506 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
07d6d2b8 507 shndx = extshndx_buf;
6cdc0ccc
AM
508 isym < isymend;
509 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
8384fb8f
AM
510 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
511 {
512 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
695344c0 513 /* xgettext:c-format */
871b3ab2 514 _bfd_error_handler (_("%pB symbol number %lu references"
63a5468a 515 " nonexistent SHT_SYMTAB_SHNDX section"),
4eca0228 516 ibfd, (unsigned long) symoffset);
c9594989 517 free (alloc_intsym);
8384fb8f
AM
518 intsym_buf = NULL;
519 goto out;
520 }
6cdc0ccc
AM
521
522 out:
c9594989
AM
523 free (alloc_ext);
524 free (alloc_extshndx);
6cdc0ccc
AM
525
526 return intsym_buf;
527}
528
5cab59f6
AM
529/* Look up a symbol name. */
530const char *
be8dd2ca
AM
531bfd_elf_sym_name (bfd *abfd,
532 Elf_Internal_Shdr *symtab_hdr,
26c61ae5
L
533 Elf_Internal_Sym *isym,
534 asection *sym_sec)
5cab59f6 535{
26c61ae5 536 const char *name;
5cab59f6 537 unsigned int iname = isym->st_name;
be8dd2ca 538 unsigned int shindex = symtab_hdr->sh_link;
26c61ae5 539
138f35cc
JJ
540 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
541 /* Check for a bogus st_shndx to avoid crashing. */
4fbb74a6 542 && isym->st_shndx < elf_numsections (abfd))
5cab59f6
AM
543 {
544 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
545 shindex = elf_elfheader (abfd)->e_shstrndx;
546 }
547
26c61ae5
L
548 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
549 if (name == NULL)
550 name = "(null)";
551 else if (sym_sec && *name == '\0')
fd361982 552 name = bfd_section_name (sym_sec);
26c61ae5
L
553
554 return name;
5cab59f6
AM
555}
556
dbb410c3
AM
557/* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
558 sections. The first element is the flags, the rest are section
559 pointers. */
560
561typedef union elf_internal_group {
562 Elf_Internal_Shdr *shdr;
563 unsigned int flags;
564} Elf_Internal_Group;
565
b885599b
AM
566/* Return the name of the group signature symbol. Why isn't the
567 signature just a string? */
568
569static const char *
217aa764 570group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
b885599b 571{
9dce4196 572 Elf_Internal_Shdr *hdr;
9dce4196
AM
573 unsigned char esym[sizeof (Elf64_External_Sym)];
574 Elf_External_Sym_Shndx eshndx;
575 Elf_Internal_Sym isym;
b885599b 576
13792e9d
L
577 /* First we need to ensure the symbol table is available. Make sure
578 that it is a symbol table section. */
4fbb74a6
AM
579 if (ghdr->sh_link >= elf_numsections (abfd))
580 return NULL;
13792e9d
L
581 hdr = elf_elfsections (abfd) [ghdr->sh_link];
582 if (hdr->sh_type != SHT_SYMTAB
583 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
b885599b
AM
584 return NULL;
585
9dce4196
AM
586 /* Go read the symbol. */
587 hdr = &elf_tdata (abfd)->symtab_hdr;
6cdc0ccc
AM
588 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
589 &isym, esym, &eshndx) == NULL)
b885599b 590 return NULL;
9dce4196 591
26c61ae5 592 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
b885599b
AM
593}
594
dbb410c3
AM
595/* Set next_in_group list pointer, and group name for NEWSECT. */
596
b34976b6 597static bfd_boolean
217aa764 598setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
dbb410c3
AM
599{
600 unsigned int num_group = elf_tdata (abfd)->num_group;
601
602 /* If num_group is zero, read in all SHT_GROUP sections. The count
603 is set to -1 if there are no SHT_GROUP sections. */
604 if (num_group == 0)
605 {
606 unsigned int i, shnum;
607
608 /* First count the number of groups. If we have a SHT_GROUP
609 section with just a flag word (ie. sh_size is 4), ignore it. */
9ad5cbcf 610 shnum = elf_numsections (abfd);
dbb410c3 611 num_group = 0;
08a40648 612
44534af3 613#define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
1783205a 614 ( (shdr)->sh_type == SHT_GROUP \
44534af3 615 && (shdr)->sh_size >= minsize \
1783205a
NC
616 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
617 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
08a40648 618
dbb410c3
AM
619 for (i = 0; i < shnum; i++)
620 {
621 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
1783205a 622
44534af3 623 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
dbb410c3
AM
624 num_group += 1;
625 }
626
627 if (num_group == 0)
20dbb49d
L
628 {
629 num_group = (unsigned) -1;
630 elf_tdata (abfd)->num_group = num_group;
ce497010 631 elf_tdata (abfd)->group_sect_ptr = NULL;
20dbb49d
L
632 }
633 else
dbb410c3
AM
634 {
635 /* We keep a list of elf section headers for group sections,
636 so we can find them quickly. */
1f4361a7 637 size_t amt;
d0fb9a8d 638
20dbb49d 639 elf_tdata (abfd)->num_group = num_group;
1f4361a7
AM
640 amt = num_group * sizeof (Elf_Internal_Shdr *);
641 elf_tdata (abfd)->group_sect_ptr
642 = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
dbb410c3 643 if (elf_tdata (abfd)->group_sect_ptr == NULL)
b34976b6 644 return FALSE;
dbb410c3 645 num_group = 0;
ce497010 646
dbb410c3
AM
647 for (i = 0; i < shnum; i++)
648 {
649 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
1783205a 650
44534af3 651 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
dbb410c3 652 {
973ffd63 653 unsigned char *src;
dbb410c3
AM
654 Elf_Internal_Group *dest;
655
07d6d2b8
AM
656 /* Make sure the group section has a BFD section
657 attached to it. */
658 if (!bfd_section_from_shdr (abfd, i))
659 return FALSE;
660
dbb410c3
AM
661 /* Add to list of sections. */
662 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
663 num_group += 1;
664
665 /* Read the raw contents. */
1f4361a7
AM
666 BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0);
667 shdr->contents = NULL;
668 if (_bfd_mul_overflow (shdr->sh_size,
669 sizeof (*dest) / 4, &amt)
1f4361a7 670 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
2bb3687b
AM
671 || !(shdr->contents
672 = _bfd_alloc_and_read (abfd, amt, shdr->sh_size)))
493a3386
NC
673 {
674 _bfd_error_handler
695344c0 675 /* xgettext:c-format */
871b3ab2 676 (_("%pB: invalid size field in group section"
2dcf00ce
AM
677 " header: %#" PRIx64 ""),
678 abfd, (uint64_t) shdr->sh_size);
493a3386
NC
679 bfd_set_error (bfd_error_bad_value);
680 -- num_group;
493a3386
NC
681 continue;
682 }
708d7d0d 683
dbb410c3
AM
684 /* Translate raw contents, a flag word followed by an
685 array of elf section indices all in target byte order,
686 to the flag word followed by an array of elf section
687 pointers. */
688 src = shdr->contents + shdr->sh_size;
689 dest = (Elf_Internal_Group *) (shdr->contents + amt);
06614111 690
dbb410c3
AM
691 while (1)
692 {
693 unsigned int idx;
694
695 src -= 4;
696 --dest;
697 idx = H_GET_32 (abfd, src);
698 if (src == shdr->contents)
699 {
327301a4 700 dest->shdr = NULL;
dbb410c3 701 dest->flags = idx;
b885599b
AM
702 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
703 shdr->bfd_section->flags
704 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
dbb410c3
AM
705 break;
706 }
4bba0fb1 707 if (idx < shnum)
bae363f1
L
708 {
709 dest->shdr = elf_elfsections (abfd)[idx];
710 /* PR binutils/23199: All sections in a
711 section group should be marked with
712 SHF_GROUP. But some tools generate
713 broken objects without SHF_GROUP. Fix
714 them up here. */
715 dest->shdr->sh_flags |= SHF_GROUP;
716 }
4bba0fb1
AM
717 if (idx >= shnum
718 || dest->shdr->sh_type == SHT_GROUP)
dbb410c3 719 {
4eca0228 720 _bfd_error_handler
4bba0fb1
AM
721 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
722 abfd, i);
723 dest->shdr = NULL;
dbb410c3 724 }
dbb410c3
AM
725 }
726 }
727 }
493a3386
NC
728
729 /* PR 17510: Corrupt binaries might contain invalid groups. */
730 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
731 {
732 elf_tdata (abfd)->num_group = num_group;
733
734 /* If all groups are invalid then fail. */
735 if (num_group == 0)
736 {
737 elf_tdata (abfd)->group_sect_ptr = NULL;
738 elf_tdata (abfd)->num_group = num_group = -1;
4eca0228 739 _bfd_error_handler
871b3ab2 740 (_("%pB: no valid group sections found"), abfd);
493a3386
NC
741 bfd_set_error (bfd_error_bad_value);
742 }
743 }
dbb410c3
AM
744 }
745 }
746
747 if (num_group != (unsigned) -1)
748 {
564e11c9
JW
749 unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
750 unsigned int j;
dbb410c3 751
564e11c9 752 for (j = 0; j < num_group; j++)
dbb410c3 753 {
564e11c9
JW
754 /* Begin search from previous found group. */
755 unsigned i = (j + search_offset) % num_group;
756
dbb410c3 757 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
ce497010 758 Elf_Internal_Group *idx;
0c54f692 759 bfd_size_type n_elt;
ce497010
NC
760
761 if (shdr == NULL)
762 continue;
763
764 idx = (Elf_Internal_Group *) shdr->contents;
0c54f692
NC
765 if (idx == NULL || shdr->sh_size < 4)
766 {
767 /* See PR 21957 for a reproducer. */
768 /* xgettext:c-format */
871b3ab2 769 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
0c54f692
NC
770 abfd, shdr->bfd_section);
771 elf_tdata (abfd)->group_sect_ptr[i] = NULL;
772 bfd_set_error (bfd_error_bad_value);
773 return FALSE;
774 }
ce497010 775 n_elt = shdr->sh_size / 4;
dbb410c3
AM
776
777 /* Look through this group's sections to see if current
778 section is a member. */
779 while (--n_elt != 0)
780 if ((++idx)->shdr == hdr)
781 {
e0e8c97f 782 asection *s = NULL;
dbb410c3
AM
783
784 /* We are a member of this group. Go looking through
785 other members to see if any others are linked via
786 next_in_group. */
787 idx = (Elf_Internal_Group *) shdr->contents;
788 n_elt = shdr->sh_size / 4;
789 while (--n_elt != 0)
4bba0fb1
AM
790 if ((++idx)->shdr != NULL
791 && (s = idx->shdr->bfd_section) != NULL
945906ff 792 && elf_next_in_group (s) != NULL)
dbb410c3
AM
793 break;
794 if (n_elt != 0)
795 {
dbb410c3
AM
796 /* Snarf the group name from other member, and
797 insert current section in circular list. */
945906ff
AM
798 elf_group_name (newsect) = elf_group_name (s);
799 elf_next_in_group (newsect) = elf_next_in_group (s);
800 elf_next_in_group (s) = newsect;
dbb410c3
AM
801 }
802 else
803 {
dbb410c3
AM
804 const char *gname;
805
b885599b
AM
806 gname = group_signature (abfd, shdr);
807 if (gname == NULL)
b34976b6 808 return FALSE;
945906ff 809 elf_group_name (newsect) = gname;
dbb410c3
AM
810
811 /* Start a circular list with one element. */
945906ff 812 elf_next_in_group (newsect) = newsect;
dbb410c3 813 }
b885599b 814
9dce4196
AM
815 /* If the group section has been created, point to the
816 new member. */
dbb410c3 817 if (shdr->bfd_section != NULL)
945906ff 818 elf_next_in_group (shdr->bfd_section) = newsect;
b885599b 819
564e11c9
JW
820 elf_tdata (abfd)->group_search_offset = i;
821 j = num_group - 1;
dbb410c3
AM
822 break;
823 }
824 }
825 }
826
945906ff 827 if (elf_group_name (newsect) == NULL)
dbb410c3 828 {
695344c0 829 /* xgettext:c-format */
871b3ab2 830 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
4eca0228 831 abfd, newsect);
493a3386 832 return FALSE;
dbb410c3 833 }
b34976b6 834 return TRUE;
dbb410c3
AM
835}
836
3d7f7666 837bfd_boolean
dd863624 838_bfd_elf_setup_sections (bfd *abfd)
3d7f7666
L
839{
840 unsigned int i;
841 unsigned int num_group = elf_tdata (abfd)->num_group;
842 bfd_boolean result = TRUE;
dd863624
L
843 asection *s;
844
845 /* Process SHF_LINK_ORDER. */
846 for (s = abfd->sections; s != NULL; s = s->next)
847 {
848 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
849 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
850 {
851 unsigned int elfsec = this_hdr->sh_link;
b71702f1
NC
852 /* An sh_link value of 0 is now allowed. It indicates that linked
853 to section has already been discarded, but that the current
854 section has been retained for some other reason. This linking
855 section is still a candidate for later garbage collection
856 however. */
dd863624
L
857 if (elfsec == 0)
858 {
b71702f1 859 elf_linked_to_section (s) = NULL;
dd863624
L
860 }
861 else
862 {
91d6fa6a 863 asection *linksec = NULL;
25bbc984 864
4fbb74a6
AM
865 if (elfsec < elf_numsections (abfd))
866 {
867 this_hdr = elf_elfsections (abfd)[elfsec];
91d6fa6a 868 linksec = this_hdr->bfd_section;
4fbb74a6 869 }
25bbc984
L
870
871 /* PR 1991, 2008:
872 Some strip/objcopy may leave an incorrect value in
873 sh_link. We don't want to proceed. */
91d6fa6a 874 if (linksec == NULL)
25bbc984 875 {
4eca0228 876 _bfd_error_handler
695344c0 877 /* xgettext:c-format */
871b3ab2 878 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
c08bb8dd 879 s->owner, elfsec, s);
25bbc984
L
880 result = FALSE;
881 }
882
91d6fa6a 883 elf_linked_to_section (s) = linksec;
dd863624
L
884 }
885 }
53720c49
AM
886 else if (this_hdr->sh_type == SHT_GROUP
887 && elf_next_in_group (s) == NULL)
888 {
4eca0228 889 _bfd_error_handler
695344c0 890 /* xgettext:c-format */
871b3ab2 891 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
53720c49
AM
892 abfd, elf_section_data (s)->this_idx);
893 result = FALSE;
894 }
dd863624 895 }
3d7f7666 896
dd863624 897 /* Process section groups. */
3d7f7666
L
898 if (num_group == (unsigned) -1)
899 return result;
900
901 for (i = 0; i < num_group; i++)
902 {
903 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
4b0e8a5f
NC
904 Elf_Internal_Group *idx;
905 unsigned int n_elt;
3d7f7666 906
4b0e8a5f
NC
907 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
908 if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
909 {
4eca0228 910 _bfd_error_handler
695344c0 911 /* xgettext:c-format */
871b3ab2 912 (_("%pB: section group entry number %u is corrupt"),
4b0e8a5f
NC
913 abfd, i);
914 result = FALSE;
915 continue;
916 }
917
918 idx = (Elf_Internal_Group *) shdr->contents;
919 n_elt = shdr->sh_size / 4;
1b786873 920
3d7f7666 921 while (--n_elt != 0)
24d3e51b
NC
922 {
923 ++ idx;
924
925 if (idx->shdr == NULL)
926 continue;
927 else if (idx->shdr->bfd_section)
928 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
db4677b8
AM
929 else if (idx->shdr->sh_type != SHT_RELA
930 && idx->shdr->sh_type != SHT_REL)
24d3e51b
NC
931 {
932 /* There are some unknown sections in the group. */
933 _bfd_error_handler
934 /* xgettext:c-format */
871b3ab2 935 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
24d3e51b
NC
936 abfd,
937 idx->shdr->sh_type,
938 bfd_elf_string_from_elf_section (abfd,
939 (elf_elfheader (abfd)
940 ->e_shstrndx),
941 idx->shdr->sh_name),
942 shdr->bfd_section);
943 result = FALSE;
944 }
945 }
3d7f7666 946 }
24d3e51b 947
3d7f7666
L
948 return result;
949}
950
72adc230
AM
951bfd_boolean
952bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
953{
954 return elf_next_in_group (sec) != NULL;
955}
956
cb7f4b29
AM
957const char *
958bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
959{
960 if (elf_sec_group (sec) != NULL)
961 return elf_group_name (sec);
962 return NULL;
963}
964
f6fe1ccd
L
965static char *
966convert_debug_to_zdebug (bfd *abfd, const char *name)
967{
968 unsigned int len = strlen (name);
969 char *new_name = bfd_alloc (abfd, len + 2);
970 if (new_name == NULL)
971 return NULL;
972 new_name[0] = '.';
973 new_name[1] = 'z';
974 memcpy (new_name + 2, name + 1, len);
975 return new_name;
976}
977
978static char *
979convert_zdebug_to_debug (bfd *abfd, const char *name)
980{
981 unsigned int len = strlen (name);
982 char *new_name = bfd_alloc (abfd, len);
983 if (new_name == NULL)
984 return NULL;
985 new_name[0] = '.';
986 memcpy (new_name + 1, name + 2, len - 1);
987 return new_name;
988}
989
cc5277b1
ML
990/* This a copy of lto_section defined in GCC (lto-streamer.h). */
991
992struct lto_section
993{
994 int16_t major_version;
995 int16_t minor_version;
996 unsigned char slim_object;
997
998 /* Flags is a private field that is not defined publicly. */
999 uint16_t flags;
1000};
1001
252b5132
RH
1002/* Make a BFD section from an ELF section. We store a pointer to the
1003 BFD section in the bfd_section field of the header. */
1004
b34976b6 1005bfd_boolean
217aa764
AM
1006_bfd_elf_make_section_from_shdr (bfd *abfd,
1007 Elf_Internal_Shdr *hdr,
6dc132d9
L
1008 const char *name,
1009 int shindex)
252b5132
RH
1010{
1011 asection *newsect;
1012 flagword flags;
9c5bfbb7 1013 const struct elf_backend_data *bed;
502794d4 1014 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132
RH
1015
1016 if (hdr->bfd_section != NULL)
4e011fb5 1017 return TRUE;
252b5132
RH
1018
1019 newsect = bfd_make_section_anyway (abfd, name);
1020 if (newsect == NULL)
b34976b6 1021 return FALSE;
252b5132 1022
1829f4b2
AM
1023 hdr->bfd_section = newsect;
1024 elf_section_data (newsect)->this_hdr = *hdr;
6dc132d9 1025 elf_section_data (newsect)->this_idx = shindex;
1829f4b2 1026
2f89ff8d
L
1027 /* Always use the real type/flags. */
1028 elf_section_type (newsect) = hdr->sh_type;
1029 elf_section_flags (newsect) = hdr->sh_flags;
1030
252b5132
RH
1031 newsect->filepos = hdr->sh_offset;
1032
252b5132
RH
1033 flags = SEC_NO_FLAGS;
1034 if (hdr->sh_type != SHT_NOBITS)
1035 flags |= SEC_HAS_CONTENTS;
dbb410c3 1036 if (hdr->sh_type == SHT_GROUP)
7bdf4127 1037 flags |= SEC_GROUP;
252b5132
RH
1038 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1039 {
1040 flags |= SEC_ALLOC;
1041 if (hdr->sh_type != SHT_NOBITS)
1042 flags |= SEC_LOAD;
1043 }
1044 if ((hdr->sh_flags & SHF_WRITE) == 0)
1045 flags |= SEC_READONLY;
1046 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1047 flags |= SEC_CODE;
1048 else if ((flags & SEC_LOAD) != 0)
1049 flags |= SEC_DATA;
f5fa8ca2
JJ
1050 if ((hdr->sh_flags & SHF_MERGE) != 0)
1051 {
1052 flags |= SEC_MERGE;
1053 newsect->entsize = hdr->sh_entsize;
f5fa8ca2 1054 }
84865015
NC
1055 if ((hdr->sh_flags & SHF_STRINGS) != 0)
1056 flags |= SEC_STRINGS;
dbb410c3
AM
1057 if (hdr->sh_flags & SHF_GROUP)
1058 if (!setup_group (abfd, hdr, newsect))
b34976b6 1059 return FALSE;
13ae64f3
JJ
1060 if ((hdr->sh_flags & SHF_TLS) != 0)
1061 flags |= SEC_THREAD_LOCAL;
18ae9cc1
L
1062 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1063 flags |= SEC_EXCLUDE;
252b5132 1064
df3a023b
AM
1065 switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
1066 {
1067 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1068 but binutils as of 2019-07-23 did not set the EI_OSABI header
1069 byte. */
df3a023b
AM
1070 case ELFOSABI_GNU:
1071 case ELFOSABI_FREEBSD:
99fabbc9
JL
1072 if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
1073 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
1074 /* Fall through */
1075 case ELFOSABI_NONE:
df3a023b
AM
1076 if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
1077 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1078 break;
1079 }
1080
3d2b39cf 1081 if ((flags & SEC_ALLOC) == 0)
7a6cc5fb 1082 {
3d2b39cf
L
1083 /* The debugging sections appear to be recognized only by name,
1084 not any sort of flag. Their SEC_ALLOC bits are cleared. */
3d2b39cf
L
1085 if (name [0] == '.')
1086 {
bb294208
AM
1087 if (strncmp (name, ".debug", 6) == 0
1088 || strncmp (name, ".gnu.linkonce.wi.", 17) == 0
1089 || strncmp (name, ".zdebug", 7) == 0)
1090 flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
1091 else if (strncmp (name, GNU_BUILD_ATTRS_SECTION_NAME, 21) == 0
1092 || strncmp (name, ".note.gnu", 9) == 0)
502794d4
CE
1093 {
1094 flags |= SEC_ELF_OCTETS;
1095 opb = 1;
1096 }
bb294208
AM
1097 else if (strncmp (name, ".line", 5) == 0
1098 || strncmp (name, ".stab", 5) == 0
1099 || strcmp (name, ".gdb_index") == 0)
3d2b39cf
L
1100 flags |= SEC_DEBUGGING;
1101 }
1102 }
252b5132 1103
502794d4
CE
1104 if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
1105 || !bfd_set_section_size (newsect, hdr->sh_size)
1106 || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign)))
1107 return FALSE;
1108
252b5132
RH
1109 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1110 only link a single copy of the section. This is used to support
1111 g++. g++ will emit each template expansion in its own section.
1112 The symbols will be defined as weak, so that multiple definitions
1113 are permitted. The GNU linker extension is to actually discard
1114 all but one of the sections. */
0112cd26 1115 if (CONST_STRNEQ (name, ".gnu.linkonce")
b885599b 1116 && elf_next_in_group (newsect) == NULL)
252b5132
RH
1117 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1118
8c803a2d
AM
1119 if (!bfd_set_section_flags (newsect, flags))
1120 return FALSE;
1121
fa152c49
JW
1122 bed = get_elf_backend_data (abfd);
1123 if (bed->elf_backend_section_flags)
8c803a2d 1124 if (!bed->elf_backend_section_flags (hdr))
b34976b6 1125 return FALSE;
fa152c49 1126
718175fa
JK
1127 /* We do not parse the PT_NOTE segments as we are interested even in the
1128 separate debug info files which may have the segments offsets corrupted.
1129 PT_NOTEs from the core files are currently not parsed using BFD. */
1130 if (hdr->sh_type == SHT_NOTE)
1131 {
baea7ef1 1132 bfd_byte *contents;
718175fa 1133
baea7ef1 1134 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
718175fa
JK
1135 return FALSE;
1136
276da9b3
L
1137 elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1138 hdr->sh_offset, hdr->sh_addralign);
718175fa
JK
1139 free (contents);
1140 }
1141
8c803a2d 1142 if ((newsect->flags & SEC_ALLOC) != 0)
252b5132
RH
1143 {
1144 Elf_Internal_Phdr *phdr;
6ffd7900
AM
1145 unsigned int i, nload;
1146
1147 /* Some ELF linkers produce binaries with all the program header
1148 p_paddr fields zero. If we have such a binary with more than
1149 one PT_LOAD header, then leave the section lma equal to vma
1150 so that we don't create sections with overlapping lma. */
1151 phdr = elf_tdata (abfd)->phdr;
1152 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1153 if (phdr->p_paddr != 0)
1154 break;
1155 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1156 ++nload;
1157 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1158 return TRUE;
252b5132 1159
252b5132
RH
1160 phdr = elf_tdata (abfd)->phdr;
1161 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1162 {
86b2281f
AM
1163 if (((phdr->p_type == PT_LOAD
1164 && (hdr->sh_flags & SHF_TLS) == 0)
1165 || phdr->p_type == PT_TLS)
9a83a553 1166 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
252b5132 1167 {
8c803a2d 1168 if ((newsect->flags & SEC_LOAD) == 0)
88967714 1169 newsect->lma = (phdr->p_paddr
502794d4 1170 + hdr->sh_addr - phdr->p_vaddr) / opb;
88967714
AM
1171 else
1172 /* We used to use the same adjustment for SEC_LOAD
1173 sections, but that doesn't work if the segment
1174 is packed with code from multiple VMAs.
1175 Instead we calculate the section LMA based on
1176 the segment LMA. It is assumed that the
1177 segment will contain sections with contiguous
1178 LMAs, even if the VMAs are not. */
1179 newsect->lma = (phdr->p_paddr
502794d4 1180 + hdr->sh_offset - phdr->p_offset) / opb;
88967714
AM
1181
1182 /* With contiguous segments, we can't tell from file
1183 offsets whether a section with zero size should
1184 be placed at the end of one segment or the
1185 beginning of the next. Decide based on vaddr. */
1186 if (hdr->sh_addr >= phdr->p_vaddr
1187 && (hdr->sh_addr + hdr->sh_size
1188 <= phdr->p_vaddr + phdr->p_memsz))
1189 break;
252b5132
RH
1190 }
1191 }
1192 }
1193
4a114e3e
L
1194 /* Compress/decompress DWARF debug sections with names: .debug_* and
1195 .zdebug_*, after the section flags is set. */
8c803a2d 1196 if ((newsect->flags & SEC_DEBUGGING)
4a114e3e
L
1197 && ((name[1] == 'd' && name[6] == '_')
1198 || (name[1] == 'z' && name[7] == '_')))
1199 {
1200 enum { nothing, compress, decompress } action = nothing;
151411f8 1201 int compression_header_size;
dab394de 1202 bfd_size_type uncompressed_size;
4207142d 1203 unsigned int uncompressed_align_power;
151411f8
L
1204 bfd_boolean compressed
1205 = bfd_is_section_compressed_with_header (abfd, newsect,
dab394de 1206 &compression_header_size,
4207142d
MW
1207 &uncompressed_size,
1208 &uncompressed_align_power);
151411f8 1209 if (compressed)
4a114e3e
L
1210 {
1211 /* Compressed section. Check if we should decompress. */
1212 if ((abfd->flags & BFD_DECOMPRESS))
1213 action = decompress;
1214 }
151411f8
L
1215
1216 /* Compress the uncompressed section or convert from/to .zdebug*
1217 section. Check if we should compress. */
1218 if (action == nothing)
4a114e3e 1219 {
151411f8
L
1220 if (newsect->size != 0
1221 && (abfd->flags & BFD_COMPRESS)
1222 && compression_header_size >= 0
dab394de 1223 && uncompressed_size > 0
151411f8
L
1224 && (!compressed
1225 || ((compression_header_size > 0)
1226 != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
4a114e3e 1227 action = compress;
151411f8
L
1228 else
1229 return TRUE;
4a114e3e
L
1230 }
1231
151411f8 1232 if (action == compress)
4a114e3e 1233 {
4a114e3e
L
1234 if (!bfd_init_section_compress_status (abfd, newsect))
1235 {
4eca0228 1236 _bfd_error_handler
695344c0 1237 /* xgettext:c-format */
871b3ab2 1238 (_("%pB: unable to initialize compress status for section %s"),
4a114e3e
L
1239 abfd, name);
1240 return FALSE;
1241 }
151411f8
L
1242 }
1243 else
1244 {
4a114e3e
L
1245 if (!bfd_init_section_decompress_status (abfd, newsect))
1246 {
4eca0228 1247 _bfd_error_handler
695344c0 1248 /* xgettext:c-format */
871b3ab2 1249 (_("%pB: unable to initialize decompress status for section %s"),
4a114e3e
L
1250 abfd, name);
1251 return FALSE;
1252 }
151411f8
L
1253 }
1254
f6fe1ccd 1255 if (abfd->is_linker_input)
151411f8 1256 {
f6fe1ccd
L
1257 if (name[1] == 'z'
1258 && (action == decompress
1259 || (action == compress
1260 && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
4e011fb5 1261 {
f6fe1ccd
L
1262 /* Convert section name from .zdebug_* to .debug_* so
1263 that linker will consider this section as a debug
1264 section. */
1265 char *new_name = convert_zdebug_to_debug (abfd, name);
151411f8
L
1266 if (new_name == NULL)
1267 return FALSE;
fd361982 1268 bfd_rename_section (newsect, new_name);
151411f8 1269 }
4a114e3e 1270 }
f6fe1ccd
L
1271 else
1272 /* For objdump, don't rename the section. For objcopy, delay
1273 section rename to elf_fake_sections. */
1274 newsect->flags |= SEC_ELF_RENAME;
4a114e3e
L
1275 }
1276
cc5277b1
ML
1277 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1278 section. */
1279 const char *lto_section_name = ".gnu.lto_.lto.";
1280 if (strncmp (name, lto_section_name, strlen (lto_section_name)) == 0)
1281 {
1282 struct lto_section lsection;
1283 if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
1284 sizeof (struct lto_section)))
1285 abfd->lto_slim_object = lsection.slim_object;
1286 }
1287
b34976b6 1288 return TRUE;
252b5132
RH
1289}
1290
84865015
NC
1291const char *const bfd_elf_section_type_names[] =
1292{
252b5132
RH
1293 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1294 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1295 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1296};
1297
1049f94e 1298/* ELF relocs are against symbols. If we are producing relocatable
252b5132
RH
1299 output, and the reloc is against an external symbol, and nothing
1300 has given us any additional addend, the resulting reloc will also
1301 be against the same symbol. In such a case, we don't want to
1302 change anything about the way the reloc is handled, since it will
1303 all be done at final link time. Rather than put special case code
1304 into bfd_perform_relocation, all the reloc types use this howto
1305 function. It just short circuits the reloc if producing
1049f94e 1306 relocatable output against an external symbol. */
252b5132 1307
252b5132 1308bfd_reloc_status_type
217aa764
AM
1309bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1310 arelent *reloc_entry,
1311 asymbol *symbol,
1312 void *data ATTRIBUTE_UNUSED,
1313 asection *input_section,
1314 bfd *output_bfd,
1315 char **error_message ATTRIBUTE_UNUSED)
1316{
1317 if (output_bfd != NULL
252b5132
RH
1318 && (symbol->flags & BSF_SECTION_SYM) == 0
1319 && (! reloc_entry->howto->partial_inplace
1320 || reloc_entry->addend == 0))
1321 {
1322 reloc_entry->address += input_section->output_offset;
1323 return bfd_reloc_ok;
1324 }
1325
1326 return bfd_reloc_continue;
1327}
1328\f
84865015
NC
1329/* Returns TRUE if section A matches section B.
1330 Names, addresses and links may be different, but everything else
1331 should be the same. */
1332
1333static bfd_boolean
5522f910
NC
1334section_match (const Elf_Internal_Shdr * a,
1335 const Elf_Internal_Shdr * b)
84865015 1336{
ac85e67c
AM
1337 if (a->sh_type != b->sh_type
1338 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1339 || a->sh_addralign != b->sh_addralign
1340 || a->sh_entsize != b->sh_entsize)
1341 return FALSE;
1342 if (a->sh_type == SHT_SYMTAB
1343 || a->sh_type == SHT_STRTAB)
1344 return TRUE;
1345 return a->sh_size == b->sh_size;
84865015
NC
1346}
1347
1348/* Find a section in OBFD that has the same characteristics
1349 as IHEADER. Return the index of this section or SHN_UNDEF if
1350 none can be found. Check's section HINT first, as this is likely
1351 to be the correct section. */
1352
1353static unsigned int
5cc4ca83
ST
1354find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1355 const unsigned int hint)
84865015
NC
1356{
1357 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1358 unsigned int i;
1359
a55c9876
NC
1360 BFD_ASSERT (iheader != NULL);
1361
1362 /* See PR 20922 for a reproducer of the NULL test. */
5cc4ca83
ST
1363 if (hint < elf_numsections (obfd)
1364 && oheaders[hint] != NULL
a55c9876 1365 && section_match (oheaders[hint], iheader))
84865015
NC
1366 return hint;
1367
1368 for (i = 1; i < elf_numsections (obfd); i++)
1369 {
1370 Elf_Internal_Shdr * oheader = oheaders[i];
1371
a55c9876
NC
1372 if (oheader == NULL)
1373 continue;
84865015
NC
1374 if (section_match (oheader, iheader))
1375 /* FIXME: Do we care if there is a potential for
1376 multiple matches ? */
1377 return i;
1378 }
1379
1380 return SHN_UNDEF;
1381}
1382
5522f910
NC
1383/* PR 19938: Attempt to set the ELF section header fields of an OS or
1384 Processor specific section, based upon a matching input section.
1385 Returns TRUE upon success, FALSE otherwise. */
07d6d2b8 1386
5522f910
NC
1387static bfd_boolean
1388copy_special_section_fields (const bfd *ibfd,
1389 bfd *obfd,
1390 const Elf_Internal_Shdr *iheader,
1391 Elf_Internal_Shdr *oheader,
1392 const unsigned int secnum)
1393{
1394 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1395 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1396 bfd_boolean changed = FALSE;
1397 unsigned int sh_link;
1398
1399 if (oheader->sh_type == SHT_NOBITS)
1400 {
1401 /* This is a feature for objcopy --only-keep-debug:
1402 When a section's type is changed to NOBITS, we preserve
1403 the sh_link and sh_info fields so that they can be
1404 matched up with the original.
1405
1406 Note: Strictly speaking these assignments are wrong.
1407 The sh_link and sh_info fields should point to the
1408 relevent sections in the output BFD, which may not be in
1409 the same location as they were in the input BFD. But
1410 the whole point of this action is to preserve the
1411 original values of the sh_link and sh_info fields, so
1412 that they can be matched up with the section headers in
1413 the original file. So strictly speaking we may be
1414 creating an invalid ELF file, but it is only for a file
1415 that just contains debug info and only for sections
1416 without any contents. */
1417 if (oheader->sh_link == 0)
1418 oheader->sh_link = iheader->sh_link;
1419 if (oheader->sh_info == 0)
1420 oheader->sh_info = iheader->sh_info;
1421 return TRUE;
1422 }
1423
1424 /* Allow the target a chance to decide how these fields should be set. */
a859124d
AM
1425 if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1426 iheader, oheader))
5522f910
NC
1427 return TRUE;
1428
1429 /* We have an iheader which might match oheader, and which has non-zero
1430 sh_info and/or sh_link fields. Attempt to follow those links and find
1431 the section in the output bfd which corresponds to the linked section
1432 in the input bfd. */
1433 if (iheader->sh_link != SHN_UNDEF)
1434 {
4f3ca05b
NC
1435 /* See PR 20931 for a reproducer. */
1436 if (iheader->sh_link >= elf_numsections (ibfd))
1437 {
76cfced5 1438 _bfd_error_handler
4f3ca05b 1439 /* xgettext:c-format */
9793eb77 1440 (_("%pB: invalid sh_link field (%d) in section number %d"),
4f3ca05b
NC
1441 ibfd, iheader->sh_link, secnum);
1442 return FALSE;
1443 }
1444
5522f910
NC
1445 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1446 if (sh_link != SHN_UNDEF)
1447 {
1448 oheader->sh_link = sh_link;
1449 changed = TRUE;
1450 }
1451 else
1452 /* FIXME: Should we install iheader->sh_link
1453 if we could not find a match ? */
76cfced5 1454 _bfd_error_handler
695344c0 1455 /* xgettext:c-format */
9793eb77 1456 (_("%pB: failed to find link section for section %d"), obfd, secnum);
5522f910
NC
1457 }
1458
1459 if (iheader->sh_info)
1460 {
1461 /* The sh_info field can hold arbitrary information, but if the
1462 SHF_LINK_INFO flag is set then it should be interpreted as a
1463 section index. */
1464 if (iheader->sh_flags & SHF_INFO_LINK)
1465 {
1466 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1467 iheader->sh_info);
1468 if (sh_link != SHN_UNDEF)
1469 oheader->sh_flags |= SHF_INFO_LINK;
1470 }
1471 else
1472 /* No idea what it means - just copy it. */
1473 sh_link = iheader->sh_info;
1474
1475 if (sh_link != SHN_UNDEF)
1476 {
1477 oheader->sh_info = sh_link;
1478 changed = TRUE;
1479 }
1480 else
76cfced5 1481 _bfd_error_handler
695344c0 1482 /* xgettext:c-format */
9793eb77 1483 (_("%pB: failed to find info section for section %d"), obfd, secnum);
5522f910
NC
1484 }
1485
1486 return changed;
1487}
07d6d2b8 1488
0ac4564e
L
1489/* Copy the program header and other data from one object module to
1490 another. */
252b5132 1491
b34976b6 1492bfd_boolean
217aa764 1493_bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
2d502050 1494{
5522f910
NC
1495 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1496 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1497 const struct elf_backend_data *bed;
84865015
NC
1498 unsigned int i;
1499
2d502050 1500 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
84865015 1501 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 1502 return TRUE;
2d502050 1503
57b828ef
L
1504 if (!elf_flags_init (obfd))
1505 {
1506 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1507 elf_flags_init (obfd) = TRUE;
1508 }
2d502050 1509
0ac4564e 1510 elf_gp (obfd) = elf_gp (ibfd);
57b828ef
L
1511
1512 /* Also copy the EI_OSABI field. */
1513 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1514 elf_elfheader (ibfd)->e_ident[EI_OSABI];
104d59d1 1515
5522f910
NC
1516 /* If set, copy the EI_ABIVERSION field. */
1517 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1518 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1519 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
07d6d2b8 1520
104d59d1
JM
1521 /* Copy object attributes. */
1522 _bfd_elf_copy_obj_attributes (ibfd, obfd);
63b9bbb7 1523
84865015
NC
1524 if (iheaders == NULL || oheaders == NULL)
1525 return TRUE;
63b9bbb7 1526
5522f910
NC
1527 bed = get_elf_backend_data (obfd);
1528
1529 /* Possibly copy other fields in the section header. */
84865015 1530 for (i = 1; i < elf_numsections (obfd); i++)
63b9bbb7 1531 {
84865015
NC
1532 unsigned int j;
1533 Elf_Internal_Shdr * oheader = oheaders[i];
63b9bbb7 1534
5522f910
NC
1535 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1536 because of a special case need for generating separate debug info
1537 files. See below for more details. */
84865015
NC
1538 if (oheader == NULL
1539 || (oheader->sh_type != SHT_NOBITS
5522f910
NC
1540 && oheader->sh_type < SHT_LOOS))
1541 continue;
1542
1543 /* Ignore empty sections, and sections whose
1544 fields have already been initialised. */
1545 if (oheader->sh_size == 0
84865015
NC
1546 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1547 continue;
63b9bbb7 1548
84865015 1549 /* Scan for the matching section in the input bfd.
5522f910
NC
1550 First we try for a direct mapping between the input and output sections. */
1551 for (j = 1; j < elf_numsections (ibfd); j++)
1552 {
1553 const Elf_Internal_Shdr * iheader = iheaders[j];
1554
1555 if (iheader == NULL)
1556 continue;
1557
1558 if (oheader->bfd_section != NULL
1559 && iheader->bfd_section != NULL
1560 && iheader->bfd_section->output_section != NULL
1561 && iheader->bfd_section->output_section == oheader->bfd_section)
1562 {
1563 /* We have found a connection from the input section to the
1564 output section. Attempt to copy the header fields. If
1565 this fails then do not try any further sections - there
1566 should only be a one-to-one mapping between input and output. */
1567 if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1568 j = elf_numsections (ibfd);
1569 break;
1570 }
1571 }
1572
1573 if (j < elf_numsections (ibfd))
1574 continue;
1575
1576 /* That failed. So try to deduce the corresponding input section.
84865015
NC
1577 Unfortunately we cannot compare names as the output string table
1578 is empty, so instead we check size, address and type. */
1579 for (j = 1; j < elf_numsections (ibfd); j++)
1580 {
5522f910 1581 const Elf_Internal_Shdr * iheader = iheaders[j];
84865015 1582
5522f910
NC
1583 if (iheader == NULL)
1584 continue;
1585
1586 /* Try matching fields in the input section's header.
1587 Since --only-keep-debug turns all non-debug sections into
84865015
NC
1588 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1589 input type. */
1590 if ((oheader->sh_type == SHT_NOBITS
1591 || iheader->sh_type == oheader->sh_type)
5522f910
NC
1592 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1593 == (oheader->sh_flags & ~ SHF_INFO_LINK)
84865015
NC
1594 && iheader->sh_addralign == oheader->sh_addralign
1595 && iheader->sh_entsize == oheader->sh_entsize
1596 && iheader->sh_size == oheader->sh_size
1597 && iheader->sh_addr == oheader->sh_addr
1598 && (iheader->sh_info != oheader->sh_info
1599 || iheader->sh_link != oheader->sh_link))
63b9bbb7 1600 {
5522f910
NC
1601 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1602 break;
63b9bbb7
NC
1603 }
1604 }
5522f910
NC
1605
1606 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1607 {
1608 /* Final attempt. Call the backend copy function
1609 with a NULL input section. */
a859124d
AM
1610 (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1611 NULL, oheader);
5522f910 1612 }
63b9bbb7
NC
1613 }
1614
b34976b6 1615 return TRUE;
2d502050
L
1616}
1617
cedc298e
L
1618static const char *
1619get_segment_type (unsigned int p_type)
1620{
1621 const char *pt;
1622 switch (p_type)
1623 {
1624 case PT_NULL: pt = "NULL"; break;
1625 case PT_LOAD: pt = "LOAD"; break;
1626 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1627 case PT_INTERP: pt = "INTERP"; break;
1628 case PT_NOTE: pt = "NOTE"; break;
1629 case PT_SHLIB: pt = "SHLIB"; break;
1630 case PT_PHDR: pt = "PHDR"; break;
1631 case PT_TLS: pt = "TLS"; break;
1632 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
2b05f1b7 1633 case PT_GNU_STACK: pt = "STACK"; break;
cedc298e
L
1634 case PT_GNU_RELRO: pt = "RELRO"; break;
1635 default: pt = NULL; break;
1636 }
1637 return pt;
1638}
1639
f0b79d91
L
1640/* Print out the program headers. */
1641
b34976b6 1642bfd_boolean
217aa764 1643_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
252b5132 1644{
a50b1753 1645 FILE *f = (FILE *) farg;
252b5132
RH
1646 Elf_Internal_Phdr *p;
1647 asection *s;
1648 bfd_byte *dynbuf = NULL;
1649
1650 p = elf_tdata (abfd)->phdr;
1651 if (p != NULL)
1652 {
1653 unsigned int i, c;
1654
1655 fprintf (f, _("\nProgram Header:\n"));
1656 c = elf_elfheader (abfd)->e_phnum;
1657 for (i = 0; i < c; i++, p++)
1658 {
cedc298e 1659 const char *pt = get_segment_type (p->p_type);
252b5132
RH
1660 char buf[20];
1661
cedc298e 1662 if (pt == NULL)
252b5132 1663 {
cedc298e
L
1664 sprintf (buf, "0x%lx", p->p_type);
1665 pt = buf;
252b5132 1666 }
dc810e39 1667 fprintf (f, "%8s off 0x", pt);
60b89a18 1668 bfd_fprintf_vma (abfd, f, p->p_offset);
252b5132 1669 fprintf (f, " vaddr 0x");
60b89a18 1670 bfd_fprintf_vma (abfd, f, p->p_vaddr);
252b5132 1671 fprintf (f, " paddr 0x");
60b89a18 1672 bfd_fprintf_vma (abfd, f, p->p_paddr);
252b5132
RH
1673 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1674 fprintf (f, " filesz 0x");
60b89a18 1675 bfd_fprintf_vma (abfd, f, p->p_filesz);
252b5132 1676 fprintf (f, " memsz 0x");
60b89a18 1677 bfd_fprintf_vma (abfd, f, p->p_memsz);
252b5132
RH
1678 fprintf (f, " flags %c%c%c",
1679 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1680 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1681 (p->p_flags & PF_X) != 0 ? 'x' : '-');
dc810e39
AM
1682 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1683 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
252b5132
RH
1684 fprintf (f, "\n");
1685 }
1686 }
1687
1688 s = bfd_get_section_by_name (abfd, ".dynamic");
1689 if (s != NULL)
1690 {
cb33740c 1691 unsigned int elfsec;
dc810e39 1692 unsigned long shlink;
252b5132
RH
1693 bfd_byte *extdyn, *extdynend;
1694 size_t extdynsize;
217aa764 1695 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
252b5132
RH
1696
1697 fprintf (f, _("\nDynamic Section:\n"));
1698
eea6121a 1699 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
252b5132
RH
1700 goto error_return;
1701
1702 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 1703 if (elfsec == SHN_BAD)
252b5132 1704 goto error_return;
dc810e39 1705 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
252b5132
RH
1706
1707 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1708 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1709
1710 extdyn = dynbuf;
06614111
NC
1711 /* PR 17512: file: 6f427532. */
1712 if (s->size < extdynsize)
1713 goto error_return;
eea6121a 1714 extdynend = extdyn + s->size;
1036838a 1715 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
07d6d2b8 1716 Fix range check. */
1036838a 1717 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
252b5132
RH
1718 {
1719 Elf_Internal_Dyn dyn;
ad9563d6 1720 const char *name = "";
252b5132 1721 char ab[20];
b34976b6 1722 bfd_boolean stringp;
ad9563d6 1723 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 1724
217aa764 1725 (*swap_dyn_in) (abfd, extdyn, &dyn);
252b5132
RH
1726
1727 if (dyn.d_tag == DT_NULL)
1728 break;
1729
b34976b6 1730 stringp = FALSE;
252b5132
RH
1731 switch (dyn.d_tag)
1732 {
1733 default:
ad9563d6
CM
1734 if (bed->elf_backend_get_target_dtag)
1735 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1736
1737 if (!strcmp (name, ""))
1738 {
cd9af601 1739 sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag);
ad9563d6
CM
1740 name = ab;
1741 }
252b5132
RH
1742 break;
1743
b34976b6 1744 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
252b5132
RH
1745 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1746 case DT_PLTGOT: name = "PLTGOT"; break;
1747 case DT_HASH: name = "HASH"; break;
1748 case DT_STRTAB: name = "STRTAB"; break;
1749 case DT_SYMTAB: name = "SYMTAB"; break;
1750 case DT_RELA: name = "RELA"; break;
1751 case DT_RELASZ: name = "RELASZ"; break;
1752 case DT_RELAENT: name = "RELAENT"; break;
1753 case DT_STRSZ: name = "STRSZ"; break;
1754 case DT_SYMENT: name = "SYMENT"; break;
1755 case DT_INIT: name = "INIT"; break;
1756 case DT_FINI: name = "FINI"; break;
b34976b6
AM
1757 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1758 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
252b5132
RH
1759 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1760 case DT_REL: name = "REL"; break;
1761 case DT_RELSZ: name = "RELSZ"; break;
1762 case DT_RELENT: name = "RELENT"; break;
1763 case DT_PLTREL: name = "PLTREL"; break;
1764 case DT_DEBUG: name = "DEBUG"; break;
1765 case DT_TEXTREL: name = "TEXTREL"; break;
1766 case DT_JMPREL: name = "JMPREL"; break;
94558834
L
1767 case DT_BIND_NOW: name = "BIND_NOW"; break;
1768 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1769 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1770 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1771 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
b34976b6 1772 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
94558834
L
1773 case DT_FLAGS: name = "FLAGS"; break;
1774 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1775 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
d48188b9 1776 case DT_CHECKSUM: name = "CHECKSUM"; break;
94558834
L
1777 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1778 case DT_MOVEENT: name = "MOVEENT"; break;
1779 case DT_MOVESZ: name = "MOVESZ"; break;
1780 case DT_FEATURE: name = "FEATURE"; break;
1781 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1782 case DT_SYMINSZ: name = "SYMINSZ"; break;
1783 case DT_SYMINENT: name = "SYMINENT"; break;
b34976b6
AM
1784 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1785 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1786 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
94558834
L
1787 case DT_PLTPAD: name = "PLTPAD"; break;
1788 case DT_MOVETAB: name = "MOVETAB"; break;
1789 case DT_SYMINFO: name = "SYMINFO"; break;
1790 case DT_RELACOUNT: name = "RELACOUNT"; break;
1791 case DT_RELCOUNT: name = "RELCOUNT"; break;
1792 case DT_FLAGS_1: name = "FLAGS_1"; break;
252b5132
RH
1793 case DT_VERSYM: name = "VERSYM"; break;
1794 case DT_VERDEF: name = "VERDEF"; break;
1795 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1796 case DT_VERNEED: name = "VERNEED"; break;
1797 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
b34976b6 1798 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
94558834 1799 case DT_USED: name = "USED"; break;
b34976b6 1800 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
fdc90cb4 1801 case DT_GNU_HASH: name = "GNU_HASH"; break;
252b5132
RH
1802 }
1803
ad9563d6 1804 fprintf (f, " %-20s ", name);
252b5132 1805 if (! stringp)
a1f3c56e
AN
1806 {
1807 fprintf (f, "0x");
1808 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1809 }
252b5132
RH
1810 else
1811 {
1812 const char *string;
dc810e39 1813 unsigned int tagv = dyn.d_un.d_val;
252b5132 1814
dc810e39 1815 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
252b5132
RH
1816 if (string == NULL)
1817 goto error_return;
1818 fprintf (f, "%s", string);
1819 }
1820 fprintf (f, "\n");
1821 }
1822
1823 free (dynbuf);
1824 dynbuf = NULL;
1825 }
1826
1827 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1828 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1829 {
fc0e6df6 1830 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
b34976b6 1831 return FALSE;
252b5132
RH
1832 }
1833
1834 if (elf_dynverdef (abfd) != 0)
1835 {
1836 Elf_Internal_Verdef *t;
1837
1838 fprintf (f, _("\nVersion definitions:\n"));
1839 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1840 {
1841 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
d0fb9a8d
JJ
1842 t->vd_flags, t->vd_hash,
1843 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1844 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
252b5132
RH
1845 {
1846 Elf_Internal_Verdaux *a;
1847
1848 fprintf (f, "\t");
1849 for (a = t->vd_auxptr->vda_nextptr;
1850 a != NULL;
1851 a = a->vda_nextptr)
d0fb9a8d
JJ
1852 fprintf (f, "%s ",
1853 a->vda_nodename ? a->vda_nodename : "<corrupt>");
252b5132
RH
1854 fprintf (f, "\n");
1855 }
1856 }
1857 }
1858
1859 if (elf_dynverref (abfd) != 0)
1860 {
1861 Elf_Internal_Verneed *t;
1862
1863 fprintf (f, _("\nVersion References:\n"));
1864 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1865 {
1866 Elf_Internal_Vernaux *a;
1867
d0fb9a8d
JJ
1868 fprintf (f, _(" required from %s:\n"),
1869 t->vn_filename ? t->vn_filename : "<corrupt>");
252b5132
RH
1870 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1871 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
d0fb9a8d
JJ
1872 a->vna_flags, a->vna_other,
1873 a->vna_nodename ? a->vna_nodename : "<corrupt>");
252b5132
RH
1874 }
1875 }
1876
b34976b6 1877 return TRUE;
252b5132
RH
1878
1879 error_return:
c9594989 1880 free (dynbuf);
b34976b6 1881 return FALSE;
252b5132
RH
1882}
1883
7e6e972f
L
1884/* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
1885 and return symbol version for symbol version itself. */
bb4d2ac2
L
1886
1887const char *
1081065c
L
1888_bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
1889 bfd_boolean base_p,
1890 bfd_boolean *hidden)
bb4d2ac2
L
1891{
1892 const char *version_string = NULL;
1893 if (elf_dynversym (abfd) != 0
1894 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1895 {
1896 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1897
1898 *hidden = (vernum & VERSYM_HIDDEN) != 0;
1899 vernum &= VERSYM_VERSION;
1900
1901 if (vernum == 0)
1902 version_string = "";
1f6f5dba
L
1903 else if (vernum == 1
1904 && (vernum > elf_tdata (abfd)->cverdefs
1905 || (elf_tdata (abfd)->verdef[0].vd_flags
1906 == VER_FLG_BASE)))
7e6e972f 1907 version_string = base_p ? "Base" : "";
bb4d2ac2 1908 else if (vernum <= elf_tdata (abfd)->cverdefs)
7e6e972f
L
1909 {
1910 const char *nodename
1911 = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
8d55d10a
AM
1912 version_string = "";
1913 if (base_p
1914 || nodename == NULL
1915 || symbol->name == NULL
1916 || strcmp (symbol->name, nodename) != 0)
1917 version_string = nodename;
7e6e972f 1918 }
bb4d2ac2
L
1919 else
1920 {
1921 Elf_Internal_Verneed *t;
1922
7a815dd5 1923 version_string = _("<corrupt>");
bb4d2ac2
L
1924 for (t = elf_tdata (abfd)->verref;
1925 t != NULL;
1926 t = t->vn_nextref)
1927 {
1928 Elf_Internal_Vernaux *a;
1929
1930 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1931 {
1932 if (a->vna_other == vernum)
1933 {
1934 version_string = a->vna_nodename;
1935 break;
1936 }
1937 }
1938 }
1939 }
1940 }
1941 return version_string;
1942}
1943
252b5132
RH
1944/* Display ELF-specific fields of a symbol. */
1945
1946void
217aa764
AM
1947bfd_elf_print_symbol (bfd *abfd,
1948 void *filep,
1949 asymbol *symbol,
1950 bfd_print_symbol_type how)
252b5132 1951{
a50b1753 1952 FILE *file = (FILE *) filep;
252b5132
RH
1953 switch (how)
1954 {
1955 case bfd_print_symbol_name:
1956 fprintf (file, "%s", symbol->name);
1957 break;
1958 case bfd_print_symbol_more:
1959 fprintf (file, "elf ");
60b89a18 1960 bfd_fprintf_vma (abfd, file, symbol->value);
cd9af601 1961 fprintf (file, " %x", symbol->flags);
252b5132
RH
1962 break;
1963 case bfd_print_symbol_all:
1964 {
4e8a9624
AM
1965 const char *section_name;
1966 const char *name = NULL;
9c5bfbb7 1967 const struct elf_backend_data *bed;
7a13edea 1968 unsigned char st_other;
dbb410c3 1969 bfd_vma val;
bb4d2ac2
L
1970 const char *version_string;
1971 bfd_boolean hidden;
c044fabd 1972
252b5132 1973 section_name = symbol->section ? symbol->section->name : "(*none*)";
587ff49e
RH
1974
1975 bed = get_elf_backend_data (abfd);
1976 if (bed->elf_backend_print_symbol_all)
c044fabd 1977 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
587ff49e
RH
1978
1979 if (name == NULL)
1980 {
7ee38065 1981 name = symbol->name;
217aa764 1982 bfd_print_symbol_vandf (abfd, file, symbol);
587ff49e
RH
1983 }
1984
252b5132
RH
1985 fprintf (file, " %s\t", section_name);
1986 /* Print the "other" value for a symbol. For common symbols,
1987 we've already printed the size; now print the alignment.
1988 For other symbols, we have no specified alignment, and
1989 we've printed the address; now print the size. */
dcf6c779 1990 if (symbol->section && bfd_is_com_section (symbol->section))
dbb410c3
AM
1991 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1992 else
1993 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1994 bfd_fprintf_vma (abfd, file, val);
252b5132
RH
1995
1996 /* If we have version information, print it. */
60bb06bc
L
1997 version_string = _bfd_elf_get_symbol_version_string (abfd,
1998 symbol,
1081065c 1999 TRUE,
60bb06bc 2000 &hidden);
bb4d2ac2 2001 if (version_string)
252b5132 2002 {
bb4d2ac2 2003 if (!hidden)
252b5132
RH
2004 fprintf (file, " %-11s", version_string);
2005 else
2006 {
2007 int i;
2008
2009 fprintf (file, " (%s)", version_string);
2010 for (i = 10 - strlen (version_string); i > 0; --i)
2011 putc (' ', file);
2012 }
2013 }
2014
2015 /* If the st_other field is not zero, print it. */
7a13edea 2016 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
c044fabd 2017
7a13edea
NC
2018 switch (st_other)
2019 {
2020 case 0: break;
2021 case STV_INTERNAL: fprintf (file, " .internal"); break;
2022 case STV_HIDDEN: fprintf (file, " .hidden"); break;
2023 case STV_PROTECTED: fprintf (file, " .protected"); break;
2024 default:
2025 /* Some other non-defined flags are also present, so print
2026 everything hex. */
2027 fprintf (file, " 0x%02x", (unsigned int) st_other);
2028 }
252b5132 2029
587ff49e 2030 fprintf (file, " %s", name);
252b5132
RH
2031 }
2032 break;
2033 }
2034}
252b5132
RH
2035\f
2036/* ELF .o/exec file reading */
2037
c044fabd 2038/* Create a new bfd section from an ELF section header. */
252b5132 2039
b34976b6 2040bfd_boolean
217aa764 2041bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
252b5132 2042{
4fbb74a6
AM
2043 Elf_Internal_Shdr *hdr;
2044 Elf_Internal_Ehdr *ehdr;
2045 const struct elf_backend_data *bed;
90937f86 2046 const char *name;
bf67003b 2047 bfd_boolean ret = TRUE;
252b5132 2048
4fbb74a6
AM
2049 if (shindex >= elf_numsections (abfd))
2050 return FALSE;
2051
a86c6c19
AM
2052 /* PR17512: A corrupt ELF binary might contain a loop of sections via
2053 sh_link or sh_info. Detect this here, by refusing to load a
2054 section that we are already in the process of loading. */
2055 if (elf_tdata (abfd)->being_created[shindex])
bf67003b 2056 {
a86c6c19
AM
2057 _bfd_error_handler
2058 (_("%pB: warning: loop in section dependencies detected"), abfd);
2059 return FALSE;
bf67003b 2060 }
a86c6c19 2061 elf_tdata (abfd)->being_created[shindex] = TRUE;
bf67003b 2062
4fbb74a6
AM
2063 hdr = elf_elfsections (abfd)[shindex];
2064 ehdr = elf_elfheader (abfd);
2065 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1b3a8575 2066 hdr->sh_name);
933d961a 2067 if (name == NULL)
bf67003b 2068 goto fail;
252b5132 2069
4fbb74a6 2070 bed = get_elf_backend_data (abfd);
252b5132
RH
2071 switch (hdr->sh_type)
2072 {
2073 case SHT_NULL:
2074 /* Inactive section. Throw it away. */
bf67003b 2075 goto success;
252b5132 2076
bf67003b
NC
2077 case SHT_PROGBITS: /* Normal section with contents. */
2078 case SHT_NOBITS: /* .bss section. */
2079 case SHT_HASH: /* .hash section. */
2080 case SHT_NOTE: /* .note section. */
25e27870
L
2081 case SHT_INIT_ARRAY: /* .init_array section. */
2082 case SHT_FINI_ARRAY: /* .fini_array section. */
2083 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
7f1204bb 2084 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
fdc90cb4 2085 case SHT_GNU_HASH: /* .gnu.hash section. */
bf67003b
NC
2086 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2087 goto success;
252b5132 2088
797fc050 2089 case SHT_DYNAMIC: /* Dynamic linking information. */
6dc132d9 2090 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b
NC
2091 goto fail;
2092
cfcac11d
NC
2093 if (hdr->sh_link > elf_numsections (abfd))
2094 {
caa83f8b 2095 /* PR 10478: Accept Solaris binaries with a sh_link
cfcac11d
NC
2096 field set to SHN_BEFORE or SHN_AFTER. */
2097 switch (bfd_get_arch (abfd))
2098 {
caa83f8b 2099 case bfd_arch_i386:
cfcac11d
NC
2100 case bfd_arch_sparc:
2101 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
2102 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
2103 break;
2104 /* Otherwise fall through. */
2105 default:
bf67003b 2106 goto fail;
cfcac11d
NC
2107 }
2108 }
2109 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
bf67003b 2110 goto fail;
cfcac11d 2111 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
797fc050
AM
2112 {
2113 Elf_Internal_Shdr *dynsymhdr;
2114
2115 /* The shared libraries distributed with hpux11 have a bogus
2116 sh_link field for the ".dynamic" section. Find the
2117 string table for the ".dynsym" section instead. */
2118 if (elf_dynsymtab (abfd) != 0)
2119 {
2120 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2121 hdr->sh_link = dynsymhdr->sh_link;
2122 }
2123 else
2124 {
2125 unsigned int i, num_sec;
2126
2127 num_sec = elf_numsections (abfd);
2128 for (i = 1; i < num_sec; i++)
2129 {
2130 dynsymhdr = elf_elfsections (abfd)[i];
2131 if (dynsymhdr->sh_type == SHT_DYNSYM)
2132 {
2133 hdr->sh_link = dynsymhdr->sh_link;
2134 break;
2135 }
2136 }
2137 }
2138 }
bf67003b 2139 goto success;
797fc050 2140
bf67003b 2141 case SHT_SYMTAB: /* A symbol table. */
252b5132 2142 if (elf_onesymtab (abfd) == shindex)
bf67003b 2143 goto success;
252b5132 2144
a50b2160 2145 if (hdr->sh_entsize != bed->s->sizeof_sym)
bf67003b
NC
2146 goto fail;
2147
3337c1e5 2148 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
eee3b786
AM
2149 {
2150 if (hdr->sh_size != 0)
bf67003b 2151 goto fail;
eee3b786
AM
2152 /* Some assemblers erroneously set sh_info to one with a
2153 zero sh_size. ld sees this as a global symbol count
2154 of (unsigned) -1. Fix it here. */
2155 hdr->sh_info = 0;
bf67003b 2156 goto success;
eee3b786 2157 }
bf67003b 2158
16ad13ec
NC
2159 /* PR 18854: A binary might contain more than one symbol table.
2160 Unusual, but possible. Warn, but continue. */
2161 if (elf_onesymtab (abfd) != 0)
2162 {
4eca0228 2163 _bfd_error_handler
695344c0 2164 /* xgettext:c-format */
871b3ab2 2165 (_("%pB: warning: multiple symbol tables detected"
63a5468a 2166 " - ignoring the table in section %u"),
16ad13ec
NC
2167 abfd, shindex);
2168 goto success;
2169 }
252b5132 2170 elf_onesymtab (abfd) = shindex;
6a40cf0c
NC
2171 elf_symtab_hdr (abfd) = *hdr;
2172 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
252b5132
RH
2173 abfd->flags |= HAS_SYMS;
2174
2175 /* Sometimes a shared object will map in the symbol table. If
08a40648
AM
2176 SHF_ALLOC is set, and this is a shared object, then we also
2177 treat this section as a BFD section. We can not base the
2178 decision purely on SHF_ALLOC, because that flag is sometimes
2179 set in a relocatable object file, which would confuse the
2180 linker. */
252b5132
RH
2181 if ((hdr->sh_flags & SHF_ALLOC) != 0
2182 && (abfd->flags & DYNAMIC) != 0
6dc132d9
L
2183 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2184 shindex))
bf67003b 2185 goto fail;
252b5132 2186
1b3a8575
AM
2187 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2188 can't read symbols without that section loaded as well. It
2189 is most likely specified by the next section header. */
6a40cf0c
NC
2190 {
2191 elf_section_list * entry;
2192 unsigned int i, num_sec;
1b3a8575 2193
6a40cf0c
NC
2194 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2195 if (entry->hdr.sh_link == shindex)
2196 goto success;
2197
2198 num_sec = elf_numsections (abfd);
2199 for (i = shindex + 1; i < num_sec; i++)
2200 {
2201 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2202
2203 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2204 && hdr2->sh_link == shindex)
2205 break;
2206 }
2207
2208 if (i == num_sec)
2209 for (i = 1; i < shindex; i++)
1b3a8575
AM
2210 {
2211 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
6a40cf0c 2212
1b3a8575
AM
2213 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2214 && hdr2->sh_link == shindex)
2215 break;
2216 }
6a40cf0c
NC
2217
2218 if (i != shindex)
2219 ret = bfd_section_from_shdr (abfd, i);
2220 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2221 goto success;
2222 }
252b5132 2223
bf67003b 2224 case SHT_DYNSYM: /* A dynamic symbol table. */
252b5132 2225 if (elf_dynsymtab (abfd) == shindex)
bf67003b 2226 goto success;
252b5132 2227
a50b2160 2228 if (hdr->sh_entsize != bed->s->sizeof_sym)
bf67003b
NC
2229 goto fail;
2230
eee3b786
AM
2231 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2232 {
2233 if (hdr->sh_size != 0)
bf67003b
NC
2234 goto fail;
2235
eee3b786
AM
2236 /* Some linkers erroneously set sh_info to one with a
2237 zero sh_size. ld sees this as a global symbol count
2238 of (unsigned) -1. Fix it here. */
2239 hdr->sh_info = 0;
bf67003b 2240 goto success;
eee3b786 2241 }
bf67003b 2242
16ad13ec
NC
2243 /* PR 18854: A binary might contain more than one dynamic symbol table.
2244 Unusual, but possible. Warn, but continue. */
2245 if (elf_dynsymtab (abfd) != 0)
2246 {
4eca0228 2247 _bfd_error_handler
695344c0 2248 /* xgettext:c-format */
871b3ab2 2249 (_("%pB: warning: multiple dynamic symbol tables detected"
63a5468a 2250 " - ignoring the table in section %u"),
16ad13ec
NC
2251 abfd, shindex);
2252 goto success;
2253 }
252b5132
RH
2254 elf_dynsymtab (abfd) = shindex;
2255 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2256 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2257 abfd->flags |= HAS_SYMS;
2258
2259 /* Besides being a symbol table, we also treat this as a regular
2260 section, so that objcopy can handle it. */
bf67003b
NC
2261 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2262 goto success;
252b5132 2263
bf67003b 2264 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
6a40cf0c
NC
2265 {
2266 elf_section_list * entry;
9ad5cbcf 2267
6a40cf0c
NC
2268 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2269 if (entry->ndx == shindex)
2270 goto success;
07d6d2b8 2271
7a6e0d89 2272 entry = bfd_alloc (abfd, sizeof (*entry));
6a40cf0c
NC
2273 if (entry == NULL)
2274 goto fail;
2275 entry->ndx = shindex;
2276 entry->hdr = * hdr;
2277 entry->next = elf_symtab_shndx_list (abfd);
2278 elf_symtab_shndx_list (abfd) = entry;
2279 elf_elfsections (abfd)[shindex] = & entry->hdr;
2280 goto success;
2281 }
9ad5cbcf 2282
bf67003b 2283 case SHT_STRTAB: /* A string table. */
252b5132 2284 if (hdr->bfd_section != NULL)
bf67003b
NC
2285 goto success;
2286
252b5132
RH
2287 if (ehdr->e_shstrndx == shindex)
2288 {
2289 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2290 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
bf67003b 2291 goto success;
252b5132 2292 }
bf67003b 2293
1b3a8575
AM
2294 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2295 {
2296 symtab_strtab:
2297 elf_tdata (abfd)->strtab_hdr = *hdr;
2298 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
bf67003b 2299 goto success;
1b3a8575 2300 }
bf67003b 2301
1b3a8575
AM
2302 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2303 {
2304 dynsymtab_strtab:
2305 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2306 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2307 elf_elfsections (abfd)[shindex] = hdr;
2308 /* We also treat this as a regular section, so that objcopy
2309 can handle it. */
bf67003b
NC
2310 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2311 shindex);
2312 goto success;
1b3a8575 2313 }
252b5132 2314
1b3a8575
AM
2315 /* If the string table isn't one of the above, then treat it as a
2316 regular section. We need to scan all the headers to be sure,
2317 just in case this strtab section appeared before the above. */
2318 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2319 {
2320 unsigned int i, num_sec;
252b5132 2321
1b3a8575
AM
2322 num_sec = elf_numsections (abfd);
2323 for (i = 1; i < num_sec; i++)
2324 {
2325 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2326 if (hdr2->sh_link == shindex)
2327 {
933d961a
JJ
2328 /* Prevent endless recursion on broken objects. */
2329 if (i == shindex)
bf67003b 2330 goto fail;
1b3a8575 2331 if (! bfd_section_from_shdr (abfd, i))
bf67003b 2332 goto fail;
1b3a8575
AM
2333 if (elf_onesymtab (abfd) == i)
2334 goto symtab_strtab;
2335 if (elf_dynsymtab (abfd) == i)
2336 goto dynsymtab_strtab;
2337 }
2338 }
2339 }
bf67003b
NC
2340 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2341 goto success;
252b5132
RH
2342
2343 case SHT_REL:
2344 case SHT_RELA:
2345 /* *These* do a lot of work -- but build no sections! */
2346 {
2347 asection *target_sect;
d4730f92 2348 Elf_Internal_Shdr *hdr2, **p_hdr;
9ad5cbcf 2349 unsigned int num_sec = elf_numsections (abfd);
d4730f92 2350 struct bfd_elf_section_data *esdt;
252b5132 2351
aa2ca951
JJ
2352 if (hdr->sh_entsize
2353 != (bfd_size_type) (hdr->sh_type == SHT_REL
a50b2160 2354 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
bf67003b 2355 goto fail;
a50b2160 2356
03ae5f59 2357 /* Check for a bogus link to avoid crashing. */
4fbb74a6 2358 if (hdr->sh_link >= num_sec)
03ae5f59 2359 {
4eca0228 2360 _bfd_error_handler
695344c0 2361 /* xgettext:c-format */
871b3ab2 2362 (_("%pB: invalid link %u for reloc section %s (index %u)"),
4eca0228 2363 abfd, hdr->sh_link, name, shindex);
bf67003b
NC
2364 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2365 shindex);
2366 goto success;
03ae5f59
ILT
2367 }
2368
252b5132
RH
2369 /* For some incomprehensible reason Oracle distributes
2370 libraries for Solaris in which some of the objects have
2371 bogus sh_link fields. It would be nice if we could just
2372 reject them, but, unfortunately, some people need to use
2373 them. We scan through the section headers; if we find only
2374 one suitable symbol table, we clobber the sh_link to point
83b89087
L
2375 to it. I hope this doesn't break anything.
2376
2377 Don't do it on executable nor shared library. */
2378 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
2379 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
252b5132
RH
2380 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
2381 {
9ad5cbcf 2382 unsigned int scan;
252b5132
RH
2383 int found;
2384
2385 found = 0;
9ad5cbcf 2386 for (scan = 1; scan < num_sec; scan++)
252b5132
RH
2387 {
2388 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
2389 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
2390 {
2391 if (found != 0)
2392 {
2393 found = 0;
2394 break;
2395 }
2396 found = scan;
2397 }
2398 }
2399 if (found != 0)
2400 hdr->sh_link = found;
2401 }
2402
2403 /* Get the symbol table. */
1b3a8575
AM
2404 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2405 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
252b5132 2406 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
bf67003b 2407 goto fail;
252b5132 2408
a4bcd733
AM
2409 /* If this is an alloc section in an executable or shared
2410 library, or the reloc section does not use the main symbol
2411 table we don't treat it as a reloc section. BFD can't
2412 adequately represent such a section, so at least for now,
2413 we don't try. We just present it as a normal section. We
2414 also can't use it as a reloc section if it points to the
2415 null section, an invalid section, another reloc section, or
2416 its sh_link points to the null section. */
2417 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2418 && (hdr->sh_flags & SHF_ALLOC) != 0)
83b89087 2419 || hdr->sh_link == SHN_UNDEF
a4bcd733 2420 || hdr->sh_link != elf_onesymtab (abfd)
185ef66d 2421 || hdr->sh_info == SHN_UNDEF
185ef66d
AM
2422 || hdr->sh_info >= num_sec
2423 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2424 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
bf67003b
NC
2425 {
2426 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2427 shindex);
2428 goto success;
2429 }
252b5132
RH
2430
2431 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
bf67003b
NC
2432 goto fail;
2433
252b5132
RH
2434 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2435 if (target_sect == NULL)
bf67003b 2436 goto fail;
252b5132 2437
d4730f92
BS
2438 esdt = elf_section_data (target_sect);
2439 if (hdr->sh_type == SHT_RELA)
2440 p_hdr = &esdt->rela.hdr;
252b5132 2441 else
d4730f92
BS
2442 p_hdr = &esdt->rel.hdr;
2443
a7ba3896
NC
2444 /* PR 17512: file: 0b4f81b7.
2445 Also see PR 24456, for a file which deliberately has two reloc
2446 sections. */
06614111 2447 if (*p_hdr != NULL)
a7ba3896 2448 {
a859124d 2449 if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
a8e14f4c
NC
2450 {
2451 _bfd_error_handler
2452 /* xgettext:c-format */
a859124d
AM
2453 (_("%pB: warning: secondary relocation section '%s' "
2454 "for section %pA found - ignoring"),
a8e14f4c
NC
2455 abfd, name, target_sect);
2456 }
a7ba3896
NC
2457 goto success;
2458 }
a8e14f4c 2459
ef53be89 2460 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
d4730f92 2461 if (hdr2 == NULL)
bf67003b 2462 goto fail;
252b5132 2463 *hdr2 = *hdr;
d4730f92 2464 *p_hdr = hdr2;
252b5132 2465 elf_elfsections (abfd)[shindex] = hdr2;
056bafd4
MR
2466 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2467 * bed->s->int_rels_per_ext_rel);
252b5132
RH
2468 target_sect->flags |= SEC_RELOC;
2469 target_sect->relocation = NULL;
2470 target_sect->rel_filepos = hdr->sh_offset;
bf572ba0
MM
2471 /* In the section to which the relocations apply, mark whether
2472 its relocations are of the REL or RELA variety. */
72730e0c 2473 if (hdr->sh_size != 0)
d4730f92
BS
2474 {
2475 if (hdr->sh_type == SHT_RELA)
2476 target_sect->use_rela_p = 1;
2477 }
252b5132 2478 abfd->flags |= HAS_RELOC;
bf67003b 2479 goto success;
252b5132 2480 }
252b5132
RH
2481
2482 case SHT_GNU_verdef:
2483 elf_dynverdef (abfd) = shindex;
2484 elf_tdata (abfd)->dynverdef_hdr = *hdr;
bf67003b
NC
2485 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2486 goto success;
252b5132
RH
2487
2488 case SHT_GNU_versym:
a50b2160 2489 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
bf67003b
NC
2490 goto fail;
2491
252b5132
RH
2492 elf_dynversym (abfd) = shindex;
2493 elf_tdata (abfd)->dynversym_hdr = *hdr;
bf67003b
NC
2494 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2495 goto success;
252b5132
RH
2496
2497 case SHT_GNU_verneed:
2498 elf_dynverref (abfd) = shindex;
2499 elf_tdata (abfd)->dynverref_hdr = *hdr;
bf67003b
NC
2500 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2501 goto success;
252b5132
RH
2502
2503 case SHT_SHLIB:
bf67003b 2504 goto success;
252b5132 2505
dbb410c3 2506 case SHT_GROUP:
44534af3 2507 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
bf67003b
NC
2508 goto fail;
2509
6dc132d9 2510 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b
NC
2511 goto fail;
2512
bf67003b 2513 goto success;
dbb410c3 2514
252b5132 2515 default:
104d59d1
JM
2516 /* Possibly an attributes section. */
2517 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2518 || hdr->sh_type == bed->obj_attrs_section_type)
2519 {
2520 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b 2521 goto fail;
104d59d1 2522 _bfd_elf_parse_attributes (abfd, hdr);
bf67003b 2523 goto success;
104d59d1
JM
2524 }
2525
252b5132 2526 /* Check for any processor-specific section types. */
3eb70a79 2527 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
bf67003b 2528 goto success;
3eb70a79
L
2529
2530 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2531 {
2532 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2533 /* FIXME: How to properly handle allocated section reserved
2534 for applications? */
4eca0228 2535 _bfd_error_handler
695344c0 2536 /* xgettext:c-format */
871b3ab2 2537 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2538 abfd, hdr->sh_type, name);
3eb70a79 2539 else
bf67003b
NC
2540 {
2541 /* Allow sections reserved for applications. */
2542 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2543 shindex);
2544 goto success;
2545 }
3eb70a79
L
2546 }
2547 else if (hdr->sh_type >= SHT_LOPROC
2548 && hdr->sh_type <= SHT_HIPROC)
2549 /* FIXME: We should handle this section. */
4eca0228 2550 _bfd_error_handler
695344c0 2551 /* xgettext:c-format */
871b3ab2 2552 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2553 abfd, hdr->sh_type, name);
3eb70a79 2554 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
ff15b240
NC
2555 {
2556 /* Unrecognised OS-specific sections. */
2557 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2558 /* SHF_OS_NONCONFORMING indicates that special knowledge is
08a40648 2559 required to correctly process the section and the file should
ff15b240 2560 be rejected with an error message. */
4eca0228 2561 _bfd_error_handler
695344c0 2562 /* xgettext:c-format */
871b3ab2 2563 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2564 abfd, hdr->sh_type, name);
ff15b240 2565 else
bf67003b
NC
2566 {
2567 /* Otherwise it should be processed. */
2568 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2569 goto success;
2570 }
ff15b240 2571 }
3eb70a79
L
2572 else
2573 /* FIXME: We should handle this section. */
4eca0228 2574 _bfd_error_handler
695344c0 2575 /* xgettext:c-format */
871b3ab2 2576 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2577 abfd, hdr->sh_type, name);
3eb70a79 2578
bf67003b 2579 goto fail;
252b5132
RH
2580 }
2581
bf67003b
NC
2582 fail:
2583 ret = FALSE;
2584 success:
a86c6c19 2585 elf_tdata (abfd)->being_created[shindex] = FALSE;
bf67003b 2586 return ret;
252b5132
RH
2587}
2588
87d72d41 2589/* Return the local symbol specified by ABFD, R_SYMNDX. */
ec338859 2590
87d72d41
AM
2591Elf_Internal_Sym *
2592bfd_sym_from_r_symndx (struct sym_cache *cache,
2593 bfd *abfd,
2594 unsigned long r_symndx)
ec338859 2595{
ec338859
AM
2596 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2597
a5d1b3b5
AM
2598 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2599 {
2600 Elf_Internal_Shdr *symtab_hdr;
2601 unsigned char esym[sizeof (Elf64_External_Sym)];
2602 Elf_External_Sym_Shndx eshndx;
ec338859 2603
a5d1b3b5
AM
2604 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2605 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
87d72d41 2606 &cache->sym[ent], esym, &eshndx) == NULL)
a5d1b3b5 2607 return NULL;
9ad5cbcf 2608
a5d1b3b5
AM
2609 if (cache->abfd != abfd)
2610 {
2611 memset (cache->indx, -1, sizeof (cache->indx));
2612 cache->abfd = abfd;
2613 }
2614 cache->indx[ent] = r_symndx;
ec338859 2615 }
a5d1b3b5 2616
87d72d41 2617 return &cache->sym[ent];
ec338859
AM
2618}
2619
252b5132
RH
2620/* Given an ELF section number, retrieve the corresponding BFD
2621 section. */
2622
2623asection *
91d6fa6a 2624bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
252b5132 2625{
91d6fa6a 2626 if (sec_index >= elf_numsections (abfd))
252b5132 2627 return NULL;
91d6fa6a 2628 return elf_elfsections (abfd)[sec_index]->bfd_section;
252b5132
RH
2629}
2630
b35d266b 2631static const struct bfd_elf_special_section special_sections_b[] =
2f89ff8d 2632{
0112cd26 2633 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 2634 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2635};
2636
b35d266b 2637static const struct bfd_elf_special_section special_sections_c[] =
7f4d3958 2638{
0112cd26 2639 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
1ff6de03 2640 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2641 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2642};
2643
b35d266b 2644static const struct bfd_elf_special_section special_sections_d[] =
7f4d3958 2645{
07d6d2b8
AM
2646 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2647 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
a9a72a65
DE
2648 /* There are more DWARF sections than these, but they needn't be added here
2649 unless you have to cope with broken compilers that don't emit section
2650 attributes or you want to help the user writing assembler. */
07d6d2b8
AM
2651 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2652 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2653 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2654 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
0112cd26 2655 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
07d6d2b8
AM
2656 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2657 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2658 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2659 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2660};
2661
b35d266b 2662static const struct bfd_elf_special_section special_sections_f[] =
7f4d3958 2663{
07d6d2b8 2664 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
6f9dbcd4 2665 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8 2666 { NULL, 0 , 0, 0, 0 }
7f4d3958
L
2667};
2668
b35d266b 2669static const struct bfd_elf_special_section special_sections_g[] =
7f4d3958 2670{
0112cd26 2671 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2c6f3e56
JL
2672 { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2673 { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2674 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2675 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2676 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
0112cd26
NC
2677 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2678 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
07d6d2b8
AM
2679 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2680 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2681 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2682 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2683};
2684
b35d266b 2685static const struct bfd_elf_special_section special_sections_h[] =
7f4d3958 2686{
07d6d2b8
AM
2687 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2688 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2689};
2690
b35d266b 2691static const struct bfd_elf_special_section special_sections_i[] =
7f4d3958 2692{
07d6d2b8 2693 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
6f9dbcd4 2694 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2695 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2696 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2697};
2698
b35d266b 2699static const struct bfd_elf_special_section special_sections_l[] =
7f4d3958 2700{
0112cd26 2701 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2702 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2703};
2704
b35d266b 2705static const struct bfd_elf_special_section special_sections_n[] =
7f4d3958 2706{
2c6f3e56 2707 { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 2708 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
07d6d2b8
AM
2709 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2710 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2711};
2712
b35d266b 2713static const struct bfd_elf_special_section special_sections_p[] =
7f4d3958 2714{
2c6f3e56 2715 { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
6f9dbcd4 2716 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2717 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2718 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2719};
2720
b35d266b 2721static const struct bfd_elf_special_section special_sections_r[] =
7f4d3958 2722{
0112cd26
NC
2723 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2724 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
07d6d2b8
AM
2725 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2726 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2727 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2728};
2729
b35d266b 2730static const struct bfd_elf_special_section special_sections_s[] =
7f4d3958 2731{
0112cd26
NC
2732 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2733 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2734 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
60ff4dc4
HPN
2735 /* See struct bfd_elf_special_section declaration for the semantics of
2736 this special case where .prefix_length != strlen (.prefix). */
2737 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
07d6d2b8 2738 { NULL, 0, 0, 0, 0 }
2f89ff8d
L
2739};
2740
b35d266b 2741static const struct bfd_elf_special_section special_sections_t[] =
7f4d3958 2742{
07d6d2b8
AM
2743 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2744 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
0112cd26 2745 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
07d6d2b8 2746 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2747};
2748
1b315056
CS
2749static const struct bfd_elf_special_section special_sections_z[] =
2750{
07d6d2b8
AM
2751 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2752 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
1b315056
CS
2753 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2754 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2755 { NULL, 0, 0, 0, 0 }
1b315056
CS
2756};
2757
e4c93b56 2758static const struct bfd_elf_special_section * const special_sections[] =
7f4d3958 2759{
7f4d3958 2760 special_sections_b, /* 'b' */
98ece1b3 2761 special_sections_c, /* 'c' */
7f4d3958
L
2762 special_sections_d, /* 'd' */
2763 NULL, /* 'e' */
2764 special_sections_f, /* 'f' */
2765 special_sections_g, /* 'g' */
2766 special_sections_h, /* 'h' */
2767 special_sections_i, /* 'i' */
2768 NULL, /* 'j' */
2769 NULL, /* 'k' */
2770 special_sections_l, /* 'l' */
2771 NULL, /* 'm' */
2772 special_sections_n, /* 'n' */
2773 NULL, /* 'o' */
2774 special_sections_p, /* 'p' */
2775 NULL, /* 'q' */
2776 special_sections_r, /* 'r' */
2777 special_sections_s, /* 's' */
2778 special_sections_t, /* 't' */
1b315056
CS
2779 NULL, /* 'u' */
2780 NULL, /* 'v' */
2781 NULL, /* 'w' */
2782 NULL, /* 'x' */
2783 NULL, /* 'y' */
2784 special_sections_z /* 'z' */
7f4d3958
L
2785};
2786
551b43fd
AM
2787const struct bfd_elf_special_section *
2788_bfd_elf_get_special_section (const char *name,
2789 const struct bfd_elf_special_section *spec,
2790 unsigned int rela)
2f89ff8d
L
2791{
2792 int i;
7f4d3958 2793 int len;
7f4d3958 2794
551b43fd 2795 len = strlen (name);
7f4d3958 2796
551b43fd 2797 for (i = 0; spec[i].prefix != NULL; i++)
7dcb9820
AM
2798 {
2799 int suffix_len;
551b43fd 2800 int prefix_len = spec[i].prefix_length;
7dcb9820
AM
2801
2802 if (len < prefix_len)
2803 continue;
551b43fd 2804 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
7dcb9820
AM
2805 continue;
2806
551b43fd 2807 suffix_len = spec[i].suffix_length;
7dcb9820
AM
2808 if (suffix_len <= 0)
2809 {
2810 if (name[prefix_len] != 0)
2811 {
2812 if (suffix_len == 0)
2813 continue;
2814 if (name[prefix_len] != '.'
2815 && (suffix_len == -2
551b43fd 2816 || (rela && spec[i].type == SHT_REL)))
7dcb9820
AM
2817 continue;
2818 }
2819 }
2820 else
2821 {
2822 if (len < prefix_len + suffix_len)
2823 continue;
2824 if (memcmp (name + len - suffix_len,
551b43fd 2825 spec[i].prefix + prefix_len,
7dcb9820
AM
2826 suffix_len) != 0)
2827 continue;
2828 }
551b43fd 2829 return &spec[i];
7dcb9820 2830 }
2f89ff8d
L
2831
2832 return NULL;
2833}
2834
7dcb9820 2835const struct bfd_elf_special_section *
29ef7005 2836_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2f89ff8d 2837{
551b43fd
AM
2838 int i;
2839 const struct bfd_elf_special_section *spec;
29ef7005 2840 const struct elf_backend_data *bed;
2f89ff8d
L
2841
2842 /* See if this is one of the special sections. */
551b43fd
AM
2843 if (sec->name == NULL)
2844 return NULL;
2f89ff8d 2845
29ef7005
L
2846 bed = get_elf_backend_data (abfd);
2847 spec = bed->special_sections;
2848 if (spec)
2849 {
2850 spec = _bfd_elf_get_special_section (sec->name,
2851 bed->special_sections,
2852 sec->use_rela_p);
2853 if (spec != NULL)
2854 return spec;
2855 }
2856
551b43fd
AM
2857 if (sec->name[0] != '.')
2858 return NULL;
2f89ff8d 2859
551b43fd 2860 i = sec->name[1] - 'b';
1b315056 2861 if (i < 0 || i > 'z' - 'b')
551b43fd
AM
2862 return NULL;
2863
2864 spec = special_sections[i];
2f89ff8d 2865
551b43fd
AM
2866 if (spec == NULL)
2867 return NULL;
2868
2869 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2f89ff8d
L
2870}
2871
b34976b6 2872bfd_boolean
217aa764 2873_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
252b5132
RH
2874{
2875 struct bfd_elf_section_data *sdata;
551b43fd 2876 const struct elf_backend_data *bed;
7dcb9820 2877 const struct bfd_elf_special_section *ssect;
252b5132 2878
f0abc2a1
AM
2879 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2880 if (sdata == NULL)
2881 {
a50b1753 2882 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
07d6d2b8 2883 sizeof (*sdata));
f0abc2a1
AM
2884 if (sdata == NULL)
2885 return FALSE;
217aa764 2886 sec->used_by_bfd = sdata;
f0abc2a1 2887 }
bf572ba0 2888
551b43fd
AM
2889 /* Indicate whether or not this section should use RELA relocations. */
2890 bed = get_elf_backend_data (abfd);
2891 sec->use_rela_p = bed->default_use_rela_p;
2892
8c803a2d
AM
2893 /* Set up ELF section type and flags for newly created sections, if
2894 there is an ABI mandated section. */
2895 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2896 if (ssect != NULL)
2f89ff8d 2897 {
8c803a2d
AM
2898 elf_section_type (sec) = ssect->type;
2899 elf_section_flags (sec) = ssect->attr;
2f89ff8d
L
2900 }
2901
f592407e 2902 return _bfd_generic_new_section_hook (abfd, sec);
252b5132
RH
2903}
2904
2905/* Create a new bfd section from an ELF program header.
2906
2907 Since program segments have no names, we generate a synthetic name
2908 of the form segment<NUM>, where NUM is generally the index in the
2909 program header table. For segments that are split (see below) we
2910 generate the names segment<NUM>a and segment<NUM>b.
2911
2912 Note that some program segments may have a file size that is different than
2913 (less than) the memory size. All this means is that at execution the
2914 system must allocate the amount of memory specified by the memory size,
2915 but only initialize it with the first "file size" bytes read from the
2916 file. This would occur for example, with program segments consisting
2917 of combined data+bss.
2918
2919 To handle the above situation, this routine generates TWO bfd sections
2920 for the single program segment. The first has the length specified by
2921 the file size of the segment, and the second has the length specified
2922 by the difference between the two sizes. In effect, the segment is split
d5191d0c 2923 into its initialized and uninitialized parts.
252b5132
RH
2924
2925 */
2926
b34976b6 2927bfd_boolean
217aa764
AM
2928_bfd_elf_make_section_from_phdr (bfd *abfd,
2929 Elf_Internal_Phdr *hdr,
91d6fa6a 2930 int hdr_index,
a50b1753 2931 const char *type_name)
252b5132
RH
2932{
2933 asection *newsect;
2934 char *name;
2935 char namebuf[64];
d4c88bbb 2936 size_t len;
252b5132 2937 int split;
502794d4 2938 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132
RH
2939
2940 split = ((hdr->p_memsz > 0)
2941 && (hdr->p_filesz > 0)
2942 && (hdr->p_memsz > hdr->p_filesz));
d5191d0c
AM
2943
2944 if (hdr->p_filesz > 0)
252b5132 2945 {
91d6fa6a 2946 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
d5191d0c 2947 len = strlen (namebuf) + 1;
a50b1753 2948 name = (char *) bfd_alloc (abfd, len);
d5191d0c
AM
2949 if (!name)
2950 return FALSE;
2951 memcpy (name, namebuf, len);
2952 newsect = bfd_make_section (abfd, name);
2953 if (newsect == NULL)
2954 return FALSE;
502794d4
CE
2955 newsect->vma = hdr->p_vaddr / opb;
2956 newsect->lma = hdr->p_paddr / opb;
d5191d0c
AM
2957 newsect->size = hdr->p_filesz;
2958 newsect->filepos = hdr->p_offset;
2959 newsect->flags |= SEC_HAS_CONTENTS;
2960 newsect->alignment_power = bfd_log2 (hdr->p_align);
2961 if (hdr->p_type == PT_LOAD)
252b5132 2962 {
d5191d0c
AM
2963 newsect->flags |= SEC_ALLOC;
2964 newsect->flags |= SEC_LOAD;
2965 if (hdr->p_flags & PF_X)
2966 {
2967 /* FIXME: all we known is that it has execute PERMISSION,
2968 may be data. */
2969 newsect->flags |= SEC_CODE;
2970 }
2971 }
2972 if (!(hdr->p_flags & PF_W))
2973 {
2974 newsect->flags |= SEC_READONLY;
252b5132 2975 }
252b5132
RH
2976 }
2977
d5191d0c 2978 if (hdr->p_memsz > hdr->p_filesz)
252b5132 2979 {
d5191d0c
AM
2980 bfd_vma align;
2981
91d6fa6a 2982 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
d4c88bbb 2983 len = strlen (namebuf) + 1;
a50b1753 2984 name = (char *) bfd_alloc (abfd, len);
252b5132 2985 if (!name)
b34976b6 2986 return FALSE;
d4c88bbb 2987 memcpy (name, namebuf, len);
252b5132
RH
2988 newsect = bfd_make_section (abfd, name);
2989 if (newsect == NULL)
b34976b6 2990 return FALSE;
502794d4
CE
2991 newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
2992 newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
eea6121a 2993 newsect->size = hdr->p_memsz - hdr->p_filesz;
d5191d0c
AM
2994 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2995 align = newsect->vma & -newsect->vma;
2996 if (align == 0 || align > hdr->p_align)
2997 align = hdr->p_align;
2998 newsect->alignment_power = bfd_log2 (align);
252b5132
RH
2999 if (hdr->p_type == PT_LOAD)
3000 {
3001 newsect->flags |= SEC_ALLOC;
3002 if (hdr->p_flags & PF_X)
3003 newsect->flags |= SEC_CODE;
3004 }
3005 if (!(hdr->p_flags & PF_W))
3006 newsect->flags |= SEC_READONLY;
3007 }
3008
b34976b6 3009 return TRUE;
252b5132
RH
3010}
3011
864619bb
KS
3012static bfd_boolean
3013_bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3014{
3015 /* The return value is ignored. Build-ids are considered optional. */
3016 if (templ->xvec->flavour == bfd_target_elf_flavour)
3017 return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3018 (templ, offset);
3019 return FALSE;
3020}
3021
b34976b6 3022bfd_boolean
91d6fa6a 3023bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
20cfcaae 3024{
9c5bfbb7 3025 const struct elf_backend_data *bed;
20cfcaae
NC
3026
3027 switch (hdr->p_type)
3028 {
3029 case PT_NULL:
91d6fa6a 3030 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
20cfcaae
NC
3031
3032 case PT_LOAD:
864619bb
KS
3033 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3034 return FALSE;
3035 if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3036 _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3037 return TRUE;
20cfcaae
NC
3038
3039 case PT_DYNAMIC:
91d6fa6a 3040 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
20cfcaae
NC
3041
3042 case PT_INTERP:
91d6fa6a 3043 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
20cfcaae
NC
3044
3045 case PT_NOTE:
91d6fa6a 3046 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
b34976b6 3047 return FALSE;
276da9b3
L
3048 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3049 hdr->p_align))
b34976b6
AM
3050 return FALSE;
3051 return TRUE;
20cfcaae
NC
3052
3053 case PT_SHLIB:
91d6fa6a 3054 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
20cfcaae
NC
3055
3056 case PT_PHDR:
91d6fa6a 3057 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
20cfcaae 3058
811072d8 3059 case PT_GNU_EH_FRAME:
91d6fa6a 3060 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
811072d8
RM
3061 "eh_frame_hdr");
3062
2b05f1b7 3063 case PT_GNU_STACK:
91d6fa6a 3064 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
9ee5e499 3065
8c37241b 3066 case PT_GNU_RELRO:
91d6fa6a 3067 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
8c37241b 3068
20cfcaae 3069 default:
8c1acd09 3070 /* Check for any processor-specific program segment types. */
20cfcaae 3071 bed = get_elf_backend_data (abfd);
91d6fa6a 3072 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
20cfcaae
NC
3073 }
3074}
3075
d4730f92
BS
3076/* Return the REL_HDR for SEC, assuming there is only a single one, either
3077 REL or RELA. */
3078
3079Elf_Internal_Shdr *
3080_bfd_elf_single_rel_hdr (asection *sec)
3081{
3082 if (elf_section_data (sec)->rel.hdr)
3083 {
3084 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3085 return elf_section_data (sec)->rel.hdr;
3086 }
3087 else
3088 return elf_section_data (sec)->rela.hdr;
3089}
3090
3e19fb8f
L
3091static bfd_boolean
3092_bfd_elf_set_reloc_sh_name (bfd *abfd,
3093 Elf_Internal_Shdr *rel_hdr,
3094 const char *sec_name,
3095 bfd_boolean use_rela_p)
3096{
3097 char *name = (char *) bfd_alloc (abfd,
3098 sizeof ".rela" + strlen (sec_name));
3099 if (name == NULL)
3100 return FALSE;
3101
3102 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3103 rel_hdr->sh_name =
3104 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3105 FALSE);
3106 if (rel_hdr->sh_name == (unsigned int) -1)
3107 return FALSE;
3108
3109 return TRUE;
3110}
3111
d4730f92
BS
3112/* Allocate and initialize a section-header for a new reloc section,
3113 containing relocations against ASECT. It is stored in RELDATA. If
3114 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3115 relocations. */
23bc299b 3116
5d13b3b3 3117static bfd_boolean
217aa764 3118_bfd_elf_init_reloc_shdr (bfd *abfd,
d4730f92 3119 struct bfd_elf_section_reloc_data *reldata,
f6fe1ccd 3120 const char *sec_name,
3e19fb8f
L
3121 bfd_boolean use_rela_p,
3122 bfd_boolean delay_st_name_p)
23bc299b 3123{
d4730f92 3124 Elf_Internal_Shdr *rel_hdr;
9c5bfbb7 3125 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 3126
d4730f92 3127 BFD_ASSERT (reldata->hdr == NULL);
ef53be89 3128 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
d4730f92 3129 reldata->hdr = rel_hdr;
23bc299b 3130
3e19fb8f
L
3131 if (delay_st_name_p)
3132 rel_hdr->sh_name = (unsigned int) -1;
3133 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3134 use_rela_p))
b34976b6 3135 return FALSE;
23bc299b
MM
3136 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3137 rel_hdr->sh_entsize = (use_rela_p
3138 ? bed->s->sizeof_rela
3139 : bed->s->sizeof_rel);
72de5009 3140 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
28e07a05 3141 rel_hdr->sh_flags = 0;
23bc299b
MM
3142 rel_hdr->sh_addr = 0;
3143 rel_hdr->sh_size = 0;
3144 rel_hdr->sh_offset = 0;
3145
b34976b6 3146 return TRUE;
23bc299b
MM
3147}
3148
94be91de
JB
3149/* Return the default section type based on the passed in section flags. */
3150
3151int
3152bfd_elf_get_default_section_type (flagword flags)
3153{
0e41bebb 3154 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
2e76e85a 3155 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
94be91de
JB
3156 return SHT_NOBITS;
3157 return SHT_PROGBITS;
3158}
3159
d4730f92
BS
3160struct fake_section_arg
3161{
3162 struct bfd_link_info *link_info;
3163 bfd_boolean failed;
3164};
3165
252b5132
RH
3166/* Set up an ELF internal section header for a section. */
3167
252b5132 3168static void
d4730f92 3169elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
252b5132 3170{
d4730f92 3171 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
9c5bfbb7 3172 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 3173 struct bfd_elf_section_data *esd = elf_section_data (asect);
252b5132 3174 Elf_Internal_Shdr *this_hdr;
0414f35b 3175 unsigned int sh_type;
0ce398f1 3176 const char *name = asect->name;
3e19fb8f 3177 bfd_boolean delay_st_name_p = FALSE;
233bf4f8 3178 bfd_vma mask;
252b5132 3179
d4730f92 3180 if (arg->failed)
252b5132
RH
3181 {
3182 /* We already failed; just get out of the bfd_map_over_sections
08a40648 3183 loop. */
252b5132
RH
3184 return;
3185 }
3186
d4730f92 3187 this_hdr = &esd->this_hdr;
252b5132 3188
f6fe1ccd 3189 if (arg->link_info)
0ce398f1 3190 {
f6fe1ccd
L
3191 /* ld: compress DWARF debug sections with names: .debug_*. */
3192 if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
3193 && (asect->flags & SEC_DEBUGGING)
3194 && name[1] == 'd'
3195 && name[6] == '_')
3196 {
3197 /* Set SEC_ELF_COMPRESS to indicate this section should be
3198 compressed. */
3199 asect->flags |= SEC_ELF_COMPRESS;
dd905818 3200 /* If this section will be compressed, delay adding section
3e19fb8f
L
3201 name to section name section after it is compressed in
3202 _bfd_elf_assign_file_positions_for_non_load. */
3203 delay_st_name_p = TRUE;
f6fe1ccd
L
3204 }
3205 }
3206 else if ((asect->flags & SEC_ELF_RENAME))
3207 {
3208 /* objcopy: rename output DWARF debug section. */
3209 if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
3210 {
3211 /* When we decompress or compress with SHF_COMPRESSED,
3212 convert section name from .zdebug_* to .debug_* if
3213 needed. */
3214 if (name[1] == 'z')
3215 {
3216 char *new_name = convert_zdebug_to_debug (abfd, name);
3217 if (new_name == NULL)
3218 {
3219 arg->failed = TRUE;
3220 return;
3221 }
3222 name = new_name;
3223 }
3224 }
3225 else if (asect->compress_status == COMPRESS_SECTION_DONE)
0ce398f1 3226 {
f6fe1ccd
L
3227 /* PR binutils/18087: Compression does not always make a
3228 section smaller. So only rename the section when
3229 compression has actually taken place. If input section
3230 name is .zdebug_*, we should never compress it again. */
3231 char *new_name = convert_debug_to_zdebug (abfd, name);
0ce398f1
L
3232 if (new_name == NULL)
3233 {
3234 arg->failed = TRUE;
3235 return;
3236 }
f6fe1ccd
L
3237 BFD_ASSERT (name[1] != 'z');
3238 name = new_name;
0ce398f1
L
3239 }
3240 }
3241
3e19fb8f
L
3242 if (delay_st_name_p)
3243 this_hdr->sh_name = (unsigned int) -1;
3244 else
252b5132 3245 {
3e19fb8f
L
3246 this_hdr->sh_name
3247 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3248 name, FALSE);
3249 if (this_hdr->sh_name == (unsigned int) -1)
3250 {
3251 arg->failed = TRUE;
3252 return;
3253 }
252b5132
RH
3254 }
3255
a4d8e49b 3256 /* Don't clear sh_flags. Assembler may set additional bits. */
252b5132
RH
3257
3258 if ((asect->flags & SEC_ALLOC) != 0
3259 || asect->user_set_vma)
502794d4 3260 this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
252b5132
RH
3261 else
3262 this_hdr->sh_addr = 0;
3263
3264 this_hdr->sh_offset = 0;
eea6121a 3265 this_hdr->sh_size = asect->size;
252b5132 3266 this_hdr->sh_link = 0;
c86934ce
NC
3267 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3268 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3269 {
4eca0228 3270 _bfd_error_handler
695344c0 3271 /* xgettext:c-format */
9793eb77 3272 (_("%pB: error: alignment power %d of section `%pA' is too big"),
c08bb8dd 3273 abfd, asect->alignment_power, asect);
c86934ce
NC
3274 arg->failed = TRUE;
3275 return;
3276 }
233bf4f8
AM
3277 /* Set sh_addralign to the highest power of two given by alignment
3278 consistent with the section VMA. Linker scripts can force VMA. */
3279 mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3280 this_hdr->sh_addralign = mask & -mask;
252b5132
RH
3281 /* The sh_entsize and sh_info fields may have been set already by
3282 copy_private_section_data. */
3283
3284 this_hdr->bfd_section = asect;
3285 this_hdr->contents = NULL;
3286
3cddba1e
L
3287 /* If the section type is unspecified, we set it based on
3288 asect->flags. */
98ece1b3
AM
3289 if ((asect->flags & SEC_GROUP) != 0)
3290 sh_type = SHT_GROUP;
98ece1b3 3291 else
94be91de 3292 sh_type = bfd_elf_get_default_section_type (asect->flags);
98ece1b3 3293
3cddba1e 3294 if (this_hdr->sh_type == SHT_NULL)
98ece1b3
AM
3295 this_hdr->sh_type = sh_type;
3296 else if (this_hdr->sh_type == SHT_NOBITS
3297 && sh_type == SHT_PROGBITS
3298 && (asect->flags & SEC_ALLOC) != 0)
3cddba1e 3299 {
98ece1b3
AM
3300 /* Warn if we are changing a NOBITS section to PROGBITS, but
3301 allow the link to proceed. This can happen when users link
3302 non-bss input sections to bss output sections, or emit data
3303 to a bss output section via a linker script. */
4eca0228 3304 _bfd_error_handler
871b3ab2 3305 (_("warning: section `%pA' type changed to PROGBITS"), asect);
98ece1b3 3306 this_hdr->sh_type = sh_type;
3cddba1e
L
3307 }
3308
2f89ff8d 3309 switch (this_hdr->sh_type)
252b5132 3310 {
2f89ff8d 3311 default:
2f89ff8d
L
3312 break;
3313
3314 case SHT_STRTAB:
2f89ff8d
L
3315 case SHT_NOTE:
3316 case SHT_NOBITS:
3317 case SHT_PROGBITS:
3318 break;
606851fb
AM
3319
3320 case SHT_INIT_ARRAY:
3321 case SHT_FINI_ARRAY:
3322 case SHT_PREINIT_ARRAY:
3323 this_hdr->sh_entsize = bed->s->arch_size / 8;
3324 break;
2f89ff8d
L
3325
3326 case SHT_HASH:
c7ac6ff8 3327 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2f89ff8d 3328 break;
5de3bf90 3329
2f89ff8d 3330 case SHT_DYNSYM:
252b5132 3331 this_hdr->sh_entsize = bed->s->sizeof_sym;
2f89ff8d
L
3332 break;
3333
3334 case SHT_DYNAMIC:
252b5132 3335 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2f89ff8d
L
3336 break;
3337
3338 case SHT_RELA:
3339 if (get_elf_backend_data (abfd)->may_use_rela_p)
3340 this_hdr->sh_entsize = bed->s->sizeof_rela;
3341 break;
3342
3343 case SHT_REL:
3344 if (get_elf_backend_data (abfd)->may_use_rel_p)
3345 this_hdr->sh_entsize = bed->s->sizeof_rel;
3346 break;
3347
3348 case SHT_GNU_versym:
252b5132 3349 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2f89ff8d
L
3350 break;
3351
3352 case SHT_GNU_verdef:
252b5132
RH
3353 this_hdr->sh_entsize = 0;
3354 /* objcopy or strip will copy over sh_info, but may not set
08a40648
AM
3355 cverdefs. The linker will set cverdefs, but sh_info will be
3356 zero. */
252b5132
RH
3357 if (this_hdr->sh_info == 0)
3358 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3359 else
3360 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3361 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2f89ff8d
L
3362 break;
3363
3364 case SHT_GNU_verneed:
252b5132
RH
3365 this_hdr->sh_entsize = 0;
3366 /* objcopy or strip will copy over sh_info, but may not set
08a40648
AM
3367 cverrefs. The linker will set cverrefs, but sh_info will be
3368 zero. */
252b5132
RH
3369 if (this_hdr->sh_info == 0)
3370 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3371 else
3372 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3373 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2f89ff8d
L
3374 break;
3375
3376 case SHT_GROUP:
1783205a 3377 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2f89ff8d 3378 break;
fdc90cb4
JJ
3379
3380 case SHT_GNU_HASH:
3381 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3382 break;
dbb410c3 3383 }
252b5132
RH
3384
3385 if ((asect->flags & SEC_ALLOC) != 0)
3386 this_hdr->sh_flags |= SHF_ALLOC;
3387 if ((asect->flags & SEC_READONLY) == 0)
3388 this_hdr->sh_flags |= SHF_WRITE;
3389 if ((asect->flags & SEC_CODE) != 0)
3390 this_hdr->sh_flags |= SHF_EXECINSTR;
f5fa8ca2
JJ
3391 if ((asect->flags & SEC_MERGE) != 0)
3392 {
3393 this_hdr->sh_flags |= SHF_MERGE;
3394 this_hdr->sh_entsize = asect->entsize;
f5fa8ca2 3395 }
84865015
NC
3396 if ((asect->flags & SEC_STRINGS) != 0)
3397 this_hdr->sh_flags |= SHF_STRINGS;
1126897b 3398 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
dbb410c3 3399 this_hdr->sh_flags |= SHF_GROUP;
13ae64f3 3400 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
704afa60
JJ
3401 {
3402 this_hdr->sh_flags |= SHF_TLS;
3a800eb9
AM
3403 if (asect->size == 0
3404 && (asect->flags & SEC_HAS_CONTENTS) == 0)
704afa60 3405 {
3a800eb9 3406 struct bfd_link_order *o = asect->map_tail.link_order;
b34976b6 3407
704afa60 3408 this_hdr->sh_size = 0;
3a800eb9
AM
3409 if (o != NULL)
3410 {
704afa60 3411 this_hdr->sh_size = o->offset + o->size;
3a800eb9
AM
3412 if (this_hdr->sh_size != 0)
3413 this_hdr->sh_type = SHT_NOBITS;
3414 }
704afa60
JJ
3415 }
3416 }
18ae9cc1
L
3417 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3418 this_hdr->sh_flags |= SHF_EXCLUDE;
252b5132 3419
d4730f92
BS
3420 /* If the section has relocs, set up a section header for the
3421 SHT_REL[A] section. If two relocation sections are required for
3422 this section, it is up to the processor-specific back-end to
3423 create the other. */
3424 if ((asect->flags & SEC_RELOC) != 0)
3425 {
3426 /* When doing a relocatable link, create both REL and RELA sections if
3427 needed. */
3428 if (arg->link_info
3429 /* Do the normal setup if we wouldn't create any sections here. */
3430 && esd->rel.count + esd->rela.count > 0
0e1862bb
L
3431 && (bfd_link_relocatable (arg->link_info)
3432 || arg->link_info->emitrelocations))
d4730f92
BS
3433 {
3434 if (esd->rel.count && esd->rel.hdr == NULL
28e07a05 3435 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
db4677b8 3436 FALSE, delay_st_name_p))
d4730f92
BS
3437 {
3438 arg->failed = TRUE;
3439 return;
3440 }
3441 if (esd->rela.count && esd->rela.hdr == NULL
28e07a05 3442 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
db4677b8 3443 TRUE, delay_st_name_p))
d4730f92
BS
3444 {
3445 arg->failed = TRUE;
3446 return;
3447 }
3448 }
3449 else if (!_bfd_elf_init_reloc_shdr (abfd,
3450 (asect->use_rela_p
3451 ? &esd->rela : &esd->rel),
f6fe1ccd 3452 name,
3e19fb8f
L
3453 asect->use_rela_p,
3454 delay_st_name_p))
db4677b8 3455 {
d4730f92 3456 arg->failed = TRUE;
db4677b8
AM
3457 return;
3458 }
d4730f92
BS
3459 }
3460
252b5132 3461 /* Check for processor-specific section types. */
0414f35b 3462 sh_type = this_hdr->sh_type;
e1fddb6b
AO
3463 if (bed->elf_backend_fake_sections
3464 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
db4677b8
AM
3465 {
3466 arg->failed = TRUE;
3467 return;
3468 }
252b5132 3469
42bb2e33 3470 if (sh_type == SHT_NOBITS && asect->size != 0)
0414f35b
AM
3471 {
3472 /* Don't change the header type from NOBITS if we are being
42bb2e33 3473 called for objcopy --only-keep-debug. */
0414f35b
AM
3474 this_hdr->sh_type = sh_type;
3475 }
252b5132
RH
3476}
3477
bcacc0f5
AM
3478/* Fill in the contents of a SHT_GROUP section. Called from
3479 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3480 when ELF targets use the generic linker, ld. Called for ld -r
3481 from bfd_elf_final_link. */
dbb410c3 3482
1126897b 3483void
217aa764 3484bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
dbb410c3 3485{
a50b1753 3486 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
9dce4196 3487 asection *elt, *first;
dbb410c3 3488 unsigned char *loc;
b34976b6 3489 bfd_boolean gas;
dbb410c3 3490
7e4111ad
L
3491 /* Ignore linker created group section. See elfNN_ia64_object_p in
3492 elfxx-ia64.c. */
ce5aecf8
AM
3493 if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3494 || sec->size == 0
dbb410c3
AM
3495 || *failedptr)
3496 return;
3497
bcacc0f5
AM
3498 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3499 {
3500 unsigned long symindx = 0;
3501
3502 /* elf_group_id will have been set up by objcopy and the
3503 generic linker. */
3504 if (elf_group_id (sec) != NULL)
3505 symindx = elf_group_id (sec)->udata.i;
1126897b 3506
bcacc0f5
AM
3507 if (symindx == 0)
3508 {
3509 /* If called from the assembler, swap_out_syms will have set up
6a541707
NC
3510 elf_section_syms.
3511 PR 25699: A corrupt input file could contain bogus group info. */
3512 if (elf_section_syms (abfd) == NULL)
3513 {
3514 *failedptr = TRUE;
3515 return;
3516 }
bcacc0f5
AM
3517 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3518 }
3519 elf_section_data (sec)->this_hdr.sh_info = symindx;
3520 }
3521 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
1126897b 3522 {
bcacc0f5
AM
3523 /* The ELF backend linker sets sh_info to -2 when the group
3524 signature symbol is global, and thus the index can't be
3525 set until all local symbols are output. */
53720c49
AM
3526 asection *igroup;
3527 struct bfd_elf_section_data *sec_data;
3528 unsigned long symndx;
3529 unsigned long extsymoff;
bcacc0f5
AM
3530 struct elf_link_hash_entry *h;
3531
53720c49
AM
3532 /* The point of this little dance to the first SHF_GROUP section
3533 then back to the SHT_GROUP section is that this gets us to
3534 the SHT_GROUP in the input object. */
3535 igroup = elf_sec_group (elf_next_in_group (sec));
3536 sec_data = elf_section_data (igroup);
3537 symndx = sec_data->this_hdr.sh_info;
3538 extsymoff = 0;
bcacc0f5
AM
3539 if (!elf_bad_symtab (igroup->owner))
3540 {
3541 Elf_Internal_Shdr *symtab_hdr;
3542
3543 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3544 extsymoff = symtab_hdr->sh_info;
3545 }
3546 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3547 while (h->root.type == bfd_link_hash_indirect
3548 || h->root.type == bfd_link_hash_warning)
3549 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3550
3551 elf_section_data (sec)->this_hdr.sh_info = h->indx;
1126897b 3552 }
dbb410c3 3553
1126897b 3554 /* The contents won't be allocated for "ld -r" or objcopy. */
b34976b6 3555 gas = TRUE;
dbb410c3
AM
3556 if (sec->contents == NULL)
3557 {
b34976b6 3558 gas = FALSE;
a50b1753 3559 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
9dce4196
AM
3560
3561 /* Arrange for the section to be written out. */
3562 elf_section_data (sec)->this_hdr.contents = sec->contents;
dbb410c3
AM
3563 if (sec->contents == NULL)
3564 {
b34976b6 3565 *failedptr = TRUE;
dbb410c3
AM
3566 return;
3567 }
3568 }
3569
eea6121a 3570 loc = sec->contents + sec->size;
dbb410c3 3571
9dce4196
AM
3572 /* Get the pointer to the first section in the group that gas
3573 squirreled away here. objcopy arranges for this to be set to the
3574 start of the input section group. */
3575 first = elt = elf_next_in_group (sec);
dbb410c3
AM
3576
3577 /* First element is a flag word. Rest of section is elf section
3578 indices for all the sections of the group. Write them backwards
3579 just to keep the group in the same order as given in .section
3580 directives, not that it matters. */
3581 while (elt != NULL)
3582 {
9dce4196 3583 asection *s;
9dce4196 3584
9dce4196 3585 s = elt;
415f38a6
AM
3586 if (!gas)
3587 s = s->output_section;
3588 if (s != NULL
3589 && !bfd_is_abs_section (s))
01e1a5bc 3590 {
db4677b8 3591 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
28e07a05
AM
3592 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3593
3594 if (elf_sec->rel.hdr != NULL
3595 && (gas
3596 || (input_elf_sec->rel.hdr != NULL
3597 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
db4677b8 3598 {
28e07a05 3599 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
db4677b8
AM
3600 loc -= 4;
3601 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3602 }
28e07a05
AM
3603 if (elf_sec->rela.hdr != NULL
3604 && (gas
3605 || (input_elf_sec->rela.hdr != NULL
3606 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
db4677b8 3607 {
28e07a05 3608 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
db4677b8
AM
3609 loc -= 4;
3610 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3611 }
01e1a5bc 3612 loc -= 4;
db4677b8 3613 H_PUT_32 (abfd, elf_sec->this_idx, loc);
01e1a5bc 3614 }
945906ff 3615 elt = elf_next_in_group (elt);
9dce4196
AM
3616 if (elt == first)
3617 break;
dbb410c3
AM
3618 }
3619
7bdf4127
AB
3620 loc -= 4;
3621 BFD_ASSERT (loc == sec->contents);
dbb410c3 3622
9dce4196 3623 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
dbb410c3
AM
3624}
3625
bce964aa
AM
3626/* Given NAME, the name of a relocation section stripped of its
3627 .rel/.rela prefix, return the section in ABFD to which the
3628 relocations apply. */
bd53a53a
L
3629
3630asection *
bce964aa
AM
3631_bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3632{
3633 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3634 section likely apply to .got.plt or .got section. */
3635 if (get_elf_backend_data (abfd)->want_got_plt
3636 && strcmp (name, ".plt") == 0)
3637 {
3638 asection *sec;
3639
3640 name = ".got.plt";
3641 sec = bfd_get_section_by_name (abfd, name);
3642 if (sec != NULL)
3643 return sec;
3644 name = ".got";
3645 }
3646
3647 return bfd_get_section_by_name (abfd, name);
3648}
3649
3650/* Return the section to which RELOC_SEC applies. */
3651
3652static asection *
3653elf_get_reloc_section (asection *reloc_sec)
bd53a53a
L
3654{
3655 const char *name;
3656 unsigned int type;
3657 bfd *abfd;
bce964aa 3658 const struct elf_backend_data *bed;
bd53a53a
L
3659
3660 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3661 if (type != SHT_REL && type != SHT_RELA)
3662 return NULL;
3663
3664 /* We look up the section the relocs apply to by name. */
3665 name = reloc_sec->name;
bce964aa
AM
3666 if (strncmp (name, ".rel", 4) != 0)
3667 return NULL;
3668 name += 4;
3669 if (type == SHT_RELA && *name++ != 'a')
3670 return NULL;
bd53a53a 3671
bd53a53a 3672 abfd = reloc_sec->owner;
bce964aa
AM
3673 bed = get_elf_backend_data (abfd);
3674 return bed->get_reloc_section (abfd, name);
bd53a53a
L
3675}
3676
252b5132
RH
3677/* Assign all ELF section numbers. The dummy first section is handled here
3678 too. The link/info pointers for the standard section types are filled
67411cbf
AM
3679 in here too, while we're at it. LINK_INFO will be 0 when arriving
3680 here for objcopy, and when using the generic ELF linker. */
252b5132 3681
b34976b6 3682static bfd_boolean
da9f89d4 3683assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
252b5132
RH
3684{
3685 struct elf_obj_tdata *t = elf_tdata (abfd);
3686 asection *sec;
3e19fb8f 3687 unsigned int section_number;
252b5132 3688 Elf_Internal_Shdr **i_shdrp;
47cc2cf5 3689 struct bfd_elf_section_data *d;
3516e984 3690 bfd_boolean need_symtab;
446f7ed5 3691 size_t amt;
252b5132
RH
3692
3693 section_number = 1;
3694
2b0f7ef9
JJ
3695 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3696
da9f89d4 3697 /* SHT_GROUP sections are in relocatable files only. */
7bdf4127 3698 if (link_info == NULL || !link_info->resolve_section_groups)
252b5132 3699 {
ef53be89 3700 size_t reloc_count = 0;
14f2c699 3701
da9f89d4 3702 /* Put SHT_GROUP sections first. */
04dd1667 3703 for (sec = abfd->sections; sec != NULL; sec = sec->next)
47cc2cf5 3704 {
5daa8fe7 3705 d = elf_section_data (sec);
da9f89d4
L
3706
3707 if (d->this_hdr.sh_type == SHT_GROUP)
08a40648 3708 {
5daa8fe7 3709 if (sec->flags & SEC_LINKER_CREATED)
da9f89d4
L
3710 {
3711 /* Remove the linker created SHT_GROUP sections. */
5daa8fe7 3712 bfd_section_list_remove (abfd, sec);
da9f89d4 3713 abfd->section_count--;
da9f89d4 3714 }
08a40648 3715 else
4fbb74a6 3716 d->this_idx = section_number++;
da9f89d4 3717 }
14f2c699
L
3718
3719 /* Count relocations. */
3720 reloc_count += sec->reloc_count;
47cc2cf5 3721 }
14f2c699
L
3722
3723 /* Clear HAS_RELOC if there are no relocations. */
3724 if (reloc_count == 0)
3725 abfd->flags &= ~HAS_RELOC;
47cc2cf5
PB
3726 }
3727
3728 for (sec = abfd->sections; sec; sec = sec->next)
3729 {
3730 d = elf_section_data (sec);
3731
3732 if (d->this_hdr.sh_type != SHT_GROUP)
4fbb74a6 3733 d->this_idx = section_number++;
3e19fb8f
L
3734 if (d->this_hdr.sh_name != (unsigned int) -1)
3735 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
d4730f92 3736 if (d->rel.hdr)
2b0f7ef9 3737 {
d4730f92 3738 d->rel.idx = section_number++;
3e19fb8f
L
3739 if (d->rel.hdr->sh_name != (unsigned int) -1)
3740 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
2b0f7ef9 3741 }
d4730f92
BS
3742 else
3743 d->rel.idx = 0;
23bc299b 3744
d4730f92 3745 if (d->rela.hdr)
2b0f7ef9 3746 {
d4730f92 3747 d->rela.idx = section_number++;
3e19fb8f
L
3748 if (d->rela.hdr->sh_name != (unsigned int) -1)
3749 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
2b0f7ef9 3750 }
23bc299b 3751 else
d4730f92 3752 d->rela.idx = 0;
252b5132
RH
3753 }
3754
3516e984
L
3755 need_symtab = (bfd_get_symcount (abfd) > 0
3756 || (link_info == NULL
3757 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3758 == HAS_RELOC)));
3759 if (need_symtab)
252b5132 3760 {
12bd6957 3761 elf_onesymtab (abfd) = section_number++;
2b0f7ef9 3762 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4fbb74a6 3763 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
9ad5cbcf 3764 {
7a6e0d89 3765 elf_section_list *entry;
6a40cf0c
NC
3766
3767 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
3768
7a6e0d89 3769 entry = bfd_zalloc (abfd, sizeof (*entry));
6a40cf0c
NC
3770 entry->ndx = section_number++;
3771 elf_symtab_shndx_list (abfd) = entry;
3772 entry->hdr.sh_name
9ad5cbcf 3773 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
b34976b6 3774 ".symtab_shndx", FALSE);
6a40cf0c 3775 if (entry->hdr.sh_name == (unsigned int) -1)
b34976b6 3776 return FALSE;
9ad5cbcf 3777 }
12bd6957 3778 elf_strtab_sec (abfd) = section_number++;
2b0f7ef9 3779 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
252b5132
RH
3780 }
3781
dd905818
NC
3782 elf_shstrtab_sec (abfd) = section_number++;
3783 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3784 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3785
1c52a645
L
3786 if (section_number >= SHN_LORESERVE)
3787 {
695344c0 3788 /* xgettext:c-format */
871b3ab2 3789 _bfd_error_handler (_("%pB: too many sections: %u"),
1c52a645
L
3790 abfd, section_number);
3791 return FALSE;
3792 }
3793
9ad5cbcf 3794 elf_numsections (abfd) = section_number;
252b5132
RH
3795 elf_elfheader (abfd)->e_shnum = section_number;
3796
3797 /* Set up the list of section header pointers, in agreement with the
3798 indices. */
446f7ed5
AM
3799 amt = section_number * sizeof (Elf_Internal_Shdr *);
3800 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
252b5132 3801 if (i_shdrp == NULL)
b34976b6 3802 return FALSE;
252b5132 3803
a50b1753 3804 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
07d6d2b8 3805 sizeof (Elf_Internal_Shdr));
252b5132
RH
3806 if (i_shdrp[0] == NULL)
3807 {
3808 bfd_release (abfd, i_shdrp);
b34976b6 3809 return FALSE;
252b5132 3810 }
252b5132
RH
3811
3812 elf_elfsections (abfd) = i_shdrp;
3813
12bd6957 3814 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3516e984 3815 if (need_symtab)
252b5132 3816 {
12bd6957 3817 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4fbb74a6 3818 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
9ad5cbcf 3819 {
6a40cf0c
NC
3820 elf_section_list * entry = elf_symtab_shndx_list (abfd);
3821 BFD_ASSERT (entry != NULL);
3822 i_shdrp[entry->ndx] = & entry->hdr;
3823 entry->hdr.sh_link = elf_onesymtab (abfd);
9ad5cbcf 3824 }
12bd6957
AM
3825 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3826 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
252b5132 3827 }
38ce5b11 3828
252b5132
RH
3829 for (sec = abfd->sections; sec; sec = sec->next)
3830 {
252b5132 3831 asection *s;
252b5132 3832
91d6fa6a
NC
3833 d = elf_section_data (sec);
3834
252b5132 3835 i_shdrp[d->this_idx] = &d->this_hdr;
d4730f92
BS
3836 if (d->rel.idx != 0)
3837 i_shdrp[d->rel.idx] = d->rel.hdr;
3838 if (d->rela.idx != 0)
3839 i_shdrp[d->rela.idx] = d->rela.hdr;
252b5132
RH
3840
3841 /* Fill in the sh_link and sh_info fields while we're at it. */
3842
3843 /* sh_link of a reloc section is the section index of the symbol
3844 table. sh_info is the section index of the section to which
3845 the relocation entries apply. */
d4730f92 3846 if (d->rel.idx != 0)
252b5132 3847 {
12bd6957 3848 d->rel.hdr->sh_link = elf_onesymtab (abfd);
d4730f92 3849 d->rel.hdr->sh_info = d->this_idx;
9ef5d938 3850 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
252b5132 3851 }
d4730f92 3852 if (d->rela.idx != 0)
23bc299b 3853 {
12bd6957 3854 d->rela.hdr->sh_link = elf_onesymtab (abfd);
d4730f92 3855 d->rela.hdr->sh_info = d->this_idx;
9ef5d938 3856 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
23bc299b 3857 }
252b5132 3858
38ce5b11
L
3859 /* We need to set up sh_link for SHF_LINK_ORDER. */
3860 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3861 {
3862 s = elf_linked_to_section (sec);
b71702f1
NC
3863 /* We can now have a NULL linked section pointer.
3864 This happens when the sh_link field is 0, which is done
3865 when a linked to section is discarded but the linking
3866 section has been retained for some reason. */
38ce5b11 3867 if (s)
38ce5b11 3868 {
67411cbf
AM
3869 /* Check discarded linkonce section. */
3870 if (discarded_section (s))
38ce5b11 3871 {
67411cbf
AM
3872 asection *kept;
3873 _bfd_error_handler
3874 /* xgettext:c-format */
3875 (_("%pB: sh_link of section `%pA' points to"
3876 " discarded section `%pA' of `%pB'"),
3877 abfd, d->this_hdr.bfd_section, s, s->owner);
3878 /* Point to the kept section if it has the same
3879 size as the discarded one. */
3880 kept = _bfd_elf_check_kept_section (s, link_info);
3881 if (kept == NULL)
f2876037 3882 {
f2876037
L
3883 bfd_set_error (bfd_error_bad_value);
3884 return FALSE;
3885 }
67411cbf
AM
3886 s = kept;
3887 }
3888 /* Handle objcopy. */
3889 else if (s->output_section == NULL)
3890 {
3891 _bfd_error_handler
3892 /* xgettext:c-format */
3893 (_("%pB: sh_link of section `%pA' points to"
3894 " removed section `%pA' of `%pB'"),
3895 abfd, d->this_hdr.bfd_section, s, s->owner);
3896 bfd_set_error (bfd_error_bad_value);
3897 return FALSE;
f2876037 3898 }
67411cbf 3899 s = s->output_section;
ccd2ec6a
L
3900 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3901 }
38ce5b11
L
3902 }
3903
252b5132
RH
3904 switch (d->this_hdr.sh_type)
3905 {
3906 case SHT_REL:
3907 case SHT_RELA:
3908 /* A reloc section which we are treating as a normal BFD
3909 section. sh_link is the section index of the symbol
3910 table. sh_info is the section index of the section to
3911 which the relocation entries apply. We assume that an
3912 allocated reloc section uses the dynamic symbol table.
3913 FIXME: How can we be sure? */
3914 s = bfd_get_section_by_name (abfd, ".dynsym");
3915 if (s != NULL)
3916 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3917
bce964aa 3918 s = elf_get_reloc_section (sec);
252b5132 3919 if (s != NULL)
9ef5d938
L
3920 {
3921 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3922 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3923 }
252b5132
RH
3924 break;
3925
3926 case SHT_STRTAB:
3927 /* We assume that a section named .stab*str is a stabs
3928 string section. We look for a section with the same name
3929 but without the trailing ``str'', and set its sh_link
3930 field to point to this section. */
0112cd26 3931 if (CONST_STRNEQ (sec->name, ".stab")
252b5132
RH
3932 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3933 {
3934 size_t len;
3935 char *alc;
3936
3937 len = strlen (sec->name);
a50b1753 3938 alc = (char *) bfd_malloc (len - 2);
252b5132 3939 if (alc == NULL)
b34976b6 3940 return FALSE;
d4c88bbb 3941 memcpy (alc, sec->name, len - 3);
252b5132
RH
3942 alc[len - 3] = '\0';
3943 s = bfd_get_section_by_name (abfd, alc);
3944 free (alc);
3945 if (s != NULL)
3946 {
3947 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3948
3949 /* This is a .stab section. */
34ca5531 3950 elf_section_data (s)->this_hdr.sh_entsize = 12;
252b5132
RH
3951 }
3952 }
3953 break;
3954
3955 case SHT_DYNAMIC:
3956 case SHT_DYNSYM:
3957 case SHT_GNU_verneed:
3958 case SHT_GNU_verdef:
3959 /* sh_link is the section header index of the string table
3960 used for the dynamic entries, or the symbol table, or the
3961 version strings. */
3962 s = bfd_get_section_by_name (abfd, ".dynstr");
3963 if (s != NULL)
3964 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3965 break;
3966
7f1204bb
JJ
3967 case SHT_GNU_LIBLIST:
3968 /* sh_link is the section header index of the prelink library
08a40648
AM
3969 list used for the dynamic entries, or the symbol table, or
3970 the version strings. */
7f1204bb
JJ
3971 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3972 ? ".dynstr" : ".gnu.libstr");
3973 if (s != NULL)
3974 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3975 break;
3976
252b5132 3977 case SHT_HASH:
fdc90cb4 3978 case SHT_GNU_HASH:
252b5132
RH
3979 case SHT_GNU_versym:
3980 /* sh_link is the section header index of the symbol table
3981 this hash table or version table is for. */
3982 s = bfd_get_section_by_name (abfd, ".dynsym");
3983 if (s != NULL)
3984 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3985 break;
dbb410c3
AM
3986
3987 case SHT_GROUP:
12bd6957 3988 d->this_hdr.sh_link = elf_onesymtab (abfd);
252b5132
RH
3989 }
3990 }
3991
3e19fb8f
L
3992 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
3993 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
3994 debug section name from .debug_* to .zdebug_* if needed. */
3995
b34976b6 3996 return TRUE;
252b5132
RH
3997}
3998
5372391b 3999static bfd_boolean
217aa764 4000sym_is_global (bfd *abfd, asymbol *sym)
252b5132
RH
4001{
4002 /* If the backend has a special mapping, use it. */
9c5bfbb7 4003 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764
AM
4004 if (bed->elf_backend_sym_is_global)
4005 return (*bed->elf_backend_sym_is_global) (abfd, sym);
252b5132 4006
e47bf690 4007 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
e6f7f6d1
AM
4008 || bfd_is_und_section (bfd_asymbol_section (sym))
4009 || bfd_is_com_section (bfd_asymbol_section (sym)));
252b5132
RH
4010}
4011
76359541
TP
4012/* Filter global symbols of ABFD to include in the import library. All
4013 SYMCOUNT symbols of ABFD can be examined from their pointers in
4014 SYMS. Pointers of symbols to keep should be stored contiguously at
4015 the beginning of that array.
4016
4017 Returns the number of symbols to keep. */
4018
4019unsigned int
4020_bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4021 asymbol **syms, long symcount)
4022{
4023 long src_count, dst_count = 0;
4024
4025 for (src_count = 0; src_count < symcount; src_count++)
4026 {
4027 asymbol *sym = syms[src_count];
4028 char *name = (char *) bfd_asymbol_name (sym);
4029 struct bfd_link_hash_entry *h;
4030
4031 if (!sym_is_global (abfd, sym))
4032 continue;
4033
4034 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE);
5df1bc57
AM
4035 if (h == NULL)
4036 continue;
76359541
TP
4037 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4038 continue;
76359541
TP
4039 if (h->linker_def || h->ldscript_def)
4040 continue;
4041
4042 syms[dst_count++] = sym;
4043 }
4044
4045 syms[dst_count] = NULL;
4046
4047 return dst_count;
4048}
4049
5372391b 4050/* Don't output section symbols for sections that are not going to be
c6d8cab4 4051 output, that are duplicates or there is no BFD section. */
5372391b
AM
4052
4053static bfd_boolean
4054ignore_section_sym (bfd *abfd, asymbol *sym)
4055{
c6d8cab4
L
4056 elf_symbol_type *type_ptr;
4057
db0c309f
NC
4058 if (sym == NULL)
4059 return FALSE;
4060
c6d8cab4
L
4061 if ((sym->flags & BSF_SECTION_SYM) == 0)
4062 return FALSE;
4063
d1bcae83
L
4064 /* Ignore the section symbol if it isn't used. */
4065 if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
4066 return TRUE;
4067
db0c309f
NC
4068 if (sym->section == NULL)
4069 return TRUE;
4070
c1229f84 4071 type_ptr = elf_symbol_from (sym);
c6d8cab4
L
4072 return ((type_ptr != NULL
4073 && type_ptr->internal_elf_sym.st_shndx != 0
4074 && bfd_is_abs_section (sym->section))
4075 || !(sym->section->owner == abfd
db0c309f
NC
4076 || (sym->section->output_section != NULL
4077 && sym->section->output_section->owner == abfd
2633a79c
AM
4078 && sym->section->output_offset == 0)
4079 || bfd_is_abs_section (sym->section)));
5372391b
AM
4080}
4081
2633a79c
AM
4082/* Map symbol from it's internal number to the external number, moving
4083 all local symbols to be at the head of the list. */
4084
b34976b6 4085static bfd_boolean
12bd6957 4086elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
252b5132 4087{
dc810e39 4088 unsigned int symcount = bfd_get_symcount (abfd);
252b5132
RH
4089 asymbol **syms = bfd_get_outsymbols (abfd);
4090 asymbol **sect_syms;
dc810e39
AM
4091 unsigned int num_locals = 0;
4092 unsigned int num_globals = 0;
4093 unsigned int num_locals2 = 0;
4094 unsigned int num_globals2 = 0;
7292b3ac 4095 unsigned int max_index = 0;
dc810e39 4096 unsigned int idx;
252b5132
RH
4097 asection *asect;
4098 asymbol **new_syms;
446f7ed5 4099 size_t amt;
252b5132
RH
4100
4101#ifdef DEBUG
4102 fprintf (stderr, "elf_map_symbols\n");
4103 fflush (stderr);
4104#endif
4105
252b5132
RH
4106 for (asect = abfd->sections; asect; asect = asect->next)
4107 {
4108 if (max_index < asect->index)
4109 max_index = asect->index;
4110 }
4111
4112 max_index++;
446f7ed5
AM
4113 amt = max_index * sizeof (asymbol *);
4114 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
252b5132 4115 if (sect_syms == NULL)
b34976b6 4116 return FALSE;
252b5132 4117 elf_section_syms (abfd) = sect_syms;
4e89ac30 4118 elf_num_section_syms (abfd) = max_index;
252b5132 4119
079e9a2f
AM
4120 /* Init sect_syms entries for any section symbols we have already
4121 decided to output. */
252b5132
RH
4122 for (idx = 0; idx < symcount; idx++)
4123 {
dc810e39 4124 asymbol *sym = syms[idx];
c044fabd 4125
252b5132 4126 if ((sym->flags & BSF_SECTION_SYM) != 0
0f0a5e58 4127 && sym->value == 0
2633a79c
AM
4128 && !ignore_section_sym (abfd, sym)
4129 && !bfd_is_abs_section (sym->section))
252b5132 4130 {
5372391b 4131 asection *sec = sym->section;
252b5132 4132
5372391b
AM
4133 if (sec->owner != abfd)
4134 sec = sec->output_section;
252b5132 4135
5372391b 4136 sect_syms[sec->index] = syms[idx];
252b5132
RH
4137 }
4138 }
4139
252b5132
RH
4140 /* Classify all of the symbols. */
4141 for (idx = 0; idx < symcount; idx++)
4142 {
2633a79c 4143 if (sym_is_global (abfd, syms[idx]))
252b5132 4144 num_globals++;
2633a79c
AM
4145 else if (!ignore_section_sym (abfd, syms[idx]))
4146 num_locals++;
252b5132 4147 }
079e9a2f 4148
5372391b 4149 /* We will be adding a section symbol for each normal BFD section. Most
079e9a2f
AM
4150 sections will already have a section symbol in outsymbols, but
4151 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4152 at least in that case. */
252b5132
RH
4153 for (asect = abfd->sections; asect; asect = asect->next)
4154 {
d1bcae83
L
4155 asymbol *sym = asect->symbol;
4156 /* Don't include ignored section symbols. */
4157 if (!ignore_section_sym (abfd, sym)
4158 && sect_syms[asect->index] == NULL)
252b5132 4159 {
079e9a2f 4160 if (!sym_is_global (abfd, asect->symbol))
252b5132
RH
4161 num_locals++;
4162 else
4163 num_globals++;
252b5132
RH
4164 }
4165 }
4166
4167 /* Now sort the symbols so the local symbols are first. */
446f7ed5
AM
4168 amt = (num_locals + num_globals) * sizeof (asymbol *);
4169 new_syms = (asymbol **) bfd_alloc (abfd, amt);
252b5132 4170 if (new_syms == NULL)
b34976b6 4171 return FALSE;
252b5132
RH
4172
4173 for (idx = 0; idx < symcount; idx++)
4174 {
4175 asymbol *sym = syms[idx];
dc810e39 4176 unsigned int i;
252b5132 4177
2633a79c
AM
4178 if (sym_is_global (abfd, sym))
4179 i = num_locals + num_globals2++;
d1bcae83 4180 /* Don't include ignored section symbols. */
2633a79c 4181 else if (!ignore_section_sym (abfd, sym))
252b5132
RH
4182 i = num_locals2++;
4183 else
2633a79c 4184 continue;
252b5132
RH
4185 new_syms[i] = sym;
4186 sym->udata.i = i + 1;
4187 }
4188 for (asect = abfd->sections; asect; asect = asect->next)
4189 {
d1bcae83
L
4190 asymbol *sym = asect->symbol;
4191 if (!ignore_section_sym (abfd, sym)
4192 && sect_syms[asect->index] == NULL)
252b5132 4193 {
dc810e39 4194 unsigned int i;
252b5132 4195
079e9a2f 4196 sect_syms[asect->index] = sym;
252b5132
RH
4197 if (!sym_is_global (abfd, sym))
4198 i = num_locals2++;
4199 else
4200 i = num_locals + num_globals2++;
4201 new_syms[i] = sym;
4202 sym->udata.i = i + 1;
4203 }
4204 }
4205
4206 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4207
12bd6957 4208 *pnum_locals = num_locals;
b34976b6 4209 return TRUE;
252b5132
RH
4210}
4211
4212/* Align to the maximum file alignment that could be required for any
4213 ELF data structure. */
4214
268b6b39 4215static inline file_ptr
217aa764 4216align_file_position (file_ptr off, int align)
252b5132
RH
4217{
4218 return (off + align - 1) & ~(align - 1);
4219}
4220
4221/* Assign a file position to a section, optionally aligning to the
4222 required section alignment. */
4223
217aa764
AM
4224file_ptr
4225_bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4226 file_ptr offset,
4227 bfd_boolean align)
252b5132 4228{
72de5009
AM
4229 if (align && i_shdrp->sh_addralign > 1)
4230 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
252b5132
RH
4231 i_shdrp->sh_offset = offset;
4232 if (i_shdrp->bfd_section != NULL)
4233 i_shdrp->bfd_section->filepos = offset;
4234 if (i_shdrp->sh_type != SHT_NOBITS)
4235 offset += i_shdrp->sh_size;
4236 return offset;
4237}
4238
4239/* Compute the file positions we are going to put the sections at, and
4240 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4241 is not NULL, this is being called by the ELF backend linker. */
4242
b34976b6 4243bfd_boolean
217aa764
AM
4244_bfd_elf_compute_section_file_positions (bfd *abfd,
4245 struct bfd_link_info *link_info)
252b5132 4246{
9c5bfbb7 4247 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 4248 struct fake_section_arg fsargs;
b34976b6 4249 bfd_boolean failed;
ef10c3ac 4250 struct elf_strtab_hash *strtab = NULL;
252b5132 4251 Elf_Internal_Shdr *shstrtab_hdr;
3516e984 4252 bfd_boolean need_symtab;
252b5132
RH
4253
4254 if (abfd->output_has_begun)
b34976b6 4255 return TRUE;
252b5132
RH
4256
4257 /* Do any elf backend specific processing first. */
4258 if (bed->elf_backend_begin_write_processing)
4259 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4260
ed7e9d0b 4261 if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
b34976b6 4262 return FALSE;
252b5132 4263
d4730f92
BS
4264 fsargs.failed = FALSE;
4265 fsargs.link_info = link_info;
4266 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4267 if (fsargs.failed)
b34976b6 4268 return FALSE;
252b5132 4269
da9f89d4 4270 if (!assign_section_numbers (abfd, link_info))
b34976b6 4271 return FALSE;
252b5132
RH
4272
4273 /* The backend linker builds symbol table information itself. */
3516e984
L
4274 need_symtab = (link_info == NULL
4275 && (bfd_get_symcount (abfd) > 0
4276 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4277 == HAS_RELOC)));
4278 if (need_symtab)
252b5132
RH
4279 {
4280 /* Non-zero if doing a relocatable link. */
4281 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4282
3d16b64e 4283 if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
b34976b6 4284 return FALSE;
252b5132
RH
4285 }
4286
d4730f92 4287 failed = FALSE;
1126897b 4288 if (link_info == NULL)
dbb410c3 4289 {
1126897b 4290 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
dbb410c3 4291 if (failed)
b34976b6 4292 return FALSE;
dbb410c3
AM
4293 }
4294
252b5132 4295 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
ed7e9d0b 4296 /* sh_name was set in init_file_header. */
252b5132 4297 shstrtab_hdr->sh_type = SHT_STRTAB;
84865015 4298 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
252b5132 4299 shstrtab_hdr->sh_addr = 0;
946748d5 4300 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
252b5132
RH
4301 shstrtab_hdr->sh_entsize = 0;
4302 shstrtab_hdr->sh_link = 0;
4303 shstrtab_hdr->sh_info = 0;
3e19fb8f 4304 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
252b5132
RH
4305 shstrtab_hdr->sh_addralign = 1;
4306
c84fca4d 4307 if (!assign_file_positions_except_relocs (abfd, link_info))
b34976b6 4308 return FALSE;
252b5132 4309
3516e984 4310 if (need_symtab)
252b5132
RH
4311 {
4312 file_ptr off;
4313 Elf_Internal_Shdr *hdr;
4314
12bd6957 4315 off = elf_next_file_pos (abfd);
252b5132 4316
6a40cf0c 4317 hdr = & elf_symtab_hdr (abfd);
b34976b6 4318 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
252b5132 4319
6a40cf0c
NC
4320 if (elf_symtab_shndx_list (abfd) != NULL)
4321 {
4322 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4323 if (hdr->sh_size != 0)
4324 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4325 /* FIXME: What about other symtab_shndx sections in the list ? */
4326 }
9ad5cbcf 4327
252b5132 4328 hdr = &elf_tdata (abfd)->strtab_hdr;
b34976b6 4329 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
252b5132 4330
12bd6957 4331 elf_next_file_pos (abfd) = off;
252b5132
RH
4332
4333 /* Now that we know where the .strtab section goes, write it
08a40648 4334 out. */
252b5132 4335 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 4336 || ! _bfd_elf_strtab_emit (abfd, strtab))
b34976b6 4337 return FALSE;
ef10c3ac 4338 _bfd_elf_strtab_free (strtab);
252b5132
RH
4339 }
4340
b34976b6 4341 abfd->output_has_begun = TRUE;
252b5132 4342
b34976b6 4343 return TRUE;
252b5132
RH
4344}
4345
8ded5a0f
AM
4346/* Make an initial estimate of the size of the program header. If we
4347 get the number wrong here, we'll redo section placement. */
4348
4349static bfd_size_type
4350get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4351{
4352 size_t segs;
4353 asection *s;
2b05f1b7 4354 const struct elf_backend_data *bed;
8ded5a0f
AM
4355
4356 /* Assume we will need exactly two PT_LOAD segments: one for text
4357 and one for data. */
4358 segs = 2;
4359
4360 s = bfd_get_section_by_name (abfd, ".interp");
1b9e270b 4361 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
8ded5a0f
AM
4362 {
4363 /* If we have a loadable interpreter section, we need a
4364 PT_INTERP segment. In this case, assume we also need a
4365 PT_PHDR segment, although that may not be true for all
4366 targets. */
e9a38e0f 4367 segs += 2;
8ded5a0f
AM
4368 }
4369
4370 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4371 {
4372 /* We need a PT_DYNAMIC segment. */
4373 ++segs;
f210dcff 4374 }
08a40648 4375
ceae84aa 4376 if (info != NULL && info->relro)
f210dcff
L
4377 {
4378 /* We need a PT_GNU_RELRO segment. */
4379 ++segs;
8ded5a0f
AM
4380 }
4381
12bd6957 4382 if (elf_eh_frame_hdr (abfd))
8ded5a0f
AM
4383 {
4384 /* We need a PT_GNU_EH_FRAME segment. */
4385 ++segs;
4386 }
4387
12bd6957 4388 if (elf_stack_flags (abfd))
8ded5a0f 4389 {
2b05f1b7
L
4390 /* We need a PT_GNU_STACK segment. */
4391 ++segs;
4392 }
94b11780 4393
0a59decb
L
4394 s = bfd_get_section_by_name (abfd,
4395 NOTE_GNU_PROPERTY_SECTION_NAME);
4396 if (s != NULL && s->size != 0)
4397 {
4398 /* We need a PT_GNU_PROPERTY segment. */
4399 ++segs;
4400 }
4401
2b05f1b7
L
4402 for (s = abfd->sections; s != NULL; s = s->next)
4403 {
8ded5a0f 4404 if ((s->flags & SEC_LOAD) != 0
23e463ed 4405 && elf_section_type (s) == SHT_NOTE)
8ded5a0f 4406 {
23e463ed 4407 unsigned int alignment_power;
8ded5a0f
AM
4408 /* We need a PT_NOTE segment. */
4409 ++segs;
23e463ed
L
4410 /* Try to create just one PT_NOTE segment for all adjacent
4411 loadable SHT_NOTE sections. gABI requires that within a
4412 PT_NOTE segment (and also inside of each SHT_NOTE section)
4413 each note should have the same alignment. So we check
4414 whether the sections are correctly aligned. */
4415 alignment_power = s->alignment_power;
4416 while (s->next != NULL
4417 && s->next->alignment_power == alignment_power
4418 && (s->next->flags & SEC_LOAD) != 0
4419 && elf_section_type (s->next) == SHT_NOTE)
4420 s = s->next;
8ded5a0f
AM
4421 }
4422 }
4423
4424 for (s = abfd->sections; s != NULL; s = s->next)
4425 {
4426 if (s->flags & SEC_THREAD_LOCAL)
4427 {
4428 /* We need a PT_TLS segment. */
4429 ++segs;
4430 break;
4431 }
4432 }
4433
2b05f1b7 4434 bed = get_elf_backend_data (abfd);
a91e1603 4435
df3a023b
AM
4436 if ((abfd->flags & D_PAGED) != 0
4437 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4438 {
4439 /* Add a PT_GNU_MBIND segment for each mbind section. */
c410035d
AM
4440 bfd_vma commonpagesize;
4441 unsigned int page_align_power;
4442
4443 if (info != NULL)
4444 commonpagesize = info->commonpagesize;
4445 else
4446 commonpagesize = bed->commonpagesize;
4447 page_align_power = bfd_log2 (commonpagesize);
df3a023b
AM
4448 for (s = abfd->sections; s != NULL; s = s->next)
4449 if (elf_section_flags (s) & SHF_GNU_MBIND)
4450 {
4451 if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4452 {
4453 _bfd_error_handler
4454 /* xgettext:c-format */
4455 (_("%pB: GNU_MBIND section `%pA' has invalid "
4456 "sh_info field: %d"),
4457 abfd, s, elf_section_data (s)->this_hdr.sh_info);
4458 continue;
4459 }
4460 /* Align mbind section to page size. */
4461 if (s->alignment_power < page_align_power)
4462 s->alignment_power = page_align_power;
4463 segs ++;
4464 }
4465 }
4466
4467 /* Let the backend count up any program headers it might need. */
4468 if (bed->elf_backend_additional_program_headers)
8ded5a0f
AM
4469 {
4470 int a;
4471
4472 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4473 if (a == -1)
4474 abort ();
4475 segs += a;
4476 }
4477
4478 return segs * bed->s->sizeof_phdr;
4479}
4480
2ea37f1c
NC
4481/* Find the segment that contains the output_section of section. */
4482
4483Elf_Internal_Phdr *
4484_bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4485{
4486 struct elf_segment_map *m;
4487 Elf_Internal_Phdr *p;
4488
12bd6957 4489 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
2ea37f1c
NC
4490 m != NULL;
4491 m = m->next, p++)
4492 {
4493 int i;
4494
4495 for (i = m->count - 1; i >= 0; i--)
4496 if (m->sections[i] == section)
4497 return p;
4498 }
4499
4500 return NULL;
4501}
4502
252b5132
RH
4503/* Create a mapping from a set of sections to a program segment. */
4504
217aa764
AM
4505static struct elf_segment_map *
4506make_mapping (bfd *abfd,
4507 asection **sections,
4508 unsigned int from,
4509 unsigned int to,
4510 bfd_boolean phdr)
252b5132
RH
4511{
4512 struct elf_segment_map *m;
4513 unsigned int i;
4514 asection **hdrpp;
986f0783 4515 size_t amt;
252b5132 4516
00bee008
AM
4517 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4518 amt += (to - from) * sizeof (asection *);
a50b1753 4519 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
4520 if (m == NULL)
4521 return NULL;
4522 m->next = NULL;
4523 m->p_type = PT_LOAD;
4524 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4525 m->sections[i - from] = *hdrpp;
4526 m->count = to - from;
4527
4528 if (from == 0 && phdr)
4529 {
4530 /* Include the headers in the first PT_LOAD segment. */
4531 m->includes_filehdr = 1;
4532 m->includes_phdrs = 1;
4533 }
4534
4535 return m;
4536}
4537
229fcec5
MM
4538/* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4539 on failure. */
4540
4541struct elf_segment_map *
4542_bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4543{
4544 struct elf_segment_map *m;
4545
a50b1753 4546 m = (struct elf_segment_map *) bfd_zalloc (abfd,
07d6d2b8 4547 sizeof (struct elf_segment_map));
229fcec5
MM
4548 if (m == NULL)
4549 return NULL;
4550 m->next = NULL;
4551 m->p_type = PT_DYNAMIC;
4552 m->count = 1;
4553 m->sections[0] = dynsec;
08a40648 4554
229fcec5
MM
4555 return m;
4556}
4557
8ded5a0f 4558/* Possibly add or remove segments from the segment map. */
252b5132 4559
b34976b6 4560static bfd_boolean
3dea8fca
AM
4561elf_modify_segment_map (bfd *abfd,
4562 struct bfd_link_info *info,
4563 bfd_boolean remove_empty_load)
252b5132 4564{
252e386e 4565 struct elf_segment_map **m;
8ded5a0f 4566 const struct elf_backend_data *bed;
252b5132 4567
8ded5a0f
AM
4568 /* The placement algorithm assumes that non allocated sections are
4569 not in PT_LOAD segments. We ensure this here by removing such
4570 sections from the segment map. We also remove excluded
252e386e
AM
4571 sections. Finally, any PT_LOAD segment without sections is
4572 removed. */
12bd6957 4573 m = &elf_seg_map (abfd);
252e386e 4574 while (*m)
8ded5a0f
AM
4575 {
4576 unsigned int i, new_count;
252b5132 4577
252e386e 4578 for (new_count = 0, i = 0; i < (*m)->count; i++)
8ded5a0f 4579 {
252e386e
AM
4580 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4581 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4582 || (*m)->p_type != PT_LOAD))
8ded5a0f 4583 {
252e386e
AM
4584 (*m)->sections[new_count] = (*m)->sections[i];
4585 new_count++;
8ded5a0f
AM
4586 }
4587 }
252e386e 4588 (*m)->count = new_count;
252b5132 4589
1a9ccd70
NC
4590 if (remove_empty_load
4591 && (*m)->p_type == PT_LOAD
4592 && (*m)->count == 0
4593 && !(*m)->includes_phdrs)
252e386e
AM
4594 *m = (*m)->next;
4595 else
4596 m = &(*m)->next;
8ded5a0f 4597 }
252b5132 4598
8ded5a0f
AM
4599 bed = get_elf_backend_data (abfd);
4600 if (bed->elf_backend_modify_segment_map != NULL)
252b5132 4601 {
252e386e 4602 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
8ded5a0f 4603 return FALSE;
252b5132 4604 }
252b5132 4605
8ded5a0f
AM
4606 return TRUE;
4607}
252b5132 4608
dbc88fc1
AM
4609#define IS_TBSS(s) \
4610 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4611
8ded5a0f 4612/* Set up a mapping from BFD sections to program segments. */
252b5132 4613
8ded5a0f
AM
4614bfd_boolean
4615_bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
4616{
4617 unsigned int count;
4618 struct elf_segment_map *m;
4619 asection **sections = NULL;
4620 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3dea8fca 4621 bfd_boolean no_user_phdrs;
252b5132 4622
12bd6957 4623 no_user_phdrs = elf_seg_map (abfd) == NULL;
d324f6d6
RM
4624
4625 if (info != NULL)
4626 info->user_phdrs = !no_user_phdrs;
4627
3dea8fca 4628 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
252b5132 4629 {
8ded5a0f
AM
4630 asection *s;
4631 unsigned int i;
4632 struct elf_segment_map *mfirst;
4633 struct elf_segment_map **pm;
4634 asection *last_hdr;
4635 bfd_vma last_size;
00bee008 4636 unsigned int hdr_index;
8ded5a0f
AM
4637 bfd_vma maxpagesize;
4638 asection **hdrpp;
64029e93 4639 bfd_boolean phdr_in_segment;
8ded5a0f 4640 bfd_boolean writable;
2888249f 4641 bfd_boolean executable;
446f7ed5 4642 unsigned int tls_count = 0;
8ded5a0f 4643 asection *first_tls = NULL;
a91e1603 4644 asection *first_mbind = NULL;
8ded5a0f 4645 asection *dynsec, *eh_frame_hdr;
446f7ed5 4646 size_t amt;
66631823
CE
4647 bfd_vma addr_mask, wrap_to = 0; /* Bytes. */
4648 bfd_size_type phdr_size; /* Octets/bytes. */
502794d4 4649 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132 4650
8ded5a0f 4651 /* Select the allocated sections, and sort them. */
252b5132 4652
446f7ed5
AM
4653 amt = bfd_count_sections (abfd) * sizeof (asection *);
4654 sections = (asection **) bfd_malloc (amt);
8ded5a0f 4655 if (sections == NULL)
252b5132 4656 goto error_return;
252b5132 4657
8d06853e
AM
4658 /* Calculate top address, avoiding undefined behaviour of shift
4659 left operator when shift count is equal to size of type
4660 being shifted. */
4661 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4662 addr_mask = (addr_mask << 1) + 1;
4663
8ded5a0f
AM
4664 i = 0;
4665 for (s = abfd->sections; s != NULL; s = s->next)
4666 {
4667 if ((s->flags & SEC_ALLOC) != 0)
4668 {
48db3297
AM
4669 /* target_index is unused until bfd_elf_final_link
4670 starts output of section symbols. Use it to make
4671 qsort stable. */
4672 s->target_index = i;
8ded5a0f
AM
4673 sections[i] = s;
4674 ++i;
8d06853e 4675 /* A wrapping section potentially clashes with header. */
66631823
CE
4676 if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
4677 wrap_to = (s->lma + s->size / opb) & addr_mask;
8ded5a0f
AM
4678 }
4679 }
4680 BFD_ASSERT (i <= bfd_count_sections (abfd));
4681 count = i;
252b5132 4682
8ded5a0f 4683 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
252b5132 4684
64029e93
AM
4685 phdr_size = elf_program_header_size (abfd);
4686 if (phdr_size == (bfd_size_type) -1)
4687 phdr_size = get_program_header_size (abfd, info);
4688 phdr_size += bed->s->sizeof_ehdr;
502794d4
CE
4689 /* phdr_size is compared to LMA values which are in bytes. */
4690 phdr_size /= opb;
c410035d
AM
4691 if (info != NULL)
4692 maxpagesize = info->maxpagesize;
4693 else
4694 maxpagesize = bed->maxpagesize;
64029e93
AM
4695 if (maxpagesize == 0)
4696 maxpagesize = 1;
4697 phdr_in_segment = info != NULL && info->load_phdrs;
4698 if (count != 0
4699 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
4700 >= (phdr_size & (maxpagesize - 1))))
4701 /* For compatibility with old scripts that may not be using
4702 SIZEOF_HEADERS, add headers when it looks like space has
4703 been left for them. */
4704 phdr_in_segment = TRUE;
252b5132 4705
64029e93 4706 /* Build the mapping. */
8ded5a0f
AM
4707 mfirst = NULL;
4708 pm = &mfirst;
252b5132 4709
8ded5a0f
AM
4710 /* If we have a .interp section, then create a PT_PHDR segment for
4711 the program headers and a PT_INTERP segment for the .interp
4712 section. */
4713 s = bfd_get_section_by_name (abfd, ".interp");
1b9e270b 4714 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
8ded5a0f
AM
4715 {
4716 amt = sizeof (struct elf_segment_map);
a50b1753 4717 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4718 if (m == NULL)
4719 goto error_return;
4720 m->next = NULL;
4721 m->p_type = PT_PHDR;
f882209d 4722 m->p_flags = PF_R;
8ded5a0f
AM
4723 m->p_flags_valid = 1;
4724 m->includes_phdrs = 1;
64029e93 4725 phdr_in_segment = TRUE;
8ded5a0f
AM
4726 *pm = m;
4727 pm = &m->next;
252b5132 4728
8ded5a0f 4729 amt = sizeof (struct elf_segment_map);
a50b1753 4730 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4731 if (m == NULL)
4732 goto error_return;
4733 m->next = NULL;
4734 m->p_type = PT_INTERP;
4735 m->count = 1;
4736 m->sections[0] = s;
4737
4738 *pm = m;
4739 pm = &m->next;
252b5132 4740 }
8ded5a0f
AM
4741
4742 /* Look through the sections. We put sections in the same program
4743 segment when the start of the second section can be placed within
4744 a few bytes of the end of the first section. */
4745 last_hdr = NULL;
4746 last_size = 0;
00bee008 4747 hdr_index = 0;
8ded5a0f 4748 writable = FALSE;
2888249f 4749 executable = FALSE;
8ded5a0f
AM
4750 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4751 if (dynsec != NULL
4752 && (dynsec->flags & SEC_LOAD) == 0)
4753 dynsec = NULL;
4754
64029e93
AM
4755 if ((abfd->flags & D_PAGED) == 0)
4756 phdr_in_segment = FALSE;
4757
8ded5a0f
AM
4758 /* Deal with -Ttext or something similar such that the first section
4759 is not adjacent to the program headers. This is an
4760 approximation, since at this point we don't know exactly how many
4761 program headers we will need. */
64029e93 4762 if (phdr_in_segment && count > 0)
252b5132 4763 {
66631823 4764 bfd_vma phdr_lma; /* Bytes. */
64029e93
AM
4765 bfd_boolean separate_phdr = FALSE;
4766
4767 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
4768 if (info != NULL
4769 && info->separate_code
4770 && (sections[0]->flags & SEC_CODE) != 0)
1a9ccd70 4771 {
64029e93
AM
4772 /* If data sections should be separate from code and
4773 thus not executable, and the first section is
4774 executable then put the file and program headers in
4775 their own PT_LOAD. */
4776 separate_phdr = TRUE;
4777 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
4778 == (sections[0]->lma & addr_mask & -maxpagesize)))
4779 {
4780 /* The file and program headers are currently on the
4781 same page as the first section. Put them on the
4782 previous page if we can. */
4783 if (phdr_lma >= maxpagesize)
4784 phdr_lma -= maxpagesize;
4785 else
4786 separate_phdr = FALSE;
4787 }
4788 }
4789 if ((sections[0]->lma & addr_mask) < phdr_lma
4790 || (sections[0]->lma & addr_mask) < phdr_size)
4791 /* If file and program headers would be placed at the end
4792 of memory then it's probably better to omit them. */
4793 phdr_in_segment = FALSE;
4794 else if (phdr_lma < wrap_to)
4795 /* If a section wraps around to where we'll be placing
4796 file and program headers, then the headers will be
4797 overwritten. */
4798 phdr_in_segment = FALSE;
4799 else if (separate_phdr)
4800 {
4801 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
4802 if (m == NULL)
4803 goto error_return;
66631823 4804 m->p_paddr = phdr_lma * opb;
64029e93
AM
4805 m->p_vaddr_offset
4806 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
4807 m->p_paddr_valid = 1;
4808 *pm = m;
4809 pm = &m->next;
4810 phdr_in_segment = FALSE;
1a9ccd70 4811 }
252b5132
RH
4812 }
4813
8ded5a0f 4814 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
252b5132 4815 {
8ded5a0f
AM
4816 asection *hdr;
4817 bfd_boolean new_segment;
4818
4819 hdr = *hdrpp;
4820
4821 /* See if this section and the last one will fit in the same
4822 segment. */
4823
4824 if (last_hdr == NULL)
4825 {
4826 /* If we don't have a segment yet, then we don't need a new
4827 one (we build the last one after this loop). */
4828 new_segment = FALSE;
4829 }
4830 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4831 {
4832 /* If this section has a different relation between the
4833 virtual address and the load address, then we need a new
4834 segment. */
4835 new_segment = TRUE;
4836 }
b5599592
AM
4837 else if (hdr->lma < last_hdr->lma + last_size
4838 || last_hdr->lma + last_size < last_hdr->lma)
4839 {
4840 /* If this section has a load address that makes it overlap
4841 the previous section, then we need a new segment. */
4842 new_segment = TRUE;
4843 }
76cb3a89
AM
4844 else if ((abfd->flags & D_PAGED) != 0
4845 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4846 == (hdr->lma & -maxpagesize)))
4847 {
4848 /* If we are demand paged then we can't map two disk
4849 pages onto the same memory page. */
4850 new_segment = FALSE;
4851 }
39948a60
NC
4852 /* In the next test we have to be careful when last_hdr->lma is close
4853 to the end of the address space. If the aligned address wraps
4854 around to the start of the address space, then there are no more
4855 pages left in memory and it is OK to assume that the current
4856 section can be included in the current segment. */
76cb3a89
AM
4857 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4858 + maxpagesize > last_hdr->lma)
4859 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4860 + maxpagesize <= hdr->lma))
8ded5a0f
AM
4861 {
4862 /* If putting this section in this segment would force us to
4863 skip a page in the segment, then we need a new segment. */
4864 new_segment = TRUE;
4865 }
4866 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
76cb3a89 4867 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
8ded5a0f 4868 {
e5654c0f
AM
4869 /* We don't want to put a loaded section after a
4870 nonloaded (ie. bss style) section in the same segment
4871 as that will force the non-loaded section to be loaded.
76cb3a89 4872 Consider .tbss sections as loaded for this purpose. */
8ded5a0f
AM
4873 new_segment = TRUE;
4874 }
4875 else if ((abfd->flags & D_PAGED) == 0)
4876 {
4877 /* If the file is not demand paged, which means that we
4878 don't require the sections to be correctly aligned in the
4879 file, then there is no other reason for a new segment. */
4880 new_segment = FALSE;
4881 }
2888249f
L
4882 else if (info != NULL
4883 && info->separate_code
4884 && executable != ((hdr->flags & SEC_CODE) != 0))
4885 {
4886 new_segment = TRUE;
4887 }
8ded5a0f 4888 else if (! writable
76cb3a89 4889 && (hdr->flags & SEC_READONLY) == 0)
8ded5a0f
AM
4890 {
4891 /* We don't want to put a writable section in a read only
76cb3a89 4892 segment. */
8ded5a0f
AM
4893 new_segment = TRUE;
4894 }
4895 else
4896 {
4897 /* Otherwise, we can use the same segment. */
4898 new_segment = FALSE;
4899 }
4900
2889e75b 4901 /* Allow interested parties a chance to override our decision. */
ceae84aa
AM
4902 if (last_hdr != NULL
4903 && info != NULL
4904 && info->callbacks->override_segment_assignment != NULL)
4905 new_segment
4906 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4907 last_hdr,
4908 new_segment);
2889e75b 4909
8ded5a0f
AM
4910 if (! new_segment)
4911 {
4912 if ((hdr->flags & SEC_READONLY) == 0)
4913 writable = TRUE;
2888249f
L
4914 if ((hdr->flags & SEC_CODE) != 0)
4915 executable = TRUE;
8ded5a0f
AM
4916 last_hdr = hdr;
4917 /* .tbss sections effectively have zero size. */
502794d4 4918 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
8ded5a0f
AM
4919 continue;
4920 }
4921
4922 /* We need a new program segment. We must create a new program
00bee008 4923 header holding all the sections from hdr_index until hdr. */
8ded5a0f 4924
00bee008 4925 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
8ded5a0f
AM
4926 if (m == NULL)
4927 goto error_return;
4928
4929 *pm = m;
4930 pm = &m->next;
4931
252b5132 4932 if ((hdr->flags & SEC_READONLY) == 0)
b34976b6 4933 writable = TRUE;
8ded5a0f
AM
4934 else
4935 writable = FALSE;
4936
2888249f
L
4937 if ((hdr->flags & SEC_CODE) == 0)
4938 executable = FALSE;
4939 else
4940 executable = TRUE;
4941
baaff79e
JJ
4942 last_hdr = hdr;
4943 /* .tbss sections effectively have zero size. */
502794d4 4944 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
00bee008 4945 hdr_index = i;
8ded5a0f 4946 phdr_in_segment = FALSE;
252b5132
RH
4947 }
4948
86b2281f
AM
4949 /* Create a final PT_LOAD program segment, but not if it's just
4950 for .tbss. */
4951 if (last_hdr != NULL
00bee008 4952 && (i - hdr_index != 1
dbc88fc1 4953 || !IS_TBSS (last_hdr)))
8ded5a0f 4954 {
00bee008 4955 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
8ded5a0f
AM
4956 if (m == NULL)
4957 goto error_return;
252b5132 4958
8ded5a0f
AM
4959 *pm = m;
4960 pm = &m->next;
4961 }
252b5132 4962
8ded5a0f
AM
4963 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4964 if (dynsec != NULL)
4965 {
4966 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4967 if (m == NULL)
4968 goto error_return;
4969 *pm = m;
4970 pm = &m->next;
4971 }
252b5132 4972
23e463ed 4973 /* For each batch of consecutive loadable SHT_NOTE sections,
1c5265b5
JJ
4974 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4975 because if we link together nonloadable .note sections and
4976 loadable .note sections, we will generate two .note sections
23e463ed 4977 in the output file. */
8ded5a0f
AM
4978 for (s = abfd->sections; s != NULL; s = s->next)
4979 {
4980 if ((s->flags & SEC_LOAD) != 0
23e463ed 4981 && elf_section_type (s) == SHT_NOTE)
8ded5a0f 4982 {
1c5265b5 4983 asection *s2;
23e463ed 4984 unsigned int alignment_power = s->alignment_power;
91d6fa6a
NC
4985
4986 count = 1;
23e463ed
L
4987 for (s2 = s; s2->next != NULL; s2 = s2->next)
4988 {
4989 if (s2->next->alignment_power == alignment_power
4990 && (s2->next->flags & SEC_LOAD) != 0
4991 && elf_section_type (s2->next) == SHT_NOTE
66631823 4992 && align_power (s2->lma + s2->size / opb,
23e463ed
L
4993 alignment_power)
4994 == s2->next->lma)
4995 count++;
4996 else
4997 break;
4998 }
00bee008
AM
4999 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5000 amt += count * sizeof (asection *);
a50b1753 5001 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5002 if (m == NULL)
5003 goto error_return;
5004 m->next = NULL;
5005 m->p_type = PT_NOTE;
1c5265b5
JJ
5006 m->count = count;
5007 while (count > 1)
5008 {
5009 m->sections[m->count - count--] = s;
5010 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5011 s = s->next;
5012 }
5013 m->sections[m->count - 1] = s;
5014 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
8ded5a0f
AM
5015 *pm = m;
5016 pm = &m->next;
5017 }
5018 if (s->flags & SEC_THREAD_LOCAL)
5019 {
5020 if (! tls_count)
5021 first_tls = s;
5022 tls_count++;
5023 }
a91e1603
L
5024 if (first_mbind == NULL
5025 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5026 first_mbind = s;
8ded5a0f 5027 }
252b5132 5028
8ded5a0f
AM
5029 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5030 if (tls_count > 0)
5031 {
00bee008
AM
5032 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5033 amt += tls_count * sizeof (asection *);
a50b1753 5034 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5035 if (m == NULL)
5036 goto error_return;
5037 m->next = NULL;
5038 m->p_type = PT_TLS;
5039 m->count = tls_count;
5040 /* Mandated PF_R. */
5041 m->p_flags = PF_R;
5042 m->p_flags_valid = 1;
d923cae0 5043 s = first_tls;
446f7ed5 5044 for (i = 0; i < tls_count; ++i)
8ded5a0f 5045 {
d923cae0
L
5046 if ((s->flags & SEC_THREAD_LOCAL) == 0)
5047 {
5048 _bfd_error_handler
871b3ab2 5049 (_("%pB: TLS sections are not adjacent:"), abfd);
d923cae0
L
5050 s = first_tls;
5051 i = 0;
446f7ed5 5052 while (i < tls_count)
d923cae0
L
5053 {
5054 if ((s->flags & SEC_THREAD_LOCAL) != 0)
5055 {
871b3ab2 5056 _bfd_error_handler (_(" TLS: %pA"), s);
d923cae0
L
5057 i++;
5058 }
5059 else
871b3ab2 5060 _bfd_error_handler (_(" non-TLS: %pA"), s);
d923cae0
L
5061 s = s->next;
5062 }
5063 bfd_set_error (bfd_error_bad_value);
5064 goto error_return;
5065 }
5066 m->sections[i] = s;
5067 s = s->next;
8ded5a0f 5068 }
252b5132 5069
8ded5a0f
AM
5070 *pm = m;
5071 pm = &m->next;
5072 }
252b5132 5073
df3a023b
AM
5074 if (first_mbind
5075 && (abfd->flags & D_PAGED) != 0
5076 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
a91e1603
L
5077 for (s = first_mbind; s != NULL; s = s->next)
5078 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
df3a023b 5079 && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
a91e1603
L
5080 {
5081 /* Mandated PF_R. */
5082 unsigned long p_flags = PF_R;
5083 if ((s->flags & SEC_READONLY) == 0)
5084 p_flags |= PF_W;
5085 if ((s->flags & SEC_CODE) != 0)
5086 p_flags |= PF_X;
5087
5088 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5089 m = bfd_zalloc (abfd, amt);
5090 if (m == NULL)
5091 goto error_return;
5092 m->next = NULL;
5093 m->p_type = (PT_GNU_MBIND_LO
5094 + elf_section_data (s)->this_hdr.sh_info);
5095 m->count = 1;
5096 m->p_flags_valid = 1;
5097 m->sections[0] = s;
5098 m->p_flags = p_flags;
5099
5100 *pm = m;
5101 pm = &m->next;
5102 }
5103
0a59decb
L
5104 s = bfd_get_section_by_name (abfd,
5105 NOTE_GNU_PROPERTY_SECTION_NAME);
5106 if (s != NULL && s->size != 0)
5107 {
5108 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5109 m = bfd_zalloc (abfd, amt);
5110 if (m == NULL)
5111 goto error_return;
5112 m->next = NULL;
5113 m->p_type = PT_GNU_PROPERTY;
5114 m->count = 1;
5115 m->p_flags_valid = 1;
5116 m->sections[0] = s;
5117 m->p_flags = PF_R;
5118 *pm = m;
5119 pm = &m->next;
5120 }
5121
8ded5a0f
AM
5122 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5123 segment. */
12bd6957 5124 eh_frame_hdr = elf_eh_frame_hdr (abfd);
8ded5a0f
AM
5125 if (eh_frame_hdr != NULL
5126 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
252b5132 5127 {
dc810e39 5128 amt = sizeof (struct elf_segment_map);
a50b1753 5129 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
5130 if (m == NULL)
5131 goto error_return;
5132 m->next = NULL;
8ded5a0f 5133 m->p_type = PT_GNU_EH_FRAME;
252b5132 5134 m->count = 1;
8ded5a0f 5135 m->sections[0] = eh_frame_hdr->output_section;
252b5132
RH
5136
5137 *pm = m;
5138 pm = &m->next;
5139 }
13ae64f3 5140
12bd6957 5141 if (elf_stack_flags (abfd))
13ae64f3 5142 {
8ded5a0f 5143 amt = sizeof (struct elf_segment_map);
a50b1753 5144 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5145 if (m == NULL)
5146 goto error_return;
5147 m->next = NULL;
2b05f1b7 5148 m->p_type = PT_GNU_STACK;
12bd6957 5149 m->p_flags = elf_stack_flags (abfd);
04c3a755 5150 m->p_align = bed->stack_align;
8ded5a0f 5151 m->p_flags_valid = 1;
04c3a755
NS
5152 m->p_align_valid = m->p_align != 0;
5153 if (info->stacksize > 0)
5154 {
5155 m->p_size = info->stacksize;
5156 m->p_size_valid = 1;
5157 }
252b5132 5158
8ded5a0f
AM
5159 *pm = m;
5160 pm = &m->next;
5161 }
65765700 5162
ceae84aa 5163 if (info != NULL && info->relro)
8ded5a0f 5164 {
f210dcff
L
5165 for (m = mfirst; m != NULL; m = m->next)
5166 {
3832a4d8
AM
5167 if (m->p_type == PT_LOAD
5168 && m->count != 0
5169 && m->sections[0]->vma >= info->relro_start
5170 && m->sections[0]->vma < info->relro_end)
f210dcff 5171 {
3832a4d8
AM
5172 i = m->count;
5173 while (--i != (unsigned) -1)
ec2e748a
NC
5174 {
5175 if (m->sections[i]->size > 0
5176 && (m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
5177 == (SEC_LOAD | SEC_HAS_CONTENTS))
5178 break;
5179 }
3832a4d8 5180
43a8475c 5181 if (i != (unsigned) -1)
f210dcff
L
5182 break;
5183 }
be01b344 5184 }
f210dcff
L
5185
5186 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5187 if (m != NULL)
5188 {
5189 amt = sizeof (struct elf_segment_map);
a50b1753 5190 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
f210dcff
L
5191 if (m == NULL)
5192 goto error_return;
5193 m->next = NULL;
5194 m->p_type = PT_GNU_RELRO;
f210dcff
L
5195 *pm = m;
5196 pm = &m->next;
5197 }
8ded5a0f 5198 }
9ee5e499 5199
8ded5a0f 5200 free (sections);
12bd6957 5201 elf_seg_map (abfd) = mfirst;
9ee5e499
JJ
5202 }
5203
3dea8fca 5204 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
8ded5a0f 5205 return FALSE;
8c37241b 5206
12bd6957 5207 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
8ded5a0f 5208 ++count;
12bd6957 5209 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
252b5132 5210
b34976b6 5211 return TRUE;
252b5132
RH
5212
5213 error_return:
c9594989 5214 free (sections);
b34976b6 5215 return FALSE;
252b5132
RH
5216}
5217
5218/* Sort sections by address. */
5219
5220static int
217aa764 5221elf_sort_sections (const void *arg1, const void *arg2)
252b5132
RH
5222{
5223 const asection *sec1 = *(const asection **) arg1;
5224 const asection *sec2 = *(const asection **) arg2;
eecdbe52 5225 bfd_size_type size1, size2;
252b5132
RH
5226
5227 /* Sort by LMA first, since this is the address used to
5228 place the section into a segment. */
5229 if (sec1->lma < sec2->lma)
5230 return -1;
5231 else if (sec1->lma > sec2->lma)
5232 return 1;
5233
5234 /* Then sort by VMA. Normally the LMA and the VMA will be
5235 the same, and this will do nothing. */
5236 if (sec1->vma < sec2->vma)
5237 return -1;
5238 else if (sec1->vma > sec2->vma)
5239 return 1;
5240
5241 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5242
8d748d1d
AM
5243#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5244 && (x)->size != 0)
252b5132
RH
5245
5246 if (TOEND (sec1))
5247 {
48db3297 5248 if (!TOEND (sec2))
252b5132
RH
5249 return 1;
5250 }
00a7cdc5 5251 else if (TOEND (sec2))
252b5132
RH
5252 return -1;
5253
5254#undef TOEND
5255
00a7cdc5
NC
5256 /* Sort by size, to put zero sized sections
5257 before others at the same address. */
252b5132 5258
eea6121a
AM
5259 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5260 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
eecdbe52
JJ
5261
5262 if (size1 < size2)
252b5132 5263 return -1;
eecdbe52 5264 if (size1 > size2)
252b5132
RH
5265 return 1;
5266
5267 return sec1->target_index - sec2->target_index;
5268}
5269
30fe1832
AM
5270/* This qsort comparison functions sorts PT_LOAD segments first and
5271 by p_paddr, for assign_file_positions_for_load_sections. */
5272
5273static int
5274elf_sort_segments (const void *arg1, const void *arg2)
5275{
5276 const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5277 const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5278
5279 if (m1->p_type != m2->p_type)
5280 {
5281 if (m1->p_type == PT_NULL)
5282 return 1;
5283 if (m2->p_type == PT_NULL)
5284 return -1;
5285 return m1->p_type < m2->p_type ? -1 : 1;
5286 }
5287 if (m1->includes_filehdr != m2->includes_filehdr)
5288 return m1->includes_filehdr ? -1 : 1;
5289 if (m1->no_sort_lma != m2->no_sort_lma)
5290 return m1->no_sort_lma ? -1 : 1;
5291 if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5292 {
4b3ecb3b 5293 bfd_vma lma1, lma2; /* Octets. */
30fe1832
AM
5294 lma1 = 0;
5295 if (m1->p_paddr_valid)
4b3ecb3b 5296 lma1 = m1->p_paddr;
30fe1832 5297 else if (m1->count != 0)
4b3ecb3b
AM
5298 {
5299 unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5300 m1->sections[0]);
5301 lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5302 }
30fe1832
AM
5303 lma2 = 0;
5304 if (m2->p_paddr_valid)
4b3ecb3b 5305 lma2 = m2->p_paddr;
30fe1832 5306 else if (m2->count != 0)
4b3ecb3b
AM
5307 {
5308 unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5309 m2->sections[0]);
5310 lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5311 }
30fe1832
AM
5312 if (lma1 != lma2)
5313 return lma1 < lma2 ? -1 : 1;
5314 }
5315 if (m1->idx != m2->idx)
5316 return m1->idx < m2->idx ? -1 : 1;
5317 return 0;
5318}
5319
340b6d91
AC
5320/* Ian Lance Taylor writes:
5321
5322 We shouldn't be using % with a negative signed number. That's just
5323 not good. We have to make sure either that the number is not
5324 negative, or that the number has an unsigned type. When the types
5325 are all the same size they wind up as unsigned. When file_ptr is a
5326 larger signed type, the arithmetic winds up as signed long long,
5327 which is wrong.
5328
5329 What we're trying to say here is something like ``increase OFF by
5330 the least amount that will cause it to be equal to the VMA modulo
5331 the page size.'' */
5332/* In other words, something like:
5333
5334 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5335 off_offset = off % bed->maxpagesize;
5336 if (vma_offset < off_offset)
5337 adjustment = vma_offset + bed->maxpagesize - off_offset;
5338 else
5339 adjustment = vma_offset - off_offset;
08a40648 5340
de194d85 5341 which can be collapsed into the expression below. */
340b6d91
AC
5342
5343static file_ptr
5344vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5345{
dc9155b2
NC
5346 /* PR binutils/16199: Handle an alignment of zero. */
5347 if (maxpagesize == 0)
5348 maxpagesize = 1;
340b6d91
AC
5349 return ((vma - off) % maxpagesize);
5350}
5351
6d33f217
L
5352static void
5353print_segment_map (const struct elf_segment_map *m)
5354{
5355 unsigned int j;
5356 const char *pt = get_segment_type (m->p_type);
5357 char buf[32];
5358
5359 if (pt == NULL)
5360 {
5361 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5362 sprintf (buf, "LOPROC+%7.7x",
5363 (unsigned int) (m->p_type - PT_LOPROC));
5364 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5365 sprintf (buf, "LOOS+%7.7x",
5366 (unsigned int) (m->p_type - PT_LOOS));
5367 else
5368 snprintf (buf, sizeof (buf), "%8.8x",
5369 (unsigned int) m->p_type);
5370 pt = buf;
5371 }
4a97a0e5 5372 fflush (stdout);
6d33f217
L
5373 fprintf (stderr, "%s:", pt);
5374 for (j = 0; j < m->count; j++)
5375 fprintf (stderr, " %s", m->sections [j]->name);
5376 putc ('\n',stderr);
4a97a0e5 5377 fflush (stderr);
6d33f217
L
5378}
5379
32812159
AM
5380static bfd_boolean
5381write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
5382{
5383 void *buf;
5384 bfd_boolean ret;
5385
5386 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5387 return FALSE;
5388 buf = bfd_zmalloc (len);
5389 if (buf == NULL)
5390 return FALSE;
5391 ret = bfd_bwrite (buf, len, abfd) == len;
5392 free (buf);
5393 return ret;
5394}
5395
252b5132
RH
5396/* Assign file positions to the sections based on the mapping from
5397 sections to segments. This function also sets up some fields in
f3520d2f 5398 the file header. */
252b5132 5399
b34976b6 5400static bfd_boolean
f3520d2f
AM
5401assign_file_positions_for_load_sections (bfd *abfd,
5402 struct bfd_link_info *link_info)
252b5132
RH
5403{
5404 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 5405 struct elf_segment_map *m;
30fe1832 5406 struct elf_segment_map *phdr_load_seg;
252b5132 5407 Elf_Internal_Phdr *phdrs;
252b5132 5408 Elf_Internal_Phdr *p;
502794d4 5409 file_ptr off; /* Octets. */
3f570048 5410 bfd_size_type maxpagesize;
30fe1832 5411 unsigned int alloc, actual;
0920dee7 5412 unsigned int i, j;
30fe1832 5413 struct elf_segment_map **sorted_seg_map;
502794d4 5414 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132 5415
e36284ab 5416 if (link_info == NULL
ceae84aa 5417 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
8ded5a0f 5418 return FALSE;
252b5132 5419
8ded5a0f 5420 alloc = 0;
12bd6957 5421 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
30fe1832 5422 m->idx = alloc++;
252b5132 5423
82f2dbf7
NC
5424 if (alloc)
5425 {
5426 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5427 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5428 }
5429 else
5430 {
5431 /* PR binutils/12467. */
5432 elf_elfheader (abfd)->e_phoff = 0;
5433 elf_elfheader (abfd)->e_phentsize = 0;
5434 }
d324f6d6 5435
8ded5a0f 5436 elf_elfheader (abfd)->e_phnum = alloc;
252b5132 5437
12bd6957 5438 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
30fe1832
AM
5439 {
5440 actual = alloc;
5441 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5442 }
8ded5a0f 5443 else
30fe1832
AM
5444 {
5445 actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5446 BFD_ASSERT (elf_program_header_size (abfd)
5447 == actual * bed->s->sizeof_phdr);
5448 BFD_ASSERT (actual >= alloc);
5449 }
252b5132
RH
5450
5451 if (alloc == 0)
f3520d2f 5452 {
12bd6957 5453 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
8ded5a0f 5454 return TRUE;
f3520d2f 5455 }
252b5132 5456
12bd6957 5457 /* We're writing the size in elf_program_header_size (abfd),
57268894
HPN
5458 see assign_file_positions_except_relocs, so make sure we have
5459 that amount allocated, with trailing space cleared.
12bd6957
AM
5460 The variable alloc contains the computed need, while
5461 elf_program_header_size (abfd) contains the size used for the
57268894
HPN
5462 layout.
5463 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5464 where the layout is forced to according to a larger size in the
5465 last iterations for the testcase ld-elf/header. */
30fe1832
AM
5466 phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
5467 + alloc * sizeof (*sorted_seg_map)));
5468 sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
f3520d2f 5469 elf_tdata (abfd)->phdr = phdrs;
252b5132 5470 if (phdrs == NULL)
b34976b6 5471 return FALSE;
252b5132 5472
30fe1832 5473 for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
252b5132 5474 {
30fe1832 5475 sorted_seg_map[j] = m;
252b5132 5476 /* If elf_segment_map is not from map_sections_to_segments, the
08a40648 5477 sections may not be correctly ordered. NOTE: sorting should
52e9b619
MS
5478 not be done to the PT_NOTE section of a corefile, which may
5479 contain several pseudo-sections artificially created by bfd.
5480 Sorting these pseudo-sections breaks things badly. */
47d9a591
AM
5481 if (m->count > 1
5482 && !(elf_elfheader (abfd)->e_type == ET_CORE
52e9b619 5483 && m->p_type == PT_NOTE))
48db3297
AM
5484 {
5485 for (i = 0; i < m->count; i++)
5486 m->sections[i]->target_index = i;
5487 qsort (m->sections, (size_t) m->count, sizeof (asection *),
5488 elf_sort_sections);
5489 }
30fe1832
AM
5490 }
5491 if (alloc > 1)
5492 qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
5493 elf_sort_segments);
5494
5495 maxpagesize = 1;
5496 if ((abfd->flags & D_PAGED) != 0)
c410035d
AM
5497 {
5498 if (link_info != NULL)
5499 maxpagesize = link_info->maxpagesize;
5500 else
5501 maxpagesize = bed->maxpagesize;
5502 }
30fe1832
AM
5503
5504 /* Sections must map to file offsets past the ELF file header. */
5505 off = bed->s->sizeof_ehdr;
5506 /* And if one of the PT_LOAD headers doesn't include the program
5507 headers then we'll be mapping program headers in the usual
5508 position after the ELF file header. */
5509 phdr_load_seg = NULL;
5510 for (j = 0; j < alloc; j++)
5511 {
5512 m = sorted_seg_map[j];
5513 if (m->p_type != PT_LOAD)
5514 break;
5515 if (m->includes_phdrs)
5516 {
5517 phdr_load_seg = m;
5518 break;
5519 }
5520 }
5521 if (phdr_load_seg == NULL)
5522 off += actual * bed->s->sizeof_phdr;
5523
5524 for (j = 0; j < alloc; j++)
5525 {
5526 asection **secpp;
502794d4 5527 bfd_vma off_adjust; /* Octets. */
30fe1832 5528 bfd_boolean no_contents;
252b5132 5529
b301b248
AM
5530 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5531 number of sections with contents contributing to both p_filesz
5532 and p_memsz, followed by a number of sections with no contents
5533 that just contribute to p_memsz. In this loop, OFF tracks next
02bf8d82 5534 available file offset for PT_LOAD and PT_NOTE segments. */
30fe1832
AM
5535 m = sorted_seg_map[j];
5536 p = phdrs + m->idx;
252b5132 5537 p->p_type = m->p_type;
28a7f3e7 5538 p->p_flags = m->p_flags;
252b5132 5539
3f570048 5540 if (m->count == 0)
502794d4 5541 p->p_vaddr = m->p_vaddr_offset * opb;
3f570048 5542 else
502794d4 5543 p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
3f570048
AM
5544
5545 if (m->p_paddr_valid)
5546 p->p_paddr = m->p_paddr;
5547 else if (m->count == 0)
5548 p->p_paddr = 0;
5549 else
502794d4 5550 p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
3f570048
AM
5551
5552 if (p->p_type == PT_LOAD
5553 && (abfd->flags & D_PAGED) != 0)
5554 {
5555 /* p_align in demand paged PT_LOAD segments effectively stores
5556 the maximum page size. When copying an executable with
5557 objcopy, we set m->p_align from the input file. Use this
5558 value for maxpagesize rather than bed->maxpagesize, which
5559 may be different. Note that we use maxpagesize for PT_TLS
5560 segment alignment later in this function, so we are relying
5561 on at least one PT_LOAD segment appearing before a PT_TLS
5562 segment. */
5563 if (m->p_align_valid)
5564 maxpagesize = m->p_align;
5565
5566 p->p_align = maxpagesize;
5567 }
3271a814
NS
5568 else if (m->p_align_valid)
5569 p->p_align = m->p_align;
e970b90a
DJ
5570 else if (m->count == 0)
5571 p->p_align = 1 << bed->s->log_file_align;
30fe1832
AM
5572
5573 if (m == phdr_load_seg)
5574 {
5575 if (!m->includes_filehdr)
5576 p->p_offset = off;
5577 off += actual * bed->s->sizeof_phdr;
5578 }
3f570048 5579
bf988460
AM
5580 no_contents = FALSE;
5581 off_adjust = 0;
252b5132 5582 if (p->p_type == PT_LOAD
b301b248 5583 && m->count > 0)
252b5132 5584 {
66631823 5585 bfd_size_type align; /* Bytes. */
a49e53ed 5586 unsigned int align_power = 0;
b301b248 5587
3271a814
NS
5588 if (m->p_align_valid)
5589 align = p->p_align;
5590 else
252b5132 5591 {
3271a814
NS
5592 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5593 {
5594 unsigned int secalign;
08a40648 5595
fd361982 5596 secalign = bfd_section_alignment (*secpp);
3271a814
NS
5597 if (secalign > align_power)
5598 align_power = secalign;
5599 }
5600 align = (bfd_size_type) 1 << align_power;
5601 if (align < maxpagesize)
5602 align = maxpagesize;
b301b248 5603 }
252b5132 5604
02bf8d82
AM
5605 for (i = 0; i < m->count; i++)
5606 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
5607 /* If we aren't making room for this section, then
5608 it must be SHT_NOBITS regardless of what we've
5609 set via struct bfd_elf_special_section. */
5610 elf_section_type (m->sections[i]) = SHT_NOBITS;
5611
bf988460 5612 /* Find out whether this segment contains any loadable
aea274d3
AM
5613 sections. */
5614 no_contents = TRUE;
5615 for (i = 0; i < m->count; i++)
5616 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
5617 {
5618 no_contents = FALSE;
5619 break;
5620 }
bf988460 5621
66631823 5622 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
a8c75b76
AM
5623
5624 /* Broken hardware and/or kernel require that files do not
5625 map the same page with different permissions on some hppa
5626 processors. */
30fe1832
AM
5627 if (j != 0
5628 && (abfd->flags & D_PAGED) != 0
a8c75b76
AM
5629 && bed->no_page_alias
5630 && (off & (maxpagesize - 1)) != 0
502794d4
CE
5631 && ((off & -maxpagesize)
5632 == ((off + off_adjust) & -maxpagesize)))
a8c75b76 5633 off_adjust += maxpagesize;
bf988460
AM
5634 off += off_adjust;
5635 if (no_contents)
5636 {
5637 /* We shouldn't need to align the segment on disk since
5638 the segment doesn't need file space, but the gABI
5639 arguably requires the alignment and glibc ld.so
5640 checks it. So to comply with the alignment
5641 requirement but not waste file space, we adjust
5642 p_offset for just this segment. (OFF_ADJUST is
5643 subtracted from OFF later.) This may put p_offset
5644 past the end of file, but that shouldn't matter. */
5645 }
5646 else
5647 off_adjust = 0;
252b5132 5648 }
b1a6d0b1
NC
5649 /* Make sure the .dynamic section is the first section in the
5650 PT_DYNAMIC segment. */
5651 else if (p->p_type == PT_DYNAMIC
5652 && m->count > 1
5653 && strcmp (m->sections[0]->name, ".dynamic") != 0)
5654 {
5655 _bfd_error_handler
871b3ab2 5656 (_("%pB: The first section in the PT_DYNAMIC segment"
63a5468a 5657 " is not the .dynamic section"),
b301b248 5658 abfd);
b1a6d0b1
NC
5659 bfd_set_error (bfd_error_bad_value);
5660 return FALSE;
5661 }
3f001e84
JK
5662 /* Set the note section type to SHT_NOTE. */
5663 else if (p->p_type == PT_NOTE)
5664 for (i = 0; i < m->count; i++)
5665 elf_section_type (m->sections[i]) = SHT_NOTE;
252b5132 5666
252b5132
RH
5667 if (m->includes_filehdr)
5668 {
bf988460 5669 if (!m->p_flags_valid)
252b5132 5670 p->p_flags |= PF_R;
252b5132
RH
5671 p->p_filesz = bed->s->sizeof_ehdr;
5672 p->p_memsz = bed->s->sizeof_ehdr;
30fe1832 5673 if (p->p_type == PT_LOAD)
252b5132 5674 {
30fe1832 5675 if (m->count > 0)
252b5132 5676 {
30fe1832
AM
5677 if (p->p_vaddr < (bfd_vma) off
5678 || (!m->p_paddr_valid
5679 && p->p_paddr < (bfd_vma) off))
5680 {
5681 _bfd_error_handler
5682 (_("%pB: not enough room for program headers,"
5683 " try linking with -N"),
5684 abfd);
5685 bfd_set_error (bfd_error_bad_value);
5686 return FALSE;
5687 }
5688 p->p_vaddr -= off;
5689 if (!m->p_paddr_valid)
5690 p->p_paddr -= off;
252b5132 5691 }
30fe1832
AM
5692 }
5693 else if (sorted_seg_map[0]->includes_filehdr)
5694 {
5695 Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
5696 p->p_vaddr = filehdr->p_vaddr;
bf988460 5697 if (!m->p_paddr_valid)
30fe1832 5698 p->p_paddr = filehdr->p_paddr;
252b5132 5699 }
252b5132
RH
5700 }
5701
5702 if (m->includes_phdrs)
5703 {
bf988460 5704 if (!m->p_flags_valid)
252b5132 5705 p->p_flags |= PF_R;
30fe1832
AM
5706 p->p_filesz += actual * bed->s->sizeof_phdr;
5707 p->p_memsz += actual * bed->s->sizeof_phdr;
f3520d2f 5708 if (!m->includes_filehdr)
252b5132 5709 {
30fe1832 5710 if (p->p_type == PT_LOAD)
252b5132 5711 {
30fe1832
AM
5712 elf_elfheader (abfd)->e_phoff = p->p_offset;
5713 if (m->count > 0)
5714 {
5715 p->p_vaddr -= off - p->p_offset;
5716 if (!m->p_paddr_valid)
5717 p->p_paddr -= off - p->p_offset;
5718 }
5719 }
5720 else if (phdr_load_seg != NULL)
5721 {
5722 Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
502794d4 5723 bfd_vma phdr_off = 0; /* Octets. */
30fe1832
AM
5724 if (phdr_load_seg->includes_filehdr)
5725 phdr_off = bed->s->sizeof_ehdr;
5726 p->p_vaddr = phdr->p_vaddr + phdr_off;
bf988460 5727 if (!m->p_paddr_valid)
30fe1832
AM
5728 p->p_paddr = phdr->p_paddr + phdr_off;
5729 p->p_offset = phdr->p_offset + phdr_off;
252b5132 5730 }
30fe1832
AM
5731 else
5732 p->p_offset = bed->s->sizeof_ehdr;
2b0bc088 5733 }
252b5132
RH
5734 }
5735
5736 if (p->p_type == PT_LOAD
5737 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5738 {
bf988460 5739 if (!m->includes_filehdr && !m->includes_phdrs)
0bc3450e
AM
5740 {
5741 p->p_offset = off;
5742 if (no_contents)
67641dd3
AM
5743 {
5744 /* Put meaningless p_offset for PT_LOAD segments
5745 without file contents somewhere within the first
5746 page, in an attempt to not point past EOF. */
5747 bfd_size_type align = maxpagesize;
5748 if (align < p->p_align)
5749 align = p->p_align;
5750 if (align < 1)
5751 align = 1;
5752 p->p_offset = off % align;
5753 }
0bc3450e 5754 }
252b5132
RH
5755 else
5756 {
502794d4 5757 file_ptr adjust; /* Octets. */
252b5132
RH
5758
5759 adjust = off - (p->p_offset + p->p_filesz);
bf988460
AM
5760 if (!no_contents)
5761 p->p_filesz += adjust;
252b5132
RH
5762 p->p_memsz += adjust;
5763 }
5764 }
5765
1ea63fd2
AM
5766 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5767 maps. Set filepos for sections in PT_LOAD segments, and in
5768 core files, for sections in PT_NOTE segments.
5769 assign_file_positions_for_non_load_sections will set filepos
5770 for other sections and update p_filesz for other segments. */
252b5132
RH
5771 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5772 {
5773 asection *sec;
252b5132 5774 bfd_size_type align;
627b32bc 5775 Elf_Internal_Shdr *this_hdr;
252b5132
RH
5776
5777 sec = *secpp;
02bf8d82 5778 this_hdr = &elf_section_data (sec)->this_hdr;
fd361982 5779 align = (bfd_size_type) 1 << bfd_section_alignment (sec);
252b5132 5780
88967714
AM
5781 if ((p->p_type == PT_LOAD
5782 || p->p_type == PT_TLS)
5783 && (this_hdr->sh_type != SHT_NOBITS
5784 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5785 && ((this_hdr->sh_flags & SHF_TLS) == 0
5786 || p->p_type == PT_TLS))))
252b5132 5787 {
502794d4
CE
5788 bfd_vma p_start = p->p_paddr; /* Octets. */
5789 bfd_vma p_end = p_start + p->p_memsz; /* Octets. */
5790 bfd_vma s_start = sec->lma * opb; /* Octets. */
5791 bfd_vma adjust = s_start - p_end; /* Octets. */
252b5132 5792
a2d1e028
L
5793 if (adjust != 0
5794 && (s_start < p_end
5795 || p_end < p_start))
252b5132 5796 {
4eca0228 5797 _bfd_error_handler
695344c0 5798 /* xgettext:c-format */
2dcf00ce 5799 (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64),
502794d4
CE
5800 abfd, sec, (uint64_t) s_start / opb,
5801 (uint64_t) p_end / opb);
88967714 5802 adjust = 0;
502794d4 5803 sec->lma = p_end / opb;
1cfb7d1e 5804 }
3ac9b6c9 5805 p->p_memsz += adjust;
1cfb7d1e 5806
d16e3d2e 5807 if (p->p_type == PT_LOAD)
88967714 5808 {
d16e3d2e 5809 if (this_hdr->sh_type != SHT_NOBITS)
32812159 5810 {
d16e3d2e 5811 off_adjust = 0;
30fe1832
AM
5812 if (p->p_filesz + adjust < p->p_memsz)
5813 {
5814 /* We have a PROGBITS section following NOBITS ones.
5815 Allocate file space for the NOBITS section(s) and
5816 zero it. */
5817 adjust = p->p_memsz - p->p_filesz;
5818 if (!write_zeros (abfd, off, adjust))
5819 return FALSE;
5820 }
d16e3d2e
AM
5821 }
5822 /* We only adjust sh_offset in SHT_NOBITS sections
5823 as would seem proper for their address when the
5824 section is first in the segment. sh_offset
5825 doesn't really have any significance for
5826 SHT_NOBITS anyway, apart from a notional position
5827 relative to other sections. Historically we
5828 didn't bother with adjusting sh_offset and some
5829 programs depend on it not being adjusted. See
5830 pr12921 and pr25662. */
5831 if (this_hdr->sh_type != SHT_NOBITS || i == 0)
5832 {
30fe1832 5833 off += adjust;
d16e3d2e
AM
5834 if (this_hdr->sh_type == SHT_NOBITS)
5835 off_adjust += adjust;
32812159 5836 }
252b5132 5837 }
d16e3d2e
AM
5838 if (this_hdr->sh_type != SHT_NOBITS)
5839 p->p_filesz += adjust;
252b5132
RH
5840 }
5841
5842 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5843 {
b301b248
AM
5844 /* The section at i == 0 is the one that actually contains
5845 everything. */
4a938328
MS
5846 if (i == 0)
5847 {
627b32bc 5848 this_hdr->sh_offset = sec->filepos = off;
6a3cd2b4
AM
5849 off += this_hdr->sh_size;
5850 p->p_filesz = this_hdr->sh_size;
b301b248
AM
5851 p->p_memsz = 0;
5852 p->p_align = 1;
252b5132 5853 }
4a938328 5854 else
252b5132 5855 {
b301b248 5856 /* The rest are fake sections that shouldn't be written. */
252b5132 5857 sec->filepos = 0;
eea6121a 5858 sec->size = 0;
b301b248
AM
5859 sec->flags = 0;
5860 continue;
252b5132 5861 }
252b5132
RH
5862 }
5863 else
5864 {
1e951488 5865 if (p->p_type == PT_LOAD)
b301b248 5866 {
1e951488
AM
5867 this_hdr->sh_offset = sec->filepos = off;
5868 if (this_hdr->sh_type != SHT_NOBITS)
5869 off += this_hdr->sh_size;
5870 }
5871 else if (this_hdr->sh_type == SHT_NOBITS
5872 && (this_hdr->sh_flags & SHF_TLS) != 0
5873 && this_hdr->sh_offset == 0)
5874 {
5875 /* This is a .tbss section that didn't get a PT_LOAD.
5876 (See _bfd_elf_map_sections_to_segments "Create a
5877 final PT_LOAD".) Set sh_offset to the value it
5878 would have if we had created a zero p_filesz and
5879 p_memsz PT_LOAD header for the section. This
5880 also makes the PT_TLS header have the same
5881 p_offset value. */
5882 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5883 off, align);
5884 this_hdr->sh_offset = sec->filepos = off + adjust;
b301b248 5885 }
252b5132 5886
02bf8d82 5887 if (this_hdr->sh_type != SHT_NOBITS)
b301b248 5888 {
6a3cd2b4 5889 p->p_filesz += this_hdr->sh_size;
02bf8d82
AM
5890 /* A load section without SHF_ALLOC is something like
5891 a note section in a PT_NOTE segment. These take
5892 file space but are not loaded into memory. */
5893 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6a3cd2b4 5894 p->p_memsz += this_hdr->sh_size;
b301b248 5895 }
6a3cd2b4 5896 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
13ae64f3 5897 {
6a3cd2b4
AM
5898 if (p->p_type == PT_TLS)
5899 p->p_memsz += this_hdr->sh_size;
5900
5901 /* .tbss is special. It doesn't contribute to p_memsz of
5902 normal segments. */
5903 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
5904 p->p_memsz += this_hdr->sh_size;
13ae64f3
JJ
5905 }
5906
b10a8ae0
L
5907 if (align > p->p_align
5908 && !m->p_align_valid
5909 && (p->p_type != PT_LOAD
5910 || (abfd->flags & D_PAGED) == 0))
252b5132
RH
5911 p->p_align = align;
5912 }
5913
bf988460 5914 if (!m->p_flags_valid)
252b5132
RH
5915 {
5916 p->p_flags |= PF_R;
02bf8d82 5917 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
252b5132 5918 p->p_flags |= PF_X;
02bf8d82 5919 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
252b5132
RH
5920 p->p_flags |= PF_W;
5921 }
5922 }
43a8475c 5923
bf988460 5924 off -= off_adjust;
0920dee7 5925
30fe1832
AM
5926 /* PR ld/20815 - Check that the program header segment, if
5927 present, will be loaded into memory. */
5928 if (p->p_type == PT_PHDR
5929 && phdr_load_seg == NULL
5930 && !(bed->elf_backend_allow_non_load_phdr != NULL
5931 && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
5932 {
5933 /* The fix for this error is usually to edit the linker script being
5934 used and set up the program headers manually. Either that or
5935 leave room for the headers at the start of the SECTIONS. */
5936 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
5937 " by LOAD segment"),
5938 abfd);
7b3c2715
AM
5939 if (link_info == NULL)
5940 return FALSE;
5941 /* Arrange for the linker to exit with an error, deleting
5942 the output file unless --noinhibit-exec is given. */
5943 link_info->callbacks->info ("%X");
30fe1832
AM
5944 }
5945
7c928300
AM
5946 /* Check that all sections are in a PT_LOAD segment.
5947 Don't check funky gdb generated core files. */
5948 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
9a83a553
AM
5949 {
5950 bfd_boolean check_vma = TRUE;
5951
5952 for (i = 1; i < m->count; i++)
5953 if (m->sections[i]->vma == m->sections[i - 1]->vma
5954 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
5955 ->this_hdr), p) != 0
5956 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
5957 ->this_hdr), p) != 0)
0920dee7 5958 {
9a83a553
AM
5959 /* Looks like we have overlays packed into the segment. */
5960 check_vma = FALSE;
5961 break;
0920dee7 5962 }
9a83a553
AM
5963
5964 for (i = 0; i < m->count; i++)
5965 {
5966 Elf_Internal_Shdr *this_hdr;
5967 asection *sec;
5968
5969 sec = m->sections[i];
5970 this_hdr = &(elf_section_data(sec)->this_hdr);
86b2281f
AM
5971 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
5972 && !ELF_TBSS_SPECIAL (this_hdr, p))
9a83a553 5973 {
4eca0228 5974 _bfd_error_handler
695344c0 5975 /* xgettext:c-format */
871b3ab2 5976 (_("%pB: section `%pA' can't be allocated in segment %d"),
9a83a553
AM
5977 abfd, sec, j);
5978 print_segment_map (m);
5979 }
5980 }
5981 }
252b5132
RH
5982 }
5983
12bd6957 5984 elf_next_file_pos (abfd) = off;
30fe1832
AM
5985
5986 if (link_info != NULL
5987 && phdr_load_seg != NULL
5988 && phdr_load_seg->includes_filehdr)
5989 {
5990 /* There is a segment that contains both the file headers and the
5991 program headers, so provide a symbol __ehdr_start pointing there.
5992 A program can use this to examine itself robustly. */
5993
5994 struct elf_link_hash_entry *hash
5995 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
5996 FALSE, FALSE, TRUE);
5997 /* If the symbol was referenced and not defined, define it. */
5998 if (hash != NULL
5999 && (hash->root.type == bfd_link_hash_new
6000 || hash->root.type == bfd_link_hash_undefined
6001 || hash->root.type == bfd_link_hash_undefweak
6002 || hash->root.type == bfd_link_hash_common))
6003 {
6004 asection *s = NULL;
66631823 6005 bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
30fe1832
AM
6006
6007 if (phdr_load_seg->count != 0)
6008 /* The segment contains sections, so use the first one. */
6009 s = phdr_load_seg->sections[0];
6010 else
6011 /* Use the first (i.e. lowest-addressed) section in any segment. */
6012 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6013 if (m->p_type == PT_LOAD && m->count != 0)
6014 {
6015 s = m->sections[0];
6016 break;
6017 }
6018
6019 if (s != NULL)
6020 {
6021 hash->root.u.def.value = filehdr_vaddr - s->vma;
6022 hash->root.u.def.section = s;
6023 }
6024 else
6025 {
6026 hash->root.u.def.value = filehdr_vaddr;
6027 hash->root.u.def.section = bfd_abs_section_ptr;
6028 }
6029
6030 hash->root.type = bfd_link_hash_defined;
6031 hash->def_regular = 1;
6032 hash->non_elf = 0;
6033 }
6034 }
6035
f3520d2f
AM
6036 return TRUE;
6037}
6038
1faa385f
NC
6039/* Determine if a bfd is a debuginfo file. Unfortunately there
6040 is no defined method for detecting such files, so we have to
6041 use heuristics instead. */
6042
6043bfd_boolean
6044is_debuginfo_file (bfd *abfd)
6045{
6046 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6047 return FALSE;
6048
6049 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6050 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6051 Elf_Internal_Shdr **headerp;
6052
6053 for (headerp = start_headers; headerp < end_headers; headerp ++)
6054 {
6055 Elf_Internal_Shdr *header = * headerp;
6056
6057 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6058 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6059 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6060 && header->sh_type != SHT_NOBITS
6061 && header->sh_type != SHT_NOTE)
6062 return FALSE;
6063 }
6064
6065 return TRUE;
6066}
6067
1ff6de03
NA
6068/* Assign file positions for the other sections, except for compressed debugging
6069 and other sections assigned in _bfd_elf_assign_file_positions_for_non_load(). */
f3520d2f
AM
6070
6071static bfd_boolean
6072assign_file_positions_for_non_load_sections (bfd *abfd,
6073 struct bfd_link_info *link_info)
6074{
6075 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6076 Elf_Internal_Shdr **i_shdrpp;
e06efbf1 6077 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
f3520d2f
AM
6078 Elf_Internal_Phdr *phdrs;
6079 Elf_Internal_Phdr *p;
6080 struct elf_segment_map *m;
f3520d2f 6081 file_ptr off;
66631823 6082 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
c410035d 6083 bfd_vma maxpagesize;
f3520d2f 6084
c410035d
AM
6085 if (link_info != NULL)
6086 maxpagesize = link_info->maxpagesize;
6087 else
6088 maxpagesize = bed->maxpagesize;
5c182d5f 6089 i_shdrpp = elf_elfsections (abfd);
e06efbf1 6090 end_hdrpp = i_shdrpp + elf_numsections (abfd);
12bd6957 6091 off = elf_next_file_pos (abfd);
e06efbf1 6092 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
5c182d5f 6093 {
5c182d5f
AM
6094 Elf_Internal_Shdr *hdr;
6095
6096 hdr = *hdrpp;
6097 if (hdr->bfd_section != NULL
252e386e
AM
6098 && (hdr->bfd_section->filepos != 0
6099 || (hdr->sh_type == SHT_NOBITS
6100 && hdr->contents == NULL)))
627b32bc 6101 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
5c182d5f
AM
6102 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6103 {
1faa385f
NC
6104 if (hdr->sh_size != 0
6105 /* PR 24717 - debuginfo files are known to be not strictly
6106 compliant with the ELF standard. In particular they often
6107 have .note.gnu.property sections that are outside of any
6108 loadable segment. This is not a problem for such files,
6109 so do not warn about them. */
6110 && ! is_debuginfo_file (abfd))
4eca0228 6111 _bfd_error_handler
695344c0 6112 /* xgettext:c-format */
871b3ab2 6113 (_("%pB: warning: allocated section `%s' not in segment"),
e8d2ba53
AM
6114 abfd,
6115 (hdr->bfd_section == NULL
6116 ? "*unknown*"
6117 : hdr->bfd_section->name));
3ba71138
L
6118 /* We don't need to page align empty sections. */
6119 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
5c182d5f 6120 off += vma_page_aligned_bias (hdr->sh_addr, off,
c410035d 6121 maxpagesize);
5c182d5f
AM
6122 else
6123 off += vma_page_aligned_bias (hdr->sh_addr, off,
6124 hdr->sh_addralign);
6125 off = _bfd_elf_assign_file_position_for_section (hdr, off,
6126 FALSE);
6127 }
6128 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6129 && hdr->bfd_section == NULL)
1ff6de03
NA
6130 /* We don't know the offset of these sections yet: their size has
6131 not been decided. */
0ce398f1 6132 || (hdr->bfd_section != NULL
1ff6de03
NA
6133 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
6134 || (bfd_section_is_ctf (hdr->bfd_section)
6135 && abfd->is_linker_output)))
12bd6957 6136 || hdr == i_shdrpp[elf_onesymtab (abfd)]
6a40cf0c
NC
6137 || (elf_symtab_shndx_list (abfd) != NULL
6138 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
3e19fb8f
L
6139 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6140 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
5c182d5f
AM
6141 hdr->sh_offset = -1;
6142 else
6143 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5c182d5f 6144 }
30fe1832 6145 elf_next_file_pos (abfd) = off;
5c182d5f 6146
252b5132
RH
6147 /* Now that we have set the section file positions, we can set up
6148 the file positions for the non PT_LOAD segments. */
f3520d2f 6149 phdrs = elf_tdata (abfd)->phdr;
12bd6957 6150 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
252b5132 6151 {
129af99f 6152 if (p->p_type == PT_GNU_RELRO)
252b5132 6153 {
66631823 6154 bfd_vma start, end; /* Bytes. */
01f7e10c 6155 bfd_boolean ok;
1ea63fd2 6156
129af99f 6157 if (link_info != NULL)
8c37241b 6158 {
129af99f 6159 /* During linking the range of the RELRO segment is passed
f2731e0c
AM
6160 in link_info. Note that there may be padding between
6161 relro_start and the first RELRO section. */
6162 start = link_info->relro_start;
6163 end = link_info->relro_end;
6164 }
6165 else if (m->count != 0)
6166 {
6167 if (!m->p_size_valid)
6168 abort ();
6169 start = m->sections[0]->vma;
66631823 6170 end = start + m->p_size / opb;
f2731e0c
AM
6171 }
6172 else
6173 {
6174 start = 0;
6175 end = 0;
6176 }
6177
01f7e10c 6178 ok = FALSE;
f2731e0c
AM
6179 if (start < end)
6180 {
6181 struct elf_segment_map *lm;
6182 const Elf_Internal_Phdr *lp;
6183 unsigned int i;
6184
6185 /* Find a LOAD segment containing a section in the RELRO
6186 segment. */
12bd6957 6187 for (lm = elf_seg_map (abfd), lp = phdrs;
3146fac4
AM
6188 lm != NULL;
6189 lm = lm->next, lp++)
8c37241b
JJ
6190 {
6191 if (lp->p_type == PT_LOAD
3146fac4 6192 && lm->count != 0
dbc88fc1
AM
6193 && (lm->sections[lm->count - 1]->vma
6194 + (!IS_TBSS (lm->sections[lm->count - 1])
66631823 6195 ? lm->sections[lm->count - 1]->size / opb
dbc88fc1 6196 : 0)) > start
f2731e0c 6197 && lm->sections[0]->vma < end)
8c37241b
JJ
6198 break;
6199 }
f2731e0c 6200
01f7e10c 6201 if (lm != NULL)
129af99f 6202 {
01f7e10c
AM
6203 /* Find the section starting the RELRO segment. */
6204 for (i = 0; i < lm->count; i++)
6205 {
6206 asection *s = lm->sections[i];
6207 if (s->vma >= start
6208 && s->vma < end
6209 && s->size != 0)
6210 break;
6211 }
6212
6213 if (i < lm->count)
6214 {
502794d4
CE
6215 p->p_vaddr = lm->sections[i]->vma * opb;
6216 p->p_paddr = lm->sections[i]->lma * opb;
01f7e10c 6217 p->p_offset = lm->sections[i]->filepos;
66631823 6218 p->p_memsz = end * opb - p->p_vaddr;
01f7e10c
AM
6219 p->p_filesz = p->p_memsz;
6220
6221 /* The RELRO segment typically ends a few bytes
6222 into .got.plt but other layouts are possible.
6223 In cases where the end does not match any
6224 loaded section (for instance is in file
6225 padding), trim p_filesz back to correspond to
6226 the end of loaded section contents. */
6227 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6228 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6229
6230 /* Preserve the alignment and flags if they are
6231 valid. The gold linker generates RW/4 for
6232 the PT_GNU_RELRO section. It is better for
6233 objcopy/strip to honor these attributes
6234 otherwise gdb will choke when using separate
6235 debug files. */
6236 if (!m->p_align_valid)
6237 p->p_align = 1;
6238 if (!m->p_flags_valid)
6239 p->p_flags = PF_R;
6240 ok = TRUE;
6241 }
129af99f 6242 }
b84a33b5 6243 }
01f7e10c
AM
6244 if (link_info != NULL)
6245 BFD_ASSERT (ok);
6246 if (!ok)
6247 memset (p, 0, sizeof *p);
129af99f 6248 }
04c3a755
NS
6249 else if (p->p_type == PT_GNU_STACK)
6250 {
6251 if (m->p_size_valid)
6252 p->p_memsz = m->p_size;
6253 }
129af99f
AS
6254 else if (m->count != 0)
6255 {
e06efbf1 6256 unsigned int i;
1a9ccd70 6257
129af99f
AS
6258 if (p->p_type != PT_LOAD
6259 && (p->p_type != PT_NOTE
6260 || bfd_get_format (abfd) != bfd_core))
6261 {
1a9ccd70
NC
6262 /* A user specified segment layout may include a PHDR
6263 segment that overlaps with a LOAD segment... */
6264 if (p->p_type == PT_PHDR)
6265 {
6266 m->count = 0;
6267 continue;
6268 }
6269
c86934ce
NC
6270 if (m->includes_filehdr || m->includes_phdrs)
6271 {
b1fa9dd6 6272 /* PR 17512: file: 2195325e. */
4eca0228 6273 _bfd_error_handler
871b3ab2 6274 (_("%pB: error: non-load segment %d includes file header "
76cfced5
AM
6275 "and/or program header"),
6276 abfd, (int) (p - phdrs));
c86934ce
NC
6277 return FALSE;
6278 }
129af99f 6279
86b2281f 6280 p->p_filesz = 0;
129af99f 6281 p->p_offset = m->sections[0]->filepos;
86b2281f
AM
6282 for (i = m->count; i-- != 0;)
6283 {
6284 asection *sect = m->sections[i];
6285 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6286 if (hdr->sh_type != SHT_NOBITS)
6287 {
6288 p->p_filesz = (sect->filepos - m->sections[0]->filepos
6289 + hdr->sh_size);
6290 break;
6291 }
6292 }
129af99f
AS
6293 }
6294 }
252b5132
RH
6295 }
6296
b34976b6 6297 return TRUE;
252b5132
RH
6298}
6299
6a40cf0c
NC
6300static elf_section_list *
6301find_section_in_list (unsigned int i, elf_section_list * list)
6302{
6303 for (;list != NULL; list = list->next)
6304 if (list->ndx == i)
6305 break;
6306 return list;
6307}
6308
252b5132
RH
6309/* Work out the file positions of all the sections. This is called by
6310 _bfd_elf_compute_section_file_positions. All the section sizes and
6311 VMAs must be known before this is called.
6312
e0638f70 6313 Reloc sections come in two flavours: Those processed specially as
1ff6de03
NA
6314 "side-channel" data attached to a section to which they apply, and those that
6315 bfd doesn't process as relocations. The latter sort are stored in a normal
6316 bfd section by bfd_section_from_shdr. We don't consider the former sort
6317 here, unless they form part of the loadable image. Reloc sections not
6318 assigned here (and compressed debugging sections and CTF sections which
6319 nothing else in the file can rely upon) will be handled later by
e0638f70 6320 assign_file_positions_for_relocs.
252b5132
RH
6321
6322 We also don't set the positions of the .symtab and .strtab here. */
6323
b34976b6 6324static bfd_boolean
c84fca4d
AO
6325assign_file_positions_except_relocs (bfd *abfd,
6326 struct bfd_link_info *link_info)
252b5132 6327{
5c182d5f
AM
6328 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6329 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
9c5bfbb7 6330 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6d6c25c8 6331 unsigned int alloc;
252b5132
RH
6332
6333 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6334 && bfd_get_format (abfd) != bfd_core)
6335 {
5c182d5f
AM
6336 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6337 unsigned int num_sec = elf_numsections (abfd);
252b5132
RH
6338 Elf_Internal_Shdr **hdrpp;
6339 unsigned int i;
a485e98e 6340 file_ptr off;
252b5132
RH
6341
6342 /* Start after the ELF header. */
6343 off = i_ehdrp->e_ehsize;
6344
6345 /* We are not creating an executable, which means that we are
6346 not creating a program header, and that the actual order of
6347 the sections in the file is unimportant. */
9ad5cbcf 6348 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
252b5132
RH
6349 {
6350 Elf_Internal_Shdr *hdr;
6351
6352 hdr = *hdrpp;
e0638f70
AM
6353 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6354 && hdr->bfd_section == NULL)
1ff6de03
NA
6355 /* Do not assign offsets for these sections yet: we don't know
6356 their sizes. */
0ce398f1 6357 || (hdr->bfd_section != NULL
1ff6de03
NA
6358 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
6359 || (bfd_section_is_ctf (hdr->bfd_section)
6360 && abfd->is_linker_output)))
12bd6957 6361 || i == elf_onesymtab (abfd)
6a40cf0c
NC
6362 || (elf_symtab_shndx_list (abfd) != NULL
6363 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
3e19fb8f
L
6364 || i == elf_strtab_sec (abfd)
6365 || i == elf_shstrtab_sec (abfd))
252b5132
RH
6366 {
6367 hdr->sh_offset = -1;
252b5132 6368 }
9ad5cbcf 6369 else
b34976b6 6370 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
252b5132 6371 }
a485e98e
AM
6372
6373 elf_next_file_pos (abfd) = off;
6d6c25c8 6374 elf_program_header_size (abfd) = 0;
252b5132
RH
6375 }
6376 else
6377 {
252b5132 6378 /* Assign file positions for the loaded sections based on the
08a40648 6379 assignment of sections to segments. */
f3520d2f
AM
6380 if (!assign_file_positions_for_load_sections (abfd, link_info))
6381 return FALSE;
6382
6383 /* And for non-load sections. */
6384 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6385 return FALSE;
6d6c25c8 6386 }
f3520d2f 6387
6d6c25c8
AM
6388 if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
6389 return FALSE;
1a9ccd70 6390
6d6c25c8
AM
6391 /* Write out the program headers. */
6392 alloc = i_ehdrp->e_phnum;
6393 if (alloc != 0)
6394 {
30fe1832 6395 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
cd584857
NC
6396 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6397 return FALSE;
252b5132
RH
6398 }
6399
b34976b6 6400 return TRUE;
252b5132
RH
6401}
6402
ed7e9d0b
AM
6403bfd_boolean
6404_bfd_elf_init_file_header (bfd *abfd,
6405 struct bfd_link_info *info ATTRIBUTE_UNUSED)
252b5132 6406{
3d540e93 6407 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
2b0f7ef9 6408 struct elf_strtab_hash *shstrtab;
9c5bfbb7 6409 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
6410
6411 i_ehdrp = elf_elfheader (abfd);
252b5132 6412
2b0f7ef9 6413 shstrtab = _bfd_elf_strtab_init ();
252b5132 6414 if (shstrtab == NULL)
b34976b6 6415 return FALSE;
252b5132
RH
6416
6417 elf_shstrtab (abfd) = shstrtab;
6418
6419 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6420 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6421 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6422 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6423
6424 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6425 i_ehdrp->e_ident[EI_DATA] =
6426 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6427 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6428
252b5132
RH
6429 if ((abfd->flags & DYNAMIC) != 0)
6430 i_ehdrp->e_type = ET_DYN;
6431 else if ((abfd->flags & EXEC_P) != 0)
6432 i_ehdrp->e_type = ET_EXEC;
6433 else if (bfd_get_format (abfd) == bfd_core)
6434 i_ehdrp->e_type = ET_CORE;
6435 else
6436 i_ehdrp->e_type = ET_REL;
6437
6438 switch (bfd_get_arch (abfd))
6439 {
6440 case bfd_arch_unknown:
6441 i_ehdrp->e_machine = EM_NONE;
6442 break;
aa4f99bb
AO
6443
6444 /* There used to be a long list of cases here, each one setting
6445 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6446 in the corresponding bfd definition. To avoid duplication,
6447 the switch was removed. Machines that need special handling
6448 can generally do it in elf_backend_final_write_processing(),
6449 unless they need the information earlier than the final write.
6450 Such need can generally be supplied by replacing the tests for
6451 e_machine with the conditions used to determine it. */
252b5132 6452 default:
9c5bfbb7
AM
6453 i_ehdrp->e_machine = bed->elf_machine_code;
6454 }
aa4f99bb 6455
252b5132
RH
6456 i_ehdrp->e_version = bed->s->ev_current;
6457 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6458
c044fabd 6459 /* No program header, for now. */
252b5132
RH
6460 i_ehdrp->e_phoff = 0;
6461 i_ehdrp->e_phentsize = 0;
6462 i_ehdrp->e_phnum = 0;
6463
c044fabd 6464 /* Each bfd section is section header entry. */
252b5132
RH
6465 i_ehdrp->e_entry = bfd_get_start_address (abfd);
6466 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6467
252b5132 6468 elf_tdata (abfd)->symtab_hdr.sh_name =
b34976b6 6469 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
252b5132 6470 elf_tdata (abfd)->strtab_hdr.sh_name =
b34976b6 6471 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
252b5132 6472 elf_tdata (abfd)->shstrtab_hdr.sh_name =
b34976b6 6473 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
252b5132 6474 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
17ca87fc 6475 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
252b5132 6476 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
b34976b6 6477 return FALSE;
252b5132 6478
b34976b6 6479 return TRUE;
252b5132
RH
6480}
6481
6d6c25c8
AM
6482/* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
6483
6484 FIXME: We used to have code here to sort the PT_LOAD segments into
6485 ascending order, as per the ELF spec. But this breaks some programs,
6486 including the Linux kernel. But really either the spec should be
6487 changed or the programs updated. */
6488
6489bfd_boolean
6490_bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
6491{
6492 if (link_info != NULL && bfd_link_pie (link_info))
6493 {
6494 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
6495 unsigned int num_segments = i_ehdrp->e_phnum;
6496 struct elf_obj_tdata *tdata = elf_tdata (obfd);
6497 Elf_Internal_Phdr *segment = tdata->phdr;
6498 Elf_Internal_Phdr *end_segment = &segment[num_segments];
6499
6500 /* Find the lowest p_vaddr in PT_LOAD segments. */
6501 bfd_vma p_vaddr = (bfd_vma) -1;
6502 for (; segment < end_segment; segment++)
6503 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
6504 p_vaddr = segment->p_vaddr;
6505
6506 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6507 segments is non-zero. */
6508 if (p_vaddr)
6509 i_ehdrp->e_type = ET_EXEC;
6510 }
6511 return TRUE;
6512}
6513
252b5132 6514/* Assign file positions for all the reloc sections which are not part
a485e98e 6515 of the loadable file image, and the file position of section headers. */
252b5132 6516
0ce398f1
L
6517static bfd_boolean
6518_bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
252b5132
RH
6519{
6520 file_ptr off;
e06efbf1 6521 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
3e19fb8f 6522 Elf_Internal_Shdr *shdrp;
a485e98e
AM
6523 Elf_Internal_Ehdr *i_ehdrp;
6524 const struct elf_backend_data *bed;
252b5132 6525
12bd6957 6526 off = elf_next_file_pos (abfd);
252b5132 6527
e06efbf1
L
6528 shdrpp = elf_elfsections (abfd);
6529 end_shdrpp = shdrpp + elf_numsections (abfd);
6530 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
252b5132 6531 {
252b5132 6532 shdrp = *shdrpp;
0ce398f1
L
6533 if (shdrp->sh_offset == -1)
6534 {
3e19fb8f 6535 asection *sec = shdrp->bfd_section;
0ce398f1
L
6536 bfd_boolean is_rel = (shdrp->sh_type == SHT_REL
6537 || shdrp->sh_type == SHT_RELA);
1ff6de03 6538 bfd_boolean is_ctf = sec && bfd_section_is_ctf (sec);
0ce398f1 6539 if (is_rel
1ff6de03 6540 || is_ctf
3e19fb8f 6541 || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
0ce398f1 6542 {
1ff6de03 6543 if (!is_rel && !is_ctf)
0ce398f1 6544 {
3e19fb8f
L
6545 const char *name = sec->name;
6546 struct bfd_elf_section_data *d;
6547
0ce398f1 6548 /* Compress DWARF debug sections. */
3e19fb8f 6549 if (!bfd_compress_section (abfd, sec,
0ce398f1
L
6550 shdrp->contents))
6551 return FALSE;
3e19fb8f
L
6552
6553 if (sec->compress_status == COMPRESS_SECTION_DONE
6554 && (abfd->flags & BFD_COMPRESS_GABI) == 0)
6555 {
6556 /* If section is compressed with zlib-gnu, convert
6557 section name from .debug_* to .zdebug_*. */
6558 char *new_name
6559 = convert_debug_to_zdebug (abfd, name);
6560 if (new_name == NULL)
6561 return FALSE;
6562 name = new_name;
6563 }
dd905818 6564 /* Add section name to section name section. */
3e19fb8f
L
6565 if (shdrp->sh_name != (unsigned int) -1)
6566 abort ();
6567 shdrp->sh_name
6568 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
6569 name, FALSE);
6570 d = elf_section_data (sec);
6571
dd905818 6572 /* Add reloc section name to section name section. */
3e19fb8f
L
6573 if (d->rel.hdr
6574 && !_bfd_elf_set_reloc_sh_name (abfd,
6575 d->rel.hdr,
6576 name, FALSE))
6577 return FALSE;
6578 if (d->rela.hdr
6579 && !_bfd_elf_set_reloc_sh_name (abfd,
6580 d->rela.hdr,
91cb26da 6581 name, TRUE))
3e19fb8f
L
6582 return FALSE;
6583
0ce398f1 6584 /* Update section size and contents. */
3e19fb8f
L
6585 shdrp->sh_size = sec->size;
6586 shdrp->contents = sec->contents;
0ce398f1
L
6587 shdrp->bfd_section->contents = NULL;
6588 }
1ff6de03
NA
6589 else if (is_ctf)
6590 {
6591 /* Update section size and contents. */
6592 shdrp->sh_size = sec->size;
6593 shdrp->contents = sec->contents;
6594 }
6595
0ce398f1
L
6596 off = _bfd_elf_assign_file_position_for_section (shdrp,
6597 off,
6598 TRUE);
6599 }
6600 }
252b5132
RH
6601 }
6602
3e19fb8f
L
6603 /* Place section name section after DWARF debug sections have been
6604 compressed. */
6605 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
6606 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
6607 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
6608 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
6609
6610 /* Place the section headers. */
a485e98e
AM
6611 i_ehdrp = elf_elfheader (abfd);
6612 bed = get_elf_backend_data (abfd);
6613 off = align_file_position (off, 1 << bed->s->log_file_align);
6614 i_ehdrp->e_shoff = off;
6615 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
12bd6957 6616 elf_next_file_pos (abfd) = off;
0ce398f1
L
6617
6618 return TRUE;
252b5132
RH
6619}
6620
b34976b6 6621bfd_boolean
217aa764 6622_bfd_elf_write_object_contents (bfd *abfd)
252b5132 6623{
9c5bfbb7 6624 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 6625 Elf_Internal_Shdr **i_shdrp;
b34976b6 6626 bfd_boolean failed;
9ad5cbcf 6627 unsigned int count, num_sec;
30e8ee25 6628 struct elf_obj_tdata *t;
252b5132
RH
6629
6630 if (! abfd->output_has_begun
217aa764 6631 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
b34976b6 6632 return FALSE;
db727370
JL
6633 /* Do not rewrite ELF data when the BFD has been opened for update.
6634 abfd->output_has_begun was set to TRUE on opening, so creation of new
6635 sections, and modification of existing section sizes was restricted.
6636 This means the ELF header, program headers and section headers can't have
6637 changed.
6638 If the contents of any sections has been modified, then those changes have
6639 already been written to the BFD. */
6640 else if (abfd->direction == both_direction)
6641 {
6642 BFD_ASSERT (abfd->output_has_begun);
6643 return TRUE;
6644 }
252b5132
RH
6645
6646 i_shdrp = elf_elfsections (abfd);
252b5132 6647
b34976b6 6648 failed = FALSE;
252b5132
RH
6649 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
6650 if (failed)
b34976b6 6651 return FALSE;
252b5132 6652
0ce398f1
L
6653 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
6654 return FALSE;
252b5132 6655
c044fabd 6656 /* After writing the headers, we need to write the sections too... */
9ad5cbcf
AM
6657 num_sec = elf_numsections (abfd);
6658 for (count = 1; count < num_sec; count++)
252b5132 6659 {
3e19fb8f
L
6660 i_shdrp[count]->sh_name
6661 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
6662 i_shdrp[count]->sh_name);
252b5132 6663 if (bed->elf_backend_section_processing)
75506100
MR
6664 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
6665 return FALSE;
252b5132
RH
6666 if (i_shdrp[count]->contents)
6667 {
dc810e39
AM
6668 bfd_size_type amt = i_shdrp[count]->sh_size;
6669
252b5132 6670 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
dc810e39 6671 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
b34976b6 6672 return FALSE;
252b5132
RH
6673 }
6674 }
6675
6676 /* Write out the section header names. */
30e8ee25 6677 t = elf_tdata (abfd);
26ae6d5e 6678 if (elf_shstrtab (abfd) != NULL
30e8ee25 6679 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
08a40648 6680 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
b34976b6 6681 return FALSE;
252b5132 6682
cc364be6
AM
6683 if (!(*bed->elf_backend_final_write_processing) (abfd))
6684 return FALSE;
252b5132 6685
ff59fc36
RM
6686 if (!bed->s->write_shdrs_and_ehdr (abfd))
6687 return FALSE;
6688
6689 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
c0355132
AM
6690 if (t->o->build_id.after_write_object_contents != NULL)
6691 return (*t->o->build_id.after_write_object_contents) (abfd);
ff59fc36
RM
6692
6693 return TRUE;
252b5132
RH
6694}
6695
b34976b6 6696bfd_boolean
217aa764 6697_bfd_elf_write_corefile_contents (bfd *abfd)
252b5132 6698{
c044fabd 6699 /* Hopefully this can be done just like an object file. */
252b5132
RH
6700 return _bfd_elf_write_object_contents (abfd);
6701}
c044fabd
KH
6702
6703/* Given a section, search the header to find them. */
6704
cb33740c 6705unsigned int
198beae2 6706_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
252b5132 6707{
9c5bfbb7 6708 const struct elf_backend_data *bed;
91d6fa6a 6709 unsigned int sec_index;
252b5132 6710
9ad5cbcf
AM
6711 if (elf_section_data (asect) != NULL
6712 && elf_section_data (asect)->this_idx != 0)
6713 return elf_section_data (asect)->this_idx;
6714
6715 if (bfd_is_abs_section (asect))
91d6fa6a 6716 sec_index = SHN_ABS;
af746e92 6717 else if (bfd_is_com_section (asect))
91d6fa6a 6718 sec_index = SHN_COMMON;
af746e92 6719 else if (bfd_is_und_section (asect))
91d6fa6a 6720 sec_index = SHN_UNDEF;
af746e92 6721 else
91d6fa6a 6722 sec_index = SHN_BAD;
252b5132 6723
af746e92 6724 bed = get_elf_backend_data (abfd);
252b5132
RH
6725 if (bed->elf_backend_section_from_bfd_section)
6726 {
91d6fa6a 6727 int retval = sec_index;
9ad5cbcf 6728
af746e92
AM
6729 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6730 return retval;
252b5132
RH
6731 }
6732
91d6fa6a 6733 if (sec_index == SHN_BAD)
af746e92 6734 bfd_set_error (bfd_error_nonrepresentable_section);
252b5132 6735
91d6fa6a 6736 return sec_index;
252b5132
RH
6737}
6738
6739/* Given a BFD symbol, return the index in the ELF symbol table, or -1
6740 on error. */
6741
6742int
217aa764 6743_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
252b5132
RH
6744{
6745 asymbol *asym_ptr = *asym_ptr_ptr;
6746 int idx;
6747 flagword flags = asym_ptr->flags;
6748
6749 /* When gas creates relocations against local labels, it creates its
6750 own symbol for the section, but does put the symbol into the
6751 symbol chain, so udata is 0. When the linker is generating
6752 relocatable output, this section symbol may be for one of the
6753 input sections rather than the output section. */
6754 if (asym_ptr->udata.i == 0
6755 && (flags & BSF_SECTION_SYM)
6756 && asym_ptr->section)
6757 {
5372391b 6758 asection *sec;
252b5132
RH
6759 int indx;
6760
5372391b
AM
6761 sec = asym_ptr->section;
6762 if (sec->owner != abfd && sec->output_section != NULL)
6763 sec = sec->output_section;
6764 if (sec->owner == abfd
6765 && (indx = sec->index) < elf_num_section_syms (abfd)
4e89ac30 6766 && elf_section_syms (abfd)[indx] != NULL)
252b5132
RH
6767 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
6768 }
6769
6770 idx = asym_ptr->udata.i;
6771
6772 if (idx == 0)
6773 {
6774 /* This case can occur when using --strip-symbol on a symbol
08a40648 6775 which is used in a relocation entry. */
4eca0228 6776 _bfd_error_handler
695344c0 6777 /* xgettext:c-format */
871b3ab2 6778 (_("%pB: symbol `%s' required but not present"),
d003868e 6779 abfd, bfd_asymbol_name (asym_ptr));
252b5132
RH
6780 bfd_set_error (bfd_error_no_symbols);
6781 return -1;
6782 }
6783
6784#if DEBUG & 4
6785 {
6786 fprintf (stderr,
cd9af601
AM
6787 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6788 (long) asym_ptr, asym_ptr->name, idx, flags);
252b5132
RH
6789 fflush (stderr);
6790 }
6791#endif
6792
6793 return idx;
6794}
6795
84d1d650 6796/* Rewrite program header information. */
252b5132 6797
b34976b6 6798static bfd_boolean
c410035d 6799rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
252b5132 6800{
b34976b6
AM
6801 Elf_Internal_Ehdr *iehdr;
6802 struct elf_segment_map *map;
6803 struct elf_segment_map *map_first;
6804 struct elf_segment_map **pointer_to_map;
6805 Elf_Internal_Phdr *segment;
6806 asection *section;
6807 unsigned int i;
6808 unsigned int num_segments;
6809 bfd_boolean phdr_included = FALSE;
5c44b38e 6810 bfd_boolean p_paddr_valid;
b34976b6
AM
6811 struct elf_segment_map *phdr_adjust_seg = NULL;
6812 unsigned int phdr_adjust_num = 0;
9c5bfbb7 6813 const struct elf_backend_data *bed;
502794d4 6814 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
bc67d8a6 6815
caf47ea6 6816 bed = get_elf_backend_data (ibfd);
252b5132
RH
6817 iehdr = elf_elfheader (ibfd);
6818
bc67d8a6 6819 map_first = NULL;
c044fabd 6820 pointer_to_map = &map_first;
252b5132
RH
6821
6822 num_segments = elf_elfheader (ibfd)->e_phnum;
bc67d8a6
NC
6823
6824 /* Returns the end address of the segment + 1. */
aecc8f8a
AM
6825#define SEGMENT_END(segment, start) \
6826 (start + (segment->p_memsz > segment->p_filesz \
6827 ? segment->p_memsz : segment->p_filesz))
bc67d8a6 6828
eecdbe52
JJ
6829#define SECTION_SIZE(section, segment) \
6830 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6831 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
eea6121a 6832 ? section->size : 0)
eecdbe52 6833
b34976b6 6834 /* Returns TRUE if the given section is contained within
bc67d8a6 6835 the given segment. VMA addresses are compared. */
502794d4
CE
6836#define IS_CONTAINED_BY_VMA(section, segment, opb) \
6837 (section->vma * (opb) >= segment->p_vaddr \
6838 && (section->vma * (opb) + SECTION_SIZE (section, segment) \
aecc8f8a 6839 <= (SEGMENT_END (segment, segment->p_vaddr))))
c044fabd 6840
b34976b6 6841 /* Returns TRUE if the given section is contained within
bc67d8a6 6842 the given segment. LMA addresses are compared. */
502794d4
CE
6843#define IS_CONTAINED_BY_LMA(section, segment, base, opb) \
6844 (section->lma * (opb) >= base \
6845 && (section->lma + SECTION_SIZE (section, segment) / (opb) >= section->lma) \
6846 && (section->lma * (opb) + SECTION_SIZE (section, segment) \
aecc8f8a 6847 <= SEGMENT_END (segment, base)))
252b5132 6848
0efc80c8
L
6849 /* Handle PT_NOTE segment. */
6850#define IS_NOTE(p, s) \
aecc8f8a 6851 (p->p_type == PT_NOTE \
0efc80c8 6852 && elf_section_type (s) == SHT_NOTE \
aecc8f8a 6853 && (bfd_vma) s->filepos >= p->p_offset \
cb3ff1e5 6854 && ((bfd_vma) s->filepos + s->size \
aecc8f8a 6855 <= p->p_offset + p->p_filesz))
252b5132 6856
0efc80c8
L
6857 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6858 etc. */
6859#define IS_COREFILE_NOTE(p, s) \
6860 (IS_NOTE (p, s) \
6861 && bfd_get_format (ibfd) == bfd_core \
6862 && s->vma == 0 \
6863 && s->lma == 0)
6864
252b5132
RH
6865 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6866 linker, which generates a PT_INTERP section with p_vaddr and
6867 p_memsz set to 0. */
aecc8f8a
AM
6868#define IS_SOLARIS_PT_INTERP(p, s) \
6869 (p->p_vaddr == 0 \
6870 && p->p_paddr == 0 \
6871 && p->p_memsz == 0 \
6872 && p->p_filesz > 0 \
6873 && (s->flags & SEC_HAS_CONTENTS) != 0 \
eea6121a 6874 && s->size > 0 \
aecc8f8a 6875 && (bfd_vma) s->filepos >= p->p_offset \
cb3ff1e5 6876 && ((bfd_vma) s->filepos + s->size \
aecc8f8a 6877 <= p->p_offset + p->p_filesz))
5c440b1e 6878
bc67d8a6
NC
6879 /* Decide if the given section should be included in the given segment.
6880 A section will be included if:
f5ffc919 6881 1. It is within the address space of the segment -- we use the LMA
08a40648 6882 if that is set for the segment and the VMA otherwise,
0efc80c8 6883 2. It is an allocated section or a NOTE section in a PT_NOTE
d324f6d6 6884 segment.
bc67d8a6 6885 3. There is an output section associated with it,
eecdbe52 6886 4. The section has not already been allocated to a previous segment.
2b05f1b7 6887 5. PT_GNU_STACK segments do not include any sections.
03394ac9 6888 6. PT_TLS segment includes only SHF_TLS sections.
6f79b219
JJ
6889 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6890 8. PT_DYNAMIC should not contain empty sections at the beginning
08a40648 6891 (with the possible exception of .dynamic). */
502794d4 6892#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
2b05f1b7 6893 ((((segment->p_paddr \
502794d4
CE
6894 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr, opb) \
6895 : IS_CONTAINED_BY_VMA (section, segment, opb)) \
2b05f1b7 6896 && (section->flags & SEC_ALLOC) != 0) \
0efc80c8 6897 || IS_NOTE (segment, section)) \
2b05f1b7
L
6898 && segment->p_type != PT_GNU_STACK \
6899 && (segment->p_type != PT_TLS \
6900 || (section->flags & SEC_THREAD_LOCAL)) \
6901 && (segment->p_type == PT_LOAD \
6902 || segment->p_type == PT_TLS \
6903 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6904 && (segment->p_type != PT_DYNAMIC \
6905 || SECTION_SIZE (section, segment) > 0 \
6906 || (segment->p_paddr \
502794d4
CE
6907 ? segment->p_paddr != section->lma * (opb) \
6908 : segment->p_vaddr != section->vma * (opb)) \
fd361982 6909 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
9933dc52 6910 && (segment->p_type != PT_LOAD || !section->segment_mark))
bc67d8a6 6911
9f17e2a6
L
6912/* If the output section of a section in the input segment is NULL,
6913 it is removed from the corresponding output segment. */
502794d4
CE
6914#define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
6915 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
9f17e2a6
L
6916 && section->output_section != NULL)
6917
b34976b6 6918 /* Returns TRUE iff seg1 starts after the end of seg2. */
b5f852ea
NC
6919#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6920 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6921
6922 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6923 their VMA address ranges and their LMA address ranges overlap.
6924 It is possible to have overlapping VMA ranges without overlapping LMA
6925 ranges. RedBoot images for example can have both .data and .bss mapped
6926 to the same VMA range, but with the .data section mapped to a different
6927 LMA. */
aecc8f8a 6928#define SEGMENT_OVERLAPS(seg1, seg2) \
b5f852ea 6929 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
08a40648 6930 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
b5f852ea 6931 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
08a40648 6932 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
bc67d8a6
NC
6933
6934 /* Initialise the segment mark field. */
6935 for (section = ibfd->sections; section != NULL; section = section->next)
b34976b6 6936 section->segment_mark = FALSE;
bc67d8a6 6937
5c44b38e
AM
6938 /* The Solaris linker creates program headers in which all the
6939 p_paddr fields are zero. When we try to objcopy or strip such a
6940 file, we get confused. Check for this case, and if we find it
6941 don't set the p_paddr_valid fields. */
6942 p_paddr_valid = FALSE;
6943 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6944 i < num_segments;
6945 i++, segment++)
6946 if (segment->p_paddr != 0)
6947 {
6948 p_paddr_valid = TRUE;
6949 break;
6950 }
6951
252b5132 6952 /* Scan through the segments specified in the program header
bc67d8a6 6953 of the input BFD. For this first scan we look for overlaps
9ad5cbcf 6954 in the loadable segments. These can be created by weird
aecc8f8a 6955 parameters to objcopy. Also, fix some solaris weirdness. */
bc67d8a6
NC
6956 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6957 i < num_segments;
c044fabd 6958 i++, segment++)
252b5132 6959 {
252b5132 6960 unsigned int j;
c044fabd 6961 Elf_Internal_Phdr *segment2;
252b5132 6962
aecc8f8a
AM
6963 if (segment->p_type == PT_INTERP)
6964 for (section = ibfd->sections; section; section = section->next)
6965 if (IS_SOLARIS_PT_INTERP (segment, section))
6966 {
6967 /* Mininal change so that the normal section to segment
4cc11e76 6968 assignment code will work. */
502794d4 6969 segment->p_vaddr = section->vma * opb;
aecc8f8a
AM
6970 break;
6971 }
6972
bc67d8a6 6973 if (segment->p_type != PT_LOAD)
b10a8ae0
L
6974 {
6975 /* Remove PT_GNU_RELRO segment. */
6976 if (segment->p_type == PT_GNU_RELRO)
6977 segment->p_type = PT_NULL;
6978 continue;
6979 }
c044fabd 6980
bc67d8a6 6981 /* Determine if this segment overlaps any previous segments. */
0067a569 6982 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
bc67d8a6
NC
6983 {
6984 bfd_signed_vma extra_length;
c044fabd 6985
bc67d8a6 6986 if (segment2->p_type != PT_LOAD
0067a569 6987 || !SEGMENT_OVERLAPS (segment, segment2))
bc67d8a6 6988 continue;
c044fabd 6989
bc67d8a6
NC
6990 /* Merge the two segments together. */
6991 if (segment2->p_vaddr < segment->p_vaddr)
6992 {
c044fabd 6993 /* Extend SEGMENT2 to include SEGMENT and then delete
08a40648 6994 SEGMENT. */
0067a569
AM
6995 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
6996 - SEGMENT_END (segment2, segment2->p_vaddr));
c044fabd 6997
bc67d8a6
NC
6998 if (extra_length > 0)
6999 {
0067a569 7000 segment2->p_memsz += extra_length;
bc67d8a6
NC
7001 segment2->p_filesz += extra_length;
7002 }
c044fabd 7003
bc67d8a6 7004 segment->p_type = PT_NULL;
c044fabd 7005
bc67d8a6
NC
7006 /* Since we have deleted P we must restart the outer loop. */
7007 i = 0;
7008 segment = elf_tdata (ibfd)->phdr;
7009 break;
7010 }
7011 else
7012 {
c044fabd 7013 /* Extend SEGMENT to include SEGMENT2 and then delete
08a40648 7014 SEGMENT2. */
0067a569
AM
7015 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
7016 - SEGMENT_END (segment, segment->p_vaddr));
c044fabd 7017
bc67d8a6
NC
7018 if (extra_length > 0)
7019 {
0067a569 7020 segment->p_memsz += extra_length;
bc67d8a6
NC
7021 segment->p_filesz += extra_length;
7022 }
c044fabd 7023
bc67d8a6
NC
7024 segment2->p_type = PT_NULL;
7025 }
7026 }
7027 }
c044fabd 7028
bc67d8a6
NC
7029 /* The second scan attempts to assign sections to segments. */
7030 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7031 i < num_segments;
0067a569 7032 i++, segment++)
bc67d8a6 7033 {
0067a569
AM
7034 unsigned int section_count;
7035 asection **sections;
7036 asection *output_section;
7037 unsigned int isec;
9933dc52
AM
7038 asection *matching_lma;
7039 asection *suggested_lma;
0067a569 7040 unsigned int j;
446f7ed5 7041 size_t amt;
0067a569 7042 asection *first_section;
bc67d8a6
NC
7043
7044 if (segment->p_type == PT_NULL)
7045 continue;
c044fabd 7046
9f17e2a6 7047 first_section = NULL;
bc67d8a6 7048 /* Compute how many sections might be placed into this segment. */
b5f852ea
NC
7049 for (section = ibfd->sections, section_count = 0;
7050 section != NULL;
7051 section = section->next)
9f17e2a6
L
7052 {
7053 /* Find the first section in the input segment, which may be
7054 removed from the corresponding output segment. */
502794d4 7055 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb))
9f17e2a6
L
7056 {
7057 if (first_section == NULL)
7058 first_section = section;
7059 if (section->output_section != NULL)
7060 ++section_count;
7061 }
7062 }
811072d8 7063
b5f852ea
NC
7064 /* Allocate a segment map big enough to contain
7065 all of the sections we have selected. */
00bee008 7066 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
446f7ed5 7067 amt += section_count * sizeof (asection *);
a50b1753 7068 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
bc67d8a6 7069 if (map == NULL)
b34976b6 7070 return FALSE;
252b5132
RH
7071
7072 /* Initialise the fields of the segment map. Default to
7073 using the physical address of the segment in the input BFD. */
0067a569
AM
7074 map->next = NULL;
7075 map->p_type = segment->p_type;
7076 map->p_flags = segment->p_flags;
bc67d8a6 7077 map->p_flags_valid = 1;
55d55ac7 7078
c410035d
AM
7079 if (map->p_type == PT_LOAD
7080 && (ibfd->flags & D_PAGED) != 0
7081 && maxpagesize > 1
7082 && segment->p_align > 1)
7083 {
7084 map->p_align = segment->p_align;
7085 if (segment->p_align > maxpagesize)
7086 map->p_align = maxpagesize;
7087 map->p_align_valid = 1;
7088 }
7089
9f17e2a6
L
7090 /* If the first section in the input segment is removed, there is
7091 no need to preserve segment physical address in the corresponding
7092 output segment. */
945c025a 7093 if (!first_section || first_section->output_section != NULL)
9f17e2a6
L
7094 {
7095 map->p_paddr = segment->p_paddr;
5c44b38e 7096 map->p_paddr_valid = p_paddr_valid;
9f17e2a6 7097 }
252b5132
RH
7098
7099 /* Determine if this segment contains the ELF file header
7100 and if it contains the program headers themselves. */
bc67d8a6
NC
7101 map->includes_filehdr = (segment->p_offset == 0
7102 && segment->p_filesz >= iehdr->e_ehsize);
bc67d8a6 7103 map->includes_phdrs = 0;
252b5132 7104
0067a569 7105 if (!phdr_included || segment->p_type != PT_LOAD)
252b5132 7106 {
bc67d8a6
NC
7107 map->includes_phdrs =
7108 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7109 && (segment->p_offset + segment->p_filesz
252b5132
RH
7110 >= ((bfd_vma) iehdr->e_phoff
7111 + iehdr->e_phnum * iehdr->e_phentsize)));
c044fabd 7112
bc67d8a6 7113 if (segment->p_type == PT_LOAD && map->includes_phdrs)
b34976b6 7114 phdr_included = TRUE;
252b5132
RH
7115 }
7116
bc67d8a6 7117 if (section_count == 0)
252b5132
RH
7118 {
7119 /* Special segments, such as the PT_PHDR segment, may contain
7120 no sections, but ordinary, loadable segments should contain
1ed89aa9 7121 something. They are allowed by the ELF spec however, so only
07d6d2b8 7122 a warning is produced.
f98450c6
NC
7123 There is however the valid use case of embedded systems which
7124 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7125 flash memory with zeros. No warning is shown for that case. */
7126 if (segment->p_type == PT_LOAD
7127 && (segment->p_filesz > 0 || segment->p_memsz == 0))
7128 /* xgettext:c-format */
9793eb77
AM
7129 _bfd_error_handler
7130 (_("%pB: warning: empty loadable segment detected"
7131 " at vaddr=%#" PRIx64 ", is this intentional?"),
7132 ibfd, (uint64_t) segment->p_vaddr);
252b5132 7133
502794d4 7134 map->p_vaddr_offset = segment->p_vaddr / opb;
bc67d8a6 7135 map->count = 0;
c044fabd
KH
7136 *pointer_to_map = map;
7137 pointer_to_map = &map->next;
252b5132
RH
7138
7139 continue;
7140 }
7141
7142 /* Now scan the sections in the input BFD again and attempt
7143 to add their corresponding output sections to the segment map.
7144 The problem here is how to handle an output section which has
7145 been moved (ie had its LMA changed). There are four possibilities:
7146
7147 1. None of the sections have been moved.
7148 In this case we can continue to use the segment LMA from the
7149 input BFD.
7150
7151 2. All of the sections have been moved by the same amount.
7152 In this case we can change the segment's LMA to match the LMA
7153 of the first section.
7154
7155 3. Some of the sections have been moved, others have not.
7156 In this case those sections which have not been moved can be
7157 placed in the current segment which will have to have its size,
7158 and possibly its LMA changed, and a new segment or segments will
7159 have to be created to contain the other sections.
7160
b5f852ea 7161 4. The sections have been moved, but not by the same amount.
252b5132
RH
7162 In this case we can change the segment's LMA to match the LMA
7163 of the first section and we will have to create a new segment
7164 or segments to contain the other sections.
7165
7166 In order to save time, we allocate an array to hold the section
7167 pointers that we are interested in. As these sections get assigned
7168 to a segment, they are removed from this array. */
7169
446f7ed5
AM
7170 amt = section_count * sizeof (asection *);
7171 sections = (asection **) bfd_malloc (amt);
252b5132 7172 if (sections == NULL)
b34976b6 7173 return FALSE;
252b5132
RH
7174
7175 /* Step One: Scan for segment vs section LMA conflicts.
7176 Also add the sections to the section array allocated above.
7177 Also add the sections to the current segment. In the common
7178 case, where the sections have not been moved, this means that
7179 we have completely filled the segment, and there is nothing
7180 more to do. */
252b5132 7181 isec = 0;
9933dc52
AM
7182 matching_lma = NULL;
7183 suggested_lma = NULL;
252b5132 7184
461c4b2e 7185 for (section = first_section, j = 0;
bc67d8a6
NC
7186 section != NULL;
7187 section = section->next)
252b5132 7188 {
502794d4 7189 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb))
c0f7859b 7190 {
bc67d8a6
NC
7191 output_section = section->output_section;
7192
0067a569 7193 sections[j++] = section;
252b5132
RH
7194
7195 /* The Solaris native linker always sets p_paddr to 0.
7196 We try to catch that case here, and set it to the
5e8d7549
NC
7197 correct value. Note - some backends require that
7198 p_paddr be left as zero. */
5c44b38e 7199 if (!p_paddr_valid
4455705d 7200 && segment->p_vaddr != 0
0067a569 7201 && !bed->want_p_paddr_set_to_zero
252b5132 7202 && isec == 0
bc67d8a6 7203 && output_section->lma != 0
9933dc52
AM
7204 && (align_power (segment->p_vaddr
7205 + (map->includes_filehdr
7206 ? iehdr->e_ehsize : 0)
7207 + (map->includes_phdrs
7208 ? iehdr->e_phnum * iehdr->e_phentsize
7209 : 0),
66631823
CE
7210 output_section->alignment_power * opb)
7211 == (output_section->vma * opb)))
bc67d8a6 7212 map->p_paddr = segment->p_vaddr;
252b5132
RH
7213
7214 /* Match up the physical address of the segment with the
7215 LMA address of the output section. */
502794d4
CE
7216 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr,
7217 opb)
5e8d7549 7218 || IS_COREFILE_NOTE (segment, section)
0067a569 7219 || (bed->want_p_paddr_set_to_zero
502794d4 7220 && IS_CONTAINED_BY_VMA (output_section, segment, opb)))
252b5132 7221 {
9933dc52
AM
7222 if (matching_lma == NULL
7223 || output_section->lma < matching_lma->lma)
7224 matching_lma = output_section;
252b5132
RH
7225
7226 /* We assume that if the section fits within the segment
bc67d8a6 7227 then it does not overlap any other section within that
252b5132 7228 segment. */
0067a569
AM
7229 map->sections[isec++] = output_section;
7230 }
9933dc52
AM
7231 else if (suggested_lma == NULL)
7232 suggested_lma = output_section;
147d51c2
L
7233
7234 if (j == section_count)
7235 break;
252b5132
RH
7236 }
7237 }
7238
bc67d8a6 7239 BFD_ASSERT (j == section_count);
252b5132
RH
7240
7241 /* Step Two: Adjust the physical address of the current segment,
7242 if necessary. */
bc67d8a6 7243 if (isec == section_count)
252b5132
RH
7244 {
7245 /* All of the sections fitted within the segment as currently
7246 specified. This is the default case. Add the segment to
7247 the list of built segments and carry on to process the next
7248 program header in the input BFD. */
bc67d8a6 7249 map->count = section_count;
c044fabd
KH
7250 *pointer_to_map = map;
7251 pointer_to_map = &map->next;
08a40648 7252
5c44b38e 7253 if (p_paddr_valid
30fe1832
AM
7254 && !bed->want_p_paddr_set_to_zero)
7255 {
7256 bfd_vma hdr_size = 0;
7257 if (map->includes_filehdr)
7258 hdr_size = iehdr->e_ehsize;
7259 if (map->includes_phdrs)
7260 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7261
7262 /* Account for padding before the first section in the
7263 segment. */
502794d4
CE
7264 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7265 - matching_lma->lma);
30fe1832 7266 }
08a40648 7267
252b5132
RH
7268 free (sections);
7269 continue;
7270 }
252b5132
RH
7271 else
7272 {
9933dc52
AM
7273 /* Change the current segment's physical address to match
7274 the LMA of the first section that fitted, or if no
7275 section fitted, the first section. */
7276 if (matching_lma == NULL)
7277 matching_lma = suggested_lma;
7278
66631823 7279 map->p_paddr = matching_lma->lma * opb;
72730e0c 7280
bc67d8a6
NC
7281 /* Offset the segment physical address from the lma
7282 to allow for space taken up by elf headers. */
9933dc52 7283 if (map->includes_phdrs)
010c8431 7284 {
9933dc52
AM
7285 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7286
7287 /* iehdr->e_phnum is just an estimate of the number
7288 of program headers that we will need. Make a note
7289 here of the number we used and the segment we chose
7290 to hold these headers, so that we can adjust the
7291 offset when we know the correct value. */
7292 phdr_adjust_num = iehdr->e_phnum;
7293 phdr_adjust_seg = map;
010c8431 7294 }
252b5132 7295
9933dc52 7296 if (map->includes_filehdr)
bc67d8a6 7297 {
9933dc52
AM
7298 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7299 map->p_paddr -= iehdr->e_ehsize;
7300 /* We've subtracted off the size of headers from the
7301 first section lma, but there may have been some
7302 alignment padding before that section too. Try to
7303 account for that by adjusting the segment lma down to
7304 the same alignment. */
7305 if (segment->p_align != 0 && segment->p_align < align)
7306 align = segment->p_align;
66631823 7307 map->p_paddr &= -(align * opb);
bc67d8a6 7308 }
252b5132
RH
7309 }
7310
7311 /* Step Three: Loop over the sections again, this time assigning
caf47ea6 7312 those that fit to the current segment and removing them from the
252b5132
RH
7313 sections array; but making sure not to leave large gaps. Once all
7314 possible sections have been assigned to the current segment it is
7315 added to the list of built segments and if sections still remain
7316 to be assigned, a new segment is constructed before repeating
7317 the loop. */
7318 isec = 0;
7319 do
7320 {
bc67d8a6 7321 map->count = 0;
9933dc52 7322 suggested_lma = NULL;
252b5132
RH
7323
7324 /* Fill the current segment with sections that fit. */
bc67d8a6 7325 for (j = 0; j < section_count; j++)
252b5132 7326 {
bc67d8a6 7327 section = sections[j];
252b5132 7328
bc67d8a6 7329 if (section == NULL)
252b5132
RH
7330 continue;
7331
bc67d8a6 7332 output_section = section->output_section;
252b5132 7333
bc67d8a6 7334 BFD_ASSERT (output_section != NULL);
c044fabd 7335
502794d4
CE
7336 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr,
7337 opb)
bc67d8a6 7338 || IS_COREFILE_NOTE (segment, section))
252b5132 7339 {
bc67d8a6 7340 if (map->count == 0)
252b5132
RH
7341 {
7342 /* If the first section in a segment does not start at
bc67d8a6
NC
7343 the beginning of the segment, then something is
7344 wrong. */
9933dc52
AM
7345 if (align_power (map->p_paddr
7346 + (map->includes_filehdr
7347 ? iehdr->e_ehsize : 0)
7348 + (map->includes_phdrs
7349 ? iehdr->e_phnum * iehdr->e_phentsize
7350 : 0),
66631823
CE
7351 output_section->alignment_power * opb)
7352 != output_section->lma * opb)
9aea1e31 7353 goto sorry;
252b5132
RH
7354 }
7355 else
7356 {
0067a569 7357 asection *prev_sec;
252b5132 7358
bc67d8a6 7359 prev_sec = map->sections[map->count - 1];
252b5132
RH
7360
7361 /* If the gap between the end of the previous section
bc67d8a6
NC
7362 and the start of this section is more than
7363 maxpagesize then we need to start a new segment. */
eea6121a 7364 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
079e9a2f 7365 maxpagesize)
caf47ea6 7366 < BFD_ALIGN (output_section->lma, maxpagesize))
0067a569 7367 || (prev_sec->lma + prev_sec->size
079e9a2f 7368 > output_section->lma))
252b5132 7369 {
9933dc52
AM
7370 if (suggested_lma == NULL)
7371 suggested_lma = output_section;
252b5132
RH
7372
7373 continue;
7374 }
7375 }
7376
bc67d8a6 7377 map->sections[map->count++] = output_section;
252b5132
RH
7378 ++isec;
7379 sections[j] = NULL;
9933dc52
AM
7380 if (segment->p_type == PT_LOAD)
7381 section->segment_mark = TRUE;
0067a569 7382 }
9933dc52
AM
7383 else if (suggested_lma == NULL)
7384 suggested_lma = output_section;
252b5132
RH
7385 }
7386
beab4532
NC
7387 /* PR 23932. A corrupt input file may contain sections that cannot
7388 be assigned to any segment - because for example they have a
9984857c
NC
7389 negative size - or segments that do not contain any sections.
7390 But there are also valid reasons why a segment can be empty.
7391 So allow a count of zero. */
252b5132
RH
7392
7393 /* Add the current segment to the list of built segments. */
c044fabd
KH
7394 *pointer_to_map = map;
7395 pointer_to_map = &map->next;
252b5132 7396
bc67d8a6 7397 if (isec < section_count)
252b5132
RH
7398 {
7399 /* We still have not allocated all of the sections to
7400 segments. Create a new segment here, initialise it
7401 and carry on looping. */
00bee008 7402 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
446f7ed5 7403 amt += section_count * sizeof (asection *);
5964fc3a 7404 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
bc67d8a6 7405 if (map == NULL)
5ed6aba4
NC
7406 {
7407 free (sections);
7408 return FALSE;
7409 }
252b5132
RH
7410
7411 /* Initialise the fields of the segment map. Set the physical
7412 physical address to the LMA of the first section that has
7413 not yet been assigned. */
0067a569
AM
7414 map->next = NULL;
7415 map->p_type = segment->p_type;
7416 map->p_flags = segment->p_flags;
7417 map->p_flags_valid = 1;
66631823 7418 map->p_paddr = suggested_lma->lma * opb;
5c44b38e 7419 map->p_paddr_valid = p_paddr_valid;
bc67d8a6 7420 map->includes_filehdr = 0;
0067a569 7421 map->includes_phdrs = 0;
252b5132 7422 }
9984857c
NC
7423
7424 continue;
7425 sorry:
7426 bfd_set_error (bfd_error_sorry);
7427 free (sections);
7428 return FALSE;
252b5132 7429 }
bc67d8a6 7430 while (isec < section_count);
252b5132
RH
7431
7432 free (sections);
7433 }
7434
12bd6957 7435 elf_seg_map (obfd) = map_first;
bc67d8a6
NC
7436
7437 /* If we had to estimate the number of program headers that were
9ad5cbcf 7438 going to be needed, then check our estimate now and adjust
bc67d8a6
NC
7439 the offset if necessary. */
7440 if (phdr_adjust_seg != NULL)
7441 {
7442 unsigned int count;
c044fabd 7443
bc67d8a6 7444 for (count = 0, map = map_first; map != NULL; map = map->next)
c044fabd 7445 count++;
252b5132 7446
bc67d8a6
NC
7447 if (count > phdr_adjust_num)
7448 phdr_adjust_seg->p_paddr
7449 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
9933dc52
AM
7450
7451 for (map = map_first; map != NULL; map = map->next)
7452 if (map->p_type == PT_PHDR)
7453 {
7454 bfd_vma adjust
7455 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7456 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7457 break;
7458 }
bc67d8a6 7459 }
c044fabd 7460
bc67d8a6 7461#undef SEGMENT_END
eecdbe52 7462#undef SECTION_SIZE
bc67d8a6
NC
7463#undef IS_CONTAINED_BY_VMA
7464#undef IS_CONTAINED_BY_LMA
0efc80c8 7465#undef IS_NOTE
252b5132 7466#undef IS_COREFILE_NOTE
bc67d8a6 7467#undef IS_SOLARIS_PT_INTERP
9f17e2a6 7468#undef IS_SECTION_IN_INPUT_SEGMENT
bc67d8a6
NC
7469#undef INCLUDE_SECTION_IN_SEGMENT
7470#undef SEGMENT_AFTER_SEGMENT
7471#undef SEGMENT_OVERLAPS
b34976b6 7472 return TRUE;
252b5132
RH
7473}
7474
84d1d650
L
7475/* Copy ELF program header information. */
7476
7477static bfd_boolean
7478copy_elf_program_header (bfd *ibfd, bfd *obfd)
7479{
7480 Elf_Internal_Ehdr *iehdr;
7481 struct elf_segment_map *map;
7482 struct elf_segment_map *map_first;
7483 struct elf_segment_map **pointer_to_map;
7484 Elf_Internal_Phdr *segment;
7485 unsigned int i;
7486 unsigned int num_segments;
7487 bfd_boolean phdr_included = FALSE;
88967714 7488 bfd_boolean p_paddr_valid;
502794d4 7489 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
84d1d650
L
7490
7491 iehdr = elf_elfheader (ibfd);
7492
7493 map_first = NULL;
7494 pointer_to_map = &map_first;
7495
88967714
AM
7496 /* If all the segment p_paddr fields are zero, don't set
7497 map->p_paddr_valid. */
7498 p_paddr_valid = FALSE;
84d1d650 7499 num_segments = elf_elfheader (ibfd)->e_phnum;
88967714
AM
7500 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7501 i < num_segments;
7502 i++, segment++)
7503 if (segment->p_paddr != 0)
7504 {
7505 p_paddr_valid = TRUE;
7506 break;
7507 }
7508
84d1d650
L
7509 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7510 i < num_segments;
7511 i++, segment++)
7512 {
7513 asection *section;
7514 unsigned int section_count;
986f0783 7515 size_t amt;
84d1d650 7516 Elf_Internal_Shdr *this_hdr;
53020534 7517 asection *first_section = NULL;
a76e6f2f 7518 asection *lowest_section;
84d1d650 7519
84d1d650
L
7520 /* Compute how many sections are in this segment. */
7521 for (section = ibfd->sections, section_count = 0;
7522 section != NULL;
7523 section = section->next)
7524 {
7525 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7526 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
3271a814 7527 {
a76e6f2f
AM
7528 if (first_section == NULL)
7529 first_section = section;
3271a814
NS
7530 section_count++;
7531 }
84d1d650
L
7532 }
7533
7534 /* Allocate a segment map big enough to contain
7535 all of the sections we have selected. */
00bee008 7536 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
986f0783 7537 amt += section_count * sizeof (asection *);
a50b1753 7538 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
84d1d650
L
7539 if (map == NULL)
7540 return FALSE;
7541
7542 /* Initialize the fields of the output segment map with the
7543 input segment. */
7544 map->next = NULL;
7545 map->p_type = segment->p_type;
7546 map->p_flags = segment->p_flags;
7547 map->p_flags_valid = 1;
7548 map->p_paddr = segment->p_paddr;
88967714 7549 map->p_paddr_valid = p_paddr_valid;
3f570048
AM
7550 map->p_align = segment->p_align;
7551 map->p_align_valid = 1;
3271a814 7552 map->p_vaddr_offset = 0;
84d1d650 7553
04c3a755
NS
7554 if (map->p_type == PT_GNU_RELRO
7555 || map->p_type == PT_GNU_STACK)
b10a8ae0
L
7556 {
7557 /* The PT_GNU_RELRO segment may contain the first a few
7558 bytes in the .got.plt section even if the whole .got.plt
7559 section isn't in the PT_GNU_RELRO segment. We won't
04c3a755
NS
7560 change the size of the PT_GNU_RELRO segment.
7561 Similarly, PT_GNU_STACK size is significant on uclinux
7562 systems. */
9433b9b1 7563 map->p_size = segment->p_memsz;
b10a8ae0
L
7564 map->p_size_valid = 1;
7565 }
7566
84d1d650
L
7567 /* Determine if this segment contains the ELF file header
7568 and if it contains the program headers themselves. */
7569 map->includes_filehdr = (segment->p_offset == 0
7570 && segment->p_filesz >= iehdr->e_ehsize);
7571
7572 map->includes_phdrs = 0;
7573 if (! phdr_included || segment->p_type != PT_LOAD)
7574 {
7575 map->includes_phdrs =
7576 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7577 && (segment->p_offset + segment->p_filesz
7578 >= ((bfd_vma) iehdr->e_phoff
7579 + iehdr->e_phnum * iehdr->e_phentsize)));
7580
7581 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7582 phdr_included = TRUE;
7583 }
7584
bbefd0a9 7585 lowest_section = NULL;
84d1d650
L
7586 if (section_count != 0)
7587 {
7588 unsigned int isec = 0;
7589
53020534 7590 for (section = first_section;
84d1d650
L
7591 section != NULL;
7592 section = section->next)
7593 {
7594 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7595 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
53020534
L
7596 {
7597 map->sections[isec++] = section->output_section;
a76e6f2f
AM
7598 if ((section->flags & SEC_ALLOC) != 0)
7599 {
7600 bfd_vma seg_off;
7601
bbefd0a9
AM
7602 if (lowest_section == NULL
7603 || section->lma < lowest_section->lma)
fb8a5684
AM
7604 lowest_section = section;
7605
a76e6f2f
AM
7606 /* Section lmas are set up from PT_LOAD header
7607 p_paddr in _bfd_elf_make_section_from_shdr.
7608 If this header has a p_paddr that disagrees
7609 with the section lma, flag the p_paddr as
7610 invalid. */
7611 if ((section->flags & SEC_LOAD) != 0)
7612 seg_off = this_hdr->sh_offset - segment->p_offset;
7613 else
7614 seg_off = this_hdr->sh_addr - segment->p_vaddr;
502794d4 7615 if (section->lma * opb - segment->p_paddr != seg_off)
a76e6f2f
AM
7616 map->p_paddr_valid = FALSE;
7617 }
53020534
L
7618 if (isec == section_count)
7619 break;
7620 }
84d1d650
L
7621 }
7622 }
7623
5d695627 7624 if (section_count == 0)
502794d4 7625 map->p_vaddr_offset = segment->p_vaddr / opb;
30fe1832
AM
7626 else if (map->p_paddr_valid)
7627 {
7628 /* Account for padding before the first section in the segment. */
7629 bfd_vma hdr_size = 0;
7630 if (map->includes_filehdr)
7631 hdr_size = iehdr->e_ehsize;
7632 if (map->includes_phdrs)
7633 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7634
502794d4 7635 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
30fe1832
AM
7636 - (lowest_section ? lowest_section->lma : 0));
7637 }
a76e6f2f 7638
84d1d650
L
7639 map->count = section_count;
7640 *pointer_to_map = map;
7641 pointer_to_map = &map->next;
7642 }
7643
12bd6957 7644 elf_seg_map (obfd) = map_first;
84d1d650
L
7645 return TRUE;
7646}
7647
7648/* Copy private BFD data. This copies or rewrites ELF program header
7649 information. */
7650
7651static bfd_boolean
7652copy_private_bfd_data (bfd *ibfd, bfd *obfd)
7653{
c410035d
AM
7654 bfd_vma maxpagesize;
7655
84d1d650
L
7656 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7657 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7658 return TRUE;
7659
7660 if (elf_tdata (ibfd)->phdr == NULL)
7661 return TRUE;
7662
7663 if (ibfd->xvec == obfd->xvec)
7664 {
cb3ff1e5
NC
7665 /* Check to see if any sections in the input BFD
7666 covered by ELF program header have changed. */
d55ce4e2 7667 Elf_Internal_Phdr *segment;
84d1d650
L
7668 asection *section, *osec;
7669 unsigned int i, num_segments;
7670 Elf_Internal_Shdr *this_hdr;
147d51c2
L
7671 const struct elf_backend_data *bed;
7672
7673 bed = get_elf_backend_data (ibfd);
7674
7675 /* Regenerate the segment map if p_paddr is set to 0. */
7676 if (bed->want_p_paddr_set_to_zero)
7677 goto rewrite;
84d1d650
L
7678
7679 /* Initialize the segment mark field. */
7680 for (section = obfd->sections; section != NULL;
7681 section = section->next)
7682 section->segment_mark = FALSE;
7683
7684 num_segments = elf_elfheader (ibfd)->e_phnum;
7685 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7686 i < num_segments;
7687 i++, segment++)
7688 {
5f6999aa
NC
7689 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7690 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7691 which severly confuses things, so always regenerate the segment
7692 map in this case. */
7693 if (segment->p_paddr == 0
7694 && segment->p_memsz == 0
7695 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
cb3ff1e5 7696 goto rewrite;
5f6999aa 7697
84d1d650
L
7698 for (section = ibfd->sections;
7699 section != NULL; section = section->next)
7700 {
7701 /* We mark the output section so that we know it comes
7702 from the input BFD. */
7703 osec = section->output_section;
7704 if (osec)
7705 osec->segment_mark = TRUE;
7706
7707 /* Check if this section is covered by the segment. */
7708 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7709 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
84d1d650
L
7710 {
7711 /* FIXME: Check if its output section is changed or
7712 removed. What else do we need to check? */
7713 if (osec == NULL
7714 || section->flags != osec->flags
7715 || section->lma != osec->lma
7716 || section->vma != osec->vma
7717 || section->size != osec->size
7718 || section->rawsize != osec->rawsize
7719 || section->alignment_power != osec->alignment_power)
7720 goto rewrite;
7721 }
7722 }
7723 }
7724
cb3ff1e5 7725 /* Check to see if any output section do not come from the
84d1d650
L
7726 input BFD. */
7727 for (section = obfd->sections; section != NULL;
7728 section = section->next)
7729 {
535b785f 7730 if (!section->segment_mark)
84d1d650
L
7731 goto rewrite;
7732 else
7733 section->segment_mark = FALSE;
7734 }
7735
7736 return copy_elf_program_header (ibfd, obfd);
7737 }
7738
dc1e8a47 7739 rewrite:
c410035d 7740 maxpagesize = 0;
f1d85785
L
7741 if (ibfd->xvec == obfd->xvec)
7742 {
7743 /* When rewriting program header, set the output maxpagesize to
7744 the maximum alignment of input PT_LOAD segments. */
7745 Elf_Internal_Phdr *segment;
7746 unsigned int i;
7747 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
f1d85785
L
7748
7749 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7750 i < num_segments;
7751 i++, segment++)
7752 if (segment->p_type == PT_LOAD
7753 && maxpagesize < segment->p_align)
c86934ce
NC
7754 {
7755 /* PR 17512: file: f17299af. */
7756 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
695344c0 7757 /* xgettext:c-format */
2dcf00ce
AM
7758 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7759 PRIx64 " is too large"),
7760 ibfd, (uint64_t) segment->p_align);
c86934ce
NC
7761 else
7762 maxpagesize = segment->p_align;
7763 }
f1d85785 7764 }
c410035d
AM
7765 if (maxpagesize == 0)
7766 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
f1d85785 7767
c410035d 7768 return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
84d1d650
L
7769}
7770
ccd2ec6a
L
7771/* Initialize private output section information from input section. */
7772
7773bfd_boolean
7774_bfd_elf_init_private_section_data (bfd *ibfd,
7775 asection *isec,
7776 bfd *obfd,
7777 asection *osec,
7778 struct bfd_link_info *link_info)
7779
7780{
7781 Elf_Internal_Shdr *ihdr, *ohdr;
0e1862bb
L
7782 bfd_boolean final_link = (link_info != NULL
7783 && !bfd_link_relocatable (link_info));
ccd2ec6a
L
7784
7785 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7786 || obfd->xvec->flavour != bfd_target_elf_flavour)
7787 return TRUE;
7788
ba85c43e
NC
7789 BFD_ASSERT (elf_section_data (osec) != NULL);
7790
8c803a2d
AM
7791 /* If this is a known ABI section, ELF section type and flags may
7792 have been set up when OSEC was created. For normal sections we
7793 allow the user to override the type and flags other than
7794 SHF_MASKOS and SHF_MASKPROC. */
7795 if (elf_section_type (osec) == SHT_PROGBITS
7796 || elf_section_type (osec) == SHT_NOTE
7797 || elf_section_type (osec) == SHT_NOBITS)
7798 elf_section_type (osec) = SHT_NULL;
7799 /* For objcopy and relocatable link, copy the ELF section type from
7800 the input file if the BFD section flags are the same. (If they
7801 are different the user may be doing something like
7802 "objcopy --set-section-flags .text=alloc,data".) For a final
7803 link allow some flags that the linker clears to differ. */
42bb2e33 7804 if (elf_section_type (osec) == SHT_NULL
dfa7b0b8
AM
7805 && (osec->flags == isec->flags
7806 || (final_link
7807 && ((osec->flags ^ isec->flags)
0814be7d 7808 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
42bb2e33 7809 elf_section_type (osec) = elf_section_type (isec);
d270463e
L
7810
7811 /* FIXME: Is this correct for all OS/PROC specific flags? */
8c803a2d
AM
7812 elf_section_flags (osec) = (elf_section_flags (isec)
7813 & (SHF_MASKOS | SHF_MASKPROC));
ccd2ec6a 7814
a91e1603 7815 /* Copy sh_info from input for mbind section. */
df3a023b
AM
7816 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
7817 && elf_section_flags (isec) & SHF_GNU_MBIND)
a91e1603
L
7818 elf_section_data (osec)->this_hdr.sh_info
7819 = elf_section_data (isec)->this_hdr.sh_info;
7820
ccd2ec6a
L
7821 /* Set things up for objcopy and relocatable link. The output
7822 SHT_GROUP section will have its elf_next_in_group pointing back
7823 to the input group members. Ignore linker created group section.
7824 See elfNN_ia64_object_p in elfxx-ia64.c. */
7bdf4127
AB
7825 if ((link_info == NULL
7826 || !link_info->resolve_section_groups)
7827 && (elf_sec_group (isec) == NULL
7828 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
ccd2ec6a 7829 {
7bdf4127
AB
7830 if (elf_section_flags (isec) & SHF_GROUP)
7831 elf_section_flags (osec) |= SHF_GROUP;
7832 elf_next_in_group (osec) = elf_next_in_group (isec);
7833 elf_section_data (osec)->group = elf_section_data (isec)->group;
ccd2ec6a
L
7834 }
7835
7bdf4127
AB
7836 /* If not decompress, preserve SHF_COMPRESSED. */
7837 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
7838 elf_section_flags (osec) |= (elf_section_flags (isec)
7839 & SHF_COMPRESSED);
7840
ccd2ec6a
L
7841 ihdr = &elf_section_data (isec)->this_hdr;
7842
7843 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7844 don't use the output section of the linked-to section since it
7845 may be NULL at this point. */
7846 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
7847 {
7848 ohdr = &elf_section_data (osec)->this_hdr;
7849 ohdr->sh_flags |= SHF_LINK_ORDER;
7850 elf_linked_to_section (osec) = elf_linked_to_section (isec);
7851 }
7852
7853 osec->use_rela_p = isec->use_rela_p;
7854
7855 return TRUE;
7856}
7857
252b5132
RH
7858/* Copy private section information. This copies over the entsize
7859 field, and sometimes the info field. */
7860
b34976b6 7861bfd_boolean
217aa764
AM
7862_bfd_elf_copy_private_section_data (bfd *ibfd,
7863 asection *isec,
7864 bfd *obfd,
7865 asection *osec)
252b5132
RH
7866{
7867 Elf_Internal_Shdr *ihdr, *ohdr;
7868
7869 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7870 || obfd->xvec->flavour != bfd_target_elf_flavour)
b34976b6 7871 return TRUE;
252b5132 7872
252b5132
RH
7873 ihdr = &elf_section_data (isec)->this_hdr;
7874 ohdr = &elf_section_data (osec)->this_hdr;
7875
7876 ohdr->sh_entsize = ihdr->sh_entsize;
7877
7878 if (ihdr->sh_type == SHT_SYMTAB
7879 || ihdr->sh_type == SHT_DYNSYM
7880 || ihdr->sh_type == SHT_GNU_verneed
7881 || ihdr->sh_type == SHT_GNU_verdef)
7882 ohdr->sh_info = ihdr->sh_info;
7883
ccd2ec6a
L
7884 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
7885 NULL);
252b5132
RH
7886}
7887
d0bf826b
AM
7888/* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7889 necessary if we are removing either the SHT_GROUP section or any of
7890 the group member sections. DISCARDED is the value that a section's
7891 output_section has if the section will be discarded, NULL when this
7892 function is called from objcopy, bfd_abs_section_ptr when called
7893 from the linker. */
80fccad2
BW
7894
7895bfd_boolean
d0bf826b 7896_bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
80fccad2 7897{
30288845
AM
7898 asection *isec;
7899
30288845 7900 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
415f38a6 7901 if (elf_section_type (isec) == SHT_GROUP)
30288845
AM
7902 {
7903 asection *first = elf_next_in_group (isec);
7904 asection *s = first;
d0bf826b
AM
7905 bfd_size_type removed = 0;
7906
30288845
AM
7907 while (s != NULL)
7908 {
415f38a6
AM
7909 /* If this member section is being output but the
7910 SHT_GROUP section is not, then clear the group info
7911 set up by _bfd_elf_copy_private_section_data. */
d0bf826b
AM
7912 if (s->output_section != discarded
7913 && isec->output_section == discarded)
30288845
AM
7914 {
7915 elf_section_flags (s->output_section) &= ~SHF_GROUP;
7916 elf_group_name (s->output_section) = NULL;
7917 }
3349112e 7918 else
6e5e9d58
AM
7919 {
7920 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
3349112e
L
7921 if (s->output_section == discarded
7922 && isec->output_section != discarded)
7923 {
7924 /* Conversely, if the member section is not being
7925 output but the SHT_GROUP section is, then adjust
7926 its size. */
7927 removed += 4;
7928 if (elf_sec->rel.hdr != NULL
7929 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
7930 removed += 4;
7931 if (elf_sec->rela.hdr != NULL
7932 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
7933 removed += 4;
7934 }
7935 else
7936 {
7937 /* Also adjust for zero-sized relocation member
7938 section. */
7939 if (elf_sec->rel.hdr != NULL
7940 && elf_sec->rel.hdr->sh_size == 0)
7941 removed += 4;
7942 if (elf_sec->rela.hdr != NULL
7943 && elf_sec->rela.hdr->sh_size == 0)
7944 removed += 4;
7945 }
6e5e9d58 7946 }
30288845
AM
7947 s = elf_next_in_group (s);
7948 if (s == first)
7949 break;
7950 }
d0bf826b
AM
7951 if (removed != 0)
7952 {
7953 if (discarded != NULL)
7954 {
7955 /* If we've been called for ld -r, then we need to
6e5e9d58 7956 adjust the input section size. */
d0bf826b
AM
7957 if (isec->rawsize == 0)
7958 isec->rawsize = isec->size;
7959 isec->size = isec->rawsize - removed;
6e5e9d58
AM
7960 if (isec->size <= 4)
7961 {
7962 isec->size = 0;
7963 isec->flags |= SEC_EXCLUDE;
7964 }
d0bf826b
AM
7965 }
7966 else
7967 {
7968 /* Adjust the output section size when called from
7969 objcopy. */
7970 isec->output_section->size -= removed;
6e5e9d58
AM
7971 if (isec->output_section->size <= 4)
7972 {
7973 isec->output_section->size = 0;
7974 isec->output_section->flags |= SEC_EXCLUDE;
7975 }
d0bf826b
AM
7976 }
7977 }
30288845
AM
7978 }
7979
80fccad2
BW
7980 return TRUE;
7981}
7982
d0bf826b
AM
7983/* Copy private header information. */
7984
7985bfd_boolean
7986_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
7987{
7988 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7989 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7990 return TRUE;
7991
7992 /* Copy over private BFD data if it has not already been copied.
7993 This must be done here, rather than in the copy_private_bfd_data
7994 entry point, because the latter is called after the section
7995 contents have been set, which means that the program headers have
7996 already been worked out. */
12bd6957 7997 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
d0bf826b
AM
7998 {
7999 if (! copy_private_bfd_data (ibfd, obfd))
8000 return FALSE;
8001 }
8002
8003 return _bfd_elf_fixup_group_sections (ibfd, NULL);
8004}
8005
252b5132
RH
8006/* Copy private symbol information. If this symbol is in a section
8007 which we did not map into a BFD section, try to map the section
8008 index correctly. We use special macro definitions for the mapped
8009 section indices; these definitions are interpreted by the
8010 swap_out_syms function. */
8011
9ad5cbcf
AM
8012#define MAP_ONESYMTAB (SHN_HIOS + 1)
8013#define MAP_DYNSYMTAB (SHN_HIOS + 2)
8014#define MAP_STRTAB (SHN_HIOS + 3)
8015#define MAP_SHSTRTAB (SHN_HIOS + 4)
8016#define MAP_SYM_SHNDX (SHN_HIOS + 5)
252b5132 8017
b34976b6 8018bfd_boolean
217aa764
AM
8019_bfd_elf_copy_private_symbol_data (bfd *ibfd,
8020 asymbol *isymarg,
8021 bfd *obfd,
8022 asymbol *osymarg)
252b5132
RH
8023{
8024 elf_symbol_type *isym, *osym;
8025
8026 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8027 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 8028 return TRUE;
252b5132 8029
c1229f84
AM
8030 isym = elf_symbol_from (isymarg);
8031 osym = elf_symbol_from (osymarg);
252b5132
RH
8032
8033 if (isym != NULL
8424d8f5 8034 && isym->internal_elf_sym.st_shndx != 0
252b5132
RH
8035 && osym != NULL
8036 && bfd_is_abs_section (isym->symbol.section))
8037 {
8038 unsigned int shndx;
8039
8040 shndx = isym->internal_elf_sym.st_shndx;
8041 if (shndx == elf_onesymtab (ibfd))
8042 shndx = MAP_ONESYMTAB;
8043 else if (shndx == elf_dynsymtab (ibfd))
8044 shndx = MAP_DYNSYMTAB;
12bd6957 8045 else if (shndx == elf_strtab_sec (ibfd))
252b5132 8046 shndx = MAP_STRTAB;
12bd6957 8047 else if (shndx == elf_shstrtab_sec (ibfd))
252b5132 8048 shndx = MAP_SHSTRTAB;
6a40cf0c 8049 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
9ad5cbcf 8050 shndx = MAP_SYM_SHNDX;
252b5132
RH
8051 osym->internal_elf_sym.st_shndx = shndx;
8052 }
8053
b34976b6 8054 return TRUE;
252b5132
RH
8055}
8056
8057/* Swap out the symbols. */
8058
b34976b6 8059static bfd_boolean
217aa764 8060swap_out_syms (bfd *abfd,
ef10c3ac 8061 struct elf_strtab_hash **sttp,
3d16b64e
NA
8062 int relocatable_p,
8063 struct bfd_link_info *info)
252b5132 8064{
9c5bfbb7 8065 const struct elf_backend_data *bed;
1f4361a7 8066 unsigned int symcount;
079e9a2f 8067 asymbol **syms;
ef10c3ac 8068 struct elf_strtab_hash *stt;
079e9a2f 8069 Elf_Internal_Shdr *symtab_hdr;
9ad5cbcf 8070 Elf_Internal_Shdr *symtab_shndx_hdr;
079e9a2f 8071 Elf_Internal_Shdr *symstrtab_hdr;
ef10c3ac 8072 struct elf_sym_strtab *symstrtab;
f075ee0c
AM
8073 bfd_byte *outbound_syms;
8074 bfd_byte *outbound_shndx;
ef10c3ac
L
8075 unsigned long outbound_syms_index;
8076 unsigned long outbound_shndx_index;
1f4361a7 8077 unsigned int idx;
12bd6957 8078 unsigned int num_locals;
1f4361a7 8079 size_t amt;
174fd7f9 8080 bfd_boolean name_local_sections;
252b5132 8081
12bd6957 8082 if (!elf_map_symbols (abfd, &num_locals))
b34976b6 8083 return FALSE;
252b5132 8084
c044fabd 8085 /* Dump out the symtabs. */
ef10c3ac 8086 stt = _bfd_elf_strtab_init ();
079e9a2f 8087 if (stt == NULL)
b34976b6 8088 return FALSE;
252b5132 8089
079e9a2f
AM
8090 bed = get_elf_backend_data (abfd);
8091 symcount = bfd_get_symcount (abfd);
8092 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8093 symtab_hdr->sh_type = SHT_SYMTAB;
8094 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8095 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
12bd6957 8096 symtab_hdr->sh_info = num_locals + 1;
72de5009 8097 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
079e9a2f
AM
8098
8099 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8100 symstrtab_hdr->sh_type = SHT_STRTAB;
8101
ef10c3ac 8102 /* Allocate buffer to swap out the .strtab section. */
1f4361a7
AM
8103 if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8104 || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
ef10c3ac 8105 {
1f4361a7 8106 bfd_set_error (bfd_error_no_memory);
ef10c3ac
L
8107 _bfd_elf_strtab_free (stt);
8108 return FALSE;
8109 }
8110
1f4361a7
AM
8111 if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8112 || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
5ed6aba4 8113 {
1f4361a7
AM
8114 error_no_mem:
8115 bfd_set_error (bfd_error_no_memory);
8116 error_return:
ef10c3ac 8117 free (symstrtab);
1f4361a7 8118 _bfd_elf_strtab_free (stt);
5ed6aba4
NC
8119 return FALSE;
8120 }
217aa764 8121 symtab_hdr->contents = outbound_syms;
ef10c3ac 8122 outbound_syms_index = 0;
252b5132 8123
9ad5cbcf 8124 outbound_shndx = NULL;
ef10c3ac 8125 outbound_shndx_index = 0;
6a40cf0c
NC
8126
8127 if (elf_symtab_shndx_list (abfd))
9ad5cbcf 8128 {
6a40cf0c
NC
8129 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8130 if (symtab_shndx_hdr->sh_name != 0)
8131 {
1f4361a7
AM
8132 if (_bfd_mul_overflow (symcount + 1,
8133 sizeof (Elf_External_Sym_Shndx), &amt))
8134 goto error_no_mem;
8135 outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt);
6a40cf0c
NC
8136 if (outbound_shndx == NULL)
8137 goto error_return;
5ed6aba4 8138
6a40cf0c
NC
8139 symtab_shndx_hdr->contents = outbound_shndx;
8140 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8141 symtab_shndx_hdr->sh_size = amt;
8142 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8143 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8144 }
8145 /* FIXME: What about any other headers in the list ? */
9ad5cbcf
AM
8146 }
8147
589e6347 8148 /* Now generate the data (for "contents"). */
079e9a2f
AM
8149 {
8150 /* Fill in zeroth symbol and swap it out. */
8151 Elf_Internal_Sym sym;
8152 sym.st_name = 0;
8153 sym.st_value = 0;
8154 sym.st_size = 0;
8155 sym.st_info = 0;
8156 sym.st_other = 0;
8157 sym.st_shndx = SHN_UNDEF;
35fc36a8 8158 sym.st_target_internal = 0;
ef10c3ac
L
8159 symstrtab[0].sym = sym;
8160 symstrtab[0].dest_index = outbound_syms_index;
8161 symstrtab[0].destshndx_index = outbound_shndx_index;
8162 outbound_syms_index++;
9ad5cbcf 8163 if (outbound_shndx != NULL)
ef10c3ac 8164 outbound_shndx_index++;
079e9a2f 8165 }
252b5132 8166
174fd7f9
RS
8167 name_local_sections
8168 = (bed->elf_backend_name_local_section_symbols
8169 && bed->elf_backend_name_local_section_symbols (abfd));
8170
079e9a2f 8171 syms = bfd_get_outsymbols (abfd);
ef10c3ac 8172 for (idx = 0; idx < symcount;)
252b5132 8173 {
252b5132 8174 Elf_Internal_Sym sym;
079e9a2f
AM
8175 bfd_vma value = syms[idx]->value;
8176 elf_symbol_type *type_ptr;
8177 flagword flags = syms[idx]->flags;
8178 int type;
252b5132 8179
174fd7f9
RS
8180 if (!name_local_sections
8181 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
079e9a2f
AM
8182 {
8183 /* Local section symbols have no name. */
ef10c3ac 8184 sym.st_name = (unsigned long) -1;
079e9a2f
AM
8185 }
8186 else
8187 {
ef10c3ac
L
8188 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8189 to get the final offset for st_name. */
8190 sym.st_name
8191 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
8192 FALSE);
079e9a2f 8193 if (sym.st_name == (unsigned long) -1)
ef10c3ac 8194 goto error_return;
079e9a2f 8195 }
252b5132 8196
c1229f84 8197 type_ptr = elf_symbol_from (syms[idx]);
252b5132 8198
079e9a2f
AM
8199 if ((flags & BSF_SECTION_SYM) == 0
8200 && bfd_is_com_section (syms[idx]->section))
8201 {
8202 /* ELF common symbols put the alignment into the `value' field,
8203 and the size into the `size' field. This is backwards from
8204 how BFD handles it, so reverse it here. */
8205 sym.st_size = value;
8206 if (type_ptr == NULL
8207 || type_ptr->internal_elf_sym.st_value == 0)
8208 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8209 else
8210 sym.st_value = type_ptr->internal_elf_sym.st_value;
8211 sym.st_shndx = _bfd_elf_section_from_bfd_section
8212 (abfd, syms[idx]->section);
8213 }
8214 else
8215 {
8216 asection *sec = syms[idx]->section;
cb33740c 8217 unsigned int shndx;
252b5132 8218
079e9a2f
AM
8219 if (sec->output_section)
8220 {
8221 value += sec->output_offset;
8222 sec = sec->output_section;
8223 }
589e6347 8224
079e9a2f
AM
8225 /* Don't add in the section vma for relocatable output. */
8226 if (! relocatable_p)
8227 value += sec->vma;
8228 sym.st_value = value;
8229 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8230
8231 if (bfd_is_abs_section (sec)
8232 && type_ptr != NULL
8233 && type_ptr->internal_elf_sym.st_shndx != 0)
8234 {
8235 /* This symbol is in a real ELF section which we did
8236 not create as a BFD section. Undo the mapping done
8237 by copy_private_symbol_data. */
8238 shndx = type_ptr->internal_elf_sym.st_shndx;
8239 switch (shndx)
8240 {
8241 case MAP_ONESYMTAB:
8242 shndx = elf_onesymtab (abfd);
8243 break;
8244 case MAP_DYNSYMTAB:
8245 shndx = elf_dynsymtab (abfd);
8246 break;
8247 case MAP_STRTAB:
12bd6957 8248 shndx = elf_strtab_sec (abfd);
079e9a2f
AM
8249 break;
8250 case MAP_SHSTRTAB:
12bd6957 8251 shndx = elf_shstrtab_sec (abfd);
079e9a2f 8252 break;
9ad5cbcf 8253 case MAP_SYM_SHNDX:
6a40cf0c
NC
8254 if (elf_symtab_shndx_list (abfd))
8255 shndx = elf_symtab_shndx_list (abfd)->ndx;
9ad5cbcf 8256 break;
00e49dff
NC
8257 case SHN_COMMON:
8258 case SHN_ABS:
15bc576a 8259 shndx = SHN_ABS;
079e9a2f 8260 break;
00e49dff
NC
8261 default:
8262 if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8263 {
8264 if (bed->symbol_section_index)
8265 shndx = bed->symbol_section_index (abfd, type_ptr);
8266 /* Otherwise just leave the index alone. */
8267 }
8268 else
8269 {
8270 if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8271 _bfd_error_handler (_("%pB: \
8272Unable to handle section index %x in ELF symbol. Using ABS instead."),
8273 abfd, shndx);
8274 shndx = SHN_ABS;
8275 }
8276 break;
079e9a2f
AM
8277 }
8278 }
8279 else
8280 {
8281 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
252b5132 8282
cb33740c 8283 if (shndx == SHN_BAD)
079e9a2f
AM
8284 {
8285 asection *sec2;
8286
8287 /* Writing this would be a hell of a lot easier if
8288 we had some decent documentation on bfd, and
8289 knew what to expect of the library, and what to
8290 demand of applications. For example, it
8291 appears that `objcopy' might not set the
8292 section of a symbol to be a section that is
8293 actually in the output file. */
8294 sec2 = bfd_get_section_by_name (abfd, sec->name);
5df1bc57
AM
8295 if (sec2 != NULL)
8296 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8297 if (shndx == SHN_BAD)
589e6347 8298 {
695344c0 8299 /* xgettext:c-format */
9793eb77
AM
8300 _bfd_error_handler
8301 (_("unable to find equivalent output section"
8302 " for symbol '%s' from section '%s'"),
8303 syms[idx]->name ? syms[idx]->name : "<Local sym>",
8304 sec->name);
811072d8 8305 bfd_set_error (bfd_error_invalid_operation);
ef10c3ac 8306 goto error_return;
589e6347 8307 }
079e9a2f
AM
8308 }
8309 }
252b5132 8310
079e9a2f
AM
8311 sym.st_shndx = shndx;
8312 }
252b5132 8313
13ae64f3
JJ
8314 if ((flags & BSF_THREAD_LOCAL) != 0)
8315 type = STT_TLS;
d8045f23
NC
8316 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8317 type = STT_GNU_IFUNC;
13ae64f3 8318 else if ((flags & BSF_FUNCTION) != 0)
079e9a2f
AM
8319 type = STT_FUNC;
8320 else if ((flags & BSF_OBJECT) != 0)
8321 type = STT_OBJECT;
d9352518
DB
8322 else if ((flags & BSF_RELC) != 0)
8323 type = STT_RELC;
8324 else if ((flags & BSF_SRELC) != 0)
8325 type = STT_SRELC;
079e9a2f
AM
8326 else
8327 type = STT_NOTYPE;
252b5132 8328
13ae64f3
JJ
8329 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8330 type = STT_TLS;
8331
589e6347 8332 /* Processor-specific types. */
079e9a2f
AM
8333 if (type_ptr != NULL
8334 && bed->elf_backend_get_symbol_type)
8335 type = ((*bed->elf_backend_get_symbol_type)
8336 (&type_ptr->internal_elf_sym, type));
252b5132 8337
079e9a2f
AM
8338 if (flags & BSF_SECTION_SYM)
8339 {
8340 if (flags & BSF_GLOBAL)
8341 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8342 else
8343 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8344 }
8345 else if (bfd_is_com_section (syms[idx]->section))
0a40daed 8346 {
b8871f35
L
8347 if (type != STT_TLS)
8348 {
8349 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8350 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8351 ? STT_COMMON : STT_OBJECT);
8352 else
8353 type = ((flags & BSF_ELF_COMMON) != 0
8354 ? STT_COMMON : STT_OBJECT);
8355 }
8356 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
0a40daed 8357 }
079e9a2f
AM
8358 else if (bfd_is_und_section (syms[idx]->section))
8359 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8360 ? STB_WEAK
8361 : STB_GLOBAL),
8362 type);
8363 else if (flags & BSF_FILE)
8364 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8365 else
8366 {
8367 int bind = STB_LOCAL;
252b5132 8368
079e9a2f
AM
8369 if (flags & BSF_LOCAL)
8370 bind = STB_LOCAL;
3e7a7d11
NC
8371 else if (flags & BSF_GNU_UNIQUE)
8372 bind = STB_GNU_UNIQUE;
079e9a2f
AM
8373 else if (flags & BSF_WEAK)
8374 bind = STB_WEAK;
8375 else if (flags & BSF_GLOBAL)
8376 bind = STB_GLOBAL;
252b5132 8377
079e9a2f
AM
8378 sym.st_info = ELF_ST_INFO (bind, type);
8379 }
252b5132 8380
079e9a2f 8381 if (type_ptr != NULL)
35fc36a8
RS
8382 {
8383 sym.st_other = type_ptr->internal_elf_sym.st_other;
8384 sym.st_target_internal
8385 = type_ptr->internal_elf_sym.st_target_internal;
8386 }
079e9a2f 8387 else
35fc36a8
RS
8388 {
8389 sym.st_other = 0;
8390 sym.st_target_internal = 0;
8391 }
252b5132 8392
ef10c3ac
L
8393 idx++;
8394 symstrtab[idx].sym = sym;
8395 symstrtab[idx].dest_index = outbound_syms_index;
8396 symstrtab[idx].destshndx_index = outbound_shndx_index;
8397
8398 outbound_syms_index++;
9ad5cbcf 8399 if (outbound_shndx != NULL)
ef10c3ac
L
8400 outbound_shndx_index++;
8401 }
8402
8403 /* Finalize the .strtab section. */
8404 _bfd_elf_strtab_finalize (stt);
8405
8406 /* Swap out the .strtab section. */
8407 for (idx = 0; idx <= symcount; idx++)
8408 {
8409 struct elf_sym_strtab *elfsym = &symstrtab[idx];
8410 if (elfsym->sym.st_name == (unsigned long) -1)
8411 elfsym->sym.st_name = 0;
8412 else
8413 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8414 elfsym->sym.st_name);
3d16b64e
NA
8415 if (info && info->callbacks->ctf_new_symbol)
8416 info->callbacks->ctf_new_symbol (elfsym->dest_index,
8417 &elfsym->sym);
8418
8419 /* Inform the linker of the addition of this symbol. */
8420
ef10c3ac
L
8421 bed->s->swap_symbol_out (abfd, &elfsym->sym,
8422 (outbound_syms
8423 + (elfsym->dest_index
8424 * bed->s->sizeof_sym)),
8425 (outbound_shndx
8426 + (elfsym->destshndx_index
8427 * sizeof (Elf_External_Sym_Shndx))));
079e9a2f 8428 }
ef10c3ac 8429 free (symstrtab);
252b5132 8430
079e9a2f 8431 *sttp = stt;
ef10c3ac 8432 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
079e9a2f 8433 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 8434 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
079e9a2f
AM
8435 symstrtab_hdr->sh_addr = 0;
8436 symstrtab_hdr->sh_entsize = 0;
8437 symstrtab_hdr->sh_link = 0;
8438 symstrtab_hdr->sh_info = 0;
8439 symstrtab_hdr->sh_addralign = 1;
252b5132 8440
b34976b6 8441 return TRUE;
252b5132
RH
8442}
8443
8444/* Return the number of bytes required to hold the symtab vector.
8445
8446 Note that we base it on the count plus 1, since we will null terminate
8447 the vector allocated based on this size. However, the ELF symbol table
8448 always has a dummy entry as symbol #0, so it ends up even. */
8449
8450long
217aa764 8451_bfd_elf_get_symtab_upper_bound (bfd *abfd)
252b5132 8452{
3a551c7a 8453 bfd_size_type symcount;
252b5132
RH
8454 long symtab_size;
8455 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
8456
8457 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
b5f386d5 8458 if (symcount > LONG_MAX / sizeof (asymbol *))
3a551c7a
AM
8459 {
8460 bfd_set_error (bfd_error_file_too_big);
8461 return -1;
8462 }
b5f386d5
AM
8463 symtab_size = symcount * (sizeof (asymbol *));
8464 if (symcount == 0)
8465 symtab_size = sizeof (asymbol *);
8466 else if (!bfd_write_p (abfd))
8467 {
8468 ufile_ptr filesize = bfd_get_file_size (abfd);
8469
8470 if (filesize != 0 && (unsigned long) symtab_size > filesize)
8471 {
8472 bfd_set_error (bfd_error_file_truncated);
8473 return -1;
8474 }
8475 }
252b5132
RH
8476
8477 return symtab_size;
8478}
8479
8480long
217aa764 8481_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
252b5132 8482{
3a551c7a 8483 bfd_size_type symcount;
252b5132
RH
8484 long symtab_size;
8485 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
8486
8487 if (elf_dynsymtab (abfd) == 0)
8488 {
8489 bfd_set_error (bfd_error_invalid_operation);
8490 return -1;
8491 }
8492
8493 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
b5f386d5 8494 if (symcount > LONG_MAX / sizeof (asymbol *))
3a551c7a
AM
8495 {
8496 bfd_set_error (bfd_error_file_too_big);
8497 return -1;
8498 }
b5f386d5
AM
8499 symtab_size = symcount * (sizeof (asymbol *));
8500 if (symcount == 0)
8501 symtab_size = sizeof (asymbol *);
8502 else if (!bfd_write_p (abfd))
8503 {
8504 ufile_ptr filesize = bfd_get_file_size (abfd);
8505
8506 if (filesize != 0 && (unsigned long) symtab_size > filesize)
8507 {
8508 bfd_set_error (bfd_error_file_truncated);
8509 return -1;
8510 }
8511 }
252b5132
RH
8512
8513 return symtab_size;
8514}
8515
8516long
3c568b8a 8517_bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
252b5132 8518{
b5f386d5 8519 if (asect->reloc_count != 0 && !bfd_write_p (abfd))
3c568b8a
AM
8520 {
8521 /* Sanity check reloc section size. */
8522 struct bfd_elf_section_data *d = elf_section_data (asect);
8523 Elf_Internal_Shdr *rel_hdr = &d->this_hdr;
8524 bfd_size_type ext_rel_size = rel_hdr->sh_size;
8525 ufile_ptr filesize = bfd_get_file_size (abfd);
8526
8527 if (filesize != 0 && ext_rel_size > filesize)
8528 {
8529 bfd_set_error (bfd_error_file_truncated);
8530 return -1;
8531 }
8532 }
8533
242a1159 8534#if SIZEOF_LONG == SIZEOF_INT
7a6e0d89
AM
8535 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
8536 {
8537 bfd_set_error (bfd_error_file_too_big);
8538 return -1;
8539 }
242a1159 8540#endif
252b5132
RH
8541 return (asect->reloc_count + 1) * sizeof (arelent *);
8542}
8543
8544/* Canonicalize the relocs. */
8545
8546long
217aa764
AM
8547_bfd_elf_canonicalize_reloc (bfd *abfd,
8548 sec_ptr section,
8549 arelent **relptr,
8550 asymbol **symbols)
252b5132
RH
8551{
8552 arelent *tblptr;
8553 unsigned int i;
9c5bfbb7 8554 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 8555
b34976b6 8556 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
252b5132
RH
8557 return -1;
8558
8559 tblptr = section->relocation;
8560 for (i = 0; i < section->reloc_count; i++)
8561 *relptr++ = tblptr++;
8562
8563 *relptr = NULL;
8564
8565 return section->reloc_count;
8566}
8567
8568long
6cee3f79 8569_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
252b5132 8570{
9c5bfbb7 8571 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764 8572 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
252b5132
RH
8573
8574 if (symcount >= 0)
ed48ec2e 8575 abfd->symcount = symcount;
252b5132
RH
8576 return symcount;
8577}
8578
8579long
217aa764
AM
8580_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
8581 asymbol **allocation)
252b5132 8582{
9c5bfbb7 8583 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764 8584 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
1f70368c
DJ
8585
8586 if (symcount >= 0)
ed48ec2e 8587 abfd->dynsymcount = symcount;
1f70368c 8588 return symcount;
252b5132
RH
8589}
8590
8615f3f2
AM
8591/* Return the size required for the dynamic reloc entries. Any loadable
8592 section that was actually installed in the BFD, and has type SHT_REL
8593 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8594 dynamic reloc section. */
252b5132
RH
8595
8596long
217aa764 8597_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
252b5132 8598{
3c568b8a 8599 bfd_size_type count, ext_rel_size;
252b5132
RH
8600 asection *s;
8601
8602 if (elf_dynsymtab (abfd) == 0)
8603 {
8604 bfd_set_error (bfd_error_invalid_operation);
8605 return -1;
8606 }
8607
3a551c7a 8608 count = 1;
3c568b8a 8609 ext_rel_size = 0;
252b5132 8610 for (s = abfd->sections; s != NULL; s = s->next)
266b05cf 8611 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132
RH
8612 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8613 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
3a551c7a 8614 {
3c568b8a
AM
8615 ext_rel_size += s->size;
8616 if (ext_rel_size < s->size)
8617 {
8618 bfd_set_error (bfd_error_file_truncated);
8619 return -1;
8620 }
3a551c7a
AM
8621 count += s->size / elf_section_data (s)->this_hdr.sh_entsize;
8622 if (count > LONG_MAX / sizeof (arelent *))
8623 {
8624 bfd_set_error (bfd_error_file_too_big);
8625 return -1;
8626 }
8627 }
b5f386d5 8628 if (count > 1 && !bfd_write_p (abfd))
3c568b8a
AM
8629 {
8630 /* Sanity check reloc section sizes. */
8631 ufile_ptr filesize = bfd_get_file_size (abfd);
8632 if (filesize != 0 && ext_rel_size > filesize)
8633 {
8634 bfd_set_error (bfd_error_file_truncated);
8635 return -1;
8636 }
8637 }
3a551c7a 8638 return count * sizeof (arelent *);
252b5132
RH
8639}
8640
8615f3f2
AM
8641/* Canonicalize the dynamic relocation entries. Note that we return the
8642 dynamic relocations as a single block, although they are actually
8643 associated with particular sections; the interface, which was
8644 designed for SunOS style shared libraries, expects that there is only
8645 one set of dynamic relocs. Any loadable section that was actually
8646 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8647 dynamic symbol table, is considered to be a dynamic reloc section. */
252b5132
RH
8648
8649long
217aa764
AM
8650_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
8651 arelent **storage,
8652 asymbol **syms)
252b5132 8653{
217aa764 8654 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
252b5132
RH
8655 asection *s;
8656 long ret;
8657
8658 if (elf_dynsymtab (abfd) == 0)
8659 {
8660 bfd_set_error (bfd_error_invalid_operation);
8661 return -1;
8662 }
8663
8664 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8665 ret = 0;
8666 for (s = abfd->sections; s != NULL; s = s->next)
8667 {
266b05cf 8668 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132
RH
8669 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8670 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8671 {
8672 arelent *p;
8673 long count, i;
8674
b34976b6 8675 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
252b5132 8676 return -1;
eea6121a 8677 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
252b5132
RH
8678 p = s->relocation;
8679 for (i = 0; i < count; i++)
8680 *storage++ = p++;
8681 ret += count;
8682 }
8683 }
8684
8685 *storage = NULL;
8686
8687 return ret;
8688}
8689\f
8690/* Read in the version information. */
8691
b34976b6 8692bfd_boolean
fc0e6df6 8693_bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
252b5132
RH
8694{
8695 bfd_byte *contents = NULL;
fc0e6df6 8696 unsigned int freeidx = 0;
1f4361a7 8697 size_t amt;
fc0e6df6
PB
8698
8699 if (elf_dynverref (abfd) != 0)
8700 {
8701 Elf_Internal_Shdr *hdr;
8702 Elf_External_Verneed *everneed;
8703 Elf_Internal_Verneed *iverneed;
8704 unsigned int i;
d0fb9a8d 8705 bfd_byte *contents_end;
fc0e6df6
PB
8706
8707 hdr = &elf_tdata (abfd)->dynverref_hdr;
8708
bd61e135
AM
8709 if (hdr->sh_info == 0
8710 || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
d0fb9a8d 8711 {
dc1e8a47 8712 error_return_bad_verref:
4eca0228 8713 _bfd_error_handler
871b3ab2 8714 (_("%pB: .gnu.version_r invalid entry"), abfd);
601a03ba 8715 bfd_set_error (bfd_error_bad_value);
dc1e8a47 8716 error_return_verref:
d0fb9a8d
JJ
8717 elf_tdata (abfd)->verref = NULL;
8718 elf_tdata (abfd)->cverrefs = 0;
8719 goto error_return;
8720 }
601a03ba 8721
2bb3687b
AM
8722 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
8723 goto error_return_verref;
8724 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
8725 if (contents == NULL)
d0fb9a8d 8726 goto error_return_verref;
fc0e6df6 8727
1f4361a7
AM
8728 if (_bfd_mul_overflow (hdr->sh_info, sizeof (Elf_Internal_Verneed), &amt))
8729 {
8730 bfd_set_error (bfd_error_file_too_big);
8731 goto error_return_verref;
8732 }
8733 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_alloc (abfd, amt);
601a03ba 8734 if (elf_tdata (abfd)->verref == NULL)
d0fb9a8d
JJ
8735 goto error_return_verref;
8736
8737 BFD_ASSERT (sizeof (Elf_External_Verneed)
8738 == sizeof (Elf_External_Vernaux));
8739 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
fc0e6df6
PB
8740 everneed = (Elf_External_Verneed *) contents;
8741 iverneed = elf_tdata (abfd)->verref;
8742 for (i = 0; i < hdr->sh_info; i++, iverneed++)
8743 {
8744 Elf_External_Vernaux *evernaux;
8745 Elf_Internal_Vernaux *ivernaux;
8746 unsigned int j;
8747
8748 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
8749
8750 iverneed->vn_bfd = abfd;
8751
8752 iverneed->vn_filename =
8753 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8754 iverneed->vn_file);
8755 if (iverneed->vn_filename == NULL)
601a03ba 8756 goto error_return_bad_verref;
fc0e6df6 8757
d0fb9a8d
JJ
8758 if (iverneed->vn_cnt == 0)
8759 iverneed->vn_auxptr = NULL;
8760 else
8761 {
1f4361a7
AM
8762 if (_bfd_mul_overflow (iverneed->vn_cnt,
8763 sizeof (Elf_Internal_Vernaux), &amt))
8764 {
8765 bfd_set_error (bfd_error_file_too_big);
8766 goto error_return_verref;
8767 }
a50b1753 8768 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
1f4361a7 8769 bfd_alloc (abfd, amt);
d0fb9a8d
JJ
8770 if (iverneed->vn_auxptr == NULL)
8771 goto error_return_verref;
8772 }
8773
8774 if (iverneed->vn_aux
8775 > (size_t) (contents_end - (bfd_byte *) everneed))
601a03ba 8776 goto error_return_bad_verref;
fc0e6df6
PB
8777
8778 evernaux = ((Elf_External_Vernaux *)
8779 ((bfd_byte *) everneed + iverneed->vn_aux));
8780 ivernaux = iverneed->vn_auxptr;
8781 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
8782 {
8783 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
8784
8785 ivernaux->vna_nodename =
8786 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8787 ivernaux->vna_name);
8788 if (ivernaux->vna_nodename == NULL)
601a03ba 8789 goto error_return_bad_verref;
fc0e6df6 8790
25ff461f
AM
8791 if (ivernaux->vna_other > freeidx)
8792 freeidx = ivernaux->vna_other;
8793
8794 ivernaux->vna_nextptr = NULL;
8795 if (ivernaux->vna_next == 0)
8796 {
8797 iverneed->vn_cnt = j + 1;
8798 break;
8799 }
fc0e6df6
PB
8800 if (j + 1 < iverneed->vn_cnt)
8801 ivernaux->vna_nextptr = ivernaux + 1;
fc0e6df6 8802
d0fb9a8d
JJ
8803 if (ivernaux->vna_next
8804 > (size_t) (contents_end - (bfd_byte *) evernaux))
601a03ba 8805 goto error_return_bad_verref;
d0fb9a8d 8806
fc0e6df6
PB
8807 evernaux = ((Elf_External_Vernaux *)
8808 ((bfd_byte *) evernaux + ivernaux->vna_next));
fc0e6df6
PB
8809 }
8810
25ff461f
AM
8811 iverneed->vn_nextref = NULL;
8812 if (iverneed->vn_next == 0)
8813 break;
fc0e6df6
PB
8814 if (i + 1 < hdr->sh_info)
8815 iverneed->vn_nextref = iverneed + 1;
fc0e6df6 8816
d0fb9a8d
JJ
8817 if (iverneed->vn_next
8818 > (size_t) (contents_end - (bfd_byte *) everneed))
601a03ba 8819 goto error_return_bad_verref;
d0fb9a8d 8820
fc0e6df6
PB
8821 everneed = ((Elf_External_Verneed *)
8822 ((bfd_byte *) everneed + iverneed->vn_next));
8823 }
25ff461f 8824 elf_tdata (abfd)->cverrefs = i;
fc0e6df6
PB
8825
8826 free (contents);
8827 contents = NULL;
8828 }
252b5132
RH
8829
8830 if (elf_dynverdef (abfd) != 0)
8831 {
8832 Elf_Internal_Shdr *hdr;
8833 Elf_External_Verdef *everdef;
8834 Elf_Internal_Verdef *iverdef;
f631889e
UD
8835 Elf_Internal_Verdef *iverdefarr;
8836 Elf_Internal_Verdef iverdefmem;
252b5132 8837 unsigned int i;
062e2358 8838 unsigned int maxidx;
d0fb9a8d 8839 bfd_byte *contents_end_def, *contents_end_aux;
252b5132
RH
8840
8841 hdr = &elf_tdata (abfd)->dynverdef_hdr;
8842
601a03ba
AM
8843 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
8844 {
8845 error_return_bad_verdef:
4eca0228 8846 _bfd_error_handler
871b3ab2 8847 (_("%pB: .gnu.version_d invalid entry"), abfd);
601a03ba
AM
8848 bfd_set_error (bfd_error_bad_value);
8849 error_return_verdef:
8850 elf_tdata (abfd)->verdef = NULL;
8851 elf_tdata (abfd)->cverdefs = 0;
8852 goto error_return;
8853 }
8854
2bb3687b 8855 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
601a03ba 8856 goto error_return_verdef;
2bb3687b
AM
8857 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
8858 if (contents == NULL)
601a03ba 8859 goto error_return_verdef;
d0fb9a8d
JJ
8860
8861 BFD_ASSERT (sizeof (Elf_External_Verdef)
8862 >= sizeof (Elf_External_Verdaux));
8863 contents_end_def = contents + hdr->sh_size
8864 - sizeof (Elf_External_Verdef);
8865 contents_end_aux = contents + hdr->sh_size
8866 - sizeof (Elf_External_Verdaux);
8867
f631889e
UD
8868 /* We know the number of entries in the section but not the maximum
8869 index. Therefore we have to run through all entries and find
8870 the maximum. */
252b5132 8871 everdef = (Elf_External_Verdef *) contents;
f631889e
UD
8872 maxidx = 0;
8873 for (i = 0; i < hdr->sh_info; ++i)
8874 {
8875 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8876
601a03ba
AM
8877 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
8878 goto error_return_bad_verdef;
062e2358
AM
8879 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
8880 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
f631889e 8881
25ff461f
AM
8882 if (iverdefmem.vd_next == 0)
8883 break;
8884
d0fb9a8d
JJ
8885 if (iverdefmem.vd_next
8886 > (size_t) (contents_end_def - (bfd_byte *) everdef))
601a03ba 8887 goto error_return_bad_verdef;
d0fb9a8d 8888
f631889e
UD
8889 everdef = ((Elf_External_Verdef *)
8890 ((bfd_byte *) everdef + iverdefmem.vd_next));
8891 }
8892
fc0e6df6
PB
8893 if (default_imported_symver)
8894 {
8895 if (freeidx > maxidx)
8896 maxidx = ++freeidx;
8897 else
8898 freeidx = ++maxidx;
8899 }
1f4361a7
AM
8900 if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
8901 {
8902 bfd_set_error (bfd_error_file_too_big);
8903 goto error_return_verdef;
8904 }
8905 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
f631889e 8906 if (elf_tdata (abfd)->verdef == NULL)
601a03ba 8907 goto error_return_verdef;
f631889e
UD
8908
8909 elf_tdata (abfd)->cverdefs = maxidx;
8910
8911 everdef = (Elf_External_Verdef *) contents;
8912 iverdefarr = elf_tdata (abfd)->verdef;
8913 for (i = 0; i < hdr->sh_info; i++)
252b5132
RH
8914 {
8915 Elf_External_Verdaux *everdaux;
8916 Elf_Internal_Verdaux *iverdaux;
8917 unsigned int j;
8918
f631889e
UD
8919 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8920
d0fb9a8d 8921 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
601a03ba 8922 goto error_return_bad_verdef;
d0fb9a8d 8923
f631889e 8924 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
595bce75 8925 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
252b5132
RH
8926
8927 iverdef->vd_bfd = abfd;
8928
d0fb9a8d
JJ
8929 if (iverdef->vd_cnt == 0)
8930 iverdef->vd_auxptr = NULL;
8931 else
8932 {
1f4361a7
AM
8933 if (_bfd_mul_overflow (iverdef->vd_cnt,
8934 sizeof (Elf_Internal_Verdaux), &amt))
8935 {
8936 bfd_set_error (bfd_error_file_too_big);
8937 goto error_return_verdef;
8938 }
a50b1753 8939 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
1f4361a7 8940 bfd_alloc (abfd, amt);
d0fb9a8d
JJ
8941 if (iverdef->vd_auxptr == NULL)
8942 goto error_return_verdef;
8943 }
8944
8945 if (iverdef->vd_aux
8946 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
601a03ba 8947 goto error_return_bad_verdef;
252b5132
RH
8948
8949 everdaux = ((Elf_External_Verdaux *)
8950 ((bfd_byte *) everdef + iverdef->vd_aux));
8951 iverdaux = iverdef->vd_auxptr;
8952 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
8953 {
8954 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
8955
8956 iverdaux->vda_nodename =
8957 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8958 iverdaux->vda_name);
8959 if (iverdaux->vda_nodename == NULL)
601a03ba 8960 goto error_return_bad_verdef;
252b5132 8961
25ff461f
AM
8962 iverdaux->vda_nextptr = NULL;
8963 if (iverdaux->vda_next == 0)
8964 {
8965 iverdef->vd_cnt = j + 1;
8966 break;
8967 }
252b5132
RH
8968 if (j + 1 < iverdef->vd_cnt)
8969 iverdaux->vda_nextptr = iverdaux + 1;
252b5132 8970
d0fb9a8d
JJ
8971 if (iverdaux->vda_next
8972 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
601a03ba 8973 goto error_return_bad_verdef;
d0fb9a8d 8974
252b5132
RH
8975 everdaux = ((Elf_External_Verdaux *)
8976 ((bfd_byte *) everdaux + iverdaux->vda_next));
8977 }
8978
595bce75 8979 iverdef->vd_nodename = NULL;
d0fb9a8d
JJ
8980 if (iverdef->vd_cnt)
8981 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
252b5132 8982
25ff461f
AM
8983 iverdef->vd_nextdef = NULL;
8984 if (iverdef->vd_next == 0)
8985 break;
d0fb9a8d 8986 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
252b5132 8987 iverdef->vd_nextdef = iverdef + 1;
252b5132
RH
8988
8989 everdef = ((Elf_External_Verdef *)
8990 ((bfd_byte *) everdef + iverdef->vd_next));
8991 }
8992
8993 free (contents);
8994 contents = NULL;
8995 }
fc0e6df6 8996 else if (default_imported_symver)
252b5132 8997 {
fc0e6df6
PB
8998 if (freeidx < 3)
8999 freeidx = 3;
9000 else
9001 freeidx++;
252b5132 9002
1f4361a7
AM
9003 if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
9004 {
9005 bfd_set_error (bfd_error_file_too_big);
9006 goto error_return;
9007 }
9008 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
fc0e6df6 9009 if (elf_tdata (abfd)->verdef == NULL)
252b5132
RH
9010 goto error_return;
9011
fc0e6df6
PB
9012 elf_tdata (abfd)->cverdefs = freeidx;
9013 }
252b5132 9014
fc0e6df6
PB
9015 /* Create a default version based on the soname. */
9016 if (default_imported_symver)
9017 {
9018 Elf_Internal_Verdef *iverdef;
9019 Elf_Internal_Verdaux *iverdaux;
252b5132 9020
5bb3703f 9021 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
252b5132 9022
fc0e6df6
PB
9023 iverdef->vd_version = VER_DEF_CURRENT;
9024 iverdef->vd_flags = 0;
9025 iverdef->vd_ndx = freeidx;
9026 iverdef->vd_cnt = 1;
252b5132 9027
fc0e6df6 9028 iverdef->vd_bfd = abfd;
252b5132 9029
fc0e6df6
PB
9030 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9031 if (iverdef->vd_nodename == NULL)
d0fb9a8d 9032 goto error_return_verdef;
fc0e6df6 9033 iverdef->vd_nextdef = NULL;
601a03ba
AM
9034 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
9035 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
d0fb9a8d
JJ
9036 if (iverdef->vd_auxptr == NULL)
9037 goto error_return_verdef;
252b5132 9038
fc0e6df6
PB
9039 iverdaux = iverdef->vd_auxptr;
9040 iverdaux->vda_nodename = iverdef->vd_nodename;
252b5132
RH
9041 }
9042
b34976b6 9043 return TRUE;
252b5132
RH
9044
9045 error_return:
c9594989 9046 free (contents);
b34976b6 9047 return FALSE;
252b5132
RH
9048}
9049\f
9050asymbol *
217aa764 9051_bfd_elf_make_empty_symbol (bfd *abfd)
252b5132
RH
9052{
9053 elf_symbol_type *newsym;
9054
7a6e0d89 9055 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
252b5132
RH
9056 if (!newsym)
9057 return NULL;
201159ec
NC
9058 newsym->symbol.the_bfd = abfd;
9059 return &newsym->symbol;
252b5132
RH
9060}
9061
9062void
217aa764
AM
9063_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9064 asymbol *symbol,
9065 symbol_info *ret)
252b5132
RH
9066{
9067 bfd_symbol_info (symbol, ret);
9068}
9069
9070/* Return whether a symbol name implies a local symbol. Most targets
9071 use this function for the is_local_label_name entry point, but some
9072 override it. */
9073
b34976b6 9074bfd_boolean
217aa764
AM
9075_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9076 const char *name)
252b5132
RH
9077{
9078 /* Normal local symbols start with ``.L''. */
9079 if (name[0] == '.' && name[1] == 'L')
b34976b6 9080 return TRUE;
252b5132
RH
9081
9082 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9083 DWARF debugging symbols starting with ``..''. */
9084 if (name[0] == '.' && name[1] == '.')
b34976b6 9085 return TRUE;
252b5132
RH
9086
9087 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9088 emitting DWARF debugging output. I suspect this is actually a
9089 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9090 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9091 underscore to be emitted on some ELF targets). For ease of use,
9092 we treat such symbols as local. */
9093 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
b34976b6 9094 return TRUE;
252b5132 9095
b1fa9dd6
NC
9096 /* Treat assembler generated fake symbols, dollar local labels and
9097 forward-backward labels (aka local labels) as locals.
9098 These labels have the form:
9099
07d6d2b8 9100 L0^A.* (fake symbols)
b1fa9dd6
NC
9101
9102 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9103
9104 Versions which start with .L will have already been matched above,
9105 so we only need to match the rest. */
9106 if (name[0] == 'L' && ISDIGIT (name[1]))
9107 {
9108 bfd_boolean ret = FALSE;
9109 const char * p;
9110 char c;
9111
9112 for (p = name + 2; (c = *p); p++)
9113 {
9114 if (c == 1 || c == 2)
9115 {
9116 if (c == 1 && p == name + 2)
9117 /* A fake symbol. */
9118 return TRUE;
9119
9120 /* FIXME: We are being paranoid here and treating symbols like
9121 L0^Bfoo as if there were non-local, on the grounds that the
9122 assembler will never generate them. But can any symbol
9123 containing an ASCII value in the range 1-31 ever be anything
9124 other than some kind of local ? */
9125 ret = TRUE;
9126 }
9127
9128 if (! ISDIGIT (c))
9129 {
9130 ret = FALSE;
9131 break;
9132 }
9133 }
9134 return ret;
9135 }
ffa54770 9136
b34976b6 9137 return FALSE;
252b5132
RH
9138}
9139
9140alent *
217aa764
AM
9141_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9142 asymbol *symbol ATTRIBUTE_UNUSED)
252b5132
RH
9143{
9144 abort ();
9145 return NULL;
9146}
9147
b34976b6 9148bfd_boolean
217aa764
AM
9149_bfd_elf_set_arch_mach (bfd *abfd,
9150 enum bfd_architecture arch,
9151 unsigned long machine)
252b5132
RH
9152{
9153 /* If this isn't the right architecture for this backend, and this
9154 isn't the generic backend, fail. */
9155 if (arch != get_elf_backend_data (abfd)->arch
9156 && arch != bfd_arch_unknown
9157 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
b34976b6 9158 return FALSE;
252b5132
RH
9159
9160 return bfd_default_set_arch_mach (abfd, arch, machine);
9161}
9162
d1fad7c6
NC
9163/* Find the nearest line to a particular section and offset,
9164 for error reporting. */
9165
b34976b6 9166bfd_boolean
217aa764 9167_bfd_elf_find_nearest_line (bfd *abfd,
217aa764 9168 asymbol **symbols,
fb167eb2 9169 asection *section,
217aa764
AM
9170 bfd_vma offset,
9171 const char **filename_ptr,
9172 const char **functionname_ptr,
fb167eb2
AM
9173 unsigned int *line_ptr,
9174 unsigned int *discriminator_ptr)
d1fad7c6 9175{
b34976b6 9176 bfd_boolean found;
d1fad7c6 9177
fb167eb2 9178 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
4e8a9624 9179 filename_ptr, functionname_ptr,
fb167eb2 9180 line_ptr, discriminator_ptr,
9defd221 9181 dwarf_debug_sections,
e7679060
AM
9182 &elf_tdata (abfd)->dwarf2_find_line_info))
9183 return TRUE;
9184
9185 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9186 filename_ptr, functionname_ptr, line_ptr))
d1fad7c6
NC
9187 {
9188 if (!*functionname_ptr)
e00e8198
AM
9189 _bfd_elf_find_function (abfd, symbols, section, offset,
9190 *filename_ptr ? NULL : filename_ptr,
9191 functionname_ptr);
b34976b6 9192 return TRUE;
d1fad7c6
NC
9193 }
9194
9195 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4e8a9624
AM
9196 &found, filename_ptr,
9197 functionname_ptr, line_ptr,
9198 &elf_tdata (abfd)->line_info))
b34976b6 9199 return FALSE;
dc43ada5 9200 if (found && (*functionname_ptr || *line_ptr))
b34976b6 9201 return TRUE;
d1fad7c6
NC
9202
9203 if (symbols == NULL)
b34976b6 9204 return FALSE;
d1fad7c6 9205
e00e8198
AM
9206 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9207 filename_ptr, functionname_ptr))
b34976b6 9208 return FALSE;
d1fad7c6 9209
252b5132 9210 *line_ptr = 0;
b34976b6 9211 return TRUE;
252b5132
RH
9212}
9213
5420f73d
L
9214/* Find the line for a symbol. */
9215
9216bfd_boolean
9217_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9218 const char **filename_ptr, unsigned int *line_ptr)
9b8d1a36 9219{
fb167eb2
AM
9220 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9221 filename_ptr, NULL, line_ptr, NULL,
9defd221 9222 dwarf_debug_sections,
fb167eb2 9223 &elf_tdata (abfd)->dwarf2_find_line_info);
5420f73d
L
9224}
9225
4ab527b0
FF
9226/* After a call to bfd_find_nearest_line, successive calls to
9227 bfd_find_inliner_info can be used to get source information about
9228 each level of function inlining that terminated at the address
9229 passed to bfd_find_nearest_line. Currently this is only supported
9230 for DWARF2 with appropriate DWARF3 extensions. */
9231
9232bfd_boolean
9233_bfd_elf_find_inliner_info (bfd *abfd,
9234 const char **filename_ptr,
9235 const char **functionname_ptr,
9236 unsigned int *line_ptr)
9237{
9238 bfd_boolean found;
9239 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9240 functionname_ptr, line_ptr,
9241 & elf_tdata (abfd)->dwarf2_find_line_info);
9242 return found;
9243}
9244
252b5132 9245int
a6b96beb 9246_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
252b5132 9247{
8ded5a0f
AM
9248 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9249 int ret = bed->s->sizeof_ehdr;
252b5132 9250
0e1862bb 9251 if (!bfd_link_relocatable (info))
8ded5a0f 9252 {
12bd6957 9253 bfd_size_type phdr_size = elf_program_header_size (abfd);
8ded5a0f 9254
62d7a5f6
AM
9255 if (phdr_size == (bfd_size_type) -1)
9256 {
9257 struct elf_segment_map *m;
9258
9259 phdr_size = 0;
12bd6957 9260 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
62d7a5f6 9261 phdr_size += bed->s->sizeof_phdr;
8ded5a0f 9262
62d7a5f6
AM
9263 if (phdr_size == 0)
9264 phdr_size = get_program_header_size (abfd, info);
9265 }
8ded5a0f 9266
12bd6957 9267 elf_program_header_size (abfd) = phdr_size;
8ded5a0f
AM
9268 ret += phdr_size;
9269 }
9270
252b5132
RH
9271 return ret;
9272}
9273
b34976b6 9274bfd_boolean
217aa764
AM
9275_bfd_elf_set_section_contents (bfd *abfd,
9276 sec_ptr section,
0f867abe 9277 const void *location,
217aa764
AM
9278 file_ptr offset,
9279 bfd_size_type count)
252b5132
RH
9280{
9281 Elf_Internal_Shdr *hdr;
1b6aeedb 9282 file_ptr pos;
252b5132
RH
9283
9284 if (! abfd->output_has_begun
217aa764 9285 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
b34976b6 9286 return FALSE;
252b5132 9287
0ce398f1
L
9288 if (!count)
9289 return TRUE;
9290
252b5132 9291 hdr = &elf_section_data (section)->this_hdr;
0ce398f1
L
9292 if (hdr->sh_offset == (file_ptr) -1)
9293 {
a0dcf297
NC
9294 unsigned char *contents;
9295
1ff6de03
NA
9296 if (bfd_section_is_ctf (section))
9297 /* Nothing to do with this section: the contents are generated
9298 later. */
9299 return TRUE;
9300
a0dcf297
NC
9301 if ((section->flags & SEC_ELF_COMPRESS) == 0)
9302 {
9303 _bfd_error_handler
9304 (_("%pB:%pA: error: attempting to write into an unallocated compressed section"),
9305 abfd, section);
9306 bfd_set_error (bfd_error_invalid_operation);
9307 return FALSE;
9308 }
9309
9310 if ((offset + count) > hdr->sh_size)
9311 {
9312 _bfd_error_handler
9313 (_("%pB:%pA: error: attempting to write over the end of the section"),
9314 abfd, section);
9315
9316 bfd_set_error (bfd_error_invalid_operation);
9317 return FALSE;
9318 }
9319
9320 contents = hdr->contents;
9321 if (contents == NULL)
9322 {
9323 _bfd_error_handler
9324 (_("%pB:%pA: error: attempting to write section into an empty buffer"),
9325 abfd, section);
9326
9327 bfd_set_error (bfd_error_invalid_operation);
9328 return FALSE;
9329 }
9330
0ce398f1
L
9331 memcpy (contents + offset, location, count);
9332 return TRUE;
9333 }
a0dcf297 9334
dc810e39
AM
9335 pos = hdr->sh_offset + offset;
9336 if (bfd_seek (abfd, pos, SEEK_SET) != 0
9337 || bfd_bwrite (location, count, abfd) != count)
b34976b6 9338 return FALSE;
252b5132 9339
b34976b6 9340 return TRUE;
252b5132
RH
9341}
9342
f3185997 9343bfd_boolean
217aa764
AM
9344_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
9345 arelent *cache_ptr ATTRIBUTE_UNUSED,
9346 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
252b5132
RH
9347{
9348 abort ();
f3185997 9349 return FALSE;
252b5132
RH
9350}
9351
252b5132
RH
9352/* Try to convert a non-ELF reloc into an ELF one. */
9353
b34976b6 9354bfd_boolean
217aa764 9355_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
252b5132 9356{
c044fabd 9357 /* Check whether we really have an ELF howto. */
252b5132
RH
9358
9359 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
9360 {
9361 bfd_reloc_code_real_type code;
9362 reloc_howto_type *howto;
9363
9364 /* Alien reloc: Try to determine its type to replace it with an
c044fabd 9365 equivalent ELF reloc. */
252b5132
RH
9366
9367 if (areloc->howto->pc_relative)
9368 {
9369 switch (areloc->howto->bitsize)
9370 {
9371 case 8:
9372 code = BFD_RELOC_8_PCREL;
9373 break;
9374 case 12:
9375 code = BFD_RELOC_12_PCREL;
9376 break;
9377 case 16:
9378 code = BFD_RELOC_16_PCREL;
9379 break;
9380 case 24:
9381 code = BFD_RELOC_24_PCREL;
9382 break;
9383 case 32:
9384 code = BFD_RELOC_32_PCREL;
9385 break;
9386 case 64:
9387 code = BFD_RELOC_64_PCREL;
9388 break;
9389 default:
9390 goto fail;
9391 }
9392
9393 howto = bfd_reloc_type_lookup (abfd, code);
9394
94698d01 9395 if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
252b5132
RH
9396 {
9397 if (howto->pcrel_offset)
9398 areloc->addend += areloc->address;
9399 else
9400 areloc->addend -= areloc->address; /* addend is unsigned!! */
9401 }
9402 }
9403 else
9404 {
9405 switch (areloc->howto->bitsize)
9406 {
9407 case 8:
9408 code = BFD_RELOC_8;
9409 break;
9410 case 14:
9411 code = BFD_RELOC_14;
9412 break;
9413 case 16:
9414 code = BFD_RELOC_16;
9415 break;
9416 case 26:
9417 code = BFD_RELOC_26;
9418 break;
9419 case 32:
9420 code = BFD_RELOC_32;
9421 break;
9422 case 64:
9423 code = BFD_RELOC_64;
9424 break;
9425 default:
9426 goto fail;
9427 }
9428
9429 howto = bfd_reloc_type_lookup (abfd, code);
9430 }
9431
9432 if (howto)
9433 areloc->howto = howto;
9434 else
9435 goto fail;
9436 }
9437
b34976b6 9438 return TRUE;
252b5132
RH
9439
9440 fail:
0aa13fee
AM
9441 /* xgettext:c-format */
9442 _bfd_error_handler (_("%pB: %s unsupported"),
9443 abfd, areloc->howto->name);
9aea1e31 9444 bfd_set_error (bfd_error_sorry);
b34976b6 9445 return FALSE;
252b5132
RH
9446}
9447
b34976b6 9448bfd_boolean
217aa764 9449_bfd_elf_close_and_cleanup (bfd *abfd)
252b5132 9450{
d9071b0c 9451 struct elf_obj_tdata *tdata = elf_tdata (abfd);
0ed18fa1
AM
9452 if (tdata != NULL
9453 && (bfd_get_format (abfd) == bfd_object
9454 || bfd_get_format (abfd) == bfd_core))
252b5132 9455 {
c0355132 9456 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
2b0f7ef9 9457 _bfd_elf_strtab_free (elf_shstrtab (abfd));
d9071b0c 9458 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
252b5132
RH
9459 }
9460
9461 return _bfd_generic_close_and_cleanup (abfd);
9462}
9463
9464/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9465 in the relocation's offset. Thus we cannot allow any sort of sanity
9466 range-checking to interfere. There is nothing else to do in processing
9467 this reloc. */
9468
9469bfd_reloc_status_type
217aa764
AM
9470_bfd_elf_rel_vtable_reloc_fn
9471 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
fc0a2244 9472 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
217aa764
AM
9473 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
9474 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
252b5132
RH
9475{
9476 return bfd_reloc_ok;
9477}
252b5132
RH
9478\f
9479/* Elf core file support. Much of this only works on native
9480 toolchains, since we rely on knowing the
9481 machine-dependent procfs structure in order to pick
c044fabd 9482 out details about the corefile. */
252b5132
RH
9483
9484#ifdef HAVE_SYS_PROCFS_H
9485# include <sys/procfs.h>
9486#endif
9487
261b8d08
PA
9488/* Return a PID that identifies a "thread" for threaded cores, or the
9489 PID of the main process for non-threaded cores. */
252b5132
RH
9490
9491static int
217aa764 9492elfcore_make_pid (bfd *abfd)
252b5132 9493{
261b8d08
PA
9494 int pid;
9495
228e534f 9496 pid = elf_tdata (abfd)->core->lwpid;
261b8d08 9497 if (pid == 0)
228e534f 9498 pid = elf_tdata (abfd)->core->pid;
261b8d08
PA
9499
9500 return pid;
252b5132
RH
9501}
9502
252b5132
RH
9503/* If there isn't a section called NAME, make one, using
9504 data from SECT. Note, this function will generate a
9505 reference to NAME, so you shouldn't deallocate or
c044fabd 9506 overwrite it. */
252b5132 9507
b34976b6 9508static bfd_boolean
217aa764 9509elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
252b5132 9510{
c044fabd 9511 asection *sect2;
252b5132
RH
9512
9513 if (bfd_get_section_by_name (abfd, name) != NULL)
b34976b6 9514 return TRUE;
252b5132 9515
117ed4f8 9516 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
252b5132 9517 if (sect2 == NULL)
b34976b6 9518 return FALSE;
252b5132 9519
eea6121a 9520 sect2->size = sect->size;
252b5132 9521 sect2->filepos = sect->filepos;
252b5132 9522 sect2->alignment_power = sect->alignment_power;
b34976b6 9523 return TRUE;
252b5132
RH
9524}
9525
bb0082d6
AM
9526/* Create a pseudosection containing SIZE bytes at FILEPOS. This
9527 actually creates up to two pseudosections:
9528 - For the single-threaded case, a section named NAME, unless
9529 such a section already exists.
9530 - For the multi-threaded case, a section named "NAME/PID", where
9531 PID is elfcore_make_pid (abfd).
24d3e51b 9532 Both pseudosections have identical contents. */
b34976b6 9533bfd_boolean
217aa764
AM
9534_bfd_elfcore_make_pseudosection (bfd *abfd,
9535 char *name,
9536 size_t size,
9537 ufile_ptr filepos)
bb0082d6
AM
9538{
9539 char buf[100];
9540 char *threaded_name;
d4c88bbb 9541 size_t len;
bb0082d6
AM
9542 asection *sect;
9543
9544 /* Build the section name. */
9545
9546 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
d4c88bbb 9547 len = strlen (buf) + 1;
a50b1753 9548 threaded_name = (char *) bfd_alloc (abfd, len);
bb0082d6 9549 if (threaded_name == NULL)
b34976b6 9550 return FALSE;
d4c88bbb 9551 memcpy (threaded_name, buf, len);
bb0082d6 9552
117ed4f8
AM
9553 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
9554 SEC_HAS_CONTENTS);
bb0082d6 9555 if (sect == NULL)
b34976b6 9556 return FALSE;
eea6121a 9557 sect->size = size;
bb0082d6 9558 sect->filepos = filepos;
bb0082d6
AM
9559 sect->alignment_power = 2;
9560
936e320b 9561 return elfcore_maybe_make_sect (abfd, name, sect);
bb0082d6
AM
9562}
9563
58e07198
CZ
9564static bfd_boolean
9565elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
9566 size_t offs)
9567{
9568 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9569 SEC_HAS_CONTENTS);
9570
9571 if (sect == NULL)
9572 return FALSE;
9573
9574 sect->size = note->descsz - offs;
9575 sect->filepos = note->descpos + offs;
9576 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9577
9578 return TRUE;
9579}
9580
252b5132 9581/* prstatus_t exists on:
4a938328 9582 solaris 2.5+
252b5132
RH
9583 linux 2.[01] + glibc
9584 unixware 4.2
9585*/
9586
9587#if defined (HAVE_PRSTATUS_T)
a7b97311 9588
b34976b6 9589static bfd_boolean
217aa764 9590elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 9591{
eea6121a 9592 size_t size;
7ee38065 9593 int offset;
252b5132 9594
4a938328
MS
9595 if (note->descsz == sizeof (prstatus_t))
9596 {
9597 prstatus_t prstat;
252b5132 9598
eea6121a 9599 size = sizeof (prstat.pr_reg);
7ee38065 9600 offset = offsetof (prstatus_t, pr_reg);
4a938328 9601 memcpy (&prstat, note->descdata, sizeof (prstat));
252b5132 9602
fa49d224
NC
9603 /* Do not overwrite the core signal if it
9604 has already been set by another thread. */
228e534f
AM
9605 if (elf_tdata (abfd)->core->signal == 0)
9606 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9607 if (elf_tdata (abfd)->core->pid == 0)
9608 elf_tdata (abfd)->core->pid = prstat.pr_pid;
252b5132 9609
4a938328
MS
9610 /* pr_who exists on:
9611 solaris 2.5+
9612 unixware 4.2
9613 pr_who doesn't exist on:
9614 linux 2.[01]
9615 */
252b5132 9616#if defined (HAVE_PRSTATUS_T_PR_WHO)
228e534f 9617 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
261b8d08 9618#else
228e534f 9619 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
252b5132 9620#endif
4a938328 9621 }
7ee38065 9622#if defined (HAVE_PRSTATUS32_T)
4a938328
MS
9623 else if (note->descsz == sizeof (prstatus32_t))
9624 {
9625 /* 64-bit host, 32-bit corefile */
9626 prstatus32_t prstat;
9627
eea6121a 9628 size = sizeof (prstat.pr_reg);
7ee38065 9629 offset = offsetof (prstatus32_t, pr_reg);
4a938328
MS
9630 memcpy (&prstat, note->descdata, sizeof (prstat));
9631
fa49d224
NC
9632 /* Do not overwrite the core signal if it
9633 has already been set by another thread. */
228e534f
AM
9634 if (elf_tdata (abfd)->core->signal == 0)
9635 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9636 if (elf_tdata (abfd)->core->pid == 0)
9637 elf_tdata (abfd)->core->pid = prstat.pr_pid;
4a938328
MS
9638
9639 /* pr_who exists on:
9640 solaris 2.5+
9641 unixware 4.2
9642 pr_who doesn't exist on:
9643 linux 2.[01]
9644 */
7ee38065 9645#if defined (HAVE_PRSTATUS32_T_PR_WHO)
228e534f 9646 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
261b8d08 9647#else
228e534f 9648 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
4a938328
MS
9649#endif
9650 }
7ee38065 9651#endif /* HAVE_PRSTATUS32_T */
4a938328
MS
9652 else
9653 {
9654 /* Fail - we don't know how to handle any other
9655 note size (ie. data object type). */
b34976b6 9656 return TRUE;
4a938328 9657 }
252b5132 9658
bb0082d6 9659 /* Make a ".reg/999" section and a ".reg" section. */
936e320b 9660 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 9661 size, note->descpos + offset);
252b5132
RH
9662}
9663#endif /* defined (HAVE_PRSTATUS_T) */
9664
bb0082d6 9665/* Create a pseudosection containing the exact contents of NOTE. */
b34976b6 9666static bfd_boolean
217aa764
AM
9667elfcore_make_note_pseudosection (bfd *abfd,
9668 char *name,
9669 Elf_Internal_Note *note)
252b5132 9670{
936e320b
AM
9671 return _bfd_elfcore_make_pseudosection (abfd, name,
9672 note->descsz, note->descpos);
252b5132
RH
9673}
9674
ff08c6bb
JB
9675/* There isn't a consistent prfpregset_t across platforms,
9676 but it doesn't matter, because we don't have to pick this
c044fabd
KH
9677 data structure apart. */
9678
b34976b6 9679static bfd_boolean
217aa764 9680elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
9681{
9682 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9683}
9684
ff08c6bb 9685/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
971d4640 9686 type of NT_PRXFPREG. Just include the whole note's contents
ff08c6bb 9687 literally. */
c044fabd 9688
b34976b6 9689static bfd_boolean
217aa764 9690elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
9691{
9692 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9693}
9694
4339cae0
L
9695/* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9696 with a note type of NT_X86_XSTATE. Just include the whole note's
9697 contents literally. */
9698
9699static bfd_boolean
9700elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
9701{
9702 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
9703}
9704
97753bd5
AM
9705static bfd_boolean
9706elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
9707{
9708 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
9709}
9710
89eeb0bc
LM
9711static bfd_boolean
9712elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
9713{
9714 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
9715}
97753bd5 9716
cb2366c1
EBM
9717static bfd_boolean
9718elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
9719{
9720 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
9721}
9722
9723static bfd_boolean
9724elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
9725{
9726 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
9727}
9728
9729static bfd_boolean
9730elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
9731{
9732 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
9733}
9734
9735static bfd_boolean
9736elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
9737{
9738 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
9739}
9740
9741static bfd_boolean
9742elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
9743{
9744 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
9745}
9746
9747static bfd_boolean
9748elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
9749{
9750 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
9751}
9752
9753static bfd_boolean
9754elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
9755{
9756 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
9757}
9758
9759static bfd_boolean
9760elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
9761{
9762 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
9763}
9764
9765static bfd_boolean
9766elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
9767{
9768 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
9769}
9770
9771static bfd_boolean
9772elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
9773{
9774 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
9775}
9776
9777static bfd_boolean
9778elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
9779{
9780 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
9781}
9782
9783static bfd_boolean
9784elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
9785{
9786 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
9787}
9788
9789static bfd_boolean
9790elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
9791{
9792 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
9793}
9794
0675e188
UW
9795static bfd_boolean
9796elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
9797{
9798 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
9799}
9800
d7eeb400
MS
9801static bfd_boolean
9802elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
9803{
9804 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
9805}
9806
9807static bfd_boolean
9808elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
9809{
9810 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
9811}
9812
9813static bfd_boolean
9814elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
9815{
9816 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
9817}
9818
9819static bfd_boolean
9820elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
9821{
9822 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
9823}
9824
9825static bfd_boolean
9826elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
9827{
9828 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
9829}
9830
355b81d9
UW
9831static bfd_boolean
9832elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
9833{
9834 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
9835}
9836
9837static bfd_boolean
9838elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
9839{
9840 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
9841}
9842
abb3f6cc
NC
9843static bfd_boolean
9844elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
9845{
9846 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
9847}
9848
4ef9f41a
AA
9849static bfd_boolean
9850elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
9851{
9852 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
9853}
9854
9855static bfd_boolean
9856elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
9857{
9858 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
9859}
9860
88ab90e8
AA
9861static bfd_boolean
9862elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
9863{
9864 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
9865}
9866
9867static bfd_boolean
9868elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
9869{
9870 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
9871}
9872
faa9a424
UW
9873static bfd_boolean
9874elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
9875{
9876 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
9877}
9878
652451f8
YZ
9879static bfd_boolean
9880elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
9881{
9882 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
9883}
9884
9885static bfd_boolean
9886elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
9887{
9888 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
9889}
9890
9891static bfd_boolean
9892elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
9893{
9894 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
9895}
9896
ad1cc4e4
AH
9897static bfd_boolean
9898elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
9899{
9900 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
9901}
9902
e6c3b5bf
AH
9903static bfd_boolean
9904elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
9905{
9906 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
9907}
9908
27456742
AK
9909static bfd_boolean
9910elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
9911{
9912 return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
9913}
9914
252b5132 9915#if defined (HAVE_PRPSINFO_T)
4a938328 9916typedef prpsinfo_t elfcore_psinfo_t;
7ee38065 9917#if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
9918typedef prpsinfo32_t elfcore_psinfo32_t;
9919#endif
252b5132
RH
9920#endif
9921
9922#if defined (HAVE_PSINFO_T)
4a938328 9923typedef psinfo_t elfcore_psinfo_t;
7ee38065 9924#if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
9925typedef psinfo32_t elfcore_psinfo32_t;
9926#endif
252b5132
RH
9927#endif
9928
252b5132
RH
9929/* return a malloc'ed copy of a string at START which is at
9930 most MAX bytes long, possibly without a terminating '\0'.
c044fabd 9931 the copy will always have a terminating '\0'. */
252b5132 9932
936e320b 9933char *
217aa764 9934_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
252b5132 9935{
dc810e39 9936 char *dups;
a50b1753 9937 char *end = (char *) memchr (start, '\0', max);
dc810e39 9938 size_t len;
252b5132
RH
9939
9940 if (end == NULL)
9941 len = max;
9942 else
9943 len = end - start;
9944
a50b1753 9945 dups = (char *) bfd_alloc (abfd, len + 1);
dc810e39 9946 if (dups == NULL)
252b5132
RH
9947 return NULL;
9948
dc810e39
AM
9949 memcpy (dups, start, len);
9950 dups[len] = '\0';
252b5132 9951
dc810e39 9952 return dups;
252b5132
RH
9953}
9954
bb0082d6 9955#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
b34976b6 9956static bfd_boolean
217aa764 9957elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
252b5132 9958{
4a938328
MS
9959 if (note->descsz == sizeof (elfcore_psinfo_t))
9960 {
9961 elfcore_psinfo_t psinfo;
252b5132 9962
7ee38065 9963 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 9964
335e41d4 9965#if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
228e534f 9966 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
335e41d4 9967#endif
228e534f 9968 elf_tdata (abfd)->core->program
936e320b
AM
9969 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9970 sizeof (psinfo.pr_fname));
252b5132 9971
228e534f 9972 elf_tdata (abfd)->core->command
936e320b
AM
9973 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9974 sizeof (psinfo.pr_psargs));
4a938328 9975 }
7ee38065 9976#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
4a938328
MS
9977 else if (note->descsz == sizeof (elfcore_psinfo32_t))
9978 {
9979 /* 64-bit host, 32-bit corefile */
9980 elfcore_psinfo32_t psinfo;
9981
7ee38065 9982 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 9983
335e41d4 9984#if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
228e534f 9985 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
335e41d4 9986#endif
228e534f 9987 elf_tdata (abfd)->core->program
936e320b
AM
9988 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9989 sizeof (psinfo.pr_fname));
4a938328 9990
228e534f 9991 elf_tdata (abfd)->core->command
936e320b
AM
9992 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9993 sizeof (psinfo.pr_psargs));
4a938328
MS
9994 }
9995#endif
9996
9997 else
9998 {
9999 /* Fail - we don't know how to handle any other
10000 note size (ie. data object type). */
b34976b6 10001 return TRUE;
4a938328 10002 }
252b5132
RH
10003
10004 /* Note that for some reason, a spurious space is tacked
10005 onto the end of the args in some (at least one anyway)
c044fabd 10006 implementations, so strip it off if it exists. */
252b5132
RH
10007
10008 {
228e534f 10009 char *command = elf_tdata (abfd)->core->command;
252b5132
RH
10010 int n = strlen (command);
10011
10012 if (0 < n && command[n - 1] == ' ')
10013 command[n - 1] = '\0';
10014 }
10015
b34976b6 10016 return TRUE;
252b5132
RH
10017}
10018#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10019
252b5132 10020#if defined (HAVE_PSTATUS_T)
b34976b6 10021static bfd_boolean
217aa764 10022elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 10023{
f572a39d
AM
10024 if (note->descsz == sizeof (pstatus_t)
10025#if defined (HAVE_PXSTATUS_T)
10026 || note->descsz == sizeof (pxstatus_t)
10027#endif
10028 )
4a938328
MS
10029 {
10030 pstatus_t pstat;
252b5132 10031
4a938328 10032 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 10033
228e534f 10034 elf_tdata (abfd)->core->pid = pstat.pr_pid;
4a938328 10035 }
7ee38065 10036#if defined (HAVE_PSTATUS32_T)
4a938328
MS
10037 else if (note->descsz == sizeof (pstatus32_t))
10038 {
10039 /* 64-bit host, 32-bit corefile */
10040 pstatus32_t pstat;
252b5132 10041
4a938328 10042 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 10043
228e534f 10044 elf_tdata (abfd)->core->pid = pstat.pr_pid;
4a938328
MS
10045 }
10046#endif
252b5132
RH
10047 /* Could grab some more details from the "representative"
10048 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
c044fabd 10049 NT_LWPSTATUS note, presumably. */
252b5132 10050
b34976b6 10051 return TRUE;
252b5132
RH
10052}
10053#endif /* defined (HAVE_PSTATUS_T) */
10054
252b5132 10055#if defined (HAVE_LWPSTATUS_T)
b34976b6 10056static bfd_boolean
217aa764 10057elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132
RH
10058{
10059 lwpstatus_t lwpstat;
10060 char buf[100];
c044fabd 10061 char *name;
d4c88bbb 10062 size_t len;
c044fabd 10063 asection *sect;
252b5132 10064
f572a39d
AM
10065 if (note->descsz != sizeof (lwpstat)
10066#if defined (HAVE_LWPXSTATUS_T)
10067 && note->descsz != sizeof (lwpxstatus_t)
10068#endif
10069 )
b34976b6 10070 return TRUE;
252b5132
RH
10071
10072 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10073
228e534f 10074 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
a1504221
JB
10075 /* Do not overwrite the core signal if it has already been set by
10076 another thread. */
228e534f
AM
10077 if (elf_tdata (abfd)->core->signal == 0)
10078 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
252b5132 10079
c044fabd 10080 /* Make a ".reg/999" section. */
252b5132
RH
10081
10082 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
d4c88bbb 10083 len = strlen (buf) + 1;
217aa764 10084 name = bfd_alloc (abfd, len);
252b5132 10085 if (name == NULL)
b34976b6 10086 return FALSE;
d4c88bbb 10087 memcpy (name, buf, len);
252b5132 10088
117ed4f8 10089 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 10090 if (sect == NULL)
b34976b6 10091 return FALSE;
252b5132
RH
10092
10093#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 10094 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
252b5132
RH
10095 sect->filepos = note->descpos
10096 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10097#endif
10098
10099#if defined (HAVE_LWPSTATUS_T_PR_REG)
eea6121a 10100 sect->size = sizeof (lwpstat.pr_reg);
252b5132
RH
10101 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10102#endif
10103
252b5132
RH
10104 sect->alignment_power = 2;
10105
10106 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
b34976b6 10107 return FALSE;
252b5132
RH
10108
10109 /* Make a ".reg2/999" section */
10110
10111 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
d4c88bbb 10112 len = strlen (buf) + 1;
217aa764 10113 name = bfd_alloc (abfd, len);
252b5132 10114 if (name == NULL)
b34976b6 10115 return FALSE;
d4c88bbb 10116 memcpy (name, buf, len);
252b5132 10117
117ed4f8 10118 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 10119 if (sect == NULL)
b34976b6 10120 return FALSE;
252b5132
RH
10121
10122#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 10123 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
252b5132
RH
10124 sect->filepos = note->descpos
10125 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
10126#endif
10127
10128#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
eea6121a 10129 sect->size = sizeof (lwpstat.pr_fpreg);
252b5132
RH
10130 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
10131#endif
10132
252b5132
RH
10133 sect->alignment_power = 2;
10134
936e320b 10135 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
252b5132
RH
10136}
10137#endif /* defined (HAVE_LWPSTATUS_T) */
10138
8fbac78b
JT
10139/* These constants, and the structure offsets used below, are defined by
10140 Cygwin's core_dump.h */
10141#define NOTE_INFO_PROCESS 1
10142#define NOTE_INFO_THREAD 2
10143#define NOTE_INFO_MODULE 3
d61f3d03 10144#define NOTE_INFO_MODULE64 4
8fbac78b 10145
b34976b6 10146static bfd_boolean
217aa764 10147elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
16e9c715
NC
10148{
10149 char buf[30];
c044fabd 10150 char *name;
d4c88bbb 10151 size_t len;
3cdad084 10152 unsigned int name_size;
c044fabd 10153 asection *sect;
2fef9373 10154 unsigned int type;
4a6636fb
PA
10155 int is_active_thread;
10156 bfd_vma base_addr;
16e9c715 10157
04ec0fa2 10158 if (note->descsz < 4)
b34976b6 10159 return TRUE;
16e9c715 10160
4a6636fb
PA
10161 if (! CONST_STRNEQ (note->namedata, "win32"))
10162 return TRUE;
10163
10164 type = bfd_get_32 (abfd, note->descdata);
c044fabd 10165
7e0d77ef
NC
10166 struct
10167 {
404ec933
JT
10168 const char *type_name;
10169 unsigned long min_size;
10170 } size_check[] =
10171 {
10172 { "NOTE_INFO_PROCESS", 12 },
10173 { "NOTE_INFO_THREAD", 12 },
10174 { "NOTE_INFO_MODULE", 12 },
10175 { "NOTE_INFO_MODULE64", 16 },
10176 };
10177
7e0d77ef 10178 if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
404ec933
JT
10179 return TRUE;
10180
10181 if (note->descsz < size_check[type - 1].min_size)
10182 {
10183 _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes is too small"),
10184 abfd, size_check[type - 1].type_name, note->descsz);
10185 return TRUE;
10186 }
10187
4a6636fb 10188 switch (type)
16e9c715 10189 {
8fbac78b 10190 case NOTE_INFO_PROCESS:
228e534f 10191 /* FIXME: need to add ->core->command. */
ff2084b9 10192 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
ff2084b9 10193 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
c044fabd 10194 break;
16e9c715 10195
8fbac78b 10196 case NOTE_INFO_THREAD:
ff2084b9
JT
10197 /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
10198 structure. */
4a6636fb 10199 /* thread_info.tid */
ff2084b9 10200 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
c044fabd 10201
d4c88bbb 10202 len = strlen (buf) + 1;
a50b1753 10203 name = (char *) bfd_alloc (abfd, len);
16e9c715 10204 if (name == NULL)
b34976b6 10205 return FALSE;
c044fabd 10206
d4c88bbb 10207 memcpy (name, buf, len);
16e9c715 10208
117ed4f8 10209 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
16e9c715 10210 if (sect == NULL)
b34976b6 10211 return FALSE;
c044fabd 10212
4a6636fb 10213 /* sizeof (thread_info.thread_context) */
03c29a6f 10214 sect->size = note->descsz - 12;
4a6636fb
PA
10215 /* offsetof (thread_info.thread_context) */
10216 sect->filepos = note->descpos + 12;
16e9c715
NC
10217 sect->alignment_power = 2;
10218
4a6636fb
PA
10219 /* thread_info.is_active_thread */
10220 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
10221
10222 if (is_active_thread)
16e9c715 10223 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
b34976b6 10224 return FALSE;
16e9c715
NC
10225 break;
10226
8fbac78b 10227 case NOTE_INFO_MODULE:
d61f3d03 10228 case NOTE_INFO_MODULE64:
16e9c715 10229 /* Make a ".module/xxxxxxxx" section. */
d61f3d03
JT
10230 if (type == NOTE_INFO_MODULE)
10231 {
d61f3d03
JT
10232 /* module_info.base_address */
10233 base_addr = bfd_get_32 (abfd, note->descdata + 4);
10234 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
10235 /* module_info.module_name_size */
10236 name_size = bfd_get_32 (abfd, note->descdata + 8);
10237 }
10238 else /* NOTE_INFO_MODULE64 */
10239 {
d61f3d03
JT
10240 /* module_info.base_address */
10241 base_addr = bfd_get_64 (abfd, note->descdata + 4);
10242 sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
10243 /* module_info.module_name_size */
10244 name_size = bfd_get_32 (abfd, note->descdata + 12);
10245 }
c044fabd 10246
d4c88bbb 10247 len = strlen (buf) + 1;
a50b1753 10248 name = (char *) bfd_alloc (abfd, len);
16e9c715 10249 if (name == NULL)
b34976b6 10250 return FALSE;
c044fabd 10251
d4c88bbb 10252 memcpy (name, buf, len);
252b5132 10253
117ed4f8 10254 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
c044fabd 10255
16e9c715 10256 if (sect == NULL)
b34976b6 10257 return FALSE;
c044fabd 10258
04ec0fa2 10259 if (note->descsz < 12 + name_size)
404ec933 10260 {
3cdad084 10261 _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu is too small to contain a name of size %u"),
404ec933
JT
10262 abfd, note->descsz, name_size);
10263 return TRUE;
10264 }
04ec0fa2 10265
eea6121a 10266 sect->size = note->descsz;
16e9c715 10267 sect->filepos = note->descpos;
16e9c715
NC
10268 sect->alignment_power = 2;
10269 break;
10270
10271 default:
b34976b6 10272 return TRUE;
16e9c715
NC
10273 }
10274
b34976b6 10275 return TRUE;
16e9c715 10276}
252b5132 10277
b34976b6 10278static bfd_boolean
217aa764 10279elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
252b5132 10280{
9c5bfbb7 10281 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
bb0082d6 10282
252b5132
RH
10283 switch (note->type)
10284 {
10285 default:
b34976b6 10286 return TRUE;
252b5132 10287
252b5132 10288 case NT_PRSTATUS:
bb0082d6
AM
10289 if (bed->elf_backend_grok_prstatus)
10290 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
b34976b6 10291 return TRUE;
bb0082d6 10292#if defined (HAVE_PRSTATUS_T)
252b5132 10293 return elfcore_grok_prstatus (abfd, note);
bb0082d6 10294#else
b34976b6 10295 return TRUE;
252b5132
RH
10296#endif
10297
10298#if defined (HAVE_PSTATUS_T)
10299 case NT_PSTATUS:
10300 return elfcore_grok_pstatus (abfd, note);
10301#endif
10302
10303#if defined (HAVE_LWPSTATUS_T)
10304 case NT_LWPSTATUS:
10305 return elfcore_grok_lwpstatus (abfd, note);
10306#endif
10307
10308 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
10309 return elfcore_grok_prfpreg (abfd, note);
10310
c044fabd 10311 case NT_WIN32PSTATUS:
16e9c715 10312 return elfcore_grok_win32pstatus (abfd, note);
16e9c715 10313
c044fabd 10314 case NT_PRXFPREG: /* Linux SSE extension */
e377ab71
MK
10315 if (note->namesz == 6
10316 && strcmp (note->namedata, "LINUX") == 0)
ff08c6bb
JB
10317 return elfcore_grok_prxfpreg (abfd, note);
10318 else
b34976b6 10319 return TRUE;
ff08c6bb 10320
4339cae0
L
10321 case NT_X86_XSTATE: /* Linux XSAVE extension */
10322 if (note->namesz == 6
10323 && strcmp (note->namedata, "LINUX") == 0)
10324 return elfcore_grok_xstatereg (abfd, note);
10325 else
10326 return TRUE;
10327
97753bd5
AM
10328 case NT_PPC_VMX:
10329 if (note->namesz == 6
10330 && strcmp (note->namedata, "LINUX") == 0)
10331 return elfcore_grok_ppc_vmx (abfd, note);
10332 else
10333 return TRUE;
10334
89eeb0bc
LM
10335 case NT_PPC_VSX:
10336 if (note->namesz == 6
07d6d2b8
AM
10337 && strcmp (note->namedata, "LINUX") == 0)
10338 return elfcore_grok_ppc_vsx (abfd, note);
89eeb0bc 10339 else
07d6d2b8 10340 return TRUE;
89eeb0bc 10341
cb2366c1
EBM
10342 case NT_PPC_TAR:
10343 if (note->namesz == 6
4b24dd1a
AM
10344 && strcmp (note->namedata, "LINUX") == 0)
10345 return elfcore_grok_ppc_tar (abfd, note);
cb2366c1 10346 else
4b24dd1a 10347 return TRUE;
cb2366c1
EBM
10348
10349 case NT_PPC_PPR:
10350 if (note->namesz == 6
4b24dd1a
AM
10351 && strcmp (note->namedata, "LINUX") == 0)
10352 return elfcore_grok_ppc_ppr (abfd, note);
cb2366c1 10353 else
4b24dd1a 10354 return TRUE;
cb2366c1
EBM
10355
10356 case NT_PPC_DSCR:
10357 if (note->namesz == 6
4b24dd1a
AM
10358 && strcmp (note->namedata, "LINUX") == 0)
10359 return elfcore_grok_ppc_dscr (abfd, note);
cb2366c1 10360 else
4b24dd1a 10361 return TRUE;
cb2366c1
EBM
10362
10363 case NT_PPC_EBB:
10364 if (note->namesz == 6
4b24dd1a
AM
10365 && strcmp (note->namedata, "LINUX") == 0)
10366 return elfcore_grok_ppc_ebb (abfd, note);
cb2366c1 10367 else
4b24dd1a 10368 return TRUE;
cb2366c1
EBM
10369
10370 case NT_PPC_PMU:
10371 if (note->namesz == 6
4b24dd1a
AM
10372 && strcmp (note->namedata, "LINUX") == 0)
10373 return elfcore_grok_ppc_pmu (abfd, note);
cb2366c1 10374 else
4b24dd1a 10375 return TRUE;
cb2366c1
EBM
10376
10377 case NT_PPC_TM_CGPR:
10378 if (note->namesz == 6
4b24dd1a
AM
10379 && strcmp (note->namedata, "LINUX") == 0)
10380 return elfcore_grok_ppc_tm_cgpr (abfd, note);
cb2366c1 10381 else
4b24dd1a 10382 return TRUE;
cb2366c1
EBM
10383
10384 case NT_PPC_TM_CFPR:
10385 if (note->namesz == 6
4b24dd1a
AM
10386 && strcmp (note->namedata, "LINUX") == 0)
10387 return elfcore_grok_ppc_tm_cfpr (abfd, note);
cb2366c1 10388 else
4b24dd1a 10389 return TRUE;
cb2366c1
EBM
10390
10391 case NT_PPC_TM_CVMX:
10392 if (note->namesz == 6
4b24dd1a
AM
10393 && strcmp (note->namedata, "LINUX") == 0)
10394 return elfcore_grok_ppc_tm_cvmx (abfd, note);
cb2366c1 10395 else
4b24dd1a 10396 return TRUE;
cb2366c1
EBM
10397
10398 case NT_PPC_TM_CVSX:
10399 if (note->namesz == 6
4b24dd1a
AM
10400 && strcmp (note->namedata, "LINUX") == 0)
10401 return elfcore_grok_ppc_tm_cvsx (abfd, note);
cb2366c1 10402 else
4b24dd1a 10403 return TRUE;
cb2366c1
EBM
10404
10405 case NT_PPC_TM_SPR:
10406 if (note->namesz == 6
4b24dd1a
AM
10407 && strcmp (note->namedata, "LINUX") == 0)
10408 return elfcore_grok_ppc_tm_spr (abfd, note);
cb2366c1 10409 else
4b24dd1a 10410 return TRUE;
cb2366c1
EBM
10411
10412 case NT_PPC_TM_CTAR:
10413 if (note->namesz == 6
4b24dd1a
AM
10414 && strcmp (note->namedata, "LINUX") == 0)
10415 return elfcore_grok_ppc_tm_ctar (abfd, note);
cb2366c1 10416 else
4b24dd1a 10417 return TRUE;
cb2366c1
EBM
10418
10419 case NT_PPC_TM_CPPR:
10420 if (note->namesz == 6
4b24dd1a
AM
10421 && strcmp (note->namedata, "LINUX") == 0)
10422 return elfcore_grok_ppc_tm_cppr (abfd, note);
cb2366c1 10423 else
4b24dd1a 10424 return TRUE;
cb2366c1
EBM
10425
10426 case NT_PPC_TM_CDSCR:
10427 if (note->namesz == 6
4b24dd1a
AM
10428 && strcmp (note->namedata, "LINUX") == 0)
10429 return elfcore_grok_ppc_tm_cdscr (abfd, note);
cb2366c1 10430 else
4b24dd1a 10431 return TRUE;
cb2366c1 10432
0675e188
UW
10433 case NT_S390_HIGH_GPRS:
10434 if (note->namesz == 6
07d6d2b8
AM
10435 && strcmp (note->namedata, "LINUX") == 0)
10436 return elfcore_grok_s390_high_gprs (abfd, note);
0675e188 10437 else
07d6d2b8 10438 return TRUE;
0675e188 10439
d7eeb400
MS
10440 case NT_S390_TIMER:
10441 if (note->namesz == 6
07d6d2b8
AM
10442 && strcmp (note->namedata, "LINUX") == 0)
10443 return elfcore_grok_s390_timer (abfd, note);
d7eeb400 10444 else
07d6d2b8 10445 return TRUE;
d7eeb400
MS
10446
10447 case NT_S390_TODCMP:
10448 if (note->namesz == 6
07d6d2b8
AM
10449 && strcmp (note->namedata, "LINUX") == 0)
10450 return elfcore_grok_s390_todcmp (abfd, note);
d7eeb400 10451 else
07d6d2b8 10452 return TRUE;
d7eeb400
MS
10453
10454 case NT_S390_TODPREG:
10455 if (note->namesz == 6
07d6d2b8
AM
10456 && strcmp (note->namedata, "LINUX") == 0)
10457 return elfcore_grok_s390_todpreg (abfd, note);
d7eeb400 10458 else
07d6d2b8 10459 return TRUE;
d7eeb400
MS
10460
10461 case NT_S390_CTRS:
10462 if (note->namesz == 6
07d6d2b8
AM
10463 && strcmp (note->namedata, "LINUX") == 0)
10464 return elfcore_grok_s390_ctrs (abfd, note);
d7eeb400 10465 else
07d6d2b8 10466 return TRUE;
d7eeb400
MS
10467
10468 case NT_S390_PREFIX:
10469 if (note->namesz == 6
07d6d2b8
AM
10470 && strcmp (note->namedata, "LINUX") == 0)
10471 return elfcore_grok_s390_prefix (abfd, note);
d7eeb400 10472 else
07d6d2b8 10473 return TRUE;
d7eeb400 10474
355b81d9
UW
10475 case NT_S390_LAST_BREAK:
10476 if (note->namesz == 6
07d6d2b8
AM
10477 && strcmp (note->namedata, "LINUX") == 0)
10478 return elfcore_grok_s390_last_break (abfd, note);
355b81d9 10479 else
07d6d2b8 10480 return TRUE;
355b81d9
UW
10481
10482 case NT_S390_SYSTEM_CALL:
10483 if (note->namesz == 6
07d6d2b8
AM
10484 && strcmp (note->namedata, "LINUX") == 0)
10485 return elfcore_grok_s390_system_call (abfd, note);
355b81d9 10486 else
07d6d2b8 10487 return TRUE;
355b81d9 10488
abb3f6cc
NC
10489 case NT_S390_TDB:
10490 if (note->namesz == 6
07d6d2b8
AM
10491 && strcmp (note->namedata, "LINUX") == 0)
10492 return elfcore_grok_s390_tdb (abfd, note);
abb3f6cc 10493 else
07d6d2b8 10494 return TRUE;
abb3f6cc 10495
4ef9f41a
AA
10496 case NT_S390_VXRS_LOW:
10497 if (note->namesz == 6
10498 && strcmp (note->namedata, "LINUX") == 0)
10499 return elfcore_grok_s390_vxrs_low (abfd, note);
10500 else
10501 return TRUE;
10502
10503 case NT_S390_VXRS_HIGH:
10504 if (note->namesz == 6
10505 && strcmp (note->namedata, "LINUX") == 0)
10506 return elfcore_grok_s390_vxrs_high (abfd, note);
10507 else
10508 return TRUE;
10509
88ab90e8
AA
10510 case NT_S390_GS_CB:
10511 if (note->namesz == 6
10512 && strcmp (note->namedata, "LINUX") == 0)
8fe09d74 10513 return elfcore_grok_s390_gs_cb (abfd, note);
88ab90e8
AA
10514 else
10515 return TRUE;
10516
10517 case NT_S390_GS_BC:
10518 if (note->namesz == 6
10519 && strcmp (note->namedata, "LINUX") == 0)
8fe09d74 10520 return elfcore_grok_s390_gs_bc (abfd, note);
88ab90e8
AA
10521 else
10522 return TRUE;
10523
27456742
AK
10524 case NT_ARC_V2:
10525 if (note->namesz == 6
10526 && strcmp (note->namedata, "LINUX") == 0)
10527 return elfcore_grok_arc_v2 (abfd, note);
10528 else
10529 return TRUE;
10530
faa9a424
UW
10531 case NT_ARM_VFP:
10532 if (note->namesz == 6
10533 && strcmp (note->namedata, "LINUX") == 0)
10534 return elfcore_grok_arm_vfp (abfd, note);
10535 else
10536 return TRUE;
10537
652451f8
YZ
10538 case NT_ARM_TLS:
10539 if (note->namesz == 6
10540 && strcmp (note->namedata, "LINUX") == 0)
10541 return elfcore_grok_aarch_tls (abfd, note);
10542 else
10543 return TRUE;
10544
10545 case NT_ARM_HW_BREAK:
10546 if (note->namesz == 6
10547 && strcmp (note->namedata, "LINUX") == 0)
10548 return elfcore_grok_aarch_hw_break (abfd, note);
10549 else
10550 return TRUE;
10551
10552 case NT_ARM_HW_WATCH:
10553 if (note->namesz == 6
10554 && strcmp (note->namedata, "LINUX") == 0)
10555 return elfcore_grok_aarch_hw_watch (abfd, note);
10556 else
10557 return TRUE;
10558
ad1cc4e4
AH
10559 case NT_ARM_SVE:
10560 if (note->namesz == 6
10561 && strcmp (note->namedata, "LINUX") == 0)
10562 return elfcore_grok_aarch_sve (abfd, note);
10563 else
10564 return TRUE;
10565
e6c3b5bf
AH
10566 case NT_ARM_PAC_MASK:
10567 if (note->namesz == 6
10568 && strcmp (note->namedata, "LINUX") == 0)
10569 return elfcore_grok_aarch_pauth (abfd, note);
10570 else
10571 return TRUE;
10572
252b5132
RH
10573 case NT_PRPSINFO:
10574 case NT_PSINFO:
bb0082d6
AM
10575 if (bed->elf_backend_grok_psinfo)
10576 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
b34976b6 10577 return TRUE;
bb0082d6 10578#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
252b5132 10579 return elfcore_grok_psinfo (abfd, note);
bb0082d6 10580#else
b34976b6 10581 return TRUE;
252b5132 10582#endif
3333a7c3
RM
10583
10584 case NT_AUXV:
58e07198 10585 return elfcore_make_auxv_note_section (abfd, note, 0);
9015683b 10586
451b7c33
TT
10587 case NT_FILE:
10588 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
10589 note);
10590
9015683b
TT
10591 case NT_SIGINFO:
10592 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
10593 note);
5b2c414d 10594
252b5132
RH
10595 }
10596}
10597
718175fa
JK
10598static bfd_boolean
10599elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
10600{
c74f7d1c 10601 struct bfd_build_id* build_id;
30e8ee25
AM
10602
10603 if (note->descsz == 0)
10604 return FALSE;
10605
c74f7d1c
JT
10606 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
10607 if (build_id == NULL)
718175fa
JK
10608 return FALSE;
10609
c74f7d1c
JT
10610 build_id->size = note->descsz;
10611 memcpy (build_id->data, note->descdata, note->descsz);
10612 abfd->build_id = build_id;
718175fa
JK
10613
10614 return TRUE;
10615}
10616
10617static bfd_boolean
10618elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
10619{
10620 switch (note->type)
10621 {
10622 default:
10623 return TRUE;
10624
46bed679
L
10625 case NT_GNU_PROPERTY_TYPE_0:
10626 return _bfd_elf_parse_gnu_properties (abfd, note);
10627
718175fa
JK
10628 case NT_GNU_BUILD_ID:
10629 return elfobj_grok_gnu_build_id (abfd, note);
10630 }
10631}
10632
e21e5835
NC
10633static bfd_boolean
10634elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
10635{
10636 struct sdt_note *cur =
7a6e0d89
AM
10637 (struct sdt_note *) bfd_alloc (abfd,
10638 sizeof (struct sdt_note) + note->descsz);
e21e5835
NC
10639
10640 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
10641 cur->size = (bfd_size_type) note->descsz;
10642 memcpy (cur->data, note->descdata, note->descsz);
10643
10644 elf_tdata (abfd)->sdt_note_head = cur;
10645
10646 return TRUE;
10647}
10648
10649static bfd_boolean
10650elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
10651{
10652 switch (note->type)
10653 {
10654 case NT_STAPSDT:
10655 return elfobj_grok_stapsdt_note_1 (abfd, note);
10656
10657 default:
10658 return TRUE;
10659 }
10660}
10661
aa1ed4a9
JB
10662static bfd_boolean
10663elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
10664{
10665 size_t offset;
10666
b5430a3c 10667 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
aa1ed4a9 10668 {
b5430a3c 10669 case ELFCLASS32:
0064d223
JB
10670 if (note->descsz < 108)
10671 return FALSE;
aa1ed4a9
JB
10672 break;
10673
b5430a3c 10674 case ELFCLASS64:
0064d223
JB
10675 if (note->descsz < 120)
10676 return FALSE;
aa1ed4a9
JB
10677 break;
10678
10679 default:
10680 return FALSE;
10681 }
10682
0064d223
JB
10683 /* Check for version 1 in pr_version. */
10684 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10685 return FALSE;
80a04378 10686
0064d223
JB
10687 offset = 4;
10688
10689 /* Skip over pr_psinfosz. */
b5430a3c 10690 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
0064d223
JB
10691 offset += 4;
10692 else
10693 {
10694 offset += 4; /* Padding before pr_psinfosz. */
10695 offset += 8;
10696 }
10697
aa1ed4a9
JB
10698 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10699 elf_tdata (abfd)->core->program
10700 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
10701 offset += 17;
10702
10703 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10704 elf_tdata (abfd)->core->command
10705 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
0064d223
JB
10706 offset += 81;
10707
10708 /* Padding before pr_pid. */
10709 offset += 2;
10710
10711 /* The pr_pid field was added in version "1a". */
10712 if (note->descsz < offset + 4)
10713 return TRUE;
10714
10715 elf_tdata (abfd)->core->pid
10716 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
aa1ed4a9
JB
10717
10718 return TRUE;
10719}
10720
10721static bfd_boolean
10722elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
10723{
10724 size_t offset;
10725 size_t size;
24d3e51b 10726 size_t min_size;
aa1ed4a9 10727
24d3e51b
NC
10728 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10729 Also compute minimum size of this note. */
b5430a3c 10730 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
aa1ed4a9 10731 {
b5430a3c 10732 case ELFCLASS32:
24d3e51b
NC
10733 offset = 4 + 4;
10734 min_size = offset + (4 * 2) + 4 + 4 + 4;
aa1ed4a9
JB
10735 break;
10736
b5430a3c 10737 case ELFCLASS64:
24d3e51b
NC
10738 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
10739 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
aa1ed4a9
JB
10740 break;
10741
10742 default:
10743 return FALSE;
10744 }
10745
24d3e51b
NC
10746 if (note->descsz < min_size)
10747 return FALSE;
10748
10749 /* Check for version 1 in pr_version. */
10750 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10751 return FALSE;
aa1ed4a9 10752
24d3e51b
NC
10753 /* Extract size of pr_reg from pr_gregsetsz. */
10754 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
b5430a3c 10755 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
24d3e51b
NC
10756 {
10757 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10758 offset += 4 * 2;
10759 }
b5430a3c 10760 else
24d3e51b
NC
10761 {
10762 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
10763 offset += 8 * 2;
10764 }
aa1ed4a9 10765
24d3e51b 10766 /* Skip over pr_osreldate. */
aa1ed4a9
JB
10767 offset += 4;
10768
24d3e51b 10769 /* Read signal from pr_cursig. */
aa1ed4a9
JB
10770 if (elf_tdata (abfd)->core->signal == 0)
10771 elf_tdata (abfd)->core->signal
10772 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10773 offset += 4;
10774
24d3e51b 10775 /* Read TID from pr_pid. */
aa1ed4a9
JB
10776 elf_tdata (abfd)->core->lwpid
10777 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10778 offset += 4;
10779
24d3e51b 10780 /* Padding before pr_reg. */
b5430a3c 10781 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
aa1ed4a9
JB
10782 offset += 4;
10783
24d3e51b
NC
10784 /* Make sure that there is enough data remaining in the note. */
10785 if ((note->descsz - offset) < size)
10786 return FALSE;
10787
aa1ed4a9
JB
10788 /* Make a ".reg/999" section and a ".reg" section. */
10789 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10790 size, note->descpos + offset);
10791}
10792
10793static bfd_boolean
10794elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
10795{
544c67cd
JB
10796 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10797
aa1ed4a9
JB
10798 switch (note->type)
10799 {
10800 case NT_PRSTATUS:
544c67cd
JB
10801 if (bed->elf_backend_grok_freebsd_prstatus)
10802 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
10803 return TRUE;
aa1ed4a9
JB
10804 return elfcore_grok_freebsd_prstatus (abfd, note);
10805
10806 case NT_FPREGSET:
10807 return elfcore_grok_prfpreg (abfd, note);
10808
10809 case NT_PRPSINFO:
10810 return elfcore_grok_freebsd_psinfo (abfd, note);
10811
10812 case NT_FREEBSD_THRMISC:
10813 if (note->namesz == 8)
10814 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
10815 else
10816 return TRUE;
10817
ddb2bbcf
JB
10818 case NT_FREEBSD_PROCSTAT_PROC:
10819 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
10820 note);
10821
10822 case NT_FREEBSD_PROCSTAT_FILES:
10823 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
10824 note);
10825
10826 case NT_FREEBSD_PROCSTAT_VMMAP:
10827 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
10828 note);
10829
3350c5f5 10830 case NT_FREEBSD_PROCSTAT_AUXV:
58e07198 10831 return elfcore_make_auxv_note_section (abfd, note, 4);
3350c5f5 10832
aa1ed4a9
JB
10833 case NT_X86_XSTATE:
10834 if (note->namesz == 8)
10835 return elfcore_grok_xstatereg (abfd, note);
10836 else
10837 return TRUE;
10838
e6f3b9c3
JB
10839 case NT_FREEBSD_PTLWPINFO:
10840 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
10841 note);
10842
6d5be5d6
JB
10843 case NT_ARM_VFP:
10844 return elfcore_grok_arm_vfp (abfd, note);
10845
aa1ed4a9
JB
10846 default:
10847 return TRUE;
10848 }
10849}
10850
b34976b6 10851static bfd_boolean
217aa764 10852elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
50b2bdb7
AM
10853{
10854 char *cp;
10855
10856 cp = strchr (note->namedata, '@');
10857 if (cp != NULL)
10858 {
d2b64500 10859 *lwpidp = atoi(cp + 1);
b34976b6 10860 return TRUE;
50b2bdb7 10861 }
b34976b6 10862 return FALSE;
50b2bdb7
AM
10863}
10864
b34976b6 10865static bfd_boolean
217aa764 10866elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7 10867{
80a04378
NC
10868 if (note->descsz <= 0x7c + 31)
10869 return FALSE;
10870
50b2bdb7 10871 /* Signal number at offset 0x08. */
228e534f 10872 elf_tdata (abfd)->core->signal
50b2bdb7
AM
10873 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10874
10875 /* Process ID at offset 0x50. */
228e534f 10876 elf_tdata (abfd)->core->pid
50b2bdb7
AM
10877 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
10878
10879 /* Command name at 0x7c (max 32 bytes, including nul). */
228e534f 10880 elf_tdata (abfd)->core->command
50b2bdb7
AM
10881 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
10882
7720ba9f
MK
10883 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
10884 note);
50b2bdb7
AM
10885}
10886
b34976b6 10887static bfd_boolean
217aa764 10888elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7
AM
10889{
10890 int lwp;
10891
10892 if (elfcore_netbsd_get_lwpid (note, &lwp))
228e534f 10893 elf_tdata (abfd)->core->lwpid = lwp;
50b2bdb7 10894
58e07198 10895 switch (note->type)
50b2bdb7 10896 {
58e07198 10897 case NT_NETBSDCORE_PROCINFO:
50b2bdb7 10898 /* NetBSD-specific core "procinfo". Note that we expect to
08a40648
AM
10899 find this note before any of the others, which is fine,
10900 since the kernel writes this note out first when it
10901 creates a core file. */
50b2bdb7 10902 return elfcore_grok_netbsd_procinfo (abfd, note);
58e07198
CZ
10903#ifdef NT_NETBSDCORE_AUXV
10904 case NT_NETBSDCORE_AUXV:
10905 /* NetBSD-specific Elf Auxiliary Vector data. */
10906 return elfcore_make_auxv_note_section (abfd, note, 4);
06d949ec
KR
10907#endif
10908#ifdef NT_NETBSDCORE_LWPSTATUS
10909 case NT_NETBSDCORE_LWPSTATUS:
10910 return elfcore_make_note_pseudosection (abfd,
10911 ".note.netbsdcore.lwpstatus",
10912 note);
58e07198
CZ
10913#endif
10914 default:
10915 break;
50b2bdb7
AM
10916 }
10917
06d949ec 10918 /* As of March 2020 there are no other machine-independent notes
b4db1224
JT
10919 defined for NetBSD core files. If the note type is less
10920 than the start of the machine-dependent note types, we don't
10921 understand it. */
47d9a591 10922
b4db1224 10923 if (note->type < NT_NETBSDCORE_FIRSTMACH)
b34976b6 10924 return TRUE;
50b2bdb7
AM
10925
10926
10927 switch (bfd_get_arch (abfd))
10928 {
08a40648
AM
10929 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10930 PT_GETFPREGS == mach+2. */
50b2bdb7 10931
015ec493 10932 case bfd_arch_aarch64:
50b2bdb7
AM
10933 case bfd_arch_alpha:
10934 case bfd_arch_sparc:
10935 switch (note->type)
08a40648
AM
10936 {
10937 case NT_NETBSDCORE_FIRSTMACH+0:
10938 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 10939
08a40648
AM
10940 case NT_NETBSDCORE_FIRSTMACH+2:
10941 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 10942
08a40648
AM
10943 default:
10944 return TRUE;
10945 }
50b2bdb7 10946
58e07198
CZ
10947 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
10948 There's also old PT___GETREGS40 == mach + 1 for old reg
10949 structure which lacks GBR. */
10950
10951 case bfd_arch_sh:
10952 switch (note->type)
10953 {
10954 case NT_NETBSDCORE_FIRSTMACH+3:
10955 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10956
10957 case NT_NETBSDCORE_FIRSTMACH+5:
10958 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10959
10960 default:
10961 return TRUE;
10962 }
10963
08a40648
AM
10964 /* On all other arch's, PT_GETREGS == mach+1 and
10965 PT_GETFPREGS == mach+3. */
50b2bdb7
AM
10966
10967 default:
10968 switch (note->type)
08a40648
AM
10969 {
10970 case NT_NETBSDCORE_FIRSTMACH+1:
10971 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 10972
08a40648
AM
10973 case NT_NETBSDCORE_FIRSTMACH+3:
10974 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 10975
08a40648
AM
10976 default:
10977 return TRUE;
10978 }
50b2bdb7
AM
10979 }
10980 /* NOTREACHED */
10981}
10982
67cc5033
MK
10983static bfd_boolean
10984elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
10985{
80a04378
NC
10986 if (note->descsz <= 0x48 + 31)
10987 return FALSE;
10988
67cc5033 10989 /* Signal number at offset 0x08. */
228e534f 10990 elf_tdata (abfd)->core->signal
67cc5033
MK
10991 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10992
10993 /* Process ID at offset 0x20. */
228e534f 10994 elf_tdata (abfd)->core->pid
67cc5033
MK
10995 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
10996
10997 /* Command name at 0x48 (max 32 bytes, including nul). */
228e534f 10998 elf_tdata (abfd)->core->command
67cc5033
MK
10999 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11000
11001 return TRUE;
11002}
11003
11004static bfd_boolean
11005elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
11006{
11007 if (note->type == NT_OPENBSD_PROCINFO)
11008 return elfcore_grok_openbsd_procinfo (abfd, note);
11009
11010 if (note->type == NT_OPENBSD_REGS)
11011 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11012
11013 if (note->type == NT_OPENBSD_FPREGS)
11014 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11015
11016 if (note->type == NT_OPENBSD_XFPREGS)
11017 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
11018
11019 if (note->type == NT_OPENBSD_AUXV)
58e07198 11020 return elfcore_make_auxv_note_section (abfd, note, 0);
67cc5033
MK
11021
11022 if (note->type == NT_OPENBSD_WCOOKIE)
11023 {
11024 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
11025 SEC_HAS_CONTENTS);
11026
11027 if (sect == NULL)
11028 return FALSE;
11029 sect->size = note->descsz;
11030 sect->filepos = note->descpos;
11031 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
11032
11033 return TRUE;
11034 }
11035
11036 return TRUE;
11037}
11038
07c6e936 11039static bfd_boolean
d3fd4074 11040elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
07c6e936
NC
11041{
11042 void *ddata = note->descdata;
11043 char buf[100];
11044 char *name;
11045 asection *sect;
f8843e87
AM
11046 short sig;
11047 unsigned flags;
07c6e936 11048
80a04378
NC
11049 if (note->descsz < 16)
11050 return FALSE;
11051
07c6e936 11052 /* nto_procfs_status 'pid' field is at offset 0. */
228e534f 11053 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
07c6e936 11054
f8843e87
AM
11055 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
11056 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
11057
11058 /* nto_procfs_status 'flags' field is at offset 8. */
11059 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
07c6e936
NC
11060
11061 /* nto_procfs_status 'what' field is at offset 14. */
f8843e87
AM
11062 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
11063 {
228e534f
AM
11064 elf_tdata (abfd)->core->signal = sig;
11065 elf_tdata (abfd)->core->lwpid = *tid;
f8843e87 11066 }
07c6e936 11067
f8843e87
AM
11068 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
11069 do not come from signals so we make sure we set the current
11070 thread just in case. */
11071 if (flags & 0x00000080)
228e534f 11072 elf_tdata (abfd)->core->lwpid = *tid;
07c6e936
NC
11073
11074 /* Make a ".qnx_core_status/%d" section. */
d3fd4074 11075 sprintf (buf, ".qnx_core_status/%ld", *tid);
07c6e936 11076
a50b1753 11077 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
07c6e936
NC
11078 if (name == NULL)
11079 return FALSE;
11080 strcpy (name, buf);
11081
117ed4f8 11082 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936
NC
11083 if (sect == NULL)
11084 return FALSE;
11085
07d6d2b8
AM
11086 sect->size = note->descsz;
11087 sect->filepos = note->descpos;
07c6e936
NC
11088 sect->alignment_power = 2;
11089
11090 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
11091}
11092
11093static bfd_boolean
d69f560c
KW
11094elfcore_grok_nto_regs (bfd *abfd,
11095 Elf_Internal_Note *note,
d3fd4074 11096 long tid,
d69f560c 11097 char *base)
07c6e936
NC
11098{
11099 char buf[100];
11100 char *name;
11101 asection *sect;
11102
d69f560c 11103 /* Make a "(base)/%d" section. */
d3fd4074 11104 sprintf (buf, "%s/%ld", base, tid);
07c6e936 11105
a50b1753 11106 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
07c6e936
NC
11107 if (name == NULL)
11108 return FALSE;
11109 strcpy (name, buf);
11110
117ed4f8 11111 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936
NC
11112 if (sect == NULL)
11113 return FALSE;
11114
07d6d2b8
AM
11115 sect->size = note->descsz;
11116 sect->filepos = note->descpos;
07c6e936
NC
11117 sect->alignment_power = 2;
11118
f8843e87 11119 /* This is the current thread. */
228e534f 11120 if (elf_tdata (abfd)->core->lwpid == tid)
d69f560c 11121 return elfcore_maybe_make_sect (abfd, base, sect);
f8843e87
AM
11122
11123 return TRUE;
07c6e936
NC
11124}
11125
11126#define BFD_QNT_CORE_INFO 7
11127#define BFD_QNT_CORE_STATUS 8
11128#define BFD_QNT_CORE_GREG 9
11129#define BFD_QNT_CORE_FPREG 10
11130
11131static bfd_boolean
217aa764 11132elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
07c6e936
NC
11133{
11134 /* Every GREG section has a STATUS section before it. Store the
811072d8 11135 tid from the previous call to pass down to the next gregs
07c6e936 11136 function. */
d3fd4074 11137 static long tid = 1;
07c6e936
NC
11138
11139 switch (note->type)
11140 {
d69f560c
KW
11141 case BFD_QNT_CORE_INFO:
11142 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
11143 case BFD_QNT_CORE_STATUS:
11144 return elfcore_grok_nto_status (abfd, note, &tid);
11145 case BFD_QNT_CORE_GREG:
11146 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
11147 case BFD_QNT_CORE_FPREG:
11148 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
11149 default:
11150 return TRUE;
07c6e936
NC
11151 }
11152}
11153
b15fa79e
AM
11154static bfd_boolean
11155elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
11156{
11157 char *name;
11158 asection *sect;
11159 size_t len;
11160
11161 /* Use note name as section name. */
11162 len = note->namesz;
a50b1753 11163 name = (char *) bfd_alloc (abfd, len);
b15fa79e
AM
11164 if (name == NULL)
11165 return FALSE;
11166 memcpy (name, note->namedata, len);
11167 name[len - 1] = '\0';
11168
11169 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11170 if (sect == NULL)
11171 return FALSE;
11172
07d6d2b8
AM
11173 sect->size = note->descsz;
11174 sect->filepos = note->descpos;
b15fa79e
AM
11175 sect->alignment_power = 1;
11176
11177 return TRUE;
11178}
11179
7c76fa91
MS
11180/* Function: elfcore_write_note
11181
47d9a591 11182 Inputs:
a39f3346 11183 buffer to hold note, and current size of buffer
7c76fa91
MS
11184 name of note
11185 type of note
11186 data for note
11187 size of data for note
11188
a39f3346
AM
11189 Writes note to end of buffer. ELF64 notes are written exactly as
11190 for ELF32, despite the current (as of 2006) ELF gabi specifying
11191 that they ought to have 8-byte namesz and descsz field, and have
11192 8-byte alignment. Other writers, eg. Linux kernel, do the same.
11193
7c76fa91 11194 Return:
a39f3346 11195 Pointer to realloc'd buffer, *BUFSIZ updated. */
7c76fa91
MS
11196
11197char *
a39f3346 11198elfcore_write_note (bfd *abfd,
217aa764 11199 char *buf,
a39f3346 11200 int *bufsiz,
217aa764 11201 const char *name,
a39f3346 11202 int type,
217aa764 11203 const void *input,
a39f3346 11204 int size)
7c76fa91
MS
11205{
11206 Elf_External_Note *xnp;
d4c88bbb 11207 size_t namesz;
d4c88bbb 11208 size_t newspace;
a39f3346 11209 char *dest;
7c76fa91 11210
d4c88bbb 11211 namesz = 0;
d4c88bbb 11212 if (name != NULL)
a39f3346 11213 namesz = strlen (name) + 1;
d4c88bbb 11214
a39f3346 11215 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
d4c88bbb 11216
a50b1753 11217 buf = (char *) realloc (buf, *bufsiz + newspace);
14b1c01e
AM
11218 if (buf == NULL)
11219 return buf;
a39f3346 11220 dest = buf + *bufsiz;
7c76fa91
MS
11221 *bufsiz += newspace;
11222 xnp = (Elf_External_Note *) dest;
11223 H_PUT_32 (abfd, namesz, xnp->namesz);
11224 H_PUT_32 (abfd, size, xnp->descsz);
11225 H_PUT_32 (abfd, type, xnp->type);
d4c88bbb
AM
11226 dest = xnp->name;
11227 if (name != NULL)
11228 {
11229 memcpy (dest, name, namesz);
11230 dest += namesz;
a39f3346 11231 while (namesz & 3)
d4c88bbb
AM
11232 {
11233 *dest++ = '\0';
a39f3346 11234 ++namesz;
d4c88bbb
AM
11235 }
11236 }
11237 memcpy (dest, input, size);
a39f3346
AM
11238 dest += size;
11239 while (size & 3)
11240 {
11241 *dest++ = '\0';
11242 ++size;
11243 }
11244 return buf;
7c76fa91
MS
11245}
11246
602f1657
AM
11247/* gcc-8 warns (*) on all the strncpy calls in this function about
11248 possible string truncation. The "truncation" is not a bug. We
11249 have an external representation of structs with fields that are not
11250 necessarily NULL terminated and corresponding internal
11251 representation fields that are one larger so that they can always
11252 be NULL terminated.
11253 gcc versions between 4.2 and 4.6 do not allow pragma control of
11254 diagnostics inside functions, giving a hard error if you try to use
11255 the finer control available with later versions.
11256 gcc prior to 4.2 warns about diagnostic push and pop.
11257 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
11258 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
11259 (*) Depending on your system header files! */
d99b4b92 11260#if GCC_VERSION >= 8000
602f1657
AM
11261# pragma GCC diagnostic push
11262# pragma GCC diagnostic ignored "-Wstringop-truncation"
d99b4b92 11263#endif
7c76fa91 11264char *
217aa764
AM
11265elfcore_write_prpsinfo (bfd *abfd,
11266 char *buf,
11267 int *bufsiz,
11268 const char *fname,
11269 const char *psargs)
7c76fa91 11270{
183e98be
AM
11271 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11272
11273 if (bed->elf_backend_write_core_note != NULL)
11274 {
11275 char *ret;
11276 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11277 NT_PRPSINFO, fname, psargs);
11278 if (ret != NULL)
11279 return ret;
11280 }
7c76fa91 11281
1f20dca5 11282#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
602f1657 11283# if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
183e98be
AM
11284 if (bed->s->elfclass == ELFCLASS32)
11285 {
602f1657 11286# if defined (HAVE_PSINFO32_T)
183e98be
AM
11287 psinfo32_t data;
11288 int note_type = NT_PSINFO;
602f1657 11289# else
183e98be
AM
11290 prpsinfo32_t data;
11291 int note_type = NT_PRPSINFO;
602f1657 11292# endif
183e98be
AM
11293
11294 memset (&data, 0, sizeof (data));
11295 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11296 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11297 return elfcore_write_note (abfd, buf, bufsiz,
1f20dca5 11298 "CORE", note_type, &data, sizeof (data));
183e98be
AM
11299 }
11300 else
602f1657 11301# endif
183e98be 11302 {
602f1657 11303# if defined (HAVE_PSINFO_T)
183e98be
AM
11304 psinfo_t data;
11305 int note_type = NT_PSINFO;
602f1657 11306# else
183e98be
AM
11307 prpsinfo_t data;
11308 int note_type = NT_PRPSINFO;
602f1657 11309# endif
7c76fa91 11310
183e98be
AM
11311 memset (&data, 0, sizeof (data));
11312 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11313 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11314 return elfcore_write_note (abfd, buf, bufsiz,
1f20dca5 11315 "CORE", note_type, &data, sizeof (data));
183e98be 11316 }
7c76fa91
MS
11317#endif /* PSINFO_T or PRPSINFO_T */
11318
1f20dca5
UW
11319 free (buf);
11320 return NULL;
11321}
d99b4b92 11322#if GCC_VERSION >= 8000
602f1657 11323# pragma GCC diagnostic pop
d99b4b92 11324#endif
1f20dca5 11325
70a38d42
SDJ
11326char *
11327elfcore_write_linux_prpsinfo32
11328 (bfd *abfd, char *buf, int *bufsiz,
11329 const struct elf_internal_linux_prpsinfo *prpsinfo)
11330{
a2f63b2e
MR
11331 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
11332 {
11333 struct elf_external_linux_prpsinfo32_ugid16 data;
11334
11335 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
11336 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11337 &data, sizeof (data));
11338 }
11339 else
11340 {
11341 struct elf_external_linux_prpsinfo32_ugid32 data;
70a38d42 11342
a2f63b2e
MR
11343 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
11344 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11345 &data, sizeof (data));
11346 }
70a38d42
SDJ
11347}
11348
11349char *
11350elfcore_write_linux_prpsinfo64
11351 (bfd *abfd, char *buf, int *bufsiz,
11352 const struct elf_internal_linux_prpsinfo *prpsinfo)
11353{
3c9a7b0d
MR
11354 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
11355 {
11356 struct elf_external_linux_prpsinfo64_ugid16 data;
11357
11358 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
11359 return elfcore_write_note (abfd, buf, bufsiz,
11360 "CORE", NT_PRPSINFO, &data, sizeof (data));
11361 }
11362 else
11363 {
11364 struct elf_external_linux_prpsinfo64_ugid32 data;
70a38d42 11365
3c9a7b0d
MR
11366 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
11367 return elfcore_write_note (abfd, buf, bufsiz,
11368 "CORE", NT_PRPSINFO, &data, sizeof (data));
11369 }
70a38d42
SDJ
11370}
11371
7c76fa91 11372char *
217aa764
AM
11373elfcore_write_prstatus (bfd *abfd,
11374 char *buf,
11375 int *bufsiz,
11376 long pid,
11377 int cursig,
11378 const void *gregs)
7c76fa91 11379{
183e98be 11380 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 11381
183e98be
AM
11382 if (bed->elf_backend_write_core_note != NULL)
11383 {
11384 char *ret;
11385 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11386 NT_PRSTATUS,
11387 pid, cursig, gregs);
11388 if (ret != NULL)
11389 return ret;
11390 }
11391
1f20dca5 11392#if defined (HAVE_PRSTATUS_T)
183e98be
AM
11393#if defined (HAVE_PRSTATUS32_T)
11394 if (bed->s->elfclass == ELFCLASS32)
11395 {
11396 prstatus32_t prstat;
11397
11398 memset (&prstat, 0, sizeof (prstat));
11399 prstat.pr_pid = pid;
11400 prstat.pr_cursig = cursig;
11401 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
1f20dca5 11402 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
183e98be
AM
11403 NT_PRSTATUS, &prstat, sizeof (prstat));
11404 }
11405 else
11406#endif
11407 {
11408 prstatus_t prstat;
11409
11410 memset (&prstat, 0, sizeof (prstat));
11411 prstat.pr_pid = pid;
11412 prstat.pr_cursig = cursig;
11413 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
1f20dca5 11414 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
183e98be
AM
11415 NT_PRSTATUS, &prstat, sizeof (prstat));
11416 }
7c76fa91
MS
11417#endif /* HAVE_PRSTATUS_T */
11418
1f20dca5
UW
11419 free (buf);
11420 return NULL;
11421}
11422
51316059
MS
11423#if defined (HAVE_LWPSTATUS_T)
11424char *
217aa764
AM
11425elfcore_write_lwpstatus (bfd *abfd,
11426 char *buf,
11427 int *bufsiz,
11428 long pid,
11429 int cursig,
11430 const void *gregs)
51316059
MS
11431{
11432 lwpstatus_t lwpstat;
183e98be 11433 const char *note_name = "CORE";
51316059
MS
11434
11435 memset (&lwpstat, 0, sizeof (lwpstat));
11436 lwpstat.pr_lwpid = pid >> 16;
11437 lwpstat.pr_cursig = cursig;
11438#if defined (HAVE_LWPSTATUS_T_PR_REG)
d1e8523e 11439 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
51316059
MS
11440#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11441#if !defined(gregs)
11442 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
11443 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
11444#else
11445 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
11446 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
11447#endif
11448#endif
47d9a591 11449 return elfcore_write_note (abfd, buf, bufsiz, note_name,
51316059
MS
11450 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
11451}
11452#endif /* HAVE_LWPSTATUS_T */
11453
7c76fa91
MS
11454#if defined (HAVE_PSTATUS_T)
11455char *
217aa764
AM
11456elfcore_write_pstatus (bfd *abfd,
11457 char *buf,
11458 int *bufsiz,
11459 long pid,
6c10990d
NC
11460 int cursig ATTRIBUTE_UNUSED,
11461 const void *gregs ATTRIBUTE_UNUSED)
7c76fa91 11462{
183e98be
AM
11463 const char *note_name = "CORE";
11464#if defined (HAVE_PSTATUS32_T)
11465 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 11466
183e98be
AM
11467 if (bed->s->elfclass == ELFCLASS32)
11468 {
11469 pstatus32_t pstat;
11470
11471 memset (&pstat, 0, sizeof (pstat));
11472 pstat.pr_pid = pid & 0xffff;
11473 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11474 NT_PSTATUS, &pstat, sizeof (pstat));
11475 return buf;
11476 }
11477 else
11478#endif
11479 {
11480 pstatus_t pstat;
11481
11482 memset (&pstat, 0, sizeof (pstat));
11483 pstat.pr_pid = pid & 0xffff;
11484 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11485 NT_PSTATUS, &pstat, sizeof (pstat));
11486 return buf;
11487 }
7c76fa91
MS
11488}
11489#endif /* HAVE_PSTATUS_T */
11490
11491char *
217aa764
AM
11492elfcore_write_prfpreg (bfd *abfd,
11493 char *buf,
11494 int *bufsiz,
11495 const void *fpregs,
11496 int size)
7c76fa91 11497{
183e98be 11498 const char *note_name = "CORE";
47d9a591 11499 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
11500 note_name, NT_FPREGSET, fpregs, size);
11501}
11502
11503char *
217aa764
AM
11504elfcore_write_prxfpreg (bfd *abfd,
11505 char *buf,
11506 int *bufsiz,
11507 const void *xfpregs,
11508 int size)
7c76fa91
MS
11509{
11510 char *note_name = "LINUX";
47d9a591 11511 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
11512 note_name, NT_PRXFPREG, xfpregs, size);
11513}
11514
4339cae0
L
11515char *
11516elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
11517 const void *xfpregs, int size)
11518{
97de3545
JB
11519 char *note_name;
11520 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
11521 note_name = "FreeBSD";
11522 else
11523 note_name = "LINUX";
4339cae0
L
11524 return elfcore_write_note (abfd, buf, bufsiz,
11525 note_name, NT_X86_XSTATE, xfpregs, size);
11526}
11527
97753bd5
AM
11528char *
11529elfcore_write_ppc_vmx (bfd *abfd,
11530 char *buf,
11531 int *bufsiz,
11532 const void *ppc_vmx,
11533 int size)
11534{
11535 char *note_name = "LINUX";
11536 return elfcore_write_note (abfd, buf, bufsiz,
11537 note_name, NT_PPC_VMX, ppc_vmx, size);
11538}
11539
89eeb0bc
LM
11540char *
11541elfcore_write_ppc_vsx (bfd *abfd,
07d6d2b8
AM
11542 char *buf,
11543 int *bufsiz,
11544 const void *ppc_vsx,
11545 int size)
89eeb0bc
LM
11546{
11547 char *note_name = "LINUX";
11548 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11549 note_name, NT_PPC_VSX, ppc_vsx, size);
89eeb0bc
LM
11550}
11551
cb2366c1
EBM
11552char *
11553elfcore_write_ppc_tar (bfd *abfd,
4b24dd1a
AM
11554 char *buf,
11555 int *bufsiz,
11556 const void *ppc_tar,
11557 int size)
cb2366c1
EBM
11558{
11559 char *note_name = "LINUX";
11560 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11561 note_name, NT_PPC_TAR, ppc_tar, size);
cb2366c1
EBM
11562}
11563
11564char *
11565elfcore_write_ppc_ppr (bfd *abfd,
4b24dd1a
AM
11566 char *buf,
11567 int *bufsiz,
11568 const void *ppc_ppr,
11569 int size)
cb2366c1
EBM
11570{
11571 char *note_name = "LINUX";
11572 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11573 note_name, NT_PPC_PPR, ppc_ppr, size);
cb2366c1
EBM
11574}
11575
11576char *
11577elfcore_write_ppc_dscr (bfd *abfd,
4b24dd1a
AM
11578 char *buf,
11579 int *bufsiz,
11580 const void *ppc_dscr,
11581 int size)
cb2366c1
EBM
11582{
11583 char *note_name = "LINUX";
11584 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11585 note_name, NT_PPC_DSCR, ppc_dscr, size);
cb2366c1
EBM
11586}
11587
11588char *
11589elfcore_write_ppc_ebb (bfd *abfd,
4b24dd1a
AM
11590 char *buf,
11591 int *bufsiz,
11592 const void *ppc_ebb,
11593 int size)
cb2366c1
EBM
11594{
11595 char *note_name = "LINUX";
11596 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11597 note_name, NT_PPC_EBB, ppc_ebb, size);
cb2366c1
EBM
11598}
11599
11600char *
11601elfcore_write_ppc_pmu (bfd *abfd,
4b24dd1a
AM
11602 char *buf,
11603 int *bufsiz,
11604 const void *ppc_pmu,
11605 int size)
cb2366c1
EBM
11606{
11607 char *note_name = "LINUX";
11608 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11609 note_name, NT_PPC_PMU, ppc_pmu, size);
cb2366c1
EBM
11610}
11611
11612char *
11613elfcore_write_ppc_tm_cgpr (bfd *abfd,
4b24dd1a
AM
11614 char *buf,
11615 int *bufsiz,
11616 const void *ppc_tm_cgpr,
11617 int size)
cb2366c1
EBM
11618{
11619 char *note_name = "LINUX";
11620 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11621 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
cb2366c1
EBM
11622}
11623
11624char *
11625elfcore_write_ppc_tm_cfpr (bfd *abfd,
4b24dd1a
AM
11626 char *buf,
11627 int *bufsiz,
11628 const void *ppc_tm_cfpr,
11629 int size)
cb2366c1
EBM
11630{
11631 char *note_name = "LINUX";
11632 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11633 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
cb2366c1
EBM
11634}
11635
11636char *
11637elfcore_write_ppc_tm_cvmx (bfd *abfd,
4b24dd1a
AM
11638 char *buf,
11639 int *bufsiz,
11640 const void *ppc_tm_cvmx,
11641 int size)
cb2366c1
EBM
11642{
11643 char *note_name = "LINUX";
11644 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11645 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
cb2366c1
EBM
11646}
11647
11648char *
11649elfcore_write_ppc_tm_cvsx (bfd *abfd,
4b24dd1a
AM
11650 char *buf,
11651 int *bufsiz,
11652 const void *ppc_tm_cvsx,
11653 int size)
cb2366c1
EBM
11654{
11655 char *note_name = "LINUX";
11656 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11657 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
cb2366c1
EBM
11658}
11659
11660char *
11661elfcore_write_ppc_tm_spr (bfd *abfd,
4b24dd1a
AM
11662 char *buf,
11663 int *bufsiz,
11664 const void *ppc_tm_spr,
11665 int size)
cb2366c1
EBM
11666{
11667 char *note_name = "LINUX";
11668 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11669 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
cb2366c1
EBM
11670}
11671
11672char *
11673elfcore_write_ppc_tm_ctar (bfd *abfd,
4b24dd1a
AM
11674 char *buf,
11675 int *bufsiz,
11676 const void *ppc_tm_ctar,
11677 int size)
cb2366c1
EBM
11678{
11679 char *note_name = "LINUX";
11680 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11681 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
cb2366c1
EBM
11682}
11683
11684char *
11685elfcore_write_ppc_tm_cppr (bfd *abfd,
4b24dd1a
AM
11686 char *buf,
11687 int *bufsiz,
11688 const void *ppc_tm_cppr,
11689 int size)
cb2366c1
EBM
11690{
11691 char *note_name = "LINUX";
11692 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11693 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
cb2366c1
EBM
11694}
11695
11696char *
11697elfcore_write_ppc_tm_cdscr (bfd *abfd,
4b24dd1a
AM
11698 char *buf,
11699 int *bufsiz,
11700 const void *ppc_tm_cdscr,
11701 int size)
cb2366c1
EBM
11702{
11703 char *note_name = "LINUX";
11704 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11705 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
cb2366c1
EBM
11706}
11707
0675e188
UW
11708static char *
11709elfcore_write_s390_high_gprs (bfd *abfd,
11710 char *buf,
11711 int *bufsiz,
11712 const void *s390_high_gprs,
11713 int size)
11714{
11715 char *note_name = "LINUX";
11716 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11717 note_name, NT_S390_HIGH_GPRS,
0675e188
UW
11718 s390_high_gprs, size);
11719}
11720
d7eeb400
MS
11721char *
11722elfcore_write_s390_timer (bfd *abfd,
07d6d2b8
AM
11723 char *buf,
11724 int *bufsiz,
11725 const void *s390_timer,
11726 int size)
d7eeb400
MS
11727{
11728 char *note_name = "LINUX";
11729 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11730 note_name, NT_S390_TIMER, s390_timer, size);
d7eeb400
MS
11731}
11732
11733char *
11734elfcore_write_s390_todcmp (bfd *abfd,
07d6d2b8
AM
11735 char *buf,
11736 int *bufsiz,
11737 const void *s390_todcmp,
11738 int size)
d7eeb400
MS
11739{
11740 char *note_name = "LINUX";
11741 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11742 note_name, NT_S390_TODCMP, s390_todcmp, size);
d7eeb400
MS
11743}
11744
11745char *
11746elfcore_write_s390_todpreg (bfd *abfd,
07d6d2b8
AM
11747 char *buf,
11748 int *bufsiz,
11749 const void *s390_todpreg,
11750 int size)
d7eeb400
MS
11751{
11752 char *note_name = "LINUX";
11753 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11754 note_name, NT_S390_TODPREG, s390_todpreg, size);
d7eeb400
MS
11755}
11756
11757char *
11758elfcore_write_s390_ctrs (bfd *abfd,
07d6d2b8
AM
11759 char *buf,
11760 int *bufsiz,
11761 const void *s390_ctrs,
11762 int size)
d7eeb400
MS
11763{
11764 char *note_name = "LINUX";
11765 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11766 note_name, NT_S390_CTRS, s390_ctrs, size);
d7eeb400
MS
11767}
11768
11769char *
11770elfcore_write_s390_prefix (bfd *abfd,
07d6d2b8
AM
11771 char *buf,
11772 int *bufsiz,
11773 const void *s390_prefix,
11774 int size)
d7eeb400
MS
11775{
11776 char *note_name = "LINUX";
11777 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11778 note_name, NT_S390_PREFIX, s390_prefix, size);
d7eeb400
MS
11779}
11780
355b81d9
UW
11781char *
11782elfcore_write_s390_last_break (bfd *abfd,
11783 char *buf,
11784 int *bufsiz,
11785 const void *s390_last_break,
11786 int size)
11787{
11788 char *note_name = "LINUX";
11789 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11790 note_name, NT_S390_LAST_BREAK,
355b81d9
UW
11791 s390_last_break, size);
11792}
11793
11794char *
11795elfcore_write_s390_system_call (bfd *abfd,
11796 char *buf,
11797 int *bufsiz,
11798 const void *s390_system_call,
11799 int size)
11800{
11801 char *note_name = "LINUX";
11802 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11803 note_name, NT_S390_SYSTEM_CALL,
355b81d9
UW
11804 s390_system_call, size);
11805}
11806
abb3f6cc
NC
11807char *
11808elfcore_write_s390_tdb (bfd *abfd,
11809 char *buf,
11810 int *bufsiz,
11811 const void *s390_tdb,
11812 int size)
11813{
11814 char *note_name = "LINUX";
11815 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11816 note_name, NT_S390_TDB, s390_tdb, size);
abb3f6cc
NC
11817}
11818
4ef9f41a
AA
11819char *
11820elfcore_write_s390_vxrs_low (bfd *abfd,
11821 char *buf,
11822 int *bufsiz,
11823 const void *s390_vxrs_low,
11824 int size)
11825{
11826 char *note_name = "LINUX";
11827 return elfcore_write_note (abfd, buf, bufsiz,
11828 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
11829}
11830
11831char *
11832elfcore_write_s390_vxrs_high (bfd *abfd,
11833 char *buf,
11834 int *bufsiz,
11835 const void *s390_vxrs_high,
11836 int size)
11837{
11838 char *note_name = "LINUX";
11839 return elfcore_write_note (abfd, buf, bufsiz,
11840 note_name, NT_S390_VXRS_HIGH,
11841 s390_vxrs_high, size);
11842}
11843
88ab90e8
AA
11844char *
11845elfcore_write_s390_gs_cb (bfd *abfd,
11846 char *buf,
11847 int *bufsiz,
11848 const void *s390_gs_cb,
11849 int size)
11850{
11851 char *note_name = "LINUX";
11852 return elfcore_write_note (abfd, buf, bufsiz,
11853 note_name, NT_S390_GS_CB,
11854 s390_gs_cb, size);
11855}
11856
11857char *
11858elfcore_write_s390_gs_bc (bfd *abfd,
11859 char *buf,
11860 int *bufsiz,
11861 const void *s390_gs_bc,
11862 int size)
11863{
11864 char *note_name = "LINUX";
11865 return elfcore_write_note (abfd, buf, bufsiz,
11866 note_name, NT_S390_GS_BC,
11867 s390_gs_bc, size);
11868}
11869
faa9a424
UW
11870char *
11871elfcore_write_arm_vfp (bfd *abfd,
11872 char *buf,
11873 int *bufsiz,
11874 const void *arm_vfp,
11875 int size)
11876{
11877 char *note_name = "LINUX";
11878 return elfcore_write_note (abfd, buf, bufsiz,
11879 note_name, NT_ARM_VFP, arm_vfp, size);
11880}
11881
652451f8
YZ
11882char *
11883elfcore_write_aarch_tls (bfd *abfd,
11884 char *buf,
11885 int *bufsiz,
11886 const void *aarch_tls,
11887 int size)
11888{
11889 char *note_name = "LINUX";
11890 return elfcore_write_note (abfd, buf, bufsiz,
11891 note_name, NT_ARM_TLS, aarch_tls, size);
11892}
11893
11894char *
11895elfcore_write_aarch_hw_break (bfd *abfd,
11896 char *buf,
11897 int *bufsiz,
11898 const void *aarch_hw_break,
11899 int size)
11900{
11901 char *note_name = "LINUX";
11902 return elfcore_write_note (abfd, buf, bufsiz,
11903 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
11904}
11905
11906char *
11907elfcore_write_aarch_hw_watch (bfd *abfd,
11908 char *buf,
11909 int *bufsiz,
11910 const void *aarch_hw_watch,
11911 int size)
11912{
11913 char *note_name = "LINUX";
11914 return elfcore_write_note (abfd, buf, bufsiz,
11915 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
11916}
11917
ad1cc4e4
AH
11918char *
11919elfcore_write_aarch_sve (bfd *abfd,
11920 char *buf,
11921 int *bufsiz,
11922 const void *aarch_sve,
11923 int size)
11924{
11925 char *note_name = "LINUX";
11926 return elfcore_write_note (abfd, buf, bufsiz,
11927 note_name, NT_ARM_SVE, aarch_sve, size);
11928}
11929
e6c3b5bf
AH
11930char *
11931elfcore_write_aarch_pauth (bfd *abfd,
11932 char *buf,
11933 int *bufsiz,
11934 const void *aarch_pauth,
11935 int size)
11936{
11937 char *note_name = "LINUX";
11938 return elfcore_write_note (abfd, buf, bufsiz,
11939 note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
11940}
11941
27456742
AK
11942char *
11943elfcore_write_arc_v2 (bfd *abfd,
11944 char *buf,
11945 int *bufsiz,
11946 const void *arc_v2,
11947 int size)
11948{
11949 char *note_name = "LINUX";
11950 return elfcore_write_note (abfd, buf, bufsiz,
11951 note_name, NT_ARC_V2, arc_v2, size);
11952}
11953
bb864ac1
CES
11954char *
11955elfcore_write_register_note (bfd *abfd,
11956 char *buf,
11957 int *bufsiz,
11958 const char *section,
11959 const void *data,
11960 int size)
11961{
11962 if (strcmp (section, ".reg2") == 0)
11963 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
11964 if (strcmp (section, ".reg-xfp") == 0)
11965 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
4339cae0
L
11966 if (strcmp (section, ".reg-xstate") == 0)
11967 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
bb864ac1
CES
11968 if (strcmp (section, ".reg-ppc-vmx") == 0)
11969 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
89eeb0bc
LM
11970 if (strcmp (section, ".reg-ppc-vsx") == 0)
11971 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
cb2366c1
EBM
11972 if (strcmp (section, ".reg-ppc-tar") == 0)
11973 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
11974 if (strcmp (section, ".reg-ppc-ppr") == 0)
11975 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
11976 if (strcmp (section, ".reg-ppc-dscr") == 0)
11977 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
11978 if (strcmp (section, ".reg-ppc-ebb") == 0)
11979 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
11980 if (strcmp (section, ".reg-ppc-pmu") == 0)
11981 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
11982 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
11983 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
11984 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
11985 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
11986 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
11987 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
11988 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
11989 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
11990 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
11991 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
11992 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
11993 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
11994 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
11995 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
11996 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
11997 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
0675e188
UW
11998 if (strcmp (section, ".reg-s390-high-gprs") == 0)
11999 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
d7eeb400
MS
12000 if (strcmp (section, ".reg-s390-timer") == 0)
12001 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
12002 if (strcmp (section, ".reg-s390-todcmp") == 0)
12003 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
12004 if (strcmp (section, ".reg-s390-todpreg") == 0)
12005 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
12006 if (strcmp (section, ".reg-s390-ctrs") == 0)
12007 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
12008 if (strcmp (section, ".reg-s390-prefix") == 0)
12009 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
355b81d9
UW
12010 if (strcmp (section, ".reg-s390-last-break") == 0)
12011 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
12012 if (strcmp (section, ".reg-s390-system-call") == 0)
12013 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
abb3f6cc
NC
12014 if (strcmp (section, ".reg-s390-tdb") == 0)
12015 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
4ef9f41a
AA
12016 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
12017 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
12018 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
12019 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
88ab90e8
AA
12020 if (strcmp (section, ".reg-s390-gs-cb") == 0)
12021 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
12022 if (strcmp (section, ".reg-s390-gs-bc") == 0)
12023 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
faa9a424
UW
12024 if (strcmp (section, ".reg-arm-vfp") == 0)
12025 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
652451f8
YZ
12026 if (strcmp (section, ".reg-aarch-tls") == 0)
12027 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
12028 if (strcmp (section, ".reg-aarch-hw-break") == 0)
12029 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
12030 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
12031 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
ad1cc4e4
AH
12032 if (strcmp (section, ".reg-aarch-sve") == 0)
12033 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
e6c3b5bf
AH
12034 if (strcmp (section, ".reg-aarch-pauth") == 0)
12035 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
27456742
AK
12036 if (strcmp (section, ".reg-arc-v2") == 0)
12037 return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
bb864ac1
CES
12038 return NULL;
12039}
12040
4cb1265b
MS
12041char *
12042elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
12043 const void *buf, int bufsiz)
12044{
12045 return elfcore_write_note (obfd, note_data, note_size,
12046 "CORE", NT_FILE, buf, bufsiz);
12047}
12048
b34976b6 12049static bfd_boolean
276da9b3
L
12050elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
12051 size_t align)
252b5132 12052{
c044fabd 12053 char *p;
252b5132 12054
276da9b3
L
12055 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
12056 gABI specifies that PT_NOTE alignment should be aligned to 4
12057 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
12058 align is less than 4, we use 4 byte alignment. */
12059 if (align < 4)
12060 align = 4;
ef135d43
NC
12061 if (align != 4 && align != 8)
12062 return FALSE;
276da9b3 12063
252b5132
RH
12064 p = buf;
12065 while (p < buf + size)
12066 {
c044fabd 12067 Elf_External_Note *xnp = (Elf_External_Note *) p;
252b5132
RH
12068 Elf_Internal_Note in;
12069
baea7ef1
AM
12070 if (offsetof (Elf_External_Note, name) > buf - p + size)
12071 return FALSE;
12072
dc810e39 12073 in.type = H_GET_32 (abfd, xnp->type);
252b5132 12074
dc810e39 12075 in.namesz = H_GET_32 (abfd, xnp->namesz);
252b5132 12076 in.namedata = xnp->name;
baea7ef1
AM
12077 if (in.namesz > buf - in.namedata + size)
12078 return FALSE;
252b5132 12079
dc810e39 12080 in.descsz = H_GET_32 (abfd, xnp->descsz);
276da9b3 12081 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
252b5132 12082 in.descpos = offset + (in.descdata - buf);
baea7ef1
AM
12083 if (in.descsz != 0
12084 && (in.descdata >= buf + size
12085 || in.descsz > buf - in.descdata + size))
12086 return FALSE;
252b5132 12087
718175fa 12088 switch (bfd_get_format (abfd))
07d6d2b8 12089 {
718175fa
JK
12090 default:
12091 return TRUE;
12092
12093 case bfd_core:
f64e188b 12094 {
8acbedd6 12095#define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
f64e188b 12096 struct
718175fa 12097 {
f64e188b 12098 const char * string;
8acbedd6 12099 size_t len;
f64e188b 12100 bfd_boolean (* func)(bfd *, Elf_Internal_Note *);
718175fa 12101 }
f64e188b 12102 grokers[] =
b15fa79e 12103 {
8acbedd6 12104 GROKER_ELEMENT ("", elfcore_grok_note),
aa1ed4a9 12105 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
8acbedd6
KS
12106 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
12107 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note),
12108 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
864619bb
KS
12109 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
12110 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note)
f64e188b 12111 };
8acbedd6 12112#undef GROKER_ELEMENT
f64e188b
NC
12113 int i;
12114
12115 for (i = ARRAY_SIZE (grokers); i--;)
8acbedd6
KS
12116 {
12117 if (in.namesz >= grokers[i].len
12118 && strncmp (in.namedata, grokers[i].string,
12119 grokers[i].len) == 0)
12120 {
12121 if (! grokers[i].func (abfd, & in))
12122 return FALSE;
12123 break;
12124 }
12125 }
f64e188b
NC
12126 break;
12127 }
718175fa
JK
12128
12129 case bfd_object:
12130 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
12131 {
12132 if (! elfobj_grok_gnu_note (abfd, &in))
12133 return FALSE;
12134 }
e21e5835
NC
12135 else if (in.namesz == sizeof "stapsdt"
12136 && strcmp (in.namedata, "stapsdt") == 0)
12137 {
12138 if (! elfobj_grok_stapsdt_note (abfd, &in))
12139 return FALSE;
12140 }
718175fa 12141 break;
08a40648 12142 }
252b5132 12143
276da9b3 12144 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
252b5132
RH
12145 }
12146
718175fa
JK
12147 return TRUE;
12148}
12149
864619bb 12150bfd_boolean
276da9b3
L
12151elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
12152 size_t align)
718175fa
JK
12153{
12154 char *buf;
12155
957e1fc1 12156 if (size == 0 || (size + 1) == 0)
718175fa
JK
12157 return TRUE;
12158
12159 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
12160 return FALSE;
12161
2bb3687b 12162 buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
718175fa
JK
12163 if (buf == NULL)
12164 return FALSE;
12165
f64e188b
NC
12166 /* PR 17512: file: ec08f814
12167 0-termintate the buffer so that string searches will not overflow. */
12168 buf[size] = 0;
12169
2bb3687b 12170 if (!elf_parse_notes (abfd, buf, size, offset, align))
718175fa
JK
12171 {
12172 free (buf);
12173 return FALSE;
12174 }
12175
252b5132 12176 free (buf);
b34976b6 12177 return TRUE;
252b5132 12178}
98d8431c
JB
12179\f
12180/* Providing external access to the ELF program header table. */
12181
12182/* Return an upper bound on the number of bytes required to store a
12183 copy of ABFD's program header table entries. Return -1 if an error
12184 occurs; bfd_get_error will return an appropriate code. */
c044fabd 12185
98d8431c 12186long
217aa764 12187bfd_get_elf_phdr_upper_bound (bfd *abfd)
98d8431c
JB
12188{
12189 if (abfd->xvec->flavour != bfd_target_elf_flavour)
12190 {
12191 bfd_set_error (bfd_error_wrong_format);
12192 return -1;
12193 }
12194
936e320b 12195 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
98d8431c
JB
12196}
12197
98d8431c
JB
12198/* Copy ABFD's program header table entries to *PHDRS. The entries
12199 will be stored as an array of Elf_Internal_Phdr structures, as
12200 defined in include/elf/internal.h. To find out how large the
12201 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
12202
12203 Return the number of program header table entries read, or -1 if an
12204 error occurs; bfd_get_error will return an appropriate code. */
c044fabd 12205
98d8431c 12206int
217aa764 12207bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
98d8431c
JB
12208{
12209 int num_phdrs;
12210
12211 if (abfd->xvec->flavour != bfd_target_elf_flavour)
12212 {
12213 bfd_set_error (bfd_error_wrong_format);
12214 return -1;
12215 }
12216
12217 num_phdrs = elf_elfheader (abfd)->e_phnum;
01bcaf63
TT
12218 if (num_phdrs != 0)
12219 memcpy (phdrs, elf_tdata (abfd)->phdr,
12220 num_phdrs * sizeof (Elf_Internal_Phdr));
98d8431c
JB
12221
12222 return num_phdrs;
12223}
ae4221d7 12224
db6751f2 12225enum elf_reloc_type_class
7e612e98
AM
12226_bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12227 const asection *rel_sec ATTRIBUTE_UNUSED,
12228 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
db6751f2
JJ
12229{
12230 return reloc_class_normal;
12231}
f8df10f4 12232
47d9a591 12233/* For RELA architectures, return the relocation value for a
f8df10f4
JJ
12234 relocation against a local symbol. */
12235
12236bfd_vma
217aa764
AM
12237_bfd_elf_rela_local_sym (bfd *abfd,
12238 Elf_Internal_Sym *sym,
8517fae7 12239 asection **psec,
217aa764 12240 Elf_Internal_Rela *rel)
f8df10f4 12241{
8517fae7 12242 asection *sec = *psec;
f8df10f4
JJ
12243 bfd_vma relocation;
12244
6835821b
AM
12245 relocation = (sec->output_section->vma
12246 + sec->output_offset
12247 + sym->st_value);
f8df10f4 12248 if ((sec->flags & SEC_MERGE)
c629eae0 12249 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
dbaa2011 12250 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
f8df10f4 12251 {
f8df10f4 12252 rel->r_addend =
8517fae7 12253 _bfd_merged_section_offset (abfd, psec,
65765700 12254 elf_section_data (sec)->sec_info,
753731ee
AM
12255 sym->st_value + rel->r_addend);
12256 if (sec != *psec)
12257 {
12258 /* If we have changed the section, and our original section is
12259 marked with SEC_EXCLUDE, it means that the original
12260 SEC_MERGE section has been completely subsumed in some
12261 other SEC_MERGE section. In this case, we need to leave
12262 some info around for --emit-relocs. */
12263 if ((sec->flags & SEC_EXCLUDE) != 0)
12264 sec->kept_section = *psec;
12265 sec = *psec;
12266 }
8517fae7
AM
12267 rel->r_addend -= relocation;
12268 rel->r_addend += sec->output_section->vma + sec->output_offset;
f8df10f4
JJ
12269 }
12270 return relocation;
12271}
c629eae0
JJ
12272
12273bfd_vma
217aa764
AM
12274_bfd_elf_rel_local_sym (bfd *abfd,
12275 Elf_Internal_Sym *sym,
12276 asection **psec,
12277 bfd_vma addend)
47d9a591 12278{
c629eae0
JJ
12279 asection *sec = *psec;
12280
6835821b 12281 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
c629eae0
JJ
12282 return sym->st_value + addend;
12283
12284 return _bfd_merged_section_offset (abfd, psec,
65765700 12285 elf_section_data (sec)->sec_info,
753731ee 12286 sym->st_value + addend);
c629eae0
JJ
12287}
12288
37b01f6a
DG
12289/* Adjust an address within a section. Given OFFSET within SEC, return
12290 the new offset within the section, based upon changes made to the
12291 section. Returns -1 if the offset is now invalid.
12292 The offset (in abnd out) is in target sized bytes, however big a
12293 byte may be. */
12294
c629eae0 12295bfd_vma
217aa764 12296_bfd_elf_section_offset (bfd *abfd,
92e4ec35 12297 struct bfd_link_info *info,
217aa764
AM
12298 asection *sec,
12299 bfd_vma offset)
c629eae0 12300{
68bfbfcc 12301 switch (sec->sec_info_type)
65765700 12302 {
dbaa2011 12303 case SEC_INFO_TYPE_STABS:
eea6121a
AM
12304 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
12305 offset);
dbaa2011 12306 case SEC_INFO_TYPE_EH_FRAME:
92e4ec35 12307 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
37b01f6a 12308
65765700 12309 default:
310fd250
L
12310 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
12311 {
37b01f6a 12312 /* Reverse the offset. */
310fd250
L
12313 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12314 bfd_size_type address_size = bed->s->arch_size / 8;
37b01f6a
DG
12315
12316 /* address_size and sec->size are in octets. Convert
12317 to bytes before subtracting the original offset. */
61826503 12318 offset = ((sec->size - address_size)
bb294208 12319 / bfd_octets_per_byte (abfd, sec) - offset);
310fd250 12320 }
65765700
JJ
12321 return offset;
12322 }
c629eae0 12323}
3333a7c3
RM
12324\f
12325/* Create a new BFD as if by bfd_openr. Rather than opening a file,
12326 reconstruct an ELF file by reading the segments out of remote memory
12327 based on the ELF file header at EHDR_VMA and the ELF program headers it
12328 points to. If not null, *LOADBASEP is filled in with the difference
12329 between the VMAs from which the segments were read, and the VMAs the
12330 file headers (and hence BFD's idea of each section's VMA) put them at.
12331
12332 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12333 remote memory at target address VMA into the local buffer at MYADDR; it
12334 should return zero on success or an `errno' code on failure. TEMPL must
12335 be a BFD for an ELF target with the word size and byte order found in
12336 the remote memory. */
12337
12338bfd *
217aa764
AM
12339bfd_elf_bfd_from_remote_memory
12340 (bfd *templ,
12341 bfd_vma ehdr_vma,
f0a5d95a 12342 bfd_size_type size,
217aa764 12343 bfd_vma *loadbasep,
fe78531d 12344 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
3333a7c3
RM
12345{
12346 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
5979d6b6 12347 (templ, ehdr_vma, size, loadbasep, target_read_memory);
3333a7c3 12348}
4c45e5c9
JJ
12349\f
12350long
c9727e01
AM
12351_bfd_elf_get_synthetic_symtab (bfd *abfd,
12352 long symcount ATTRIBUTE_UNUSED,
12353 asymbol **syms ATTRIBUTE_UNUSED,
8615f3f2 12354 long dynsymcount,
c9727e01
AM
12355 asymbol **dynsyms,
12356 asymbol **ret)
4c45e5c9
JJ
12357{
12358 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12359 asection *relplt;
12360 asymbol *s;
12361 const char *relplt_name;
12362 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
12363 arelent *p;
12364 long count, i, n;
12365 size_t size;
12366 Elf_Internal_Shdr *hdr;
12367 char *names;
12368 asection *plt;
12369
8615f3f2
AM
12370 *ret = NULL;
12371
90e3cdf2
JJ
12372 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
12373 return 0;
12374
8615f3f2
AM
12375 if (dynsymcount <= 0)
12376 return 0;
12377
4c45e5c9
JJ
12378 if (!bed->plt_sym_val)
12379 return 0;
12380
12381 relplt_name = bed->relplt_name;
12382 if (relplt_name == NULL)
d35fd659 12383 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
4c45e5c9
JJ
12384 relplt = bfd_get_section_by_name (abfd, relplt_name);
12385 if (relplt == NULL)
12386 return 0;
12387
12388 hdr = &elf_section_data (relplt)->this_hdr;
12389 if (hdr->sh_link != elf_dynsymtab (abfd)
12390 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
12391 return 0;
12392
12393 plt = bfd_get_section_by_name (abfd, ".plt");
12394 if (plt == NULL)
12395 return 0;
12396
12397 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
c9727e01 12398 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
4c45e5c9
JJ
12399 return -1;
12400
eea6121a 12401 count = relplt->size / hdr->sh_entsize;
4c45e5c9
JJ
12402 size = count * sizeof (asymbol);
12403 p = relplt->relocation;
cb53bf42 12404 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
041de40d
AM
12405 {
12406 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
12407 if (p->addend != 0)
12408 {
12409#ifdef BFD64
12410 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
12411#else
12412 size += sizeof ("+0x") - 1 + 8;
12413#endif
12414 }
12415 }
4c45e5c9 12416
a50b1753 12417 s = *ret = (asymbol *) bfd_malloc (size);
4c45e5c9
JJ
12418 if (s == NULL)
12419 return -1;
12420
12421 names = (char *) (s + count);
12422 p = relplt->relocation;
12423 n = 0;
cb53bf42 12424 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
4c45e5c9
JJ
12425 {
12426 size_t len;
12427 bfd_vma addr;
12428
12429 addr = bed->plt_sym_val (i, plt, p);
12430 if (addr == (bfd_vma) -1)
12431 continue;
12432
12433 *s = **p->sym_ptr_ptr;
65a7a66f
AM
12434 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
12435 we are defining a symbol, ensure one of them is set. */
12436 if ((s->flags & BSF_LOCAL) == 0)
12437 s->flags |= BSF_GLOBAL;
6ba2a415 12438 s->flags |= BSF_SYNTHETIC;
4c45e5c9
JJ
12439 s->section = plt;
12440 s->value = addr - plt->vma;
12441 s->name = names;
8f39ba8e 12442 s->udata.p = NULL;
4c45e5c9
JJ
12443 len = strlen ((*p->sym_ptr_ptr)->name);
12444 memcpy (names, (*p->sym_ptr_ptr)->name, len);
12445 names += len;
041de40d
AM
12446 if (p->addend != 0)
12447 {
1d770845 12448 char buf[30], *a;
d324f6d6 12449
041de40d
AM
12450 memcpy (names, "+0x", sizeof ("+0x") - 1);
12451 names += sizeof ("+0x") - 1;
1d770845
L
12452 bfd_sprintf_vma (abfd, buf, p->addend);
12453 for (a = buf; *a == '0'; ++a)
12454 ;
12455 len = strlen (a);
12456 memcpy (names, a, len);
12457 names += len;
041de40d 12458 }
4c45e5c9
JJ
12459 memcpy (names, "@plt", sizeof ("@plt"));
12460 names += sizeof ("@plt");
8f39ba8e 12461 ++s, ++n;
4c45e5c9
JJ
12462 }
12463
12464 return n;
12465}
3d7f7666 12466
821e6ff6
AM
12467/* It is only used by x86-64 so far.
12468 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
7eacd66b
AM
12469 but current usage would allow all of _bfd_std_section to be zero. */
12470static const asymbol lcomm_sym
12471 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
3b22753a 12472asection _bfd_elf_large_com_section
7eacd66b 12473 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
821e6ff6 12474 "LARGE_COMMON", 0, SEC_IS_COMMON);
ecca9871 12475
cc364be6
AM
12476bfd_boolean
12477_bfd_elf_final_write_processing (bfd *abfd)
06f44071
AM
12478{
12479 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
d1036acb
L
12480
12481 i_ehdrp = elf_elfheader (abfd);
12482
06f44071
AM
12483 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12484 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
d8045f23 12485
df3a023b 12486 /* Set the osabi field to ELFOSABI_GNU if the binary contains
99fabbc9
JL
12487 SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
12488 or STB_GNU_UNIQUE binding. */
cc364be6
AM
12489 if (elf_tdata (abfd)->has_gnu_osabi != 0)
12490 {
12491 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12492 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
12493 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
12494 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
12495 {
12496 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
99fabbc9
JL
12497 _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
12498 "and FreeBSD targets"));
cc364be6 12499 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
99fabbc9
JL
12500 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
12501 "only by GNU and FreeBSD targets"));
cc364be6 12502 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
99fabbc9
JL
12503 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
12504 "only by GNU and FreeBSD targets"));
12505 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
12506 _bfd_error_handler (_("GNU_RETAIN section is supported "
12507 "only by GNU and FreeBSD targets"));
9aea1e31 12508 bfd_set_error (bfd_error_sorry);
cc364be6
AM
12509 return FALSE;
12510 }
12511 }
12512 return TRUE;
d1036acb 12513}
fcb93ecf
PB
12514
12515
12516/* Return TRUE for ELF symbol types that represent functions.
12517 This is the default version of this function, which is sufficient for
d8045f23 12518 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
fcb93ecf
PB
12519
12520bfd_boolean
12521_bfd_elf_is_function_type (unsigned int type)
12522{
d8045f23
NC
12523 return (type == STT_FUNC
12524 || type == STT_GNU_IFUNC);
fcb93ecf 12525}
9f296da3 12526
aef36ac1
AM
12527/* If the ELF symbol SYM might be a function in SEC, return the
12528 function size and set *CODE_OFF to the function's entry point,
12529 otherwise return zero. */
9f296da3 12530
aef36ac1
AM
12531bfd_size_type
12532_bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
12533 bfd_vma *code_off)
9f296da3 12534{
aef36ac1
AM
12535 bfd_size_type size;
12536
ff9e0f5b 12537 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
aef36ac1
AM
12538 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
12539 || sym->section != sec)
12540 return 0;
ff9e0f5b 12541
ff9e0f5b 12542 *code_off = sym->value;
aef36ac1
AM
12543 size = 0;
12544 if (!(sym->flags & BSF_SYNTHETIC))
12545 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
12546 if (size == 0)
12547 size = 1;
12548 return size;
9f296da3 12549}
a8e14f4c
NC
12550
12551/* Set to non-zero to enable some debug messages. */
12552#define DEBUG_SECONDARY_RELOCS 0
12553
12554/* An internal-to-the-bfd-library only section type
12555 used to indicate a cached secondary reloc section. */
12556#define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
12557
12558/* Create a BFD section to hold a secondary reloc section. */
12559
12560bfd_boolean
12561_bfd_elf_init_secondary_reloc_section (bfd * abfd,
12562 Elf_Internal_Shdr *hdr,
12563 const char * name,
12564 unsigned int shindex)
12565{
12566 /* We only support RELA secondary relocs. */
12567 if (hdr->sh_type != SHT_RELA)
12568 return FALSE;
12569
12570#if DEBUG_SECONDARY_RELOCS
12571 fprintf (stderr, "secondary reloc section %s encountered\n", name);
12572#endif
12573 hdr->sh_type = SHT_SECONDARY_RELOC;
12574 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
12575}
12576
12577/* Read in any secondary relocs associated with SEC. */
12578
12579bfd_boolean
f60742b2
NC
12580_bfd_elf_slurp_secondary_reloc_section (bfd * abfd,
12581 asection * sec,
12582 asymbol ** symbols,
12583 bfd_boolean dynamic)
a8e14f4c
NC
12584{
12585 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
12586 asection * relsec;
12587 bfd_boolean result = TRUE;
12588 bfd_vma (*r_sym) (bfd_vma);
12589
12590#if BFD_DEFAULT_TARGET_SIZE > 32
12591 if (bfd_arch_bits_per_address (abfd) != 32)
12592 r_sym = elf64_r_sym;
12593 else
12594#endif
12595 r_sym = elf32_r_sym;
12596
12597 /* Discover if there are any secondary reloc sections
12598 associated with SEC. */
12599 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
12600 {
12601 Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
12602
12603 if (hdr->sh_type == SHT_SECONDARY_RELOC
8642dafa
AM
12604 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
12605 && (hdr->sh_entsize == ebd->s->sizeof_rel
12606 || hdr->sh_entsize == ebd->s->sizeof_rela))
a8e14f4c
NC
12607 {
12608 bfd_byte * native_relocs;
12609 bfd_byte * native_reloc;
12610 arelent * internal_relocs;
12611 arelent * internal_reloc;
12612 unsigned int i;
12613 unsigned int entsize;
12614 unsigned int symcount;
12615 unsigned int reloc_count;
12616 size_t amt;
12617
12618 if (ebd->elf_info_to_howto == NULL)
12619 return FALSE;
12620
12621#if DEBUG_SECONDARY_RELOCS
12622 fprintf (stderr, "read secondary relocs for %s from %s\n",
12623 sec->name, relsec->name);
12624#endif
12625 entsize = hdr->sh_entsize;
12626
12627 native_relocs = bfd_malloc (hdr->sh_size);
12628 if (native_relocs == NULL)
12629 {
12630 result = FALSE;
12631 continue;
12632 }
12633
12634 reloc_count = NUM_SHDR_ENTRIES (hdr);
12635 if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
12636 {
ecbbbdba 12637 free (native_relocs);
a8e14f4c
NC
12638 bfd_set_error (bfd_error_file_too_big);
12639 result = FALSE;
12640 continue;
12641 }
12642
12643 internal_relocs = (arelent *) bfd_alloc (abfd, amt);
12644 if (internal_relocs == NULL)
12645 {
12646 free (native_relocs);
12647 result = FALSE;
12648 continue;
12649 }
12650
12651 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
12652 || (bfd_bread (native_relocs, hdr->sh_size, abfd)
12653 != hdr->sh_size))
12654 {
12655 free (native_relocs);
ecbbbdba
NC
12656 /* The internal_relocs will be freed when
12657 the memory for the bfd is released. */
a8e14f4c
NC
12658 result = FALSE;
12659 continue;
12660 }
12661
f60742b2
NC
12662 if (dynamic)
12663 symcount = bfd_get_dynamic_symcount (abfd);
12664 else
12665 symcount = bfd_get_symcount (abfd);
a8e14f4c
NC
12666
12667 for (i = 0, internal_reloc = internal_relocs,
12668 native_reloc = native_relocs;
12669 i < reloc_count;
12670 i++, internal_reloc++, native_reloc += entsize)
12671 {
12672 bfd_boolean res;
12673 Elf_Internal_Rela rela;
12674
8ee54925
NC
12675 if (entsize == ebd->s->sizeof_rel)
12676 ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
12677 else /* entsize == ebd->s->sizeof_rela */
12678 ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
a8e14f4c
NC
12679
12680 /* The address of an ELF reloc is section relative for an object
12681 file, and absolute for an executable file or shared library.
12682 The address of a normal BFD reloc is always section relative,
12683 and the address of a dynamic reloc is absolute.. */
12684 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
12685 internal_reloc->address = rela.r_offset;
12686 else
12687 internal_reloc->address = rela.r_offset - sec->vma;
12688
12689 if (r_sym (rela.r_info) == STN_UNDEF)
12690 {
12691 /* FIXME: This and the error case below mean that we
12692 have a symbol on relocs that is not elf_symbol_type. */
12693 internal_reloc->sym_ptr_ptr =
12694 bfd_abs_section_ptr->symbol_ptr_ptr;
12695 }
12696 else if (r_sym (rela.r_info) > symcount)
12697 {
12698 _bfd_error_handler
12699 /* xgettext:c-format */
12700 (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
12701 abfd, sec, i, (long) r_sym (rela.r_info));
12702 bfd_set_error (bfd_error_bad_value);
12703 internal_reloc->sym_ptr_ptr =
12704 bfd_abs_section_ptr->symbol_ptr_ptr;
12705 result = FALSE;
12706 }
12707 else
12708 {
12709 asymbol **ps;
12710
12711 ps = symbols + r_sym (rela.r_info) - 1;
a8e14f4c
NC
12712 internal_reloc->sym_ptr_ptr = ps;
12713 /* Make sure that this symbol is not removed by strip. */
12714 (*ps)->flags |= BSF_KEEP;
12715 }
12716
12717 internal_reloc->addend = rela.r_addend;
12718
12719 res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
12720 if (! res || internal_reloc->howto == NULL)
12721 {
12722#if DEBUG_SECONDARY_RELOCS
12723 fprintf (stderr, "there is no howto associated with reloc %lx\n",
12724 rela.r_info);
12725#endif
12726 result = FALSE;
12727 }
12728 }
12729
12730 free (native_relocs);
12731 /* Store the internal relocs. */
12732 elf_section_data (relsec)->sec_info = internal_relocs;
12733 }
12734 }
12735
12736 return result;
12737}
12738
12739/* Set the ELF section header fields of an output secondary reloc section. */
12740
12741bfd_boolean
12742_bfd_elf_copy_special_section_fields (const bfd * ibfd ATTRIBUTE_UNUSED,
12743 bfd * obfd ATTRIBUTE_UNUSED,
12744 const Elf_Internal_Shdr * isection,
12745 Elf_Internal_Shdr * osection)
12746{
12747 asection * isec;
12748 asection * osec;
44466e45 12749 struct bfd_elf_section_data * esd;
a8e14f4c
NC
12750
12751 if (isection == NULL)
12752 return FALSE;
12753
12754 if (isection->sh_type != SHT_SECONDARY_RELOC)
12755 return TRUE;
12756
12757 isec = isection->bfd_section;
12758 if (isec == NULL)
12759 return FALSE;
12760
12761 osec = osection->bfd_section;
12762 if (osec == NULL)
12763 return FALSE;
12764
44466e45
NC
12765 esd = elf_section_data (osec);
12766 BFD_ASSERT (esd->sec_info == NULL);
12767 esd->sec_info = elf_section_data (isec)->sec_info;
a8e14f4c
NC
12768 osection->sh_type = SHT_RELA;
12769 osection->sh_link = elf_onesymtab (obfd);
12770 if (osection->sh_link == 0)
12771 {
12772 /* There is no symbol table - we are hosed... */
12773 _bfd_error_handler
12774 /* xgettext:c-format */
12775 (_("%pB(%pA): link section cannot be set because the output file does not have a symbol table"),
12776 obfd, osec);
12777 bfd_set_error (bfd_error_bad_value);
12778 return FALSE;
12779 }
12780
12781 /* Find the output section that corresponds to the isection's sh_info link. */
327ef784
NC
12782 if (isection->sh_info == 0
12783 || isection->sh_info >= elf_numsections (ibfd))
12784 {
12785 _bfd_error_handler
12786 /* xgettext:c-format */
12787 (_("%pB(%pA): info section index is invalid"),
12788 obfd, osec);
12789 bfd_set_error (bfd_error_bad_value);
12790 return FALSE;
12791 }
12792
a8e14f4c
NC
12793 isection = elf_elfsections (ibfd)[isection->sh_info];
12794
327ef784
NC
12795 if (isection == NULL
12796 || isection->bfd_section == NULL
12797 || isection->bfd_section->output_section == NULL)
12798 {
12799 _bfd_error_handler
12800 /* xgettext:c-format */
12801 (_("%pB(%pA): info section index cannot be set because the section is not in the output"),
12802 obfd, osec);
12803 bfd_set_error (bfd_error_bad_value);
12804 return FALSE;
12805 }
12806
44466e45
NC
12807 esd = elf_section_data (isection->bfd_section->output_section);
12808 BFD_ASSERT (esd != NULL);
12809 osection->sh_info = esd->this_idx;
12810 esd->has_secondary_relocs = TRUE;
a8e14f4c
NC
12811#if DEBUG_SECONDARY_RELOCS
12812 fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
12813 osec->name, osection->sh_link, osection->sh_info);
44466e45
NC
12814 fprintf (stderr, "mark section %s as having secondary relocs\n",
12815 bfd_section_name (isection->bfd_section->output_section));
a8e14f4c
NC
12816#endif
12817
12818 return TRUE;
12819}
12820
44466e45
NC
12821/* Write out a secondary reloc section.
12822
12823 FIXME: Currently this function can result in a serious performance penalty
12824 for files with secondary relocs and lots of sections. The proper way to
12825 fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
12826 relocs together and then to have this function just walk that chain. */
a8e14f4c
NC
12827
12828bfd_boolean
12829_bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
12830{
12831 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
12832 bfd_vma addr_offset;
12833 asection * relsec;
12834 bfd_vma (*r_info) (bfd_vma, bfd_vma);
ac4bf06c
NC
12835 bfd_boolean result = TRUE;
12836
12837 if (sec == NULL)
12838 return FALSE;
a8e14f4c
NC
12839
12840#if BFD_DEFAULT_TARGET_SIZE > 32
12841 if (bfd_arch_bits_per_address (abfd) != 32)
12842 r_info = elf64_r_info;
12843 else
12844#endif
12845 r_info = elf32_r_info;
12846
a8e14f4c
NC
12847 /* The address of an ELF reloc is section relative for an object
12848 file, and absolute for an executable file or shared library.
12849 The address of a BFD reloc is always section relative. */
12850 addr_offset = 0;
12851 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
12852 addr_offset = sec->vma;
12853
12854 /* Discover if there are any secondary reloc sections
12855 associated with SEC. */
12856 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
12857 {
12858 const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
12859 Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
12860
12861 if (hdr->sh_type == SHT_RELA
12862 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
12863 {
12864 asymbol * last_sym;
12865 int last_sym_idx;
12866 unsigned int reloc_count;
12867 unsigned int idx;
8ee54925 12868 unsigned int entsize;
a8e14f4c
NC
12869 arelent * src_irel;
12870 bfd_byte * dst_rela;
12871
ac4bf06c
NC
12872 if (hdr->contents != NULL)
12873 {
12874 _bfd_error_handler
12875 /* xgettext:c-format */
12876 (_("%pB(%pA): error: secondary reloc section processed twice"),
12877 abfd, relsec);
12878 bfd_set_error (bfd_error_bad_value);
12879 result = FALSE;
12880 continue;
12881 }
a8e14f4c 12882
8ee54925
NC
12883 entsize = hdr->sh_entsize;
12884 if (entsize == 0)
ac267c75
NC
12885 {
12886 _bfd_error_handler
12887 /* xgettext:c-format */
12888 (_("%pB(%pA): error: secondary reloc section has zero sized entries"),
12889 abfd, relsec);
12890 bfd_set_error (bfd_error_bad_value);
12891 result = FALSE;
12892 continue;
12893 }
8ee54925
NC
12894 else if (entsize != ebd->s->sizeof_rel
12895 && entsize != ebd->s->sizeof_rela)
12896 {
12897 _bfd_error_handler
12898 /* xgettext:c-format */
12899 (_("%pB(%pA): error: secondary reloc section has non-standard sized entries"),
12900 abfd, relsec);
12901 bfd_set_error (bfd_error_bad_value);
12902 result = FALSE;
12903 continue;
12904 }
ac267c75 12905
8ee54925 12906 reloc_count = hdr->sh_size / entsize;
ac4bf06c
NC
12907 if (reloc_count <= 0)
12908 {
12909 _bfd_error_handler
12910 /* xgettext:c-format */
12911 (_("%pB(%pA): error: secondary reloc section is empty!"),
12912 abfd, relsec);
12913 bfd_set_error (bfd_error_bad_value);
12914 result = FALSE;
12915 continue;
12916 }
a8e14f4c
NC
12917
12918 hdr->contents = bfd_alloc (abfd, hdr->sh_size);
12919 if (hdr->contents == NULL)
12920 continue;
12921
12922#if DEBUG_SECONDARY_RELOCS
12923 fprintf (stderr, "write %u secondary relocs for %s from %s\n",
12924 reloc_count, sec->name, relsec->name);
12925#endif
12926 last_sym = NULL;
12927 last_sym_idx = 0;
12928 dst_rela = hdr->contents;
12929 src_irel = (arelent *) esd->sec_info;
ac4bf06c
NC
12930 if (src_irel == NULL)
12931 {
12932 _bfd_error_handler
12933 /* xgettext:c-format */
12934 (_("%pB(%pA): error: internal relocs missing for secondary reloc section"),
12935 abfd, relsec);
12936 bfd_set_error (bfd_error_bad_value);
12937 result = FALSE;
12938 continue;
12939 }
a8e14f4c 12940
8ee54925 12941 for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
a8e14f4c
NC
12942 {
12943 Elf_Internal_Rela src_rela;
12944 arelent *ptr;
12945 asymbol *sym;
12946 int n;
12947
12948 ptr = src_irel + idx;
ac4bf06c
NC
12949 if (ptr == NULL)
12950 {
12951 _bfd_error_handler
12952 /* xgettext:c-format */
12953 (_("%pB(%pA): error: reloc table entry %u is empty"),
12954 abfd, relsec, idx);
12955 bfd_set_error (bfd_error_bad_value);
12956 result = FALSE;
12957 break;
12958 }
a8e14f4c 12959
ac4bf06c
NC
12960 if (ptr->sym_ptr_ptr == NULL)
12961 {
12962 /* FIXME: Is this an error ? */
12963 n = 0;
12964 }
a8e14f4c
NC
12965 else
12966 {
ac4bf06c
NC
12967 sym = *ptr->sym_ptr_ptr;
12968
12969 if (sym == last_sym)
12970 n = last_sym_idx;
12971 else
a8e14f4c 12972 {
ac4bf06c
NC
12973 n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
12974 if (n < 0)
12975 {
12976 _bfd_error_handler
12977 /* xgettext:c-format */
12978 (_("%pB(%pA): error: secondary reloc %u references a missing symbol"),
12979 abfd, relsec, idx);
12980 bfd_set_error (bfd_error_bad_value);
12981 result = FALSE;
12982 n = 0;
12983 }
12984
12985 last_sym = sym;
12986 last_sym_idx = n;
a8e14f4c 12987 }
a8e14f4c 12988
ac4bf06c
NC
12989 if (sym->the_bfd != NULL
12990 && sym->the_bfd->xvec != abfd->xvec
12991 && ! _bfd_elf_validate_reloc (abfd, ptr))
12992 {
12993 _bfd_error_handler
12994 /* xgettext:c-format */
12995 (_("%pB(%pA): error: secondary reloc %u references a deleted symbol"),
12996 abfd, relsec, idx);
12997 bfd_set_error (bfd_error_bad_value);
12998 result = FALSE;
12999 n = 0;
13000 }
a8e14f4c
NC
13001 }
13002
ac4bf06c 13003 src_rela.r_offset = ptr->address + addr_offset;
a8e14f4c
NC
13004 if (ptr->howto == NULL)
13005 {
ac4bf06c
NC
13006 _bfd_error_handler
13007 /* xgettext:c-format */
13008 (_("%pB(%pA): error: secondary reloc %u is of an unknown type"),
13009 abfd, relsec, idx);
13010 bfd_set_error (bfd_error_bad_value);
13011 result = FALSE;
13012 src_rela.r_info = r_info (0, 0);
a8e14f4c 13013 }
ac4bf06c
NC
13014 else
13015 src_rela.r_info = r_info (n, ptr->howto->type);
a8e14f4c 13016 src_rela.r_addend = ptr->addend;
8ee54925
NC
13017
13018 if (entsize == ebd->s->sizeof_rel)
13019 ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
13020 else /* entsize == ebd->s->sizeof_rela */
13021 ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
a8e14f4c
NC
13022 }
13023 }
13024 }
13025
ac4bf06c 13026 return result;
a8e14f4c 13027}
This page took 2.861316 seconds and 4 git commands to generate.