Update gdb/NEWS after GDB 14 branch creation.
[binutils-gdb.git] / bfd / elf.c
blobb5b0c69e09742592d772b93f2dd59becfdee37de
1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2023 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 to OBJECT_ID. */
226 bool
227 bfd_elf_allocate_object (bfd *abfd,
228 size_t object_size,
229 enum elf_target_id object_id)
231 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
232 abfd->tdata.any = bfd_zalloc (abfd, object_size);
233 if (abfd->tdata.any == NULL)
234 return false;
236 elf_object_id (abfd) = object_id;
237 if (abfd->direction != read_direction)
239 struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
240 if (o == NULL)
241 return false;
242 elf_tdata (abfd)->o = o;
243 elf_program_header_size (abfd) = (bfd_size_type) -1;
245 return true;
249 bool
250 bfd_elf_make_object (bfd *abfd)
252 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
253 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
254 bed->target_id);
257 bool
258 bfd_elf_mkcorefile (bfd *abfd)
260 /* I think this can be done just like an object file. */
261 if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
262 return false;
263 elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
264 return elf_tdata (abfd)->core != NULL;
267 char *
268 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
270 Elf_Internal_Shdr **i_shdrp;
271 bfd_byte *shstrtab = NULL;
272 file_ptr offset;
273 bfd_size_type shstrtabsize;
275 i_shdrp = elf_elfsections (abfd);
276 if (i_shdrp == 0
277 || shindex >= elf_numsections (abfd)
278 || i_shdrp[shindex] == 0)
279 return NULL;
281 shstrtab = i_shdrp[shindex]->contents;
282 if (shstrtab == NULL)
284 /* No cached one, attempt to read, and cache what we read. */
285 offset = i_shdrp[shindex]->sh_offset;
286 shstrtabsize = i_shdrp[shindex]->sh_size;
288 /* Allocate and clear an extra byte at the end, to prevent crashes
289 in case the string table is not terminated. */
290 if (shstrtabsize + 1 <= 1
291 || bfd_seek (abfd, offset, SEEK_SET) != 0
292 || (shstrtab = _bfd_alloc_and_read (abfd, shstrtabsize + 1,
293 shstrtabsize)) == NULL)
295 /* Once we've failed to read it, make sure we don't keep
296 trying. Otherwise, we'll keep allocating space for
297 the string table over and over. */
298 i_shdrp[shindex]->sh_size = 0;
300 else
301 shstrtab[shstrtabsize] = '\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 intsym_buf = NULL;
453 goto out;
455 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
456 if (extsym_buf == NULL)
458 alloc_ext = bfd_malloc (amt);
459 extsym_buf = alloc_ext;
461 if (extsym_buf == NULL
462 || bfd_seek (ibfd, pos, SEEK_SET) != 0
463 || bfd_read (extsym_buf, amt, ibfd) != amt)
465 intsym_buf = NULL;
466 goto out;
469 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
470 extshndx_buf = NULL;
471 else
473 if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
475 bfd_set_error (bfd_error_file_too_big);
476 intsym_buf = NULL;
477 goto out;
479 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
480 if (extshndx_buf == NULL)
482 alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
483 extshndx_buf = alloc_extshndx;
485 if (extshndx_buf == NULL
486 || bfd_seek (ibfd, pos, SEEK_SET) != 0
487 || bfd_read (extshndx_buf, amt, ibfd) != amt)
489 intsym_buf = NULL;
490 goto out;
494 if (intsym_buf == NULL)
496 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
498 bfd_set_error (bfd_error_file_too_big);
499 goto out;
501 alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
502 intsym_buf = alloc_intsym;
503 if (intsym_buf == NULL)
504 goto out;
507 /* Convert the symbols to internal form. */
508 isymend = intsym_buf + symcount;
509 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
510 shndx = extshndx_buf;
511 isym < isymend;
512 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
513 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
515 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
516 /* xgettext:c-format */
517 _bfd_error_handler (_("%pB symbol number %lu references"
518 " nonexistent SHT_SYMTAB_SHNDX section"),
519 ibfd, (unsigned long) symoffset);
520 free (alloc_intsym);
521 intsym_buf = NULL;
522 goto out;
525 out:
526 free (alloc_ext);
527 free (alloc_extshndx);
529 return intsym_buf;
532 /* Look up a symbol name. */
533 const char *
534 bfd_elf_sym_name (bfd *abfd,
535 Elf_Internal_Shdr *symtab_hdr,
536 Elf_Internal_Sym *isym,
537 asection *sym_sec)
539 const char *name;
540 unsigned int iname = isym->st_name;
541 unsigned int shindex = symtab_hdr->sh_link;
543 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
544 /* Check for a bogus st_shndx to avoid crashing. */
545 && isym->st_shndx < elf_numsections (abfd))
547 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
548 shindex = elf_elfheader (abfd)->e_shstrndx;
551 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
552 if (name == NULL)
553 name = "(null)";
554 else if (sym_sec && *name == '\0')
555 name = bfd_section_name (sym_sec);
557 return name;
560 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
561 sections. The first element is the flags, the rest are section
562 pointers. */
564 typedef union elf_internal_group {
565 Elf_Internal_Shdr *shdr;
566 unsigned int flags;
567 } Elf_Internal_Group;
569 /* Return the name of the group signature symbol. Why isn't the
570 signature just a string? */
572 static const char *
573 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
575 Elf_Internal_Shdr *hdr;
576 unsigned char esym[sizeof (Elf64_External_Sym)];
577 Elf_External_Sym_Shndx eshndx;
578 Elf_Internal_Sym isym;
580 /* First we need to ensure the symbol table is available. Make sure
581 that it is a symbol table section. */
582 if (ghdr->sh_link >= elf_numsections (abfd))
583 return NULL;
584 hdr = elf_elfsections (abfd) [ghdr->sh_link];
585 if (hdr->sh_type != SHT_SYMTAB
586 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
587 return NULL;
589 /* Go read the symbol. */
590 hdr = &elf_tdata (abfd)->symtab_hdr;
591 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
592 &isym, esym, &eshndx) == NULL)
593 return NULL;
595 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
598 /* Set next_in_group list pointer, and group name for NEWSECT. */
600 static bool
601 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
603 unsigned int num_group = elf_tdata (abfd)->num_group;
605 /* If num_group is zero, read in all SHT_GROUP sections. The count
606 is set to -1 if there are no SHT_GROUP sections. */
607 if (num_group == 0)
609 unsigned int i, shnum;
611 /* First count the number of groups. If we have a SHT_GROUP
612 section with just a flag word (ie. sh_size is 4), ignore it. */
613 shnum = elf_numsections (abfd);
614 num_group = 0;
616 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
617 ( (shdr)->sh_type == SHT_GROUP \
618 && (shdr)->sh_size >= minsize \
619 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
620 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
622 for (i = 0; i < shnum; i++)
624 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
626 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
627 num_group += 1;
630 if (num_group == 0)
632 num_group = (unsigned) -1;
633 elf_tdata (abfd)->num_group = num_group;
634 elf_tdata (abfd)->group_sect_ptr = NULL;
636 else
638 /* We keep a list of elf section headers for group sections,
639 so we can find them quickly. */
640 size_t amt;
642 elf_tdata (abfd)->num_group = num_group;
643 amt = num_group * sizeof (Elf_Internal_Shdr *);
644 elf_tdata (abfd)->group_sect_ptr
645 = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
646 if (elf_tdata (abfd)->group_sect_ptr == NULL)
647 return false;
648 num_group = 0;
650 for (i = 0; i < shnum; i++)
652 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
654 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
656 unsigned char *src;
657 Elf_Internal_Group *dest;
659 /* Make sure the group section has a BFD section
660 attached to it. */
661 if (!bfd_section_from_shdr (abfd, i))
662 return false;
664 /* Add to list of sections. */
665 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
666 num_group += 1;
668 /* Read the raw contents. */
669 BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0);
670 shdr->contents = NULL;
671 if (_bfd_mul_overflow (shdr->sh_size,
672 sizeof (*dest) / 4, &amt)
673 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
674 || !(shdr->contents
675 = _bfd_alloc_and_read (abfd, amt, shdr->sh_size)))
677 _bfd_error_handler
678 /* xgettext:c-format */
679 (_("%pB: invalid size field in group section"
680 " header: %#" PRIx64 ""),
681 abfd, (uint64_t) shdr->sh_size);
682 bfd_set_error (bfd_error_bad_value);
683 -- num_group;
684 continue;
687 /* Translate raw contents, a flag word followed by an
688 array of elf section indices all in target byte order,
689 to the flag word followed by an array of elf section
690 pointers. */
691 src = shdr->contents + shdr->sh_size;
692 dest = (Elf_Internal_Group *) (shdr->contents + amt);
694 while (1)
696 unsigned int idx;
698 src -= 4;
699 --dest;
700 idx = H_GET_32 (abfd, src);
701 if (src == shdr->contents)
703 dest->shdr = NULL;
704 dest->flags = idx;
705 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
706 shdr->bfd_section->flags
707 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
708 break;
710 if (idx < shnum)
712 dest->shdr = elf_elfsections (abfd)[idx];
713 /* PR binutils/23199: All sections in a
714 section group should be marked with
715 SHF_GROUP. But some tools generate
716 broken objects without SHF_GROUP. Fix
717 them up here. */
718 dest->shdr->sh_flags |= SHF_GROUP;
720 if (idx >= shnum
721 || dest->shdr->sh_type == SHT_GROUP)
723 _bfd_error_handler
724 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
725 abfd, i);
726 dest->shdr = NULL;
732 /* PR 17510: Corrupt binaries might contain invalid groups. */
733 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
735 elf_tdata (abfd)->num_group = num_group;
737 /* If all groups are invalid then fail. */
738 if (num_group == 0)
740 elf_tdata (abfd)->group_sect_ptr = NULL;
741 elf_tdata (abfd)->num_group = num_group = -1;
742 _bfd_error_handler
743 (_("%pB: no valid group sections found"), abfd);
744 bfd_set_error (bfd_error_bad_value);
750 if (num_group != (unsigned) -1)
752 unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
753 unsigned int j;
755 for (j = 0; j < num_group; j++)
757 /* Begin search from previous found group. */
758 unsigned i = (j + search_offset) % num_group;
760 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
761 Elf_Internal_Group *idx;
762 bfd_size_type n_elt;
764 if (shdr == NULL)
765 continue;
767 idx = (Elf_Internal_Group *) shdr->contents;
768 if (idx == NULL || shdr->sh_size < 4)
770 /* See PR 21957 for a reproducer. */
771 /* xgettext:c-format */
772 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
773 abfd, shdr->bfd_section);
774 elf_tdata (abfd)->group_sect_ptr[i] = NULL;
775 bfd_set_error (bfd_error_bad_value);
776 return false;
778 n_elt = shdr->sh_size / 4;
780 /* Look through this group's sections to see if current
781 section is a member. */
782 while (--n_elt != 0)
783 if ((++idx)->shdr == hdr)
785 asection *s = NULL;
787 /* We are a member of this group. Go looking through
788 other members to see if any others are linked via
789 next_in_group. */
790 idx = (Elf_Internal_Group *) shdr->contents;
791 n_elt = shdr->sh_size / 4;
792 while (--n_elt != 0)
793 if ((++idx)->shdr != NULL
794 && (s = idx->shdr->bfd_section) != NULL
795 && elf_next_in_group (s) != NULL)
796 break;
797 if (n_elt != 0)
799 /* Snarf the group name from other member, and
800 insert current section in circular list. */
801 elf_group_name (newsect) = elf_group_name (s);
802 elf_next_in_group (newsect) = elf_next_in_group (s);
803 elf_next_in_group (s) = newsect;
805 else
807 const char *gname;
809 gname = group_signature (abfd, shdr);
810 if (gname == NULL)
811 return false;
812 elf_group_name (newsect) = gname;
814 /* Start a circular list with one element. */
815 elf_next_in_group (newsect) = newsect;
818 /* If the group section has been created, point to the
819 new member. */
820 if (shdr->bfd_section != NULL)
821 elf_next_in_group (shdr->bfd_section) = newsect;
823 elf_tdata (abfd)->group_search_offset = i;
824 j = num_group - 1;
825 break;
830 if (elf_group_name (newsect) == NULL)
832 /* xgettext:c-format */
833 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
834 abfd, newsect);
835 /* PR 29532: Return true here, even though the group info has not been
836 read. Separate debug info files can have empty group sections, but
837 we do not want this to prevent them from being loaded as otherwise
838 GDB will not be able to use them. */
839 return true;
841 return true;
844 bool
845 _bfd_elf_setup_sections (bfd *abfd)
847 unsigned int i;
848 unsigned int num_group = elf_tdata (abfd)->num_group;
849 bool result = true;
850 asection *s;
852 /* Process SHF_LINK_ORDER. */
853 for (s = abfd->sections; s != NULL; s = s->next)
855 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
856 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
858 unsigned int elfsec = this_hdr->sh_link;
859 /* An sh_link value of 0 is now allowed. It indicates that linked
860 to section has already been discarded, but that the current
861 section has been retained for some other reason. This linking
862 section is still a candidate for later garbage collection
863 however. */
864 if (elfsec == 0)
866 elf_linked_to_section (s) = NULL;
868 else
870 asection *linksec = NULL;
872 if (elfsec < elf_numsections (abfd))
874 this_hdr = elf_elfsections (abfd)[elfsec];
875 linksec = this_hdr->bfd_section;
878 /* PR 1991, 2008:
879 Some strip/objcopy may leave an incorrect value in
880 sh_link. We don't want to proceed. */
881 if (linksec == NULL)
883 _bfd_error_handler
884 /* xgettext:c-format */
885 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
886 s->owner, elfsec, s);
887 result = false;
890 elf_linked_to_section (s) = linksec;
893 else if (this_hdr->sh_type == SHT_GROUP
894 && elf_next_in_group (s) == NULL)
896 _bfd_error_handler
897 /* xgettext:c-format */
898 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
899 abfd, elf_section_data (s)->this_idx);
900 result = false;
904 /* Process section groups. */
905 if (num_group == (unsigned) -1)
906 return result;
908 for (i = 0; i < num_group; i++)
910 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
911 Elf_Internal_Group *idx;
912 unsigned int n_elt;
914 /* PR binutils/18758: Beware of corrupt binaries with invalid
915 group data. */
916 if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
918 _bfd_error_handler
919 /* xgettext:c-format */
920 (_("%pB: section group entry number %u is corrupt"),
921 abfd, i);
922 result = false;
923 continue;
926 idx = (Elf_Internal_Group *) shdr->contents;
927 n_elt = shdr->sh_size / 4;
929 while (--n_elt != 0)
931 ++ idx;
933 if (idx->shdr == NULL)
934 continue;
935 else if (idx->shdr->bfd_section)
936 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
937 else if (idx->shdr->sh_type != SHT_RELA
938 && idx->shdr->sh_type != SHT_REL)
940 /* There are some unknown sections in the group. */
941 _bfd_error_handler
942 /* xgettext:c-format */
943 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
944 abfd,
945 idx->shdr->sh_type,
946 bfd_elf_string_from_elf_section (abfd,
947 (elf_elfheader (abfd)
948 ->e_shstrndx),
949 idx->shdr->sh_name),
950 shdr->bfd_section);
951 result = false;
956 return result;
959 bool
960 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
962 return elf_next_in_group (sec) != NULL;
965 const char *
966 bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
968 if (elf_sec_group (sec) != NULL)
969 return elf_group_name (sec);
970 return NULL;
973 /* This a copy of lto_section defined in GCC (lto-streamer.h). */
975 struct lto_section
977 int16_t major_version;
978 int16_t minor_version;
979 unsigned char slim_object;
981 /* Flags is a private field that is not defined publicly. */
982 uint16_t flags;
985 /* Make a BFD section from an ELF section. We store a pointer to the
986 BFD section in the bfd_section field of the header. */
988 bool
989 _bfd_elf_make_section_from_shdr (bfd *abfd,
990 Elf_Internal_Shdr *hdr,
991 const char *name,
992 int shindex)
994 asection *newsect;
995 flagword flags;
996 const struct elf_backend_data *bed;
997 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
999 if (hdr->bfd_section != NULL)
1000 return true;
1002 newsect = bfd_make_section_anyway (abfd, name);
1003 if (newsect == NULL)
1004 return false;
1006 hdr->bfd_section = newsect;
1007 elf_section_data (newsect)->this_hdr = *hdr;
1008 elf_section_data (newsect)->this_idx = shindex;
1010 /* Always use the real type/flags. */
1011 elf_section_type (newsect) = hdr->sh_type;
1012 elf_section_flags (newsect) = hdr->sh_flags;
1014 newsect->filepos = hdr->sh_offset;
1016 flags = SEC_NO_FLAGS;
1017 if (hdr->sh_type != SHT_NOBITS)
1018 flags |= SEC_HAS_CONTENTS;
1019 if (hdr->sh_type == SHT_GROUP)
1020 flags |= SEC_GROUP;
1021 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1023 flags |= SEC_ALLOC;
1024 if (hdr->sh_type != SHT_NOBITS)
1025 flags |= SEC_LOAD;
1027 if ((hdr->sh_flags & SHF_WRITE) == 0)
1028 flags |= SEC_READONLY;
1029 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1030 flags |= SEC_CODE;
1031 else if ((flags & SEC_LOAD) != 0)
1032 flags |= SEC_DATA;
1033 if ((hdr->sh_flags & SHF_MERGE) != 0)
1035 flags |= SEC_MERGE;
1036 newsect->entsize = hdr->sh_entsize;
1038 if ((hdr->sh_flags & SHF_STRINGS) != 0)
1039 flags |= SEC_STRINGS;
1040 if (hdr->sh_flags & SHF_GROUP)
1041 if (!setup_group (abfd, hdr, newsect))
1042 return false;
1043 if ((hdr->sh_flags & SHF_TLS) != 0)
1044 flags |= SEC_THREAD_LOCAL;
1045 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1046 flags |= SEC_EXCLUDE;
1048 switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
1050 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1051 but binutils as of 2019-07-23 did not set the EI_OSABI header
1052 byte. */
1053 case ELFOSABI_GNU:
1054 case ELFOSABI_FREEBSD:
1055 if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
1056 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
1057 /* Fall through */
1058 case ELFOSABI_NONE:
1059 if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
1060 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1061 break;
1064 if ((flags & SEC_ALLOC) == 0)
1066 /* The debugging sections appear to be recognized only by name,
1067 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1068 if (name [0] == '.')
1070 if (startswith (name, ".debug")
1071 || startswith (name, ".gnu.debuglto_.debug_")
1072 || startswith (name, ".gnu.linkonce.wi.")
1073 || startswith (name, ".zdebug"))
1074 flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
1075 else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
1076 || startswith (name, ".note.gnu"))
1078 flags |= SEC_ELF_OCTETS;
1079 opb = 1;
1081 else if (startswith (name, ".line")
1082 || startswith (name, ".stab")
1083 || strcmp (name, ".gdb_index") == 0)
1084 flags |= SEC_DEBUGGING;
1088 if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
1089 || !bfd_set_section_size (newsect, hdr->sh_size)
1090 || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
1091 & -hdr->sh_addralign)))
1092 return false;
1094 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1095 only link a single copy of the section. This is used to support
1096 g++. g++ will emit each template expansion in its own section.
1097 The symbols will be defined as weak, so that multiple definitions
1098 are permitted. The GNU linker extension is to actually discard
1099 all but one of the sections. */
1100 if (startswith (name, ".gnu.linkonce")
1101 && elf_next_in_group (newsect) == NULL)
1102 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1104 if (!bfd_set_section_flags (newsect, flags))
1105 return false;
1107 bed = get_elf_backend_data (abfd);
1108 if (bed->elf_backend_section_flags)
1109 if (!bed->elf_backend_section_flags (hdr))
1110 return false;
1112 /* We do not parse the PT_NOTE segments as we are interested even in the
1113 separate debug info files which may have the segments offsets corrupted.
1114 PT_NOTEs from the core files are currently not parsed using BFD. */
1115 if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0)
1117 bfd_byte *contents;
1119 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
1120 return false;
1122 elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1123 hdr->sh_offset, hdr->sh_addralign);
1124 free (contents);
1127 if ((newsect->flags & SEC_ALLOC) != 0)
1129 Elf_Internal_Phdr *phdr;
1130 unsigned int i, nload;
1132 /* Some ELF linkers produce binaries with all the program header
1133 p_paddr fields zero. If we have such a binary with more than
1134 one PT_LOAD header, then leave the section lma equal to vma
1135 so that we don't create sections with overlapping lma. */
1136 phdr = elf_tdata (abfd)->phdr;
1137 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1138 if (phdr->p_paddr != 0)
1139 break;
1140 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1141 ++nload;
1142 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1143 return true;
1145 phdr = elf_tdata (abfd)->phdr;
1146 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1148 if (((phdr->p_type == PT_LOAD
1149 && (hdr->sh_flags & SHF_TLS) == 0)
1150 || phdr->p_type == PT_TLS)
1151 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1153 if ((newsect->flags & SEC_LOAD) == 0)
1154 newsect->lma = (phdr->p_paddr
1155 + hdr->sh_addr - phdr->p_vaddr) / opb;
1156 else
1157 /* We used to use the same adjustment for SEC_LOAD
1158 sections, but that doesn't work if the segment
1159 is packed with code from multiple VMAs.
1160 Instead we calculate the section LMA based on
1161 the segment LMA. It is assumed that the
1162 segment will contain sections with contiguous
1163 LMAs, even if the VMAs are not. */
1164 newsect->lma = (phdr->p_paddr
1165 + hdr->sh_offset - phdr->p_offset) / opb;
1167 /* With contiguous segments, we can't tell from file
1168 offsets whether a section with zero size should
1169 be placed at the end of one segment or the
1170 beginning of the next. Decide based on vaddr. */
1171 if (hdr->sh_addr >= phdr->p_vaddr
1172 && (hdr->sh_addr + hdr->sh_size
1173 <= phdr->p_vaddr + phdr->p_memsz))
1174 break;
1179 /* Compress/decompress DWARF debug sections with names: .debug_*,
1180 .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set. */
1181 if ((newsect->flags & SEC_DEBUGGING) != 0
1182 && (newsect->flags & SEC_HAS_CONTENTS) != 0
1183 && (newsect->flags & SEC_ELF_OCTETS) != 0)
1185 enum { nothing, compress, decompress } action = nothing;
1186 int compression_header_size;
1187 bfd_size_type uncompressed_size;
1188 unsigned int uncompressed_align_power;
1189 enum compression_type ch_type = ch_none;
1190 bool compressed
1191 = bfd_is_section_compressed_info (abfd, newsect,
1192 &compression_header_size,
1193 &uncompressed_size,
1194 &uncompressed_align_power,
1195 &ch_type);
1197 /* Should we decompress? */
1198 if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed)
1199 action = decompress;
1201 /* Should we compress? Or convert to a different compression? */
1202 else if ((abfd->flags & BFD_COMPRESS) != 0
1203 && newsect->size != 0
1204 && compression_header_size >= 0
1205 && uncompressed_size > 0)
1207 if (!compressed)
1208 action = compress;
1209 else
1211 enum compression_type new_ch_type = ch_none;
1212 if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
1213 new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0
1214 ? ch_compress_zstd : ch_compress_zlib);
1215 if (new_ch_type != ch_type)
1216 action = compress;
1220 if (action == compress)
1222 if (!bfd_init_section_compress_status (abfd, newsect))
1224 _bfd_error_handler
1225 /* xgettext:c-format */
1226 (_("%pB: unable to compress section %s"), abfd, name);
1227 return false;
1230 else if (action == decompress)
1232 if (!bfd_init_section_decompress_status (abfd, newsect))
1234 _bfd_error_handler
1235 /* xgettext:c-format */
1236 (_("%pB: unable to decompress section %s"), abfd, name);
1237 return false;
1239 #ifndef HAVE_ZSTD
1240 if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD)
1242 _bfd_error_handler
1243 /* xgettext:c-format */
1244 (_ ("%pB: section %s is compressed with zstd, but BFD "
1245 "is not built with zstd support"),
1246 abfd, name);
1247 newsect->compress_status = COMPRESS_SECTION_NONE;
1248 return false;
1250 #endif
1251 if (abfd->is_linker_input
1252 && name[1] == 'z')
1254 /* Rename section from .zdebug_* to .debug_* so that ld
1255 scripts will see this section as a debug section. */
1256 char *new_name = bfd_zdebug_name_to_debug (abfd, name);
1257 if (new_name == NULL)
1258 return false;
1259 bfd_rename_section (newsect, new_name);
1264 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1265 section. */
1266 if (startswith (name, ".gnu.lto_.lto."))
1268 struct lto_section lsection;
1269 if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
1270 sizeof (struct lto_section)))
1271 abfd->lto_slim_object = lsection.slim_object;
1274 return true;
1277 const char *const bfd_elf_section_type_names[] =
1279 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1280 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1281 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1284 /* ELF relocs are against symbols. If we are producing relocatable
1285 output, and the reloc is against an external symbol, and nothing
1286 has given us any additional addend, the resulting reloc will also
1287 be against the same symbol. In such a case, we don't want to
1288 change anything about the way the reloc is handled, since it will
1289 all be done at final link time. Rather than put special case code
1290 into bfd_perform_relocation, all the reloc types use this howto
1291 function, or should call this function for relocatable output. */
1293 bfd_reloc_status_type
1294 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1295 arelent *reloc_entry,
1296 asymbol *symbol,
1297 void *data ATTRIBUTE_UNUSED,
1298 asection *input_section,
1299 bfd *output_bfd,
1300 char **error_message ATTRIBUTE_UNUSED)
1302 if (output_bfd != NULL
1303 && (symbol->flags & BSF_SECTION_SYM) == 0
1304 && (! reloc_entry->howto->partial_inplace
1305 || reloc_entry->addend == 0))
1307 reloc_entry->address += input_section->output_offset;
1308 return bfd_reloc_ok;
1311 /* In some cases the relocation should be treated as output section
1312 relative, as when linking ELF DWARF into PE COFF. Many ELF
1313 targets lack section relative relocations and instead use
1314 ordinary absolute relocations for references between DWARF
1315 sections. That is arguably a bug in those targets but it happens
1316 to work for the usual case of linking to non-loaded ELF debug
1317 sections with VMAs forced to zero. PE COFF on the other hand
1318 doesn't allow a section VMA of zero. */
1319 if (output_bfd == NULL
1320 && !reloc_entry->howto->pc_relative
1321 && (symbol->section->flags & SEC_DEBUGGING) != 0
1322 && (input_section->flags & SEC_DEBUGGING) != 0)
1323 reloc_entry->addend -= symbol->section->output_section->vma;
1325 return bfd_reloc_continue;
1328 /* Returns TRUE if section A matches section B.
1329 Names, addresses and links may be different, but everything else
1330 should be the same. */
1332 static bool
1333 section_match (const Elf_Internal_Shdr * a,
1334 const Elf_Internal_Shdr * b)
1336 if (a->sh_type != b->sh_type
1337 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1338 || a->sh_addralign != b->sh_addralign
1339 || a->sh_entsize != b->sh_entsize)
1340 return false;
1341 if (a->sh_type == SHT_SYMTAB
1342 || a->sh_type == SHT_STRTAB)
1343 return true;
1344 return a->sh_size == b->sh_size;
1347 /* Find a section in OBFD that has the same characteristics
1348 as IHEADER. Return the index of this section or SHN_UNDEF if
1349 none can be found. Check's section HINT first, as this is likely
1350 to be the correct section. */
1352 static unsigned int
1353 find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1354 const unsigned int hint)
1356 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1357 unsigned int i;
1359 BFD_ASSERT (iheader != NULL);
1361 /* See PR 20922 for a reproducer of the NULL test. */
1362 if (hint < elf_numsections (obfd)
1363 && oheaders[hint] != NULL
1364 && section_match (oheaders[hint], iheader))
1365 return hint;
1367 for (i = 1; i < elf_numsections (obfd); i++)
1369 Elf_Internal_Shdr * oheader = oheaders[i];
1371 if (oheader == NULL)
1372 continue;
1373 if (section_match (oheader, iheader))
1374 /* FIXME: Do we care if there is a potential for
1375 multiple matches ? */
1376 return i;
1379 return SHN_UNDEF;
1382 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1383 Processor specific section, based upon a matching input section.
1384 Returns TRUE upon success, FALSE otherwise. */
1386 static bool
1387 copy_special_section_fields (const bfd *ibfd,
1388 bfd *obfd,
1389 const Elf_Internal_Shdr *iheader,
1390 Elf_Internal_Shdr *oheader,
1391 const unsigned int secnum)
1393 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1394 const Elf_Internal_Shdr **iheaders
1395 = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1396 bool changed = false;
1397 unsigned int sh_link;
1399 if (oheader->sh_type == SHT_NOBITS)
1401 /* This is a feature for objcopy --only-keep-debug:
1402 When a section's type is changed to NOBITS, we preserve
1403 the sh_link and sh_info fields so that they can be
1404 matched up with the original.
1406 Note: Strictly speaking these assignments are wrong.
1407 The sh_link and sh_info fields should point to the
1408 relevent sections in the output BFD, which may not be in
1409 the same location as they were in the input BFD. But
1410 the whole point of this action is to preserve the
1411 original values of the sh_link and sh_info fields, so
1412 that they can be matched up with the section headers in
1413 the original file. So strictly speaking we may be
1414 creating an invalid ELF file, but it is only for a file
1415 that just contains debug info and only for sections
1416 without any contents. */
1417 if (oheader->sh_link == 0)
1418 oheader->sh_link = iheader->sh_link;
1419 if (oheader->sh_info == 0)
1420 oheader->sh_info = iheader->sh_info;
1421 return true;
1424 /* Allow the target a chance to decide how these fields should be set. */
1425 if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1426 iheader, oheader))
1427 return true;
1429 /* We have an iheader which might match oheader, and which has non-zero
1430 sh_info and/or sh_link fields. Attempt to follow those links and find
1431 the section in the output bfd which corresponds to the linked section
1432 in the input bfd. */
1433 if (iheader->sh_link != SHN_UNDEF)
1435 /* See PR 20931 for a reproducer. */
1436 if (iheader->sh_link >= elf_numsections (ibfd))
1438 _bfd_error_handler
1439 /* xgettext:c-format */
1440 (_("%pB: invalid sh_link field (%d) in section number %d"),
1441 ibfd, iheader->sh_link, secnum);
1442 return false;
1445 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1446 if (sh_link != SHN_UNDEF)
1448 oheader->sh_link = sh_link;
1449 changed = true;
1451 else
1452 /* FIXME: Should we install iheader->sh_link
1453 if we could not find a match ? */
1454 _bfd_error_handler
1455 /* xgettext:c-format */
1456 (_("%pB: failed to find link section for section %d"), obfd, secnum);
1459 if (iheader->sh_info)
1461 /* The sh_info field can hold arbitrary information, but if the
1462 SHF_LINK_INFO flag is set then it should be interpreted as a
1463 section index. */
1464 if (iheader->sh_flags & SHF_INFO_LINK)
1466 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1467 iheader->sh_info);
1468 if (sh_link != SHN_UNDEF)
1469 oheader->sh_flags |= SHF_INFO_LINK;
1471 else
1472 /* No idea what it means - just copy it. */
1473 sh_link = iheader->sh_info;
1475 if (sh_link != SHN_UNDEF)
1477 oheader->sh_info = sh_link;
1478 changed = true;
1480 else
1481 _bfd_error_handler
1482 /* xgettext:c-format */
1483 (_("%pB: failed to find info section for section %d"), obfd, secnum);
1486 return changed;
1489 /* Copy the program header and other data from one object module to
1490 another. */
1492 bool
1493 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1495 const Elf_Internal_Shdr **iheaders
1496 = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1497 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1498 const struct elf_backend_data *bed;
1499 unsigned int i;
1501 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1502 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1503 return true;
1505 if (!elf_flags_init (obfd))
1507 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1508 elf_flags_init (obfd) = true;
1511 elf_gp (obfd) = elf_gp (ibfd);
1513 /* Also copy the EI_OSABI field. */
1514 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1515 elf_elfheader (ibfd)->e_ident[EI_OSABI];
1517 /* If set, copy the EI_ABIVERSION field. */
1518 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1519 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1520 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1522 /* Copy object attributes. */
1523 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1525 if (iheaders == NULL || oheaders == NULL)
1526 return true;
1528 bed = get_elf_backend_data (obfd);
1530 /* Possibly copy other fields in the section header. */
1531 for (i = 1; i < elf_numsections (obfd); i++)
1533 unsigned int j;
1534 Elf_Internal_Shdr * oheader = oheaders[i];
1536 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1537 because of a special case need for generating separate debug info
1538 files. See below for more details. */
1539 if (oheader == NULL
1540 || (oheader->sh_type != SHT_NOBITS
1541 && oheader->sh_type < SHT_LOOS))
1542 continue;
1544 /* Ignore empty sections, and sections whose
1545 fields have already been initialised. */
1546 if (oheader->sh_size == 0
1547 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1548 continue;
1550 /* Scan for the matching section in the input bfd.
1551 First we try for a direct mapping between the input and
1552 output sections. */
1553 for (j = 1; j < elf_numsections (ibfd); j++)
1555 const Elf_Internal_Shdr * iheader = iheaders[j];
1557 if (iheader == NULL)
1558 continue;
1560 if (oheader->bfd_section != NULL
1561 && iheader->bfd_section != NULL
1562 && iheader->bfd_section->output_section != NULL
1563 && iheader->bfd_section->output_section == oheader->bfd_section)
1565 /* We have found a connection from the input section to
1566 the output section. Attempt to copy the header fields.
1567 If this fails then do not try any further sections -
1568 there should only be a one-to-one mapping between
1569 input and output. */
1570 if (!copy_special_section_fields (ibfd, obfd,
1571 iheader, oheader, i))
1572 j = elf_numsections (ibfd);
1573 break;
1577 if (j < elf_numsections (ibfd))
1578 continue;
1580 /* That failed. So try to deduce the corresponding input section.
1581 Unfortunately we cannot compare names as the output string table
1582 is empty, so instead we check size, address and type. */
1583 for (j = 1; j < elf_numsections (ibfd); j++)
1585 const Elf_Internal_Shdr * iheader = iheaders[j];
1587 if (iheader == NULL)
1588 continue;
1590 /* Try matching fields in the input section's header.
1591 Since --only-keep-debug turns all non-debug sections into
1592 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1593 input type. */
1594 if ((oheader->sh_type == SHT_NOBITS
1595 || iheader->sh_type == oheader->sh_type)
1596 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1597 == (oheader->sh_flags & ~ SHF_INFO_LINK)
1598 && iheader->sh_addralign == oheader->sh_addralign
1599 && iheader->sh_entsize == oheader->sh_entsize
1600 && iheader->sh_size == oheader->sh_size
1601 && iheader->sh_addr == oheader->sh_addr
1602 && (iheader->sh_info != oheader->sh_info
1603 || iheader->sh_link != oheader->sh_link))
1605 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1606 break;
1610 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1612 /* Final attempt. Call the backend copy function
1613 with a NULL input section. */
1614 (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1615 NULL, oheader);
1619 return true;
1622 static const char *
1623 get_segment_type (unsigned int p_type)
1625 const char *pt;
1626 switch (p_type)
1628 case PT_NULL: pt = "NULL"; break;
1629 case PT_LOAD: pt = "LOAD"; break;
1630 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1631 case PT_INTERP: pt = "INTERP"; break;
1632 case PT_NOTE: pt = "NOTE"; break;
1633 case PT_SHLIB: pt = "SHLIB"; break;
1634 case PT_PHDR: pt = "PHDR"; break;
1635 case PT_TLS: pt = "TLS"; break;
1636 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1637 case PT_GNU_STACK: pt = "STACK"; break;
1638 case PT_GNU_RELRO: pt = "RELRO"; break;
1639 case PT_GNU_SFRAME: pt = "SFRAME"; break;
1640 default: pt = NULL; break;
1642 return pt;
1645 /* Print out the program headers. */
1647 bool
1648 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1650 FILE *f = (FILE *) farg;
1651 Elf_Internal_Phdr *p;
1652 asection *s;
1653 bfd_byte *dynbuf = NULL;
1655 p = elf_tdata (abfd)->phdr;
1656 if (p != NULL)
1658 unsigned int i, c;
1660 fprintf (f, _("\nProgram Header:\n"));
1661 c = elf_elfheader (abfd)->e_phnum;
1662 for (i = 0; i < c; i++, p++)
1664 const char *pt = get_segment_type (p->p_type);
1665 char buf[20];
1667 if (pt == NULL)
1669 sprintf (buf, "0x%lx", p->p_type);
1670 pt = buf;
1672 fprintf (f, "%8s off 0x", pt);
1673 bfd_fprintf_vma (abfd, f, p->p_offset);
1674 fprintf (f, " vaddr 0x");
1675 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1676 fprintf (f, " paddr 0x");
1677 bfd_fprintf_vma (abfd, f, p->p_paddr);
1678 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1679 fprintf (f, " filesz 0x");
1680 bfd_fprintf_vma (abfd, f, p->p_filesz);
1681 fprintf (f, " memsz 0x");
1682 bfd_fprintf_vma (abfd, f, p->p_memsz);
1683 fprintf (f, " flags %c%c%c",
1684 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1685 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1686 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1687 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1688 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1689 fprintf (f, "\n");
1693 s = bfd_get_section_by_name (abfd, ".dynamic");
1694 if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0)
1696 unsigned int elfsec;
1697 unsigned long shlink;
1698 bfd_byte *extdyn, *extdynend;
1699 size_t extdynsize;
1700 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1702 fprintf (f, _("\nDynamic Section:\n"));
1704 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1705 goto error_return;
1707 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1708 if (elfsec == SHN_BAD)
1709 goto error_return;
1710 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1712 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1713 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1715 for (extdyn = dynbuf, extdynend = dynbuf + s->size;
1716 (size_t) (extdynend - extdyn) >= extdynsize;
1717 extdyn += extdynsize)
1719 Elf_Internal_Dyn dyn;
1720 const char *name = "";
1721 char ab[20];
1722 bool stringp;
1723 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1725 (*swap_dyn_in) (abfd, extdyn, &dyn);
1727 if (dyn.d_tag == DT_NULL)
1728 break;
1730 stringp = false;
1731 switch (dyn.d_tag)
1733 default:
1734 if (bed->elf_backend_get_target_dtag)
1735 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1737 if (!strcmp (name, ""))
1739 sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
1740 name = ab;
1742 break;
1744 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
1745 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1746 case DT_PLTGOT: name = "PLTGOT"; break;
1747 case DT_HASH: name = "HASH"; break;
1748 case DT_STRTAB: name = "STRTAB"; break;
1749 case DT_SYMTAB: name = "SYMTAB"; break;
1750 case DT_RELA: name = "RELA"; break;
1751 case DT_RELASZ: name = "RELASZ"; break;
1752 case DT_RELAENT: name = "RELAENT"; break;
1753 case DT_STRSZ: name = "STRSZ"; break;
1754 case DT_SYMENT: name = "SYMENT"; break;
1755 case DT_INIT: name = "INIT"; break;
1756 case DT_FINI: name = "FINI"; break;
1757 case DT_SONAME: name = "SONAME"; stringp = true; break;
1758 case DT_RPATH: name = "RPATH"; stringp = true; break;
1759 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1760 case DT_REL: name = "REL"; break;
1761 case DT_RELSZ: name = "RELSZ"; break;
1762 case DT_RELENT: name = "RELENT"; break;
1763 case DT_RELR: name = "RELR"; break;
1764 case DT_RELRSZ: name = "RELRSZ"; break;
1765 case DT_RELRENT: name = "RELRENT"; break;
1766 case DT_PLTREL: name = "PLTREL"; break;
1767 case DT_DEBUG: name = "DEBUG"; break;
1768 case DT_TEXTREL: name = "TEXTREL"; break;
1769 case DT_JMPREL: name = "JMPREL"; break;
1770 case DT_BIND_NOW: name = "BIND_NOW"; break;
1771 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1772 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1773 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1774 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1775 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
1776 case DT_FLAGS: name = "FLAGS"; break;
1777 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1778 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1779 case DT_CHECKSUM: name = "CHECKSUM"; break;
1780 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1781 case DT_MOVEENT: name = "MOVEENT"; break;
1782 case DT_MOVESZ: name = "MOVESZ"; break;
1783 case DT_FEATURE: name = "FEATURE"; break;
1784 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1785 case DT_SYMINSZ: name = "SYMINSZ"; break;
1786 case DT_SYMINENT: name = "SYMINENT"; break;
1787 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1788 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1789 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
1790 case DT_PLTPAD: name = "PLTPAD"; break;
1791 case DT_MOVETAB: name = "MOVETAB"; break;
1792 case DT_SYMINFO: name = "SYMINFO"; break;
1793 case DT_RELACOUNT: name = "RELACOUNT"; break;
1794 case DT_RELCOUNT: name = "RELCOUNT"; break;
1795 case DT_FLAGS_1: name = "FLAGS_1"; break;
1796 case DT_VERSYM: name = "VERSYM"; break;
1797 case DT_VERDEF: name = "VERDEF"; break;
1798 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1799 case DT_VERNEED: name = "VERNEED"; break;
1800 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1801 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
1802 case DT_USED: name = "USED"; break;
1803 case DT_FILTER: name = "FILTER"; stringp = true; break;
1804 case DT_GNU_HASH: name = "GNU_HASH"; break;
1807 fprintf (f, " %-20s ", name);
1808 if (! stringp)
1810 fprintf (f, "0x");
1811 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1813 else
1815 const char *string;
1816 unsigned int tagv = dyn.d_un.d_val;
1818 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1819 if (string == NULL)
1820 goto error_return;
1821 fprintf (f, "%s", string);
1823 fprintf (f, "\n");
1826 free (dynbuf);
1827 dynbuf = NULL;
1830 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1831 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1833 if (! _bfd_elf_slurp_version_tables (abfd, false))
1834 return false;
1837 if (elf_dynverdef (abfd) != 0)
1839 Elf_Internal_Verdef *t;
1841 fprintf (f, _("\nVersion definitions:\n"));
1842 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1844 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1845 t->vd_flags, t->vd_hash,
1846 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1847 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1849 Elf_Internal_Verdaux *a;
1851 fprintf (f, "\t");
1852 for (a = t->vd_auxptr->vda_nextptr;
1853 a != NULL;
1854 a = a->vda_nextptr)
1855 fprintf (f, "%s ",
1856 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1857 fprintf (f, "\n");
1862 if (elf_dynverref (abfd) != 0)
1864 Elf_Internal_Verneed *t;
1866 fprintf (f, _("\nVersion References:\n"));
1867 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1869 Elf_Internal_Vernaux *a;
1871 fprintf (f, _(" required from %s:\n"),
1872 t->vn_filename ? t->vn_filename : "<corrupt>");
1873 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1874 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1875 a->vna_flags, a->vna_other,
1876 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1880 return true;
1882 error_return:
1883 free (dynbuf);
1884 return false;
1887 /* Find the file offset corresponding to VMA by using the program
1888 headers. */
1890 static file_ptr
1891 offset_from_vma (Elf_Internal_Phdr *phdrs, size_t phnum, bfd_vma vma,
1892 size_t size, size_t *max_size_p)
1894 Elf_Internal_Phdr *seg;
1895 size_t i;
1897 for (seg = phdrs, i = 0; i < phnum; ++seg, ++i)
1898 if (seg->p_type == PT_LOAD
1899 && vma >= (seg->p_vaddr & -seg->p_align)
1900 && vma + size <= seg->p_vaddr + seg->p_filesz)
1902 if (max_size_p)
1903 *max_size_p = seg->p_vaddr + seg->p_filesz - vma;
1904 return vma - seg->p_vaddr + seg->p_offset;
1907 if (max_size_p)
1908 *max_size_p = 0;
1909 bfd_set_error (bfd_error_invalid_operation);
1910 return (file_ptr) -1;
1913 /* Convert hash table to internal form. */
1915 static bfd_vma *
1916 get_hash_table_data (bfd *abfd, bfd_size_type number,
1917 unsigned int ent_size, bfd_size_type filesize)
1919 unsigned char *e_data = NULL;
1920 bfd_vma *i_data = NULL;
1921 bfd_size_type size;
1923 if (ent_size != 4 && ent_size != 8)
1924 return NULL;
1926 if ((size_t) number != number)
1928 bfd_set_error (bfd_error_file_too_big);
1929 return NULL;
1932 size = ent_size * number;
1933 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
1934 attempting to allocate memory when the read is bound to fail. */
1935 if (size > filesize
1936 || number >= ~(size_t) 0 / ent_size
1937 || number >= ~(size_t) 0 / sizeof (*i_data))
1939 bfd_set_error (bfd_error_file_too_big);
1940 return NULL;
1943 e_data = _bfd_malloc_and_read (abfd, size, size);
1944 if (e_data == NULL)
1945 return NULL;
1947 i_data = (bfd_vma *) bfd_malloc (number * sizeof (*i_data));
1948 if (i_data == NULL)
1950 free (e_data);
1951 return NULL;
1954 if (ent_size == 4)
1955 while (number--)
1956 i_data[number] = bfd_get_32 (abfd, e_data + number * ent_size);
1957 else
1958 while (number--)
1959 i_data[number] = bfd_get_64 (abfd, e_data + number * ent_size);
1961 free (e_data);
1962 return i_data;
1965 /* Address of .MIPS.xhash section. FIXME: What is the best way to
1966 support DT_MIPS_XHASH? */
1967 #define DT_MIPS_XHASH 0x70000036
1969 /* Reconstruct dynamic symbol table from PT_DYNAMIC segment. */
1971 bool
1972 _bfd_elf_get_dynamic_symbols (bfd *abfd, Elf_Internal_Phdr *phdr,
1973 Elf_Internal_Phdr *phdrs, size_t phnum,
1974 bfd_size_type filesize)
1976 bfd_byte *extdyn, *extdynend;
1977 size_t extdynsize;
1978 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1979 bool (*swap_symbol_in) (bfd *, const void *, const void *,
1980 Elf_Internal_Sym *);
1981 Elf_Internal_Dyn dyn;
1982 bfd_vma dt_hash = 0;
1983 bfd_vma dt_gnu_hash = 0;
1984 bfd_vma dt_mips_xhash = 0;
1985 bfd_vma dt_strtab = 0;
1986 bfd_vma dt_symtab = 0;
1987 size_t dt_strsz = 0;
1988 bfd_vma dt_versym = 0;
1989 bfd_vma dt_verdef = 0;
1990 bfd_vma dt_verneed = 0;
1991 bfd_byte *dynbuf = NULL;
1992 char *strbuf = NULL;
1993 bfd_vma *gnubuckets = NULL;
1994 bfd_vma *gnuchains = NULL;
1995 bfd_vma *mipsxlat = NULL;
1996 file_ptr saved_filepos, filepos;
1997 bool res = false;
1998 size_t amt;
1999 bfd_byte *esymbuf = NULL, *esym;
2000 bfd_size_type symcount;
2001 Elf_Internal_Sym *isymbuf = NULL;
2002 Elf_Internal_Sym *isym, *isymend;
2003 bfd_byte *versym = NULL;
2004 bfd_byte *verdef = NULL;
2005 bfd_byte *verneed = NULL;
2006 size_t verdef_size = 0;
2007 size_t verneed_size = 0;
2008 size_t extsym_size;
2009 const struct elf_backend_data *bed;
2011 /* Return TRUE if symbol table is bad. */
2012 if (elf_bad_symtab (abfd))
2013 return true;
2015 /* Return TRUE if DT_HASH/DT_GNU_HASH have bee processed before. */
2016 if (elf_tdata (abfd)->dt_strtab != NULL)
2017 return true;
2019 bed = get_elf_backend_data (abfd);
2021 /* Save file position for elf_object_p. */
2022 saved_filepos = bfd_tell (abfd);
2024 if (bfd_seek (abfd, phdr->p_offset, SEEK_SET) != 0)
2025 goto error_return;
2027 dynbuf = _bfd_malloc_and_read (abfd, phdr->p_filesz, phdr->p_filesz);
2028 if (dynbuf == NULL)
2029 goto error_return;
2031 extsym_size = bed->s->sizeof_sym;
2032 extdynsize = bed->s->sizeof_dyn;
2033 swap_dyn_in = bed->s->swap_dyn_in;
2035 extdyn = dynbuf;
2036 if (phdr->p_filesz < extdynsize)
2037 goto error_return;
2038 extdynend = extdyn + phdr->p_filesz;
2039 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
2041 swap_dyn_in (abfd, extdyn, &dyn);
2043 if (dyn.d_tag == DT_NULL)
2044 break;
2046 switch (dyn.d_tag)
2048 case DT_HASH:
2049 dt_hash = dyn.d_un.d_val;
2050 break;
2051 case DT_GNU_HASH:
2052 if (bed->elf_machine_code != EM_MIPS
2053 && bed->elf_machine_code != EM_MIPS_RS3_LE)
2054 dt_gnu_hash = dyn.d_un.d_val;
2055 break;
2056 case DT_STRTAB:
2057 dt_strtab = dyn.d_un.d_val;
2058 break;
2059 case DT_SYMTAB:
2060 dt_symtab = dyn.d_un.d_val;
2061 break;
2062 case DT_STRSZ:
2063 dt_strsz = dyn.d_un.d_val;
2064 break;
2065 case DT_SYMENT:
2066 if (dyn.d_un.d_val != extsym_size)
2067 goto error_return;
2068 break;
2069 case DT_VERSYM:
2070 dt_versym = dyn.d_un.d_val;
2071 break;
2072 case DT_VERDEF:
2073 dt_verdef = dyn.d_un.d_val;
2074 break;
2075 case DT_VERNEED:
2076 dt_verneed = dyn.d_un.d_val;
2077 break;
2078 default:
2079 if (dyn.d_tag == DT_MIPS_XHASH
2080 && (bed->elf_machine_code == EM_MIPS
2081 || bed->elf_machine_code == EM_MIPS_RS3_LE))
2083 dt_gnu_hash = dyn.d_un.d_val;
2084 dt_mips_xhash = dyn.d_un.d_val;
2086 break;
2090 /* Check if we can reconstruct dynamic symbol table from PT_DYNAMIC
2091 segment. */
2092 if ((!dt_hash && !dt_gnu_hash)
2093 || !dt_strtab
2094 || !dt_symtab
2095 || !dt_strsz)
2096 goto error_return;
2098 /* Get dynamic string table. */
2099 filepos = offset_from_vma (phdrs, phnum, dt_strtab, dt_strsz, NULL);
2100 if (filepos == (file_ptr) -1
2101 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2102 goto error_return;
2104 /* Dynamic string table must be valid until ABFD is closed. */
2105 strbuf = (char *) _bfd_alloc_and_read (abfd, dt_strsz + 1, dt_strsz);
2106 if (strbuf == NULL)
2107 goto error_return;
2108 /* Since this is a string table, make sure that it is terminated. */
2109 strbuf[dt_strsz] = 0;
2111 /* Get the real symbol count from DT_HASH or DT_GNU_HASH. Prefer
2112 DT_HASH since it is simpler than DT_GNU_HASH. */
2113 if (dt_hash)
2115 unsigned char nb[16];
2116 unsigned int hash_ent_size;
2118 switch (bed->elf_machine_code)
2120 case EM_ALPHA:
2121 case EM_S390:
2122 case EM_S390_OLD:
2123 if (bed->s->elfclass == ELFCLASS64)
2125 hash_ent_size = 8;
2126 break;
2128 /* FALLTHROUGH */
2129 default:
2130 hash_ent_size = 4;
2131 break;
2134 filepos = offset_from_vma (phdrs, phnum, dt_hash, sizeof (nb),
2135 NULL);
2136 if (filepos == (file_ptr) -1
2137 || bfd_seek (abfd, filepos, SEEK_SET) != 0
2138 || bfd_read (nb, 2 * hash_ent_size, abfd) != 2 * hash_ent_size)
2139 goto error_return;
2141 /* The number of dynamic symbol table entries equals the number
2142 of chains. */
2143 if (hash_ent_size == 8)
2144 symcount = bfd_get_64 (abfd, nb + hash_ent_size);
2145 else
2146 symcount = bfd_get_32 (abfd, nb + hash_ent_size);
2148 else
2150 /* For DT_GNU_HASH, only defined symbols with non-STB_LOCAL
2151 bindings are in hash table. Since in dynamic symbol table,
2152 all symbols with STB_LOCAL binding are placed before symbols
2153 with other bindings and all undefined symbols are placed
2154 before defined ones, the highest symbol index in DT_GNU_HASH
2155 is the highest dynamic symbol table index. */
2156 unsigned char nb[16];
2157 bfd_vma ngnubuckets;
2158 bfd_vma gnusymidx;
2159 size_t i, ngnuchains;
2160 bfd_vma maxchain = 0xffffffff, bitmaskwords;
2161 bfd_vma buckets_vma;
2163 filepos = offset_from_vma (phdrs, phnum, dt_gnu_hash,
2164 sizeof (nb), NULL);
2165 if (filepos == (file_ptr) -1
2166 || bfd_seek (abfd, filepos, SEEK_SET) != 0
2167 || bfd_read (nb, sizeof (nb), abfd) != sizeof (nb))
2168 goto error_return;
2170 ngnubuckets = bfd_get_32 (abfd, nb);
2171 gnusymidx = bfd_get_32 (abfd, nb + 4);
2172 bitmaskwords = bfd_get_32 (abfd, nb + 8);
2173 buckets_vma = dt_gnu_hash + 16;
2174 if (bed->s->elfclass == ELFCLASS32)
2175 buckets_vma += bitmaskwords * 4;
2176 else
2177 buckets_vma += bitmaskwords * 8;
2178 filepos = offset_from_vma (phdrs, phnum, buckets_vma, 4, NULL);
2179 if (filepos == (file_ptr) -1
2180 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2181 goto error_return;
2183 gnubuckets = get_hash_table_data (abfd, ngnubuckets, 4, filesize);
2184 if (gnubuckets == NULL)
2185 goto error_return;
2187 for (i = 0; i < ngnubuckets; i++)
2188 if (gnubuckets[i] != 0)
2190 if (gnubuckets[i] < gnusymidx)
2191 goto error_return;
2193 if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
2194 maxchain = gnubuckets[i];
2197 if (maxchain == 0xffffffff)
2199 symcount = 0;
2200 goto empty_gnu_hash;
2203 maxchain -= gnusymidx;
2204 filepos = offset_from_vma (phdrs, phnum,
2205 (buckets_vma +
2206 4 * (ngnubuckets + maxchain)),
2207 4, NULL);
2208 if (filepos == (file_ptr) -1
2209 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2210 goto error_return;
2214 if (bfd_read (nb, 4, abfd) != 4)
2215 goto error_return;
2216 ++maxchain;
2217 if (maxchain == 0)
2218 goto error_return;
2220 while ((bfd_get_32 (abfd, nb) & 1) == 0);
2222 filepos = offset_from_vma (phdrs, phnum,
2223 (buckets_vma + 4 * ngnubuckets),
2224 4, NULL);
2225 if (filepos == (file_ptr) -1
2226 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2227 goto error_return;
2229 gnuchains = get_hash_table_data (abfd, maxchain, 4, filesize);
2230 if (gnuchains == NULL)
2231 goto error_return;
2232 ngnuchains = maxchain;
2234 if (dt_mips_xhash)
2236 filepos = offset_from_vma (phdrs, phnum,
2237 (buckets_vma
2238 + 4 * (ngnubuckets + maxchain)),
2239 4, NULL);
2240 if (filepos == (file_ptr) -1
2241 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2242 goto error_return;
2244 mipsxlat = get_hash_table_data (abfd, maxchain, 4, filesize);
2245 if (mipsxlat == NULL)
2246 goto error_return;
2249 symcount = 0;
2250 for (i = 0; i < ngnubuckets; ++i)
2251 if (gnubuckets[i] != 0)
2253 bfd_vma si = gnubuckets[i];
2254 bfd_vma off = si - gnusymidx;
2257 if (mipsxlat)
2259 if (mipsxlat[off] >= symcount)
2260 symcount = mipsxlat[off] + 1;
2262 else
2264 if (si >= symcount)
2265 symcount = si + 1;
2267 si++;
2269 while (off < ngnuchains && (gnuchains[off++] & 1) == 0);
2273 /* Swap in dynamic symbol table. */
2274 if (_bfd_mul_overflow (symcount, extsym_size, &amt))
2276 bfd_set_error (bfd_error_file_too_big);
2277 goto error_return;
2280 filepos = offset_from_vma (phdrs, phnum, dt_symtab, amt, NULL);
2281 if (filepos == (file_ptr) -1
2282 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2283 goto error_return;
2284 esymbuf = _bfd_malloc_and_read (abfd, amt, amt);
2285 if (esymbuf == NULL)
2286 goto error_return;
2288 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
2290 bfd_set_error (bfd_error_file_too_big);
2291 goto error_return;
2294 /* Dynamic symbol table must be valid until ABFD is closed. */
2295 isymbuf = (Elf_Internal_Sym *) bfd_alloc (abfd, amt);
2296 if (isymbuf == NULL)
2297 goto error_return;
2299 swap_symbol_in = bed->s->swap_symbol_in;
2301 /* Convert the symbols to internal form. */
2302 isymend = isymbuf + symcount;
2303 for (esym = esymbuf, isym = isymbuf;
2304 isym < isymend;
2305 esym += extsym_size, isym++)
2306 if (!swap_symbol_in (abfd, esym, NULL, isym)
2307 || isym->st_name >= dt_strsz)
2309 bfd_set_error (bfd_error_invalid_operation);
2310 goto error_return;
2313 if (dt_versym)
2315 /* Swap in DT_VERSYM. */
2316 if (_bfd_mul_overflow (symcount, 2, &amt))
2318 bfd_set_error (bfd_error_file_too_big);
2319 goto error_return;
2322 filepos = offset_from_vma (phdrs, phnum, dt_versym, amt, NULL);
2323 if (filepos == (file_ptr) -1
2324 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2325 goto error_return;
2327 /* DT_VERSYM info must be valid until ABFD is closed. */
2328 versym = _bfd_alloc_and_read (abfd, amt, amt);
2330 if (dt_verdef)
2332 /* Read in DT_VERDEF. */
2333 filepos = offset_from_vma (phdrs, phnum, dt_verdef,
2334 0, &verdef_size);
2335 if (filepos == (file_ptr) -1
2336 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2337 goto error_return;
2339 /* DT_VERDEF info must be valid until ABFD is closed. */
2340 verdef = _bfd_alloc_and_read (abfd, verdef_size,
2341 verdef_size);
2344 if (dt_verneed)
2346 /* Read in DT_VERNEED. */
2347 filepos = offset_from_vma (phdrs, phnum, dt_verneed,
2348 0, &verneed_size);
2349 if (filepos == (file_ptr) -1
2350 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2351 goto error_return;
2353 /* DT_VERNEED info must be valid until ABFD is closed. */
2354 verneed = _bfd_alloc_and_read (abfd, verneed_size,
2355 verneed_size);
2359 empty_gnu_hash:
2360 elf_tdata (abfd)->dt_strtab = strbuf;
2361 elf_tdata (abfd)->dt_strsz = dt_strsz;
2362 elf_tdata (abfd)->dt_symtab = isymbuf;
2363 elf_tdata (abfd)->dt_symtab_count = symcount;
2364 elf_tdata (abfd)->dt_versym = versym;
2365 elf_tdata (abfd)->dt_verdef = verdef;
2366 elf_tdata (abfd)->dt_verneed = verneed;
2367 elf_tdata (abfd)->dt_verdef_count
2368 = verdef_size / sizeof (Elf_External_Verdef);
2369 elf_tdata (abfd)->dt_verneed_count
2370 = verneed_size / sizeof (Elf_External_Verneed);
2372 res = true;
2374 error_return:
2375 /* Restore file position for elf_object_p. */
2376 if (bfd_seek (abfd, saved_filepos, SEEK_SET) != 0)
2377 res = false;
2378 free (dynbuf);
2379 free (esymbuf);
2380 free (gnubuckets);
2381 free (gnuchains);
2382 free (mipsxlat);
2383 return res;
2386 /* Reconstruct section from dynamic symbol. */
2388 asection *
2389 _bfd_elf_get_section_from_dynamic_symbol (bfd *abfd,
2390 Elf_Internal_Sym *isym)
2392 asection *sec;
2393 flagword flags;
2395 if (!elf_use_dt_symtab_p (abfd))
2396 return NULL;
2398 flags = SEC_ALLOC | SEC_LOAD;
2399 switch (ELF_ST_TYPE (isym->st_info))
2401 case STT_FUNC:
2402 case STT_GNU_IFUNC:
2403 sec = bfd_get_section_by_name (abfd, ".text");
2404 if (sec == NULL)
2405 sec = bfd_make_section_with_flags (abfd,
2406 ".text",
2407 flags | SEC_CODE);
2408 break;
2409 case STT_COMMON:
2410 sec = bfd_com_section_ptr;
2411 break;
2412 case STT_OBJECT:
2413 sec = bfd_get_section_by_name (abfd, ".data");
2414 if (sec == NULL)
2415 sec = bfd_make_section_with_flags (abfd,
2416 ".data",
2417 flags | SEC_DATA);
2418 break;
2419 case STT_TLS:
2420 sec = bfd_get_section_by_name (abfd, ".tdata");
2421 if (sec == NULL)
2422 sec = bfd_make_section_with_flags (abfd,
2423 ".tdata",
2424 (flags
2425 | SEC_DATA
2426 | SEC_THREAD_LOCAL));
2427 break;
2428 default:
2429 sec = bfd_abs_section_ptr;
2430 break;
2433 return sec;
2436 /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
2437 and return symbol version for symbol version itself. */
2439 const char *
2440 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
2441 bool base_p,
2442 bool *hidden)
2444 const char *version_string = NULL;
2445 if ((elf_dynversym (abfd) != 0
2446 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
2447 || (elf_tdata (abfd)->dt_versym != NULL
2448 && (elf_tdata (abfd)->dt_verdef != NULL
2449 || elf_tdata (abfd)->dt_verneed != NULL)))
2451 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
2453 *hidden = (vernum & VERSYM_HIDDEN) != 0;
2454 vernum &= VERSYM_VERSION;
2456 if (vernum == 0)
2457 version_string = "";
2458 else if (vernum == 1
2459 && (vernum > elf_tdata (abfd)->cverdefs
2460 || (elf_tdata (abfd)->verdef[0].vd_flags
2461 == VER_FLG_BASE)))
2462 version_string = base_p ? "Base" : "";
2463 else if (vernum <= elf_tdata (abfd)->cverdefs)
2465 const char *nodename
2466 = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
2467 version_string = "";
2468 if (base_p
2469 || nodename == NULL
2470 || symbol->name == NULL
2471 || strcmp (symbol->name, nodename) != 0)
2472 version_string = nodename;
2474 else
2476 Elf_Internal_Verneed *t;
2478 version_string = _("<corrupt>");
2479 for (t = elf_tdata (abfd)->verref;
2480 t != NULL;
2481 t = t->vn_nextref)
2483 Elf_Internal_Vernaux *a;
2485 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2487 if (a->vna_other == vernum)
2489 *hidden = true;
2490 version_string = a->vna_nodename;
2491 break;
2497 return version_string;
2500 /* Display ELF-specific fields of a symbol. */
2502 void
2503 bfd_elf_print_symbol (bfd *abfd,
2504 void *filep,
2505 asymbol *symbol,
2506 bfd_print_symbol_type how)
2508 FILE *file = (FILE *) filep;
2509 switch (how)
2511 case bfd_print_symbol_name:
2512 fprintf (file, "%s", symbol->name);
2513 break;
2514 case bfd_print_symbol_more:
2515 fprintf (file, "elf ");
2516 bfd_fprintf_vma (abfd, file, symbol->value);
2517 fprintf (file, " %x", symbol->flags);
2518 break;
2519 case bfd_print_symbol_all:
2521 const char *section_name;
2522 const char *name = NULL;
2523 const struct elf_backend_data *bed;
2524 unsigned char st_other;
2525 bfd_vma val;
2526 const char *version_string;
2527 bool hidden;
2529 section_name = symbol->section ? symbol->section->name : "(*none*)";
2531 bed = get_elf_backend_data (abfd);
2532 if (bed->elf_backend_print_symbol_all)
2533 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
2535 if (name == NULL)
2537 name = symbol->name;
2538 bfd_print_symbol_vandf (abfd, file, symbol);
2541 fprintf (file, " %s\t", section_name);
2542 /* Print the "other" value for a symbol. For common symbols,
2543 we've already printed the size; now print the alignment.
2544 For other symbols, we have no specified alignment, and
2545 we've printed the address; now print the size. */
2546 if (symbol->section && bfd_is_com_section (symbol->section))
2547 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
2548 else
2549 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
2550 bfd_fprintf_vma (abfd, file, val);
2552 /* If we have version information, print it. */
2553 version_string = _bfd_elf_get_symbol_version_string (abfd,
2554 symbol,
2555 true,
2556 &hidden);
2557 if (version_string)
2559 if (!hidden)
2560 fprintf (file, " %-11s", version_string);
2561 else
2563 int i;
2565 fprintf (file, " (%s)", version_string);
2566 for (i = 10 - strlen (version_string); i > 0; --i)
2567 putc (' ', file);
2571 /* If the st_other field is not zero, print it. */
2572 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
2574 switch (st_other)
2576 case 0: break;
2577 case STV_INTERNAL: fprintf (file, " .internal"); break;
2578 case STV_HIDDEN: fprintf (file, " .hidden"); break;
2579 case STV_PROTECTED: fprintf (file, " .protected"); break;
2580 default:
2581 /* Some other non-defined flags are also present, so print
2582 everything hex. */
2583 fprintf (file, " 0x%02x", (unsigned int) st_other);
2586 fprintf (file, " %s", name);
2588 break;
2592 /* ELF .o/exec file reading */
2594 /* Create a new bfd section from an ELF section header. */
2596 bool
2597 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
2599 Elf_Internal_Shdr *hdr;
2600 Elf_Internal_Ehdr *ehdr;
2601 const struct elf_backend_data *bed;
2602 const char *name;
2603 bool ret = true;
2605 if (shindex >= elf_numsections (abfd))
2606 return false;
2608 /* PR17512: A corrupt ELF binary might contain a loop of sections via
2609 sh_link or sh_info. Detect this here, by refusing to load a
2610 section that we are already in the process of loading. */
2611 if (elf_tdata (abfd)->being_created[shindex])
2613 _bfd_error_handler
2614 (_("%pB: warning: loop in section dependencies detected"), abfd);
2615 return false;
2617 elf_tdata (abfd)->being_created[shindex] = true;
2619 hdr = elf_elfsections (abfd)[shindex];
2620 ehdr = elf_elfheader (abfd);
2621 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2622 hdr->sh_name);
2623 if (name == NULL)
2624 goto fail;
2626 bed = get_elf_backend_data (abfd);
2627 switch (hdr->sh_type)
2629 case SHT_NULL:
2630 /* Inactive section. Throw it away. */
2631 goto success;
2633 case SHT_PROGBITS: /* Normal section with contents. */
2634 case SHT_NOBITS: /* .bss section. */
2635 case SHT_HASH: /* .hash section. */
2636 case SHT_NOTE: /* .note section. */
2637 case SHT_INIT_ARRAY: /* .init_array section. */
2638 case SHT_FINI_ARRAY: /* .fini_array section. */
2639 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
2640 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
2641 case SHT_GNU_HASH: /* .gnu.hash section. */
2642 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2643 goto success;
2645 case SHT_DYNAMIC: /* Dynamic linking information. */
2646 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2647 goto fail;
2649 if (hdr->sh_link > elf_numsections (abfd))
2651 /* PR 10478: Accept Solaris binaries with a sh_link field
2652 set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1). */
2653 switch (bfd_get_arch (abfd))
2655 case bfd_arch_i386:
2656 case bfd_arch_sparc:
2657 if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
2658 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
2659 break;
2660 /* Otherwise fall through. */
2661 default:
2662 goto fail;
2665 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2666 goto fail;
2667 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2669 Elf_Internal_Shdr *dynsymhdr;
2671 /* The shared libraries distributed with hpux11 have a bogus
2672 sh_link field for the ".dynamic" section. Find the
2673 string table for the ".dynsym" section instead. */
2674 if (elf_dynsymtab (abfd) != 0)
2676 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2677 hdr->sh_link = dynsymhdr->sh_link;
2679 else
2681 unsigned int i, num_sec;
2683 num_sec = elf_numsections (abfd);
2684 for (i = 1; i < num_sec; i++)
2686 dynsymhdr = elf_elfsections (abfd)[i];
2687 if (dynsymhdr->sh_type == SHT_DYNSYM)
2689 hdr->sh_link = dynsymhdr->sh_link;
2690 break;
2695 goto success;
2697 case SHT_SYMTAB: /* A symbol table. */
2698 if (elf_onesymtab (abfd) == shindex)
2699 goto success;
2701 if (hdr->sh_entsize != bed->s->sizeof_sym)
2702 goto fail;
2704 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2706 if (hdr->sh_size != 0)
2707 goto fail;
2708 /* Some assemblers erroneously set sh_info to one with a
2709 zero sh_size. ld sees this as a global symbol count
2710 of (unsigned) -1. Fix it here. */
2711 hdr->sh_info = 0;
2712 goto success;
2715 /* PR 18854: A binary might contain more than one symbol table.
2716 Unusual, but possible. Warn, but continue. */
2717 if (elf_onesymtab (abfd) != 0)
2719 _bfd_error_handler
2720 /* xgettext:c-format */
2721 (_("%pB: warning: multiple symbol tables detected"
2722 " - ignoring the table in section %u"),
2723 abfd, shindex);
2724 goto success;
2726 elf_onesymtab (abfd) = shindex;
2727 elf_symtab_hdr (abfd) = *hdr;
2728 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2729 abfd->flags |= HAS_SYMS;
2731 /* Sometimes a shared object will map in the symbol table. If
2732 SHF_ALLOC is set, and this is a shared object, then we also
2733 treat this section as a BFD section. We can not base the
2734 decision purely on SHF_ALLOC, because that flag is sometimes
2735 set in a relocatable object file, which would confuse the
2736 linker. */
2737 if ((hdr->sh_flags & SHF_ALLOC) != 0
2738 && (abfd->flags & DYNAMIC) != 0
2739 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2740 shindex))
2741 goto fail;
2743 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2744 can't read symbols without that section loaded as well. It
2745 is most likely specified by the next section header. */
2747 elf_section_list * entry;
2748 unsigned int i, num_sec;
2750 for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2751 if (entry->hdr.sh_link == shindex)
2752 goto success;
2754 num_sec = elf_numsections (abfd);
2755 for (i = shindex + 1; i < num_sec; i++)
2757 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2759 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2760 && hdr2->sh_link == shindex)
2761 break;
2764 if (i == num_sec)
2765 for (i = 1; i < shindex; i++)
2767 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2769 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2770 && hdr2->sh_link == shindex)
2771 break;
2774 if (i != shindex)
2775 ret = bfd_section_from_shdr (abfd, i);
2776 /* else FIXME: we have failed to find the symbol table.
2777 Should we issue an error? */
2778 goto success;
2781 case SHT_DYNSYM: /* A dynamic symbol table. */
2782 if (elf_dynsymtab (abfd) == shindex)
2783 goto success;
2785 if (hdr->sh_entsize != bed->s->sizeof_sym)
2786 goto fail;
2788 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2790 if (hdr->sh_size != 0)
2791 goto fail;
2793 /* Some linkers erroneously set sh_info to one with a
2794 zero sh_size. ld sees this as a global symbol count
2795 of (unsigned) -1. Fix it here. */
2796 hdr->sh_info = 0;
2797 goto success;
2800 /* PR 18854: A binary might contain more than one dynamic symbol table.
2801 Unusual, but possible. Warn, but continue. */
2802 if (elf_dynsymtab (abfd) != 0)
2804 _bfd_error_handler
2805 /* xgettext:c-format */
2806 (_("%pB: warning: multiple dynamic symbol tables detected"
2807 " - ignoring the table in section %u"),
2808 abfd, shindex);
2809 goto success;
2811 elf_dynsymtab (abfd) = shindex;
2812 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2813 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2814 abfd->flags |= HAS_SYMS;
2816 /* Besides being a symbol table, we also treat this as a regular
2817 section, so that objcopy can handle it. */
2818 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2819 goto success;
2821 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
2823 elf_section_list * entry;
2825 for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2826 if (entry->ndx == shindex)
2827 goto success;
2829 entry = bfd_alloc (abfd, sizeof (*entry));
2830 if (entry == NULL)
2831 goto fail;
2832 entry->ndx = shindex;
2833 entry->hdr = * hdr;
2834 entry->next = elf_symtab_shndx_list (abfd);
2835 elf_symtab_shndx_list (abfd) = entry;
2836 elf_elfsections (abfd)[shindex] = & entry->hdr;
2837 goto success;
2840 case SHT_STRTAB: /* A string table. */
2841 if (hdr->bfd_section != NULL)
2842 goto success;
2844 if (ehdr->e_shstrndx == shindex)
2846 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2847 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2848 goto success;
2851 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2853 symtab_strtab:
2854 elf_tdata (abfd)->strtab_hdr = *hdr;
2855 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2856 goto success;
2859 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2861 dynsymtab_strtab:
2862 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2863 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2864 elf_elfsections (abfd)[shindex] = hdr;
2865 /* We also treat this as a regular section, so that objcopy
2866 can handle it. */
2867 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2868 shindex);
2869 goto success;
2872 /* If the string table isn't one of the above, then treat it as a
2873 regular section. We need to scan all the headers to be sure,
2874 just in case this strtab section appeared before the above. */
2875 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2877 unsigned int i, num_sec;
2879 num_sec = elf_numsections (abfd);
2880 for (i = 1; i < num_sec; i++)
2882 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2883 if (hdr2->sh_link == shindex)
2885 /* Prevent endless recursion on broken objects. */
2886 if (i == shindex)
2887 goto fail;
2888 if (! bfd_section_from_shdr (abfd, i))
2889 goto fail;
2890 if (elf_onesymtab (abfd) == i)
2891 goto symtab_strtab;
2892 if (elf_dynsymtab (abfd) == i)
2893 goto dynsymtab_strtab;
2897 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2898 goto success;
2900 case SHT_REL:
2901 case SHT_RELA:
2902 case SHT_RELR:
2903 /* *These* do a lot of work -- but build no sections! */
2905 asection *target_sect;
2906 Elf_Internal_Shdr *hdr2, **p_hdr;
2907 unsigned int num_sec = elf_numsections (abfd);
2908 struct bfd_elf_section_data *esdt;
2909 bfd_size_type size;
2911 if (hdr->sh_type == SHT_REL)
2912 size = bed->s->sizeof_rel;
2913 else if (hdr->sh_type == SHT_RELA)
2914 size = bed->s->sizeof_rela;
2915 else
2916 size = bed->s->arch_size / 8;
2917 if (hdr->sh_entsize != size)
2918 goto fail;
2920 /* Check for a bogus link to avoid crashing. */
2921 if (hdr->sh_link >= num_sec)
2923 _bfd_error_handler
2924 /* xgettext:c-format */
2925 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2926 abfd, hdr->sh_link, name, shindex);
2927 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2928 goto success;
2931 /* Get the symbol table. */
2932 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2933 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2934 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2935 goto fail;
2937 /* If this is an alloc section in an executable or shared
2938 library, or the reloc section does not use the main symbol
2939 table we don't treat it as a reloc section. BFD can't
2940 adequately represent such a section, so at least for now,
2941 we don't try. We just present it as a normal section. We
2942 also can't use it as a reloc section if it points to the
2943 null section, an invalid section, another reloc section, or
2944 its sh_link points to the null section. */
2945 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2946 && (hdr->sh_flags & SHF_ALLOC) != 0)
2947 || (hdr->sh_flags & SHF_COMPRESSED) != 0
2948 || hdr->sh_type == SHT_RELR
2949 || hdr->sh_link == SHN_UNDEF
2950 || hdr->sh_link != elf_onesymtab (abfd)
2951 || hdr->sh_info == SHN_UNDEF
2952 || hdr->sh_info >= num_sec
2953 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2954 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2956 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2957 goto success;
2960 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2961 goto fail;
2963 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2964 if (target_sect == NULL)
2965 goto fail;
2967 esdt = elf_section_data (target_sect);
2968 if (hdr->sh_type == SHT_RELA)
2969 p_hdr = &esdt->rela.hdr;
2970 else
2971 p_hdr = &esdt->rel.hdr;
2973 /* PR 17512: file: 0b4f81b7.
2974 Also see PR 24456, for a file which deliberately has two reloc
2975 sections. */
2976 if (*p_hdr != NULL)
2978 if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
2980 _bfd_error_handler
2981 /* xgettext:c-format */
2982 (_("%pB: warning: secondary relocation section '%s' "
2983 "for section %pA found - ignoring"),
2984 abfd, name, target_sect);
2986 else
2987 esdt->has_secondary_relocs = true;
2988 goto success;
2991 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
2992 if (hdr2 == NULL)
2993 goto fail;
2994 *hdr2 = *hdr;
2995 *p_hdr = hdr2;
2996 elf_elfsections (abfd)[shindex] = hdr2;
2997 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2998 * bed->s->int_rels_per_ext_rel);
2999 target_sect->flags |= SEC_RELOC;
3000 target_sect->relocation = NULL;
3001 target_sect->rel_filepos = hdr->sh_offset;
3002 /* In the section to which the relocations apply, mark whether
3003 its relocations are of the REL or RELA variety. */
3004 if (hdr->sh_size != 0)
3006 if (hdr->sh_type == SHT_RELA)
3007 target_sect->use_rela_p = 1;
3009 abfd->flags |= HAS_RELOC;
3010 goto success;
3013 case SHT_GNU_verdef:
3014 if (hdr->sh_info != 0)
3015 elf_dynverdef (abfd) = shindex;
3016 elf_tdata (abfd)->dynverdef_hdr = *hdr;
3017 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3018 goto success;
3020 case SHT_GNU_versym:
3021 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
3022 goto fail;
3024 elf_dynversym (abfd) = shindex;
3025 elf_tdata (abfd)->dynversym_hdr = *hdr;
3026 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3027 goto success;
3029 case SHT_GNU_verneed:
3030 if (hdr->sh_info != 0)
3031 elf_dynverref (abfd) = shindex;
3032 elf_tdata (abfd)->dynverref_hdr = *hdr;
3033 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3034 goto success;
3036 case SHT_SHLIB:
3037 goto success;
3039 case SHT_GROUP:
3040 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
3041 goto fail;
3043 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
3044 goto fail;
3046 goto success;
3048 default:
3049 /* Possibly an attributes section. */
3050 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
3051 || hdr->sh_type == bed->obj_attrs_section_type)
3053 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
3054 goto fail;
3055 _bfd_elf_parse_attributes (abfd, hdr);
3056 goto success;
3059 /* Check for any processor-specific section types. */
3060 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
3061 goto success;
3063 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
3065 if ((hdr->sh_flags & SHF_ALLOC) != 0)
3066 /* FIXME: How to properly handle allocated section reserved
3067 for applications? */
3068 _bfd_error_handler
3069 /* xgettext:c-format */
3070 (_("%pB: unknown type [%#x] section `%s'"),
3071 abfd, hdr->sh_type, name);
3072 else
3074 /* Allow sections reserved for applications. */
3075 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3076 goto success;
3079 else if (hdr->sh_type >= SHT_LOPROC
3080 && hdr->sh_type <= SHT_HIPROC)
3081 /* FIXME: We should handle this section. */
3082 _bfd_error_handler
3083 /* xgettext:c-format */
3084 (_("%pB: unknown type [%#x] section `%s'"),
3085 abfd, hdr->sh_type, name);
3086 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
3088 /* Unrecognised OS-specific sections. */
3089 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
3090 /* SHF_OS_NONCONFORMING indicates that special knowledge is
3091 required to correctly process the section and the file should
3092 be rejected with an error message. */
3093 _bfd_error_handler
3094 /* xgettext:c-format */
3095 (_("%pB: unknown type [%#x] section `%s'"),
3096 abfd, hdr->sh_type, name);
3097 else
3099 /* Otherwise it should be processed. */
3100 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3101 goto success;
3104 else
3105 /* FIXME: We should handle this section. */
3106 _bfd_error_handler
3107 /* xgettext:c-format */
3108 (_("%pB: unknown type [%#x] section `%s'"),
3109 abfd, hdr->sh_type, name);
3111 goto fail;
3114 fail:
3115 ret = false;
3116 success:
3117 elf_tdata (abfd)->being_created[shindex] = false;
3118 return ret;
3121 /* Return the local symbol specified by ABFD, R_SYMNDX. */
3123 Elf_Internal_Sym *
3124 bfd_sym_from_r_symndx (struct sym_cache *cache,
3125 bfd *abfd,
3126 unsigned long r_symndx)
3128 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
3130 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
3132 Elf_Internal_Shdr *symtab_hdr;
3133 unsigned char esym[sizeof (Elf64_External_Sym)];
3134 Elf_External_Sym_Shndx eshndx;
3136 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3137 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
3138 &cache->sym[ent], esym, &eshndx) == NULL)
3139 return NULL;
3141 if (cache->abfd != abfd)
3143 memset (cache->indx, -1, sizeof (cache->indx));
3144 cache->abfd = abfd;
3146 cache->indx[ent] = r_symndx;
3149 return &cache->sym[ent];
3152 /* Given an ELF section number, retrieve the corresponding BFD
3153 section. */
3155 asection *
3156 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
3158 if (sec_index >= elf_numsections (abfd))
3159 return NULL;
3160 return elf_elfsections (abfd)[sec_index]->bfd_section;
3163 static const struct bfd_elf_special_section special_sections_b[] =
3165 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3166 { NULL, 0, 0, 0, 0 }
3169 static const struct bfd_elf_special_section special_sections_c[] =
3171 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
3172 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 },
3173 { NULL, 0, 0, 0, 0 }
3176 static const struct bfd_elf_special_section special_sections_d[] =
3178 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3179 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3180 /* There are more DWARF sections than these, but they needn't be added here
3181 unless you have to cope with broken compilers that don't emit section
3182 attributes or you want to help the user writing assembler. */
3183 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
3184 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
3185 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
3186 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
3187 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
3188 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
3189 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
3190 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
3191 { NULL, 0, 0, 0, 0 }
3194 static const struct bfd_elf_special_section special_sections_f[] =
3196 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3197 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
3198 { NULL, 0 , 0, 0, 0 }
3201 static const struct bfd_elf_special_section special_sections_g[] =
3203 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3204 { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3205 { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3206 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
3207 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3208 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
3209 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
3210 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
3211 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
3212 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
3213 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
3214 { NULL, 0, 0, 0, 0 }
3217 static const struct bfd_elf_special_section special_sections_h[] =
3219 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
3220 { NULL, 0, 0, 0, 0 }
3223 static const struct bfd_elf_special_section special_sections_i[] =
3225 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3226 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3227 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
3228 { NULL, 0, 0, 0, 0 }
3231 static const struct bfd_elf_special_section special_sections_l[] =
3233 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
3234 { NULL, 0, 0, 0, 0 }
3237 static const struct bfd_elf_special_section special_sections_n[] =
3239 { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3240 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
3241 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
3242 { NULL, 0, 0, 0, 0 }
3245 static const struct bfd_elf_special_section special_sections_p[] =
3247 { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3248 { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3249 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3250 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3251 { NULL, 0, 0, 0, 0 }
3254 static const struct bfd_elf_special_section special_sections_r[] =
3256 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
3257 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
3258 { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
3259 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
3260 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
3261 { NULL, 0, 0, 0, 0 }
3264 static const struct bfd_elf_special_section special_sections_s[] =
3266 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
3267 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
3268 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
3269 /* See struct bfd_elf_special_section declaration for the semantics of
3270 this special case where .prefix_length != strlen (.prefix). */
3271 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
3272 { NULL, 0, 0, 0, 0 }
3275 static const struct bfd_elf_special_section special_sections_t[] =
3277 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3278 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
3279 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
3280 { NULL, 0, 0, 0, 0 }
3283 static const struct bfd_elf_special_section special_sections_z[] =
3285 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
3286 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
3287 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
3288 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
3289 { NULL, 0, 0, 0, 0 }
3292 static const struct bfd_elf_special_section * const special_sections[] =
3294 special_sections_b, /* 'b' */
3295 special_sections_c, /* 'c' */
3296 special_sections_d, /* 'd' */
3297 NULL, /* 'e' */
3298 special_sections_f, /* 'f' */
3299 special_sections_g, /* 'g' */
3300 special_sections_h, /* 'h' */
3301 special_sections_i, /* 'i' */
3302 NULL, /* 'j' */
3303 NULL, /* 'k' */
3304 special_sections_l, /* 'l' */
3305 NULL, /* 'm' */
3306 special_sections_n, /* 'n' */
3307 NULL, /* 'o' */
3308 special_sections_p, /* 'p' */
3309 NULL, /* 'q' */
3310 special_sections_r, /* 'r' */
3311 special_sections_s, /* 's' */
3312 special_sections_t, /* 't' */
3313 NULL, /* 'u' */
3314 NULL, /* 'v' */
3315 NULL, /* 'w' */
3316 NULL, /* 'x' */
3317 NULL, /* 'y' */
3318 special_sections_z /* 'z' */
3321 const struct bfd_elf_special_section *
3322 _bfd_elf_get_special_section (const char *name,
3323 const struct bfd_elf_special_section *spec,
3324 unsigned int rela)
3326 int i;
3327 int len;
3329 len = strlen (name);
3331 for (i = 0; spec[i].prefix != NULL; i++)
3333 int suffix_len;
3334 int prefix_len = spec[i].prefix_length;
3336 if (len < prefix_len)
3337 continue;
3338 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
3339 continue;
3341 suffix_len = spec[i].suffix_length;
3342 if (suffix_len <= 0)
3344 if (name[prefix_len] != 0)
3346 if (suffix_len == 0)
3347 continue;
3348 if (name[prefix_len] != '.'
3349 && (suffix_len == -2
3350 || (rela && spec[i].type == SHT_REL)))
3351 continue;
3354 else
3356 if (len < prefix_len + suffix_len)
3357 continue;
3358 if (memcmp (name + len - suffix_len,
3359 spec[i].prefix + prefix_len,
3360 suffix_len) != 0)
3361 continue;
3363 return &spec[i];
3366 return NULL;
3369 const struct bfd_elf_special_section *
3370 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
3372 int i;
3373 const struct bfd_elf_special_section *spec;
3374 const struct elf_backend_data *bed;
3376 /* See if this is one of the special sections. */
3377 if (sec->name == NULL)
3378 return NULL;
3380 bed = get_elf_backend_data (abfd);
3381 spec = bed->special_sections;
3382 if (spec)
3384 spec = _bfd_elf_get_special_section (sec->name,
3385 bed->special_sections,
3386 sec->use_rela_p);
3387 if (spec != NULL)
3388 return spec;
3391 if (sec->name[0] != '.')
3392 return NULL;
3394 i = sec->name[1] - 'b';
3395 if (i < 0 || i > 'z' - 'b')
3396 return NULL;
3398 spec = special_sections[i];
3400 if (spec == NULL)
3401 return NULL;
3403 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
3406 bool
3407 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
3409 struct bfd_elf_section_data *sdata;
3410 const struct elf_backend_data *bed;
3411 const struct bfd_elf_special_section *ssect;
3413 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
3414 if (sdata == NULL)
3416 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
3417 sizeof (*sdata));
3418 if (sdata == NULL)
3419 return false;
3420 sec->used_by_bfd = sdata;
3423 /* Indicate whether or not this section should use RELA relocations. */
3424 bed = get_elf_backend_data (abfd);
3425 sec->use_rela_p = bed->default_use_rela_p;
3427 /* Set up ELF section type and flags for newly created sections, if
3428 there is an ABI mandated section. */
3429 ssect = (*bed->get_sec_type_attr) (abfd, sec);
3430 if (ssect != NULL)
3432 elf_section_type (sec) = ssect->type;
3433 elf_section_flags (sec) = ssect->attr;
3436 return _bfd_generic_new_section_hook (abfd, sec);
3439 /* Create a new bfd section from an ELF program header.
3441 Since program segments have no names, we generate a synthetic name
3442 of the form segment<NUM>, where NUM is generally the index in the
3443 program header table. For segments that are split (see below) we
3444 generate the names segment<NUM>a and segment<NUM>b.
3446 Note that some program segments may have a file size that is different than
3447 (less than) the memory size. All this means is that at execution the
3448 system must allocate the amount of memory specified by the memory size,
3449 but only initialize it with the first "file size" bytes read from the
3450 file. This would occur for example, with program segments consisting
3451 of combined data+bss.
3453 To handle the above situation, this routine generates TWO bfd sections
3454 for the single program segment. The first has the length specified by
3455 the file size of the segment, and the second has the length specified
3456 by the difference between the two sizes. In effect, the segment is split
3457 into its initialized and uninitialized parts. */
3459 bool
3460 _bfd_elf_make_section_from_phdr (bfd *abfd,
3461 Elf_Internal_Phdr *hdr,
3462 int hdr_index,
3463 const char *type_name)
3465 asection *newsect;
3466 char *name;
3467 char namebuf[64];
3468 size_t len;
3469 int split;
3470 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
3472 split = ((hdr->p_memsz > 0)
3473 && (hdr->p_filesz > 0)
3474 && (hdr->p_memsz > hdr->p_filesz));
3476 if (hdr->p_filesz > 0)
3478 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
3479 len = strlen (namebuf) + 1;
3480 name = (char *) bfd_alloc (abfd, len);
3481 if (!name)
3482 return false;
3483 memcpy (name, namebuf, len);
3484 newsect = bfd_make_section (abfd, name);
3485 if (newsect == NULL)
3486 return false;
3487 newsect->vma = hdr->p_vaddr / opb;
3488 newsect->lma = hdr->p_paddr / opb;
3489 newsect->size = hdr->p_filesz;
3490 newsect->filepos = hdr->p_offset;
3491 newsect->flags |= SEC_HAS_CONTENTS;
3492 newsect->alignment_power = bfd_log2 (hdr->p_align);
3493 if (hdr->p_type == PT_LOAD)
3495 newsect->flags |= SEC_ALLOC;
3496 newsect->flags |= SEC_LOAD;
3497 if (hdr->p_flags & PF_X)
3499 /* FIXME: all we known is that it has execute PERMISSION,
3500 may be data. */
3501 newsect->flags |= SEC_CODE;
3504 if (!(hdr->p_flags & PF_W))
3506 newsect->flags |= SEC_READONLY;
3510 if (hdr->p_memsz > hdr->p_filesz)
3512 bfd_vma align;
3514 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
3515 len = strlen (namebuf) + 1;
3516 name = (char *) bfd_alloc (abfd, len);
3517 if (!name)
3518 return false;
3519 memcpy (name, namebuf, len);
3520 newsect = bfd_make_section (abfd, name);
3521 if (newsect == NULL)
3522 return false;
3523 newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
3524 newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
3525 newsect->size = hdr->p_memsz - hdr->p_filesz;
3526 newsect->filepos = hdr->p_offset + hdr->p_filesz;
3527 align = newsect->vma & -newsect->vma;
3528 if (align == 0 || align > hdr->p_align)
3529 align = hdr->p_align;
3530 newsect->alignment_power = bfd_log2 (align);
3531 if (hdr->p_type == PT_LOAD)
3533 newsect->flags |= SEC_ALLOC;
3534 if (hdr->p_flags & PF_X)
3535 newsect->flags |= SEC_CODE;
3537 if (!(hdr->p_flags & PF_W))
3538 newsect->flags |= SEC_READONLY;
3541 return true;
3544 static bool
3545 _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3547 /* The return value is ignored. Build-ids are considered optional. */
3548 if (templ->xvec->flavour == bfd_target_elf_flavour)
3549 return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3550 (templ, offset);
3551 return false;
3554 bool
3555 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
3557 const struct elf_backend_data *bed;
3559 switch (hdr->p_type)
3561 case PT_NULL:
3562 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3564 case PT_LOAD:
3565 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3566 return false;
3567 if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3568 _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3569 return true;
3571 case PT_DYNAMIC:
3572 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3574 case PT_INTERP:
3575 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3577 case PT_NOTE:
3578 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3579 return false;
3580 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3581 hdr->p_align))
3582 return false;
3583 return true;
3585 case PT_SHLIB:
3586 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3588 case PT_PHDR:
3589 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3591 case PT_GNU_EH_FRAME:
3592 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3593 "eh_frame_hdr");
3595 case PT_GNU_STACK:
3596 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3598 case PT_GNU_RELRO:
3599 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3601 case PT_GNU_SFRAME:
3602 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3603 "sframe");
3605 default:
3606 /* Check for any processor-specific program segment types. */
3607 bed = get_elf_backend_data (abfd);
3608 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3612 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3613 REL or RELA. */
3615 Elf_Internal_Shdr *
3616 _bfd_elf_single_rel_hdr (asection *sec)
3618 if (elf_section_data (sec)->rel.hdr)
3620 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3621 return elf_section_data (sec)->rel.hdr;
3623 else
3624 return elf_section_data (sec)->rela.hdr;
3627 static bool
3628 _bfd_elf_set_reloc_sh_name (bfd *abfd,
3629 Elf_Internal_Shdr *rel_hdr,
3630 const char *sec_name,
3631 bool use_rela_p)
3633 char *name = (char *) bfd_alloc (abfd,
3634 sizeof ".rela" + strlen (sec_name));
3635 if (name == NULL)
3636 return false;
3638 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3639 rel_hdr->sh_name =
3640 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3641 false);
3642 if (rel_hdr->sh_name == (unsigned int) -1)
3643 return false;
3645 return true;
3648 /* Allocate and initialize a section-header for a new reloc section,
3649 containing relocations against ASECT. It is stored in RELDATA. If
3650 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3651 relocations. */
3653 static bool
3654 _bfd_elf_init_reloc_shdr (bfd *abfd,
3655 struct bfd_elf_section_reloc_data *reldata,
3656 const char *sec_name,
3657 bool use_rela_p,
3658 bool delay_st_name_p)
3660 Elf_Internal_Shdr *rel_hdr;
3661 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3663 BFD_ASSERT (reldata->hdr == NULL);
3664 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3665 if (rel_hdr == NULL)
3666 return false;
3667 reldata->hdr = rel_hdr;
3669 if (delay_st_name_p)
3670 rel_hdr->sh_name = (unsigned int) -1;
3671 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3672 use_rela_p))
3673 return false;
3674 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3675 rel_hdr->sh_entsize = (use_rela_p
3676 ? bed->s->sizeof_rela
3677 : bed->s->sizeof_rel);
3678 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3679 rel_hdr->sh_flags = 0;
3680 rel_hdr->sh_addr = 0;
3681 rel_hdr->sh_size = 0;
3682 rel_hdr->sh_offset = 0;
3684 return true;
3687 /* Return the default section type based on the passed in section flags. */
3690 bfd_elf_get_default_section_type (flagword flags)
3692 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3693 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3694 return SHT_NOBITS;
3695 return SHT_PROGBITS;
3698 struct fake_section_arg
3700 struct bfd_link_info *link_info;
3701 bool failed;
3704 /* Set up an ELF internal section header for a section. */
3706 static void
3707 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3709 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3710 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3711 struct bfd_elf_section_data *esd = elf_section_data (asect);
3712 Elf_Internal_Shdr *this_hdr;
3713 unsigned int sh_type;
3714 const char *name = asect->name;
3715 bool delay_st_name_p = false;
3716 bfd_vma mask;
3718 if (arg->failed)
3720 /* We already failed; just get out of the bfd_map_over_sections
3721 loop. */
3722 return;
3725 this_hdr = &esd->this_hdr;
3727 /* ld: compress DWARF debug sections with names: .debug_*. */
3728 if (arg->link_info
3729 && (abfd->flags & BFD_COMPRESS) != 0
3730 && (asect->flags & SEC_DEBUGGING) != 0
3731 && name[1] == 'd'
3732 && name[6] == '_')
3734 /* If this section will be compressed, delay adding section
3735 name to section name section after it is compressed in
3736 _bfd_elf_assign_file_positions_for_non_load. */
3737 delay_st_name_p = true;
3740 if (delay_st_name_p)
3741 this_hdr->sh_name = (unsigned int) -1;
3742 else
3744 this_hdr->sh_name
3745 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3746 name, false);
3747 if (this_hdr->sh_name == (unsigned int) -1)
3749 arg->failed = true;
3750 return;
3754 /* Don't clear sh_flags. Assembler may set additional bits. */
3756 if ((asect->flags & SEC_ALLOC) != 0
3757 || asect->user_set_vma)
3758 this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
3759 else
3760 this_hdr->sh_addr = 0;
3762 this_hdr->sh_offset = 0;
3763 this_hdr->sh_size = asect->size;
3764 this_hdr->sh_link = 0;
3765 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3766 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3768 _bfd_error_handler
3769 /* xgettext:c-format */
3770 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3771 abfd, asect->alignment_power, asect);
3772 arg->failed = true;
3773 return;
3775 /* Set sh_addralign to the highest power of two given by alignment
3776 consistent with the section VMA. Linker scripts can force VMA. */
3777 mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3778 this_hdr->sh_addralign = mask & -mask;
3779 /* The sh_entsize and sh_info fields may have been set already by
3780 copy_private_section_data. */
3782 this_hdr->bfd_section = asect;
3783 this_hdr->contents = NULL;
3785 /* If the section type is unspecified, we set it based on
3786 asect->flags. */
3787 if (asect->type != 0)
3788 sh_type = asect->type;
3789 else if ((asect->flags & SEC_GROUP) != 0)
3790 sh_type = SHT_GROUP;
3791 else
3792 sh_type = bfd_elf_get_default_section_type (asect->flags);
3794 if (this_hdr->sh_type == SHT_NULL)
3795 this_hdr->sh_type = sh_type;
3796 else if (this_hdr->sh_type == SHT_NOBITS
3797 && sh_type == SHT_PROGBITS
3798 && (asect->flags & SEC_ALLOC) != 0)
3800 /* Warn if we are changing a NOBITS section to PROGBITS, but
3801 allow the link to proceed. This can happen when users link
3802 non-bss input sections to bss output sections, or emit data
3803 to a bss output section via a linker script. */
3804 _bfd_error_handler
3805 (_("warning: section `%pA' type changed to PROGBITS"), asect);
3806 this_hdr->sh_type = sh_type;
3809 switch (this_hdr->sh_type)
3811 default:
3812 break;
3814 case SHT_STRTAB:
3815 case SHT_NOTE:
3816 case SHT_NOBITS:
3817 case SHT_PROGBITS:
3818 break;
3820 case SHT_INIT_ARRAY:
3821 case SHT_FINI_ARRAY:
3822 case SHT_PREINIT_ARRAY:
3823 this_hdr->sh_entsize = bed->s->arch_size / 8;
3824 break;
3826 case SHT_HASH:
3827 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3828 break;
3830 case SHT_DYNSYM:
3831 this_hdr->sh_entsize = bed->s->sizeof_sym;
3832 break;
3834 case SHT_DYNAMIC:
3835 this_hdr->sh_entsize = bed->s->sizeof_dyn;
3836 break;
3838 case SHT_RELA:
3839 if (get_elf_backend_data (abfd)->may_use_rela_p)
3840 this_hdr->sh_entsize = bed->s->sizeof_rela;
3841 break;
3843 case SHT_REL:
3844 if (get_elf_backend_data (abfd)->may_use_rel_p)
3845 this_hdr->sh_entsize = bed->s->sizeof_rel;
3846 break;
3848 case SHT_GNU_versym:
3849 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3850 break;
3852 case SHT_GNU_verdef:
3853 this_hdr->sh_entsize = 0;
3854 /* objcopy or strip will copy over sh_info, but may not set
3855 cverdefs. The linker will set cverdefs, but sh_info will be
3856 zero. */
3857 if (this_hdr->sh_info == 0)
3858 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3859 else
3860 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3861 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3862 break;
3864 case SHT_GNU_verneed:
3865 this_hdr->sh_entsize = 0;
3866 /* objcopy or strip will copy over sh_info, but may not set
3867 cverrefs. The linker will set cverrefs, but sh_info will be
3868 zero. */
3869 if (this_hdr->sh_info == 0)
3870 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3871 else
3872 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3873 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3874 break;
3876 case SHT_GROUP:
3877 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3878 break;
3880 case SHT_GNU_HASH:
3881 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3882 break;
3885 if ((asect->flags & SEC_ALLOC) != 0)
3886 this_hdr->sh_flags |= SHF_ALLOC;
3887 if ((asect->flags & SEC_READONLY) == 0)
3888 this_hdr->sh_flags |= SHF_WRITE;
3889 if ((asect->flags & SEC_CODE) != 0)
3890 this_hdr->sh_flags |= SHF_EXECINSTR;
3891 if ((asect->flags & SEC_MERGE) != 0)
3893 this_hdr->sh_flags |= SHF_MERGE;
3894 this_hdr->sh_entsize = asect->entsize;
3896 if ((asect->flags & SEC_STRINGS) != 0)
3897 this_hdr->sh_flags |= SHF_STRINGS;
3898 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3899 this_hdr->sh_flags |= SHF_GROUP;
3900 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3902 this_hdr->sh_flags |= SHF_TLS;
3903 if (asect->size == 0
3904 && (asect->flags & SEC_HAS_CONTENTS) == 0)
3906 struct bfd_link_order *o = asect->map_tail.link_order;
3908 this_hdr->sh_size = 0;
3909 if (o != NULL)
3911 this_hdr->sh_size = o->offset + o->size;
3912 if (this_hdr->sh_size != 0)
3913 this_hdr->sh_type = SHT_NOBITS;
3917 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3918 this_hdr->sh_flags |= SHF_EXCLUDE;
3920 /* If the section has relocs, set up a section header for the
3921 SHT_REL[A] section. If two relocation sections are required for
3922 this section, it is up to the processor-specific back-end to
3923 create the other. */
3924 if ((asect->flags & SEC_RELOC) != 0)
3926 /* When doing a relocatable link, create both REL and RELA sections if
3927 needed. */
3928 if (arg->link_info
3929 /* Do the normal setup if we wouldn't create any sections here. */
3930 && esd->rel.count + esd->rela.count > 0
3931 && (bfd_link_relocatable (arg->link_info)
3932 || arg->link_info->emitrelocations))
3934 if (esd->rel.count && esd->rel.hdr == NULL
3935 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
3936 false, delay_st_name_p))
3938 arg->failed = true;
3939 return;
3941 if (esd->rela.count && esd->rela.hdr == NULL
3942 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
3943 true, delay_st_name_p))
3945 arg->failed = true;
3946 return;
3949 else if (!_bfd_elf_init_reloc_shdr (abfd,
3950 (asect->use_rela_p
3951 ? &esd->rela : &esd->rel),
3952 name,
3953 asect->use_rela_p,
3954 delay_st_name_p))
3956 arg->failed = true;
3957 return;
3961 /* Check for processor-specific section types. */
3962 sh_type = this_hdr->sh_type;
3963 if (bed->elf_backend_fake_sections
3964 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3966 arg->failed = true;
3967 return;
3970 if (sh_type == SHT_NOBITS && asect->size != 0)
3972 /* Don't change the header type from NOBITS if we are being
3973 called for objcopy --only-keep-debug. */
3974 this_hdr->sh_type = sh_type;
3978 /* Fill in the contents of a SHT_GROUP section. Called from
3979 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3980 when ELF targets use the generic linker, ld. Called for ld -r
3981 from bfd_elf_final_link. */
3983 void
3984 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3986 bool *failedptr = (bool *) failedptrarg;
3987 asection *elt, *first;
3988 unsigned char *loc;
3989 bool gas;
3991 /* Ignore linker created group section. See elfNN_ia64_object_p in
3992 elfxx-ia64.c. */
3993 if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3994 || sec->size == 0
3995 || *failedptr)
3996 return;
3998 if (elf_section_data (sec)->this_hdr.sh_info == 0)
4000 unsigned long symindx = 0;
4002 /* elf_group_id will have been set up by objcopy and the
4003 generic linker. */
4004 if (elf_group_id (sec) != NULL)
4005 symindx = elf_group_id (sec)->udata.i;
4007 if (symindx == 0)
4009 /* If called from the assembler, swap_out_syms will have set up
4010 elf_section_syms.
4011 PR 25699: A corrupt input file could contain bogus group info. */
4012 if (sec->index >= elf_num_section_syms (abfd)
4013 || elf_section_syms (abfd)[sec->index] == NULL)
4015 *failedptr = true;
4016 return;
4018 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
4020 elf_section_data (sec)->this_hdr.sh_info = symindx;
4022 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
4024 /* The ELF backend linker sets sh_info to -2 when the group
4025 signature symbol is global, and thus the index can't be
4026 set until all local symbols are output. */
4027 asection *igroup;
4028 struct bfd_elf_section_data *sec_data;
4029 unsigned long symndx;
4030 unsigned long extsymoff;
4031 struct elf_link_hash_entry *h;
4033 /* The point of this little dance to the first SHF_GROUP section
4034 then back to the SHT_GROUP section is that this gets us to
4035 the SHT_GROUP in the input object. */
4036 igroup = elf_sec_group (elf_next_in_group (sec));
4037 sec_data = elf_section_data (igroup);
4038 symndx = sec_data->this_hdr.sh_info;
4039 extsymoff = 0;
4040 if (!elf_bad_symtab (igroup->owner))
4042 Elf_Internal_Shdr *symtab_hdr;
4044 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
4045 extsymoff = symtab_hdr->sh_info;
4047 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
4048 while (h->root.type == bfd_link_hash_indirect
4049 || h->root.type == bfd_link_hash_warning)
4050 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4052 elf_section_data (sec)->this_hdr.sh_info = h->indx;
4055 /* The contents won't be allocated for "ld -r" or objcopy. */
4056 gas = true;
4057 if (sec->contents == NULL)
4059 gas = false;
4060 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
4062 /* Arrange for the section to be written out. */
4063 elf_section_data (sec)->this_hdr.contents = sec->contents;
4064 if (sec->contents == NULL)
4066 *failedptr = true;
4067 return;
4071 loc = sec->contents + sec->size;
4073 /* Get the pointer to the first section in the group that gas
4074 squirreled away here. objcopy arranges for this to be set to the
4075 start of the input section group. */
4076 first = elt = elf_next_in_group (sec);
4078 /* First element is a flag word. Rest of section is elf section
4079 indices for all the sections of the group. Write them backwards
4080 just to keep the group in the same order as given in .section
4081 directives, not that it matters. */
4082 while (elt != NULL)
4084 asection *s;
4086 s = elt;
4087 if (!gas)
4088 s = s->output_section;
4089 if (s != NULL
4090 && !bfd_is_abs_section (s))
4092 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
4093 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
4095 if (elf_sec->rel.hdr != NULL
4096 && (gas
4097 || (input_elf_sec->rel.hdr != NULL
4098 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
4100 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
4101 loc -= 4;
4102 if (loc == sec->contents)
4103 break;
4104 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
4106 if (elf_sec->rela.hdr != NULL
4107 && (gas
4108 || (input_elf_sec->rela.hdr != NULL
4109 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
4111 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
4112 loc -= 4;
4113 if (loc == sec->contents)
4114 break;
4115 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
4117 loc -= 4;
4118 if (loc == sec->contents)
4119 break;
4120 H_PUT_32 (abfd, elf_sec->this_idx, loc);
4122 elt = elf_next_in_group (elt);
4123 if (elt == first)
4124 break;
4127 /* We should always get here with loc == sec->contents + 4, but it is
4128 possible to craft bogus SHT_GROUP sections that will cause segfaults
4129 in objcopy without checking loc here and in the loop above. */
4130 if (loc == sec->contents)
4131 BFD_ASSERT (0);
4132 else
4134 loc -= 4;
4135 if (loc != sec->contents)
4137 BFD_ASSERT (0);
4138 memset (sec->contents + 4, 0, loc - sec->contents);
4139 loc = sec->contents;
4143 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
4146 /* Given NAME, the name of a relocation section stripped of its
4147 .rel/.rela prefix, return the section in ABFD to which the
4148 relocations apply. */
4150 asection *
4151 _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
4153 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
4154 section likely apply to .got.plt or .got section. */
4155 if (get_elf_backend_data (abfd)->want_got_plt
4156 && strcmp (name, ".plt") == 0)
4158 asection *sec;
4160 name = ".got.plt";
4161 sec = bfd_get_section_by_name (abfd, name);
4162 if (sec != NULL)
4163 return sec;
4164 name = ".got";
4167 return bfd_get_section_by_name (abfd, name);
4170 /* Return the section to which RELOC_SEC applies. */
4172 static asection *
4173 elf_get_reloc_section (asection *reloc_sec)
4175 const char *name;
4176 unsigned int type;
4177 bfd *abfd;
4178 const struct elf_backend_data *bed;
4180 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
4181 if (type != SHT_REL && type != SHT_RELA)
4182 return NULL;
4184 /* We look up the section the relocs apply to by name. */
4185 name = reloc_sec->name;
4186 if (!startswith (name, ".rel"))
4187 return NULL;
4188 name += 4;
4189 if (type == SHT_RELA && *name++ != 'a')
4190 return NULL;
4192 abfd = reloc_sec->owner;
4193 bed = get_elf_backend_data (abfd);
4194 return bed->get_reloc_section (abfd, name);
4197 /* Assign all ELF section numbers. The dummy first section is handled here
4198 too. The link/info pointers for the standard section types are filled
4199 in here too, while we're at it. LINK_INFO will be 0 when arriving
4200 here for gas, objcopy, and when using the generic ELF linker. */
4202 static bool
4203 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
4205 struct elf_obj_tdata *t = elf_tdata (abfd);
4206 asection *sec;
4207 unsigned int section_number;
4208 Elf_Internal_Shdr **i_shdrp;
4209 struct bfd_elf_section_data *d;
4210 bool need_symtab;
4211 size_t amt;
4213 section_number = 1;
4215 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
4217 /* SHT_GROUP sections are in relocatable files only. */
4218 if (link_info == NULL || !link_info->resolve_section_groups)
4220 size_t reloc_count = 0;
4222 /* Put SHT_GROUP sections first. */
4223 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4225 d = elf_section_data (sec);
4227 if (d->this_hdr.sh_type == SHT_GROUP)
4229 if (sec->flags & SEC_LINKER_CREATED)
4231 /* Remove the linker created SHT_GROUP sections. */
4232 bfd_section_list_remove (abfd, sec);
4233 abfd->section_count--;
4235 else
4236 d->this_idx = section_number++;
4239 /* Count relocations. */
4240 reloc_count += sec->reloc_count;
4243 /* Set/clear HAS_RELOC depending on whether there are relocations. */
4244 if (reloc_count == 0)
4245 abfd->flags &= ~HAS_RELOC;
4246 else
4247 abfd->flags |= HAS_RELOC;
4250 for (sec = abfd->sections; sec; sec = sec->next)
4252 d = elf_section_data (sec);
4254 if (d->this_hdr.sh_type != SHT_GROUP)
4255 d->this_idx = section_number++;
4256 if (d->this_hdr.sh_name != (unsigned int) -1)
4257 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
4258 if (d->rel.hdr)
4260 d->rel.idx = section_number++;
4261 if (d->rel.hdr->sh_name != (unsigned int) -1)
4262 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
4264 else
4265 d->rel.idx = 0;
4267 if (d->rela.hdr)
4269 d->rela.idx = section_number++;
4270 if (d->rela.hdr->sh_name != (unsigned int) -1)
4271 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
4273 else
4274 d->rela.idx = 0;
4277 need_symtab = (bfd_get_symcount (abfd) > 0
4278 || (link_info == NULL
4279 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4280 == HAS_RELOC)));
4281 if (need_symtab)
4283 elf_onesymtab (abfd) = section_number++;
4284 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4285 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
4287 elf_section_list *entry;
4289 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
4291 entry = bfd_zalloc (abfd, sizeof (*entry));
4292 entry->ndx = section_number++;
4293 elf_symtab_shndx_list (abfd) = entry;
4294 entry->hdr.sh_name
4295 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
4296 ".symtab_shndx", false);
4297 if (entry->hdr.sh_name == (unsigned int) -1)
4298 return false;
4300 elf_strtab_sec (abfd) = section_number++;
4301 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
4304 elf_shstrtab_sec (abfd) = section_number++;
4305 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
4306 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
4308 if (section_number >= SHN_LORESERVE)
4310 /* xgettext:c-format */
4311 _bfd_error_handler (_("%pB: too many sections: %u"),
4312 abfd, section_number);
4313 return false;
4316 elf_numsections (abfd) = section_number;
4317 elf_elfheader (abfd)->e_shnum = section_number;
4319 /* Set up the list of section header pointers, in agreement with the
4320 indices. */
4321 amt = section_number * sizeof (Elf_Internal_Shdr *);
4322 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
4323 if (i_shdrp == NULL)
4324 return false;
4326 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
4327 sizeof (Elf_Internal_Shdr));
4328 if (i_shdrp[0] == NULL)
4330 bfd_release (abfd, i_shdrp);
4331 return false;
4334 elf_elfsections (abfd) = i_shdrp;
4336 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
4337 if (need_symtab)
4339 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4340 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
4342 elf_section_list * entry = elf_symtab_shndx_list (abfd);
4343 BFD_ASSERT (entry != NULL);
4344 i_shdrp[entry->ndx] = & entry->hdr;
4345 entry->hdr.sh_link = elf_onesymtab (abfd);
4347 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
4348 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
4351 for (sec = abfd->sections; sec; sec = sec->next)
4353 asection *s;
4355 d = elf_section_data (sec);
4357 i_shdrp[d->this_idx] = &d->this_hdr;
4358 if (d->rel.idx != 0)
4359 i_shdrp[d->rel.idx] = d->rel.hdr;
4360 if (d->rela.idx != 0)
4361 i_shdrp[d->rela.idx] = d->rela.hdr;
4363 /* Fill in the sh_link and sh_info fields while we're at it. */
4365 /* sh_link of a reloc section is the section index of the symbol
4366 table. sh_info is the section index of the section to which
4367 the relocation entries apply. */
4368 if (d->rel.idx != 0)
4370 d->rel.hdr->sh_link = elf_onesymtab (abfd);
4371 d->rel.hdr->sh_info = d->this_idx;
4372 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
4374 if (d->rela.idx != 0)
4376 d->rela.hdr->sh_link = elf_onesymtab (abfd);
4377 d->rela.hdr->sh_info = d->this_idx;
4378 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
4381 /* We need to set up sh_link for SHF_LINK_ORDER. */
4382 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
4384 s = elf_linked_to_section (sec);
4385 /* We can now have a NULL linked section pointer.
4386 This happens when the sh_link field is 0, which is done
4387 when a linked to section is discarded but the linking
4388 section has been retained for some reason. */
4389 if (s)
4391 /* Check discarded linkonce section. */
4392 if (discarded_section (s))
4394 asection *kept;
4395 _bfd_error_handler
4396 /* xgettext:c-format */
4397 (_("%pB: sh_link of section `%pA' points to"
4398 " discarded section `%pA' of `%pB'"),
4399 abfd, d->this_hdr.bfd_section, s, s->owner);
4400 /* Point to the kept section if it has the same
4401 size as the discarded one. */
4402 kept = _bfd_elf_check_kept_section (s, link_info);
4403 if (kept == NULL)
4405 bfd_set_error (bfd_error_bad_value);
4406 return false;
4408 s = kept;
4410 /* Handle objcopy. */
4411 else if (s->output_section == NULL)
4413 _bfd_error_handler
4414 /* xgettext:c-format */
4415 (_("%pB: sh_link of section `%pA' points to"
4416 " removed section `%pA' of `%pB'"),
4417 abfd, d->this_hdr.bfd_section, s, s->owner);
4418 bfd_set_error (bfd_error_bad_value);
4419 return false;
4421 s = s->output_section;
4422 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4426 switch (d->this_hdr.sh_type)
4428 case SHT_REL:
4429 case SHT_RELA:
4430 /* sh_link is the section index of the symbol table.
4431 sh_info is the section index of the section to which the
4432 relocation entries apply. */
4433 if (d->this_hdr.sh_link == 0)
4435 /* FIXME maybe: If this is a reloc section which we are
4436 treating as a normal section then we likely should
4437 not be assuming its sh_link is .dynsym or .symtab. */
4438 if ((sec->flags & SEC_ALLOC) != 0)
4440 s = bfd_get_section_by_name (abfd, ".dynsym");
4441 if (s != NULL)
4442 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4444 else
4445 d->this_hdr.sh_link = elf_onesymtab (abfd);
4448 s = elf_get_reloc_section (sec);
4449 if (s != NULL)
4451 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
4452 d->this_hdr.sh_flags |= SHF_INFO_LINK;
4454 break;
4456 case SHT_STRTAB:
4457 /* We assume that a section named .stab*str is a stabs
4458 string section. We look for a section with the same name
4459 but without the trailing ``str'', and set its sh_link
4460 field to point to this section. */
4461 if (startswith (sec->name, ".stab")
4462 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
4464 size_t len;
4465 char *alc;
4467 len = strlen (sec->name);
4468 alc = (char *) bfd_malloc (len - 2);
4469 if (alc == NULL)
4470 return false;
4471 memcpy (alc, sec->name, len - 3);
4472 alc[len - 3] = '\0';
4473 s = bfd_get_section_by_name (abfd, alc);
4474 free (alc);
4475 if (s != NULL)
4477 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
4479 /* This is a .stab section. */
4480 elf_section_data (s)->this_hdr.sh_entsize = 12;
4483 break;
4485 case SHT_DYNAMIC:
4486 case SHT_DYNSYM:
4487 case SHT_GNU_verneed:
4488 case SHT_GNU_verdef:
4489 /* sh_link is the section header index of the string table
4490 used for the dynamic entries, or the symbol table, or the
4491 version strings. */
4492 s = bfd_get_section_by_name (abfd, ".dynstr");
4493 if (s != NULL)
4494 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4495 break;
4497 case SHT_GNU_LIBLIST:
4498 /* sh_link is the section header index of the prelink library
4499 list used for the dynamic entries, or the symbol table, or
4500 the version strings. */
4501 s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
4502 ? ".dynstr" : ".gnu.libstr"));
4503 if (s != NULL)
4504 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4505 break;
4507 case SHT_HASH:
4508 case SHT_GNU_HASH:
4509 case SHT_GNU_versym:
4510 /* sh_link is the section header index of the symbol table
4511 this hash table or version table is for. */
4512 s = bfd_get_section_by_name (abfd, ".dynsym");
4513 if (s != NULL)
4514 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4515 break;
4517 case SHT_GROUP:
4518 d->this_hdr.sh_link = elf_onesymtab (abfd);
4522 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4523 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4524 debug section name from .debug_* to .zdebug_* if needed. */
4526 return true;
4529 static bool
4530 sym_is_global (bfd *abfd, asymbol *sym)
4532 /* If the backend has a special mapping, use it. */
4533 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4534 if (bed->elf_backend_sym_is_global)
4535 return (*bed->elf_backend_sym_is_global) (abfd, sym);
4537 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
4538 || bfd_is_und_section (bfd_asymbol_section (sym))
4539 || bfd_is_com_section (bfd_asymbol_section (sym)));
4542 /* Filter global symbols of ABFD to include in the import library. All
4543 SYMCOUNT symbols of ABFD can be examined from their pointers in
4544 SYMS. Pointers of symbols to keep should be stored contiguously at
4545 the beginning of that array.
4547 Returns the number of symbols to keep. */
4549 unsigned int
4550 _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4551 asymbol **syms, long symcount)
4553 long src_count, dst_count = 0;
4555 for (src_count = 0; src_count < symcount; src_count++)
4557 asymbol *sym = syms[src_count];
4558 char *name = (char *) bfd_asymbol_name (sym);
4559 struct bfd_link_hash_entry *h;
4561 if (!sym_is_global (abfd, sym))
4562 continue;
4564 h = bfd_link_hash_lookup (info->hash, name, false, false, false);
4565 if (h == NULL)
4566 continue;
4567 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4568 continue;
4569 if (h->linker_def || h->ldscript_def)
4570 continue;
4572 syms[dst_count++] = sym;
4575 syms[dst_count] = NULL;
4577 return dst_count;
4580 /* Don't output section symbols for sections that are not going to be
4581 output, that are duplicates or there is no BFD section. */
4583 static bool
4584 ignore_section_sym (bfd *abfd, asymbol *sym)
4586 elf_symbol_type *type_ptr;
4588 if (sym == NULL)
4589 return false;
4591 if ((sym->flags & BSF_SECTION_SYM) == 0)
4592 return false;
4594 /* Ignore the section symbol if it isn't used. */
4595 if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
4596 return true;
4598 if (sym->section == NULL)
4599 return true;
4601 type_ptr = elf_symbol_from (sym);
4602 return ((type_ptr != NULL
4603 && type_ptr->internal_elf_sym.st_shndx != 0
4604 && bfd_is_abs_section (sym->section))
4605 || !(sym->section->owner == abfd
4606 || (sym->section->output_section != NULL
4607 && sym->section->output_section->owner == abfd
4608 && sym->section->output_offset == 0)
4609 || bfd_is_abs_section (sym->section)));
4612 /* Map symbol from it's internal number to the external number, moving
4613 all local symbols to be at the head of the list. */
4615 static bool
4616 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4618 unsigned int symcount = bfd_get_symcount (abfd);
4619 asymbol **syms = bfd_get_outsymbols (abfd);
4620 asymbol **sect_syms;
4621 unsigned int num_locals = 0;
4622 unsigned int num_globals = 0;
4623 unsigned int num_locals2 = 0;
4624 unsigned int num_globals2 = 0;
4625 unsigned int max_index = 0;
4626 unsigned int idx;
4627 asection *asect;
4628 asymbol **new_syms;
4629 size_t amt;
4631 #ifdef DEBUG
4632 fprintf (stderr, "elf_map_symbols\n");
4633 fflush (stderr);
4634 #endif
4636 for (asect = abfd->sections; asect; asect = asect->next)
4638 if (max_index < asect->index)
4639 max_index = asect->index;
4642 max_index++;
4643 amt = max_index * sizeof (asymbol *);
4644 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
4645 if (sect_syms == NULL)
4646 return false;
4647 elf_section_syms (abfd) = sect_syms;
4648 elf_num_section_syms (abfd) = max_index;
4650 /* Init sect_syms entries for any section symbols we have already
4651 decided to output. */
4652 for (idx = 0; idx < symcount; idx++)
4654 asymbol *sym = syms[idx];
4656 if ((sym->flags & BSF_SECTION_SYM) != 0
4657 && sym->value == 0
4658 && !ignore_section_sym (abfd, sym)
4659 && !bfd_is_abs_section (sym->section))
4661 asection *sec = sym->section;
4663 if (sec->owner != abfd)
4664 sec = sec->output_section;
4666 sect_syms[sec->index] = syms[idx];
4670 /* Classify all of the symbols. */
4671 for (idx = 0; idx < symcount; idx++)
4673 if (sym_is_global (abfd, syms[idx]))
4674 num_globals++;
4675 else if (!ignore_section_sym (abfd, syms[idx]))
4676 num_locals++;
4679 /* We will be adding a section symbol for each normal BFD section. Most
4680 sections will already have a section symbol in outsymbols, but
4681 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4682 at least in that case. */
4683 for (asect = abfd->sections; asect; asect = asect->next)
4685 asymbol *sym = asect->symbol;
4686 /* Don't include ignored section symbols. */
4687 if (!ignore_section_sym (abfd, sym)
4688 && sect_syms[asect->index] == NULL)
4690 if (!sym_is_global (abfd, asect->symbol))
4691 num_locals++;
4692 else
4693 num_globals++;
4697 /* Now sort the symbols so the local symbols are first. */
4698 amt = (num_locals + num_globals) * sizeof (asymbol *);
4699 new_syms = (asymbol **) bfd_alloc (abfd, amt);
4700 if (new_syms == NULL)
4701 return false;
4703 for (idx = 0; idx < symcount; idx++)
4705 asymbol *sym = syms[idx];
4706 unsigned int i;
4708 if (sym_is_global (abfd, sym))
4709 i = num_locals + num_globals2++;
4710 /* Don't include ignored section symbols. */
4711 else if (!ignore_section_sym (abfd, sym))
4712 i = num_locals2++;
4713 else
4714 continue;
4715 new_syms[i] = sym;
4716 sym->udata.i = i + 1;
4718 for (asect = abfd->sections; asect; asect = asect->next)
4720 asymbol *sym = asect->symbol;
4721 if (!ignore_section_sym (abfd, sym)
4722 && sect_syms[asect->index] == NULL)
4724 unsigned int i;
4726 sect_syms[asect->index] = sym;
4727 if (!sym_is_global (abfd, sym))
4728 i = num_locals2++;
4729 else
4730 i = num_locals + num_globals2++;
4731 new_syms[i] = sym;
4732 sym->udata.i = i + 1;
4736 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4738 *pnum_locals = num_locals;
4739 return true;
4742 /* Align to the maximum file alignment that could be required for any
4743 ELF data structure. */
4745 static inline file_ptr
4746 align_file_position (file_ptr off, int align)
4748 return (off + align - 1) & ~(align - 1);
4751 /* Assign a file position to a section, optionally aligning to the
4752 required section alignment. */
4754 file_ptr
4755 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4756 file_ptr offset,
4757 bool align)
4759 if (align && i_shdrp->sh_addralign > 1)
4760 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign & -i_shdrp->sh_addralign);
4761 i_shdrp->sh_offset = offset;
4762 if (i_shdrp->bfd_section != NULL)
4763 i_shdrp->bfd_section->filepos = offset;
4764 if (i_shdrp->sh_type != SHT_NOBITS)
4765 offset += i_shdrp->sh_size;
4766 return offset;
4769 /* Compute the file positions we are going to put the sections at, and
4770 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4771 is not NULL, this is being called by the ELF backend linker. */
4773 bool
4774 _bfd_elf_compute_section_file_positions (bfd *abfd,
4775 struct bfd_link_info *link_info)
4777 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4778 struct fake_section_arg fsargs;
4779 bool failed;
4780 struct elf_strtab_hash *strtab = NULL;
4781 Elf_Internal_Shdr *shstrtab_hdr;
4782 bool need_symtab;
4784 if (abfd->output_has_begun)
4785 return true;
4787 /* Do any elf backend specific processing first. */
4788 if (bed->elf_backend_begin_write_processing)
4789 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4791 if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
4792 return false;
4794 fsargs.failed = false;
4795 fsargs.link_info = link_info;
4796 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4797 if (fsargs.failed)
4798 return false;
4800 if (!assign_section_numbers (abfd, link_info))
4801 return false;
4803 /* The backend linker builds symbol table information itself. */
4804 need_symtab = (link_info == NULL
4805 && (bfd_get_symcount (abfd) > 0
4806 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4807 == HAS_RELOC)));
4808 if (need_symtab)
4810 /* Non-zero if doing a relocatable link. */
4811 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4813 if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
4814 return false;
4817 failed = false;
4818 if (link_info == NULL)
4820 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4821 if (failed)
4822 goto err_free_strtab;
4825 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4826 /* sh_name was set in init_file_header. */
4827 shstrtab_hdr->sh_type = SHT_STRTAB;
4828 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
4829 shstrtab_hdr->sh_addr = 0;
4830 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4831 shstrtab_hdr->sh_entsize = 0;
4832 shstrtab_hdr->sh_link = 0;
4833 shstrtab_hdr->sh_info = 0;
4834 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4835 shstrtab_hdr->sh_addralign = 1;
4837 if (!assign_file_positions_except_relocs (abfd, link_info))
4838 goto err_free_strtab;
4840 if (strtab != NULL)
4842 file_ptr off;
4843 Elf_Internal_Shdr *hdr;
4845 off = elf_next_file_pos (abfd);
4847 hdr = & elf_symtab_hdr (abfd);
4848 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4850 if (elf_symtab_shndx_list (abfd) != NULL)
4852 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4853 if (hdr->sh_size != 0)
4854 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4855 /* FIXME: What about other symtab_shndx sections in the list ? */
4858 hdr = &elf_tdata (abfd)->strtab_hdr;
4859 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4861 elf_next_file_pos (abfd) = off;
4863 /* Now that we know where the .strtab section goes, write it
4864 out. */
4865 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4866 || ! _bfd_elf_strtab_emit (abfd, strtab))
4867 goto err_free_strtab;
4868 _bfd_elf_strtab_free (strtab);
4871 abfd->output_has_begun = true;
4872 return true;
4874 err_free_strtab:
4875 if (strtab != NULL)
4876 _bfd_elf_strtab_free (strtab);
4877 return false;
4880 /* Retrieve .eh_frame_hdr. Prior to size_dynamic_sections the
4881 function effectively returns whether --eh-frame-hdr is given on the
4882 command line. After size_dynamic_sections the result reflects
4883 whether .eh_frame_hdr will actually be output (sizing isn't done
4884 until ldemul_after_allocation). */
4886 static asection *
4887 elf_eh_frame_hdr (const struct bfd_link_info *info)
4889 if (info != NULL && is_elf_hash_table (info->hash))
4890 return elf_hash_table (info)->eh_info.hdr_sec;
4891 return NULL;
4894 /* Make an initial estimate of the size of the program header. If we
4895 get the number wrong here, we'll redo section placement. */
4897 static bfd_size_type
4898 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4900 size_t segs;
4901 asection *s;
4902 const struct elf_backend_data *bed;
4904 /* Assume we will need exactly two PT_LOAD segments: one for text
4905 and one for data. */
4906 segs = 2;
4908 s = bfd_get_section_by_name (abfd, ".interp");
4909 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4911 /* If we have a loadable interpreter section, we need a
4912 PT_INTERP segment. In this case, assume we also need a
4913 PT_PHDR segment, although that may not be true for all
4914 targets. */
4915 segs += 2;
4918 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4920 /* We need a PT_DYNAMIC segment. */
4921 ++segs;
4924 if (info != NULL && info->relro)
4926 /* We need a PT_GNU_RELRO segment. */
4927 ++segs;
4930 if (elf_eh_frame_hdr (info))
4932 /* We need a PT_GNU_EH_FRAME segment. */
4933 ++segs;
4936 if (elf_stack_flags (abfd))
4938 /* We need a PT_GNU_STACK segment. */
4939 ++segs;
4942 if (elf_sframe (abfd))
4944 /* We need a PT_GNU_SFRAME segment. */
4945 ++segs;
4948 s = bfd_get_section_by_name (abfd,
4949 NOTE_GNU_PROPERTY_SECTION_NAME);
4950 if (s != NULL && s->size != 0)
4952 /* We need a PT_GNU_PROPERTY segment. */
4953 ++segs;
4956 for (s = abfd->sections; s != NULL; s = s->next)
4958 if ((s->flags & SEC_LOAD) != 0
4959 && elf_section_type (s) == SHT_NOTE)
4961 unsigned int alignment_power;
4962 /* We need a PT_NOTE segment. */
4963 ++segs;
4964 /* Try to create just one PT_NOTE segment for all adjacent
4965 loadable SHT_NOTE sections. gABI requires that within a
4966 PT_NOTE segment (and also inside of each SHT_NOTE section)
4967 each note should have the same alignment. So we check
4968 whether the sections are correctly aligned. */
4969 alignment_power = s->alignment_power;
4970 while (s->next != NULL
4971 && s->next->alignment_power == alignment_power
4972 && (s->next->flags & SEC_LOAD) != 0
4973 && elf_section_type (s->next) == SHT_NOTE)
4974 s = s->next;
4978 for (s = abfd->sections; s != NULL; s = s->next)
4980 if (s->flags & SEC_THREAD_LOCAL)
4982 /* We need a PT_TLS segment. */
4983 ++segs;
4984 break;
4988 bed = get_elf_backend_data (abfd);
4990 if ((abfd->flags & D_PAGED) != 0
4991 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4993 /* Add a PT_GNU_MBIND segment for each mbind section. */
4994 bfd_vma commonpagesize;
4995 unsigned int page_align_power;
4997 if (info != NULL)
4998 commonpagesize = info->commonpagesize;
4999 else
5000 commonpagesize = bed->commonpagesize;
5001 page_align_power = bfd_log2 (commonpagesize);
5002 for (s = abfd->sections; s != NULL; s = s->next)
5003 if (elf_section_flags (s) & SHF_GNU_MBIND)
5005 if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
5007 _bfd_error_handler
5008 /* xgettext:c-format */
5009 (_("%pB: GNU_MBIND section `%pA' has invalid "
5010 "sh_info field: %d"),
5011 abfd, s, elf_section_data (s)->this_hdr.sh_info);
5012 continue;
5014 /* Align mbind section to page size. */
5015 if (s->alignment_power < page_align_power)
5016 s->alignment_power = page_align_power;
5017 segs ++;
5021 /* Let the backend count up any program headers it might need. */
5022 if (bed->elf_backend_additional_program_headers)
5024 int a;
5026 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
5027 if (a == -1)
5028 abort ();
5029 segs += a;
5032 return segs * bed->s->sizeof_phdr;
5035 /* Find the segment that contains the output_section of section. */
5037 Elf_Internal_Phdr *
5038 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
5040 struct elf_segment_map *m;
5041 Elf_Internal_Phdr *p;
5043 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
5044 m != NULL;
5045 m = m->next, p++)
5047 int i;
5049 for (i = m->count - 1; i >= 0; i--)
5050 if (m->sections[i] == section)
5051 return p;
5054 return NULL;
5057 /* Create a mapping from a set of sections to a program segment. */
5059 static struct elf_segment_map *
5060 make_mapping (bfd *abfd,
5061 asection **sections,
5062 unsigned int from,
5063 unsigned int to,
5064 bool phdr)
5066 struct elf_segment_map *m;
5067 unsigned int i;
5068 asection **hdrpp;
5069 size_t amt;
5071 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5072 amt += (to - from) * sizeof (asection *);
5073 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5074 if (m == NULL)
5075 return NULL;
5076 m->next = NULL;
5077 m->p_type = PT_LOAD;
5078 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
5079 m->sections[i - from] = *hdrpp;
5080 m->count = to - from;
5082 if (from == 0 && phdr)
5084 /* Include the headers in the first PT_LOAD segment. */
5085 m->includes_filehdr = 1;
5086 m->includes_phdrs = 1;
5089 return m;
5092 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
5093 on failure. */
5095 struct elf_segment_map *
5096 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
5098 struct elf_segment_map *m;
5100 m = (struct elf_segment_map *) bfd_zalloc (abfd,
5101 sizeof (struct elf_segment_map));
5102 if (m == NULL)
5103 return NULL;
5104 m->next = NULL;
5105 m->p_type = PT_DYNAMIC;
5106 m->count = 1;
5107 m->sections[0] = dynsec;
5109 return m;
5112 /* Possibly add or remove segments from the segment map. */
5114 static bool
5115 elf_modify_segment_map (bfd *abfd,
5116 struct bfd_link_info *info,
5117 bool remove_empty_load)
5119 struct elf_segment_map **m;
5120 const struct elf_backend_data *bed;
5122 /* The placement algorithm assumes that non allocated sections are
5123 not in PT_LOAD segments. We ensure this here by removing such
5124 sections from the segment map. We also remove excluded
5125 sections. Finally, any PT_LOAD segment without sections is
5126 removed. */
5127 m = &elf_seg_map (abfd);
5128 while (*m)
5130 unsigned int i, new_count;
5132 for (new_count = 0, i = 0; i < (*m)->count; i++)
5134 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
5135 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
5136 || (*m)->p_type != PT_LOAD))
5138 (*m)->sections[new_count] = (*m)->sections[i];
5139 new_count++;
5142 (*m)->count = new_count;
5144 if (remove_empty_load
5145 && (*m)->p_type == PT_LOAD
5146 && (*m)->count == 0
5147 && !(*m)->includes_phdrs)
5148 *m = (*m)->next;
5149 else
5150 m = &(*m)->next;
5153 bed = get_elf_backend_data (abfd);
5154 if (bed->elf_backend_modify_segment_map != NULL)
5156 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
5157 return false;
5160 return true;
5163 #define IS_TBSS(s) \
5164 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
5166 /* Set up a mapping from BFD sections to program segments. Update
5167 NEED_LAYOUT if the section layout is changed. */
5169 bool
5170 _bfd_elf_map_sections_to_segments (bfd *abfd,
5171 struct bfd_link_info *info,
5172 bool *need_layout)
5174 unsigned int count;
5175 struct elf_segment_map *m;
5176 asection **sections = NULL;
5177 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5178 bool no_user_phdrs;
5180 no_user_phdrs = elf_seg_map (abfd) == NULL;
5182 if (info != NULL)
5184 info->user_phdrs = !no_user_phdrs;
5186 /* Size the relative relocations if DT_RELR is enabled. */
5187 if (info->enable_dt_relr
5188 && need_layout != NULL
5189 && bed->size_relative_relocs
5190 && !bed->size_relative_relocs (info, need_layout))
5191 info->callbacks->einfo
5192 (_("%F%P: failed to size relative relocations\n"));
5195 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
5197 asection *s;
5198 unsigned int i;
5199 struct elf_segment_map *mfirst;
5200 struct elf_segment_map **pm;
5201 asection *last_hdr;
5202 bfd_vma last_size;
5203 unsigned int hdr_index;
5204 bfd_vma maxpagesize;
5205 asection **hdrpp;
5206 bool phdr_in_segment;
5207 bool writable;
5208 bool executable;
5209 unsigned int tls_count = 0;
5210 asection *first_tls = NULL;
5211 asection *first_mbind = NULL;
5212 asection *dynsec, *eh_frame_hdr;
5213 asection *sframe;
5214 size_t amt;
5215 bfd_vma addr_mask, wrap_to = 0; /* Bytes. */
5216 bfd_size_type phdr_size; /* Octets/bytes. */
5217 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5219 /* Select the allocated sections, and sort them. */
5221 amt = bfd_count_sections (abfd) * sizeof (asection *);
5222 sections = (asection **) bfd_malloc (amt);
5223 if (sections == NULL)
5224 goto error_return;
5226 /* Calculate top address, avoiding undefined behaviour of shift
5227 left operator when shift count is equal to size of type
5228 being shifted. */
5229 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
5230 addr_mask = (addr_mask << 1) + 1;
5232 i = 0;
5233 for (s = abfd->sections; s != NULL; s = s->next)
5235 if ((s->flags & SEC_ALLOC) != 0)
5237 /* target_index is unused until bfd_elf_final_link
5238 starts output of section symbols. Use it to make
5239 qsort stable. */
5240 s->target_index = i;
5241 sections[i] = s;
5242 ++i;
5243 /* A wrapping section potentially clashes with header. */
5244 if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
5245 wrap_to = (s->lma + s->size / opb) & addr_mask;
5248 BFD_ASSERT (i <= bfd_count_sections (abfd));
5249 count = i;
5251 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
5253 phdr_size = elf_program_header_size (abfd);
5254 if (phdr_size == (bfd_size_type) -1)
5255 phdr_size = get_program_header_size (abfd, info);
5256 phdr_size += bed->s->sizeof_ehdr;
5257 /* phdr_size is compared to LMA values which are in bytes. */
5258 phdr_size /= opb;
5259 if (info != NULL)
5260 maxpagesize = info->maxpagesize;
5261 else
5262 maxpagesize = bed->maxpagesize;
5263 if (maxpagesize == 0)
5264 maxpagesize = 1;
5265 phdr_in_segment = info != NULL && info->load_phdrs;
5266 if (count != 0
5267 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
5268 >= (phdr_size & (maxpagesize - 1))))
5269 /* For compatibility with old scripts that may not be using
5270 SIZEOF_HEADERS, add headers when it looks like space has
5271 been left for them. */
5272 phdr_in_segment = true;
5274 /* Build the mapping. */
5275 mfirst = NULL;
5276 pm = &mfirst;
5278 /* If we have a .interp section, then create a PT_PHDR segment for
5279 the program headers and a PT_INTERP segment for the .interp
5280 section. */
5281 s = bfd_get_section_by_name (abfd, ".interp");
5282 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
5284 amt = sizeof (struct elf_segment_map);
5285 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5286 if (m == NULL)
5287 goto error_return;
5288 m->next = NULL;
5289 m->p_type = PT_PHDR;
5290 m->p_flags = PF_R;
5291 m->p_flags_valid = 1;
5292 m->includes_phdrs = 1;
5293 phdr_in_segment = true;
5294 *pm = m;
5295 pm = &m->next;
5297 amt = sizeof (struct elf_segment_map);
5298 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5299 if (m == NULL)
5300 goto error_return;
5301 m->next = NULL;
5302 m->p_type = PT_INTERP;
5303 m->count = 1;
5304 m->sections[0] = s;
5306 *pm = m;
5307 pm = &m->next;
5310 /* Look through the sections. We put sections in the same program
5311 segment when the start of the second section can be placed within
5312 a few bytes of the end of the first section. */
5313 last_hdr = NULL;
5314 last_size = 0;
5315 hdr_index = 0;
5316 writable = false;
5317 executable = false;
5318 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
5319 if (dynsec != NULL
5320 && (dynsec->flags & SEC_LOAD) == 0)
5321 dynsec = NULL;
5323 if ((abfd->flags & D_PAGED) == 0)
5324 phdr_in_segment = false;
5326 /* Deal with -Ttext or something similar such that the first section
5327 is not adjacent to the program headers. This is an
5328 approximation, since at this point we don't know exactly how many
5329 program headers we will need. */
5330 if (phdr_in_segment && count > 0)
5332 bfd_vma phdr_lma; /* Bytes. */
5333 bool separate_phdr = false;
5335 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
5336 if (info != NULL
5337 && info->separate_code
5338 && (sections[0]->flags & SEC_CODE) != 0)
5340 /* If data sections should be separate from code and
5341 thus not executable, and the first section is
5342 executable then put the file and program headers in
5343 their own PT_LOAD. */
5344 separate_phdr = true;
5345 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
5346 == (sections[0]->lma & addr_mask & -maxpagesize)))
5348 /* The file and program headers are currently on the
5349 same page as the first section. Put them on the
5350 previous page if we can. */
5351 if (phdr_lma >= maxpagesize)
5352 phdr_lma -= maxpagesize;
5353 else
5354 separate_phdr = false;
5357 if ((sections[0]->lma & addr_mask) < phdr_lma
5358 || (sections[0]->lma & addr_mask) < phdr_size)
5359 /* If file and program headers would be placed at the end
5360 of memory then it's probably better to omit them. */
5361 phdr_in_segment = false;
5362 else if (phdr_lma < wrap_to)
5363 /* If a section wraps around to where we'll be placing
5364 file and program headers, then the headers will be
5365 overwritten. */
5366 phdr_in_segment = false;
5367 else if (separate_phdr)
5369 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
5370 if (m == NULL)
5371 goto error_return;
5372 m->p_paddr = phdr_lma * opb;
5373 m->p_vaddr_offset
5374 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
5375 m->p_paddr_valid = 1;
5376 *pm = m;
5377 pm = &m->next;
5378 phdr_in_segment = false;
5382 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
5384 asection *hdr;
5385 bool new_segment;
5387 hdr = *hdrpp;
5389 /* See if this section and the last one will fit in the same
5390 segment. */
5392 if (last_hdr == NULL)
5394 /* If we don't have a segment yet, then we don't need a new
5395 one (we build the last one after this loop). */
5396 new_segment = false;
5398 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
5400 /* If this section has a different relation between the
5401 virtual address and the load address, then we need a new
5402 segment. */
5403 new_segment = true;
5405 else if (hdr->lma < last_hdr->lma + last_size
5406 || last_hdr->lma + last_size < last_hdr->lma)
5408 /* If this section has a load address that makes it overlap
5409 the previous section, then we need a new segment. */
5410 new_segment = true;
5412 else if ((abfd->flags & D_PAGED) != 0
5413 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
5414 == (hdr->lma & -maxpagesize)))
5416 /* If we are demand paged then we can't map two disk
5417 pages onto the same memory page. */
5418 new_segment = false;
5420 /* In the next test we have to be careful when last_hdr->lma is close
5421 to the end of the address space. If the aligned address wraps
5422 around to the start of the address space, then there are no more
5423 pages left in memory and it is OK to assume that the current
5424 section can be included in the current segment. */
5425 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5426 + maxpagesize > last_hdr->lma)
5427 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5428 + maxpagesize <= hdr->lma))
5430 /* If putting this section in this segment would force us to
5431 skip a page in the segment, then we need a new segment. */
5432 new_segment = true;
5434 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
5435 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
5437 /* We don't want to put a loaded section after a
5438 nonloaded (ie. bss style) section in the same segment
5439 as that will force the non-loaded section to be loaded.
5440 Consider .tbss sections as loaded for this purpose. */
5441 new_segment = true;
5443 else if ((abfd->flags & D_PAGED) == 0)
5445 /* If the file is not demand paged, which means that we
5446 don't require the sections to be correctly aligned in the
5447 file, then there is no other reason for a new segment. */
5448 new_segment = false;
5450 else if (info != NULL
5451 && info->separate_code
5452 && executable != ((hdr->flags & SEC_CODE) != 0))
5454 new_segment = true;
5456 else if (! writable
5457 && (hdr->flags & SEC_READONLY) == 0)
5459 /* We don't want to put a writable section in a read only
5460 segment. */
5461 new_segment = true;
5463 else
5465 /* Otherwise, we can use the same segment. */
5466 new_segment = false;
5469 /* Allow interested parties a chance to override our decision. */
5470 if (last_hdr != NULL
5471 && info != NULL
5472 && info->callbacks->override_segment_assignment != NULL)
5473 new_segment
5474 = info->callbacks->override_segment_assignment (info, abfd, hdr,
5475 last_hdr,
5476 new_segment);
5478 if (! new_segment)
5480 if ((hdr->flags & SEC_READONLY) == 0)
5481 writable = true;
5482 if ((hdr->flags & SEC_CODE) != 0)
5483 executable = true;
5484 last_hdr = hdr;
5485 /* .tbss sections effectively have zero size. */
5486 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5487 continue;
5490 /* We need a new program segment. We must create a new program
5491 header holding all the sections from hdr_index until hdr. */
5493 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5494 if (m == NULL)
5495 goto error_return;
5497 *pm = m;
5498 pm = &m->next;
5500 if ((hdr->flags & SEC_READONLY) == 0)
5501 writable = true;
5502 else
5503 writable = false;
5505 if ((hdr->flags & SEC_CODE) == 0)
5506 executable = false;
5507 else
5508 executable = true;
5510 last_hdr = hdr;
5511 /* .tbss sections effectively have zero size. */
5512 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5513 hdr_index = i;
5514 phdr_in_segment = false;
5517 /* Create a final PT_LOAD program segment, but not if it's just
5518 for .tbss. */
5519 if (last_hdr != NULL
5520 && (i - hdr_index != 1
5521 || !IS_TBSS (last_hdr)))
5523 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5524 if (m == NULL)
5525 goto error_return;
5527 *pm = m;
5528 pm = &m->next;
5531 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
5532 if (dynsec != NULL)
5534 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
5535 if (m == NULL)
5536 goto error_return;
5537 *pm = m;
5538 pm = &m->next;
5541 /* For each batch of consecutive loadable SHT_NOTE sections,
5542 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
5543 because if we link together nonloadable .note sections and
5544 loadable .note sections, we will generate two .note sections
5545 in the output file. */
5546 for (s = abfd->sections; s != NULL; s = s->next)
5548 if ((s->flags & SEC_LOAD) != 0
5549 && elf_section_type (s) == SHT_NOTE)
5551 asection *s2;
5552 unsigned int alignment_power = s->alignment_power;
5554 count = 1;
5555 for (s2 = s; s2->next != NULL; s2 = s2->next)
5557 if (s2->next->alignment_power == alignment_power
5558 && (s2->next->flags & SEC_LOAD) != 0
5559 && elf_section_type (s2->next) == SHT_NOTE
5560 && align_power (s2->lma + s2->size / opb,
5561 alignment_power)
5562 == s2->next->lma)
5563 count++;
5564 else
5565 break;
5567 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5568 amt += count * sizeof (asection *);
5569 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5570 if (m == NULL)
5571 goto error_return;
5572 m->next = NULL;
5573 m->p_type = PT_NOTE;
5574 m->count = count;
5575 while (count > 1)
5577 m->sections[m->count - count--] = s;
5578 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5579 s = s->next;
5581 m->sections[m->count - 1] = s;
5582 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5583 *pm = m;
5584 pm = &m->next;
5586 if (s->flags & SEC_THREAD_LOCAL)
5588 if (! tls_count)
5589 first_tls = s;
5590 tls_count++;
5592 if (first_mbind == NULL
5593 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5594 first_mbind = s;
5597 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5598 if (tls_count > 0)
5600 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5601 amt += tls_count * sizeof (asection *);
5602 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5603 if (m == NULL)
5604 goto error_return;
5605 m->next = NULL;
5606 m->p_type = PT_TLS;
5607 m->count = tls_count;
5608 /* Mandated PF_R. */
5609 m->p_flags = PF_R;
5610 m->p_flags_valid = 1;
5611 s = first_tls;
5612 for (i = 0; i < tls_count; ++i)
5614 if ((s->flags & SEC_THREAD_LOCAL) == 0)
5616 _bfd_error_handler
5617 (_("%pB: TLS sections are not adjacent:"), abfd);
5618 s = first_tls;
5619 i = 0;
5620 while (i < tls_count)
5622 if ((s->flags & SEC_THREAD_LOCAL) != 0)
5624 _bfd_error_handler (_(" TLS: %pA"), s);
5625 i++;
5627 else
5628 _bfd_error_handler (_(" non-TLS: %pA"), s);
5629 s = s->next;
5631 bfd_set_error (bfd_error_bad_value);
5632 goto error_return;
5634 m->sections[i] = s;
5635 s = s->next;
5638 *pm = m;
5639 pm = &m->next;
5642 if (first_mbind
5643 && (abfd->flags & D_PAGED) != 0
5644 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
5645 for (s = first_mbind; s != NULL; s = s->next)
5646 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5647 && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
5649 /* Mandated PF_R. */
5650 unsigned long p_flags = PF_R;
5651 if ((s->flags & SEC_READONLY) == 0)
5652 p_flags |= PF_W;
5653 if ((s->flags & SEC_CODE) != 0)
5654 p_flags |= PF_X;
5656 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5657 m = bfd_zalloc (abfd, amt);
5658 if (m == NULL)
5659 goto error_return;
5660 m->next = NULL;
5661 m->p_type = (PT_GNU_MBIND_LO
5662 + elf_section_data (s)->this_hdr.sh_info);
5663 m->count = 1;
5664 m->p_flags_valid = 1;
5665 m->sections[0] = s;
5666 m->p_flags = p_flags;
5668 *pm = m;
5669 pm = &m->next;
5672 s = bfd_get_section_by_name (abfd,
5673 NOTE_GNU_PROPERTY_SECTION_NAME);
5674 if (s != NULL && s->size != 0)
5676 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5677 m = bfd_zalloc (abfd, amt);
5678 if (m == NULL)
5679 goto error_return;
5680 m->next = NULL;
5681 m->p_type = PT_GNU_PROPERTY;
5682 m->count = 1;
5683 m->p_flags_valid = 1;
5684 m->sections[0] = s;
5685 m->p_flags = PF_R;
5686 *pm = m;
5687 pm = &m->next;
5690 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5691 segment. */
5692 eh_frame_hdr = elf_eh_frame_hdr (info);
5693 if (eh_frame_hdr != NULL
5694 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5696 amt = sizeof (struct elf_segment_map);
5697 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5698 if (m == NULL)
5699 goto error_return;
5700 m->next = NULL;
5701 m->p_type = PT_GNU_EH_FRAME;
5702 m->count = 1;
5703 m->sections[0] = eh_frame_hdr->output_section;
5705 *pm = m;
5706 pm = &m->next;
5709 /* If there is a .sframe section, throw in a PT_GNU_SFRAME
5710 segment. */
5711 sframe = elf_sframe (abfd);
5712 if (sframe != NULL
5713 && (sframe->output_section->flags & SEC_LOAD) != 0
5714 && sframe->size != 0)
5716 amt = sizeof (struct elf_segment_map);
5717 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5718 if (m == NULL)
5719 goto error_return;
5720 m->next = NULL;
5721 m->p_type = PT_GNU_SFRAME;
5722 m->count = 1;
5723 m->sections[0] = sframe->output_section;
5725 *pm = m;
5726 pm = &m->next;
5729 if (elf_stack_flags (abfd))
5731 amt = sizeof (struct elf_segment_map);
5732 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5733 if (m == NULL)
5734 goto error_return;
5735 m->next = NULL;
5736 m->p_type = PT_GNU_STACK;
5737 m->p_flags = elf_stack_flags (abfd);
5738 m->p_align = bed->stack_align;
5739 m->p_flags_valid = 1;
5740 m->p_align_valid = m->p_align != 0;
5741 if (info->stacksize > 0)
5743 m->p_size = info->stacksize;
5744 m->p_size_valid = 1;
5747 *pm = m;
5748 pm = &m->next;
5751 if (info != NULL && info->relro)
5753 for (m = mfirst; m != NULL; m = m->next)
5755 if (m->p_type == PT_LOAD
5756 && m->count != 0
5757 && m->sections[0]->vma >= info->relro_start
5758 && m->sections[0]->vma < info->relro_end)
5760 i = m->count;
5761 while (--i != (unsigned) -1)
5763 if (m->sections[i]->size > 0
5764 && (m->sections[i]->flags & SEC_LOAD) != 0
5765 && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
5766 break;
5769 if (i != (unsigned) -1)
5770 break;
5774 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5775 if (m != NULL)
5777 amt = sizeof (struct elf_segment_map);
5778 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5779 if (m == NULL)
5780 goto error_return;
5781 m->next = NULL;
5782 m->p_type = PT_GNU_RELRO;
5783 *pm = m;
5784 pm = &m->next;
5788 free (sections);
5789 elf_seg_map (abfd) = mfirst;
5792 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
5793 return false;
5795 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5796 ++count;
5797 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5799 return true;
5801 error_return:
5802 free (sections);
5803 return false;
5806 /* Sort sections by address. */
5808 static int
5809 elf_sort_sections (const void *arg1, const void *arg2)
5811 const asection *sec1 = *(const asection **) arg1;
5812 const asection *sec2 = *(const asection **) arg2;
5813 bfd_size_type size1, size2;
5815 /* Sort by LMA first, since this is the address used to
5816 place the section into a segment. */
5817 if (sec1->lma < sec2->lma)
5818 return -1;
5819 else if (sec1->lma > sec2->lma)
5820 return 1;
5822 /* Then sort by VMA. Normally the LMA and the VMA will be
5823 the same, and this will do nothing. */
5824 if (sec1->vma < sec2->vma)
5825 return -1;
5826 else if (sec1->vma > sec2->vma)
5827 return 1;
5829 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5831 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5832 && (x)->size != 0)
5834 if (TOEND (sec1))
5836 if (!TOEND (sec2))
5837 return 1;
5839 else if (TOEND (sec2))
5840 return -1;
5842 #undef TOEND
5844 /* Sort by size, to put zero sized sections
5845 before others at the same address. */
5847 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5848 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5850 if (size1 < size2)
5851 return -1;
5852 if (size1 > size2)
5853 return 1;
5855 return sec1->target_index - sec2->target_index;
5858 /* This qsort comparison functions sorts PT_LOAD segments first and
5859 by p_paddr, for assign_file_positions_for_load_sections. */
5861 static int
5862 elf_sort_segments (const void *arg1, const void *arg2)
5864 const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5865 const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5867 if (m1->p_type != m2->p_type)
5869 if (m1->p_type == PT_NULL)
5870 return 1;
5871 if (m2->p_type == PT_NULL)
5872 return -1;
5873 return m1->p_type < m2->p_type ? -1 : 1;
5875 if (m1->includes_filehdr != m2->includes_filehdr)
5876 return m1->includes_filehdr ? -1 : 1;
5877 if (m1->no_sort_lma != m2->no_sort_lma)
5878 return m1->no_sort_lma ? -1 : 1;
5879 if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5881 bfd_vma lma1, lma2; /* Octets. */
5882 lma1 = 0;
5883 if (m1->p_paddr_valid)
5884 lma1 = m1->p_paddr;
5885 else if (m1->count != 0)
5887 unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5888 m1->sections[0]);
5889 lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5891 lma2 = 0;
5892 if (m2->p_paddr_valid)
5893 lma2 = m2->p_paddr;
5894 else if (m2->count != 0)
5896 unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5897 m2->sections[0]);
5898 lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5900 if (lma1 != lma2)
5901 return lma1 < lma2 ? -1 : 1;
5903 if (m1->idx != m2->idx)
5904 return m1->idx < m2->idx ? -1 : 1;
5905 return 0;
5908 /* Ian Lance Taylor writes:
5910 We shouldn't be using % with a negative signed number. That's just
5911 not good. We have to make sure either that the number is not
5912 negative, or that the number has an unsigned type. When the types
5913 are all the same size they wind up as unsigned. When file_ptr is a
5914 larger signed type, the arithmetic winds up as signed long long,
5915 which is wrong.
5917 What we're trying to say here is something like ``increase OFF by
5918 the least amount that will cause it to be equal to the VMA modulo
5919 the page size.'' */
5920 /* In other words, something like:
5922 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5923 off_offset = off % bed->maxpagesize;
5924 if (vma_offset < off_offset)
5925 adjustment = vma_offset + bed->maxpagesize - off_offset;
5926 else
5927 adjustment = vma_offset - off_offset;
5929 which can be collapsed into the expression below. */
5931 static file_ptr
5932 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5934 /* PR binutils/16199: Handle an alignment of zero. */
5935 if (maxpagesize == 0)
5936 maxpagesize = 1;
5937 return ((vma - off) % maxpagesize);
5940 static void
5941 print_segment_map (const struct elf_segment_map *m)
5943 unsigned int j;
5944 const char *pt = get_segment_type (m->p_type);
5945 char buf[32];
5947 if (pt == NULL)
5949 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5950 sprintf (buf, "LOPROC+%7.7x",
5951 (unsigned int) (m->p_type - PT_LOPROC));
5952 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5953 sprintf (buf, "LOOS+%7.7x",
5954 (unsigned int) (m->p_type - PT_LOOS));
5955 else
5956 snprintf (buf, sizeof (buf), "%8.8x",
5957 (unsigned int) m->p_type);
5958 pt = buf;
5960 fflush (stdout);
5961 fprintf (stderr, "%s:", pt);
5962 for (j = 0; j < m->count; j++)
5963 fprintf (stderr, " %s", m->sections [j]->name);
5964 putc ('\n',stderr);
5965 fflush (stderr);
5968 /* Assign file positions to the sections based on the mapping from
5969 sections to segments. This function also sets up some fields in
5970 the file header. */
5972 static bool
5973 assign_file_positions_for_load_sections (bfd *abfd,
5974 struct bfd_link_info *link_info)
5976 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5977 struct elf_segment_map *m;
5978 struct elf_segment_map *phdr_load_seg;
5979 Elf_Internal_Phdr *phdrs;
5980 Elf_Internal_Phdr *p;
5981 file_ptr off; /* Octets. */
5982 bfd_size_type maxpagesize;
5983 unsigned int alloc, actual;
5984 unsigned int i, j;
5985 struct elf_segment_map **sorted_seg_map;
5986 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5988 if (link_info == NULL
5989 && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
5990 return false;
5992 alloc = 0;
5993 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5994 m->idx = alloc++;
5996 if (alloc)
5998 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5999 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
6001 else
6003 /* PR binutils/12467. */
6004 elf_elfheader (abfd)->e_phoff = 0;
6005 elf_elfheader (abfd)->e_phentsize = 0;
6008 elf_elfheader (abfd)->e_phnum = alloc;
6010 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
6012 actual = alloc;
6013 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
6015 else
6017 actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
6018 BFD_ASSERT (elf_program_header_size (abfd)
6019 == actual * bed->s->sizeof_phdr);
6020 BFD_ASSERT (actual >= alloc);
6023 if (alloc == 0)
6025 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
6026 return true;
6029 /* We're writing the size in elf_program_header_size (abfd),
6030 see assign_file_positions_except_relocs, so make sure we have
6031 that amount allocated, with trailing space cleared.
6032 The variable alloc contains the computed need, while
6033 elf_program_header_size (abfd) contains the size used for the
6034 layout.
6035 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
6036 where the layout is forced to according to a larger size in the
6037 last iterations for the testcase ld-elf/header. */
6038 phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
6039 + alloc * sizeof (*sorted_seg_map)));
6040 sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
6041 elf_tdata (abfd)->phdr = phdrs;
6042 if (phdrs == NULL)
6043 return false;
6045 for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
6047 sorted_seg_map[j] = m;
6048 /* If elf_segment_map is not from map_sections_to_segments, the
6049 sections may not be correctly ordered. NOTE: sorting should
6050 not be done to the PT_NOTE section of a corefile, which may
6051 contain several pseudo-sections artificially created by bfd.
6052 Sorting these pseudo-sections breaks things badly. */
6053 if (m->count > 1
6054 && !(elf_elfheader (abfd)->e_type == ET_CORE
6055 && m->p_type == PT_NOTE))
6057 for (i = 0; i < m->count; i++)
6058 m->sections[i]->target_index = i;
6059 qsort (m->sections, (size_t) m->count, sizeof (asection *),
6060 elf_sort_sections);
6063 if (alloc > 1)
6064 qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
6065 elf_sort_segments);
6067 maxpagesize = 1;
6068 if ((abfd->flags & D_PAGED) != 0)
6070 if (link_info != NULL)
6071 maxpagesize = link_info->maxpagesize;
6072 else
6073 maxpagesize = bed->maxpagesize;
6076 /* Sections must map to file offsets past the ELF file header. */
6077 off = bed->s->sizeof_ehdr;
6078 /* And if one of the PT_LOAD headers doesn't include the program
6079 headers then we'll be mapping program headers in the usual
6080 position after the ELF file header. */
6081 phdr_load_seg = NULL;
6082 for (j = 0; j < alloc; j++)
6084 m = sorted_seg_map[j];
6085 if (m->p_type != PT_LOAD)
6086 break;
6087 if (m->includes_phdrs)
6089 phdr_load_seg = m;
6090 break;
6093 if (phdr_load_seg == NULL)
6094 off += actual * bed->s->sizeof_phdr;
6096 for (j = 0; j < alloc; j++)
6098 asection **secpp;
6099 bfd_vma off_adjust; /* Octets. */
6100 bool no_contents;
6101 bfd_size_type p_align;
6102 bool p_align_p;
6104 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
6105 number of sections with contents contributing to both p_filesz
6106 and p_memsz, followed by a number of sections with no contents
6107 that just contribute to p_memsz. In this loop, OFF tracks next
6108 available file offset for PT_LOAD and PT_NOTE segments. */
6109 m = sorted_seg_map[j];
6110 p = phdrs + m->idx;
6111 p->p_type = m->p_type;
6112 p->p_flags = m->p_flags;
6113 p_align = bed->p_align;
6114 p_align_p = false;
6116 if (m->count == 0)
6117 p->p_vaddr = m->p_vaddr_offset * opb;
6118 else
6119 p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
6121 if (m->p_paddr_valid)
6122 p->p_paddr = m->p_paddr;
6123 else if (m->count == 0)
6124 p->p_paddr = 0;
6125 else
6126 p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
6128 if (p->p_type == PT_LOAD
6129 && (abfd->flags & D_PAGED) != 0)
6131 /* p_align in demand paged PT_LOAD segments effectively stores
6132 the maximum page size. When copying an executable with
6133 objcopy, we set m->p_align from the input file. Use this
6134 value for maxpagesize rather than bed->maxpagesize, which
6135 may be different. Note that we use maxpagesize for PT_TLS
6136 segment alignment later in this function, so we are relying
6137 on at least one PT_LOAD segment appearing before a PT_TLS
6138 segment. */
6139 if (m->p_align_valid)
6140 maxpagesize = m->p_align;
6141 else if (p_align != 0
6142 && (link_info == NULL
6143 || !link_info->maxpagesize_is_set))
6144 /* Set p_align to the default p_align value while laying
6145 out segments aligning to the maximum page size or the
6146 largest section alignment. The run-time loader can
6147 align segments to the default p_align value or the
6148 maximum page size, depending on system page size. */
6149 p_align_p = true;
6151 p->p_align = maxpagesize;
6153 else if (m->p_align_valid)
6154 p->p_align = m->p_align;
6155 else if (m->count == 0)
6156 p->p_align = 1 << bed->s->log_file_align;
6158 if (m == phdr_load_seg)
6160 if (!m->includes_filehdr)
6161 p->p_offset = off;
6162 off += actual * bed->s->sizeof_phdr;
6165 no_contents = false;
6166 off_adjust = 0;
6167 if (p->p_type == PT_LOAD
6168 && m->count > 0)
6170 bfd_size_type align; /* Bytes. */
6171 unsigned int align_power = 0;
6173 if (m->p_align_valid)
6174 align = p->p_align;
6175 else
6177 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6179 unsigned int secalign;
6181 secalign = bfd_section_alignment (*secpp);
6182 if (secalign > align_power)
6183 align_power = secalign;
6185 align = (bfd_size_type) 1 << align_power;
6186 if (align < maxpagesize)
6188 /* If a section requires alignment higher than the
6189 default p_align value, don't set p_align to the
6190 default p_align value. */
6191 if (align > p_align)
6192 p_align_p = false;
6193 align = maxpagesize;
6195 else
6197 /* If a section requires alignment higher than the
6198 maximum page size, set p_align to the section
6199 alignment. */
6200 p_align_p = true;
6201 p_align = align;
6205 for (i = 0; i < m->count; i++)
6206 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
6207 /* If we aren't making room for this section, then
6208 it must be SHT_NOBITS regardless of what we've
6209 set via struct bfd_elf_special_section. */
6210 elf_section_type (m->sections[i]) = SHT_NOBITS;
6212 /* Find out whether this segment contains any loadable
6213 sections. */
6214 no_contents = true;
6215 for (i = 0; i < m->count; i++)
6216 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
6218 no_contents = false;
6219 break;
6222 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
6224 /* Broken hardware and/or kernel require that files do not
6225 map the same page with different permissions on some hppa
6226 processors. */
6227 if (j != 0
6228 && (abfd->flags & D_PAGED) != 0
6229 && bed->no_page_alias
6230 && (off & (maxpagesize - 1)) != 0
6231 && ((off & -maxpagesize)
6232 == ((off + off_adjust) & -maxpagesize)))
6233 off_adjust += maxpagesize;
6234 off += off_adjust;
6235 if (no_contents)
6237 /* We shouldn't need to align the segment on disk since
6238 the segment doesn't need file space, but the gABI
6239 arguably requires the alignment and glibc ld.so
6240 checks it. So to comply with the alignment
6241 requirement but not waste file space, we adjust
6242 p_offset for just this segment. (OFF_ADJUST is
6243 subtracted from OFF later.) This may put p_offset
6244 past the end of file, but that shouldn't matter. */
6246 else
6247 off_adjust = 0;
6249 /* Make sure the .dynamic section is the first section in the
6250 PT_DYNAMIC segment. */
6251 else if (p->p_type == PT_DYNAMIC
6252 && m->count > 1
6253 && strcmp (m->sections[0]->name, ".dynamic") != 0)
6255 _bfd_error_handler
6256 (_("%pB: The first section in the PT_DYNAMIC segment"
6257 " is not the .dynamic section"),
6258 abfd);
6259 bfd_set_error (bfd_error_bad_value);
6260 return false;
6262 /* Set the note section type to SHT_NOTE. */
6263 else if (p->p_type == PT_NOTE)
6264 for (i = 0; i < m->count; i++)
6265 elf_section_type (m->sections[i]) = SHT_NOTE;
6267 if (m->includes_filehdr)
6269 if (!m->p_flags_valid)
6270 p->p_flags |= PF_R;
6271 p->p_filesz = bed->s->sizeof_ehdr;
6272 p->p_memsz = bed->s->sizeof_ehdr;
6273 if (p->p_type == PT_LOAD)
6275 if (m->count > 0)
6277 if (p->p_vaddr < (bfd_vma) off
6278 || (!m->p_paddr_valid
6279 && p->p_paddr < (bfd_vma) off))
6281 _bfd_error_handler
6282 (_("%pB: not enough room for program headers,"
6283 " try linking with -N"),
6284 abfd);
6285 bfd_set_error (bfd_error_bad_value);
6286 return false;
6288 p->p_vaddr -= off;
6289 if (!m->p_paddr_valid)
6290 p->p_paddr -= off;
6293 else if (sorted_seg_map[0]->includes_filehdr)
6295 Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
6296 p->p_vaddr = filehdr->p_vaddr;
6297 if (!m->p_paddr_valid)
6298 p->p_paddr = filehdr->p_paddr;
6302 if (m->includes_phdrs)
6304 if (!m->p_flags_valid)
6305 p->p_flags |= PF_R;
6306 p->p_filesz += actual * bed->s->sizeof_phdr;
6307 p->p_memsz += actual * bed->s->sizeof_phdr;
6308 if (!m->includes_filehdr)
6310 if (p->p_type == PT_LOAD)
6312 elf_elfheader (abfd)->e_phoff = p->p_offset;
6313 if (m->count > 0)
6315 p->p_vaddr -= off - p->p_offset;
6316 if (!m->p_paddr_valid)
6317 p->p_paddr -= off - p->p_offset;
6320 else if (phdr_load_seg != NULL)
6322 Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
6323 bfd_vma phdr_off = 0; /* Octets. */
6324 if (phdr_load_seg->includes_filehdr)
6325 phdr_off = bed->s->sizeof_ehdr;
6326 p->p_vaddr = phdr->p_vaddr + phdr_off;
6327 if (!m->p_paddr_valid)
6328 p->p_paddr = phdr->p_paddr + phdr_off;
6329 p->p_offset = phdr->p_offset + phdr_off;
6331 else
6332 p->p_offset = bed->s->sizeof_ehdr;
6336 if (p->p_type == PT_LOAD
6337 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
6339 if (!m->includes_filehdr && !m->includes_phdrs)
6341 p->p_offset = off;
6342 if (no_contents)
6344 /* Put meaningless p_offset for PT_LOAD segments
6345 without file contents somewhere within the first
6346 page, in an attempt to not point past EOF. */
6347 bfd_size_type align = maxpagesize;
6348 if (align < p->p_align)
6349 align = p->p_align;
6350 if (align < 1)
6351 align = 1;
6352 p->p_offset = off % align;
6355 else
6357 file_ptr adjust; /* Octets. */
6359 adjust = off - (p->p_offset + p->p_filesz);
6360 if (!no_contents)
6361 p->p_filesz += adjust;
6362 p->p_memsz += adjust;
6366 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
6367 maps. Set filepos for sections in PT_LOAD segments, and in
6368 core files, for sections in PT_NOTE segments.
6369 assign_file_positions_for_non_load_sections will set filepos
6370 for other sections and update p_filesz for other segments. */
6371 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6373 asection *sec;
6374 bfd_size_type align;
6375 Elf_Internal_Shdr *this_hdr;
6377 sec = *secpp;
6378 this_hdr = &elf_section_data (sec)->this_hdr;
6379 align = (bfd_size_type) 1 << bfd_section_alignment (sec);
6381 if ((p->p_type == PT_LOAD
6382 || p->p_type == PT_TLS)
6383 && (this_hdr->sh_type != SHT_NOBITS
6384 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
6385 && ((this_hdr->sh_flags & SHF_TLS) == 0
6386 || p->p_type == PT_TLS))))
6388 bfd_vma p_start = p->p_paddr; /* Octets. */
6389 bfd_vma p_end = p_start + p->p_memsz; /* Octets. */
6390 bfd_vma s_start = sec->lma * opb; /* Octets. */
6391 bfd_vma adjust = s_start - p_end; /* Octets. */
6393 if (adjust != 0
6394 && (s_start < p_end
6395 || p_end < p_start))
6397 _bfd_error_handler
6398 /* xgettext:c-format */
6399 (_("%pB: section %pA lma %#" PRIx64
6400 " adjusted to %#" PRIx64),
6401 abfd, sec, (uint64_t) s_start / opb,
6402 (uint64_t) p_end / opb);
6403 adjust = 0;
6404 sec->lma = p_end / opb;
6406 p->p_memsz += adjust;
6408 if (p->p_type == PT_LOAD)
6410 if (this_hdr->sh_type != SHT_NOBITS)
6412 off_adjust = 0;
6413 if (p->p_filesz + adjust < p->p_memsz)
6415 /* We have a PROGBITS section following NOBITS ones.
6416 Allocate file space for the NOBITS section(s).
6417 We don't need to write out the zeros, posix
6418 fseek past the end of data already written
6419 followed by a write at that location is
6420 guaranteed to result in zeros being read
6421 from the gap. */
6422 adjust = p->p_memsz - p->p_filesz;
6425 /* We only adjust sh_offset in SHT_NOBITS sections
6426 as would seem proper for their address when the
6427 section is first in the segment. sh_offset
6428 doesn't really have any significance for
6429 SHT_NOBITS anyway, apart from a notional position
6430 relative to other sections. Historically we
6431 didn't bother with adjusting sh_offset and some
6432 programs depend on it not being adjusted. See
6433 pr12921 and pr25662. */
6434 if (this_hdr->sh_type != SHT_NOBITS || i == 0)
6436 off += adjust;
6437 if (this_hdr->sh_type == SHT_NOBITS)
6438 off_adjust += adjust;
6441 if (this_hdr->sh_type != SHT_NOBITS)
6442 p->p_filesz += adjust;
6445 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
6447 /* The section at i == 0 is the one that actually contains
6448 everything. */
6449 if (i == 0)
6451 this_hdr->sh_offset = sec->filepos = off;
6452 off += this_hdr->sh_size;
6453 p->p_filesz = this_hdr->sh_size;
6454 p->p_memsz = 0;
6455 p->p_align = 1;
6457 else
6459 /* The rest are fake sections that shouldn't be written. */
6460 sec->filepos = 0;
6461 sec->size = 0;
6462 sec->flags = 0;
6463 continue;
6466 else
6468 if (p->p_type == PT_LOAD)
6470 this_hdr->sh_offset = sec->filepos = off;
6471 if (this_hdr->sh_type != SHT_NOBITS)
6472 off += this_hdr->sh_size;
6474 else if (this_hdr->sh_type == SHT_NOBITS
6475 && (this_hdr->sh_flags & SHF_TLS) != 0
6476 && this_hdr->sh_offset == 0)
6478 /* This is a .tbss section that didn't get a PT_LOAD.
6479 (See _bfd_elf_map_sections_to_segments "Create a
6480 final PT_LOAD".) Set sh_offset to the value it
6481 would have if we had created a zero p_filesz and
6482 p_memsz PT_LOAD header for the section. This
6483 also makes the PT_TLS header have the same
6484 p_offset value. */
6485 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
6486 off, align);
6487 this_hdr->sh_offset = sec->filepos = off + adjust;
6490 if (this_hdr->sh_type != SHT_NOBITS)
6492 p->p_filesz += this_hdr->sh_size;
6493 /* A load section without SHF_ALLOC is something like
6494 a note section in a PT_NOTE segment. These take
6495 file space but are not loaded into memory. */
6496 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6497 p->p_memsz += this_hdr->sh_size;
6499 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6501 if (p->p_type == PT_TLS)
6502 p->p_memsz += this_hdr->sh_size;
6504 /* .tbss is special. It doesn't contribute to p_memsz of
6505 normal segments. */
6506 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
6507 p->p_memsz += this_hdr->sh_size;
6510 if (align > p->p_align
6511 && !m->p_align_valid
6512 && (p->p_type != PT_LOAD
6513 || (abfd->flags & D_PAGED) == 0))
6514 p->p_align = align;
6517 if (!m->p_flags_valid)
6519 p->p_flags |= PF_R;
6520 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
6521 p->p_flags |= PF_X;
6522 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
6523 p->p_flags |= PF_W;
6527 off -= off_adjust;
6529 /* PR ld/20815 - Check that the program header segment, if
6530 present, will be loaded into memory. */
6531 if (p->p_type == PT_PHDR
6532 && phdr_load_seg == NULL
6533 && !(bed->elf_backend_allow_non_load_phdr != NULL
6534 && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
6536 /* The fix for this error is usually to edit the linker script being
6537 used and set up the program headers manually. Either that or
6538 leave room for the headers at the start of the SECTIONS. */
6539 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6540 " by LOAD segment"),
6541 abfd);
6542 if (link_info == NULL)
6543 return false;
6544 /* Arrange for the linker to exit with an error, deleting
6545 the output file unless --noinhibit-exec is given. */
6546 link_info->callbacks->info ("%X");
6549 /* Check that all sections are in a PT_LOAD segment.
6550 Don't check funky gdb generated core files. */
6551 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
6553 bool check_vma = true;
6555 for (i = 1; i < m->count; i++)
6556 if (m->sections[i]->vma == m->sections[i - 1]->vma
6557 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
6558 ->this_hdr), p) != 0
6559 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
6560 ->this_hdr), p) != 0)
6562 /* Looks like we have overlays packed into the segment. */
6563 check_vma = false;
6564 break;
6567 for (i = 0; i < m->count; i++)
6569 Elf_Internal_Shdr *this_hdr;
6570 asection *sec;
6572 sec = m->sections[i];
6573 this_hdr = &(elf_section_data(sec)->this_hdr);
6574 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
6575 && !ELF_TBSS_SPECIAL (this_hdr, p))
6577 _bfd_error_handler
6578 /* xgettext:c-format */
6579 (_("%pB: section `%pA' can't be allocated in segment %d"),
6580 abfd, sec, j);
6581 print_segment_map (m);
6585 if (p_align_p)
6586 p->p_align = p_align;
6590 elf_next_file_pos (abfd) = off;
6592 if (link_info != NULL
6593 && phdr_load_seg != NULL
6594 && phdr_load_seg->includes_filehdr)
6596 /* There is a segment that contains both the file headers and the
6597 program headers, so provide a symbol __ehdr_start pointing there.
6598 A program can use this to examine itself robustly. */
6600 struct elf_link_hash_entry *hash
6601 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
6602 false, false, true);
6603 /* If the symbol was referenced and not defined, define it. */
6604 if (hash != NULL
6605 && (hash->root.type == bfd_link_hash_new
6606 || hash->root.type == bfd_link_hash_undefined
6607 || hash->root.type == bfd_link_hash_undefweak
6608 || hash->root.type == bfd_link_hash_common))
6610 asection *s = NULL;
6611 bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
6613 if (phdr_load_seg->count != 0)
6614 /* The segment contains sections, so use the first one. */
6615 s = phdr_load_seg->sections[0];
6616 else
6617 /* Use the first (i.e. lowest-addressed) section in any segment. */
6618 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6619 if (m->p_type == PT_LOAD && m->count != 0)
6621 s = m->sections[0];
6622 break;
6625 if (s != NULL)
6627 hash->root.u.def.value = filehdr_vaddr - s->vma;
6628 hash->root.u.def.section = s;
6630 else
6632 hash->root.u.def.value = filehdr_vaddr;
6633 hash->root.u.def.section = bfd_abs_section_ptr;
6636 hash->root.type = bfd_link_hash_defined;
6637 hash->def_regular = 1;
6638 hash->non_elf = 0;
6642 return true;
6645 /* Determine if a bfd is a debuginfo file. Unfortunately there
6646 is no defined method for detecting such files, so we have to
6647 use heuristics instead. */
6649 bool
6650 is_debuginfo_file (bfd *abfd)
6652 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6653 return false;
6655 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6656 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6657 Elf_Internal_Shdr **headerp;
6659 for (headerp = start_headers; headerp < end_headers; headerp ++)
6661 Elf_Internal_Shdr *header = * headerp;
6663 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6664 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6665 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6666 && header->sh_type != SHT_NOBITS
6667 && header->sh_type != SHT_NOTE)
6668 return false;
6671 return true;
6674 /* Assign file positions for other sections, except for compressed debug
6675 and sections assigned in _bfd_elf_assign_file_positions_for_non_load. */
6677 static bool
6678 assign_file_positions_for_non_load_sections (bfd *abfd,
6679 struct bfd_link_info *link_info)
6681 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6682 Elf_Internal_Shdr **i_shdrpp;
6683 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
6684 Elf_Internal_Phdr *phdrs;
6685 Elf_Internal_Phdr *p;
6686 struct elf_segment_map *m;
6687 file_ptr off;
6688 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
6689 bfd_vma maxpagesize;
6691 if (link_info != NULL)
6692 maxpagesize = link_info->maxpagesize;
6693 else
6694 maxpagesize = bed->maxpagesize;
6695 i_shdrpp = elf_elfsections (abfd);
6696 end_hdrpp = i_shdrpp + elf_numsections (abfd);
6697 off = elf_next_file_pos (abfd);
6698 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
6700 Elf_Internal_Shdr *hdr;
6701 bfd_vma align;
6703 hdr = *hdrpp;
6704 if (hdr->bfd_section != NULL
6705 && (hdr->bfd_section->filepos != 0
6706 || (hdr->sh_type == SHT_NOBITS
6707 && hdr->contents == NULL)))
6708 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
6709 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6711 if (hdr->sh_size != 0
6712 /* PR 24717 - debuginfo files are known to be not strictly
6713 compliant with the ELF standard. In particular they often
6714 have .note.gnu.property sections that are outside of any
6715 loadable segment. This is not a problem for such files,
6716 so do not warn about them. */
6717 && ! is_debuginfo_file (abfd))
6718 _bfd_error_handler
6719 /* xgettext:c-format */
6720 (_("%pB: warning: allocated section `%s' not in segment"),
6721 abfd,
6722 (hdr->bfd_section == NULL
6723 ? "*unknown*"
6724 : hdr->bfd_section->name));
6725 /* We don't need to page align empty sections. */
6726 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
6727 align = maxpagesize;
6728 else
6729 align = hdr->sh_addralign & -hdr->sh_addralign;
6730 off += vma_page_aligned_bias (hdr->sh_addr, off, align);
6731 off = _bfd_elf_assign_file_position_for_section (hdr, off,
6732 false);
6734 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6735 && hdr->bfd_section == NULL)
6736 /* We don't know the offset of these sections yet:
6737 their size has not been decided. */
6738 || (abfd->is_linker_output
6739 && hdr->bfd_section != NULL
6740 && (hdr->sh_name == -1u
6741 || bfd_section_is_ctf (hdr->bfd_section)))
6742 || hdr == i_shdrpp[elf_onesymtab (abfd)]
6743 || (elf_symtab_shndx_list (abfd) != NULL
6744 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6745 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6746 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
6747 hdr->sh_offset = -1;
6748 else
6749 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6751 elf_next_file_pos (abfd) = off;
6753 /* Now that we have set the section file positions, we can set up
6754 the file positions for the non PT_LOAD segments. */
6755 phdrs = elf_tdata (abfd)->phdr;
6756 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6758 if (p->p_type == PT_GNU_RELRO)
6760 bfd_vma start, end; /* Bytes. */
6761 bool ok;
6763 if (link_info != NULL)
6765 /* During linking the range of the RELRO segment is passed
6766 in link_info. Note that there may be padding between
6767 relro_start and the first RELRO section. */
6768 start = link_info->relro_start;
6769 end = link_info->relro_end;
6771 else if (m->count != 0)
6773 if (!m->p_size_valid)
6774 abort ();
6775 start = m->sections[0]->vma;
6776 end = start + m->p_size / opb;
6778 else
6780 start = 0;
6781 end = 0;
6784 ok = false;
6785 if (start < end)
6787 struct elf_segment_map *lm;
6788 const Elf_Internal_Phdr *lp;
6789 unsigned int i;
6791 /* Find a LOAD segment containing a section in the RELRO
6792 segment. */
6793 for (lm = elf_seg_map (abfd), lp = phdrs;
6794 lm != NULL;
6795 lm = lm->next, lp++)
6797 if (lp->p_type == PT_LOAD
6798 && lm->count != 0
6799 && (lm->sections[lm->count - 1]->vma
6800 + (!IS_TBSS (lm->sections[lm->count - 1])
6801 ? lm->sections[lm->count - 1]->size / opb
6802 : 0)) > start
6803 && lm->sections[0]->vma < end)
6804 break;
6807 if (lm != NULL)
6809 /* Find the section starting the RELRO segment. */
6810 for (i = 0; i < lm->count; i++)
6812 asection *s = lm->sections[i];
6813 if (s->vma >= start
6814 && s->vma < end
6815 && s->size != 0)
6816 break;
6819 if (i < lm->count)
6821 p->p_vaddr = lm->sections[i]->vma * opb;
6822 p->p_paddr = lm->sections[i]->lma * opb;
6823 p->p_offset = lm->sections[i]->filepos;
6824 p->p_memsz = end * opb - p->p_vaddr;
6825 p->p_filesz = p->p_memsz;
6827 /* The RELRO segment typically ends a few bytes
6828 into .got.plt but other layouts are possible.
6829 In cases where the end does not match any
6830 loaded section (for instance is in file
6831 padding), trim p_filesz back to correspond to
6832 the end of loaded section contents. */
6833 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6834 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6836 /* Preserve the alignment and flags if they are
6837 valid. The gold linker generates RW/4 for
6838 the PT_GNU_RELRO section. It is better for
6839 objcopy/strip to honor these attributes
6840 otherwise gdb will choke when using separate
6841 debug files. */
6842 if (!m->p_align_valid)
6843 p->p_align = 1;
6844 if (!m->p_flags_valid)
6845 p->p_flags = PF_R;
6846 ok = true;
6851 if (!ok)
6853 if (link_info != NULL)
6854 _bfd_error_handler
6855 (_("%pB: warning: unable to allocate any sections"
6856 " to PT_GNU_RELRO segment"),
6857 abfd);
6858 memset (p, 0, sizeof *p);
6861 else if (p->p_type == PT_GNU_STACK)
6863 if (m->p_size_valid)
6864 p->p_memsz = m->p_size;
6866 else if (m->count != 0)
6868 unsigned int i;
6870 if (p->p_type != PT_LOAD
6871 && (p->p_type != PT_NOTE
6872 || bfd_get_format (abfd) != bfd_core))
6874 /* A user specified segment layout may include a PHDR
6875 segment that overlaps with a LOAD segment... */
6876 if (p->p_type == PT_PHDR)
6878 m->count = 0;
6879 continue;
6882 if (m->includes_filehdr || m->includes_phdrs)
6884 /* PR 17512: file: 2195325e. */
6885 _bfd_error_handler
6886 (_("%pB: error: non-load segment %d includes file header "
6887 "and/or program header"),
6888 abfd, (int) (p - phdrs));
6889 return false;
6892 p->p_filesz = 0;
6893 p->p_offset = m->sections[0]->filepos;
6894 for (i = m->count; i-- != 0;)
6896 asection *sect = m->sections[i];
6897 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6898 if (hdr->sh_type != SHT_NOBITS)
6900 p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
6901 /* NB: p_memsz of the loadable PT_NOTE segment
6902 should be the same as p_filesz. */
6903 if (p->p_type == PT_NOTE
6904 && (hdr->sh_flags & SHF_ALLOC) != 0)
6905 p->p_memsz = p->p_filesz;
6906 break;
6913 return true;
6916 static elf_section_list *
6917 find_section_in_list (unsigned int i, elf_section_list * list)
6919 for (;list != NULL; list = list->next)
6920 if (list->ndx == i)
6921 break;
6922 return list;
6925 /* Work out the file positions of all the sections. This is called by
6926 _bfd_elf_compute_section_file_positions. All the section sizes and
6927 VMAs must be known before this is called.
6929 Reloc sections come in two flavours: Those processed specially as
6930 "side-channel" data attached to a section to which they apply, and
6931 those that bfd doesn't process as relocations. The latter sort are
6932 stored in a normal bfd section by bfd_section_from_shdr. We don't
6933 consider the former sort here, unless they form part of the loadable
6934 image. Reloc sections not assigned here (and compressed debugging
6935 sections and CTF sections which nothing else in the file can rely
6936 upon) will be handled later by assign_file_positions_for_relocs.
6938 We also don't set the positions of the .symtab and .strtab here. */
6940 static bool
6941 assign_file_positions_except_relocs (bfd *abfd,
6942 struct bfd_link_info *link_info)
6944 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6945 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6946 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6947 unsigned int alloc;
6949 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6950 && bfd_get_format (abfd) != bfd_core)
6952 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6953 unsigned int num_sec = elf_numsections (abfd);
6954 Elf_Internal_Shdr **hdrpp;
6955 unsigned int i;
6956 file_ptr off;
6958 /* Start after the ELF header. */
6959 off = i_ehdrp->e_ehsize;
6961 /* We are not creating an executable, which means that we are
6962 not creating a program header, and that the actual order of
6963 the sections in the file is unimportant. */
6964 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6966 Elf_Internal_Shdr *hdr;
6968 hdr = *hdrpp;
6969 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6970 && hdr->bfd_section == NULL)
6971 /* Do not assign offsets for these sections yet: we don't know
6972 their sizes. */
6973 || (abfd->is_linker_output
6974 && hdr->bfd_section != NULL
6975 && (hdr->sh_name == -1u
6976 || bfd_section_is_ctf (hdr->bfd_section)))
6977 || i == elf_onesymtab (abfd)
6978 || (elf_symtab_shndx_list (abfd) != NULL
6979 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6980 || i == elf_strtab_sec (abfd)
6981 || i == elf_shstrtab_sec (abfd))
6983 hdr->sh_offset = -1;
6985 else
6986 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6989 elf_next_file_pos (abfd) = off;
6990 elf_program_header_size (abfd) = 0;
6992 else
6994 /* Assign file positions for the loaded sections based on the
6995 assignment of sections to segments. */
6996 if (!assign_file_positions_for_load_sections (abfd, link_info))
6997 return false;
6999 /* And for non-load sections. */
7000 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
7001 return false;
7004 if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
7005 return false;
7007 /* Write out the program headers. */
7008 alloc = i_ehdrp->e_phnum;
7009 if (alloc != 0)
7011 if (link_info != NULL && ! link_info->no_warn_rwx_segments)
7013 /* Memory resident segments with non-zero size and RWX
7014 permissions are a security risk, so we generate a warning
7015 here if we are creating any. */
7016 unsigned int i;
7018 for (i = 0; i < alloc; i++)
7020 const Elf_Internal_Phdr * phdr = tdata->phdr + i;
7022 if (phdr->p_memsz == 0)
7023 continue;
7025 if (phdr->p_type == PT_TLS && (phdr->p_flags & PF_X))
7026 _bfd_error_handler (_("warning: %pB has a TLS segment"
7027 " with execute permission"),
7028 abfd);
7029 else if (phdr->p_type == PT_LOAD
7030 && ((phdr->p_flags & (PF_R | PF_W | PF_X))
7031 == (PF_R | PF_W | PF_X)))
7032 _bfd_error_handler (_("warning: %pB has a LOAD segment"
7033 " with RWX permissions"),
7034 abfd);
7038 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
7039 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
7040 return false;
7043 return true;
7046 bool
7047 _bfd_elf_init_file_header (bfd *abfd,
7048 struct bfd_link_info *info ATTRIBUTE_UNUSED)
7050 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
7051 struct elf_strtab_hash *shstrtab;
7052 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7054 i_ehdrp = elf_elfheader (abfd);
7056 shstrtab = _bfd_elf_strtab_init ();
7057 if (shstrtab == NULL)
7058 return false;
7060 elf_shstrtab (abfd) = shstrtab;
7062 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
7063 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
7064 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
7065 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
7067 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
7068 i_ehdrp->e_ident[EI_DATA] =
7069 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
7070 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
7072 if ((abfd->flags & DYNAMIC) != 0)
7073 i_ehdrp->e_type = ET_DYN;
7074 else if ((abfd->flags & EXEC_P) != 0)
7075 i_ehdrp->e_type = ET_EXEC;
7076 else if (bfd_get_format (abfd) == bfd_core)
7077 i_ehdrp->e_type = ET_CORE;
7078 else
7079 i_ehdrp->e_type = ET_REL;
7081 switch (bfd_get_arch (abfd))
7083 case bfd_arch_unknown:
7084 i_ehdrp->e_machine = EM_NONE;
7085 break;
7087 /* There used to be a long list of cases here, each one setting
7088 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
7089 in the corresponding bfd definition. To avoid duplication,
7090 the switch was removed. Machines that need special handling
7091 can generally do it in elf_backend_final_write_processing(),
7092 unless they need the information earlier than the final write.
7093 Such need can generally be supplied by replacing the tests for
7094 e_machine with the conditions used to determine it. */
7095 default:
7096 i_ehdrp->e_machine = bed->elf_machine_code;
7099 i_ehdrp->e_version = bed->s->ev_current;
7100 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
7102 /* No program header, for now. */
7103 i_ehdrp->e_phoff = 0;
7104 i_ehdrp->e_phentsize = 0;
7105 i_ehdrp->e_phnum = 0;
7107 /* Each bfd section is section header entry. */
7108 i_ehdrp->e_entry = bfd_get_start_address (abfd);
7109 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
7111 elf_tdata (abfd)->symtab_hdr.sh_name =
7112 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
7113 elf_tdata (abfd)->strtab_hdr.sh_name =
7114 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
7115 elf_tdata (abfd)->shstrtab_hdr.sh_name =
7116 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
7117 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
7118 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
7119 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
7120 return false;
7122 return true;
7125 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
7127 FIXME: We used to have code here to sort the PT_LOAD segments into
7128 ascending order, as per the ELF spec. But this breaks some programs,
7129 including the Linux kernel. But really either the spec should be
7130 changed or the programs updated. */
7132 bool
7133 _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
7135 if (link_info != NULL && bfd_link_pie (link_info))
7137 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
7138 unsigned int num_segments = i_ehdrp->e_phnum;
7139 struct elf_obj_tdata *tdata = elf_tdata (obfd);
7140 Elf_Internal_Phdr *segment = tdata->phdr;
7141 Elf_Internal_Phdr *end_segment = &segment[num_segments];
7143 /* Find the lowest p_vaddr in PT_LOAD segments. */
7144 bfd_vma p_vaddr = (bfd_vma) -1;
7145 for (; segment < end_segment; segment++)
7146 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
7147 p_vaddr = segment->p_vaddr;
7149 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
7150 segments is non-zero. */
7151 if (p_vaddr)
7152 i_ehdrp->e_type = ET_EXEC;
7154 return true;
7157 /* Assign file positions for all the reloc sections which are not part
7158 of the loadable file image, and the file position of section headers. */
7160 static bool
7161 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
7163 file_ptr off;
7164 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
7165 Elf_Internal_Shdr *shdrp;
7166 Elf_Internal_Ehdr *i_ehdrp;
7167 const struct elf_backend_data *bed;
7169 /* Skip non-load sections without section header. */
7170 if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
7171 return true;
7173 off = elf_next_file_pos (abfd);
7175 shdrpp = elf_elfsections (abfd);
7176 end_shdrpp = shdrpp + elf_numsections (abfd);
7177 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
7179 shdrp = *shdrpp;
7180 if (shdrp->sh_offset == -1)
7182 asection *sec = shdrp->bfd_section;
7183 if (sec == NULL
7184 || shdrp->sh_type == SHT_REL
7185 || shdrp->sh_type == SHT_RELA)
7187 else if (bfd_section_is_ctf (sec))
7189 /* Update section size and contents. */
7190 shdrp->sh_size = sec->size;
7191 shdrp->contents = sec->contents;
7193 else if (shdrp->sh_name == -1u)
7195 const char *name = sec->name;
7196 struct bfd_elf_section_data *d;
7198 /* Compress DWARF debug sections. */
7199 if (!bfd_compress_section (abfd, sec, shdrp->contents))
7200 return false;
7202 if (sec->compress_status == COMPRESS_SECTION_DONE
7203 && (abfd->flags & BFD_COMPRESS_GABI) == 0
7204 && name[1] == 'd')
7206 /* If section is compressed with zlib-gnu, convert
7207 section name from .debug_* to .zdebug_*. */
7208 char *new_name = bfd_debug_name_to_zdebug (abfd, name);
7209 if (new_name == NULL)
7210 return false;
7211 name = new_name;
7213 /* Add section name to section name section. */
7214 shdrp->sh_name
7215 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
7216 name, false);
7217 d = elf_section_data (sec);
7219 /* Add reloc section name to section name section. */
7220 if (d->rel.hdr
7221 && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr,
7222 name, false))
7223 return false;
7224 if (d->rela.hdr
7225 && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr,
7226 name, true))
7227 return false;
7229 /* Update section size and contents. */
7230 shdrp->sh_size = sec->size;
7231 shdrp->contents = sec->contents;
7232 sec->contents = NULL;
7235 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
7239 /* Place section name section after DWARF debug sections have been
7240 compressed. */
7241 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
7242 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
7243 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
7244 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
7246 /* Place the section headers. */
7247 i_ehdrp = elf_elfheader (abfd);
7248 bed = get_elf_backend_data (abfd);
7249 off = align_file_position (off, 1 << bed->s->log_file_align);
7250 i_ehdrp->e_shoff = off;
7251 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
7252 elf_next_file_pos (abfd) = off;
7254 return true;
7257 bool
7258 _bfd_elf_write_object_contents (bfd *abfd)
7260 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7261 Elf_Internal_Shdr **i_shdrp;
7262 bool failed;
7263 unsigned int count, num_sec;
7264 struct elf_obj_tdata *t;
7266 if (! abfd->output_has_begun
7267 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7268 return false;
7269 /* Do not rewrite ELF data when the BFD has been opened for update.
7270 abfd->output_has_begun was set to TRUE on opening, so creation of
7271 new sections, and modification of existing section sizes was
7272 restricted. This means the ELF header, program headers and
7273 section headers can't have changed. If the contents of any
7274 sections has been modified, then those changes have already been
7275 written to the BFD. */
7276 else if (abfd->direction == both_direction)
7278 BFD_ASSERT (abfd->output_has_begun);
7279 return true;
7282 i_shdrp = elf_elfsections (abfd);
7284 failed = false;
7285 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
7286 if (failed)
7287 return false;
7289 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
7290 return false;
7292 /* After writing the headers, we need to write the sections too... */
7293 num_sec = elf_numsections (abfd);
7294 for (count = 1; count < num_sec; count++)
7296 /* Don't set the sh_name field without section header. */
7297 if ((abfd->flags & BFD_NO_SECTION_HEADER) == 0)
7298 i_shdrp[count]->sh_name
7299 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
7300 i_shdrp[count]->sh_name);
7301 if (bed->elf_backend_section_processing)
7302 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
7303 return false;
7304 if (i_shdrp[count]->contents)
7306 bfd_size_type amt = i_shdrp[count]->sh_size;
7308 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
7309 || bfd_write (i_shdrp[count]->contents, amt, abfd) != amt)
7310 return false;
7314 /* Write out the section header names. */
7315 t = elf_tdata (abfd);
7316 if (elf_shstrtab (abfd) != NULL
7317 && t->shstrtab_hdr.sh_offset != -1
7318 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
7319 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
7320 return false;
7322 if (!(*bed->elf_backend_final_write_processing) (abfd))
7323 return false;
7325 if (!bed->s->write_shdrs_and_ehdr (abfd))
7326 return false;
7328 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
7329 if (t->o->build_id.after_write_object_contents != NULL
7330 && !(*t->o->build_id.after_write_object_contents) (abfd))
7331 return false;
7332 if (t->o->package_metadata.after_write_object_contents != NULL
7333 && !(*t->o->package_metadata.after_write_object_contents) (abfd))
7334 return false;
7336 return true;
7339 bool
7340 _bfd_elf_write_corefile_contents (bfd *abfd)
7342 /* Hopefully this can be done just like an object file. */
7343 return _bfd_elf_write_object_contents (abfd);
7346 /* Given a section, search the header to find them. */
7348 unsigned int
7349 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
7351 const struct elf_backend_data *bed;
7352 unsigned int sec_index;
7354 if (elf_section_data (asect) != NULL
7355 && elf_section_data (asect)->this_idx != 0)
7356 return elf_section_data (asect)->this_idx;
7358 if (bfd_is_abs_section (asect))
7359 sec_index = SHN_ABS;
7360 else if (bfd_is_com_section (asect))
7361 sec_index = SHN_COMMON;
7362 else if (bfd_is_und_section (asect))
7363 sec_index = SHN_UNDEF;
7364 else
7365 sec_index = SHN_BAD;
7367 bed = get_elf_backend_data (abfd);
7368 if (bed->elf_backend_section_from_bfd_section)
7370 int retval = sec_index;
7372 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
7373 return retval;
7376 if (sec_index == SHN_BAD)
7377 bfd_set_error (bfd_error_nonrepresentable_section);
7379 return sec_index;
7382 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
7383 on error. */
7386 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
7388 asymbol *asym_ptr = *asym_ptr_ptr;
7389 int idx;
7390 flagword flags = asym_ptr->flags;
7392 /* When gas creates relocations against local labels, it creates its
7393 own symbol for the section, but does put the symbol into the
7394 symbol chain, so udata is 0. When the linker is generating
7395 relocatable output, this section symbol may be for one of the
7396 input sections rather than the output section. */
7397 if (asym_ptr->udata.i == 0
7398 && (flags & BSF_SECTION_SYM)
7399 && asym_ptr->section)
7401 asection *sec;
7403 sec = asym_ptr->section;
7404 if (sec->owner != abfd && sec->output_section != NULL)
7405 sec = sec->output_section;
7406 if (sec->owner == abfd
7407 && sec->index < elf_num_section_syms (abfd)
7408 && elf_section_syms (abfd)[sec->index] != NULL)
7409 asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
7412 idx = asym_ptr->udata.i;
7414 if (idx == 0)
7416 /* This case can occur when using --strip-symbol on a symbol
7417 which is used in a relocation entry. */
7418 _bfd_error_handler
7419 /* xgettext:c-format */
7420 (_("%pB: symbol `%s' required but not present"),
7421 abfd, bfd_asymbol_name (asym_ptr));
7422 bfd_set_error (bfd_error_no_symbols);
7423 return -1;
7426 #if DEBUG & 4
7428 fprintf (stderr,
7429 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
7430 " flags = 0x%.8x\n",
7431 (long) asym_ptr, asym_ptr->name, idx, flags);
7432 fflush (stderr);
7434 #endif
7436 return idx;
7439 static inline bfd_vma
7440 segment_size (Elf_Internal_Phdr *segment)
7442 return (segment->p_memsz > segment->p_filesz
7443 ? segment->p_memsz : segment->p_filesz);
7447 /* Returns the end address of the segment + 1. */
7448 static inline bfd_vma
7449 segment_end (Elf_Internal_Phdr *segment, bfd_vma start)
7451 return start + segment_size (segment);
7454 static inline bfd_size_type
7455 section_size (asection *section, Elf_Internal_Phdr *segment)
7457 if ((section->flags & SEC_HAS_CONTENTS) != 0
7458 || (section->flags & SEC_THREAD_LOCAL) == 0
7459 || segment->p_type == PT_TLS)
7460 return section->size;
7461 return 0;
7464 /* Returns TRUE if the given section is contained within the given
7465 segment. LMA addresses are compared against PADDR when
7466 USE_VADDR is false, VMA against VADDR when true. */
7467 static bool
7468 is_contained_by (asection *section, Elf_Internal_Phdr *segment,
7469 bfd_vma paddr, bfd_vma vaddr, unsigned int opb,
7470 bool use_vaddr)
7472 bfd_vma seg_addr = !use_vaddr ? paddr : vaddr;
7473 bfd_vma addr = !use_vaddr ? section->lma : section->vma;
7474 bfd_vma octet;
7475 if (_bfd_mul_overflow (addr, opb, &octet))
7476 return false;
7477 /* The third and fourth lines below are testing that the section end
7478 address is within the segment. It's written this way to avoid
7479 overflow. Add seg_addr + section_size to both sides of the
7480 inequality to make it obvious. */
7481 return (octet >= seg_addr
7482 && segment_size (segment) >= section_size (section, segment)
7483 && (octet - seg_addr
7484 <= segment_size (segment) - section_size (section, segment)));
7487 /* Handle PT_NOTE segment. */
7488 static bool
7489 is_note (asection *s, Elf_Internal_Phdr *p)
7491 return (p->p_type == PT_NOTE
7492 && elf_section_type (s) == SHT_NOTE
7493 && (ufile_ptr) s->filepos >= p->p_offset
7494 && p->p_filesz >= s->size
7495 && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
7498 /* Rewrite program header information. */
7500 static bool
7501 rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
7503 Elf_Internal_Ehdr *iehdr;
7504 struct elf_segment_map *map;
7505 struct elf_segment_map *map_first;
7506 struct elf_segment_map **pointer_to_map;
7507 Elf_Internal_Phdr *segment;
7508 asection *section;
7509 unsigned int i;
7510 unsigned int num_segments;
7511 bool phdr_included = false;
7512 bool p_paddr_valid;
7513 struct elf_segment_map *phdr_adjust_seg = NULL;
7514 unsigned int phdr_adjust_num = 0;
7515 const struct elf_backend_data *bed;
7516 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
7518 bed = get_elf_backend_data (ibfd);
7519 iehdr = elf_elfheader (ibfd);
7521 map_first = NULL;
7522 pointer_to_map = &map_first;
7524 num_segments = elf_elfheader (ibfd)->e_phnum;
7526 /* The complicated case when p_vaddr is 0 is to handle the Solaris
7527 linker, which generates a PT_INTERP section with p_vaddr and
7528 p_memsz set to 0. */
7529 #define IS_SOLARIS_PT_INTERP(p, s) \
7530 (p->p_vaddr == 0 \
7531 && p->p_paddr == 0 \
7532 && p->p_memsz == 0 \
7533 && p->p_filesz > 0 \
7534 && (s->flags & SEC_HAS_CONTENTS) != 0 \
7535 && s->size > 0 \
7536 && (bfd_vma) s->filepos >= p->p_offset \
7537 && ((bfd_vma) s->filepos + s->size \
7538 <= p->p_offset + p->p_filesz))
7540 /* Decide if the given section should be included in the given segment.
7541 A section will be included if:
7542 1. It is within the address space of the segment -- we use the LMA
7543 if that is set for the segment and the VMA otherwise,
7544 2. It is an allocated section or a NOTE section in a PT_NOTE
7545 segment.
7546 3. There is an output section associated with it,
7547 4. The section has not already been allocated to a previous segment.
7548 5. PT_GNU_STACK segments do not include any sections.
7549 6. PT_TLS segment includes only SHF_TLS sections.
7550 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
7551 8. PT_DYNAMIC should not contain empty sections at the beginning
7552 (with the possible exception of .dynamic). */
7553 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
7554 (((is_contained_by (section, segment, segment->p_paddr, \
7555 segment->p_vaddr, opb, \
7556 bed->want_p_paddr_set_to_zero) \
7557 && (section->flags & SEC_ALLOC) != 0) \
7558 || is_note (section, segment)) \
7559 && segment->p_type != PT_GNU_STACK \
7560 && (segment->p_type != PT_TLS \
7561 || (section->flags & SEC_THREAD_LOCAL)) \
7562 && (segment->p_type == PT_LOAD \
7563 || segment->p_type == PT_TLS \
7564 || (section->flags & SEC_THREAD_LOCAL) == 0) \
7565 && (segment->p_type != PT_DYNAMIC \
7566 || section_size (section, segment) > 0 \
7567 || (segment->p_paddr \
7568 ? segment->p_paddr != section->lma * (opb) \
7569 : segment->p_vaddr != section->vma * (opb)) \
7570 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
7571 && (segment->p_type != PT_LOAD || !section->segment_mark))
7573 /* If the output section of a section in the input segment is NULL,
7574 it is removed from the corresponding output segment. */
7575 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
7576 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
7577 && section->output_section != NULL)
7579 /* Returns TRUE iff seg1 starts after the end of seg2. */
7580 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
7581 (seg1->field >= segment_end (seg2, seg2->field))
7583 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7584 their VMA address ranges and their LMA address ranges overlap.
7585 It is possible to have overlapping VMA ranges without overlapping LMA
7586 ranges. RedBoot images for example can have both .data and .bss mapped
7587 to the same VMA range, but with the .data section mapped to a different
7588 LMA. */
7589 #define SEGMENT_OVERLAPS(seg1, seg2) \
7590 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
7591 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
7592 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
7593 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7595 /* Initialise the segment mark field, and discard stupid alignment. */
7596 for (section = ibfd->sections; section != NULL; section = section->next)
7598 asection *o = section->output_section;
7599 if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
7600 o->alignment_power = 0;
7601 section->segment_mark = false;
7604 /* The Solaris linker creates program headers in which all the
7605 p_paddr fields are zero. When we try to objcopy or strip such a
7606 file, we get confused. Check for this case, and if we find it
7607 don't set the p_paddr_valid fields. */
7608 p_paddr_valid = false;
7609 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7610 i < num_segments;
7611 i++, segment++)
7612 if (segment->p_paddr != 0)
7614 p_paddr_valid = true;
7615 break;
7618 /* Scan through the segments specified in the program header
7619 of the input BFD. For this first scan we look for overlaps
7620 in the loadable segments. These can be created by weird
7621 parameters to objcopy. Also, fix some solaris weirdness. */
7622 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7623 i < num_segments;
7624 i++, segment++)
7626 unsigned int j;
7627 Elf_Internal_Phdr *segment2;
7629 if (segment->p_type == PT_INTERP)
7630 for (section = ibfd->sections; section; section = section->next)
7631 if (IS_SOLARIS_PT_INTERP (segment, section))
7633 /* Mininal change so that the normal section to segment
7634 assignment code will work. */
7635 segment->p_vaddr = section->vma * opb;
7636 break;
7639 if (segment->p_type != PT_LOAD)
7641 /* Remove PT_GNU_RELRO segment. */
7642 if (segment->p_type == PT_GNU_RELRO)
7643 segment->p_type = PT_NULL;
7644 continue;
7647 /* Determine if this segment overlaps any previous segments. */
7648 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
7650 bfd_signed_vma extra_length;
7652 if (segment2->p_type != PT_LOAD
7653 || !SEGMENT_OVERLAPS (segment, segment2))
7654 continue;
7656 /* Merge the two segments together. */
7657 if (segment2->p_vaddr < segment->p_vaddr)
7659 /* Extend SEGMENT2 to include SEGMENT and then delete
7660 SEGMENT. */
7661 extra_length = (segment_end (segment, segment->p_vaddr)
7662 - segment_end (segment2, segment2->p_vaddr));
7664 if (extra_length > 0)
7666 segment2->p_memsz += extra_length;
7667 segment2->p_filesz += extra_length;
7670 segment->p_type = PT_NULL;
7672 /* Since we have deleted P we must restart the outer loop. */
7673 i = 0;
7674 segment = elf_tdata (ibfd)->phdr;
7675 break;
7677 else
7679 /* Extend SEGMENT to include SEGMENT2 and then delete
7680 SEGMENT2. */
7681 extra_length = (segment_end (segment2, segment2->p_vaddr)
7682 - segment_end (segment, segment->p_vaddr));
7684 if (extra_length > 0)
7686 segment->p_memsz += extra_length;
7687 segment->p_filesz += extra_length;
7690 segment2->p_type = PT_NULL;
7695 /* The second scan attempts to assign sections to segments. */
7696 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7697 i < num_segments;
7698 i++, segment++)
7700 unsigned int section_count;
7701 asection **sections;
7702 asection *output_section;
7703 unsigned int isec;
7704 asection *matching_lma;
7705 asection *suggested_lma;
7706 unsigned int j;
7707 size_t amt;
7708 asection *first_section;
7710 if (segment->p_type == PT_NULL)
7711 continue;
7713 first_section = NULL;
7714 /* Compute how many sections might be placed into this segment. */
7715 for (section = ibfd->sections, section_count = 0;
7716 section != NULL;
7717 section = section->next)
7719 /* Find the first section in the input segment, which may be
7720 removed from the corresponding output segment. */
7721 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb))
7723 if (first_section == NULL)
7724 first_section = section;
7725 if (section->output_section != NULL)
7726 ++section_count;
7730 /* Allocate a segment map big enough to contain
7731 all of the sections we have selected. */
7732 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7733 amt += section_count * sizeof (asection *);
7734 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7735 if (map == NULL)
7736 return false;
7738 /* Initialise the fields of the segment map. Default to
7739 using the physical address of the segment in the input BFD. */
7740 map->next = NULL;
7741 map->p_type = segment->p_type;
7742 map->p_flags = segment->p_flags;
7743 map->p_flags_valid = 1;
7745 if (map->p_type == PT_LOAD
7746 && (ibfd->flags & D_PAGED) != 0
7747 && maxpagesize > 1
7748 && segment->p_align > 1)
7750 map->p_align = segment->p_align;
7751 if (segment->p_align > maxpagesize)
7752 map->p_align = maxpagesize;
7753 map->p_align_valid = 1;
7756 /* If the first section in the input segment is removed, there is
7757 no need to preserve segment physical address in the corresponding
7758 output segment. */
7759 if (!first_section || first_section->output_section != NULL)
7761 map->p_paddr = segment->p_paddr;
7762 map->p_paddr_valid = p_paddr_valid;
7765 /* Determine if this segment contains the ELF file header
7766 and if it contains the program headers themselves. */
7767 map->includes_filehdr = (segment->p_offset == 0
7768 && segment->p_filesz >= iehdr->e_ehsize);
7769 map->includes_phdrs = 0;
7771 if (!phdr_included || segment->p_type != PT_LOAD)
7773 map->includes_phdrs =
7774 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7775 && (segment->p_offset + segment->p_filesz
7776 >= ((bfd_vma) iehdr->e_phoff
7777 + iehdr->e_phnum * iehdr->e_phentsize)));
7779 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7780 phdr_included = true;
7783 if (section_count == 0)
7785 /* Special segments, such as the PT_PHDR segment, may contain
7786 no sections, but ordinary, loadable segments should contain
7787 something. They are allowed by the ELF spec however, so only
7788 a warning is produced.
7789 Don't warn if an empty PT_LOAD contains the program headers.
7790 There is however the valid use case of embedded systems which
7791 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7792 flash memory with zeros. No warning is shown for that case. */
7793 if (segment->p_type == PT_LOAD
7794 && !map->includes_phdrs
7795 && (segment->p_filesz > 0 || segment->p_memsz == 0))
7796 /* xgettext:c-format */
7797 _bfd_error_handler
7798 (_("%pB: warning: empty loadable segment detected"
7799 " at vaddr=%#" PRIx64 ", is this intentional?"),
7800 ibfd, (uint64_t) segment->p_vaddr);
7802 map->p_vaddr_offset = segment->p_vaddr / opb;
7803 map->count = 0;
7804 *pointer_to_map = map;
7805 pointer_to_map = &map->next;
7807 continue;
7810 /* Now scan the sections in the input BFD again and attempt
7811 to add their corresponding output sections to the segment map.
7812 The problem here is how to handle an output section which has
7813 been moved (ie had its LMA changed). There are four possibilities:
7815 1. None of the sections have been moved.
7816 In this case we can continue to use the segment LMA from the
7817 input BFD.
7819 2. All of the sections have been moved by the same amount.
7820 In this case we can change the segment's LMA to match the LMA
7821 of the first section.
7823 3. Some of the sections have been moved, others have not.
7824 In this case those sections which have not been moved can be
7825 placed in the current segment which will have to have its size,
7826 and possibly its LMA changed, and a new segment or segments will
7827 have to be created to contain the other sections.
7829 4. The sections have been moved, but not by the same amount.
7830 In this case we can change the segment's LMA to match the LMA
7831 of the first section and we will have to create a new segment
7832 or segments to contain the other sections.
7834 In order to save time, we allocate an array to hold the section
7835 pointers that we are interested in. As these sections get assigned
7836 to a segment, they are removed from this array. */
7838 amt = section_count * sizeof (asection *);
7839 sections = (asection **) bfd_malloc (amt);
7840 if (sections == NULL)
7841 return false;
7843 /* Step One: Scan for segment vs section LMA conflicts.
7844 Also add the sections to the section array allocated above.
7845 Also add the sections to the current segment. In the common
7846 case, where the sections have not been moved, this means that
7847 we have completely filled the segment, and there is nothing
7848 more to do. */
7849 isec = 0;
7850 matching_lma = NULL;
7851 suggested_lma = NULL;
7853 for (section = first_section, j = 0;
7854 section != NULL;
7855 section = section->next)
7857 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb))
7859 output_section = section->output_section;
7861 sections[j++] = section;
7863 /* The Solaris native linker always sets p_paddr to 0.
7864 We try to catch that case here, and set it to the
7865 correct value. Note - some backends require that
7866 p_paddr be left as zero. */
7867 if (!p_paddr_valid
7868 && segment->p_vaddr != 0
7869 && !bed->want_p_paddr_set_to_zero
7870 && isec == 0
7871 && output_section->lma != 0
7872 && (align_power (segment->p_vaddr
7873 + (map->includes_filehdr
7874 ? iehdr->e_ehsize : 0)
7875 + (map->includes_phdrs
7876 ? iehdr->e_phnum * iehdr->e_phentsize
7877 : 0),
7878 output_section->alignment_power * opb)
7879 == (output_section->vma * opb)))
7880 map->p_paddr = segment->p_vaddr;
7882 /* Match up the physical address of the segment with the
7883 LMA address of the output section. */
7884 if (is_contained_by (output_section, segment, map->p_paddr,
7885 0, opb, false)
7886 || is_note (section, segment))
7888 if (matching_lma == NULL
7889 || output_section->lma < matching_lma->lma)
7890 matching_lma = output_section;
7892 /* We assume that if the section fits within the segment
7893 then it does not overlap any other section within that
7894 segment. */
7895 map->sections[isec++] = output_section;
7897 else if (suggested_lma == NULL)
7898 suggested_lma = output_section;
7900 if (j == section_count)
7901 break;
7905 BFD_ASSERT (j == section_count);
7907 /* Step Two: Adjust the physical address of the current segment,
7908 if necessary. */
7909 if (isec == section_count)
7911 /* All of the sections fitted within the segment as currently
7912 specified. This is the default case. Add the segment to
7913 the list of built segments and carry on to process the next
7914 program header in the input BFD. */
7915 map->count = section_count;
7916 *pointer_to_map = map;
7917 pointer_to_map = &map->next;
7919 if (p_paddr_valid
7920 && !bed->want_p_paddr_set_to_zero)
7922 bfd_vma hdr_size = 0;
7923 if (map->includes_filehdr)
7924 hdr_size = iehdr->e_ehsize;
7925 if (map->includes_phdrs)
7926 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7928 /* Account for padding before the first section in the
7929 segment. */
7930 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7931 - matching_lma->lma);
7934 free (sections);
7935 continue;
7937 else
7939 /* Change the current segment's physical address to match
7940 the LMA of the first section that fitted, or if no
7941 section fitted, the first section. */
7942 if (matching_lma == NULL)
7943 matching_lma = suggested_lma;
7945 map->p_paddr = matching_lma->lma * opb;
7947 /* Offset the segment physical address from the lma
7948 to allow for space taken up by elf headers. */
7949 if (map->includes_phdrs)
7951 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7953 /* iehdr->e_phnum is just an estimate of the number
7954 of program headers that we will need. Make a note
7955 here of the number we used and the segment we chose
7956 to hold these headers, so that we can adjust the
7957 offset when we know the correct value. */
7958 phdr_adjust_num = iehdr->e_phnum;
7959 phdr_adjust_seg = map;
7962 if (map->includes_filehdr)
7964 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7965 map->p_paddr -= iehdr->e_ehsize;
7966 /* We've subtracted off the size of headers from the
7967 first section lma, but there may have been some
7968 alignment padding before that section too. Try to
7969 account for that by adjusting the segment lma down to
7970 the same alignment. */
7971 if (segment->p_align != 0 && segment->p_align < align)
7972 align = segment->p_align;
7973 map->p_paddr &= -(align * opb);
7977 /* Step Three: Loop over the sections again, this time assigning
7978 those that fit to the current segment and removing them from the
7979 sections array; but making sure not to leave large gaps. Once all
7980 possible sections have been assigned to the current segment it is
7981 added to the list of built segments and if sections still remain
7982 to be assigned, a new segment is constructed before repeating
7983 the loop. */
7984 isec = 0;
7987 map->count = 0;
7988 suggested_lma = NULL;
7990 /* Fill the current segment with sections that fit. */
7991 for (j = 0; j < section_count; j++)
7993 section = sections[j];
7995 if (section == NULL)
7996 continue;
7998 output_section = section->output_section;
8000 BFD_ASSERT (output_section != NULL);
8002 if (is_contained_by (output_section, segment, map->p_paddr,
8003 0, opb, false)
8004 || is_note (section, segment))
8006 if (map->count == 0)
8008 /* If the first section in a segment does not start at
8009 the beginning of the segment, then something is
8010 wrong. */
8011 if (align_power (map->p_paddr
8012 + (map->includes_filehdr
8013 ? iehdr->e_ehsize : 0)
8014 + (map->includes_phdrs
8015 ? iehdr->e_phnum * iehdr->e_phentsize
8016 : 0),
8017 output_section->alignment_power * opb)
8018 != output_section->lma * opb)
8019 goto sorry;
8021 else
8023 asection *prev_sec;
8025 prev_sec = map->sections[map->count - 1];
8027 /* If the gap between the end of the previous section
8028 and the start of this section is more than
8029 maxpagesize then we need to start a new segment. */
8030 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
8031 maxpagesize)
8032 < BFD_ALIGN (output_section->lma, maxpagesize))
8033 || (prev_sec->lma + prev_sec->size
8034 > output_section->lma))
8036 if (suggested_lma == NULL)
8037 suggested_lma = output_section;
8039 continue;
8043 map->sections[map->count++] = output_section;
8044 ++isec;
8045 sections[j] = NULL;
8046 if (segment->p_type == PT_LOAD)
8047 section->segment_mark = true;
8049 else if (suggested_lma == NULL)
8050 suggested_lma = output_section;
8053 /* PR 23932. A corrupt input file may contain sections that cannot
8054 be assigned to any segment - because for example they have a
8055 negative size - or segments that do not contain any sections.
8056 But there are also valid reasons why a segment can be empty.
8057 So allow a count of zero. */
8059 /* Add the current segment to the list of built segments. */
8060 *pointer_to_map = map;
8061 pointer_to_map = &map->next;
8063 if (isec < section_count)
8065 /* We still have not allocated all of the sections to
8066 segments. Create a new segment here, initialise it
8067 and carry on looping. */
8068 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8069 amt += section_count * sizeof (asection *);
8070 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
8071 if (map == NULL)
8073 free (sections);
8074 return false;
8077 /* Initialise the fields of the segment map. Set the physical
8078 physical address to the LMA of the first section that has
8079 not yet been assigned. */
8080 map->next = NULL;
8081 map->p_type = segment->p_type;
8082 map->p_flags = segment->p_flags;
8083 map->p_flags_valid = 1;
8084 map->p_paddr = suggested_lma->lma * opb;
8085 map->p_paddr_valid = p_paddr_valid;
8086 map->includes_filehdr = 0;
8087 map->includes_phdrs = 0;
8090 continue;
8091 sorry:
8092 bfd_set_error (bfd_error_sorry);
8093 free (sections);
8094 return false;
8096 while (isec < section_count);
8098 free (sections);
8101 elf_seg_map (obfd) = map_first;
8103 /* If we had to estimate the number of program headers that were
8104 going to be needed, then check our estimate now and adjust
8105 the offset if necessary. */
8106 if (phdr_adjust_seg != NULL)
8108 unsigned int count;
8110 for (count = 0, map = map_first; map != NULL; map = map->next)
8111 count++;
8113 if (count > phdr_adjust_num)
8114 phdr_adjust_seg->p_paddr
8115 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
8117 for (map = map_first; map != NULL; map = map->next)
8118 if (map->p_type == PT_PHDR)
8120 bfd_vma adjust
8121 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
8122 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
8123 break;
8127 #undef IS_SOLARIS_PT_INTERP
8128 #undef IS_SECTION_IN_INPUT_SEGMENT
8129 #undef INCLUDE_SECTION_IN_SEGMENT
8130 #undef SEGMENT_AFTER_SEGMENT
8131 #undef SEGMENT_OVERLAPS
8132 return true;
8135 /* Return true if p_align in the ELF program header in ABFD is valid. */
8137 static bool
8138 elf_is_p_align_valid (bfd *abfd)
8140 unsigned int i;
8141 Elf_Internal_Phdr *segment;
8142 unsigned int num_segments;
8143 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8144 bfd_size_type maxpagesize = bed->maxpagesize;
8145 bfd_size_type p_align = bed->p_align;
8147 /* Return true if the default p_align value isn't set or the maximum
8148 page size is the same as the minimum page size. */
8149 if (p_align == 0 || maxpagesize == bed->minpagesize)
8150 return true;
8152 /* When the default p_align value is set, p_align may be set to the
8153 default p_align value while segments are aligned to the maximum
8154 page size. In this case, the input p_align will be ignored and
8155 the maximum page size will be used to align the output segments. */
8156 segment = elf_tdata (abfd)->phdr;
8157 num_segments = elf_elfheader (abfd)->e_phnum;
8158 for (i = 0; i < num_segments; i++, segment++)
8159 if (segment->p_type == PT_LOAD
8160 && (segment->p_align != p_align
8161 || vma_page_aligned_bias (segment->p_vaddr,
8162 segment->p_offset,
8163 maxpagesize) != 0))
8164 return true;
8166 return false;
8169 /* Copy ELF program header information. */
8171 static bool
8172 copy_elf_program_header (bfd *ibfd, bfd *obfd)
8174 Elf_Internal_Ehdr *iehdr;
8175 struct elf_segment_map *map;
8176 struct elf_segment_map *map_first;
8177 struct elf_segment_map **pointer_to_map;
8178 Elf_Internal_Phdr *segment;
8179 unsigned int i;
8180 unsigned int num_segments;
8181 bool phdr_included = false;
8182 bool p_paddr_valid;
8183 bool p_palign_valid;
8184 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
8186 iehdr = elf_elfheader (ibfd);
8188 map_first = NULL;
8189 pointer_to_map = &map_first;
8191 /* If all the segment p_paddr fields are zero, don't set
8192 map->p_paddr_valid. */
8193 p_paddr_valid = false;
8194 num_segments = elf_elfheader (ibfd)->e_phnum;
8195 for (i = 0, segment = elf_tdata (ibfd)->phdr;
8196 i < num_segments;
8197 i++, segment++)
8198 if (segment->p_paddr != 0)
8200 p_paddr_valid = true;
8201 break;
8204 p_palign_valid = elf_is_p_align_valid (ibfd);
8206 for (i = 0, segment = elf_tdata (ibfd)->phdr;
8207 i < num_segments;
8208 i++, segment++)
8210 asection *section;
8211 unsigned int section_count;
8212 size_t amt;
8213 Elf_Internal_Shdr *this_hdr;
8214 asection *first_section = NULL;
8215 asection *lowest_section;
8217 /* Compute how many sections are in this segment. */
8218 for (section = ibfd->sections, section_count = 0;
8219 section != NULL;
8220 section = section->next)
8222 this_hdr = &(elf_section_data(section)->this_hdr);
8223 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8225 if (first_section == NULL)
8226 first_section = section;
8227 section_count++;
8231 /* Allocate a segment map big enough to contain
8232 all of the sections we have selected. */
8233 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8234 amt += section_count * sizeof (asection *);
8235 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
8236 if (map == NULL)
8237 return false;
8239 /* Initialize the fields of the output segment map with the
8240 input segment. */
8241 map->next = NULL;
8242 map->p_type = segment->p_type;
8243 map->p_flags = segment->p_flags;
8244 map->p_flags_valid = 1;
8245 map->p_paddr = segment->p_paddr;
8246 map->p_paddr_valid = p_paddr_valid;
8247 map->p_align = segment->p_align;
8248 /* Keep p_align of PT_GNU_STACK for stack alignment. */
8249 map->p_align_valid = (map->p_type == PT_GNU_STACK
8250 || p_palign_valid);
8251 map->p_vaddr_offset = 0;
8253 if (map->p_type == PT_GNU_RELRO
8254 || map->p_type == PT_GNU_STACK)
8256 /* The PT_GNU_RELRO segment may contain the first a few
8257 bytes in the .got.plt section even if the whole .got.plt
8258 section isn't in the PT_GNU_RELRO segment. We won't
8259 change the size of the PT_GNU_RELRO segment.
8260 Similarly, PT_GNU_STACK size is significant on uclinux
8261 systems. */
8262 map->p_size = segment->p_memsz;
8263 map->p_size_valid = 1;
8266 /* Determine if this segment contains the ELF file header
8267 and if it contains the program headers themselves. */
8268 map->includes_filehdr = (segment->p_offset == 0
8269 && segment->p_filesz >= iehdr->e_ehsize);
8271 map->includes_phdrs = 0;
8272 if (! phdr_included || segment->p_type != PT_LOAD)
8274 map->includes_phdrs =
8275 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
8276 && (segment->p_offset + segment->p_filesz
8277 >= ((bfd_vma) iehdr->e_phoff
8278 + iehdr->e_phnum * iehdr->e_phentsize)));
8280 if (segment->p_type == PT_LOAD && map->includes_phdrs)
8281 phdr_included = true;
8284 lowest_section = NULL;
8285 if (section_count != 0)
8287 unsigned int isec = 0;
8289 for (section = first_section;
8290 section != NULL;
8291 section = section->next)
8293 this_hdr = &(elf_section_data(section)->this_hdr);
8294 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8296 map->sections[isec++] = section->output_section;
8297 if ((section->flags & SEC_ALLOC) != 0)
8299 bfd_vma seg_off;
8301 if (lowest_section == NULL
8302 || section->lma < lowest_section->lma)
8303 lowest_section = section;
8305 /* Section lmas are set up from PT_LOAD header
8306 p_paddr in _bfd_elf_make_section_from_shdr.
8307 If this header has a p_paddr that disagrees
8308 with the section lma, flag the p_paddr as
8309 invalid. */
8310 if ((section->flags & SEC_LOAD) != 0)
8311 seg_off = this_hdr->sh_offset - segment->p_offset;
8312 else
8313 seg_off = this_hdr->sh_addr - segment->p_vaddr;
8314 if (section->lma * opb - segment->p_paddr != seg_off)
8315 map->p_paddr_valid = false;
8317 if (isec == section_count)
8318 break;
8323 if (section_count == 0)
8324 map->p_vaddr_offset = segment->p_vaddr / opb;
8325 else if (map->p_paddr_valid)
8327 /* Account for padding before the first section in the segment. */
8328 bfd_vma hdr_size = 0;
8329 if (map->includes_filehdr)
8330 hdr_size = iehdr->e_ehsize;
8331 if (map->includes_phdrs)
8332 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
8334 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
8335 - (lowest_section ? lowest_section->lma : 0));
8338 map->count = section_count;
8339 *pointer_to_map = map;
8340 pointer_to_map = &map->next;
8343 elf_seg_map (obfd) = map_first;
8344 return true;
8347 /* Copy private BFD data. This copies or rewrites ELF program header
8348 information. */
8350 static bool
8351 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
8353 bfd_vma maxpagesize;
8355 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8356 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8357 return true;
8359 if (elf_tdata (ibfd)->phdr == NULL)
8360 return true;
8362 if (ibfd->xvec == obfd->xvec)
8364 /* Check to see if any sections in the input BFD
8365 covered by ELF program header have changed. */
8366 Elf_Internal_Phdr *segment;
8367 asection *section, *osec;
8368 unsigned int i, num_segments;
8369 Elf_Internal_Shdr *this_hdr;
8370 const struct elf_backend_data *bed;
8372 bed = get_elf_backend_data (ibfd);
8374 /* Regenerate the segment map if p_paddr is set to 0. */
8375 if (bed->want_p_paddr_set_to_zero)
8376 goto rewrite;
8378 /* Initialize the segment mark field. */
8379 for (section = obfd->sections; section != NULL;
8380 section = section->next)
8381 section->segment_mark = false;
8383 num_segments = elf_elfheader (ibfd)->e_phnum;
8384 for (i = 0, segment = elf_tdata (ibfd)->phdr;
8385 i < num_segments;
8386 i++, segment++)
8388 /* PR binutils/3535. The Solaris linker always sets the p_paddr
8389 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
8390 which severly confuses things, so always regenerate the segment
8391 map in this case. */
8392 if (segment->p_paddr == 0
8393 && segment->p_memsz == 0
8394 && (segment->p_type == PT_INTERP
8395 || segment->p_type == PT_DYNAMIC))
8396 goto rewrite;
8398 for (section = ibfd->sections;
8399 section != NULL; section = section->next)
8401 /* We mark the output section so that we know it comes
8402 from the input BFD. */
8403 osec = section->output_section;
8404 if (osec)
8405 osec->segment_mark = true;
8407 /* Check if this section is covered by the segment. */
8408 this_hdr = &(elf_section_data(section)->this_hdr);
8409 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8411 /* FIXME: Check if its output section is changed or
8412 removed. What else do we need to check? */
8413 if (osec == NULL
8414 || section->flags != osec->flags
8415 || section->lma != osec->lma
8416 || section->vma != osec->vma
8417 || section->size != osec->size
8418 || section->rawsize != osec->rawsize
8419 || section->alignment_power != osec->alignment_power)
8420 goto rewrite;
8425 /* Check to see if any output section do not come from the
8426 input BFD. */
8427 for (section = obfd->sections; section != NULL;
8428 section = section->next)
8430 if (!section->segment_mark)
8431 goto rewrite;
8432 else
8433 section->segment_mark = false;
8436 return copy_elf_program_header (ibfd, obfd);
8439 rewrite:
8440 maxpagesize = 0;
8441 if (ibfd->xvec == obfd->xvec)
8443 /* When rewriting program header, set the output maxpagesize to
8444 the maximum alignment of input PT_LOAD segments. */
8445 Elf_Internal_Phdr *segment;
8446 unsigned int i;
8447 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
8449 for (i = 0, segment = elf_tdata (ibfd)->phdr;
8450 i < num_segments;
8451 i++, segment++)
8452 if (segment->p_type == PT_LOAD
8453 && maxpagesize < segment->p_align)
8455 /* PR 17512: file: f17299af. */
8456 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
8457 /* xgettext:c-format */
8458 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
8459 PRIx64 " is too large"),
8460 ibfd, (uint64_t) segment->p_align);
8461 else
8462 maxpagesize = segment->p_align;
8465 if (maxpagesize == 0)
8466 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
8468 return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
8471 /* Initialize private output section information from input section. */
8473 bool
8474 _bfd_elf_init_private_section_data (bfd *ibfd,
8475 asection *isec,
8476 bfd *obfd,
8477 asection *osec,
8478 struct bfd_link_info *link_info)
8481 Elf_Internal_Shdr *ihdr, *ohdr;
8482 bool final_link = (link_info != NULL
8483 && !bfd_link_relocatable (link_info));
8485 if (ibfd->xvec->flavour != bfd_target_elf_flavour
8486 || obfd->xvec->flavour != bfd_target_elf_flavour)
8487 return true;
8489 BFD_ASSERT (elf_section_data (osec) != NULL);
8491 /* If this is a known ABI section, ELF section type and flags may
8492 have been set up when OSEC was created. For normal sections we
8493 allow the user to override the type and flags other than
8494 SHF_MASKOS and SHF_MASKPROC. */
8495 if (elf_section_type (osec) == SHT_PROGBITS
8496 || elf_section_type (osec) == SHT_NOTE
8497 || elf_section_type (osec) == SHT_NOBITS)
8498 elf_section_type (osec) = SHT_NULL;
8499 /* For objcopy and relocatable link, copy the ELF section type from
8500 the input file if the BFD section flags are the same. (If they
8501 are different the user may be doing something like
8502 "objcopy --set-section-flags .text=alloc,data".) For a final
8503 link allow some flags that the linker clears to differ. */
8504 if (elf_section_type (osec) == SHT_NULL
8505 && (osec->flags == isec->flags
8506 || (final_link
8507 && ((osec->flags ^ isec->flags)
8508 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
8509 elf_section_type (osec) = elf_section_type (isec);
8511 /* FIXME: Is this correct for all OS/PROC specific flags? */
8512 elf_section_flags (osec) = (elf_section_flags (isec)
8513 & (SHF_MASKOS | SHF_MASKPROC));
8515 /* Copy sh_info from input for mbind section. */
8516 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
8517 && elf_section_flags (isec) & SHF_GNU_MBIND)
8518 elf_section_data (osec)->this_hdr.sh_info
8519 = elf_section_data (isec)->this_hdr.sh_info;
8521 /* Set things up for objcopy and relocatable link. The output
8522 SHT_GROUP section will have its elf_next_in_group pointing back
8523 to the input group members. Ignore linker created group section.
8524 See elfNN_ia64_object_p in elfxx-ia64.c. */
8525 if ((link_info == NULL
8526 || !link_info->resolve_section_groups)
8527 && (elf_sec_group (isec) == NULL
8528 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
8530 if (elf_section_flags (isec) & SHF_GROUP)
8531 elf_section_flags (osec) |= SHF_GROUP;
8532 elf_next_in_group (osec) = elf_next_in_group (isec);
8533 elf_section_data (osec)->group = elf_section_data (isec)->group;
8536 /* If not decompress, preserve SHF_COMPRESSED. */
8537 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
8538 elf_section_flags (osec) |= (elf_section_flags (isec)
8539 & SHF_COMPRESSED);
8541 ihdr = &elf_section_data (isec)->this_hdr;
8543 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
8544 don't use the output section of the linked-to section since it
8545 may be NULL at this point. */
8546 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
8548 ohdr = &elf_section_data (osec)->this_hdr;
8549 ohdr->sh_flags |= SHF_LINK_ORDER;
8550 elf_linked_to_section (osec) = elf_linked_to_section (isec);
8553 osec->use_rela_p = isec->use_rela_p;
8555 return true;
8558 /* Copy private section information. This copies over the entsize
8559 field, and sometimes the info field. */
8561 bool
8562 _bfd_elf_copy_private_section_data (bfd *ibfd,
8563 asection *isec,
8564 bfd *obfd,
8565 asection *osec)
8567 Elf_Internal_Shdr *ihdr, *ohdr;
8569 if (ibfd->xvec->flavour != bfd_target_elf_flavour
8570 || obfd->xvec->flavour != bfd_target_elf_flavour)
8571 return true;
8573 ihdr = &elf_section_data (isec)->this_hdr;
8574 ohdr = &elf_section_data (osec)->this_hdr;
8576 ohdr->sh_entsize = ihdr->sh_entsize;
8578 if (ihdr->sh_type == SHT_SYMTAB
8579 || ihdr->sh_type == SHT_DYNSYM
8580 || ihdr->sh_type == SHT_GNU_verneed
8581 || ihdr->sh_type == SHT_GNU_verdef)
8582 ohdr->sh_info = ihdr->sh_info;
8584 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
8585 NULL);
8588 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8589 necessary if we are removing either the SHT_GROUP section or any of
8590 the group member sections. DISCARDED is the value that a section's
8591 output_section has if the section will be discarded, NULL when this
8592 function is called from objcopy, bfd_abs_section_ptr when called
8593 from the linker. */
8595 bool
8596 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
8598 asection *isec;
8600 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
8601 if (elf_section_type (isec) == SHT_GROUP)
8603 asection *first = elf_next_in_group (isec);
8604 asection *s = first;
8605 bfd_size_type removed = 0;
8607 while (s != NULL)
8609 /* If this member section is being output but the
8610 SHT_GROUP section is not, then clear the group info
8611 set up by _bfd_elf_copy_private_section_data. */
8612 if (s->output_section != discarded
8613 && isec->output_section == discarded)
8615 elf_section_flags (s->output_section) &= ~SHF_GROUP;
8616 elf_group_name (s->output_section) = NULL;
8618 else
8620 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
8621 if (s->output_section == discarded
8622 && isec->output_section != discarded)
8624 /* Conversely, if the member section is not being
8625 output but the SHT_GROUP section is, then adjust
8626 its size. */
8627 removed += 4;
8628 if (elf_sec->rel.hdr != NULL
8629 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
8630 removed += 4;
8631 if (elf_sec->rela.hdr != NULL
8632 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
8633 removed += 4;
8635 else
8637 /* Also adjust for zero-sized relocation member
8638 section. */
8639 if (elf_sec->rel.hdr != NULL
8640 && elf_sec->rel.hdr->sh_size == 0)
8641 removed += 4;
8642 if (elf_sec->rela.hdr != NULL
8643 && elf_sec->rela.hdr->sh_size == 0)
8644 removed += 4;
8647 s = elf_next_in_group (s);
8648 if (s == first)
8649 break;
8651 if (removed != 0)
8653 if (discarded != NULL)
8655 /* If we've been called for ld -r, then we need to
8656 adjust the input section size. */
8657 if (isec->rawsize == 0)
8658 isec->rawsize = isec->size;
8659 isec->size = isec->rawsize - removed;
8660 if (isec->size <= 4)
8662 isec->size = 0;
8663 isec->flags |= SEC_EXCLUDE;
8666 else if (isec->output_section != NULL)
8668 /* Adjust the output section size when called from
8669 objcopy. */
8670 isec->output_section->size -= removed;
8671 if (isec->output_section->size <= 4)
8673 isec->output_section->size = 0;
8674 isec->output_section->flags |= SEC_EXCLUDE;
8680 return true;
8683 /* Copy private header information. */
8685 bool
8686 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8688 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8689 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8690 return true;
8692 /* Copy over private BFD data if it has not already been copied.
8693 This must be done here, rather than in the copy_private_bfd_data
8694 entry point, because the latter is called after the section
8695 contents have been set, which means that the program headers have
8696 already been worked out. */
8697 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
8699 if (! copy_private_bfd_data (ibfd, obfd))
8700 return false;
8703 return _bfd_elf_fixup_group_sections (ibfd, NULL);
8706 /* Copy private symbol information. If this symbol is in a section
8707 which we did not map into a BFD section, try to map the section
8708 index correctly. We use special macro definitions for the mapped
8709 section indices; these definitions are interpreted by the
8710 swap_out_syms function. */
8712 #define MAP_ONESYMTAB (SHN_HIOS + 1)
8713 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
8714 #define MAP_STRTAB (SHN_HIOS + 3)
8715 #define MAP_SHSTRTAB (SHN_HIOS + 4)
8716 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
8718 bool
8719 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
8720 asymbol *isymarg,
8721 bfd *obfd,
8722 asymbol *osymarg)
8724 elf_symbol_type *isym, *osym;
8726 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8727 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8728 return true;
8730 isym = elf_symbol_from (isymarg);
8731 osym = elf_symbol_from (osymarg);
8733 if (isym != NULL
8734 && isym->internal_elf_sym.st_shndx != 0
8735 && osym != NULL
8736 && bfd_is_abs_section (isym->symbol.section))
8738 unsigned int shndx;
8740 shndx = isym->internal_elf_sym.st_shndx;
8741 if (shndx == elf_onesymtab (ibfd))
8742 shndx = MAP_ONESYMTAB;
8743 else if (shndx == elf_dynsymtab (ibfd))
8744 shndx = MAP_DYNSYMTAB;
8745 else if (shndx == elf_strtab_sec (ibfd))
8746 shndx = MAP_STRTAB;
8747 else if (shndx == elf_shstrtab_sec (ibfd))
8748 shndx = MAP_SHSTRTAB;
8749 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
8750 shndx = MAP_SYM_SHNDX;
8751 osym->internal_elf_sym.st_shndx = shndx;
8754 return true;
8757 /* Swap out the symbols. */
8759 static bool
8760 swap_out_syms (bfd *abfd,
8761 struct elf_strtab_hash **sttp,
8762 int relocatable_p,
8763 struct bfd_link_info *info)
8765 const struct elf_backend_data *bed;
8766 unsigned int symcount;
8767 asymbol **syms;
8768 struct elf_strtab_hash *stt;
8769 Elf_Internal_Shdr *symtab_hdr;
8770 Elf_Internal_Shdr *symtab_shndx_hdr;
8771 Elf_Internal_Shdr *symstrtab_hdr;
8772 struct elf_sym_strtab *symstrtab;
8773 bfd_byte *outbound_syms;
8774 bfd_byte *outbound_shndx;
8775 unsigned long outbound_syms_index;
8776 unsigned int idx;
8777 unsigned int num_locals;
8778 size_t amt;
8779 bool name_local_sections;
8781 if (!elf_map_symbols (abfd, &num_locals))
8782 return false;
8784 /* Dump out the symtabs. */
8785 stt = _bfd_elf_strtab_init ();
8786 if (stt == NULL)
8787 return false;
8789 bed = get_elf_backend_data (abfd);
8790 symcount = bfd_get_symcount (abfd);
8791 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8792 symtab_hdr->sh_type = SHT_SYMTAB;
8793 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8794 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
8795 symtab_hdr->sh_info = num_locals + 1;
8796 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
8798 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8799 symstrtab_hdr->sh_type = SHT_STRTAB;
8801 /* Allocate buffer to swap out the .strtab section. */
8802 if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8803 || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
8805 bfd_set_error (bfd_error_no_memory);
8806 _bfd_elf_strtab_free (stt);
8807 return false;
8810 if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8811 || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
8813 error_no_mem:
8814 bfd_set_error (bfd_error_no_memory);
8815 error_return:
8816 free (symstrtab);
8817 _bfd_elf_strtab_free (stt);
8818 return false;
8820 symtab_hdr->contents = outbound_syms;
8821 outbound_syms_index = 0;
8823 outbound_shndx = NULL;
8825 if (elf_symtab_shndx_list (abfd))
8827 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8828 if (symtab_shndx_hdr->sh_name != 0)
8830 if (_bfd_mul_overflow (symcount + 1,
8831 sizeof (Elf_External_Sym_Shndx), &amt))
8832 goto error_no_mem;
8833 outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt);
8834 if (outbound_shndx == NULL)
8835 goto error_return;
8837 symtab_shndx_hdr->contents = outbound_shndx;
8838 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8839 symtab_shndx_hdr->sh_size = amt;
8840 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8841 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8843 /* FIXME: What about any other headers in the list ? */
8846 /* Now generate the data (for "contents"). */
8848 /* Fill in zeroth symbol and swap it out. */
8849 Elf_Internal_Sym sym;
8850 sym.st_name = 0;
8851 sym.st_value = 0;
8852 sym.st_size = 0;
8853 sym.st_info = 0;
8854 sym.st_other = 0;
8855 sym.st_shndx = SHN_UNDEF;
8856 sym.st_target_internal = 0;
8857 symstrtab[0].sym = sym;
8858 symstrtab[0].dest_index = outbound_syms_index;
8859 outbound_syms_index++;
8862 name_local_sections
8863 = (bed->elf_backend_name_local_section_symbols
8864 && bed->elf_backend_name_local_section_symbols (abfd));
8866 syms = bfd_get_outsymbols (abfd);
8867 for (idx = 0; idx < symcount;)
8869 Elf_Internal_Sym sym;
8870 bfd_vma value = syms[idx]->value;
8871 elf_symbol_type *type_ptr;
8872 flagword flags = syms[idx]->flags;
8873 int type;
8875 if (!name_local_sections
8876 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8878 /* Local section symbols have no name. */
8879 sym.st_name = (unsigned long) -1;
8881 else
8883 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8884 to get the final offset for st_name. */
8885 sym.st_name
8886 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
8887 false);
8888 if (sym.st_name == (unsigned long) -1)
8889 goto error_return;
8892 type_ptr = elf_symbol_from (syms[idx]);
8894 if ((flags & BSF_SECTION_SYM) == 0
8895 && bfd_is_com_section (syms[idx]->section))
8897 /* ELF common symbols put the alignment into the `value' field,
8898 and the size into the `size' field. This is backwards from
8899 how BFD handles it, so reverse it here. */
8900 sym.st_size = value;
8901 if (type_ptr == NULL
8902 || type_ptr->internal_elf_sym.st_value == 0)
8903 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8904 else
8905 sym.st_value = type_ptr->internal_elf_sym.st_value;
8906 sym.st_shndx = _bfd_elf_section_from_bfd_section
8907 (abfd, syms[idx]->section);
8909 else
8911 asection *sec = syms[idx]->section;
8912 unsigned int shndx;
8914 if (sec->output_section)
8916 value += sec->output_offset;
8917 sec = sec->output_section;
8920 /* Don't add in the section vma for relocatable output. */
8921 if (! relocatable_p)
8922 value += sec->vma;
8923 sym.st_value = value;
8924 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8926 if (bfd_is_abs_section (sec)
8927 && type_ptr != NULL
8928 && type_ptr->internal_elf_sym.st_shndx != 0)
8930 /* This symbol is in a real ELF section which we did
8931 not create as a BFD section. Undo the mapping done
8932 by copy_private_symbol_data. */
8933 shndx = type_ptr->internal_elf_sym.st_shndx;
8934 switch (shndx)
8936 case MAP_ONESYMTAB:
8937 shndx = elf_onesymtab (abfd);
8938 break;
8939 case MAP_DYNSYMTAB:
8940 shndx = elf_dynsymtab (abfd);
8941 break;
8942 case MAP_STRTAB:
8943 shndx = elf_strtab_sec (abfd);
8944 break;
8945 case MAP_SHSTRTAB:
8946 shndx = elf_shstrtab_sec (abfd);
8947 break;
8948 case MAP_SYM_SHNDX:
8949 if (elf_symtab_shndx_list (abfd))
8950 shndx = elf_symtab_shndx_list (abfd)->ndx;
8951 break;
8952 case SHN_COMMON:
8953 case SHN_ABS:
8954 shndx = SHN_ABS;
8955 break;
8956 default:
8957 if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8959 if (bed->symbol_section_index)
8960 shndx = bed->symbol_section_index (abfd, type_ptr);
8961 /* Otherwise just leave the index alone. */
8963 else
8965 if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8966 _bfd_error_handler (_("%pB: \
8967 Unable to handle section index %x in ELF symbol. Using ABS instead."),
8968 abfd, shndx);
8969 shndx = SHN_ABS;
8971 break;
8974 else
8976 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8978 if (shndx == SHN_BAD)
8980 asection *sec2;
8982 /* Writing this would be a hell of a lot easier if
8983 we had some decent documentation on bfd, and
8984 knew what to expect of the library, and what to
8985 demand of applications. For example, it
8986 appears that `objcopy' might not set the
8987 section of a symbol to be a section that is
8988 actually in the output file. */
8989 sec2 = bfd_get_section_by_name (abfd, sec->name);
8990 if (sec2 != NULL)
8991 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8992 if (shndx == SHN_BAD)
8994 /* xgettext:c-format */
8995 _bfd_error_handler
8996 (_("unable to find equivalent output section"
8997 " for symbol '%s' from section '%s'"),
8998 syms[idx]->name ? syms[idx]->name : "<Local sym>",
8999 sec->name);
9000 bfd_set_error (bfd_error_invalid_operation);
9001 goto error_return;
9006 sym.st_shndx = shndx;
9009 if ((flags & BSF_THREAD_LOCAL) != 0)
9010 type = STT_TLS;
9011 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
9012 type = STT_GNU_IFUNC;
9013 else if ((flags & BSF_FUNCTION) != 0)
9014 type = STT_FUNC;
9015 else if ((flags & BSF_OBJECT) != 0)
9016 type = STT_OBJECT;
9017 else if ((flags & BSF_RELC) != 0)
9018 type = STT_RELC;
9019 else if ((flags & BSF_SRELC) != 0)
9020 type = STT_SRELC;
9021 else
9022 type = STT_NOTYPE;
9024 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
9025 type = STT_TLS;
9027 /* Processor-specific types. */
9028 if (type_ptr != NULL
9029 && bed->elf_backend_get_symbol_type)
9030 type = ((*bed->elf_backend_get_symbol_type)
9031 (&type_ptr->internal_elf_sym, type));
9033 if (flags & BSF_SECTION_SYM)
9035 if (flags & BSF_GLOBAL)
9036 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9037 else
9038 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
9040 else if (bfd_is_com_section (syms[idx]->section))
9042 if (type != STT_TLS)
9044 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
9045 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
9046 ? STT_COMMON : STT_OBJECT);
9047 else
9048 type = ((flags & BSF_ELF_COMMON) != 0
9049 ? STT_COMMON : STT_OBJECT);
9051 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9053 else if (bfd_is_und_section (syms[idx]->section))
9054 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
9055 ? STB_WEAK
9056 : STB_GLOBAL),
9057 type);
9058 else if (flags & BSF_FILE)
9059 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9060 else
9062 int bind = STB_LOCAL;
9064 if (flags & BSF_LOCAL)
9065 bind = STB_LOCAL;
9066 else if (flags & BSF_GNU_UNIQUE)
9067 bind = STB_GNU_UNIQUE;
9068 else if (flags & BSF_WEAK)
9069 bind = STB_WEAK;
9070 else if (flags & BSF_GLOBAL)
9071 bind = STB_GLOBAL;
9073 sym.st_info = ELF_ST_INFO (bind, type);
9076 if (type_ptr != NULL)
9078 sym.st_other = type_ptr->internal_elf_sym.st_other;
9079 sym.st_target_internal
9080 = type_ptr->internal_elf_sym.st_target_internal;
9082 else
9084 sym.st_other = 0;
9085 sym.st_target_internal = 0;
9088 idx++;
9089 symstrtab[idx].sym = sym;
9090 symstrtab[idx].dest_index = outbound_syms_index;
9092 outbound_syms_index++;
9095 /* Finalize the .strtab section. */
9096 _bfd_elf_strtab_finalize (stt);
9098 /* Swap out the .strtab section. */
9099 for (idx = 0; idx <= symcount; idx++)
9101 struct elf_sym_strtab *elfsym = &symstrtab[idx];
9102 if (elfsym->sym.st_name == (unsigned long) -1)
9103 elfsym->sym.st_name = 0;
9104 else
9105 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
9106 elfsym->sym.st_name);
9107 if (info && info->callbacks->ctf_new_symbol)
9108 info->callbacks->ctf_new_symbol (elfsym->dest_index,
9109 &elfsym->sym);
9111 /* Inform the linker of the addition of this symbol. */
9113 bed->s->swap_symbol_out (abfd, &elfsym->sym,
9114 (outbound_syms
9115 + (elfsym->dest_index
9116 * bed->s->sizeof_sym)),
9117 NPTR_ADD (outbound_shndx,
9118 (elfsym->dest_index
9119 * sizeof (Elf_External_Sym_Shndx))));
9121 free (symstrtab);
9123 *sttp = stt;
9124 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
9125 symstrtab_hdr->sh_type = SHT_STRTAB;
9126 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
9127 symstrtab_hdr->sh_addr = 0;
9128 symstrtab_hdr->sh_entsize = 0;
9129 symstrtab_hdr->sh_link = 0;
9130 symstrtab_hdr->sh_info = 0;
9131 symstrtab_hdr->sh_addralign = 1;
9133 return true;
9136 /* Return the number of bytes required to hold the symtab vector.
9138 Note that we base it on the count plus 1, since we will null terminate
9139 the vector allocated based on this size. However, the ELF symbol table
9140 always has a dummy entry as symbol #0, so it ends up even. */
9142 long
9143 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
9145 bfd_size_type symcount;
9146 long symtab_size;
9147 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
9149 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9150 if (symcount > LONG_MAX / sizeof (asymbol *))
9152 bfd_set_error (bfd_error_file_too_big);
9153 return -1;
9155 symtab_size = symcount * (sizeof (asymbol *));
9156 if (symcount == 0)
9157 symtab_size = sizeof (asymbol *);
9158 else if (!bfd_write_p (abfd))
9160 ufile_ptr filesize = bfd_get_file_size (abfd);
9162 if (filesize != 0 && (unsigned long) symtab_size > filesize)
9164 bfd_set_error (bfd_error_file_truncated);
9165 return -1;
9169 return symtab_size;
9172 long
9173 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
9175 bfd_size_type symcount;
9176 long symtab_size;
9177 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
9179 if (elf_dynsymtab (abfd) == 0)
9181 /* Check if there is dynamic symbol table. */
9182 symcount = elf_tdata (abfd)->dt_symtab_count;
9183 if (symcount)
9184 goto compute_symtab_size;
9186 bfd_set_error (bfd_error_invalid_operation);
9187 return -1;
9190 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9191 if (symcount > LONG_MAX / sizeof (asymbol *))
9193 bfd_set_error (bfd_error_file_too_big);
9194 return -1;
9197 compute_symtab_size:
9198 symtab_size = symcount * (sizeof (asymbol *));
9199 if (symcount == 0)
9200 symtab_size = sizeof (asymbol *);
9201 else if (!bfd_write_p (abfd))
9203 ufile_ptr filesize = bfd_get_file_size (abfd);
9205 if (filesize != 0 && (unsigned long) symtab_size > filesize)
9207 bfd_set_error (bfd_error_file_truncated);
9208 return -1;
9212 return symtab_size;
9215 long
9216 _bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
9218 if (asect->reloc_count != 0 && !bfd_write_p (abfd))
9220 /* Sanity check reloc section size. */
9221 ufile_ptr filesize = bfd_get_file_size (abfd);
9223 if (filesize != 0)
9225 struct bfd_elf_section_data *d = elf_section_data (asect);
9226 bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
9227 bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
9229 if (rel_size + rela_size > filesize
9230 || rel_size + rela_size < rel_size)
9232 bfd_set_error (bfd_error_file_truncated);
9233 return -1;
9238 #if SIZEOF_LONG == SIZEOF_INT
9239 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
9241 bfd_set_error (bfd_error_file_too_big);
9242 return -1;
9244 #endif
9245 return (asect->reloc_count + 1L) * sizeof (arelent *);
9248 /* Canonicalize the relocs. */
9250 long
9251 _bfd_elf_canonicalize_reloc (bfd *abfd,
9252 sec_ptr section,
9253 arelent **relptr,
9254 asymbol **symbols)
9256 arelent *tblptr;
9257 unsigned int i;
9258 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9260 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
9261 return -1;
9263 tblptr = section->relocation;
9264 for (i = 0; i < section->reloc_count; i++)
9265 *relptr++ = tblptr++;
9267 *relptr = NULL;
9269 return section->reloc_count;
9272 long
9273 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
9275 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9276 long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
9278 if (symcount >= 0)
9279 abfd->symcount = symcount;
9280 return symcount;
9283 long
9284 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
9285 asymbol **allocation)
9287 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9288 long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
9290 if (symcount >= 0)
9291 abfd->dynsymcount = symcount;
9292 return symcount;
9295 /* Return the size required for the dynamic reloc entries. Any loadable
9296 section that was actually installed in the BFD, and has type SHT_REL
9297 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
9298 dynamic reloc section. */
9300 long
9301 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
9303 bfd_size_type count, ext_rel_size;
9304 asection *s;
9306 if (elf_dynsymtab (abfd) == 0)
9308 bfd_set_error (bfd_error_invalid_operation);
9309 return -1;
9312 count = 1;
9313 ext_rel_size = 0;
9314 for (s = abfd->sections; s != NULL; s = s->next)
9315 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9316 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9317 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9318 && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9320 ext_rel_size += elf_section_data (s)->this_hdr.sh_size;
9321 if (ext_rel_size < elf_section_data (s)->this_hdr.sh_size)
9323 bfd_set_error (bfd_error_file_truncated);
9324 return -1;
9326 count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9327 if (count > LONG_MAX / sizeof (arelent *))
9329 bfd_set_error (bfd_error_file_too_big);
9330 return -1;
9333 if (count > 1 && !bfd_write_p (abfd))
9335 /* Sanity check reloc section sizes. */
9336 ufile_ptr filesize = bfd_get_file_size (abfd);
9337 if (filesize != 0 && ext_rel_size > filesize)
9339 bfd_set_error (bfd_error_file_truncated);
9340 return -1;
9343 return count * sizeof (arelent *);
9346 /* Canonicalize the dynamic relocation entries. Note that we return the
9347 dynamic relocations as a single block, although they are actually
9348 associated with particular sections; the interface, which was
9349 designed for SunOS style shared libraries, expects that there is only
9350 one set of dynamic relocs. Any loadable section that was actually
9351 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
9352 dynamic symbol table, is considered to be a dynamic reloc section. */
9354 long
9355 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
9356 arelent **storage,
9357 asymbol **syms)
9359 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
9360 asection *s;
9361 long ret;
9363 if (elf_dynsymtab (abfd) == 0)
9365 bfd_set_error (bfd_error_invalid_operation);
9366 return -1;
9369 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
9370 ret = 0;
9371 for (s = abfd->sections; s != NULL; s = s->next)
9373 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9374 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9375 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9376 && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9378 arelent *p;
9379 long count, i;
9381 if (! (*slurp_relocs) (abfd, s, syms, true))
9382 return -1;
9383 count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9384 p = s->relocation;
9385 for (i = 0; i < count; i++)
9386 *storage++ = p++;
9387 ret += count;
9391 *storage = NULL;
9393 return ret;
9396 /* Read in the version information. */
9398 bool
9399 _bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
9401 bfd_byte *contents = NULL;
9402 bool free_contents = false;
9403 unsigned int freeidx = 0;
9404 size_t amt;
9406 if (elf_dynverref (abfd) != 0 || elf_tdata (abfd)->dt_verneed != NULL)
9408 Elf_Internal_Shdr *hdr;
9409 Elf_External_Verneed *everneed;
9410 Elf_Internal_Verneed *iverneed;
9411 unsigned int i;
9412 bfd_byte *contents_end;
9413 size_t verneed_count;
9414 size_t verneed_size;
9416 if (elf_tdata (abfd)->dt_verneed != NULL)
9418 hdr = NULL;
9419 contents = elf_tdata (abfd)->dt_verneed;
9420 verneed_count = elf_tdata (abfd)->dt_verneed_count;
9421 verneed_size = verneed_count * sizeof (Elf_External_Verneed);
9423 else
9425 hdr = &elf_tdata (abfd)->dynverref_hdr;
9427 if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
9429 error_return_bad_verref:
9430 _bfd_error_handler
9431 (_("%pB: .gnu.version_r invalid entry"), abfd);
9432 bfd_set_error (bfd_error_bad_value);
9433 error_return_verref:
9434 elf_tdata (abfd)->verref = NULL;
9435 elf_tdata (abfd)->cverrefs = 0;
9436 goto error_return;
9439 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9440 goto error_return_verref;
9441 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
9442 if (contents == NULL)
9443 goto error_return_verref;
9445 free_contents = true;
9446 verneed_size = hdr->sh_size;
9447 verneed_count = hdr->sh_info;
9450 if (_bfd_mul_overflow (verneed_count,
9451 sizeof (Elf_Internal_Verneed), &amt))
9453 bfd_set_error (bfd_error_file_too_big);
9454 goto error_return_verref;
9456 if (amt == 0)
9457 goto error_return_verref;
9458 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
9459 if (elf_tdata (abfd)->verref == NULL)
9460 goto error_return_verref;
9462 BFD_ASSERT (sizeof (Elf_External_Verneed)
9463 == sizeof (Elf_External_Vernaux));
9464 contents_end = (contents + verneed_size
9465 - sizeof (Elf_External_Verneed));
9466 everneed = (Elf_External_Verneed *) contents;
9467 iverneed = elf_tdata (abfd)->verref;
9468 for (i = 0; i < verneed_count; i++, iverneed++)
9470 Elf_External_Vernaux *evernaux;
9471 Elf_Internal_Vernaux *ivernaux;
9472 unsigned int j;
9474 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
9476 iverneed->vn_bfd = abfd;
9478 if (elf_use_dt_symtab_p (abfd))
9480 if (iverneed->vn_file < elf_tdata (abfd)->dt_strsz)
9481 iverneed->vn_filename
9482 = elf_tdata (abfd)->dt_strtab + iverneed->vn_file;
9483 else
9484 iverneed->vn_filename = NULL;
9486 else if (hdr == NULL)
9487 goto error_return_bad_verref;
9488 else
9489 iverneed->vn_filename
9490 = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9491 iverneed->vn_file);
9492 if (iverneed->vn_filename == NULL)
9493 goto error_return_bad_verref;
9495 if (iverneed->vn_cnt == 0)
9496 iverneed->vn_auxptr = NULL;
9497 else
9499 if (_bfd_mul_overflow (iverneed->vn_cnt,
9500 sizeof (Elf_Internal_Vernaux), &amt))
9502 bfd_set_error (bfd_error_file_too_big);
9503 goto error_return_verref;
9505 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
9506 bfd_alloc (abfd, amt);
9507 if (iverneed->vn_auxptr == NULL)
9508 goto error_return_verref;
9511 if (iverneed->vn_aux
9512 > (size_t) (contents_end - (bfd_byte *) everneed))
9513 goto error_return_bad_verref;
9515 evernaux = ((Elf_External_Vernaux *)
9516 ((bfd_byte *) everneed + iverneed->vn_aux));
9517 ivernaux = iverneed->vn_auxptr;
9518 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
9520 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
9522 if (elf_use_dt_symtab_p (abfd))
9524 if (ivernaux->vna_name < elf_tdata (abfd)->dt_strsz)
9525 ivernaux->vna_nodename
9526 = elf_tdata (abfd)->dt_strtab + ivernaux->vna_name;
9527 else
9528 ivernaux->vna_nodename = NULL;
9530 else if (hdr == NULL)
9531 goto error_return_bad_verref;
9532 else
9533 ivernaux->vna_nodename
9534 = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9535 ivernaux->vna_name);
9536 if (ivernaux->vna_nodename == NULL)
9537 goto error_return_bad_verref;
9539 if (ivernaux->vna_other > freeidx)
9540 freeidx = ivernaux->vna_other;
9542 ivernaux->vna_nextptr = NULL;
9543 if (ivernaux->vna_next == 0)
9545 iverneed->vn_cnt = j + 1;
9546 break;
9548 if (j + 1 < iverneed->vn_cnt)
9549 ivernaux->vna_nextptr = ivernaux + 1;
9551 if (ivernaux->vna_next
9552 > (size_t) (contents_end - (bfd_byte *) evernaux))
9553 goto error_return_bad_verref;
9555 evernaux = ((Elf_External_Vernaux *)
9556 ((bfd_byte *) evernaux + ivernaux->vna_next));
9559 iverneed->vn_nextref = NULL;
9560 if (iverneed->vn_next == 0)
9561 break;
9562 if (hdr != NULL && (i + 1 < hdr->sh_info))
9563 iverneed->vn_nextref = iverneed + 1;
9565 if (iverneed->vn_next
9566 > (size_t) (contents_end - (bfd_byte *) everneed))
9567 goto error_return_bad_verref;
9569 everneed = ((Elf_External_Verneed *)
9570 ((bfd_byte *) everneed + iverneed->vn_next));
9572 elf_tdata (abfd)->cverrefs = i;
9574 if (elf_tdata (abfd)->dt_verneed == NULL)
9575 free (contents);
9576 contents = NULL;
9579 if (elf_dynverdef (abfd) != 0 || elf_tdata (abfd)->dt_verdef != NULL)
9581 Elf_Internal_Shdr *hdr;
9582 Elf_External_Verdef *everdef;
9583 Elf_Internal_Verdef *iverdef;
9584 Elf_Internal_Verdef *iverdefarr;
9585 Elf_Internal_Verdef iverdefmem;
9586 unsigned int i;
9587 unsigned int maxidx;
9588 bfd_byte *contents_end_def, *contents_end_aux;
9589 size_t verdef_count;
9590 size_t verdef_size;
9592 if (elf_tdata (abfd)->dt_verdef != NULL)
9594 hdr = NULL;
9595 contents = elf_tdata (abfd)->dt_verdef;
9596 verdef_count = elf_tdata (abfd)->dt_verdef_count;
9597 verdef_size = verdef_count * sizeof (Elf_External_Verdef);
9599 else
9601 hdr = &elf_tdata (abfd)->dynverdef_hdr;
9603 if (hdr->sh_size < sizeof (Elf_External_Verdef))
9605 error_return_bad_verdef:
9606 _bfd_error_handler
9607 (_("%pB: .gnu.version_d invalid entry"), abfd);
9608 bfd_set_error (bfd_error_bad_value);
9609 error_return_verdef:
9610 elf_tdata (abfd)->verdef = NULL;
9611 elf_tdata (abfd)->cverdefs = 0;
9612 goto error_return;
9615 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9616 goto error_return_verdef;
9617 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
9618 if (contents == NULL)
9619 goto error_return_verdef;
9621 BFD_ASSERT (sizeof (Elf_External_Verdef)
9622 >= sizeof (Elf_External_Verdaux));
9624 verdef_count = hdr->sh_info;
9625 verdef_size = hdr->sh_size;
9628 contents_end_def = (contents + verdef_size
9629 - sizeof (Elf_External_Verdef));
9630 contents_end_aux = (contents + verdef_size
9631 - sizeof (Elf_External_Verdaux));
9633 /* We know the number of entries in the section but not the maximum
9634 index. Therefore we have to run through all entries and find
9635 the maximum. */
9636 everdef = (Elf_External_Verdef *) contents;
9637 maxidx = 0;
9638 for (i = 0; i < verdef_count; ++i)
9640 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9642 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
9643 goto error_return_bad_verdef;
9644 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
9645 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
9647 if (iverdefmem.vd_next == 0)
9648 break;
9650 if (iverdefmem.vd_next
9651 > (size_t) (contents_end_def - (bfd_byte *) everdef))
9652 goto error_return_bad_verdef;
9654 everdef = ((Elf_External_Verdef *)
9655 ((bfd_byte *) everdef + iverdefmem.vd_next));
9658 if (default_imported_symver)
9660 if (freeidx > maxidx)
9661 maxidx = ++freeidx;
9662 else
9663 freeidx = ++maxidx;
9665 if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
9667 bfd_set_error (bfd_error_file_too_big);
9668 goto error_return_verdef;
9671 if (amt == 0)
9672 goto error_return_verdef;
9673 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9674 if (elf_tdata (abfd)->verdef == NULL)
9675 goto error_return_verdef;
9677 elf_tdata (abfd)->cverdefs = maxidx;
9679 everdef = (Elf_External_Verdef *) contents;
9680 iverdefarr = elf_tdata (abfd)->verdef;
9681 for (i = 0; i < verdef_count; ++i)
9683 Elf_External_Verdaux *everdaux;
9684 Elf_Internal_Verdaux *iverdaux;
9685 unsigned int j;
9687 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9689 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
9690 goto error_return_bad_verdef;
9692 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
9693 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
9695 iverdef->vd_bfd = abfd;
9697 if (iverdef->vd_cnt == 0)
9698 iverdef->vd_auxptr = NULL;
9699 else
9701 if (_bfd_mul_overflow (iverdef->vd_cnt,
9702 sizeof (Elf_Internal_Verdaux), &amt))
9704 bfd_set_error (bfd_error_file_too_big);
9705 goto error_return_verdef;
9707 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
9708 bfd_alloc (abfd, amt);
9709 if (iverdef->vd_auxptr == NULL)
9710 goto error_return_verdef;
9713 if (iverdef->vd_aux
9714 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
9715 goto error_return_bad_verdef;
9717 everdaux = ((Elf_External_Verdaux *)
9718 ((bfd_byte *) everdef + iverdef->vd_aux));
9719 iverdaux = iverdef->vd_auxptr;
9720 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
9722 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
9724 if (elf_use_dt_symtab_p (abfd))
9726 if (iverdaux->vda_name < elf_tdata (abfd)->dt_strsz)
9727 iverdaux->vda_nodename
9728 = elf_tdata (abfd)->dt_strtab + iverdaux->vda_name;
9729 else
9730 iverdaux->vda_nodename = NULL;
9732 else
9733 iverdaux->vda_nodename
9734 = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9735 iverdaux->vda_name);
9736 if (iverdaux->vda_nodename == NULL)
9737 goto error_return_bad_verdef;
9739 iverdaux->vda_nextptr = NULL;
9740 if (iverdaux->vda_next == 0)
9742 iverdef->vd_cnt = j + 1;
9743 break;
9745 if (j + 1 < iverdef->vd_cnt)
9746 iverdaux->vda_nextptr = iverdaux + 1;
9748 if (iverdaux->vda_next
9749 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
9750 goto error_return_bad_verdef;
9752 everdaux = ((Elf_External_Verdaux *)
9753 ((bfd_byte *) everdaux + iverdaux->vda_next));
9756 iverdef->vd_nodename = NULL;
9757 if (iverdef->vd_cnt)
9758 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
9760 iverdef->vd_nextdef = NULL;
9761 if (iverdef->vd_next == 0)
9762 break;
9763 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
9764 iverdef->vd_nextdef = iverdef + 1;
9766 everdef = ((Elf_External_Verdef *)
9767 ((bfd_byte *) everdef + iverdef->vd_next));
9770 if (elf_tdata (abfd)->dt_verdef == NULL)
9771 free (contents);
9772 contents = NULL;
9774 else if (default_imported_symver)
9776 if (freeidx < 3)
9777 freeidx = 3;
9778 else
9779 freeidx++;
9781 if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
9783 bfd_set_error (bfd_error_file_too_big);
9784 goto error_return;
9786 if (amt == 0)
9787 goto error_return;
9788 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9789 if (elf_tdata (abfd)->verdef == NULL)
9790 goto error_return;
9792 elf_tdata (abfd)->cverdefs = freeidx;
9795 /* Create a default version based on the soname. */
9796 if (default_imported_symver)
9798 Elf_Internal_Verdef *iverdef;
9799 Elf_Internal_Verdaux *iverdaux;
9801 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
9803 iverdef->vd_version = VER_DEF_CURRENT;
9804 iverdef->vd_flags = 0;
9805 iverdef->vd_ndx = freeidx;
9806 iverdef->vd_cnt = 1;
9808 iverdef->vd_bfd = abfd;
9810 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9811 if (iverdef->vd_nodename == NULL)
9812 goto error_return_verdef;
9813 iverdef->vd_nextdef = NULL;
9814 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
9815 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
9816 if (iverdef->vd_auxptr == NULL)
9817 goto error_return_verdef;
9819 iverdaux = iverdef->vd_auxptr;
9820 iverdaux->vda_nodename = iverdef->vd_nodename;
9823 return true;
9825 error_return:
9826 if (free_contents)
9827 free (contents);
9828 return false;
9831 asymbol *
9832 _bfd_elf_make_empty_symbol (bfd *abfd)
9834 elf_symbol_type *newsym;
9836 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
9837 if (!newsym)
9838 return NULL;
9839 newsym->symbol.the_bfd = abfd;
9840 return &newsym->symbol;
9843 void
9844 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9845 asymbol *symbol,
9846 symbol_info *ret)
9848 bfd_symbol_info (symbol, ret);
9851 /* Return whether a symbol name implies a local symbol. Most targets
9852 use this function for the is_local_label_name entry point, but some
9853 override it. */
9855 bool
9856 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9857 const char *name)
9859 /* Normal local symbols start with ``.L''. */
9860 if (name[0] == '.' && name[1] == 'L')
9861 return true;
9863 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9864 DWARF debugging symbols starting with ``..''. */
9865 if (name[0] == '.' && name[1] == '.')
9866 return true;
9868 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9869 emitting DWARF debugging output. I suspect this is actually a
9870 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9871 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9872 underscore to be emitted on some ELF targets). For ease of use,
9873 we treat such symbols as local. */
9874 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
9875 return true;
9877 /* Treat assembler generated fake symbols, dollar local labels and
9878 forward-backward labels (aka local labels) as locals.
9879 These labels have the form:
9881 L0^A.* (fake symbols)
9883 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9885 Versions which start with .L will have already been matched above,
9886 so we only need to match the rest. */
9887 if (name[0] == 'L' && ISDIGIT (name[1]))
9889 bool ret = false;
9890 const char * p;
9891 char c;
9893 for (p = name + 2; (c = *p); p++)
9895 if (c == 1 || c == 2)
9897 if (c == 1 && p == name + 2)
9898 /* A fake symbol. */
9899 return true;
9901 /* FIXME: We are being paranoid here and treating symbols like
9902 L0^Bfoo as if there were non-local, on the grounds that the
9903 assembler will never generate them. But can any symbol
9904 containing an ASCII value in the range 1-31 ever be anything
9905 other than some kind of local ? */
9906 ret = true;
9909 if (! ISDIGIT (c))
9911 ret = false;
9912 break;
9915 return ret;
9918 return false;
9921 alent *
9922 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9923 asymbol *symbol ATTRIBUTE_UNUSED)
9925 abort ();
9926 return NULL;
9929 bool
9930 _bfd_elf_set_arch_mach (bfd *abfd,
9931 enum bfd_architecture arch,
9932 unsigned long machine)
9934 /* If this isn't the right architecture for this backend, and this
9935 isn't the generic backend, fail. */
9936 if (arch != get_elf_backend_data (abfd)->arch
9937 && arch != bfd_arch_unknown
9938 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
9939 return false;
9941 return bfd_default_set_arch_mach (abfd, arch, machine);
9944 /* Find the nearest line to a particular section and offset,
9945 for error reporting. */
9947 bool
9948 _bfd_elf_find_nearest_line (bfd *abfd,
9949 asymbol **symbols,
9950 asection *section,
9951 bfd_vma offset,
9952 const char **filename_ptr,
9953 const char **functionname_ptr,
9954 unsigned int *line_ptr,
9955 unsigned int *discriminator_ptr)
9957 return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
9958 offset, filename_ptr,
9959 functionname_ptr, line_ptr,
9960 discriminator_ptr);
9963 /* Find the nearest line to a particular section and offset,
9964 for error reporting. ALT_BFD representing a .gnu_debugaltlink file
9965 can be optionally specified. */
9967 bool
9968 _bfd_elf_find_nearest_line_with_alt (bfd *abfd,
9969 const char *alt_filename,
9970 asymbol **symbols,
9971 asection *section,
9972 bfd_vma offset,
9973 const char **filename_ptr,
9974 const char **functionname_ptr,
9975 unsigned int *line_ptr,
9976 unsigned int *discriminator_ptr)
9978 bool found;
9980 if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
9981 section, offset, filename_ptr,
9982 functionname_ptr, line_ptr,
9983 discriminator_ptr,
9984 dwarf_debug_sections,
9985 &elf_tdata (abfd)->dwarf2_find_line_info))
9986 return true;
9988 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9989 filename_ptr, functionname_ptr, line_ptr))
9991 if (!*functionname_ptr)
9992 _bfd_elf_find_function (abfd, symbols, section, offset,
9993 *filename_ptr ? NULL : filename_ptr,
9994 functionname_ptr);
9995 return true;
9998 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
9999 &found, filename_ptr,
10000 functionname_ptr, line_ptr,
10001 &elf_tdata (abfd)->line_info))
10002 return false;
10003 if (found && (*functionname_ptr || *line_ptr))
10004 return true;
10006 if (symbols == NULL)
10007 return false;
10009 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
10010 filename_ptr, functionname_ptr))
10011 return false;
10013 *line_ptr = 0;
10014 return true;
10017 /* Find the line for a symbol. */
10019 bool
10020 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
10021 const char **filename_ptr, unsigned int *line_ptr)
10023 struct elf_obj_tdata *tdata = elf_tdata (abfd);
10024 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
10025 filename_ptr, NULL, line_ptr, NULL,
10026 dwarf_debug_sections,
10027 &tdata->dwarf2_find_line_info);
10030 /* After a call to bfd_find_nearest_line, successive calls to
10031 bfd_find_inliner_info can be used to get source information about
10032 each level of function inlining that terminated at the address
10033 passed to bfd_find_nearest_line. Currently this is only supported
10034 for DWARF2 with appropriate DWARF3 extensions. */
10036 bool
10037 _bfd_elf_find_inliner_info (bfd *abfd,
10038 const char **filename_ptr,
10039 const char **functionname_ptr,
10040 unsigned int *line_ptr)
10042 struct elf_obj_tdata *tdata = elf_tdata (abfd);
10043 return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
10044 functionname_ptr, line_ptr,
10045 &tdata->dwarf2_find_line_info);
10049 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
10051 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10052 int ret = bed->s->sizeof_ehdr;
10054 if (!bfd_link_relocatable (info))
10056 bfd_size_type phdr_size = elf_program_header_size (abfd);
10058 if (phdr_size == (bfd_size_type) -1)
10060 struct elf_segment_map *m;
10062 phdr_size = 0;
10063 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
10064 phdr_size += bed->s->sizeof_phdr;
10066 if (phdr_size == 0)
10067 phdr_size = get_program_header_size (abfd, info);
10070 elf_program_header_size (abfd) = phdr_size;
10071 ret += phdr_size;
10074 return ret;
10077 bool
10078 _bfd_elf_set_section_contents (bfd *abfd,
10079 sec_ptr section,
10080 const void *location,
10081 file_ptr offset,
10082 bfd_size_type count)
10084 Elf_Internal_Shdr *hdr;
10086 if (! abfd->output_has_begun
10087 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
10088 return false;
10090 if (!count)
10091 return true;
10093 hdr = &elf_section_data (section)->this_hdr;
10094 if (hdr->sh_offset == (file_ptr) -1)
10096 unsigned char *contents;
10098 if (bfd_section_is_ctf (section))
10099 /* Nothing to do with this section: the contents are generated
10100 later. */
10101 return true;
10103 if ((offset + count) > hdr->sh_size)
10105 _bfd_error_handler
10106 (_("%pB:%pA: error: attempting to write"
10107 " over the end of the section"),
10108 abfd, section);
10110 bfd_set_error (bfd_error_invalid_operation);
10111 return false;
10114 contents = hdr->contents;
10115 if (contents == NULL)
10117 _bfd_error_handler
10118 (_("%pB:%pA: error: attempting to write"
10119 " section into an empty buffer"),
10120 abfd, section);
10122 bfd_set_error (bfd_error_invalid_operation);
10123 return false;
10126 memcpy (contents + offset, location, count);
10127 return true;
10130 return _bfd_generic_set_section_contents (abfd, section,
10131 location, offset, count);
10134 bool
10135 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
10136 arelent *cache_ptr ATTRIBUTE_UNUSED,
10137 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
10139 abort ();
10140 return false;
10143 /* Try to convert a non-ELF reloc into an ELF one. */
10145 bool
10146 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
10148 /* Check whether we really have an ELF howto. */
10150 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
10152 bfd_reloc_code_real_type code;
10153 reloc_howto_type *howto;
10155 /* Alien reloc: Try to determine its type to replace it with an
10156 equivalent ELF reloc. */
10158 if (areloc->howto->pc_relative)
10160 switch (areloc->howto->bitsize)
10162 case 8:
10163 code = BFD_RELOC_8_PCREL;
10164 break;
10165 case 12:
10166 code = BFD_RELOC_12_PCREL;
10167 break;
10168 case 16:
10169 code = BFD_RELOC_16_PCREL;
10170 break;
10171 case 24:
10172 code = BFD_RELOC_24_PCREL;
10173 break;
10174 case 32:
10175 code = BFD_RELOC_32_PCREL;
10176 break;
10177 case 64:
10178 code = BFD_RELOC_64_PCREL;
10179 break;
10180 default:
10181 goto fail;
10184 howto = bfd_reloc_type_lookup (abfd, code);
10186 if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
10188 if (howto->pcrel_offset)
10189 areloc->addend += areloc->address;
10190 else
10191 areloc->addend -= areloc->address; /* addend is unsigned!! */
10194 else
10196 switch (areloc->howto->bitsize)
10198 case 8:
10199 code = BFD_RELOC_8;
10200 break;
10201 case 14:
10202 code = BFD_RELOC_14;
10203 break;
10204 case 16:
10205 code = BFD_RELOC_16;
10206 break;
10207 case 26:
10208 code = BFD_RELOC_26;
10209 break;
10210 case 32:
10211 code = BFD_RELOC_32;
10212 break;
10213 case 64:
10214 code = BFD_RELOC_64;
10215 break;
10216 default:
10217 goto fail;
10220 howto = bfd_reloc_type_lookup (abfd, code);
10223 if (howto)
10224 areloc->howto = howto;
10225 else
10226 goto fail;
10229 return true;
10231 fail:
10232 /* xgettext:c-format */
10233 _bfd_error_handler (_("%pB: %s unsupported"),
10234 abfd, areloc->howto->name);
10235 bfd_set_error (bfd_error_sorry);
10236 return false;
10239 bool
10240 _bfd_elf_free_cached_info (bfd *abfd)
10242 struct elf_obj_tdata *tdata;
10244 if ((bfd_get_format (abfd) == bfd_object
10245 || bfd_get_format (abfd) == bfd_core)
10246 && (tdata = elf_tdata (abfd)) != NULL)
10248 if (tdata->o != NULL && elf_shstrtab (abfd) != NULL)
10249 _bfd_elf_strtab_free (elf_shstrtab (abfd));
10250 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
10251 _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info);
10252 _bfd_stab_cleanup (abfd, &tdata->line_info);
10255 return _bfd_generic_bfd_free_cached_info (abfd);
10258 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
10259 in the relocation's offset. Thus we cannot allow any sort of sanity
10260 range-checking to interfere. There is nothing else to do in processing
10261 this reloc. */
10263 bfd_reloc_status_type
10264 _bfd_elf_rel_vtable_reloc_fn
10265 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
10266 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
10267 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
10268 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
10270 return bfd_reloc_ok;
10273 /* Elf core file support. Much of this only works on native
10274 toolchains, since we rely on knowing the
10275 machine-dependent procfs structure in order to pick
10276 out details about the corefile. */
10278 #ifdef HAVE_SYS_PROCFS_H
10279 # include <sys/procfs.h>
10280 #endif
10282 /* Return a PID that identifies a "thread" for threaded cores, or the
10283 PID of the main process for non-threaded cores. */
10285 static int
10286 elfcore_make_pid (bfd *abfd)
10288 int pid;
10290 pid = elf_tdata (abfd)->core->lwpid;
10291 if (pid == 0)
10292 pid = elf_tdata (abfd)->core->pid;
10294 return pid;
10297 /* If there isn't a section called NAME, make one, using data from
10298 SECT. Note, this function will generate a reference to NAME, so
10299 you shouldn't deallocate or overwrite it. */
10301 static bool
10302 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
10304 asection *sect2;
10306 if (bfd_get_section_by_name (abfd, name) != NULL)
10307 return true;
10309 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
10310 if (sect2 == NULL)
10311 return false;
10313 sect2->size = sect->size;
10314 sect2->filepos = sect->filepos;
10315 sect2->alignment_power = sect->alignment_power;
10316 return true;
10319 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
10320 actually creates up to two pseudosections:
10321 - For the single-threaded case, a section named NAME, unless
10322 such a section already exists.
10323 - For the multi-threaded case, a section named "NAME/PID", where
10324 PID is elfcore_make_pid (abfd).
10325 Both pseudosections have identical contents. */
10326 bool
10327 _bfd_elfcore_make_pseudosection (bfd *abfd,
10328 char *name,
10329 size_t size,
10330 ufile_ptr filepos)
10332 char buf[100];
10333 char *threaded_name;
10334 size_t len;
10335 asection *sect;
10337 /* Build the section name. */
10339 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
10340 len = strlen (buf) + 1;
10341 threaded_name = (char *) bfd_alloc (abfd, len);
10342 if (threaded_name == NULL)
10343 return false;
10344 memcpy (threaded_name, buf, len);
10346 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
10347 SEC_HAS_CONTENTS);
10348 if (sect == NULL)
10349 return false;
10350 sect->size = size;
10351 sect->filepos = filepos;
10352 sect->alignment_power = 2;
10354 return elfcore_maybe_make_sect (abfd, name, sect);
10357 static bool
10358 elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
10359 size_t offs)
10361 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10362 SEC_HAS_CONTENTS);
10364 if (sect == NULL)
10365 return false;
10367 sect->size = note->descsz - offs;
10368 sect->filepos = note->descpos + offs;
10369 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10371 return true;
10374 /* prstatus_t exists on:
10375 solaris 2.5+
10376 linux 2.[01] + glibc
10377 unixware 4.2
10380 #if defined (HAVE_PRSTATUS_T)
10382 static bool
10383 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
10385 size_t size;
10386 int offset;
10388 if (note->descsz == sizeof (prstatus_t))
10390 prstatus_t prstat;
10392 size = sizeof (prstat.pr_reg);
10393 offset = offsetof (prstatus_t, pr_reg);
10394 memcpy (&prstat, note->descdata, sizeof (prstat));
10396 /* Do not overwrite the core signal if it
10397 has already been set by another thread. */
10398 if (elf_tdata (abfd)->core->signal == 0)
10399 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10400 if (elf_tdata (abfd)->core->pid == 0)
10401 elf_tdata (abfd)->core->pid = prstat.pr_pid;
10403 /* pr_who exists on:
10404 solaris 2.5+
10405 unixware 4.2
10406 pr_who doesn't exist on:
10407 linux 2.[01]
10409 #if defined (HAVE_PRSTATUS_T_PR_WHO)
10410 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10411 #else
10412 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10413 #endif
10415 #if defined (HAVE_PRSTATUS32_T)
10416 else if (note->descsz == sizeof (prstatus32_t))
10418 /* 64-bit host, 32-bit corefile */
10419 prstatus32_t prstat;
10421 size = sizeof (prstat.pr_reg);
10422 offset = offsetof (prstatus32_t, pr_reg);
10423 memcpy (&prstat, note->descdata, sizeof (prstat));
10425 /* Do not overwrite the core signal if it
10426 has already been set by another thread. */
10427 if (elf_tdata (abfd)->core->signal == 0)
10428 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10429 if (elf_tdata (abfd)->core->pid == 0)
10430 elf_tdata (abfd)->core->pid = prstat.pr_pid;
10432 /* pr_who exists on:
10433 solaris 2.5+
10434 unixware 4.2
10435 pr_who doesn't exist on:
10436 linux 2.[01]
10438 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
10439 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10440 #else
10441 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10442 #endif
10444 #endif /* HAVE_PRSTATUS32_T */
10445 else
10447 /* Fail - we don't know how to handle any other
10448 note size (ie. data object type). */
10449 return true;
10452 /* Make a ".reg/999" section and a ".reg" section. */
10453 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10454 size, note->descpos + offset);
10456 #endif /* defined (HAVE_PRSTATUS_T) */
10458 /* Create a pseudosection containing the exact contents of NOTE. */
10459 static bool
10460 elfcore_make_note_pseudosection (bfd *abfd,
10461 char *name,
10462 Elf_Internal_Note *note)
10464 return _bfd_elfcore_make_pseudosection (abfd, name,
10465 note->descsz, note->descpos);
10468 /* There isn't a consistent prfpregset_t across platforms,
10469 but it doesn't matter, because we don't have to pick this
10470 data structure apart. */
10472 static bool
10473 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
10475 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10478 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
10479 type of NT_PRXFPREG. Just include the whole note's contents
10480 literally. */
10482 static bool
10483 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
10485 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
10488 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
10489 with a note type of NT_X86_XSTATE. Just include the whole note's
10490 contents literally. */
10492 static bool
10493 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
10495 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
10498 static bool
10499 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
10501 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
10504 static bool
10505 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
10507 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
10510 static bool
10511 elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
10513 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
10516 static bool
10517 elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
10519 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
10522 static bool
10523 elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
10525 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
10528 static bool
10529 elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
10531 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
10534 static bool
10535 elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
10537 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
10540 static bool
10541 elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
10543 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
10546 static bool
10547 elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
10549 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
10552 static bool
10553 elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
10555 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
10558 static bool
10559 elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
10561 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
10564 static bool
10565 elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
10567 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
10570 static bool
10571 elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
10573 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
10576 static bool
10577 elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
10579 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
10582 static bool
10583 elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
10585 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
10588 static bool
10589 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
10591 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
10594 static bool
10595 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
10597 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
10600 static bool
10601 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
10603 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
10606 static bool
10607 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
10609 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
10612 static bool
10613 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
10615 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
10618 static bool
10619 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
10621 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
10624 static bool
10625 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
10627 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
10630 static bool
10631 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
10633 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
10636 static bool
10637 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
10639 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
10642 static bool
10643 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
10645 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
10648 static bool
10649 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
10651 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
10654 static bool
10655 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
10657 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
10660 static bool
10661 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
10663 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
10666 static bool
10667 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
10669 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
10672 static bool
10673 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
10675 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
10678 static bool
10679 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
10681 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
10684 static bool
10685 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
10687 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
10690 static bool
10691 elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
10693 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
10696 static bool
10697 elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
10699 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
10702 static bool
10703 elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
10705 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
10706 note);
10709 static bool
10710 elfcore_grok_aarch_ssve (bfd *abfd, Elf_Internal_Note *note)
10712 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-ssve", note);
10715 static bool
10716 elfcore_grok_aarch_za (bfd *abfd, Elf_Internal_Note *note)
10718 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-za", note);
10721 /* Convert NOTE into a bfd_section called ".reg-aarch-zt". Return TRUE if
10722 successful, otherwise return FALSE. */
10724 static bool
10725 elfcore_grok_aarch_zt (bfd *abfd, Elf_Internal_Note *note)
10727 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-zt", note);
10730 static bool
10731 elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
10733 return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
10736 /* Convert NOTE into a bfd_section called ".reg-riscv-csr". Return TRUE if
10737 successful otherwise, return FALSE. */
10739 static bool
10740 elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
10742 return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
10745 /* Convert NOTE into a bfd_section called ".gdb-tdesc". Return TRUE if
10746 successful otherwise, return FALSE. */
10748 static bool
10749 elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
10751 return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
10754 static bool
10755 elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
10757 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note);
10760 static bool
10761 elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
10763 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note);
10766 static bool
10767 elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
10769 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note);
10772 static bool
10773 elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
10775 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note);
10778 #if defined (HAVE_PRPSINFO_T)
10779 typedef prpsinfo_t elfcore_psinfo_t;
10780 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
10781 typedef prpsinfo32_t elfcore_psinfo32_t;
10782 #endif
10783 #endif
10785 #if defined (HAVE_PSINFO_T)
10786 typedef psinfo_t elfcore_psinfo_t;
10787 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
10788 typedef psinfo32_t elfcore_psinfo32_t;
10789 #endif
10790 #endif
10792 /* return a malloc'ed copy of a string at START which is at
10793 most MAX bytes long, possibly without a terminating '\0'.
10794 the copy will always have a terminating '\0'. */
10796 char *
10797 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
10799 char *dups;
10800 char *end = (char *) memchr (start, '\0', max);
10801 size_t len;
10803 if (end == NULL)
10804 len = max;
10805 else
10806 len = end - start;
10808 dups = (char *) bfd_alloc (abfd, len + 1);
10809 if (dups == NULL)
10810 return NULL;
10812 memcpy (dups, start, len);
10813 dups[len] = '\0';
10815 return dups;
10818 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10819 static bool
10820 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
10822 if (note->descsz == sizeof (elfcore_psinfo_t))
10824 elfcore_psinfo_t psinfo;
10826 memcpy (&psinfo, note->descdata, sizeof (psinfo));
10828 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10829 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10830 #endif
10831 elf_tdata (abfd)->core->program
10832 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10833 sizeof (psinfo.pr_fname));
10835 elf_tdata (abfd)->core->command
10836 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10837 sizeof (psinfo.pr_psargs));
10839 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10840 else if (note->descsz == sizeof (elfcore_psinfo32_t))
10842 /* 64-bit host, 32-bit corefile */
10843 elfcore_psinfo32_t psinfo;
10845 memcpy (&psinfo, note->descdata, sizeof (psinfo));
10847 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10848 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10849 #endif
10850 elf_tdata (abfd)->core->program
10851 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10852 sizeof (psinfo.pr_fname));
10854 elf_tdata (abfd)->core->command
10855 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10856 sizeof (psinfo.pr_psargs));
10858 #endif
10860 else
10862 /* Fail - we don't know how to handle any other
10863 note size (ie. data object type). */
10864 return true;
10867 /* Note that for some reason, a spurious space is tacked
10868 onto the end of the args in some (at least one anyway)
10869 implementations, so strip it off if it exists. */
10872 char *command = elf_tdata (abfd)->core->command;
10873 int n = strlen (command);
10875 if (0 < n && command[n - 1] == ' ')
10876 command[n - 1] = '\0';
10879 return true;
10881 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10883 #if defined (HAVE_PSTATUS_T)
10884 static bool
10885 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
10887 if (note->descsz == sizeof (pstatus_t)
10888 #if defined (HAVE_PXSTATUS_T)
10889 || note->descsz == sizeof (pxstatus_t)
10890 #endif
10893 pstatus_t pstat;
10895 memcpy (&pstat, note->descdata, sizeof (pstat));
10897 elf_tdata (abfd)->core->pid = pstat.pr_pid;
10899 #if defined (HAVE_PSTATUS32_T)
10900 else if (note->descsz == sizeof (pstatus32_t))
10902 /* 64-bit host, 32-bit corefile */
10903 pstatus32_t pstat;
10905 memcpy (&pstat, note->descdata, sizeof (pstat));
10907 elf_tdata (abfd)->core->pid = pstat.pr_pid;
10909 #endif
10910 /* Could grab some more details from the "representative"
10911 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10912 NT_LWPSTATUS note, presumably. */
10914 return true;
10916 #endif /* defined (HAVE_PSTATUS_T) */
10918 #if defined (HAVE_LWPSTATUS_T)
10919 static bool
10920 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
10922 lwpstatus_t lwpstat;
10923 char buf[100];
10924 char *name;
10925 size_t len;
10926 asection *sect;
10928 if (note->descsz != sizeof (lwpstat)
10929 #if defined (HAVE_LWPXSTATUS_T)
10930 && note->descsz != sizeof (lwpxstatus_t)
10931 #endif
10933 return true;
10935 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10937 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
10938 /* Do not overwrite the core signal if it has already been set by
10939 another thread. */
10940 if (elf_tdata (abfd)->core->signal == 0)
10941 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
10943 /* Make a ".reg/999" section. */
10945 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
10946 len = strlen (buf) + 1;
10947 name = bfd_alloc (abfd, len);
10948 if (name == NULL)
10949 return false;
10950 memcpy (name, buf, len);
10952 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10953 if (sect == NULL)
10954 return false;
10956 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10957 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
10958 sect->filepos = note->descpos
10959 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10960 #endif
10962 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10963 sect->size = sizeof (lwpstat.pr_reg);
10964 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10965 #endif
10967 sect->alignment_power = 2;
10969 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
10970 return false;
10972 /* Make a ".reg2/999" section */
10974 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
10975 len = strlen (buf) + 1;
10976 name = bfd_alloc (abfd, len);
10977 if (name == NULL)
10978 return false;
10979 memcpy (name, buf, len);
10981 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10982 if (sect == NULL)
10983 return false;
10985 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10986 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
10987 sect->filepos = note->descpos
10988 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
10989 #endif
10991 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
10992 sect->size = sizeof (lwpstat.pr_fpreg);
10993 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
10994 #endif
10996 sect->alignment_power = 2;
10998 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
11000 #endif /* defined (HAVE_LWPSTATUS_T) */
11002 /* These constants, and the structure offsets used below, are defined by
11003 Cygwin's core_dump.h */
11004 #define NOTE_INFO_PROCESS 1
11005 #define NOTE_INFO_THREAD 2
11006 #define NOTE_INFO_MODULE 3
11007 #define NOTE_INFO_MODULE64 4
11009 static bool
11010 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
11012 char buf[30];
11013 char *name;
11014 size_t len;
11015 unsigned int name_size;
11016 asection *sect;
11017 unsigned int type;
11018 int is_active_thread;
11019 bfd_vma base_addr;
11021 if (note->descsz < 4)
11022 return true;
11024 if (! startswith (note->namedata, "win32"))
11025 return true;
11027 type = bfd_get_32 (abfd, note->descdata);
11029 struct
11031 const char *type_name;
11032 unsigned long min_size;
11033 } size_check[] =
11035 { "NOTE_INFO_PROCESS", 12 },
11036 { "NOTE_INFO_THREAD", 12 },
11037 { "NOTE_INFO_MODULE", 12 },
11038 { "NOTE_INFO_MODULE64", 16 },
11041 if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
11042 return true;
11044 if (note->descsz < size_check[type - 1].min_size)
11046 _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
11047 " is too small"),
11048 abfd, size_check[type - 1].type_name, note->descsz);
11049 return true;
11052 switch (type)
11054 case NOTE_INFO_PROCESS:
11055 /* FIXME: need to add ->core->command. */
11056 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
11057 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
11058 break;
11060 case NOTE_INFO_THREAD:
11061 /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
11062 structure. */
11063 /* thread_info.tid */
11064 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
11066 len = strlen (buf) + 1;
11067 name = (char *) bfd_alloc (abfd, len);
11068 if (name == NULL)
11069 return false;
11071 memcpy (name, buf, len);
11073 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11074 if (sect == NULL)
11075 return false;
11077 /* sizeof (thread_info.thread_context) */
11078 sect->size = note->descsz - 12;
11079 /* offsetof (thread_info.thread_context) */
11080 sect->filepos = note->descpos + 12;
11081 sect->alignment_power = 2;
11083 /* thread_info.is_active_thread */
11084 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
11086 if (is_active_thread)
11087 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
11088 return false;
11089 break;
11091 case NOTE_INFO_MODULE:
11092 case NOTE_INFO_MODULE64:
11093 /* Make a ".module/xxxxxxxx" section. */
11094 if (type == NOTE_INFO_MODULE)
11096 /* module_info.base_address */
11097 base_addr = bfd_get_32 (abfd, note->descdata + 4);
11098 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
11099 /* module_info.module_name_size */
11100 name_size = bfd_get_32 (abfd, note->descdata + 8);
11102 else /* NOTE_INFO_MODULE64 */
11104 /* module_info.base_address */
11105 base_addr = bfd_get_64 (abfd, note->descdata + 4);
11106 sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
11107 /* module_info.module_name_size */
11108 name_size = bfd_get_32 (abfd, note->descdata + 12);
11111 len = strlen (buf) + 1;
11112 name = (char *) bfd_alloc (abfd, len);
11113 if (name == NULL)
11114 return false;
11116 memcpy (name, buf, len);
11118 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11120 if (sect == NULL)
11121 return false;
11123 if (note->descsz < 12 + name_size)
11125 _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
11126 " is too small to contain a name of size %u"),
11127 abfd, note->descsz, name_size);
11128 return true;
11131 sect->size = note->descsz;
11132 sect->filepos = note->descpos;
11133 sect->alignment_power = 2;
11134 break;
11136 default:
11137 return true;
11140 return true;
11143 static bool
11144 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
11146 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11148 switch (note->type)
11150 default:
11151 return true;
11153 case NT_PRSTATUS:
11154 if (bed->elf_backend_grok_prstatus)
11155 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
11156 return true;
11157 #if defined (HAVE_PRSTATUS_T)
11158 return elfcore_grok_prstatus (abfd, note);
11159 #else
11160 return true;
11161 #endif
11163 #if defined (HAVE_PSTATUS_T)
11164 case NT_PSTATUS:
11165 return elfcore_grok_pstatus (abfd, note);
11166 #endif
11168 #if defined (HAVE_LWPSTATUS_T)
11169 case NT_LWPSTATUS:
11170 return elfcore_grok_lwpstatus (abfd, note);
11171 #endif
11173 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
11174 return elfcore_grok_prfpreg (abfd, note);
11176 case NT_WIN32PSTATUS:
11177 return elfcore_grok_win32pstatus (abfd, note);
11179 case NT_PRXFPREG: /* Linux SSE extension */
11180 if (note->namesz == 6
11181 && strcmp (note->namedata, "LINUX") == 0)
11182 return elfcore_grok_prxfpreg (abfd, note);
11183 else
11184 return true;
11186 case NT_X86_XSTATE: /* Linux XSAVE extension */
11187 if (note->namesz == 6
11188 && strcmp (note->namedata, "LINUX") == 0)
11189 return elfcore_grok_xstatereg (abfd, note);
11190 else
11191 return true;
11193 case NT_PPC_VMX:
11194 if (note->namesz == 6
11195 && strcmp (note->namedata, "LINUX") == 0)
11196 return elfcore_grok_ppc_vmx (abfd, note);
11197 else
11198 return true;
11200 case NT_PPC_VSX:
11201 if (note->namesz == 6
11202 && strcmp (note->namedata, "LINUX") == 0)
11203 return elfcore_grok_ppc_vsx (abfd, note);
11204 else
11205 return true;
11207 case NT_PPC_TAR:
11208 if (note->namesz == 6
11209 && strcmp (note->namedata, "LINUX") == 0)
11210 return elfcore_grok_ppc_tar (abfd, note);
11211 else
11212 return true;
11214 case NT_PPC_PPR:
11215 if (note->namesz == 6
11216 && strcmp (note->namedata, "LINUX") == 0)
11217 return elfcore_grok_ppc_ppr (abfd, note);
11218 else
11219 return true;
11221 case NT_PPC_DSCR:
11222 if (note->namesz == 6
11223 && strcmp (note->namedata, "LINUX") == 0)
11224 return elfcore_grok_ppc_dscr (abfd, note);
11225 else
11226 return true;
11228 case NT_PPC_EBB:
11229 if (note->namesz == 6
11230 && strcmp (note->namedata, "LINUX") == 0)
11231 return elfcore_grok_ppc_ebb (abfd, note);
11232 else
11233 return true;
11235 case NT_PPC_PMU:
11236 if (note->namesz == 6
11237 && strcmp (note->namedata, "LINUX") == 0)
11238 return elfcore_grok_ppc_pmu (abfd, note);
11239 else
11240 return true;
11242 case NT_PPC_TM_CGPR:
11243 if (note->namesz == 6
11244 && strcmp (note->namedata, "LINUX") == 0)
11245 return elfcore_grok_ppc_tm_cgpr (abfd, note);
11246 else
11247 return true;
11249 case NT_PPC_TM_CFPR:
11250 if (note->namesz == 6
11251 && strcmp (note->namedata, "LINUX") == 0)
11252 return elfcore_grok_ppc_tm_cfpr (abfd, note);
11253 else
11254 return true;
11256 case NT_PPC_TM_CVMX:
11257 if (note->namesz == 6
11258 && strcmp (note->namedata, "LINUX") == 0)
11259 return elfcore_grok_ppc_tm_cvmx (abfd, note);
11260 else
11261 return true;
11263 case NT_PPC_TM_CVSX:
11264 if (note->namesz == 6
11265 && strcmp (note->namedata, "LINUX") == 0)
11266 return elfcore_grok_ppc_tm_cvsx (abfd, note);
11267 else
11268 return true;
11270 case NT_PPC_TM_SPR:
11271 if (note->namesz == 6
11272 && strcmp (note->namedata, "LINUX") == 0)
11273 return elfcore_grok_ppc_tm_spr (abfd, note);
11274 else
11275 return true;
11277 case NT_PPC_TM_CTAR:
11278 if (note->namesz == 6
11279 && strcmp (note->namedata, "LINUX") == 0)
11280 return elfcore_grok_ppc_tm_ctar (abfd, note);
11281 else
11282 return true;
11284 case NT_PPC_TM_CPPR:
11285 if (note->namesz == 6
11286 && strcmp (note->namedata, "LINUX") == 0)
11287 return elfcore_grok_ppc_tm_cppr (abfd, note);
11288 else
11289 return true;
11291 case NT_PPC_TM_CDSCR:
11292 if (note->namesz == 6
11293 && strcmp (note->namedata, "LINUX") == 0)
11294 return elfcore_grok_ppc_tm_cdscr (abfd, note);
11295 else
11296 return true;
11298 case NT_S390_HIGH_GPRS:
11299 if (note->namesz == 6
11300 && strcmp (note->namedata, "LINUX") == 0)
11301 return elfcore_grok_s390_high_gprs (abfd, note);
11302 else
11303 return true;
11305 case NT_S390_TIMER:
11306 if (note->namesz == 6
11307 && strcmp (note->namedata, "LINUX") == 0)
11308 return elfcore_grok_s390_timer (abfd, note);
11309 else
11310 return true;
11312 case NT_S390_TODCMP:
11313 if (note->namesz == 6
11314 && strcmp (note->namedata, "LINUX") == 0)
11315 return elfcore_grok_s390_todcmp (abfd, note);
11316 else
11317 return true;
11319 case NT_S390_TODPREG:
11320 if (note->namesz == 6
11321 && strcmp (note->namedata, "LINUX") == 0)
11322 return elfcore_grok_s390_todpreg (abfd, note);
11323 else
11324 return true;
11326 case NT_S390_CTRS:
11327 if (note->namesz == 6
11328 && strcmp (note->namedata, "LINUX") == 0)
11329 return elfcore_grok_s390_ctrs (abfd, note);
11330 else
11331 return true;
11333 case NT_S390_PREFIX:
11334 if (note->namesz == 6
11335 && strcmp (note->namedata, "LINUX") == 0)
11336 return elfcore_grok_s390_prefix (abfd, note);
11337 else
11338 return true;
11340 case NT_S390_LAST_BREAK:
11341 if (note->namesz == 6
11342 && strcmp (note->namedata, "LINUX") == 0)
11343 return elfcore_grok_s390_last_break (abfd, note);
11344 else
11345 return true;
11347 case NT_S390_SYSTEM_CALL:
11348 if (note->namesz == 6
11349 && strcmp (note->namedata, "LINUX") == 0)
11350 return elfcore_grok_s390_system_call (abfd, note);
11351 else
11352 return true;
11354 case NT_S390_TDB:
11355 if (note->namesz == 6
11356 && strcmp (note->namedata, "LINUX") == 0)
11357 return elfcore_grok_s390_tdb (abfd, note);
11358 else
11359 return true;
11361 case NT_S390_VXRS_LOW:
11362 if (note->namesz == 6
11363 && strcmp (note->namedata, "LINUX") == 0)
11364 return elfcore_grok_s390_vxrs_low (abfd, note);
11365 else
11366 return true;
11368 case NT_S390_VXRS_HIGH:
11369 if (note->namesz == 6
11370 && strcmp (note->namedata, "LINUX") == 0)
11371 return elfcore_grok_s390_vxrs_high (abfd, note);
11372 else
11373 return true;
11375 case NT_S390_GS_CB:
11376 if (note->namesz == 6
11377 && strcmp (note->namedata, "LINUX") == 0)
11378 return elfcore_grok_s390_gs_cb (abfd, note);
11379 else
11380 return true;
11382 case NT_S390_GS_BC:
11383 if (note->namesz == 6
11384 && strcmp (note->namedata, "LINUX") == 0)
11385 return elfcore_grok_s390_gs_bc (abfd, note);
11386 else
11387 return true;
11389 case NT_ARC_V2:
11390 if (note->namesz == 6
11391 && strcmp (note->namedata, "LINUX") == 0)
11392 return elfcore_grok_arc_v2 (abfd, note);
11393 else
11394 return true;
11396 case NT_ARM_VFP:
11397 if (note->namesz == 6
11398 && strcmp (note->namedata, "LINUX") == 0)
11399 return elfcore_grok_arm_vfp (abfd, note);
11400 else
11401 return true;
11403 case NT_ARM_TLS:
11404 if (note->namesz == 6
11405 && strcmp (note->namedata, "LINUX") == 0)
11406 return elfcore_grok_aarch_tls (abfd, note);
11407 else
11408 return true;
11410 case NT_ARM_HW_BREAK:
11411 if (note->namesz == 6
11412 && strcmp (note->namedata, "LINUX") == 0)
11413 return elfcore_grok_aarch_hw_break (abfd, note);
11414 else
11415 return true;
11417 case NT_ARM_HW_WATCH:
11418 if (note->namesz == 6
11419 && strcmp (note->namedata, "LINUX") == 0)
11420 return elfcore_grok_aarch_hw_watch (abfd, note);
11421 else
11422 return true;
11424 case NT_ARM_SVE:
11425 if (note->namesz == 6
11426 && strcmp (note->namedata, "LINUX") == 0)
11427 return elfcore_grok_aarch_sve (abfd, note);
11428 else
11429 return true;
11431 case NT_ARM_PAC_MASK:
11432 if (note->namesz == 6
11433 && strcmp (note->namedata, "LINUX") == 0)
11434 return elfcore_grok_aarch_pauth (abfd, note);
11435 else
11436 return true;
11438 case NT_ARM_TAGGED_ADDR_CTRL:
11439 if (note->namesz == 6
11440 && strcmp (note->namedata, "LINUX") == 0)
11441 return elfcore_grok_aarch_mte (abfd, note);
11442 else
11443 return true;
11445 case NT_ARM_SSVE:
11446 if (note->namesz == 6
11447 && strcmp (note->namedata, "LINUX") == 0)
11448 return elfcore_grok_aarch_ssve (abfd, note);
11449 else
11450 return true;
11452 case NT_ARM_ZA:
11453 if (note->namesz == 6
11454 && strcmp (note->namedata, "LINUX") == 0)
11455 return elfcore_grok_aarch_za (abfd, note);
11456 else
11457 return true;
11459 case NT_ARM_ZT:
11460 if (note->namesz == 6
11461 && strcmp (note->namedata, "LINUX") == 0)
11462 return elfcore_grok_aarch_zt (abfd, note);
11463 else
11464 return true;
11466 case NT_GDB_TDESC:
11467 if (note->namesz == 4
11468 && strcmp (note->namedata, "GDB") == 0)
11469 return elfcore_grok_gdb_tdesc (abfd, note);
11470 else
11471 return true;
11473 case NT_RISCV_CSR:
11474 if (note->namesz == 4
11475 && strcmp (note->namedata, "GDB") == 0)
11476 return elfcore_grok_riscv_csr (abfd, note);
11477 else
11478 return true;
11480 case NT_LARCH_CPUCFG:
11481 if (note->namesz == 6
11482 && strcmp (note->namedata, "LINUX") == 0)
11483 return elfcore_grok_loongarch_cpucfg (abfd, note);
11484 else
11485 return true;
11487 case NT_LARCH_LBT:
11488 if (note->namesz == 6
11489 && strcmp (note->namedata, "LINUX") == 0)
11490 return elfcore_grok_loongarch_lbt (abfd, note);
11491 else
11492 return true;
11494 case NT_LARCH_LSX:
11495 if (note->namesz == 6
11496 && strcmp (note->namedata, "LINUX") == 0)
11497 return elfcore_grok_loongarch_lsx (abfd, note);
11498 else
11499 return true;
11501 case NT_LARCH_LASX:
11502 if (note->namesz == 6
11503 && strcmp (note->namedata, "LINUX") == 0)
11504 return elfcore_grok_loongarch_lasx (abfd, note);
11505 else
11506 return true;
11508 case NT_PRPSINFO:
11509 case NT_PSINFO:
11510 if (bed->elf_backend_grok_psinfo)
11511 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
11512 return true;
11513 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11514 return elfcore_grok_psinfo (abfd, note);
11515 #else
11516 return true;
11517 #endif
11519 case NT_AUXV:
11520 return elfcore_make_auxv_note_section (abfd, note, 0);
11522 case NT_FILE:
11523 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
11524 note);
11526 case NT_SIGINFO:
11527 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
11528 note);
11533 static bool
11534 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
11536 struct bfd_build_id* build_id;
11538 if (note->descsz == 0)
11539 return false;
11541 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
11542 if (build_id == NULL)
11543 return false;
11545 build_id->size = note->descsz;
11546 memcpy (build_id->data, note->descdata, note->descsz);
11547 abfd->build_id = build_id;
11549 return true;
11552 static bool
11553 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
11555 switch (note->type)
11557 default:
11558 return true;
11560 case NT_GNU_PROPERTY_TYPE_0:
11561 return _bfd_elf_parse_gnu_properties (abfd, note);
11563 case NT_GNU_BUILD_ID:
11564 return elfobj_grok_gnu_build_id (abfd, note);
11568 static bool
11569 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
11571 struct sdt_note *cur =
11572 (struct sdt_note *) bfd_alloc (abfd,
11573 sizeof (struct sdt_note) + note->descsz);
11575 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
11576 cur->size = (bfd_size_type) note->descsz;
11577 memcpy (cur->data, note->descdata, note->descsz);
11579 elf_tdata (abfd)->sdt_note_head = cur;
11581 return true;
11584 static bool
11585 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
11587 switch (note->type)
11589 case NT_STAPSDT:
11590 return elfobj_grok_stapsdt_note_1 (abfd, note);
11592 default:
11593 return true;
11597 static bool
11598 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
11600 size_t offset;
11602 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11604 case ELFCLASS32:
11605 if (note->descsz < 108)
11606 return false;
11607 break;
11609 case ELFCLASS64:
11610 if (note->descsz < 120)
11611 return false;
11612 break;
11614 default:
11615 return false;
11618 /* Check for version 1 in pr_version. */
11619 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11620 return false;
11622 offset = 4;
11624 /* Skip over pr_psinfosz. */
11625 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11626 offset += 4;
11627 else
11629 offset += 4; /* Padding before pr_psinfosz. */
11630 offset += 8;
11633 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
11634 elf_tdata (abfd)->core->program
11635 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
11636 offset += 17;
11638 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
11639 elf_tdata (abfd)->core->command
11640 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
11641 offset += 81;
11643 /* Padding before pr_pid. */
11644 offset += 2;
11646 /* The pr_pid field was added in version "1a". */
11647 if (note->descsz < offset + 4)
11648 return true;
11650 elf_tdata (abfd)->core->pid
11651 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11653 return true;
11656 static bool
11657 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
11659 size_t offset;
11660 size_t size;
11661 size_t min_size;
11663 /* Compute offset of pr_getregsz, skipping over pr_statussz.
11664 Also compute minimum size of this note. */
11665 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11667 case ELFCLASS32:
11668 offset = 4 + 4;
11669 min_size = offset + (4 * 2) + 4 + 4 + 4;
11670 break;
11672 case ELFCLASS64:
11673 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
11674 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
11675 break;
11677 default:
11678 return false;
11681 if (note->descsz < min_size)
11682 return false;
11684 /* Check for version 1 in pr_version. */
11685 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11686 return false;
11688 /* Extract size of pr_reg from pr_gregsetsz. */
11689 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
11690 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11692 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11693 offset += 4 * 2;
11695 else
11697 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
11698 offset += 8 * 2;
11701 /* Skip over pr_osreldate. */
11702 offset += 4;
11704 /* Read signal from pr_cursig. */
11705 if (elf_tdata (abfd)->core->signal == 0)
11706 elf_tdata (abfd)->core->signal
11707 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11708 offset += 4;
11710 /* Read TID from pr_pid. */
11711 elf_tdata (abfd)->core->lwpid
11712 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11713 offset += 4;
11715 /* Padding before pr_reg. */
11716 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
11717 offset += 4;
11719 /* Make sure that there is enough data remaining in the note. */
11720 if ((note->descsz - offset) < size)
11721 return false;
11723 /* Make a ".reg/999" section and a ".reg" section. */
11724 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
11725 size, note->descpos + offset);
11728 static bool
11729 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
11731 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11733 switch (note->type)
11735 case NT_PRSTATUS:
11736 if (bed->elf_backend_grok_freebsd_prstatus)
11737 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
11738 return true;
11739 return elfcore_grok_freebsd_prstatus (abfd, note);
11741 case NT_FPREGSET:
11742 return elfcore_grok_prfpreg (abfd, note);
11744 case NT_PRPSINFO:
11745 return elfcore_grok_freebsd_psinfo (abfd, note);
11747 case NT_FREEBSD_THRMISC:
11748 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
11750 case NT_FREEBSD_PROCSTAT_PROC:
11751 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
11752 note);
11754 case NT_FREEBSD_PROCSTAT_FILES:
11755 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
11756 note);
11758 case NT_FREEBSD_PROCSTAT_VMMAP:
11759 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
11760 note);
11762 case NT_FREEBSD_PROCSTAT_AUXV:
11763 return elfcore_make_auxv_note_section (abfd, note, 4);
11765 case NT_FREEBSD_X86_SEGBASES:
11766 return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note);
11768 case NT_X86_XSTATE:
11769 return elfcore_grok_xstatereg (abfd, note);
11771 case NT_FREEBSD_PTLWPINFO:
11772 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
11773 note);
11775 case NT_ARM_TLS:
11776 return elfcore_grok_aarch_tls (abfd, note);
11778 case NT_ARM_VFP:
11779 return elfcore_grok_arm_vfp (abfd, note);
11781 default:
11782 return true;
11786 static bool
11787 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
11789 char *cp;
11791 cp = strchr (note->namedata, '@');
11792 if (cp != NULL)
11794 *lwpidp = atoi(cp + 1);
11795 return true;
11797 return false;
11800 static bool
11801 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11803 if (note->descsz <= 0x7c + 31)
11804 return false;
11806 /* Signal number at offset 0x08. */
11807 elf_tdata (abfd)->core->signal
11808 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11810 /* Process ID at offset 0x50. */
11811 elf_tdata (abfd)->core->pid
11812 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
11814 /* Command name at 0x7c (max 32 bytes, including nul). */
11815 elf_tdata (abfd)->core->command
11816 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
11818 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
11819 note);
11822 static bool
11823 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
11825 int lwp;
11827 if (elfcore_netbsd_get_lwpid (note, &lwp))
11828 elf_tdata (abfd)->core->lwpid = lwp;
11830 switch (note->type)
11832 case NT_NETBSDCORE_PROCINFO:
11833 /* NetBSD-specific core "procinfo". Note that we expect to
11834 find this note before any of the others, which is fine,
11835 since the kernel writes this note out first when it
11836 creates a core file. */
11837 return elfcore_grok_netbsd_procinfo (abfd, note);
11838 case NT_NETBSDCORE_AUXV:
11839 /* NetBSD-specific Elf Auxiliary Vector data. */
11840 return elfcore_make_auxv_note_section (abfd, note, 4);
11841 case NT_NETBSDCORE_LWPSTATUS:
11842 return elfcore_make_note_pseudosection (abfd,
11843 ".note.netbsdcore.lwpstatus",
11844 note);
11845 default:
11846 break;
11849 /* As of March 2020 there are no other machine-independent notes
11850 defined for NetBSD core files. If the note type is less
11851 than the start of the machine-dependent note types, we don't
11852 understand it. */
11854 if (note->type < NT_NETBSDCORE_FIRSTMACH)
11855 return true;
11858 switch (bfd_get_arch (abfd))
11860 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11861 PT_GETFPREGS == mach+2. */
11863 case bfd_arch_aarch64:
11864 case bfd_arch_alpha:
11865 case bfd_arch_sparc:
11866 switch (note->type)
11868 case NT_NETBSDCORE_FIRSTMACH+0:
11869 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11871 case NT_NETBSDCORE_FIRSTMACH+2:
11872 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11874 default:
11875 return true;
11878 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11879 There's also old PT___GETREGS40 == mach + 1 for old reg
11880 structure which lacks GBR. */
11882 case bfd_arch_sh:
11883 switch (note->type)
11885 case NT_NETBSDCORE_FIRSTMACH+3:
11886 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11888 case NT_NETBSDCORE_FIRSTMACH+5:
11889 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11891 default:
11892 return true;
11895 /* On all other arch's, PT_GETREGS == mach+1 and
11896 PT_GETFPREGS == mach+3. */
11898 default:
11899 switch (note->type)
11901 case NT_NETBSDCORE_FIRSTMACH+1:
11902 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11904 case NT_NETBSDCORE_FIRSTMACH+3:
11905 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11907 default:
11908 return true;
11911 /* NOTREACHED */
11914 static bool
11915 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11917 if (note->descsz <= 0x48 + 31)
11918 return false;
11920 /* Signal number at offset 0x08. */
11921 elf_tdata (abfd)->core->signal
11922 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11924 /* Process ID at offset 0x20. */
11925 elf_tdata (abfd)->core->pid
11926 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
11928 /* Command name at 0x48 (max 32 bytes, including nul). */
11929 elf_tdata (abfd)->core->command
11930 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11932 return true;
11935 /* Processes Solaris's process status note.
11936 sig_off ~ offsetof(prstatus_t, pr_cursig)
11937 pid_off ~ offsetof(prstatus_t, pr_pid)
11938 lwpid_off ~ offsetof(prstatus_t, pr_who)
11939 gregset_size ~ sizeof(gregset_t)
11940 gregset_offset ~ offsetof(prstatus_t, pr_reg) */
11942 static bool
11943 elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
11944 int pid_off, int lwpid_off, size_t gregset_size,
11945 size_t gregset_offset)
11947 asection *sect = NULL;
11948 elf_tdata (abfd)->core->signal
11949 = bfd_get_16 (abfd, note->descdata + sig_off);
11950 elf_tdata (abfd)->core->pid
11951 = bfd_get_32 (abfd, note->descdata + pid_off);
11952 elf_tdata (abfd)->core->lwpid
11953 = bfd_get_32 (abfd, note->descdata + lwpid_off);
11955 sect = bfd_get_section_by_name (abfd, ".reg");
11956 if (sect != NULL)
11957 sect->size = gregset_size;
11959 return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11960 note->descpos + gregset_offset);
11963 /* Gets program and arguments from a core.
11964 prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
11965 comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs) */
11967 static bool
11968 elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
11969 int prog_off, int comm_off)
11971 elf_tdata (abfd)->core->program
11972 = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
11973 elf_tdata (abfd)->core->command
11974 = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
11976 return true;
11979 /* Processes Solaris's LWP status note.
11980 gregset_size ~ sizeof(gregset_t)
11981 gregset_off ~ offsetof(lwpstatus_t, pr_reg)
11982 fpregset_size ~ sizeof(fpregset_t)
11983 fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg) */
11985 static bool
11986 elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
11987 size_t gregset_size, int gregset_off,
11988 size_t fpregset_size, int fpregset_off)
11990 asection *sect = NULL;
11991 char reg2_section_name[16] = { 0 };
11993 (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2",
11994 elf_tdata (abfd)->core->lwpid);
11996 /* offsetof(lwpstatus_t, pr_lwpid) */
11997 elf_tdata (abfd)->core->lwpid
11998 = bfd_get_32 (abfd, note->descdata + 4);
11999 /* offsetof(lwpstatus_t, pr_cursig) */
12000 elf_tdata (abfd)->core->signal
12001 = bfd_get_16 (abfd, note->descdata + 12);
12003 sect = bfd_get_section_by_name (abfd, ".reg");
12004 if (sect != NULL)
12005 sect->size = gregset_size;
12006 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
12007 note->descpos + gregset_off))
12008 return false;
12010 sect = bfd_get_section_by_name (abfd, reg2_section_name);
12011 if (sect != NULL)
12013 sect->size = fpregset_size;
12014 sect->filepos = note->descpos + fpregset_off;
12015 sect->alignment_power = 2;
12017 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size,
12018 note->descpos + fpregset_off))
12019 return false;
12021 return true;
12024 static bool
12025 elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
12027 if (note == NULL)
12028 return false;
12030 /* core files are identified as 32- or 64-bit, SPARC or x86,
12031 by the size of the descsz which matches the sizeof()
12032 the type appropriate for that note type (e.g., prstatus_t for
12033 SOLARIS_NT_PRSTATUS) for the corresponding architecture
12034 on Solaris. The core file bitness may differ from the bitness of
12035 gdb itself, so fixed values are used instead of sizeof().
12036 Appropriate fixed offsets are also used to obtain data from
12037 the note. */
12039 switch ((int) note->type)
12041 case SOLARIS_NT_PRSTATUS:
12042 switch (note->descsz)
12044 case 508: /* sizeof(prstatus_t) SPARC 32-bit */
12045 return elfcore_grok_solaris_prstatus(abfd, note,
12046 136, 216, 308, 152, 356);
12047 case 904: /* sizeof(prstatus_t) SPARC 64-bit */
12048 return elfcore_grok_solaris_prstatus(abfd, note,
12049 264, 360, 520, 304, 600);
12050 case 432: /* sizeof(prstatus_t) Intel 32-bit */
12051 return elfcore_grok_solaris_prstatus(abfd, note,
12052 136, 216, 308, 76, 356);
12053 case 824: /* sizeof(prstatus_t) Intel 64-bit */
12054 return elfcore_grok_solaris_prstatus(abfd, note,
12055 264, 360, 520, 224, 600);
12056 default:
12057 return true;
12060 case SOLARIS_NT_PSINFO:
12061 case SOLARIS_NT_PRPSINFO:
12062 switch (note->descsz)
12064 case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
12065 return elfcore_grok_solaris_info(abfd, note, 84, 100);
12066 case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
12067 return elfcore_grok_solaris_info(abfd, note, 120, 136);
12068 case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
12069 return elfcore_grok_solaris_info(abfd, note, 88, 104);
12070 case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
12071 return elfcore_grok_solaris_info(abfd, note, 136, 152);
12072 default:
12073 return true;
12076 case SOLARIS_NT_LWPSTATUS:
12077 switch (note->descsz)
12079 case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
12080 return elfcore_grok_solaris_lwpstatus(abfd, note,
12081 152, 344, 400, 496);
12082 case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
12083 return elfcore_grok_solaris_lwpstatus(abfd, note,
12084 304, 544, 544, 848);
12085 case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
12086 return elfcore_grok_solaris_lwpstatus(abfd, note,
12087 76, 344, 380, 420);
12088 case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
12089 return elfcore_grok_solaris_lwpstatus(abfd, note,
12090 224, 544, 528, 768);
12091 default:
12092 return true;
12095 case SOLARIS_NT_LWPSINFO:
12096 /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
12097 if (note->descsz == 128 || note->descsz == 152)
12098 elf_tdata (abfd)->core->lwpid =
12099 bfd_get_32 (abfd, note->descdata + 4);
12100 break;
12102 default:
12103 break;
12106 return true;
12109 /* For name starting with "CORE" this may be either a Solaris
12110 core file or a gdb-generated core file. Do Solaris-specific
12111 processing on selected note types first with
12112 elfcore_grok_solaris_note(), then process the note
12113 in elfcore_grok_note(). */
12115 static bool
12116 elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
12118 if (!elfcore_grok_solaris_note_impl (abfd, note))
12119 return false;
12121 return elfcore_grok_note (abfd, note);
12124 static bool
12125 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
12127 if (note->type == NT_OPENBSD_PROCINFO)
12128 return elfcore_grok_openbsd_procinfo (abfd, note);
12130 if (note->type == NT_OPENBSD_REGS)
12131 return elfcore_make_note_pseudosection (abfd, ".reg", note);
12133 if (note->type == NT_OPENBSD_FPREGS)
12134 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
12136 if (note->type == NT_OPENBSD_XFPREGS)
12137 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
12139 if (note->type == NT_OPENBSD_AUXV)
12140 return elfcore_make_auxv_note_section (abfd, note, 0);
12142 if (note->type == NT_OPENBSD_WCOOKIE)
12144 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
12145 SEC_HAS_CONTENTS);
12147 if (sect == NULL)
12148 return false;
12149 sect->size = note->descsz;
12150 sect->filepos = note->descpos;
12151 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
12153 return true;
12156 return true;
12159 static bool
12160 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
12162 void *ddata = note->descdata;
12163 char buf[100];
12164 char *name;
12165 asection *sect;
12166 short sig;
12167 unsigned flags;
12169 if (note->descsz < 16)
12170 return false;
12172 /* nto_procfs_status 'pid' field is at offset 0. */
12173 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
12175 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
12176 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
12178 /* nto_procfs_status 'flags' field is at offset 8. */
12179 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
12181 /* nto_procfs_status 'what' field is at offset 14. */
12182 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
12184 elf_tdata (abfd)->core->signal = sig;
12185 elf_tdata (abfd)->core->lwpid = *tid;
12188 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
12189 do not come from signals so we make sure we set the current
12190 thread just in case. */
12191 if (flags & 0x00000080)
12192 elf_tdata (abfd)->core->lwpid = *tid;
12194 /* Make a ".qnx_core_status/%d" section. */
12195 sprintf (buf, ".qnx_core_status/%ld", *tid);
12197 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
12198 if (name == NULL)
12199 return false;
12200 strcpy (name, buf);
12202 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12203 if (sect == NULL)
12204 return false;
12206 sect->size = note->descsz;
12207 sect->filepos = note->descpos;
12208 sect->alignment_power = 2;
12210 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
12213 static bool
12214 elfcore_grok_nto_regs (bfd *abfd,
12215 Elf_Internal_Note *note,
12216 long tid,
12217 char *base)
12219 char buf[100];
12220 char *name;
12221 asection *sect;
12223 /* Make a "(base)/%d" section. */
12224 sprintf (buf, "%s/%ld", base, tid);
12226 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
12227 if (name == NULL)
12228 return false;
12229 strcpy (name, buf);
12231 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12232 if (sect == NULL)
12233 return false;
12235 sect->size = note->descsz;
12236 sect->filepos = note->descpos;
12237 sect->alignment_power = 2;
12239 /* This is the current thread. */
12240 if (elf_tdata (abfd)->core->lwpid == tid)
12241 return elfcore_maybe_make_sect (abfd, base, sect);
12243 return true;
12246 static bool
12247 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
12249 /* Every GREG section has a STATUS section before it. Store the
12250 tid from the previous call to pass down to the next gregs
12251 function. */
12252 static long tid = 1;
12254 switch (note->type)
12256 case QNT_CORE_INFO:
12257 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
12258 case QNT_CORE_STATUS:
12259 return elfcore_grok_nto_status (abfd, note, &tid);
12260 case QNT_CORE_GREG:
12261 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
12262 case QNT_CORE_FPREG:
12263 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
12264 default:
12265 return true;
12269 static bool
12270 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
12272 char *name;
12273 asection *sect;
12274 size_t len;
12276 /* Use note name as section name. */
12277 len = note->namesz;
12278 name = (char *) bfd_alloc (abfd, len);
12279 if (name == NULL)
12280 return false;
12281 memcpy (name, note->namedata, len);
12282 name[len - 1] = '\0';
12284 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12285 if (sect == NULL)
12286 return false;
12288 sect->size = note->descsz;
12289 sect->filepos = note->descpos;
12290 sect->alignment_power = 1;
12292 return true;
12295 /* Function: elfcore_write_note
12297 Inputs:
12298 buffer to hold note, and current size of buffer
12299 name of note
12300 type of note
12301 data for note
12302 size of data for note
12304 Writes note to end of buffer. ELF64 notes are written exactly as
12305 for ELF32, despite the current (as of 2006) ELF gabi specifying
12306 that they ought to have 8-byte namesz and descsz field, and have
12307 8-byte alignment. Other writers, eg. Linux kernel, do the same.
12309 Return:
12310 Pointer to realloc'd buffer, *BUFSIZ updated. */
12312 char *
12313 elfcore_write_note (bfd *abfd,
12314 char *buf,
12315 int *bufsiz,
12316 const char *name,
12317 int type,
12318 const void *input,
12319 int size)
12321 Elf_External_Note *xnp;
12322 size_t namesz;
12323 size_t newspace;
12324 char *dest;
12326 namesz = 0;
12327 if (name != NULL)
12328 namesz = strlen (name) + 1;
12330 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
12332 buf = (char *) realloc (buf, *bufsiz + newspace);
12333 if (buf == NULL)
12334 return buf;
12335 dest = buf + *bufsiz;
12336 *bufsiz += newspace;
12337 xnp = (Elf_External_Note *) dest;
12338 H_PUT_32 (abfd, namesz, xnp->namesz);
12339 H_PUT_32 (abfd, size, xnp->descsz);
12340 H_PUT_32 (abfd, type, xnp->type);
12341 dest = xnp->name;
12342 if (name != NULL)
12344 memcpy (dest, name, namesz);
12345 dest += namesz;
12346 while (namesz & 3)
12348 *dest++ = '\0';
12349 ++namesz;
12352 memcpy (dest, input, size);
12353 dest += size;
12354 while (size & 3)
12356 *dest++ = '\0';
12357 ++size;
12359 return buf;
12362 /* gcc-8 warns (*) on all the strncpy calls in this function about
12363 possible string truncation. The "truncation" is not a bug. We
12364 have an external representation of structs with fields that are not
12365 necessarily NULL terminated and corresponding internal
12366 representation fields that are one larger so that they can always
12367 be NULL terminated.
12368 gcc versions between 4.2 and 4.6 do not allow pragma control of
12369 diagnostics inside functions, giving a hard error if you try to use
12370 the finer control available with later versions.
12371 gcc prior to 4.2 warns about diagnostic push and pop.
12372 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
12373 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
12374 (*) Depending on your system header files! */
12375 #if GCC_VERSION >= 8000
12376 # pragma GCC diagnostic push
12377 # pragma GCC diagnostic ignored "-Wstringop-truncation"
12378 #endif
12379 char *
12380 elfcore_write_prpsinfo (bfd *abfd,
12381 char *buf,
12382 int *bufsiz,
12383 const char *fname,
12384 const char *psargs)
12386 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12388 if (bed->elf_backend_write_core_note != NULL)
12390 char *ret;
12391 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12392 NT_PRPSINFO, fname, psargs);
12393 if (ret != NULL)
12394 return ret;
12397 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
12398 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
12399 if (bed->s->elfclass == ELFCLASS32)
12401 # if defined (HAVE_PSINFO32_T)
12402 psinfo32_t data;
12403 int note_type = NT_PSINFO;
12404 # else
12405 prpsinfo32_t data;
12406 int note_type = NT_PRPSINFO;
12407 # endif
12409 memset (&data, 0, sizeof (data));
12410 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12411 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12412 return elfcore_write_note (abfd, buf, bufsiz,
12413 "CORE", note_type, &data, sizeof (data));
12415 else
12416 # endif
12418 # if defined (HAVE_PSINFO_T)
12419 psinfo_t data;
12420 int note_type = NT_PSINFO;
12421 # else
12422 prpsinfo_t data;
12423 int note_type = NT_PRPSINFO;
12424 # endif
12426 memset (&data, 0, sizeof (data));
12427 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12428 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12429 return elfcore_write_note (abfd, buf, bufsiz,
12430 "CORE", note_type, &data, sizeof (data));
12432 #endif /* PSINFO_T or PRPSINFO_T */
12434 free (buf);
12435 return NULL;
12437 #if GCC_VERSION >= 8000
12438 # pragma GCC diagnostic pop
12439 #endif
12441 char *
12442 elfcore_write_linux_prpsinfo32
12443 (bfd *abfd, char *buf, int *bufsiz,
12444 const struct elf_internal_linux_prpsinfo *prpsinfo)
12446 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
12448 struct elf_external_linux_prpsinfo32_ugid16 data;
12450 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
12451 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
12452 &data, sizeof (data));
12454 else
12456 struct elf_external_linux_prpsinfo32_ugid32 data;
12458 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
12459 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
12460 &data, sizeof (data));
12464 char *
12465 elfcore_write_linux_prpsinfo64
12466 (bfd *abfd, char *buf, int *bufsiz,
12467 const struct elf_internal_linux_prpsinfo *prpsinfo)
12469 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
12471 struct elf_external_linux_prpsinfo64_ugid16 data;
12473 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
12474 return elfcore_write_note (abfd, buf, bufsiz,
12475 "CORE", NT_PRPSINFO, &data, sizeof (data));
12477 else
12479 struct elf_external_linux_prpsinfo64_ugid32 data;
12481 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
12482 return elfcore_write_note (abfd, buf, bufsiz,
12483 "CORE", NT_PRPSINFO, &data, sizeof (data));
12487 char *
12488 elfcore_write_prstatus (bfd *abfd,
12489 char *buf,
12490 int *bufsiz,
12491 long pid,
12492 int cursig,
12493 const void *gregs)
12495 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12497 if (bed->elf_backend_write_core_note != NULL)
12499 char *ret;
12500 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12501 NT_PRSTATUS,
12502 pid, cursig, gregs);
12503 if (ret != NULL)
12504 return ret;
12507 #if defined (HAVE_PRSTATUS_T)
12508 #if defined (HAVE_PRSTATUS32_T)
12509 if (bed->s->elfclass == ELFCLASS32)
12511 prstatus32_t prstat;
12513 memset (&prstat, 0, sizeof (prstat));
12514 prstat.pr_pid = pid;
12515 prstat.pr_cursig = cursig;
12516 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12517 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
12518 NT_PRSTATUS, &prstat, sizeof (prstat));
12520 else
12521 #endif
12523 prstatus_t prstat;
12525 memset (&prstat, 0, sizeof (prstat));
12526 prstat.pr_pid = pid;
12527 prstat.pr_cursig = cursig;
12528 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12529 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
12530 NT_PRSTATUS, &prstat, sizeof (prstat));
12532 #endif /* HAVE_PRSTATUS_T */
12534 free (buf);
12535 return NULL;
12538 #if defined (HAVE_LWPSTATUS_T)
12539 char *
12540 elfcore_write_lwpstatus (bfd *abfd,
12541 char *buf,
12542 int *bufsiz,
12543 long pid,
12544 int cursig,
12545 const void *gregs)
12547 lwpstatus_t lwpstat;
12548 const char *note_name = "CORE";
12550 memset (&lwpstat, 0, sizeof (lwpstat));
12551 lwpstat.pr_lwpid = pid >> 16;
12552 lwpstat.pr_cursig = cursig;
12553 #if defined (HAVE_LWPSTATUS_T_PR_REG)
12554 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
12555 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
12556 #if !defined(gregs)
12557 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
12558 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
12559 #else
12560 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
12561 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
12562 #endif
12563 #endif
12564 return elfcore_write_note (abfd, buf, bufsiz, note_name,
12565 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
12567 #endif /* HAVE_LWPSTATUS_T */
12569 #if defined (HAVE_PSTATUS_T)
12570 char *
12571 elfcore_write_pstatus (bfd *abfd,
12572 char *buf,
12573 int *bufsiz,
12574 long pid,
12575 int cursig ATTRIBUTE_UNUSED,
12576 const void *gregs ATTRIBUTE_UNUSED)
12578 const char *note_name = "CORE";
12579 #if defined (HAVE_PSTATUS32_T)
12580 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12582 if (bed->s->elfclass == ELFCLASS32)
12584 pstatus32_t pstat;
12586 memset (&pstat, 0, sizeof (pstat));
12587 pstat.pr_pid = pid & 0xffff;
12588 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
12589 NT_PSTATUS, &pstat, sizeof (pstat));
12590 return buf;
12592 else
12593 #endif
12595 pstatus_t pstat;
12597 memset (&pstat, 0, sizeof (pstat));
12598 pstat.pr_pid = pid & 0xffff;
12599 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
12600 NT_PSTATUS, &pstat, sizeof (pstat));
12601 return buf;
12604 #endif /* HAVE_PSTATUS_T */
12606 char *
12607 elfcore_write_prfpreg (bfd *abfd,
12608 char *buf,
12609 int *bufsiz,
12610 const void *fpregs,
12611 int size)
12613 const char *note_name = "CORE";
12614 return elfcore_write_note (abfd, buf, bufsiz,
12615 note_name, NT_FPREGSET, fpregs, size);
12618 char *
12619 elfcore_write_prxfpreg (bfd *abfd,
12620 char *buf,
12621 int *bufsiz,
12622 const void *xfpregs,
12623 int size)
12625 char *note_name = "LINUX";
12626 return elfcore_write_note (abfd, buf, bufsiz,
12627 note_name, NT_PRXFPREG, xfpregs, size);
12630 char *
12631 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
12632 const void *xfpregs, int size)
12634 char *note_name;
12635 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
12636 note_name = "FreeBSD";
12637 else
12638 note_name = "LINUX";
12639 return elfcore_write_note (abfd, buf, bufsiz,
12640 note_name, NT_X86_XSTATE, xfpregs, size);
12643 char *
12644 elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
12645 const void *regs, int size)
12647 char *note_name = "FreeBSD";
12648 return elfcore_write_note (abfd, buf, bufsiz,
12649 note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
12652 char *
12653 elfcore_write_ppc_vmx (bfd *abfd,
12654 char *buf,
12655 int *bufsiz,
12656 const void *ppc_vmx,
12657 int size)
12659 char *note_name = "LINUX";
12660 return elfcore_write_note (abfd, buf, bufsiz,
12661 note_name, NT_PPC_VMX, ppc_vmx, size);
12664 char *
12665 elfcore_write_ppc_vsx (bfd *abfd,
12666 char *buf,
12667 int *bufsiz,
12668 const void *ppc_vsx,
12669 int size)
12671 char *note_name = "LINUX";
12672 return elfcore_write_note (abfd, buf, bufsiz,
12673 note_name, NT_PPC_VSX, ppc_vsx, size);
12676 char *
12677 elfcore_write_ppc_tar (bfd *abfd,
12678 char *buf,
12679 int *bufsiz,
12680 const void *ppc_tar,
12681 int size)
12683 char *note_name = "LINUX";
12684 return elfcore_write_note (abfd, buf, bufsiz,
12685 note_name, NT_PPC_TAR, ppc_tar, size);
12688 char *
12689 elfcore_write_ppc_ppr (bfd *abfd,
12690 char *buf,
12691 int *bufsiz,
12692 const void *ppc_ppr,
12693 int size)
12695 char *note_name = "LINUX";
12696 return elfcore_write_note (abfd, buf, bufsiz,
12697 note_name, NT_PPC_PPR, ppc_ppr, size);
12700 char *
12701 elfcore_write_ppc_dscr (bfd *abfd,
12702 char *buf,
12703 int *bufsiz,
12704 const void *ppc_dscr,
12705 int size)
12707 char *note_name = "LINUX";
12708 return elfcore_write_note (abfd, buf, bufsiz,
12709 note_name, NT_PPC_DSCR, ppc_dscr, size);
12712 char *
12713 elfcore_write_ppc_ebb (bfd *abfd,
12714 char *buf,
12715 int *bufsiz,
12716 const void *ppc_ebb,
12717 int size)
12719 char *note_name = "LINUX";
12720 return elfcore_write_note (abfd, buf, bufsiz,
12721 note_name, NT_PPC_EBB, ppc_ebb, size);
12724 char *
12725 elfcore_write_ppc_pmu (bfd *abfd,
12726 char *buf,
12727 int *bufsiz,
12728 const void *ppc_pmu,
12729 int size)
12731 char *note_name = "LINUX";
12732 return elfcore_write_note (abfd, buf, bufsiz,
12733 note_name, NT_PPC_PMU, ppc_pmu, size);
12736 char *
12737 elfcore_write_ppc_tm_cgpr (bfd *abfd,
12738 char *buf,
12739 int *bufsiz,
12740 const void *ppc_tm_cgpr,
12741 int size)
12743 char *note_name = "LINUX";
12744 return elfcore_write_note (abfd, buf, bufsiz,
12745 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
12748 char *
12749 elfcore_write_ppc_tm_cfpr (bfd *abfd,
12750 char *buf,
12751 int *bufsiz,
12752 const void *ppc_tm_cfpr,
12753 int size)
12755 char *note_name = "LINUX";
12756 return elfcore_write_note (abfd, buf, bufsiz,
12757 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
12760 char *
12761 elfcore_write_ppc_tm_cvmx (bfd *abfd,
12762 char *buf,
12763 int *bufsiz,
12764 const void *ppc_tm_cvmx,
12765 int size)
12767 char *note_name = "LINUX";
12768 return elfcore_write_note (abfd, buf, bufsiz,
12769 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
12772 char *
12773 elfcore_write_ppc_tm_cvsx (bfd *abfd,
12774 char *buf,
12775 int *bufsiz,
12776 const void *ppc_tm_cvsx,
12777 int size)
12779 char *note_name = "LINUX";
12780 return elfcore_write_note (abfd, buf, bufsiz,
12781 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
12784 char *
12785 elfcore_write_ppc_tm_spr (bfd *abfd,
12786 char *buf,
12787 int *bufsiz,
12788 const void *ppc_tm_spr,
12789 int size)
12791 char *note_name = "LINUX";
12792 return elfcore_write_note (abfd, buf, bufsiz,
12793 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
12796 char *
12797 elfcore_write_ppc_tm_ctar (bfd *abfd,
12798 char *buf,
12799 int *bufsiz,
12800 const void *ppc_tm_ctar,
12801 int size)
12803 char *note_name = "LINUX";
12804 return elfcore_write_note (abfd, buf, bufsiz,
12805 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
12808 char *
12809 elfcore_write_ppc_tm_cppr (bfd *abfd,
12810 char *buf,
12811 int *bufsiz,
12812 const void *ppc_tm_cppr,
12813 int size)
12815 char *note_name = "LINUX";
12816 return elfcore_write_note (abfd, buf, bufsiz,
12817 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
12820 char *
12821 elfcore_write_ppc_tm_cdscr (bfd *abfd,
12822 char *buf,
12823 int *bufsiz,
12824 const void *ppc_tm_cdscr,
12825 int size)
12827 char *note_name = "LINUX";
12828 return elfcore_write_note (abfd, buf, bufsiz,
12829 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
12832 static char *
12833 elfcore_write_s390_high_gprs (bfd *abfd,
12834 char *buf,
12835 int *bufsiz,
12836 const void *s390_high_gprs,
12837 int size)
12839 char *note_name = "LINUX";
12840 return elfcore_write_note (abfd, buf, bufsiz,
12841 note_name, NT_S390_HIGH_GPRS,
12842 s390_high_gprs, size);
12845 char *
12846 elfcore_write_s390_timer (bfd *abfd,
12847 char *buf,
12848 int *bufsiz,
12849 const void *s390_timer,
12850 int size)
12852 char *note_name = "LINUX";
12853 return elfcore_write_note (abfd, buf, bufsiz,
12854 note_name, NT_S390_TIMER, s390_timer, size);
12857 char *
12858 elfcore_write_s390_todcmp (bfd *abfd,
12859 char *buf,
12860 int *bufsiz,
12861 const void *s390_todcmp,
12862 int size)
12864 char *note_name = "LINUX";
12865 return elfcore_write_note (abfd, buf, bufsiz,
12866 note_name, NT_S390_TODCMP, s390_todcmp, size);
12869 char *
12870 elfcore_write_s390_todpreg (bfd *abfd,
12871 char *buf,
12872 int *bufsiz,
12873 const void *s390_todpreg,
12874 int size)
12876 char *note_name = "LINUX";
12877 return elfcore_write_note (abfd, buf, bufsiz,
12878 note_name, NT_S390_TODPREG, s390_todpreg, size);
12881 char *
12882 elfcore_write_s390_ctrs (bfd *abfd,
12883 char *buf,
12884 int *bufsiz,
12885 const void *s390_ctrs,
12886 int size)
12888 char *note_name = "LINUX";
12889 return elfcore_write_note (abfd, buf, bufsiz,
12890 note_name, NT_S390_CTRS, s390_ctrs, size);
12893 char *
12894 elfcore_write_s390_prefix (bfd *abfd,
12895 char *buf,
12896 int *bufsiz,
12897 const void *s390_prefix,
12898 int size)
12900 char *note_name = "LINUX";
12901 return elfcore_write_note (abfd, buf, bufsiz,
12902 note_name, NT_S390_PREFIX, s390_prefix, size);
12905 char *
12906 elfcore_write_s390_last_break (bfd *abfd,
12907 char *buf,
12908 int *bufsiz,
12909 const void *s390_last_break,
12910 int size)
12912 char *note_name = "LINUX";
12913 return elfcore_write_note (abfd, buf, bufsiz,
12914 note_name, NT_S390_LAST_BREAK,
12915 s390_last_break, size);
12918 char *
12919 elfcore_write_s390_system_call (bfd *abfd,
12920 char *buf,
12921 int *bufsiz,
12922 const void *s390_system_call,
12923 int size)
12925 char *note_name = "LINUX";
12926 return elfcore_write_note (abfd, buf, bufsiz,
12927 note_name, NT_S390_SYSTEM_CALL,
12928 s390_system_call, size);
12931 char *
12932 elfcore_write_s390_tdb (bfd *abfd,
12933 char *buf,
12934 int *bufsiz,
12935 const void *s390_tdb,
12936 int size)
12938 char *note_name = "LINUX";
12939 return elfcore_write_note (abfd, buf, bufsiz,
12940 note_name, NT_S390_TDB, s390_tdb, size);
12943 char *
12944 elfcore_write_s390_vxrs_low (bfd *abfd,
12945 char *buf,
12946 int *bufsiz,
12947 const void *s390_vxrs_low,
12948 int size)
12950 char *note_name = "LINUX";
12951 return elfcore_write_note (abfd, buf, bufsiz,
12952 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
12955 char *
12956 elfcore_write_s390_vxrs_high (bfd *abfd,
12957 char *buf,
12958 int *bufsiz,
12959 const void *s390_vxrs_high,
12960 int size)
12962 char *note_name = "LINUX";
12963 return elfcore_write_note (abfd, buf, bufsiz,
12964 note_name, NT_S390_VXRS_HIGH,
12965 s390_vxrs_high, size);
12968 char *
12969 elfcore_write_s390_gs_cb (bfd *abfd,
12970 char *buf,
12971 int *bufsiz,
12972 const void *s390_gs_cb,
12973 int size)
12975 char *note_name = "LINUX";
12976 return elfcore_write_note (abfd, buf, bufsiz,
12977 note_name, NT_S390_GS_CB,
12978 s390_gs_cb, size);
12981 char *
12982 elfcore_write_s390_gs_bc (bfd *abfd,
12983 char *buf,
12984 int *bufsiz,
12985 const void *s390_gs_bc,
12986 int size)
12988 char *note_name = "LINUX";
12989 return elfcore_write_note (abfd, buf, bufsiz,
12990 note_name, NT_S390_GS_BC,
12991 s390_gs_bc, size);
12994 char *
12995 elfcore_write_arm_vfp (bfd *abfd,
12996 char *buf,
12997 int *bufsiz,
12998 const void *arm_vfp,
12999 int size)
13001 char *note_name = "LINUX";
13002 return elfcore_write_note (abfd, buf, bufsiz,
13003 note_name, NT_ARM_VFP, arm_vfp, size);
13006 char *
13007 elfcore_write_aarch_tls (bfd *abfd,
13008 char *buf,
13009 int *bufsiz,
13010 const void *aarch_tls,
13011 int size)
13013 char *note_name = "LINUX";
13014 return elfcore_write_note (abfd, buf, bufsiz,
13015 note_name, NT_ARM_TLS, aarch_tls, size);
13018 char *
13019 elfcore_write_aarch_hw_break (bfd *abfd,
13020 char *buf,
13021 int *bufsiz,
13022 const void *aarch_hw_break,
13023 int size)
13025 char *note_name = "LINUX";
13026 return elfcore_write_note (abfd, buf, bufsiz,
13027 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
13030 char *
13031 elfcore_write_aarch_hw_watch (bfd *abfd,
13032 char *buf,
13033 int *bufsiz,
13034 const void *aarch_hw_watch,
13035 int size)
13037 char *note_name = "LINUX";
13038 return elfcore_write_note (abfd, buf, bufsiz,
13039 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
13042 char *
13043 elfcore_write_aarch_sve (bfd *abfd,
13044 char *buf,
13045 int *bufsiz,
13046 const void *aarch_sve,
13047 int size)
13049 char *note_name = "LINUX";
13050 return elfcore_write_note (abfd, buf, bufsiz,
13051 note_name, NT_ARM_SVE, aarch_sve, size);
13054 char *
13055 elfcore_write_aarch_pauth (bfd *abfd,
13056 char *buf,
13057 int *bufsiz,
13058 const void *aarch_pauth,
13059 int size)
13061 char *note_name = "LINUX";
13062 return elfcore_write_note (abfd, buf, bufsiz,
13063 note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
13066 char *
13067 elfcore_write_aarch_mte (bfd *abfd,
13068 char *buf,
13069 int *bufsiz,
13070 const void *aarch_mte,
13071 int size)
13073 char *note_name = "LINUX";
13074 return elfcore_write_note (abfd, buf, bufsiz,
13075 note_name, NT_ARM_TAGGED_ADDR_CTRL,
13076 aarch_mte,
13077 size);
13080 char *
13081 elfcore_write_aarch_ssve (bfd *abfd,
13082 char *buf,
13083 int *bufsiz,
13084 const void *aarch_ssve,
13085 int size)
13087 char *note_name = "LINUX";
13088 return elfcore_write_note (abfd, buf, bufsiz,
13089 note_name, NT_ARM_SSVE,
13090 aarch_ssve,
13091 size);
13094 char *
13095 elfcore_write_aarch_za (bfd *abfd,
13096 char *buf,
13097 int *bufsiz,
13098 const void *aarch_za,
13099 int size)
13101 char *note_name = "LINUX";
13102 return elfcore_write_note (abfd, buf, bufsiz,
13103 note_name, NT_ARM_ZA,
13104 aarch_za,
13105 size);
13108 /* Write the buffer of zt register values in aarch_zt (length SIZE) into
13109 the note buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
13110 written into. Return a pointer to the new start of the note buffer, to
13111 replace BUF which may no longer be valid. */
13113 char *
13114 elfcore_write_aarch_zt (bfd *abfd,
13115 char *buf,
13116 int *bufsiz,
13117 const void *aarch_zt,
13118 int size)
13120 char *note_name = "LINUX";
13121 return elfcore_write_note (abfd, buf, bufsiz,
13122 note_name, NT_ARM_ZT,
13123 aarch_zt,
13124 size);
13127 char *
13128 elfcore_write_arc_v2 (bfd *abfd,
13129 char *buf,
13130 int *bufsiz,
13131 const void *arc_v2,
13132 int size)
13134 char *note_name = "LINUX";
13135 return elfcore_write_note (abfd, buf, bufsiz,
13136 note_name, NT_ARC_V2, arc_v2, size);
13139 char *
13140 elfcore_write_loongarch_cpucfg (bfd *abfd,
13141 char *buf,
13142 int *bufsiz,
13143 const void *loongarch_cpucfg,
13144 int size)
13146 char *note_name = "LINUX";
13147 return elfcore_write_note (abfd, buf, bufsiz,
13148 note_name, NT_LARCH_CPUCFG,
13149 loongarch_cpucfg, size);
13152 char *
13153 elfcore_write_loongarch_lbt (bfd *abfd,
13154 char *buf,
13155 int *bufsiz,
13156 const void *loongarch_lbt,
13157 int size)
13159 char *note_name = "LINUX";
13160 return elfcore_write_note (abfd, buf, bufsiz,
13161 note_name, NT_LARCH_LBT, loongarch_lbt, size);
13164 char *
13165 elfcore_write_loongarch_lsx (bfd *abfd,
13166 char *buf,
13167 int *bufsiz,
13168 const void *loongarch_lsx,
13169 int size)
13171 char *note_name = "LINUX";
13172 return elfcore_write_note (abfd, buf, bufsiz,
13173 note_name, NT_LARCH_LSX, loongarch_lsx, size);
13176 char *
13177 elfcore_write_loongarch_lasx (bfd *abfd,
13178 char *buf,
13179 int *bufsiz,
13180 const void *loongarch_lasx,
13181 int size)
13183 char *note_name = "LINUX";
13184 return elfcore_write_note (abfd, buf, bufsiz,
13185 note_name, NT_LARCH_LASX, loongarch_lasx, size);
13188 /* Write the buffer of csr values in CSRS (length SIZE) into the note
13189 buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
13190 written into. Return a pointer to the new start of the note buffer, to
13191 replace BUF which may no longer be valid. */
13193 char *
13194 elfcore_write_riscv_csr (bfd *abfd,
13195 char *buf,
13196 int *bufsiz,
13197 const void *csrs,
13198 int size)
13200 const char *note_name = "GDB";
13201 return elfcore_write_note (abfd, buf, bufsiz,
13202 note_name, NT_RISCV_CSR, csrs, size);
13205 /* Write the target description (a string) pointed to by TDESC, length
13206 SIZE, into the note buffer BUF, and update *BUFSIZ. ABFD is the bfd the
13207 note is being written into. Return a pointer to the new start of the
13208 note buffer, to replace BUF which may no longer be valid. */
13210 char *
13211 elfcore_write_gdb_tdesc (bfd *abfd,
13212 char *buf,
13213 int *bufsiz,
13214 const void *tdesc,
13215 int size)
13217 const char *note_name = "GDB";
13218 return elfcore_write_note (abfd, buf, bufsiz,
13219 note_name, NT_GDB_TDESC, tdesc, size);
13222 char *
13223 elfcore_write_register_note (bfd *abfd,
13224 char *buf,
13225 int *bufsiz,
13226 const char *section,
13227 const void *data,
13228 int size)
13230 if (strcmp (section, ".reg2") == 0)
13231 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
13232 if (strcmp (section, ".reg-xfp") == 0)
13233 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
13234 if (strcmp (section, ".reg-xstate") == 0)
13235 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
13236 if (strcmp (section, ".reg-x86-segbases") == 0)
13237 return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
13238 if (strcmp (section, ".reg-ppc-vmx") == 0)
13239 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
13240 if (strcmp (section, ".reg-ppc-vsx") == 0)
13241 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
13242 if (strcmp (section, ".reg-ppc-tar") == 0)
13243 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
13244 if (strcmp (section, ".reg-ppc-ppr") == 0)
13245 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
13246 if (strcmp (section, ".reg-ppc-dscr") == 0)
13247 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
13248 if (strcmp (section, ".reg-ppc-ebb") == 0)
13249 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
13250 if (strcmp (section, ".reg-ppc-pmu") == 0)
13251 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
13252 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
13253 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
13254 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
13255 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
13256 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
13257 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
13258 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
13259 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
13260 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
13261 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
13262 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
13263 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
13264 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
13265 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
13266 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
13267 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
13268 if (strcmp (section, ".reg-s390-high-gprs") == 0)
13269 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
13270 if (strcmp (section, ".reg-s390-timer") == 0)
13271 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
13272 if (strcmp (section, ".reg-s390-todcmp") == 0)
13273 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
13274 if (strcmp (section, ".reg-s390-todpreg") == 0)
13275 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
13276 if (strcmp (section, ".reg-s390-ctrs") == 0)
13277 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
13278 if (strcmp (section, ".reg-s390-prefix") == 0)
13279 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
13280 if (strcmp (section, ".reg-s390-last-break") == 0)
13281 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
13282 if (strcmp (section, ".reg-s390-system-call") == 0)
13283 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
13284 if (strcmp (section, ".reg-s390-tdb") == 0)
13285 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
13286 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
13287 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
13288 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
13289 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
13290 if (strcmp (section, ".reg-s390-gs-cb") == 0)
13291 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
13292 if (strcmp (section, ".reg-s390-gs-bc") == 0)
13293 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
13294 if (strcmp (section, ".reg-arm-vfp") == 0)
13295 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
13296 if (strcmp (section, ".reg-aarch-tls") == 0)
13297 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
13298 if (strcmp (section, ".reg-aarch-hw-break") == 0)
13299 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
13300 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
13301 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
13302 if (strcmp (section, ".reg-aarch-sve") == 0)
13303 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
13304 if (strcmp (section, ".reg-aarch-pauth") == 0)
13305 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
13306 if (strcmp (section, ".reg-aarch-mte") == 0)
13307 return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
13308 if (strcmp (section, ".reg-aarch-ssve") == 0)
13309 return elfcore_write_aarch_ssve (abfd, buf, bufsiz, data, size);
13310 if (strcmp (section, ".reg-aarch-za") == 0)
13311 return elfcore_write_aarch_za (abfd, buf, bufsiz, data, size);
13312 if (strcmp (section, ".reg-aarch-zt") == 0)
13313 return elfcore_write_aarch_zt (abfd, buf, bufsiz, data, size);
13314 if (strcmp (section, ".reg-arc-v2") == 0)
13315 return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
13316 if (strcmp (section, ".gdb-tdesc") == 0)
13317 return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
13318 if (strcmp (section, ".reg-riscv-csr") == 0)
13319 return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
13320 if (strcmp (section, ".reg-loongarch-cpucfg") == 0)
13321 return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size);
13322 if (strcmp (section, ".reg-loongarch-lbt") == 0)
13323 return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size);
13324 if (strcmp (section, ".reg-loongarch-lsx") == 0)
13325 return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
13326 if (strcmp (section, ".reg-loongarch-lasx") == 0)
13327 return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
13328 return NULL;
13331 char *
13332 elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
13333 const void *buf, int bufsiz)
13335 return elfcore_write_note (obfd, note_data, note_size,
13336 "CORE", NT_FILE, buf, bufsiz);
13339 static bool
13340 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
13341 size_t align)
13343 char *p;
13345 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
13346 gABI specifies that PT_NOTE alignment should be aligned to 4
13347 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
13348 align is less than 4, we use 4 byte alignment. */
13349 if (align < 4)
13350 align = 4;
13351 if (align != 4 && align != 8)
13352 return false;
13354 p = buf;
13355 while (p < buf + size)
13357 Elf_External_Note *xnp = (Elf_External_Note *) p;
13358 Elf_Internal_Note in;
13360 if (offsetof (Elf_External_Note, name) > buf - p + size)
13361 return false;
13363 in.type = H_GET_32 (abfd, xnp->type);
13365 in.namesz = H_GET_32 (abfd, xnp->namesz);
13366 in.namedata = xnp->name;
13367 if (in.namesz > buf - in.namedata + size)
13368 return false;
13370 in.descsz = H_GET_32 (abfd, xnp->descsz);
13371 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
13372 in.descpos = offset + (in.descdata - buf);
13373 if (in.descsz != 0
13374 && (in.descdata >= buf + size
13375 || in.descsz > buf - in.descdata + size))
13376 return false;
13378 switch (bfd_get_format (abfd))
13380 default:
13381 return true;
13383 case bfd_core:
13385 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
13386 struct
13388 const char * string;
13389 size_t len;
13390 bool (*func) (bfd *, Elf_Internal_Note *);
13392 grokers[] =
13394 GROKER_ELEMENT ("", elfcore_grok_note),
13395 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
13396 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
13397 GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
13398 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
13399 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
13400 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
13401 GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note)
13403 #undef GROKER_ELEMENT
13404 int i;
13406 for (i = ARRAY_SIZE (grokers); i--;)
13408 if (in.namesz >= grokers[i].len
13409 && strncmp (in.namedata, grokers[i].string,
13410 grokers[i].len) == 0)
13412 if (! grokers[i].func (abfd, & in))
13413 return false;
13414 break;
13417 break;
13420 case bfd_object:
13421 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
13423 if (! elfobj_grok_gnu_note (abfd, &in))
13424 return false;
13426 else if (in.namesz == sizeof "stapsdt"
13427 && strcmp (in.namedata, "stapsdt") == 0)
13429 if (! elfobj_grok_stapsdt_note (abfd, &in))
13430 return false;
13432 break;
13435 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
13438 return true;
13441 bool
13442 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
13443 size_t align)
13445 char *buf;
13447 if (size == 0 || (size + 1) == 0)
13448 return true;
13450 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
13451 return false;
13453 buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
13454 if (buf == NULL)
13455 return false;
13457 /* PR 17512: file: ec08f814
13458 0-termintate the buffer so that string searches will not overflow. */
13459 buf[size] = 0;
13461 if (!elf_parse_notes (abfd, buf, size, offset, align))
13463 free (buf);
13464 return false;
13467 free (buf);
13468 return true;
13471 /* Providing external access to the ELF program header table. */
13473 /* Return an upper bound on the number of bytes required to store a
13474 copy of ABFD's program header table entries. Return -1 if an error
13475 occurs; bfd_get_error will return an appropriate code. */
13477 long
13478 bfd_get_elf_phdr_upper_bound (bfd *abfd)
13480 if (abfd->xvec->flavour != bfd_target_elf_flavour)
13482 bfd_set_error (bfd_error_wrong_format);
13483 return -1;
13486 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
13489 /* Copy ABFD's program header table entries to *PHDRS. The entries
13490 will be stored as an array of Elf_Internal_Phdr structures, as
13491 defined in include/elf/internal.h. To find out how large the
13492 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
13494 Return the number of program header table entries read, or -1 if an
13495 error occurs; bfd_get_error will return an appropriate code. */
13498 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
13500 int num_phdrs;
13502 if (abfd->xvec->flavour != bfd_target_elf_flavour)
13504 bfd_set_error (bfd_error_wrong_format);
13505 return -1;
13508 num_phdrs = elf_elfheader (abfd)->e_phnum;
13509 if (num_phdrs != 0)
13510 memcpy (phdrs, elf_tdata (abfd)->phdr,
13511 num_phdrs * sizeof (Elf_Internal_Phdr));
13513 return num_phdrs;
13516 enum elf_reloc_type_class
13517 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
13518 const asection *rel_sec ATTRIBUTE_UNUSED,
13519 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
13521 return reloc_class_normal;
13524 /* For RELA architectures, return the relocation value for a
13525 relocation against a local symbol. */
13527 bfd_vma
13528 _bfd_elf_rela_local_sym (bfd *abfd,
13529 Elf_Internal_Sym *sym,
13530 asection **psec,
13531 Elf_Internal_Rela *rel)
13533 asection *sec = *psec;
13534 bfd_vma relocation;
13536 relocation = (sec->output_section->vma
13537 + sec->output_offset
13538 + sym->st_value);
13539 if ((sec->flags & SEC_MERGE)
13540 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
13541 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
13543 rel->r_addend =
13544 _bfd_merged_section_offset (abfd, psec,
13545 elf_section_data (sec)->sec_info,
13546 sym->st_value + rel->r_addend);
13547 if (sec != *psec)
13549 /* If we have changed the section, and our original section is
13550 marked with SEC_EXCLUDE, it means that the original
13551 SEC_MERGE section has been completely subsumed in some
13552 other SEC_MERGE section. In this case, we need to leave
13553 some info around for --emit-relocs. */
13554 if ((sec->flags & SEC_EXCLUDE) != 0)
13555 sec->kept_section = *psec;
13556 sec = *psec;
13558 rel->r_addend -= relocation;
13559 rel->r_addend += sec->output_section->vma + sec->output_offset;
13561 return relocation;
13564 bfd_vma
13565 _bfd_elf_rel_local_sym (bfd *abfd,
13566 Elf_Internal_Sym *sym,
13567 asection **psec,
13568 bfd_vma addend)
13570 asection *sec = *psec;
13572 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
13573 return sym->st_value + addend;
13575 return _bfd_merged_section_offset (abfd, psec,
13576 elf_section_data (sec)->sec_info,
13577 sym->st_value + addend);
13580 /* Adjust an address within a section. Given OFFSET within SEC, return
13581 the new offset within the section, based upon changes made to the
13582 section. Returns -1 if the offset is now invalid.
13583 The offset (in abnd out) is in target sized bytes, however big a
13584 byte may be. */
13586 bfd_vma
13587 _bfd_elf_section_offset (bfd *abfd,
13588 struct bfd_link_info *info,
13589 asection *sec,
13590 bfd_vma offset)
13592 switch (sec->sec_info_type)
13594 case SEC_INFO_TYPE_STABS:
13595 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
13596 offset);
13597 case SEC_INFO_TYPE_EH_FRAME:
13598 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
13600 default:
13601 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
13603 /* Reverse the offset. */
13604 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13605 bfd_size_type address_size = bed->s->arch_size / 8;
13607 /* address_size and sec->size are in octets. Convert
13608 to bytes before subtracting the original offset. */
13609 offset = ((sec->size - address_size)
13610 / bfd_octets_per_byte (abfd, sec) - offset);
13612 return offset;
13616 long
13617 _bfd_elf_get_synthetic_symtab (bfd *abfd,
13618 long symcount ATTRIBUTE_UNUSED,
13619 asymbol **syms ATTRIBUTE_UNUSED,
13620 long dynsymcount,
13621 asymbol **dynsyms,
13622 asymbol **ret)
13624 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13625 asection *relplt;
13626 asymbol *s;
13627 const char *relplt_name;
13628 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
13629 arelent *p;
13630 long count, i, n;
13631 size_t size;
13632 Elf_Internal_Shdr *hdr;
13633 char *names;
13634 asection *plt;
13636 *ret = NULL;
13638 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
13639 return 0;
13641 if (dynsymcount <= 0)
13642 return 0;
13644 if (!bed->plt_sym_val)
13645 return 0;
13647 relplt_name = bed->relplt_name;
13648 if (relplt_name == NULL)
13649 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
13650 relplt = bfd_get_section_by_name (abfd, relplt_name);
13651 if (relplt == NULL)
13652 return 0;
13654 hdr = &elf_section_data (relplt)->this_hdr;
13655 if (hdr->sh_link != elf_dynsymtab (abfd)
13656 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
13657 return 0;
13659 plt = bfd_get_section_by_name (abfd, ".plt");
13660 if (plt == NULL)
13661 return 0;
13663 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
13664 if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
13665 return -1;
13667 count = NUM_SHDR_ENTRIES (hdr);
13668 size = count * sizeof (asymbol);
13669 p = relplt->relocation;
13670 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13672 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
13673 if (p->addend != 0)
13675 #ifdef BFD64
13676 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
13677 #else
13678 size += sizeof ("+0x") - 1 + 8;
13679 #endif
13683 s = *ret = (asymbol *) bfd_malloc (size);
13684 if (s == NULL)
13685 return -1;
13687 names = (char *) (s + count);
13688 p = relplt->relocation;
13689 n = 0;
13690 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13692 size_t len;
13693 bfd_vma addr;
13695 addr = bed->plt_sym_val (i, plt, p);
13696 if (addr == (bfd_vma) -1)
13697 continue;
13699 *s = **p->sym_ptr_ptr;
13700 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
13701 we are defining a symbol, ensure one of them is set. */
13702 if ((s->flags & BSF_LOCAL) == 0)
13703 s->flags |= BSF_GLOBAL;
13704 s->flags |= BSF_SYNTHETIC;
13705 s->section = plt;
13706 s->value = addr - plt->vma;
13707 s->name = names;
13708 s->udata.p = NULL;
13709 len = strlen ((*p->sym_ptr_ptr)->name);
13710 memcpy (names, (*p->sym_ptr_ptr)->name, len);
13711 names += len;
13712 if (p->addend != 0)
13714 char buf[30], *a;
13716 memcpy (names, "+0x", sizeof ("+0x") - 1);
13717 names += sizeof ("+0x") - 1;
13718 bfd_sprintf_vma (abfd, buf, p->addend);
13719 for (a = buf; *a == '0'; ++a)
13721 len = strlen (a);
13722 memcpy (names, a, len);
13723 names += len;
13725 memcpy (names, "@plt", sizeof ("@plt"));
13726 names += sizeof ("@plt");
13727 ++s, ++n;
13730 return n;
13733 /* It is only used by x86-64 so far.
13734 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
13735 but current usage would allow all of _bfd_std_section to be zero. */
13736 static const asymbol lcomm_sym
13737 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
13738 asection _bfd_elf_large_com_section
13739 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
13740 "LARGE_COMMON", 0, SEC_IS_COMMON);
13742 bool
13743 _bfd_elf_final_write_processing (bfd *abfd)
13745 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
13747 i_ehdrp = elf_elfheader (abfd);
13749 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13750 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
13752 /* Set the osabi field to ELFOSABI_GNU if the binary contains
13753 SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13754 or STB_GNU_UNIQUE binding. */
13755 if (elf_tdata (abfd)->has_gnu_osabi != 0)
13757 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13758 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
13759 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
13760 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
13762 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
13763 _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13764 "and FreeBSD targets"));
13765 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
13766 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13767 "only by GNU and FreeBSD targets"));
13768 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
13769 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13770 "only by GNU and FreeBSD targets"));
13771 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
13772 _bfd_error_handler (_("GNU_RETAIN section is supported "
13773 "only by GNU and FreeBSD targets"));
13774 bfd_set_error (bfd_error_sorry);
13775 return false;
13778 return true;
13782 /* Return TRUE for ELF symbol types that represent functions.
13783 This is the default version of this function, which is sufficient for
13784 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
13786 bool
13787 _bfd_elf_is_function_type (unsigned int type)
13789 return (type == STT_FUNC
13790 || type == STT_GNU_IFUNC);
13793 /* If the ELF symbol SYM might be a function in SEC, return the
13794 function size and set *CODE_OFF to the function's entry point,
13795 otherwise return zero. */
13797 bfd_size_type
13798 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13799 bfd_vma *code_off)
13801 bfd_size_type size;
13802 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
13804 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
13805 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
13806 || sym->section != sec)
13807 return 0;
13809 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
13811 /* In theory we should check that the symbol's type satisfies
13812 _bfd_elf_is_function_type(), but there are some function-like
13813 symbols which would fail this test. (eg _start). Instead
13814 we check for hidden, local, notype symbols with zero size.
13815 This type of symbol is generated by the annobin plugin for gcc
13816 and clang, and should not be considered to be a function symbol. */
13817 if (size == 0
13818 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
13819 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
13820 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
13821 return 0;
13823 *code_off = sym->value;
13824 /* Do not return 0 for the function's size. */
13825 return size ? size : 1;
13828 /* Set to non-zero to enable some debug messages. */
13829 #define DEBUG_SECONDARY_RELOCS 0
13831 /* An internal-to-the-bfd-library only section type
13832 used to indicate a cached secondary reloc section. */
13833 #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
13835 /* Create a BFD section to hold a secondary reloc section. */
13837 bool
13838 _bfd_elf_init_secondary_reloc_section (bfd * abfd,
13839 Elf_Internal_Shdr *hdr,
13840 const char * name,
13841 unsigned int shindex)
13843 /* We only support RELA secondary relocs. */
13844 if (hdr->sh_type != SHT_RELA)
13845 return false;
13847 #if DEBUG_SECONDARY_RELOCS
13848 fprintf (stderr, "secondary reloc section %s encountered\n", name);
13849 #endif
13850 hdr->sh_type = SHT_SECONDARY_RELOC;
13851 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
13854 /* Read in any secondary relocs associated with SEC. */
13856 bool
13857 _bfd_elf_slurp_secondary_reloc_section (bfd * abfd,
13858 asection * sec,
13859 asymbol ** symbols,
13860 bool dynamic)
13862 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13863 asection * relsec;
13864 bool result = true;
13865 bfd_vma (*r_sym) (bfd_vma);
13866 ufile_ptr filesize;
13868 #if BFD_DEFAULT_TARGET_SIZE > 32
13869 if (bfd_arch_bits_per_address (abfd) != 32)
13870 r_sym = elf64_r_sym;
13871 else
13872 #endif
13873 r_sym = elf32_r_sym;
13875 if (!elf_section_data (sec)->has_secondary_relocs)
13876 return true;
13878 /* Discover if there are any secondary reloc sections
13879 associated with SEC. */
13880 filesize = bfd_get_file_size (abfd);
13881 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13883 Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
13885 if (hdr->sh_type == SHT_SECONDARY_RELOC
13886 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
13887 && (hdr->sh_entsize == ebd->s->sizeof_rel
13888 || hdr->sh_entsize == ebd->s->sizeof_rela))
13890 bfd_byte * native_relocs;
13891 bfd_byte * native_reloc;
13892 arelent * internal_relocs;
13893 arelent * internal_reloc;
13894 size_t i;
13895 unsigned int entsize;
13896 unsigned int symcount;
13897 bfd_size_type reloc_count;
13898 size_t amt;
13900 if (ebd->elf_info_to_howto == NULL)
13901 return false;
13903 #if DEBUG_SECONDARY_RELOCS
13904 fprintf (stderr, "read secondary relocs for %s from %s\n",
13905 sec->name, relsec->name);
13906 #endif
13907 entsize = hdr->sh_entsize;
13909 if (filesize != 0
13910 && ((ufile_ptr) hdr->sh_offset > filesize
13911 || hdr->sh_size > filesize - hdr->sh_offset))
13913 bfd_set_error (bfd_error_file_truncated);
13914 result = false;
13915 continue;
13918 native_relocs = bfd_malloc (hdr->sh_size);
13919 if (native_relocs == NULL)
13921 result = false;
13922 continue;
13925 reloc_count = NUM_SHDR_ENTRIES (hdr);
13926 if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
13928 free (native_relocs);
13929 bfd_set_error (bfd_error_file_too_big);
13930 result = false;
13931 continue;
13934 internal_relocs = (arelent *) bfd_alloc (abfd, amt);
13935 if (internal_relocs == NULL)
13937 free (native_relocs);
13938 result = false;
13939 continue;
13942 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
13943 || bfd_read (native_relocs, hdr->sh_size, abfd) != hdr->sh_size)
13945 free (native_relocs);
13946 /* The internal_relocs will be freed when
13947 the memory for the bfd is released. */
13948 result = false;
13949 continue;
13952 if (dynamic)
13953 symcount = bfd_get_dynamic_symcount (abfd);
13954 else
13955 symcount = bfd_get_symcount (abfd);
13957 for (i = 0, internal_reloc = internal_relocs,
13958 native_reloc = native_relocs;
13959 i < reloc_count;
13960 i++, internal_reloc++, native_reloc += entsize)
13962 bool res;
13963 Elf_Internal_Rela rela;
13965 if (entsize == ebd->s->sizeof_rel)
13966 ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
13967 else /* entsize == ebd->s->sizeof_rela */
13968 ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
13970 /* The address of an ELF reloc is section relative for an object
13971 file, and absolute for an executable file or shared library.
13972 The address of a normal BFD reloc is always section relative,
13973 and the address of a dynamic reloc is absolute.. */
13974 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
13975 internal_reloc->address = rela.r_offset;
13976 else
13977 internal_reloc->address = rela.r_offset - sec->vma;
13979 if (r_sym (rela.r_info) == STN_UNDEF)
13981 /* FIXME: This and the error case below mean that we
13982 have a symbol on relocs that is not elf_symbol_type. */
13983 internal_reloc->sym_ptr_ptr =
13984 bfd_abs_section_ptr->symbol_ptr_ptr;
13986 else if (r_sym (rela.r_info) > symcount)
13988 _bfd_error_handler
13989 /* xgettext:c-format */
13990 (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
13991 abfd, sec, i, (long) r_sym (rela.r_info));
13992 bfd_set_error (bfd_error_bad_value);
13993 internal_reloc->sym_ptr_ptr =
13994 bfd_abs_section_ptr->symbol_ptr_ptr;
13995 result = false;
13997 else
13999 asymbol **ps;
14001 ps = symbols + r_sym (rela.r_info) - 1;
14002 internal_reloc->sym_ptr_ptr = ps;
14003 /* Make sure that this symbol is not removed by strip. */
14004 (*ps)->flags |= BSF_KEEP;
14007 internal_reloc->addend = rela.r_addend;
14009 res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
14010 if (! res || internal_reloc->howto == NULL)
14012 #if DEBUG_SECONDARY_RELOCS
14013 fprintf (stderr,
14014 "there is no howto associated with reloc %lx\n",
14015 rela.r_info);
14016 #endif
14017 result = false;
14021 free (native_relocs);
14022 /* Store the internal relocs. */
14023 elf_section_data (relsec)->sec_info = internal_relocs;
14027 return result;
14030 /* Set the ELF section header fields of an output secondary reloc section. */
14032 bool
14033 _bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
14034 bfd *obfd ATTRIBUTE_UNUSED,
14035 const Elf_Internal_Shdr *isection,
14036 Elf_Internal_Shdr *osection)
14038 asection * isec;
14039 asection * osec;
14040 struct bfd_elf_section_data * esd;
14042 if (isection == NULL)
14043 return false;
14045 if (isection->sh_type != SHT_SECONDARY_RELOC)
14046 return true;
14048 isec = isection->bfd_section;
14049 if (isec == NULL)
14050 return false;
14052 osec = osection->bfd_section;
14053 if (osec == NULL)
14054 return false;
14056 esd = elf_section_data (osec);
14057 BFD_ASSERT (esd->sec_info == NULL);
14058 esd->sec_info = elf_section_data (isec)->sec_info;
14059 osection->sh_type = SHT_RELA;
14060 osection->sh_link = elf_onesymtab (obfd);
14061 if (osection->sh_link == 0)
14063 /* There is no symbol table - we are hosed... */
14064 _bfd_error_handler
14065 /* xgettext:c-format */
14066 (_("%pB(%pA): link section cannot be set"
14067 " because the output file does not have a symbol table"),
14068 obfd, osec);
14069 bfd_set_error (bfd_error_bad_value);
14070 return false;
14073 /* Find the output section that corresponds to the isection's
14074 sh_info link. */
14075 if (isection->sh_info == 0
14076 || isection->sh_info >= elf_numsections (ibfd))
14078 _bfd_error_handler
14079 /* xgettext:c-format */
14080 (_("%pB(%pA): info section index is invalid"),
14081 obfd, osec);
14082 bfd_set_error (bfd_error_bad_value);
14083 return false;
14086 isection = elf_elfsections (ibfd)[isection->sh_info];
14088 if (isection == NULL
14089 || isection->bfd_section == NULL
14090 || isection->bfd_section->output_section == NULL)
14092 _bfd_error_handler
14093 /* xgettext:c-format */
14094 (_("%pB(%pA): info section index cannot be set"
14095 " because the section is not in the output"),
14096 obfd, osec);
14097 bfd_set_error (bfd_error_bad_value);
14098 return false;
14101 esd = elf_section_data (isection->bfd_section->output_section);
14102 BFD_ASSERT (esd != NULL);
14103 osection->sh_info = esd->this_idx;
14104 esd->has_secondary_relocs = true;
14105 #if DEBUG_SECONDARY_RELOCS
14106 fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
14107 osec->name, osection->sh_link, osection->sh_info);
14108 fprintf (stderr, "mark section %s as having secondary relocs\n",
14109 bfd_section_name (isection->bfd_section->output_section));
14110 #endif
14112 return true;
14115 /* Write out a secondary reloc section.
14117 FIXME: Currently this function can result in a serious performance penalty
14118 for files with secondary relocs and lots of sections. The proper way to
14119 fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
14120 relocs together and then to have this function just walk that chain. */
14122 bool
14123 _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
14125 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
14126 bfd_vma addr_offset;
14127 asection * relsec;
14128 bfd_vma (*r_info) (bfd_vma, bfd_vma);
14129 bool result = true;
14131 if (sec == NULL)
14132 return false;
14134 #if BFD_DEFAULT_TARGET_SIZE > 32
14135 if (bfd_arch_bits_per_address (abfd) != 32)
14136 r_info = elf64_r_info;
14137 else
14138 #endif
14139 r_info = elf32_r_info;
14141 /* The address of an ELF reloc is section relative for an object
14142 file, and absolute for an executable file or shared library.
14143 The address of a BFD reloc is always section relative. */
14144 addr_offset = 0;
14145 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
14146 addr_offset = sec->vma;
14148 /* Discover if there are any secondary reloc sections
14149 associated with SEC. */
14150 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
14152 const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
14153 Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
14155 if (hdr->sh_type == SHT_RELA
14156 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
14158 asymbol * last_sym;
14159 int last_sym_idx;
14160 size_t reloc_count;
14161 size_t idx;
14162 bfd_size_type entsize;
14163 arelent * src_irel;
14164 bfd_byte * dst_rela;
14166 if (hdr->contents != NULL)
14168 _bfd_error_handler
14169 /* xgettext:c-format */
14170 (_("%pB(%pA): error: secondary reloc section processed twice"),
14171 abfd, relsec);
14172 bfd_set_error (bfd_error_bad_value);
14173 result = false;
14174 continue;
14177 entsize = hdr->sh_entsize;
14178 if (entsize == 0)
14180 _bfd_error_handler
14181 /* xgettext:c-format */
14182 (_("%pB(%pA): error: secondary reloc section"
14183 " has zero sized entries"),
14184 abfd, relsec);
14185 bfd_set_error (bfd_error_bad_value);
14186 result = false;
14187 continue;
14189 else if (entsize != ebd->s->sizeof_rel
14190 && entsize != ebd->s->sizeof_rela)
14192 _bfd_error_handler
14193 /* xgettext:c-format */
14194 (_("%pB(%pA): error: secondary reloc section"
14195 " has non-standard sized entries"),
14196 abfd, relsec);
14197 bfd_set_error (bfd_error_bad_value);
14198 result = false;
14199 continue;
14202 reloc_count = hdr->sh_size / entsize;
14203 hdr->sh_size = entsize * reloc_count;
14204 if (reloc_count == 0)
14206 _bfd_error_handler
14207 /* xgettext:c-format */
14208 (_("%pB(%pA): error: secondary reloc section is empty!"),
14209 abfd, relsec);
14210 bfd_set_error (bfd_error_bad_value);
14211 result = false;
14212 continue;
14215 hdr->contents = bfd_alloc (abfd, hdr->sh_size);
14216 if (hdr->contents == NULL)
14217 continue;
14219 #if DEBUG_SECONDARY_RELOCS
14220 fprintf (stderr, "write %u secondary relocs for %s from %s\n",
14221 reloc_count, sec->name, relsec->name);
14222 #endif
14223 last_sym = NULL;
14224 last_sym_idx = 0;
14225 dst_rela = hdr->contents;
14226 src_irel = (arelent *) esd->sec_info;
14227 if (src_irel == NULL)
14229 _bfd_error_handler
14230 /* xgettext:c-format */
14231 (_("%pB(%pA): error: internal relocs missing"
14232 " for secondary reloc section"),
14233 abfd, relsec);
14234 bfd_set_error (bfd_error_bad_value);
14235 result = false;
14236 continue;
14239 for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
14241 Elf_Internal_Rela src_rela;
14242 arelent *ptr;
14243 asymbol *sym;
14244 int n;
14246 ptr = src_irel + idx;
14247 if (ptr == NULL)
14249 _bfd_error_handler
14250 /* xgettext:c-format */
14251 (_("%pB(%pA): error: reloc table entry %zu is empty"),
14252 abfd, relsec, idx);
14253 bfd_set_error (bfd_error_bad_value);
14254 result = false;
14255 break;
14258 if (ptr->sym_ptr_ptr == NULL)
14260 /* FIXME: Is this an error ? */
14261 n = 0;
14263 else
14265 sym = *ptr->sym_ptr_ptr;
14267 if (sym == last_sym)
14268 n = last_sym_idx;
14269 else
14271 n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
14272 if (n < 0)
14274 _bfd_error_handler
14275 /* xgettext:c-format */
14276 (_("%pB(%pA): error: secondary reloc %zu"
14277 " references a missing symbol"),
14278 abfd, relsec, idx);
14279 bfd_set_error (bfd_error_bad_value);
14280 result = false;
14281 n = 0;
14284 last_sym = sym;
14285 last_sym_idx = n;
14288 if (sym->the_bfd != NULL
14289 && sym->the_bfd->xvec != abfd->xvec
14290 && ! _bfd_elf_validate_reloc (abfd, ptr))
14292 _bfd_error_handler
14293 /* xgettext:c-format */
14294 (_("%pB(%pA): error: secondary reloc %zu"
14295 " references a deleted symbol"),
14296 abfd, relsec, idx);
14297 bfd_set_error (bfd_error_bad_value);
14298 result = false;
14299 n = 0;
14303 src_rela.r_offset = ptr->address + addr_offset;
14304 if (ptr->howto == NULL)
14306 _bfd_error_handler
14307 /* xgettext:c-format */
14308 (_("%pB(%pA): error: secondary reloc %zu"
14309 " is of an unknown type"),
14310 abfd, relsec, idx);
14311 bfd_set_error (bfd_error_bad_value);
14312 result = false;
14313 src_rela.r_info = r_info (0, 0);
14315 else
14316 src_rela.r_info = r_info (n, ptr->howto->type);
14317 src_rela.r_addend = ptr->addend;
14319 if (entsize == ebd->s->sizeof_rel)
14320 ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
14321 else /* entsize == ebd->s->sizeof_rela */
14322 ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
14327 return result;