Add translations for various sub-directories
[binutils-gdb.git] / bfd / elf.c
blob0fa580d8d78421b31bc2d2a79dac42b776fbefb2
1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2025 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
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
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
24 SECTION
25 ELF backends
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).
31 Documentation of the internals of the support code still needs
32 to be written. The code is changing quickly enough that we
33 haven't bothered yet. */
35 /* For sparc64-cross-sparc32. */
36 #define _SYSCALL32
37 #include "sysdep.h"
38 #include <limits.h>
39 #include "bfd.h"
40 #include "bfdlink.h"
41 #include "libbfd.h"
42 #define ARCH_SIZE 0
43 #include "elf-bfd.h"
44 #include "libiberty.h"
45 #include "safe-ctype.h"
46 #include "elf-linux-core.h"
48 #ifdef CORE_HEADER
49 #include CORE_HEADER
50 #endif
52 static int elf_sort_sections (const void *, const void *);
53 static bool assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
54 static bool swap_out_syms (bfd *, struct elf_strtab_hash **, int,
55 struct bfd_link_info *);
56 static bool elf_parse_notes (bfd *abfd, char *buf, size_t size,
57 file_ptr offset, size_t align);
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. */
63 /* Swap in a Verdef structure. */
65 void
66 _bfd_elf_swap_verdef_in (bfd *abfd,
67 const Elf_External_Verdef *src,
68 Elf_Internal_Verdef *dst)
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);
79 /* Swap out a Verdef structure. */
81 void
82 _bfd_elf_swap_verdef_out (bfd *abfd,
83 const Elf_Internal_Verdef *src,
84 Elf_External_Verdef *dst)
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);
95 /* Swap in a Verdaux structure. */
97 void
98 _bfd_elf_swap_verdaux_in (bfd *abfd,
99 const Elf_External_Verdaux *src,
100 Elf_Internal_Verdaux *dst)
102 dst->vda_name = H_GET_32 (abfd, src->vda_name);
103 dst->vda_next = H_GET_32 (abfd, src->vda_next);
106 /* Swap out a Verdaux structure. */
108 void
109 _bfd_elf_swap_verdaux_out (bfd *abfd,
110 const Elf_Internal_Verdaux *src,
111 Elf_External_Verdaux *dst)
113 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
114 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
117 /* Swap in a Verneed structure. */
119 void
120 _bfd_elf_swap_verneed_in (bfd *abfd,
121 const Elf_External_Verneed *src,
122 Elf_Internal_Verneed *dst)
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);
131 /* Swap out a Verneed structure. */
133 void
134 _bfd_elf_swap_verneed_out (bfd *abfd,
135 const Elf_Internal_Verneed *src,
136 Elf_External_Verneed *dst)
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);
145 /* Swap in a Vernaux structure. */
147 void
148 _bfd_elf_swap_vernaux_in (bfd *abfd,
149 const Elf_External_Vernaux *src,
150 Elf_Internal_Vernaux *dst)
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);
159 /* Swap out a Vernaux structure. */
161 void
162 _bfd_elf_swap_vernaux_out (bfd *abfd,
163 const Elf_Internal_Vernaux *src,
164 Elf_External_Vernaux *dst)
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);
173 /* Swap in a Versym structure. */
175 void
176 _bfd_elf_swap_versym_in (bfd *abfd,
177 const Elf_External_Versym *src,
178 Elf_Internal_Versym *dst)
180 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
183 /* Swap out a Versym structure. */
185 void
186 _bfd_elf_swap_versym_out (bfd *abfd,
187 const Elf_Internal_Versym *src,
188 Elf_External_Versym *dst)
190 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
193 /* Standard ELF hash function. Do not change this function; you will
194 cause invalid hash tables to be generated. */
196 unsigned long
197 bfd_elf_hash (const char *namearg)
199 uint32_t h = 0;
201 for (const unsigned char *name = (const unsigned char *) namearg;
202 *name; name++)
204 h = (h << 4) + *name;
205 h ^= (h >> 24) & 0xf0;
207 return h & 0x0fffffff;
210 /* DT_GNU_HASH hash function. Do not change this function; you will
211 cause invalid hash tables to be generated. */
213 unsigned long
214 bfd_elf_gnu_hash (const char *namearg)
216 uint32_t h = 5381;
218 for (const unsigned char *name = (const unsigned char *) namearg;
219 *name; name++)
220 h = (h << 5) + h + *name;
221 return h;
224 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
225 the object_id field of an elf_obj_tdata field set. */
226 bool
227 bfd_elf_allocate_object (bfd *abfd,
228 size_t object_size)
230 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
231 abfd->tdata.any = bfd_zalloc (abfd, object_size);
232 if (abfd->tdata.any == NULL)
233 return false;
235 elf_object_id (abfd) = get_elf_backend_data (abfd)->target_id;
236 if (abfd->direction != read_direction)
238 struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
239 if (o == NULL)
240 return false;
241 elf_tdata (abfd)->o = o;
242 elf_program_header_size (abfd) = (bfd_size_type) -1;
244 return true;
248 bool
249 bfd_elf_make_object (bfd *abfd)
251 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata));
254 bool
255 bfd_elf_mkcorefile (bfd *abfd)
257 /* I think this can be done just like an object file. */
258 if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
259 return false;
260 elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
261 return elf_tdata (abfd)->core != NULL;
264 char *
265 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
267 Elf_Internal_Shdr **i_shdrp;
268 bfd_byte *shstrtab = NULL;
269 file_ptr offset;
270 bfd_size_type shstrtabsize;
272 i_shdrp = elf_elfsections (abfd);
273 if (i_shdrp == 0
274 || shindex >= elf_numsections (abfd)
275 || i_shdrp[shindex] == 0)
276 return NULL;
278 shstrtab = i_shdrp[shindex]->contents;
279 if (shstrtab == NULL)
281 /* No cached one, attempt to read, and cache what we read. */
282 offset = i_shdrp[shindex]->sh_offset;
283 shstrtabsize = i_shdrp[shindex]->sh_size;
285 if (shstrtabsize == 0
286 || bfd_seek (abfd, offset, SEEK_SET) != 0
287 || (shstrtab = _bfd_mmap_persistent (abfd, shstrtabsize)) == NULL)
289 /* Once we've failed to read it, make sure we don't keep
290 trying. Otherwise, we'll keep allocating space for
291 the string table over and over. */
292 i_shdrp[shindex]->sh_size = 0;
294 else if (shstrtab[shstrtabsize - 1] != 0)
296 /* It is an error if a string table isn't terminated. */
297 _bfd_error_handler
298 /* xgettext:c-format */
299 (_("%pB: string table [%u] is corrupt"), abfd, shindex);
300 shstrtab[shstrtabsize - 1] = 0;
302 i_shdrp[shindex]->contents = shstrtab;
304 return (char *) shstrtab;
307 char *
308 bfd_elf_string_from_elf_section (bfd *abfd,
309 unsigned int shindex,
310 unsigned int strindex)
312 Elf_Internal_Shdr *hdr;
314 if (strindex == 0)
315 return "";
317 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
318 return NULL;
320 hdr = elf_elfsections (abfd)[shindex];
322 if (hdr->contents == NULL)
324 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
326 /* PR 17512: file: f057ec89. */
327 /* xgettext:c-format */
328 _bfd_error_handler (_("%pB: attempt to load strings from"
329 " a non-string section (number %d)"),
330 abfd, shindex);
331 return NULL;
334 if (bfd_elf_get_str_section (abfd, shindex) == NULL)
335 return NULL;
337 else
339 /* PR 24273: The string section's contents may have already
340 been loaded elsewhere, eg because a corrupt file has the
341 string section index in the ELF header pointing at a group
342 section. So be paranoid, and test that the last byte of
343 the section is zero. */
344 if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
345 return NULL;
348 if (strindex >= hdr->sh_size)
350 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
351 _bfd_error_handler
352 /* xgettext:c-format */
353 (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
354 abfd, strindex, (uint64_t) hdr->sh_size,
355 (shindex == shstrndx && strindex == hdr->sh_name
356 ? ".shstrtab"
357 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
358 return NULL;
361 return ((char *) hdr->contents) + strindex;
364 /* Read and convert symbols to internal format.
365 SYMCOUNT specifies the number of symbols to read, starting from
366 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
367 are non-NULL, they are used to store the internal symbols, external
368 symbols, and symbol section index extensions, respectively.
369 Returns a pointer to the internal symbol buffer (malloced if necessary)
370 or NULL if there were no symbols or some kind of problem. */
372 Elf_Internal_Sym *
373 bfd_elf_get_elf_syms (bfd *ibfd,
374 Elf_Internal_Shdr *symtab_hdr,
375 size_t symcount,
376 size_t symoffset,
377 Elf_Internal_Sym *intsym_buf,
378 void *extsym_buf,
379 Elf_External_Sym_Shndx *extshndx_buf)
381 Elf_Internal_Shdr *shndx_hdr;
382 void *alloc_ext;
383 const bfd_byte *esym;
384 Elf_External_Sym_Shndx *alloc_extshndx;
385 Elf_External_Sym_Shndx *shndx;
386 Elf_Internal_Sym *alloc_intsym;
387 Elf_Internal_Sym *isym;
388 Elf_Internal_Sym *isymend;
389 const struct elf_backend_data *bed;
390 size_t extsym_size;
391 size_t amt;
392 file_ptr pos;
394 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
395 abort ();
397 if (symcount == 0)
398 return intsym_buf;
400 if (elf_use_dt_symtab_p (ibfd))
402 /* Use dynamic symbol table. */
403 if (elf_tdata (ibfd)->dt_symtab_count != symcount + symoffset)
405 bfd_set_error (bfd_error_invalid_operation);
406 return NULL;
408 return elf_tdata (ibfd)->dt_symtab + symoffset;
411 /* Normal syms might have section extension entries. */
412 shndx_hdr = NULL;
413 if (elf_symtab_shndx_list (ibfd) != NULL)
415 elf_section_list * entry;
416 Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
418 /* Find an index section that is linked to this symtab section. */
419 for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
421 /* PR 20063. */
422 if (entry->hdr.sh_link >= elf_numsections (ibfd))
423 continue;
425 if (sections[entry->hdr.sh_link] == symtab_hdr)
427 shndx_hdr = & entry->hdr;
428 break;
432 if (shndx_hdr == NULL)
434 if (symtab_hdr == &elf_symtab_hdr (ibfd))
435 /* Not really accurate, but this was how the old code used
436 to work. */
437 shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr;
438 /* Otherwise we do nothing. The assumption is that
439 the index table will not be needed. */
443 /* Read the symbols. */
444 alloc_ext = NULL;
445 alloc_extshndx = NULL;
446 alloc_intsym = NULL;
447 bed = get_elf_backend_data (ibfd);
448 extsym_size = bed->s->sizeof_sym;
449 if (_bfd_mul_overflow (symcount, extsym_size, &amt))
451 bfd_set_error (bfd_error_file_too_big);
452 return NULL;
454 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
455 size_t alloc_ext_size = amt;
456 if (bfd_seek (ibfd, pos, SEEK_SET) != 0
457 || !_bfd_mmap_read_temporary (&extsym_buf, &alloc_ext_size,
458 &alloc_ext, ibfd, false))
460 intsym_buf = NULL;
461 goto out2;
464 size_t alloc_extshndx_size = 0;
465 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
466 extshndx_buf = NULL;
467 else
469 if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
471 bfd_set_error (bfd_error_file_too_big);
472 intsym_buf = NULL;
473 goto out1;
475 alloc_extshndx_size = amt;
476 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
477 if (bfd_seek (ibfd, pos, SEEK_SET) != 0
478 || !_bfd_mmap_read_temporary ((void **) &extshndx_buf,
479 &alloc_extshndx_size,
480 (void **) &alloc_extshndx,
481 ibfd, false))
483 intsym_buf = NULL;
484 goto out1;
488 if (intsym_buf == NULL)
490 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
492 bfd_set_error (bfd_error_file_too_big);
493 goto out1;
495 alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
496 intsym_buf = alloc_intsym;
497 if (intsym_buf == NULL)
498 goto out1;
501 /* Convert the symbols to internal form. */
502 isymend = intsym_buf + symcount;
503 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
504 shndx = extshndx_buf;
505 isym < isymend;
506 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
507 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
509 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
510 /* xgettext:c-format */
511 _bfd_error_handler (_("%pB symbol number %lu references"
512 " nonexistent SHT_SYMTAB_SHNDX section"),
513 ibfd, (unsigned long) symoffset);
514 free (alloc_intsym);
515 intsym_buf = NULL;
516 goto out1;
519 out1:
520 _bfd_munmap_temporary (alloc_extshndx, alloc_extshndx_size);
521 out2:
522 _bfd_munmap_temporary (alloc_ext, alloc_ext_size);
524 return intsym_buf;
527 /* Look up a symbol name. */
528 static const char *
529 bfd_elf_sym_name_raw (bfd *abfd,
530 Elf_Internal_Shdr *symtab_hdr,
531 Elf_Internal_Sym *isym)
533 unsigned int iname = isym->st_name;
534 unsigned int shindex = symtab_hdr->sh_link;
536 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
537 /* Check for a bogus st_shndx to avoid crashing. */
538 && isym->st_shndx < elf_numsections (abfd))
540 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
541 shindex = elf_elfheader (abfd)->e_shstrndx;
544 return bfd_elf_string_from_elf_section (abfd, shindex, iname);
547 const char *
548 bfd_elf_sym_name (bfd *abfd,
549 Elf_Internal_Shdr *symtab_hdr,
550 Elf_Internal_Sym *isym,
551 asection *sym_sec)
553 const char *name = bfd_elf_sym_name_raw (abfd, symtab_hdr, isym);
554 if (name == NULL)
555 name = bfd_symbol_error_name;
556 else if (sym_sec && *name == '\0')
557 name = bfd_section_name (sym_sec);
559 return name;
562 /* Return the name of the group signature symbol. Why isn't the
563 signature just a string? */
565 static const char *
566 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
568 Elf_Internal_Shdr *hdr;
569 unsigned char esym[sizeof (Elf64_External_Sym)];
570 Elf_External_Sym_Shndx eshndx;
571 Elf_Internal_Sym isym;
573 /* First we need to ensure the symbol table is available. Make sure
574 that it is a symbol table section. */
575 if (ghdr->sh_link >= elf_numsections (abfd))
576 return NULL;
577 hdr = elf_elfsections (abfd) [ghdr->sh_link];
578 if (hdr->sh_type != SHT_SYMTAB
579 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
580 return NULL;
582 /* Go read the symbol. */
583 hdr = &elf_tdata (abfd)->symtab_hdr;
584 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
585 &isym, esym, &eshndx) == NULL)
586 return NULL;
588 return bfd_elf_sym_name_raw (abfd, hdr, &isym);
591 static bool
592 is_valid_group_section_header (Elf_Internal_Shdr *shdr, size_t minsize)
594 return (shdr->sh_size >= minsize
595 && shdr->sh_entsize == GRP_ENTRY_SIZE
596 && shdr->sh_size % GRP_ENTRY_SIZE == 0
597 && shdr->bfd_section != NULL);
601 /* Set next_in_group, sec_group list pointers, and group names. */
603 static bool
604 process_sht_group_entries (bfd *abfd,
605 Elf_Internal_Shdr *ghdr, unsigned int gidx)
607 unsigned char *contents;
609 /* Read the raw contents. */
610 if (!bfd_malloc_and_get_section (abfd, ghdr->bfd_section, &contents))
612 _bfd_error_handler
613 /* xgettext:c-format */
614 (_("%pB: could not read contents of group [%u]"), abfd, gidx);
615 return false;
618 asection *last_elt = NULL;
619 const char *gname = NULL;
620 unsigned char *p = contents + ghdr->sh_size;
621 while (1)
623 unsigned int idx;
624 Elf_Internal_Shdr *shdr;
625 asection *elt;
627 p -= 4;
628 idx = H_GET_32 (abfd, p);
629 if (p == contents)
631 if ((idx & GRP_COMDAT) != 0)
632 ghdr->bfd_section->flags
633 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
634 break;
637 if (idx == 0
638 || idx >= elf_numsections (abfd)
639 || (shdr = elf_elfsections (abfd)[idx])->sh_type == SHT_GROUP
640 || ((elt = shdr->bfd_section) != NULL
641 && elf_sec_group (elt) != NULL
642 && elf_sec_group (elt) != ghdr->bfd_section))
644 _bfd_error_handler
645 (_("%pB: invalid entry (%#x) in group [%u]"),
646 abfd, idx, gidx);
647 continue;
650 /* PR binutils/23199: According to the ELF gABI all sections in
651 a group must be marked with SHF_GROUP, but some tools
652 generate broken objects. Fix them up here. */
653 shdr->sh_flags |= SHF_GROUP;
655 if (elt == NULL)
657 if (shdr->sh_type != SHT_RELA && shdr->sh_type != SHT_REL)
659 const char *name = bfd_elf_string_from_elf_section
660 (abfd, elf_elfheader (abfd)->e_shstrndx, shdr->sh_name);
662 _bfd_error_handler
663 /* xgettext:c-format */
664 (_("%pB: unexpected type (%#x) section `%s' in group [%u]"),
665 abfd, shdr->sh_type, name, gidx);
667 continue;
670 /* Don't try to add a section to elf_next_in_group list twice. */
671 if (elf_sec_group (elt) != NULL)
672 continue;
674 if (last_elt == NULL)
676 /* Start a circular list with one element.
677 It will be in reverse order to match what gas does. */
678 elf_next_in_group (elt) = elt;
679 /* Point the group section to it. */
680 elf_next_in_group (ghdr->bfd_section) = elt;
681 gname = group_signature (abfd, ghdr);
682 if (gname == NULL)
684 free (contents);
685 return false;
688 else
690 elf_next_in_group (elt) = elf_next_in_group (last_elt);
691 elf_next_in_group (last_elt) = elt;
693 last_elt = elt;
694 elf_group_name (elt) = gname;
695 elf_sec_group (elt) = ghdr->bfd_section;
698 free (contents);
699 return true;
702 bool
703 _bfd_elf_setup_sections (bfd *abfd)
705 bool result = true;
707 /* Process SHF_LINK_ORDER. */
708 for (asection *s = abfd->sections; s != NULL; s = s->next)
710 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
711 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
713 unsigned int elfsec = this_hdr->sh_link;
714 /* An sh_link value of 0 is now allowed. It indicates that linked
715 to section has already been discarded, but that the current
716 section has been retained for some other reason. This linking
717 section is still a candidate for later garbage collection
718 however. */
719 if (elfsec == 0)
721 elf_linked_to_section (s) = NULL;
723 else
725 asection *linksec = NULL;
727 if (elfsec < elf_numsections (abfd))
729 this_hdr = elf_elfsections (abfd)[elfsec];
730 linksec = this_hdr->bfd_section;
733 /* PR 1991, 2008:
734 Some strip/objcopy may leave an incorrect value in
735 sh_link. We don't want to proceed. */
736 if (linksec == NULL)
738 _bfd_error_handler
739 /* xgettext:c-format */
740 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
741 s->owner, elfsec, s);
742 result = false;
745 elf_linked_to_section (s) = linksec;
750 /* Process section groups. */
751 for (unsigned int i = 1; i < elf_numsections (abfd); i++)
753 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
755 if (shdr && shdr->sh_type == SHT_GROUP)
757 if (is_valid_group_section_header (shdr, GRP_ENTRY_SIZE))
759 if (shdr->sh_size >= 2 * GRP_ENTRY_SIZE
760 && !process_sht_group_entries (abfd, shdr, i))
761 result = false;
763 else
765 /* PR binutils/18758: Beware of corrupt binaries with
766 invalid group data. */
767 _bfd_error_handler
768 /* xgettext:c-format */
769 (_("%pB: section group entry number %u is corrupt"), abfd, i);
770 result = false;
775 return result;
778 bool
779 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
781 return elf_next_in_group (sec) != NULL;
784 const char *
785 bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
787 if (elf_sec_group (sec) != NULL)
788 return elf_group_name (sec);
789 return NULL;
792 /* Make a BFD section from an ELF section. We store a pointer to the
793 BFD section in the bfd_section field of the header. */
795 bool
796 _bfd_elf_make_section_from_shdr (bfd *abfd,
797 Elf_Internal_Shdr *hdr,
798 const char *name,
799 int shindex)
801 asection *newsect;
802 flagword flags;
803 const struct elf_backend_data *bed;
804 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
806 if (hdr->bfd_section != NULL)
807 return true;
809 newsect = bfd_make_section_anyway (abfd, name);
810 if (newsect == NULL)
811 return false;
813 hdr->bfd_section = newsect;
814 elf_section_data (newsect)->this_hdr = *hdr;
815 elf_section_data (newsect)->this_idx = shindex;
817 /* Always use the real type/flags. */
818 elf_section_type (newsect) = hdr->sh_type;
819 elf_section_flags (newsect) = hdr->sh_flags;
821 newsect->filepos = hdr->sh_offset;
823 flags = SEC_NO_FLAGS;
824 if (hdr->sh_type != SHT_NOBITS)
825 flags |= SEC_HAS_CONTENTS;
826 if (hdr->sh_type == SHT_GROUP)
827 flags |= SEC_GROUP;
828 if ((hdr->sh_flags & SHF_ALLOC) != 0)
830 flags |= SEC_ALLOC;
831 if (hdr->sh_type != SHT_NOBITS)
832 flags |= SEC_LOAD;
834 if ((hdr->sh_flags & SHF_WRITE) == 0)
835 flags |= SEC_READONLY;
836 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
837 flags |= SEC_CODE;
838 else if ((flags & SEC_LOAD) != 0)
839 flags |= SEC_DATA;
840 if ((hdr->sh_flags & SHF_MERGE) != 0)
842 flags |= SEC_MERGE;
843 newsect->entsize = hdr->sh_entsize;
845 if ((hdr->sh_flags & SHF_STRINGS) != 0)
847 flags |= SEC_STRINGS;
848 newsect->entsize = hdr->sh_entsize;
850 if ((hdr->sh_flags & SHF_TLS) != 0)
851 flags |= SEC_THREAD_LOCAL;
852 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
853 flags |= SEC_EXCLUDE;
855 switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
857 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
858 but binutils as of 2019-07-23 did not set the EI_OSABI header
859 byte. */
860 case ELFOSABI_GNU:
861 case ELFOSABI_FREEBSD:
862 if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
863 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
864 /* Fall through */
865 case ELFOSABI_NONE:
866 if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
867 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
868 break;
871 if ((flags & SEC_ALLOC) == 0)
873 /* The debugging sections appear to be recognized only by name,
874 not any sort of flag. Their SEC_ALLOC bits are cleared. */
875 if (name [0] == '.')
877 if (startswith (name, ".debug")
878 || startswith (name, ".gnu.debuglto_.debug_")
879 || startswith (name, ".gnu.linkonce.wi.")
880 || startswith (name, ".zdebug"))
881 flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
882 else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
883 || startswith (name, ".note.gnu"))
885 flags |= SEC_ELF_OCTETS;
886 opb = 1;
888 else if (startswith (name, ".line")
889 || startswith (name, ".stab")
890 || strcmp (name, ".gdb_index") == 0)
891 flags |= SEC_DEBUGGING;
895 if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
896 || !bfd_set_section_size (newsect, hdr->sh_size)
897 || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
898 & -hdr->sh_addralign)))
899 return false;
901 /* As a GNU extension, if the name begins with .gnu.linkonce, we
902 only link a single copy of the section. This is used to support
903 g++. g++ will emit each template expansion in its own section.
904 The symbols will be defined as weak, so that multiple definitions
905 are permitted. The GNU linker extension is to actually discard
906 all but one of the sections. */
907 if (startswith (name, ".gnu.linkonce")
908 && elf_next_in_group (newsect) == NULL)
909 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
911 if (!bfd_set_section_flags (newsect, flags))
912 return false;
914 bed = get_elf_backend_data (abfd);
915 if (bed->elf_backend_section_flags)
916 if (!bed->elf_backend_section_flags (hdr))
917 return false;
919 /* We do not parse the PT_NOTE segments as we are interested even in the
920 separate debug info files which may have the segments offsets corrupted.
921 PT_NOTEs from the core files are currently not parsed using BFD. */
922 if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0)
924 bfd_byte *contents;
926 if (!_bfd_elf_mmap_section_contents (abfd, newsect, &contents))
927 return false;
929 elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
930 hdr->sh_offset, hdr->sh_addralign);
931 _bfd_elf_munmap_section_contents (newsect, contents);
934 if ((newsect->flags & SEC_ALLOC) != 0)
936 Elf_Internal_Phdr *phdr;
937 unsigned int i, nload;
939 /* Some ELF linkers produce binaries with all the program header
940 p_paddr fields zero. If we have such a binary with more than
941 one PT_LOAD header, then leave the section lma equal to vma
942 so that we don't create sections with overlapping lma. */
943 phdr = elf_tdata (abfd)->phdr;
944 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
945 if (phdr->p_paddr != 0)
946 break;
947 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
948 ++nload;
949 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
950 return true;
952 phdr = elf_tdata (abfd)->phdr;
953 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
955 if (((phdr->p_type == PT_LOAD
956 && (hdr->sh_flags & SHF_TLS) == 0)
957 || phdr->p_type == PT_TLS)
958 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
960 if ((newsect->flags & SEC_LOAD) == 0)
961 newsect->lma = (phdr->p_paddr
962 + hdr->sh_addr - phdr->p_vaddr) / opb;
963 else
964 /* We used to use the same adjustment for SEC_LOAD
965 sections, but that doesn't work if the segment
966 is packed with code from multiple VMAs.
967 Instead we calculate the section LMA based on
968 the segment LMA. It is assumed that the
969 segment will contain sections with contiguous
970 LMAs, even if the VMAs are not. */
971 newsect->lma = (phdr->p_paddr
972 + hdr->sh_offset - phdr->p_offset) / opb;
974 /* With contiguous segments, we can't tell from file
975 offsets whether a section with zero size should
976 be placed at the end of one segment or the
977 beginning of the next. Decide based on vaddr. */
978 if (hdr->sh_addr >= phdr->p_vaddr
979 && (hdr->sh_addr + hdr->sh_size
980 <= phdr->p_vaddr + phdr->p_memsz))
981 break;
986 /* Compress/decompress DWARF debug sections with names: .debug_*,
987 .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set. */
988 if ((newsect->flags & SEC_DEBUGGING) != 0
989 && (newsect->flags & SEC_HAS_CONTENTS) != 0
990 && (newsect->flags & SEC_ELF_OCTETS) != 0)
992 enum { nothing, compress, decompress } action = nothing;
993 int compression_header_size;
994 bfd_size_type uncompressed_size;
995 unsigned int uncompressed_align_power;
996 enum compression_type ch_type = ch_none;
997 bool compressed
998 = bfd_is_section_compressed_info (abfd, newsect,
999 &compression_header_size,
1000 &uncompressed_size,
1001 &uncompressed_align_power,
1002 &ch_type);
1004 /* Should we decompress? */
1005 if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed)
1006 action = decompress;
1008 /* Should we compress? Or convert to a different compression? */
1009 else if ((abfd->flags & BFD_COMPRESS) != 0
1010 && newsect->size != 0
1011 && compression_header_size >= 0
1012 && uncompressed_size > 0)
1014 if (!compressed)
1015 action = compress;
1016 else
1018 enum compression_type new_ch_type = ch_none;
1019 if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
1020 new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0
1021 ? ch_compress_zstd : ch_compress_zlib);
1022 if (new_ch_type != ch_type)
1023 action = compress;
1027 if (action == compress)
1029 if (!bfd_init_section_compress_status (abfd, newsect))
1031 _bfd_error_handler
1032 /* xgettext:c-format */
1033 (_("%pB: unable to compress section %s"), abfd, name);
1034 return false;
1037 else if (action == decompress)
1039 if (!bfd_init_section_decompress_status (abfd, newsect))
1041 _bfd_error_handler
1042 /* xgettext:c-format */
1043 (_("%pB: unable to decompress section %s"), abfd, name);
1044 return false;
1046 #ifndef HAVE_ZSTD
1047 if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD)
1049 _bfd_error_handler
1050 /* xgettext:c-format */
1051 (_ ("%pB: section %s is compressed with zstd, but BFD "
1052 "is not built with zstd support"),
1053 abfd, name);
1054 newsect->compress_status = COMPRESS_SECTION_NONE;
1055 return false;
1057 #endif
1058 if (abfd->is_linker_input
1059 && name[1] == 'z')
1061 /* Rename section from .zdebug_* to .debug_* so that ld
1062 scripts will see this section as a debug section. */
1063 char *new_name = bfd_zdebug_name_to_debug (abfd, name);
1064 if (new_name == NULL)
1065 return false;
1066 bfd_rename_section (newsect, new_name);
1071 return true;
1074 const char *const bfd_elf_section_type_names[] =
1076 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1077 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1078 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1081 /* ELF relocs are against symbols. If we are producing relocatable
1082 output, and the reloc is against an external symbol, and nothing
1083 has given us any additional addend, the resulting reloc will also
1084 be against the same symbol. In such a case, we don't want to
1085 change anything about the way the reloc is handled, since it will
1086 all be done at final link time. Rather than put special case code
1087 into bfd_perform_relocation, all the reloc types use this howto
1088 function, or should call this function for relocatable output. */
1090 bfd_reloc_status_type
1091 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1092 arelent *reloc_entry,
1093 asymbol *symbol,
1094 void *data ATTRIBUTE_UNUSED,
1095 asection *input_section,
1096 bfd *output_bfd,
1097 char **error_message ATTRIBUTE_UNUSED)
1099 if (output_bfd != NULL
1100 && (symbol->flags & BSF_SECTION_SYM) == 0
1101 && (! reloc_entry->howto->partial_inplace
1102 || reloc_entry->addend == 0))
1104 reloc_entry->address += input_section->output_offset;
1105 return bfd_reloc_ok;
1108 /* In some cases the relocation should be treated as output section
1109 relative, as when linking ELF DWARF into PE COFF. Many ELF
1110 targets lack section relative relocations and instead use
1111 ordinary absolute relocations for references between DWARF
1112 sections. That is arguably a bug in those targets but it happens
1113 to work for the usual case of linking to non-loaded ELF debug
1114 sections with VMAs forced to zero. PE COFF on the other hand
1115 doesn't allow a section VMA of zero. */
1116 if (output_bfd == NULL
1117 && !reloc_entry->howto->pc_relative
1118 && (symbol->section->flags & SEC_DEBUGGING) != 0
1119 && (input_section->flags & SEC_DEBUGGING) != 0)
1120 reloc_entry->addend -= symbol->section->output_section->vma;
1122 return bfd_reloc_continue;
1125 /* Returns TRUE if section A matches section B.
1126 Names, addresses and links may be different, but everything else
1127 should be the same. */
1129 static bool
1130 section_match (const Elf_Internal_Shdr * a,
1131 const Elf_Internal_Shdr * b)
1133 if (a->sh_type != b->sh_type
1134 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1135 || a->sh_addralign != b->sh_addralign
1136 || a->sh_entsize != b->sh_entsize)
1137 return false;
1138 if (a->sh_type == SHT_SYMTAB
1139 || a->sh_type == SHT_STRTAB)
1140 return true;
1141 return a->sh_size == b->sh_size;
1144 /* Find a section in OBFD that has the same characteristics
1145 as IHEADER. Return the index of this section or SHN_UNDEF if
1146 none can be found. Check's section HINT first, as this is likely
1147 to be the correct section. */
1149 static unsigned int
1150 find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1151 const unsigned int hint)
1153 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1154 unsigned int i;
1156 BFD_ASSERT (iheader != NULL);
1158 /* See PR 20922 for a reproducer of the NULL test. */
1159 if (hint < elf_numsections (obfd)
1160 && oheaders[hint] != NULL
1161 && section_match (oheaders[hint], iheader))
1162 return hint;
1164 for (i = 1; i < elf_numsections (obfd); i++)
1166 Elf_Internal_Shdr * oheader = oheaders[i];
1168 if (oheader == NULL)
1169 continue;
1170 if (section_match (oheader, iheader))
1171 /* FIXME: Do we care if there is a potential for
1172 multiple matches ? */
1173 return i;
1176 return SHN_UNDEF;
1179 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1180 Processor specific section, based upon a matching input section.
1181 Returns TRUE upon success, FALSE otherwise. */
1183 static bool
1184 copy_special_section_fields (const bfd *ibfd,
1185 bfd *obfd,
1186 const Elf_Internal_Shdr *iheader,
1187 Elf_Internal_Shdr *oheader,
1188 const unsigned int secnum)
1190 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1191 const Elf_Internal_Shdr **iheaders
1192 = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1193 bool changed = false;
1194 unsigned int sh_link;
1196 if (oheader->sh_type == SHT_NOBITS)
1198 /* This is a feature for objcopy --only-keep-debug:
1199 When a section's type is changed to NOBITS, we preserve
1200 the sh_link and sh_info fields so that they can be
1201 matched up with the original.
1203 Note: Strictly speaking these assignments are wrong.
1204 The sh_link and sh_info fields should point to the
1205 relevent sections in the output BFD, which may not be in
1206 the same location as they were in the input BFD. But
1207 the whole point of this action is to preserve the
1208 original values of the sh_link and sh_info fields, so
1209 that they can be matched up with the section headers in
1210 the original file. So strictly speaking we may be
1211 creating an invalid ELF file, but it is only for a file
1212 that just contains debug info and only for sections
1213 without any contents. */
1214 if (oheader->sh_link == 0)
1215 oheader->sh_link = iheader->sh_link;
1216 if (oheader->sh_info == 0)
1217 oheader->sh_info = iheader->sh_info;
1218 return true;
1221 /* Allow the target a chance to decide how these fields should be set. */
1222 if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1223 iheader, oheader))
1224 return true;
1226 /* We have an iheader which might match oheader, and which has non-zero
1227 sh_info and/or sh_link fields. Attempt to follow those links and find
1228 the section in the output bfd which corresponds to the linked section
1229 in the input bfd. */
1230 if (iheader->sh_link != SHN_UNDEF)
1232 /* See PR 20931 for a reproducer. */
1233 if (iheader->sh_link >= elf_numsections (ibfd))
1235 _bfd_error_handler
1236 /* xgettext:c-format */
1237 (_("%pB: invalid sh_link field (%d) in section number %d"),
1238 ibfd, iheader->sh_link, secnum);
1239 return false;
1242 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1243 if (sh_link != SHN_UNDEF)
1245 oheader->sh_link = sh_link;
1246 changed = true;
1248 else
1249 /* FIXME: Should we install iheader->sh_link
1250 if we could not find a match ? */
1251 _bfd_error_handler
1252 /* xgettext:c-format */
1253 (_("%pB: failed to find link section for section %d"), obfd, secnum);
1256 if (iheader->sh_info)
1258 /* The sh_info field can hold arbitrary information, but if the
1259 SHF_LINK_INFO flag is set then it should be interpreted as a
1260 section index. */
1261 if (iheader->sh_flags & SHF_INFO_LINK)
1263 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1264 iheader->sh_info);
1265 if (sh_link != SHN_UNDEF)
1266 oheader->sh_flags |= SHF_INFO_LINK;
1268 else
1269 /* No idea what it means - just copy it. */
1270 sh_link = iheader->sh_info;
1272 if (sh_link != SHN_UNDEF)
1274 oheader->sh_info = sh_link;
1275 changed = true;
1277 else
1278 _bfd_error_handler
1279 /* xgettext:c-format */
1280 (_("%pB: failed to find info section for section %d"), obfd, secnum);
1283 return changed;
1286 /* Copy the program header and other data from one object module to
1287 another. */
1289 bool
1290 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1292 const Elf_Internal_Shdr **iheaders
1293 = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1294 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1295 const struct elf_backend_data *bed;
1296 unsigned int i;
1298 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1299 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1300 return true;
1302 if (!elf_flags_init (obfd))
1304 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1305 elf_flags_init (obfd) = true;
1308 elf_gp (obfd) = elf_gp (ibfd);
1310 /* Also copy the EI_OSABI field. */
1311 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1312 elf_elfheader (ibfd)->e_ident[EI_OSABI];
1314 /* If set, copy the EI_ABIVERSION field. */
1315 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1316 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1317 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1319 /* Copy object attributes. */
1320 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1322 if (iheaders == NULL || oheaders == NULL)
1323 return true;
1325 bed = get_elf_backend_data (obfd);
1327 /* Possibly copy other fields in the section header. */
1328 for (i = 1; i < elf_numsections (obfd); i++)
1330 unsigned int j;
1331 Elf_Internal_Shdr * oheader = oheaders[i];
1333 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1334 because of a special case need for generating separate debug info
1335 files. See below for more details. */
1336 if (oheader == NULL
1337 || (oheader->sh_type != SHT_NOBITS
1338 && oheader->sh_type < SHT_LOOS))
1339 continue;
1341 /* Ignore empty sections, and sections whose
1342 fields have already been initialised. */
1343 if (oheader->sh_size == 0
1344 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1345 continue;
1347 /* Scan for the matching section in the input bfd.
1348 First we try for a direct mapping between the input and
1349 output sections. */
1350 for (j = 1; j < elf_numsections (ibfd); j++)
1352 const Elf_Internal_Shdr * iheader = iheaders[j];
1354 if (iheader == NULL)
1355 continue;
1357 if (oheader->bfd_section != NULL
1358 && iheader->bfd_section != NULL
1359 && iheader->bfd_section->output_section != NULL
1360 && iheader->bfd_section->output_section == oheader->bfd_section)
1362 /* We have found a connection from the input section to
1363 the output section. Attempt to copy the header fields.
1364 If this fails then do not try any further sections -
1365 there should only be a one-to-one mapping between
1366 input and output. */
1367 if (!copy_special_section_fields (ibfd, obfd,
1368 iheader, oheader, i))
1369 j = elf_numsections (ibfd);
1370 break;
1374 if (j < elf_numsections (ibfd))
1375 continue;
1377 /* That failed. So try to deduce the corresponding input section.
1378 Unfortunately we cannot compare names as the output string table
1379 is empty, so instead we check size, address and type. */
1380 for (j = 1; j < elf_numsections (ibfd); j++)
1382 const Elf_Internal_Shdr * iheader = iheaders[j];
1384 if (iheader == NULL)
1385 continue;
1387 /* Try matching fields in the input section's header.
1388 Since --only-keep-debug turns all non-debug sections into
1389 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1390 input type. */
1391 if ((oheader->sh_type == SHT_NOBITS
1392 || iheader->sh_type == oheader->sh_type)
1393 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1394 == (oheader->sh_flags & ~ SHF_INFO_LINK)
1395 && iheader->sh_addralign == oheader->sh_addralign
1396 && iheader->sh_entsize == oheader->sh_entsize
1397 && iheader->sh_size == oheader->sh_size
1398 && iheader->sh_addr == oheader->sh_addr
1399 && (iheader->sh_info != oheader->sh_info
1400 || iheader->sh_link != oheader->sh_link))
1402 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1403 break;
1407 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1409 /* Final attempt. Call the backend copy function
1410 with a NULL input section. */
1411 (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1412 NULL, oheader);
1416 return true;
1419 static const char *
1420 get_segment_type (unsigned int p_type)
1422 const char *pt;
1423 switch (p_type)
1425 case PT_NULL: pt = "NULL"; break;
1426 case PT_LOAD: pt = "LOAD"; break;
1427 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1428 case PT_INTERP: pt = "INTERP"; break;
1429 case PT_NOTE: pt = "NOTE"; break;
1430 case PT_SHLIB: pt = "SHLIB"; break;
1431 case PT_PHDR: pt = "PHDR"; break;
1432 case PT_TLS: pt = "TLS"; break;
1433 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1434 case PT_GNU_STACK: pt = "STACK"; break;
1435 case PT_GNU_RELRO: pt = "RELRO"; break;
1436 case PT_GNU_SFRAME: pt = "SFRAME"; break;
1437 default: pt = NULL; break;
1439 return pt;
1442 /* Print out the program headers. */
1444 bool
1445 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1447 FILE *f = (FILE *) farg;
1448 Elf_Internal_Phdr *p;
1449 asection *s;
1450 bfd_byte *dynbuf = NULL;
1452 p = elf_tdata (abfd)->phdr;
1453 if (p != NULL)
1455 unsigned int i, c;
1457 fprintf (f, _("\nProgram Header:\n"));
1458 c = elf_elfheader (abfd)->e_phnum;
1459 for (i = 0; i < c; i++, p++)
1461 const char *pt = get_segment_type (p->p_type);
1462 char buf[20];
1464 if (pt == NULL)
1466 sprintf (buf, "0x%lx", p->p_type);
1467 pt = buf;
1469 fprintf (f, "%8s off 0x", pt);
1470 bfd_fprintf_vma (abfd, f, p->p_offset);
1471 fprintf (f, " vaddr 0x");
1472 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1473 fprintf (f, " paddr 0x");
1474 bfd_fprintf_vma (abfd, f, p->p_paddr);
1475 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1476 fprintf (f, " filesz 0x");
1477 bfd_fprintf_vma (abfd, f, p->p_filesz);
1478 fprintf (f, " memsz 0x");
1479 bfd_fprintf_vma (abfd, f, p->p_memsz);
1480 fprintf (f, " flags %c%c%c",
1481 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1482 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1483 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1484 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1485 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1486 fprintf (f, "\n");
1490 s = bfd_get_section_by_name (abfd, ".dynamic");
1491 if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0)
1493 unsigned int elfsec;
1494 unsigned long shlink;
1495 bfd_byte *extdyn, *extdynend;
1496 size_t extdynsize;
1497 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1499 fprintf (f, _("\nDynamic Section:\n"));
1501 if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
1502 goto error_return;
1504 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1505 if (elfsec == SHN_BAD)
1506 goto error_return;
1507 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1509 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1510 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1512 for (extdyn = dynbuf, extdynend = dynbuf + s->size;
1513 (size_t) (extdynend - extdyn) >= extdynsize;
1514 extdyn += extdynsize)
1516 Elf_Internal_Dyn dyn;
1517 const char *name = "";
1518 char ab[20];
1519 bool stringp;
1520 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1522 (*swap_dyn_in) (abfd, extdyn, &dyn);
1524 if (dyn.d_tag == DT_NULL)
1525 break;
1527 stringp = false;
1528 switch (dyn.d_tag)
1530 default:
1531 if (bed->elf_backend_get_target_dtag)
1532 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1534 if (!strcmp (name, ""))
1536 sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
1537 name = ab;
1539 break;
1541 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
1542 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1543 case DT_PLTGOT: name = "PLTGOT"; break;
1544 case DT_HASH: name = "HASH"; break;
1545 case DT_STRTAB: name = "STRTAB"; break;
1546 case DT_SYMTAB: name = "SYMTAB"; break;
1547 case DT_RELA: name = "RELA"; break;
1548 case DT_RELASZ: name = "RELASZ"; break;
1549 case DT_RELAENT: name = "RELAENT"; break;
1550 case DT_STRSZ: name = "STRSZ"; break;
1551 case DT_SYMENT: name = "SYMENT"; break;
1552 case DT_INIT: name = "INIT"; break;
1553 case DT_FINI: name = "FINI"; break;
1554 case DT_SONAME: name = "SONAME"; stringp = true; break;
1555 case DT_RPATH: name = "RPATH"; stringp = true; break;
1556 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1557 case DT_REL: name = "REL"; break;
1558 case DT_RELSZ: name = "RELSZ"; break;
1559 case DT_RELENT: name = "RELENT"; break;
1560 case DT_RELR: name = "RELR"; break;
1561 case DT_RELRSZ: name = "RELRSZ"; break;
1562 case DT_RELRENT: name = "RELRENT"; break;
1563 case DT_PLTREL: name = "PLTREL"; break;
1564 case DT_DEBUG: name = "DEBUG"; break;
1565 case DT_TEXTREL: name = "TEXTREL"; break;
1566 case DT_JMPREL: name = "JMPREL"; break;
1567 case DT_BIND_NOW: name = "BIND_NOW"; break;
1568 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1569 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1570 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1571 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1572 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
1573 case DT_FLAGS: name = "FLAGS"; break;
1574 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1575 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1576 case DT_CHECKSUM: name = "CHECKSUM"; break;
1577 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1578 case DT_MOVEENT: name = "MOVEENT"; break;
1579 case DT_MOVESZ: name = "MOVESZ"; break;
1580 case DT_FEATURE: name = "FEATURE"; break;
1581 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1582 case DT_SYMINSZ: name = "SYMINSZ"; break;
1583 case DT_SYMINENT: name = "SYMINENT"; break;
1584 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1585 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1586 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
1587 case DT_PLTPAD: name = "PLTPAD"; break;
1588 case DT_MOVETAB: name = "MOVETAB"; break;
1589 case DT_SYMINFO: name = "SYMINFO"; break;
1590 case DT_RELACOUNT: name = "RELACOUNT"; break;
1591 case DT_RELCOUNT: name = "RELCOUNT"; break;
1592 case DT_FLAGS_1: name = "FLAGS_1"; break;
1593 case DT_VERSYM: name = "VERSYM"; break;
1594 case DT_VERDEF: name = "VERDEF"; break;
1595 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1596 case DT_VERNEED: name = "VERNEED"; break;
1597 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1598 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
1599 case DT_USED: name = "USED"; break;
1600 case DT_FILTER: name = "FILTER"; stringp = true; break;
1601 case DT_GNU_HASH: name = "GNU_HASH"; break;
1604 fprintf (f, " %-20s ", name);
1605 if (! stringp)
1607 fprintf (f, "0x");
1608 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1610 else
1612 const char *string;
1613 unsigned int tagv = dyn.d_un.d_val;
1615 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1616 if (string == NULL)
1617 goto error_return;
1618 fprintf (f, "%s", string);
1620 fprintf (f, "\n");
1623 _bfd_elf_munmap_section_contents (s, dynbuf);
1624 dynbuf = NULL;
1627 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1628 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1630 if (! _bfd_elf_slurp_version_tables (abfd, false))
1631 return false;
1634 if (elf_dynverdef (abfd) != 0)
1636 Elf_Internal_Verdef *t;
1638 fprintf (f, _("\nVersion definitions:\n"));
1639 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1641 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1642 t->vd_flags, t->vd_hash,
1643 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1644 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1646 Elf_Internal_Verdaux *a;
1648 fprintf (f, "\t");
1649 for (a = t->vd_auxptr->vda_nextptr;
1650 a != NULL;
1651 a = a->vda_nextptr)
1652 fprintf (f, "%s ",
1653 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1654 fprintf (f, "\n");
1659 if (elf_dynverref (abfd) != 0)
1661 Elf_Internal_Verneed *t;
1663 fprintf (f, _("\nVersion References:\n"));
1664 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1666 Elf_Internal_Vernaux *a;
1668 fprintf (f, _(" required from %s:\n"),
1669 t->vn_filename ? t->vn_filename : "<corrupt>");
1670 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1671 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1672 a->vna_flags, a->vna_other,
1673 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1677 return true;
1679 error_return:
1680 _bfd_elf_munmap_section_contents (s, dynbuf);
1681 return false;
1684 /* Find the file offset corresponding to VMA by using the program
1685 headers. */
1687 static file_ptr
1688 offset_from_vma (Elf_Internal_Phdr *phdrs, size_t phnum, bfd_vma vma,
1689 size_t size, size_t *max_size_p)
1691 Elf_Internal_Phdr *seg;
1692 size_t i;
1694 for (seg = phdrs, i = 0; i < phnum; ++seg, ++i)
1695 if (seg->p_type == PT_LOAD
1696 && vma >= (seg->p_vaddr & -seg->p_align)
1697 && vma + size <= seg->p_vaddr + seg->p_filesz)
1699 if (max_size_p)
1700 *max_size_p = seg->p_vaddr + seg->p_filesz - vma;
1701 return vma - seg->p_vaddr + seg->p_offset;
1704 if (max_size_p)
1705 *max_size_p = 0;
1706 bfd_set_error (bfd_error_invalid_operation);
1707 return (file_ptr) -1;
1710 /* Convert hash table to internal form. */
1712 static bfd_vma *
1713 get_hash_table_data (bfd *abfd, bfd_size_type number,
1714 unsigned int ent_size, bfd_size_type filesize)
1716 unsigned char *e_data = NULL;
1717 bfd_vma *i_data = NULL;
1718 bfd_size_type size;
1719 void *e_data_addr;
1720 size_t e_data_size ATTRIBUTE_UNUSED;
1722 if (ent_size != 4 && ent_size != 8)
1723 return NULL;
1725 if ((size_t) number != number)
1727 bfd_set_error (bfd_error_file_too_big);
1728 return NULL;
1731 size = ent_size * number;
1732 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
1733 attempting to allocate memory when the read is bound to fail. */
1734 if (size > filesize
1735 || number >= ~(size_t) 0 / ent_size
1736 || number >= ~(size_t) 0 / sizeof (*i_data))
1738 bfd_set_error (bfd_error_file_too_big);
1739 return NULL;
1742 e_data = _bfd_mmap_temporary (abfd, size, &e_data_addr, &e_data_size);
1743 if (e_data == NULL)
1744 return NULL;
1746 i_data = (bfd_vma *) bfd_malloc (number * sizeof (*i_data));
1747 if (i_data == NULL)
1749 _bfd_munmap_temporary (e_data_addr, e_data_size);
1750 return NULL;
1753 if (ent_size == 4)
1754 while (number--)
1755 i_data[number] = bfd_get_32 (abfd, e_data + number * ent_size);
1756 else
1757 while (number--)
1758 i_data[number] = bfd_get_64 (abfd, e_data + number * ent_size);
1760 _bfd_munmap_temporary (e_data_addr, e_data_size);
1761 return i_data;
1764 /* Address of .MIPS.xhash section. FIXME: What is the best way to
1765 support DT_MIPS_XHASH? */
1766 #define DT_MIPS_XHASH 0x70000036
1768 /* Reconstruct dynamic symbol table from PT_DYNAMIC segment. */
1770 bool
1771 _bfd_elf_get_dynamic_symbols (bfd *abfd, Elf_Internal_Phdr *phdr,
1772 Elf_Internal_Phdr *phdrs, size_t phnum,
1773 bfd_size_type filesize)
1775 bfd_byte *extdyn, *extdynend;
1776 size_t extdynsize;
1777 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1778 bool (*swap_symbol_in) (bfd *, const void *, const void *,
1779 Elf_Internal_Sym *);
1780 Elf_Internal_Dyn dyn;
1781 bfd_vma dt_hash = 0;
1782 bfd_vma dt_gnu_hash = 0;
1783 bfd_vma dt_mips_xhash = 0;
1784 bfd_vma dt_strtab = 0;
1785 bfd_vma dt_symtab = 0;
1786 size_t dt_strsz = 0;
1787 bfd_vma dt_versym = 0;
1788 bfd_vma dt_verdef = 0;
1789 bfd_vma dt_verneed = 0;
1790 bfd_byte *dynbuf = NULL;
1791 char *strbuf = NULL;
1792 bfd_vma *gnubuckets = NULL;
1793 bfd_vma *gnuchains = NULL;
1794 bfd_vma *mipsxlat = NULL;
1795 file_ptr saved_filepos, filepos;
1796 bool res = false;
1797 size_t amt;
1798 bfd_byte *esymbuf = NULL, *esym;
1799 bfd_size_type symcount;
1800 Elf_Internal_Sym *isymbuf = NULL;
1801 Elf_Internal_Sym *isym, *isymend;
1802 bfd_byte *versym = NULL;
1803 bfd_byte *verdef = NULL;
1804 bfd_byte *verneed = NULL;
1805 size_t verdef_size = 0;
1806 size_t verneed_size = 0;
1807 size_t extsym_size;
1808 const struct elf_backend_data *bed;
1809 void *dynbuf_addr = NULL;
1810 void *esymbuf_addr = NULL;
1811 size_t dynbuf_size = 0;
1812 size_t esymbuf_size = 0;
1814 /* Return TRUE if symbol table is bad. */
1815 if (elf_bad_symtab (abfd))
1816 return true;
1818 /* Return TRUE if DT_HASH/DT_GNU_HASH have bee processed before. */
1819 if (elf_tdata (abfd)->dt_strtab != NULL)
1820 return true;
1822 bed = get_elf_backend_data (abfd);
1824 /* Save file position for elf_object_p. */
1825 saved_filepos = bfd_tell (abfd);
1827 if (bfd_seek (abfd, phdr->p_offset, SEEK_SET) != 0)
1828 goto error_return;
1830 dynbuf_size = phdr->p_filesz;
1831 dynbuf = _bfd_mmap_temporary (abfd, dynbuf_size, &dynbuf_addr, &dynbuf_size);
1832 if (dynbuf == NULL)
1833 goto error_return;
1835 extsym_size = bed->s->sizeof_sym;
1836 extdynsize = bed->s->sizeof_dyn;
1837 swap_dyn_in = bed->s->swap_dyn_in;
1839 extdyn = dynbuf;
1840 if (phdr->p_filesz < extdynsize)
1841 goto error_return;
1842 extdynend = extdyn + phdr->p_filesz;
1843 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
1845 swap_dyn_in (abfd, extdyn, &dyn);
1847 if (dyn.d_tag == DT_NULL)
1848 break;
1850 switch (dyn.d_tag)
1852 case DT_HASH:
1853 dt_hash = dyn.d_un.d_val;
1854 break;
1855 case DT_GNU_HASH:
1856 if (bed->elf_machine_code != EM_MIPS
1857 && bed->elf_machine_code != EM_MIPS_RS3_LE)
1858 dt_gnu_hash = dyn.d_un.d_val;
1859 break;
1860 case DT_STRTAB:
1861 dt_strtab = dyn.d_un.d_val;
1862 break;
1863 case DT_SYMTAB:
1864 dt_symtab = dyn.d_un.d_val;
1865 break;
1866 case DT_STRSZ:
1867 dt_strsz = dyn.d_un.d_val;
1868 break;
1869 case DT_SYMENT:
1870 if (dyn.d_un.d_val != extsym_size)
1871 goto error_return;
1872 break;
1873 case DT_VERSYM:
1874 dt_versym = dyn.d_un.d_val;
1875 break;
1876 case DT_VERDEF:
1877 dt_verdef = dyn.d_un.d_val;
1878 break;
1879 case DT_VERNEED:
1880 dt_verneed = dyn.d_un.d_val;
1881 break;
1882 default:
1883 if (dyn.d_tag == DT_MIPS_XHASH
1884 && (bed->elf_machine_code == EM_MIPS
1885 || bed->elf_machine_code == EM_MIPS_RS3_LE))
1887 dt_gnu_hash = dyn.d_un.d_val;
1888 dt_mips_xhash = dyn.d_un.d_val;
1890 break;
1894 /* Check if we can reconstruct dynamic symbol table from PT_DYNAMIC
1895 segment. */
1896 if ((!dt_hash && !dt_gnu_hash)
1897 || !dt_strtab
1898 || !dt_symtab
1899 || !dt_strsz)
1900 goto error_return;
1902 /* Get dynamic string table. */
1903 filepos = offset_from_vma (phdrs, phnum, dt_strtab, dt_strsz, NULL);
1904 if (filepos == (file_ptr) -1
1905 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
1906 goto error_return;
1908 /* Dynamic string table must be valid until ABFD is closed. */
1909 strbuf = (char *) _bfd_mmap_persistent (abfd, dt_strsz);
1910 if (strbuf == NULL)
1911 goto error_return;
1912 if (strbuf[dt_strsz - 1] != 0)
1914 /* It is an error if a string table is't terminated. */
1915 _bfd_error_handler
1916 /* xgettext:c-format */
1917 (_("%pB: DT_STRTAB table is corrupt"), abfd);
1918 strbuf[dt_strsz - 1] = 0;
1921 /* Get the real symbol count from DT_HASH or DT_GNU_HASH. Prefer
1922 DT_HASH since it is simpler than DT_GNU_HASH. */
1923 if (dt_hash)
1925 unsigned char nb[16];
1926 unsigned int hash_ent_size;
1928 switch (bed->elf_machine_code)
1930 case EM_ALPHA:
1931 case EM_S390:
1932 case EM_S390_OLD:
1933 if (bed->s->elfclass == ELFCLASS64)
1935 hash_ent_size = 8;
1936 break;
1938 /* FALLTHROUGH */
1939 default:
1940 hash_ent_size = 4;
1941 break;
1944 filepos = offset_from_vma (phdrs, phnum, dt_hash, 2 * hash_ent_size,
1945 NULL);
1946 if (filepos == (file_ptr) -1
1947 || bfd_seek (abfd, filepos, SEEK_SET) != 0
1948 || bfd_read (nb, 2 * hash_ent_size, abfd) != 2 * hash_ent_size)
1949 goto error_return;
1951 /* The number of dynamic symbol table entries equals the number
1952 of chains. */
1953 if (hash_ent_size == 8)
1954 symcount = bfd_get_64 (abfd, nb + hash_ent_size);
1955 else
1956 symcount = bfd_get_32 (abfd, nb + hash_ent_size);
1958 else
1960 /* For DT_GNU_HASH, only defined symbols with non-STB_LOCAL
1961 bindings are in hash table. Since in dynamic symbol table,
1962 all symbols with STB_LOCAL binding are placed before symbols
1963 with other bindings and all undefined symbols are placed
1964 before defined ones, the highest symbol index in DT_GNU_HASH
1965 is the highest dynamic symbol table index. */
1966 unsigned char nb[16];
1967 bfd_vma ngnubuckets;
1968 bfd_vma gnusymidx;
1969 size_t i, ngnuchains;
1970 bfd_vma maxchain = 0xffffffff, bitmaskwords;
1971 bfd_vma buckets_vma;
1973 filepos = offset_from_vma (phdrs, phnum, dt_gnu_hash,
1974 sizeof (nb), NULL);
1975 if (filepos == (file_ptr) -1
1976 || bfd_seek (abfd, filepos, SEEK_SET) != 0
1977 || bfd_read (nb, sizeof (nb), abfd) != sizeof (nb))
1978 goto error_return;
1980 ngnubuckets = bfd_get_32 (abfd, nb);
1981 gnusymidx = bfd_get_32 (abfd, nb + 4);
1982 bitmaskwords = bfd_get_32 (abfd, nb + 8);
1983 buckets_vma = dt_gnu_hash + 16;
1984 if (bed->s->elfclass == ELFCLASS32)
1985 buckets_vma += bitmaskwords * 4;
1986 else
1987 buckets_vma += bitmaskwords * 8;
1988 filepos = offset_from_vma (phdrs, phnum, buckets_vma, 4, NULL);
1989 if (filepos == (file_ptr) -1
1990 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
1991 goto error_return;
1993 gnubuckets = get_hash_table_data (abfd, ngnubuckets, 4, filesize);
1994 if (gnubuckets == NULL)
1995 goto error_return;
1997 for (i = 0; i < ngnubuckets; i++)
1998 if (gnubuckets[i] != 0)
2000 if (gnubuckets[i] < gnusymidx)
2001 goto error_return;
2003 if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
2004 maxchain = gnubuckets[i];
2007 if (maxchain == 0xffffffff)
2009 symcount = 0;
2010 goto empty_gnu_hash;
2013 maxchain -= gnusymidx;
2014 filepos = offset_from_vma (phdrs, phnum,
2015 buckets_vma + 4 * (ngnubuckets + maxchain),
2016 4, NULL);
2017 if (filepos == (file_ptr) -1
2018 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2019 goto error_return;
2023 if (bfd_read (nb, 4, abfd) != 4)
2024 goto error_return;
2025 ++maxchain;
2026 if (maxchain == 0)
2027 goto error_return;
2029 while ((bfd_get_32 (abfd, nb) & 1) == 0);
2031 filepos = offset_from_vma (phdrs, phnum,
2032 buckets_vma + 4 * ngnubuckets,
2033 4, NULL);
2034 if (filepos == (file_ptr) -1
2035 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2036 goto error_return;
2038 gnuchains = get_hash_table_data (abfd, maxchain, 4, filesize);
2039 if (gnuchains == NULL)
2040 goto error_return;
2041 ngnuchains = maxchain;
2043 if (dt_mips_xhash)
2045 filepos = offset_from_vma (phdrs, phnum,
2046 buckets_vma + 4 * (ngnubuckets + maxchain),
2047 4, NULL);
2048 if (filepos == (file_ptr) -1
2049 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2050 goto error_return;
2052 mipsxlat = get_hash_table_data (abfd, maxchain, 4, filesize);
2053 if (mipsxlat == NULL)
2054 goto error_return;
2057 symcount = 0;
2058 for (i = 0; i < ngnubuckets; ++i)
2059 if (gnubuckets[i] != 0)
2061 bfd_vma si = gnubuckets[i];
2062 bfd_vma off = si - gnusymidx;
2065 if (mipsxlat)
2067 if (mipsxlat[off] >= symcount)
2068 symcount = mipsxlat[off] + 1;
2070 else
2072 if (si >= symcount)
2073 symcount = si + 1;
2075 si++;
2077 while (off < ngnuchains && (gnuchains[off++] & 1) == 0);
2081 /* Swap in dynamic symbol table. */
2082 if (_bfd_mul_overflow (symcount, extsym_size, &amt))
2084 bfd_set_error (bfd_error_file_too_big);
2085 goto error_return;
2088 filepos = offset_from_vma (phdrs, phnum, dt_symtab, amt, NULL);
2089 if (filepos == (file_ptr) -1
2090 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2091 goto error_return;
2092 esymbuf_size = amt;
2093 esymbuf = _bfd_mmap_temporary (abfd, esymbuf_size,
2094 &esymbuf_addr, &esymbuf_size);
2095 if (esymbuf == NULL)
2096 goto error_return;
2098 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
2100 bfd_set_error (bfd_error_file_too_big);
2101 goto error_return;
2104 /* Dynamic symbol table must be valid until ABFD is closed. */
2105 isymbuf = (Elf_Internal_Sym *) bfd_alloc (abfd, amt);
2106 if (isymbuf == NULL)
2107 goto error_return;
2109 swap_symbol_in = bed->s->swap_symbol_in;
2111 /* Convert the symbols to internal form. */
2112 isymend = isymbuf + symcount;
2113 for (esym = esymbuf, isym = isymbuf;
2114 isym < isymend;
2115 esym += extsym_size, isym++)
2116 if (!swap_symbol_in (abfd, esym, NULL, isym)
2117 || isym->st_name >= dt_strsz)
2119 bfd_set_error (bfd_error_invalid_operation);
2120 goto error_return;
2123 if (dt_versym)
2125 /* Swap in DT_VERSYM. */
2126 if (_bfd_mul_overflow (symcount, 2, &amt))
2128 bfd_set_error (bfd_error_file_too_big);
2129 goto error_return;
2132 filepos = offset_from_vma (phdrs, phnum, dt_versym, amt, NULL);
2133 if (filepos == (file_ptr) -1
2134 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2135 goto error_return;
2137 /* DT_VERSYM info must be valid until ABFD is closed. */
2138 versym = _bfd_mmap_persistent (abfd, amt);
2140 if (dt_verdef)
2142 /* Read in DT_VERDEF. */
2143 filepos = offset_from_vma (phdrs, phnum, dt_verdef,
2144 0, &verdef_size);
2145 if (filepos == (file_ptr) -1
2146 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2147 goto error_return;
2149 /* DT_VERDEF info must be valid until ABFD is closed. */
2150 verdef = _bfd_mmap_persistent (abfd, verdef_size);
2153 if (dt_verneed)
2155 /* Read in DT_VERNEED. */
2156 filepos = offset_from_vma (phdrs, phnum, dt_verneed,
2157 0, &verneed_size);
2158 if (filepos == (file_ptr) -1
2159 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2160 goto error_return;
2162 /* DT_VERNEED info must be valid until ABFD is closed. */
2163 verneed = _bfd_mmap_persistent (abfd, verneed_size);
2167 empty_gnu_hash:
2168 elf_tdata (abfd)->dt_strtab = strbuf;
2169 elf_tdata (abfd)->dt_strsz = dt_strsz;
2170 elf_tdata (abfd)->dt_symtab = isymbuf;
2171 elf_tdata (abfd)->dt_symtab_count = symcount;
2172 elf_tdata (abfd)->dt_versym = versym;
2173 elf_tdata (abfd)->dt_verdef = verdef;
2174 elf_tdata (abfd)->dt_verneed = verneed;
2175 elf_tdata (abfd)->dt_verdef_count
2176 = verdef_size / sizeof (Elf_External_Verdef);
2177 elf_tdata (abfd)->dt_verneed_count
2178 = verneed_size / sizeof (Elf_External_Verneed);
2180 res = true;
2182 error_return:
2183 /* Restore file position for elf_object_p. */
2184 if (bfd_seek (abfd, saved_filepos, SEEK_SET) != 0)
2185 res = false;
2186 _bfd_munmap_temporary (dynbuf_addr, dynbuf_size);
2187 _bfd_munmap_temporary (esymbuf_addr, esymbuf_size);
2188 free (gnubuckets);
2189 free (gnuchains);
2190 free (mipsxlat);
2191 return res;
2194 /* Reconstruct section from dynamic symbol. */
2196 asection *
2197 _bfd_elf_get_section_from_dynamic_symbol (bfd *abfd,
2198 Elf_Internal_Sym *isym)
2200 asection *sec;
2201 flagword flags;
2203 if (!elf_use_dt_symtab_p (abfd))
2204 return NULL;
2206 flags = SEC_ALLOC | SEC_LOAD;
2207 switch (ELF_ST_TYPE (isym->st_info))
2209 case STT_FUNC:
2210 case STT_GNU_IFUNC:
2211 sec = bfd_get_section_by_name (abfd, ".text");
2212 if (sec == NULL)
2213 sec = bfd_make_section_with_flags (abfd,
2214 ".text",
2215 flags | SEC_CODE);
2216 break;
2217 case STT_COMMON:
2218 sec = bfd_com_section_ptr;
2219 break;
2220 case STT_OBJECT:
2221 sec = bfd_get_section_by_name (abfd, ".data");
2222 if (sec == NULL)
2223 sec = bfd_make_section_with_flags (abfd,
2224 ".data",
2225 flags | SEC_DATA);
2226 break;
2227 case STT_TLS:
2228 sec = bfd_get_section_by_name (abfd, ".tdata");
2229 if (sec == NULL)
2230 sec = bfd_make_section_with_flags (abfd,
2231 ".tdata",
2232 (flags
2233 | SEC_DATA
2234 | SEC_THREAD_LOCAL));
2235 break;
2236 default:
2237 sec = bfd_abs_section_ptr;
2238 break;
2241 return sec;
2244 /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
2245 and return symbol version for symbol version itself. */
2247 const char *
2248 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
2249 bool base_p,
2250 bool *hidden)
2252 const char *version_string = NULL;
2253 if ((elf_dynversym (abfd) != 0
2254 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
2255 || (elf_tdata (abfd)->dt_versym != NULL
2256 && (elf_tdata (abfd)->dt_verdef != NULL
2257 || elf_tdata (abfd)->dt_verneed != NULL)))
2259 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
2261 *hidden = (vernum & VERSYM_HIDDEN) != 0;
2262 vernum &= VERSYM_VERSION;
2264 if (vernum == 0)
2265 version_string = "";
2266 else if (vernum == 1
2267 && (vernum > elf_tdata (abfd)->cverdefs
2268 || (elf_tdata (abfd)->verdef[0].vd_flags
2269 == VER_FLG_BASE)))
2270 version_string = base_p ? "Base" : "";
2271 else if (vernum <= elf_tdata (abfd)->cverdefs)
2273 const char *nodename
2274 = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
2275 version_string = "";
2276 if (base_p
2277 || nodename == NULL
2278 || symbol->name == NULL
2279 || strcmp (symbol->name, nodename) != 0)
2280 version_string = nodename;
2282 else
2284 Elf_Internal_Verneed *t;
2286 version_string = _("<corrupt>");
2287 for (t = elf_tdata (abfd)->verref;
2288 t != NULL;
2289 t = t->vn_nextref)
2291 Elf_Internal_Vernaux *a;
2293 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2295 if (a->vna_other == vernum)
2297 *hidden = true;
2298 version_string = a->vna_nodename;
2299 break;
2305 return version_string;
2308 /* Display ELF-specific fields of a symbol. */
2310 void
2311 bfd_elf_print_symbol (bfd *abfd,
2312 void *filep,
2313 asymbol *symbol,
2314 bfd_print_symbol_type how)
2316 FILE *file = (FILE *) filep;
2317 const char *symname = (symbol->name != bfd_symbol_error_name
2318 ? symbol->name : _("<corrupt>"));
2320 switch (how)
2322 case bfd_print_symbol_name:
2323 fprintf (file, "%s", symname);
2324 break;
2325 case bfd_print_symbol_more:
2326 fprintf (file, "elf ");
2327 bfd_fprintf_vma (abfd, file, symbol->value);
2328 fprintf (file, " %x", symbol->flags);
2329 break;
2330 case bfd_print_symbol_all:
2332 const char *section_name;
2333 const char *name = NULL;
2334 const struct elf_backend_data *bed;
2335 unsigned char st_other;
2336 bfd_vma val;
2337 const char *version_string;
2338 bool hidden;
2340 section_name = symbol->section ? symbol->section->name : "(*none*)";
2342 bed = get_elf_backend_data (abfd);
2343 if (bed->elf_backend_print_symbol_all)
2344 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
2346 if (name != NULL)
2347 symname = name;
2348 else
2349 bfd_print_symbol_vandf (abfd, file, symbol);
2351 fprintf (file, " %s\t", section_name);
2352 /* Print the "other" value for a symbol. For common symbols,
2353 we've already printed the size; now print the alignment.
2354 For other symbols, we have no specified alignment, and
2355 we've printed the address; now print the size. */
2356 if (symbol->section && bfd_is_com_section (symbol->section))
2357 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
2358 else
2359 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
2360 bfd_fprintf_vma (abfd, file, val);
2362 /* If we have version information, print it. */
2363 version_string = _bfd_elf_get_symbol_version_string (abfd,
2364 symbol,
2365 true,
2366 &hidden);
2367 if (version_string)
2369 if (!hidden)
2370 fprintf (file, " %-11s", version_string);
2371 else
2373 int i;
2375 fprintf (file, " (%s)", version_string);
2376 for (i = 10 - strlen (version_string); i > 0; --i)
2377 putc (' ', file);
2381 /* If the st_other field is not zero, print it. */
2382 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
2384 switch (st_other)
2386 case 0: break;
2387 case STV_INTERNAL: fprintf (file, " .internal"); break;
2388 case STV_HIDDEN: fprintf (file, " .hidden"); break;
2389 case STV_PROTECTED: fprintf (file, " .protected"); break;
2390 default:
2391 /* Some other non-defined flags are also present, so print
2392 everything hex. */
2393 fprintf (file, " 0x%02x", (unsigned int) st_other);
2396 fprintf (file, " %s", symname);
2398 break;
2402 /* ELF .o/exec file reading */
2404 /* Create a new bfd section from an ELF section header. */
2406 bool
2407 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
2409 Elf_Internal_Shdr *hdr;
2410 Elf_Internal_Ehdr *ehdr;
2411 const struct elf_backend_data *bed;
2412 const char *name;
2413 bool ret = true;
2415 if (shindex >= elf_numsections (abfd))
2416 return false;
2418 /* PR17512: A corrupt ELF binary might contain a loop of sections via
2419 sh_link or sh_info. Detect this here, by refusing to load a
2420 section that we are already in the process of loading. */
2421 if (elf_tdata (abfd)->being_created[shindex])
2423 _bfd_error_handler
2424 (_("%pB: warning: loop in section dependencies detected"), abfd);
2425 return false;
2427 elf_tdata (abfd)->being_created[shindex] = true;
2429 hdr = elf_elfsections (abfd)[shindex];
2430 ehdr = elf_elfheader (abfd);
2431 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2432 hdr->sh_name);
2433 if (name == NULL)
2434 goto fail;
2436 bed = get_elf_backend_data (abfd);
2437 switch (hdr->sh_type)
2439 case SHT_NULL:
2440 /* Inactive section. Throw it away. */
2441 goto success;
2443 case SHT_PROGBITS: /* Normal section with contents. */
2444 case SHT_NOBITS: /* .bss section. */
2445 case SHT_HASH: /* .hash section. */
2446 case SHT_NOTE: /* .note section. */
2447 case SHT_INIT_ARRAY: /* .init_array section. */
2448 case SHT_FINI_ARRAY: /* .fini_array section. */
2449 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
2450 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
2451 case SHT_GNU_HASH: /* .gnu.hash section. */
2452 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2453 goto success;
2455 case SHT_DYNAMIC: /* Dynamic linking information. */
2456 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2457 goto fail;
2459 if (hdr->sh_link > elf_numsections (abfd))
2461 /* PR 10478: Accept Solaris binaries with a sh_link field
2462 set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1). */
2463 switch (bfd_get_arch (abfd))
2465 case bfd_arch_i386:
2466 case bfd_arch_sparc:
2467 if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
2468 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
2469 break;
2470 /* Otherwise fall through. */
2471 default:
2472 goto fail;
2475 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2476 goto fail;
2477 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2479 Elf_Internal_Shdr *dynsymhdr;
2481 /* The shared libraries distributed with hpux11 have a bogus
2482 sh_link field for the ".dynamic" section. Find the
2483 string table for the ".dynsym" section instead. */
2484 if (elf_dynsymtab (abfd) != 0)
2486 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2487 hdr->sh_link = dynsymhdr->sh_link;
2489 else
2491 unsigned int i, num_sec;
2493 num_sec = elf_numsections (abfd);
2494 for (i = 1; i < num_sec; i++)
2496 dynsymhdr = elf_elfsections (abfd)[i];
2497 if (dynsymhdr->sh_type == SHT_DYNSYM)
2499 hdr->sh_link = dynsymhdr->sh_link;
2500 break;
2505 goto success;
2507 case SHT_SYMTAB: /* A symbol table. */
2508 if (elf_onesymtab (abfd) == shindex)
2509 goto success;
2511 if (hdr->sh_entsize != bed->s->sizeof_sym)
2512 goto fail;
2514 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2516 if (hdr->sh_size != 0)
2517 goto fail;
2518 /* Some assemblers erroneously set sh_info to one with a
2519 zero sh_size. ld sees this as a global symbol count
2520 of (unsigned) -1. Fix it here. */
2521 hdr->sh_info = 0;
2522 goto success;
2525 /* PR 18854: A binary might contain more than one symbol table.
2526 Unusual, but possible. Warn, but continue. */
2527 if (elf_onesymtab (abfd) != 0)
2529 _bfd_error_handler
2530 /* xgettext:c-format */
2531 (_("%pB: warning: multiple symbol tables detected"
2532 " - ignoring the table in section %u"),
2533 abfd, shindex);
2534 goto success;
2536 elf_onesymtab (abfd) = shindex;
2537 elf_symtab_hdr (abfd) = *hdr;
2538 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2539 abfd->flags |= HAS_SYMS;
2541 /* Sometimes a shared object will map in the symbol table. If
2542 SHF_ALLOC is set, and this is a shared object, then we also
2543 treat this section as a BFD section. We can not base the
2544 decision purely on SHF_ALLOC, because that flag is sometimes
2545 set in a relocatable object file, which would confuse the
2546 linker. */
2547 if ((hdr->sh_flags & SHF_ALLOC) != 0
2548 && (abfd->flags & DYNAMIC) != 0
2549 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2550 shindex))
2551 goto fail;
2553 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2554 can't read symbols without that section loaded as well. It
2555 is most likely specified by the next section header. */
2557 elf_section_list * entry;
2558 unsigned int i, num_sec;
2560 for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2561 if (entry->hdr.sh_link == shindex)
2562 goto success;
2564 num_sec = elf_numsections (abfd);
2565 for (i = shindex + 1; i < num_sec; i++)
2567 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2569 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2570 && hdr2->sh_link == shindex)
2571 break;
2574 if (i == num_sec)
2575 for (i = 1; i < shindex; i++)
2577 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2579 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2580 && hdr2->sh_link == shindex)
2581 break;
2584 if (i != shindex)
2585 ret = bfd_section_from_shdr (abfd, i);
2586 /* else FIXME: we have failed to find the symbol table.
2587 Should we issue an error? */
2588 goto success;
2591 case SHT_DYNSYM: /* A dynamic symbol table. */
2592 if (elf_dynsymtab (abfd) == shindex)
2593 goto success;
2595 if (hdr->sh_entsize != bed->s->sizeof_sym)
2596 goto fail;
2598 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2600 if (hdr->sh_size != 0)
2601 goto fail;
2603 /* Some linkers erroneously set sh_info to one with a
2604 zero sh_size. ld sees this as a global symbol count
2605 of (unsigned) -1. Fix it here. */
2606 hdr->sh_info = 0;
2607 goto success;
2610 /* PR 18854: A binary might contain more than one dynamic symbol table.
2611 Unusual, but possible. Warn, but continue. */
2612 if (elf_dynsymtab (abfd) != 0)
2614 _bfd_error_handler
2615 /* xgettext:c-format */
2616 (_("%pB: warning: multiple dynamic symbol tables detected"
2617 " - ignoring the table in section %u"),
2618 abfd, shindex);
2619 goto success;
2621 elf_dynsymtab (abfd) = shindex;
2622 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2623 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2624 abfd->flags |= HAS_SYMS;
2626 /* Besides being a symbol table, we also treat this as a regular
2627 section, so that objcopy can handle it. */
2628 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2629 goto success;
2631 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
2633 elf_section_list * entry;
2635 for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2636 if (entry->ndx == shindex)
2637 goto success;
2639 entry = bfd_alloc (abfd, sizeof (*entry));
2640 if (entry == NULL)
2641 goto fail;
2642 entry->ndx = shindex;
2643 entry->hdr = * hdr;
2644 entry->next = elf_symtab_shndx_list (abfd);
2645 elf_symtab_shndx_list (abfd) = entry;
2646 elf_elfsections (abfd)[shindex] = & entry->hdr;
2647 goto success;
2650 case SHT_STRTAB: /* A string table. */
2651 if (hdr->bfd_section != NULL)
2652 goto success;
2654 if (ehdr->e_shstrndx == shindex)
2656 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2657 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2658 goto success;
2661 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2663 symtab_strtab:
2664 elf_tdata (abfd)->strtab_hdr = *hdr;
2665 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2666 goto success;
2669 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2671 dynsymtab_strtab:
2672 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2673 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2674 elf_elfsections (abfd)[shindex] = hdr;
2675 /* We also treat this as a regular section, so that objcopy
2676 can handle it. */
2677 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2678 shindex);
2679 goto success;
2682 /* If the string table isn't one of the above, then treat it as a
2683 regular section. We need to scan all the headers to be sure,
2684 just in case this strtab section appeared before the above. */
2685 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2687 unsigned int i, num_sec;
2689 num_sec = elf_numsections (abfd);
2690 for (i = 1; i < num_sec; i++)
2692 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2693 if (hdr2->sh_link == shindex)
2695 /* Prevent endless recursion on broken objects. */
2696 if (i == shindex)
2697 goto fail;
2698 if (! bfd_section_from_shdr (abfd, i))
2699 goto fail;
2700 if (elf_onesymtab (abfd) == i)
2701 goto symtab_strtab;
2702 if (elf_dynsymtab (abfd) == i)
2703 goto dynsymtab_strtab;
2707 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2708 goto success;
2710 case SHT_REL:
2711 case SHT_RELA:
2712 case SHT_RELR:
2713 /* *These* do a lot of work -- but build no sections! */
2715 asection *target_sect;
2716 Elf_Internal_Shdr *hdr2, **p_hdr;
2717 unsigned int num_sec = elf_numsections (abfd);
2718 struct bfd_elf_section_data *esdt;
2719 bfd_size_type size;
2721 if (hdr->sh_type == SHT_REL)
2722 size = bed->s->sizeof_rel;
2723 else if (hdr->sh_type == SHT_RELA)
2724 size = bed->s->sizeof_rela;
2725 else
2726 size = bed->s->arch_size / 8;
2727 if (hdr->sh_entsize != size)
2728 goto fail;
2730 /* Check for a bogus link to avoid crashing. */
2731 if (hdr->sh_link >= num_sec)
2733 _bfd_error_handler
2734 /* xgettext:c-format */
2735 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2736 abfd, hdr->sh_link, name, shindex);
2737 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2738 goto success;
2741 /* Get the symbol table. */
2742 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2743 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2744 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2745 goto fail;
2747 /* If this is an alloc section in an executable or shared
2748 library, or the reloc section does not use the main symbol
2749 table we don't treat it as a reloc section. BFD can't
2750 adequately represent such a section, so at least for now,
2751 we don't try. We just present it as a normal section. We
2752 also can't use it as a reloc section if it points to the
2753 null section, an invalid section, another reloc section, or
2754 its sh_link points to the null section. */
2755 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2756 && (hdr->sh_flags & SHF_ALLOC) != 0)
2757 || (hdr->sh_flags & SHF_COMPRESSED) != 0
2758 || hdr->sh_type == SHT_RELR
2759 || hdr->sh_link == SHN_UNDEF
2760 || hdr->sh_link != elf_onesymtab (abfd)
2761 || hdr->sh_info == SHN_UNDEF
2762 || hdr->sh_info >= num_sec
2763 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2764 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2766 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2767 goto success;
2770 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2771 goto fail;
2773 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2774 if (target_sect == NULL)
2775 goto fail;
2777 esdt = elf_section_data (target_sect);
2778 if (hdr->sh_type == SHT_RELA)
2779 p_hdr = &esdt->rela.hdr;
2780 else
2781 p_hdr = &esdt->rel.hdr;
2783 /* PR 17512: file: 0b4f81b7.
2784 Also see PR 24456, for a file which deliberately has two reloc
2785 sections. */
2786 if (*p_hdr != NULL)
2788 if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
2790 _bfd_error_handler
2791 /* xgettext:c-format */
2792 (_("%pB: warning: secondary relocation section '%s' "
2793 "for section %pA found - ignoring"),
2794 abfd, name, target_sect);
2796 else
2797 esdt->has_secondary_relocs = true;
2798 goto success;
2801 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
2802 if (hdr2 == NULL)
2803 goto fail;
2804 *hdr2 = *hdr;
2805 *p_hdr = hdr2;
2806 elf_elfsections (abfd)[shindex] = hdr2;
2807 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2808 * bed->s->int_rels_per_ext_rel);
2809 target_sect->flags |= SEC_RELOC;
2810 target_sect->relocation = NULL;
2811 target_sect->rel_filepos = hdr->sh_offset;
2812 /* In the section to which the relocations apply, mark whether
2813 its relocations are of the REL or RELA variety. */
2814 if (hdr->sh_size != 0)
2816 if (hdr->sh_type == SHT_RELA)
2817 target_sect->use_rela_p = 1;
2819 abfd->flags |= HAS_RELOC;
2820 goto success;
2823 case SHT_GNU_verdef:
2824 if (hdr->sh_info != 0)
2825 elf_dynverdef (abfd) = shindex;
2826 elf_tdata (abfd)->dynverdef_hdr = *hdr;
2827 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2828 goto success;
2830 case SHT_GNU_versym:
2831 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2832 goto fail;
2834 elf_dynversym (abfd) = shindex;
2835 elf_tdata (abfd)->dynversym_hdr = *hdr;
2836 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2837 goto success;
2839 case SHT_GNU_verneed:
2840 if (hdr->sh_info != 0)
2841 elf_dynverref (abfd) = shindex;
2842 elf_tdata (abfd)->dynverref_hdr = *hdr;
2843 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2844 goto success;
2846 case SHT_SHLIB:
2847 goto success;
2849 case SHT_GROUP:
2850 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2851 goto fail;
2853 goto success;
2855 default:
2856 /* Possibly an attributes section. */
2857 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2858 || hdr->sh_type == bed->obj_attrs_section_type)
2860 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2861 goto fail;
2862 _bfd_elf_parse_attributes (abfd, hdr);
2863 goto success;
2866 /* Check for any processor-specific section types. */
2867 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2868 goto success;
2870 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2872 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2873 /* FIXME: How to properly handle allocated section reserved
2874 for applications? */
2875 _bfd_error_handler
2876 /* xgettext:c-format */
2877 (_("%pB: unknown type [%#x] section `%s'"),
2878 abfd, hdr->sh_type, name);
2879 else
2881 /* Allow sections reserved for applications. */
2882 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2883 goto success;
2886 else if (hdr->sh_type >= SHT_LOPROC
2887 && hdr->sh_type <= SHT_HIPROC)
2888 /* FIXME: We should handle this section. */
2889 _bfd_error_handler
2890 /* xgettext:c-format */
2891 (_("%pB: unknown type [%#x] section `%s'"),
2892 abfd, hdr->sh_type, name);
2893 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2895 /* Unrecognised OS-specific sections. */
2896 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2897 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2898 required to correctly process the section and the file should
2899 be rejected with an error message. */
2900 _bfd_error_handler
2901 /* xgettext:c-format */
2902 (_("%pB: unknown type [%#x] section `%s'"),
2903 abfd, hdr->sh_type, name);
2904 else
2906 /* Otherwise it should be processed. */
2907 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2908 goto success;
2911 else
2912 /* FIXME: We should handle this section. */
2913 _bfd_error_handler
2914 /* xgettext:c-format */
2915 (_("%pB: unknown type [%#x] section `%s'"),
2916 abfd, hdr->sh_type, name);
2918 goto fail;
2921 fail:
2922 ret = false;
2923 success:
2924 elf_tdata (abfd)->being_created[shindex] = false;
2925 return ret;
2928 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2930 Elf_Internal_Sym *
2931 bfd_sym_from_r_symndx (struct sym_cache *cache,
2932 bfd *abfd,
2933 unsigned long r_symndx)
2935 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2937 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2939 Elf_Internal_Shdr *symtab_hdr;
2940 unsigned char esym[sizeof (Elf64_External_Sym)];
2941 Elf_External_Sym_Shndx eshndx;
2943 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2944 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2945 &cache->sym[ent], esym, &eshndx) == NULL)
2946 return NULL;
2948 if (cache->abfd != abfd)
2950 memset (cache->indx, -1, sizeof (cache->indx));
2951 cache->abfd = abfd;
2953 cache->indx[ent] = r_symndx;
2956 return &cache->sym[ent];
2959 /* Given an ELF section number, retrieve the corresponding BFD
2960 section. */
2962 asection *
2963 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2965 if (sec_index >= elf_numsections (abfd))
2966 return NULL;
2967 return elf_elfsections (abfd)[sec_index]->bfd_section;
2970 static const struct bfd_elf_special_section special_sections_b[] =
2972 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2973 { NULL, 0, 0, 0, 0 }
2976 static const struct bfd_elf_special_section special_sections_c[] =
2978 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2979 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 },
2980 { NULL, 0, 0, 0, 0 }
2983 static const struct bfd_elf_special_section special_sections_d[] =
2985 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2986 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2987 /* There are more DWARF sections than these, but they needn't be added here
2988 unless you have to cope with broken compilers that don't emit section
2989 attributes or you want to help the user writing assembler. */
2990 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2991 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2992 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2993 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2994 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2995 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2996 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2997 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2998 { NULL, 0, 0, 0, 0 }
3001 static const struct bfd_elf_special_section special_sections_f[] =
3003 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3004 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
3005 { NULL, 0 , 0, 0, 0 }
3008 static const struct bfd_elf_special_section special_sections_g[] =
3010 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3011 { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3012 { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3013 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
3014 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3015 { STRING_COMMA_LEN (".gnu_object_only"), 0, SHT_GNU_OBJECT_ONLY, SHF_EXCLUDE },
3016 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
3017 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
3018 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
3019 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
3020 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
3021 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
3022 { NULL, 0, 0, 0, 0 }
3025 static const struct bfd_elf_special_section special_sections_h[] =
3027 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
3028 { NULL, 0, 0, 0, 0 }
3031 static const struct bfd_elf_special_section special_sections_i[] =
3033 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3034 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3035 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
3036 { NULL, 0, 0, 0, 0 }
3039 static const struct bfd_elf_special_section special_sections_l[] =
3041 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
3042 { NULL, 0, 0, 0, 0 }
3045 static const struct bfd_elf_special_section special_sections_n[] =
3047 { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3048 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
3049 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
3050 { NULL, 0, 0, 0, 0 }
3053 static const struct bfd_elf_special_section special_sections_p[] =
3055 { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3056 { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3057 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3058 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3059 { NULL, 0, 0, 0, 0 }
3062 static const struct bfd_elf_special_section special_sections_r[] =
3064 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
3065 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
3066 { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
3067 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
3068 /* .relro_padding is generated by lld. It should not be confused with a
3069 reloc containing section, because otherwise elf_fake_sections() will
3070 set the entsize to 8, which may not be an actual multiple of the
3071 section's size.
3072 Note - this entry must appear before the ".rel" entry below. */
3073 { STRING_COMMA_LEN (".relro_padding"), 0, SHT_NOBITS, SHF_ALLOC | SHF_WRITE },
3074 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
3075 { NULL, 0, 0, 0, 0 }
3078 static const struct bfd_elf_special_section special_sections_s[] =
3080 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
3081 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
3082 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
3083 /* See struct bfd_elf_special_section declaration for the semantics of
3084 this special case where .prefix_length != strlen (.prefix). */
3085 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
3086 { NULL, 0, 0, 0, 0 }
3089 static const struct bfd_elf_special_section special_sections_t[] =
3091 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3092 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
3093 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
3094 { NULL, 0, 0, 0, 0 }
3097 static const struct bfd_elf_special_section special_sections_z[] =
3099 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
3100 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
3101 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
3102 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
3103 { NULL, 0, 0, 0, 0 }
3106 static const struct bfd_elf_special_section * const special_sections[] =
3108 special_sections_b, /* 'b' */
3109 special_sections_c, /* 'c' */
3110 special_sections_d, /* 'd' */
3111 NULL, /* 'e' */
3112 special_sections_f, /* 'f' */
3113 special_sections_g, /* 'g' */
3114 special_sections_h, /* 'h' */
3115 special_sections_i, /* 'i' */
3116 NULL, /* 'j' */
3117 NULL, /* 'k' */
3118 special_sections_l, /* 'l' */
3119 NULL, /* 'm' */
3120 special_sections_n, /* 'n' */
3121 NULL, /* 'o' */
3122 special_sections_p, /* 'p' */
3123 NULL, /* 'q' */
3124 special_sections_r, /* 'r' */
3125 special_sections_s, /* 's' */
3126 special_sections_t, /* 't' */
3127 NULL, /* 'u' */
3128 NULL, /* 'v' */
3129 NULL, /* 'w' */
3130 NULL, /* 'x' */
3131 NULL, /* 'y' */
3132 special_sections_z /* 'z' */
3135 const struct bfd_elf_special_section *
3136 _bfd_elf_get_special_section (const char *name,
3137 const struct bfd_elf_special_section *spec,
3138 unsigned int rela)
3140 int i;
3141 int len;
3143 len = strlen (name);
3145 for (i = 0; spec[i].prefix != NULL; i++)
3147 int suffix_len;
3148 int prefix_len = spec[i].prefix_length;
3150 if (len < prefix_len)
3151 continue;
3152 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
3153 continue;
3155 suffix_len = spec[i].suffix_length;
3156 if (suffix_len <= 0)
3158 if (name[prefix_len] != 0)
3160 if (suffix_len == 0)
3161 continue;
3162 if (name[prefix_len] != '.'
3163 && (suffix_len == -2
3164 || (rela && spec[i].type == SHT_REL)))
3165 continue;
3168 else
3170 if (len < prefix_len + suffix_len)
3171 continue;
3172 if (memcmp (name + len - suffix_len,
3173 spec[i].prefix + prefix_len,
3174 suffix_len) != 0)
3175 continue;
3177 return &spec[i];
3180 return NULL;
3183 const struct bfd_elf_special_section *
3184 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
3186 int i;
3187 const struct bfd_elf_special_section *spec;
3188 const struct elf_backend_data *bed;
3190 /* See if this is one of the special sections. */
3191 if (sec->name == NULL)
3192 return NULL;
3194 bed = get_elf_backend_data (abfd);
3195 spec = bed->special_sections;
3196 if (spec)
3198 spec = _bfd_elf_get_special_section (sec->name,
3199 bed->special_sections,
3200 sec->use_rela_p);
3201 if (spec != NULL)
3202 return spec;
3205 if (sec->name[0] != '.')
3206 return NULL;
3208 i = sec->name[1] - 'b';
3209 if (i < 0 || i > 'z' - 'b')
3210 return NULL;
3212 spec = special_sections[i];
3214 if (spec == NULL)
3215 return NULL;
3217 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
3220 bool
3221 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
3223 struct bfd_elf_section_data *sdata;
3224 const struct elf_backend_data *bed;
3225 const struct bfd_elf_special_section *ssect;
3227 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
3228 if (sdata == NULL)
3230 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
3231 sizeof (*sdata));
3232 if (sdata == NULL)
3233 return false;
3234 sec->used_by_bfd = sdata;
3237 /* Indicate whether or not this section should use RELA relocations. */
3238 bed = get_elf_backend_data (abfd);
3239 sec->use_rela_p = bed->default_use_rela_p;
3241 /* Set up ELF section type and flags for newly created sections, if
3242 there is an ABI mandated section. */
3243 ssect = (*bed->get_sec_type_attr) (abfd, sec);
3244 if (ssect != NULL)
3246 elf_section_type (sec) = ssect->type;
3247 elf_section_flags (sec) = ssect->attr;
3250 return _bfd_generic_new_section_hook (abfd, sec);
3253 /* Create a new bfd section from an ELF program header.
3255 Since program segments have no names, we generate a synthetic name
3256 of the form segment<NUM>, where NUM is generally the index in the
3257 program header table. For segments that are split (see below) we
3258 generate the names segment<NUM>a and segment<NUM>b.
3260 Note that some program segments may have a file size that is different than
3261 (less than) the memory size. All this means is that at execution the
3262 system must allocate the amount of memory specified by the memory size,
3263 but only initialize it with the first "file size" bytes read from the
3264 file. This would occur for example, with program segments consisting
3265 of combined data+bss.
3267 To handle the above situation, this routine generates TWO bfd sections
3268 for the single program segment. The first has the length specified by
3269 the file size of the segment, and the second has the length specified
3270 by the difference between the two sizes. In effect, the segment is split
3271 into its initialized and uninitialized parts. */
3273 bool
3274 _bfd_elf_make_section_from_phdr (bfd *abfd,
3275 Elf_Internal_Phdr *hdr,
3276 int hdr_index,
3277 const char *type_name)
3279 asection *newsect;
3280 char *name;
3281 char namebuf[64];
3282 size_t len;
3283 int split;
3284 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
3286 split = ((hdr->p_memsz > 0)
3287 && (hdr->p_filesz > 0)
3288 && (hdr->p_memsz > hdr->p_filesz));
3290 if (hdr->p_filesz > 0)
3292 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
3293 len = strlen (namebuf) + 1;
3294 name = (char *) bfd_alloc (abfd, len);
3295 if (!name)
3296 return false;
3297 memcpy (name, namebuf, len);
3298 newsect = bfd_make_section (abfd, name);
3299 if (newsect == NULL)
3300 return false;
3301 newsect->vma = hdr->p_vaddr / opb;
3302 newsect->lma = hdr->p_paddr / opb;
3303 newsect->size = hdr->p_filesz;
3304 newsect->filepos = hdr->p_offset;
3305 newsect->flags |= SEC_HAS_CONTENTS;
3306 newsect->alignment_power = bfd_log2 (hdr->p_align);
3307 if (hdr->p_type == PT_LOAD)
3309 newsect->flags |= SEC_ALLOC;
3310 newsect->flags |= SEC_LOAD;
3311 if (hdr->p_flags & PF_X)
3313 /* FIXME: all we known is that it has execute PERMISSION,
3314 may be data. */
3315 newsect->flags |= SEC_CODE;
3318 if (!(hdr->p_flags & PF_W))
3320 newsect->flags |= SEC_READONLY;
3324 if (hdr->p_memsz > hdr->p_filesz)
3326 bfd_vma align;
3328 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
3329 len = strlen (namebuf) + 1;
3330 name = (char *) bfd_alloc (abfd, len);
3331 if (!name)
3332 return false;
3333 memcpy (name, namebuf, len);
3334 newsect = bfd_make_section (abfd, name);
3335 if (newsect == NULL)
3336 return false;
3337 newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
3338 newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
3339 newsect->size = hdr->p_memsz - hdr->p_filesz;
3340 newsect->filepos = hdr->p_offset + hdr->p_filesz;
3341 align = newsect->vma & -newsect->vma;
3342 if (align == 0 || align > hdr->p_align)
3343 align = hdr->p_align;
3344 newsect->alignment_power = bfd_log2 (align);
3345 if (hdr->p_type == PT_LOAD)
3347 newsect->flags |= SEC_ALLOC;
3348 if (hdr->p_flags & PF_X)
3349 newsect->flags |= SEC_CODE;
3351 if (!(hdr->p_flags & PF_W))
3352 newsect->flags |= SEC_READONLY;
3355 return true;
3358 static bool
3359 _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3361 /* The return value is ignored. Build-ids are considered optional. */
3362 if (templ->xvec->flavour == bfd_target_elf_flavour)
3363 return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3364 (templ, offset);
3365 return false;
3368 bool
3369 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
3371 const struct elf_backend_data *bed;
3373 switch (hdr->p_type)
3375 case PT_NULL:
3376 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3378 case PT_LOAD:
3379 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3380 return false;
3381 if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3382 _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3383 return true;
3385 case PT_DYNAMIC:
3386 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3388 case PT_INTERP:
3389 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3391 case PT_NOTE:
3392 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3393 return false;
3394 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3395 hdr->p_align))
3396 return false;
3397 return true;
3399 case PT_SHLIB:
3400 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3402 case PT_PHDR:
3403 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3405 case PT_GNU_EH_FRAME:
3406 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3407 "eh_frame_hdr");
3409 case PT_GNU_STACK:
3410 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3412 case PT_GNU_RELRO:
3413 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3415 case PT_GNU_SFRAME:
3416 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3417 "sframe");
3419 default:
3420 /* Check for any processor-specific program segment types. */
3421 bed = get_elf_backend_data (abfd);
3422 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3426 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3427 REL or RELA. */
3429 Elf_Internal_Shdr *
3430 _bfd_elf_single_rel_hdr (asection *sec)
3432 if (elf_section_data (sec)->rel.hdr)
3434 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3435 return elf_section_data (sec)->rel.hdr;
3437 else
3438 return elf_section_data (sec)->rela.hdr;
3441 static bool
3442 _bfd_elf_set_reloc_sh_name (bfd *abfd,
3443 Elf_Internal_Shdr *rel_hdr,
3444 const char *sec_name,
3445 bool use_rela_p)
3447 char *name = (char *) bfd_alloc (abfd,
3448 sizeof ".rela" + strlen (sec_name));
3449 if (name == NULL)
3450 return false;
3452 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3453 rel_hdr->sh_name =
3454 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3455 false);
3456 if (rel_hdr->sh_name == (unsigned int) -1)
3457 return false;
3459 return true;
3462 /* Allocate and initialize a section-header for a new reloc section,
3463 containing relocations against ASECT. It is stored in RELDATA. If
3464 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3465 relocations. */
3467 static bool
3468 _bfd_elf_init_reloc_shdr (bfd *abfd,
3469 struct bfd_elf_section_reloc_data *reldata,
3470 const char *sec_name,
3471 bool use_rela_p,
3472 bool delay_st_name_p)
3474 Elf_Internal_Shdr *rel_hdr;
3475 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3477 BFD_ASSERT (reldata->hdr == NULL);
3478 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3479 if (rel_hdr == NULL)
3480 return false;
3481 reldata->hdr = rel_hdr;
3483 if (delay_st_name_p)
3484 rel_hdr->sh_name = (unsigned int) -1;
3485 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3486 use_rela_p))
3487 return false;
3488 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3489 rel_hdr->sh_entsize = (use_rela_p
3490 ? bed->s->sizeof_rela
3491 : bed->s->sizeof_rel);
3492 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3493 rel_hdr->sh_flags = 0;
3494 rel_hdr->sh_addr = 0;
3495 rel_hdr->sh_size = 0;
3496 rel_hdr->sh_offset = 0;
3498 return true;
3501 /* Return the default section type based on the passed in section flags. */
3504 bfd_elf_get_default_section_type (flagword flags)
3506 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3507 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3508 return SHT_NOBITS;
3509 return SHT_PROGBITS;
3512 struct fake_section_arg
3514 struct bfd_link_info *link_info;
3515 bool failed;
3518 /* Set up an ELF internal section header for a section. */
3520 static void
3521 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3523 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3524 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3525 struct bfd_elf_section_data *esd = elf_section_data (asect);
3526 Elf_Internal_Shdr *this_hdr;
3527 unsigned int sh_type;
3528 const char *name = asect->name;
3529 bool delay_st_name_p = false;
3530 bfd_vma mask;
3532 if (arg->failed)
3534 /* We already failed; just get out of the bfd_map_over_sections
3535 loop. */
3536 return;
3539 this_hdr = &esd->this_hdr;
3541 /* ld: compress DWARF debug sections with names: .debug_*. */
3542 if (arg->link_info
3543 && (abfd->flags & BFD_COMPRESS) != 0
3544 && (asect->flags & SEC_DEBUGGING) != 0
3545 && name[1] == 'd'
3546 && name[6] == '_')
3548 /* If this section will be compressed, delay adding section
3549 name to section name section after it is compressed in
3550 _bfd_elf_assign_file_positions_for_non_load. */
3551 delay_st_name_p = true;
3554 if (delay_st_name_p)
3555 this_hdr->sh_name = (unsigned int) -1;
3556 else
3558 this_hdr->sh_name
3559 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3560 name, false);
3561 if (this_hdr->sh_name == (unsigned int) -1)
3563 arg->failed = true;
3564 return;
3568 /* Don't clear sh_flags. Assembler may set additional bits. */
3570 if ((asect->flags & SEC_ALLOC) != 0
3571 || asect->user_set_vma)
3572 this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
3573 else
3574 this_hdr->sh_addr = 0;
3576 this_hdr->sh_offset = 0;
3577 this_hdr->sh_size = asect->size;
3578 this_hdr->sh_link = 0;
3579 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3580 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3582 _bfd_error_handler
3583 /* xgettext:c-format */
3584 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3585 abfd, asect->alignment_power, asect);
3586 arg->failed = true;
3587 return;
3589 /* Set sh_addralign to the highest power of two given by alignment
3590 consistent with the section VMA. Linker scripts can force VMA. */
3591 mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3592 this_hdr->sh_addralign = mask & -mask;
3593 /* The sh_entsize and sh_info fields may have been set already by
3594 copy_private_section_data. */
3596 this_hdr->bfd_section = asect;
3597 this_hdr->contents = NULL;
3599 /* If the section type is unspecified, we set it based on
3600 asect->flags. */
3601 if (asect->type != 0)
3602 sh_type = asect->type;
3603 else if ((asect->flags & SEC_GROUP) != 0)
3604 sh_type = SHT_GROUP;
3605 else
3606 sh_type = bfd_elf_get_default_section_type (asect->flags);
3608 if (this_hdr->sh_type == SHT_NULL)
3609 this_hdr->sh_type = sh_type;
3610 else if (this_hdr->sh_type == SHT_NOBITS
3611 && sh_type == SHT_PROGBITS
3612 && (asect->flags & SEC_ALLOC) != 0)
3614 /* Warn if we are changing a NOBITS section to PROGBITS, but
3615 allow the link to proceed. This can happen when users link
3616 non-bss input sections to bss output sections, or emit data
3617 to a bss output section via a linker script. */
3618 _bfd_error_handler
3619 (_("warning: section `%pA' type changed to PROGBITS"), asect);
3620 this_hdr->sh_type = sh_type;
3623 switch (this_hdr->sh_type)
3625 default:
3626 break;
3628 case SHT_STRTAB:
3629 case SHT_NOTE:
3630 case SHT_NOBITS:
3631 case SHT_PROGBITS:
3632 break;
3634 case SHT_INIT_ARRAY:
3635 case SHT_FINI_ARRAY:
3636 case SHT_PREINIT_ARRAY:
3637 this_hdr->sh_entsize = bed->s->arch_size / 8;
3638 break;
3640 case SHT_HASH:
3641 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3642 break;
3644 case SHT_DYNSYM:
3645 this_hdr->sh_entsize = bed->s->sizeof_sym;
3646 break;
3648 case SHT_DYNAMIC:
3649 this_hdr->sh_entsize = bed->s->sizeof_dyn;
3650 break;
3652 case SHT_RELA:
3653 if (get_elf_backend_data (abfd)->may_use_rela_p)
3654 this_hdr->sh_entsize = bed->s->sizeof_rela;
3655 break;
3657 case SHT_REL:
3658 if (get_elf_backend_data (abfd)->may_use_rel_p)
3659 this_hdr->sh_entsize = bed->s->sizeof_rel;
3660 break;
3662 case SHT_GNU_versym:
3663 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3664 break;
3666 case SHT_GNU_verdef:
3667 this_hdr->sh_entsize = 0;
3668 /* objcopy or strip will copy over sh_info, but may not set
3669 cverdefs. The linker will set cverdefs, but sh_info will be
3670 zero. */
3671 if (this_hdr->sh_info == 0)
3672 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3673 else
3674 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3675 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3676 break;
3678 case SHT_GNU_verneed:
3679 this_hdr->sh_entsize = 0;
3680 /* objcopy or strip will copy over sh_info, but may not set
3681 cverrefs. The linker will set cverrefs, but sh_info will be
3682 zero. */
3683 if (this_hdr->sh_info == 0)
3684 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3685 else
3686 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3687 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3688 break;
3690 case SHT_GROUP:
3691 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3692 break;
3694 case SHT_GNU_HASH:
3695 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3696 break;
3699 if ((asect->flags & SEC_ALLOC) != 0)
3700 this_hdr->sh_flags |= SHF_ALLOC;
3701 if ((asect->flags & SEC_READONLY) == 0)
3702 this_hdr->sh_flags |= SHF_WRITE;
3703 if ((asect->flags & SEC_CODE) != 0)
3704 this_hdr->sh_flags |= SHF_EXECINSTR;
3705 if ((asect->flags & SEC_MERGE) != 0)
3707 this_hdr->sh_flags |= SHF_MERGE;
3708 this_hdr->sh_entsize = asect->entsize;
3710 if ((asect->flags & SEC_STRINGS) != 0)
3712 this_hdr->sh_flags |= SHF_STRINGS;
3713 this_hdr->sh_entsize = asect->entsize;
3715 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3716 this_hdr->sh_flags |= SHF_GROUP;
3717 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3719 this_hdr->sh_flags |= SHF_TLS;
3720 if (asect->size == 0
3721 && (asect->flags & SEC_HAS_CONTENTS) == 0)
3723 struct bfd_link_order *o = asect->map_tail.link_order;
3725 this_hdr->sh_size = 0;
3726 if (o != NULL)
3728 this_hdr->sh_size = o->offset + o->size;
3729 if (this_hdr->sh_size != 0)
3730 this_hdr->sh_type = SHT_NOBITS;
3734 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3735 this_hdr->sh_flags |= SHF_EXCLUDE;
3737 /* If the section has relocs, set up a section header for the
3738 SHT_REL[A] section. If two relocation sections are required for
3739 this section, it is up to the processor-specific back-end to
3740 create the other. */
3741 if ((asect->flags & SEC_RELOC) != 0)
3743 /* When doing a relocatable link, create both REL and RELA sections if
3744 needed. */
3745 if (arg->link_info
3746 /* Do the normal setup if we wouldn't create any sections here. */
3747 && esd->rel.count + esd->rela.count > 0
3748 && (bfd_link_relocatable (arg->link_info)
3749 || arg->link_info->emitrelocations))
3751 if (esd->rel.count && esd->rel.hdr == NULL
3752 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
3753 false, delay_st_name_p))
3755 arg->failed = true;
3756 return;
3758 if (esd->rela.count && esd->rela.hdr == NULL
3759 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
3760 true, delay_st_name_p))
3762 arg->failed = true;
3763 return;
3766 else if (!_bfd_elf_init_reloc_shdr (abfd,
3767 (asect->use_rela_p
3768 ? &esd->rela : &esd->rel),
3769 name,
3770 asect->use_rela_p,
3771 delay_st_name_p))
3773 arg->failed = true;
3774 return;
3778 /* Check for processor-specific section types. */
3779 sh_type = this_hdr->sh_type;
3780 if (bed->elf_backend_fake_sections
3781 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3783 arg->failed = true;
3784 return;
3787 if (sh_type == SHT_NOBITS && asect->size != 0)
3789 /* Don't change the header type from NOBITS if we are being
3790 called for objcopy --only-keep-debug. */
3791 this_hdr->sh_type = sh_type;
3795 /* Fill in the contents of a SHT_GROUP section. Called from
3796 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3797 when ELF targets use the generic linker, ld. Called for ld -r
3798 from bfd_elf_final_link. */
3800 void
3801 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3803 bool *failedptr = (bool *) failedptrarg;
3804 asection *elt, *first;
3805 unsigned char *loc;
3806 bool gas;
3808 /* Ignore linker created group section. See elfNN_ia64_object_p in
3809 elfxx-ia64.c. */
3810 if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3811 || sec->size == 0
3812 || *failedptr)
3813 return;
3815 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3817 unsigned long symindx = 0;
3819 /* elf_group_id will have been set up by objcopy and the
3820 generic linker. */
3821 if (elf_group_id (sec) != NULL)
3822 symindx = elf_group_id (sec)->udata.i;
3824 if (symindx == 0)
3826 /* If called from the assembler, swap_out_syms will have set up
3827 elf_section_syms.
3828 PR 25699: A corrupt input file could contain bogus group info. */
3829 if (sec->index >= elf_num_section_syms (abfd)
3830 || elf_section_syms (abfd)[sec->index] == NULL)
3832 *failedptr = true;
3833 return;
3835 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3837 elf_section_data (sec)->this_hdr.sh_info = symindx;
3839 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
3841 /* The ELF backend linker sets sh_info to -2 when the group
3842 signature symbol is global, and thus the index can't be
3843 set until all local symbols are output. */
3844 asection *igroup;
3845 struct bfd_elf_section_data *sec_data;
3846 unsigned long symndx;
3847 unsigned long extsymoff;
3848 struct elf_link_hash_entry *h;
3850 /* The point of this little dance to the first SHF_GROUP section
3851 then back to the SHT_GROUP section is that this gets us to
3852 the SHT_GROUP in the input object. */
3853 igroup = elf_sec_group (elf_next_in_group (sec));
3854 sec_data = elf_section_data (igroup);
3855 symndx = sec_data->this_hdr.sh_info;
3856 extsymoff = 0;
3857 if (!elf_bad_symtab (igroup->owner))
3859 Elf_Internal_Shdr *symtab_hdr;
3861 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3862 extsymoff = symtab_hdr->sh_info;
3864 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3865 while (h->root.type == bfd_link_hash_indirect
3866 || h->root.type == bfd_link_hash_warning)
3867 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3869 elf_section_data (sec)->this_hdr.sh_info = h->indx;
3872 /* The contents won't be allocated for "ld -r" or objcopy. */
3873 gas = true;
3874 if (sec->contents == NULL)
3876 gas = false;
3877 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
3879 /* Arrange for the section to be written out. */
3880 elf_section_data (sec)->this_hdr.contents = sec->contents;
3881 if (sec->contents == NULL)
3883 *failedptr = true;
3884 return;
3886 sec->alloced = 1;
3889 loc = sec->contents + sec->size;
3891 /* Get the pointer to the first section in the group that gas
3892 squirreled away here. objcopy arranges for this to be set to the
3893 start of the input section group. */
3894 first = elt = elf_next_in_group (sec);
3896 /* First element is a flag word. Rest of section is elf section
3897 indices for all the sections of the group. Write them backwards
3898 just to keep the group in the same order as given in .section
3899 directives, not that it matters. */
3900 while (elt != NULL)
3902 asection *s;
3904 s = elt;
3905 if (!gas)
3906 s = s->output_section;
3907 if (s != NULL
3908 && !bfd_is_abs_section (s))
3910 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
3911 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3913 if (elf_sec->rel.hdr != NULL
3914 && (gas
3915 || (input_elf_sec->rel.hdr != NULL
3916 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
3918 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
3919 loc -= 4;
3920 if (loc == sec->contents)
3921 break;
3922 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3924 if (elf_sec->rela.hdr != NULL
3925 && (gas
3926 || (input_elf_sec->rela.hdr != NULL
3927 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
3929 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
3930 loc -= 4;
3931 if (loc == sec->contents)
3932 break;
3933 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3935 loc -= 4;
3936 if (loc == sec->contents)
3937 break;
3938 H_PUT_32 (abfd, elf_sec->this_idx, loc);
3940 elt = elf_next_in_group (elt);
3941 if (elt == first)
3942 break;
3945 /* We should always get here with loc == sec->contents + 4, but it is
3946 possible to craft bogus SHT_GROUP sections that will cause segfaults
3947 in objcopy without checking loc here and in the loop above. */
3948 if (loc == sec->contents)
3949 BFD_ASSERT (0);
3950 else
3952 loc -= 4;
3953 if (loc != sec->contents)
3955 BFD_ASSERT (0);
3956 memset (sec->contents + 4, 0, loc - sec->contents);
3957 loc = sec->contents;
3961 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
3964 /* Given NAME, the name of a relocation section stripped of its
3965 .rel/.rela prefix, return the section in ABFD to which the
3966 relocations apply. */
3968 asection *
3969 _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3971 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3972 section likely apply to .got.plt or .got section. */
3973 if (get_elf_backend_data (abfd)->want_got_plt
3974 && strcmp (name, ".plt") == 0)
3976 asection *sec;
3978 name = ".got.plt";
3979 sec = bfd_get_section_by_name (abfd, name);
3980 if (sec != NULL)
3981 return sec;
3982 name = ".got";
3985 return bfd_get_section_by_name (abfd, name);
3988 /* Return the section to which RELOC_SEC applies. */
3990 static asection *
3991 elf_get_reloc_section (asection *reloc_sec)
3993 const char *name;
3994 unsigned int type;
3995 bfd *abfd;
3996 const struct elf_backend_data *bed;
3998 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3999 if (type != SHT_REL && type != SHT_RELA)
4000 return NULL;
4002 /* We look up the section the relocs apply to by name. */
4003 name = reloc_sec->name;
4004 if (!startswith (name, ".rel"))
4005 return NULL;
4006 name += 4;
4007 if (type == SHT_RELA && *name++ != 'a')
4008 return NULL;
4010 abfd = reloc_sec->owner;
4011 bed = get_elf_backend_data (abfd);
4012 return bed->get_reloc_section (abfd, name);
4015 /* Assign all ELF section numbers. The dummy first section is handled here
4016 too. The link/info pointers for the standard section types are filled
4017 in here too, while we're at it. LINK_INFO will be 0 when arriving
4018 here for gas, objcopy, and when using the generic ELF linker. */
4020 static bool
4021 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
4023 struct elf_obj_tdata *t = elf_tdata (abfd);
4024 asection *sec;
4025 unsigned int section_number;
4026 Elf_Internal_Shdr **i_shdrp;
4027 struct bfd_elf_section_data *d;
4028 bool need_symtab;
4029 size_t amt;
4031 section_number = 1;
4033 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
4035 /* SHT_GROUP sections are in relocatable files only. */
4036 if (link_info == NULL || !link_info->resolve_section_groups)
4038 size_t reloc_count = 0;
4040 /* Put SHT_GROUP sections first. */
4041 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4043 d = elf_section_data (sec);
4045 if (d->this_hdr.sh_type == SHT_GROUP)
4047 if (sec->flags & SEC_LINKER_CREATED)
4049 /* Remove the linker created SHT_GROUP sections. */
4050 bfd_section_list_remove (abfd, sec);
4051 abfd->section_count--;
4053 else
4054 d->this_idx = section_number++;
4057 /* Count relocations. */
4058 reloc_count += sec->reloc_count;
4061 /* Set/clear HAS_RELOC depending on whether there are relocations. */
4062 if (reloc_count == 0)
4063 abfd->flags &= ~HAS_RELOC;
4064 else
4065 abfd->flags |= HAS_RELOC;
4068 for (sec = abfd->sections; sec; sec = sec->next)
4070 d = elf_section_data (sec);
4072 if (d->this_hdr.sh_type != SHT_GROUP)
4073 d->this_idx = section_number++;
4074 if (d->this_hdr.sh_name != (unsigned int) -1)
4075 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
4076 if (d->rel.hdr)
4078 d->rel.idx = section_number++;
4079 if (d->rel.hdr->sh_name != (unsigned int) -1)
4080 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
4082 else
4083 d->rel.idx = 0;
4085 if (d->rela.hdr)
4087 d->rela.idx = section_number++;
4088 if (d->rela.hdr->sh_name != (unsigned int) -1)
4089 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
4091 else
4092 d->rela.idx = 0;
4095 need_symtab = (bfd_get_symcount (abfd) > 0
4096 || (link_info == NULL
4097 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4098 == HAS_RELOC)));
4099 if (need_symtab)
4101 elf_onesymtab (abfd) = section_number++;
4102 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4103 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
4105 elf_section_list *entry;
4107 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
4109 entry = bfd_zalloc (abfd, sizeof (*entry));
4110 entry->ndx = section_number++;
4111 elf_symtab_shndx_list (abfd) = entry;
4112 entry->hdr.sh_name
4113 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
4114 ".symtab_shndx", false);
4115 if (entry->hdr.sh_name == (unsigned int) -1)
4116 return false;
4118 elf_strtab_sec (abfd) = section_number++;
4119 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
4122 elf_shstrtab_sec (abfd) = section_number++;
4123 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
4124 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
4126 if (section_number >= SHN_LORESERVE)
4128 /* xgettext:c-format */
4129 _bfd_error_handler (_("%pB: too many sections: %u"),
4130 abfd, section_number);
4131 return false;
4134 elf_numsections (abfd) = section_number;
4135 elf_elfheader (abfd)->e_shnum = section_number;
4137 /* Set up the list of section header pointers, in agreement with the
4138 indices. */
4139 amt = section_number * sizeof (Elf_Internal_Shdr *);
4140 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
4141 if (i_shdrp == NULL)
4142 return false;
4144 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
4145 sizeof (Elf_Internal_Shdr));
4146 if (i_shdrp[0] == NULL)
4148 bfd_release (abfd, i_shdrp);
4149 return false;
4152 elf_elfsections (abfd) = i_shdrp;
4154 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
4155 if (need_symtab)
4157 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4158 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
4160 elf_section_list * entry = elf_symtab_shndx_list (abfd);
4161 BFD_ASSERT (entry != NULL);
4162 i_shdrp[entry->ndx] = & entry->hdr;
4163 entry->hdr.sh_link = elf_onesymtab (abfd);
4165 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
4166 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
4169 for (sec = abfd->sections; sec; sec = sec->next)
4171 asection *s;
4173 d = elf_section_data (sec);
4175 i_shdrp[d->this_idx] = &d->this_hdr;
4176 if (d->rel.idx != 0)
4177 i_shdrp[d->rel.idx] = d->rel.hdr;
4178 if (d->rela.idx != 0)
4179 i_shdrp[d->rela.idx] = d->rela.hdr;
4181 /* Fill in the sh_link and sh_info fields while we're at it. */
4183 /* sh_link of a reloc section is the section index of the symbol
4184 table. sh_info is the section index of the section to which
4185 the relocation entries apply. */
4186 if (d->rel.idx != 0)
4188 d->rel.hdr->sh_link = elf_onesymtab (abfd);
4189 d->rel.hdr->sh_info = d->this_idx;
4190 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
4192 if (d->rela.idx != 0)
4194 d->rela.hdr->sh_link = elf_onesymtab (abfd);
4195 d->rela.hdr->sh_info = d->this_idx;
4196 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
4199 /* We need to set up sh_link for SHF_LINK_ORDER. */
4200 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
4202 s = elf_linked_to_section (sec);
4203 /* We can now have a NULL linked section pointer.
4204 This happens when the sh_link field is 0, which is done
4205 when a linked to section is discarded but the linking
4206 section has been retained for some reason. */
4207 if (s)
4209 /* Check discarded linkonce section. */
4210 if (discarded_section (s))
4212 asection *kept;
4213 _bfd_error_handler
4214 /* xgettext:c-format */
4215 (_("%pB: sh_link of section `%pA' points to"
4216 " discarded section `%pA' of `%pB'"),
4217 abfd, d->this_hdr.bfd_section, s, s->owner);
4218 /* Point to the kept section if it has the same
4219 size as the discarded one. */
4220 kept = _bfd_elf_check_kept_section (s, link_info);
4221 if (kept == NULL)
4223 bfd_set_error (bfd_error_bad_value);
4224 return false;
4226 s = kept;
4228 /* Handle objcopy. */
4229 else if (s->output_section == NULL)
4231 _bfd_error_handler
4232 /* xgettext:c-format */
4233 (_("%pB: sh_link of section `%pA' points to"
4234 " removed section `%pA' of `%pB'"),
4235 abfd, d->this_hdr.bfd_section, s, s->owner);
4236 bfd_set_error (bfd_error_bad_value);
4237 return false;
4239 s = s->output_section;
4240 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4244 switch (d->this_hdr.sh_type)
4246 case SHT_REL:
4247 case SHT_RELA:
4248 /* sh_link is the section index of the symbol table.
4249 sh_info is the section index of the section to which the
4250 relocation entries apply. */
4251 if (d->this_hdr.sh_link == 0)
4253 /* FIXME maybe: If this is a reloc section which we are
4254 treating as a normal section then we likely should
4255 not be assuming its sh_link is .dynsym or .symtab. */
4256 if ((sec->flags & SEC_ALLOC) != 0)
4258 s = bfd_get_section_by_name (abfd, ".dynsym");
4259 if (s != NULL)
4260 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4262 else
4263 d->this_hdr.sh_link = elf_onesymtab (abfd);
4266 s = elf_get_reloc_section (sec);
4267 if (s != NULL)
4269 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
4270 d->this_hdr.sh_flags |= SHF_INFO_LINK;
4272 break;
4274 case SHT_STRTAB:
4275 /* We assume that a section named .stab*str is a stabs
4276 string section. We look for a section with the same name
4277 but without the trailing ``str'', and set its sh_link
4278 field to point to this section. */
4279 if (startswith (sec->name, ".stab")
4280 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
4282 size_t len;
4283 char *alc;
4285 len = strlen (sec->name);
4286 alc = (char *) bfd_malloc (len - 2);
4287 if (alc == NULL)
4288 return false;
4289 memcpy (alc, sec->name, len - 3);
4290 alc[len - 3] = '\0';
4291 s = bfd_get_section_by_name (abfd, alc);
4292 free (alc);
4293 if (s != NULL)
4295 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
4297 /* This is a .stab section. */
4298 elf_section_data (s)->this_hdr.sh_entsize = 12;
4301 break;
4303 case SHT_DYNAMIC:
4304 case SHT_DYNSYM:
4305 case SHT_GNU_verneed:
4306 case SHT_GNU_verdef:
4307 /* sh_link is the section header index of the string table
4308 used for the dynamic entries, or the symbol table, or the
4309 version strings. */
4310 s = bfd_get_section_by_name (abfd, ".dynstr");
4311 if (s != NULL)
4312 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4313 break;
4315 case SHT_GNU_LIBLIST:
4316 /* sh_link is the section header index of the prelink library
4317 list used for the dynamic entries, or the symbol table, or
4318 the version strings. */
4319 s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
4320 ? ".dynstr" : ".gnu.libstr"));
4321 if (s != NULL)
4322 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4323 break;
4325 case SHT_HASH:
4326 case SHT_GNU_HASH:
4327 case SHT_GNU_versym:
4328 /* sh_link is the section header index of the symbol table
4329 this hash table or version table is for. */
4330 s = bfd_get_section_by_name (abfd, ".dynsym");
4331 if (s != NULL)
4332 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4333 break;
4335 case SHT_GROUP:
4336 d->this_hdr.sh_link = elf_onesymtab (abfd);
4340 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4341 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4342 debug section name from .debug_* to .zdebug_* if needed. */
4344 return true;
4347 static bool
4348 sym_is_global (bfd *abfd, asymbol *sym)
4350 /* If the backend has a special mapping, use it. */
4351 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4352 if (bed->elf_backend_sym_is_global)
4353 return (*bed->elf_backend_sym_is_global) (abfd, sym);
4355 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
4356 || bfd_is_und_section (bfd_asymbol_section (sym))
4357 || bfd_is_com_section (bfd_asymbol_section (sym)));
4360 /* Filter global symbols of ABFD to include in the import library. All
4361 SYMCOUNT symbols of ABFD can be examined from their pointers in
4362 SYMS. Pointers of symbols to keep should be stored contiguously at
4363 the beginning of that array.
4365 Returns the number of symbols to keep. */
4367 unsigned int
4368 _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4369 asymbol **syms, long symcount)
4371 long src_count, dst_count = 0;
4373 for (src_count = 0; src_count < symcount; src_count++)
4375 asymbol *sym = syms[src_count];
4376 char *name = (char *) bfd_asymbol_name (sym);
4377 struct bfd_link_hash_entry *h;
4379 if (!sym_is_global (abfd, sym))
4380 continue;
4382 h = bfd_link_hash_lookup (info->hash, name, false, false, false);
4383 if (h == NULL)
4384 continue;
4385 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4386 continue;
4387 if (h->linker_def || h->ldscript_def)
4388 continue;
4390 syms[dst_count++] = sym;
4393 syms[dst_count] = NULL;
4395 return dst_count;
4398 /* Don't output symbols for sections that are not going to be output,
4399 that are duplicates or there is no BFD section. */
4401 static bool
4402 ignore_sym (asymbol *sym)
4404 if (sym == NULL)
4405 return false;
4407 if (sym->section == NULL)
4408 return true;
4410 if ((sym->flags & BSF_SECTION_SYM) != 0)
4412 if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
4413 return true;
4414 /* With ld -r on generic elf targets it is possible to have
4415 multiple section symbols in the output for a given section.
4416 We'd like to get rid of all but the first one. This drops
4417 them if the first input section is non-zero size, but fails
4418 to do so if the first input section is zero sized. */
4419 if (sym->section->output_offset != 0)
4420 return true;
4423 return discarded_section (sym->section);
4426 /* Map symbol from it's internal number to the external number, moving
4427 all local symbols to be at the head of the list. */
4429 static bool
4430 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4432 unsigned int symcount = bfd_get_symcount (abfd);
4433 asymbol **syms = bfd_get_outsymbols (abfd);
4434 asymbol **sect_syms;
4435 unsigned int num_locals = 0;
4436 unsigned int num_globals = 0;
4437 unsigned int max_index = 0;
4438 unsigned int idx;
4439 asection *asect;
4440 asymbol **new_syms;
4441 size_t amt;
4443 #ifdef DEBUG
4444 fprintf (stderr, "elf_map_symbols\n");
4445 fflush (stderr);
4446 #endif
4448 for (asect = abfd->sections; asect; asect = asect->next)
4450 if (max_index < asect->index)
4451 max_index = asect->index;
4454 max_index++;
4455 amt = max_index * sizeof (asymbol *);
4456 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
4457 if (sect_syms == NULL)
4458 return false;
4459 elf_section_syms (abfd) = sect_syms;
4460 elf_num_section_syms (abfd) = max_index;
4462 /* Init sect_syms entries for any section symbols we have already
4463 decided to output. */
4464 for (idx = 0; idx < symcount; idx++)
4466 asymbol *sym = syms[idx];
4468 if ((sym->flags & BSF_SECTION_SYM) != 0
4469 && sym->value == 0
4470 && !ignore_sym (sym)
4471 && !bfd_is_abs_section (sym->section))
4473 asection *sec = sym->section;
4475 if (sec->owner != abfd)
4476 sec = sec->output_section;
4478 sect_syms[sec->index] = syms[idx];
4482 /* Classify all of the symbols. */
4483 for (idx = 0; idx < symcount; idx++)
4485 if (ignore_sym (syms[idx]))
4486 continue;
4487 if (sym_is_global (abfd, syms[idx]))
4488 num_globals++;
4489 else
4490 num_locals++;
4493 /* We will be adding a section symbol for each normal BFD section. Most
4494 sections will already have a section symbol in outsymbols, but
4495 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4496 at least in that case. */
4497 for (asect = abfd->sections; asect; asect = asect->next)
4499 asymbol *sym = asect->symbol;
4500 /* Don't include ignored section symbols. */
4501 if (!ignore_sym (sym)
4502 && sect_syms[asect->index] == NULL)
4504 if (sym_is_global (abfd, asect->symbol))
4505 num_globals++;
4506 else
4507 num_locals++;
4511 /* Now sort the symbols so the local symbols are first. */
4512 amt = (num_locals + num_globals) * sizeof (asymbol *);
4513 new_syms = (asymbol **) bfd_alloc (abfd, amt);
4514 if (new_syms == NULL)
4515 return false;
4517 unsigned int num_globals2 = 0;
4518 unsigned int num_locals2 = 0;
4519 for (idx = 0; idx < symcount; idx++)
4521 asymbol *sym = syms[idx];
4522 unsigned int i;
4524 if (ignore_sym (sym))
4525 continue;
4527 if (sym_is_global (abfd, sym))
4528 i = num_locals + num_globals2++;
4529 else
4530 i = num_locals2++;
4531 new_syms[i] = sym;
4532 sym->udata.i = i + 1;
4534 for (asect = abfd->sections; asect; asect = asect->next)
4536 asymbol *sym = asect->symbol;
4537 if (!ignore_sym (sym)
4538 && sect_syms[asect->index] == NULL)
4540 unsigned int i;
4542 sect_syms[asect->index] = sym;
4543 if (sym_is_global (abfd, sym))
4544 i = num_locals + num_globals2++;
4545 else
4546 i = num_locals2++;
4547 new_syms[i] = sym;
4548 sym->udata.i = i + 1;
4552 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4554 *pnum_locals = num_locals;
4555 return true;
4558 /* Assign a file position to a section, optionally aligning to the
4559 required section alignment. */
4561 file_ptr
4562 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4563 file_ptr offset,
4564 bool align,
4565 unsigned char log_file_align)
4567 if (i_shdrp->sh_addralign > 1)
4569 file_ptr salign = i_shdrp->sh_addralign & -i_shdrp->sh_addralign;
4571 if (align)
4572 offset = BFD_ALIGN (offset, salign);
4573 else if (log_file_align)
4575 /* Heuristic: Cap alignment at log_file_align. */
4576 file_ptr falign = 1u << log_file_align;
4578 offset = BFD_ALIGN (offset, salign < falign ? salign : falign);
4581 i_shdrp->sh_offset = offset;
4582 if (i_shdrp->bfd_section != NULL)
4583 i_shdrp->bfd_section->filepos = offset;
4584 if (i_shdrp->sh_type != SHT_NOBITS)
4585 offset += i_shdrp->sh_size;
4586 return offset;
4589 /* Compute the file positions we are going to put the sections at, and
4590 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4591 is not NULL, this is being called by the ELF backend linker. */
4593 bool
4594 _bfd_elf_compute_section_file_positions (bfd *abfd,
4595 struct bfd_link_info *link_info)
4597 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4598 struct fake_section_arg fsargs;
4599 bool failed;
4600 struct elf_strtab_hash *strtab = NULL;
4601 Elf_Internal_Shdr *shstrtab_hdr;
4602 bool need_symtab;
4604 if (abfd->output_has_begun)
4605 return true;
4607 /* Do any elf backend specific processing first. */
4608 if (bed->elf_backend_begin_write_processing)
4609 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4611 if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
4612 return false;
4614 fsargs.failed = false;
4615 fsargs.link_info = link_info;
4616 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4617 if (fsargs.failed)
4618 return false;
4620 if (!assign_section_numbers (abfd, link_info))
4621 return false;
4623 /* The backend linker builds symbol table information itself. */
4624 need_symtab = (link_info == NULL
4625 && (bfd_get_symcount (abfd) > 0
4626 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4627 == HAS_RELOC)));
4628 if (need_symtab)
4630 /* Non-zero if doing a relocatable link. */
4631 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4633 if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
4634 return false;
4637 failed = false;
4638 if (link_info == NULL)
4640 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4641 if (failed)
4642 goto err_free_strtab;
4645 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4646 /* sh_name was set in init_file_header. */
4647 shstrtab_hdr->sh_type = SHT_STRTAB;
4648 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
4649 shstrtab_hdr->sh_addr = 0;
4650 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4651 shstrtab_hdr->sh_entsize = 0;
4652 shstrtab_hdr->sh_link = 0;
4653 shstrtab_hdr->sh_info = 0;
4654 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4655 shstrtab_hdr->sh_addralign = 1;
4657 if (!assign_file_positions_except_relocs (abfd, link_info))
4658 goto err_free_strtab;
4660 if (strtab != NULL)
4662 file_ptr off;
4663 Elf_Internal_Shdr *hdr;
4665 off = elf_next_file_pos (abfd);
4667 hdr = & elf_symtab_hdr (abfd);
4668 off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4670 if (elf_symtab_shndx_list (abfd) != NULL)
4672 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4673 if (hdr->sh_size != 0)
4674 off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4675 /* FIXME: What about other symtab_shndx sections in the list ? */
4678 hdr = &elf_tdata (abfd)->strtab_hdr;
4679 off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4681 elf_next_file_pos (abfd) = off;
4683 /* Now that we know where the .strtab section goes, write it
4684 out. */
4685 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4686 || ! _bfd_elf_strtab_emit (abfd, strtab))
4687 goto err_free_strtab;
4688 _bfd_elf_strtab_free (strtab);
4691 abfd->output_has_begun = true;
4692 return true;
4694 err_free_strtab:
4695 if (strtab != NULL)
4696 _bfd_elf_strtab_free (strtab);
4697 return false;
4700 /* Retrieve .eh_frame_hdr. Prior to size_dynamic_sections the
4701 function effectively returns whether --eh-frame-hdr is given on the
4702 command line. After size_dynamic_sections the result reflects
4703 whether .eh_frame_hdr will actually be output (sizing isn't done
4704 until ldemul_after_allocation). */
4706 static asection *
4707 elf_eh_frame_hdr (const struct bfd_link_info *info)
4709 if (info != NULL && is_elf_hash_table (info->hash))
4710 return elf_hash_table (info)->eh_info.hdr_sec;
4711 return NULL;
4714 /* Make an initial estimate of the size of the program header. If we
4715 get the number wrong here, we'll redo section placement. */
4717 static bfd_size_type
4718 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4720 size_t segs;
4721 asection *s;
4722 const struct elf_backend_data *bed;
4724 /* Assume we will need exactly two PT_LOAD segments: one for text
4725 and one for data. */
4726 segs = 2;
4728 s = bfd_get_section_by_name (abfd, ".interp");
4729 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4731 /* If we have a loadable interpreter section, we need a
4732 PT_INTERP segment. In this case, assume we also need a
4733 PT_PHDR segment, although that may not be true for all
4734 targets. */
4735 segs += 2;
4738 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4740 /* We need a PT_DYNAMIC segment. */
4741 ++segs;
4744 if (info != NULL && info->relro)
4746 /* We need a PT_GNU_RELRO segment. */
4747 ++segs;
4750 if (elf_eh_frame_hdr (info))
4752 /* We need a PT_GNU_EH_FRAME segment. */
4753 ++segs;
4756 if (elf_stack_flags (abfd))
4758 /* We need a PT_GNU_STACK segment. */
4759 ++segs;
4762 if (elf_sframe (abfd))
4764 /* We need a PT_GNU_SFRAME segment. */
4765 ++segs;
4768 s = bfd_get_section_by_name (abfd,
4769 NOTE_GNU_PROPERTY_SECTION_NAME);
4770 if (s != NULL && s->size != 0)
4772 /* We need a PT_GNU_PROPERTY segment. */
4773 ++segs;
4776 for (s = abfd->sections; s != NULL; s = s->next)
4778 if ((s->flags & SEC_LOAD) != 0
4779 && elf_section_type (s) == SHT_NOTE)
4781 unsigned int alignment_power;
4782 /* We need a PT_NOTE segment. */
4783 ++segs;
4784 /* Try to create just one PT_NOTE segment for all adjacent
4785 loadable SHT_NOTE sections. gABI requires that within a
4786 PT_NOTE segment (and also inside of each SHT_NOTE section)
4787 each note should have the same alignment. So we check
4788 whether the sections are correctly aligned. */
4789 alignment_power = s->alignment_power;
4790 while (s->next != NULL
4791 && s->next->alignment_power == alignment_power
4792 && (s->next->flags & SEC_LOAD) != 0
4793 && elf_section_type (s->next) == SHT_NOTE)
4794 s = s->next;
4798 for (s = abfd->sections; s != NULL; s = s->next)
4800 if (s->flags & SEC_THREAD_LOCAL)
4802 /* We need a PT_TLS segment. */
4803 ++segs;
4804 break;
4808 bed = get_elf_backend_data (abfd);
4810 if ((abfd->flags & D_PAGED) != 0
4811 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4813 /* Add a PT_GNU_MBIND segment for each mbind section. */
4814 bfd_vma commonpagesize;
4815 unsigned int page_align_power;
4817 if (info != NULL)
4818 commonpagesize = info->commonpagesize;
4819 else
4820 commonpagesize = bed->commonpagesize;
4821 page_align_power = bfd_log2 (commonpagesize);
4822 for (s = abfd->sections; s != NULL; s = s->next)
4823 if (elf_section_flags (s) & SHF_GNU_MBIND)
4825 if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4827 _bfd_error_handler
4828 /* xgettext:c-format */
4829 (_("%pB: GNU_MBIND section `%pA' has invalid "
4830 "sh_info field: %d"),
4831 abfd, s, elf_section_data (s)->this_hdr.sh_info);
4832 continue;
4834 /* Align mbind section to page size. */
4835 if (s->alignment_power < page_align_power)
4836 s->alignment_power = page_align_power;
4837 segs ++;
4841 /* Let the backend count up any program headers it might need. */
4842 if (bed->elf_backend_additional_program_headers)
4844 int a;
4846 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4847 if (a == -1)
4848 abort ();
4849 segs += a;
4852 return segs * bed->s->sizeof_phdr;
4855 /* Find the segment that contains the output_section of section. */
4857 Elf_Internal_Phdr *
4858 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4860 struct elf_segment_map *m;
4861 Elf_Internal_Phdr *p;
4863 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
4864 m != NULL;
4865 m = m->next, p++)
4867 int i;
4869 for (i = m->count - 1; i >= 0; i--)
4870 if (m->sections[i] == section)
4871 return p;
4874 return NULL;
4877 /* Create a mapping from a set of sections to a program segment. */
4879 static struct elf_segment_map *
4880 make_mapping (bfd *abfd,
4881 asection **sections,
4882 unsigned int from,
4883 unsigned int to,
4884 bool phdr)
4886 struct elf_segment_map *m;
4887 unsigned int i;
4888 asection **hdrpp;
4889 size_t amt;
4891 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4892 amt += (to - from) * sizeof (asection *);
4893 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4894 if (m == NULL)
4895 return NULL;
4896 m->next = NULL;
4897 m->p_type = PT_LOAD;
4898 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4899 m->sections[i - from] = *hdrpp;
4900 m->count = to - from;
4902 if (from == 0 && phdr)
4904 /* Include the headers in the first PT_LOAD segment. */
4905 m->includes_filehdr = 1;
4906 m->includes_phdrs = 1;
4909 return m;
4912 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4913 on failure. */
4915 struct elf_segment_map *
4916 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4918 struct elf_segment_map *m;
4920 m = (struct elf_segment_map *) bfd_zalloc (abfd,
4921 sizeof (struct elf_segment_map));
4922 if (m == NULL)
4923 return NULL;
4924 m->next = NULL;
4925 m->p_type = PT_DYNAMIC;
4926 m->count = 1;
4927 m->sections[0] = dynsec;
4929 return m;
4932 /* Possibly add or remove segments from the segment map. */
4934 static bool
4935 elf_modify_segment_map (bfd *abfd,
4936 struct bfd_link_info *info,
4937 bool remove_empty_load)
4939 struct elf_segment_map **m;
4940 const struct elf_backend_data *bed;
4942 /* The placement algorithm assumes that non allocated sections are
4943 not in PT_LOAD segments. We ensure this here by removing such
4944 sections from the segment map. We also remove excluded
4945 sections. Finally, any PT_LOAD segment without sections is
4946 removed. */
4947 m = &elf_seg_map (abfd);
4948 while (*m)
4950 unsigned int i, new_count;
4952 for (new_count = 0, i = 0; i < (*m)->count; i++)
4954 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4955 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4956 || (*m)->p_type != PT_LOAD))
4958 (*m)->sections[new_count] = (*m)->sections[i];
4959 new_count++;
4962 (*m)->count = new_count;
4964 if (remove_empty_load
4965 && (*m)->p_type == PT_LOAD
4966 && (*m)->count == 0
4967 && !(*m)->includes_phdrs)
4968 *m = (*m)->next;
4969 else
4970 m = &(*m)->next;
4973 bed = get_elf_backend_data (abfd);
4974 if (bed->elf_backend_modify_segment_map != NULL)
4976 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
4977 return false;
4980 return true;
4983 #define IS_TBSS(s) \
4984 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4986 /* Set up a mapping from BFD sections to program segments. Update
4987 NEED_LAYOUT if the section layout is changed. */
4989 bool
4990 _bfd_elf_map_sections_to_segments (bfd *abfd,
4991 struct bfd_link_info *info,
4992 bool *need_layout)
4994 unsigned int count;
4995 struct elf_segment_map *m;
4996 asection **sections = NULL;
4997 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4998 bool no_user_phdrs;
5000 no_user_phdrs = elf_seg_map (abfd) == NULL;
5002 if (info != NULL)
5004 info->user_phdrs = !no_user_phdrs;
5006 /* Size the relative relocations if DT_RELR is enabled. */
5007 if (info->enable_dt_relr
5008 && need_layout != NULL
5009 && bed->size_relative_relocs
5010 && !bed->size_relative_relocs (info, need_layout))
5011 info->callbacks->einfo
5012 (_("%F%P: failed to size relative relocations\n"));
5015 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
5017 asection *s;
5018 unsigned int i;
5019 struct elf_segment_map *mfirst;
5020 struct elf_segment_map **pm;
5021 asection *last_hdr;
5022 bfd_vma last_size;
5023 unsigned int hdr_index;
5024 bfd_vma maxpagesize;
5025 asection **hdrpp;
5026 bool phdr_in_segment;
5027 bool writable;
5028 bool executable;
5029 unsigned int tls_count = 0;
5030 asection *first_tls = NULL;
5031 asection *first_mbind = NULL;
5032 asection *dynsec, *eh_frame_hdr;
5033 asection *sframe;
5034 size_t amt;
5035 bfd_vma addr_mask, wrap_to = 0; /* Bytes. */
5036 bfd_size_type phdr_size; /* Octets/bytes. */
5037 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5039 /* Select the allocated sections, and sort them. */
5041 amt = bfd_count_sections (abfd) * sizeof (asection *);
5042 sections = (asection **) bfd_malloc (amt);
5043 if (sections == NULL)
5044 goto error_return;
5046 /* Calculate top address, avoiding undefined behaviour of shift
5047 left operator when shift count is equal to size of type
5048 being shifted. */
5049 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
5050 addr_mask = (addr_mask << 1) + 1;
5052 i = 0;
5053 for (s = abfd->sections; s != NULL; s = s->next)
5055 if ((s->flags & SEC_ALLOC) != 0)
5057 /* target_index is unused until bfd_elf_final_link
5058 starts output of section symbols. Use it to make
5059 qsort stable. */
5060 s->target_index = i;
5061 sections[i] = s;
5062 ++i;
5063 /* A wrapping section potentially clashes with header. */
5064 if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
5065 wrap_to = (s->lma + s->size / opb) & addr_mask;
5068 BFD_ASSERT (i <= bfd_count_sections (abfd));
5069 count = i;
5071 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
5073 phdr_size = elf_program_header_size (abfd);
5074 if (phdr_size == (bfd_size_type) -1)
5075 phdr_size = get_program_header_size (abfd, info);
5076 phdr_size += bed->s->sizeof_ehdr;
5077 /* phdr_size is compared to LMA values which are in bytes. */
5078 phdr_size /= opb;
5079 if (info != NULL)
5080 maxpagesize = info->maxpagesize;
5081 else
5082 maxpagesize = bed->maxpagesize;
5083 if (maxpagesize == 0)
5084 maxpagesize = 1;
5085 phdr_in_segment = info != NULL && info->load_phdrs;
5086 if (count != 0
5087 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
5088 >= (phdr_size & (maxpagesize - 1))))
5089 /* For compatibility with old scripts that may not be using
5090 SIZEOF_HEADERS, add headers when it looks like space has
5091 been left for them. */
5092 phdr_in_segment = true;
5094 /* Build the mapping. */
5095 mfirst = NULL;
5096 pm = &mfirst;
5098 /* If we have a .interp section, then create a PT_PHDR segment for
5099 the program headers and a PT_INTERP segment for the .interp
5100 section. */
5101 s = bfd_get_section_by_name (abfd, ".interp");
5102 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
5104 amt = sizeof (struct elf_segment_map);
5105 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5106 if (m == NULL)
5107 goto error_return;
5108 m->next = NULL;
5109 m->p_type = PT_PHDR;
5110 m->p_flags = PF_R;
5111 m->p_flags_valid = 1;
5112 m->includes_phdrs = 1;
5113 phdr_in_segment = true;
5114 *pm = m;
5115 pm = &m->next;
5117 amt = sizeof (struct elf_segment_map);
5118 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5119 if (m == NULL)
5120 goto error_return;
5121 m->next = NULL;
5122 m->p_type = PT_INTERP;
5123 m->count = 1;
5124 m->sections[0] = s;
5126 *pm = m;
5127 pm = &m->next;
5130 /* Look through the sections. We put sections in the same program
5131 segment when the start of the second section can be placed within
5132 a few bytes of the end of the first section. */
5133 last_hdr = NULL;
5134 last_size = 0;
5135 hdr_index = 0;
5136 writable = false;
5137 executable = false;
5138 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
5139 if (dynsec != NULL
5140 && (dynsec->flags & SEC_LOAD) == 0)
5141 dynsec = NULL;
5143 if ((abfd->flags & D_PAGED) == 0)
5144 phdr_in_segment = false;
5146 /* Deal with -Ttext or something similar such that the first section
5147 is not adjacent to the program headers. This is an
5148 approximation, since at this point we don't know exactly how many
5149 program headers we will need. */
5150 if (phdr_in_segment && count > 0)
5152 bfd_vma phdr_lma; /* Bytes. */
5153 bool separate_phdr = false;
5155 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
5156 if (info != NULL
5157 && info->separate_code
5158 && (sections[0]->flags & SEC_CODE) != 0)
5160 /* If data sections should be separate from code and
5161 thus not executable, and the first section is
5162 executable then put the file and program headers in
5163 their own PT_LOAD. */
5164 if (!info->one_rosegment)
5165 separate_phdr = true;
5167 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
5168 == (sections[0]->lma & addr_mask & -maxpagesize)))
5170 /* The file and program headers are currently on the
5171 same page as the first section. Put them on the
5172 previous page if we can. */
5173 if (phdr_lma >= maxpagesize)
5174 phdr_lma -= maxpagesize;
5175 else
5176 separate_phdr = false;
5179 if ((sections[0]->lma & addr_mask) < phdr_lma
5180 || (sections[0]->lma & addr_mask) < phdr_size)
5181 /* If file and program headers would be placed at the end
5182 of memory then it's probably better to omit them. */
5183 phdr_in_segment = false;
5184 else if (phdr_lma < wrap_to)
5185 /* If a section wraps around to where we'll be placing
5186 file and program headers, then the headers will be
5187 overwritten. */
5188 phdr_in_segment = false;
5189 else if (separate_phdr)
5191 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
5192 if (m == NULL)
5193 goto error_return;
5194 m->p_paddr = phdr_lma * opb;
5195 m->p_vaddr_offset
5196 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
5197 m->p_paddr_valid = 1;
5198 *pm = m;
5199 pm = &m->next;
5200 phdr_in_segment = false;
5204 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
5206 asection *hdr;
5207 bool new_segment;
5209 hdr = *hdrpp;
5211 /* See if this section and the last one will fit in the same
5212 segment. */
5214 if (last_hdr == NULL)
5216 /* If we don't have a segment yet, then we don't need a new
5217 one (we build the last one after this loop). */
5218 new_segment = false;
5220 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
5222 /* If this section has a different relation between the
5223 virtual address and the load address, then we need a new
5224 segment. */
5225 new_segment = true;
5227 else if (hdr->lma < last_hdr->lma + last_size
5228 || last_hdr->lma + last_size < last_hdr->lma)
5230 /* If this section has a load address that makes it overlap
5231 the previous section, then we need a new segment. */
5232 new_segment = true;
5234 else if ((abfd->flags & D_PAGED) != 0
5235 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
5236 == (hdr->lma & -maxpagesize)))
5238 /* If we are demand paged then we can't map two disk
5239 pages onto the same memory page. */
5240 new_segment = false;
5242 /* In the next test we have to be careful when last_hdr->lma is close
5243 to the end of the address space. If the aligned address wraps
5244 around to the start of the address space, then there are no more
5245 pages left in memory and it is OK to assume that the current
5246 section can be included in the current segment. */
5247 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5248 + maxpagesize > last_hdr->lma)
5249 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5250 + maxpagesize <= hdr->lma))
5252 /* If putting this section in this segment would force us to
5253 skip a page in the segment, then we need a new segment. */
5254 new_segment = true;
5256 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
5257 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
5259 /* We don't want to put a loaded section after a
5260 nonloaded (ie. bss style) section in the same segment
5261 as that will force the non-loaded section to be loaded.
5262 Consider .tbss sections as loaded for this purpose. */
5263 new_segment = true;
5265 else if ((abfd->flags & D_PAGED) == 0)
5267 /* If the file is not demand paged, which means that we
5268 don't require the sections to be correctly aligned in the
5269 file, then there is no other reason for a new segment. */
5270 new_segment = false;
5272 else if (info != NULL
5273 && info->separate_code
5274 && executable != ((hdr->flags & SEC_CODE) != 0))
5276 new_segment = true;
5278 else if (! writable
5279 && (hdr->flags & SEC_READONLY) == 0)
5281 /* We don't want to put a writable section in a read only
5282 segment. */
5283 new_segment = true;
5285 else
5287 /* Otherwise, we can use the same segment. */
5288 new_segment = false;
5291 /* Allow interested parties a chance to override our decision. */
5292 if (last_hdr != NULL
5293 && info != NULL
5294 && info->callbacks->override_segment_assignment != NULL)
5295 new_segment
5296 = info->callbacks->override_segment_assignment (info, abfd, hdr,
5297 last_hdr,
5298 new_segment);
5300 if (! new_segment)
5302 if ((hdr->flags & SEC_READONLY) == 0)
5303 writable = true;
5304 if ((hdr->flags & SEC_CODE) != 0)
5305 executable = true;
5306 last_hdr = hdr;
5307 /* .tbss sections effectively have zero size. */
5308 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5309 continue;
5312 /* We need a new program segment. We must create a new program
5313 header holding all the sections from hdr_index until hdr. */
5315 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5316 if (m == NULL)
5317 goto error_return;
5319 *pm = m;
5320 pm = &m->next;
5322 if ((hdr->flags & SEC_READONLY) == 0)
5323 writable = true;
5324 else
5325 writable = false;
5327 if ((hdr->flags & SEC_CODE) == 0)
5328 executable = false;
5329 else
5330 executable = true;
5332 last_hdr = hdr;
5333 /* .tbss sections effectively have zero size. */
5334 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5335 hdr_index = i;
5336 phdr_in_segment = false;
5339 /* Create a final PT_LOAD program segment, but not if it's just
5340 for .tbss. */
5341 if (last_hdr != NULL
5342 && (i - hdr_index != 1
5343 || !IS_TBSS (last_hdr)))
5345 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5346 if (m == NULL)
5347 goto error_return;
5349 *pm = m;
5350 pm = &m->next;
5353 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
5354 if (dynsec != NULL)
5356 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
5357 if (m == NULL)
5358 goto error_return;
5359 *pm = m;
5360 pm = &m->next;
5363 /* For each batch of consecutive loadable SHT_NOTE sections,
5364 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
5365 because if we link together nonloadable .note sections and
5366 loadable .note sections, we will generate two .note sections
5367 in the output file. */
5368 for (s = abfd->sections; s != NULL; s = s->next)
5370 if ((s->flags & SEC_LOAD) != 0
5371 && elf_section_type (s) == SHT_NOTE)
5373 asection *s2;
5374 unsigned int alignment_power = s->alignment_power;
5376 count = 1;
5377 for (s2 = s; s2->next != NULL; s2 = s2->next)
5379 if (s2->next->alignment_power == alignment_power
5380 && (s2->next->flags & SEC_LOAD) != 0
5381 && elf_section_type (s2->next) == SHT_NOTE
5382 && align_power (s2->lma + s2->size / opb,
5383 alignment_power)
5384 == s2->next->lma)
5385 count++;
5386 else
5387 break;
5389 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5390 amt += count * sizeof (asection *);
5391 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5392 if (m == NULL)
5393 goto error_return;
5394 m->next = NULL;
5395 m->p_type = PT_NOTE;
5396 m->count = count;
5397 while (count > 1)
5399 m->sections[m->count - count--] = s;
5400 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5401 s = s->next;
5403 m->sections[m->count - 1] = s;
5404 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5405 *pm = m;
5406 pm = &m->next;
5408 if (s->flags & SEC_THREAD_LOCAL)
5410 if (! tls_count)
5411 first_tls = s;
5412 tls_count++;
5414 if (first_mbind == NULL
5415 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5416 first_mbind = s;
5419 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5420 if (tls_count > 0)
5422 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5423 amt += tls_count * sizeof (asection *);
5424 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5425 if (m == NULL)
5426 goto error_return;
5427 m->next = NULL;
5428 m->p_type = PT_TLS;
5429 m->count = tls_count;
5430 /* Mandated PF_R. */
5431 m->p_flags = PF_R;
5432 m->p_flags_valid = 1;
5433 s = first_tls;
5434 for (i = 0; i < tls_count; ++i)
5436 if ((s->flags & SEC_THREAD_LOCAL) == 0)
5438 _bfd_error_handler
5439 (_("%pB: TLS sections are not adjacent:"), abfd);
5440 s = first_tls;
5441 i = 0;
5442 while (i < tls_count)
5444 if ((s->flags & SEC_THREAD_LOCAL) != 0)
5446 _bfd_error_handler (_(" TLS: %pA"), s);
5447 i++;
5449 else
5450 _bfd_error_handler (_(" non-TLS: %pA"), s);
5451 s = s->next;
5453 bfd_set_error (bfd_error_bad_value);
5454 goto error_return;
5456 m->sections[i] = s;
5457 s = s->next;
5460 *pm = m;
5461 pm = &m->next;
5464 if (first_mbind
5465 && (abfd->flags & D_PAGED) != 0
5466 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
5467 for (s = first_mbind; s != NULL; s = s->next)
5468 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5469 && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
5471 /* Mandated PF_R. */
5472 unsigned long p_flags = PF_R;
5473 if ((s->flags & SEC_READONLY) == 0)
5474 p_flags |= PF_W;
5475 if ((s->flags & SEC_CODE) != 0)
5476 p_flags |= PF_X;
5478 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5479 m = bfd_zalloc (abfd, amt);
5480 if (m == NULL)
5481 goto error_return;
5482 m->next = NULL;
5483 m->p_type = (PT_GNU_MBIND_LO
5484 + elf_section_data (s)->this_hdr.sh_info);
5485 m->count = 1;
5486 m->p_flags_valid = 1;
5487 m->sections[0] = s;
5488 m->p_flags = p_flags;
5490 *pm = m;
5491 pm = &m->next;
5494 s = bfd_get_section_by_name (abfd,
5495 NOTE_GNU_PROPERTY_SECTION_NAME);
5496 if (s != NULL && s->size != 0)
5498 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5499 m = bfd_zalloc (abfd, amt);
5500 if (m == NULL)
5501 goto error_return;
5502 m->next = NULL;
5503 m->p_type = PT_GNU_PROPERTY;
5504 m->count = 1;
5505 m->p_flags_valid = 1;
5506 m->sections[0] = s;
5507 m->p_flags = PF_R;
5508 *pm = m;
5509 pm = &m->next;
5512 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5513 segment. */
5514 eh_frame_hdr = elf_eh_frame_hdr (info);
5515 if (eh_frame_hdr != NULL
5516 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5518 amt = sizeof (struct elf_segment_map);
5519 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5520 if (m == NULL)
5521 goto error_return;
5522 m->next = NULL;
5523 m->p_type = PT_GNU_EH_FRAME;
5524 m->count = 1;
5525 m->sections[0] = eh_frame_hdr->output_section;
5527 *pm = m;
5528 pm = &m->next;
5531 /* If there is a .sframe section, throw in a PT_GNU_SFRAME
5532 segment. */
5533 sframe = elf_sframe (abfd);
5534 if (sframe != NULL
5535 && (sframe->output_section->flags & SEC_LOAD) != 0
5536 && sframe->size != 0)
5538 amt = sizeof (struct elf_segment_map);
5539 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5540 if (m == NULL)
5541 goto error_return;
5542 m->next = NULL;
5543 m->p_type = PT_GNU_SFRAME;
5544 m->count = 1;
5545 m->sections[0] = sframe->output_section;
5547 *pm = m;
5548 pm = &m->next;
5551 if (elf_stack_flags (abfd))
5553 amt = sizeof (struct elf_segment_map);
5554 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5555 if (m == NULL)
5556 goto error_return;
5557 m->next = NULL;
5558 m->p_type = PT_GNU_STACK;
5559 m->p_flags = elf_stack_flags (abfd);
5560 m->p_align = bed->stack_align;
5561 m->p_flags_valid = 1;
5562 m->p_align_valid = m->p_align != 0;
5563 if (info->stacksize > 0)
5565 m->p_size = info->stacksize;
5566 m->p_size_valid = 1;
5569 *pm = m;
5570 pm = &m->next;
5573 if (info != NULL && info->relro)
5575 for (m = mfirst; m != NULL; m = m->next)
5577 if (m->p_type == PT_LOAD
5578 && m->count != 0
5579 && m->sections[0]->vma >= info->relro_start
5580 && m->sections[0]->vma < info->relro_end)
5582 i = m->count;
5583 while (--i != (unsigned) -1)
5585 if (m->sections[i]->size > 0
5586 && (m->sections[i]->flags & SEC_LOAD) != 0
5587 && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
5588 break;
5591 if (i != (unsigned) -1)
5592 break;
5596 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5597 if (m != NULL)
5599 amt = sizeof (struct elf_segment_map);
5600 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5601 if (m == NULL)
5602 goto error_return;
5603 m->next = NULL;
5604 m->p_type = PT_GNU_RELRO;
5605 *pm = m;
5606 pm = &m->next;
5610 free (sections);
5611 elf_seg_map (abfd) = mfirst;
5614 if (!elf_modify_segment_map (abfd, info, no_user_phdrs || info == NULL))
5615 return false;
5617 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5618 ++count;
5619 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5621 return true;
5623 error_return:
5624 free (sections);
5625 return false;
5628 /* Sort sections by address. */
5630 static int
5631 elf_sort_sections (const void *arg1, const void *arg2)
5633 const asection *sec1 = *(const asection **) arg1;
5634 const asection *sec2 = *(const asection **) arg2;
5635 bfd_size_type size1, size2;
5637 /* Sort by LMA first, since this is the address used to
5638 place the section into a segment. */
5639 if (sec1->lma < sec2->lma)
5640 return -1;
5641 else if (sec1->lma > sec2->lma)
5642 return 1;
5644 /* Then sort by VMA. Normally the LMA and the VMA will be
5645 the same, and this will do nothing. */
5646 if (sec1->vma < sec2->vma)
5647 return -1;
5648 else if (sec1->vma > sec2->vma)
5649 return 1;
5651 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5653 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5654 && (x)->size != 0)
5656 if (TOEND (sec1))
5658 if (!TOEND (sec2))
5659 return 1;
5661 else if (TOEND (sec2))
5662 return -1;
5664 #undef TOEND
5666 /* Sort by size, to put zero sized sections
5667 before others at the same address. */
5669 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5670 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5672 if (size1 < size2)
5673 return -1;
5674 if (size1 > size2)
5675 return 1;
5677 return sec1->target_index - sec2->target_index;
5680 /* This qsort comparison functions sorts PT_LOAD segments first and
5681 by p_paddr, for assign_file_positions_for_load_sections. */
5683 static int
5684 elf_sort_segments (const void *arg1, const void *arg2)
5686 const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5687 const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5689 if (m1->p_type != m2->p_type)
5691 if (m1->p_type == PT_NULL)
5692 return 1;
5693 if (m2->p_type == PT_NULL)
5694 return -1;
5695 return m1->p_type < m2->p_type ? -1 : 1;
5697 if (m1->includes_filehdr != m2->includes_filehdr)
5698 return m1->includes_filehdr ? -1 : 1;
5699 if (m1->no_sort_lma != m2->no_sort_lma)
5700 return m1->no_sort_lma ? -1 : 1;
5701 if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5703 bfd_vma lma1, lma2; /* Octets. */
5704 lma1 = 0;
5705 if (m1->p_paddr_valid)
5706 lma1 = m1->p_paddr;
5707 else if (m1->count != 0)
5709 unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5710 m1->sections[0]);
5711 lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5713 lma2 = 0;
5714 if (m2->p_paddr_valid)
5715 lma2 = m2->p_paddr;
5716 else if (m2->count != 0)
5718 unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5719 m2->sections[0]);
5720 lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5722 if (lma1 != lma2)
5723 return lma1 < lma2 ? -1 : 1;
5725 if (m1->idx != m2->idx)
5726 return m1->idx < m2->idx ? -1 : 1;
5727 return 0;
5730 /* Ian Lance Taylor writes:
5732 We shouldn't be using % with a negative signed number. That's just
5733 not good. We have to make sure either that the number is not
5734 negative, or that the number has an unsigned type. When the types
5735 are all the same size they wind up as unsigned. When file_ptr is a
5736 larger signed type, the arithmetic winds up as signed long long,
5737 which is wrong.
5739 What we're trying to say here is something like ``increase OFF by
5740 the least amount that will cause it to be equal to the VMA modulo
5741 the page size.'' */
5742 /* In other words, something like:
5744 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5745 off_offset = off % bed->maxpagesize;
5746 if (vma_offset < off_offset)
5747 adjustment = vma_offset + bed->maxpagesize - off_offset;
5748 else
5749 adjustment = vma_offset - off_offset;
5751 which can be collapsed into the expression below. */
5753 static file_ptr
5754 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5756 /* PR binutils/16199: Handle an alignment of zero. */
5757 if (maxpagesize == 0)
5758 maxpagesize = 1;
5759 return ((vma - off) % maxpagesize);
5762 static void
5763 print_segment_map (const struct elf_segment_map *m)
5765 unsigned int j;
5766 const char *pt = get_segment_type (m->p_type);
5767 char buf[32];
5769 if (pt == NULL)
5771 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5772 sprintf (buf, "LOPROC+%7.7x",
5773 (unsigned int) (m->p_type - PT_LOPROC));
5774 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5775 sprintf (buf, "LOOS+%7.7x",
5776 (unsigned int) (m->p_type - PT_LOOS));
5777 else
5778 snprintf (buf, sizeof (buf), "%8.8x",
5779 (unsigned int) m->p_type);
5780 pt = buf;
5782 fflush (stdout);
5783 fprintf (stderr, "%s:", pt);
5784 for (j = 0; j < m->count; j++)
5785 fprintf (stderr, " %s", m->sections [j]->name);
5786 putc ('\n',stderr);
5787 fflush (stderr);
5790 /* Assign file positions to the sections based on the mapping from
5791 sections to segments. This function also sets up some fields in
5792 the file header. */
5794 static bool
5795 assign_file_positions_for_load_sections (bfd *abfd,
5796 struct bfd_link_info *link_info)
5798 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5799 struct elf_segment_map *m;
5800 struct elf_segment_map *phdr_load_seg;
5801 Elf_Internal_Phdr *phdrs;
5802 Elf_Internal_Phdr *p;
5803 file_ptr off; /* Octets. */
5804 bfd_size_type maxpagesize;
5805 unsigned int alloc, actual;
5806 unsigned int i, j;
5807 struct elf_segment_map **sorted_seg_map;
5808 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5810 if (link_info == NULL
5811 && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
5812 return false;
5814 alloc = 0;
5815 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5816 m->idx = alloc++;
5818 if (alloc)
5820 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5821 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5823 else
5825 /* PR binutils/12467. */
5826 elf_elfheader (abfd)->e_phoff = 0;
5827 elf_elfheader (abfd)->e_phentsize = 0;
5830 elf_elfheader (abfd)->e_phnum = alloc;
5832 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
5834 actual = alloc;
5835 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5837 else
5839 actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5840 BFD_ASSERT (elf_program_header_size (abfd)
5841 == actual * bed->s->sizeof_phdr);
5842 BFD_ASSERT (actual >= alloc);
5845 if (alloc == 0)
5847 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
5848 return true;
5851 /* We're writing the size in elf_program_header_size (abfd),
5852 see assign_file_positions_except_relocs, so make sure we have
5853 that amount allocated, with trailing space cleared.
5854 The variable alloc contains the computed need, while
5855 elf_program_header_size (abfd) contains the size used for the
5856 layout.
5857 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5858 where the layout is forced to according to a larger size in the
5859 last iterations for the testcase ld-elf/header. */
5860 phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
5861 + alloc * sizeof (*sorted_seg_map)));
5862 sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
5863 elf_tdata (abfd)->phdr = phdrs;
5864 if (phdrs == NULL)
5865 return false;
5867 for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
5869 sorted_seg_map[j] = m;
5870 /* If elf_segment_map is not from map_sections_to_segments, the
5871 sections may not be correctly ordered. NOTE: sorting should
5872 not be done to the PT_NOTE section of a corefile, which may
5873 contain several pseudo-sections artificially created by bfd.
5874 Sorting these pseudo-sections breaks things badly. */
5875 if (m->count > 1
5876 && !(elf_elfheader (abfd)->e_type == ET_CORE
5877 && m->p_type == PT_NOTE))
5879 for (i = 0; i < m->count; i++)
5880 m->sections[i]->target_index = i;
5881 qsort (m->sections, (size_t) m->count, sizeof (asection *),
5882 elf_sort_sections);
5885 if (alloc > 1)
5886 qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
5887 elf_sort_segments);
5889 maxpagesize = 1;
5890 if ((abfd->flags & D_PAGED) != 0)
5892 if (link_info != NULL)
5893 maxpagesize = link_info->maxpagesize;
5894 else
5895 maxpagesize = bed->maxpagesize;
5898 /* Sections must map to file offsets past the ELF file header. */
5899 off = bed->s->sizeof_ehdr;
5900 /* And if one of the PT_LOAD headers doesn't include the program
5901 headers then we'll be mapping program headers in the usual
5902 position after the ELF file header. */
5903 phdr_load_seg = NULL;
5904 for (j = 0; j < alloc; j++)
5906 m = sorted_seg_map[j];
5907 if (m->p_type != PT_LOAD)
5908 break;
5909 if (m->includes_phdrs)
5911 phdr_load_seg = m;
5912 break;
5915 if (phdr_load_seg == NULL)
5916 off += actual * bed->s->sizeof_phdr;
5918 for (j = 0; j < alloc; j++)
5920 asection **secpp;
5921 bfd_vma off_adjust; /* Octets. */
5922 bool no_contents;
5923 bfd_size_type p_align;
5924 bool p_align_p;
5926 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5927 number of sections with contents contributing to both p_filesz
5928 and p_memsz, followed by a number of sections with no contents
5929 that just contribute to p_memsz. In this loop, OFF tracks next
5930 available file offset for PT_LOAD and PT_NOTE segments. */
5931 m = sorted_seg_map[j];
5932 p = phdrs + m->idx;
5933 p->p_type = m->p_type;
5934 p->p_flags = m->p_flags;
5935 p_align = bed->p_align;
5936 p_align_p = false;
5938 if (m->count == 0)
5939 p->p_vaddr = m->p_vaddr_offset * opb;
5940 else
5941 p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
5943 if (m->p_paddr_valid)
5944 p->p_paddr = m->p_paddr;
5945 else if (m->count == 0)
5946 p->p_paddr = 0;
5947 else
5948 p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
5950 if (p->p_type == PT_LOAD
5951 && (abfd->flags & D_PAGED) != 0)
5953 /* p_align in demand paged PT_LOAD segments effectively stores
5954 the maximum page size. When copying an executable with
5955 objcopy, we set m->p_align from the input file. Use this
5956 value for maxpagesize rather than bed->maxpagesize, which
5957 may be different. Note that we use maxpagesize for PT_TLS
5958 segment alignment later in this function, so we are relying
5959 on at least one PT_LOAD segment appearing before a PT_TLS
5960 segment. */
5961 if (m->p_align_valid)
5962 maxpagesize = m->p_align;
5963 else if (p_align != 0
5964 && (link_info == NULL
5965 || !link_info->maxpagesize_is_set))
5966 /* Set p_align to the default p_align value while laying
5967 out segments aligning to the maximum page size or the
5968 largest section alignment. The run-time loader can
5969 align segments to the default p_align value or the
5970 maximum page size, depending on system page size. */
5971 p_align_p = true;
5973 p->p_align = maxpagesize;
5975 else if (m->p_align_valid)
5976 p->p_align = m->p_align;
5977 else if (m->count == 0)
5978 p->p_align = 1 << bed->s->log_file_align;
5980 if (m == phdr_load_seg)
5981 off += actual * bed->s->sizeof_phdr;
5983 no_contents = false;
5984 off_adjust = 0;
5985 if (p->p_type == PT_LOAD
5986 && m->count > 0)
5988 bfd_size_type align; /* Bytes. */
5989 unsigned int align_power = 0;
5991 if (m->p_align_valid)
5992 align = p->p_align;
5993 else
5995 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5997 unsigned int secalign;
5999 secalign = bfd_section_alignment (*secpp);
6000 if (secalign > align_power)
6001 align_power = secalign;
6003 align = (bfd_size_type) 1 << align_power;
6004 if (align < maxpagesize)
6006 /* If a section requires alignment higher than the
6007 default p_align value, don't set p_align to the
6008 default p_align value. */
6009 if (align > p_align)
6010 p_align_p = false;
6011 align = maxpagesize;
6013 else
6015 /* If a section requires alignment higher than the
6016 maximum page size, set p_align to the section
6017 alignment. */
6018 p_align_p = true;
6019 p_align = align;
6023 for (i = 0; i < m->count; i++)
6024 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
6025 /* If we aren't making room for this section, then
6026 it must be SHT_NOBITS regardless of what we've
6027 set via struct bfd_elf_special_section. */
6028 elf_section_type (m->sections[i]) = SHT_NOBITS;
6030 /* Find out whether this segment contains any loadable
6031 sections. */
6032 no_contents = true;
6033 for (i = 0; i < m->count; i++)
6034 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
6036 no_contents = false;
6037 break;
6040 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
6042 /* Broken hardware and/or kernel require that files do not
6043 map the same page with different permissions on some hppa
6044 processors. */
6045 if (j != 0
6046 && (abfd->flags & D_PAGED) != 0
6047 && bed->no_page_alias
6048 && (off & (maxpagesize - 1)) != 0
6049 && ((off & -maxpagesize)
6050 == ((off + off_adjust) & -maxpagesize)))
6051 off_adjust += maxpagesize;
6052 off += off_adjust;
6053 if (no_contents)
6055 /* We shouldn't need to align the segment on disk since
6056 the segment doesn't need file space, but the gABI
6057 arguably requires the alignment and glibc ld.so
6058 checks it. So to comply with the alignment
6059 requirement but not waste file space, we adjust
6060 p_offset for just this segment. (OFF_ADJUST is
6061 subtracted from OFF later.) This may put p_offset
6062 past the end of file, but that shouldn't matter. */
6064 else
6065 off_adjust = 0;
6067 /* Make sure the .dynamic section is the first section in the
6068 PT_DYNAMIC segment. */
6069 else if (p->p_type == PT_DYNAMIC
6070 && m->count > 1
6071 && strcmp (m->sections[0]->name, ".dynamic") != 0)
6073 _bfd_error_handler
6074 (_("%pB: The first section in the PT_DYNAMIC segment"
6075 " is not the .dynamic section"),
6076 abfd);
6077 bfd_set_error (bfd_error_bad_value);
6078 return false;
6080 /* Set the note section type to SHT_NOTE. */
6081 else if (p->p_type == PT_NOTE)
6082 for (i = 0; i < m->count; i++)
6083 elf_section_type (m->sections[i]) = SHT_NOTE;
6085 if (m->includes_filehdr)
6087 if (!m->p_flags_valid)
6088 p->p_flags |= PF_R;
6089 p->p_filesz = bed->s->sizeof_ehdr;
6090 p->p_memsz = bed->s->sizeof_ehdr;
6091 if (p->p_type == PT_LOAD)
6093 if (m->count > 0)
6095 if (p->p_vaddr < (bfd_vma) off
6096 || (!m->p_paddr_valid
6097 && p->p_paddr < (bfd_vma) off))
6099 _bfd_error_handler
6100 (_("%pB: not enough room for program headers,"
6101 " try linking with -N"),
6102 abfd);
6103 bfd_set_error (bfd_error_bad_value);
6104 return false;
6106 p->p_vaddr -= off;
6107 if (!m->p_paddr_valid)
6108 p->p_paddr -= off;
6111 else if (sorted_seg_map[0]->includes_filehdr)
6113 Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
6114 p->p_vaddr = filehdr->p_vaddr;
6115 if (!m->p_paddr_valid)
6116 p->p_paddr = filehdr->p_paddr;
6120 if (m->includes_phdrs)
6122 if (!m->p_flags_valid)
6123 p->p_flags |= PF_R;
6124 p->p_filesz += actual * bed->s->sizeof_phdr;
6125 p->p_memsz += actual * bed->s->sizeof_phdr;
6126 if (!m->includes_filehdr)
6128 if (p->p_type == PT_LOAD)
6130 p->p_offset = off - actual * bed->s->sizeof_phdr;
6131 elf_elfheader (abfd)->e_phoff = p->p_offset;
6132 if (m->count > 0)
6134 p->p_vaddr -= off - p->p_offset;
6135 if (!m->p_paddr_valid)
6136 p->p_paddr -= off - p->p_offset;
6139 else if (phdr_load_seg != NULL)
6141 /* Also set PT_PHDR to match phdr_load_seg. We've
6142 sorted segments so that phdr_load_seg will
6143 already be set by the code immediately above. */
6144 Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
6145 bfd_vma phdr_off = 0; /* Octets. */
6146 if (phdr_load_seg->includes_filehdr)
6147 phdr_off = bed->s->sizeof_ehdr;
6148 p->p_vaddr = phdr->p_vaddr + phdr_off;
6149 if (!m->p_paddr_valid)
6150 p->p_paddr = phdr->p_paddr + phdr_off;
6151 p->p_offset = phdr->p_offset + phdr_off;
6153 else
6154 p->p_offset = bed->s->sizeof_ehdr;
6158 if (p->p_type == PT_LOAD
6159 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
6161 if (!m->includes_filehdr && !m->includes_phdrs)
6163 p->p_offset = off;
6164 if (no_contents)
6166 /* Put meaningless p_offset for PT_LOAD segments
6167 without file contents somewhere within the first
6168 page, in an attempt to not point past EOF. */
6169 bfd_size_type align = maxpagesize;
6170 if (align < p->p_align)
6171 align = p->p_align;
6172 if (align < 1)
6173 align = 1;
6174 p->p_offset = off % align;
6177 else
6179 file_ptr adjust; /* Octets. */
6181 adjust = off - (p->p_offset + p->p_filesz);
6182 if (!no_contents)
6183 p->p_filesz += adjust;
6184 p->p_memsz += adjust;
6188 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
6189 maps. Set filepos for sections in PT_LOAD segments, and in
6190 core files, for sections in PT_NOTE segments.
6191 assign_file_positions_for_non_load_sections will set filepos
6192 for other sections and update p_filesz for other segments. */
6193 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6195 asection *sec;
6196 bfd_size_type align;
6197 Elf_Internal_Shdr *this_hdr;
6199 sec = *secpp;
6200 this_hdr = &elf_section_data (sec)->this_hdr;
6201 align = (bfd_size_type) 1 << bfd_section_alignment (sec);
6203 if ((p->p_type == PT_LOAD
6204 || p->p_type == PT_TLS)
6205 && (this_hdr->sh_type != SHT_NOBITS
6206 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
6207 && ((this_hdr->sh_flags & SHF_TLS) == 0
6208 || p->p_type == PT_TLS))))
6210 bfd_vma p_start = p->p_paddr; /* Octets. */
6211 bfd_vma p_end = p_start + p->p_memsz; /* Octets. */
6212 bfd_vma s_start = sec->lma * opb; /* Octets. */
6213 bfd_vma adjust = s_start - p_end; /* Octets. */
6215 if (adjust != 0
6216 && (s_start < p_end
6217 || p_end < p_start))
6219 _bfd_error_handler
6220 /* xgettext:c-format */
6221 (_("%pB: section %pA lma %#" PRIx64
6222 " adjusted to %#" PRIx64),
6223 abfd, sec, (uint64_t) s_start / opb,
6224 (uint64_t) p_end / opb);
6225 adjust = 0;
6226 sec->lma = p_end / opb;
6228 p->p_memsz += adjust;
6230 if (p->p_type == PT_LOAD)
6232 if (this_hdr->sh_type != SHT_NOBITS)
6234 off_adjust = 0;
6235 if (p->p_filesz + adjust < p->p_memsz)
6237 /* We have a PROGBITS section following NOBITS ones.
6238 Allocate file space for the NOBITS section(s).
6239 We don't need to write out the zeros, posix
6240 fseek past the end of data already written
6241 followed by a write at that location is
6242 guaranteed to result in zeros being read
6243 from the gap. */
6244 adjust = p->p_memsz - p->p_filesz;
6247 /* We only adjust sh_offset in SHT_NOBITS sections
6248 as would seem proper for their address when the
6249 section is first in the segment. sh_offset
6250 doesn't really have any significance for
6251 SHT_NOBITS anyway, apart from a notional position
6252 relative to other sections. Historically we
6253 didn't bother with adjusting sh_offset and some
6254 programs depend on it not being adjusted. See
6255 pr12921 and pr25662. */
6256 if (this_hdr->sh_type != SHT_NOBITS || i == 0)
6258 off += adjust;
6259 if (this_hdr->sh_type == SHT_NOBITS)
6260 off_adjust += adjust;
6263 if (this_hdr->sh_type != SHT_NOBITS)
6264 p->p_filesz += adjust;
6267 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
6269 /* The section at i == 0 is the one that actually contains
6270 everything. */
6271 if (i == 0)
6273 this_hdr->sh_offset = sec->filepos = off;
6274 off += this_hdr->sh_size;
6275 p->p_filesz = this_hdr->sh_size;
6276 p->p_memsz = 0;
6277 p->p_align = 1;
6279 else
6281 /* The rest are fake sections that shouldn't be written. */
6282 sec->filepos = 0;
6283 sec->size = 0;
6284 sec->flags = 0;
6285 continue;
6288 else
6290 if (p->p_type == PT_LOAD)
6292 this_hdr->sh_offset = sec->filepos = off;
6293 if (this_hdr->sh_type != SHT_NOBITS)
6294 off += this_hdr->sh_size;
6296 else if (this_hdr->sh_type == SHT_NOBITS
6297 && (this_hdr->sh_flags & SHF_TLS) != 0
6298 && this_hdr->sh_offset == 0)
6300 /* This is a .tbss section that didn't get a PT_LOAD.
6301 (See _bfd_elf_map_sections_to_segments "Create a
6302 final PT_LOAD".) Set sh_offset to the value it
6303 would have if we had created a zero p_filesz and
6304 p_memsz PT_LOAD header for the section. This
6305 also makes the PT_TLS header have the same
6306 p_offset value. */
6307 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
6308 off, align);
6309 this_hdr->sh_offset = sec->filepos = off + adjust;
6312 if (this_hdr->sh_type != SHT_NOBITS)
6314 p->p_filesz += this_hdr->sh_size;
6315 /* A load section without SHF_ALLOC is something like
6316 a note section in a PT_NOTE segment. These take
6317 file space but are not loaded into memory. */
6318 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6319 p->p_memsz += this_hdr->sh_size;
6321 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6323 if (p->p_type == PT_TLS)
6324 p->p_memsz += this_hdr->sh_size;
6326 /* .tbss is special. It doesn't contribute to p_memsz of
6327 normal segments. */
6328 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
6329 p->p_memsz += this_hdr->sh_size;
6332 if (align > p->p_align
6333 && !m->p_align_valid
6334 && (p->p_type != PT_LOAD
6335 || (abfd->flags & D_PAGED) == 0))
6336 p->p_align = align;
6339 if (!m->p_flags_valid)
6341 p->p_flags |= PF_R;
6342 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
6343 p->p_flags |= PF_X;
6344 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
6345 p->p_flags |= PF_W;
6349 off -= off_adjust;
6351 /* PR ld/20815 - Check that the program header segment, if
6352 present, will be loaded into memory. */
6353 if (p->p_type == PT_PHDR
6354 && phdr_load_seg == NULL
6355 && !(bed->elf_backend_allow_non_load_phdr != NULL
6356 && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
6358 /* The fix for this error is usually to edit the linker script being
6359 used and set up the program headers manually. Either that or
6360 leave room for the headers at the start of the SECTIONS. */
6361 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6362 " by LOAD segment"),
6363 abfd);
6364 if (link_info == NULL)
6365 return false;
6366 /* Arrange for the linker to exit with an error, deleting
6367 the output file unless --noinhibit-exec is given. */
6368 link_info->callbacks->info ("%X");
6371 /* Check that all sections are in a PT_LOAD segment.
6372 Don't check funky gdb generated core files. */
6373 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
6375 bool check_vma = true;
6377 for (i = 1; i < m->count; i++)
6378 if (m->sections[i]->vma == m->sections[i - 1]->vma
6379 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
6380 ->this_hdr), p) != 0
6381 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
6382 ->this_hdr), p) != 0)
6384 /* Looks like we have overlays packed into the segment. */
6385 check_vma = false;
6386 break;
6389 for (i = 0; i < m->count; i++)
6391 Elf_Internal_Shdr *this_hdr;
6392 asection *sec;
6394 sec = m->sections[i];
6395 this_hdr = &(elf_section_data(sec)->this_hdr);
6396 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
6397 && !ELF_TBSS_SPECIAL (this_hdr, p))
6399 _bfd_error_handler
6400 /* xgettext:c-format */
6401 (_("%pB: section `%pA' can't be allocated in segment %u"),
6402 abfd, sec, m->idx);
6403 print_segment_map (m);
6407 if (p_align_p)
6408 p->p_align = p_align;
6412 elf_next_file_pos (abfd) = off;
6414 if (link_info != NULL
6415 && phdr_load_seg != NULL
6416 && phdr_load_seg->includes_filehdr)
6418 /* There is a segment that contains both the file headers and the
6419 program headers, so provide a symbol __ehdr_start pointing there.
6420 A program can use this to examine itself robustly. */
6422 struct elf_link_hash_entry *hash
6423 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
6424 false, false, true);
6425 /* If the symbol was referenced and not defined, define it. */
6426 if (hash != NULL
6427 && (hash->root.type == bfd_link_hash_new
6428 || hash->root.type == bfd_link_hash_undefined
6429 || hash->root.type == bfd_link_hash_undefweak
6430 || hash->root.type == bfd_link_hash_common))
6432 asection *s = NULL;
6433 bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
6435 if (phdr_load_seg->count != 0)
6436 /* The segment contains sections, so use the first one. */
6437 s = phdr_load_seg->sections[0];
6438 else
6439 /* Use the first (i.e. lowest-addressed) section in any segment. */
6440 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6441 if (m->p_type == PT_LOAD && m->count != 0)
6443 s = m->sections[0];
6444 break;
6447 if (s != NULL)
6449 hash->root.u.def.value = filehdr_vaddr - s->vma;
6450 hash->root.u.def.section = s;
6452 else
6454 hash->root.u.def.value = filehdr_vaddr;
6455 hash->root.u.def.section = bfd_abs_section_ptr;
6458 hash->root.type = bfd_link_hash_defined;
6459 hash->def_regular = 1;
6460 hash->non_elf = 0;
6464 return true;
6467 /* Determine if a bfd is a debuginfo file. Unfortunately there
6468 is no defined method for detecting such files, so we have to
6469 use heuristics instead. */
6471 bool
6472 is_debuginfo_file (bfd *abfd)
6474 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6475 return false;
6477 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6478 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6479 Elf_Internal_Shdr **headerp;
6481 for (headerp = start_headers; headerp < end_headers; headerp ++)
6483 Elf_Internal_Shdr *header = * headerp;
6485 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6486 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6487 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6488 && header->sh_type != SHT_NOBITS
6489 && header->sh_type != SHT_NOTE)
6490 return false;
6493 return true;
6496 /* Assign file positions for other sections, except for compressed debug
6497 and sections assigned in _bfd_elf_assign_file_positions_for_non_load. */
6499 static bool
6500 assign_file_positions_for_non_load_sections (bfd *abfd,
6501 struct bfd_link_info *link_info)
6503 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6504 Elf_Internal_Shdr **i_shdrpp;
6505 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
6506 Elf_Internal_Phdr *phdrs;
6507 Elf_Internal_Phdr *p;
6508 struct elf_segment_map *m;
6509 file_ptr off;
6510 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
6511 bfd_vma maxpagesize;
6513 if (link_info != NULL)
6514 maxpagesize = link_info->maxpagesize;
6515 else
6516 maxpagesize = bed->maxpagesize;
6517 i_shdrpp = elf_elfsections (abfd);
6518 end_hdrpp = i_shdrpp + elf_numsections (abfd);
6519 off = elf_next_file_pos (abfd);
6520 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
6522 Elf_Internal_Shdr *hdr;
6523 bfd_vma align;
6525 hdr = *hdrpp;
6526 if (hdr->bfd_section != NULL
6527 && (hdr->bfd_section->filepos != 0
6528 || (hdr->sh_type == SHT_NOBITS
6529 && hdr->contents == NULL)))
6530 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
6531 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6533 if (hdr->sh_size != 0
6534 /* PR 24717 - debuginfo files are known to be not strictly
6535 compliant with the ELF standard. In particular they often
6536 have .note.gnu.property sections that are outside of any
6537 loadable segment. This is not a problem for such files,
6538 so do not warn about them. */
6539 && ! is_debuginfo_file (abfd))
6540 _bfd_error_handler
6541 /* xgettext:c-format */
6542 (_("%pB: warning: allocated section `%s' not in segment"),
6543 abfd,
6544 (hdr->bfd_section == NULL
6545 ? "*unknown*"
6546 : hdr->bfd_section->name));
6547 /* We don't need to page align empty sections. */
6548 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
6549 align = maxpagesize;
6550 else
6551 align = hdr->sh_addralign & -hdr->sh_addralign;
6552 off += vma_page_aligned_bias (hdr->sh_addr, off, align);
6553 off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
6554 bed->s->log_file_align);
6556 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6557 && hdr->bfd_section == NULL)
6558 /* We don't know the offset of these sections yet:
6559 their size has not been decided. */
6560 || (abfd->is_linker_output
6561 && hdr->bfd_section != NULL
6562 && (hdr->sh_name == -1u
6563 || bfd_section_is_ctf (hdr->bfd_section)))
6564 || hdr == i_shdrpp[elf_onesymtab (abfd)]
6565 || (elf_symtab_shndx_list (abfd) != NULL
6566 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6567 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6568 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
6569 hdr->sh_offset = -1;
6570 else
6571 off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
6573 elf_next_file_pos (abfd) = off;
6575 /* Now that we have set the section file positions, we can set up
6576 the file positions for the non PT_LOAD segments. */
6577 phdrs = elf_tdata (abfd)->phdr;
6578 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6580 if (p->p_type == PT_GNU_RELRO)
6582 bfd_vma start, end; /* Bytes. */
6583 bool ok;
6585 if (link_info != NULL)
6587 /* During linking the range of the RELRO segment is passed
6588 in link_info. Note that there may be padding between
6589 relro_start and the first RELRO section. */
6590 start = link_info->relro_start;
6591 end = link_info->relro_end;
6593 else if (m->count != 0)
6595 if (!m->p_size_valid)
6596 abort ();
6597 start = m->sections[0]->vma;
6598 end = start + m->p_size / opb;
6600 else
6602 start = 0;
6603 end = 0;
6606 ok = false;
6607 if (start < end)
6609 struct elf_segment_map *lm;
6610 const Elf_Internal_Phdr *lp;
6611 unsigned int i;
6613 /* Find a LOAD segment containing a section in the RELRO
6614 segment. */
6615 for (lm = elf_seg_map (abfd), lp = phdrs;
6616 lm != NULL;
6617 lm = lm->next, lp++)
6619 if (lp->p_type == PT_LOAD
6620 && lm->count != 0
6621 && (lm->sections[lm->count - 1]->vma
6622 + (!IS_TBSS (lm->sections[lm->count - 1])
6623 ? lm->sections[lm->count - 1]->size / opb
6624 : 0)) > start
6625 && lm->sections[0]->vma < end)
6626 break;
6629 if (lm != NULL)
6631 /* Find the section starting the RELRO segment. */
6632 for (i = 0; i < lm->count; i++)
6634 asection *s = lm->sections[i];
6635 if (s->vma >= start
6636 && s->vma < end
6637 && s->size != 0)
6638 break;
6641 if (i < lm->count)
6643 p->p_vaddr = lm->sections[i]->vma * opb;
6644 p->p_paddr = lm->sections[i]->lma * opb;
6645 p->p_offset = lm->sections[i]->filepos;
6646 p->p_memsz = end * opb - p->p_vaddr;
6647 p->p_filesz = p->p_memsz;
6649 /* The RELRO segment typically ends a few bytes
6650 into .got.plt but other layouts are possible.
6651 In cases where the end does not match any
6652 loaded section (for instance is in file
6653 padding), trim p_filesz back to correspond to
6654 the end of loaded section contents. */
6655 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6656 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6658 /* Preserve the alignment and flags if they are
6659 valid. The gold linker generates RW/4 for
6660 the PT_GNU_RELRO section. It is better for
6661 objcopy/strip to honor these attributes
6662 otherwise gdb will choke when using separate
6663 debug files. */
6664 if (!m->p_align_valid)
6665 p->p_align = 1;
6666 if (!m->p_flags_valid)
6667 p->p_flags = PF_R;
6668 ok = true;
6673 if (!ok)
6675 if (link_info != NULL)
6676 _bfd_error_handler
6677 (_("%pB: warning: unable to allocate any sections"
6678 " to PT_GNU_RELRO segment"),
6679 abfd);
6680 memset (p, 0, sizeof *p);
6683 else if (p->p_type == PT_GNU_STACK)
6685 if (m->p_size_valid)
6686 p->p_memsz = m->p_size;
6688 else if (m->count != 0)
6690 unsigned int i;
6692 if (p->p_type != PT_LOAD
6693 && (p->p_type != PT_NOTE
6694 || bfd_get_format (abfd) != bfd_core))
6696 /* A user specified segment layout may include a PHDR
6697 segment that overlaps with a LOAD segment... */
6698 if (p->p_type == PT_PHDR)
6700 m->count = 0;
6701 continue;
6704 if (m->includes_filehdr || m->includes_phdrs)
6706 /* PR 17512: file: 2195325e. */
6707 _bfd_error_handler
6708 (_("%pB: error: non-load segment %d includes file header "
6709 "and/or program header"),
6710 abfd, (int) (p - phdrs));
6711 return false;
6714 p->p_filesz = 0;
6715 p->p_offset = m->sections[0]->filepos;
6716 for (i = m->count; i-- != 0;)
6718 asection *sect = m->sections[i];
6719 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6720 if (hdr->sh_type != SHT_NOBITS)
6722 p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
6723 /* NB: p_memsz of the loadable PT_NOTE segment
6724 should be the same as p_filesz. */
6725 if (p->p_type == PT_NOTE
6726 && (hdr->sh_flags & SHF_ALLOC) != 0)
6727 p->p_memsz = p->p_filesz;
6728 break;
6735 return true;
6738 static elf_section_list *
6739 find_section_in_list (unsigned int i, elf_section_list * list)
6741 for (;list != NULL; list = list->next)
6742 if (list->ndx == i)
6743 break;
6744 return list;
6747 /* Work out the file positions of all the sections. This is called by
6748 _bfd_elf_compute_section_file_positions. All the section sizes and
6749 VMAs must be known before this is called.
6751 Reloc sections come in two flavours: Those processed specially as
6752 "side-channel" data attached to a section to which they apply, and
6753 those that bfd doesn't process as relocations. The latter sort are
6754 stored in a normal bfd section by bfd_section_from_shdr. We don't
6755 consider the former sort here, unless they form part of the loadable
6756 image. Reloc sections not assigned here (and compressed debugging
6757 sections and CTF sections which nothing else in the file can rely
6758 upon) will be handled later by assign_file_positions_for_relocs.
6760 We also don't set the positions of the .symtab and .strtab here. */
6762 static bool
6763 assign_file_positions_except_relocs (bfd *abfd,
6764 struct bfd_link_info *link_info)
6766 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6767 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6768 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6769 unsigned int alloc;
6771 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6772 && bfd_get_format (abfd) != bfd_core)
6774 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6775 unsigned int num_sec = elf_numsections (abfd);
6776 Elf_Internal_Shdr **hdrpp;
6777 unsigned int i;
6778 file_ptr off;
6780 /* Start after the ELF header. */
6781 off = i_ehdrp->e_ehsize;
6783 /* We are not creating an executable, which means that we are
6784 not creating a program header, and that the actual order of
6785 the sections in the file is unimportant. */
6786 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6788 Elf_Internal_Shdr *hdr;
6790 hdr = *hdrpp;
6791 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6792 && hdr->bfd_section == NULL)
6793 /* Do not assign offsets for these sections yet: we don't know
6794 their sizes. */
6795 || (abfd->is_linker_output
6796 && hdr->bfd_section != NULL
6797 && (hdr->sh_name == -1u
6798 || bfd_section_is_ctf (hdr->bfd_section)))
6799 || i == elf_onesymtab (abfd)
6800 || (elf_symtab_shndx_list (abfd) != NULL
6801 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6802 || i == elf_strtab_sec (abfd)
6803 || i == elf_shstrtab_sec (abfd))
6805 hdr->sh_offset = -1;
6807 else
6808 /* There shouldn't be a need to effect "capped" file alignment here,
6809 yet at least the Linux kernel's modpost utility was found to be
6810 unhappy without. While the issue was addressed there, let's be
6811 kind for at least the foreseeable future ... */
6812 off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
6813 bed->s->log_file_align);
6816 elf_next_file_pos (abfd) = off;
6817 elf_program_header_size (abfd) = 0;
6819 else
6821 /* Assign file positions for the loaded sections based on the
6822 assignment of sections to segments. */
6823 if (!assign_file_positions_for_load_sections (abfd, link_info))
6824 return false;
6826 /* And for non-load sections. */
6827 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6828 return false;
6831 if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
6832 return false;
6834 /* Write out the program headers. */
6835 alloc = i_ehdrp->e_phnum;
6836 if (alloc != 0)
6838 if (link_info != NULL && ! link_info->no_warn_rwx_segments)
6840 bool warned_tls = false;
6841 bool warned_rwx = false;
6843 /* Memory resident segments with non-zero size and RWX
6844 permissions are a security risk, so we generate a warning
6845 here if we are creating any. */
6846 unsigned int i;
6848 for (i = 0; i < alloc; i++)
6850 const Elf_Internal_Phdr * phdr = tdata->phdr + i;
6852 if (phdr->p_memsz == 0)
6853 continue;
6855 if (! warned_tls
6856 && phdr->p_type == PT_TLS
6857 && (phdr->p_flags & PF_X))
6859 if (link_info->warn_is_error_for_rwx_segments)
6861 _bfd_error_handler (_("\
6862 error: %pB has a TLS segment with execute permission"),
6863 abfd);
6864 return false;
6867 _bfd_error_handler (_("\
6868 warning: %pB has a TLS segment with execute permission"),
6869 abfd);
6870 if (warned_rwx)
6871 break;
6873 warned_tls = true;
6875 else if (! warned_rwx
6876 && phdr->p_type == PT_LOAD
6877 && ((phdr->p_flags & (PF_R | PF_W | PF_X))
6878 == (PF_R | PF_W | PF_X)))
6880 if (link_info->warn_is_error_for_rwx_segments)
6882 _bfd_error_handler (_("\
6883 error: %pB has a LOAD segment with RWX permissions"),
6884 abfd);
6885 return false;
6888 _bfd_error_handler (_("\
6889 warning: %pB has a LOAD segment with RWX permissions"),
6890 abfd);
6891 if (warned_tls)
6892 break;
6894 warned_rwx = true;
6899 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
6900 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6901 return false;
6904 return true;
6907 bool
6908 _bfd_elf_init_file_header (bfd *abfd,
6909 struct bfd_link_info *info ATTRIBUTE_UNUSED)
6911 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
6912 struct elf_strtab_hash *shstrtab;
6913 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6915 i_ehdrp = elf_elfheader (abfd);
6917 shstrtab = _bfd_elf_strtab_init ();
6918 if (shstrtab == NULL)
6919 return false;
6921 elf_shstrtab (abfd) = shstrtab;
6923 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6924 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6925 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6926 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6928 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6929 i_ehdrp->e_ident[EI_DATA] =
6930 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6931 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6933 if ((abfd->flags & DYNAMIC) != 0)
6934 i_ehdrp->e_type = ET_DYN;
6935 else if ((abfd->flags & EXEC_P) != 0)
6936 i_ehdrp->e_type = ET_EXEC;
6937 else if (bfd_get_format (abfd) == bfd_core)
6938 i_ehdrp->e_type = ET_CORE;
6939 else
6940 i_ehdrp->e_type = ET_REL;
6942 switch (bfd_get_arch (abfd))
6944 case bfd_arch_unknown:
6945 i_ehdrp->e_machine = EM_NONE;
6946 break;
6948 /* There used to be a long list of cases here, each one setting
6949 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6950 in the corresponding bfd definition. To avoid duplication,
6951 the switch was removed. Machines that need special handling
6952 can generally do it in elf_backend_final_write_processing(),
6953 unless they need the information earlier than the final write.
6954 Such need can generally be supplied by replacing the tests for
6955 e_machine with the conditions used to determine it. */
6956 default:
6957 i_ehdrp->e_machine = bed->elf_machine_code;
6960 i_ehdrp->e_version = bed->s->ev_current;
6961 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6963 /* No program header, for now. */
6964 i_ehdrp->e_phoff = 0;
6965 i_ehdrp->e_phentsize = 0;
6966 i_ehdrp->e_phnum = 0;
6968 /* Each bfd section is section header entry. */
6969 i_ehdrp->e_entry = bfd_get_start_address (abfd);
6970 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6972 elf_tdata (abfd)->symtab_hdr.sh_name =
6973 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
6974 elf_tdata (abfd)->strtab_hdr.sh_name =
6975 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
6976 elf_tdata (abfd)->shstrtab_hdr.sh_name =
6977 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
6978 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
6979 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
6980 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
6981 return false;
6983 return true;
6986 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
6988 FIXME: We used to have code here to sort the PT_LOAD segments into
6989 ascending order, as per the ELF spec. But this breaks some programs,
6990 including the Linux kernel. But really either the spec should be
6991 changed or the programs updated. */
6993 bool
6994 _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
6996 if (link_info != NULL && bfd_link_pie (link_info))
6998 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
6999 unsigned int num_segments = i_ehdrp->e_phnum;
7000 struct elf_obj_tdata *tdata = elf_tdata (obfd);
7001 Elf_Internal_Phdr *segment = tdata->phdr;
7002 Elf_Internal_Phdr *end_segment = &segment[num_segments];
7004 /* Find the lowest p_vaddr in PT_LOAD segments. */
7005 bfd_vma p_vaddr = (bfd_vma) -1;
7006 for (; segment < end_segment; segment++)
7007 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
7008 p_vaddr = segment->p_vaddr;
7010 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
7011 segments is non-zero. */
7012 if (p_vaddr)
7013 i_ehdrp->e_type = ET_EXEC;
7015 return true;
7018 /* Assign file positions for all the reloc sections which are not part
7019 of the loadable file image, and the file position of section headers. */
7021 static bool
7022 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
7024 file_ptr off;
7025 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
7026 Elf_Internal_Shdr *shdrp;
7027 Elf_Internal_Ehdr *i_ehdrp;
7028 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7030 /* Skip non-load sections without section header. */
7031 if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
7032 return true;
7034 off = elf_next_file_pos (abfd);
7036 shdrpp = elf_elfsections (abfd);
7037 end_shdrpp = shdrpp + elf_numsections (abfd);
7038 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
7040 shdrp = *shdrpp;
7041 if (shdrp->sh_offset == -1)
7043 asection *sec = shdrp->bfd_section;
7044 if (sec == NULL
7045 || shdrp->sh_type == SHT_REL
7046 || shdrp->sh_type == SHT_RELA)
7048 else if (bfd_section_is_ctf (sec))
7050 /* Update section size and contents. */
7051 shdrp->sh_size = sec->size;
7052 shdrp->contents = sec->contents;
7054 else if (shdrp->sh_name == -1u)
7056 const char *name = sec->name;
7057 struct bfd_elf_section_data *d;
7059 /* Compress DWARF debug sections. */
7060 if (!bfd_compress_section (abfd, sec, shdrp->contents))
7061 return false;
7063 if (sec->compress_status == COMPRESS_SECTION_DONE
7064 && (abfd->flags & BFD_COMPRESS_GABI) == 0
7065 && name[1] == 'd')
7067 /* If section is compressed with zlib-gnu, convert
7068 section name from .debug_* to .zdebug_*. */
7069 char *new_name = bfd_debug_name_to_zdebug (abfd, name);
7070 if (new_name == NULL)
7071 return false;
7072 name = new_name;
7074 /* Add section name to section name section. */
7075 shdrp->sh_name
7076 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
7077 name, false);
7078 d = elf_section_data (sec);
7080 /* Add reloc section name to section name section. */
7081 if (d->rel.hdr
7082 && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr,
7083 name, false))
7084 return false;
7085 if (d->rela.hdr
7086 && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr,
7087 name, true))
7088 return false;
7090 /* Update section size and contents. */
7091 shdrp->sh_size = sec->size;
7092 shdrp->contents = sec->contents;
7093 sec->contents = NULL;
7096 off = _bfd_elf_assign_file_position_for_section (shdrp, off,
7097 (abfd->flags & (EXEC_P | DYNAMIC))
7098 || bfd_get_format (abfd) == bfd_core,
7099 bed->s->log_file_align);
7103 /* Place section name section after DWARF debug sections have been
7104 compressed. */
7105 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
7106 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
7107 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
7108 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true, 0);
7110 /* Place the section headers. */
7111 i_ehdrp = elf_elfheader (abfd);
7112 off = BFD_ALIGN (off, 1u << bed->s->log_file_align);
7113 i_ehdrp->e_shoff = off;
7114 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
7115 elf_next_file_pos (abfd) = off;
7117 return true;
7120 bool
7121 _bfd_elf_write_object_contents (bfd *abfd)
7123 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7124 Elf_Internal_Shdr **i_shdrp;
7125 bool failed;
7126 unsigned int count, num_sec;
7127 struct elf_obj_tdata *t;
7129 if (! abfd->output_has_begun
7130 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7131 return false;
7132 /* Do not rewrite ELF data when the BFD has been opened for update.
7133 abfd->output_has_begun was set to TRUE on opening, so creation of
7134 new sections, and modification of existing section sizes was
7135 restricted. This means the ELF header, program headers and
7136 section headers can't have changed. If the contents of any
7137 sections has been modified, then those changes have already been
7138 written to the BFD. */
7139 else if (abfd->direction == both_direction)
7141 BFD_ASSERT (abfd->output_has_begun);
7142 return true;
7145 i_shdrp = elf_elfsections (abfd);
7147 failed = false;
7148 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
7149 if (failed)
7150 return false;
7152 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
7153 return false;
7155 /* After writing the headers, we need to write the sections too... */
7156 num_sec = elf_numsections (abfd);
7157 for (count = 1; count < num_sec; count++)
7159 /* Don't set the sh_name field without section header. */
7160 if ((abfd->flags & BFD_NO_SECTION_HEADER) == 0)
7161 i_shdrp[count]->sh_name
7162 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
7163 i_shdrp[count]->sh_name);
7164 if (bed->elf_backend_section_processing)
7165 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
7166 return false;
7167 if (i_shdrp[count]->contents)
7169 bfd_size_type amt = i_shdrp[count]->sh_size;
7171 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
7172 || bfd_write (i_shdrp[count]->contents, amt, abfd) != amt)
7173 return false;
7177 /* Write out the section header names. */
7178 t = elf_tdata (abfd);
7179 if (elf_shstrtab (abfd) != NULL
7180 && t->shstrtab_hdr.sh_offset != -1
7181 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
7182 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
7183 return false;
7185 if (!(*bed->elf_backend_final_write_processing) (abfd))
7186 return false;
7188 if (!bed->s->write_shdrs_and_ehdr (abfd))
7189 return false;
7191 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
7192 if (t->o->build_id.after_write_object_contents != NULL
7193 && !(*t->o->build_id.after_write_object_contents) (abfd))
7194 return false;
7195 if (t->o->package_metadata.after_write_object_contents != NULL
7196 && !(*t->o->package_metadata.after_write_object_contents) (abfd))
7197 return false;
7199 return true;
7202 bool
7203 _bfd_elf_write_corefile_contents (bfd *abfd)
7205 /* Hopefully this can be done just like an object file. */
7206 return _bfd_elf_write_object_contents (abfd);
7209 /* Given a section, search the header to find them. */
7211 unsigned int
7212 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
7214 const struct elf_backend_data *bed;
7215 unsigned int sec_index;
7217 if (elf_section_data (asect) != NULL
7218 && elf_section_data (asect)->this_idx != 0)
7219 return elf_section_data (asect)->this_idx;
7221 if (bfd_is_abs_section (asect))
7222 sec_index = SHN_ABS;
7223 else if (bfd_is_com_section (asect))
7224 sec_index = SHN_COMMON;
7225 else if (bfd_is_und_section (asect))
7226 sec_index = SHN_UNDEF;
7227 else
7228 sec_index = SHN_BAD;
7230 bed = get_elf_backend_data (abfd);
7231 if (bed->elf_backend_section_from_bfd_section)
7233 int retval = sec_index;
7235 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
7236 return retval;
7239 if (sec_index == SHN_BAD)
7240 bfd_set_error (bfd_error_nonrepresentable_section);
7242 return sec_index;
7245 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
7246 on error. */
7249 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
7251 asymbol *asym_ptr = *asym_ptr_ptr;
7252 int idx;
7253 flagword flags = asym_ptr->flags;
7255 /* When gas creates relocations against local labels, it creates its
7256 own symbol for the section, but does put the symbol into the
7257 symbol chain, so udata is 0. When the linker is generating
7258 relocatable output, this section symbol may be for one of the
7259 input sections rather than the output section. */
7260 if (asym_ptr->udata.i == 0
7261 && (flags & BSF_SECTION_SYM)
7262 && asym_ptr->section)
7264 asection *sec;
7266 sec = asym_ptr->section;
7267 if (sec->owner != abfd && sec->output_section != NULL)
7268 sec = sec->output_section;
7269 if (sec->owner == abfd
7270 && sec->index < elf_num_section_syms (abfd)
7271 && elf_section_syms (abfd)[sec->index] != NULL)
7272 asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
7275 idx = asym_ptr->udata.i;
7277 if (idx == 0)
7279 /* This case can occur when using --strip-symbol on a symbol
7280 which is used in a relocation entry. */
7281 _bfd_error_handler
7282 /* xgettext:c-format */
7283 (_("%pB: symbol `%s' required but not present"),
7284 abfd, bfd_asymbol_name (asym_ptr));
7285 bfd_set_error (bfd_error_no_symbols);
7286 return -1;
7289 #if DEBUG & 4
7291 fprintf (stderr,
7292 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
7293 " flags = 0x%.8x\n",
7294 (long) asym_ptr, asym_ptr->name, idx, flags);
7295 fflush (stderr);
7297 #endif
7299 return idx;
7302 static inline bfd_vma
7303 segment_size (Elf_Internal_Phdr *segment)
7305 return (segment->p_memsz > segment->p_filesz
7306 ? segment->p_memsz : segment->p_filesz);
7310 /* Returns the end address of the segment + 1. */
7311 static inline bfd_vma
7312 segment_end (Elf_Internal_Phdr *segment, bfd_vma start)
7314 return start + segment_size (segment);
7317 static inline bfd_size_type
7318 section_size (asection *section, Elf_Internal_Phdr *segment)
7320 if ((section->flags & SEC_HAS_CONTENTS) != 0
7321 || (section->flags & SEC_THREAD_LOCAL) == 0
7322 || segment->p_type == PT_TLS)
7323 return section->size;
7324 return 0;
7327 /* Returns TRUE if the given section is contained within the given
7328 segment. LMA addresses are compared against PADDR when
7329 USE_VADDR is false, VMA against VADDR when true. */
7330 static bool
7331 is_contained_by (asection *section, Elf_Internal_Phdr *segment,
7332 bfd_vma paddr, bfd_vma vaddr, unsigned int opb,
7333 bool use_vaddr)
7335 bfd_vma seg_addr = !use_vaddr ? paddr : vaddr;
7336 bfd_vma addr = !use_vaddr ? section->lma : section->vma;
7337 bfd_vma octet;
7338 if (_bfd_mul_overflow (addr, opb, &octet))
7339 return false;
7340 /* The third and fourth lines below are testing that the section end
7341 address is within the segment. It's written this way to avoid
7342 overflow. Add seg_addr + section_size to both sides of the
7343 inequality to make it obvious. */
7344 return (octet >= seg_addr
7345 && segment_size (segment) >= section_size (section, segment)
7346 && (octet - seg_addr
7347 <= segment_size (segment) - section_size (section, segment)));
7350 /* Handle PT_NOTE segment. */
7351 static bool
7352 is_note (asection *s, Elf_Internal_Phdr *p)
7354 return (p->p_type == PT_NOTE
7355 && elf_section_type (s) == SHT_NOTE
7356 && (ufile_ptr) s->filepos >= p->p_offset
7357 && p->p_filesz >= s->size
7358 && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
7361 /* Rewrite program header information. */
7363 static bool
7364 rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
7366 Elf_Internal_Ehdr *iehdr;
7367 struct elf_segment_map *map;
7368 struct elf_segment_map *map_first;
7369 struct elf_segment_map **pointer_to_map;
7370 Elf_Internal_Phdr *segment;
7371 asection *section;
7372 unsigned int i;
7373 unsigned int num_segments;
7374 bool phdr_included = false;
7375 bool p_paddr_valid;
7376 struct elf_segment_map *phdr_adjust_seg = NULL;
7377 unsigned int phdr_adjust_num = 0;
7378 const struct elf_backend_data *bed;
7379 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
7381 bed = get_elf_backend_data (ibfd);
7382 iehdr = elf_elfheader (ibfd);
7384 map_first = NULL;
7385 pointer_to_map = &map_first;
7387 num_segments = elf_elfheader (ibfd)->e_phnum;
7389 /* The complicated case when p_vaddr is 0 is to handle the Solaris
7390 linker, which generates a PT_INTERP section with p_vaddr and
7391 p_memsz set to 0. */
7392 #define IS_SOLARIS_PT_INTERP(p, s) \
7393 (p->p_vaddr == 0 \
7394 && p->p_paddr == 0 \
7395 && p->p_memsz == 0 \
7396 && p->p_filesz > 0 \
7397 && (s->flags & SEC_HAS_CONTENTS) != 0 \
7398 && s->size > 0 \
7399 && (bfd_vma) s->filepos >= p->p_offset \
7400 && ((bfd_vma) s->filepos + s->size \
7401 <= p->p_offset + p->p_filesz))
7403 /* Decide if the given section should be included in the given segment.
7404 A section will be included if:
7405 1. It is within the address space of the segment -- we use the LMA
7406 if that is set for the segment and the VMA otherwise,
7407 2. It is an allocated section or a NOTE section in a PT_NOTE
7408 segment.
7409 3. There is an output section associated with it,
7410 4. The section has not already been allocated to a previous segment.
7411 5. PT_GNU_STACK segments do not include any sections.
7412 6. PT_TLS segment includes only SHF_TLS sections.
7413 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
7414 8. PT_DYNAMIC should not contain empty sections at the beginning
7415 (with the possible exception of .dynamic). */
7416 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, opb, paddr_valid) \
7417 (((is_contained_by (section, segment, segment->p_paddr, \
7418 segment->p_vaddr, opb, !paddr_valid) \
7419 && (section->flags & SEC_ALLOC) != 0) \
7420 || is_note (section, segment)) \
7421 && segment->p_type != PT_GNU_STACK \
7422 && (segment->p_type != PT_TLS \
7423 || (section->flags & SEC_THREAD_LOCAL)) \
7424 && (segment->p_type == PT_LOAD \
7425 || segment->p_type == PT_TLS \
7426 || (section->flags & SEC_THREAD_LOCAL) == 0) \
7427 && (segment->p_type != PT_DYNAMIC \
7428 || section_size (section, segment) > 0 \
7429 || (segment->p_paddr \
7430 ? segment->p_paddr != section->lma * (opb) \
7431 : segment->p_vaddr != section->vma * (opb)) \
7432 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
7433 && (segment->p_type != PT_LOAD || !section->segment_mark))
7435 /* If the output section of a section in the input segment is NULL,
7436 it is removed from the corresponding output segment. */
7437 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, opb, paddr_valid) \
7438 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, paddr_valid) \
7439 && section->output_section != NULL)
7441 /* Returns TRUE iff seg1 starts after the end of seg2. */
7442 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
7443 (seg1->field >= segment_end (seg2, seg2->field))
7445 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7446 their VMA address ranges and their LMA address ranges overlap.
7447 It is possible to have overlapping VMA ranges without overlapping LMA
7448 ranges. RedBoot images for example can have both .data and .bss mapped
7449 to the same VMA range, but with the .data section mapped to a different
7450 LMA. */
7451 #define SEGMENT_OVERLAPS(seg1, seg2) \
7452 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
7453 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
7454 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
7455 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7457 /* Initialise the segment mark field, and discard stupid alignment. */
7458 for (section = ibfd->sections; section != NULL; section = section->next)
7460 asection *o = section->output_section;
7461 if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
7462 o->alignment_power = 0;
7463 section->segment_mark = false;
7466 /* The Solaris linker creates program headers in which all the
7467 p_paddr fields are zero. When we try to objcopy or strip such a
7468 file, we get confused. Check for this case, and if we find it
7469 don't set the p_paddr_valid fields. */
7470 p_paddr_valid = false;
7471 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7472 i < num_segments;
7473 i++, segment++)
7474 if (segment->p_paddr != 0)
7476 p_paddr_valid = true;
7477 break;
7480 /* Scan through the segments specified in the program header
7481 of the input BFD. For this first scan we look for overlaps
7482 in the loadable segments. These can be created by weird
7483 parameters to objcopy. Also, fix some solaris weirdness. */
7484 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7485 i < num_segments;
7486 i++, segment++)
7488 unsigned int j;
7489 Elf_Internal_Phdr *segment2;
7491 if (segment->p_type == PT_INTERP)
7492 for (section = ibfd->sections; section; section = section->next)
7493 if (IS_SOLARIS_PT_INTERP (segment, section))
7495 /* Mininal change so that the normal section to segment
7496 assignment code will work. */
7497 segment->p_vaddr = section->vma * opb;
7498 break;
7501 if (segment->p_type != PT_LOAD)
7503 /* Remove PT_GNU_RELRO segment. */
7504 if (segment->p_type == PT_GNU_RELRO)
7505 segment->p_type = PT_NULL;
7506 continue;
7509 /* Determine if this segment overlaps any previous segments. */
7510 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
7512 bfd_signed_vma extra_length;
7514 if (segment2->p_type != PT_LOAD
7515 || !SEGMENT_OVERLAPS (segment, segment2))
7516 continue;
7518 /* Merge the two segments together. */
7519 if (segment2->p_vaddr < segment->p_vaddr)
7521 /* Extend SEGMENT2 to include SEGMENT and then delete
7522 SEGMENT. */
7523 extra_length = (segment_end (segment, segment->p_vaddr)
7524 - segment_end (segment2, segment2->p_vaddr));
7526 if (extra_length > 0)
7528 segment2->p_memsz += extra_length;
7529 segment2->p_filesz += extra_length;
7532 segment->p_type = PT_NULL;
7534 /* Since we have deleted P we must restart the outer loop. */
7535 i = 0;
7536 segment = elf_tdata (ibfd)->phdr;
7537 break;
7539 else
7541 /* Extend SEGMENT to include SEGMENT2 and then delete
7542 SEGMENT2. */
7543 extra_length = (segment_end (segment2, segment2->p_vaddr)
7544 - segment_end (segment, segment->p_vaddr));
7546 if (extra_length > 0)
7548 segment->p_memsz += extra_length;
7549 segment->p_filesz += extra_length;
7552 segment2->p_type = PT_NULL;
7557 /* The second scan attempts to assign sections to segments. */
7558 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7559 i < num_segments;
7560 i++, segment++)
7562 unsigned int section_count;
7563 asection **sections;
7564 asection *output_section;
7565 unsigned int isec;
7566 asection *matching_lma;
7567 asection *suggested_lma;
7568 unsigned int j;
7569 size_t amt;
7570 asection *first_section;
7572 if (segment->p_type == PT_NULL)
7573 continue;
7575 first_section = NULL;
7576 /* Compute how many sections might be placed into this segment. */
7577 for (section = ibfd->sections, section_count = 0;
7578 section != NULL;
7579 section = section->next)
7581 /* Find the first section in the input segment, which may be
7582 removed from the corresponding output segment. */
7583 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, p_paddr_valid))
7585 if (first_section == NULL)
7586 first_section = section;
7587 if (section->output_section != NULL)
7588 ++section_count;
7592 /* Allocate a segment map big enough to contain
7593 all of the sections we have selected. */
7594 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7595 amt += section_count * sizeof (asection *);
7596 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7597 if (map == NULL)
7598 return false;
7600 /* Initialise the fields of the segment map. Default to
7601 using the physical address of the segment in the input BFD. */
7602 map->next = NULL;
7603 map->p_type = segment->p_type;
7604 map->p_flags = segment->p_flags;
7605 map->p_flags_valid = 1;
7607 if (map->p_type == PT_LOAD
7608 && (ibfd->flags & D_PAGED) != 0
7609 && maxpagesize > 1
7610 && segment->p_align > 1)
7612 map->p_align = segment->p_align;
7613 if (segment->p_align > maxpagesize)
7614 map->p_align = maxpagesize;
7615 map->p_align_valid = 1;
7618 /* If the first section in the input segment is removed, there is
7619 no need to preserve segment physical address in the corresponding
7620 output segment. */
7621 if (!first_section || first_section->output_section != NULL)
7623 map->p_paddr = segment->p_paddr;
7624 map->p_paddr_valid = p_paddr_valid;
7627 /* Determine if this segment contains the ELF file header
7628 and if it contains the program headers themselves. */
7629 map->includes_filehdr = (segment->p_offset == 0
7630 && segment->p_filesz >= iehdr->e_ehsize);
7631 map->includes_phdrs = 0;
7633 if (!phdr_included || segment->p_type != PT_LOAD)
7635 map->includes_phdrs =
7636 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7637 && (segment->p_offset + segment->p_filesz
7638 >= ((bfd_vma) iehdr->e_phoff
7639 + iehdr->e_phnum * iehdr->e_phentsize)));
7641 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7642 phdr_included = true;
7645 if (section_count == 0)
7647 /* Special segments, such as the PT_PHDR segment, may contain
7648 no sections, but ordinary, loadable segments should contain
7649 something. They are allowed by the ELF spec however, so only
7650 a warning is produced.
7651 Don't warn if an empty PT_LOAD contains the program headers.
7652 There is however the valid use case of embedded systems which
7653 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7654 flash memory with zeros. No warning is shown for that case. */
7655 if (segment->p_type == PT_LOAD
7656 && !map->includes_phdrs
7657 && (segment->p_filesz > 0 || segment->p_memsz == 0))
7658 /* xgettext:c-format */
7659 _bfd_error_handler
7660 (_("%pB: warning: empty loadable segment detected"
7661 " at vaddr=%#" PRIx64 ", is this intentional?"),
7662 ibfd, (uint64_t) segment->p_vaddr);
7664 map->p_vaddr_offset = segment->p_vaddr / opb;
7665 map->count = 0;
7666 *pointer_to_map = map;
7667 pointer_to_map = &map->next;
7669 continue;
7672 /* Now scan the sections in the input BFD again and attempt
7673 to add their corresponding output sections to the segment map.
7674 The problem here is how to handle an output section which has
7675 been moved (ie had its LMA changed). There are four possibilities:
7677 1. None of the sections have been moved.
7678 In this case we can continue to use the segment LMA from the
7679 input BFD.
7681 2. All of the sections have been moved by the same amount.
7682 In this case we can change the segment's LMA to match the LMA
7683 of the first section.
7685 3. Some of the sections have been moved, others have not.
7686 In this case those sections which have not been moved can be
7687 placed in the current segment which will have to have its size,
7688 and possibly its LMA changed, and a new segment or segments will
7689 have to be created to contain the other sections.
7691 4. The sections have been moved, but not by the same amount.
7692 In this case we can change the segment's LMA to match the LMA
7693 of the first section and we will have to create a new segment
7694 or segments to contain the other sections.
7696 In order to save time, we allocate an array to hold the section
7697 pointers that we are interested in. As these sections get assigned
7698 to a segment, they are removed from this array. */
7700 amt = section_count * sizeof (asection *);
7701 sections = (asection **) bfd_malloc (amt);
7702 if (sections == NULL)
7703 return false;
7705 /* Step One: Scan for segment vs section LMA conflicts.
7706 Also add the sections to the section array allocated above.
7707 Also add the sections to the current segment. In the common
7708 case, where the sections have not been moved, this means that
7709 we have completely filled the segment, and there is nothing
7710 more to do. */
7711 isec = 0;
7712 matching_lma = NULL;
7713 suggested_lma = NULL;
7715 for (section = first_section, j = 0;
7716 section != NULL;
7717 section = section->next)
7719 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, opb, p_paddr_valid))
7721 output_section = section->output_section;
7723 sections[j++] = section;
7725 /* The Solaris native linker always sets p_paddr to 0.
7726 We try to catch that case here, and set it to the
7727 correct value. Note - some backends require that
7728 p_paddr be left as zero. */
7729 if (!p_paddr_valid
7730 && segment->p_vaddr != 0
7731 && !bed->want_p_paddr_set_to_zero
7732 && isec == 0
7733 && output_section->lma != 0
7734 && (align_power (segment->p_vaddr
7735 + (map->includes_filehdr
7736 ? iehdr->e_ehsize : 0)
7737 + (map->includes_phdrs
7738 ? iehdr->e_phnum * iehdr->e_phentsize
7739 : 0),
7740 output_section->alignment_power * opb)
7741 == (output_section->vma * opb)))
7742 map->p_paddr = segment->p_vaddr;
7744 /* Match up the physical address of the segment with the
7745 LMA address of the output section. */
7746 if (is_contained_by (output_section, segment, map->p_paddr,
7747 0, opb, false)
7748 || is_note (section, segment))
7750 if (matching_lma == NULL
7751 || output_section->lma < matching_lma->lma)
7752 matching_lma = output_section;
7754 /* We assume that if the section fits within the segment
7755 then it does not overlap any other section within that
7756 segment. */
7757 map->sections[isec++] = output_section;
7759 else if (suggested_lma == NULL)
7760 suggested_lma = output_section;
7762 if (j == section_count)
7763 break;
7767 BFD_ASSERT (j == section_count);
7769 /* Step Two: Adjust the physical address of the current segment,
7770 if necessary. */
7771 if (isec == section_count)
7773 /* All of the sections fitted within the segment as currently
7774 specified. This is the default case. Add the segment to
7775 the list of built segments and carry on to process the next
7776 program header in the input BFD. */
7777 map->count = section_count;
7778 *pointer_to_map = map;
7779 pointer_to_map = &map->next;
7781 if (p_paddr_valid
7782 && !bed->want_p_paddr_set_to_zero)
7784 bfd_vma hdr_size = 0;
7785 if (map->includes_filehdr)
7786 hdr_size = iehdr->e_ehsize;
7787 if (map->includes_phdrs)
7788 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7790 /* Account for padding before the first section in the
7791 segment. */
7792 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7793 - matching_lma->lma);
7796 free (sections);
7797 continue;
7799 else
7801 /* Change the current segment's physical address to match
7802 the LMA of the first section that fitted, or if no
7803 section fitted, the first section. */
7804 if (matching_lma == NULL)
7805 matching_lma = suggested_lma;
7807 map->p_paddr = matching_lma->lma * opb;
7809 /* Offset the segment physical address from the lma
7810 to allow for space taken up by elf headers. */
7811 if (map->includes_phdrs)
7813 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7815 /* iehdr->e_phnum is just an estimate of the number
7816 of program headers that we will need. Make a note
7817 here of the number we used and the segment we chose
7818 to hold these headers, so that we can adjust the
7819 offset when we know the correct value. */
7820 phdr_adjust_num = iehdr->e_phnum;
7821 phdr_adjust_seg = map;
7824 if (map->includes_filehdr)
7826 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7827 map->p_paddr -= iehdr->e_ehsize;
7828 /* We've subtracted off the size of headers from the
7829 first section lma, but there may have been some
7830 alignment padding before that section too. Try to
7831 account for that by adjusting the segment lma down to
7832 the same alignment. */
7833 if (segment->p_align != 0 && segment->p_align < align)
7834 align = segment->p_align;
7835 map->p_paddr &= -(align * opb);
7839 /* Step Three: Loop over the sections again, this time assigning
7840 those that fit to the current segment and removing them from the
7841 sections array; but making sure not to leave large gaps. Once all
7842 possible sections have been assigned to the current segment it is
7843 added to the list of built segments and if sections still remain
7844 to be assigned, a new segment is constructed before repeating
7845 the loop. */
7846 isec = 0;
7849 map->count = 0;
7850 suggested_lma = NULL;
7852 /* Fill the current segment with sections that fit. */
7853 for (j = 0; j < section_count; j++)
7855 section = sections[j];
7857 if (section == NULL)
7858 continue;
7860 output_section = section->output_section;
7862 BFD_ASSERT (output_section != NULL);
7864 if (is_contained_by (output_section, segment, map->p_paddr,
7865 0, opb, false)
7866 || is_note (section, segment))
7868 if (map->count == 0)
7870 /* If the first section in a segment does not start at
7871 the beginning of the segment, then something is
7872 wrong. */
7873 if (align_power (map->p_paddr
7874 + (map->includes_filehdr
7875 ? iehdr->e_ehsize : 0)
7876 + (map->includes_phdrs
7877 ? iehdr->e_phnum * iehdr->e_phentsize
7878 : 0),
7879 output_section->alignment_power * opb)
7880 != output_section->lma * opb)
7881 goto sorry;
7883 else
7885 asection *prev_sec;
7887 prev_sec = map->sections[map->count - 1];
7889 /* If the gap between the end of the previous section
7890 and the start of this section is more than
7891 maxpagesize then we need to start a new segment. */
7892 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
7893 maxpagesize)
7894 < BFD_ALIGN (output_section->lma, maxpagesize))
7895 || (prev_sec->lma + prev_sec->size
7896 > output_section->lma))
7898 if (suggested_lma == NULL)
7899 suggested_lma = output_section;
7901 continue;
7905 map->sections[map->count++] = output_section;
7906 ++isec;
7907 sections[j] = NULL;
7908 if (segment->p_type == PT_LOAD)
7909 section->segment_mark = true;
7911 else if (suggested_lma == NULL)
7912 suggested_lma = output_section;
7915 /* PR 23932. A corrupt input file may contain sections that cannot
7916 be assigned to any segment - because for example they have a
7917 negative size - or segments that do not contain any sections.
7918 But there are also valid reasons why a segment can be empty.
7919 So allow a count of zero. */
7921 /* Add the current segment to the list of built segments. */
7922 *pointer_to_map = map;
7923 pointer_to_map = &map->next;
7925 if (isec < section_count)
7927 /* We still have not allocated all of the sections to
7928 segments. Create a new segment here, initialise it
7929 and carry on looping. */
7930 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7931 amt += section_count * sizeof (asection *);
7932 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7933 if (map == NULL)
7935 free (sections);
7936 return false;
7939 /* Initialise the fields of the segment map. Set the physical
7940 physical address to the LMA of the first section that has
7941 not yet been assigned. */
7942 map->next = NULL;
7943 map->p_type = segment->p_type;
7944 map->p_flags = segment->p_flags;
7945 map->p_flags_valid = 1;
7946 map->p_paddr = suggested_lma->lma * opb;
7947 map->p_paddr_valid = p_paddr_valid;
7948 map->includes_filehdr = 0;
7949 map->includes_phdrs = 0;
7952 continue;
7953 sorry:
7954 bfd_set_error (bfd_error_sorry);
7955 free (sections);
7956 return false;
7958 while (isec < section_count);
7960 free (sections);
7963 elf_seg_map (obfd) = map_first;
7965 /* If we had to estimate the number of program headers that were
7966 going to be needed, then check our estimate now and adjust
7967 the offset if necessary. */
7968 if (phdr_adjust_seg != NULL)
7970 unsigned int count;
7972 for (count = 0, map = map_first; map != NULL; map = map->next)
7973 count++;
7975 if (count > phdr_adjust_num)
7976 phdr_adjust_seg->p_paddr
7977 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
7979 for (map = map_first; map != NULL; map = map->next)
7980 if (map->p_type == PT_PHDR)
7982 bfd_vma adjust
7983 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7984 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7985 break;
7989 #undef IS_SOLARIS_PT_INTERP
7990 #undef IS_SECTION_IN_INPUT_SEGMENT
7991 #undef INCLUDE_SECTION_IN_SEGMENT
7992 #undef SEGMENT_AFTER_SEGMENT
7993 #undef SEGMENT_OVERLAPS
7994 return true;
7997 /* Return true if p_align in the ELF program header in ABFD is valid. */
7999 static bool
8000 elf_is_p_align_valid (bfd *abfd)
8002 unsigned int i;
8003 Elf_Internal_Phdr *segment;
8004 unsigned int num_segments;
8005 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8006 bfd_size_type maxpagesize = bed->maxpagesize;
8007 bfd_size_type p_align = bed->p_align;
8009 /* Return true if the default p_align value isn't set or the maximum
8010 page size is the same as the minimum page size. */
8011 if (p_align == 0 || maxpagesize == bed->minpagesize)
8012 return true;
8014 /* When the default p_align value is set, p_align may be set to the
8015 default p_align value while segments are aligned to the maximum
8016 page size. In this case, the input p_align will be ignored and
8017 the maximum page size will be used to align the output segments. */
8018 segment = elf_tdata (abfd)->phdr;
8019 num_segments = elf_elfheader (abfd)->e_phnum;
8020 for (i = 0; i < num_segments; i++, segment++)
8021 if (segment->p_type == PT_LOAD
8022 && (segment->p_align != p_align
8023 || vma_page_aligned_bias (segment->p_vaddr,
8024 segment->p_offset,
8025 maxpagesize) != 0))
8026 return true;
8028 return false;
8031 /* Copy ELF program header information. */
8033 static bool
8034 copy_elf_program_header (bfd *ibfd, bfd *obfd)
8036 Elf_Internal_Ehdr *iehdr;
8037 struct elf_segment_map *map;
8038 struct elf_segment_map *map_first;
8039 struct elf_segment_map **pointer_to_map;
8040 Elf_Internal_Phdr *segment;
8041 unsigned int i;
8042 unsigned int num_segments;
8043 bool phdr_included = false;
8044 bool p_paddr_valid;
8045 bool p_palign_valid;
8046 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
8048 iehdr = elf_elfheader (ibfd);
8050 map_first = NULL;
8051 pointer_to_map = &map_first;
8053 /* If all the segment p_paddr fields are zero, don't set
8054 map->p_paddr_valid. */
8055 p_paddr_valid = false;
8056 num_segments = elf_elfheader (ibfd)->e_phnum;
8057 for (i = 0, segment = elf_tdata (ibfd)->phdr;
8058 i < num_segments;
8059 i++, segment++)
8060 if (segment->p_paddr != 0)
8062 p_paddr_valid = true;
8063 break;
8066 p_palign_valid = elf_is_p_align_valid (ibfd);
8068 for (i = 0, segment = elf_tdata (ibfd)->phdr;
8069 i < num_segments;
8070 i++, segment++)
8072 asection *section;
8073 unsigned int section_count;
8074 size_t amt;
8075 Elf_Internal_Shdr *this_hdr;
8076 asection *first_section = NULL;
8077 asection *lowest_section;
8079 /* Compute how many sections are in this segment. */
8080 for (section = ibfd->sections, section_count = 0;
8081 section != NULL;
8082 section = section->next)
8084 this_hdr = &(elf_section_data(section)->this_hdr);
8085 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8087 if (first_section == NULL)
8088 first_section = section;
8089 section_count++;
8093 /* Allocate a segment map big enough to contain
8094 all of the sections we have selected. */
8095 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8096 amt += section_count * sizeof (asection *);
8097 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
8098 if (map == NULL)
8099 return false;
8101 /* Initialize the fields of the output segment map with the
8102 input segment. */
8103 map->next = NULL;
8104 map->p_type = segment->p_type;
8105 map->p_flags = segment->p_flags;
8106 map->p_flags_valid = 1;
8107 map->p_paddr = segment->p_paddr;
8108 map->p_paddr_valid = p_paddr_valid;
8109 map->p_align = segment->p_align;
8110 /* Keep p_align of PT_GNU_STACK for stack alignment. */
8111 map->p_align_valid = (map->p_type == PT_GNU_STACK
8112 || p_palign_valid);
8113 map->p_vaddr_offset = 0;
8115 if (map->p_type == PT_GNU_RELRO
8116 || map->p_type == PT_GNU_STACK)
8118 /* The PT_GNU_RELRO segment may contain the first a few
8119 bytes in the .got.plt section even if the whole .got.plt
8120 section isn't in the PT_GNU_RELRO segment. We won't
8121 change the size of the PT_GNU_RELRO segment.
8122 Similarly, PT_GNU_STACK size is significant on uclinux
8123 systems. */
8124 map->p_size = segment->p_memsz;
8125 map->p_size_valid = 1;
8128 /* Determine if this segment contains the ELF file header
8129 and if it contains the program headers themselves. */
8130 map->includes_filehdr = (segment->p_offset == 0
8131 && segment->p_filesz >= iehdr->e_ehsize);
8133 map->includes_phdrs = 0;
8134 if (! phdr_included || segment->p_type != PT_LOAD)
8136 map->includes_phdrs =
8137 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
8138 && (segment->p_offset + segment->p_filesz
8139 >= ((bfd_vma) iehdr->e_phoff
8140 + iehdr->e_phnum * iehdr->e_phentsize)));
8142 if (segment->p_type == PT_LOAD && map->includes_phdrs)
8143 phdr_included = true;
8146 lowest_section = NULL;
8147 if (section_count != 0)
8149 unsigned int isec = 0;
8151 for (section = first_section;
8152 section != NULL;
8153 section = section->next)
8155 this_hdr = &(elf_section_data(section)->this_hdr);
8156 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8158 map->sections[isec++] = section->output_section;
8159 if ((section->flags & SEC_ALLOC) != 0)
8161 bfd_vma seg_off;
8163 if (lowest_section == NULL
8164 || section->lma < lowest_section->lma)
8165 lowest_section = section;
8167 /* Section lmas are set up from PT_LOAD header
8168 p_paddr in _bfd_elf_make_section_from_shdr.
8169 If this header has a p_paddr that disagrees
8170 with the section lma, flag the p_paddr as
8171 invalid. */
8172 if ((section->flags & SEC_LOAD) != 0)
8173 seg_off = this_hdr->sh_offset - segment->p_offset;
8174 else
8175 seg_off = this_hdr->sh_addr - segment->p_vaddr;
8176 if (section->lma * opb - segment->p_paddr != seg_off)
8177 map->p_paddr_valid = false;
8179 if (isec == section_count)
8180 break;
8185 if (section_count == 0)
8186 map->p_vaddr_offset = segment->p_vaddr / opb;
8187 else if (map->p_paddr_valid)
8189 /* Account for padding before the first section in the segment. */
8190 bfd_vma hdr_size = 0;
8191 if (map->includes_filehdr)
8192 hdr_size = iehdr->e_ehsize;
8193 if (map->includes_phdrs)
8194 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
8196 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
8197 - (lowest_section ? lowest_section->lma : 0));
8200 map->count = section_count;
8201 *pointer_to_map = map;
8202 pointer_to_map = &map->next;
8205 elf_seg_map (obfd) = map_first;
8206 return true;
8209 /* Copy private BFD data. This copies or rewrites ELF program header
8210 information. */
8212 static bool
8213 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
8215 bfd_vma maxpagesize;
8217 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8218 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8219 return true;
8221 if (elf_tdata (ibfd)->phdr == NULL)
8222 return true;
8224 if (ibfd->xvec == obfd->xvec)
8226 /* Check to see if any sections in the input BFD
8227 covered by ELF program header have changed. */
8228 Elf_Internal_Phdr *segment;
8229 asection * section;
8230 asection * osec;
8231 asection * prev;
8232 unsigned int i, num_segments;
8233 Elf_Internal_Shdr *this_hdr;
8234 const struct elf_backend_data *bed;
8236 bed = get_elf_backend_data (ibfd);
8238 /* Regenerate the segment map if p_paddr is set to 0. */
8239 if (bed->want_p_paddr_set_to_zero)
8240 goto rewrite;
8242 /* Initialize the segment mark field. */
8243 for (section = obfd->sections; section != NULL;
8244 section = section->next)
8245 section->segment_mark = false;
8247 num_segments = elf_elfheader (ibfd)->e_phnum;
8248 for (i = 0, segment = elf_tdata (ibfd)->phdr;
8249 i < num_segments;
8250 i++, segment++)
8252 /* PR binutils/3535. The Solaris linker always sets the p_paddr
8253 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
8254 which severly confuses things, so always regenerate the segment
8255 map in this case. */
8256 if (segment->p_paddr == 0
8257 && segment->p_memsz == 0
8258 && (segment->p_type == PT_INTERP
8259 || segment->p_type == PT_DYNAMIC))
8260 goto rewrite;
8262 for (section = ibfd->sections, prev = NULL;
8263 section != NULL; section = section->next)
8265 /* We mark the output section so that we know it comes
8266 from the input BFD. */
8267 osec = section->output_section;
8268 if (osec)
8269 osec->segment_mark = true;
8271 /* Check if this section is covered by the segment. */
8272 this_hdr = &(elf_section_data(section)->this_hdr);
8273 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8275 /* FIXME: Check if its output section is changed or
8276 removed. What else do we need to check? */
8277 if (osec == NULL
8278 || section->flags != osec->flags
8279 || section->lma != osec->lma
8280 || section->vma != osec->vma
8281 || section->size != osec->size
8282 || section->rawsize != osec->rawsize
8283 || section->alignment_power != osec->alignment_power)
8284 goto rewrite;
8286 /* PR 31450: If this is an allocated section then make sure
8287 that this section's vma to lma relationship is the same
8288 as previous (allocated) section's. */
8289 if (prev != NULL
8290 && section->flags & SEC_ALLOC
8291 && section->lma - section->vma != prev->lma - prev->vma)
8292 goto rewrite;
8294 if (section->flags & SEC_ALLOC)
8295 prev = section;
8300 /* Check to see if any output section do not come from the
8301 input BFD. */
8302 for (section = obfd->sections; section != NULL;
8303 section = section->next)
8305 if (!section->segment_mark)
8306 goto rewrite;
8307 else
8308 section->segment_mark = false;
8311 return copy_elf_program_header (ibfd, obfd);
8314 rewrite:
8315 maxpagesize = 0;
8316 if (ibfd->xvec == obfd->xvec)
8318 /* When rewriting program header, set the output maxpagesize to
8319 the maximum alignment of input PT_LOAD segments. */
8320 Elf_Internal_Phdr *segment;
8321 unsigned int i;
8322 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
8324 for (i = 0, segment = elf_tdata (ibfd)->phdr;
8325 i < num_segments;
8326 i++, segment++)
8327 if (segment->p_type == PT_LOAD
8328 && maxpagesize < segment->p_align)
8330 /* PR 17512: file: f17299af. */
8331 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
8332 /* xgettext:c-format */
8333 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
8334 PRIx64 " is too large"),
8335 ibfd, (uint64_t) segment->p_align);
8336 else
8337 maxpagesize = segment->p_align;
8340 if (maxpagesize == 0)
8341 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
8343 return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
8346 /* Initialize private output section information from input section. */
8348 bool
8349 _bfd_elf_init_private_section_data (bfd *ibfd,
8350 asection *isec,
8351 bfd *obfd,
8352 asection *osec,
8353 struct bfd_link_info *link_info)
8356 Elf_Internal_Shdr *ihdr, *ohdr;
8357 bool final_link = (link_info != NULL
8358 && !bfd_link_relocatable (link_info));
8360 if (ibfd->xvec->flavour != bfd_target_elf_flavour
8361 || obfd->xvec->flavour != bfd_target_elf_flavour)
8362 return true;
8364 BFD_ASSERT (elf_section_data (osec) != NULL);
8366 /* If this is a known ABI section, ELF section type and flags may
8367 have been set up when OSEC was created. For normal sections we
8368 allow the user to override the type and flags other than
8369 SHF_MASKOS and SHF_MASKPROC. */
8370 if (elf_section_type (osec) == SHT_PROGBITS
8371 || elf_section_type (osec) == SHT_NOTE
8372 || elf_section_type (osec) == SHT_NOBITS)
8373 elf_section_type (osec) = SHT_NULL;
8374 /* For objcopy and relocatable link, copy the ELF section type from
8375 the input file if the BFD section flags are the same. (If they
8376 are different the user may be doing something like
8377 "objcopy --set-section-flags .text=alloc,data".) For a final
8378 link allow some flags that the linker clears to differ. */
8379 if (elf_section_type (osec) == SHT_NULL
8380 && (osec->flags == isec->flags
8381 || (final_link
8382 && ((osec->flags ^ isec->flags)
8383 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
8384 elf_section_type (osec) = elf_section_type (isec);
8386 /* FIXME: Is this correct for all OS/PROC specific flags? */
8387 elf_section_flags (osec) = (elf_section_flags (isec)
8388 & (SHF_MASKOS | SHF_MASKPROC));
8390 /* Copy sh_info from input for mbind section. */
8391 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
8392 && elf_section_flags (isec) & SHF_GNU_MBIND)
8393 elf_section_data (osec)->this_hdr.sh_info
8394 = elf_section_data (isec)->this_hdr.sh_info;
8396 /* Set things up for objcopy and relocatable link. The output
8397 SHT_GROUP section will have its elf_next_in_group pointing back
8398 to the input group members. Ignore linker created group section.
8399 See elfNN_ia64_object_p in elfxx-ia64.c. */
8400 if ((link_info == NULL
8401 || !link_info->resolve_section_groups)
8402 && (elf_sec_group (isec) == NULL
8403 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
8405 if (elf_section_flags (isec) & SHF_GROUP)
8406 elf_section_flags (osec) |= SHF_GROUP;
8407 elf_next_in_group (osec) = elf_next_in_group (isec);
8408 elf_section_data (osec)->group = elf_section_data (isec)->group;
8411 /* If not decompress, preserve SHF_COMPRESSED. */
8412 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
8413 elf_section_flags (osec) |= (elf_section_flags (isec)
8414 & SHF_COMPRESSED);
8416 ihdr = &elf_section_data (isec)->this_hdr;
8418 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
8419 don't use the output section of the linked-to section since it
8420 may be NULL at this point. */
8421 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
8423 ohdr = &elf_section_data (osec)->this_hdr;
8424 ohdr->sh_flags |= SHF_LINK_ORDER;
8425 elf_linked_to_section (osec) = elf_linked_to_section (isec);
8428 osec->use_rela_p = isec->use_rela_p;
8430 return true;
8433 /* Copy private section information. This copies over the entsize
8434 field, and sometimes the info field. */
8436 bool
8437 _bfd_elf_copy_private_section_data (bfd *ibfd,
8438 asection *isec,
8439 bfd *obfd,
8440 asection *osec)
8442 Elf_Internal_Shdr *ihdr, *ohdr;
8444 if (ibfd->xvec->flavour != bfd_target_elf_flavour
8445 || obfd->xvec->flavour != bfd_target_elf_flavour)
8446 return true;
8448 ihdr = &elf_section_data (isec)->this_hdr;
8449 ohdr = &elf_section_data (osec)->this_hdr;
8451 ohdr->sh_entsize = ihdr->sh_entsize;
8453 if (ihdr->sh_type == SHT_SYMTAB
8454 || ihdr->sh_type == SHT_DYNSYM
8455 || ihdr->sh_type == SHT_GNU_verneed
8456 || ihdr->sh_type == SHT_GNU_verdef)
8457 ohdr->sh_info = ihdr->sh_info;
8459 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
8460 NULL);
8463 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8464 necessary if we are removing either the SHT_GROUP section or any of
8465 the group member sections. DISCARDED is the value that a section's
8466 output_section has if the section will be discarded, NULL when this
8467 function is called from objcopy, bfd_abs_section_ptr when called
8468 from the linker. */
8470 bool
8471 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
8473 asection *isec;
8475 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
8476 if (elf_section_type (isec) == SHT_GROUP)
8478 asection *first = elf_next_in_group (isec);
8479 asection *s = first;
8480 bfd_size_type removed = 0;
8482 while (s != NULL)
8484 /* If this member section is being output but the
8485 SHT_GROUP section is not, then clear the group info
8486 set up by _bfd_elf_copy_private_section_data. */
8487 if (s->output_section != discarded
8488 && isec->output_section == discarded)
8490 elf_section_flags (s->output_section) &= ~SHF_GROUP;
8491 elf_group_name (s->output_section) = NULL;
8493 else
8495 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
8496 if (s->output_section == discarded
8497 && isec->output_section != discarded)
8499 /* Conversely, if the member section is not being
8500 output but the SHT_GROUP section is, then adjust
8501 its size. */
8502 removed += 4;
8503 if (elf_sec->rel.hdr != NULL
8504 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
8505 removed += 4;
8506 if (elf_sec->rela.hdr != NULL
8507 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
8508 removed += 4;
8510 else
8512 /* Also adjust for zero-sized relocation member
8513 section. */
8514 if (elf_sec->rel.hdr != NULL
8515 && elf_sec->rel.hdr->sh_size == 0)
8516 removed += 4;
8517 if (elf_sec->rela.hdr != NULL
8518 && elf_sec->rela.hdr->sh_size == 0)
8519 removed += 4;
8522 s = elf_next_in_group (s);
8523 if (s == first)
8524 break;
8526 if (removed != 0)
8528 if (discarded != NULL)
8530 /* If we've been called for ld -r, then we need to
8531 adjust the input section size. */
8532 if (isec->rawsize == 0)
8533 isec->rawsize = isec->size;
8534 isec->size = isec->rawsize - removed;
8535 if (isec->size <= 4)
8537 isec->size = 0;
8538 isec->flags |= SEC_EXCLUDE;
8541 else if (isec->output_section != NULL)
8543 /* Adjust the output section size when called from
8544 objcopy. */
8545 isec->output_section->size -= removed;
8546 if (isec->output_section->size <= 4)
8548 isec->output_section->size = 0;
8549 isec->output_section->flags |= SEC_EXCLUDE;
8555 return true;
8558 /* Copy private header information. */
8560 bool
8561 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8563 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8564 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8565 return true;
8567 /* Copy over private BFD data if it has not already been copied.
8568 This must be done here, rather than in the copy_private_bfd_data
8569 entry point, because the latter is called after the section
8570 contents have been set, which means that the program headers have
8571 already been worked out. */
8572 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
8574 if (! copy_private_bfd_data (ibfd, obfd))
8575 return false;
8578 return _bfd_elf_fixup_group_sections (ibfd, NULL);
8581 /* Copy private symbol information. If this symbol is in a section
8582 which we did not map into a BFD section, try to map the section
8583 index correctly. We use special macro definitions for the mapped
8584 section indices; these definitions are interpreted by the
8585 swap_out_syms function. */
8587 #define MAP_ONESYMTAB (SHN_HIOS + 1)
8588 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
8589 #define MAP_STRTAB (SHN_HIOS + 3)
8590 #define MAP_SHSTRTAB (SHN_HIOS + 4)
8591 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
8593 bool
8594 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
8595 asymbol *isymarg,
8596 bfd *obfd,
8597 asymbol *osymarg)
8599 elf_symbol_type *isym, *osym;
8601 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8602 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8603 return true;
8605 isym = elf_symbol_from (isymarg);
8606 osym = elf_symbol_from (osymarg);
8608 if (isym != NULL
8609 && isym->internal_elf_sym.st_shndx != 0
8610 && osym != NULL
8611 && bfd_is_abs_section (isym->symbol.section))
8613 unsigned int shndx;
8615 shndx = isym->internal_elf_sym.st_shndx;
8616 if (shndx == elf_onesymtab (ibfd))
8617 shndx = MAP_ONESYMTAB;
8618 else if (shndx == elf_dynsymtab (ibfd))
8619 shndx = MAP_DYNSYMTAB;
8620 else if (shndx == elf_elfsections (ibfd)[elf_onesymtab (ibfd)]->sh_link)
8621 shndx = MAP_STRTAB;
8622 else if (shndx == elf_elfheader (ibfd)->e_shstrndx)
8623 shndx = MAP_SHSTRTAB;
8624 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
8625 shndx = MAP_SYM_SHNDX;
8626 osym->internal_elf_sym.st_shndx = shndx;
8629 return true;
8632 /* Swap out the symbols. */
8634 static bool
8635 swap_out_syms (bfd *abfd,
8636 struct elf_strtab_hash **sttp,
8637 int relocatable_p,
8638 struct bfd_link_info *info)
8640 const struct elf_backend_data *bed;
8641 unsigned int symcount;
8642 asymbol **syms;
8643 struct elf_strtab_hash *stt;
8644 Elf_Internal_Shdr *symtab_hdr;
8645 Elf_Internal_Shdr *symtab_shndx_hdr;
8646 Elf_Internal_Shdr *symstrtab_hdr;
8647 struct elf_sym_strtab *symstrtab;
8648 bfd_byte *outbound_syms;
8649 bfd_byte *outbound_shndx;
8650 unsigned long outbound_syms_index;
8651 unsigned int idx;
8652 unsigned int num_locals;
8653 size_t amt;
8654 bool name_local_sections;
8656 if (!elf_map_symbols (abfd, &num_locals))
8657 return false;
8659 /* Dump out the symtabs. */
8660 stt = _bfd_elf_strtab_init ();
8661 if (stt == NULL)
8662 return false;
8664 bed = get_elf_backend_data (abfd);
8665 symcount = bfd_get_symcount (abfd);
8666 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8667 symtab_hdr->sh_type = SHT_SYMTAB;
8668 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8669 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
8670 symtab_hdr->sh_info = num_locals + 1;
8671 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
8673 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8674 symstrtab_hdr->sh_type = SHT_STRTAB;
8676 /* Allocate buffer to swap out the .strtab section. */
8677 if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8678 || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
8680 bfd_set_error (bfd_error_no_memory);
8681 _bfd_elf_strtab_free (stt);
8682 return false;
8685 if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8686 || (outbound_syms = bfd_malloc (amt)) == NULL)
8688 error_no_mem:
8689 bfd_set_error (bfd_error_no_memory);
8690 error_return:
8691 free (symstrtab);
8692 _bfd_elf_strtab_free (stt);
8693 return false;
8695 symtab_hdr->contents = outbound_syms;
8696 outbound_syms_index = 0;
8698 outbound_shndx = NULL;
8700 if (elf_symtab_shndx_list (abfd))
8702 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8703 if (symtab_shndx_hdr->sh_name != 0)
8705 if (_bfd_mul_overflow (symcount + 1,
8706 sizeof (Elf_External_Sym_Shndx), &amt))
8707 goto error_no_mem;
8708 outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt);
8709 if (outbound_shndx == NULL)
8710 goto error_return;
8712 symtab_shndx_hdr->contents = outbound_shndx;
8713 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8714 symtab_shndx_hdr->sh_size = amt;
8715 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8716 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8718 /* FIXME: What about any other headers in the list ? */
8721 /* Now generate the data (for "contents"). */
8723 /* Fill in zeroth symbol and swap it out. */
8724 Elf_Internal_Sym sym;
8725 sym.st_name = 0;
8726 sym.st_value = 0;
8727 sym.st_size = 0;
8728 sym.st_info = 0;
8729 sym.st_other = 0;
8730 sym.st_shndx = SHN_UNDEF;
8731 sym.st_target_internal = 0;
8732 symstrtab[outbound_syms_index].sym = sym;
8733 symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
8734 outbound_syms_index++;
8737 name_local_sections
8738 = (bed->elf_backend_name_local_section_symbols
8739 && bed->elf_backend_name_local_section_symbols (abfd));
8741 syms = bfd_get_outsymbols (abfd);
8742 for (idx = 0; idx < symcount; idx++)
8744 Elf_Internal_Sym sym;
8746 flagword flags = syms[idx]->flags;
8747 if (!name_local_sections
8748 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8750 /* Local section symbols have no name. */
8751 sym.st_name = 0;
8753 else
8755 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8756 to get the final offset for st_name. */
8757 size_t stridx = _bfd_elf_strtab_add (stt, syms[idx]->name, false);
8758 if (stridx == (size_t) -1)
8759 goto error_return;
8760 sym.st_name = stridx;
8763 bfd_vma value = syms[idx]->value;
8764 elf_symbol_type *type_ptr = elf_symbol_from (syms[idx]);
8765 asection *sec = syms[idx]->section;
8767 if ((flags & BSF_SECTION_SYM) == 0 && bfd_is_com_section (sec))
8769 /* ELF common symbols put the alignment into the `value' field,
8770 and the size into the `size' field. This is backwards from
8771 how BFD handles it, so reverse it here. */
8772 sym.st_size = value;
8773 if (type_ptr == NULL
8774 || type_ptr->internal_elf_sym.st_value == 0)
8775 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8776 else
8777 sym.st_value = type_ptr->internal_elf_sym.st_value;
8778 sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8780 else
8782 unsigned int shndx;
8784 if (sec->output_section)
8786 value += sec->output_offset;
8787 sec = sec->output_section;
8790 /* Don't add in the section vma for relocatable output. */
8791 if (! relocatable_p)
8792 value += sec->vma;
8793 sym.st_value = value;
8794 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8796 if (bfd_is_abs_section (sec)
8797 && type_ptr != NULL
8798 && type_ptr->internal_elf_sym.st_shndx != 0)
8800 /* This symbol is in a real ELF section which we did
8801 not create as a BFD section. Undo the mapping done
8802 by copy_private_symbol_data. */
8803 shndx = type_ptr->internal_elf_sym.st_shndx;
8804 switch (shndx)
8806 case MAP_ONESYMTAB:
8807 shndx = elf_onesymtab (abfd);
8808 break;
8809 case MAP_DYNSYMTAB:
8810 shndx = elf_dynsymtab (abfd);
8811 break;
8812 case MAP_STRTAB:
8813 shndx = elf_strtab_sec (abfd);
8814 break;
8815 case MAP_SHSTRTAB:
8816 shndx = elf_shstrtab_sec (abfd);
8817 break;
8818 case MAP_SYM_SHNDX:
8819 if (elf_symtab_shndx_list (abfd))
8820 shndx = elf_symtab_shndx_list (abfd)->ndx;
8821 break;
8822 case SHN_COMMON:
8823 case SHN_ABS:
8824 shndx = SHN_ABS;
8825 break;
8826 default:
8827 if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8829 if (bed->symbol_section_index)
8830 shndx = bed->symbol_section_index (abfd, type_ptr);
8831 /* Otherwise just leave the index alone. */
8833 else
8835 if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8836 _bfd_error_handler (_("%pB: \
8837 Unable to handle section index %x in ELF symbol. Using ABS instead."),
8838 abfd, shndx);
8839 shndx = SHN_ABS;
8841 break;
8844 else
8846 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8848 if (shndx == SHN_BAD)
8850 asection *sec2;
8852 /* Writing this would be a hell of a lot easier if
8853 we had some decent documentation on bfd, and
8854 knew what to expect of the library, and what to
8855 demand of applications. For example, it
8856 appears that `objcopy' might not set the
8857 section of a symbol to be a section that is
8858 actually in the output file. */
8859 sec2 = bfd_get_section_by_name (abfd, sec->name);
8860 if (sec2 != NULL)
8861 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8862 if (shndx == SHN_BAD)
8864 /* xgettext:c-format */
8865 _bfd_error_handler
8866 (_("unable to find equivalent output section"
8867 " for symbol '%s' from section '%s'"),
8868 syms[idx]->name ? syms[idx]->name : "<Local sym>",
8869 sec->name);
8870 bfd_set_error (bfd_error_invalid_operation);
8871 goto error_return;
8876 sym.st_shndx = shndx;
8879 int type;
8880 if ((flags & BSF_THREAD_LOCAL) != 0)
8881 type = STT_TLS;
8882 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8883 type = STT_GNU_IFUNC;
8884 else if ((flags & BSF_FUNCTION) != 0)
8885 type = STT_FUNC;
8886 else if ((flags & BSF_OBJECT) != 0)
8887 type = STT_OBJECT;
8888 else if ((flags & BSF_RELC) != 0)
8889 type = STT_RELC;
8890 else if ((flags & BSF_SRELC) != 0)
8891 type = STT_SRELC;
8892 else
8893 type = STT_NOTYPE;
8895 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8896 type = STT_TLS;
8898 /* Processor-specific types. */
8899 if (type_ptr != NULL
8900 && bed->elf_backend_get_symbol_type)
8901 type = ((*bed->elf_backend_get_symbol_type)
8902 (&type_ptr->internal_elf_sym, type));
8904 if (flags & BSF_SECTION_SYM)
8906 if (flags & BSF_GLOBAL)
8907 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8908 else
8909 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8911 else if (bfd_is_com_section (syms[idx]->section))
8913 if (type != STT_TLS)
8915 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8916 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8917 ? STT_COMMON : STT_OBJECT);
8918 else
8919 type = ((flags & BSF_ELF_COMMON) != 0
8920 ? STT_COMMON : STT_OBJECT);
8922 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
8924 else if (bfd_is_und_section (syms[idx]->section))
8925 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8926 ? STB_WEAK
8927 : STB_GLOBAL),
8928 type);
8929 else if (flags & BSF_FILE)
8930 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8931 else
8933 int bind = STB_LOCAL;
8935 if (flags & BSF_LOCAL)
8936 bind = STB_LOCAL;
8937 else if (flags & BSF_GNU_UNIQUE)
8938 bind = STB_GNU_UNIQUE;
8939 else if (flags & BSF_WEAK)
8940 bind = STB_WEAK;
8941 else if (flags & BSF_GLOBAL)
8942 bind = STB_GLOBAL;
8944 sym.st_info = ELF_ST_INFO (bind, type);
8947 if (type_ptr != NULL)
8949 sym.st_other = type_ptr->internal_elf_sym.st_other;
8950 sym.st_target_internal
8951 = type_ptr->internal_elf_sym.st_target_internal;
8953 else
8955 sym.st_other = 0;
8956 sym.st_target_internal = 0;
8959 symstrtab[outbound_syms_index].sym = sym;
8960 symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
8961 outbound_syms_index++;
8964 /* Finalize the .strtab section. */
8965 _bfd_elf_strtab_finalize (stt);
8967 /* Swap out the .strtab section. */
8968 for (idx = 0; idx < outbound_syms_index; idx++)
8970 struct elf_sym_strtab *elfsym = &symstrtab[idx];
8971 if (elfsym->sym.st_name != 0)
8972 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8973 elfsym->sym.st_name);
8974 if (info && info->callbacks->ctf_new_symbol)
8975 info->callbacks->ctf_new_symbol (elfsym->dest_index,
8976 &elfsym->sym);
8978 /* Inform the linker of the addition of this symbol. */
8980 bed->s->swap_symbol_out (abfd, &elfsym->sym,
8981 (outbound_syms
8982 + (elfsym->dest_index
8983 * bed->s->sizeof_sym)),
8984 NPTR_ADD (outbound_shndx,
8985 (elfsym->dest_index
8986 * sizeof (Elf_External_Sym_Shndx))));
8988 free (symstrtab);
8990 *sttp = stt;
8991 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
8992 symstrtab_hdr->sh_type = SHT_STRTAB;
8993 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
8994 symstrtab_hdr->sh_addr = 0;
8995 symstrtab_hdr->sh_entsize = 0;
8996 symstrtab_hdr->sh_link = 0;
8997 symstrtab_hdr->sh_info = 0;
8998 symstrtab_hdr->sh_addralign = 1;
9000 return true;
9003 /* Return the number of bytes required to hold the symtab vector.
9005 Note that we base it on the count plus 1, since we will null terminate
9006 the vector allocated based on this size. However, the ELF symbol table
9007 always has a dummy entry as symbol #0, so it ends up even. */
9009 long
9010 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
9012 bfd_size_type symcount;
9013 long symtab_size;
9014 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
9016 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9017 if (symcount > LONG_MAX / sizeof (asymbol *))
9019 bfd_set_error (bfd_error_file_too_big);
9020 return -1;
9022 symtab_size = symcount * (sizeof (asymbol *));
9023 if (symcount == 0)
9024 symtab_size = sizeof (asymbol *);
9025 else if (!bfd_write_p (abfd))
9027 ufile_ptr filesize = bfd_get_file_size (abfd);
9029 if (filesize != 0 && (unsigned long) symtab_size > filesize)
9031 bfd_set_error (bfd_error_file_truncated);
9032 return -1;
9036 return symtab_size;
9039 long
9040 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
9042 bfd_size_type symcount;
9043 long symtab_size;
9044 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
9046 if (elf_dynsymtab (abfd) == 0)
9048 /* Check if there is dynamic symbol table. */
9049 symcount = elf_tdata (abfd)->dt_symtab_count;
9050 if (symcount)
9051 goto compute_symtab_size;
9053 bfd_set_error (bfd_error_invalid_operation);
9054 return -1;
9057 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9058 if (symcount > LONG_MAX / sizeof (asymbol *))
9060 bfd_set_error (bfd_error_file_too_big);
9061 return -1;
9064 compute_symtab_size:
9065 symtab_size = symcount * (sizeof (asymbol *));
9066 if (symcount == 0)
9067 symtab_size = sizeof (asymbol *);
9068 else if (!bfd_write_p (abfd))
9070 ufile_ptr filesize = bfd_get_file_size (abfd);
9072 if (filesize != 0 && (unsigned long) symtab_size > filesize)
9074 bfd_set_error (bfd_error_file_truncated);
9075 return -1;
9079 return symtab_size;
9082 long
9083 _bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
9085 if (asect->reloc_count != 0 && !bfd_write_p (abfd))
9087 /* Sanity check reloc section size. */
9088 ufile_ptr filesize = bfd_get_file_size (abfd);
9090 if (filesize != 0)
9092 struct bfd_elf_section_data *d = elf_section_data (asect);
9093 bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
9094 bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
9096 if (rel_size + rela_size > filesize
9097 || rel_size + rela_size < rel_size)
9099 bfd_set_error (bfd_error_file_truncated);
9100 return -1;
9105 #if SIZEOF_LONG == SIZEOF_INT
9106 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
9108 bfd_set_error (bfd_error_file_too_big);
9109 return -1;
9111 #endif
9112 return (asect->reloc_count + 1L) * sizeof (arelent *);
9115 /* Canonicalize the relocs. */
9117 long
9118 _bfd_elf_canonicalize_reloc (bfd *abfd,
9119 sec_ptr section,
9120 arelent **relptr,
9121 asymbol **symbols)
9123 arelent *tblptr;
9124 unsigned int i;
9125 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9127 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
9128 return -1;
9130 tblptr = section->relocation;
9131 for (i = 0; i < section->reloc_count; i++)
9132 *relptr++ = tblptr++;
9134 *relptr = NULL;
9136 return section->reloc_count;
9139 long
9140 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
9142 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9143 long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
9145 if (symcount >= 0)
9146 abfd->symcount = symcount;
9147 return symcount;
9150 long
9151 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
9152 asymbol **allocation)
9154 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9155 long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
9157 if (symcount >= 0)
9158 abfd->dynsymcount = symcount;
9159 return symcount;
9162 /* Return the size required for the dynamic reloc entries. Any loadable
9163 section that was actually installed in the BFD, and has type SHT_REL
9164 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
9165 dynamic reloc section. */
9167 long
9168 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
9170 bfd_size_type count, ext_rel_size;
9171 asection *s;
9173 if (elf_dynsymtab (abfd) == 0)
9175 bfd_set_error (bfd_error_invalid_operation);
9176 return -1;
9179 count = 1;
9180 ext_rel_size = 0;
9181 for (s = abfd->sections; s != NULL; s = s->next)
9182 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9183 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9184 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9185 && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9187 ext_rel_size += elf_section_data (s)->this_hdr.sh_size;
9188 if (ext_rel_size < elf_section_data (s)->this_hdr.sh_size)
9190 bfd_set_error (bfd_error_file_truncated);
9191 return -1;
9193 count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9194 if (count > LONG_MAX / sizeof (arelent *))
9196 bfd_set_error (bfd_error_file_too_big);
9197 return -1;
9200 if (count > 1 && !bfd_write_p (abfd))
9202 /* Sanity check reloc section sizes. */
9203 ufile_ptr filesize = bfd_get_file_size (abfd);
9204 if (filesize != 0 && ext_rel_size > filesize)
9206 bfd_set_error (bfd_error_file_truncated);
9207 return -1;
9210 return count * sizeof (arelent *);
9213 /* Canonicalize the dynamic relocation entries. Note that we return the
9214 dynamic relocations as a single block, although they are actually
9215 associated with particular sections; the interface, which was
9216 designed for SunOS style shared libraries, expects that there is only
9217 one set of dynamic relocs. Any loadable section that was actually
9218 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
9219 dynamic symbol table, is considered to be a dynamic reloc section. */
9221 long
9222 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
9223 arelent **storage,
9224 asymbol **syms)
9226 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
9227 asection *s;
9228 long ret;
9230 if (elf_dynsymtab (abfd) == 0)
9232 bfd_set_error (bfd_error_invalid_operation);
9233 return -1;
9236 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
9237 ret = 0;
9238 for (s = abfd->sections; s != NULL; s = s->next)
9240 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9241 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9242 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9243 && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9245 arelent *p;
9246 long count, i;
9248 if (! (*slurp_relocs) (abfd, s, syms, true))
9249 return -1;
9250 count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9251 p = s->relocation;
9252 for (i = 0; i < count; i++)
9253 *storage++ = p++;
9254 ret += count;
9258 *storage = NULL;
9260 return ret;
9263 /* Read in the version information. */
9265 bool
9266 _bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
9268 bfd_byte *contents = NULL;
9269 unsigned int freeidx = 0;
9270 size_t amt;
9271 void *contents_addr = NULL;
9272 size_t contents_size = 0;
9274 if (elf_dynverref (abfd) != 0 || elf_tdata (abfd)->dt_verneed != NULL)
9276 Elf_Internal_Shdr *hdr;
9277 Elf_External_Verneed *everneed;
9278 Elf_Internal_Verneed *iverneed;
9279 unsigned int i;
9280 bfd_byte *contents_end;
9281 size_t verneed_count;
9282 size_t verneed_size;
9284 if (elf_tdata (abfd)->dt_verneed != NULL)
9286 hdr = NULL;
9287 contents = elf_tdata (abfd)->dt_verneed;
9288 verneed_count = elf_tdata (abfd)->dt_verneed_count;
9289 verneed_size = verneed_count * sizeof (Elf_External_Verneed);
9291 else
9293 hdr = &elf_tdata (abfd)->dynverref_hdr;
9295 if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
9297 error_return_bad_verref:
9298 _bfd_error_handler
9299 (_("%pB: .gnu.version_r invalid entry"), abfd);
9300 bfd_set_error (bfd_error_bad_value);
9301 error_return_verref:
9302 elf_tdata (abfd)->verref = NULL;
9303 elf_tdata (abfd)->cverrefs = 0;
9304 goto error_return;
9307 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9308 goto error_return_verref;
9309 contents_size = hdr->sh_size;
9310 contents = _bfd_mmap_temporary (abfd, contents_size,
9311 &contents_addr, &contents_size);
9312 if (contents == NULL)
9313 goto error_return_verref;
9315 verneed_size = hdr->sh_size;
9316 verneed_count = hdr->sh_info;
9319 if (_bfd_mul_overflow (verneed_count,
9320 sizeof (Elf_Internal_Verneed), &amt))
9322 bfd_set_error (bfd_error_file_too_big);
9323 goto error_return_verref;
9325 if (amt == 0)
9326 goto error_return_verref;
9327 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
9328 if (elf_tdata (abfd)->verref == NULL)
9329 goto error_return_verref;
9331 BFD_ASSERT (sizeof (Elf_External_Verneed)
9332 == sizeof (Elf_External_Vernaux));
9333 contents_end = (contents + verneed_size
9334 - sizeof (Elf_External_Verneed));
9335 everneed = (Elf_External_Verneed *) contents;
9336 iverneed = elf_tdata (abfd)->verref;
9337 for (i = 0; i < verneed_count; i++, iverneed++)
9339 Elf_External_Vernaux *evernaux;
9340 Elf_Internal_Vernaux *ivernaux;
9341 unsigned int j;
9343 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
9345 iverneed->vn_bfd = abfd;
9347 if (elf_use_dt_symtab_p (abfd))
9349 if (iverneed->vn_file < elf_tdata (abfd)->dt_strsz)
9350 iverneed->vn_filename
9351 = elf_tdata (abfd)->dt_strtab + iverneed->vn_file;
9352 else
9353 iverneed->vn_filename = NULL;
9355 else if (hdr == NULL)
9356 goto error_return_bad_verref;
9357 else
9358 iverneed->vn_filename
9359 = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9360 iverneed->vn_file);
9361 if (iverneed->vn_filename == NULL)
9362 goto error_return_bad_verref;
9364 if (iverneed->vn_cnt == 0)
9365 iverneed->vn_auxptr = NULL;
9366 else
9368 if (_bfd_mul_overflow (iverneed->vn_cnt,
9369 sizeof (Elf_Internal_Vernaux), &amt))
9371 bfd_set_error (bfd_error_file_too_big);
9372 goto error_return_verref;
9374 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
9375 bfd_alloc (abfd, amt);
9376 if (iverneed->vn_auxptr == NULL)
9377 goto error_return_verref;
9380 if (iverneed->vn_aux
9381 > (size_t) (contents_end - (bfd_byte *) everneed))
9382 goto error_return_bad_verref;
9384 evernaux = ((Elf_External_Vernaux *)
9385 ((bfd_byte *) everneed + iverneed->vn_aux));
9386 ivernaux = iverneed->vn_auxptr;
9387 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
9389 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
9391 if (elf_use_dt_symtab_p (abfd))
9393 if (ivernaux->vna_name < elf_tdata (abfd)->dt_strsz)
9394 ivernaux->vna_nodename
9395 = elf_tdata (abfd)->dt_strtab + ivernaux->vna_name;
9396 else
9397 ivernaux->vna_nodename = NULL;
9399 else if (hdr == NULL)
9400 goto error_return_bad_verref;
9401 else
9402 ivernaux->vna_nodename
9403 = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9404 ivernaux->vna_name);
9405 if (ivernaux->vna_nodename == NULL)
9406 goto error_return_bad_verref;
9408 if (ivernaux->vna_other > freeidx)
9409 freeidx = ivernaux->vna_other;
9411 ivernaux->vna_nextptr = NULL;
9412 if (ivernaux->vna_next == 0)
9414 iverneed->vn_cnt = j + 1;
9415 break;
9417 if (j + 1 < iverneed->vn_cnt)
9418 ivernaux->vna_nextptr = ivernaux + 1;
9420 if (ivernaux->vna_next
9421 > (size_t) (contents_end - (bfd_byte *) evernaux))
9422 goto error_return_bad_verref;
9424 evernaux = ((Elf_External_Vernaux *)
9425 ((bfd_byte *) evernaux + ivernaux->vna_next));
9428 iverneed->vn_nextref = NULL;
9429 if (iverneed->vn_next == 0)
9430 break;
9431 if (hdr != NULL && (i + 1 < hdr->sh_info))
9432 iverneed->vn_nextref = iverneed + 1;
9434 if (iverneed->vn_next
9435 > (size_t) (contents_end - (bfd_byte *) everneed))
9436 goto error_return_bad_verref;
9438 everneed = ((Elf_External_Verneed *)
9439 ((bfd_byte *) everneed + iverneed->vn_next));
9441 elf_tdata (abfd)->cverrefs = i;
9443 if (contents != elf_tdata (abfd)->dt_verneed)
9444 _bfd_munmap_temporary (contents_addr, contents_size);
9445 contents = NULL;
9446 contents_addr = NULL;
9449 if (elf_dynverdef (abfd) != 0 || elf_tdata (abfd)->dt_verdef != NULL)
9451 Elf_Internal_Shdr *hdr;
9452 Elf_External_Verdef *everdef;
9453 Elf_Internal_Verdef *iverdef;
9454 Elf_Internal_Verdef *iverdefarr;
9455 Elf_Internal_Verdef iverdefmem;
9456 unsigned int i;
9457 unsigned int maxidx;
9458 bfd_byte *contents_end_def, *contents_end_aux;
9459 size_t verdef_count;
9460 size_t verdef_size;
9462 if (elf_tdata (abfd)->dt_verdef != NULL)
9464 hdr = NULL;
9465 contents = elf_tdata (abfd)->dt_verdef;
9466 verdef_count = elf_tdata (abfd)->dt_verdef_count;
9467 verdef_size = verdef_count * sizeof (Elf_External_Verdef);
9469 else
9471 hdr = &elf_tdata (abfd)->dynverdef_hdr;
9473 if (hdr->sh_size < sizeof (Elf_External_Verdef))
9475 error_return_bad_verdef:
9476 _bfd_error_handler
9477 (_("%pB: .gnu.version_d invalid entry"), abfd);
9478 bfd_set_error (bfd_error_bad_value);
9479 error_return_verdef:
9480 elf_tdata (abfd)->verdef = NULL;
9481 elf_tdata (abfd)->cverdefs = 0;
9482 goto error_return;
9485 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9486 goto error_return_verdef;
9487 contents_size = hdr->sh_size;
9488 contents = _bfd_mmap_temporary (abfd, contents_size,
9489 &contents_addr, &contents_size);
9490 if (contents == NULL)
9491 goto error_return_verdef;
9493 BFD_ASSERT (sizeof (Elf_External_Verdef)
9494 >= sizeof (Elf_External_Verdaux));
9496 verdef_count = hdr->sh_info;
9497 verdef_size = hdr->sh_size;
9500 contents_end_def = (contents + verdef_size
9501 - sizeof (Elf_External_Verdef));
9502 contents_end_aux = (contents + verdef_size
9503 - sizeof (Elf_External_Verdaux));
9505 /* We know the number of entries in the section but not the maximum
9506 index. Therefore we have to run through all entries and find
9507 the maximum. */
9508 everdef = (Elf_External_Verdef *) contents;
9509 maxidx = 0;
9510 for (i = 0; i < verdef_count; ++i)
9512 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9514 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
9515 goto error_return_bad_verdef;
9516 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
9517 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
9519 if (iverdefmem.vd_next == 0)
9520 break;
9522 if (iverdefmem.vd_next
9523 > (size_t) (contents_end_def - (bfd_byte *) everdef))
9524 goto error_return_bad_verdef;
9526 everdef = ((Elf_External_Verdef *)
9527 ((bfd_byte *) everdef + iverdefmem.vd_next));
9530 if (default_imported_symver)
9532 if (freeidx > maxidx)
9533 maxidx = ++freeidx;
9534 else
9535 freeidx = ++maxidx;
9537 if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
9539 bfd_set_error (bfd_error_file_too_big);
9540 goto error_return_verdef;
9543 if (amt == 0)
9544 goto error_return_verdef;
9545 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9546 if (elf_tdata (abfd)->verdef == NULL)
9547 goto error_return_verdef;
9549 elf_tdata (abfd)->cverdefs = maxidx;
9551 everdef = (Elf_External_Verdef *) contents;
9552 iverdefarr = elf_tdata (abfd)->verdef;
9553 for (i = 0; i < verdef_count; ++i)
9555 Elf_External_Verdaux *everdaux;
9556 Elf_Internal_Verdaux *iverdaux;
9557 unsigned int j;
9559 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9561 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
9562 goto error_return_bad_verdef;
9564 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
9565 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
9567 iverdef->vd_bfd = abfd;
9569 if (iverdef->vd_cnt == 0)
9570 iverdef->vd_auxptr = NULL;
9571 else
9573 if (_bfd_mul_overflow (iverdef->vd_cnt,
9574 sizeof (Elf_Internal_Verdaux), &amt))
9576 bfd_set_error (bfd_error_file_too_big);
9577 goto error_return_verdef;
9579 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
9580 bfd_alloc (abfd, amt);
9581 if (iverdef->vd_auxptr == NULL)
9582 goto error_return_verdef;
9585 if (iverdef->vd_aux
9586 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
9587 goto error_return_bad_verdef;
9589 everdaux = ((Elf_External_Verdaux *)
9590 ((bfd_byte *) everdef + iverdef->vd_aux));
9591 iverdaux = iverdef->vd_auxptr;
9592 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
9594 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
9596 if (elf_use_dt_symtab_p (abfd))
9598 if (iverdaux->vda_name < elf_tdata (abfd)->dt_strsz)
9599 iverdaux->vda_nodename
9600 = elf_tdata (abfd)->dt_strtab + iverdaux->vda_name;
9601 else
9602 iverdaux->vda_nodename = NULL;
9604 else
9605 iverdaux->vda_nodename
9606 = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9607 iverdaux->vda_name);
9608 if (iverdaux->vda_nodename == NULL)
9609 goto error_return_bad_verdef;
9611 iverdaux->vda_nextptr = NULL;
9612 if (iverdaux->vda_next == 0)
9614 iverdef->vd_cnt = j + 1;
9615 break;
9617 if (j + 1 < iverdef->vd_cnt)
9618 iverdaux->vda_nextptr = iverdaux + 1;
9620 if (iverdaux->vda_next
9621 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
9622 goto error_return_bad_verdef;
9624 everdaux = ((Elf_External_Verdaux *)
9625 ((bfd_byte *) everdaux + iverdaux->vda_next));
9628 iverdef->vd_nodename = NULL;
9629 if (iverdef->vd_cnt)
9630 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
9632 iverdef->vd_nextdef = NULL;
9633 if (iverdef->vd_next == 0)
9634 break;
9635 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
9636 iverdef->vd_nextdef = iverdef + 1;
9638 everdef = ((Elf_External_Verdef *)
9639 ((bfd_byte *) everdef + iverdef->vd_next));
9642 if (contents != elf_tdata (abfd)->dt_verdef)
9643 _bfd_munmap_temporary (contents_addr, contents_size);
9644 contents = NULL;
9645 contents_addr = NULL;
9647 else if (default_imported_symver)
9649 if (freeidx < 3)
9650 freeidx = 3;
9651 else
9652 freeidx++;
9654 if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
9656 bfd_set_error (bfd_error_file_too_big);
9657 goto error_return;
9659 if (amt == 0)
9660 goto error_return;
9661 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9662 if (elf_tdata (abfd)->verdef == NULL)
9663 goto error_return;
9665 elf_tdata (abfd)->cverdefs = freeidx;
9668 /* Create a default version based on the soname. */
9669 if (default_imported_symver)
9671 Elf_Internal_Verdef *iverdef;
9672 Elf_Internal_Verdaux *iverdaux;
9674 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
9676 iverdef->vd_version = VER_DEF_CURRENT;
9677 iverdef->vd_flags = 0;
9678 iverdef->vd_ndx = freeidx;
9679 iverdef->vd_cnt = 1;
9681 iverdef->vd_bfd = abfd;
9683 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9684 if (iverdef->vd_nodename == NULL)
9685 goto error_return_verdef;
9686 iverdef->vd_nextdef = NULL;
9687 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
9688 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
9689 if (iverdef->vd_auxptr == NULL)
9690 goto error_return_verdef;
9692 iverdaux = iverdef->vd_auxptr;
9693 iverdaux->vda_nodename = iverdef->vd_nodename;
9696 return true;
9698 error_return:
9699 if (contents != elf_tdata (abfd)->dt_verneed
9700 && contents != elf_tdata (abfd)->dt_verdef)
9701 _bfd_munmap_temporary (contents_addr, contents_size);
9702 return false;
9705 asymbol *
9706 _bfd_elf_make_empty_symbol (bfd *abfd)
9708 elf_symbol_type *newsym;
9710 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
9711 if (!newsym)
9712 return NULL;
9713 newsym->symbol.the_bfd = abfd;
9714 return &newsym->symbol;
9717 void
9718 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9719 asymbol *symbol,
9720 symbol_info *ret)
9722 bfd_symbol_info (symbol, ret);
9725 /* Return whether a symbol name implies a local symbol. Most targets
9726 use this function for the is_local_label_name entry point, but some
9727 override it. */
9729 bool
9730 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9731 const char *name)
9733 /* Normal local symbols start with ``.L''. */
9734 if (name[0] == '.' && name[1] == 'L')
9735 return true;
9737 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9738 DWARF debugging symbols starting with ``..''. */
9739 if (name[0] == '.' && name[1] == '.')
9740 return true;
9742 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9743 emitting DWARF debugging output. I suspect this is actually a
9744 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9745 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9746 underscore to be emitted on some ELF targets). For ease of use,
9747 we treat such symbols as local. */
9748 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
9749 return true;
9751 /* Treat assembler generated fake symbols, dollar local labels and
9752 forward-backward labels (aka local labels) as locals.
9753 These labels have the form:
9755 L0^A.* (fake symbols)
9757 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9759 Versions which start with .L will have already been matched above,
9760 so we only need to match the rest. */
9761 if (name[0] == 'L' && ISDIGIT (name[1]))
9763 bool ret = false;
9764 const char * p;
9765 char c;
9767 for (p = name + 2; (c = *p); p++)
9769 if (c == 1 || c == 2)
9771 if (c == 1 && p == name + 2)
9772 /* A fake symbol. */
9773 return true;
9775 /* FIXME: We are being paranoid here and treating symbols like
9776 L0^Bfoo as if there were non-local, on the grounds that the
9777 assembler will never generate them. But can any symbol
9778 containing an ASCII value in the range 1-31 ever be anything
9779 other than some kind of local ? */
9780 ret = true;
9783 if (! ISDIGIT (c))
9785 ret = false;
9786 break;
9789 return ret;
9792 return false;
9795 alent *
9796 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9797 asymbol *symbol ATTRIBUTE_UNUSED)
9799 abort ();
9800 return NULL;
9803 bool
9804 _bfd_elf_set_arch_mach (bfd *abfd,
9805 enum bfd_architecture arch,
9806 unsigned long machine)
9808 /* If this isn't the right architecture for this backend, and this
9809 isn't the generic backend, fail. */
9810 if (arch != get_elf_backend_data (abfd)->arch
9811 && arch != bfd_arch_unknown
9812 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
9813 return false;
9815 return bfd_default_set_arch_mach (abfd, arch, machine);
9818 /* Find the nearest line to a particular section and offset,
9819 for error reporting. */
9821 bool
9822 _bfd_elf_find_nearest_line (bfd *abfd,
9823 asymbol **symbols,
9824 asection *section,
9825 bfd_vma offset,
9826 const char **filename_ptr,
9827 const char **functionname_ptr,
9828 unsigned int *line_ptr,
9829 unsigned int *discriminator_ptr)
9831 return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
9832 offset, filename_ptr,
9833 functionname_ptr, line_ptr,
9834 discriminator_ptr);
9837 /* Find the nearest line to a particular section and offset,
9838 for error reporting. ALT_BFD representing a .gnu_debugaltlink file
9839 can be optionally specified. */
9841 bool
9842 _bfd_elf_find_nearest_line_with_alt (bfd *abfd,
9843 const char *alt_filename,
9844 asymbol **symbols,
9845 asection *section,
9846 bfd_vma offset,
9847 const char **filename_ptr,
9848 const char **functionname_ptr,
9849 unsigned int *line_ptr,
9850 unsigned int *discriminator_ptr)
9852 bool found;
9854 if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
9855 section, offset, filename_ptr,
9856 functionname_ptr, line_ptr,
9857 discriminator_ptr,
9858 dwarf_debug_sections,
9859 &elf_tdata (abfd)->dwarf2_find_line_info))
9860 return true;
9862 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9863 filename_ptr, functionname_ptr, line_ptr))
9865 if (!*functionname_ptr)
9866 _bfd_elf_find_function (abfd, symbols, section, offset,
9867 *filename_ptr ? NULL : filename_ptr,
9868 functionname_ptr);
9869 return true;
9872 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
9873 &found, filename_ptr,
9874 functionname_ptr, line_ptr,
9875 &elf_tdata (abfd)->line_info))
9876 return false;
9877 if (found && (*functionname_ptr || *line_ptr))
9878 return true;
9880 if (symbols == NULL)
9881 return false;
9883 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9884 filename_ptr, functionname_ptr))
9885 return false;
9887 *line_ptr = 0;
9888 return true;
9891 /* Find the line for a symbol. */
9893 bool
9894 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9895 const char **filename_ptr, unsigned int *line_ptr)
9897 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9898 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9899 filename_ptr, NULL, line_ptr, NULL,
9900 dwarf_debug_sections,
9901 &tdata->dwarf2_find_line_info);
9904 /* After a call to bfd_find_nearest_line, successive calls to
9905 bfd_find_inliner_info can be used to get source information about
9906 each level of function inlining that terminated at the address
9907 passed to bfd_find_nearest_line. Currently this is only supported
9908 for DWARF2 with appropriate DWARF3 extensions. */
9910 bool
9911 _bfd_elf_find_inliner_info (bfd *abfd,
9912 const char **filename_ptr,
9913 const char **functionname_ptr,
9914 unsigned int *line_ptr)
9916 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9917 return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9918 functionname_ptr, line_ptr,
9919 &tdata->dwarf2_find_line_info);
9923 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
9925 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9926 int ret = bed->s->sizeof_ehdr;
9928 if (!bfd_link_relocatable (info))
9930 bfd_size_type phdr_size = elf_program_header_size (abfd);
9932 if (phdr_size == (bfd_size_type) -1)
9934 struct elf_segment_map *m;
9936 phdr_size = 0;
9937 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
9938 phdr_size += bed->s->sizeof_phdr;
9940 if (phdr_size == 0)
9941 phdr_size = get_program_header_size (abfd, info);
9944 elf_program_header_size (abfd) = phdr_size;
9945 ret += phdr_size;
9948 return ret;
9951 bool
9952 _bfd_elf_set_section_contents (bfd *abfd,
9953 sec_ptr section,
9954 const void *location,
9955 file_ptr offset,
9956 bfd_size_type count)
9958 Elf_Internal_Shdr *hdr;
9960 if (! abfd->output_has_begun
9961 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
9962 return false;
9964 if (!count)
9965 return true;
9967 hdr = &elf_section_data (section)->this_hdr;
9968 if (hdr->sh_offset == (file_ptr) -1)
9970 unsigned char *contents;
9972 if (bfd_section_is_ctf (section))
9973 /* Nothing to do with this section: the contents are generated
9974 later. */
9975 return true;
9977 if ((offset + count) > hdr->sh_size)
9979 _bfd_error_handler
9980 (_("%pB:%pA: error: attempting to write"
9981 " over the end of the section"),
9982 abfd, section);
9984 bfd_set_error (bfd_error_invalid_operation);
9985 return false;
9988 contents = hdr->contents;
9989 if (contents == NULL)
9991 _bfd_error_handler
9992 (_("%pB:%pA: error: attempting to write"
9993 " section into an empty buffer"),
9994 abfd, section);
9996 bfd_set_error (bfd_error_invalid_operation);
9997 return false;
10000 memcpy (contents + offset, location, count);
10001 return true;
10004 return _bfd_generic_set_section_contents (abfd, section,
10005 location, offset, count);
10008 bool
10009 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
10010 arelent *cache_ptr ATTRIBUTE_UNUSED,
10011 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
10013 abort ();
10014 return false;
10017 /* Try to convert a non-ELF reloc into an ELF one. */
10019 bool
10020 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
10022 /* Check whether we really have an ELF howto. */
10024 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
10026 bfd_reloc_code_real_type code;
10027 reloc_howto_type *howto;
10029 /* Alien reloc: Try to determine its type to replace it with an
10030 equivalent ELF reloc. */
10032 if (areloc->howto->pc_relative)
10034 switch (areloc->howto->bitsize)
10036 case 8:
10037 code = BFD_RELOC_8_PCREL;
10038 break;
10039 case 12:
10040 code = BFD_RELOC_12_PCREL;
10041 break;
10042 case 16:
10043 code = BFD_RELOC_16_PCREL;
10044 break;
10045 case 24:
10046 code = BFD_RELOC_24_PCREL;
10047 break;
10048 case 32:
10049 code = BFD_RELOC_32_PCREL;
10050 break;
10051 case 64:
10052 code = BFD_RELOC_64_PCREL;
10053 break;
10054 default:
10055 goto fail;
10058 howto = bfd_reloc_type_lookup (abfd, code);
10060 if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
10062 if (howto->pcrel_offset)
10063 areloc->addend += areloc->address;
10064 else
10065 areloc->addend -= areloc->address; /* addend is unsigned!! */
10068 else
10070 switch (areloc->howto->bitsize)
10072 case 8:
10073 code = BFD_RELOC_8;
10074 break;
10075 case 14:
10076 code = BFD_RELOC_14;
10077 break;
10078 case 16:
10079 code = BFD_RELOC_16;
10080 break;
10081 case 26:
10082 code = BFD_RELOC_26;
10083 break;
10084 case 32:
10085 code = BFD_RELOC_32;
10086 break;
10087 case 64:
10088 code = BFD_RELOC_64;
10089 break;
10090 default:
10091 goto fail;
10094 howto = bfd_reloc_type_lookup (abfd, code);
10097 if (howto)
10098 areloc->howto = howto;
10099 else
10100 goto fail;
10103 return true;
10105 fail:
10106 /* xgettext:c-format */
10107 _bfd_error_handler (_("%pB: %s unsupported"),
10108 abfd, areloc->howto->name);
10109 bfd_set_error (bfd_error_sorry);
10110 return false;
10113 bool
10114 _bfd_elf_free_cached_info (bfd *abfd)
10116 struct elf_obj_tdata *tdata;
10118 if ((bfd_get_format (abfd) == bfd_object
10119 || bfd_get_format (abfd) == bfd_core)
10120 && (tdata = elf_tdata (abfd)) != NULL)
10122 if (tdata->o != NULL && elf_shstrtab (abfd) != NULL)
10123 _bfd_elf_strtab_free (elf_shstrtab (abfd));
10124 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
10125 _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info);
10126 _bfd_stab_cleanup (abfd, &tdata->line_info);
10127 for (asection *sec = abfd->sections; sec != NULL; sec = sec->next)
10129 _bfd_elf_munmap_section_contents (sec, sec->contents);
10130 if (!sec->alloced)
10132 free (elf_section_data (sec)->this_hdr.contents);
10133 elf_section_data (sec)->this_hdr.contents = NULL;
10135 free (elf_section_data (sec)->relocs);
10136 elf_section_data (sec)->relocs = NULL;
10137 if (sec->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
10139 struct eh_frame_sec_info *sec_info
10140 = elf_section_data (sec)->sec_info;
10141 free (sec_info->cies);
10144 free (tdata->symtab_hdr.contents);
10145 tdata->symtab_hdr.contents = NULL;
10148 return _bfd_generic_bfd_free_cached_info (abfd);
10151 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
10152 in the relocation's offset. Thus we cannot allow any sort of sanity
10153 range-checking to interfere. There is nothing else to do in processing
10154 this reloc. */
10156 bfd_reloc_status_type
10157 _bfd_elf_rel_vtable_reloc_fn
10158 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
10159 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
10160 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
10161 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
10163 return bfd_reloc_ok;
10166 /* Elf core file support. Much of this only works on native
10167 toolchains, since we rely on knowing the
10168 machine-dependent procfs structure in order to pick
10169 out details about the corefile. */
10171 #ifdef HAVE_SYS_PROCFS_H
10172 # include <sys/procfs.h>
10173 #endif
10175 /* Return a PID that identifies a "thread" for threaded cores, or the
10176 PID of the main process for non-threaded cores. */
10178 static int
10179 elfcore_make_pid (bfd *abfd)
10181 int pid;
10183 pid = elf_tdata (abfd)->core->lwpid;
10184 if (pid == 0)
10185 pid = elf_tdata (abfd)->core->pid;
10187 return pid;
10190 /* If there isn't a section called NAME, make one, using data from
10191 SECT. Note, this function will generate a reference to NAME, so
10192 you shouldn't deallocate or overwrite it. */
10194 static bool
10195 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
10197 asection *sect2;
10199 if (bfd_get_section_by_name (abfd, name) != NULL)
10200 return true;
10202 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
10203 if (sect2 == NULL)
10204 return false;
10206 sect2->size = sect->size;
10207 sect2->filepos = sect->filepos;
10208 sect2->alignment_power = sect->alignment_power;
10209 return true;
10212 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
10213 actually creates up to two pseudosections:
10214 - For the single-threaded case, a section named NAME, unless
10215 such a section already exists.
10216 - For the multi-threaded case, a section named "NAME/PID", where
10217 PID is elfcore_make_pid (abfd).
10218 Both pseudosections have identical contents. */
10219 bool
10220 _bfd_elfcore_make_pseudosection (bfd *abfd,
10221 char *name,
10222 size_t size,
10223 ufile_ptr filepos)
10225 char buf[100];
10226 char *threaded_name;
10227 size_t len;
10228 asection *sect;
10230 /* Build the section name. */
10232 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
10233 len = strlen (buf) + 1;
10234 threaded_name = (char *) bfd_alloc (abfd, len);
10235 if (threaded_name == NULL)
10236 return false;
10237 memcpy (threaded_name, buf, len);
10239 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
10240 SEC_HAS_CONTENTS);
10241 if (sect == NULL)
10242 return false;
10243 sect->size = size;
10244 sect->filepos = filepos;
10245 sect->alignment_power = 2;
10247 return elfcore_maybe_make_sect (abfd, name, sect);
10250 static bool
10251 elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
10252 size_t offs)
10254 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10255 SEC_HAS_CONTENTS);
10257 if (sect == NULL)
10258 return false;
10260 sect->size = note->descsz - offs;
10261 sect->filepos = note->descpos + offs;
10262 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10264 return true;
10267 /* prstatus_t exists on:
10268 solaris 2.5+
10269 linux 2.[01] + glibc
10270 unixware 4.2
10273 #if defined (HAVE_PRSTATUS_T)
10275 static bool
10276 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
10278 size_t size;
10279 int offset;
10281 if (note->descsz == sizeof (prstatus_t))
10283 prstatus_t prstat;
10285 size = sizeof (prstat.pr_reg);
10286 offset = offsetof (prstatus_t, pr_reg);
10287 memcpy (&prstat, note->descdata, sizeof (prstat));
10289 /* Do not overwrite the core signal if it
10290 has already been set by another thread. */
10291 if (elf_tdata (abfd)->core->signal == 0)
10292 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10293 if (elf_tdata (abfd)->core->pid == 0)
10294 elf_tdata (abfd)->core->pid = prstat.pr_pid;
10296 /* pr_who exists on:
10297 solaris 2.5+
10298 unixware 4.2
10299 pr_who doesn't exist on:
10300 linux 2.[01]
10302 #if defined (HAVE_PRSTATUS_T_PR_WHO)
10303 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10304 #else
10305 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10306 #endif
10308 #if defined (HAVE_PRSTATUS32_T)
10309 else if (note->descsz == sizeof (prstatus32_t))
10311 /* 64-bit host, 32-bit corefile */
10312 prstatus32_t prstat;
10314 size = sizeof (prstat.pr_reg);
10315 offset = offsetof (prstatus32_t, pr_reg);
10316 memcpy (&prstat, note->descdata, sizeof (prstat));
10318 /* Do not overwrite the core signal if it
10319 has already been set by another thread. */
10320 if (elf_tdata (abfd)->core->signal == 0)
10321 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10322 if (elf_tdata (abfd)->core->pid == 0)
10323 elf_tdata (abfd)->core->pid = prstat.pr_pid;
10325 /* pr_who exists on:
10326 solaris 2.5+
10327 unixware 4.2
10328 pr_who doesn't exist on:
10329 linux 2.[01]
10331 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
10332 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10333 #else
10334 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10335 #endif
10337 #endif /* HAVE_PRSTATUS32_T */
10338 else
10340 /* Fail - we don't know how to handle any other
10341 note size (ie. data object type). */
10342 return true;
10345 /* Make a ".reg/999" section and a ".reg" section. */
10346 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10347 size, note->descpos + offset);
10349 #endif /* defined (HAVE_PRSTATUS_T) */
10351 /* Create a pseudosection containing the exact contents of NOTE. */
10352 static bool
10353 elfcore_make_note_pseudosection (bfd *abfd,
10354 char *name,
10355 Elf_Internal_Note *note)
10357 return _bfd_elfcore_make_pseudosection (abfd, name,
10358 note->descsz, note->descpos);
10361 /* There isn't a consistent prfpregset_t across platforms,
10362 but it doesn't matter, because we don't have to pick this
10363 data structure apart. */
10365 static bool
10366 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
10368 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10371 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
10372 type of NT_PRXFPREG. Just include the whole note's contents
10373 literally. */
10375 static bool
10376 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
10378 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
10381 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
10382 with a note type of NT_X86_XSTATE. Just include the whole note's
10383 contents literally. */
10385 static bool
10386 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
10388 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
10391 static bool
10392 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
10394 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
10397 static bool
10398 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
10400 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
10403 static bool
10404 elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
10406 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
10409 static bool
10410 elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
10412 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
10415 static bool
10416 elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
10418 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
10421 static bool
10422 elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
10424 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
10427 static bool
10428 elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
10430 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
10433 static bool
10434 elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
10436 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
10439 static bool
10440 elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
10442 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
10445 static bool
10446 elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
10448 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
10451 static bool
10452 elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
10454 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
10457 static bool
10458 elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
10460 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
10463 static bool
10464 elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
10466 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
10469 static bool
10470 elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
10472 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
10475 static bool
10476 elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
10478 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
10481 static bool
10482 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
10484 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
10487 static bool
10488 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
10490 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
10493 static bool
10494 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
10496 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
10499 static bool
10500 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
10502 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
10505 static bool
10506 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
10508 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
10511 static bool
10512 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
10514 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
10517 static bool
10518 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
10520 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
10523 static bool
10524 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
10526 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
10529 static bool
10530 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
10532 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
10535 static bool
10536 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
10538 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
10541 static bool
10542 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
10544 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
10547 static bool
10548 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
10550 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
10553 static bool
10554 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
10556 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
10559 static bool
10560 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
10562 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
10565 static bool
10566 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
10568 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
10571 static bool
10572 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
10574 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
10577 static bool
10578 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
10580 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
10583 static bool
10584 elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
10586 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
10589 static bool
10590 elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
10592 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
10595 static bool
10596 elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
10598 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
10599 note);
10602 static bool
10603 elfcore_grok_aarch_ssve (bfd *abfd, Elf_Internal_Note *note)
10605 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-ssve", note);
10608 static bool
10609 elfcore_grok_aarch_za (bfd *abfd, Elf_Internal_Note *note)
10611 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-za", note);
10614 /* Convert NOTE into a bfd_section called ".reg-aarch-zt". Return TRUE if
10615 successful, otherwise return FALSE. */
10617 static bool
10618 elfcore_grok_aarch_zt (bfd *abfd, Elf_Internal_Note *note)
10620 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-zt", note);
10623 static bool
10624 elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
10626 return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
10629 /* Convert NOTE into a bfd_section called ".reg-riscv-csr". Return TRUE if
10630 successful otherwise, return FALSE. */
10632 static bool
10633 elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
10635 return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
10638 /* Convert NOTE into a bfd_section called ".gdb-tdesc". Return TRUE if
10639 successful otherwise, return FALSE. */
10641 static bool
10642 elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
10644 return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
10647 static bool
10648 elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
10650 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note);
10653 static bool
10654 elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
10656 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note);
10659 static bool
10660 elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
10662 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note);
10665 static bool
10666 elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
10668 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note);
10671 #if defined (HAVE_PRPSINFO_T)
10672 typedef prpsinfo_t elfcore_psinfo_t;
10673 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
10674 typedef prpsinfo32_t elfcore_psinfo32_t;
10675 #endif
10676 #endif
10678 #if defined (HAVE_PSINFO_T)
10679 typedef psinfo_t elfcore_psinfo_t;
10680 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
10681 typedef psinfo32_t elfcore_psinfo32_t;
10682 #endif
10683 #endif
10685 /* return a malloc'ed copy of a string at START which is at
10686 most MAX bytes long, possibly without a terminating '\0'.
10687 the copy will always have a terminating '\0'. */
10689 char *
10690 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
10692 char *dups;
10693 char *end = (char *) memchr (start, '\0', max);
10694 size_t len;
10696 if (end == NULL)
10697 len = max;
10698 else
10699 len = end - start;
10701 dups = (char *) bfd_alloc (abfd, len + 1);
10702 if (dups == NULL)
10703 return NULL;
10705 memcpy (dups, start, len);
10706 dups[len] = '\0';
10708 return dups;
10711 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10712 static bool
10713 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
10715 if (note->descsz == sizeof (elfcore_psinfo_t))
10717 elfcore_psinfo_t psinfo;
10719 memcpy (&psinfo, note->descdata, sizeof (psinfo));
10721 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10722 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10723 #endif
10724 elf_tdata (abfd)->core->program
10725 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10726 sizeof (psinfo.pr_fname));
10728 elf_tdata (abfd)->core->command
10729 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10730 sizeof (psinfo.pr_psargs));
10732 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10733 else if (note->descsz == sizeof (elfcore_psinfo32_t))
10735 /* 64-bit host, 32-bit corefile */
10736 elfcore_psinfo32_t psinfo;
10738 memcpy (&psinfo, note->descdata, sizeof (psinfo));
10740 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10741 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10742 #endif
10743 elf_tdata (abfd)->core->program
10744 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10745 sizeof (psinfo.pr_fname));
10747 elf_tdata (abfd)->core->command
10748 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10749 sizeof (psinfo.pr_psargs));
10751 #endif
10753 else
10755 /* Fail - we don't know how to handle any other
10756 note size (ie. data object type). */
10757 return true;
10760 /* Note that for some reason, a spurious space is tacked
10761 onto the end of the args in some (at least one anyway)
10762 implementations, so strip it off if it exists. */
10765 char *command = elf_tdata (abfd)->core->command;
10766 int n = strlen (command);
10768 if (0 < n && command[n - 1] == ' ')
10769 command[n - 1] = '\0';
10772 return true;
10774 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10776 #if defined (HAVE_PSTATUS_T)
10777 static bool
10778 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
10780 if (note->descsz == sizeof (pstatus_t)
10781 #if defined (HAVE_PXSTATUS_T)
10782 || note->descsz == sizeof (pxstatus_t)
10783 #endif
10786 pstatus_t pstat;
10788 memcpy (&pstat, note->descdata, sizeof (pstat));
10790 elf_tdata (abfd)->core->pid = pstat.pr_pid;
10792 #if defined (HAVE_PSTATUS32_T)
10793 else if (note->descsz == sizeof (pstatus32_t))
10795 /* 64-bit host, 32-bit corefile */
10796 pstatus32_t pstat;
10798 memcpy (&pstat, note->descdata, sizeof (pstat));
10800 elf_tdata (abfd)->core->pid = pstat.pr_pid;
10802 #endif
10803 /* Could grab some more details from the "representative"
10804 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10805 NT_LWPSTATUS note, presumably. */
10807 return true;
10809 #endif /* defined (HAVE_PSTATUS_T) */
10811 #if defined (HAVE_LWPSTATUS_T)
10812 static bool
10813 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
10815 lwpstatus_t lwpstat;
10816 char buf[100];
10817 char *name;
10818 size_t len;
10819 asection *sect;
10821 if (note->descsz != sizeof (lwpstat)
10822 #if defined (HAVE_LWPXSTATUS_T)
10823 && note->descsz != sizeof (lwpxstatus_t)
10824 #endif
10826 return true;
10828 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10830 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
10831 /* Do not overwrite the core signal if it has already been set by
10832 another thread. */
10833 if (elf_tdata (abfd)->core->signal == 0)
10834 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
10836 /* Make a ".reg/999" section. */
10838 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
10839 len = strlen (buf) + 1;
10840 name = bfd_alloc (abfd, len);
10841 if (name == NULL)
10842 return false;
10843 memcpy (name, buf, len);
10845 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10846 if (sect == NULL)
10847 return false;
10849 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10850 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
10851 sect->filepos = note->descpos
10852 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10853 #endif
10855 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10856 sect->size = sizeof (lwpstat.pr_reg);
10857 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10858 #endif
10860 sect->alignment_power = 2;
10862 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
10863 return false;
10865 /* Make a ".reg2/999" section */
10867 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
10868 len = strlen (buf) + 1;
10869 name = bfd_alloc (abfd, len);
10870 if (name == NULL)
10871 return false;
10872 memcpy (name, buf, len);
10874 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10875 if (sect == NULL)
10876 return false;
10878 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10879 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
10880 sect->filepos = note->descpos
10881 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
10882 #endif
10884 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
10885 sect->size = sizeof (lwpstat.pr_fpreg);
10886 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
10887 #endif
10889 sect->alignment_power = 2;
10891 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
10893 #endif /* defined (HAVE_LWPSTATUS_T) */
10895 /* These constants, and the structure offsets used below, are defined by
10896 Cygwin's core_dump.h */
10897 #define NOTE_INFO_PROCESS 1
10898 #define NOTE_INFO_THREAD 2
10899 #define NOTE_INFO_MODULE 3
10900 #define NOTE_INFO_MODULE64 4
10902 static bool
10903 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
10905 char buf[30];
10906 char *name;
10907 size_t len;
10908 unsigned int name_size;
10909 asection *sect;
10910 unsigned int type;
10911 int is_active_thread;
10912 bfd_vma base_addr;
10914 if (note->descsz < 4)
10915 return true;
10917 if (! startswith (note->namedata, "win32"))
10918 return true;
10920 type = bfd_get_32 (abfd, note->descdata);
10922 struct
10924 const char *type_name;
10925 unsigned long min_size;
10926 } size_check[] =
10928 { "NOTE_INFO_PROCESS", 12 },
10929 { "NOTE_INFO_THREAD", 12 },
10930 { "NOTE_INFO_MODULE", 12 },
10931 { "NOTE_INFO_MODULE64", 16 },
10934 if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
10935 return true;
10937 if (note->descsz < size_check[type - 1].min_size)
10939 _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
10940 " is too small"),
10941 abfd, size_check[type - 1].type_name, note->descsz);
10942 return true;
10945 switch (type)
10947 case NOTE_INFO_PROCESS:
10948 /* FIXME: need to add ->core->command. */
10949 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
10950 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
10951 break;
10953 case NOTE_INFO_THREAD:
10954 /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
10955 structure. */
10956 /* thread_info.tid */
10957 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
10959 len = strlen (buf) + 1;
10960 name = (char *) bfd_alloc (abfd, len);
10961 if (name == NULL)
10962 return false;
10964 memcpy (name, buf, len);
10966 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10967 if (sect == NULL)
10968 return false;
10970 /* sizeof (thread_info.thread_context) */
10971 sect->size = note->descsz - 12;
10972 /* offsetof (thread_info.thread_context) */
10973 sect->filepos = note->descpos + 12;
10974 sect->alignment_power = 2;
10976 /* thread_info.is_active_thread */
10977 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
10979 if (is_active_thread)
10980 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
10981 return false;
10982 break;
10984 case NOTE_INFO_MODULE:
10985 case NOTE_INFO_MODULE64:
10986 /* Make a ".module/xxxxxxxx" section. */
10987 if (type == NOTE_INFO_MODULE)
10989 /* module_info.base_address */
10990 base_addr = bfd_get_32 (abfd, note->descdata + 4);
10991 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
10992 /* module_info.module_name_size */
10993 name_size = bfd_get_32 (abfd, note->descdata + 8);
10995 else /* NOTE_INFO_MODULE64 */
10997 /* module_info.base_address */
10998 base_addr = bfd_get_64 (abfd, note->descdata + 4);
10999 sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
11000 /* module_info.module_name_size */
11001 name_size = bfd_get_32 (abfd, note->descdata + 12);
11004 len = strlen (buf) + 1;
11005 name = (char *) bfd_alloc (abfd, len);
11006 if (name == NULL)
11007 return false;
11009 memcpy (name, buf, len);
11011 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11013 if (sect == NULL)
11014 return false;
11016 if (note->descsz < 12 + name_size)
11018 _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
11019 " is too small to contain a name of size %u"),
11020 abfd, note->descsz, name_size);
11021 return true;
11024 sect->size = note->descsz;
11025 sect->filepos = note->descpos;
11026 sect->alignment_power = 2;
11027 break;
11029 default:
11030 return true;
11033 return true;
11036 static bool
11037 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
11039 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11041 switch (note->type)
11043 default:
11044 return true;
11046 case NT_PRSTATUS:
11047 if (bed->elf_backend_grok_prstatus)
11048 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
11049 return true;
11050 #if defined (HAVE_PRSTATUS_T)
11051 return elfcore_grok_prstatus (abfd, note);
11052 #else
11053 return true;
11054 #endif
11056 #if defined (HAVE_PSTATUS_T)
11057 case NT_PSTATUS:
11058 return elfcore_grok_pstatus (abfd, note);
11059 #endif
11061 #if defined (HAVE_LWPSTATUS_T)
11062 case NT_LWPSTATUS:
11063 return elfcore_grok_lwpstatus (abfd, note);
11064 #endif
11066 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
11067 return elfcore_grok_prfpreg (abfd, note);
11069 case NT_WIN32PSTATUS:
11070 return elfcore_grok_win32pstatus (abfd, note);
11072 case NT_PRXFPREG: /* Linux SSE extension */
11073 if (note->namesz == 6
11074 && strcmp (note->namedata, "LINUX") == 0)
11075 return elfcore_grok_prxfpreg (abfd, note);
11076 else
11077 return true;
11079 case NT_X86_XSTATE: /* Linux XSAVE extension */
11080 if (note->namesz == 6
11081 && strcmp (note->namedata, "LINUX") == 0)
11082 return elfcore_grok_xstatereg (abfd, note);
11083 else
11084 return true;
11086 case NT_PPC_VMX:
11087 if (note->namesz == 6
11088 && strcmp (note->namedata, "LINUX") == 0)
11089 return elfcore_grok_ppc_vmx (abfd, note);
11090 else
11091 return true;
11093 case NT_PPC_VSX:
11094 if (note->namesz == 6
11095 && strcmp (note->namedata, "LINUX") == 0)
11096 return elfcore_grok_ppc_vsx (abfd, note);
11097 else
11098 return true;
11100 case NT_PPC_TAR:
11101 if (note->namesz == 6
11102 && strcmp (note->namedata, "LINUX") == 0)
11103 return elfcore_grok_ppc_tar (abfd, note);
11104 else
11105 return true;
11107 case NT_PPC_PPR:
11108 if (note->namesz == 6
11109 && strcmp (note->namedata, "LINUX") == 0)
11110 return elfcore_grok_ppc_ppr (abfd, note);
11111 else
11112 return true;
11114 case NT_PPC_DSCR:
11115 if (note->namesz == 6
11116 && strcmp (note->namedata, "LINUX") == 0)
11117 return elfcore_grok_ppc_dscr (abfd, note);
11118 else
11119 return true;
11121 case NT_PPC_EBB:
11122 if (note->namesz == 6
11123 && strcmp (note->namedata, "LINUX") == 0)
11124 return elfcore_grok_ppc_ebb (abfd, note);
11125 else
11126 return true;
11128 case NT_PPC_PMU:
11129 if (note->namesz == 6
11130 && strcmp (note->namedata, "LINUX") == 0)
11131 return elfcore_grok_ppc_pmu (abfd, note);
11132 else
11133 return true;
11135 case NT_PPC_TM_CGPR:
11136 if (note->namesz == 6
11137 && strcmp (note->namedata, "LINUX") == 0)
11138 return elfcore_grok_ppc_tm_cgpr (abfd, note);
11139 else
11140 return true;
11142 case NT_PPC_TM_CFPR:
11143 if (note->namesz == 6
11144 && strcmp (note->namedata, "LINUX") == 0)
11145 return elfcore_grok_ppc_tm_cfpr (abfd, note);
11146 else
11147 return true;
11149 case NT_PPC_TM_CVMX:
11150 if (note->namesz == 6
11151 && strcmp (note->namedata, "LINUX") == 0)
11152 return elfcore_grok_ppc_tm_cvmx (abfd, note);
11153 else
11154 return true;
11156 case NT_PPC_TM_CVSX:
11157 if (note->namesz == 6
11158 && strcmp (note->namedata, "LINUX") == 0)
11159 return elfcore_grok_ppc_tm_cvsx (abfd, note);
11160 else
11161 return true;
11163 case NT_PPC_TM_SPR:
11164 if (note->namesz == 6
11165 && strcmp (note->namedata, "LINUX") == 0)
11166 return elfcore_grok_ppc_tm_spr (abfd, note);
11167 else
11168 return true;
11170 case NT_PPC_TM_CTAR:
11171 if (note->namesz == 6
11172 && strcmp (note->namedata, "LINUX") == 0)
11173 return elfcore_grok_ppc_tm_ctar (abfd, note);
11174 else
11175 return true;
11177 case NT_PPC_TM_CPPR:
11178 if (note->namesz == 6
11179 && strcmp (note->namedata, "LINUX") == 0)
11180 return elfcore_grok_ppc_tm_cppr (abfd, note);
11181 else
11182 return true;
11184 case NT_PPC_TM_CDSCR:
11185 if (note->namesz == 6
11186 && strcmp (note->namedata, "LINUX") == 0)
11187 return elfcore_grok_ppc_tm_cdscr (abfd, note);
11188 else
11189 return true;
11191 case NT_S390_HIGH_GPRS:
11192 if (note->namesz == 6
11193 && strcmp (note->namedata, "LINUX") == 0)
11194 return elfcore_grok_s390_high_gprs (abfd, note);
11195 else
11196 return true;
11198 case NT_S390_TIMER:
11199 if (note->namesz == 6
11200 && strcmp (note->namedata, "LINUX") == 0)
11201 return elfcore_grok_s390_timer (abfd, note);
11202 else
11203 return true;
11205 case NT_S390_TODCMP:
11206 if (note->namesz == 6
11207 && strcmp (note->namedata, "LINUX") == 0)
11208 return elfcore_grok_s390_todcmp (abfd, note);
11209 else
11210 return true;
11212 case NT_S390_TODPREG:
11213 if (note->namesz == 6
11214 && strcmp (note->namedata, "LINUX") == 0)
11215 return elfcore_grok_s390_todpreg (abfd, note);
11216 else
11217 return true;
11219 case NT_S390_CTRS:
11220 if (note->namesz == 6
11221 && strcmp (note->namedata, "LINUX") == 0)
11222 return elfcore_grok_s390_ctrs (abfd, note);
11223 else
11224 return true;
11226 case NT_S390_PREFIX:
11227 if (note->namesz == 6
11228 && strcmp (note->namedata, "LINUX") == 0)
11229 return elfcore_grok_s390_prefix (abfd, note);
11230 else
11231 return true;
11233 case NT_S390_LAST_BREAK:
11234 if (note->namesz == 6
11235 && strcmp (note->namedata, "LINUX") == 0)
11236 return elfcore_grok_s390_last_break (abfd, note);
11237 else
11238 return true;
11240 case NT_S390_SYSTEM_CALL:
11241 if (note->namesz == 6
11242 && strcmp (note->namedata, "LINUX") == 0)
11243 return elfcore_grok_s390_system_call (abfd, note);
11244 else
11245 return true;
11247 case NT_S390_TDB:
11248 if (note->namesz == 6
11249 && strcmp (note->namedata, "LINUX") == 0)
11250 return elfcore_grok_s390_tdb (abfd, note);
11251 else
11252 return true;
11254 case NT_S390_VXRS_LOW:
11255 if (note->namesz == 6
11256 && strcmp (note->namedata, "LINUX") == 0)
11257 return elfcore_grok_s390_vxrs_low (abfd, note);
11258 else
11259 return true;
11261 case NT_S390_VXRS_HIGH:
11262 if (note->namesz == 6
11263 && strcmp (note->namedata, "LINUX") == 0)
11264 return elfcore_grok_s390_vxrs_high (abfd, note);
11265 else
11266 return true;
11268 case NT_S390_GS_CB:
11269 if (note->namesz == 6
11270 && strcmp (note->namedata, "LINUX") == 0)
11271 return elfcore_grok_s390_gs_cb (abfd, note);
11272 else
11273 return true;
11275 case NT_S390_GS_BC:
11276 if (note->namesz == 6
11277 && strcmp (note->namedata, "LINUX") == 0)
11278 return elfcore_grok_s390_gs_bc (abfd, note);
11279 else
11280 return true;
11282 case NT_ARC_V2:
11283 if (note->namesz == 6
11284 && strcmp (note->namedata, "LINUX") == 0)
11285 return elfcore_grok_arc_v2 (abfd, note);
11286 else
11287 return true;
11289 case NT_ARM_VFP:
11290 if (note->namesz == 6
11291 && strcmp (note->namedata, "LINUX") == 0)
11292 return elfcore_grok_arm_vfp (abfd, note);
11293 else
11294 return true;
11296 case NT_ARM_TLS:
11297 if (note->namesz == 6
11298 && strcmp (note->namedata, "LINUX") == 0)
11299 return elfcore_grok_aarch_tls (abfd, note);
11300 else
11301 return true;
11303 case NT_ARM_HW_BREAK:
11304 if (note->namesz == 6
11305 && strcmp (note->namedata, "LINUX") == 0)
11306 return elfcore_grok_aarch_hw_break (abfd, note);
11307 else
11308 return true;
11310 case NT_ARM_HW_WATCH:
11311 if (note->namesz == 6
11312 && strcmp (note->namedata, "LINUX") == 0)
11313 return elfcore_grok_aarch_hw_watch (abfd, note);
11314 else
11315 return true;
11317 case NT_ARM_SVE:
11318 if (note->namesz == 6
11319 && strcmp (note->namedata, "LINUX") == 0)
11320 return elfcore_grok_aarch_sve (abfd, note);
11321 else
11322 return true;
11324 case NT_ARM_PAC_MASK:
11325 if (note->namesz == 6
11326 && strcmp (note->namedata, "LINUX") == 0)
11327 return elfcore_grok_aarch_pauth (abfd, note);
11328 else
11329 return true;
11331 case NT_ARM_TAGGED_ADDR_CTRL:
11332 if (note->namesz == 6
11333 && strcmp (note->namedata, "LINUX") == 0)
11334 return elfcore_grok_aarch_mte (abfd, note);
11335 else
11336 return true;
11338 case NT_ARM_SSVE:
11339 if (note->namesz == 6
11340 && strcmp (note->namedata, "LINUX") == 0)
11341 return elfcore_grok_aarch_ssve (abfd, note);
11342 else
11343 return true;
11345 case NT_ARM_ZA:
11346 if (note->namesz == 6
11347 && strcmp (note->namedata, "LINUX") == 0)
11348 return elfcore_grok_aarch_za (abfd, note);
11349 else
11350 return true;
11352 case NT_ARM_ZT:
11353 if (note->namesz == 6
11354 && strcmp (note->namedata, "LINUX") == 0)
11355 return elfcore_grok_aarch_zt (abfd, note);
11356 else
11357 return true;
11359 case NT_GDB_TDESC:
11360 if (note->namesz == 4
11361 && strcmp (note->namedata, "GDB") == 0)
11362 return elfcore_grok_gdb_tdesc (abfd, note);
11363 else
11364 return true;
11366 case NT_RISCV_CSR:
11367 if (note->namesz == 4
11368 && strcmp (note->namedata, "GDB") == 0)
11369 return elfcore_grok_riscv_csr (abfd, note);
11370 else
11371 return true;
11373 case NT_LARCH_CPUCFG:
11374 if (note->namesz == 6
11375 && strcmp (note->namedata, "LINUX") == 0)
11376 return elfcore_grok_loongarch_cpucfg (abfd, note);
11377 else
11378 return true;
11380 case NT_LARCH_LBT:
11381 if (note->namesz == 6
11382 && strcmp (note->namedata, "LINUX") == 0)
11383 return elfcore_grok_loongarch_lbt (abfd, note);
11384 else
11385 return true;
11387 case NT_LARCH_LSX:
11388 if (note->namesz == 6
11389 && strcmp (note->namedata, "LINUX") == 0)
11390 return elfcore_grok_loongarch_lsx (abfd, note);
11391 else
11392 return true;
11394 case NT_LARCH_LASX:
11395 if (note->namesz == 6
11396 && strcmp (note->namedata, "LINUX") == 0)
11397 return elfcore_grok_loongarch_lasx (abfd, note);
11398 else
11399 return true;
11401 case NT_PRPSINFO:
11402 case NT_PSINFO:
11403 if (bed->elf_backend_grok_psinfo)
11404 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
11405 return true;
11406 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11407 return elfcore_grok_psinfo (abfd, note);
11408 #else
11409 return true;
11410 #endif
11412 case NT_AUXV:
11413 return elfcore_make_auxv_note_section (abfd, note, 0);
11415 case NT_FILE:
11416 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
11417 note);
11419 case NT_SIGINFO:
11420 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
11421 note);
11426 static bool
11427 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
11429 struct bfd_build_id* build_id;
11431 if (note->descsz == 0)
11432 return false;
11434 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
11435 if (build_id == NULL)
11436 return false;
11438 build_id->size = note->descsz;
11439 memcpy (build_id->data, note->descdata, note->descsz);
11440 abfd->build_id = build_id;
11442 return true;
11445 static bool
11446 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
11448 switch (note->type)
11450 default:
11451 return true;
11453 case NT_GNU_PROPERTY_TYPE_0:
11454 return _bfd_elf_parse_gnu_properties (abfd, note);
11456 case NT_GNU_BUILD_ID:
11457 return elfobj_grok_gnu_build_id (abfd, note);
11461 static bool
11462 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
11464 struct sdt_note *cur =
11465 (struct sdt_note *) bfd_alloc (abfd,
11466 sizeof (struct sdt_note) + note->descsz);
11468 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
11469 cur->size = (bfd_size_type) note->descsz;
11470 memcpy (cur->data, note->descdata, note->descsz);
11472 elf_tdata (abfd)->sdt_note_head = cur;
11474 return true;
11477 static bool
11478 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
11480 switch (note->type)
11482 case NT_STAPSDT:
11483 return elfobj_grok_stapsdt_note_1 (abfd, note);
11485 default:
11486 return true;
11490 static bool
11491 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
11493 size_t offset;
11495 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11497 case ELFCLASS32:
11498 if (note->descsz < 108)
11499 return false;
11500 break;
11502 case ELFCLASS64:
11503 if (note->descsz < 120)
11504 return false;
11505 break;
11507 default:
11508 return false;
11511 /* Check for version 1 in pr_version. */
11512 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11513 return false;
11515 offset = 4;
11517 /* Skip over pr_psinfosz. */
11518 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11519 offset += 4;
11520 else
11522 offset += 4; /* Padding before pr_psinfosz. */
11523 offset += 8;
11526 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
11527 elf_tdata (abfd)->core->program
11528 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
11529 offset += 17;
11531 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
11532 elf_tdata (abfd)->core->command
11533 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
11534 offset += 81;
11536 /* Padding before pr_pid. */
11537 offset += 2;
11539 /* The pr_pid field was added in version "1a". */
11540 if (note->descsz < offset + 4)
11541 return true;
11543 elf_tdata (abfd)->core->pid
11544 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11546 return true;
11549 static bool
11550 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
11552 size_t offset;
11553 size_t size;
11554 size_t min_size;
11556 /* Compute offset of pr_getregsz, skipping over pr_statussz.
11557 Also compute minimum size of this note. */
11558 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11560 case ELFCLASS32:
11561 offset = 4 + 4;
11562 min_size = offset + (4 * 2) + 4 + 4 + 4;
11563 break;
11565 case ELFCLASS64:
11566 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
11567 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
11568 break;
11570 default:
11571 return false;
11574 if (note->descsz < min_size)
11575 return false;
11577 /* Check for version 1 in pr_version. */
11578 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11579 return false;
11581 /* Extract size of pr_reg from pr_gregsetsz. */
11582 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
11583 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11585 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11586 offset += 4 * 2;
11588 else
11590 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
11591 offset += 8 * 2;
11594 /* Skip over pr_osreldate. */
11595 offset += 4;
11597 /* Read signal from pr_cursig. */
11598 if (elf_tdata (abfd)->core->signal == 0)
11599 elf_tdata (abfd)->core->signal
11600 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11601 offset += 4;
11603 /* Read TID from pr_pid. */
11604 elf_tdata (abfd)->core->lwpid
11605 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11606 offset += 4;
11608 /* Padding before pr_reg. */
11609 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
11610 offset += 4;
11612 /* Make sure that there is enough data remaining in the note. */
11613 if ((note->descsz - offset) < size)
11614 return false;
11616 /* Make a ".reg/999" section and a ".reg" section. */
11617 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
11618 size, note->descpos + offset);
11621 static bool
11622 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
11624 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11626 switch (note->type)
11628 case NT_PRSTATUS:
11629 if (bed->elf_backend_grok_freebsd_prstatus)
11630 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
11631 return true;
11632 return elfcore_grok_freebsd_prstatus (abfd, note);
11634 case NT_FPREGSET:
11635 return elfcore_grok_prfpreg (abfd, note);
11637 case NT_PRPSINFO:
11638 return elfcore_grok_freebsd_psinfo (abfd, note);
11640 case NT_FREEBSD_THRMISC:
11641 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
11643 case NT_FREEBSD_PROCSTAT_PROC:
11644 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
11645 note);
11647 case NT_FREEBSD_PROCSTAT_FILES:
11648 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
11649 note);
11651 case NT_FREEBSD_PROCSTAT_VMMAP:
11652 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
11653 note);
11655 case NT_FREEBSD_PROCSTAT_AUXV:
11656 return elfcore_make_auxv_note_section (abfd, note, 4);
11658 case NT_FREEBSD_X86_SEGBASES:
11659 return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note);
11661 case NT_X86_XSTATE:
11662 return elfcore_grok_xstatereg (abfd, note);
11664 case NT_FREEBSD_PTLWPINFO:
11665 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
11666 note);
11668 case NT_ARM_TLS:
11669 return elfcore_grok_aarch_tls (abfd, note);
11671 case NT_ARM_VFP:
11672 return elfcore_grok_arm_vfp (abfd, note);
11674 default:
11675 return true;
11679 static bool
11680 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
11682 char *cp;
11684 cp = strchr (note->namedata, '@');
11685 if (cp != NULL)
11687 *lwpidp = atoi(cp + 1);
11688 return true;
11690 return false;
11693 static bool
11694 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11696 if (note->descsz <= 0x7c + 31)
11697 return false;
11699 /* Signal number at offset 0x08. */
11700 elf_tdata (abfd)->core->signal
11701 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11703 /* Process ID at offset 0x50. */
11704 elf_tdata (abfd)->core->pid
11705 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
11707 /* Command name at 0x7c (max 32 bytes, including nul). */
11708 elf_tdata (abfd)->core->command
11709 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
11711 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
11712 note);
11715 static bool
11716 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
11718 int lwp;
11720 if (elfcore_netbsd_get_lwpid (note, &lwp))
11721 elf_tdata (abfd)->core->lwpid = lwp;
11723 switch (note->type)
11725 case NT_NETBSDCORE_PROCINFO:
11726 /* NetBSD-specific core "procinfo". Note that we expect to
11727 find this note before any of the others, which is fine,
11728 since the kernel writes this note out first when it
11729 creates a core file. */
11730 return elfcore_grok_netbsd_procinfo (abfd, note);
11731 case NT_NETBSDCORE_AUXV:
11732 /* NetBSD-specific Elf Auxiliary Vector data. */
11733 return elfcore_make_auxv_note_section (abfd, note, 4);
11734 case NT_NETBSDCORE_LWPSTATUS:
11735 return elfcore_make_note_pseudosection (abfd,
11736 ".note.netbsdcore.lwpstatus",
11737 note);
11738 default:
11739 break;
11742 /* As of March 2020 there are no other machine-independent notes
11743 defined for NetBSD core files. If the note type is less
11744 than the start of the machine-dependent note types, we don't
11745 understand it. */
11747 if (note->type < NT_NETBSDCORE_FIRSTMACH)
11748 return true;
11751 switch (bfd_get_arch (abfd))
11753 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11754 PT_GETFPREGS == mach+2. */
11756 case bfd_arch_aarch64:
11757 case bfd_arch_alpha:
11758 case bfd_arch_sparc:
11759 switch (note->type)
11761 case NT_NETBSDCORE_FIRSTMACH+0:
11762 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11764 case NT_NETBSDCORE_FIRSTMACH+2:
11765 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11767 default:
11768 return true;
11771 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11772 There's also old PT___GETREGS40 == mach + 1 for old reg
11773 structure which lacks GBR. */
11775 case bfd_arch_sh:
11776 switch (note->type)
11778 case NT_NETBSDCORE_FIRSTMACH+3:
11779 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11781 case NT_NETBSDCORE_FIRSTMACH+5:
11782 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11784 default:
11785 return true;
11788 /* On all other arch's, PT_GETREGS == mach+1 and
11789 PT_GETFPREGS == mach+3. */
11791 default:
11792 switch (note->type)
11794 case NT_NETBSDCORE_FIRSTMACH+1:
11795 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11797 case NT_NETBSDCORE_FIRSTMACH+3:
11798 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11800 default:
11801 return true;
11804 /* NOTREACHED */
11807 static bool
11808 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11810 if (note->descsz <= 0x48 + 31)
11811 return false;
11813 /* Signal number at offset 0x08. */
11814 elf_tdata (abfd)->core->signal
11815 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11817 /* Process ID at offset 0x20. */
11818 elf_tdata (abfd)->core->pid
11819 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
11821 /* Command name at 0x48 (max 32 bytes, including nul). */
11822 elf_tdata (abfd)->core->command
11823 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11825 return true;
11828 /* Processes Solaris's process status note.
11829 sig_off ~ offsetof(prstatus_t, pr_cursig)
11830 pid_off ~ offsetof(prstatus_t, pr_pid)
11831 lwpid_off ~ offsetof(prstatus_t, pr_who)
11832 gregset_size ~ sizeof(gregset_t)
11833 gregset_offset ~ offsetof(prstatus_t, pr_reg) */
11835 static bool
11836 elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
11837 int pid_off, int lwpid_off, size_t gregset_size,
11838 size_t gregset_offset)
11840 asection *sect = NULL;
11841 elf_tdata (abfd)->core->signal
11842 = bfd_get_16 (abfd, note->descdata + sig_off);
11843 elf_tdata (abfd)->core->pid
11844 = bfd_get_32 (abfd, note->descdata + pid_off);
11845 elf_tdata (abfd)->core->lwpid
11846 = bfd_get_32 (abfd, note->descdata + lwpid_off);
11848 sect = bfd_get_section_by_name (abfd, ".reg");
11849 if (sect != NULL)
11850 sect->size = gregset_size;
11852 return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11853 note->descpos + gregset_offset);
11856 /* Gets program and arguments from a core.
11857 prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
11858 comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs) */
11860 static bool
11861 elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
11862 int prog_off, int comm_off)
11864 elf_tdata (abfd)->core->program
11865 = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
11866 elf_tdata (abfd)->core->command
11867 = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
11869 return true;
11872 /* Processes Solaris's LWP status note.
11873 gregset_size ~ sizeof(gregset_t)
11874 gregset_off ~ offsetof(lwpstatus_t, pr_reg)
11875 fpregset_size ~ sizeof(fpregset_t)
11876 fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg) */
11878 static bool
11879 elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
11880 size_t gregset_size, int gregset_off,
11881 size_t fpregset_size, int fpregset_off)
11883 asection *sect = NULL;
11884 char reg2_section_name[16] = { 0 };
11886 (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2",
11887 elf_tdata (abfd)->core->lwpid);
11889 /* offsetof(lwpstatus_t, pr_lwpid) */
11890 elf_tdata (abfd)->core->lwpid
11891 = bfd_get_32 (abfd, note->descdata + 4);
11892 /* offsetof(lwpstatus_t, pr_cursig) */
11893 elf_tdata (abfd)->core->signal
11894 = bfd_get_16 (abfd, note->descdata + 12);
11896 sect = bfd_get_section_by_name (abfd, ".reg");
11897 if (sect != NULL)
11898 sect->size = gregset_size;
11899 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11900 note->descpos + gregset_off))
11901 return false;
11903 sect = bfd_get_section_by_name (abfd, reg2_section_name);
11904 if (sect != NULL)
11906 sect->size = fpregset_size;
11907 sect->filepos = note->descpos + fpregset_off;
11908 sect->alignment_power = 2;
11910 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size,
11911 note->descpos + fpregset_off))
11912 return false;
11914 return true;
11917 static bool
11918 elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
11920 if (note == NULL)
11921 return false;
11923 /* core files are identified as 32- or 64-bit, SPARC or x86,
11924 by the size of the descsz which matches the sizeof()
11925 the type appropriate for that note type (e.g., prstatus_t for
11926 SOLARIS_NT_PRSTATUS) for the corresponding architecture
11927 on Solaris. The core file bitness may differ from the bitness of
11928 gdb itself, so fixed values are used instead of sizeof().
11929 Appropriate fixed offsets are also used to obtain data from
11930 the note. */
11932 switch ((int) note->type)
11934 case SOLARIS_NT_PRSTATUS:
11935 switch (note->descsz)
11937 case 508: /* sizeof(prstatus_t) SPARC 32-bit */
11938 return elfcore_grok_solaris_prstatus(abfd, note,
11939 136, 216, 308, 152, 356);
11940 case 904: /* sizeof(prstatus_t) SPARC 64-bit */
11941 return elfcore_grok_solaris_prstatus(abfd, note,
11942 264, 360, 520, 304, 600);
11943 case 432: /* sizeof(prstatus_t) Intel 32-bit */
11944 return elfcore_grok_solaris_prstatus(abfd, note,
11945 136, 216, 308, 76, 356);
11946 case 824: /* sizeof(prstatus_t) Intel 64-bit */
11947 return elfcore_grok_solaris_prstatus(abfd, note,
11948 264, 360, 520, 224, 600);
11949 default:
11950 return true;
11953 case SOLARIS_NT_PSINFO:
11954 case SOLARIS_NT_PRPSINFO:
11955 switch (note->descsz)
11957 case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
11958 return elfcore_grok_solaris_info(abfd, note, 84, 100);
11959 case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
11960 return elfcore_grok_solaris_info(abfd, note, 120, 136);
11961 case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
11962 return elfcore_grok_solaris_info(abfd, note, 88, 104);
11963 case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
11964 return elfcore_grok_solaris_info(abfd, note, 136, 152);
11965 default:
11966 return true;
11969 case SOLARIS_NT_LWPSTATUS:
11970 switch (note->descsz)
11972 case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
11973 return elfcore_grok_solaris_lwpstatus(abfd, note,
11974 152, 344, 400, 496);
11975 case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
11976 return elfcore_grok_solaris_lwpstatus(abfd, note,
11977 304, 544, 544, 848);
11978 case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
11979 return elfcore_grok_solaris_lwpstatus(abfd, note,
11980 76, 344, 380, 420);
11981 case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
11982 return elfcore_grok_solaris_lwpstatus(abfd, note,
11983 224, 544, 528, 768);
11984 default:
11985 return true;
11988 case SOLARIS_NT_LWPSINFO:
11989 /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
11990 if (note->descsz == 128 || note->descsz == 152)
11991 elf_tdata (abfd)->core->lwpid =
11992 bfd_get_32 (abfd, note->descdata + 4);
11993 break;
11995 default:
11996 break;
11999 return true;
12002 /* For name starting with "CORE" this may be either a Solaris
12003 core file or a gdb-generated core file. Do Solaris-specific
12004 processing on selected note types first with
12005 elfcore_grok_solaris_note(), then process the note
12006 in elfcore_grok_note(). */
12008 static bool
12009 elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
12011 if (!elfcore_grok_solaris_note_impl (abfd, note))
12012 return false;
12014 return elfcore_grok_note (abfd, note);
12017 static bool
12018 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
12020 if (note->type == NT_OPENBSD_PROCINFO)
12021 return elfcore_grok_openbsd_procinfo (abfd, note);
12023 if (note->type == NT_OPENBSD_REGS)
12024 return elfcore_make_note_pseudosection (abfd, ".reg", note);
12026 if (note->type == NT_OPENBSD_FPREGS)
12027 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
12029 if (note->type == NT_OPENBSD_XFPREGS)
12030 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
12032 if (note->type == NT_OPENBSD_AUXV)
12033 return elfcore_make_auxv_note_section (abfd, note, 0);
12035 if (note->type == NT_OPENBSD_WCOOKIE)
12037 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
12038 SEC_HAS_CONTENTS);
12040 if (sect == NULL)
12041 return false;
12042 sect->size = note->descsz;
12043 sect->filepos = note->descpos;
12044 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
12046 return true;
12049 return true;
12052 static bool
12053 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
12055 void *ddata = note->descdata;
12056 char buf[100];
12057 char *name;
12058 asection *sect;
12059 short sig;
12060 unsigned flags;
12062 if (note->descsz < 16)
12063 return false;
12065 /* nto_procfs_status 'pid' field is at offset 0. */
12066 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
12068 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
12069 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
12071 /* nto_procfs_status 'flags' field is at offset 8. */
12072 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
12074 /* nto_procfs_status 'what' field is at offset 14. */
12075 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
12077 elf_tdata (abfd)->core->signal = sig;
12078 elf_tdata (abfd)->core->lwpid = *tid;
12081 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
12082 do not come from signals so we make sure we set the current
12083 thread just in case. */
12084 if (flags & 0x00000080)
12085 elf_tdata (abfd)->core->lwpid = *tid;
12087 /* Make a ".qnx_core_status/%d" section. */
12088 sprintf (buf, ".qnx_core_status/%ld", *tid);
12090 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
12091 if (name == NULL)
12092 return false;
12093 strcpy (name, buf);
12095 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12096 if (sect == NULL)
12097 return false;
12099 sect->size = note->descsz;
12100 sect->filepos = note->descpos;
12101 sect->alignment_power = 2;
12103 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
12106 static bool
12107 elfcore_grok_nto_regs (bfd *abfd,
12108 Elf_Internal_Note *note,
12109 long tid,
12110 char *base)
12112 char buf[100];
12113 char *name;
12114 asection *sect;
12116 /* Make a "(base)/%d" section. */
12117 sprintf (buf, "%s/%ld", base, tid);
12119 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
12120 if (name == NULL)
12121 return false;
12122 strcpy (name, buf);
12124 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12125 if (sect == NULL)
12126 return false;
12128 sect->size = note->descsz;
12129 sect->filepos = note->descpos;
12130 sect->alignment_power = 2;
12132 /* This is the current thread. */
12133 if (elf_tdata (abfd)->core->lwpid == tid)
12134 return elfcore_maybe_make_sect (abfd, base, sect);
12136 return true;
12139 static bool
12140 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
12142 /* Every GREG section has a STATUS section before it. Store the
12143 tid from the previous call to pass down to the next gregs
12144 function. */
12145 static long tid = 1;
12147 switch (note->type)
12149 case QNT_CORE_INFO:
12150 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
12151 case QNT_CORE_STATUS:
12152 return elfcore_grok_nto_status (abfd, note, &tid);
12153 case QNT_CORE_GREG:
12154 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
12155 case QNT_CORE_FPREG:
12156 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
12157 default:
12158 return true;
12162 static bool
12163 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
12165 char *name;
12166 asection *sect;
12167 size_t len;
12169 /* Use note name as section name. */
12170 len = note->namesz;
12171 name = (char *) bfd_alloc (abfd, len);
12172 if (name == NULL)
12173 return false;
12174 memcpy (name, note->namedata, len);
12175 name[len - 1] = '\0';
12177 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12178 if (sect == NULL)
12179 return false;
12181 sect->size = note->descsz;
12182 sect->filepos = note->descpos;
12183 sect->alignment_power = 1;
12185 return true;
12188 /* Function: elfcore_write_note
12190 Inputs:
12191 buffer to hold note, and current size of buffer
12192 name of note
12193 type of note
12194 data for note
12195 size of data for note
12197 Writes note to end of buffer. ELF64 notes are written exactly as
12198 for ELF32, despite the current (as of 2006) ELF gabi specifying
12199 that they ought to have 8-byte namesz and descsz field, and have
12200 8-byte alignment. Other writers, eg. Linux kernel, do the same.
12202 Return:
12203 Pointer to realloc'd buffer, *BUFSIZ updated. */
12205 char *
12206 elfcore_write_note (bfd *abfd,
12207 char *buf,
12208 int *bufsiz,
12209 const char *name,
12210 int type,
12211 const void *input,
12212 int size)
12214 Elf_External_Note *xnp;
12215 size_t namesz;
12216 size_t newspace;
12217 char *dest;
12219 namesz = 0;
12220 if (name != NULL)
12221 namesz = strlen (name) + 1;
12223 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
12225 buf = (char *) realloc (buf, *bufsiz + newspace);
12226 if (buf == NULL)
12227 return buf;
12228 dest = buf + *bufsiz;
12229 *bufsiz += newspace;
12230 xnp = (Elf_External_Note *) dest;
12231 H_PUT_32 (abfd, namesz, xnp->namesz);
12232 H_PUT_32 (abfd, size, xnp->descsz);
12233 H_PUT_32 (abfd, type, xnp->type);
12234 dest = xnp->name;
12235 if (name != NULL)
12237 memcpy (dest, name, namesz);
12238 dest += namesz;
12239 while (namesz & 3)
12241 *dest++ = '\0';
12242 ++namesz;
12245 memcpy (dest, input, size);
12246 dest += size;
12247 while (size & 3)
12249 *dest++ = '\0';
12250 ++size;
12252 return buf;
12255 /* gcc-8 warns (*) on all the strncpy calls in this function about
12256 possible string truncation. The "truncation" is not a bug. We
12257 have an external representation of structs with fields that are not
12258 necessarily NULL terminated and corresponding internal
12259 representation fields that are one larger so that they can always
12260 be NULL terminated.
12261 gcc versions between 4.2 and 4.6 do not allow pragma control of
12262 diagnostics inside functions, giving a hard error if you try to use
12263 the finer control available with later versions.
12264 gcc prior to 4.2 warns about diagnostic push and pop.
12265 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
12266 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
12267 (*) Depending on your system header files! */
12268 #if GCC_VERSION >= 8000
12269 # pragma GCC diagnostic push
12270 # pragma GCC diagnostic ignored "-Wstringop-truncation"
12271 #endif
12272 char *
12273 elfcore_write_prpsinfo (bfd *abfd,
12274 char *buf,
12275 int *bufsiz,
12276 const char *fname,
12277 const char *psargs)
12279 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12281 if (bed->elf_backend_write_core_note != NULL)
12283 char *ret;
12284 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12285 NT_PRPSINFO, fname, psargs);
12286 if (ret != NULL)
12287 return ret;
12290 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
12291 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
12292 if (bed->s->elfclass == ELFCLASS32)
12294 # if defined (HAVE_PSINFO32_T)
12295 psinfo32_t data;
12296 int note_type = NT_PSINFO;
12297 # else
12298 prpsinfo32_t data;
12299 int note_type = NT_PRPSINFO;
12300 # endif
12302 memset (&data, 0, sizeof (data));
12303 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12304 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12305 return elfcore_write_note (abfd, buf, bufsiz,
12306 "CORE", note_type, &data, sizeof (data));
12308 else
12309 # endif
12311 # if defined (HAVE_PSINFO_T)
12312 psinfo_t data;
12313 int note_type = NT_PSINFO;
12314 # else
12315 prpsinfo_t data;
12316 int note_type = NT_PRPSINFO;
12317 # endif
12319 memset (&data, 0, sizeof (data));
12320 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12321 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12322 return elfcore_write_note (abfd, buf, bufsiz,
12323 "CORE", note_type, &data, sizeof (data));
12325 #endif /* PSINFO_T or PRPSINFO_T */
12327 free (buf);
12328 return NULL;
12330 #if GCC_VERSION >= 8000
12331 # pragma GCC diagnostic pop
12332 #endif
12334 char *
12335 elfcore_write_linux_prpsinfo32
12336 (bfd *abfd, char *buf, int *bufsiz,
12337 const struct elf_internal_linux_prpsinfo *prpsinfo)
12339 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
12341 struct elf_external_linux_prpsinfo32_ugid16 data;
12343 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
12344 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
12345 &data, sizeof (data));
12347 else
12349 struct elf_external_linux_prpsinfo32_ugid32 data;
12351 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
12352 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
12353 &data, sizeof (data));
12357 char *
12358 elfcore_write_linux_prpsinfo64
12359 (bfd *abfd, char *buf, int *bufsiz,
12360 const struct elf_internal_linux_prpsinfo *prpsinfo)
12362 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
12364 struct elf_external_linux_prpsinfo64_ugid16 data;
12366 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
12367 return elfcore_write_note (abfd, buf, bufsiz,
12368 "CORE", NT_PRPSINFO, &data, sizeof (data));
12370 else
12372 struct elf_external_linux_prpsinfo64_ugid32 data;
12374 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
12375 return elfcore_write_note (abfd, buf, bufsiz,
12376 "CORE", NT_PRPSINFO, &data, sizeof (data));
12380 char *
12381 elfcore_write_prstatus (bfd *abfd,
12382 char *buf,
12383 int *bufsiz,
12384 long pid,
12385 int cursig,
12386 const void *gregs)
12388 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12390 if (bed->elf_backend_write_core_note != NULL)
12392 char *ret;
12393 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12394 NT_PRSTATUS,
12395 pid, cursig, gregs);
12396 if (ret != NULL)
12397 return ret;
12400 #if defined (HAVE_PRSTATUS_T)
12401 #if defined (HAVE_PRSTATUS32_T)
12402 if (bed->s->elfclass == ELFCLASS32)
12404 prstatus32_t prstat;
12406 memset (&prstat, 0, sizeof (prstat));
12407 prstat.pr_pid = pid;
12408 prstat.pr_cursig = cursig;
12409 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12410 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
12411 NT_PRSTATUS, &prstat, sizeof (prstat));
12413 else
12414 #endif
12416 prstatus_t prstat;
12418 memset (&prstat, 0, sizeof (prstat));
12419 prstat.pr_pid = pid;
12420 prstat.pr_cursig = cursig;
12421 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12422 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
12423 NT_PRSTATUS, &prstat, sizeof (prstat));
12425 #endif /* HAVE_PRSTATUS_T */
12427 free (buf);
12428 return NULL;
12431 #if defined (HAVE_LWPSTATUS_T)
12432 char *
12433 elfcore_write_lwpstatus (bfd *abfd,
12434 char *buf,
12435 int *bufsiz,
12436 long pid,
12437 int cursig,
12438 const void *gregs)
12440 lwpstatus_t lwpstat;
12441 const char *note_name = "CORE";
12443 memset (&lwpstat, 0, sizeof (lwpstat));
12444 lwpstat.pr_lwpid = pid >> 16;
12445 lwpstat.pr_cursig = cursig;
12446 #if defined (HAVE_LWPSTATUS_T_PR_REG)
12447 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
12448 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
12449 #if !defined(gregs)
12450 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
12451 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
12452 #else
12453 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
12454 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
12455 #endif
12456 #endif
12457 return elfcore_write_note (abfd, buf, bufsiz, note_name,
12458 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
12460 #endif /* HAVE_LWPSTATUS_T */
12462 #if defined (HAVE_PSTATUS_T)
12463 char *
12464 elfcore_write_pstatus (bfd *abfd,
12465 char *buf,
12466 int *bufsiz,
12467 long pid,
12468 int cursig ATTRIBUTE_UNUSED,
12469 const void *gregs ATTRIBUTE_UNUSED)
12471 const char *note_name = "CORE";
12472 #if defined (HAVE_PSTATUS32_T)
12473 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12475 if (bed->s->elfclass == ELFCLASS32)
12477 pstatus32_t pstat;
12479 memset (&pstat, 0, sizeof (pstat));
12480 pstat.pr_pid = pid & 0xffff;
12481 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
12482 NT_PSTATUS, &pstat, sizeof (pstat));
12483 return buf;
12485 else
12486 #endif
12488 pstatus_t pstat;
12490 memset (&pstat, 0, sizeof (pstat));
12491 pstat.pr_pid = pid & 0xffff;
12492 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
12493 NT_PSTATUS, &pstat, sizeof (pstat));
12494 return buf;
12497 #endif /* HAVE_PSTATUS_T */
12499 char *
12500 elfcore_write_prfpreg (bfd *abfd,
12501 char *buf,
12502 int *bufsiz,
12503 const void *fpregs,
12504 int size)
12506 const char *note_name = "CORE";
12507 return elfcore_write_note (abfd, buf, bufsiz,
12508 note_name, NT_FPREGSET, fpregs, size);
12511 char *
12512 elfcore_write_prxfpreg (bfd *abfd,
12513 char *buf,
12514 int *bufsiz,
12515 const void *xfpregs,
12516 int size)
12518 char *note_name = "LINUX";
12519 return elfcore_write_note (abfd, buf, bufsiz,
12520 note_name, NT_PRXFPREG, xfpregs, size);
12523 char *
12524 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
12525 const void *xfpregs, int size)
12527 char *note_name;
12528 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
12529 note_name = "FreeBSD";
12530 else
12531 note_name = "LINUX";
12532 return elfcore_write_note (abfd, buf, bufsiz,
12533 note_name, NT_X86_XSTATE, xfpregs, size);
12536 char *
12537 elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
12538 const void *regs, int size)
12540 char *note_name = "FreeBSD";
12541 return elfcore_write_note (abfd, buf, bufsiz,
12542 note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
12545 char *
12546 elfcore_write_ppc_vmx (bfd *abfd,
12547 char *buf,
12548 int *bufsiz,
12549 const void *ppc_vmx,
12550 int size)
12552 char *note_name = "LINUX";
12553 return elfcore_write_note (abfd, buf, bufsiz,
12554 note_name, NT_PPC_VMX, ppc_vmx, size);
12557 char *
12558 elfcore_write_ppc_vsx (bfd *abfd,
12559 char *buf,
12560 int *bufsiz,
12561 const void *ppc_vsx,
12562 int size)
12564 char *note_name = "LINUX";
12565 return elfcore_write_note (abfd, buf, bufsiz,
12566 note_name, NT_PPC_VSX, ppc_vsx, size);
12569 char *
12570 elfcore_write_ppc_tar (bfd *abfd,
12571 char *buf,
12572 int *bufsiz,
12573 const void *ppc_tar,
12574 int size)
12576 char *note_name = "LINUX";
12577 return elfcore_write_note (abfd, buf, bufsiz,
12578 note_name, NT_PPC_TAR, ppc_tar, size);
12581 char *
12582 elfcore_write_ppc_ppr (bfd *abfd,
12583 char *buf,
12584 int *bufsiz,
12585 const void *ppc_ppr,
12586 int size)
12588 char *note_name = "LINUX";
12589 return elfcore_write_note (abfd, buf, bufsiz,
12590 note_name, NT_PPC_PPR, ppc_ppr, size);
12593 char *
12594 elfcore_write_ppc_dscr (bfd *abfd,
12595 char *buf,
12596 int *bufsiz,
12597 const void *ppc_dscr,
12598 int size)
12600 char *note_name = "LINUX";
12601 return elfcore_write_note (abfd, buf, bufsiz,
12602 note_name, NT_PPC_DSCR, ppc_dscr, size);
12605 char *
12606 elfcore_write_ppc_ebb (bfd *abfd,
12607 char *buf,
12608 int *bufsiz,
12609 const void *ppc_ebb,
12610 int size)
12612 char *note_name = "LINUX";
12613 return elfcore_write_note (abfd, buf, bufsiz,
12614 note_name, NT_PPC_EBB, ppc_ebb, size);
12617 char *
12618 elfcore_write_ppc_pmu (bfd *abfd,
12619 char *buf,
12620 int *bufsiz,
12621 const void *ppc_pmu,
12622 int size)
12624 char *note_name = "LINUX";
12625 return elfcore_write_note (abfd, buf, bufsiz,
12626 note_name, NT_PPC_PMU, ppc_pmu, size);
12629 char *
12630 elfcore_write_ppc_tm_cgpr (bfd *abfd,
12631 char *buf,
12632 int *bufsiz,
12633 const void *ppc_tm_cgpr,
12634 int size)
12636 char *note_name = "LINUX";
12637 return elfcore_write_note (abfd, buf, bufsiz,
12638 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
12641 char *
12642 elfcore_write_ppc_tm_cfpr (bfd *abfd,
12643 char *buf,
12644 int *bufsiz,
12645 const void *ppc_tm_cfpr,
12646 int size)
12648 char *note_name = "LINUX";
12649 return elfcore_write_note (abfd, buf, bufsiz,
12650 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
12653 char *
12654 elfcore_write_ppc_tm_cvmx (bfd *abfd,
12655 char *buf,
12656 int *bufsiz,
12657 const void *ppc_tm_cvmx,
12658 int size)
12660 char *note_name = "LINUX";
12661 return elfcore_write_note (abfd, buf, bufsiz,
12662 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
12665 char *
12666 elfcore_write_ppc_tm_cvsx (bfd *abfd,
12667 char *buf,
12668 int *bufsiz,
12669 const void *ppc_tm_cvsx,
12670 int size)
12672 char *note_name = "LINUX";
12673 return elfcore_write_note (abfd, buf, bufsiz,
12674 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
12677 char *
12678 elfcore_write_ppc_tm_spr (bfd *abfd,
12679 char *buf,
12680 int *bufsiz,
12681 const void *ppc_tm_spr,
12682 int size)
12684 char *note_name = "LINUX";
12685 return elfcore_write_note (abfd, buf, bufsiz,
12686 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
12689 char *
12690 elfcore_write_ppc_tm_ctar (bfd *abfd,
12691 char *buf,
12692 int *bufsiz,
12693 const void *ppc_tm_ctar,
12694 int size)
12696 char *note_name = "LINUX";
12697 return elfcore_write_note (abfd, buf, bufsiz,
12698 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
12701 char *
12702 elfcore_write_ppc_tm_cppr (bfd *abfd,
12703 char *buf,
12704 int *bufsiz,
12705 const void *ppc_tm_cppr,
12706 int size)
12708 char *note_name = "LINUX";
12709 return elfcore_write_note (abfd, buf, bufsiz,
12710 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
12713 char *
12714 elfcore_write_ppc_tm_cdscr (bfd *abfd,
12715 char *buf,
12716 int *bufsiz,
12717 const void *ppc_tm_cdscr,
12718 int size)
12720 char *note_name = "LINUX";
12721 return elfcore_write_note (abfd, buf, bufsiz,
12722 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
12725 static char *
12726 elfcore_write_s390_high_gprs (bfd *abfd,
12727 char *buf,
12728 int *bufsiz,
12729 const void *s390_high_gprs,
12730 int size)
12732 char *note_name = "LINUX";
12733 return elfcore_write_note (abfd, buf, bufsiz,
12734 note_name, NT_S390_HIGH_GPRS,
12735 s390_high_gprs, size);
12738 char *
12739 elfcore_write_s390_timer (bfd *abfd,
12740 char *buf,
12741 int *bufsiz,
12742 const void *s390_timer,
12743 int size)
12745 char *note_name = "LINUX";
12746 return elfcore_write_note (abfd, buf, bufsiz,
12747 note_name, NT_S390_TIMER, s390_timer, size);
12750 char *
12751 elfcore_write_s390_todcmp (bfd *abfd,
12752 char *buf,
12753 int *bufsiz,
12754 const void *s390_todcmp,
12755 int size)
12757 char *note_name = "LINUX";
12758 return elfcore_write_note (abfd, buf, bufsiz,
12759 note_name, NT_S390_TODCMP, s390_todcmp, size);
12762 char *
12763 elfcore_write_s390_todpreg (bfd *abfd,
12764 char *buf,
12765 int *bufsiz,
12766 const void *s390_todpreg,
12767 int size)
12769 char *note_name = "LINUX";
12770 return elfcore_write_note (abfd, buf, bufsiz,
12771 note_name, NT_S390_TODPREG, s390_todpreg, size);
12774 char *
12775 elfcore_write_s390_ctrs (bfd *abfd,
12776 char *buf,
12777 int *bufsiz,
12778 const void *s390_ctrs,
12779 int size)
12781 char *note_name = "LINUX";
12782 return elfcore_write_note (abfd, buf, bufsiz,
12783 note_name, NT_S390_CTRS, s390_ctrs, size);
12786 char *
12787 elfcore_write_s390_prefix (bfd *abfd,
12788 char *buf,
12789 int *bufsiz,
12790 const void *s390_prefix,
12791 int size)
12793 char *note_name = "LINUX";
12794 return elfcore_write_note (abfd, buf, bufsiz,
12795 note_name, NT_S390_PREFIX, s390_prefix, size);
12798 char *
12799 elfcore_write_s390_last_break (bfd *abfd,
12800 char *buf,
12801 int *bufsiz,
12802 const void *s390_last_break,
12803 int size)
12805 char *note_name = "LINUX";
12806 return elfcore_write_note (abfd, buf, bufsiz,
12807 note_name, NT_S390_LAST_BREAK,
12808 s390_last_break, size);
12811 char *
12812 elfcore_write_s390_system_call (bfd *abfd,
12813 char *buf,
12814 int *bufsiz,
12815 const void *s390_system_call,
12816 int size)
12818 char *note_name = "LINUX";
12819 return elfcore_write_note (abfd, buf, bufsiz,
12820 note_name, NT_S390_SYSTEM_CALL,
12821 s390_system_call, size);
12824 char *
12825 elfcore_write_s390_tdb (bfd *abfd,
12826 char *buf,
12827 int *bufsiz,
12828 const void *s390_tdb,
12829 int size)
12831 char *note_name = "LINUX";
12832 return elfcore_write_note (abfd, buf, bufsiz,
12833 note_name, NT_S390_TDB, s390_tdb, size);
12836 char *
12837 elfcore_write_s390_vxrs_low (bfd *abfd,
12838 char *buf,
12839 int *bufsiz,
12840 const void *s390_vxrs_low,
12841 int size)
12843 char *note_name = "LINUX";
12844 return elfcore_write_note (abfd, buf, bufsiz,
12845 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
12848 char *
12849 elfcore_write_s390_vxrs_high (bfd *abfd,
12850 char *buf,
12851 int *bufsiz,
12852 const void *s390_vxrs_high,
12853 int size)
12855 char *note_name = "LINUX";
12856 return elfcore_write_note (abfd, buf, bufsiz,
12857 note_name, NT_S390_VXRS_HIGH,
12858 s390_vxrs_high, size);
12861 char *
12862 elfcore_write_s390_gs_cb (bfd *abfd,
12863 char *buf,
12864 int *bufsiz,
12865 const void *s390_gs_cb,
12866 int size)
12868 char *note_name = "LINUX";
12869 return elfcore_write_note (abfd, buf, bufsiz,
12870 note_name, NT_S390_GS_CB,
12871 s390_gs_cb, size);
12874 char *
12875 elfcore_write_s390_gs_bc (bfd *abfd,
12876 char *buf,
12877 int *bufsiz,
12878 const void *s390_gs_bc,
12879 int size)
12881 char *note_name = "LINUX";
12882 return elfcore_write_note (abfd, buf, bufsiz,
12883 note_name, NT_S390_GS_BC,
12884 s390_gs_bc, size);
12887 char *
12888 elfcore_write_arm_vfp (bfd *abfd,
12889 char *buf,
12890 int *bufsiz,
12891 const void *arm_vfp,
12892 int size)
12894 char *note_name = "LINUX";
12895 return elfcore_write_note (abfd, buf, bufsiz,
12896 note_name, NT_ARM_VFP, arm_vfp, size);
12899 char *
12900 elfcore_write_aarch_tls (bfd *abfd,
12901 char *buf,
12902 int *bufsiz,
12903 const void *aarch_tls,
12904 int size)
12906 char *note_name = "LINUX";
12907 return elfcore_write_note (abfd, buf, bufsiz,
12908 note_name, NT_ARM_TLS, aarch_tls, size);
12911 char *
12912 elfcore_write_aarch_hw_break (bfd *abfd,
12913 char *buf,
12914 int *bufsiz,
12915 const void *aarch_hw_break,
12916 int size)
12918 char *note_name = "LINUX";
12919 return elfcore_write_note (abfd, buf, bufsiz,
12920 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
12923 char *
12924 elfcore_write_aarch_hw_watch (bfd *abfd,
12925 char *buf,
12926 int *bufsiz,
12927 const void *aarch_hw_watch,
12928 int size)
12930 char *note_name = "LINUX";
12931 return elfcore_write_note (abfd, buf, bufsiz,
12932 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
12935 char *
12936 elfcore_write_aarch_sve (bfd *abfd,
12937 char *buf,
12938 int *bufsiz,
12939 const void *aarch_sve,
12940 int size)
12942 char *note_name = "LINUX";
12943 return elfcore_write_note (abfd, buf, bufsiz,
12944 note_name, NT_ARM_SVE, aarch_sve, size);
12947 char *
12948 elfcore_write_aarch_pauth (bfd *abfd,
12949 char *buf,
12950 int *bufsiz,
12951 const void *aarch_pauth,
12952 int size)
12954 char *note_name = "LINUX";
12955 return elfcore_write_note (abfd, buf, bufsiz,
12956 note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
12959 char *
12960 elfcore_write_aarch_mte (bfd *abfd,
12961 char *buf,
12962 int *bufsiz,
12963 const void *aarch_mte,
12964 int size)
12966 char *note_name = "LINUX";
12967 return elfcore_write_note (abfd, buf, bufsiz,
12968 note_name, NT_ARM_TAGGED_ADDR_CTRL,
12969 aarch_mte,
12970 size);
12973 char *
12974 elfcore_write_aarch_ssve (bfd *abfd,
12975 char *buf,
12976 int *bufsiz,
12977 const void *aarch_ssve,
12978 int size)
12980 char *note_name = "LINUX";
12981 return elfcore_write_note (abfd, buf, bufsiz,
12982 note_name, NT_ARM_SSVE,
12983 aarch_ssve,
12984 size);
12987 char *
12988 elfcore_write_aarch_za (bfd *abfd,
12989 char *buf,
12990 int *bufsiz,
12991 const void *aarch_za,
12992 int size)
12994 char *note_name = "LINUX";
12995 return elfcore_write_note (abfd, buf, bufsiz,
12996 note_name, NT_ARM_ZA,
12997 aarch_za,
12998 size);
13001 /* Write the buffer of zt register values in aarch_zt (length SIZE) into
13002 the note buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
13003 written into. Return a pointer to the new start of the note buffer, to
13004 replace BUF which may no longer be valid. */
13006 char *
13007 elfcore_write_aarch_zt (bfd *abfd,
13008 char *buf,
13009 int *bufsiz,
13010 const void *aarch_zt,
13011 int size)
13013 char *note_name = "LINUX";
13014 return elfcore_write_note (abfd, buf, bufsiz,
13015 note_name, NT_ARM_ZT,
13016 aarch_zt,
13017 size);
13020 char *
13021 elfcore_write_arc_v2 (bfd *abfd,
13022 char *buf,
13023 int *bufsiz,
13024 const void *arc_v2,
13025 int size)
13027 char *note_name = "LINUX";
13028 return elfcore_write_note (abfd, buf, bufsiz,
13029 note_name, NT_ARC_V2, arc_v2, size);
13032 char *
13033 elfcore_write_loongarch_cpucfg (bfd *abfd,
13034 char *buf,
13035 int *bufsiz,
13036 const void *loongarch_cpucfg,
13037 int size)
13039 char *note_name = "LINUX";
13040 return elfcore_write_note (abfd, buf, bufsiz,
13041 note_name, NT_LARCH_CPUCFG,
13042 loongarch_cpucfg, size);
13045 char *
13046 elfcore_write_loongarch_lbt (bfd *abfd,
13047 char *buf,
13048 int *bufsiz,
13049 const void *loongarch_lbt,
13050 int size)
13052 char *note_name = "LINUX";
13053 return elfcore_write_note (abfd, buf, bufsiz,
13054 note_name, NT_LARCH_LBT, loongarch_lbt, size);
13057 char *
13058 elfcore_write_loongarch_lsx (bfd *abfd,
13059 char *buf,
13060 int *bufsiz,
13061 const void *loongarch_lsx,
13062 int size)
13064 char *note_name = "LINUX";
13065 return elfcore_write_note (abfd, buf, bufsiz,
13066 note_name, NT_LARCH_LSX, loongarch_lsx, size);
13069 char *
13070 elfcore_write_loongarch_lasx (bfd *abfd,
13071 char *buf,
13072 int *bufsiz,
13073 const void *loongarch_lasx,
13074 int size)
13076 char *note_name = "LINUX";
13077 return elfcore_write_note (abfd, buf, bufsiz,
13078 note_name, NT_LARCH_LASX, loongarch_lasx, size);
13081 /* Write the buffer of csr values in CSRS (length SIZE) into the note
13082 buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
13083 written into. Return a pointer to the new start of the note buffer, to
13084 replace BUF which may no longer be valid. */
13086 char *
13087 elfcore_write_riscv_csr (bfd *abfd,
13088 char *buf,
13089 int *bufsiz,
13090 const void *csrs,
13091 int size)
13093 const char *note_name = "GDB";
13094 return elfcore_write_note (abfd, buf, bufsiz,
13095 note_name, NT_RISCV_CSR, csrs, size);
13098 /* Write the target description (a string) pointed to by TDESC, length
13099 SIZE, into the note buffer BUF, and update *BUFSIZ. ABFD is the bfd the
13100 note is being written into. Return a pointer to the new start of the
13101 note buffer, to replace BUF which may no longer be valid. */
13103 char *
13104 elfcore_write_gdb_tdesc (bfd *abfd,
13105 char *buf,
13106 int *bufsiz,
13107 const void *tdesc,
13108 int size)
13110 const char *note_name = "GDB";
13111 return elfcore_write_note (abfd, buf, bufsiz,
13112 note_name, NT_GDB_TDESC, tdesc, size);
13115 char *
13116 elfcore_write_register_note (bfd *abfd,
13117 char *buf,
13118 int *bufsiz,
13119 const char *section,
13120 const void *data,
13121 int size)
13123 if (strcmp (section, ".reg2") == 0)
13124 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
13125 if (strcmp (section, ".reg-xfp") == 0)
13126 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
13127 if (strcmp (section, ".reg-xstate") == 0)
13128 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
13129 if (strcmp (section, ".reg-x86-segbases") == 0)
13130 return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
13131 if (strcmp (section, ".reg-ppc-vmx") == 0)
13132 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
13133 if (strcmp (section, ".reg-ppc-vsx") == 0)
13134 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
13135 if (strcmp (section, ".reg-ppc-tar") == 0)
13136 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
13137 if (strcmp (section, ".reg-ppc-ppr") == 0)
13138 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
13139 if (strcmp (section, ".reg-ppc-dscr") == 0)
13140 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
13141 if (strcmp (section, ".reg-ppc-ebb") == 0)
13142 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
13143 if (strcmp (section, ".reg-ppc-pmu") == 0)
13144 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
13145 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
13146 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
13147 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
13148 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
13149 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
13150 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
13151 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
13152 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
13153 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
13154 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
13155 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
13156 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
13157 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
13158 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
13159 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
13160 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
13161 if (strcmp (section, ".reg-s390-high-gprs") == 0)
13162 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
13163 if (strcmp (section, ".reg-s390-timer") == 0)
13164 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
13165 if (strcmp (section, ".reg-s390-todcmp") == 0)
13166 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
13167 if (strcmp (section, ".reg-s390-todpreg") == 0)
13168 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
13169 if (strcmp (section, ".reg-s390-ctrs") == 0)
13170 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
13171 if (strcmp (section, ".reg-s390-prefix") == 0)
13172 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
13173 if (strcmp (section, ".reg-s390-last-break") == 0)
13174 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
13175 if (strcmp (section, ".reg-s390-system-call") == 0)
13176 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
13177 if (strcmp (section, ".reg-s390-tdb") == 0)
13178 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
13179 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
13180 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
13181 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
13182 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
13183 if (strcmp (section, ".reg-s390-gs-cb") == 0)
13184 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
13185 if (strcmp (section, ".reg-s390-gs-bc") == 0)
13186 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
13187 if (strcmp (section, ".reg-arm-vfp") == 0)
13188 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
13189 if (strcmp (section, ".reg-aarch-tls") == 0)
13190 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
13191 if (strcmp (section, ".reg-aarch-hw-break") == 0)
13192 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
13193 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
13194 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
13195 if (strcmp (section, ".reg-aarch-sve") == 0)
13196 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
13197 if (strcmp (section, ".reg-aarch-pauth") == 0)
13198 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
13199 if (strcmp (section, ".reg-aarch-mte") == 0)
13200 return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
13201 if (strcmp (section, ".reg-aarch-ssve") == 0)
13202 return elfcore_write_aarch_ssve (abfd, buf, bufsiz, data, size);
13203 if (strcmp (section, ".reg-aarch-za") == 0)
13204 return elfcore_write_aarch_za (abfd, buf, bufsiz, data, size);
13205 if (strcmp (section, ".reg-aarch-zt") == 0)
13206 return elfcore_write_aarch_zt (abfd, buf, bufsiz, data, size);
13207 if (strcmp (section, ".reg-arc-v2") == 0)
13208 return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
13209 if (strcmp (section, ".gdb-tdesc") == 0)
13210 return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
13211 if (strcmp (section, ".reg-riscv-csr") == 0)
13212 return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
13213 if (strcmp (section, ".reg-loongarch-cpucfg") == 0)
13214 return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size);
13215 if (strcmp (section, ".reg-loongarch-lbt") == 0)
13216 return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size);
13217 if (strcmp (section, ".reg-loongarch-lsx") == 0)
13218 return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
13219 if (strcmp (section, ".reg-loongarch-lasx") == 0)
13220 return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
13221 return NULL;
13224 char *
13225 elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
13226 const void *buf, int bufsiz)
13228 return elfcore_write_note (obfd, note_data, note_size,
13229 "CORE", NT_FILE, buf, bufsiz);
13232 static bool
13233 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
13234 size_t align)
13236 char *p;
13238 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
13239 gABI specifies that PT_NOTE alignment should be aligned to 4
13240 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
13241 align is less than 4, we use 4 byte alignment. */
13242 if (align < 4)
13243 align = 4;
13244 if (align != 4 && align != 8)
13245 return false;
13247 p = buf;
13248 while (p < buf + size)
13250 Elf_External_Note *xnp = (Elf_External_Note *) p;
13251 Elf_Internal_Note in;
13253 if (offsetof (Elf_External_Note, name) > buf - p + size)
13254 return false;
13256 in.type = H_GET_32 (abfd, xnp->type);
13258 in.namesz = H_GET_32 (abfd, xnp->namesz);
13259 in.namedata = xnp->name;
13260 if (in.namesz > buf - in.namedata + size)
13261 return false;
13263 in.descsz = H_GET_32 (abfd, xnp->descsz);
13264 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
13265 in.descpos = offset + (in.descdata - buf);
13266 if (in.descsz != 0
13267 && (in.descdata >= buf + size
13268 || in.descsz > buf - in.descdata + size))
13269 return false;
13271 switch (bfd_get_format (abfd))
13273 default:
13274 return true;
13276 case bfd_core:
13278 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
13279 struct
13281 const char * string;
13282 size_t len;
13283 bool (*func) (bfd *, Elf_Internal_Note *);
13285 grokers[] =
13287 GROKER_ELEMENT ("", elfcore_grok_note),
13288 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
13289 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
13290 GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
13291 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
13292 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
13293 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
13294 GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note)
13296 #undef GROKER_ELEMENT
13297 int i;
13299 for (i = ARRAY_SIZE (grokers); i--;)
13301 if (in.namesz >= grokers[i].len
13302 && strncmp (in.namedata, grokers[i].string,
13303 grokers[i].len) == 0)
13305 if (! grokers[i].func (abfd, & in))
13306 return false;
13307 break;
13310 break;
13313 case bfd_object:
13314 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
13316 if (! elfobj_grok_gnu_note (abfd, &in))
13317 return false;
13319 else if (in.namesz == sizeof "stapsdt"
13320 && strcmp (in.namedata, "stapsdt") == 0)
13322 if (! elfobj_grok_stapsdt_note (abfd, &in))
13323 return false;
13325 break;
13328 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
13331 return true;
13334 bool
13335 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
13336 size_t align)
13338 char *buf;
13340 if (size == 0 || (size + 1) == 0)
13341 return true;
13343 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
13344 return false;
13346 buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
13347 if (buf == NULL)
13348 return false;
13350 /* PR 17512: file: ec08f814
13351 0-termintate the buffer so that string searches will not overflow. */
13352 buf[size] = 0;
13354 if (!elf_parse_notes (abfd, buf, size, offset, align))
13356 free (buf);
13357 return false;
13360 free (buf);
13361 return true;
13364 /* Providing external access to the ELF program header table. */
13366 /* Return an upper bound on the number of bytes required to store a
13367 copy of ABFD's program header table entries. Return -1 if an error
13368 occurs; bfd_get_error will return an appropriate code. */
13370 long
13371 bfd_get_elf_phdr_upper_bound (bfd *abfd)
13373 if (abfd->xvec->flavour != bfd_target_elf_flavour)
13375 bfd_set_error (bfd_error_wrong_format);
13376 return -1;
13379 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
13382 /* Copy ABFD's program header table entries to *PHDRS. The entries
13383 will be stored as an array of Elf_Internal_Phdr structures, as
13384 defined in include/elf/internal.h. To find out how large the
13385 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
13387 Return the number of program header table entries read, or -1 if an
13388 error occurs; bfd_get_error will return an appropriate code. */
13391 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
13393 int num_phdrs;
13395 if (abfd->xvec->flavour != bfd_target_elf_flavour)
13397 bfd_set_error (bfd_error_wrong_format);
13398 return -1;
13401 num_phdrs = elf_elfheader (abfd)->e_phnum;
13402 if (num_phdrs != 0)
13403 memcpy (phdrs, elf_tdata (abfd)->phdr,
13404 num_phdrs * sizeof (Elf_Internal_Phdr));
13406 return num_phdrs;
13409 enum elf_reloc_type_class
13410 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
13411 const asection *rel_sec ATTRIBUTE_UNUSED,
13412 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
13414 return reloc_class_normal;
13417 /* For RELA architectures, return the relocation value for a
13418 relocation against a local symbol. */
13420 bfd_vma
13421 _bfd_elf_rela_local_sym (bfd *abfd,
13422 Elf_Internal_Sym *sym,
13423 asection **psec,
13424 Elf_Internal_Rela *rel)
13426 asection *sec = *psec;
13427 bfd_vma relocation;
13429 relocation = (sec->output_section->vma
13430 + sec->output_offset
13431 + sym->st_value);
13432 if ((sec->flags & SEC_MERGE)
13433 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
13434 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
13436 rel->r_addend =
13437 _bfd_merged_section_offset (abfd, psec,
13438 elf_section_data (sec)->sec_info,
13439 sym->st_value + rel->r_addend);
13440 if (sec != *psec)
13442 /* If we have changed the section, and our original section is
13443 marked with SEC_EXCLUDE, it means that the original
13444 SEC_MERGE section has been completely subsumed in some
13445 other SEC_MERGE section. In this case, we need to leave
13446 some info around for --emit-relocs. */
13447 if ((sec->flags & SEC_EXCLUDE) != 0)
13448 sec->kept_section = *psec;
13449 sec = *psec;
13451 rel->r_addend -= relocation;
13452 rel->r_addend += sec->output_section->vma + sec->output_offset;
13454 return relocation;
13457 bfd_vma
13458 _bfd_elf_rel_local_sym (bfd *abfd,
13459 Elf_Internal_Sym *sym,
13460 asection **psec,
13461 bfd_vma addend)
13463 asection *sec = *psec;
13465 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
13466 return sym->st_value + addend;
13468 return _bfd_merged_section_offset (abfd, psec,
13469 elf_section_data (sec)->sec_info,
13470 sym->st_value + addend);
13473 /* Adjust an address within a section. Given OFFSET within SEC, return
13474 the new offset within the section, based upon changes made to the
13475 section. Returns -1 if the offset is now invalid.
13476 The offset (in abnd out) is in target sized bytes, however big a
13477 byte may be. */
13479 bfd_vma
13480 _bfd_elf_section_offset (bfd *abfd,
13481 struct bfd_link_info *info,
13482 asection *sec,
13483 bfd_vma offset)
13485 switch (sec->sec_info_type)
13487 case SEC_INFO_TYPE_STABS:
13488 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
13489 offset);
13490 case SEC_INFO_TYPE_EH_FRAME:
13491 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
13493 default:
13494 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
13496 /* Reverse the offset. */
13497 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13498 bfd_size_type address_size = bed->s->arch_size / 8;
13500 /* address_size and sec->size are in octets. Convert
13501 to bytes before subtracting the original offset. */
13502 offset = ((sec->size - address_size)
13503 / bfd_octets_per_byte (abfd, sec) - offset);
13505 return offset;
13509 long
13510 _bfd_elf_get_synthetic_symtab (bfd *abfd,
13511 long symcount ATTRIBUTE_UNUSED,
13512 asymbol **syms ATTRIBUTE_UNUSED,
13513 long dynsymcount,
13514 asymbol **dynsyms,
13515 asymbol **ret)
13517 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13518 asection *relplt;
13519 asymbol *s;
13520 const char *relplt_name;
13521 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
13522 arelent *p;
13523 long count, i, n;
13524 size_t size;
13525 Elf_Internal_Shdr *hdr;
13526 char *names;
13527 asection *plt;
13529 *ret = NULL;
13531 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
13532 return 0;
13534 if (dynsymcount <= 0)
13535 return 0;
13537 if (!bed->plt_sym_val)
13538 return 0;
13540 relplt_name = bed->relplt_name;
13541 if (relplt_name == NULL)
13542 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
13543 relplt = bfd_get_section_by_name (abfd, relplt_name);
13544 if (relplt == NULL)
13545 return 0;
13547 hdr = &elf_section_data (relplt)->this_hdr;
13548 if (hdr->sh_link != elf_dynsymtab (abfd)
13549 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
13550 return 0;
13552 plt = bfd_get_section_by_name (abfd, ".plt");
13553 if (plt == NULL)
13554 return 0;
13556 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
13557 if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
13558 return -1;
13560 count = NUM_SHDR_ENTRIES (hdr);
13561 size = count * sizeof (asymbol);
13562 p = relplt->relocation;
13563 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13565 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
13566 if (p->addend != 0)
13568 #ifdef BFD64
13569 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
13570 #else
13571 size += sizeof ("+0x") - 1 + 8;
13572 #endif
13576 s = *ret = (asymbol *) bfd_malloc (size);
13577 if (s == NULL)
13578 return -1;
13580 names = (char *) (s + count);
13581 p = relplt->relocation;
13582 n = 0;
13583 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13585 size_t len;
13586 bfd_vma addr;
13588 addr = bed->plt_sym_val (i, plt, p);
13589 if (addr == (bfd_vma) -1)
13590 continue;
13592 *s = **p->sym_ptr_ptr;
13593 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
13594 we are defining a symbol, ensure one of them is set. */
13595 if ((s->flags & BSF_LOCAL) == 0)
13596 s->flags |= BSF_GLOBAL;
13597 s->flags |= BSF_SYNTHETIC;
13598 s->section = plt;
13599 s->value = addr - plt->vma;
13600 s->name = names;
13601 s->udata.p = NULL;
13602 len = strlen ((*p->sym_ptr_ptr)->name);
13603 memcpy (names, (*p->sym_ptr_ptr)->name, len);
13604 names += len;
13605 if (p->addend != 0)
13607 char buf[30], *a;
13609 memcpy (names, "+0x", sizeof ("+0x") - 1);
13610 names += sizeof ("+0x") - 1;
13611 bfd_sprintf_vma (abfd, buf, p->addend);
13612 for (a = buf; *a == '0'; ++a)
13614 len = strlen (a);
13615 memcpy (names, a, len);
13616 names += len;
13618 memcpy (names, "@plt", sizeof ("@plt"));
13619 names += sizeof ("@plt");
13620 ++s, ++n;
13623 return n;
13626 /* It is only used by x86-64 so far.
13627 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
13628 but current usage would allow all of _bfd_std_section to be zero. */
13629 static const asymbol lcomm_sym
13630 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
13631 asection _bfd_elf_large_com_section
13632 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
13633 "LARGE_COMMON", 0, SEC_IS_COMMON);
13635 bool
13636 _bfd_elf_final_write_processing (bfd *abfd)
13638 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
13640 i_ehdrp = elf_elfheader (abfd);
13642 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13643 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
13645 /* Set the osabi field to ELFOSABI_GNU if the binary contains
13646 SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13647 or STB_GNU_UNIQUE binding. */
13648 if (elf_tdata (abfd)->has_gnu_osabi != 0)
13650 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13651 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
13652 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
13653 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
13655 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
13656 _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13657 "and FreeBSD targets"));
13658 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
13659 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13660 "only by GNU and FreeBSD targets"));
13661 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
13662 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13663 "only by GNU and FreeBSD targets"));
13664 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
13665 _bfd_error_handler (_("GNU_RETAIN section is supported "
13666 "only by GNU and FreeBSD targets"));
13667 bfd_set_error (bfd_error_sorry);
13668 return false;
13671 return true;
13675 /* Return TRUE for ELF symbol types that represent functions.
13676 This is the default version of this function, which is sufficient for
13677 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
13679 bool
13680 _bfd_elf_is_function_type (unsigned int type)
13682 return (type == STT_FUNC
13683 || type == STT_GNU_IFUNC);
13686 /* If the ELF symbol SYM might be a function in SEC, return the
13687 function size and set *CODE_OFF to the function's entry point,
13688 otherwise return zero. */
13690 bfd_size_type
13691 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13692 bfd_vma *code_off)
13694 bfd_size_type size;
13695 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
13697 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
13698 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
13699 || sym->section != sec)
13700 return 0;
13702 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
13704 /* In theory we should check that the symbol's type satisfies
13705 _bfd_elf_is_function_type(), but there are some function-like
13706 symbols which would fail this test. (eg _start). Instead
13707 we check for hidden, local, notype symbols with zero size.
13708 This type of symbol is generated by the annobin plugin for gcc
13709 and clang, and should not be considered to be a function symbol. */
13710 if (size == 0
13711 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
13712 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
13713 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
13714 return 0;
13716 *code_off = sym->value;
13717 /* Do not return 0 for the function's size. */
13718 return size ? size : 1;
13721 /* Set to non-zero to enable some debug messages. */
13722 #define DEBUG_SECONDARY_RELOCS 0
13724 /* An internal-to-the-bfd-library only section type
13725 used to indicate a cached secondary reloc section. */
13726 #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
13728 /* Create a BFD section to hold a secondary reloc section. */
13730 bool
13731 _bfd_elf_init_secondary_reloc_section (bfd * abfd,
13732 Elf_Internal_Shdr *hdr,
13733 const char * name,
13734 unsigned int shindex)
13736 /* We only support RELA secondary relocs. */
13737 if (hdr->sh_type != SHT_RELA)
13738 return false;
13740 #if DEBUG_SECONDARY_RELOCS
13741 fprintf (stderr, "secondary reloc section %s encountered\n", name);
13742 #endif
13743 hdr->sh_type = SHT_SECONDARY_RELOC;
13744 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
13747 /* Read in any secondary relocs associated with SEC. */
13749 bool
13750 _bfd_elf_slurp_secondary_reloc_section (bfd * abfd,
13751 asection * sec,
13752 asymbol ** symbols,
13753 bool dynamic)
13755 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13756 asection * relsec;
13757 bool result = true;
13758 bfd_vma (*r_sym) (bfd_vma);
13759 ufile_ptr filesize;
13761 #if BFD_DEFAULT_TARGET_SIZE > 32
13762 if (bfd_arch_bits_per_address (abfd) != 32)
13763 r_sym = elf64_r_sym;
13764 else
13765 #endif
13766 r_sym = elf32_r_sym;
13768 if (!elf_section_data (sec)->has_secondary_relocs)
13769 return true;
13771 /* Discover if there are any secondary reloc sections
13772 associated with SEC. */
13773 filesize = bfd_get_file_size (abfd);
13774 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13776 Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
13778 if (hdr->sh_type == SHT_SECONDARY_RELOC
13779 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
13780 && (hdr->sh_entsize == ebd->s->sizeof_rel
13781 || hdr->sh_entsize == ebd->s->sizeof_rela))
13783 bfd_byte * native_relocs;
13784 bfd_byte * native_reloc;
13785 arelent * internal_relocs;
13786 arelent * internal_reloc;
13787 size_t i;
13788 unsigned int entsize;
13789 unsigned int symcount;
13790 bfd_size_type reloc_count;
13791 size_t amt;
13793 if (ebd->elf_info_to_howto == NULL)
13794 return false;
13796 #if DEBUG_SECONDARY_RELOCS
13797 fprintf (stderr, "read secondary relocs for %s from %s\n",
13798 sec->name, relsec->name);
13799 #endif
13800 entsize = hdr->sh_entsize;
13802 if (filesize != 0
13803 && ((ufile_ptr) hdr->sh_offset > filesize
13804 || hdr->sh_size > filesize - hdr->sh_offset))
13806 bfd_set_error (bfd_error_file_truncated);
13807 result = false;
13808 continue;
13811 native_relocs = bfd_malloc (hdr->sh_size);
13812 if (native_relocs == NULL)
13814 result = false;
13815 continue;
13818 reloc_count = NUM_SHDR_ENTRIES (hdr);
13819 if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
13821 free (native_relocs);
13822 bfd_set_error (bfd_error_file_too_big);
13823 result = false;
13824 continue;
13827 internal_relocs = (arelent *) bfd_alloc (abfd, amt);
13828 if (internal_relocs == NULL)
13830 free (native_relocs);
13831 result = false;
13832 continue;
13835 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
13836 || bfd_read (native_relocs, hdr->sh_size, abfd) != hdr->sh_size)
13838 free (native_relocs);
13839 /* The internal_relocs will be freed when
13840 the memory for the bfd is released. */
13841 result = false;
13842 continue;
13845 if (dynamic)
13846 symcount = bfd_get_dynamic_symcount (abfd);
13847 else
13848 symcount = bfd_get_symcount (abfd);
13850 for (i = 0, internal_reloc = internal_relocs,
13851 native_reloc = native_relocs;
13852 i < reloc_count;
13853 i++, internal_reloc++, native_reloc += entsize)
13855 bool res;
13856 Elf_Internal_Rela rela;
13858 if (entsize == ebd->s->sizeof_rel)
13859 ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
13860 else /* entsize == ebd->s->sizeof_rela */
13861 ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
13863 /* The address of an ELF reloc is section relative for an object
13864 file, and absolute for an executable file or shared library.
13865 The address of a normal BFD reloc is always section relative,
13866 and the address of a dynamic reloc is absolute.. */
13867 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
13868 internal_reloc->address = rela.r_offset;
13869 else
13870 internal_reloc->address = rela.r_offset - sec->vma;
13872 if (r_sym (rela.r_info) == STN_UNDEF)
13874 /* FIXME: This and the error case below mean that we
13875 have a symbol on relocs that is not elf_symbol_type. */
13876 internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13878 else if (r_sym (rela.r_info) > symcount)
13880 _bfd_error_handler
13881 /* xgettext:c-format */
13882 (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
13883 abfd, sec, i, (long) r_sym (rela.r_info));
13884 bfd_set_error (bfd_error_bad_value);
13885 internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13886 result = false;
13888 else
13890 asymbol **ps;
13892 ps = symbols + r_sym (rela.r_info) - 1;
13893 internal_reloc->sym_ptr_ptr = ps;
13894 /* Make sure that this symbol is not removed by strip. */
13895 (*ps)->flags |= BSF_KEEP;
13898 internal_reloc->addend = rela.r_addend;
13900 res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
13901 if (! res || internal_reloc->howto == NULL)
13903 #if DEBUG_SECONDARY_RELOCS
13904 fprintf (stderr,
13905 "there is no howto associated with reloc %lx\n",
13906 rela.r_info);
13907 #endif
13908 result = false;
13912 free (native_relocs);
13913 /* Store the internal relocs. */
13914 elf_section_data (relsec)->sec_info = internal_relocs;
13918 return result;
13921 /* Set the ELF section header fields of an output secondary reloc section. */
13923 bool
13924 _bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
13925 bfd *obfd ATTRIBUTE_UNUSED,
13926 const Elf_Internal_Shdr *isection,
13927 Elf_Internal_Shdr *osection)
13929 asection * isec;
13930 asection * osec;
13931 struct bfd_elf_section_data * esd;
13933 if (isection == NULL)
13934 return false;
13936 if (isection->sh_type != SHT_SECONDARY_RELOC)
13937 return true;
13939 isec = isection->bfd_section;
13940 if (isec == NULL)
13941 return false;
13943 osec = osection->bfd_section;
13944 if (osec == NULL)
13945 return false;
13947 esd = elf_section_data (osec);
13948 BFD_ASSERT (esd->sec_info == NULL);
13949 esd->sec_info = elf_section_data (isec)->sec_info;
13950 osection->sh_type = SHT_RELA;
13951 osection->sh_link = elf_onesymtab (obfd);
13952 if (osection->sh_link == 0)
13954 /* There is no symbol table - we are hosed... */
13955 _bfd_error_handler
13956 /* xgettext:c-format */
13957 (_("%pB(%pA): link section cannot be set"
13958 " because the output file does not have a symbol table"),
13959 obfd, osec);
13960 bfd_set_error (bfd_error_bad_value);
13961 return false;
13964 /* Find the output section that corresponds to the isection's
13965 sh_info link. */
13966 if (isection->sh_info == 0
13967 || isection->sh_info >= elf_numsections (ibfd))
13969 _bfd_error_handler
13970 /* xgettext:c-format */
13971 (_("%pB(%pA): info section index is invalid"),
13972 obfd, osec);
13973 bfd_set_error (bfd_error_bad_value);
13974 return false;
13977 isection = elf_elfsections (ibfd)[isection->sh_info];
13979 if (isection == NULL
13980 || isection->bfd_section == NULL
13981 || isection->bfd_section->output_section == NULL)
13983 _bfd_error_handler
13984 /* xgettext:c-format */
13985 (_("%pB(%pA): info section index cannot be set"
13986 " because the section is not in the output"),
13987 obfd, osec);
13988 bfd_set_error (bfd_error_bad_value);
13989 return false;
13992 esd = elf_section_data (isection->bfd_section->output_section);
13993 BFD_ASSERT (esd != NULL);
13994 osection->sh_info = esd->this_idx;
13995 esd->has_secondary_relocs = true;
13996 #if DEBUG_SECONDARY_RELOCS
13997 fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
13998 osec->name, osection->sh_link, osection->sh_info);
13999 fprintf (stderr, "mark section %s as having secondary relocs\n",
14000 bfd_section_name (isection->bfd_section->output_section));
14001 #endif
14003 return true;
14006 /* Write out a secondary reloc section.
14008 FIXME: Currently this function can result in a serious performance penalty
14009 for files with secondary relocs and lots of sections. The proper way to
14010 fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
14011 relocs together and then to have this function just walk that chain. */
14013 bool
14014 _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
14016 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
14017 bfd_vma addr_offset;
14018 asection * relsec;
14019 bfd_vma (*r_info) (bfd_vma, bfd_vma);
14020 bool result = true;
14022 if (sec == NULL)
14023 return false;
14025 #if BFD_DEFAULT_TARGET_SIZE > 32
14026 if (bfd_arch_bits_per_address (abfd) != 32)
14027 r_info = elf64_r_info;
14028 else
14029 #endif
14030 r_info = elf32_r_info;
14032 /* The address of an ELF reloc is section relative for an object
14033 file, and absolute for an executable file or shared library.
14034 The address of a BFD reloc is always section relative. */
14035 addr_offset = 0;
14036 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
14037 addr_offset = sec->vma;
14039 /* Discover if there are any secondary reloc sections
14040 associated with SEC. */
14041 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
14043 const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
14044 Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
14046 if (hdr->sh_type == SHT_RELA
14047 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
14049 asymbol * last_sym;
14050 int last_sym_idx;
14051 size_t reloc_count;
14052 size_t idx;
14053 bfd_size_type entsize;
14054 arelent * src_irel;
14055 bfd_byte * dst_rela;
14057 if (hdr->contents != NULL)
14059 _bfd_error_handler
14060 /* xgettext:c-format */
14061 (_("%pB(%pA): error: secondary reloc section processed twice"),
14062 abfd, relsec);
14063 bfd_set_error (bfd_error_bad_value);
14064 result = false;
14065 continue;
14068 entsize = hdr->sh_entsize;
14069 if (entsize == 0)
14071 _bfd_error_handler
14072 /* xgettext:c-format */
14073 (_("%pB(%pA): error: secondary reloc section"
14074 " has zero sized entries"),
14075 abfd, relsec);
14076 bfd_set_error (bfd_error_bad_value);
14077 result = false;
14078 continue;
14080 else if (entsize != ebd->s->sizeof_rel
14081 && entsize != ebd->s->sizeof_rela)
14083 _bfd_error_handler
14084 /* xgettext:c-format */
14085 (_("%pB(%pA): error: secondary reloc section"
14086 " has non-standard sized entries"),
14087 abfd, relsec);
14088 bfd_set_error (bfd_error_bad_value);
14089 result = false;
14090 continue;
14093 reloc_count = hdr->sh_size / entsize;
14094 hdr->sh_size = entsize * reloc_count;
14095 if (reloc_count == 0)
14097 _bfd_error_handler
14098 /* xgettext:c-format */
14099 (_("%pB(%pA): error: secondary reloc section is empty!"),
14100 abfd, relsec);
14101 bfd_set_error (bfd_error_bad_value);
14102 result = false;
14103 continue;
14106 hdr->contents = bfd_alloc (abfd, hdr->sh_size);
14107 if (hdr->contents == NULL)
14108 continue;
14109 relsec->alloced = 1;
14111 #if DEBUG_SECONDARY_RELOCS
14112 fprintf (stderr, "write %u secondary relocs for %s from %s\n",
14113 reloc_count, sec->name, relsec->name);
14114 #endif
14115 last_sym = NULL;
14116 last_sym_idx = 0;
14117 dst_rela = hdr->contents;
14118 src_irel = (arelent *) esd->sec_info;
14119 if (src_irel == NULL)
14121 _bfd_error_handler
14122 /* xgettext:c-format */
14123 (_("%pB(%pA): error: internal relocs missing"
14124 " for secondary reloc section"),
14125 abfd, relsec);
14126 bfd_set_error (bfd_error_bad_value);
14127 result = false;
14128 continue;
14131 for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
14133 Elf_Internal_Rela src_rela;
14134 arelent *ptr;
14135 asymbol *sym;
14136 int n;
14138 ptr = src_irel + idx;
14139 if (ptr == NULL)
14141 _bfd_error_handler
14142 /* xgettext:c-format */
14143 (_("%pB(%pA): error: reloc table entry %zu is empty"),
14144 abfd, relsec, idx);
14145 bfd_set_error (bfd_error_bad_value);
14146 result = false;
14147 break;
14150 if (ptr->sym_ptr_ptr == NULL)
14152 /* FIXME: Is this an error ? */
14153 n = 0;
14155 else
14157 sym = *ptr->sym_ptr_ptr;
14159 if (sym == last_sym)
14160 n = last_sym_idx;
14161 else
14163 n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
14164 if (n < 0)
14166 _bfd_error_handler
14167 /* xgettext:c-format */
14168 (_("%pB(%pA): error: secondary reloc %zu"
14169 " references a missing symbol"),
14170 abfd, relsec, idx);
14171 bfd_set_error (bfd_error_bad_value);
14172 result = false;
14173 n = 0;
14176 last_sym = sym;
14177 last_sym_idx = n;
14180 if (sym->the_bfd != NULL
14181 && sym->the_bfd->xvec != abfd->xvec
14182 && ! _bfd_elf_validate_reloc (abfd, ptr))
14184 _bfd_error_handler
14185 /* xgettext:c-format */
14186 (_("%pB(%pA): error: secondary reloc %zu"
14187 " references a deleted symbol"),
14188 abfd, relsec, idx);
14189 bfd_set_error (bfd_error_bad_value);
14190 result = false;
14191 n = 0;
14195 src_rela.r_offset = ptr->address + addr_offset;
14196 if (ptr->howto == NULL)
14198 _bfd_error_handler
14199 /* xgettext:c-format */
14200 (_("%pB(%pA): error: secondary reloc %zu"
14201 " is of an unknown type"),
14202 abfd, relsec, idx);
14203 bfd_set_error (bfd_error_bad_value);
14204 result = false;
14205 src_rela.r_info = r_info (0, 0);
14207 else
14208 src_rela.r_info = r_info (n, ptr->howto->type);
14209 src_rela.r_addend = ptr->addend;
14211 if (entsize == ebd->s->sizeof_rel)
14212 ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
14213 else /* entsize == ebd->s->sizeof_rela */
14214 ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
14219 return result;
14222 /* Mmap in section contents. */
14224 static bool
14225 elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
14227 #ifdef USE_MMAP
14228 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14229 if (bed->use_mmap
14230 && sec->compress_status == COMPRESS_SECTION_NONE
14231 && (sec->flags & SEC_LINKER_CREATED) == 0)
14233 /* Use mmap only if section size >= the minimum mmap section
14234 size. */
14235 size_t readsz = bfd_get_section_limit_octets (abfd, sec);
14236 size_t allocsz = bfd_get_section_alloc_size (abfd, sec);
14237 if (readsz == allocsz && readsz >= _bfd_minimum_mmap_size)
14239 if (sec->contents != NULL)
14241 if (!sec->mmapped_p)
14242 abort ();
14243 *buf = sec->contents;
14244 return true;
14246 if (sec->mmapped_p)
14247 abort ();
14248 sec->mmapped_p = 1;
14250 /* We can't use the final link preallocated buffer for mmap. */
14251 *buf = NULL;
14254 #endif
14255 /* FIXME: We should not get here if sec->alloced is set. */
14256 bool ret = bfd_get_full_section_contents (abfd, sec, buf);
14257 if (ret && sec->mmapped_p)
14258 *buf = sec->contents;
14259 return ret;
14262 /* Mmap in section contents. */
14264 bool
14265 _bfd_elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
14267 *buf = NULL;
14268 return elf_mmap_section_contents (abfd, sec, buf);
14271 /* Mmap in the full section contents for the final link. */
14273 bool
14274 _bfd_elf_link_mmap_section_contents (bfd *abfd, sec_ptr sec,
14275 bfd_byte **buf)
14277 return elf_mmap_section_contents (abfd, sec, buf);
14280 /* Munmap section contents. */
14282 void
14283 _bfd_elf_munmap_section_contents (asection *sec, void *contents)
14285 /* NB: Since _bfd_elf_munmap_section_contents is called like free,
14286 CONTENTS may be NULL. */
14287 if (contents == NULL)
14288 return;
14290 if (sec->alloced
14291 /* What a tangled web we weave with section contents.
14292 FIXME: We shouldn't need to test anything but sec->alloced
14293 here, but there are cases where a buffer is allocated for a
14294 section but then another buffer is malloc'd anyway. eg.
14295 trace through ld-elf/eh4 testcase on x86_64. */
14296 && (sec->contents == contents
14297 || elf_section_data (sec)->this_hdr.contents == contents))
14298 return;
14300 /* Don't leave pointers to data we are about to munmap or free. */
14301 if (sec->contents == contents)
14302 sec->contents = NULL;
14303 if (elf_section_data (sec)->this_hdr.contents == contents)
14304 elf_section_data (sec)->this_hdr.contents = NULL;
14306 #ifdef USE_MMAP
14307 if (sec->mmapped_p)
14309 /* When _bfd_elf_mmap_section_contents returns CONTENTS as
14310 malloced, CONTENTS_ADDR is set to NULL. */
14311 if (elf_section_data (sec)->contents_addr != NULL)
14313 /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid. */
14314 if (munmap (elf_section_data (sec)->contents_addr,
14315 elf_section_data (sec)->contents_size) != 0)
14316 abort ();
14317 sec->mmapped_p = 0;
14318 elf_section_data (sec)->contents_addr = NULL;
14319 elf_section_data (sec)->contents_size = 0;
14320 return;
14323 #endif
14325 free (contents);
14328 /* Munmap the full section contents for the final link. */
14330 void
14331 _bfd_elf_link_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED)
14333 #ifdef USE_MMAP
14334 if (sec->mmapped_p && elf_section_data (sec)->contents_addr != NULL)
14336 /* When _bfd_elf_link_mmap_section_contents returns CONTENTS as
14337 malloced, CONTENTS_ADDR is set to NULL. */
14338 /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid. */
14339 if (munmap (elf_section_data (sec)->contents_addr,
14340 elf_section_data (sec)->contents_size) != 0)
14341 abort ();
14342 sec->mmapped_p = 0;
14343 sec->contents = NULL;
14344 elf_section_data (sec)->this_hdr.contents = NULL;
14345 elf_section_data (sec)->contents_addr = NULL;
14346 elf_section_data (sec)->contents_size = 0;
14348 #endif