Automatic date update in version.in
[binutils-gdb.git] / bfd / elf.c
blob52a6a2f56c3837fe8955249850dca8318841e63d
1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2024 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
293 = _bfd_mmap_readonly_persistent (abfd, 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 if (shstrtab[shstrtabsize - 1] != '\0')
302 /* It is an error if a string table isn't terminated. */
303 _bfd_error_handler
304 /* xgettext:c-format */
305 (_("%pB(%pA): string table is corrupt"),
306 abfd, i_shdrp[shindex]->bfd_section);
307 return NULL;
309 i_shdrp[shindex]->contents = shstrtab;
311 return (char *) shstrtab;
314 char *
315 bfd_elf_string_from_elf_section (bfd *abfd,
316 unsigned int shindex,
317 unsigned int strindex)
319 Elf_Internal_Shdr *hdr;
321 if (strindex == 0)
322 return "";
324 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
325 return NULL;
327 hdr = elf_elfsections (abfd)[shindex];
329 if (hdr->contents == NULL)
331 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
333 /* PR 17512: file: f057ec89. */
334 /* xgettext:c-format */
335 _bfd_error_handler (_("%pB: attempt to load strings from"
336 " a non-string section (number %d)"),
337 abfd, shindex);
338 return NULL;
341 if (bfd_elf_get_str_section (abfd, shindex) == NULL)
342 return NULL;
344 else
346 /* PR 24273: The string section's contents may have already
347 been loaded elsewhere, eg because a corrupt file has the
348 string section index in the ELF header pointing at a group
349 section. So be paranoid, and test that the last byte of
350 the section is zero. */
351 if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
352 return NULL;
355 if (strindex >= hdr->sh_size)
357 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
358 _bfd_error_handler
359 /* xgettext:c-format */
360 (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
361 abfd, strindex, (uint64_t) hdr->sh_size,
362 (shindex == shstrndx && strindex == hdr->sh_name
363 ? ".shstrtab"
364 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
365 return NULL;
368 return ((char *) hdr->contents) + strindex;
371 /* Read and convert symbols to internal format.
372 SYMCOUNT specifies the number of symbols to read, starting from
373 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
374 are non-NULL, they are used to store the internal symbols, external
375 symbols, and symbol section index extensions, respectively.
376 Returns a pointer to the internal symbol buffer (malloced if necessary)
377 or NULL if there were no symbols or some kind of problem. */
379 Elf_Internal_Sym *
380 bfd_elf_get_elf_syms (bfd *ibfd,
381 Elf_Internal_Shdr *symtab_hdr,
382 size_t symcount,
383 size_t symoffset,
384 Elf_Internal_Sym *intsym_buf,
385 void *extsym_buf,
386 Elf_External_Sym_Shndx *extshndx_buf)
388 Elf_Internal_Shdr *shndx_hdr;
389 void *alloc_ext;
390 const bfd_byte *esym;
391 Elf_External_Sym_Shndx *alloc_extshndx;
392 Elf_External_Sym_Shndx *shndx;
393 Elf_Internal_Sym *alloc_intsym;
394 Elf_Internal_Sym *isym;
395 Elf_Internal_Sym *isymend;
396 const struct elf_backend_data *bed;
397 size_t extsym_size;
398 size_t amt;
399 file_ptr pos;
401 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
402 abort ();
404 if (symcount == 0)
405 return intsym_buf;
407 if (elf_use_dt_symtab_p (ibfd))
409 /* Use dynamic symbol table. */
410 if (elf_tdata (ibfd)->dt_symtab_count != symcount + symoffset)
412 bfd_set_error (bfd_error_invalid_operation);
413 return NULL;
415 return elf_tdata (ibfd)->dt_symtab + symoffset;
418 /* Normal syms might have section extension entries. */
419 shndx_hdr = NULL;
420 if (elf_symtab_shndx_list (ibfd) != NULL)
422 elf_section_list * entry;
423 Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
425 /* Find an index section that is linked to this symtab section. */
426 for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
428 /* PR 20063. */
429 if (entry->hdr.sh_link >= elf_numsections (ibfd))
430 continue;
432 if (sections[entry->hdr.sh_link] == symtab_hdr)
434 shndx_hdr = & entry->hdr;
435 break;
439 if (shndx_hdr == NULL)
441 if (symtab_hdr == &elf_symtab_hdr (ibfd))
442 /* Not really accurate, but this was how the old code used
443 to work. */
444 shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr;
445 /* Otherwise we do nothing. The assumption is that
446 the index table will not be needed. */
450 /* Read the symbols. */
451 alloc_ext = NULL;
452 alloc_extshndx = NULL;
453 alloc_intsym = NULL;
454 bed = get_elf_backend_data (ibfd);
455 extsym_size = bed->s->sizeof_sym;
456 if (_bfd_mul_overflow (symcount, extsym_size, &amt))
458 bfd_set_error (bfd_error_file_too_big);
459 return NULL;
461 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
462 size_t alloc_ext_size = amt;
463 if (bfd_seek (ibfd, pos, SEEK_SET) != 0
464 || !_bfd_mmap_read_temporary (&extsym_buf, &alloc_ext_size,
465 &alloc_ext, ibfd, false))
467 intsym_buf = NULL;
468 goto out2;
471 size_t alloc_extshndx_size = 0;
472 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
473 extshndx_buf = NULL;
474 else
476 if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
478 bfd_set_error (bfd_error_file_too_big);
479 intsym_buf = NULL;
480 goto out1;
482 alloc_extshndx_size = amt;
483 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
484 if (bfd_seek (ibfd, pos, SEEK_SET) != 0
485 || !_bfd_mmap_read_temporary ((void **) &extshndx_buf,
486 &alloc_extshndx_size,
487 (void **) &alloc_extshndx,
488 ibfd, false))
490 intsym_buf = NULL;
491 goto out1;
495 if (intsym_buf == NULL)
497 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
499 bfd_set_error (bfd_error_file_too_big);
500 goto out1;
502 alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
503 intsym_buf = alloc_intsym;
504 if (intsym_buf == NULL)
505 goto out1;
508 /* Convert the symbols to internal form. */
509 isymend = intsym_buf + symcount;
510 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
511 shndx = extshndx_buf;
512 isym < isymend;
513 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
514 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
516 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
517 /* xgettext:c-format */
518 _bfd_error_handler (_("%pB symbol number %lu references"
519 " nonexistent SHT_SYMTAB_SHNDX section"),
520 ibfd, (unsigned long) symoffset);
521 free (alloc_intsym);
522 intsym_buf = NULL;
523 goto out1;
526 out1:
527 _bfd_munmap_readonly_temporary (alloc_extshndx, alloc_extshndx_size);
528 out2:
529 _bfd_munmap_readonly_temporary (alloc_ext, alloc_ext_size);
531 return intsym_buf;
534 /* Look up a symbol name. */
535 const char *
536 bfd_elf_sym_name (bfd *abfd,
537 Elf_Internal_Shdr *symtab_hdr,
538 Elf_Internal_Sym *isym,
539 asection *sym_sec)
541 const char *name;
542 unsigned int iname = isym->st_name;
543 unsigned int shindex = symtab_hdr->sh_link;
545 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
546 /* Check for a bogus st_shndx to avoid crashing. */
547 && isym->st_shndx < elf_numsections (abfd))
549 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
550 shindex = elf_elfheader (abfd)->e_shstrndx;
553 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
554 if (name == NULL)
555 name = "(null)";
556 else if (sym_sec && *name == '\0')
557 name = bfd_section_name (sym_sec);
559 return name;
562 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
563 sections. The first element is the flags, the rest are section
564 pointers. */
566 typedef union elf_internal_group {
567 Elf_Internal_Shdr *shdr;
568 unsigned int flags;
569 } Elf_Internal_Group;
571 /* Return the name of the group signature symbol. Why isn't the
572 signature just a string? */
574 static const char *
575 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
577 Elf_Internal_Shdr *hdr;
578 unsigned char esym[sizeof (Elf64_External_Sym)];
579 Elf_External_Sym_Shndx eshndx;
580 Elf_Internal_Sym isym;
582 /* First we need to ensure the symbol table is available. Make sure
583 that it is a symbol table section. */
584 if (ghdr->sh_link >= elf_numsections (abfd))
585 return NULL;
586 hdr = elf_elfsections (abfd) [ghdr->sh_link];
587 if (hdr->sh_type != SHT_SYMTAB
588 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
589 return NULL;
591 /* Go read the symbol. */
592 hdr = &elf_tdata (abfd)->symtab_hdr;
593 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
594 &isym, esym, &eshndx) == NULL)
595 return NULL;
597 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
600 /* Set next_in_group list pointer, and group name for NEWSECT. */
602 static bool
603 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
605 unsigned int num_group = elf_tdata (abfd)->num_group;
607 /* If num_group is zero, read in all SHT_GROUP sections. The count
608 is set to -1 if there are no SHT_GROUP sections. */
609 if (num_group == 0)
611 unsigned int i, shnum;
613 /* First count the number of groups. If we have a SHT_GROUP
614 section with just a flag word (ie. sh_size is 4), ignore it. */
615 shnum = elf_numsections (abfd);
616 num_group = 0;
618 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
619 ( (shdr)->sh_type == SHT_GROUP \
620 && (shdr)->sh_size >= minsize \
621 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
622 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
624 for (i = 0; i < shnum; i++)
626 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
628 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
629 num_group += 1;
632 if (num_group == 0)
634 num_group = (unsigned) -1;
635 elf_tdata (abfd)->num_group = num_group;
636 elf_tdata (abfd)->group_sect_ptr = NULL;
638 else
640 /* We keep a list of elf section headers for group sections,
641 so we can find them quickly. */
642 size_t amt;
644 elf_tdata (abfd)->num_group = num_group;
645 amt = num_group * sizeof (Elf_Internal_Shdr *);
646 elf_tdata (abfd)->group_sect_ptr
647 = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
648 if (elf_tdata (abfd)->group_sect_ptr == NULL)
649 return false;
650 num_group = 0;
652 for (i = 0; i < shnum; i++)
654 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
656 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
658 unsigned char *src;
659 Elf_Internal_Group *dest;
661 /* Make sure the group section has a BFD section
662 attached to it. */
663 if (!bfd_section_from_shdr (abfd, i))
664 return false;
666 /* Add to list of sections. */
667 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
668 num_group += 1;
670 /* Read the raw contents. */
671 BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0);
672 shdr->contents = NULL;
673 if (_bfd_mul_overflow (shdr->sh_size,
674 sizeof (*dest) / 4, &amt)
675 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
676 || !(shdr->contents
677 = _bfd_alloc_and_read (abfd, amt, shdr->sh_size)))
679 _bfd_error_handler
680 /* xgettext:c-format */
681 (_("%pB: invalid size field in group section"
682 " header: %#" PRIx64 ""),
683 abfd, (uint64_t) shdr->sh_size);
684 bfd_set_error (bfd_error_bad_value);
685 -- num_group;
686 continue;
689 /* Translate raw contents, a flag word followed by an
690 array of elf section indices all in target byte order,
691 to the flag word followed by an array of elf section
692 pointers. */
693 src = shdr->contents + shdr->sh_size;
694 dest = (Elf_Internal_Group *) (shdr->contents + amt);
696 while (1)
698 unsigned int idx;
700 src -= 4;
701 --dest;
702 idx = H_GET_32 (abfd, src);
703 if (src == shdr->contents)
705 dest->shdr = NULL;
706 dest->flags = idx;
707 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
708 shdr->bfd_section->flags
709 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
710 break;
712 if (idx < shnum)
714 dest->shdr = elf_elfsections (abfd)[idx];
715 /* PR binutils/23199: All sections in a
716 section group should be marked with
717 SHF_GROUP. But some tools generate
718 broken objects without SHF_GROUP. Fix
719 them up here. */
720 dest->shdr->sh_flags |= SHF_GROUP;
722 if (idx >= shnum
723 || dest->shdr->sh_type == SHT_GROUP)
725 _bfd_error_handler
726 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
727 abfd, i);
728 dest->shdr = NULL;
734 /* PR 17510: Corrupt binaries might contain invalid groups. */
735 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
737 elf_tdata (abfd)->num_group = num_group;
739 /* If all groups are invalid then fail. */
740 if (num_group == 0)
742 elf_tdata (abfd)->group_sect_ptr = NULL;
743 elf_tdata (abfd)->num_group = num_group = -1;
744 _bfd_error_handler
745 (_("%pB: no valid group sections found"), abfd);
746 bfd_set_error (bfd_error_bad_value);
752 if (num_group != (unsigned) -1)
754 unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
755 unsigned int j;
757 for (j = 0; j < num_group; j++)
759 /* Begin search from previous found group. */
760 unsigned i = (j + search_offset) % num_group;
762 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
763 Elf_Internal_Group *idx;
764 bfd_size_type n_elt;
766 if (shdr == NULL)
767 continue;
769 idx = (Elf_Internal_Group *) shdr->contents;
770 if (idx == NULL || shdr->sh_size < 4)
772 /* See PR 21957 for a reproducer. */
773 /* xgettext:c-format */
774 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
775 abfd, shdr->bfd_section);
776 elf_tdata (abfd)->group_sect_ptr[i] = NULL;
777 bfd_set_error (bfd_error_bad_value);
778 return false;
780 n_elt = shdr->sh_size / 4;
782 /* Look through this group's sections to see if current
783 section is a member. */
784 while (--n_elt != 0)
785 if ((++idx)->shdr == hdr)
787 asection *s = NULL;
789 /* We are a member of this group. Go looking through
790 other members to see if any others are linked via
791 next_in_group. */
792 idx = (Elf_Internal_Group *) shdr->contents;
793 n_elt = shdr->sh_size / 4;
794 while (--n_elt != 0)
795 if ((++idx)->shdr != NULL
796 && (s = idx->shdr->bfd_section) != NULL
797 && elf_next_in_group (s) != NULL)
798 break;
799 if (n_elt != 0)
801 /* Snarf the group name from other member, and
802 insert current section in circular list. */
803 elf_group_name (newsect) = elf_group_name (s);
804 elf_next_in_group (newsect) = elf_next_in_group (s);
805 elf_next_in_group (s) = newsect;
807 else
809 const char *gname;
811 gname = group_signature (abfd, shdr);
812 if (gname == NULL)
813 return false;
814 elf_group_name (newsect) = gname;
816 /* Start a circular list with one element. */
817 elf_next_in_group (newsect) = newsect;
820 /* If the group section has been created, point to the
821 new member. */
822 if (shdr->bfd_section != NULL)
823 elf_next_in_group (shdr->bfd_section) = newsect;
825 elf_tdata (abfd)->group_search_offset = i;
826 j = num_group - 1;
827 break;
832 if (elf_group_name (newsect) == NULL)
834 /* xgettext:c-format */
835 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
836 abfd, newsect);
837 /* PR 29532: Return true here, even though the group info has not been
838 read. Separate debug info files can have empty group sections, but
839 we do not want this to prevent them from being loaded as otherwise
840 GDB will not be able to use them. */
841 return true;
843 return true;
846 bool
847 _bfd_elf_setup_sections (bfd *abfd)
849 unsigned int i;
850 unsigned int num_group = elf_tdata (abfd)->num_group;
851 bool result = true;
852 asection *s;
854 /* Process SHF_LINK_ORDER. */
855 for (s = abfd->sections; s != NULL; s = s->next)
857 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
858 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
860 unsigned int elfsec = this_hdr->sh_link;
861 /* An sh_link value of 0 is now allowed. It indicates that linked
862 to section has already been discarded, but that the current
863 section has been retained for some other reason. This linking
864 section is still a candidate for later garbage collection
865 however. */
866 if (elfsec == 0)
868 elf_linked_to_section (s) = NULL;
870 else
872 asection *linksec = NULL;
874 if (elfsec < elf_numsections (abfd))
876 this_hdr = elf_elfsections (abfd)[elfsec];
877 linksec = this_hdr->bfd_section;
880 /* PR 1991, 2008:
881 Some strip/objcopy may leave an incorrect value in
882 sh_link. We don't want to proceed. */
883 if (linksec == NULL)
885 _bfd_error_handler
886 /* xgettext:c-format */
887 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
888 s->owner, elfsec, s);
889 result = false;
892 elf_linked_to_section (s) = linksec;
895 else if (this_hdr->sh_type == SHT_GROUP
896 && elf_next_in_group (s) == NULL)
898 _bfd_error_handler
899 /* xgettext:c-format */
900 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
901 abfd, elf_section_data (s)->this_idx);
902 result = false;
906 /* Process section groups. */
907 if (num_group == (unsigned) -1)
908 return result;
910 for (i = 0; i < num_group; i++)
912 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
913 Elf_Internal_Group *idx;
914 unsigned int n_elt;
916 /* PR binutils/18758: Beware of corrupt binaries with invalid
917 group data. */
918 if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
920 _bfd_error_handler
921 /* xgettext:c-format */
922 (_("%pB: section group entry number %u is corrupt"),
923 abfd, i);
924 result = false;
925 continue;
928 idx = (Elf_Internal_Group *) shdr->contents;
929 n_elt = shdr->sh_size / 4;
931 while (--n_elt != 0)
933 ++ idx;
935 if (idx->shdr == NULL)
936 continue;
937 else if (idx->shdr->bfd_section)
938 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
939 else if (idx->shdr->sh_type != SHT_RELA
940 && idx->shdr->sh_type != SHT_REL)
942 /* There are some unknown sections in the group. */
943 _bfd_error_handler
944 /* xgettext:c-format */
945 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
946 abfd,
947 idx->shdr->sh_type,
948 bfd_elf_string_from_elf_section (abfd,
949 (elf_elfheader (abfd)
950 ->e_shstrndx),
951 idx->shdr->sh_name),
952 shdr->bfd_section);
953 result = false;
958 return result;
961 bool
962 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
964 return elf_next_in_group (sec) != NULL;
967 const char *
968 bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
970 if (elf_sec_group (sec) != NULL)
971 return elf_group_name (sec);
972 return NULL;
975 /* Make a BFD section from an ELF section. We store a pointer to the
976 BFD section in the bfd_section field of the header. */
978 bool
979 _bfd_elf_make_section_from_shdr (bfd *abfd,
980 Elf_Internal_Shdr *hdr,
981 const char *name,
982 int shindex)
984 asection *newsect;
985 flagword flags;
986 const struct elf_backend_data *bed;
987 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
989 if (hdr->bfd_section != NULL)
990 return true;
992 newsect = bfd_make_section_anyway (abfd, name);
993 if (newsect == NULL)
994 return false;
996 hdr->bfd_section = newsect;
997 elf_section_data (newsect)->this_hdr = *hdr;
998 elf_section_data (newsect)->this_idx = shindex;
1000 /* Always use the real type/flags. */
1001 elf_section_type (newsect) = hdr->sh_type;
1002 elf_section_flags (newsect) = hdr->sh_flags;
1004 newsect->filepos = hdr->sh_offset;
1006 flags = SEC_NO_FLAGS;
1007 if (hdr->sh_type != SHT_NOBITS)
1008 flags |= SEC_HAS_CONTENTS;
1009 if (hdr->sh_type == SHT_GROUP)
1010 flags |= SEC_GROUP;
1011 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1013 flags |= SEC_ALLOC;
1014 if (hdr->sh_type != SHT_NOBITS)
1015 flags |= SEC_LOAD;
1017 if ((hdr->sh_flags & SHF_WRITE) == 0)
1018 flags |= SEC_READONLY;
1019 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1020 flags |= SEC_CODE;
1021 else if ((flags & SEC_LOAD) != 0)
1022 flags |= SEC_DATA;
1023 if ((hdr->sh_flags & SHF_MERGE) != 0)
1025 flags |= SEC_MERGE;
1026 newsect->entsize = hdr->sh_entsize;
1028 if ((hdr->sh_flags & SHF_STRINGS) != 0)
1029 flags |= SEC_STRINGS;
1030 if (hdr->sh_flags & SHF_GROUP)
1031 if (!setup_group (abfd, hdr, newsect))
1032 return false;
1033 if ((hdr->sh_flags & SHF_TLS) != 0)
1034 flags |= SEC_THREAD_LOCAL;
1035 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1036 flags |= SEC_EXCLUDE;
1038 switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
1040 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1041 but binutils as of 2019-07-23 did not set the EI_OSABI header
1042 byte. */
1043 case ELFOSABI_GNU:
1044 case ELFOSABI_FREEBSD:
1045 if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
1046 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
1047 /* Fall through */
1048 case ELFOSABI_NONE:
1049 if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
1050 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1051 break;
1054 if ((flags & SEC_ALLOC) == 0)
1056 /* The debugging sections appear to be recognized only by name,
1057 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1058 if (name [0] == '.')
1060 if (startswith (name, ".debug")
1061 || startswith (name, ".gnu.debuglto_.debug_")
1062 || startswith (name, ".gnu.linkonce.wi.")
1063 || startswith (name, ".zdebug"))
1064 flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
1065 else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
1066 || startswith (name, ".note.gnu"))
1068 flags |= SEC_ELF_OCTETS;
1069 opb = 1;
1071 else if (startswith (name, ".line")
1072 || startswith (name, ".stab")
1073 || strcmp (name, ".gdb_index") == 0)
1074 flags |= SEC_DEBUGGING;
1078 if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
1079 || !bfd_set_section_size (newsect, hdr->sh_size)
1080 || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
1081 & -hdr->sh_addralign)))
1082 return false;
1084 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1085 only link a single copy of the section. This is used to support
1086 g++. g++ will emit each template expansion in its own section.
1087 The symbols will be defined as weak, so that multiple definitions
1088 are permitted. The GNU linker extension is to actually discard
1089 all but one of the sections. */
1090 if (startswith (name, ".gnu.linkonce")
1091 && elf_next_in_group (newsect) == NULL)
1092 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1094 if (!bfd_set_section_flags (newsect, flags))
1095 return false;
1097 bed = get_elf_backend_data (abfd);
1098 if (bed->elf_backend_section_flags)
1099 if (!bed->elf_backend_section_flags (hdr))
1100 return false;
1102 /* We do not parse the PT_NOTE segments as we are interested even in the
1103 separate debug info files which may have the segments offsets corrupted.
1104 PT_NOTEs from the core files are currently not parsed using BFD. */
1105 if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0)
1107 bfd_byte *contents;
1109 if (!_bfd_elf_mmap_section_contents (abfd, newsect, &contents))
1110 return false;
1112 elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1113 hdr->sh_offset, hdr->sh_addralign);
1114 _bfd_elf_munmap_section_contents (newsect, contents);
1117 if ((newsect->flags & SEC_ALLOC) != 0)
1119 Elf_Internal_Phdr *phdr;
1120 unsigned int i, nload;
1122 /* Some ELF linkers produce binaries with all the program header
1123 p_paddr fields zero. If we have such a binary with more than
1124 one PT_LOAD header, then leave the section lma equal to vma
1125 so that we don't create sections with overlapping lma. */
1126 phdr = elf_tdata (abfd)->phdr;
1127 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1128 if (phdr->p_paddr != 0)
1129 break;
1130 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1131 ++nload;
1132 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1133 return true;
1135 phdr = elf_tdata (abfd)->phdr;
1136 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1138 if (((phdr->p_type == PT_LOAD
1139 && (hdr->sh_flags & SHF_TLS) == 0)
1140 || phdr->p_type == PT_TLS)
1141 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1143 if ((newsect->flags & SEC_LOAD) == 0)
1144 newsect->lma = (phdr->p_paddr
1145 + hdr->sh_addr - phdr->p_vaddr) / opb;
1146 else
1147 /* We used to use the same adjustment for SEC_LOAD
1148 sections, but that doesn't work if the segment
1149 is packed with code from multiple VMAs.
1150 Instead we calculate the section LMA based on
1151 the segment LMA. It is assumed that the
1152 segment will contain sections with contiguous
1153 LMAs, even if the VMAs are not. */
1154 newsect->lma = (phdr->p_paddr
1155 + hdr->sh_offset - phdr->p_offset) / opb;
1157 /* With contiguous segments, we can't tell from file
1158 offsets whether a section with zero size should
1159 be placed at the end of one segment or the
1160 beginning of the next. Decide based on vaddr. */
1161 if (hdr->sh_addr >= phdr->p_vaddr
1162 && (hdr->sh_addr + hdr->sh_size
1163 <= phdr->p_vaddr + phdr->p_memsz))
1164 break;
1169 /* Compress/decompress DWARF debug sections with names: .debug_*,
1170 .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set. */
1171 if ((newsect->flags & SEC_DEBUGGING) != 0
1172 && (newsect->flags & SEC_HAS_CONTENTS) != 0
1173 && (newsect->flags & SEC_ELF_OCTETS) != 0)
1175 enum { nothing, compress, decompress } action = nothing;
1176 int compression_header_size;
1177 bfd_size_type uncompressed_size;
1178 unsigned int uncompressed_align_power;
1179 enum compression_type ch_type = ch_none;
1180 bool compressed
1181 = bfd_is_section_compressed_info (abfd, newsect,
1182 &compression_header_size,
1183 &uncompressed_size,
1184 &uncompressed_align_power,
1185 &ch_type);
1187 /* Should we decompress? */
1188 if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed)
1189 action = decompress;
1191 /* Should we compress? Or convert to a different compression? */
1192 else if ((abfd->flags & BFD_COMPRESS) != 0
1193 && newsect->size != 0
1194 && compression_header_size >= 0
1195 && uncompressed_size > 0)
1197 if (!compressed)
1198 action = compress;
1199 else
1201 enum compression_type new_ch_type = ch_none;
1202 if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
1203 new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0
1204 ? ch_compress_zstd : ch_compress_zlib);
1205 if (new_ch_type != ch_type)
1206 action = compress;
1210 if (action == compress)
1212 if (!bfd_init_section_compress_status (abfd, newsect))
1214 _bfd_error_handler
1215 /* xgettext:c-format */
1216 (_("%pB: unable to compress section %s"), abfd, name);
1217 return false;
1220 else if (action == decompress)
1222 if (!bfd_init_section_decompress_status (abfd, newsect))
1224 _bfd_error_handler
1225 /* xgettext:c-format */
1226 (_("%pB: unable to decompress section %s"), abfd, name);
1227 return false;
1229 #ifndef HAVE_ZSTD
1230 if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD)
1232 _bfd_error_handler
1233 /* xgettext:c-format */
1234 (_ ("%pB: section %s is compressed with zstd, but BFD "
1235 "is not built with zstd support"),
1236 abfd, name);
1237 newsect->compress_status = COMPRESS_SECTION_NONE;
1238 return false;
1240 #endif
1241 if (abfd->is_linker_input
1242 && name[1] == 'z')
1244 /* Rename section from .zdebug_* to .debug_* so that ld
1245 scripts will see this section as a debug section. */
1246 char *new_name = bfd_zdebug_name_to_debug (abfd, name);
1247 if (new_name == NULL)
1248 return false;
1249 bfd_rename_section (newsect, new_name);
1254 return true;
1257 const char *const bfd_elf_section_type_names[] =
1259 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1260 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1261 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1264 /* ELF relocs are against symbols. If we are producing relocatable
1265 output, and the reloc is against an external symbol, and nothing
1266 has given us any additional addend, the resulting reloc will also
1267 be against the same symbol. In such a case, we don't want to
1268 change anything about the way the reloc is handled, since it will
1269 all be done at final link time. Rather than put special case code
1270 into bfd_perform_relocation, all the reloc types use this howto
1271 function, or should call this function for relocatable output. */
1273 bfd_reloc_status_type
1274 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1275 arelent *reloc_entry,
1276 asymbol *symbol,
1277 void *data ATTRIBUTE_UNUSED,
1278 asection *input_section,
1279 bfd *output_bfd,
1280 char **error_message ATTRIBUTE_UNUSED)
1282 if (output_bfd != NULL
1283 && (symbol->flags & BSF_SECTION_SYM) == 0
1284 && (! reloc_entry->howto->partial_inplace
1285 || reloc_entry->addend == 0))
1287 reloc_entry->address += input_section->output_offset;
1288 return bfd_reloc_ok;
1291 /* In some cases the relocation should be treated as output section
1292 relative, as when linking ELF DWARF into PE COFF. Many ELF
1293 targets lack section relative relocations and instead use
1294 ordinary absolute relocations for references between DWARF
1295 sections. That is arguably a bug in those targets but it happens
1296 to work for the usual case of linking to non-loaded ELF debug
1297 sections with VMAs forced to zero. PE COFF on the other hand
1298 doesn't allow a section VMA of zero. */
1299 if (output_bfd == NULL
1300 && !reloc_entry->howto->pc_relative
1301 && (symbol->section->flags & SEC_DEBUGGING) != 0
1302 && (input_section->flags & SEC_DEBUGGING) != 0)
1303 reloc_entry->addend -= symbol->section->output_section->vma;
1305 return bfd_reloc_continue;
1308 /* Returns TRUE if section A matches section B.
1309 Names, addresses and links may be different, but everything else
1310 should be the same. */
1312 static bool
1313 section_match (const Elf_Internal_Shdr * a,
1314 const Elf_Internal_Shdr * b)
1316 if (a->sh_type != b->sh_type
1317 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1318 || a->sh_addralign != b->sh_addralign
1319 || a->sh_entsize != b->sh_entsize)
1320 return false;
1321 if (a->sh_type == SHT_SYMTAB
1322 || a->sh_type == SHT_STRTAB)
1323 return true;
1324 return a->sh_size == b->sh_size;
1327 /* Find a section in OBFD that has the same characteristics
1328 as IHEADER. Return the index of this section or SHN_UNDEF if
1329 none can be found. Check's section HINT first, as this is likely
1330 to be the correct section. */
1332 static unsigned int
1333 find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1334 const unsigned int hint)
1336 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1337 unsigned int i;
1339 BFD_ASSERT (iheader != NULL);
1341 /* See PR 20922 for a reproducer of the NULL test. */
1342 if (hint < elf_numsections (obfd)
1343 && oheaders[hint] != NULL
1344 && section_match (oheaders[hint], iheader))
1345 return hint;
1347 for (i = 1; i < elf_numsections (obfd); i++)
1349 Elf_Internal_Shdr * oheader = oheaders[i];
1351 if (oheader == NULL)
1352 continue;
1353 if (section_match (oheader, iheader))
1354 /* FIXME: Do we care if there is a potential for
1355 multiple matches ? */
1356 return i;
1359 return SHN_UNDEF;
1362 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1363 Processor specific section, based upon a matching input section.
1364 Returns TRUE upon success, FALSE otherwise. */
1366 static bool
1367 copy_special_section_fields (const bfd *ibfd,
1368 bfd *obfd,
1369 const Elf_Internal_Shdr *iheader,
1370 Elf_Internal_Shdr *oheader,
1371 const unsigned int secnum)
1373 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1374 const Elf_Internal_Shdr **iheaders
1375 = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1376 bool changed = false;
1377 unsigned int sh_link;
1379 if (oheader->sh_type == SHT_NOBITS)
1381 /* This is a feature for objcopy --only-keep-debug:
1382 When a section's type is changed to NOBITS, we preserve
1383 the sh_link and sh_info fields so that they can be
1384 matched up with the original.
1386 Note: Strictly speaking these assignments are wrong.
1387 The sh_link and sh_info fields should point to the
1388 relevent sections in the output BFD, which may not be in
1389 the same location as they were in the input BFD. But
1390 the whole point of this action is to preserve the
1391 original values of the sh_link and sh_info fields, so
1392 that they can be matched up with the section headers in
1393 the original file. So strictly speaking we may be
1394 creating an invalid ELF file, but it is only for a file
1395 that just contains debug info and only for sections
1396 without any contents. */
1397 if (oheader->sh_link == 0)
1398 oheader->sh_link = iheader->sh_link;
1399 if (oheader->sh_info == 0)
1400 oheader->sh_info = iheader->sh_info;
1401 return true;
1404 /* Allow the target a chance to decide how these fields should be set. */
1405 if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1406 iheader, oheader))
1407 return true;
1409 /* We have an iheader which might match oheader, and which has non-zero
1410 sh_info and/or sh_link fields. Attempt to follow those links and find
1411 the section in the output bfd which corresponds to the linked section
1412 in the input bfd. */
1413 if (iheader->sh_link != SHN_UNDEF)
1415 /* See PR 20931 for a reproducer. */
1416 if (iheader->sh_link >= elf_numsections (ibfd))
1418 _bfd_error_handler
1419 /* xgettext:c-format */
1420 (_("%pB: invalid sh_link field (%d) in section number %d"),
1421 ibfd, iheader->sh_link, secnum);
1422 return false;
1425 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1426 if (sh_link != SHN_UNDEF)
1428 oheader->sh_link = sh_link;
1429 changed = true;
1431 else
1432 /* FIXME: Should we install iheader->sh_link
1433 if we could not find a match ? */
1434 _bfd_error_handler
1435 /* xgettext:c-format */
1436 (_("%pB: failed to find link section for section %d"), obfd, secnum);
1439 if (iheader->sh_info)
1441 /* The sh_info field can hold arbitrary information, but if the
1442 SHF_LINK_INFO flag is set then it should be interpreted as a
1443 section index. */
1444 if (iheader->sh_flags & SHF_INFO_LINK)
1446 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1447 iheader->sh_info);
1448 if (sh_link != SHN_UNDEF)
1449 oheader->sh_flags |= SHF_INFO_LINK;
1451 else
1452 /* No idea what it means - just copy it. */
1453 sh_link = iheader->sh_info;
1455 if (sh_link != SHN_UNDEF)
1457 oheader->sh_info = sh_link;
1458 changed = true;
1460 else
1461 _bfd_error_handler
1462 /* xgettext:c-format */
1463 (_("%pB: failed to find info section for section %d"), obfd, secnum);
1466 return changed;
1469 /* Copy the program header and other data from one object module to
1470 another. */
1472 bool
1473 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1475 const Elf_Internal_Shdr **iheaders
1476 = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1477 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1478 const struct elf_backend_data *bed;
1479 unsigned int i;
1481 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1482 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1483 return true;
1485 if (!elf_flags_init (obfd))
1487 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1488 elf_flags_init (obfd) = true;
1491 elf_gp (obfd) = elf_gp (ibfd);
1493 /* Also copy the EI_OSABI field. */
1494 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1495 elf_elfheader (ibfd)->e_ident[EI_OSABI];
1497 /* If set, copy the EI_ABIVERSION field. */
1498 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1499 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1500 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1502 /* Copy object attributes. */
1503 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1505 if (iheaders == NULL || oheaders == NULL)
1506 return true;
1508 bed = get_elf_backend_data (obfd);
1510 /* Possibly copy other fields in the section header. */
1511 for (i = 1; i < elf_numsections (obfd); i++)
1513 unsigned int j;
1514 Elf_Internal_Shdr * oheader = oheaders[i];
1516 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1517 because of a special case need for generating separate debug info
1518 files. See below for more details. */
1519 if (oheader == NULL
1520 || (oheader->sh_type != SHT_NOBITS
1521 && oheader->sh_type < SHT_LOOS))
1522 continue;
1524 /* Ignore empty sections, and sections whose
1525 fields have already been initialised. */
1526 if (oheader->sh_size == 0
1527 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1528 continue;
1530 /* Scan for the matching section in the input bfd.
1531 First we try for a direct mapping between the input and
1532 output sections. */
1533 for (j = 1; j < elf_numsections (ibfd); j++)
1535 const Elf_Internal_Shdr * iheader = iheaders[j];
1537 if (iheader == NULL)
1538 continue;
1540 if (oheader->bfd_section != NULL
1541 && iheader->bfd_section != NULL
1542 && iheader->bfd_section->output_section != NULL
1543 && iheader->bfd_section->output_section == oheader->bfd_section)
1545 /* We have found a connection from the input section to
1546 the output section. Attempt to copy the header fields.
1547 If this fails then do not try any further sections -
1548 there should only be a one-to-one mapping between
1549 input and output. */
1550 if (!copy_special_section_fields (ibfd, obfd,
1551 iheader, oheader, i))
1552 j = elf_numsections (ibfd);
1553 break;
1557 if (j < elf_numsections (ibfd))
1558 continue;
1560 /* That failed. So try to deduce the corresponding input section.
1561 Unfortunately we cannot compare names as the output string table
1562 is empty, so instead we check size, address and type. */
1563 for (j = 1; j < elf_numsections (ibfd); j++)
1565 const Elf_Internal_Shdr * iheader = iheaders[j];
1567 if (iheader == NULL)
1568 continue;
1570 /* Try matching fields in the input section's header.
1571 Since --only-keep-debug turns all non-debug sections into
1572 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1573 input type. */
1574 if ((oheader->sh_type == SHT_NOBITS
1575 || iheader->sh_type == oheader->sh_type)
1576 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1577 == (oheader->sh_flags & ~ SHF_INFO_LINK)
1578 && iheader->sh_addralign == oheader->sh_addralign
1579 && iheader->sh_entsize == oheader->sh_entsize
1580 && iheader->sh_size == oheader->sh_size
1581 && iheader->sh_addr == oheader->sh_addr
1582 && (iheader->sh_info != oheader->sh_info
1583 || iheader->sh_link != oheader->sh_link))
1585 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1586 break;
1590 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1592 /* Final attempt. Call the backend copy function
1593 with a NULL input section. */
1594 (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1595 NULL, oheader);
1599 return true;
1602 static const char *
1603 get_segment_type (unsigned int p_type)
1605 const char *pt;
1606 switch (p_type)
1608 case PT_NULL: pt = "NULL"; break;
1609 case PT_LOAD: pt = "LOAD"; break;
1610 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1611 case PT_INTERP: pt = "INTERP"; break;
1612 case PT_NOTE: pt = "NOTE"; break;
1613 case PT_SHLIB: pt = "SHLIB"; break;
1614 case PT_PHDR: pt = "PHDR"; break;
1615 case PT_TLS: pt = "TLS"; break;
1616 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1617 case PT_GNU_STACK: pt = "STACK"; break;
1618 case PT_GNU_RELRO: pt = "RELRO"; break;
1619 case PT_GNU_SFRAME: pt = "SFRAME"; break;
1620 default: pt = NULL; break;
1622 return pt;
1625 /* Print out the program headers. */
1627 bool
1628 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1630 FILE *f = (FILE *) farg;
1631 Elf_Internal_Phdr *p;
1632 asection *s;
1633 bfd_byte *dynbuf = NULL;
1635 p = elf_tdata (abfd)->phdr;
1636 if (p != NULL)
1638 unsigned int i, c;
1640 fprintf (f, _("\nProgram Header:\n"));
1641 c = elf_elfheader (abfd)->e_phnum;
1642 for (i = 0; i < c; i++, p++)
1644 const char *pt = get_segment_type (p->p_type);
1645 char buf[20];
1647 if (pt == NULL)
1649 sprintf (buf, "0x%lx", p->p_type);
1650 pt = buf;
1652 fprintf (f, "%8s off 0x", pt);
1653 bfd_fprintf_vma (abfd, f, p->p_offset);
1654 fprintf (f, " vaddr 0x");
1655 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1656 fprintf (f, " paddr 0x");
1657 bfd_fprintf_vma (abfd, f, p->p_paddr);
1658 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1659 fprintf (f, " filesz 0x");
1660 bfd_fprintf_vma (abfd, f, p->p_filesz);
1661 fprintf (f, " memsz 0x");
1662 bfd_fprintf_vma (abfd, f, p->p_memsz);
1663 fprintf (f, " flags %c%c%c",
1664 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1665 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1666 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1667 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1668 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1669 fprintf (f, "\n");
1673 s = bfd_get_section_by_name (abfd, ".dynamic");
1674 if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0)
1676 unsigned int elfsec;
1677 unsigned long shlink;
1678 bfd_byte *extdyn, *extdynend;
1679 size_t extdynsize;
1680 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1682 fprintf (f, _("\nDynamic Section:\n"));
1684 if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
1685 goto error_return;
1687 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1688 if (elfsec == SHN_BAD)
1689 goto error_return;
1690 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1692 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1693 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1695 for (extdyn = dynbuf, extdynend = dynbuf + s->size;
1696 (size_t) (extdynend - extdyn) >= extdynsize;
1697 extdyn += extdynsize)
1699 Elf_Internal_Dyn dyn;
1700 const char *name = "";
1701 char ab[20];
1702 bool stringp;
1703 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1705 (*swap_dyn_in) (abfd, extdyn, &dyn);
1707 if (dyn.d_tag == DT_NULL)
1708 break;
1710 stringp = false;
1711 switch (dyn.d_tag)
1713 default:
1714 if (bed->elf_backend_get_target_dtag)
1715 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1717 if (!strcmp (name, ""))
1719 sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
1720 name = ab;
1722 break;
1724 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
1725 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1726 case DT_PLTGOT: name = "PLTGOT"; break;
1727 case DT_HASH: name = "HASH"; break;
1728 case DT_STRTAB: name = "STRTAB"; break;
1729 case DT_SYMTAB: name = "SYMTAB"; break;
1730 case DT_RELA: name = "RELA"; break;
1731 case DT_RELASZ: name = "RELASZ"; break;
1732 case DT_RELAENT: name = "RELAENT"; break;
1733 case DT_STRSZ: name = "STRSZ"; break;
1734 case DT_SYMENT: name = "SYMENT"; break;
1735 case DT_INIT: name = "INIT"; break;
1736 case DT_FINI: name = "FINI"; break;
1737 case DT_SONAME: name = "SONAME"; stringp = true; break;
1738 case DT_RPATH: name = "RPATH"; stringp = true; break;
1739 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1740 case DT_REL: name = "REL"; break;
1741 case DT_RELSZ: name = "RELSZ"; break;
1742 case DT_RELENT: name = "RELENT"; break;
1743 case DT_RELR: name = "RELR"; break;
1744 case DT_RELRSZ: name = "RELRSZ"; break;
1745 case DT_RELRENT: name = "RELRENT"; break;
1746 case DT_PLTREL: name = "PLTREL"; break;
1747 case DT_DEBUG: name = "DEBUG"; break;
1748 case DT_TEXTREL: name = "TEXTREL"; break;
1749 case DT_JMPREL: name = "JMPREL"; break;
1750 case DT_BIND_NOW: name = "BIND_NOW"; break;
1751 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1752 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1753 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1754 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1755 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
1756 case DT_FLAGS: name = "FLAGS"; break;
1757 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1758 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1759 case DT_CHECKSUM: name = "CHECKSUM"; break;
1760 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1761 case DT_MOVEENT: name = "MOVEENT"; break;
1762 case DT_MOVESZ: name = "MOVESZ"; break;
1763 case DT_FEATURE: name = "FEATURE"; break;
1764 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1765 case DT_SYMINSZ: name = "SYMINSZ"; break;
1766 case DT_SYMINENT: name = "SYMINENT"; break;
1767 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1768 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1769 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
1770 case DT_PLTPAD: name = "PLTPAD"; break;
1771 case DT_MOVETAB: name = "MOVETAB"; break;
1772 case DT_SYMINFO: name = "SYMINFO"; break;
1773 case DT_RELACOUNT: name = "RELACOUNT"; break;
1774 case DT_RELCOUNT: name = "RELCOUNT"; break;
1775 case DT_FLAGS_1: name = "FLAGS_1"; break;
1776 case DT_VERSYM: name = "VERSYM"; break;
1777 case DT_VERDEF: name = "VERDEF"; break;
1778 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1779 case DT_VERNEED: name = "VERNEED"; break;
1780 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1781 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
1782 case DT_USED: name = "USED"; break;
1783 case DT_FILTER: name = "FILTER"; stringp = true; break;
1784 case DT_GNU_HASH: name = "GNU_HASH"; break;
1787 fprintf (f, " %-20s ", name);
1788 if (! stringp)
1790 fprintf (f, "0x");
1791 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1793 else
1795 const char *string;
1796 unsigned int tagv = dyn.d_un.d_val;
1798 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1799 if (string == NULL)
1800 goto error_return;
1801 fprintf (f, "%s", string);
1803 fprintf (f, "\n");
1806 _bfd_elf_munmap_section_contents (s, dynbuf);
1807 dynbuf = NULL;
1810 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1811 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1813 if (! _bfd_elf_slurp_version_tables (abfd, false))
1814 return false;
1817 if (elf_dynverdef (abfd) != 0)
1819 Elf_Internal_Verdef *t;
1821 fprintf (f, _("\nVersion definitions:\n"));
1822 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1824 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1825 t->vd_flags, t->vd_hash,
1826 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1827 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1829 Elf_Internal_Verdaux *a;
1831 fprintf (f, "\t");
1832 for (a = t->vd_auxptr->vda_nextptr;
1833 a != NULL;
1834 a = a->vda_nextptr)
1835 fprintf (f, "%s ",
1836 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1837 fprintf (f, "\n");
1842 if (elf_dynverref (abfd) != 0)
1844 Elf_Internal_Verneed *t;
1846 fprintf (f, _("\nVersion References:\n"));
1847 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1849 Elf_Internal_Vernaux *a;
1851 fprintf (f, _(" required from %s:\n"),
1852 t->vn_filename ? t->vn_filename : "<corrupt>");
1853 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1854 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1855 a->vna_flags, a->vna_other,
1856 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1860 return true;
1862 error_return:
1863 _bfd_elf_munmap_section_contents (s, dynbuf);
1864 return false;
1867 /* Find the file offset corresponding to VMA by using the program
1868 headers. */
1870 static file_ptr
1871 offset_from_vma (Elf_Internal_Phdr *phdrs, size_t phnum, bfd_vma vma,
1872 size_t size, size_t *max_size_p)
1874 Elf_Internal_Phdr *seg;
1875 size_t i;
1877 for (seg = phdrs, i = 0; i < phnum; ++seg, ++i)
1878 if (seg->p_type == PT_LOAD
1879 && vma >= (seg->p_vaddr & -seg->p_align)
1880 && vma + size <= seg->p_vaddr + seg->p_filesz)
1882 if (max_size_p)
1883 *max_size_p = seg->p_vaddr + seg->p_filesz - vma;
1884 return vma - seg->p_vaddr + seg->p_offset;
1887 if (max_size_p)
1888 *max_size_p = 0;
1889 bfd_set_error (bfd_error_invalid_operation);
1890 return (file_ptr) -1;
1893 /* Convert hash table to internal form. */
1895 static bfd_vma *
1896 get_hash_table_data (bfd *abfd, bfd_size_type number,
1897 unsigned int ent_size, bfd_size_type filesize)
1899 unsigned char *e_data = NULL;
1900 bfd_vma *i_data = NULL;
1901 bfd_size_type size;
1902 void *e_data_addr;
1903 size_t e_data_size ATTRIBUTE_UNUSED;
1905 if (ent_size != 4 && ent_size != 8)
1906 return NULL;
1908 if ((size_t) number != number)
1910 bfd_set_error (bfd_error_file_too_big);
1911 return NULL;
1914 size = ent_size * number;
1915 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
1916 attempting to allocate memory when the read is bound to fail. */
1917 if (size > filesize
1918 || number >= ~(size_t) 0 / ent_size
1919 || number >= ~(size_t) 0 / sizeof (*i_data))
1921 bfd_set_error (bfd_error_file_too_big);
1922 return NULL;
1925 e_data = _bfd_mmap_readonly_temporary (abfd, size, &e_data_addr,
1926 &e_data_size);
1927 if (e_data == NULL)
1928 return NULL;
1930 i_data = (bfd_vma *) bfd_malloc (number * sizeof (*i_data));
1931 if (i_data == NULL)
1933 free (e_data);
1934 return NULL;
1937 if (ent_size == 4)
1938 while (number--)
1939 i_data[number] = bfd_get_32 (abfd, e_data + number * ent_size);
1940 else
1941 while (number--)
1942 i_data[number] = bfd_get_64 (abfd, e_data + number * ent_size);
1944 _bfd_munmap_readonly_temporary (e_data_addr, e_data_size);
1945 return i_data;
1948 /* Address of .MIPS.xhash section. FIXME: What is the best way to
1949 support DT_MIPS_XHASH? */
1950 #define DT_MIPS_XHASH 0x70000036
1952 /* Reconstruct dynamic symbol table from PT_DYNAMIC segment. */
1954 bool
1955 _bfd_elf_get_dynamic_symbols (bfd *abfd, Elf_Internal_Phdr *phdr,
1956 Elf_Internal_Phdr *phdrs, size_t phnum,
1957 bfd_size_type filesize)
1959 bfd_byte *extdyn, *extdynend;
1960 size_t extdynsize;
1961 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1962 bool (*swap_symbol_in) (bfd *, const void *, const void *,
1963 Elf_Internal_Sym *);
1964 Elf_Internal_Dyn dyn;
1965 bfd_vma dt_hash = 0;
1966 bfd_vma dt_gnu_hash = 0;
1967 bfd_vma dt_mips_xhash = 0;
1968 bfd_vma dt_strtab = 0;
1969 bfd_vma dt_symtab = 0;
1970 size_t dt_strsz = 0;
1971 bfd_vma dt_versym = 0;
1972 bfd_vma dt_verdef = 0;
1973 bfd_vma dt_verneed = 0;
1974 bfd_byte *dynbuf = NULL;
1975 char *strbuf = NULL;
1976 bfd_vma *gnubuckets = NULL;
1977 bfd_vma *gnuchains = NULL;
1978 bfd_vma *mipsxlat = NULL;
1979 file_ptr saved_filepos, filepos;
1980 bool res = false;
1981 size_t amt;
1982 bfd_byte *esymbuf = NULL, *esym;
1983 bfd_size_type symcount;
1984 Elf_Internal_Sym *isymbuf = NULL;
1985 Elf_Internal_Sym *isym, *isymend;
1986 bfd_byte *versym = NULL;
1987 bfd_byte *verdef = NULL;
1988 bfd_byte *verneed = NULL;
1989 size_t verdef_size = 0;
1990 size_t verneed_size = 0;
1991 size_t extsym_size;
1992 const struct elf_backend_data *bed;
1993 void *dynbuf_addr = NULL;
1994 void *esymbuf_addr = NULL;
1995 size_t dynbuf_size = 0;
1996 size_t esymbuf_size = 0;
1998 /* Return TRUE if symbol table is bad. */
1999 if (elf_bad_symtab (abfd))
2000 return true;
2002 /* Return TRUE if DT_HASH/DT_GNU_HASH have bee processed before. */
2003 if (elf_tdata (abfd)->dt_strtab != NULL)
2004 return true;
2006 bed = get_elf_backend_data (abfd);
2008 /* Save file position for elf_object_p. */
2009 saved_filepos = bfd_tell (abfd);
2011 if (bfd_seek (abfd, phdr->p_offset, SEEK_SET) != 0)
2012 goto error_return;
2014 dynbuf_size = phdr->p_filesz;
2015 dynbuf = _bfd_mmap_readonly_temporary (abfd, dynbuf_size,
2016 &dynbuf_addr, &dynbuf_size);
2017 if (dynbuf == NULL)
2018 goto error_return;
2020 extsym_size = bed->s->sizeof_sym;
2021 extdynsize = bed->s->sizeof_dyn;
2022 swap_dyn_in = bed->s->swap_dyn_in;
2024 extdyn = dynbuf;
2025 if (phdr->p_filesz < extdynsize)
2026 goto error_return;
2027 extdynend = extdyn + phdr->p_filesz;
2028 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
2030 swap_dyn_in (abfd, extdyn, &dyn);
2032 if (dyn.d_tag == DT_NULL)
2033 break;
2035 switch (dyn.d_tag)
2037 case DT_HASH:
2038 dt_hash = dyn.d_un.d_val;
2039 break;
2040 case DT_GNU_HASH:
2041 if (bed->elf_machine_code != EM_MIPS
2042 && bed->elf_machine_code != EM_MIPS_RS3_LE)
2043 dt_gnu_hash = dyn.d_un.d_val;
2044 break;
2045 case DT_STRTAB:
2046 dt_strtab = dyn.d_un.d_val;
2047 break;
2048 case DT_SYMTAB:
2049 dt_symtab = dyn.d_un.d_val;
2050 break;
2051 case DT_STRSZ:
2052 dt_strsz = dyn.d_un.d_val;
2053 break;
2054 case DT_SYMENT:
2055 if (dyn.d_un.d_val != extsym_size)
2056 goto error_return;
2057 break;
2058 case DT_VERSYM:
2059 dt_versym = dyn.d_un.d_val;
2060 break;
2061 case DT_VERDEF:
2062 dt_verdef = dyn.d_un.d_val;
2063 break;
2064 case DT_VERNEED:
2065 dt_verneed = dyn.d_un.d_val;
2066 break;
2067 default:
2068 if (dyn.d_tag == DT_MIPS_XHASH
2069 && (bed->elf_machine_code == EM_MIPS
2070 || bed->elf_machine_code == EM_MIPS_RS3_LE))
2072 dt_gnu_hash = dyn.d_un.d_val;
2073 dt_mips_xhash = dyn.d_un.d_val;
2075 break;
2079 /* Check if we can reconstruct dynamic symbol table from PT_DYNAMIC
2080 segment. */
2081 if ((!dt_hash && !dt_gnu_hash)
2082 || !dt_strtab
2083 || !dt_symtab
2084 || !dt_strsz)
2085 goto error_return;
2087 /* Get dynamic string table. */
2088 filepos = offset_from_vma (phdrs, phnum, dt_strtab, dt_strsz, NULL);
2089 if (filepos == (file_ptr) -1
2090 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2091 goto error_return;
2093 /* Dynamic string table must be valid until ABFD is closed. */
2094 strbuf = (char *) _bfd_mmap_readonly_persistent (abfd, dt_strsz);
2095 if (strbuf == NULL)
2096 goto error_return;
2097 if (strbuf[dt_strsz - 1] != 0)
2099 /* It is an error if a string table is't terminated. */
2100 _bfd_error_handler
2101 /* xgettext:c-format */
2102 (_("%pB: DT_STRTAB table is corrupt"), abfd);
2103 goto error_return;
2106 /* Get the real symbol count from DT_HASH or DT_GNU_HASH. Prefer
2107 DT_HASH since it is simpler than DT_GNU_HASH. */
2108 if (dt_hash)
2110 unsigned char nb[16];
2111 unsigned int hash_ent_size;
2113 switch (bed->elf_machine_code)
2115 case EM_ALPHA:
2116 case EM_S390:
2117 case EM_S390_OLD:
2118 if (bed->s->elfclass == ELFCLASS64)
2120 hash_ent_size = 8;
2121 break;
2123 /* FALLTHROUGH */
2124 default:
2125 hash_ent_size = 4;
2126 break;
2129 filepos = offset_from_vma (phdrs, phnum, dt_hash, sizeof (nb),
2130 NULL);
2131 if (filepos == (file_ptr) -1
2132 || bfd_seek (abfd, filepos, SEEK_SET) != 0
2133 || bfd_read (nb, 2 * hash_ent_size, abfd) != 2 * hash_ent_size)
2134 goto error_return;
2136 /* The number of dynamic symbol table entries equals the number
2137 of chains. */
2138 if (hash_ent_size == 8)
2139 symcount = bfd_get_64 (abfd, nb + hash_ent_size);
2140 else
2141 symcount = bfd_get_32 (abfd, nb + hash_ent_size);
2143 else
2145 /* For DT_GNU_HASH, only defined symbols with non-STB_LOCAL
2146 bindings are in hash table. Since in dynamic symbol table,
2147 all symbols with STB_LOCAL binding are placed before symbols
2148 with other bindings and all undefined symbols are placed
2149 before defined ones, the highest symbol index in DT_GNU_HASH
2150 is the highest dynamic symbol table index. */
2151 unsigned char nb[16];
2152 bfd_vma ngnubuckets;
2153 bfd_vma gnusymidx;
2154 size_t i, ngnuchains;
2155 bfd_vma maxchain = 0xffffffff, bitmaskwords;
2156 bfd_vma buckets_vma;
2158 filepos = offset_from_vma (phdrs, phnum, dt_gnu_hash,
2159 sizeof (nb), NULL);
2160 if (filepos == (file_ptr) -1
2161 || bfd_seek (abfd, filepos, SEEK_SET) != 0
2162 || bfd_read (nb, sizeof (nb), abfd) != sizeof (nb))
2163 goto error_return;
2165 ngnubuckets = bfd_get_32 (abfd, nb);
2166 gnusymidx = bfd_get_32 (abfd, nb + 4);
2167 bitmaskwords = bfd_get_32 (abfd, nb + 8);
2168 buckets_vma = dt_gnu_hash + 16;
2169 if (bed->s->elfclass == ELFCLASS32)
2170 buckets_vma += bitmaskwords * 4;
2171 else
2172 buckets_vma += bitmaskwords * 8;
2173 filepos = offset_from_vma (phdrs, phnum, buckets_vma, 4, NULL);
2174 if (filepos == (file_ptr) -1
2175 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2176 goto error_return;
2178 gnubuckets = get_hash_table_data (abfd, ngnubuckets, 4, filesize);
2179 if (gnubuckets == NULL)
2180 goto error_return;
2182 for (i = 0; i < ngnubuckets; i++)
2183 if (gnubuckets[i] != 0)
2185 if (gnubuckets[i] < gnusymidx)
2186 goto error_return;
2188 if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
2189 maxchain = gnubuckets[i];
2192 if (maxchain == 0xffffffff)
2194 symcount = 0;
2195 goto empty_gnu_hash;
2198 maxchain -= gnusymidx;
2199 filepos = offset_from_vma (phdrs, phnum,
2200 (buckets_vma +
2201 4 * (ngnubuckets + maxchain)),
2202 4, NULL);
2203 if (filepos == (file_ptr) -1
2204 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2205 goto error_return;
2209 if (bfd_read (nb, 4, abfd) != 4)
2210 goto error_return;
2211 ++maxchain;
2212 if (maxchain == 0)
2213 goto error_return;
2215 while ((bfd_get_32 (abfd, nb) & 1) == 0);
2217 filepos = offset_from_vma (phdrs, phnum,
2218 (buckets_vma + 4 * ngnubuckets),
2219 4, NULL);
2220 if (filepos == (file_ptr) -1
2221 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2222 goto error_return;
2224 gnuchains = get_hash_table_data (abfd, maxchain, 4, filesize);
2225 if (gnuchains == NULL)
2226 goto error_return;
2227 ngnuchains = maxchain;
2229 if (dt_mips_xhash)
2231 filepos = offset_from_vma (phdrs, phnum,
2232 (buckets_vma
2233 + 4 * (ngnubuckets + maxchain)),
2234 4, NULL);
2235 if (filepos == (file_ptr) -1
2236 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2237 goto error_return;
2239 mipsxlat = get_hash_table_data (abfd, maxchain, 4, filesize);
2240 if (mipsxlat == NULL)
2241 goto error_return;
2244 symcount = 0;
2245 for (i = 0; i < ngnubuckets; ++i)
2246 if (gnubuckets[i] != 0)
2248 bfd_vma si = gnubuckets[i];
2249 bfd_vma off = si - gnusymidx;
2252 if (mipsxlat)
2254 if (mipsxlat[off] >= symcount)
2255 symcount = mipsxlat[off] + 1;
2257 else
2259 if (si >= symcount)
2260 symcount = si + 1;
2262 si++;
2264 while (off < ngnuchains && (gnuchains[off++] & 1) == 0);
2268 /* Swap in dynamic symbol table. */
2269 if (_bfd_mul_overflow (symcount, extsym_size, &amt))
2271 bfd_set_error (bfd_error_file_too_big);
2272 goto error_return;
2275 filepos = offset_from_vma (phdrs, phnum, dt_symtab, amt, NULL);
2276 if (filepos == (file_ptr) -1
2277 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2278 goto error_return;
2279 esymbuf_size = amt;
2280 esymbuf = _bfd_mmap_readonly_temporary (abfd, esymbuf_size,
2281 &esymbuf_addr,
2282 &esymbuf_size);
2283 if (esymbuf == NULL)
2284 goto error_return;
2286 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
2288 bfd_set_error (bfd_error_file_too_big);
2289 goto error_return;
2292 /* Dynamic symbol table must be valid until ABFD is closed. */
2293 isymbuf = (Elf_Internal_Sym *) bfd_alloc (abfd, amt);
2294 if (isymbuf == NULL)
2295 goto error_return;
2297 swap_symbol_in = bed->s->swap_symbol_in;
2299 /* Convert the symbols to internal form. */
2300 isymend = isymbuf + symcount;
2301 for (esym = esymbuf, isym = isymbuf;
2302 isym < isymend;
2303 esym += extsym_size, isym++)
2304 if (!swap_symbol_in (abfd, esym, NULL, isym)
2305 || isym->st_name >= dt_strsz)
2307 bfd_set_error (bfd_error_invalid_operation);
2308 goto error_return;
2311 if (dt_versym)
2313 /* Swap in DT_VERSYM. */
2314 if (_bfd_mul_overflow (symcount, 2, &amt))
2316 bfd_set_error (bfd_error_file_too_big);
2317 goto error_return;
2320 filepos = offset_from_vma (phdrs, phnum, dt_versym, amt, NULL);
2321 if (filepos == (file_ptr) -1
2322 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2323 goto error_return;
2325 /* DT_VERSYM info must be valid until ABFD is closed. */
2326 versym = _bfd_mmap_readonly_persistent (abfd, amt);
2328 if (dt_verdef)
2330 /* Read in DT_VERDEF. */
2331 filepos = offset_from_vma (phdrs, phnum, dt_verdef,
2332 0, &verdef_size);
2333 if (filepos == (file_ptr) -1
2334 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2335 goto error_return;
2337 /* DT_VERDEF info must be valid until ABFD is closed. */
2338 verdef = _bfd_mmap_readonly_persistent (abfd, verdef_size);
2341 if (dt_verneed)
2343 /* Read in DT_VERNEED. */
2344 filepos = offset_from_vma (phdrs, phnum, dt_verneed,
2345 0, &verneed_size);
2346 if (filepos == (file_ptr) -1
2347 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2348 goto error_return;
2350 /* DT_VERNEED info must be valid until ABFD is closed. */
2351 verneed = _bfd_mmap_readonly_persistent (abfd, verneed_size);
2355 empty_gnu_hash:
2356 elf_tdata (abfd)->dt_strtab = strbuf;
2357 elf_tdata (abfd)->dt_strsz = dt_strsz;
2358 elf_tdata (abfd)->dt_symtab = isymbuf;
2359 elf_tdata (abfd)->dt_symtab_count = symcount;
2360 elf_tdata (abfd)->dt_versym = versym;
2361 elf_tdata (abfd)->dt_verdef = verdef;
2362 elf_tdata (abfd)->dt_verneed = verneed;
2363 elf_tdata (abfd)->dt_verdef_count
2364 = verdef_size / sizeof (Elf_External_Verdef);
2365 elf_tdata (abfd)->dt_verneed_count
2366 = verneed_size / sizeof (Elf_External_Verneed);
2368 res = true;
2370 error_return:
2371 /* Restore file position for elf_object_p. */
2372 if (bfd_seek (abfd, saved_filepos, SEEK_SET) != 0)
2373 res = false;
2374 _bfd_munmap_readonly_temporary (dynbuf_addr, dynbuf_size);
2375 _bfd_munmap_readonly_temporary (esymbuf_addr, esymbuf_size);
2376 free (gnubuckets);
2377 free (gnuchains);
2378 free (mipsxlat);
2379 return res;
2382 /* Reconstruct section from dynamic symbol. */
2384 asection *
2385 _bfd_elf_get_section_from_dynamic_symbol (bfd *abfd,
2386 Elf_Internal_Sym *isym)
2388 asection *sec;
2389 flagword flags;
2391 if (!elf_use_dt_symtab_p (abfd))
2392 return NULL;
2394 flags = SEC_ALLOC | SEC_LOAD;
2395 switch (ELF_ST_TYPE (isym->st_info))
2397 case STT_FUNC:
2398 case STT_GNU_IFUNC:
2399 sec = bfd_get_section_by_name (abfd, ".text");
2400 if (sec == NULL)
2401 sec = bfd_make_section_with_flags (abfd,
2402 ".text",
2403 flags | SEC_CODE);
2404 break;
2405 case STT_COMMON:
2406 sec = bfd_com_section_ptr;
2407 break;
2408 case STT_OBJECT:
2409 sec = bfd_get_section_by_name (abfd, ".data");
2410 if (sec == NULL)
2411 sec = bfd_make_section_with_flags (abfd,
2412 ".data",
2413 flags | SEC_DATA);
2414 break;
2415 case STT_TLS:
2416 sec = bfd_get_section_by_name (abfd, ".tdata");
2417 if (sec == NULL)
2418 sec = bfd_make_section_with_flags (abfd,
2419 ".tdata",
2420 (flags
2421 | SEC_DATA
2422 | SEC_THREAD_LOCAL));
2423 break;
2424 default:
2425 sec = bfd_abs_section_ptr;
2426 break;
2429 return sec;
2432 /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
2433 and return symbol version for symbol version itself. */
2435 const char *
2436 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
2437 bool base_p,
2438 bool *hidden)
2440 const char *version_string = NULL;
2441 if ((elf_dynversym (abfd) != 0
2442 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
2443 || (elf_tdata (abfd)->dt_versym != NULL
2444 && (elf_tdata (abfd)->dt_verdef != NULL
2445 || elf_tdata (abfd)->dt_verneed != NULL)))
2447 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
2449 *hidden = (vernum & VERSYM_HIDDEN) != 0;
2450 vernum &= VERSYM_VERSION;
2452 if (vernum == 0)
2453 version_string = "";
2454 else if (vernum == 1
2455 && (vernum > elf_tdata (abfd)->cverdefs
2456 || (elf_tdata (abfd)->verdef[0].vd_flags
2457 == VER_FLG_BASE)))
2458 version_string = base_p ? "Base" : "";
2459 else if (vernum <= elf_tdata (abfd)->cverdefs)
2461 const char *nodename
2462 = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
2463 version_string = "";
2464 if (base_p
2465 || nodename == NULL
2466 || symbol->name == NULL
2467 || strcmp (symbol->name, nodename) != 0)
2468 version_string = nodename;
2470 else
2472 Elf_Internal_Verneed *t;
2474 version_string = _("<corrupt>");
2475 for (t = elf_tdata (abfd)->verref;
2476 t != NULL;
2477 t = t->vn_nextref)
2479 Elf_Internal_Vernaux *a;
2481 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2483 if (a->vna_other == vernum)
2485 *hidden = true;
2486 version_string = a->vna_nodename;
2487 break;
2493 return version_string;
2496 /* Display ELF-specific fields of a symbol. */
2498 void
2499 bfd_elf_print_symbol (bfd *abfd,
2500 void *filep,
2501 asymbol *symbol,
2502 bfd_print_symbol_type how)
2504 FILE *file = (FILE *) filep;
2505 switch (how)
2507 case bfd_print_symbol_name:
2508 fprintf (file, "%s", symbol->name);
2509 break;
2510 case bfd_print_symbol_more:
2511 fprintf (file, "elf ");
2512 bfd_fprintf_vma (abfd, file, symbol->value);
2513 fprintf (file, " %x", symbol->flags);
2514 break;
2515 case bfd_print_symbol_all:
2517 const char *section_name;
2518 const char *name = NULL;
2519 const struct elf_backend_data *bed;
2520 unsigned char st_other;
2521 bfd_vma val;
2522 const char *version_string;
2523 bool hidden;
2525 section_name = symbol->section ? symbol->section->name : "(*none*)";
2527 bed = get_elf_backend_data (abfd);
2528 if (bed->elf_backend_print_symbol_all)
2529 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
2531 if (name == NULL)
2533 name = symbol->name;
2534 bfd_print_symbol_vandf (abfd, file, symbol);
2537 fprintf (file, " %s\t", section_name);
2538 /* Print the "other" value for a symbol. For common symbols,
2539 we've already printed the size; now print the alignment.
2540 For other symbols, we have no specified alignment, and
2541 we've printed the address; now print the size. */
2542 if (symbol->section && bfd_is_com_section (symbol->section))
2543 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
2544 else
2545 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
2546 bfd_fprintf_vma (abfd, file, val);
2548 /* If we have version information, print it. */
2549 version_string = _bfd_elf_get_symbol_version_string (abfd,
2550 symbol,
2551 true,
2552 &hidden);
2553 if (version_string)
2555 if (!hidden)
2556 fprintf (file, " %-11s", version_string);
2557 else
2559 int i;
2561 fprintf (file, " (%s)", version_string);
2562 for (i = 10 - strlen (version_string); i > 0; --i)
2563 putc (' ', file);
2567 /* If the st_other field is not zero, print it. */
2568 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
2570 switch (st_other)
2572 case 0: break;
2573 case STV_INTERNAL: fprintf (file, " .internal"); break;
2574 case STV_HIDDEN: fprintf (file, " .hidden"); break;
2575 case STV_PROTECTED: fprintf (file, " .protected"); break;
2576 default:
2577 /* Some other non-defined flags are also present, so print
2578 everything hex. */
2579 fprintf (file, " 0x%02x", (unsigned int) st_other);
2582 fprintf (file, " %s", name);
2584 break;
2588 /* ELF .o/exec file reading */
2590 /* Create a new bfd section from an ELF section header. */
2592 bool
2593 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
2595 Elf_Internal_Shdr *hdr;
2596 Elf_Internal_Ehdr *ehdr;
2597 const struct elf_backend_data *bed;
2598 const char *name;
2599 bool ret = true;
2601 if (shindex >= elf_numsections (abfd))
2602 return false;
2604 /* PR17512: A corrupt ELF binary might contain a loop of sections via
2605 sh_link or sh_info. Detect this here, by refusing to load a
2606 section that we are already in the process of loading. */
2607 if (elf_tdata (abfd)->being_created[shindex])
2609 _bfd_error_handler
2610 (_("%pB: warning: loop in section dependencies detected"), abfd);
2611 return false;
2613 elf_tdata (abfd)->being_created[shindex] = true;
2615 hdr = elf_elfsections (abfd)[shindex];
2616 ehdr = elf_elfheader (abfd);
2617 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2618 hdr->sh_name);
2619 if (name == NULL)
2620 goto fail;
2622 bed = get_elf_backend_data (abfd);
2623 switch (hdr->sh_type)
2625 case SHT_NULL:
2626 /* Inactive section. Throw it away. */
2627 goto success;
2629 case SHT_PROGBITS: /* Normal section with contents. */
2630 case SHT_NOBITS: /* .bss section. */
2631 case SHT_HASH: /* .hash section. */
2632 case SHT_NOTE: /* .note section. */
2633 case SHT_INIT_ARRAY: /* .init_array section. */
2634 case SHT_FINI_ARRAY: /* .fini_array section. */
2635 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
2636 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
2637 case SHT_GNU_HASH: /* .gnu.hash section. */
2638 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2639 goto success;
2641 case SHT_DYNAMIC: /* Dynamic linking information. */
2642 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2643 goto fail;
2645 if (hdr->sh_link > elf_numsections (abfd))
2647 /* PR 10478: Accept Solaris binaries with a sh_link field
2648 set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1). */
2649 switch (bfd_get_arch (abfd))
2651 case bfd_arch_i386:
2652 case bfd_arch_sparc:
2653 if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
2654 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
2655 break;
2656 /* Otherwise fall through. */
2657 default:
2658 goto fail;
2661 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2662 goto fail;
2663 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2665 Elf_Internal_Shdr *dynsymhdr;
2667 /* The shared libraries distributed with hpux11 have a bogus
2668 sh_link field for the ".dynamic" section. Find the
2669 string table for the ".dynsym" section instead. */
2670 if (elf_dynsymtab (abfd) != 0)
2672 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2673 hdr->sh_link = dynsymhdr->sh_link;
2675 else
2677 unsigned int i, num_sec;
2679 num_sec = elf_numsections (abfd);
2680 for (i = 1; i < num_sec; i++)
2682 dynsymhdr = elf_elfsections (abfd)[i];
2683 if (dynsymhdr->sh_type == SHT_DYNSYM)
2685 hdr->sh_link = dynsymhdr->sh_link;
2686 break;
2691 goto success;
2693 case SHT_SYMTAB: /* A symbol table. */
2694 if (elf_onesymtab (abfd) == shindex)
2695 goto success;
2697 if (hdr->sh_entsize != bed->s->sizeof_sym)
2698 goto fail;
2700 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2702 if (hdr->sh_size != 0)
2703 goto fail;
2704 /* Some assemblers erroneously set sh_info to one with a
2705 zero sh_size. ld sees this as a global symbol count
2706 of (unsigned) -1. Fix it here. */
2707 hdr->sh_info = 0;
2708 goto success;
2711 /* PR 18854: A binary might contain more than one symbol table.
2712 Unusual, but possible. Warn, but continue. */
2713 if (elf_onesymtab (abfd) != 0)
2715 _bfd_error_handler
2716 /* xgettext:c-format */
2717 (_("%pB: warning: multiple symbol tables detected"
2718 " - ignoring the table in section %u"),
2719 abfd, shindex);
2720 goto success;
2722 elf_onesymtab (abfd) = shindex;
2723 elf_symtab_hdr (abfd) = *hdr;
2724 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2725 abfd->flags |= HAS_SYMS;
2727 /* Sometimes a shared object will map in the symbol table. If
2728 SHF_ALLOC is set, and this is a shared object, then we also
2729 treat this section as a BFD section. We can not base the
2730 decision purely on SHF_ALLOC, because that flag is sometimes
2731 set in a relocatable object file, which would confuse the
2732 linker. */
2733 if ((hdr->sh_flags & SHF_ALLOC) != 0
2734 && (abfd->flags & DYNAMIC) != 0
2735 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2736 shindex))
2737 goto fail;
2739 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2740 can't read symbols without that section loaded as well. It
2741 is most likely specified by the next section header. */
2743 elf_section_list * entry;
2744 unsigned int i, num_sec;
2746 for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2747 if (entry->hdr.sh_link == shindex)
2748 goto success;
2750 num_sec = elf_numsections (abfd);
2751 for (i = shindex + 1; i < num_sec; i++)
2753 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2755 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2756 && hdr2->sh_link == shindex)
2757 break;
2760 if (i == num_sec)
2761 for (i = 1; i < shindex; i++)
2763 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2765 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2766 && hdr2->sh_link == shindex)
2767 break;
2770 if (i != shindex)
2771 ret = bfd_section_from_shdr (abfd, i);
2772 /* else FIXME: we have failed to find the symbol table.
2773 Should we issue an error? */
2774 goto success;
2777 case SHT_DYNSYM: /* A dynamic symbol table. */
2778 if (elf_dynsymtab (abfd) == shindex)
2779 goto success;
2781 if (hdr->sh_entsize != bed->s->sizeof_sym)
2782 goto fail;
2784 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2786 if (hdr->sh_size != 0)
2787 goto fail;
2789 /* Some linkers erroneously set sh_info to one with a
2790 zero sh_size. ld sees this as a global symbol count
2791 of (unsigned) -1. Fix it here. */
2792 hdr->sh_info = 0;
2793 goto success;
2796 /* PR 18854: A binary might contain more than one dynamic symbol table.
2797 Unusual, but possible. Warn, but continue. */
2798 if (elf_dynsymtab (abfd) != 0)
2800 _bfd_error_handler
2801 /* xgettext:c-format */
2802 (_("%pB: warning: multiple dynamic symbol tables detected"
2803 " - ignoring the table in section %u"),
2804 abfd, shindex);
2805 goto success;
2807 elf_dynsymtab (abfd) = shindex;
2808 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2809 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2810 abfd->flags |= HAS_SYMS;
2812 /* Besides being a symbol table, we also treat this as a regular
2813 section, so that objcopy can handle it. */
2814 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2815 goto success;
2817 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
2819 elf_section_list * entry;
2821 for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2822 if (entry->ndx == shindex)
2823 goto success;
2825 entry = bfd_alloc (abfd, sizeof (*entry));
2826 if (entry == NULL)
2827 goto fail;
2828 entry->ndx = shindex;
2829 entry->hdr = * hdr;
2830 entry->next = elf_symtab_shndx_list (abfd);
2831 elf_symtab_shndx_list (abfd) = entry;
2832 elf_elfsections (abfd)[shindex] = & entry->hdr;
2833 goto success;
2836 case SHT_STRTAB: /* A string table. */
2837 if (hdr->bfd_section != NULL)
2838 goto success;
2840 if (ehdr->e_shstrndx == shindex)
2842 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2843 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2844 goto success;
2847 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2849 symtab_strtab:
2850 elf_tdata (abfd)->strtab_hdr = *hdr;
2851 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2852 goto success;
2855 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2857 dynsymtab_strtab:
2858 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2859 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2860 elf_elfsections (abfd)[shindex] = hdr;
2861 /* We also treat this as a regular section, so that objcopy
2862 can handle it. */
2863 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2864 shindex);
2865 goto success;
2868 /* If the string table isn't one of the above, then treat it as a
2869 regular section. We need to scan all the headers to be sure,
2870 just in case this strtab section appeared before the above. */
2871 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2873 unsigned int i, num_sec;
2875 num_sec = elf_numsections (abfd);
2876 for (i = 1; i < num_sec; i++)
2878 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2879 if (hdr2->sh_link == shindex)
2881 /* Prevent endless recursion on broken objects. */
2882 if (i == shindex)
2883 goto fail;
2884 if (! bfd_section_from_shdr (abfd, i))
2885 goto fail;
2886 if (elf_onesymtab (abfd) == i)
2887 goto symtab_strtab;
2888 if (elf_dynsymtab (abfd) == i)
2889 goto dynsymtab_strtab;
2893 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2894 goto success;
2896 case SHT_REL:
2897 case SHT_RELA:
2898 case SHT_RELR:
2899 /* *These* do a lot of work -- but build no sections! */
2901 asection *target_sect;
2902 Elf_Internal_Shdr *hdr2, **p_hdr;
2903 unsigned int num_sec = elf_numsections (abfd);
2904 struct bfd_elf_section_data *esdt;
2905 bfd_size_type size;
2907 if (hdr->sh_type == SHT_REL)
2908 size = bed->s->sizeof_rel;
2909 else if (hdr->sh_type == SHT_RELA)
2910 size = bed->s->sizeof_rela;
2911 else
2912 size = bed->s->arch_size / 8;
2913 if (hdr->sh_entsize != size)
2914 goto fail;
2916 /* Check for a bogus link to avoid crashing. */
2917 if (hdr->sh_link >= num_sec)
2919 _bfd_error_handler
2920 /* xgettext:c-format */
2921 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2922 abfd, hdr->sh_link, name, shindex);
2923 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2924 goto success;
2927 /* Get the symbol table. */
2928 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2929 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2930 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2931 goto fail;
2933 /* If this is an alloc section in an executable or shared
2934 library, or the reloc section does not use the main symbol
2935 table we don't treat it as a reloc section. BFD can't
2936 adequately represent such a section, so at least for now,
2937 we don't try. We just present it as a normal section. We
2938 also can't use it as a reloc section if it points to the
2939 null section, an invalid section, another reloc section, or
2940 its sh_link points to the null section. */
2941 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2942 && (hdr->sh_flags & SHF_ALLOC) != 0)
2943 || (hdr->sh_flags & SHF_COMPRESSED) != 0
2944 || hdr->sh_type == SHT_RELR
2945 || hdr->sh_link == SHN_UNDEF
2946 || hdr->sh_link != elf_onesymtab (abfd)
2947 || hdr->sh_info == SHN_UNDEF
2948 || hdr->sh_info >= num_sec
2949 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2950 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2952 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2953 goto success;
2956 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2957 goto fail;
2959 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2960 if (target_sect == NULL)
2961 goto fail;
2963 esdt = elf_section_data (target_sect);
2964 if (hdr->sh_type == SHT_RELA)
2965 p_hdr = &esdt->rela.hdr;
2966 else
2967 p_hdr = &esdt->rel.hdr;
2969 /* PR 17512: file: 0b4f81b7.
2970 Also see PR 24456, for a file which deliberately has two reloc
2971 sections. */
2972 if (*p_hdr != NULL)
2974 if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
2976 _bfd_error_handler
2977 /* xgettext:c-format */
2978 (_("%pB: warning: secondary relocation section '%s' "
2979 "for section %pA found - ignoring"),
2980 abfd, name, target_sect);
2982 else
2983 esdt->has_secondary_relocs = true;
2984 goto success;
2987 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
2988 if (hdr2 == NULL)
2989 goto fail;
2990 *hdr2 = *hdr;
2991 *p_hdr = hdr2;
2992 elf_elfsections (abfd)[shindex] = hdr2;
2993 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2994 * bed->s->int_rels_per_ext_rel);
2995 target_sect->flags |= SEC_RELOC;
2996 target_sect->relocation = NULL;
2997 target_sect->rel_filepos = hdr->sh_offset;
2998 /* In the section to which the relocations apply, mark whether
2999 its relocations are of the REL or RELA variety. */
3000 if (hdr->sh_size != 0)
3002 if (hdr->sh_type == SHT_RELA)
3003 target_sect->use_rela_p = 1;
3005 abfd->flags |= HAS_RELOC;
3006 goto success;
3009 case SHT_GNU_verdef:
3010 if (hdr->sh_info != 0)
3011 elf_dynverdef (abfd) = shindex;
3012 elf_tdata (abfd)->dynverdef_hdr = *hdr;
3013 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3014 goto success;
3016 case SHT_GNU_versym:
3017 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
3018 goto fail;
3020 elf_dynversym (abfd) = shindex;
3021 elf_tdata (abfd)->dynversym_hdr = *hdr;
3022 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3023 goto success;
3025 case SHT_GNU_verneed:
3026 if (hdr->sh_info != 0)
3027 elf_dynverref (abfd) = shindex;
3028 elf_tdata (abfd)->dynverref_hdr = *hdr;
3029 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3030 goto success;
3032 case SHT_SHLIB:
3033 goto success;
3035 case SHT_GROUP:
3036 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
3037 goto fail;
3039 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
3040 goto fail;
3042 goto success;
3044 default:
3045 /* Possibly an attributes section. */
3046 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
3047 || hdr->sh_type == bed->obj_attrs_section_type)
3049 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
3050 goto fail;
3051 _bfd_elf_parse_attributes (abfd, hdr);
3052 goto success;
3055 /* Check for any processor-specific section types. */
3056 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
3057 goto success;
3059 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
3061 if ((hdr->sh_flags & SHF_ALLOC) != 0)
3062 /* FIXME: How to properly handle allocated section reserved
3063 for applications? */
3064 _bfd_error_handler
3065 /* xgettext:c-format */
3066 (_("%pB: unknown type [%#x] section `%s'"),
3067 abfd, hdr->sh_type, name);
3068 else
3070 /* Allow sections reserved for applications. */
3071 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3072 goto success;
3075 else if (hdr->sh_type >= SHT_LOPROC
3076 && hdr->sh_type <= SHT_HIPROC)
3077 /* FIXME: We should handle this section. */
3078 _bfd_error_handler
3079 /* xgettext:c-format */
3080 (_("%pB: unknown type [%#x] section `%s'"),
3081 abfd, hdr->sh_type, name);
3082 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
3084 /* Unrecognised OS-specific sections. */
3085 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
3086 /* SHF_OS_NONCONFORMING indicates that special knowledge is
3087 required to correctly process the section and the file should
3088 be rejected with an error message. */
3089 _bfd_error_handler
3090 /* xgettext:c-format */
3091 (_("%pB: unknown type [%#x] section `%s'"),
3092 abfd, hdr->sh_type, name);
3093 else
3095 /* Otherwise it should be processed. */
3096 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3097 goto success;
3100 else
3101 /* FIXME: We should handle this section. */
3102 _bfd_error_handler
3103 /* xgettext:c-format */
3104 (_("%pB: unknown type [%#x] section `%s'"),
3105 abfd, hdr->sh_type, name);
3107 goto fail;
3110 fail:
3111 ret = false;
3112 success:
3113 elf_tdata (abfd)->being_created[shindex] = false;
3114 return ret;
3117 /* Return the local symbol specified by ABFD, R_SYMNDX. */
3119 Elf_Internal_Sym *
3120 bfd_sym_from_r_symndx (struct sym_cache *cache,
3121 bfd *abfd,
3122 unsigned long r_symndx)
3124 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
3126 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
3128 Elf_Internal_Shdr *symtab_hdr;
3129 unsigned char esym[sizeof (Elf64_External_Sym)];
3130 Elf_External_Sym_Shndx eshndx;
3132 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3133 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
3134 &cache->sym[ent], esym, &eshndx) == NULL)
3135 return NULL;
3137 if (cache->abfd != abfd)
3139 memset (cache->indx, -1, sizeof (cache->indx));
3140 cache->abfd = abfd;
3142 cache->indx[ent] = r_symndx;
3145 return &cache->sym[ent];
3148 /* Given an ELF section number, retrieve the corresponding BFD
3149 section. */
3151 asection *
3152 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
3154 if (sec_index >= elf_numsections (abfd))
3155 return NULL;
3156 return elf_elfsections (abfd)[sec_index]->bfd_section;
3159 static const struct bfd_elf_special_section special_sections_b[] =
3161 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3162 { NULL, 0, 0, 0, 0 }
3165 static const struct bfd_elf_special_section special_sections_c[] =
3167 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
3168 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 },
3169 { NULL, 0, 0, 0, 0 }
3172 static const struct bfd_elf_special_section special_sections_d[] =
3174 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3175 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3176 /* There are more DWARF sections than these, but they needn't be added here
3177 unless you have to cope with broken compilers that don't emit section
3178 attributes or you want to help the user writing assembler. */
3179 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
3180 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
3181 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
3182 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
3183 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
3184 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
3185 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
3186 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
3187 { NULL, 0, 0, 0, 0 }
3190 static const struct bfd_elf_special_section special_sections_f[] =
3192 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3193 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
3194 { NULL, 0 , 0, 0, 0 }
3197 static const struct bfd_elf_special_section special_sections_g[] =
3199 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3200 { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3201 { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3202 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
3203 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3204 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
3205 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
3206 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
3207 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
3208 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
3209 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
3210 { NULL, 0, 0, 0, 0 }
3213 static const struct bfd_elf_special_section special_sections_h[] =
3215 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
3216 { NULL, 0, 0, 0, 0 }
3219 static const struct bfd_elf_special_section special_sections_i[] =
3221 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3222 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3223 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
3224 { NULL, 0, 0, 0, 0 }
3227 static const struct bfd_elf_special_section special_sections_l[] =
3229 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
3230 { NULL, 0, 0, 0, 0 }
3233 static const struct bfd_elf_special_section special_sections_n[] =
3235 { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3236 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
3237 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
3238 { NULL, 0, 0, 0, 0 }
3241 static const struct bfd_elf_special_section special_sections_p[] =
3243 { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3244 { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3245 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3246 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3247 { NULL, 0, 0, 0, 0 }
3250 static const struct bfd_elf_special_section special_sections_r[] =
3252 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
3253 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
3254 { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
3255 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
3256 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
3257 { NULL, 0, 0, 0, 0 }
3260 static const struct bfd_elf_special_section special_sections_s[] =
3262 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
3263 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
3264 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
3265 /* See struct bfd_elf_special_section declaration for the semantics of
3266 this special case where .prefix_length != strlen (.prefix). */
3267 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
3268 { NULL, 0, 0, 0, 0 }
3271 static const struct bfd_elf_special_section special_sections_t[] =
3273 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3274 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
3275 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
3276 { NULL, 0, 0, 0, 0 }
3279 static const struct bfd_elf_special_section special_sections_z[] =
3281 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
3282 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
3283 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
3284 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
3285 { NULL, 0, 0, 0, 0 }
3288 static const struct bfd_elf_special_section * const special_sections[] =
3290 special_sections_b, /* 'b' */
3291 special_sections_c, /* 'c' */
3292 special_sections_d, /* 'd' */
3293 NULL, /* 'e' */
3294 special_sections_f, /* 'f' */
3295 special_sections_g, /* 'g' */
3296 special_sections_h, /* 'h' */
3297 special_sections_i, /* 'i' */
3298 NULL, /* 'j' */
3299 NULL, /* 'k' */
3300 special_sections_l, /* 'l' */
3301 NULL, /* 'm' */
3302 special_sections_n, /* 'n' */
3303 NULL, /* 'o' */
3304 special_sections_p, /* 'p' */
3305 NULL, /* 'q' */
3306 special_sections_r, /* 'r' */
3307 special_sections_s, /* 's' */
3308 special_sections_t, /* 't' */
3309 NULL, /* 'u' */
3310 NULL, /* 'v' */
3311 NULL, /* 'w' */
3312 NULL, /* 'x' */
3313 NULL, /* 'y' */
3314 special_sections_z /* 'z' */
3317 const struct bfd_elf_special_section *
3318 _bfd_elf_get_special_section (const char *name,
3319 const struct bfd_elf_special_section *spec,
3320 unsigned int rela)
3322 int i;
3323 int len;
3325 len = strlen (name);
3327 for (i = 0; spec[i].prefix != NULL; i++)
3329 int suffix_len;
3330 int prefix_len = spec[i].prefix_length;
3332 if (len < prefix_len)
3333 continue;
3334 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
3335 continue;
3337 suffix_len = spec[i].suffix_length;
3338 if (suffix_len <= 0)
3340 if (name[prefix_len] != 0)
3342 if (suffix_len == 0)
3343 continue;
3344 if (name[prefix_len] != '.'
3345 && (suffix_len == -2
3346 || (rela && spec[i].type == SHT_REL)))
3347 continue;
3350 else
3352 if (len < prefix_len + suffix_len)
3353 continue;
3354 if (memcmp (name + len - suffix_len,
3355 spec[i].prefix + prefix_len,
3356 suffix_len) != 0)
3357 continue;
3359 return &spec[i];
3362 return NULL;
3365 const struct bfd_elf_special_section *
3366 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
3368 int i;
3369 const struct bfd_elf_special_section *spec;
3370 const struct elf_backend_data *bed;
3372 /* See if this is one of the special sections. */
3373 if (sec->name == NULL)
3374 return NULL;
3376 bed = get_elf_backend_data (abfd);
3377 spec = bed->special_sections;
3378 if (spec)
3380 spec = _bfd_elf_get_special_section (sec->name,
3381 bed->special_sections,
3382 sec->use_rela_p);
3383 if (spec != NULL)
3384 return spec;
3387 if (sec->name[0] != '.')
3388 return NULL;
3390 i = sec->name[1] - 'b';
3391 if (i < 0 || i > 'z' - 'b')
3392 return NULL;
3394 spec = special_sections[i];
3396 if (spec == NULL)
3397 return NULL;
3399 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
3402 bool
3403 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
3405 struct bfd_elf_section_data *sdata;
3406 const struct elf_backend_data *bed;
3407 const struct bfd_elf_special_section *ssect;
3409 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
3410 if (sdata == NULL)
3412 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
3413 sizeof (*sdata));
3414 if (sdata == NULL)
3415 return false;
3416 sec->used_by_bfd = sdata;
3419 /* Indicate whether or not this section should use RELA relocations. */
3420 bed = get_elf_backend_data (abfd);
3421 sec->use_rela_p = bed->default_use_rela_p;
3423 /* Set up ELF section type and flags for newly created sections, if
3424 there is an ABI mandated section. */
3425 ssect = (*bed->get_sec_type_attr) (abfd, sec);
3426 if (ssect != NULL)
3428 elf_section_type (sec) = ssect->type;
3429 elf_section_flags (sec) = ssect->attr;
3432 return _bfd_generic_new_section_hook (abfd, sec);
3435 /* Create a new bfd section from an ELF program header.
3437 Since program segments have no names, we generate a synthetic name
3438 of the form segment<NUM>, where NUM is generally the index in the
3439 program header table. For segments that are split (see below) we
3440 generate the names segment<NUM>a and segment<NUM>b.
3442 Note that some program segments may have a file size that is different than
3443 (less than) the memory size. All this means is that at execution the
3444 system must allocate the amount of memory specified by the memory size,
3445 but only initialize it with the first "file size" bytes read from the
3446 file. This would occur for example, with program segments consisting
3447 of combined data+bss.
3449 To handle the above situation, this routine generates TWO bfd sections
3450 for the single program segment. The first has the length specified by
3451 the file size of the segment, and the second has the length specified
3452 by the difference between the two sizes. In effect, the segment is split
3453 into its initialized and uninitialized parts. */
3455 bool
3456 _bfd_elf_make_section_from_phdr (bfd *abfd,
3457 Elf_Internal_Phdr *hdr,
3458 int hdr_index,
3459 const char *type_name)
3461 asection *newsect;
3462 char *name;
3463 char namebuf[64];
3464 size_t len;
3465 int split;
3466 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
3468 split = ((hdr->p_memsz > 0)
3469 && (hdr->p_filesz > 0)
3470 && (hdr->p_memsz > hdr->p_filesz));
3472 if (hdr->p_filesz > 0)
3474 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
3475 len = strlen (namebuf) + 1;
3476 name = (char *) bfd_alloc (abfd, len);
3477 if (!name)
3478 return false;
3479 memcpy (name, namebuf, len);
3480 newsect = bfd_make_section (abfd, name);
3481 if (newsect == NULL)
3482 return false;
3483 newsect->vma = hdr->p_vaddr / opb;
3484 newsect->lma = hdr->p_paddr / opb;
3485 newsect->size = hdr->p_filesz;
3486 newsect->filepos = hdr->p_offset;
3487 newsect->flags |= SEC_HAS_CONTENTS;
3488 newsect->alignment_power = bfd_log2 (hdr->p_align);
3489 if (hdr->p_type == PT_LOAD)
3491 newsect->flags |= SEC_ALLOC;
3492 newsect->flags |= SEC_LOAD;
3493 if (hdr->p_flags & PF_X)
3495 /* FIXME: all we known is that it has execute PERMISSION,
3496 may be data. */
3497 newsect->flags |= SEC_CODE;
3500 if (!(hdr->p_flags & PF_W))
3502 newsect->flags |= SEC_READONLY;
3506 if (hdr->p_memsz > hdr->p_filesz)
3508 bfd_vma align;
3510 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
3511 len = strlen (namebuf) + 1;
3512 name = (char *) bfd_alloc (abfd, len);
3513 if (!name)
3514 return false;
3515 memcpy (name, namebuf, len);
3516 newsect = bfd_make_section (abfd, name);
3517 if (newsect == NULL)
3518 return false;
3519 newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
3520 newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
3521 newsect->size = hdr->p_memsz - hdr->p_filesz;
3522 newsect->filepos = hdr->p_offset + hdr->p_filesz;
3523 align = newsect->vma & -newsect->vma;
3524 if (align == 0 || align > hdr->p_align)
3525 align = hdr->p_align;
3526 newsect->alignment_power = bfd_log2 (align);
3527 if (hdr->p_type == PT_LOAD)
3529 newsect->flags |= SEC_ALLOC;
3530 if (hdr->p_flags & PF_X)
3531 newsect->flags |= SEC_CODE;
3533 if (!(hdr->p_flags & PF_W))
3534 newsect->flags |= SEC_READONLY;
3537 return true;
3540 static bool
3541 _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3543 /* The return value is ignored. Build-ids are considered optional. */
3544 if (templ->xvec->flavour == bfd_target_elf_flavour)
3545 return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3546 (templ, offset);
3547 return false;
3550 bool
3551 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
3553 const struct elf_backend_data *bed;
3555 switch (hdr->p_type)
3557 case PT_NULL:
3558 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3560 case PT_LOAD:
3561 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3562 return false;
3563 if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3564 _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3565 return true;
3567 case PT_DYNAMIC:
3568 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3570 case PT_INTERP:
3571 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3573 case PT_NOTE:
3574 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3575 return false;
3576 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3577 hdr->p_align))
3578 return false;
3579 return true;
3581 case PT_SHLIB:
3582 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3584 case PT_PHDR:
3585 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3587 case PT_GNU_EH_FRAME:
3588 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3589 "eh_frame_hdr");
3591 case PT_GNU_STACK:
3592 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3594 case PT_GNU_RELRO:
3595 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3597 case PT_GNU_SFRAME:
3598 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3599 "sframe");
3601 default:
3602 /* Check for any processor-specific program segment types. */
3603 bed = get_elf_backend_data (abfd);
3604 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3608 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3609 REL or RELA. */
3611 Elf_Internal_Shdr *
3612 _bfd_elf_single_rel_hdr (asection *sec)
3614 if (elf_section_data (sec)->rel.hdr)
3616 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3617 return elf_section_data (sec)->rel.hdr;
3619 else
3620 return elf_section_data (sec)->rela.hdr;
3623 static bool
3624 _bfd_elf_set_reloc_sh_name (bfd *abfd,
3625 Elf_Internal_Shdr *rel_hdr,
3626 const char *sec_name,
3627 bool use_rela_p)
3629 char *name = (char *) bfd_alloc (abfd,
3630 sizeof ".rela" + strlen (sec_name));
3631 if (name == NULL)
3632 return false;
3634 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3635 rel_hdr->sh_name =
3636 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3637 false);
3638 if (rel_hdr->sh_name == (unsigned int) -1)
3639 return false;
3641 return true;
3644 /* Allocate and initialize a section-header for a new reloc section,
3645 containing relocations against ASECT. It is stored in RELDATA. If
3646 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3647 relocations. */
3649 static bool
3650 _bfd_elf_init_reloc_shdr (bfd *abfd,
3651 struct bfd_elf_section_reloc_data *reldata,
3652 const char *sec_name,
3653 bool use_rela_p,
3654 bool delay_st_name_p)
3656 Elf_Internal_Shdr *rel_hdr;
3657 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3659 BFD_ASSERT (reldata->hdr == NULL);
3660 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3661 if (rel_hdr == NULL)
3662 return false;
3663 reldata->hdr = rel_hdr;
3665 if (delay_st_name_p)
3666 rel_hdr->sh_name = (unsigned int) -1;
3667 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3668 use_rela_p))
3669 return false;
3670 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3671 rel_hdr->sh_entsize = (use_rela_p
3672 ? bed->s->sizeof_rela
3673 : bed->s->sizeof_rel);
3674 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3675 rel_hdr->sh_flags = 0;
3676 rel_hdr->sh_addr = 0;
3677 rel_hdr->sh_size = 0;
3678 rel_hdr->sh_offset = 0;
3680 return true;
3683 /* Return the default section type based on the passed in section flags. */
3686 bfd_elf_get_default_section_type (flagword flags)
3688 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3689 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3690 return SHT_NOBITS;
3691 return SHT_PROGBITS;
3694 struct fake_section_arg
3696 struct bfd_link_info *link_info;
3697 bool failed;
3700 /* Set up an ELF internal section header for a section. */
3702 static void
3703 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3705 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3706 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3707 struct bfd_elf_section_data *esd = elf_section_data (asect);
3708 Elf_Internal_Shdr *this_hdr;
3709 unsigned int sh_type;
3710 const char *name = asect->name;
3711 bool delay_st_name_p = false;
3712 bfd_vma mask;
3714 if (arg->failed)
3716 /* We already failed; just get out of the bfd_map_over_sections
3717 loop. */
3718 return;
3721 this_hdr = &esd->this_hdr;
3723 /* ld: compress DWARF debug sections with names: .debug_*. */
3724 if (arg->link_info
3725 && (abfd->flags & BFD_COMPRESS) != 0
3726 && (asect->flags & SEC_DEBUGGING) != 0
3727 && name[1] == 'd'
3728 && name[6] == '_')
3730 /* If this section will be compressed, delay adding section
3731 name to section name section after it is compressed in
3732 _bfd_elf_assign_file_positions_for_non_load. */
3733 delay_st_name_p = true;
3736 if (delay_st_name_p)
3737 this_hdr->sh_name = (unsigned int) -1;
3738 else
3740 this_hdr->sh_name
3741 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3742 name, false);
3743 if (this_hdr->sh_name == (unsigned int) -1)
3745 arg->failed = true;
3746 return;
3750 /* Don't clear sh_flags. Assembler may set additional bits. */
3752 if ((asect->flags & SEC_ALLOC) != 0
3753 || asect->user_set_vma)
3754 this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
3755 else
3756 this_hdr->sh_addr = 0;
3758 this_hdr->sh_offset = 0;
3759 this_hdr->sh_size = asect->size;
3760 this_hdr->sh_link = 0;
3761 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3762 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3764 _bfd_error_handler
3765 /* xgettext:c-format */
3766 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3767 abfd, asect->alignment_power, asect);
3768 arg->failed = true;
3769 return;
3771 /* Set sh_addralign to the highest power of two given by alignment
3772 consistent with the section VMA. Linker scripts can force VMA. */
3773 mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3774 this_hdr->sh_addralign = mask & -mask;
3775 /* The sh_entsize and sh_info fields may have been set already by
3776 copy_private_section_data. */
3778 this_hdr->bfd_section = asect;
3779 this_hdr->contents = NULL;
3781 /* If the section type is unspecified, we set it based on
3782 asect->flags. */
3783 if (asect->type != 0)
3784 sh_type = asect->type;
3785 else if ((asect->flags & SEC_GROUP) != 0)
3786 sh_type = SHT_GROUP;
3787 else
3788 sh_type = bfd_elf_get_default_section_type (asect->flags);
3790 if (this_hdr->sh_type == SHT_NULL)
3791 this_hdr->sh_type = sh_type;
3792 else if (this_hdr->sh_type == SHT_NOBITS
3793 && sh_type == SHT_PROGBITS
3794 && (asect->flags & SEC_ALLOC) != 0)
3796 /* Warn if we are changing a NOBITS section to PROGBITS, but
3797 allow the link to proceed. This can happen when users link
3798 non-bss input sections to bss output sections, or emit data
3799 to a bss output section via a linker script. */
3800 _bfd_error_handler
3801 (_("warning: section `%pA' type changed to PROGBITS"), asect);
3802 this_hdr->sh_type = sh_type;
3805 switch (this_hdr->sh_type)
3807 default:
3808 break;
3810 case SHT_STRTAB:
3811 case SHT_NOTE:
3812 case SHT_NOBITS:
3813 case SHT_PROGBITS:
3814 break;
3816 case SHT_INIT_ARRAY:
3817 case SHT_FINI_ARRAY:
3818 case SHT_PREINIT_ARRAY:
3819 this_hdr->sh_entsize = bed->s->arch_size / 8;
3820 break;
3822 case SHT_HASH:
3823 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3824 break;
3826 case SHT_DYNSYM:
3827 this_hdr->sh_entsize = bed->s->sizeof_sym;
3828 break;
3830 case SHT_DYNAMIC:
3831 this_hdr->sh_entsize = bed->s->sizeof_dyn;
3832 break;
3834 case SHT_RELA:
3835 if (get_elf_backend_data (abfd)->may_use_rela_p)
3836 this_hdr->sh_entsize = bed->s->sizeof_rela;
3837 break;
3839 case SHT_REL:
3840 if (get_elf_backend_data (abfd)->may_use_rel_p)
3841 this_hdr->sh_entsize = bed->s->sizeof_rel;
3842 break;
3844 case SHT_GNU_versym:
3845 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3846 break;
3848 case SHT_GNU_verdef:
3849 this_hdr->sh_entsize = 0;
3850 /* objcopy or strip will copy over sh_info, but may not set
3851 cverdefs. The linker will set cverdefs, but sh_info will be
3852 zero. */
3853 if (this_hdr->sh_info == 0)
3854 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3855 else
3856 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3857 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3858 break;
3860 case SHT_GNU_verneed:
3861 this_hdr->sh_entsize = 0;
3862 /* objcopy or strip will copy over sh_info, but may not set
3863 cverrefs. The linker will set cverrefs, but sh_info will be
3864 zero. */
3865 if (this_hdr->sh_info == 0)
3866 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3867 else
3868 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3869 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3870 break;
3872 case SHT_GROUP:
3873 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3874 break;
3876 case SHT_GNU_HASH:
3877 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3878 break;
3881 if ((asect->flags & SEC_ALLOC) != 0)
3882 this_hdr->sh_flags |= SHF_ALLOC;
3883 if ((asect->flags & SEC_READONLY) == 0)
3884 this_hdr->sh_flags |= SHF_WRITE;
3885 if ((asect->flags & SEC_CODE) != 0)
3886 this_hdr->sh_flags |= SHF_EXECINSTR;
3887 if ((asect->flags & SEC_MERGE) != 0)
3889 this_hdr->sh_flags |= SHF_MERGE;
3890 this_hdr->sh_entsize = asect->entsize;
3892 if ((asect->flags & SEC_STRINGS) != 0)
3893 this_hdr->sh_flags |= SHF_STRINGS;
3894 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3895 this_hdr->sh_flags |= SHF_GROUP;
3896 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3898 this_hdr->sh_flags |= SHF_TLS;
3899 if (asect->size == 0
3900 && (asect->flags & SEC_HAS_CONTENTS) == 0)
3902 struct bfd_link_order *o = asect->map_tail.link_order;
3904 this_hdr->sh_size = 0;
3905 if (o != NULL)
3907 this_hdr->sh_size = o->offset + o->size;
3908 if (this_hdr->sh_size != 0)
3909 this_hdr->sh_type = SHT_NOBITS;
3913 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3914 this_hdr->sh_flags |= SHF_EXCLUDE;
3916 /* If the section has relocs, set up a section header for the
3917 SHT_REL[A] section. If two relocation sections are required for
3918 this section, it is up to the processor-specific back-end to
3919 create the other. */
3920 if ((asect->flags & SEC_RELOC) != 0)
3922 /* When doing a relocatable link, create both REL and RELA sections if
3923 needed. */
3924 if (arg->link_info
3925 /* Do the normal setup if we wouldn't create any sections here. */
3926 && esd->rel.count + esd->rela.count > 0
3927 && (bfd_link_relocatable (arg->link_info)
3928 || arg->link_info->emitrelocations))
3930 if (esd->rel.count && esd->rel.hdr == NULL
3931 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
3932 false, delay_st_name_p))
3934 arg->failed = true;
3935 return;
3937 if (esd->rela.count && esd->rela.hdr == NULL
3938 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
3939 true, delay_st_name_p))
3941 arg->failed = true;
3942 return;
3945 else if (!_bfd_elf_init_reloc_shdr (abfd,
3946 (asect->use_rela_p
3947 ? &esd->rela : &esd->rel),
3948 name,
3949 asect->use_rela_p,
3950 delay_st_name_p))
3952 arg->failed = true;
3953 return;
3957 /* Check for processor-specific section types. */
3958 sh_type = this_hdr->sh_type;
3959 if (bed->elf_backend_fake_sections
3960 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3962 arg->failed = true;
3963 return;
3966 if (sh_type == SHT_NOBITS && asect->size != 0)
3968 /* Don't change the header type from NOBITS if we are being
3969 called for objcopy --only-keep-debug. */
3970 this_hdr->sh_type = sh_type;
3974 /* Fill in the contents of a SHT_GROUP section. Called from
3975 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3976 when ELF targets use the generic linker, ld. Called for ld -r
3977 from bfd_elf_final_link. */
3979 void
3980 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3982 bool *failedptr = (bool *) failedptrarg;
3983 asection *elt, *first;
3984 unsigned char *loc;
3985 bool gas;
3987 /* Ignore linker created group section. See elfNN_ia64_object_p in
3988 elfxx-ia64.c. */
3989 if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3990 || sec->size == 0
3991 || *failedptr)
3992 return;
3994 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3996 unsigned long symindx = 0;
3998 /* elf_group_id will have been set up by objcopy and the
3999 generic linker. */
4000 if (elf_group_id (sec) != NULL)
4001 symindx = elf_group_id (sec)->udata.i;
4003 if (symindx == 0)
4005 /* If called from the assembler, swap_out_syms will have set up
4006 elf_section_syms.
4007 PR 25699: A corrupt input file could contain bogus group info. */
4008 if (sec->index >= elf_num_section_syms (abfd)
4009 || elf_section_syms (abfd)[sec->index] == NULL)
4011 *failedptr = true;
4012 return;
4014 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
4016 elf_section_data (sec)->this_hdr.sh_info = symindx;
4018 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
4020 /* The ELF backend linker sets sh_info to -2 when the group
4021 signature symbol is global, and thus the index can't be
4022 set until all local symbols are output. */
4023 asection *igroup;
4024 struct bfd_elf_section_data *sec_data;
4025 unsigned long symndx;
4026 unsigned long extsymoff;
4027 struct elf_link_hash_entry *h;
4029 /* The point of this little dance to the first SHF_GROUP section
4030 then back to the SHT_GROUP section is that this gets us to
4031 the SHT_GROUP in the input object. */
4032 igroup = elf_sec_group (elf_next_in_group (sec));
4033 sec_data = elf_section_data (igroup);
4034 symndx = sec_data->this_hdr.sh_info;
4035 extsymoff = 0;
4036 if (!elf_bad_symtab (igroup->owner))
4038 Elf_Internal_Shdr *symtab_hdr;
4040 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
4041 extsymoff = symtab_hdr->sh_info;
4043 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
4044 while (h->root.type == bfd_link_hash_indirect
4045 || h->root.type == bfd_link_hash_warning)
4046 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4048 elf_section_data (sec)->this_hdr.sh_info = h->indx;
4051 /* The contents won't be allocated for "ld -r" or objcopy. */
4052 gas = true;
4053 if (sec->contents == NULL)
4055 gas = false;
4056 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
4058 /* Arrange for the section to be written out. */
4059 elf_section_data (sec)->this_hdr.contents = sec->contents;
4060 if (sec->contents == NULL)
4062 *failedptr = true;
4063 return;
4067 loc = sec->contents + sec->size;
4069 /* Get the pointer to the first section in the group that gas
4070 squirreled away here. objcopy arranges for this to be set to the
4071 start of the input section group. */
4072 first = elt = elf_next_in_group (sec);
4074 /* First element is a flag word. Rest of section is elf section
4075 indices for all the sections of the group. Write them backwards
4076 just to keep the group in the same order as given in .section
4077 directives, not that it matters. */
4078 while (elt != NULL)
4080 asection *s;
4082 s = elt;
4083 if (!gas)
4084 s = s->output_section;
4085 if (s != NULL
4086 && !bfd_is_abs_section (s))
4088 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
4089 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
4091 if (elf_sec->rel.hdr != NULL
4092 && (gas
4093 || (input_elf_sec->rel.hdr != NULL
4094 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
4096 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
4097 loc -= 4;
4098 if (loc == sec->contents)
4099 break;
4100 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
4102 if (elf_sec->rela.hdr != NULL
4103 && (gas
4104 || (input_elf_sec->rela.hdr != NULL
4105 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
4107 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
4108 loc -= 4;
4109 if (loc == sec->contents)
4110 break;
4111 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
4113 loc -= 4;
4114 if (loc == sec->contents)
4115 break;
4116 H_PUT_32 (abfd, elf_sec->this_idx, loc);
4118 elt = elf_next_in_group (elt);
4119 if (elt == first)
4120 break;
4123 /* We should always get here with loc == sec->contents + 4, but it is
4124 possible to craft bogus SHT_GROUP sections that will cause segfaults
4125 in objcopy without checking loc here and in the loop above. */
4126 if (loc == sec->contents)
4127 BFD_ASSERT (0);
4128 else
4130 loc -= 4;
4131 if (loc != sec->contents)
4133 BFD_ASSERT (0);
4134 memset (sec->contents + 4, 0, loc - sec->contents);
4135 loc = sec->contents;
4139 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
4142 /* Given NAME, the name of a relocation section stripped of its
4143 .rel/.rela prefix, return the section in ABFD to which the
4144 relocations apply. */
4146 asection *
4147 _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
4149 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
4150 section likely apply to .got.plt or .got section. */
4151 if (get_elf_backend_data (abfd)->want_got_plt
4152 && strcmp (name, ".plt") == 0)
4154 asection *sec;
4156 name = ".got.plt";
4157 sec = bfd_get_section_by_name (abfd, name);
4158 if (sec != NULL)
4159 return sec;
4160 name = ".got";
4163 return bfd_get_section_by_name (abfd, name);
4166 /* Return the section to which RELOC_SEC applies. */
4168 static asection *
4169 elf_get_reloc_section (asection *reloc_sec)
4171 const char *name;
4172 unsigned int type;
4173 bfd *abfd;
4174 const struct elf_backend_data *bed;
4176 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
4177 if (type != SHT_REL && type != SHT_RELA)
4178 return NULL;
4180 /* We look up the section the relocs apply to by name. */
4181 name = reloc_sec->name;
4182 if (!startswith (name, ".rel"))
4183 return NULL;
4184 name += 4;
4185 if (type == SHT_RELA && *name++ != 'a')
4186 return NULL;
4188 abfd = reloc_sec->owner;
4189 bed = get_elf_backend_data (abfd);
4190 return bed->get_reloc_section (abfd, name);
4193 /* Assign all ELF section numbers. The dummy first section is handled here
4194 too. The link/info pointers for the standard section types are filled
4195 in here too, while we're at it. LINK_INFO will be 0 when arriving
4196 here for gas, objcopy, and when using the generic ELF linker. */
4198 static bool
4199 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
4201 struct elf_obj_tdata *t = elf_tdata (abfd);
4202 asection *sec;
4203 unsigned int section_number;
4204 Elf_Internal_Shdr **i_shdrp;
4205 struct bfd_elf_section_data *d;
4206 bool need_symtab;
4207 size_t amt;
4209 section_number = 1;
4211 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
4213 /* SHT_GROUP sections are in relocatable files only. */
4214 if (link_info == NULL || !link_info->resolve_section_groups)
4216 size_t reloc_count = 0;
4218 /* Put SHT_GROUP sections first. */
4219 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4221 d = elf_section_data (sec);
4223 if (d->this_hdr.sh_type == SHT_GROUP)
4225 if (sec->flags & SEC_LINKER_CREATED)
4227 /* Remove the linker created SHT_GROUP sections. */
4228 bfd_section_list_remove (abfd, sec);
4229 abfd->section_count--;
4231 else
4232 d->this_idx = section_number++;
4235 /* Count relocations. */
4236 reloc_count += sec->reloc_count;
4239 /* Set/clear HAS_RELOC depending on whether there are relocations. */
4240 if (reloc_count == 0)
4241 abfd->flags &= ~HAS_RELOC;
4242 else
4243 abfd->flags |= HAS_RELOC;
4246 for (sec = abfd->sections; sec; sec = sec->next)
4248 d = elf_section_data (sec);
4250 if (d->this_hdr.sh_type != SHT_GROUP)
4251 d->this_idx = section_number++;
4252 if (d->this_hdr.sh_name != (unsigned int) -1)
4253 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
4254 if (d->rel.hdr)
4256 d->rel.idx = section_number++;
4257 if (d->rel.hdr->sh_name != (unsigned int) -1)
4258 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
4260 else
4261 d->rel.idx = 0;
4263 if (d->rela.hdr)
4265 d->rela.idx = section_number++;
4266 if (d->rela.hdr->sh_name != (unsigned int) -1)
4267 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
4269 else
4270 d->rela.idx = 0;
4273 need_symtab = (bfd_get_symcount (abfd) > 0
4274 || (link_info == NULL
4275 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4276 == HAS_RELOC)));
4277 if (need_symtab)
4279 elf_onesymtab (abfd) = section_number++;
4280 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4281 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
4283 elf_section_list *entry;
4285 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
4287 entry = bfd_zalloc (abfd, sizeof (*entry));
4288 entry->ndx = section_number++;
4289 elf_symtab_shndx_list (abfd) = entry;
4290 entry->hdr.sh_name
4291 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
4292 ".symtab_shndx", false);
4293 if (entry->hdr.sh_name == (unsigned int) -1)
4294 return false;
4296 elf_strtab_sec (abfd) = section_number++;
4297 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
4300 elf_shstrtab_sec (abfd) = section_number++;
4301 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
4302 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
4304 if (section_number >= SHN_LORESERVE)
4306 /* xgettext:c-format */
4307 _bfd_error_handler (_("%pB: too many sections: %u"),
4308 abfd, section_number);
4309 return false;
4312 elf_numsections (abfd) = section_number;
4313 elf_elfheader (abfd)->e_shnum = section_number;
4315 /* Set up the list of section header pointers, in agreement with the
4316 indices. */
4317 amt = section_number * sizeof (Elf_Internal_Shdr *);
4318 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
4319 if (i_shdrp == NULL)
4320 return false;
4322 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
4323 sizeof (Elf_Internal_Shdr));
4324 if (i_shdrp[0] == NULL)
4326 bfd_release (abfd, i_shdrp);
4327 return false;
4330 elf_elfsections (abfd) = i_shdrp;
4332 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
4333 if (need_symtab)
4335 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4336 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
4338 elf_section_list * entry = elf_symtab_shndx_list (abfd);
4339 BFD_ASSERT (entry != NULL);
4340 i_shdrp[entry->ndx] = & entry->hdr;
4341 entry->hdr.sh_link = elf_onesymtab (abfd);
4343 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
4344 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
4347 for (sec = abfd->sections; sec; sec = sec->next)
4349 asection *s;
4351 d = elf_section_data (sec);
4353 i_shdrp[d->this_idx] = &d->this_hdr;
4354 if (d->rel.idx != 0)
4355 i_shdrp[d->rel.idx] = d->rel.hdr;
4356 if (d->rela.idx != 0)
4357 i_shdrp[d->rela.idx] = d->rela.hdr;
4359 /* Fill in the sh_link and sh_info fields while we're at it. */
4361 /* sh_link of a reloc section is the section index of the symbol
4362 table. sh_info is the section index of the section to which
4363 the relocation entries apply. */
4364 if (d->rel.idx != 0)
4366 d->rel.hdr->sh_link = elf_onesymtab (abfd);
4367 d->rel.hdr->sh_info = d->this_idx;
4368 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
4370 if (d->rela.idx != 0)
4372 d->rela.hdr->sh_link = elf_onesymtab (abfd);
4373 d->rela.hdr->sh_info = d->this_idx;
4374 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
4377 /* We need to set up sh_link for SHF_LINK_ORDER. */
4378 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
4380 s = elf_linked_to_section (sec);
4381 /* We can now have a NULL linked section pointer.
4382 This happens when the sh_link field is 0, which is done
4383 when a linked to section is discarded but the linking
4384 section has been retained for some reason. */
4385 if (s)
4387 /* Check discarded linkonce section. */
4388 if (discarded_section (s))
4390 asection *kept;
4391 _bfd_error_handler
4392 /* xgettext:c-format */
4393 (_("%pB: sh_link of section `%pA' points to"
4394 " discarded section `%pA' of `%pB'"),
4395 abfd, d->this_hdr.bfd_section, s, s->owner);
4396 /* Point to the kept section if it has the same
4397 size as the discarded one. */
4398 kept = _bfd_elf_check_kept_section (s, link_info);
4399 if (kept == NULL)
4401 bfd_set_error (bfd_error_bad_value);
4402 return false;
4404 s = kept;
4406 /* Handle objcopy. */
4407 else if (s->output_section == NULL)
4409 _bfd_error_handler
4410 /* xgettext:c-format */
4411 (_("%pB: sh_link of section `%pA' points to"
4412 " removed section `%pA' of `%pB'"),
4413 abfd, d->this_hdr.bfd_section, s, s->owner);
4414 bfd_set_error (bfd_error_bad_value);
4415 return false;
4417 s = s->output_section;
4418 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4422 switch (d->this_hdr.sh_type)
4424 case SHT_REL:
4425 case SHT_RELA:
4426 /* sh_link is the section index of the symbol table.
4427 sh_info is the section index of the section to which the
4428 relocation entries apply. */
4429 if (d->this_hdr.sh_link == 0)
4431 /* FIXME maybe: If this is a reloc section which we are
4432 treating as a normal section then we likely should
4433 not be assuming its sh_link is .dynsym or .symtab. */
4434 if ((sec->flags & SEC_ALLOC) != 0)
4436 s = bfd_get_section_by_name (abfd, ".dynsym");
4437 if (s != NULL)
4438 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4440 else
4441 d->this_hdr.sh_link = elf_onesymtab (abfd);
4444 s = elf_get_reloc_section (sec);
4445 if (s != NULL)
4447 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
4448 d->this_hdr.sh_flags |= SHF_INFO_LINK;
4450 break;
4452 case SHT_STRTAB:
4453 /* We assume that a section named .stab*str is a stabs
4454 string section. We look for a section with the same name
4455 but without the trailing ``str'', and set its sh_link
4456 field to point to this section. */
4457 if (startswith (sec->name, ".stab")
4458 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
4460 size_t len;
4461 char *alc;
4463 len = strlen (sec->name);
4464 alc = (char *) bfd_malloc (len - 2);
4465 if (alc == NULL)
4466 return false;
4467 memcpy (alc, sec->name, len - 3);
4468 alc[len - 3] = '\0';
4469 s = bfd_get_section_by_name (abfd, alc);
4470 free (alc);
4471 if (s != NULL)
4473 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
4475 /* This is a .stab section. */
4476 elf_section_data (s)->this_hdr.sh_entsize = 12;
4479 break;
4481 case SHT_DYNAMIC:
4482 case SHT_DYNSYM:
4483 case SHT_GNU_verneed:
4484 case SHT_GNU_verdef:
4485 /* sh_link is the section header index of the string table
4486 used for the dynamic entries, or the symbol table, or the
4487 version strings. */
4488 s = bfd_get_section_by_name (abfd, ".dynstr");
4489 if (s != NULL)
4490 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4491 break;
4493 case SHT_GNU_LIBLIST:
4494 /* sh_link is the section header index of the prelink library
4495 list used for the dynamic entries, or the symbol table, or
4496 the version strings. */
4497 s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
4498 ? ".dynstr" : ".gnu.libstr"));
4499 if (s != NULL)
4500 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4501 break;
4503 case SHT_HASH:
4504 case SHT_GNU_HASH:
4505 case SHT_GNU_versym:
4506 /* sh_link is the section header index of the symbol table
4507 this hash table or version table is for. */
4508 s = bfd_get_section_by_name (abfd, ".dynsym");
4509 if (s != NULL)
4510 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4511 break;
4513 case SHT_GROUP:
4514 d->this_hdr.sh_link = elf_onesymtab (abfd);
4518 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4519 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4520 debug section name from .debug_* to .zdebug_* if needed. */
4522 return true;
4525 static bool
4526 sym_is_global (bfd *abfd, asymbol *sym)
4528 /* If the backend has a special mapping, use it. */
4529 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4530 if (bed->elf_backend_sym_is_global)
4531 return (*bed->elf_backend_sym_is_global) (abfd, sym);
4533 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
4534 || bfd_is_und_section (bfd_asymbol_section (sym))
4535 || bfd_is_com_section (bfd_asymbol_section (sym)));
4538 /* Filter global symbols of ABFD to include in the import library. All
4539 SYMCOUNT symbols of ABFD can be examined from their pointers in
4540 SYMS. Pointers of symbols to keep should be stored contiguously at
4541 the beginning of that array.
4543 Returns the number of symbols to keep. */
4545 unsigned int
4546 _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4547 asymbol **syms, long symcount)
4549 long src_count, dst_count = 0;
4551 for (src_count = 0; src_count < symcount; src_count++)
4553 asymbol *sym = syms[src_count];
4554 char *name = (char *) bfd_asymbol_name (sym);
4555 struct bfd_link_hash_entry *h;
4557 if (!sym_is_global (abfd, sym))
4558 continue;
4560 h = bfd_link_hash_lookup (info->hash, name, false, false, false);
4561 if (h == NULL)
4562 continue;
4563 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4564 continue;
4565 if (h->linker_def || h->ldscript_def)
4566 continue;
4568 syms[dst_count++] = sym;
4571 syms[dst_count] = NULL;
4573 return dst_count;
4576 /* Don't output section symbols for sections that are not going to be
4577 output, that are duplicates or there is no BFD section. */
4579 static bool
4580 ignore_section_sym (bfd *abfd, asymbol *sym)
4582 elf_symbol_type *type_ptr;
4584 if (sym == NULL)
4585 return false;
4587 if ((sym->flags & BSF_SECTION_SYM) == 0)
4588 return false;
4590 /* Ignore the section symbol if it isn't used. */
4591 if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
4592 return true;
4594 if (sym->section == NULL)
4595 return true;
4597 type_ptr = elf_symbol_from (sym);
4598 return ((type_ptr != NULL
4599 && type_ptr->internal_elf_sym.st_shndx != 0
4600 && bfd_is_abs_section (sym->section))
4601 || !(sym->section->owner == abfd
4602 || (sym->section->output_section != NULL
4603 && sym->section->output_section->owner == abfd
4604 && sym->section->output_offset == 0)
4605 || bfd_is_abs_section (sym->section)));
4608 /* Map symbol from it's internal number to the external number, moving
4609 all local symbols to be at the head of the list. */
4611 static bool
4612 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4614 unsigned int symcount = bfd_get_symcount (abfd);
4615 asymbol **syms = bfd_get_outsymbols (abfd);
4616 asymbol **sect_syms;
4617 unsigned int num_locals = 0;
4618 unsigned int num_globals = 0;
4619 unsigned int num_locals2 = 0;
4620 unsigned int num_globals2 = 0;
4621 unsigned int max_index = 0;
4622 unsigned int idx;
4623 asection *asect;
4624 asymbol **new_syms;
4625 size_t amt;
4627 #ifdef DEBUG
4628 fprintf (stderr, "elf_map_symbols\n");
4629 fflush (stderr);
4630 #endif
4632 for (asect = abfd->sections; asect; asect = asect->next)
4634 if (max_index < asect->index)
4635 max_index = asect->index;
4638 max_index++;
4639 amt = max_index * sizeof (asymbol *);
4640 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
4641 if (sect_syms == NULL)
4642 return false;
4643 elf_section_syms (abfd) = sect_syms;
4644 elf_num_section_syms (abfd) = max_index;
4646 /* Init sect_syms entries for any section symbols we have already
4647 decided to output. */
4648 for (idx = 0; idx < symcount; idx++)
4650 asymbol *sym = syms[idx];
4652 if ((sym->flags & BSF_SECTION_SYM) != 0
4653 && sym->value == 0
4654 && !ignore_section_sym (abfd, sym)
4655 && !bfd_is_abs_section (sym->section))
4657 asection *sec = sym->section;
4659 if (sec->owner != abfd)
4660 sec = sec->output_section;
4662 sect_syms[sec->index] = syms[idx];
4666 /* Classify all of the symbols. */
4667 for (idx = 0; idx < symcount; idx++)
4669 if (sym_is_global (abfd, syms[idx]))
4670 num_globals++;
4671 else if (!ignore_section_sym (abfd, syms[idx]))
4672 num_locals++;
4675 /* We will be adding a section symbol for each normal BFD section. Most
4676 sections will already have a section symbol in outsymbols, but
4677 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4678 at least in that case. */
4679 for (asect = abfd->sections; asect; asect = asect->next)
4681 asymbol *sym = asect->symbol;
4682 /* Don't include ignored section symbols. */
4683 if (!ignore_section_sym (abfd, sym)
4684 && sect_syms[asect->index] == NULL)
4686 if (!sym_is_global (abfd, asect->symbol))
4687 num_locals++;
4688 else
4689 num_globals++;
4693 /* Now sort the symbols so the local symbols are first. */
4694 amt = (num_locals + num_globals) * sizeof (asymbol *);
4695 new_syms = (asymbol **) bfd_alloc (abfd, amt);
4696 if (new_syms == NULL)
4697 return false;
4699 for (idx = 0; idx < symcount; idx++)
4701 asymbol *sym = syms[idx];
4702 unsigned int i;
4704 if (sym_is_global (abfd, sym))
4705 i = num_locals + num_globals2++;
4706 /* Don't include ignored section symbols. */
4707 else if (!ignore_section_sym (abfd, sym))
4708 i = num_locals2++;
4709 else
4710 continue;
4711 new_syms[i] = sym;
4712 sym->udata.i = i + 1;
4714 for (asect = abfd->sections; asect; asect = asect->next)
4716 asymbol *sym = asect->symbol;
4717 if (!ignore_section_sym (abfd, sym)
4718 && sect_syms[asect->index] == NULL)
4720 unsigned int i;
4722 sect_syms[asect->index] = sym;
4723 if (!sym_is_global (abfd, sym))
4724 i = num_locals2++;
4725 else
4726 i = num_locals + num_globals2++;
4727 new_syms[i] = sym;
4728 sym->udata.i = i + 1;
4732 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4734 *pnum_locals = num_locals;
4735 return true;
4738 /* Align to the maximum file alignment that could be required for any
4739 ELF data structure. */
4741 static inline file_ptr
4742 align_file_position (file_ptr off, int align)
4744 return (off + align - 1) & ~(align - 1);
4747 /* Assign a file position to a section, optionally aligning to the
4748 required section alignment. */
4750 file_ptr
4751 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4752 file_ptr offset,
4753 bool align)
4755 if (align && i_shdrp->sh_addralign > 1)
4756 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign & -i_shdrp->sh_addralign);
4757 i_shdrp->sh_offset = offset;
4758 if (i_shdrp->bfd_section != NULL)
4759 i_shdrp->bfd_section->filepos = offset;
4760 if (i_shdrp->sh_type != SHT_NOBITS)
4761 offset += i_shdrp->sh_size;
4762 return offset;
4765 /* Compute the file positions we are going to put the sections at, and
4766 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4767 is not NULL, this is being called by the ELF backend linker. */
4769 bool
4770 _bfd_elf_compute_section_file_positions (bfd *abfd,
4771 struct bfd_link_info *link_info)
4773 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4774 struct fake_section_arg fsargs;
4775 bool failed;
4776 struct elf_strtab_hash *strtab = NULL;
4777 Elf_Internal_Shdr *shstrtab_hdr;
4778 bool need_symtab;
4780 if (abfd->output_has_begun)
4781 return true;
4783 /* Do any elf backend specific processing first. */
4784 if (bed->elf_backend_begin_write_processing)
4785 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4787 if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
4788 return false;
4790 fsargs.failed = false;
4791 fsargs.link_info = link_info;
4792 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4793 if (fsargs.failed)
4794 return false;
4796 if (!assign_section_numbers (abfd, link_info))
4797 return false;
4799 /* The backend linker builds symbol table information itself. */
4800 need_symtab = (link_info == NULL
4801 && (bfd_get_symcount (abfd) > 0
4802 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4803 == HAS_RELOC)));
4804 if (need_symtab)
4806 /* Non-zero if doing a relocatable link. */
4807 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4809 if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
4810 return false;
4813 failed = false;
4814 if (link_info == NULL)
4816 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4817 if (failed)
4818 goto err_free_strtab;
4821 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4822 /* sh_name was set in init_file_header. */
4823 shstrtab_hdr->sh_type = SHT_STRTAB;
4824 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
4825 shstrtab_hdr->sh_addr = 0;
4826 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4827 shstrtab_hdr->sh_entsize = 0;
4828 shstrtab_hdr->sh_link = 0;
4829 shstrtab_hdr->sh_info = 0;
4830 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4831 shstrtab_hdr->sh_addralign = 1;
4833 if (!assign_file_positions_except_relocs (abfd, link_info))
4834 goto err_free_strtab;
4836 if (strtab != NULL)
4838 file_ptr off;
4839 Elf_Internal_Shdr *hdr;
4841 off = elf_next_file_pos (abfd);
4843 hdr = & elf_symtab_hdr (abfd);
4844 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4846 if (elf_symtab_shndx_list (abfd) != NULL)
4848 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4849 if (hdr->sh_size != 0)
4850 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4851 /* FIXME: What about other symtab_shndx sections in the list ? */
4854 hdr = &elf_tdata (abfd)->strtab_hdr;
4855 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4857 elf_next_file_pos (abfd) = off;
4859 /* Now that we know where the .strtab section goes, write it
4860 out. */
4861 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4862 || ! _bfd_elf_strtab_emit (abfd, strtab))
4863 goto err_free_strtab;
4864 _bfd_elf_strtab_free (strtab);
4867 abfd->output_has_begun = true;
4868 return true;
4870 err_free_strtab:
4871 if (strtab != NULL)
4872 _bfd_elf_strtab_free (strtab);
4873 return false;
4876 /* Retrieve .eh_frame_hdr. Prior to size_dynamic_sections the
4877 function effectively returns whether --eh-frame-hdr is given on the
4878 command line. After size_dynamic_sections the result reflects
4879 whether .eh_frame_hdr will actually be output (sizing isn't done
4880 until ldemul_after_allocation). */
4882 static asection *
4883 elf_eh_frame_hdr (const struct bfd_link_info *info)
4885 if (info != NULL && is_elf_hash_table (info->hash))
4886 return elf_hash_table (info)->eh_info.hdr_sec;
4887 return NULL;
4890 /* Make an initial estimate of the size of the program header. If we
4891 get the number wrong here, we'll redo section placement. */
4893 static bfd_size_type
4894 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4896 size_t segs;
4897 asection *s;
4898 const struct elf_backend_data *bed;
4900 /* Assume we will need exactly two PT_LOAD segments: one for text
4901 and one for data. */
4902 segs = 2;
4904 s = bfd_get_section_by_name (abfd, ".interp");
4905 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4907 /* If we have a loadable interpreter section, we need a
4908 PT_INTERP segment. In this case, assume we also need a
4909 PT_PHDR segment, although that may not be true for all
4910 targets. */
4911 segs += 2;
4914 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4916 /* We need a PT_DYNAMIC segment. */
4917 ++segs;
4920 if (info != NULL && info->relro)
4922 /* We need a PT_GNU_RELRO segment. */
4923 ++segs;
4926 if (elf_eh_frame_hdr (info))
4928 /* We need a PT_GNU_EH_FRAME segment. */
4929 ++segs;
4932 if (elf_stack_flags (abfd))
4934 /* We need a PT_GNU_STACK segment. */
4935 ++segs;
4938 if (elf_sframe (abfd))
4940 /* We need a PT_GNU_SFRAME segment. */
4941 ++segs;
4944 s = bfd_get_section_by_name (abfd,
4945 NOTE_GNU_PROPERTY_SECTION_NAME);
4946 if (s != NULL && s->size != 0)
4948 /* We need a PT_GNU_PROPERTY segment. */
4949 ++segs;
4952 for (s = abfd->sections; s != NULL; s = s->next)
4954 if ((s->flags & SEC_LOAD) != 0
4955 && elf_section_type (s) == SHT_NOTE)
4957 unsigned int alignment_power;
4958 /* We need a PT_NOTE segment. */
4959 ++segs;
4960 /* Try to create just one PT_NOTE segment for all adjacent
4961 loadable SHT_NOTE sections. gABI requires that within a
4962 PT_NOTE segment (and also inside of each SHT_NOTE section)
4963 each note should have the same alignment. So we check
4964 whether the sections are correctly aligned. */
4965 alignment_power = s->alignment_power;
4966 while (s->next != NULL
4967 && s->next->alignment_power == alignment_power
4968 && (s->next->flags & SEC_LOAD) != 0
4969 && elf_section_type (s->next) == SHT_NOTE)
4970 s = s->next;
4974 for (s = abfd->sections; s != NULL; s = s->next)
4976 if (s->flags & SEC_THREAD_LOCAL)
4978 /* We need a PT_TLS segment. */
4979 ++segs;
4980 break;
4984 bed = get_elf_backend_data (abfd);
4986 if ((abfd->flags & D_PAGED) != 0
4987 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4989 /* Add a PT_GNU_MBIND segment for each mbind section. */
4990 bfd_vma commonpagesize;
4991 unsigned int page_align_power;
4993 if (info != NULL)
4994 commonpagesize = info->commonpagesize;
4995 else
4996 commonpagesize = bed->commonpagesize;
4997 page_align_power = bfd_log2 (commonpagesize);
4998 for (s = abfd->sections; s != NULL; s = s->next)
4999 if (elf_section_flags (s) & SHF_GNU_MBIND)
5001 if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
5003 _bfd_error_handler
5004 /* xgettext:c-format */
5005 (_("%pB: GNU_MBIND section `%pA' has invalid "
5006 "sh_info field: %d"),
5007 abfd, s, elf_section_data (s)->this_hdr.sh_info);
5008 continue;
5010 /* Align mbind section to page size. */
5011 if (s->alignment_power < page_align_power)
5012 s->alignment_power = page_align_power;
5013 segs ++;
5017 /* Let the backend count up any program headers it might need. */
5018 if (bed->elf_backend_additional_program_headers)
5020 int a;
5022 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
5023 if (a == -1)
5024 abort ();
5025 segs += a;
5028 return segs * bed->s->sizeof_phdr;
5031 /* Find the segment that contains the output_section of section. */
5033 Elf_Internal_Phdr *
5034 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
5036 struct elf_segment_map *m;
5037 Elf_Internal_Phdr *p;
5039 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
5040 m != NULL;
5041 m = m->next, p++)
5043 int i;
5045 for (i = m->count - 1; i >= 0; i--)
5046 if (m->sections[i] == section)
5047 return p;
5050 return NULL;
5053 /* Create a mapping from a set of sections to a program segment. */
5055 static struct elf_segment_map *
5056 make_mapping (bfd *abfd,
5057 asection **sections,
5058 unsigned int from,
5059 unsigned int to,
5060 bool phdr)
5062 struct elf_segment_map *m;
5063 unsigned int i;
5064 asection **hdrpp;
5065 size_t amt;
5067 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5068 amt += (to - from) * sizeof (asection *);
5069 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5070 if (m == NULL)
5071 return NULL;
5072 m->next = NULL;
5073 m->p_type = PT_LOAD;
5074 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
5075 m->sections[i - from] = *hdrpp;
5076 m->count = to - from;
5078 if (from == 0 && phdr)
5080 /* Include the headers in the first PT_LOAD segment. */
5081 m->includes_filehdr = 1;
5082 m->includes_phdrs = 1;
5085 return m;
5088 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
5089 on failure. */
5091 struct elf_segment_map *
5092 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
5094 struct elf_segment_map *m;
5096 m = (struct elf_segment_map *) bfd_zalloc (abfd,
5097 sizeof (struct elf_segment_map));
5098 if (m == NULL)
5099 return NULL;
5100 m->next = NULL;
5101 m->p_type = PT_DYNAMIC;
5102 m->count = 1;
5103 m->sections[0] = dynsec;
5105 return m;
5108 /* Possibly add or remove segments from the segment map. */
5110 static bool
5111 elf_modify_segment_map (bfd *abfd,
5112 struct bfd_link_info *info,
5113 bool remove_empty_load)
5115 struct elf_segment_map **m;
5116 const struct elf_backend_data *bed;
5118 /* The placement algorithm assumes that non allocated sections are
5119 not in PT_LOAD segments. We ensure this here by removing such
5120 sections from the segment map. We also remove excluded
5121 sections. Finally, any PT_LOAD segment without sections is
5122 removed. */
5123 m = &elf_seg_map (abfd);
5124 while (*m)
5126 unsigned int i, new_count;
5128 for (new_count = 0, i = 0; i < (*m)->count; i++)
5130 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
5131 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
5132 || (*m)->p_type != PT_LOAD))
5134 (*m)->sections[new_count] = (*m)->sections[i];
5135 new_count++;
5138 (*m)->count = new_count;
5140 if (remove_empty_load
5141 && (*m)->p_type == PT_LOAD
5142 && (*m)->count == 0
5143 && !(*m)->includes_phdrs)
5144 *m = (*m)->next;
5145 else
5146 m = &(*m)->next;
5149 bed = get_elf_backend_data (abfd);
5150 if (bed->elf_backend_modify_segment_map != NULL)
5152 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
5153 return false;
5156 return true;
5159 #define IS_TBSS(s) \
5160 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
5162 /* Set up a mapping from BFD sections to program segments. Update
5163 NEED_LAYOUT if the section layout is changed. */
5165 bool
5166 _bfd_elf_map_sections_to_segments (bfd *abfd,
5167 struct bfd_link_info *info,
5168 bool *need_layout)
5170 unsigned int count;
5171 struct elf_segment_map *m;
5172 asection **sections = NULL;
5173 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5174 bool no_user_phdrs;
5176 no_user_phdrs = elf_seg_map (abfd) == NULL;
5178 if (info != NULL)
5180 info->user_phdrs = !no_user_phdrs;
5182 /* Size the relative relocations if DT_RELR is enabled. */
5183 if (info->enable_dt_relr
5184 && need_layout != NULL
5185 && bed->size_relative_relocs
5186 && !bed->size_relative_relocs (info, need_layout))
5187 info->callbacks->einfo
5188 (_("%F%P: failed to size relative relocations\n"));
5191 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
5193 asection *s;
5194 unsigned int i;
5195 struct elf_segment_map *mfirst;
5196 struct elf_segment_map **pm;
5197 asection *last_hdr;
5198 bfd_vma last_size;
5199 unsigned int hdr_index;
5200 bfd_vma maxpagesize;
5201 asection **hdrpp;
5202 bool phdr_in_segment;
5203 bool writable;
5204 bool executable;
5205 unsigned int tls_count = 0;
5206 asection *first_tls = NULL;
5207 asection *first_mbind = NULL;
5208 asection *dynsec, *eh_frame_hdr;
5209 asection *sframe;
5210 size_t amt;
5211 bfd_vma addr_mask, wrap_to = 0; /* Bytes. */
5212 bfd_size_type phdr_size; /* Octets/bytes. */
5213 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5215 /* Select the allocated sections, and sort them. */
5217 amt = bfd_count_sections (abfd) * sizeof (asection *);
5218 sections = (asection **) bfd_malloc (amt);
5219 if (sections == NULL)
5220 goto error_return;
5222 /* Calculate top address, avoiding undefined behaviour of shift
5223 left operator when shift count is equal to size of type
5224 being shifted. */
5225 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
5226 addr_mask = (addr_mask << 1) + 1;
5228 i = 0;
5229 for (s = abfd->sections; s != NULL; s = s->next)
5231 if ((s->flags & SEC_ALLOC) != 0)
5233 /* target_index is unused until bfd_elf_final_link
5234 starts output of section symbols. Use it to make
5235 qsort stable. */
5236 s->target_index = i;
5237 sections[i] = s;
5238 ++i;
5239 /* A wrapping section potentially clashes with header. */
5240 if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
5241 wrap_to = (s->lma + s->size / opb) & addr_mask;
5244 BFD_ASSERT (i <= bfd_count_sections (abfd));
5245 count = i;
5247 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
5249 phdr_size = elf_program_header_size (abfd);
5250 if (phdr_size == (bfd_size_type) -1)
5251 phdr_size = get_program_header_size (abfd, info);
5252 phdr_size += bed->s->sizeof_ehdr;
5253 /* phdr_size is compared to LMA values which are in bytes. */
5254 phdr_size /= opb;
5255 if (info != NULL)
5256 maxpagesize = info->maxpagesize;
5257 else
5258 maxpagesize = bed->maxpagesize;
5259 if (maxpagesize == 0)
5260 maxpagesize = 1;
5261 phdr_in_segment = info != NULL && info->load_phdrs;
5262 if (count != 0
5263 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
5264 >= (phdr_size & (maxpagesize - 1))))
5265 /* For compatibility with old scripts that may not be using
5266 SIZEOF_HEADERS, add headers when it looks like space has
5267 been left for them. */
5268 phdr_in_segment = true;
5270 /* Build the mapping. */
5271 mfirst = NULL;
5272 pm = &mfirst;
5274 /* If we have a .interp section, then create a PT_PHDR segment for
5275 the program headers and a PT_INTERP segment for the .interp
5276 section. */
5277 s = bfd_get_section_by_name (abfd, ".interp");
5278 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
5280 amt = sizeof (struct elf_segment_map);
5281 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5282 if (m == NULL)
5283 goto error_return;
5284 m->next = NULL;
5285 m->p_type = PT_PHDR;
5286 m->p_flags = PF_R;
5287 m->p_flags_valid = 1;
5288 m->includes_phdrs = 1;
5289 phdr_in_segment = true;
5290 *pm = m;
5291 pm = &m->next;
5293 amt = sizeof (struct elf_segment_map);
5294 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5295 if (m == NULL)
5296 goto error_return;
5297 m->next = NULL;
5298 m->p_type = PT_INTERP;
5299 m->count = 1;
5300 m->sections[0] = s;
5302 *pm = m;
5303 pm = &m->next;
5306 /* Look through the sections. We put sections in the same program
5307 segment when the start of the second section can be placed within
5308 a few bytes of the end of the first section. */
5309 last_hdr = NULL;
5310 last_size = 0;
5311 hdr_index = 0;
5312 writable = false;
5313 executable = false;
5314 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
5315 if (dynsec != NULL
5316 && (dynsec->flags & SEC_LOAD) == 0)
5317 dynsec = NULL;
5319 if ((abfd->flags & D_PAGED) == 0)
5320 phdr_in_segment = false;
5322 /* Deal with -Ttext or something similar such that the first section
5323 is not adjacent to the program headers. This is an
5324 approximation, since at this point we don't know exactly how many
5325 program headers we will need. */
5326 if (phdr_in_segment && count > 0)
5328 bfd_vma phdr_lma; /* Bytes. */
5329 bool separate_phdr = false;
5331 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
5332 if (info != NULL
5333 && info->separate_code
5334 && (sections[0]->flags & SEC_CODE) != 0)
5336 /* If data sections should be separate from code and
5337 thus not executable, and the first section is
5338 executable then put the file and program headers in
5339 their own PT_LOAD. */
5340 separate_phdr = true;
5341 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
5342 == (sections[0]->lma & addr_mask & -maxpagesize)))
5344 /* The file and program headers are currently on the
5345 same page as the first section. Put them on the
5346 previous page if we can. */
5347 if (phdr_lma >= maxpagesize)
5348 phdr_lma -= maxpagesize;
5349 else
5350 separate_phdr = false;
5353 if ((sections[0]->lma & addr_mask) < phdr_lma
5354 || (sections[0]->lma & addr_mask) < phdr_size)
5355 /* If file and program headers would be placed at the end
5356 of memory then it's probably better to omit them. */
5357 phdr_in_segment = false;
5358 else if (phdr_lma < wrap_to)
5359 /* If a section wraps around to where we'll be placing
5360 file and program headers, then the headers will be
5361 overwritten. */
5362 phdr_in_segment = false;
5363 else if (separate_phdr)
5365 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
5366 if (m == NULL)
5367 goto error_return;
5368 m->p_paddr = phdr_lma * opb;
5369 m->p_vaddr_offset
5370 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
5371 m->p_paddr_valid = 1;
5372 *pm = m;
5373 pm = &m->next;
5374 phdr_in_segment = false;
5378 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
5380 asection *hdr;
5381 bool new_segment;
5383 hdr = *hdrpp;
5385 /* See if this section and the last one will fit in the same
5386 segment. */
5388 if (last_hdr == NULL)
5390 /* If we don't have a segment yet, then we don't need a new
5391 one (we build the last one after this loop). */
5392 new_segment = false;
5394 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
5396 /* If this section has a different relation between the
5397 virtual address and the load address, then we need a new
5398 segment. */
5399 new_segment = true;
5401 else if (hdr->lma < last_hdr->lma + last_size
5402 || last_hdr->lma + last_size < last_hdr->lma)
5404 /* If this section has a load address that makes it overlap
5405 the previous section, then we need a new segment. */
5406 new_segment = true;
5408 else if ((abfd->flags & D_PAGED) != 0
5409 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
5410 == (hdr->lma & -maxpagesize)))
5412 /* If we are demand paged then we can't map two disk
5413 pages onto the same memory page. */
5414 new_segment = false;
5416 /* In the next test we have to be careful when last_hdr->lma is close
5417 to the end of the address space. If the aligned address wraps
5418 around to the start of the address space, then there are no more
5419 pages left in memory and it is OK to assume that the current
5420 section can be included in the current segment. */
5421 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5422 + maxpagesize > last_hdr->lma)
5423 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5424 + maxpagesize <= hdr->lma))
5426 /* If putting this section in this segment would force us to
5427 skip a page in the segment, then we need a new segment. */
5428 new_segment = true;
5430 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
5431 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
5433 /* We don't want to put a loaded section after a
5434 nonloaded (ie. bss style) section in the same segment
5435 as that will force the non-loaded section to be loaded.
5436 Consider .tbss sections as loaded for this purpose. */
5437 new_segment = true;
5439 else if ((abfd->flags & D_PAGED) == 0)
5441 /* If the file is not demand paged, which means that we
5442 don't require the sections to be correctly aligned in the
5443 file, then there is no other reason for a new segment. */
5444 new_segment = false;
5446 else if (info != NULL
5447 && info->separate_code
5448 && executable != ((hdr->flags & SEC_CODE) != 0))
5450 new_segment = true;
5452 else if (! writable
5453 && (hdr->flags & SEC_READONLY) == 0)
5455 /* We don't want to put a writable section in a read only
5456 segment. */
5457 new_segment = true;
5459 else
5461 /* Otherwise, we can use the same segment. */
5462 new_segment = false;
5465 /* Allow interested parties a chance to override our decision. */
5466 if (last_hdr != NULL
5467 && info != NULL
5468 && info->callbacks->override_segment_assignment != NULL)
5469 new_segment
5470 = info->callbacks->override_segment_assignment (info, abfd, hdr,
5471 last_hdr,
5472 new_segment);
5474 if (! new_segment)
5476 if ((hdr->flags & SEC_READONLY) == 0)
5477 writable = true;
5478 if ((hdr->flags & SEC_CODE) != 0)
5479 executable = true;
5480 last_hdr = hdr;
5481 /* .tbss sections effectively have zero size. */
5482 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5483 continue;
5486 /* We need a new program segment. We must create a new program
5487 header holding all the sections from hdr_index until hdr. */
5489 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5490 if (m == NULL)
5491 goto error_return;
5493 *pm = m;
5494 pm = &m->next;
5496 if ((hdr->flags & SEC_READONLY) == 0)
5497 writable = true;
5498 else
5499 writable = false;
5501 if ((hdr->flags & SEC_CODE) == 0)
5502 executable = false;
5503 else
5504 executable = true;
5506 last_hdr = hdr;
5507 /* .tbss sections effectively have zero size. */
5508 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5509 hdr_index = i;
5510 phdr_in_segment = false;
5513 /* Create a final PT_LOAD program segment, but not if it's just
5514 for .tbss. */
5515 if (last_hdr != NULL
5516 && (i - hdr_index != 1
5517 || !IS_TBSS (last_hdr)))
5519 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5520 if (m == NULL)
5521 goto error_return;
5523 *pm = m;
5524 pm = &m->next;
5527 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
5528 if (dynsec != NULL)
5530 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
5531 if (m == NULL)
5532 goto error_return;
5533 *pm = m;
5534 pm = &m->next;
5537 /* For each batch of consecutive loadable SHT_NOTE sections,
5538 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
5539 because if we link together nonloadable .note sections and
5540 loadable .note sections, we will generate two .note sections
5541 in the output file. */
5542 for (s = abfd->sections; s != NULL; s = s->next)
5544 if ((s->flags & SEC_LOAD) != 0
5545 && elf_section_type (s) == SHT_NOTE)
5547 asection *s2;
5548 unsigned int alignment_power = s->alignment_power;
5550 count = 1;
5551 for (s2 = s; s2->next != NULL; s2 = s2->next)
5553 if (s2->next->alignment_power == alignment_power
5554 && (s2->next->flags & SEC_LOAD) != 0
5555 && elf_section_type (s2->next) == SHT_NOTE
5556 && align_power (s2->lma + s2->size / opb,
5557 alignment_power)
5558 == s2->next->lma)
5559 count++;
5560 else
5561 break;
5563 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5564 amt += count * sizeof (asection *);
5565 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5566 if (m == NULL)
5567 goto error_return;
5568 m->next = NULL;
5569 m->p_type = PT_NOTE;
5570 m->count = count;
5571 while (count > 1)
5573 m->sections[m->count - count--] = s;
5574 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5575 s = s->next;
5577 m->sections[m->count - 1] = s;
5578 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5579 *pm = m;
5580 pm = &m->next;
5582 if (s->flags & SEC_THREAD_LOCAL)
5584 if (! tls_count)
5585 first_tls = s;
5586 tls_count++;
5588 if (first_mbind == NULL
5589 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5590 first_mbind = s;
5593 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5594 if (tls_count > 0)
5596 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5597 amt += tls_count * sizeof (asection *);
5598 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5599 if (m == NULL)
5600 goto error_return;
5601 m->next = NULL;
5602 m->p_type = PT_TLS;
5603 m->count = tls_count;
5604 /* Mandated PF_R. */
5605 m->p_flags = PF_R;
5606 m->p_flags_valid = 1;
5607 s = first_tls;
5608 for (i = 0; i < tls_count; ++i)
5610 if ((s->flags & SEC_THREAD_LOCAL) == 0)
5612 _bfd_error_handler
5613 (_("%pB: TLS sections are not adjacent:"), abfd);
5614 s = first_tls;
5615 i = 0;
5616 while (i < tls_count)
5618 if ((s->flags & SEC_THREAD_LOCAL) != 0)
5620 _bfd_error_handler (_(" TLS: %pA"), s);
5621 i++;
5623 else
5624 _bfd_error_handler (_(" non-TLS: %pA"), s);
5625 s = s->next;
5627 bfd_set_error (bfd_error_bad_value);
5628 goto error_return;
5630 m->sections[i] = s;
5631 s = s->next;
5634 *pm = m;
5635 pm = &m->next;
5638 if (first_mbind
5639 && (abfd->flags & D_PAGED) != 0
5640 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
5641 for (s = first_mbind; s != NULL; s = s->next)
5642 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5643 && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
5645 /* Mandated PF_R. */
5646 unsigned long p_flags = PF_R;
5647 if ((s->flags & SEC_READONLY) == 0)
5648 p_flags |= PF_W;
5649 if ((s->flags & SEC_CODE) != 0)
5650 p_flags |= PF_X;
5652 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5653 m = bfd_zalloc (abfd, amt);
5654 if (m == NULL)
5655 goto error_return;
5656 m->next = NULL;
5657 m->p_type = (PT_GNU_MBIND_LO
5658 + elf_section_data (s)->this_hdr.sh_info);
5659 m->count = 1;
5660 m->p_flags_valid = 1;
5661 m->sections[0] = s;
5662 m->p_flags = p_flags;
5664 *pm = m;
5665 pm = &m->next;
5668 s = bfd_get_section_by_name (abfd,
5669 NOTE_GNU_PROPERTY_SECTION_NAME);
5670 if (s != NULL && s->size != 0)
5672 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5673 m = bfd_zalloc (abfd, amt);
5674 if (m == NULL)
5675 goto error_return;
5676 m->next = NULL;
5677 m->p_type = PT_GNU_PROPERTY;
5678 m->count = 1;
5679 m->p_flags_valid = 1;
5680 m->sections[0] = s;
5681 m->p_flags = PF_R;
5682 *pm = m;
5683 pm = &m->next;
5686 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5687 segment. */
5688 eh_frame_hdr = elf_eh_frame_hdr (info);
5689 if (eh_frame_hdr != NULL
5690 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5692 amt = sizeof (struct elf_segment_map);
5693 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5694 if (m == NULL)
5695 goto error_return;
5696 m->next = NULL;
5697 m->p_type = PT_GNU_EH_FRAME;
5698 m->count = 1;
5699 m->sections[0] = eh_frame_hdr->output_section;
5701 *pm = m;
5702 pm = &m->next;
5705 /* If there is a .sframe section, throw in a PT_GNU_SFRAME
5706 segment. */
5707 sframe = elf_sframe (abfd);
5708 if (sframe != NULL
5709 && (sframe->output_section->flags & SEC_LOAD) != 0
5710 && sframe->size != 0)
5712 amt = sizeof (struct elf_segment_map);
5713 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5714 if (m == NULL)
5715 goto error_return;
5716 m->next = NULL;
5717 m->p_type = PT_GNU_SFRAME;
5718 m->count = 1;
5719 m->sections[0] = sframe->output_section;
5721 *pm = m;
5722 pm = &m->next;
5725 if (elf_stack_flags (abfd))
5727 amt = sizeof (struct elf_segment_map);
5728 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5729 if (m == NULL)
5730 goto error_return;
5731 m->next = NULL;
5732 m->p_type = PT_GNU_STACK;
5733 m->p_flags = elf_stack_flags (abfd);
5734 m->p_align = bed->stack_align;
5735 m->p_flags_valid = 1;
5736 m->p_align_valid = m->p_align != 0;
5737 if (info->stacksize > 0)
5739 m->p_size = info->stacksize;
5740 m->p_size_valid = 1;
5743 *pm = m;
5744 pm = &m->next;
5747 if (info != NULL && info->relro)
5749 for (m = mfirst; m != NULL; m = m->next)
5751 if (m->p_type == PT_LOAD
5752 && m->count != 0
5753 && m->sections[0]->vma >= info->relro_start
5754 && m->sections[0]->vma < info->relro_end)
5756 i = m->count;
5757 while (--i != (unsigned) -1)
5759 if (m->sections[i]->size > 0
5760 && (m->sections[i]->flags & SEC_LOAD) != 0
5761 && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
5762 break;
5765 if (i != (unsigned) -1)
5766 break;
5770 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5771 if (m != NULL)
5773 amt = sizeof (struct elf_segment_map);
5774 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5775 if (m == NULL)
5776 goto error_return;
5777 m->next = NULL;
5778 m->p_type = PT_GNU_RELRO;
5779 *pm = m;
5780 pm = &m->next;
5784 free (sections);
5785 elf_seg_map (abfd) = mfirst;
5788 if (!elf_modify_segment_map (abfd, info, no_user_phdrs || info == NULL))
5789 return false;
5791 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5792 ++count;
5793 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5795 return true;
5797 error_return:
5798 free (sections);
5799 return false;
5802 /* Sort sections by address. */
5804 static int
5805 elf_sort_sections (const void *arg1, const void *arg2)
5807 const asection *sec1 = *(const asection **) arg1;
5808 const asection *sec2 = *(const asection **) arg2;
5809 bfd_size_type size1, size2;
5811 /* Sort by LMA first, since this is the address used to
5812 place the section into a segment. */
5813 if (sec1->lma < sec2->lma)
5814 return -1;
5815 else if (sec1->lma > sec2->lma)
5816 return 1;
5818 /* Then sort by VMA. Normally the LMA and the VMA will be
5819 the same, and this will do nothing. */
5820 if (sec1->vma < sec2->vma)
5821 return -1;
5822 else if (sec1->vma > sec2->vma)
5823 return 1;
5825 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5827 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5828 && (x)->size != 0)
5830 if (TOEND (sec1))
5832 if (!TOEND (sec2))
5833 return 1;
5835 else if (TOEND (sec2))
5836 return -1;
5838 #undef TOEND
5840 /* Sort by size, to put zero sized sections
5841 before others at the same address. */
5843 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5844 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5846 if (size1 < size2)
5847 return -1;
5848 if (size1 > size2)
5849 return 1;
5851 return sec1->target_index - sec2->target_index;
5854 /* This qsort comparison functions sorts PT_LOAD segments first and
5855 by p_paddr, for assign_file_positions_for_load_sections. */
5857 static int
5858 elf_sort_segments (const void *arg1, const void *arg2)
5860 const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5861 const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5863 if (m1->p_type != m2->p_type)
5865 if (m1->p_type == PT_NULL)
5866 return 1;
5867 if (m2->p_type == PT_NULL)
5868 return -1;
5869 return m1->p_type < m2->p_type ? -1 : 1;
5871 if (m1->includes_filehdr != m2->includes_filehdr)
5872 return m1->includes_filehdr ? -1 : 1;
5873 if (m1->no_sort_lma != m2->no_sort_lma)
5874 return m1->no_sort_lma ? -1 : 1;
5875 if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5877 bfd_vma lma1, lma2; /* Octets. */
5878 lma1 = 0;
5879 if (m1->p_paddr_valid)
5880 lma1 = m1->p_paddr;
5881 else if (m1->count != 0)
5883 unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5884 m1->sections[0]);
5885 lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5887 lma2 = 0;
5888 if (m2->p_paddr_valid)
5889 lma2 = m2->p_paddr;
5890 else if (m2->count != 0)
5892 unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5893 m2->sections[0]);
5894 lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5896 if (lma1 != lma2)
5897 return lma1 < lma2 ? -1 : 1;
5899 if (m1->idx != m2->idx)
5900 return m1->idx < m2->idx ? -1 : 1;
5901 return 0;
5904 /* Ian Lance Taylor writes:
5906 We shouldn't be using % with a negative signed number. That's just
5907 not good. We have to make sure either that the number is not
5908 negative, or that the number has an unsigned type. When the types
5909 are all the same size they wind up as unsigned. When file_ptr is a
5910 larger signed type, the arithmetic winds up as signed long long,
5911 which is wrong.
5913 What we're trying to say here is something like ``increase OFF by
5914 the least amount that will cause it to be equal to the VMA modulo
5915 the page size.'' */
5916 /* In other words, something like:
5918 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5919 off_offset = off % bed->maxpagesize;
5920 if (vma_offset < off_offset)
5921 adjustment = vma_offset + bed->maxpagesize - off_offset;
5922 else
5923 adjustment = vma_offset - off_offset;
5925 which can be collapsed into the expression below. */
5927 static file_ptr
5928 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5930 /* PR binutils/16199: Handle an alignment of zero. */
5931 if (maxpagesize == 0)
5932 maxpagesize = 1;
5933 return ((vma - off) % maxpagesize);
5936 static void
5937 print_segment_map (const struct elf_segment_map *m)
5939 unsigned int j;
5940 const char *pt = get_segment_type (m->p_type);
5941 char buf[32];
5943 if (pt == NULL)
5945 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5946 sprintf (buf, "LOPROC+%7.7x",
5947 (unsigned int) (m->p_type - PT_LOPROC));
5948 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5949 sprintf (buf, "LOOS+%7.7x",
5950 (unsigned int) (m->p_type - PT_LOOS));
5951 else
5952 snprintf (buf, sizeof (buf), "%8.8x",
5953 (unsigned int) m->p_type);
5954 pt = buf;
5956 fflush (stdout);
5957 fprintf (stderr, "%s:", pt);
5958 for (j = 0; j < m->count; j++)
5959 fprintf (stderr, " %s", m->sections [j]->name);
5960 putc ('\n',stderr);
5961 fflush (stderr);
5964 /* Assign file positions to the sections based on the mapping from
5965 sections to segments. This function also sets up some fields in
5966 the file header. */
5968 static bool
5969 assign_file_positions_for_load_sections (bfd *abfd,
5970 struct bfd_link_info *link_info)
5972 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5973 struct elf_segment_map *m;
5974 struct elf_segment_map *phdr_load_seg;
5975 Elf_Internal_Phdr *phdrs;
5976 Elf_Internal_Phdr *p;
5977 file_ptr off; /* Octets. */
5978 bfd_size_type maxpagesize;
5979 unsigned int alloc, actual;
5980 unsigned int i, j;
5981 struct elf_segment_map **sorted_seg_map;
5982 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5984 if (link_info == NULL
5985 && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
5986 return false;
5988 alloc = 0;
5989 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5990 m->idx = alloc++;
5992 if (alloc)
5994 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5995 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5997 else
5999 /* PR binutils/12467. */
6000 elf_elfheader (abfd)->e_phoff = 0;
6001 elf_elfheader (abfd)->e_phentsize = 0;
6004 elf_elfheader (abfd)->e_phnum = alloc;
6006 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
6008 actual = alloc;
6009 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
6011 else
6013 actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
6014 BFD_ASSERT (elf_program_header_size (abfd)
6015 == actual * bed->s->sizeof_phdr);
6016 BFD_ASSERT (actual >= alloc);
6019 if (alloc == 0)
6021 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
6022 return true;
6025 /* We're writing the size in elf_program_header_size (abfd),
6026 see assign_file_positions_except_relocs, so make sure we have
6027 that amount allocated, with trailing space cleared.
6028 The variable alloc contains the computed need, while
6029 elf_program_header_size (abfd) contains the size used for the
6030 layout.
6031 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
6032 where the layout is forced to according to a larger size in the
6033 last iterations for the testcase ld-elf/header. */
6034 phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
6035 + alloc * sizeof (*sorted_seg_map)));
6036 sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
6037 elf_tdata (abfd)->phdr = phdrs;
6038 if (phdrs == NULL)
6039 return false;
6041 for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
6043 sorted_seg_map[j] = m;
6044 /* If elf_segment_map is not from map_sections_to_segments, the
6045 sections may not be correctly ordered. NOTE: sorting should
6046 not be done to the PT_NOTE section of a corefile, which may
6047 contain several pseudo-sections artificially created by bfd.
6048 Sorting these pseudo-sections breaks things badly. */
6049 if (m->count > 1
6050 && !(elf_elfheader (abfd)->e_type == ET_CORE
6051 && m->p_type == PT_NOTE))
6053 for (i = 0; i < m->count; i++)
6054 m->sections[i]->target_index = i;
6055 qsort (m->sections, (size_t) m->count, sizeof (asection *),
6056 elf_sort_sections);
6059 if (alloc > 1)
6060 qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
6061 elf_sort_segments);
6063 maxpagesize = 1;
6064 if ((abfd->flags & D_PAGED) != 0)
6066 if (link_info != NULL)
6067 maxpagesize = link_info->maxpagesize;
6068 else
6069 maxpagesize = bed->maxpagesize;
6072 /* Sections must map to file offsets past the ELF file header. */
6073 off = bed->s->sizeof_ehdr;
6074 /* And if one of the PT_LOAD headers doesn't include the program
6075 headers then we'll be mapping program headers in the usual
6076 position after the ELF file header. */
6077 phdr_load_seg = NULL;
6078 for (j = 0; j < alloc; j++)
6080 m = sorted_seg_map[j];
6081 if (m->p_type != PT_LOAD)
6082 break;
6083 if (m->includes_phdrs)
6085 phdr_load_seg = m;
6086 break;
6089 if (phdr_load_seg == NULL)
6090 off += actual * bed->s->sizeof_phdr;
6092 for (j = 0; j < alloc; j++)
6094 asection **secpp;
6095 bfd_vma off_adjust; /* Octets. */
6096 bool no_contents;
6097 bfd_size_type p_align;
6098 bool p_align_p;
6100 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
6101 number of sections with contents contributing to both p_filesz
6102 and p_memsz, followed by a number of sections with no contents
6103 that just contribute to p_memsz. In this loop, OFF tracks next
6104 available file offset for PT_LOAD and PT_NOTE segments. */
6105 m = sorted_seg_map[j];
6106 p = phdrs + m->idx;
6107 p->p_type = m->p_type;
6108 p->p_flags = m->p_flags;
6109 p_align = bed->p_align;
6110 p_align_p = false;
6112 if (m->count == 0)
6113 p->p_vaddr = m->p_vaddr_offset * opb;
6114 else
6115 p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
6117 if (m->p_paddr_valid)
6118 p->p_paddr = m->p_paddr;
6119 else if (m->count == 0)
6120 p->p_paddr = 0;
6121 else
6122 p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
6124 if (p->p_type == PT_LOAD
6125 && (abfd->flags & D_PAGED) != 0)
6127 /* p_align in demand paged PT_LOAD segments effectively stores
6128 the maximum page size. When copying an executable with
6129 objcopy, we set m->p_align from the input file. Use this
6130 value for maxpagesize rather than bed->maxpagesize, which
6131 may be different. Note that we use maxpagesize for PT_TLS
6132 segment alignment later in this function, so we are relying
6133 on at least one PT_LOAD segment appearing before a PT_TLS
6134 segment. */
6135 if (m->p_align_valid)
6136 maxpagesize = m->p_align;
6137 else if (p_align != 0
6138 && (link_info == NULL
6139 || !link_info->maxpagesize_is_set))
6140 /* Set p_align to the default p_align value while laying
6141 out segments aligning to the maximum page size or the
6142 largest section alignment. The run-time loader can
6143 align segments to the default p_align value or the
6144 maximum page size, depending on system page size. */
6145 p_align_p = true;
6147 p->p_align = maxpagesize;
6149 else if (m->p_align_valid)
6150 p->p_align = m->p_align;
6151 else if (m->count == 0)
6152 p->p_align = 1 << bed->s->log_file_align;
6154 if (m == phdr_load_seg)
6156 if (!m->includes_filehdr)
6157 p->p_offset = off;
6158 off += actual * bed->s->sizeof_phdr;
6161 no_contents = false;
6162 off_adjust = 0;
6163 if (p->p_type == PT_LOAD
6164 && m->count > 0)
6166 bfd_size_type align; /* Bytes. */
6167 unsigned int align_power = 0;
6169 if (m->p_align_valid)
6170 align = p->p_align;
6171 else
6173 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6175 unsigned int secalign;
6177 secalign = bfd_section_alignment (*secpp);
6178 if (secalign > align_power)
6179 align_power = secalign;
6181 align = (bfd_size_type) 1 << align_power;
6182 if (align < maxpagesize)
6184 /* If a section requires alignment higher than the
6185 default p_align value, don't set p_align to the
6186 default p_align value. */
6187 if (align > p_align)
6188 p_align_p = false;
6189 align = maxpagesize;
6191 else
6193 /* If a section requires alignment higher than the
6194 maximum page size, set p_align to the section
6195 alignment. */
6196 p_align_p = true;
6197 p_align = align;
6201 for (i = 0; i < m->count; i++)
6202 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
6203 /* If we aren't making room for this section, then
6204 it must be SHT_NOBITS regardless of what we've
6205 set via struct bfd_elf_special_section. */
6206 elf_section_type (m->sections[i]) = SHT_NOBITS;
6208 /* Find out whether this segment contains any loadable
6209 sections. */
6210 no_contents = true;
6211 for (i = 0; i < m->count; i++)
6212 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
6214 no_contents = false;
6215 break;
6218 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
6220 /* Broken hardware and/or kernel require that files do not
6221 map the same page with different permissions on some hppa
6222 processors. */
6223 if (j != 0
6224 && (abfd->flags & D_PAGED) != 0
6225 && bed->no_page_alias
6226 && (off & (maxpagesize - 1)) != 0
6227 && ((off & -maxpagesize)
6228 == ((off + off_adjust) & -maxpagesize)))
6229 off_adjust += maxpagesize;
6230 off += off_adjust;
6231 if (no_contents)
6233 /* We shouldn't need to align the segment on disk since
6234 the segment doesn't need file space, but the gABI
6235 arguably requires the alignment and glibc ld.so
6236 checks it. So to comply with the alignment
6237 requirement but not waste file space, we adjust
6238 p_offset for just this segment. (OFF_ADJUST is
6239 subtracted from OFF later.) This may put p_offset
6240 past the end of file, but that shouldn't matter. */
6242 else
6243 off_adjust = 0;
6245 /* Make sure the .dynamic section is the first section in the
6246 PT_DYNAMIC segment. */
6247 else if (p->p_type == PT_DYNAMIC
6248 && m->count > 1
6249 && strcmp (m->sections[0]->name, ".dynamic") != 0)
6251 _bfd_error_handler
6252 (_("%pB: The first section in the PT_DYNAMIC segment"
6253 " is not the .dynamic section"),
6254 abfd);
6255 bfd_set_error (bfd_error_bad_value);
6256 return false;
6258 /* Set the note section type to SHT_NOTE. */
6259 else if (p->p_type == PT_NOTE)
6260 for (i = 0; i < m->count; i++)
6261 elf_section_type (m->sections[i]) = SHT_NOTE;
6263 if (m->includes_filehdr)
6265 if (!m->p_flags_valid)
6266 p->p_flags |= PF_R;
6267 p->p_filesz = bed->s->sizeof_ehdr;
6268 p->p_memsz = bed->s->sizeof_ehdr;
6269 if (p->p_type == PT_LOAD)
6271 if (m->count > 0)
6273 if (p->p_vaddr < (bfd_vma) off
6274 || (!m->p_paddr_valid
6275 && p->p_paddr < (bfd_vma) off))
6277 _bfd_error_handler
6278 (_("%pB: not enough room for program headers,"
6279 " try linking with -N"),
6280 abfd);
6281 bfd_set_error (bfd_error_bad_value);
6282 return false;
6284 p->p_vaddr -= off;
6285 if (!m->p_paddr_valid)
6286 p->p_paddr -= off;
6289 else if (sorted_seg_map[0]->includes_filehdr)
6291 Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
6292 p->p_vaddr = filehdr->p_vaddr;
6293 if (!m->p_paddr_valid)
6294 p->p_paddr = filehdr->p_paddr;
6298 if (m->includes_phdrs)
6300 if (!m->p_flags_valid)
6301 p->p_flags |= PF_R;
6302 p->p_filesz += actual * bed->s->sizeof_phdr;
6303 p->p_memsz += actual * bed->s->sizeof_phdr;
6304 if (!m->includes_filehdr)
6306 if (p->p_type == PT_LOAD)
6308 elf_elfheader (abfd)->e_phoff = p->p_offset;
6309 if (m->count > 0)
6311 p->p_vaddr -= off - p->p_offset;
6312 if (!m->p_paddr_valid)
6313 p->p_paddr -= off - p->p_offset;
6316 else if (phdr_load_seg != NULL)
6318 Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
6319 bfd_vma phdr_off = 0; /* Octets. */
6320 if (phdr_load_seg->includes_filehdr)
6321 phdr_off = bed->s->sizeof_ehdr;
6322 p->p_vaddr = phdr->p_vaddr + phdr_off;
6323 if (!m->p_paddr_valid)
6324 p->p_paddr = phdr->p_paddr + phdr_off;
6325 p->p_offset = phdr->p_offset + phdr_off;
6327 else
6328 p->p_offset = bed->s->sizeof_ehdr;
6332 if (p->p_type == PT_LOAD
6333 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
6335 if (!m->includes_filehdr && !m->includes_phdrs)
6337 p->p_offset = off;
6338 if (no_contents)
6340 /* Put meaningless p_offset for PT_LOAD segments
6341 without file contents somewhere within the first
6342 page, in an attempt to not point past EOF. */
6343 bfd_size_type align = maxpagesize;
6344 if (align < p->p_align)
6345 align = p->p_align;
6346 if (align < 1)
6347 align = 1;
6348 p->p_offset = off % align;
6351 else
6353 file_ptr adjust; /* Octets. */
6355 adjust = off - (p->p_offset + p->p_filesz);
6356 if (!no_contents)
6357 p->p_filesz += adjust;
6358 p->p_memsz += adjust;
6362 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
6363 maps. Set filepos for sections in PT_LOAD segments, and in
6364 core files, for sections in PT_NOTE segments.
6365 assign_file_positions_for_non_load_sections will set filepos
6366 for other sections and update p_filesz for other segments. */
6367 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6369 asection *sec;
6370 bfd_size_type align;
6371 Elf_Internal_Shdr *this_hdr;
6373 sec = *secpp;
6374 this_hdr = &elf_section_data (sec)->this_hdr;
6375 align = (bfd_size_type) 1 << bfd_section_alignment (sec);
6377 if ((p->p_type == PT_LOAD
6378 || p->p_type == PT_TLS)
6379 && (this_hdr->sh_type != SHT_NOBITS
6380 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
6381 && ((this_hdr->sh_flags & SHF_TLS) == 0
6382 || p->p_type == PT_TLS))))
6384 bfd_vma p_start = p->p_paddr; /* Octets. */
6385 bfd_vma p_end = p_start + p->p_memsz; /* Octets. */
6386 bfd_vma s_start = sec->lma * opb; /* Octets. */
6387 bfd_vma adjust = s_start - p_end; /* Octets. */
6389 if (adjust != 0
6390 && (s_start < p_end
6391 || p_end < p_start))
6393 _bfd_error_handler
6394 /* xgettext:c-format */
6395 (_("%pB: section %pA lma %#" PRIx64
6396 " adjusted to %#" PRIx64),
6397 abfd, sec, (uint64_t) s_start / opb,
6398 (uint64_t) p_end / opb);
6399 adjust = 0;
6400 sec->lma = p_end / opb;
6402 p->p_memsz += adjust;
6404 if (p->p_type == PT_LOAD)
6406 if (this_hdr->sh_type != SHT_NOBITS)
6408 off_adjust = 0;
6409 if (p->p_filesz + adjust < p->p_memsz)
6411 /* We have a PROGBITS section following NOBITS ones.
6412 Allocate file space for the NOBITS section(s).
6413 We don't need to write out the zeros, posix
6414 fseek past the end of data already written
6415 followed by a write at that location is
6416 guaranteed to result in zeros being read
6417 from the gap. */
6418 adjust = p->p_memsz - p->p_filesz;
6421 /* We only adjust sh_offset in SHT_NOBITS sections
6422 as would seem proper for their address when the
6423 section is first in the segment. sh_offset
6424 doesn't really have any significance for
6425 SHT_NOBITS anyway, apart from a notional position
6426 relative to other sections. Historically we
6427 didn't bother with adjusting sh_offset and some
6428 programs depend on it not being adjusted. See
6429 pr12921 and pr25662. */
6430 if (this_hdr->sh_type != SHT_NOBITS || i == 0)
6432 off += adjust;
6433 if (this_hdr->sh_type == SHT_NOBITS)
6434 off_adjust += adjust;
6437 if (this_hdr->sh_type != SHT_NOBITS)
6438 p->p_filesz += adjust;
6441 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
6443 /* The section at i == 0 is the one that actually contains
6444 everything. */
6445 if (i == 0)
6447 this_hdr->sh_offset = sec->filepos = off;
6448 off += this_hdr->sh_size;
6449 p->p_filesz = this_hdr->sh_size;
6450 p->p_memsz = 0;
6451 p->p_align = 1;
6453 else
6455 /* The rest are fake sections that shouldn't be written. */
6456 sec->filepos = 0;
6457 sec->size = 0;
6458 sec->flags = 0;
6459 continue;
6462 else
6464 if (p->p_type == PT_LOAD)
6466 this_hdr->sh_offset = sec->filepos = off;
6467 if (this_hdr->sh_type != SHT_NOBITS)
6468 off += this_hdr->sh_size;
6470 else if (this_hdr->sh_type == SHT_NOBITS
6471 && (this_hdr->sh_flags & SHF_TLS) != 0
6472 && this_hdr->sh_offset == 0)
6474 /* This is a .tbss section that didn't get a PT_LOAD.
6475 (See _bfd_elf_map_sections_to_segments "Create a
6476 final PT_LOAD".) Set sh_offset to the value it
6477 would have if we had created a zero p_filesz and
6478 p_memsz PT_LOAD header for the section. This
6479 also makes the PT_TLS header have the same
6480 p_offset value. */
6481 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
6482 off, align);
6483 this_hdr->sh_offset = sec->filepos = off + adjust;
6486 if (this_hdr->sh_type != SHT_NOBITS)
6488 p->p_filesz += this_hdr->sh_size;
6489 /* A load section without SHF_ALLOC is something like
6490 a note section in a PT_NOTE segment. These take
6491 file space but are not loaded into memory. */
6492 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6493 p->p_memsz += this_hdr->sh_size;
6495 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6497 if (p->p_type == PT_TLS)
6498 p->p_memsz += this_hdr->sh_size;
6500 /* .tbss is special. It doesn't contribute to p_memsz of
6501 normal segments. */
6502 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
6503 p->p_memsz += this_hdr->sh_size;
6506 if (align > p->p_align
6507 && !m->p_align_valid
6508 && (p->p_type != PT_LOAD
6509 || (abfd->flags & D_PAGED) == 0))
6510 p->p_align = align;
6513 if (!m->p_flags_valid)
6515 p->p_flags |= PF_R;
6516 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
6517 p->p_flags |= PF_X;
6518 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
6519 p->p_flags |= PF_W;
6523 off -= off_adjust;
6525 /* PR ld/20815 - Check that the program header segment, if
6526 present, will be loaded into memory. */
6527 if (p->p_type == PT_PHDR
6528 && phdr_load_seg == NULL
6529 && !(bed->elf_backend_allow_non_load_phdr != NULL
6530 && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
6532 /* The fix for this error is usually to edit the linker script being
6533 used and set up the program headers manually. Either that or
6534 leave room for the headers at the start of the SECTIONS. */
6535 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6536 " by LOAD segment"),
6537 abfd);
6538 if (link_info == NULL)
6539 return false;
6540 /* Arrange for the linker to exit with an error, deleting
6541 the output file unless --noinhibit-exec is given. */
6542 link_info->callbacks->info ("%X");
6545 /* Check that all sections are in a PT_LOAD segment.
6546 Don't check funky gdb generated core files. */
6547 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
6549 bool check_vma = true;
6551 for (i = 1; i < m->count; i++)
6552 if (m->sections[i]->vma == m->sections[i - 1]->vma
6553 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
6554 ->this_hdr), p) != 0
6555 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
6556 ->this_hdr), p) != 0)
6558 /* Looks like we have overlays packed into the segment. */
6559 check_vma = false;
6560 break;
6563 for (i = 0; i < m->count; i++)
6565 Elf_Internal_Shdr *this_hdr;
6566 asection *sec;
6568 sec = m->sections[i];
6569 this_hdr = &(elf_section_data(sec)->this_hdr);
6570 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
6571 && !ELF_TBSS_SPECIAL (this_hdr, p))
6573 _bfd_error_handler
6574 /* xgettext:c-format */
6575 (_("%pB: section `%pA' can't be allocated in segment %d"),
6576 abfd, sec, j);
6577 print_segment_map (m);
6581 if (p_align_p)
6582 p->p_align = p_align;
6586 elf_next_file_pos (abfd) = off;
6588 if (link_info != NULL
6589 && phdr_load_seg != NULL
6590 && phdr_load_seg->includes_filehdr)
6592 /* There is a segment that contains both the file headers and the
6593 program headers, so provide a symbol __ehdr_start pointing there.
6594 A program can use this to examine itself robustly. */
6596 struct elf_link_hash_entry *hash
6597 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
6598 false, false, true);
6599 /* If the symbol was referenced and not defined, define it. */
6600 if (hash != NULL
6601 && (hash->root.type == bfd_link_hash_new
6602 || hash->root.type == bfd_link_hash_undefined
6603 || hash->root.type == bfd_link_hash_undefweak
6604 || hash->root.type == bfd_link_hash_common))
6606 asection *s = NULL;
6607 bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
6609 if (phdr_load_seg->count != 0)
6610 /* The segment contains sections, so use the first one. */
6611 s = phdr_load_seg->sections[0];
6612 else
6613 /* Use the first (i.e. lowest-addressed) section in any segment. */
6614 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6615 if (m->p_type == PT_LOAD && m->count != 0)
6617 s = m->sections[0];
6618 break;
6621 if (s != NULL)
6623 hash->root.u.def.value = filehdr_vaddr - s->vma;
6624 hash->root.u.def.section = s;
6626 else
6628 hash->root.u.def.value = filehdr_vaddr;
6629 hash->root.u.def.section = bfd_abs_section_ptr;
6632 hash->root.type = bfd_link_hash_defined;
6633 hash->def_regular = 1;
6634 hash->non_elf = 0;
6638 return true;
6641 /* Determine if a bfd is a debuginfo file. Unfortunately there
6642 is no defined method for detecting such files, so we have to
6643 use heuristics instead. */
6645 bool
6646 is_debuginfo_file (bfd *abfd)
6648 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6649 return false;
6651 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6652 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6653 Elf_Internal_Shdr **headerp;
6655 for (headerp = start_headers; headerp < end_headers; headerp ++)
6657 Elf_Internal_Shdr *header = * headerp;
6659 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6660 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6661 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6662 && header->sh_type != SHT_NOBITS
6663 && header->sh_type != SHT_NOTE)
6664 return false;
6667 return true;
6670 /* Assign file positions for other sections, except for compressed debug
6671 and sections assigned in _bfd_elf_assign_file_positions_for_non_load. */
6673 static bool
6674 assign_file_positions_for_non_load_sections (bfd *abfd,
6675 struct bfd_link_info *link_info)
6677 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6678 Elf_Internal_Shdr **i_shdrpp;
6679 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
6680 Elf_Internal_Phdr *phdrs;
6681 Elf_Internal_Phdr *p;
6682 struct elf_segment_map *m;
6683 file_ptr off;
6684 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
6685 bfd_vma maxpagesize;
6687 if (link_info != NULL)
6688 maxpagesize = link_info->maxpagesize;
6689 else
6690 maxpagesize = bed->maxpagesize;
6691 i_shdrpp = elf_elfsections (abfd);
6692 end_hdrpp = i_shdrpp + elf_numsections (abfd);
6693 off = elf_next_file_pos (abfd);
6694 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
6696 Elf_Internal_Shdr *hdr;
6697 bfd_vma align;
6699 hdr = *hdrpp;
6700 if (hdr->bfd_section != NULL
6701 && (hdr->bfd_section->filepos != 0
6702 || (hdr->sh_type == SHT_NOBITS
6703 && hdr->contents == NULL)))
6704 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
6705 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6707 if (hdr->sh_size != 0
6708 /* PR 24717 - debuginfo files are known to be not strictly
6709 compliant with the ELF standard. In particular they often
6710 have .note.gnu.property sections that are outside of any
6711 loadable segment. This is not a problem for such files,
6712 so do not warn about them. */
6713 && ! is_debuginfo_file (abfd))
6714 _bfd_error_handler
6715 /* xgettext:c-format */
6716 (_("%pB: warning: allocated section `%s' not in segment"),
6717 abfd,
6718 (hdr->bfd_section == NULL
6719 ? "*unknown*"
6720 : hdr->bfd_section->name));
6721 /* We don't need to page align empty sections. */
6722 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
6723 align = maxpagesize;
6724 else
6725 align = hdr->sh_addralign & -hdr->sh_addralign;
6726 off += vma_page_aligned_bias (hdr->sh_addr, off, align);
6727 off = _bfd_elf_assign_file_position_for_section (hdr, off,
6728 false);
6730 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6731 && hdr->bfd_section == NULL)
6732 /* We don't know the offset of these sections yet:
6733 their size has not been decided. */
6734 || (abfd->is_linker_output
6735 && hdr->bfd_section != NULL
6736 && (hdr->sh_name == -1u
6737 || bfd_section_is_ctf (hdr->bfd_section)))
6738 || hdr == i_shdrpp[elf_onesymtab (abfd)]
6739 || (elf_symtab_shndx_list (abfd) != NULL
6740 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6741 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6742 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
6743 hdr->sh_offset = -1;
6744 else
6745 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6747 elf_next_file_pos (abfd) = off;
6749 /* Now that we have set the section file positions, we can set up
6750 the file positions for the non PT_LOAD segments. */
6751 phdrs = elf_tdata (abfd)->phdr;
6752 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6754 if (p->p_type == PT_GNU_RELRO)
6756 bfd_vma start, end; /* Bytes. */
6757 bool ok;
6759 if (link_info != NULL)
6761 /* During linking the range of the RELRO segment is passed
6762 in link_info. Note that there may be padding between
6763 relro_start and the first RELRO section. */
6764 start = link_info->relro_start;
6765 end = link_info->relro_end;
6767 else if (m->count != 0)
6769 if (!m->p_size_valid)
6770 abort ();
6771 start = m->sections[0]->vma;
6772 end = start + m->p_size / opb;
6774 else
6776 start = 0;
6777 end = 0;
6780 ok = false;
6781 if (start < end)
6783 struct elf_segment_map *lm;
6784 const Elf_Internal_Phdr *lp;
6785 unsigned int i;
6787 /* Find a LOAD segment containing a section in the RELRO
6788 segment. */
6789 for (lm = elf_seg_map (abfd), lp = phdrs;
6790 lm != NULL;
6791 lm = lm->next, lp++)
6793 if (lp->p_type == PT_LOAD
6794 && lm->count != 0
6795 && (lm->sections[lm->count - 1]->vma
6796 + (!IS_TBSS (lm->sections[lm->count - 1])
6797 ? lm->sections[lm->count - 1]->size / opb
6798 : 0)) > start
6799 && lm->sections[0]->vma < end)
6800 break;
6803 if (lm != NULL)
6805 /* Find the section starting the RELRO segment. */
6806 for (i = 0; i < lm->count; i++)
6808 asection *s = lm->sections[i];
6809 if (s->vma >= start
6810 && s->vma < end
6811 && s->size != 0)
6812 break;
6815 if (i < lm->count)
6817 p->p_vaddr = lm->sections[i]->vma * opb;
6818 p->p_paddr = lm->sections[i]->lma * opb;
6819 p->p_offset = lm->sections[i]->filepos;
6820 p->p_memsz = end * opb - p->p_vaddr;
6821 p->p_filesz = p->p_memsz;
6823 /* The RELRO segment typically ends a few bytes
6824 into .got.plt but other layouts are possible.
6825 In cases where the end does not match any
6826 loaded section (for instance is in file
6827 padding), trim p_filesz back to correspond to
6828 the end of loaded section contents. */
6829 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6830 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6832 /* Preserve the alignment and flags if they are
6833 valid. The gold linker generates RW/4 for
6834 the PT_GNU_RELRO section. It is better for
6835 objcopy/strip to honor these attributes
6836 otherwise gdb will choke when using separate
6837 debug files. */
6838 if (!m->p_align_valid)
6839 p->p_align = 1;
6840 if (!m->p_flags_valid)
6841 p->p_flags = PF_R;
6842 ok = true;
6847 if (!ok)
6849 if (link_info != NULL)
6850 _bfd_error_handler
6851 (_("%pB: warning: unable to allocate any sections"
6852 " to PT_GNU_RELRO segment"),
6853 abfd);
6854 memset (p, 0, sizeof *p);
6857 else if (p->p_type == PT_GNU_STACK)
6859 if (m->p_size_valid)
6860 p->p_memsz = m->p_size;
6862 else if (m->count != 0)
6864 unsigned int i;
6866 if (p->p_type != PT_LOAD
6867 && (p->p_type != PT_NOTE
6868 || bfd_get_format (abfd) != bfd_core))
6870 /* A user specified segment layout may include a PHDR
6871 segment that overlaps with a LOAD segment... */
6872 if (p->p_type == PT_PHDR)
6874 m->count = 0;
6875 continue;
6878 if (m->includes_filehdr || m->includes_phdrs)
6880 /* PR 17512: file: 2195325e. */
6881 _bfd_error_handler
6882 (_("%pB: error: non-load segment %d includes file header "
6883 "and/or program header"),
6884 abfd, (int) (p - phdrs));
6885 return false;
6888 p->p_filesz = 0;
6889 p->p_offset = m->sections[0]->filepos;
6890 for (i = m->count; i-- != 0;)
6892 asection *sect = m->sections[i];
6893 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6894 if (hdr->sh_type != SHT_NOBITS)
6896 p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
6897 /* NB: p_memsz of the loadable PT_NOTE segment
6898 should be the same as p_filesz. */
6899 if (p->p_type == PT_NOTE
6900 && (hdr->sh_flags & SHF_ALLOC) != 0)
6901 p->p_memsz = p->p_filesz;
6902 break;
6909 return true;
6912 static elf_section_list *
6913 find_section_in_list (unsigned int i, elf_section_list * list)
6915 for (;list != NULL; list = list->next)
6916 if (list->ndx == i)
6917 break;
6918 return list;
6921 /* Work out the file positions of all the sections. This is called by
6922 _bfd_elf_compute_section_file_positions. All the section sizes and
6923 VMAs must be known before this is called.
6925 Reloc sections come in two flavours: Those processed specially as
6926 "side-channel" data attached to a section to which they apply, and
6927 those that bfd doesn't process as relocations. The latter sort are
6928 stored in a normal bfd section by bfd_section_from_shdr. We don't
6929 consider the former sort here, unless they form part of the loadable
6930 image. Reloc sections not assigned here (and compressed debugging
6931 sections and CTF sections which nothing else in the file can rely
6932 upon) will be handled later by assign_file_positions_for_relocs.
6934 We also don't set the positions of the .symtab and .strtab here. */
6936 static bool
6937 assign_file_positions_except_relocs (bfd *abfd,
6938 struct bfd_link_info *link_info)
6940 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6941 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6942 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6943 unsigned int alloc;
6945 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6946 && bfd_get_format (abfd) != bfd_core)
6948 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6949 unsigned int num_sec = elf_numsections (abfd);
6950 Elf_Internal_Shdr **hdrpp;
6951 unsigned int i;
6952 file_ptr off;
6954 /* Start after the ELF header. */
6955 off = i_ehdrp->e_ehsize;
6957 /* We are not creating an executable, which means that we are
6958 not creating a program header, and that the actual order of
6959 the sections in the file is unimportant. */
6960 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6962 Elf_Internal_Shdr *hdr;
6964 hdr = *hdrpp;
6965 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6966 && hdr->bfd_section == NULL)
6967 /* Do not assign offsets for these sections yet: we don't know
6968 their sizes. */
6969 || (abfd->is_linker_output
6970 && hdr->bfd_section != NULL
6971 && (hdr->sh_name == -1u
6972 || bfd_section_is_ctf (hdr->bfd_section)))
6973 || i == elf_onesymtab (abfd)
6974 || (elf_symtab_shndx_list (abfd) != NULL
6975 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6976 || i == elf_strtab_sec (abfd)
6977 || i == elf_shstrtab_sec (abfd))
6979 hdr->sh_offset = -1;
6981 else
6982 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6985 elf_next_file_pos (abfd) = off;
6986 elf_program_header_size (abfd) = 0;
6988 else
6990 /* Assign file positions for the loaded sections based on the
6991 assignment of sections to segments. */
6992 if (!assign_file_positions_for_load_sections (abfd, link_info))
6993 return false;
6995 /* And for non-load sections. */
6996 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6997 return false;
7000 if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
7001 return false;
7003 /* Write out the program headers. */
7004 alloc = i_ehdrp->e_phnum;
7005 if (alloc != 0)
7007 if (link_info != NULL && ! link_info->no_warn_rwx_segments)
7009 bool warned_tls = false;
7010 bool warned_rwx = false;
7012 /* Memory resident segments with non-zero size and RWX
7013 permissions are a security risk, so we generate a warning
7014 here if we are creating any. */
7015 unsigned int i;
7017 for (i = 0; i < alloc; i++)
7019 const Elf_Internal_Phdr * phdr = tdata->phdr + i;
7021 if (phdr->p_memsz == 0)
7022 continue;
7024 if (! warned_tls
7025 && phdr->p_type == PT_TLS
7026 && (phdr->p_flags & PF_X))
7028 if (link_info->warn_is_error_for_rwx_segments)
7030 _bfd_error_handler (_("\
7031 error: %pB has a TLS segment with execute permission"),
7032 abfd);
7033 return false;
7036 _bfd_error_handler (_("\
7037 warning: %pB has a TLS segment with execute permission"),
7038 abfd);
7039 if (warned_rwx)
7040 break;
7042 warned_tls = true;
7044 else if (! warned_rwx
7045 && phdr->p_type == PT_LOAD
7046 && ((phdr->p_flags & (PF_R | PF_W | PF_X))
7047 == (PF_R | PF_W | PF_X)))
7049 if (link_info->warn_is_error_for_rwx_segments)
7051 _bfd_error_handler (_("\
7052 error: %pB has a LOAD segment with RWX permissions"),
7053 abfd);
7054 return false;
7057 _bfd_error_handler (_("\
7058 warning: %pB has a LOAD segment with RWX permissions"),
7059 abfd);
7060 if (warned_tls)
7061 break;
7063 warned_rwx = true;
7068 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
7069 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
7070 return false;
7073 return true;
7076 bool
7077 _bfd_elf_init_file_header (bfd *abfd,
7078 struct bfd_link_info *info ATTRIBUTE_UNUSED)
7080 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
7081 struct elf_strtab_hash *shstrtab;
7082 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7084 i_ehdrp = elf_elfheader (abfd);
7086 shstrtab = _bfd_elf_strtab_init ();
7087 if (shstrtab == NULL)
7088 return false;
7090 elf_shstrtab (abfd) = shstrtab;
7092 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
7093 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
7094 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
7095 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
7097 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
7098 i_ehdrp->e_ident[EI_DATA] =
7099 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
7100 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
7102 if ((abfd->flags & DYNAMIC) != 0)
7103 i_ehdrp->e_type = ET_DYN;
7104 else if ((abfd->flags & EXEC_P) != 0)
7105 i_ehdrp->e_type = ET_EXEC;
7106 else if (bfd_get_format (abfd) == bfd_core)
7107 i_ehdrp->e_type = ET_CORE;
7108 else
7109 i_ehdrp->e_type = ET_REL;
7111 switch (bfd_get_arch (abfd))
7113 case bfd_arch_unknown:
7114 i_ehdrp->e_machine = EM_NONE;
7115 break;
7117 /* There used to be a long list of cases here, each one setting
7118 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
7119 in the corresponding bfd definition. To avoid duplication,
7120 the switch was removed. Machines that need special handling
7121 can generally do it in elf_backend_final_write_processing(),
7122 unless they need the information earlier than the final write.
7123 Such need can generally be supplied by replacing the tests for
7124 e_machine with the conditions used to determine it. */
7125 default:
7126 i_ehdrp->e_machine = bed->elf_machine_code;
7129 i_ehdrp->e_version = bed->s->ev_current;
7130 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
7132 /* No program header, for now. */
7133 i_ehdrp->e_phoff = 0;
7134 i_ehdrp->e_phentsize = 0;
7135 i_ehdrp->e_phnum = 0;
7137 /* Each bfd section is section header entry. */
7138 i_ehdrp->e_entry = bfd_get_start_address (abfd);
7139 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
7141 elf_tdata (abfd)->symtab_hdr.sh_name =
7142 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
7143 elf_tdata (abfd)->strtab_hdr.sh_name =
7144 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
7145 elf_tdata (abfd)->shstrtab_hdr.sh_name =
7146 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
7147 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
7148 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
7149 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
7150 return false;
7152 return true;
7155 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
7157 FIXME: We used to have code here to sort the PT_LOAD segments into
7158 ascending order, as per the ELF spec. But this breaks some programs,
7159 including the Linux kernel. But really either the spec should be
7160 changed or the programs updated. */
7162 bool
7163 _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
7165 if (link_info != NULL && bfd_link_pie (link_info))
7167 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
7168 unsigned int num_segments = i_ehdrp->e_phnum;
7169 struct elf_obj_tdata *tdata = elf_tdata (obfd);
7170 Elf_Internal_Phdr *segment = tdata->phdr;
7171 Elf_Internal_Phdr *end_segment = &segment[num_segments];
7173 /* Find the lowest p_vaddr in PT_LOAD segments. */
7174 bfd_vma p_vaddr = (bfd_vma) -1;
7175 for (; segment < end_segment; segment++)
7176 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
7177 p_vaddr = segment->p_vaddr;
7179 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
7180 segments is non-zero. */
7181 if (p_vaddr)
7182 i_ehdrp->e_type = ET_EXEC;
7184 return true;
7187 /* Assign file positions for all the reloc sections which are not part
7188 of the loadable file image, and the file position of section headers. */
7190 static bool
7191 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
7193 file_ptr off;
7194 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
7195 Elf_Internal_Shdr *shdrp;
7196 Elf_Internal_Ehdr *i_ehdrp;
7197 const struct elf_backend_data *bed;
7199 /* Skip non-load sections without section header. */
7200 if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
7201 return true;
7203 off = elf_next_file_pos (abfd);
7205 shdrpp = elf_elfsections (abfd);
7206 end_shdrpp = shdrpp + elf_numsections (abfd);
7207 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
7209 shdrp = *shdrpp;
7210 if (shdrp->sh_offset == -1)
7212 asection *sec = shdrp->bfd_section;
7213 if (sec == NULL
7214 || shdrp->sh_type == SHT_REL
7215 || shdrp->sh_type == SHT_RELA)
7217 else if (bfd_section_is_ctf (sec))
7219 /* Update section size and contents. */
7220 shdrp->sh_size = sec->size;
7221 shdrp->contents = sec->contents;
7223 else if (shdrp->sh_name == -1u)
7225 const char *name = sec->name;
7226 struct bfd_elf_section_data *d;
7228 /* Compress DWARF debug sections. */
7229 if (!bfd_compress_section (abfd, sec, shdrp->contents))
7230 return false;
7232 if (sec->compress_status == COMPRESS_SECTION_DONE
7233 && (abfd->flags & BFD_COMPRESS_GABI) == 0
7234 && name[1] == 'd')
7236 /* If section is compressed with zlib-gnu, convert
7237 section name from .debug_* to .zdebug_*. */
7238 char *new_name = bfd_debug_name_to_zdebug (abfd, name);
7239 if (new_name == NULL)
7240 return false;
7241 name = new_name;
7243 /* Add section name to section name section. */
7244 shdrp->sh_name
7245 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
7246 name, false);
7247 d = elf_section_data (sec);
7249 /* Add reloc section name to section name section. */
7250 if (d->rel.hdr
7251 && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr,
7252 name, false))
7253 return false;
7254 if (d->rela.hdr
7255 && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr,
7256 name, true))
7257 return false;
7259 /* Update section size and contents. */
7260 shdrp->sh_size = sec->size;
7261 shdrp->contents = sec->contents;
7262 sec->contents = NULL;
7265 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
7269 /* Place section name section after DWARF debug sections have been
7270 compressed. */
7271 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
7272 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
7273 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
7274 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
7276 /* Place the section headers. */
7277 i_ehdrp = elf_elfheader (abfd);
7278 bed = get_elf_backend_data (abfd);
7279 off = align_file_position (off, 1 << bed->s->log_file_align);
7280 i_ehdrp->e_shoff = off;
7281 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
7282 elf_next_file_pos (abfd) = off;
7284 return true;
7287 bool
7288 _bfd_elf_write_object_contents (bfd *abfd)
7290 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7291 Elf_Internal_Shdr **i_shdrp;
7292 bool failed;
7293 unsigned int count, num_sec;
7294 struct elf_obj_tdata *t;
7296 if (! abfd->output_has_begun
7297 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7298 return false;
7299 /* Do not rewrite ELF data when the BFD has been opened for update.
7300 abfd->output_has_begun was set to TRUE on opening, so creation of
7301 new sections, and modification of existing section sizes was
7302 restricted. This means the ELF header, program headers and
7303 section headers can't have changed. If the contents of any
7304 sections has been modified, then those changes have already been
7305 written to the BFD. */
7306 else if (abfd->direction == both_direction)
7308 BFD_ASSERT (abfd->output_has_begun);
7309 return true;
7312 i_shdrp = elf_elfsections (abfd);
7314 failed = false;
7315 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
7316 if (failed)
7317 return false;
7319 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
7320 return false;
7322 /* After writing the headers, we need to write the sections too... */
7323 num_sec = elf_numsections (abfd);
7324 for (count = 1; count < num_sec; count++)
7326 /* Don't set the sh_name field without section header. */
7327 if ((abfd->flags & BFD_NO_SECTION_HEADER) == 0)
7328 i_shdrp[count]->sh_name
7329 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
7330 i_shdrp[count]->sh_name);
7331 if (bed->elf_backend_section_processing)
7332 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
7333 return false;
7334 if (i_shdrp[count]->contents)
7336 bfd_size_type amt = i_shdrp[count]->sh_size;
7338 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
7339 || bfd_write (i_shdrp[count]->contents, amt, abfd) != amt)
7340 return false;
7344 /* Write out the section header names. */
7345 t = elf_tdata (abfd);
7346 if (elf_shstrtab (abfd) != NULL
7347 && t->shstrtab_hdr.sh_offset != -1
7348 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
7349 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
7350 return false;
7352 if (!(*bed->elf_backend_final_write_processing) (abfd))
7353 return false;
7355 if (!bed->s->write_shdrs_and_ehdr (abfd))
7356 return false;
7358 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
7359 if (t->o->build_id.after_write_object_contents != NULL
7360 && !(*t->o->build_id.after_write_object_contents) (abfd))
7361 return false;
7362 if (t->o->package_metadata.after_write_object_contents != NULL
7363 && !(*t->o->package_metadata.after_write_object_contents) (abfd))
7364 return false;
7366 return true;
7369 bool
7370 _bfd_elf_write_corefile_contents (bfd *abfd)
7372 /* Hopefully this can be done just like an object file. */
7373 return _bfd_elf_write_object_contents (abfd);
7376 /* Given a section, search the header to find them. */
7378 unsigned int
7379 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
7381 const struct elf_backend_data *bed;
7382 unsigned int sec_index;
7384 if (elf_section_data (asect) != NULL
7385 && elf_section_data (asect)->this_idx != 0)
7386 return elf_section_data (asect)->this_idx;
7388 if (bfd_is_abs_section (asect))
7389 sec_index = SHN_ABS;
7390 else if (bfd_is_com_section (asect))
7391 sec_index = SHN_COMMON;
7392 else if (bfd_is_und_section (asect))
7393 sec_index = SHN_UNDEF;
7394 else
7395 sec_index = SHN_BAD;
7397 bed = get_elf_backend_data (abfd);
7398 if (bed->elf_backend_section_from_bfd_section)
7400 int retval = sec_index;
7402 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
7403 return retval;
7406 if (sec_index == SHN_BAD)
7407 bfd_set_error (bfd_error_nonrepresentable_section);
7409 return sec_index;
7412 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
7413 on error. */
7416 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
7418 asymbol *asym_ptr = *asym_ptr_ptr;
7419 int idx;
7420 flagword flags = asym_ptr->flags;
7422 /* When gas creates relocations against local labels, it creates its
7423 own symbol for the section, but does put the symbol into the
7424 symbol chain, so udata is 0. When the linker is generating
7425 relocatable output, this section symbol may be for one of the
7426 input sections rather than the output section. */
7427 if (asym_ptr->udata.i == 0
7428 && (flags & BSF_SECTION_SYM)
7429 && asym_ptr->section)
7431 asection *sec;
7433 sec = asym_ptr->section;
7434 if (sec->owner != abfd && sec->output_section != NULL)
7435 sec = sec->output_section;
7436 if (sec->owner == abfd
7437 && sec->index < elf_num_section_syms (abfd)
7438 && elf_section_syms (abfd)[sec->index] != NULL)
7439 asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
7442 idx = asym_ptr->udata.i;
7444 if (idx == 0)
7446 /* This case can occur when using --strip-symbol on a symbol
7447 which is used in a relocation entry. */
7448 _bfd_error_handler
7449 /* xgettext:c-format */
7450 (_("%pB: symbol `%s' required but not present"),
7451 abfd, bfd_asymbol_name (asym_ptr));
7452 bfd_set_error (bfd_error_no_symbols);
7453 return -1;
7456 #if DEBUG & 4
7458 fprintf (stderr,
7459 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
7460 " flags = 0x%.8x\n",
7461 (long) asym_ptr, asym_ptr->name, idx, flags);
7462 fflush (stderr);
7464 #endif
7466 return idx;
7469 static inline bfd_vma
7470 segment_size (Elf_Internal_Phdr *segment)
7472 return (segment->p_memsz > segment->p_filesz
7473 ? segment->p_memsz : segment->p_filesz);
7477 /* Returns the end address of the segment + 1. */
7478 static inline bfd_vma
7479 segment_end (Elf_Internal_Phdr *segment, bfd_vma start)
7481 return start + segment_size (segment);
7484 static inline bfd_size_type
7485 section_size (asection *section, Elf_Internal_Phdr *segment)
7487 if ((section->flags & SEC_HAS_CONTENTS) != 0
7488 || (section->flags & SEC_THREAD_LOCAL) == 0
7489 || segment->p_type == PT_TLS)
7490 return section->size;
7491 return 0;
7494 /* Returns TRUE if the given section is contained within the given
7495 segment. LMA addresses are compared against PADDR when
7496 USE_VADDR is false, VMA against VADDR when true. */
7497 static bool
7498 is_contained_by (asection *section, Elf_Internal_Phdr *segment,
7499 bfd_vma paddr, bfd_vma vaddr, unsigned int opb,
7500 bool use_vaddr)
7502 bfd_vma seg_addr = !use_vaddr ? paddr : vaddr;
7503 bfd_vma addr = !use_vaddr ? section->lma : section->vma;
7504 bfd_vma octet;
7505 if (_bfd_mul_overflow (addr, opb, &octet))
7506 return false;
7507 /* The third and fourth lines below are testing that the section end
7508 address is within the segment. It's written this way to avoid
7509 overflow. Add seg_addr + section_size to both sides of the
7510 inequality to make it obvious. */
7511 return (octet >= seg_addr
7512 && segment_size (segment) >= section_size (section, segment)
7513 && (octet - seg_addr
7514 <= segment_size (segment) - section_size (section, segment)));
7517 /* Handle PT_NOTE segment. */
7518 static bool
7519 is_note (asection *s, Elf_Internal_Phdr *p)
7521 return (p->p_type == PT_NOTE
7522 && elf_section_type (s) == SHT_NOTE
7523 && (ufile_ptr) s->filepos >= p->p_offset
7524 && p->p_filesz >= s->size
7525 && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
7528 /* Rewrite program header information. */
7530 static bool
7531 rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
7533 Elf_Internal_Ehdr *iehdr;
7534 struct elf_segment_map *map;
7535 struct elf_segment_map *map_first;
7536 struct elf_segment_map **pointer_to_map;
7537 Elf_Internal_Phdr *segment;
7538 asection *section;
7539 unsigned int i;
7540 unsigned int num_segments;
7541 bool phdr_included = false;
7542 bool p_paddr_valid;
7543 struct elf_segment_map *phdr_adjust_seg = NULL;
7544 unsigned int phdr_adjust_num = 0;
7545 const struct elf_backend_data *bed;
7546 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
7548 bed = get_elf_backend_data (ibfd);
7549 iehdr = elf_elfheader (ibfd);
7551 map_first = NULL;
7552 pointer_to_map = &map_first;
7554 num_segments = elf_elfheader (ibfd)->e_phnum;
7556 /* The complicated case when p_vaddr is 0 is to handle the Solaris
7557 linker, which generates a PT_INTERP section with p_vaddr and
7558 p_memsz set to 0. */
7559 #define IS_SOLARIS_PT_INTERP(p, s) \
7560 (p->p_vaddr == 0 \
7561 && p->p_paddr == 0 \
7562 && p->p_memsz == 0 \
7563 && p->p_filesz > 0 \
7564 && (s->flags & SEC_HAS_CONTENTS) != 0 \
7565 && s->size > 0 \
7566 && (bfd_vma) s->filepos >= p->p_offset \
7567 && ((bfd_vma) s->filepos + s->size \
7568 <= p->p_offset + p->p_filesz))
7570 /* Decide if the given section should be included in the given segment.
7571 A section will be included if:
7572 1. It is within the address space of the segment -- we use the LMA
7573 if that is set for the segment and the VMA otherwise,
7574 2. It is an allocated section or a NOTE section in a PT_NOTE
7575 segment.
7576 3. There is an output section associated with it,
7577 4. The section has not already been allocated to a previous segment.
7578 5. PT_GNU_STACK segments do not include any sections.
7579 6. PT_TLS segment includes only SHF_TLS sections.
7580 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
7581 8. PT_DYNAMIC should not contain empty sections at the beginning
7582 (with the possible exception of .dynamic). */
7583 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
7584 (((is_contained_by (section, segment, segment->p_paddr, \
7585 segment->p_vaddr, opb, \
7586 bed->want_p_paddr_set_to_zero) \
7587 && (section->flags & SEC_ALLOC) != 0) \
7588 || is_note (section, segment)) \
7589 && segment->p_type != PT_GNU_STACK \
7590 && (segment->p_type != PT_TLS \
7591 || (section->flags & SEC_THREAD_LOCAL)) \
7592 && (segment->p_type == PT_LOAD \
7593 || segment->p_type == PT_TLS \
7594 || (section->flags & SEC_THREAD_LOCAL) == 0) \
7595 && (segment->p_type != PT_DYNAMIC \
7596 || section_size (section, segment) > 0 \
7597 || (segment->p_paddr \
7598 ? segment->p_paddr != section->lma * (opb) \
7599 : segment->p_vaddr != section->vma * (opb)) \
7600 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
7601 && (segment->p_type != PT_LOAD || !section->segment_mark))
7603 /* If the output section of a section in the input segment is NULL,
7604 it is removed from the corresponding output segment. */
7605 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
7606 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
7607 && section->output_section != NULL)
7609 /* Returns TRUE iff seg1 starts after the end of seg2. */
7610 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
7611 (seg1->field >= segment_end (seg2, seg2->field))
7613 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7614 their VMA address ranges and their LMA address ranges overlap.
7615 It is possible to have overlapping VMA ranges without overlapping LMA
7616 ranges. RedBoot images for example can have both .data and .bss mapped
7617 to the same VMA range, but with the .data section mapped to a different
7618 LMA. */
7619 #define SEGMENT_OVERLAPS(seg1, seg2) \
7620 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
7621 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
7622 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
7623 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7625 /* Initialise the segment mark field, and discard stupid alignment. */
7626 for (section = ibfd->sections; section != NULL; section = section->next)
7628 asection *o = section->output_section;
7629 if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
7630 o->alignment_power = 0;
7631 section->segment_mark = false;
7634 /* The Solaris linker creates program headers in which all the
7635 p_paddr fields are zero. When we try to objcopy or strip such a
7636 file, we get confused. Check for this case, and if we find it
7637 don't set the p_paddr_valid fields. */
7638 p_paddr_valid = false;
7639 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7640 i < num_segments;
7641 i++, segment++)
7642 if (segment->p_paddr != 0)
7644 p_paddr_valid = true;
7645 break;
7648 /* Scan through the segments specified in the program header
7649 of the input BFD. For this first scan we look for overlaps
7650 in the loadable segments. These can be created by weird
7651 parameters to objcopy. Also, fix some solaris weirdness. */
7652 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7653 i < num_segments;
7654 i++, segment++)
7656 unsigned int j;
7657 Elf_Internal_Phdr *segment2;
7659 if (segment->p_type == PT_INTERP)
7660 for (section = ibfd->sections; section; section = section->next)
7661 if (IS_SOLARIS_PT_INTERP (segment, section))
7663 /* Mininal change so that the normal section to segment
7664 assignment code will work. */
7665 segment->p_vaddr = section->vma * opb;
7666 break;
7669 if (segment->p_type != PT_LOAD)
7671 /* Remove PT_GNU_RELRO segment. */
7672 if (segment->p_type == PT_GNU_RELRO)
7673 segment->p_type = PT_NULL;
7674 continue;
7677 /* Determine if this segment overlaps any previous segments. */
7678 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
7680 bfd_signed_vma extra_length;
7682 if (segment2->p_type != PT_LOAD
7683 || !SEGMENT_OVERLAPS (segment, segment2))
7684 continue;
7686 /* Merge the two segments together. */
7687 if (segment2->p_vaddr < segment->p_vaddr)
7689 /* Extend SEGMENT2 to include SEGMENT and then delete
7690 SEGMENT. */
7691 extra_length = (segment_end (segment, segment->p_vaddr)
7692 - segment_end (segment2, segment2->p_vaddr));
7694 if (extra_length > 0)
7696 segment2->p_memsz += extra_length;
7697 segment2->p_filesz += extra_length;
7700 segment->p_type = PT_NULL;
7702 /* Since we have deleted P we must restart the outer loop. */
7703 i = 0;
7704 segment = elf_tdata (ibfd)->phdr;
7705 break;
7707 else
7709 /* Extend SEGMENT to include SEGMENT2 and then delete
7710 SEGMENT2. */
7711 extra_length = (segment_end (segment2, segment2->p_vaddr)
7712 - segment_end (segment, segment->p_vaddr));
7714 if (extra_length > 0)
7716 segment->p_memsz += extra_length;
7717 segment->p_filesz += extra_length;
7720 segment2->p_type = PT_NULL;
7725 /* The second scan attempts to assign sections to segments. */
7726 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7727 i < num_segments;
7728 i++, segment++)
7730 unsigned int section_count;
7731 asection **sections;
7732 asection *output_section;
7733 unsigned int isec;
7734 asection *matching_lma;
7735 asection *suggested_lma;
7736 unsigned int j;
7737 size_t amt;
7738 asection *first_section;
7740 if (segment->p_type == PT_NULL)
7741 continue;
7743 first_section = NULL;
7744 /* Compute how many sections might be placed into this segment. */
7745 for (section = ibfd->sections, section_count = 0;
7746 section != NULL;
7747 section = section->next)
7749 /* Find the first section in the input segment, which may be
7750 removed from the corresponding output segment. */
7751 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb))
7753 if (first_section == NULL)
7754 first_section = section;
7755 if (section->output_section != NULL)
7756 ++section_count;
7760 /* Allocate a segment map big enough to contain
7761 all of the sections we have selected. */
7762 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7763 amt += section_count * sizeof (asection *);
7764 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7765 if (map == NULL)
7766 return false;
7768 /* Initialise the fields of the segment map. Default to
7769 using the physical address of the segment in the input BFD. */
7770 map->next = NULL;
7771 map->p_type = segment->p_type;
7772 map->p_flags = segment->p_flags;
7773 map->p_flags_valid = 1;
7775 if (map->p_type == PT_LOAD
7776 && (ibfd->flags & D_PAGED) != 0
7777 && maxpagesize > 1
7778 && segment->p_align > 1)
7780 map->p_align = segment->p_align;
7781 if (segment->p_align > maxpagesize)
7782 map->p_align = maxpagesize;
7783 map->p_align_valid = 1;
7786 /* If the first section in the input segment is removed, there is
7787 no need to preserve segment physical address in the corresponding
7788 output segment. */
7789 if (!first_section || first_section->output_section != NULL)
7791 map->p_paddr = segment->p_paddr;
7792 map->p_paddr_valid = p_paddr_valid;
7795 /* Determine if this segment contains the ELF file header
7796 and if it contains the program headers themselves. */
7797 map->includes_filehdr = (segment->p_offset == 0
7798 && segment->p_filesz >= iehdr->e_ehsize);
7799 map->includes_phdrs = 0;
7801 if (!phdr_included || segment->p_type != PT_LOAD)
7803 map->includes_phdrs =
7804 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7805 && (segment->p_offset + segment->p_filesz
7806 >= ((bfd_vma) iehdr->e_phoff
7807 + iehdr->e_phnum * iehdr->e_phentsize)));
7809 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7810 phdr_included = true;
7813 if (section_count == 0)
7815 /* Special segments, such as the PT_PHDR segment, may contain
7816 no sections, but ordinary, loadable segments should contain
7817 something. They are allowed by the ELF spec however, so only
7818 a warning is produced.
7819 Don't warn if an empty PT_LOAD contains the program headers.
7820 There is however the valid use case of embedded systems which
7821 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7822 flash memory with zeros. No warning is shown for that case. */
7823 if (segment->p_type == PT_LOAD
7824 && !map->includes_phdrs
7825 && (segment->p_filesz > 0 || segment->p_memsz == 0))
7826 /* xgettext:c-format */
7827 _bfd_error_handler
7828 (_("%pB: warning: empty loadable segment detected"
7829 " at vaddr=%#" PRIx64 ", is this intentional?"),
7830 ibfd, (uint64_t) segment->p_vaddr);
7832 map->p_vaddr_offset = segment->p_vaddr / opb;
7833 map->count = 0;
7834 *pointer_to_map = map;
7835 pointer_to_map = &map->next;
7837 continue;
7840 /* Now scan the sections in the input BFD again and attempt
7841 to add their corresponding output sections to the segment map.
7842 The problem here is how to handle an output section which has
7843 been moved (ie had its LMA changed). There are four possibilities:
7845 1. None of the sections have been moved.
7846 In this case we can continue to use the segment LMA from the
7847 input BFD.
7849 2. All of the sections have been moved by the same amount.
7850 In this case we can change the segment's LMA to match the LMA
7851 of the first section.
7853 3. Some of the sections have been moved, others have not.
7854 In this case those sections which have not been moved can be
7855 placed in the current segment which will have to have its size,
7856 and possibly its LMA changed, and a new segment or segments will
7857 have to be created to contain the other sections.
7859 4. The sections have been moved, but not by the same amount.
7860 In this case we can change the segment's LMA to match the LMA
7861 of the first section and we will have to create a new segment
7862 or segments to contain the other sections.
7864 In order to save time, we allocate an array to hold the section
7865 pointers that we are interested in. As these sections get assigned
7866 to a segment, they are removed from this array. */
7868 amt = section_count * sizeof (asection *);
7869 sections = (asection **) bfd_malloc (amt);
7870 if (sections == NULL)
7871 return false;
7873 /* Step One: Scan for segment vs section LMA conflicts.
7874 Also add the sections to the section array allocated above.
7875 Also add the sections to the current segment. In the common
7876 case, where the sections have not been moved, this means that
7877 we have completely filled the segment, and there is nothing
7878 more to do. */
7879 isec = 0;
7880 matching_lma = NULL;
7881 suggested_lma = NULL;
7883 for (section = first_section, j = 0;
7884 section != NULL;
7885 section = section->next)
7887 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb))
7889 output_section = section->output_section;
7891 sections[j++] = section;
7893 /* The Solaris native linker always sets p_paddr to 0.
7894 We try to catch that case here, and set it to the
7895 correct value. Note - some backends require that
7896 p_paddr be left as zero. */
7897 if (!p_paddr_valid
7898 && segment->p_vaddr != 0
7899 && !bed->want_p_paddr_set_to_zero
7900 && isec == 0
7901 && output_section->lma != 0
7902 && (align_power (segment->p_vaddr
7903 + (map->includes_filehdr
7904 ? iehdr->e_ehsize : 0)
7905 + (map->includes_phdrs
7906 ? iehdr->e_phnum * iehdr->e_phentsize
7907 : 0),
7908 output_section->alignment_power * opb)
7909 == (output_section->vma * opb)))
7910 map->p_paddr = segment->p_vaddr;
7912 /* Match up the physical address of the segment with the
7913 LMA address of the output section. */
7914 if (is_contained_by (output_section, segment, map->p_paddr,
7915 0, opb, false)
7916 || is_note (section, segment))
7918 if (matching_lma == NULL
7919 || output_section->lma < matching_lma->lma)
7920 matching_lma = output_section;
7922 /* We assume that if the section fits within the segment
7923 then it does not overlap any other section within that
7924 segment. */
7925 map->sections[isec++] = output_section;
7927 else if (suggested_lma == NULL)
7928 suggested_lma = output_section;
7930 if (j == section_count)
7931 break;
7935 BFD_ASSERT (j == section_count);
7937 /* Step Two: Adjust the physical address of the current segment,
7938 if necessary. */
7939 if (isec == section_count)
7941 /* All of the sections fitted within the segment as currently
7942 specified. This is the default case. Add the segment to
7943 the list of built segments and carry on to process the next
7944 program header in the input BFD. */
7945 map->count = section_count;
7946 *pointer_to_map = map;
7947 pointer_to_map = &map->next;
7949 if (p_paddr_valid
7950 && !bed->want_p_paddr_set_to_zero)
7952 bfd_vma hdr_size = 0;
7953 if (map->includes_filehdr)
7954 hdr_size = iehdr->e_ehsize;
7955 if (map->includes_phdrs)
7956 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7958 /* Account for padding before the first section in the
7959 segment. */
7960 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7961 - matching_lma->lma);
7964 free (sections);
7965 continue;
7967 else
7969 /* Change the current segment's physical address to match
7970 the LMA of the first section that fitted, or if no
7971 section fitted, the first section. */
7972 if (matching_lma == NULL)
7973 matching_lma = suggested_lma;
7975 map->p_paddr = matching_lma->lma * opb;
7977 /* Offset the segment physical address from the lma
7978 to allow for space taken up by elf headers. */
7979 if (map->includes_phdrs)
7981 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7983 /* iehdr->e_phnum is just an estimate of the number
7984 of program headers that we will need. Make a note
7985 here of the number we used and the segment we chose
7986 to hold these headers, so that we can adjust the
7987 offset when we know the correct value. */
7988 phdr_adjust_num = iehdr->e_phnum;
7989 phdr_adjust_seg = map;
7992 if (map->includes_filehdr)
7994 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7995 map->p_paddr -= iehdr->e_ehsize;
7996 /* We've subtracted off the size of headers from the
7997 first section lma, but there may have been some
7998 alignment padding before that section too. Try to
7999 account for that by adjusting the segment lma down to
8000 the same alignment. */
8001 if (segment->p_align != 0 && segment->p_align < align)
8002 align = segment->p_align;
8003 map->p_paddr &= -(align * opb);
8007 /* Step Three: Loop over the sections again, this time assigning
8008 those that fit to the current segment and removing them from the
8009 sections array; but making sure not to leave large gaps. Once all
8010 possible sections have been assigned to the current segment it is
8011 added to the list of built segments and if sections still remain
8012 to be assigned, a new segment is constructed before repeating
8013 the loop. */
8014 isec = 0;
8017 map->count = 0;
8018 suggested_lma = NULL;
8020 /* Fill the current segment with sections that fit. */
8021 for (j = 0; j < section_count; j++)
8023 section = sections[j];
8025 if (section == NULL)
8026 continue;
8028 output_section = section->output_section;
8030 BFD_ASSERT (output_section != NULL);
8032 if (is_contained_by (output_section, segment, map->p_paddr,
8033 0, opb, false)
8034 || is_note (section, segment))
8036 if (map->count == 0)
8038 /* If the first section in a segment does not start at
8039 the beginning of the segment, then something is
8040 wrong. */
8041 if (align_power (map->p_paddr
8042 + (map->includes_filehdr
8043 ? iehdr->e_ehsize : 0)
8044 + (map->includes_phdrs
8045 ? iehdr->e_phnum * iehdr->e_phentsize
8046 : 0),
8047 output_section->alignment_power * opb)
8048 != output_section->lma * opb)
8049 goto sorry;
8051 else
8053 asection *prev_sec;
8055 prev_sec = map->sections[map->count - 1];
8057 /* If the gap between the end of the previous section
8058 and the start of this section is more than
8059 maxpagesize then we need to start a new segment. */
8060 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
8061 maxpagesize)
8062 < BFD_ALIGN (output_section->lma, maxpagesize))
8063 || (prev_sec->lma + prev_sec->size
8064 > output_section->lma))
8066 if (suggested_lma == NULL)
8067 suggested_lma = output_section;
8069 continue;
8073 map->sections[map->count++] = output_section;
8074 ++isec;
8075 sections[j] = NULL;
8076 if (segment->p_type == PT_LOAD)
8077 section->segment_mark = true;
8079 else if (suggested_lma == NULL)
8080 suggested_lma = output_section;
8083 /* PR 23932. A corrupt input file may contain sections that cannot
8084 be assigned to any segment - because for example they have a
8085 negative size - or segments that do not contain any sections.
8086 But there are also valid reasons why a segment can be empty.
8087 So allow a count of zero. */
8089 /* Add the current segment to the list of built segments. */
8090 *pointer_to_map = map;
8091 pointer_to_map = &map->next;
8093 if (isec < section_count)
8095 /* We still have not allocated all of the sections to
8096 segments. Create a new segment here, initialise it
8097 and carry on looping. */
8098 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8099 amt += section_count * sizeof (asection *);
8100 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
8101 if (map == NULL)
8103 free (sections);
8104 return false;
8107 /* Initialise the fields of the segment map. Set the physical
8108 physical address to the LMA of the first section that has
8109 not yet been assigned. */
8110 map->next = NULL;
8111 map->p_type = segment->p_type;
8112 map->p_flags = segment->p_flags;
8113 map->p_flags_valid = 1;
8114 map->p_paddr = suggested_lma->lma * opb;
8115 map->p_paddr_valid = p_paddr_valid;
8116 map->includes_filehdr = 0;
8117 map->includes_phdrs = 0;
8120 continue;
8121 sorry:
8122 bfd_set_error (bfd_error_sorry);
8123 free (sections);
8124 return false;
8126 while (isec < section_count);
8128 free (sections);
8131 elf_seg_map (obfd) = map_first;
8133 /* If we had to estimate the number of program headers that were
8134 going to be needed, then check our estimate now and adjust
8135 the offset if necessary. */
8136 if (phdr_adjust_seg != NULL)
8138 unsigned int count;
8140 for (count = 0, map = map_first; map != NULL; map = map->next)
8141 count++;
8143 if (count > phdr_adjust_num)
8144 phdr_adjust_seg->p_paddr
8145 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
8147 for (map = map_first; map != NULL; map = map->next)
8148 if (map->p_type == PT_PHDR)
8150 bfd_vma adjust
8151 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
8152 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
8153 break;
8157 #undef IS_SOLARIS_PT_INTERP
8158 #undef IS_SECTION_IN_INPUT_SEGMENT
8159 #undef INCLUDE_SECTION_IN_SEGMENT
8160 #undef SEGMENT_AFTER_SEGMENT
8161 #undef SEGMENT_OVERLAPS
8162 return true;
8165 /* Return true if p_align in the ELF program header in ABFD is valid. */
8167 static bool
8168 elf_is_p_align_valid (bfd *abfd)
8170 unsigned int i;
8171 Elf_Internal_Phdr *segment;
8172 unsigned int num_segments;
8173 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8174 bfd_size_type maxpagesize = bed->maxpagesize;
8175 bfd_size_type p_align = bed->p_align;
8177 /* Return true if the default p_align value isn't set or the maximum
8178 page size is the same as the minimum page size. */
8179 if (p_align == 0 || maxpagesize == bed->minpagesize)
8180 return true;
8182 /* When the default p_align value is set, p_align may be set to the
8183 default p_align value while segments are aligned to the maximum
8184 page size. In this case, the input p_align will be ignored and
8185 the maximum page size will be used to align the output segments. */
8186 segment = elf_tdata (abfd)->phdr;
8187 num_segments = elf_elfheader (abfd)->e_phnum;
8188 for (i = 0; i < num_segments; i++, segment++)
8189 if (segment->p_type == PT_LOAD
8190 && (segment->p_align != p_align
8191 || vma_page_aligned_bias (segment->p_vaddr,
8192 segment->p_offset,
8193 maxpagesize) != 0))
8194 return true;
8196 return false;
8199 /* Copy ELF program header information. */
8201 static bool
8202 copy_elf_program_header (bfd *ibfd, bfd *obfd)
8204 Elf_Internal_Ehdr *iehdr;
8205 struct elf_segment_map *map;
8206 struct elf_segment_map *map_first;
8207 struct elf_segment_map **pointer_to_map;
8208 Elf_Internal_Phdr *segment;
8209 unsigned int i;
8210 unsigned int num_segments;
8211 bool phdr_included = false;
8212 bool p_paddr_valid;
8213 bool p_palign_valid;
8214 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
8216 iehdr = elf_elfheader (ibfd);
8218 map_first = NULL;
8219 pointer_to_map = &map_first;
8221 /* If all the segment p_paddr fields are zero, don't set
8222 map->p_paddr_valid. */
8223 p_paddr_valid = false;
8224 num_segments = elf_elfheader (ibfd)->e_phnum;
8225 for (i = 0, segment = elf_tdata (ibfd)->phdr;
8226 i < num_segments;
8227 i++, segment++)
8228 if (segment->p_paddr != 0)
8230 p_paddr_valid = true;
8231 break;
8234 p_palign_valid = elf_is_p_align_valid (ibfd);
8236 for (i = 0, segment = elf_tdata (ibfd)->phdr;
8237 i < num_segments;
8238 i++, segment++)
8240 asection *section;
8241 unsigned int section_count;
8242 size_t amt;
8243 Elf_Internal_Shdr *this_hdr;
8244 asection *first_section = NULL;
8245 asection *lowest_section;
8247 /* Compute how many sections are in this segment. */
8248 for (section = ibfd->sections, section_count = 0;
8249 section != NULL;
8250 section = section->next)
8252 this_hdr = &(elf_section_data(section)->this_hdr);
8253 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8255 if (first_section == NULL)
8256 first_section = section;
8257 section_count++;
8261 /* Allocate a segment map big enough to contain
8262 all of the sections we have selected. */
8263 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8264 amt += section_count * sizeof (asection *);
8265 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
8266 if (map == NULL)
8267 return false;
8269 /* Initialize the fields of the output segment map with the
8270 input segment. */
8271 map->next = NULL;
8272 map->p_type = segment->p_type;
8273 map->p_flags = segment->p_flags;
8274 map->p_flags_valid = 1;
8275 map->p_paddr = segment->p_paddr;
8276 map->p_paddr_valid = p_paddr_valid;
8277 map->p_align = segment->p_align;
8278 /* Keep p_align of PT_GNU_STACK for stack alignment. */
8279 map->p_align_valid = (map->p_type == PT_GNU_STACK
8280 || p_palign_valid);
8281 map->p_vaddr_offset = 0;
8283 if (map->p_type == PT_GNU_RELRO
8284 || map->p_type == PT_GNU_STACK)
8286 /* The PT_GNU_RELRO segment may contain the first a few
8287 bytes in the .got.plt section even if the whole .got.plt
8288 section isn't in the PT_GNU_RELRO segment. We won't
8289 change the size of the PT_GNU_RELRO segment.
8290 Similarly, PT_GNU_STACK size is significant on uclinux
8291 systems. */
8292 map->p_size = segment->p_memsz;
8293 map->p_size_valid = 1;
8296 /* Determine if this segment contains the ELF file header
8297 and if it contains the program headers themselves. */
8298 map->includes_filehdr = (segment->p_offset == 0
8299 && segment->p_filesz >= iehdr->e_ehsize);
8301 map->includes_phdrs = 0;
8302 if (! phdr_included || segment->p_type != PT_LOAD)
8304 map->includes_phdrs =
8305 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
8306 && (segment->p_offset + segment->p_filesz
8307 >= ((bfd_vma) iehdr->e_phoff
8308 + iehdr->e_phnum * iehdr->e_phentsize)));
8310 if (segment->p_type == PT_LOAD && map->includes_phdrs)
8311 phdr_included = true;
8314 lowest_section = NULL;
8315 if (section_count != 0)
8317 unsigned int isec = 0;
8319 for (section = first_section;
8320 section != NULL;
8321 section = section->next)
8323 this_hdr = &(elf_section_data(section)->this_hdr);
8324 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8326 map->sections[isec++] = section->output_section;
8327 if ((section->flags & SEC_ALLOC) != 0)
8329 bfd_vma seg_off;
8331 if (lowest_section == NULL
8332 || section->lma < lowest_section->lma)
8333 lowest_section = section;
8335 /* Section lmas are set up from PT_LOAD header
8336 p_paddr in _bfd_elf_make_section_from_shdr.
8337 If this header has a p_paddr that disagrees
8338 with the section lma, flag the p_paddr as
8339 invalid. */
8340 if ((section->flags & SEC_LOAD) != 0)
8341 seg_off = this_hdr->sh_offset - segment->p_offset;
8342 else
8343 seg_off = this_hdr->sh_addr - segment->p_vaddr;
8344 if (section->lma * opb - segment->p_paddr != seg_off)
8345 map->p_paddr_valid = false;
8347 if (isec == section_count)
8348 break;
8353 if (section_count == 0)
8354 map->p_vaddr_offset = segment->p_vaddr / opb;
8355 else if (map->p_paddr_valid)
8357 /* Account for padding before the first section in the segment. */
8358 bfd_vma hdr_size = 0;
8359 if (map->includes_filehdr)
8360 hdr_size = iehdr->e_ehsize;
8361 if (map->includes_phdrs)
8362 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
8364 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
8365 - (lowest_section ? lowest_section->lma : 0));
8368 map->count = section_count;
8369 *pointer_to_map = map;
8370 pointer_to_map = &map->next;
8373 elf_seg_map (obfd) = map_first;
8374 return true;
8377 /* Copy private BFD data. This copies or rewrites ELF program header
8378 information. */
8380 static bool
8381 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
8383 bfd_vma maxpagesize;
8385 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8386 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8387 return true;
8389 if (elf_tdata (ibfd)->phdr == NULL)
8390 return true;
8392 if (ibfd->xvec == obfd->xvec)
8394 /* Check to see if any sections in the input BFD
8395 covered by ELF program header have changed. */
8396 Elf_Internal_Phdr *segment;
8397 asection * section;
8398 asection * osec;
8399 asection * prev;
8400 unsigned int i, num_segments;
8401 Elf_Internal_Shdr *this_hdr;
8402 const struct elf_backend_data *bed;
8404 bed = get_elf_backend_data (ibfd);
8406 /* Regenerate the segment map if p_paddr is set to 0. */
8407 if (bed->want_p_paddr_set_to_zero)
8408 goto rewrite;
8410 /* Initialize the segment mark field. */
8411 for (section = obfd->sections; section != NULL;
8412 section = section->next)
8413 section->segment_mark = false;
8415 num_segments = elf_elfheader (ibfd)->e_phnum;
8416 for (i = 0, segment = elf_tdata (ibfd)->phdr;
8417 i < num_segments;
8418 i++, segment++)
8420 /* PR binutils/3535. The Solaris linker always sets the p_paddr
8421 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
8422 which severly confuses things, so always regenerate the segment
8423 map in this case. */
8424 if (segment->p_paddr == 0
8425 && segment->p_memsz == 0
8426 && (segment->p_type == PT_INTERP
8427 || segment->p_type == PT_DYNAMIC))
8428 goto rewrite;
8430 for (section = ibfd->sections, prev = NULL;
8431 section != NULL; section = section->next)
8433 /* We mark the output section so that we know it comes
8434 from the input BFD. */
8435 osec = section->output_section;
8436 if (osec)
8437 osec->segment_mark = true;
8439 /* Check if this section is covered by the segment. */
8440 this_hdr = &(elf_section_data(section)->this_hdr);
8441 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8443 /* FIXME: Check if its output section is changed or
8444 removed. What else do we need to check? */
8445 if (osec == NULL
8446 || section->flags != osec->flags
8447 || section->lma != osec->lma
8448 || section->vma != osec->vma
8449 || section->size != osec->size
8450 || section->rawsize != osec->rawsize
8451 || section->alignment_power != osec->alignment_power)
8452 goto rewrite;
8454 /* PR 31450: If this is an allocated section then make sure
8455 that this section's vma to lma relationship is the same
8456 as previous (allocated) section's. */
8457 if (prev != NULL
8458 && section->flags & SEC_ALLOC
8459 && section->lma - section->vma != prev->lma - prev->vma)
8460 goto rewrite;
8462 if (section->flags & SEC_ALLOC)
8463 prev = section;
8468 /* Check to see if any output section do not come from the
8469 input BFD. */
8470 for (section = obfd->sections; section != NULL;
8471 section = section->next)
8473 if (!section->segment_mark)
8474 goto rewrite;
8475 else
8476 section->segment_mark = false;
8479 return copy_elf_program_header (ibfd, obfd);
8482 rewrite:
8483 maxpagesize = 0;
8484 if (ibfd->xvec == obfd->xvec)
8486 /* When rewriting program header, set the output maxpagesize to
8487 the maximum alignment of input PT_LOAD segments. */
8488 Elf_Internal_Phdr *segment;
8489 unsigned int i;
8490 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
8492 for (i = 0, segment = elf_tdata (ibfd)->phdr;
8493 i < num_segments;
8494 i++, segment++)
8495 if (segment->p_type == PT_LOAD
8496 && maxpagesize < segment->p_align)
8498 /* PR 17512: file: f17299af. */
8499 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
8500 /* xgettext:c-format */
8501 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
8502 PRIx64 " is too large"),
8503 ibfd, (uint64_t) segment->p_align);
8504 else
8505 maxpagesize = segment->p_align;
8508 if (maxpagesize == 0)
8509 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
8511 return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
8514 /* Initialize private output section information from input section. */
8516 bool
8517 _bfd_elf_init_private_section_data (bfd *ibfd,
8518 asection *isec,
8519 bfd *obfd,
8520 asection *osec,
8521 struct bfd_link_info *link_info)
8524 Elf_Internal_Shdr *ihdr, *ohdr;
8525 bool final_link = (link_info != NULL
8526 && !bfd_link_relocatable (link_info));
8528 if (ibfd->xvec->flavour != bfd_target_elf_flavour
8529 || obfd->xvec->flavour != bfd_target_elf_flavour)
8530 return true;
8532 BFD_ASSERT (elf_section_data (osec) != NULL);
8534 /* If this is a known ABI section, ELF section type and flags may
8535 have been set up when OSEC was created. For normal sections we
8536 allow the user to override the type and flags other than
8537 SHF_MASKOS and SHF_MASKPROC. */
8538 if (elf_section_type (osec) == SHT_PROGBITS
8539 || elf_section_type (osec) == SHT_NOTE
8540 || elf_section_type (osec) == SHT_NOBITS)
8541 elf_section_type (osec) = SHT_NULL;
8542 /* For objcopy and relocatable link, copy the ELF section type from
8543 the input file if the BFD section flags are the same. (If they
8544 are different the user may be doing something like
8545 "objcopy --set-section-flags .text=alloc,data".) For a final
8546 link allow some flags that the linker clears to differ. */
8547 if (elf_section_type (osec) == SHT_NULL
8548 && (osec->flags == isec->flags
8549 || (final_link
8550 && ((osec->flags ^ isec->flags)
8551 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
8552 elf_section_type (osec) = elf_section_type (isec);
8554 /* FIXME: Is this correct for all OS/PROC specific flags? */
8555 elf_section_flags (osec) = (elf_section_flags (isec)
8556 & (SHF_MASKOS | SHF_MASKPROC));
8558 /* Copy sh_info from input for mbind section. */
8559 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
8560 && elf_section_flags (isec) & SHF_GNU_MBIND)
8561 elf_section_data (osec)->this_hdr.sh_info
8562 = elf_section_data (isec)->this_hdr.sh_info;
8564 /* Set things up for objcopy and relocatable link. The output
8565 SHT_GROUP section will have its elf_next_in_group pointing back
8566 to the input group members. Ignore linker created group section.
8567 See elfNN_ia64_object_p in elfxx-ia64.c. */
8568 if ((link_info == NULL
8569 || !link_info->resolve_section_groups)
8570 && (elf_sec_group (isec) == NULL
8571 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
8573 if (elf_section_flags (isec) & SHF_GROUP)
8574 elf_section_flags (osec) |= SHF_GROUP;
8575 elf_next_in_group (osec) = elf_next_in_group (isec);
8576 elf_section_data (osec)->group = elf_section_data (isec)->group;
8579 /* If not decompress, preserve SHF_COMPRESSED. */
8580 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
8581 elf_section_flags (osec) |= (elf_section_flags (isec)
8582 & SHF_COMPRESSED);
8584 ihdr = &elf_section_data (isec)->this_hdr;
8586 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
8587 don't use the output section of the linked-to section since it
8588 may be NULL at this point. */
8589 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
8591 ohdr = &elf_section_data (osec)->this_hdr;
8592 ohdr->sh_flags |= SHF_LINK_ORDER;
8593 elf_linked_to_section (osec) = elf_linked_to_section (isec);
8596 osec->use_rela_p = isec->use_rela_p;
8598 return true;
8601 /* Copy private section information. This copies over the entsize
8602 field, and sometimes the info field. */
8604 bool
8605 _bfd_elf_copy_private_section_data (bfd *ibfd,
8606 asection *isec,
8607 bfd *obfd,
8608 asection *osec)
8610 Elf_Internal_Shdr *ihdr, *ohdr;
8612 if (ibfd->xvec->flavour != bfd_target_elf_flavour
8613 || obfd->xvec->flavour != bfd_target_elf_flavour)
8614 return true;
8616 ihdr = &elf_section_data (isec)->this_hdr;
8617 ohdr = &elf_section_data (osec)->this_hdr;
8619 ohdr->sh_entsize = ihdr->sh_entsize;
8621 if (ihdr->sh_type == SHT_SYMTAB
8622 || ihdr->sh_type == SHT_DYNSYM
8623 || ihdr->sh_type == SHT_GNU_verneed
8624 || ihdr->sh_type == SHT_GNU_verdef)
8625 ohdr->sh_info = ihdr->sh_info;
8627 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
8628 NULL);
8631 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8632 necessary if we are removing either the SHT_GROUP section or any of
8633 the group member sections. DISCARDED is the value that a section's
8634 output_section has if the section will be discarded, NULL when this
8635 function is called from objcopy, bfd_abs_section_ptr when called
8636 from the linker. */
8638 bool
8639 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
8641 asection *isec;
8643 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
8644 if (elf_section_type (isec) == SHT_GROUP)
8646 asection *first = elf_next_in_group (isec);
8647 asection *s = first;
8648 bfd_size_type removed = 0;
8650 while (s != NULL)
8652 /* If this member section is being output but the
8653 SHT_GROUP section is not, then clear the group info
8654 set up by _bfd_elf_copy_private_section_data. */
8655 if (s->output_section != discarded
8656 && isec->output_section == discarded)
8658 elf_section_flags (s->output_section) &= ~SHF_GROUP;
8659 elf_group_name (s->output_section) = NULL;
8661 else
8663 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
8664 if (s->output_section == discarded
8665 && isec->output_section != discarded)
8667 /* Conversely, if the member section is not being
8668 output but the SHT_GROUP section is, then adjust
8669 its size. */
8670 removed += 4;
8671 if (elf_sec->rel.hdr != NULL
8672 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
8673 removed += 4;
8674 if (elf_sec->rela.hdr != NULL
8675 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
8676 removed += 4;
8678 else
8680 /* Also adjust for zero-sized relocation member
8681 section. */
8682 if (elf_sec->rel.hdr != NULL
8683 && elf_sec->rel.hdr->sh_size == 0)
8684 removed += 4;
8685 if (elf_sec->rela.hdr != NULL
8686 && elf_sec->rela.hdr->sh_size == 0)
8687 removed += 4;
8690 s = elf_next_in_group (s);
8691 if (s == first)
8692 break;
8694 if (removed != 0)
8696 if (discarded != NULL)
8698 /* If we've been called for ld -r, then we need to
8699 adjust the input section size. */
8700 if (isec->rawsize == 0)
8701 isec->rawsize = isec->size;
8702 isec->size = isec->rawsize - removed;
8703 if (isec->size <= 4)
8705 isec->size = 0;
8706 isec->flags |= SEC_EXCLUDE;
8709 else if (isec->output_section != NULL)
8711 /* Adjust the output section size when called from
8712 objcopy. */
8713 isec->output_section->size -= removed;
8714 if (isec->output_section->size <= 4)
8716 isec->output_section->size = 0;
8717 isec->output_section->flags |= SEC_EXCLUDE;
8723 return true;
8726 /* Copy private header information. */
8728 bool
8729 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8731 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8732 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8733 return true;
8735 /* Copy over private BFD data if it has not already been copied.
8736 This must be done here, rather than in the copy_private_bfd_data
8737 entry point, because the latter is called after the section
8738 contents have been set, which means that the program headers have
8739 already been worked out. */
8740 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
8742 if (! copy_private_bfd_data (ibfd, obfd))
8743 return false;
8746 return _bfd_elf_fixup_group_sections (ibfd, NULL);
8749 /* Copy private symbol information. If this symbol is in a section
8750 which we did not map into a BFD section, try to map the section
8751 index correctly. We use special macro definitions for the mapped
8752 section indices; these definitions are interpreted by the
8753 swap_out_syms function. */
8755 #define MAP_ONESYMTAB (SHN_HIOS + 1)
8756 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
8757 #define MAP_STRTAB (SHN_HIOS + 3)
8758 #define MAP_SHSTRTAB (SHN_HIOS + 4)
8759 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
8761 bool
8762 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
8763 asymbol *isymarg,
8764 bfd *obfd,
8765 asymbol *osymarg)
8767 elf_symbol_type *isym, *osym;
8769 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8770 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8771 return true;
8773 isym = elf_symbol_from (isymarg);
8774 osym = elf_symbol_from (osymarg);
8776 if (isym != NULL
8777 && isym->internal_elf_sym.st_shndx != 0
8778 && osym != NULL
8779 && bfd_is_abs_section (isym->symbol.section))
8781 unsigned int shndx;
8783 shndx = isym->internal_elf_sym.st_shndx;
8784 if (shndx == elf_onesymtab (ibfd))
8785 shndx = MAP_ONESYMTAB;
8786 else if (shndx == elf_dynsymtab (ibfd))
8787 shndx = MAP_DYNSYMTAB;
8788 else if (shndx == elf_strtab_sec (ibfd))
8789 shndx = MAP_STRTAB;
8790 else if (shndx == elf_shstrtab_sec (ibfd))
8791 shndx = MAP_SHSTRTAB;
8792 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
8793 shndx = MAP_SYM_SHNDX;
8794 osym->internal_elf_sym.st_shndx = shndx;
8797 return true;
8800 /* Swap out the symbols. */
8802 static bool
8803 swap_out_syms (bfd *abfd,
8804 struct elf_strtab_hash **sttp,
8805 int relocatable_p,
8806 struct bfd_link_info *info)
8808 const struct elf_backend_data *bed;
8809 unsigned int symcount;
8810 asymbol **syms;
8811 struct elf_strtab_hash *stt;
8812 Elf_Internal_Shdr *symtab_hdr;
8813 Elf_Internal_Shdr *symtab_shndx_hdr;
8814 Elf_Internal_Shdr *symstrtab_hdr;
8815 struct elf_sym_strtab *symstrtab;
8816 bfd_byte *outbound_syms;
8817 bfd_byte *outbound_shndx;
8818 unsigned long outbound_syms_index;
8819 unsigned int idx;
8820 unsigned int num_locals;
8821 size_t amt;
8822 bool name_local_sections;
8824 if (!elf_map_symbols (abfd, &num_locals))
8825 return false;
8827 /* Dump out the symtabs. */
8828 stt = _bfd_elf_strtab_init ();
8829 if (stt == NULL)
8830 return false;
8832 bed = get_elf_backend_data (abfd);
8833 symcount = bfd_get_symcount (abfd);
8834 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8835 symtab_hdr->sh_type = SHT_SYMTAB;
8836 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8837 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
8838 symtab_hdr->sh_info = num_locals + 1;
8839 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
8841 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8842 symstrtab_hdr->sh_type = SHT_STRTAB;
8844 /* Allocate buffer to swap out the .strtab section. */
8845 if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8846 || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
8848 bfd_set_error (bfd_error_no_memory);
8849 _bfd_elf_strtab_free (stt);
8850 return false;
8853 if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8854 || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
8856 error_no_mem:
8857 bfd_set_error (bfd_error_no_memory);
8858 error_return:
8859 free (symstrtab);
8860 _bfd_elf_strtab_free (stt);
8861 return false;
8863 symtab_hdr->contents = outbound_syms;
8864 outbound_syms_index = 0;
8866 outbound_shndx = NULL;
8868 if (elf_symtab_shndx_list (abfd))
8870 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8871 if (symtab_shndx_hdr->sh_name != 0)
8873 if (_bfd_mul_overflow (symcount + 1,
8874 sizeof (Elf_External_Sym_Shndx), &amt))
8875 goto error_no_mem;
8876 outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt);
8877 if (outbound_shndx == NULL)
8878 goto error_return;
8880 symtab_shndx_hdr->contents = outbound_shndx;
8881 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8882 symtab_shndx_hdr->sh_size = amt;
8883 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8884 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8886 /* FIXME: What about any other headers in the list ? */
8889 /* Now generate the data (for "contents"). */
8891 /* Fill in zeroth symbol and swap it out. */
8892 Elf_Internal_Sym sym;
8893 sym.st_name = 0;
8894 sym.st_value = 0;
8895 sym.st_size = 0;
8896 sym.st_info = 0;
8897 sym.st_other = 0;
8898 sym.st_shndx = SHN_UNDEF;
8899 sym.st_target_internal = 0;
8900 symstrtab[0].sym = sym;
8901 symstrtab[0].dest_index = outbound_syms_index;
8902 outbound_syms_index++;
8905 name_local_sections
8906 = (bed->elf_backend_name_local_section_symbols
8907 && bed->elf_backend_name_local_section_symbols (abfd));
8909 syms = bfd_get_outsymbols (abfd);
8910 for (idx = 0; idx < symcount;)
8912 Elf_Internal_Sym sym;
8913 bfd_vma value = syms[idx]->value;
8914 elf_symbol_type *type_ptr;
8915 flagword flags = syms[idx]->flags;
8916 int type;
8918 if (!name_local_sections
8919 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8921 /* Local section symbols have no name. */
8922 sym.st_name = (unsigned long) -1;
8924 else
8926 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8927 to get the final offset for st_name. */
8928 sym.st_name
8929 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
8930 false);
8931 if (sym.st_name == (unsigned long) -1)
8932 goto error_return;
8935 type_ptr = elf_symbol_from (syms[idx]);
8937 if ((flags & BSF_SECTION_SYM) == 0
8938 && bfd_is_com_section (syms[idx]->section))
8940 /* ELF common symbols put the alignment into the `value' field,
8941 and the size into the `size' field. This is backwards from
8942 how BFD handles it, so reverse it here. */
8943 sym.st_size = value;
8944 if (type_ptr == NULL
8945 || type_ptr->internal_elf_sym.st_value == 0)
8946 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8947 else
8948 sym.st_value = type_ptr->internal_elf_sym.st_value;
8949 sym.st_shndx = _bfd_elf_section_from_bfd_section
8950 (abfd, syms[idx]->section);
8952 else
8954 asection *sec = syms[idx]->section;
8955 unsigned int shndx;
8957 if (sec->output_section)
8959 value += sec->output_offset;
8960 sec = sec->output_section;
8963 /* Don't add in the section vma for relocatable output. */
8964 if (! relocatable_p)
8965 value += sec->vma;
8966 sym.st_value = value;
8967 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8969 if (bfd_is_abs_section (sec)
8970 && type_ptr != NULL
8971 && type_ptr->internal_elf_sym.st_shndx != 0)
8973 /* This symbol is in a real ELF section which we did
8974 not create as a BFD section. Undo the mapping done
8975 by copy_private_symbol_data. */
8976 shndx = type_ptr->internal_elf_sym.st_shndx;
8977 switch (shndx)
8979 case MAP_ONESYMTAB:
8980 shndx = elf_onesymtab (abfd);
8981 break;
8982 case MAP_DYNSYMTAB:
8983 shndx = elf_dynsymtab (abfd);
8984 break;
8985 case MAP_STRTAB:
8986 shndx = elf_strtab_sec (abfd);
8987 break;
8988 case MAP_SHSTRTAB:
8989 shndx = elf_shstrtab_sec (abfd);
8990 break;
8991 case MAP_SYM_SHNDX:
8992 if (elf_symtab_shndx_list (abfd))
8993 shndx = elf_symtab_shndx_list (abfd)->ndx;
8994 break;
8995 case SHN_COMMON:
8996 case SHN_ABS:
8997 shndx = SHN_ABS;
8998 break;
8999 default:
9000 if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
9002 if (bed->symbol_section_index)
9003 shndx = bed->symbol_section_index (abfd, type_ptr);
9004 /* Otherwise just leave the index alone. */
9006 else
9008 if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
9009 _bfd_error_handler (_("%pB: \
9010 Unable to handle section index %x in ELF symbol. Using ABS instead."),
9011 abfd, shndx);
9012 shndx = SHN_ABS;
9014 break;
9017 else
9019 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
9021 if (shndx == SHN_BAD)
9023 asection *sec2;
9025 /* Writing this would be a hell of a lot easier if
9026 we had some decent documentation on bfd, and
9027 knew what to expect of the library, and what to
9028 demand of applications. For example, it
9029 appears that `objcopy' might not set the
9030 section of a symbol to be a section that is
9031 actually in the output file. */
9032 sec2 = bfd_get_section_by_name (abfd, sec->name);
9033 if (sec2 != NULL)
9034 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
9035 if (shndx == SHN_BAD)
9037 /* xgettext:c-format */
9038 _bfd_error_handler
9039 (_("unable to find equivalent output section"
9040 " for symbol '%s' from section '%s'"),
9041 syms[idx]->name ? syms[idx]->name : "<Local sym>",
9042 sec->name);
9043 bfd_set_error (bfd_error_invalid_operation);
9044 goto error_return;
9049 sym.st_shndx = shndx;
9052 if ((flags & BSF_THREAD_LOCAL) != 0)
9053 type = STT_TLS;
9054 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
9055 type = STT_GNU_IFUNC;
9056 else if ((flags & BSF_FUNCTION) != 0)
9057 type = STT_FUNC;
9058 else if ((flags & BSF_OBJECT) != 0)
9059 type = STT_OBJECT;
9060 else if ((flags & BSF_RELC) != 0)
9061 type = STT_RELC;
9062 else if ((flags & BSF_SRELC) != 0)
9063 type = STT_SRELC;
9064 else
9065 type = STT_NOTYPE;
9067 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
9068 type = STT_TLS;
9070 /* Processor-specific types. */
9071 if (type_ptr != NULL
9072 && bed->elf_backend_get_symbol_type)
9073 type = ((*bed->elf_backend_get_symbol_type)
9074 (&type_ptr->internal_elf_sym, type));
9076 if (flags & BSF_SECTION_SYM)
9078 if (flags & BSF_GLOBAL)
9079 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9080 else
9081 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
9083 else if (bfd_is_com_section (syms[idx]->section))
9085 if (type != STT_TLS)
9087 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
9088 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
9089 ? STT_COMMON : STT_OBJECT);
9090 else
9091 type = ((flags & BSF_ELF_COMMON) != 0
9092 ? STT_COMMON : STT_OBJECT);
9094 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9096 else if (bfd_is_und_section (syms[idx]->section))
9097 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
9098 ? STB_WEAK
9099 : STB_GLOBAL),
9100 type);
9101 else if (flags & BSF_FILE)
9102 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9103 else
9105 int bind = STB_LOCAL;
9107 if (flags & BSF_LOCAL)
9108 bind = STB_LOCAL;
9109 else if (flags & BSF_GNU_UNIQUE)
9110 bind = STB_GNU_UNIQUE;
9111 else if (flags & BSF_WEAK)
9112 bind = STB_WEAK;
9113 else if (flags & BSF_GLOBAL)
9114 bind = STB_GLOBAL;
9116 sym.st_info = ELF_ST_INFO (bind, type);
9119 if (type_ptr != NULL)
9121 sym.st_other = type_ptr->internal_elf_sym.st_other;
9122 sym.st_target_internal
9123 = type_ptr->internal_elf_sym.st_target_internal;
9125 else
9127 sym.st_other = 0;
9128 sym.st_target_internal = 0;
9131 idx++;
9132 symstrtab[idx].sym = sym;
9133 symstrtab[idx].dest_index = outbound_syms_index;
9135 outbound_syms_index++;
9138 /* Finalize the .strtab section. */
9139 _bfd_elf_strtab_finalize (stt);
9141 /* Swap out the .strtab section. */
9142 for (idx = 0; idx <= symcount; idx++)
9144 struct elf_sym_strtab *elfsym = &symstrtab[idx];
9145 if (elfsym->sym.st_name == (unsigned long) -1)
9146 elfsym->sym.st_name = 0;
9147 else
9148 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
9149 elfsym->sym.st_name);
9150 if (info && info->callbacks->ctf_new_symbol)
9151 info->callbacks->ctf_new_symbol (elfsym->dest_index,
9152 &elfsym->sym);
9154 /* Inform the linker of the addition of this symbol. */
9156 bed->s->swap_symbol_out (abfd, &elfsym->sym,
9157 (outbound_syms
9158 + (elfsym->dest_index
9159 * bed->s->sizeof_sym)),
9160 NPTR_ADD (outbound_shndx,
9161 (elfsym->dest_index
9162 * sizeof (Elf_External_Sym_Shndx))));
9164 free (symstrtab);
9166 *sttp = stt;
9167 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
9168 symstrtab_hdr->sh_type = SHT_STRTAB;
9169 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
9170 symstrtab_hdr->sh_addr = 0;
9171 symstrtab_hdr->sh_entsize = 0;
9172 symstrtab_hdr->sh_link = 0;
9173 symstrtab_hdr->sh_info = 0;
9174 symstrtab_hdr->sh_addralign = 1;
9176 return true;
9179 /* Return the number of bytes required to hold the symtab vector.
9181 Note that we base it on the count plus 1, since we will null terminate
9182 the vector allocated based on this size. However, the ELF symbol table
9183 always has a dummy entry as symbol #0, so it ends up even. */
9185 long
9186 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
9188 bfd_size_type symcount;
9189 long symtab_size;
9190 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
9192 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9193 if (symcount > LONG_MAX / sizeof (asymbol *))
9195 bfd_set_error (bfd_error_file_too_big);
9196 return -1;
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_dynamic_symtab_upper_bound (bfd *abfd)
9218 bfd_size_type symcount;
9219 long symtab_size;
9220 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
9222 if (elf_dynsymtab (abfd) == 0)
9224 /* Check if there is dynamic symbol table. */
9225 symcount = elf_tdata (abfd)->dt_symtab_count;
9226 if (symcount)
9227 goto compute_symtab_size;
9229 bfd_set_error (bfd_error_invalid_operation);
9230 return -1;
9233 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9234 if (symcount > LONG_MAX / sizeof (asymbol *))
9236 bfd_set_error (bfd_error_file_too_big);
9237 return -1;
9240 compute_symtab_size:
9241 symtab_size = symcount * (sizeof (asymbol *));
9242 if (symcount == 0)
9243 symtab_size = sizeof (asymbol *);
9244 else if (!bfd_write_p (abfd))
9246 ufile_ptr filesize = bfd_get_file_size (abfd);
9248 if (filesize != 0 && (unsigned long) symtab_size > filesize)
9250 bfd_set_error (bfd_error_file_truncated);
9251 return -1;
9255 return symtab_size;
9258 long
9259 _bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
9261 if (asect->reloc_count != 0 && !bfd_write_p (abfd))
9263 /* Sanity check reloc section size. */
9264 ufile_ptr filesize = bfd_get_file_size (abfd);
9266 if (filesize != 0)
9268 struct bfd_elf_section_data *d = elf_section_data (asect);
9269 bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
9270 bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
9272 if (rel_size + rela_size > filesize
9273 || rel_size + rela_size < rel_size)
9275 bfd_set_error (bfd_error_file_truncated);
9276 return -1;
9281 #if SIZEOF_LONG == SIZEOF_INT
9282 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
9284 bfd_set_error (bfd_error_file_too_big);
9285 return -1;
9287 #endif
9288 return (asect->reloc_count + 1L) * sizeof (arelent *);
9291 /* Canonicalize the relocs. */
9293 long
9294 _bfd_elf_canonicalize_reloc (bfd *abfd,
9295 sec_ptr section,
9296 arelent **relptr,
9297 asymbol **symbols)
9299 arelent *tblptr;
9300 unsigned int i;
9301 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9303 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
9304 return -1;
9306 tblptr = section->relocation;
9307 for (i = 0; i < section->reloc_count; i++)
9308 *relptr++ = tblptr++;
9310 *relptr = NULL;
9312 return section->reloc_count;
9315 long
9316 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
9318 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9319 long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
9321 if (symcount >= 0)
9322 abfd->symcount = symcount;
9323 return symcount;
9326 long
9327 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
9328 asymbol **allocation)
9330 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9331 long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
9333 if (symcount >= 0)
9334 abfd->dynsymcount = symcount;
9335 return symcount;
9338 /* Return the size required for the dynamic reloc entries. Any loadable
9339 section that was actually installed in the BFD, and has type SHT_REL
9340 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
9341 dynamic reloc section. */
9343 long
9344 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
9346 bfd_size_type count, ext_rel_size;
9347 asection *s;
9349 if (elf_dynsymtab (abfd) == 0)
9351 bfd_set_error (bfd_error_invalid_operation);
9352 return -1;
9355 count = 1;
9356 ext_rel_size = 0;
9357 for (s = abfd->sections; s != NULL; s = s->next)
9358 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9359 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9360 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9361 && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9363 ext_rel_size += elf_section_data (s)->this_hdr.sh_size;
9364 if (ext_rel_size < elf_section_data (s)->this_hdr.sh_size)
9366 bfd_set_error (bfd_error_file_truncated);
9367 return -1;
9369 count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9370 if (count > LONG_MAX / sizeof (arelent *))
9372 bfd_set_error (bfd_error_file_too_big);
9373 return -1;
9376 if (count > 1 && !bfd_write_p (abfd))
9378 /* Sanity check reloc section sizes. */
9379 ufile_ptr filesize = bfd_get_file_size (abfd);
9380 if (filesize != 0 && ext_rel_size > filesize)
9382 bfd_set_error (bfd_error_file_truncated);
9383 return -1;
9386 return count * sizeof (arelent *);
9389 /* Canonicalize the dynamic relocation entries. Note that we return the
9390 dynamic relocations as a single block, although they are actually
9391 associated with particular sections; the interface, which was
9392 designed for SunOS style shared libraries, expects that there is only
9393 one set of dynamic relocs. Any loadable section that was actually
9394 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
9395 dynamic symbol table, is considered to be a dynamic reloc section. */
9397 long
9398 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
9399 arelent **storage,
9400 asymbol **syms)
9402 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
9403 asection *s;
9404 long ret;
9406 if (elf_dynsymtab (abfd) == 0)
9408 bfd_set_error (bfd_error_invalid_operation);
9409 return -1;
9412 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
9413 ret = 0;
9414 for (s = abfd->sections; s != NULL; s = s->next)
9416 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9417 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9418 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9419 && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9421 arelent *p;
9422 long count, i;
9424 if (! (*slurp_relocs) (abfd, s, syms, true))
9425 return -1;
9426 count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9427 p = s->relocation;
9428 for (i = 0; i < count; i++)
9429 *storage++ = p++;
9430 ret += count;
9434 *storage = NULL;
9436 return ret;
9439 /* Read in the version information. */
9441 bool
9442 _bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
9444 bfd_byte *contents = NULL;
9445 unsigned int freeidx = 0;
9446 size_t amt;
9447 void *contents_addr = NULL;
9448 size_t contents_size = 0;
9450 if (elf_dynverref (abfd) != 0 || elf_tdata (abfd)->dt_verneed != NULL)
9452 Elf_Internal_Shdr *hdr;
9453 Elf_External_Verneed *everneed;
9454 Elf_Internal_Verneed *iverneed;
9455 unsigned int i;
9456 bfd_byte *contents_end;
9457 size_t verneed_count;
9458 size_t verneed_size;
9460 if (elf_tdata (abfd)->dt_verneed != NULL)
9462 hdr = NULL;
9463 contents = elf_tdata (abfd)->dt_verneed;
9464 verneed_count = elf_tdata (abfd)->dt_verneed_count;
9465 verneed_size = verneed_count * sizeof (Elf_External_Verneed);
9467 else
9469 hdr = &elf_tdata (abfd)->dynverref_hdr;
9471 if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
9473 error_return_bad_verref:
9474 _bfd_error_handler
9475 (_("%pB: .gnu.version_r invalid entry"), abfd);
9476 bfd_set_error (bfd_error_bad_value);
9477 error_return_verref:
9478 elf_tdata (abfd)->verref = NULL;
9479 elf_tdata (abfd)->cverrefs = 0;
9480 goto error_return;
9483 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9484 goto error_return_verref;
9485 contents_size = hdr->sh_size;
9486 contents = _bfd_mmap_readonly_temporary (abfd, contents_size,
9487 &contents_addr,
9488 &contents_size);
9489 if (contents == NULL)
9490 goto error_return_verref;
9492 verneed_size = hdr->sh_size;
9493 verneed_count = hdr->sh_info;
9496 if (_bfd_mul_overflow (verneed_count,
9497 sizeof (Elf_Internal_Verneed), &amt))
9499 bfd_set_error (bfd_error_file_too_big);
9500 goto error_return_verref;
9502 if (amt == 0)
9503 goto error_return_verref;
9504 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
9505 if (elf_tdata (abfd)->verref == NULL)
9506 goto error_return_verref;
9508 BFD_ASSERT (sizeof (Elf_External_Verneed)
9509 == sizeof (Elf_External_Vernaux));
9510 contents_end = (contents + verneed_size
9511 - sizeof (Elf_External_Verneed));
9512 everneed = (Elf_External_Verneed *) contents;
9513 iverneed = elf_tdata (abfd)->verref;
9514 for (i = 0; i < verneed_count; i++, iverneed++)
9516 Elf_External_Vernaux *evernaux;
9517 Elf_Internal_Vernaux *ivernaux;
9518 unsigned int j;
9520 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
9522 iverneed->vn_bfd = abfd;
9524 if (elf_use_dt_symtab_p (abfd))
9526 if (iverneed->vn_file < elf_tdata (abfd)->dt_strsz)
9527 iverneed->vn_filename
9528 = elf_tdata (abfd)->dt_strtab + iverneed->vn_file;
9529 else
9530 iverneed->vn_filename = NULL;
9532 else if (hdr == NULL)
9533 goto error_return_bad_verref;
9534 else
9535 iverneed->vn_filename
9536 = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9537 iverneed->vn_file);
9538 if (iverneed->vn_filename == NULL)
9539 goto error_return_bad_verref;
9541 if (iverneed->vn_cnt == 0)
9542 iverneed->vn_auxptr = NULL;
9543 else
9545 if (_bfd_mul_overflow (iverneed->vn_cnt,
9546 sizeof (Elf_Internal_Vernaux), &amt))
9548 bfd_set_error (bfd_error_file_too_big);
9549 goto error_return_verref;
9551 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
9552 bfd_alloc (abfd, amt);
9553 if (iverneed->vn_auxptr == NULL)
9554 goto error_return_verref;
9557 if (iverneed->vn_aux
9558 > (size_t) (contents_end - (bfd_byte *) everneed))
9559 goto error_return_bad_verref;
9561 evernaux = ((Elf_External_Vernaux *)
9562 ((bfd_byte *) everneed + iverneed->vn_aux));
9563 ivernaux = iverneed->vn_auxptr;
9564 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
9566 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
9568 if (elf_use_dt_symtab_p (abfd))
9570 if (ivernaux->vna_name < elf_tdata (abfd)->dt_strsz)
9571 ivernaux->vna_nodename
9572 = elf_tdata (abfd)->dt_strtab + ivernaux->vna_name;
9573 else
9574 ivernaux->vna_nodename = NULL;
9576 else if (hdr == NULL)
9577 goto error_return_bad_verref;
9578 else
9579 ivernaux->vna_nodename
9580 = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9581 ivernaux->vna_name);
9582 if (ivernaux->vna_nodename == NULL)
9583 goto error_return_bad_verref;
9585 if (ivernaux->vna_other > freeidx)
9586 freeidx = ivernaux->vna_other;
9588 ivernaux->vna_nextptr = NULL;
9589 if (ivernaux->vna_next == 0)
9591 iverneed->vn_cnt = j + 1;
9592 break;
9594 if (j + 1 < iverneed->vn_cnt)
9595 ivernaux->vna_nextptr = ivernaux + 1;
9597 if (ivernaux->vna_next
9598 > (size_t) (contents_end - (bfd_byte *) evernaux))
9599 goto error_return_bad_verref;
9601 evernaux = ((Elf_External_Vernaux *)
9602 ((bfd_byte *) evernaux + ivernaux->vna_next));
9605 iverneed->vn_nextref = NULL;
9606 if (iverneed->vn_next == 0)
9607 break;
9608 if (hdr != NULL && (i + 1 < hdr->sh_info))
9609 iverneed->vn_nextref = iverneed + 1;
9611 if (iverneed->vn_next
9612 > (size_t) (contents_end - (bfd_byte *) everneed))
9613 goto error_return_bad_verref;
9615 everneed = ((Elf_External_Verneed *)
9616 ((bfd_byte *) everneed + iverneed->vn_next));
9618 elf_tdata (abfd)->cverrefs = i;
9620 if (contents != elf_tdata (abfd)->dt_verneed)
9621 _bfd_munmap_readonly_temporary (contents_addr, contents_size);
9622 contents = NULL;
9623 contents_addr = NULL;
9626 if (elf_dynverdef (abfd) != 0 || elf_tdata (abfd)->dt_verdef != NULL)
9628 Elf_Internal_Shdr *hdr;
9629 Elf_External_Verdef *everdef;
9630 Elf_Internal_Verdef *iverdef;
9631 Elf_Internal_Verdef *iverdefarr;
9632 Elf_Internal_Verdef iverdefmem;
9633 unsigned int i;
9634 unsigned int maxidx;
9635 bfd_byte *contents_end_def, *contents_end_aux;
9636 size_t verdef_count;
9637 size_t verdef_size;
9639 if (elf_tdata (abfd)->dt_verdef != NULL)
9641 hdr = NULL;
9642 contents = elf_tdata (abfd)->dt_verdef;
9643 verdef_count = elf_tdata (abfd)->dt_verdef_count;
9644 verdef_size = verdef_count * sizeof (Elf_External_Verdef);
9646 else
9648 hdr = &elf_tdata (abfd)->dynverdef_hdr;
9650 if (hdr->sh_size < sizeof (Elf_External_Verdef))
9652 error_return_bad_verdef:
9653 _bfd_error_handler
9654 (_("%pB: .gnu.version_d invalid entry"), abfd);
9655 bfd_set_error (bfd_error_bad_value);
9656 error_return_verdef:
9657 elf_tdata (abfd)->verdef = NULL;
9658 elf_tdata (abfd)->cverdefs = 0;
9659 goto error_return;
9662 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9663 goto error_return_verdef;
9664 contents_size = hdr->sh_size;
9665 contents = _bfd_mmap_readonly_temporary (abfd, contents_size,
9666 &contents_addr,
9667 &contents_size);
9668 if (contents == NULL)
9669 goto error_return_verdef;
9671 BFD_ASSERT (sizeof (Elf_External_Verdef)
9672 >= sizeof (Elf_External_Verdaux));
9674 verdef_count = hdr->sh_info;
9675 verdef_size = hdr->sh_size;
9678 contents_end_def = (contents + verdef_size
9679 - sizeof (Elf_External_Verdef));
9680 contents_end_aux = (contents + verdef_size
9681 - sizeof (Elf_External_Verdaux));
9683 /* We know the number of entries in the section but not the maximum
9684 index. Therefore we have to run through all entries and find
9685 the maximum. */
9686 everdef = (Elf_External_Verdef *) contents;
9687 maxidx = 0;
9688 for (i = 0; i < verdef_count; ++i)
9690 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9692 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
9693 goto error_return_bad_verdef;
9694 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
9695 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
9697 if (iverdefmem.vd_next == 0)
9698 break;
9700 if (iverdefmem.vd_next
9701 > (size_t) (contents_end_def - (bfd_byte *) everdef))
9702 goto error_return_bad_verdef;
9704 everdef = ((Elf_External_Verdef *)
9705 ((bfd_byte *) everdef + iverdefmem.vd_next));
9708 if (default_imported_symver)
9710 if (freeidx > maxidx)
9711 maxidx = ++freeidx;
9712 else
9713 freeidx = ++maxidx;
9715 if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
9717 bfd_set_error (bfd_error_file_too_big);
9718 goto error_return_verdef;
9721 if (amt == 0)
9722 goto error_return_verdef;
9723 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9724 if (elf_tdata (abfd)->verdef == NULL)
9725 goto error_return_verdef;
9727 elf_tdata (abfd)->cverdefs = maxidx;
9729 everdef = (Elf_External_Verdef *) contents;
9730 iverdefarr = elf_tdata (abfd)->verdef;
9731 for (i = 0; i < verdef_count; ++i)
9733 Elf_External_Verdaux *everdaux;
9734 Elf_Internal_Verdaux *iverdaux;
9735 unsigned int j;
9737 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9739 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
9740 goto error_return_bad_verdef;
9742 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
9743 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
9745 iverdef->vd_bfd = abfd;
9747 if (iverdef->vd_cnt == 0)
9748 iverdef->vd_auxptr = NULL;
9749 else
9751 if (_bfd_mul_overflow (iverdef->vd_cnt,
9752 sizeof (Elf_Internal_Verdaux), &amt))
9754 bfd_set_error (bfd_error_file_too_big);
9755 goto error_return_verdef;
9757 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
9758 bfd_alloc (abfd, amt);
9759 if (iverdef->vd_auxptr == NULL)
9760 goto error_return_verdef;
9763 if (iverdef->vd_aux
9764 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
9765 goto error_return_bad_verdef;
9767 everdaux = ((Elf_External_Verdaux *)
9768 ((bfd_byte *) everdef + iverdef->vd_aux));
9769 iverdaux = iverdef->vd_auxptr;
9770 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
9772 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
9774 if (elf_use_dt_symtab_p (abfd))
9776 if (iverdaux->vda_name < elf_tdata (abfd)->dt_strsz)
9777 iverdaux->vda_nodename
9778 = elf_tdata (abfd)->dt_strtab + iverdaux->vda_name;
9779 else
9780 iverdaux->vda_nodename = NULL;
9782 else
9783 iverdaux->vda_nodename
9784 = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9785 iverdaux->vda_name);
9786 if (iverdaux->vda_nodename == NULL)
9787 goto error_return_bad_verdef;
9789 iverdaux->vda_nextptr = NULL;
9790 if (iverdaux->vda_next == 0)
9792 iverdef->vd_cnt = j + 1;
9793 break;
9795 if (j + 1 < iverdef->vd_cnt)
9796 iverdaux->vda_nextptr = iverdaux + 1;
9798 if (iverdaux->vda_next
9799 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
9800 goto error_return_bad_verdef;
9802 everdaux = ((Elf_External_Verdaux *)
9803 ((bfd_byte *) everdaux + iverdaux->vda_next));
9806 iverdef->vd_nodename = NULL;
9807 if (iverdef->vd_cnt)
9808 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
9810 iverdef->vd_nextdef = NULL;
9811 if (iverdef->vd_next == 0)
9812 break;
9813 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
9814 iverdef->vd_nextdef = iverdef + 1;
9816 everdef = ((Elf_External_Verdef *)
9817 ((bfd_byte *) everdef + iverdef->vd_next));
9820 if (contents != elf_tdata (abfd)->dt_verdef)
9821 _bfd_munmap_readonly_temporary (contents_addr, contents_size);
9822 contents = NULL;
9823 contents_addr = NULL;
9825 else if (default_imported_symver)
9827 if (freeidx < 3)
9828 freeidx = 3;
9829 else
9830 freeidx++;
9832 if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
9834 bfd_set_error (bfd_error_file_too_big);
9835 goto error_return;
9837 if (amt == 0)
9838 goto error_return;
9839 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9840 if (elf_tdata (abfd)->verdef == NULL)
9841 goto error_return;
9843 elf_tdata (abfd)->cverdefs = freeidx;
9846 /* Create a default version based on the soname. */
9847 if (default_imported_symver)
9849 Elf_Internal_Verdef *iverdef;
9850 Elf_Internal_Verdaux *iverdaux;
9852 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
9854 iverdef->vd_version = VER_DEF_CURRENT;
9855 iverdef->vd_flags = 0;
9856 iverdef->vd_ndx = freeidx;
9857 iverdef->vd_cnt = 1;
9859 iverdef->vd_bfd = abfd;
9861 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9862 if (iverdef->vd_nodename == NULL)
9863 goto error_return_verdef;
9864 iverdef->vd_nextdef = NULL;
9865 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
9866 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
9867 if (iverdef->vd_auxptr == NULL)
9868 goto error_return_verdef;
9870 iverdaux = iverdef->vd_auxptr;
9871 iverdaux->vda_nodename = iverdef->vd_nodename;
9874 return true;
9876 error_return:
9877 if (contents != elf_tdata (abfd)->dt_verneed
9878 && contents != elf_tdata (abfd)->dt_verdef)
9879 _bfd_munmap_readonly_temporary (contents_addr, contents_size);
9880 return false;
9883 asymbol *
9884 _bfd_elf_make_empty_symbol (bfd *abfd)
9886 elf_symbol_type *newsym;
9888 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
9889 if (!newsym)
9890 return NULL;
9891 newsym->symbol.the_bfd = abfd;
9892 return &newsym->symbol;
9895 void
9896 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9897 asymbol *symbol,
9898 symbol_info *ret)
9900 bfd_symbol_info (symbol, ret);
9903 /* Return whether a symbol name implies a local symbol. Most targets
9904 use this function for the is_local_label_name entry point, but some
9905 override it. */
9907 bool
9908 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9909 const char *name)
9911 /* Normal local symbols start with ``.L''. */
9912 if (name[0] == '.' && name[1] == 'L')
9913 return true;
9915 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9916 DWARF debugging symbols starting with ``..''. */
9917 if (name[0] == '.' && name[1] == '.')
9918 return true;
9920 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9921 emitting DWARF debugging output. I suspect this is actually a
9922 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9923 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9924 underscore to be emitted on some ELF targets). For ease of use,
9925 we treat such symbols as local. */
9926 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
9927 return true;
9929 /* Treat assembler generated fake symbols, dollar local labels and
9930 forward-backward labels (aka local labels) as locals.
9931 These labels have the form:
9933 L0^A.* (fake symbols)
9935 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9937 Versions which start with .L will have already been matched above,
9938 so we only need to match the rest. */
9939 if (name[0] == 'L' && ISDIGIT (name[1]))
9941 bool ret = false;
9942 const char * p;
9943 char c;
9945 for (p = name + 2; (c = *p); p++)
9947 if (c == 1 || c == 2)
9949 if (c == 1 && p == name + 2)
9950 /* A fake symbol. */
9951 return true;
9953 /* FIXME: We are being paranoid here and treating symbols like
9954 L0^Bfoo as if there were non-local, on the grounds that the
9955 assembler will never generate them. But can any symbol
9956 containing an ASCII value in the range 1-31 ever be anything
9957 other than some kind of local ? */
9958 ret = true;
9961 if (! ISDIGIT (c))
9963 ret = false;
9964 break;
9967 return ret;
9970 return false;
9973 alent *
9974 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9975 asymbol *symbol ATTRIBUTE_UNUSED)
9977 abort ();
9978 return NULL;
9981 bool
9982 _bfd_elf_set_arch_mach (bfd *abfd,
9983 enum bfd_architecture arch,
9984 unsigned long machine)
9986 /* If this isn't the right architecture for this backend, and this
9987 isn't the generic backend, fail. */
9988 if (arch != get_elf_backend_data (abfd)->arch
9989 && arch != bfd_arch_unknown
9990 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
9991 return false;
9993 return bfd_default_set_arch_mach (abfd, arch, machine);
9996 /* Find the nearest line to a particular section and offset,
9997 for error reporting. */
9999 bool
10000 _bfd_elf_find_nearest_line (bfd *abfd,
10001 asymbol **symbols,
10002 asection *section,
10003 bfd_vma offset,
10004 const char **filename_ptr,
10005 const char **functionname_ptr,
10006 unsigned int *line_ptr,
10007 unsigned int *discriminator_ptr)
10009 return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
10010 offset, filename_ptr,
10011 functionname_ptr, line_ptr,
10012 discriminator_ptr);
10015 /* Find the nearest line to a particular section and offset,
10016 for error reporting. ALT_BFD representing a .gnu_debugaltlink file
10017 can be optionally specified. */
10019 bool
10020 _bfd_elf_find_nearest_line_with_alt (bfd *abfd,
10021 const char *alt_filename,
10022 asymbol **symbols,
10023 asection *section,
10024 bfd_vma offset,
10025 const char **filename_ptr,
10026 const char **functionname_ptr,
10027 unsigned int *line_ptr,
10028 unsigned int *discriminator_ptr)
10030 bool found;
10032 if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
10033 section, offset, filename_ptr,
10034 functionname_ptr, line_ptr,
10035 discriminator_ptr,
10036 dwarf_debug_sections,
10037 &elf_tdata (abfd)->dwarf2_find_line_info))
10038 return true;
10040 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
10041 filename_ptr, functionname_ptr, line_ptr))
10043 if (!*functionname_ptr)
10044 _bfd_elf_find_function (abfd, symbols, section, offset,
10045 *filename_ptr ? NULL : filename_ptr,
10046 functionname_ptr);
10047 return true;
10050 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
10051 &found, filename_ptr,
10052 functionname_ptr, line_ptr,
10053 &elf_tdata (abfd)->line_info))
10054 return false;
10055 if (found && (*functionname_ptr || *line_ptr))
10056 return true;
10058 if (symbols == NULL)
10059 return false;
10061 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
10062 filename_ptr, functionname_ptr))
10063 return false;
10065 *line_ptr = 0;
10066 return true;
10069 /* Find the line for a symbol. */
10071 bool
10072 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
10073 const char **filename_ptr, unsigned int *line_ptr)
10075 struct elf_obj_tdata *tdata = elf_tdata (abfd);
10076 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
10077 filename_ptr, NULL, line_ptr, NULL,
10078 dwarf_debug_sections,
10079 &tdata->dwarf2_find_line_info);
10082 /* After a call to bfd_find_nearest_line, successive calls to
10083 bfd_find_inliner_info can be used to get source information about
10084 each level of function inlining that terminated at the address
10085 passed to bfd_find_nearest_line. Currently this is only supported
10086 for DWARF2 with appropriate DWARF3 extensions. */
10088 bool
10089 _bfd_elf_find_inliner_info (bfd *abfd,
10090 const char **filename_ptr,
10091 const char **functionname_ptr,
10092 unsigned int *line_ptr)
10094 struct elf_obj_tdata *tdata = elf_tdata (abfd);
10095 return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
10096 functionname_ptr, line_ptr,
10097 &tdata->dwarf2_find_line_info);
10101 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
10103 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10104 int ret = bed->s->sizeof_ehdr;
10106 if (!bfd_link_relocatable (info))
10108 bfd_size_type phdr_size = elf_program_header_size (abfd);
10110 if (phdr_size == (bfd_size_type) -1)
10112 struct elf_segment_map *m;
10114 phdr_size = 0;
10115 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
10116 phdr_size += bed->s->sizeof_phdr;
10118 if (phdr_size == 0)
10119 phdr_size = get_program_header_size (abfd, info);
10122 elf_program_header_size (abfd) = phdr_size;
10123 ret += phdr_size;
10126 return ret;
10129 bool
10130 _bfd_elf_set_section_contents (bfd *abfd,
10131 sec_ptr section,
10132 const void *location,
10133 file_ptr offset,
10134 bfd_size_type count)
10136 Elf_Internal_Shdr *hdr;
10138 if (! abfd->output_has_begun
10139 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
10140 return false;
10142 if (!count)
10143 return true;
10145 hdr = &elf_section_data (section)->this_hdr;
10146 if (hdr->sh_offset == (file_ptr) -1)
10148 unsigned char *contents;
10150 if (bfd_section_is_ctf (section))
10151 /* Nothing to do with this section: the contents are generated
10152 later. */
10153 return true;
10155 if ((offset + count) > hdr->sh_size)
10157 _bfd_error_handler
10158 (_("%pB:%pA: error: attempting to write"
10159 " over the end of the section"),
10160 abfd, section);
10162 bfd_set_error (bfd_error_invalid_operation);
10163 return false;
10166 contents = hdr->contents;
10167 if (contents == NULL)
10169 _bfd_error_handler
10170 (_("%pB:%pA: error: attempting to write"
10171 " section into an empty buffer"),
10172 abfd, section);
10174 bfd_set_error (bfd_error_invalid_operation);
10175 return false;
10178 memcpy (contents + offset, location, count);
10179 return true;
10182 return _bfd_generic_set_section_contents (abfd, section,
10183 location, offset, count);
10186 bool
10187 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
10188 arelent *cache_ptr ATTRIBUTE_UNUSED,
10189 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
10191 abort ();
10192 return false;
10195 /* Try to convert a non-ELF reloc into an ELF one. */
10197 bool
10198 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
10200 /* Check whether we really have an ELF howto. */
10202 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
10204 bfd_reloc_code_real_type code;
10205 reloc_howto_type *howto;
10207 /* Alien reloc: Try to determine its type to replace it with an
10208 equivalent ELF reloc. */
10210 if (areloc->howto->pc_relative)
10212 switch (areloc->howto->bitsize)
10214 case 8:
10215 code = BFD_RELOC_8_PCREL;
10216 break;
10217 case 12:
10218 code = BFD_RELOC_12_PCREL;
10219 break;
10220 case 16:
10221 code = BFD_RELOC_16_PCREL;
10222 break;
10223 case 24:
10224 code = BFD_RELOC_24_PCREL;
10225 break;
10226 case 32:
10227 code = BFD_RELOC_32_PCREL;
10228 break;
10229 case 64:
10230 code = BFD_RELOC_64_PCREL;
10231 break;
10232 default:
10233 goto fail;
10236 howto = bfd_reloc_type_lookup (abfd, code);
10238 if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
10240 if (howto->pcrel_offset)
10241 areloc->addend += areloc->address;
10242 else
10243 areloc->addend -= areloc->address; /* addend is unsigned!! */
10246 else
10248 switch (areloc->howto->bitsize)
10250 case 8:
10251 code = BFD_RELOC_8;
10252 break;
10253 case 14:
10254 code = BFD_RELOC_14;
10255 break;
10256 case 16:
10257 code = BFD_RELOC_16;
10258 break;
10259 case 26:
10260 code = BFD_RELOC_26;
10261 break;
10262 case 32:
10263 code = BFD_RELOC_32;
10264 break;
10265 case 64:
10266 code = BFD_RELOC_64;
10267 break;
10268 default:
10269 goto fail;
10272 howto = bfd_reloc_type_lookup (abfd, code);
10275 if (howto)
10276 areloc->howto = howto;
10277 else
10278 goto fail;
10281 return true;
10283 fail:
10284 /* xgettext:c-format */
10285 _bfd_error_handler (_("%pB: %s unsupported"),
10286 abfd, areloc->howto->name);
10287 bfd_set_error (bfd_error_sorry);
10288 return false;
10291 bool
10292 _bfd_elf_free_cached_info (bfd *abfd)
10294 struct elf_obj_tdata *tdata;
10296 if ((bfd_get_format (abfd) == bfd_object
10297 || bfd_get_format (abfd) == bfd_core)
10298 && (tdata = elf_tdata (abfd)) != NULL)
10300 if (tdata->o != NULL && elf_shstrtab (abfd) != NULL)
10301 _bfd_elf_strtab_free (elf_shstrtab (abfd));
10302 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
10303 _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info);
10304 _bfd_stab_cleanup (abfd, &tdata->line_info);
10307 return _bfd_generic_bfd_free_cached_info (abfd);
10310 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
10311 in the relocation's offset. Thus we cannot allow any sort of sanity
10312 range-checking to interfere. There is nothing else to do in processing
10313 this reloc. */
10315 bfd_reloc_status_type
10316 _bfd_elf_rel_vtable_reloc_fn
10317 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
10318 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
10319 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
10320 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
10322 return bfd_reloc_ok;
10325 /* Elf core file support. Much of this only works on native
10326 toolchains, since we rely on knowing the
10327 machine-dependent procfs structure in order to pick
10328 out details about the corefile. */
10330 #ifdef HAVE_SYS_PROCFS_H
10331 # include <sys/procfs.h>
10332 #endif
10334 /* Return a PID that identifies a "thread" for threaded cores, or the
10335 PID of the main process for non-threaded cores. */
10337 static int
10338 elfcore_make_pid (bfd *abfd)
10340 int pid;
10342 pid = elf_tdata (abfd)->core->lwpid;
10343 if (pid == 0)
10344 pid = elf_tdata (abfd)->core->pid;
10346 return pid;
10349 /* If there isn't a section called NAME, make one, using data from
10350 SECT. Note, this function will generate a reference to NAME, so
10351 you shouldn't deallocate or overwrite it. */
10353 static bool
10354 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
10356 asection *sect2;
10358 if (bfd_get_section_by_name (abfd, name) != NULL)
10359 return true;
10361 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
10362 if (sect2 == NULL)
10363 return false;
10365 sect2->size = sect->size;
10366 sect2->filepos = sect->filepos;
10367 sect2->alignment_power = sect->alignment_power;
10368 return true;
10371 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
10372 actually creates up to two pseudosections:
10373 - For the single-threaded case, a section named NAME, unless
10374 such a section already exists.
10375 - For the multi-threaded case, a section named "NAME/PID", where
10376 PID is elfcore_make_pid (abfd).
10377 Both pseudosections have identical contents. */
10378 bool
10379 _bfd_elfcore_make_pseudosection (bfd *abfd,
10380 char *name,
10381 size_t size,
10382 ufile_ptr filepos)
10384 char buf[100];
10385 char *threaded_name;
10386 size_t len;
10387 asection *sect;
10389 /* Build the section name. */
10391 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
10392 len = strlen (buf) + 1;
10393 threaded_name = (char *) bfd_alloc (abfd, len);
10394 if (threaded_name == NULL)
10395 return false;
10396 memcpy (threaded_name, buf, len);
10398 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
10399 SEC_HAS_CONTENTS);
10400 if (sect == NULL)
10401 return false;
10402 sect->size = size;
10403 sect->filepos = filepos;
10404 sect->alignment_power = 2;
10406 return elfcore_maybe_make_sect (abfd, name, sect);
10409 static bool
10410 elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
10411 size_t offs)
10413 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10414 SEC_HAS_CONTENTS);
10416 if (sect == NULL)
10417 return false;
10419 sect->size = note->descsz - offs;
10420 sect->filepos = note->descpos + offs;
10421 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10423 return true;
10426 /* prstatus_t exists on:
10427 solaris 2.5+
10428 linux 2.[01] + glibc
10429 unixware 4.2
10432 #if defined (HAVE_PRSTATUS_T)
10434 static bool
10435 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
10437 size_t size;
10438 int offset;
10440 if (note->descsz == sizeof (prstatus_t))
10442 prstatus_t prstat;
10444 size = sizeof (prstat.pr_reg);
10445 offset = offsetof (prstatus_t, pr_reg);
10446 memcpy (&prstat, note->descdata, sizeof (prstat));
10448 /* Do not overwrite the core signal if it
10449 has already been set by another thread. */
10450 if (elf_tdata (abfd)->core->signal == 0)
10451 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10452 if (elf_tdata (abfd)->core->pid == 0)
10453 elf_tdata (abfd)->core->pid = prstat.pr_pid;
10455 /* pr_who exists on:
10456 solaris 2.5+
10457 unixware 4.2
10458 pr_who doesn't exist on:
10459 linux 2.[01]
10461 #if defined (HAVE_PRSTATUS_T_PR_WHO)
10462 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10463 #else
10464 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10465 #endif
10467 #if defined (HAVE_PRSTATUS32_T)
10468 else if (note->descsz == sizeof (prstatus32_t))
10470 /* 64-bit host, 32-bit corefile */
10471 prstatus32_t prstat;
10473 size = sizeof (prstat.pr_reg);
10474 offset = offsetof (prstatus32_t, pr_reg);
10475 memcpy (&prstat, note->descdata, sizeof (prstat));
10477 /* Do not overwrite the core signal if it
10478 has already been set by another thread. */
10479 if (elf_tdata (abfd)->core->signal == 0)
10480 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10481 if (elf_tdata (abfd)->core->pid == 0)
10482 elf_tdata (abfd)->core->pid = prstat.pr_pid;
10484 /* pr_who exists on:
10485 solaris 2.5+
10486 unixware 4.2
10487 pr_who doesn't exist on:
10488 linux 2.[01]
10490 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
10491 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10492 #else
10493 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10494 #endif
10496 #endif /* HAVE_PRSTATUS32_T */
10497 else
10499 /* Fail - we don't know how to handle any other
10500 note size (ie. data object type). */
10501 return true;
10504 /* Make a ".reg/999" section and a ".reg" section. */
10505 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10506 size, note->descpos + offset);
10508 #endif /* defined (HAVE_PRSTATUS_T) */
10510 /* Create a pseudosection containing the exact contents of NOTE. */
10511 static bool
10512 elfcore_make_note_pseudosection (bfd *abfd,
10513 char *name,
10514 Elf_Internal_Note *note)
10516 return _bfd_elfcore_make_pseudosection (abfd, name,
10517 note->descsz, note->descpos);
10520 /* There isn't a consistent prfpregset_t across platforms,
10521 but it doesn't matter, because we don't have to pick this
10522 data structure apart. */
10524 static bool
10525 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
10527 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10530 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
10531 type of NT_PRXFPREG. Just include the whole note's contents
10532 literally. */
10534 static bool
10535 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
10537 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
10540 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
10541 with a note type of NT_X86_XSTATE. Just include the whole note's
10542 contents literally. */
10544 static bool
10545 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
10547 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
10550 static bool
10551 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
10553 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
10556 static bool
10557 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
10559 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
10562 static bool
10563 elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
10565 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
10568 static bool
10569 elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
10571 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
10574 static bool
10575 elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
10577 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
10580 static bool
10581 elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
10583 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
10586 static bool
10587 elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
10589 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
10592 static bool
10593 elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
10595 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
10598 static bool
10599 elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
10601 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
10604 static bool
10605 elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
10607 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
10610 static bool
10611 elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
10613 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
10616 static bool
10617 elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
10619 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
10622 static bool
10623 elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
10625 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
10628 static bool
10629 elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
10631 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
10634 static bool
10635 elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
10637 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
10640 static bool
10641 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
10643 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
10646 static bool
10647 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
10649 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
10652 static bool
10653 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
10655 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
10658 static bool
10659 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
10661 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
10664 static bool
10665 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
10667 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
10670 static bool
10671 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
10673 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
10676 static bool
10677 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
10679 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
10682 static bool
10683 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
10685 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
10688 static bool
10689 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
10691 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
10694 static bool
10695 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
10697 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
10700 static bool
10701 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
10703 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
10706 static bool
10707 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
10709 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
10712 static bool
10713 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
10715 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
10718 static bool
10719 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
10721 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
10724 static bool
10725 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
10727 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
10730 static bool
10731 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
10733 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
10736 static bool
10737 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
10739 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
10742 static bool
10743 elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
10745 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
10748 static bool
10749 elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
10751 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
10754 static bool
10755 elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
10757 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
10758 note);
10761 static bool
10762 elfcore_grok_aarch_ssve (bfd *abfd, Elf_Internal_Note *note)
10764 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-ssve", note);
10767 static bool
10768 elfcore_grok_aarch_za (bfd *abfd, Elf_Internal_Note *note)
10770 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-za", note);
10773 /* Convert NOTE into a bfd_section called ".reg-aarch-zt". Return TRUE if
10774 successful, otherwise return FALSE. */
10776 static bool
10777 elfcore_grok_aarch_zt (bfd *abfd, Elf_Internal_Note *note)
10779 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-zt", note);
10782 static bool
10783 elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
10785 return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
10788 /* Convert NOTE into a bfd_section called ".reg-riscv-csr". Return TRUE if
10789 successful otherwise, return FALSE. */
10791 static bool
10792 elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
10794 return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
10797 /* Convert NOTE into a bfd_section called ".gdb-tdesc". Return TRUE if
10798 successful otherwise, return FALSE. */
10800 static bool
10801 elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
10803 return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
10806 static bool
10807 elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
10809 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note);
10812 static bool
10813 elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
10815 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note);
10818 static bool
10819 elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
10821 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note);
10824 static bool
10825 elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
10827 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note);
10830 #if defined (HAVE_PRPSINFO_T)
10831 typedef prpsinfo_t elfcore_psinfo_t;
10832 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
10833 typedef prpsinfo32_t elfcore_psinfo32_t;
10834 #endif
10835 #endif
10837 #if defined (HAVE_PSINFO_T)
10838 typedef psinfo_t elfcore_psinfo_t;
10839 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
10840 typedef psinfo32_t elfcore_psinfo32_t;
10841 #endif
10842 #endif
10844 /* return a malloc'ed copy of a string at START which is at
10845 most MAX bytes long, possibly without a terminating '\0'.
10846 the copy will always have a terminating '\0'. */
10848 char *
10849 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
10851 char *dups;
10852 char *end = (char *) memchr (start, '\0', max);
10853 size_t len;
10855 if (end == NULL)
10856 len = max;
10857 else
10858 len = end - start;
10860 dups = (char *) bfd_alloc (abfd, len + 1);
10861 if (dups == NULL)
10862 return NULL;
10864 memcpy (dups, start, len);
10865 dups[len] = '\0';
10867 return dups;
10870 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10871 static bool
10872 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
10874 if (note->descsz == sizeof (elfcore_psinfo_t))
10876 elfcore_psinfo_t psinfo;
10878 memcpy (&psinfo, note->descdata, sizeof (psinfo));
10880 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10881 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10882 #endif
10883 elf_tdata (abfd)->core->program
10884 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10885 sizeof (psinfo.pr_fname));
10887 elf_tdata (abfd)->core->command
10888 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10889 sizeof (psinfo.pr_psargs));
10891 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10892 else if (note->descsz == sizeof (elfcore_psinfo32_t))
10894 /* 64-bit host, 32-bit corefile */
10895 elfcore_psinfo32_t psinfo;
10897 memcpy (&psinfo, note->descdata, sizeof (psinfo));
10899 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10900 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10901 #endif
10902 elf_tdata (abfd)->core->program
10903 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10904 sizeof (psinfo.pr_fname));
10906 elf_tdata (abfd)->core->command
10907 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10908 sizeof (psinfo.pr_psargs));
10910 #endif
10912 else
10914 /* Fail - we don't know how to handle any other
10915 note size (ie. data object type). */
10916 return true;
10919 /* Note that for some reason, a spurious space is tacked
10920 onto the end of the args in some (at least one anyway)
10921 implementations, so strip it off if it exists. */
10924 char *command = elf_tdata (abfd)->core->command;
10925 int n = strlen (command);
10927 if (0 < n && command[n - 1] == ' ')
10928 command[n - 1] = '\0';
10931 return true;
10933 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10935 #if defined (HAVE_PSTATUS_T)
10936 static bool
10937 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
10939 if (note->descsz == sizeof (pstatus_t)
10940 #if defined (HAVE_PXSTATUS_T)
10941 || note->descsz == sizeof (pxstatus_t)
10942 #endif
10945 pstatus_t pstat;
10947 memcpy (&pstat, note->descdata, sizeof (pstat));
10949 elf_tdata (abfd)->core->pid = pstat.pr_pid;
10951 #if defined (HAVE_PSTATUS32_T)
10952 else if (note->descsz == sizeof (pstatus32_t))
10954 /* 64-bit host, 32-bit corefile */
10955 pstatus32_t pstat;
10957 memcpy (&pstat, note->descdata, sizeof (pstat));
10959 elf_tdata (abfd)->core->pid = pstat.pr_pid;
10961 #endif
10962 /* Could grab some more details from the "representative"
10963 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10964 NT_LWPSTATUS note, presumably. */
10966 return true;
10968 #endif /* defined (HAVE_PSTATUS_T) */
10970 #if defined (HAVE_LWPSTATUS_T)
10971 static bool
10972 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
10974 lwpstatus_t lwpstat;
10975 char buf[100];
10976 char *name;
10977 size_t len;
10978 asection *sect;
10980 if (note->descsz != sizeof (lwpstat)
10981 #if defined (HAVE_LWPXSTATUS_T)
10982 && note->descsz != sizeof (lwpxstatus_t)
10983 #endif
10985 return true;
10987 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10989 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
10990 /* Do not overwrite the core signal if it has already been set by
10991 another thread. */
10992 if (elf_tdata (abfd)->core->signal == 0)
10993 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
10995 /* Make a ".reg/999" section. */
10997 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
10998 len = strlen (buf) + 1;
10999 name = bfd_alloc (abfd, len);
11000 if (name == NULL)
11001 return false;
11002 memcpy (name, buf, len);
11004 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11005 if (sect == NULL)
11006 return false;
11008 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11009 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
11010 sect->filepos = note->descpos
11011 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
11012 #endif
11014 #if defined (HAVE_LWPSTATUS_T_PR_REG)
11015 sect->size = sizeof (lwpstat.pr_reg);
11016 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
11017 #endif
11019 sect->alignment_power = 2;
11021 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
11022 return false;
11024 /* Make a ".reg2/999" section */
11026 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
11027 len = strlen (buf) + 1;
11028 name = bfd_alloc (abfd, len);
11029 if (name == NULL)
11030 return false;
11031 memcpy (name, buf, len);
11033 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11034 if (sect == NULL)
11035 return false;
11037 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11038 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
11039 sect->filepos = note->descpos
11040 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
11041 #endif
11043 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
11044 sect->size = sizeof (lwpstat.pr_fpreg);
11045 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
11046 #endif
11048 sect->alignment_power = 2;
11050 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
11052 #endif /* defined (HAVE_LWPSTATUS_T) */
11054 /* These constants, and the structure offsets used below, are defined by
11055 Cygwin's core_dump.h */
11056 #define NOTE_INFO_PROCESS 1
11057 #define NOTE_INFO_THREAD 2
11058 #define NOTE_INFO_MODULE 3
11059 #define NOTE_INFO_MODULE64 4
11061 static bool
11062 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
11064 char buf[30];
11065 char *name;
11066 size_t len;
11067 unsigned int name_size;
11068 asection *sect;
11069 unsigned int type;
11070 int is_active_thread;
11071 bfd_vma base_addr;
11073 if (note->descsz < 4)
11074 return true;
11076 if (! startswith (note->namedata, "win32"))
11077 return true;
11079 type = bfd_get_32 (abfd, note->descdata);
11081 struct
11083 const char *type_name;
11084 unsigned long min_size;
11085 } size_check[] =
11087 { "NOTE_INFO_PROCESS", 12 },
11088 { "NOTE_INFO_THREAD", 12 },
11089 { "NOTE_INFO_MODULE", 12 },
11090 { "NOTE_INFO_MODULE64", 16 },
11093 if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
11094 return true;
11096 if (note->descsz < size_check[type - 1].min_size)
11098 _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
11099 " is too small"),
11100 abfd, size_check[type - 1].type_name, note->descsz);
11101 return true;
11104 switch (type)
11106 case NOTE_INFO_PROCESS:
11107 /* FIXME: need to add ->core->command. */
11108 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
11109 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
11110 break;
11112 case NOTE_INFO_THREAD:
11113 /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
11114 structure. */
11115 /* thread_info.tid */
11116 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
11118 len = strlen (buf) + 1;
11119 name = (char *) bfd_alloc (abfd, len);
11120 if (name == NULL)
11121 return false;
11123 memcpy (name, buf, len);
11125 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11126 if (sect == NULL)
11127 return false;
11129 /* sizeof (thread_info.thread_context) */
11130 sect->size = note->descsz - 12;
11131 /* offsetof (thread_info.thread_context) */
11132 sect->filepos = note->descpos + 12;
11133 sect->alignment_power = 2;
11135 /* thread_info.is_active_thread */
11136 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
11138 if (is_active_thread)
11139 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
11140 return false;
11141 break;
11143 case NOTE_INFO_MODULE:
11144 case NOTE_INFO_MODULE64:
11145 /* Make a ".module/xxxxxxxx" section. */
11146 if (type == NOTE_INFO_MODULE)
11148 /* module_info.base_address */
11149 base_addr = bfd_get_32 (abfd, note->descdata + 4);
11150 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
11151 /* module_info.module_name_size */
11152 name_size = bfd_get_32 (abfd, note->descdata + 8);
11154 else /* NOTE_INFO_MODULE64 */
11156 /* module_info.base_address */
11157 base_addr = bfd_get_64 (abfd, note->descdata + 4);
11158 sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
11159 /* module_info.module_name_size */
11160 name_size = bfd_get_32 (abfd, note->descdata + 12);
11163 len = strlen (buf) + 1;
11164 name = (char *) bfd_alloc (abfd, len);
11165 if (name == NULL)
11166 return false;
11168 memcpy (name, buf, len);
11170 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11172 if (sect == NULL)
11173 return false;
11175 if (note->descsz < 12 + name_size)
11177 _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
11178 " is too small to contain a name of size %u"),
11179 abfd, note->descsz, name_size);
11180 return true;
11183 sect->size = note->descsz;
11184 sect->filepos = note->descpos;
11185 sect->alignment_power = 2;
11186 break;
11188 default:
11189 return true;
11192 return true;
11195 static bool
11196 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
11198 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11200 switch (note->type)
11202 default:
11203 return true;
11205 case NT_PRSTATUS:
11206 if (bed->elf_backend_grok_prstatus)
11207 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
11208 return true;
11209 #if defined (HAVE_PRSTATUS_T)
11210 return elfcore_grok_prstatus (abfd, note);
11211 #else
11212 return true;
11213 #endif
11215 #if defined (HAVE_PSTATUS_T)
11216 case NT_PSTATUS:
11217 return elfcore_grok_pstatus (abfd, note);
11218 #endif
11220 #if defined (HAVE_LWPSTATUS_T)
11221 case NT_LWPSTATUS:
11222 return elfcore_grok_lwpstatus (abfd, note);
11223 #endif
11225 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
11226 return elfcore_grok_prfpreg (abfd, note);
11228 case NT_WIN32PSTATUS:
11229 return elfcore_grok_win32pstatus (abfd, note);
11231 case NT_PRXFPREG: /* Linux SSE extension */
11232 if (note->namesz == 6
11233 && strcmp (note->namedata, "LINUX") == 0)
11234 return elfcore_grok_prxfpreg (abfd, note);
11235 else
11236 return true;
11238 case NT_X86_XSTATE: /* Linux XSAVE extension */
11239 if (note->namesz == 6
11240 && strcmp (note->namedata, "LINUX") == 0)
11241 return elfcore_grok_xstatereg (abfd, note);
11242 else
11243 return true;
11245 case NT_PPC_VMX:
11246 if (note->namesz == 6
11247 && strcmp (note->namedata, "LINUX") == 0)
11248 return elfcore_grok_ppc_vmx (abfd, note);
11249 else
11250 return true;
11252 case NT_PPC_VSX:
11253 if (note->namesz == 6
11254 && strcmp (note->namedata, "LINUX") == 0)
11255 return elfcore_grok_ppc_vsx (abfd, note);
11256 else
11257 return true;
11259 case NT_PPC_TAR:
11260 if (note->namesz == 6
11261 && strcmp (note->namedata, "LINUX") == 0)
11262 return elfcore_grok_ppc_tar (abfd, note);
11263 else
11264 return true;
11266 case NT_PPC_PPR:
11267 if (note->namesz == 6
11268 && strcmp (note->namedata, "LINUX") == 0)
11269 return elfcore_grok_ppc_ppr (abfd, note);
11270 else
11271 return true;
11273 case NT_PPC_DSCR:
11274 if (note->namesz == 6
11275 && strcmp (note->namedata, "LINUX") == 0)
11276 return elfcore_grok_ppc_dscr (abfd, note);
11277 else
11278 return true;
11280 case NT_PPC_EBB:
11281 if (note->namesz == 6
11282 && strcmp (note->namedata, "LINUX") == 0)
11283 return elfcore_grok_ppc_ebb (abfd, note);
11284 else
11285 return true;
11287 case NT_PPC_PMU:
11288 if (note->namesz == 6
11289 && strcmp (note->namedata, "LINUX") == 0)
11290 return elfcore_grok_ppc_pmu (abfd, note);
11291 else
11292 return true;
11294 case NT_PPC_TM_CGPR:
11295 if (note->namesz == 6
11296 && strcmp (note->namedata, "LINUX") == 0)
11297 return elfcore_grok_ppc_tm_cgpr (abfd, note);
11298 else
11299 return true;
11301 case NT_PPC_TM_CFPR:
11302 if (note->namesz == 6
11303 && strcmp (note->namedata, "LINUX") == 0)
11304 return elfcore_grok_ppc_tm_cfpr (abfd, note);
11305 else
11306 return true;
11308 case NT_PPC_TM_CVMX:
11309 if (note->namesz == 6
11310 && strcmp (note->namedata, "LINUX") == 0)
11311 return elfcore_grok_ppc_tm_cvmx (abfd, note);
11312 else
11313 return true;
11315 case NT_PPC_TM_CVSX:
11316 if (note->namesz == 6
11317 && strcmp (note->namedata, "LINUX") == 0)
11318 return elfcore_grok_ppc_tm_cvsx (abfd, note);
11319 else
11320 return true;
11322 case NT_PPC_TM_SPR:
11323 if (note->namesz == 6
11324 && strcmp (note->namedata, "LINUX") == 0)
11325 return elfcore_grok_ppc_tm_spr (abfd, note);
11326 else
11327 return true;
11329 case NT_PPC_TM_CTAR:
11330 if (note->namesz == 6
11331 && strcmp (note->namedata, "LINUX") == 0)
11332 return elfcore_grok_ppc_tm_ctar (abfd, note);
11333 else
11334 return true;
11336 case NT_PPC_TM_CPPR:
11337 if (note->namesz == 6
11338 && strcmp (note->namedata, "LINUX") == 0)
11339 return elfcore_grok_ppc_tm_cppr (abfd, note);
11340 else
11341 return true;
11343 case NT_PPC_TM_CDSCR:
11344 if (note->namesz == 6
11345 && strcmp (note->namedata, "LINUX") == 0)
11346 return elfcore_grok_ppc_tm_cdscr (abfd, note);
11347 else
11348 return true;
11350 case NT_S390_HIGH_GPRS:
11351 if (note->namesz == 6
11352 && strcmp (note->namedata, "LINUX") == 0)
11353 return elfcore_grok_s390_high_gprs (abfd, note);
11354 else
11355 return true;
11357 case NT_S390_TIMER:
11358 if (note->namesz == 6
11359 && strcmp (note->namedata, "LINUX") == 0)
11360 return elfcore_grok_s390_timer (abfd, note);
11361 else
11362 return true;
11364 case NT_S390_TODCMP:
11365 if (note->namesz == 6
11366 && strcmp (note->namedata, "LINUX") == 0)
11367 return elfcore_grok_s390_todcmp (abfd, note);
11368 else
11369 return true;
11371 case NT_S390_TODPREG:
11372 if (note->namesz == 6
11373 && strcmp (note->namedata, "LINUX") == 0)
11374 return elfcore_grok_s390_todpreg (abfd, note);
11375 else
11376 return true;
11378 case NT_S390_CTRS:
11379 if (note->namesz == 6
11380 && strcmp (note->namedata, "LINUX") == 0)
11381 return elfcore_grok_s390_ctrs (abfd, note);
11382 else
11383 return true;
11385 case NT_S390_PREFIX:
11386 if (note->namesz == 6
11387 && strcmp (note->namedata, "LINUX") == 0)
11388 return elfcore_grok_s390_prefix (abfd, note);
11389 else
11390 return true;
11392 case NT_S390_LAST_BREAK:
11393 if (note->namesz == 6
11394 && strcmp (note->namedata, "LINUX") == 0)
11395 return elfcore_grok_s390_last_break (abfd, note);
11396 else
11397 return true;
11399 case NT_S390_SYSTEM_CALL:
11400 if (note->namesz == 6
11401 && strcmp (note->namedata, "LINUX") == 0)
11402 return elfcore_grok_s390_system_call (abfd, note);
11403 else
11404 return true;
11406 case NT_S390_TDB:
11407 if (note->namesz == 6
11408 && strcmp (note->namedata, "LINUX") == 0)
11409 return elfcore_grok_s390_tdb (abfd, note);
11410 else
11411 return true;
11413 case NT_S390_VXRS_LOW:
11414 if (note->namesz == 6
11415 && strcmp (note->namedata, "LINUX") == 0)
11416 return elfcore_grok_s390_vxrs_low (abfd, note);
11417 else
11418 return true;
11420 case NT_S390_VXRS_HIGH:
11421 if (note->namesz == 6
11422 && strcmp (note->namedata, "LINUX") == 0)
11423 return elfcore_grok_s390_vxrs_high (abfd, note);
11424 else
11425 return true;
11427 case NT_S390_GS_CB:
11428 if (note->namesz == 6
11429 && strcmp (note->namedata, "LINUX") == 0)
11430 return elfcore_grok_s390_gs_cb (abfd, note);
11431 else
11432 return true;
11434 case NT_S390_GS_BC:
11435 if (note->namesz == 6
11436 && strcmp (note->namedata, "LINUX") == 0)
11437 return elfcore_grok_s390_gs_bc (abfd, note);
11438 else
11439 return true;
11441 case NT_ARC_V2:
11442 if (note->namesz == 6
11443 && strcmp (note->namedata, "LINUX") == 0)
11444 return elfcore_grok_arc_v2 (abfd, note);
11445 else
11446 return true;
11448 case NT_ARM_VFP:
11449 if (note->namesz == 6
11450 && strcmp (note->namedata, "LINUX") == 0)
11451 return elfcore_grok_arm_vfp (abfd, note);
11452 else
11453 return true;
11455 case NT_ARM_TLS:
11456 if (note->namesz == 6
11457 && strcmp (note->namedata, "LINUX") == 0)
11458 return elfcore_grok_aarch_tls (abfd, note);
11459 else
11460 return true;
11462 case NT_ARM_HW_BREAK:
11463 if (note->namesz == 6
11464 && strcmp (note->namedata, "LINUX") == 0)
11465 return elfcore_grok_aarch_hw_break (abfd, note);
11466 else
11467 return true;
11469 case NT_ARM_HW_WATCH:
11470 if (note->namesz == 6
11471 && strcmp (note->namedata, "LINUX") == 0)
11472 return elfcore_grok_aarch_hw_watch (abfd, note);
11473 else
11474 return true;
11476 case NT_ARM_SVE:
11477 if (note->namesz == 6
11478 && strcmp (note->namedata, "LINUX") == 0)
11479 return elfcore_grok_aarch_sve (abfd, note);
11480 else
11481 return true;
11483 case NT_ARM_PAC_MASK:
11484 if (note->namesz == 6
11485 && strcmp (note->namedata, "LINUX") == 0)
11486 return elfcore_grok_aarch_pauth (abfd, note);
11487 else
11488 return true;
11490 case NT_ARM_TAGGED_ADDR_CTRL:
11491 if (note->namesz == 6
11492 && strcmp (note->namedata, "LINUX") == 0)
11493 return elfcore_grok_aarch_mte (abfd, note);
11494 else
11495 return true;
11497 case NT_ARM_SSVE:
11498 if (note->namesz == 6
11499 && strcmp (note->namedata, "LINUX") == 0)
11500 return elfcore_grok_aarch_ssve (abfd, note);
11501 else
11502 return true;
11504 case NT_ARM_ZA:
11505 if (note->namesz == 6
11506 && strcmp (note->namedata, "LINUX") == 0)
11507 return elfcore_grok_aarch_za (abfd, note);
11508 else
11509 return true;
11511 case NT_ARM_ZT:
11512 if (note->namesz == 6
11513 && strcmp (note->namedata, "LINUX") == 0)
11514 return elfcore_grok_aarch_zt (abfd, note);
11515 else
11516 return true;
11518 case NT_GDB_TDESC:
11519 if (note->namesz == 4
11520 && strcmp (note->namedata, "GDB") == 0)
11521 return elfcore_grok_gdb_tdesc (abfd, note);
11522 else
11523 return true;
11525 case NT_RISCV_CSR:
11526 if (note->namesz == 4
11527 && strcmp (note->namedata, "GDB") == 0)
11528 return elfcore_grok_riscv_csr (abfd, note);
11529 else
11530 return true;
11532 case NT_LARCH_CPUCFG:
11533 if (note->namesz == 6
11534 && strcmp (note->namedata, "LINUX") == 0)
11535 return elfcore_grok_loongarch_cpucfg (abfd, note);
11536 else
11537 return true;
11539 case NT_LARCH_LBT:
11540 if (note->namesz == 6
11541 && strcmp (note->namedata, "LINUX") == 0)
11542 return elfcore_grok_loongarch_lbt (abfd, note);
11543 else
11544 return true;
11546 case NT_LARCH_LSX:
11547 if (note->namesz == 6
11548 && strcmp (note->namedata, "LINUX") == 0)
11549 return elfcore_grok_loongarch_lsx (abfd, note);
11550 else
11551 return true;
11553 case NT_LARCH_LASX:
11554 if (note->namesz == 6
11555 && strcmp (note->namedata, "LINUX") == 0)
11556 return elfcore_grok_loongarch_lasx (abfd, note);
11557 else
11558 return true;
11560 case NT_PRPSINFO:
11561 case NT_PSINFO:
11562 if (bed->elf_backend_grok_psinfo)
11563 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
11564 return true;
11565 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11566 return elfcore_grok_psinfo (abfd, note);
11567 #else
11568 return true;
11569 #endif
11571 case NT_AUXV:
11572 return elfcore_make_auxv_note_section (abfd, note, 0);
11574 case NT_FILE:
11575 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
11576 note);
11578 case NT_SIGINFO:
11579 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
11580 note);
11585 static bool
11586 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
11588 struct bfd_build_id* build_id;
11590 if (note->descsz == 0)
11591 return false;
11593 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
11594 if (build_id == NULL)
11595 return false;
11597 build_id->size = note->descsz;
11598 memcpy (build_id->data, note->descdata, note->descsz);
11599 abfd->build_id = build_id;
11601 return true;
11604 static bool
11605 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
11607 switch (note->type)
11609 default:
11610 return true;
11612 case NT_GNU_PROPERTY_TYPE_0:
11613 return _bfd_elf_parse_gnu_properties (abfd, note);
11615 case NT_GNU_BUILD_ID:
11616 return elfobj_grok_gnu_build_id (abfd, note);
11620 static bool
11621 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
11623 struct sdt_note *cur =
11624 (struct sdt_note *) bfd_alloc (abfd,
11625 sizeof (struct sdt_note) + note->descsz);
11627 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
11628 cur->size = (bfd_size_type) note->descsz;
11629 memcpy (cur->data, note->descdata, note->descsz);
11631 elf_tdata (abfd)->sdt_note_head = cur;
11633 return true;
11636 static bool
11637 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
11639 switch (note->type)
11641 case NT_STAPSDT:
11642 return elfobj_grok_stapsdt_note_1 (abfd, note);
11644 default:
11645 return true;
11649 static bool
11650 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
11652 size_t offset;
11654 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11656 case ELFCLASS32:
11657 if (note->descsz < 108)
11658 return false;
11659 break;
11661 case ELFCLASS64:
11662 if (note->descsz < 120)
11663 return false;
11664 break;
11666 default:
11667 return false;
11670 /* Check for version 1 in pr_version. */
11671 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11672 return false;
11674 offset = 4;
11676 /* Skip over pr_psinfosz. */
11677 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11678 offset += 4;
11679 else
11681 offset += 4; /* Padding before pr_psinfosz. */
11682 offset += 8;
11685 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
11686 elf_tdata (abfd)->core->program
11687 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
11688 offset += 17;
11690 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
11691 elf_tdata (abfd)->core->command
11692 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
11693 offset += 81;
11695 /* Padding before pr_pid. */
11696 offset += 2;
11698 /* The pr_pid field was added in version "1a". */
11699 if (note->descsz < offset + 4)
11700 return true;
11702 elf_tdata (abfd)->core->pid
11703 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11705 return true;
11708 static bool
11709 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
11711 size_t offset;
11712 size_t size;
11713 size_t min_size;
11715 /* Compute offset of pr_getregsz, skipping over pr_statussz.
11716 Also compute minimum size of this note. */
11717 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11719 case ELFCLASS32:
11720 offset = 4 + 4;
11721 min_size = offset + (4 * 2) + 4 + 4 + 4;
11722 break;
11724 case ELFCLASS64:
11725 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
11726 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
11727 break;
11729 default:
11730 return false;
11733 if (note->descsz < min_size)
11734 return false;
11736 /* Check for version 1 in pr_version. */
11737 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11738 return false;
11740 /* Extract size of pr_reg from pr_gregsetsz. */
11741 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
11742 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11744 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11745 offset += 4 * 2;
11747 else
11749 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
11750 offset += 8 * 2;
11753 /* Skip over pr_osreldate. */
11754 offset += 4;
11756 /* Read signal from pr_cursig. */
11757 if (elf_tdata (abfd)->core->signal == 0)
11758 elf_tdata (abfd)->core->signal
11759 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11760 offset += 4;
11762 /* Read TID from pr_pid. */
11763 elf_tdata (abfd)->core->lwpid
11764 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11765 offset += 4;
11767 /* Padding before pr_reg. */
11768 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
11769 offset += 4;
11771 /* Make sure that there is enough data remaining in the note. */
11772 if ((note->descsz - offset) < size)
11773 return false;
11775 /* Make a ".reg/999" section and a ".reg" section. */
11776 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
11777 size, note->descpos + offset);
11780 static bool
11781 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
11783 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11785 switch (note->type)
11787 case NT_PRSTATUS:
11788 if (bed->elf_backend_grok_freebsd_prstatus)
11789 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
11790 return true;
11791 return elfcore_grok_freebsd_prstatus (abfd, note);
11793 case NT_FPREGSET:
11794 return elfcore_grok_prfpreg (abfd, note);
11796 case NT_PRPSINFO:
11797 return elfcore_grok_freebsd_psinfo (abfd, note);
11799 case NT_FREEBSD_THRMISC:
11800 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
11802 case NT_FREEBSD_PROCSTAT_PROC:
11803 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
11804 note);
11806 case NT_FREEBSD_PROCSTAT_FILES:
11807 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
11808 note);
11810 case NT_FREEBSD_PROCSTAT_VMMAP:
11811 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
11812 note);
11814 case NT_FREEBSD_PROCSTAT_AUXV:
11815 return elfcore_make_auxv_note_section (abfd, note, 4);
11817 case NT_FREEBSD_X86_SEGBASES:
11818 return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note);
11820 case NT_X86_XSTATE:
11821 return elfcore_grok_xstatereg (abfd, note);
11823 case NT_FREEBSD_PTLWPINFO:
11824 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
11825 note);
11827 case NT_ARM_TLS:
11828 return elfcore_grok_aarch_tls (abfd, note);
11830 case NT_ARM_VFP:
11831 return elfcore_grok_arm_vfp (abfd, note);
11833 default:
11834 return true;
11838 static bool
11839 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
11841 char *cp;
11843 cp = strchr (note->namedata, '@');
11844 if (cp != NULL)
11846 *lwpidp = atoi(cp + 1);
11847 return true;
11849 return false;
11852 static bool
11853 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11855 if (note->descsz <= 0x7c + 31)
11856 return false;
11858 /* Signal number at offset 0x08. */
11859 elf_tdata (abfd)->core->signal
11860 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11862 /* Process ID at offset 0x50. */
11863 elf_tdata (abfd)->core->pid
11864 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
11866 /* Command name at 0x7c (max 32 bytes, including nul). */
11867 elf_tdata (abfd)->core->command
11868 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
11870 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
11871 note);
11874 static bool
11875 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
11877 int lwp;
11879 if (elfcore_netbsd_get_lwpid (note, &lwp))
11880 elf_tdata (abfd)->core->lwpid = lwp;
11882 switch (note->type)
11884 case NT_NETBSDCORE_PROCINFO:
11885 /* NetBSD-specific core "procinfo". Note that we expect to
11886 find this note before any of the others, which is fine,
11887 since the kernel writes this note out first when it
11888 creates a core file. */
11889 return elfcore_grok_netbsd_procinfo (abfd, note);
11890 case NT_NETBSDCORE_AUXV:
11891 /* NetBSD-specific Elf Auxiliary Vector data. */
11892 return elfcore_make_auxv_note_section (abfd, note, 4);
11893 case NT_NETBSDCORE_LWPSTATUS:
11894 return elfcore_make_note_pseudosection (abfd,
11895 ".note.netbsdcore.lwpstatus",
11896 note);
11897 default:
11898 break;
11901 /* As of March 2020 there are no other machine-independent notes
11902 defined for NetBSD core files. If the note type is less
11903 than the start of the machine-dependent note types, we don't
11904 understand it. */
11906 if (note->type < NT_NETBSDCORE_FIRSTMACH)
11907 return true;
11910 switch (bfd_get_arch (abfd))
11912 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11913 PT_GETFPREGS == mach+2. */
11915 case bfd_arch_aarch64:
11916 case bfd_arch_alpha:
11917 case bfd_arch_sparc:
11918 switch (note->type)
11920 case NT_NETBSDCORE_FIRSTMACH+0:
11921 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11923 case NT_NETBSDCORE_FIRSTMACH+2:
11924 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11926 default:
11927 return true;
11930 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11931 There's also old PT___GETREGS40 == mach + 1 for old reg
11932 structure which lacks GBR. */
11934 case bfd_arch_sh:
11935 switch (note->type)
11937 case NT_NETBSDCORE_FIRSTMACH+3:
11938 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11940 case NT_NETBSDCORE_FIRSTMACH+5:
11941 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11943 default:
11944 return true;
11947 /* On all other arch's, PT_GETREGS == mach+1 and
11948 PT_GETFPREGS == mach+3. */
11950 default:
11951 switch (note->type)
11953 case NT_NETBSDCORE_FIRSTMACH+1:
11954 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11956 case NT_NETBSDCORE_FIRSTMACH+3:
11957 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11959 default:
11960 return true;
11963 /* NOTREACHED */
11966 static bool
11967 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11969 if (note->descsz <= 0x48 + 31)
11970 return false;
11972 /* Signal number at offset 0x08. */
11973 elf_tdata (abfd)->core->signal
11974 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11976 /* Process ID at offset 0x20. */
11977 elf_tdata (abfd)->core->pid
11978 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
11980 /* Command name at 0x48 (max 32 bytes, including nul). */
11981 elf_tdata (abfd)->core->command
11982 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11984 return true;
11987 /* Processes Solaris's process status note.
11988 sig_off ~ offsetof(prstatus_t, pr_cursig)
11989 pid_off ~ offsetof(prstatus_t, pr_pid)
11990 lwpid_off ~ offsetof(prstatus_t, pr_who)
11991 gregset_size ~ sizeof(gregset_t)
11992 gregset_offset ~ offsetof(prstatus_t, pr_reg) */
11994 static bool
11995 elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
11996 int pid_off, int lwpid_off, size_t gregset_size,
11997 size_t gregset_offset)
11999 asection *sect = NULL;
12000 elf_tdata (abfd)->core->signal
12001 = bfd_get_16 (abfd, note->descdata + sig_off);
12002 elf_tdata (abfd)->core->pid
12003 = bfd_get_32 (abfd, note->descdata + pid_off);
12004 elf_tdata (abfd)->core->lwpid
12005 = bfd_get_32 (abfd, note->descdata + lwpid_off);
12007 sect = bfd_get_section_by_name (abfd, ".reg");
12008 if (sect != NULL)
12009 sect->size = gregset_size;
12011 return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
12012 note->descpos + gregset_offset);
12015 /* Gets program and arguments from a core.
12016 prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
12017 comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs) */
12019 static bool
12020 elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
12021 int prog_off, int comm_off)
12023 elf_tdata (abfd)->core->program
12024 = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
12025 elf_tdata (abfd)->core->command
12026 = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
12028 return true;
12031 /* Processes Solaris's LWP status note.
12032 gregset_size ~ sizeof(gregset_t)
12033 gregset_off ~ offsetof(lwpstatus_t, pr_reg)
12034 fpregset_size ~ sizeof(fpregset_t)
12035 fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg) */
12037 static bool
12038 elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
12039 size_t gregset_size, int gregset_off,
12040 size_t fpregset_size, int fpregset_off)
12042 asection *sect = NULL;
12043 char reg2_section_name[16] = { 0 };
12045 (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2",
12046 elf_tdata (abfd)->core->lwpid);
12048 /* offsetof(lwpstatus_t, pr_lwpid) */
12049 elf_tdata (abfd)->core->lwpid
12050 = bfd_get_32 (abfd, note->descdata + 4);
12051 /* offsetof(lwpstatus_t, pr_cursig) */
12052 elf_tdata (abfd)->core->signal
12053 = bfd_get_16 (abfd, note->descdata + 12);
12055 sect = bfd_get_section_by_name (abfd, ".reg");
12056 if (sect != NULL)
12057 sect->size = gregset_size;
12058 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
12059 note->descpos + gregset_off))
12060 return false;
12062 sect = bfd_get_section_by_name (abfd, reg2_section_name);
12063 if (sect != NULL)
12065 sect->size = fpregset_size;
12066 sect->filepos = note->descpos + fpregset_off;
12067 sect->alignment_power = 2;
12069 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size,
12070 note->descpos + fpregset_off))
12071 return false;
12073 return true;
12076 static bool
12077 elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
12079 if (note == NULL)
12080 return false;
12082 /* core files are identified as 32- or 64-bit, SPARC or x86,
12083 by the size of the descsz which matches the sizeof()
12084 the type appropriate for that note type (e.g., prstatus_t for
12085 SOLARIS_NT_PRSTATUS) for the corresponding architecture
12086 on Solaris. The core file bitness may differ from the bitness of
12087 gdb itself, so fixed values are used instead of sizeof().
12088 Appropriate fixed offsets are also used to obtain data from
12089 the note. */
12091 switch ((int) note->type)
12093 case SOLARIS_NT_PRSTATUS:
12094 switch (note->descsz)
12096 case 508: /* sizeof(prstatus_t) SPARC 32-bit */
12097 return elfcore_grok_solaris_prstatus(abfd, note,
12098 136, 216, 308, 152, 356);
12099 case 904: /* sizeof(prstatus_t) SPARC 64-bit */
12100 return elfcore_grok_solaris_prstatus(abfd, note,
12101 264, 360, 520, 304, 600);
12102 case 432: /* sizeof(prstatus_t) Intel 32-bit */
12103 return elfcore_grok_solaris_prstatus(abfd, note,
12104 136, 216, 308, 76, 356);
12105 case 824: /* sizeof(prstatus_t) Intel 64-bit */
12106 return elfcore_grok_solaris_prstatus(abfd, note,
12107 264, 360, 520, 224, 600);
12108 default:
12109 return true;
12112 case SOLARIS_NT_PSINFO:
12113 case SOLARIS_NT_PRPSINFO:
12114 switch (note->descsz)
12116 case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
12117 return elfcore_grok_solaris_info(abfd, note, 84, 100);
12118 case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
12119 return elfcore_grok_solaris_info(abfd, note, 120, 136);
12120 case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
12121 return elfcore_grok_solaris_info(abfd, note, 88, 104);
12122 case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
12123 return elfcore_grok_solaris_info(abfd, note, 136, 152);
12124 default:
12125 return true;
12128 case SOLARIS_NT_LWPSTATUS:
12129 switch (note->descsz)
12131 case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
12132 return elfcore_grok_solaris_lwpstatus(abfd, note,
12133 152, 344, 400, 496);
12134 case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
12135 return elfcore_grok_solaris_lwpstatus(abfd, note,
12136 304, 544, 544, 848);
12137 case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
12138 return elfcore_grok_solaris_lwpstatus(abfd, note,
12139 76, 344, 380, 420);
12140 case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
12141 return elfcore_grok_solaris_lwpstatus(abfd, note,
12142 224, 544, 528, 768);
12143 default:
12144 return true;
12147 case SOLARIS_NT_LWPSINFO:
12148 /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
12149 if (note->descsz == 128 || note->descsz == 152)
12150 elf_tdata (abfd)->core->lwpid =
12151 bfd_get_32 (abfd, note->descdata + 4);
12152 break;
12154 default:
12155 break;
12158 return true;
12161 /* For name starting with "CORE" this may be either a Solaris
12162 core file or a gdb-generated core file. Do Solaris-specific
12163 processing on selected note types first with
12164 elfcore_grok_solaris_note(), then process the note
12165 in elfcore_grok_note(). */
12167 static bool
12168 elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
12170 if (!elfcore_grok_solaris_note_impl (abfd, note))
12171 return false;
12173 return elfcore_grok_note (abfd, note);
12176 static bool
12177 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
12179 if (note->type == NT_OPENBSD_PROCINFO)
12180 return elfcore_grok_openbsd_procinfo (abfd, note);
12182 if (note->type == NT_OPENBSD_REGS)
12183 return elfcore_make_note_pseudosection (abfd, ".reg", note);
12185 if (note->type == NT_OPENBSD_FPREGS)
12186 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
12188 if (note->type == NT_OPENBSD_XFPREGS)
12189 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
12191 if (note->type == NT_OPENBSD_AUXV)
12192 return elfcore_make_auxv_note_section (abfd, note, 0);
12194 if (note->type == NT_OPENBSD_WCOOKIE)
12196 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
12197 SEC_HAS_CONTENTS);
12199 if (sect == NULL)
12200 return false;
12201 sect->size = note->descsz;
12202 sect->filepos = note->descpos;
12203 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
12205 return true;
12208 return true;
12211 static bool
12212 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
12214 void *ddata = note->descdata;
12215 char buf[100];
12216 char *name;
12217 asection *sect;
12218 short sig;
12219 unsigned flags;
12221 if (note->descsz < 16)
12222 return false;
12224 /* nto_procfs_status 'pid' field is at offset 0. */
12225 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
12227 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
12228 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
12230 /* nto_procfs_status 'flags' field is at offset 8. */
12231 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
12233 /* nto_procfs_status 'what' field is at offset 14. */
12234 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
12236 elf_tdata (abfd)->core->signal = sig;
12237 elf_tdata (abfd)->core->lwpid = *tid;
12240 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
12241 do not come from signals so we make sure we set the current
12242 thread just in case. */
12243 if (flags & 0x00000080)
12244 elf_tdata (abfd)->core->lwpid = *tid;
12246 /* Make a ".qnx_core_status/%d" section. */
12247 sprintf (buf, ".qnx_core_status/%ld", *tid);
12249 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
12250 if (name == NULL)
12251 return false;
12252 strcpy (name, buf);
12254 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12255 if (sect == NULL)
12256 return false;
12258 sect->size = note->descsz;
12259 sect->filepos = note->descpos;
12260 sect->alignment_power = 2;
12262 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
12265 static bool
12266 elfcore_grok_nto_regs (bfd *abfd,
12267 Elf_Internal_Note *note,
12268 long tid,
12269 char *base)
12271 char buf[100];
12272 char *name;
12273 asection *sect;
12275 /* Make a "(base)/%d" section. */
12276 sprintf (buf, "%s/%ld", base, tid);
12278 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
12279 if (name == NULL)
12280 return false;
12281 strcpy (name, buf);
12283 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12284 if (sect == NULL)
12285 return false;
12287 sect->size = note->descsz;
12288 sect->filepos = note->descpos;
12289 sect->alignment_power = 2;
12291 /* This is the current thread. */
12292 if (elf_tdata (abfd)->core->lwpid == tid)
12293 return elfcore_maybe_make_sect (abfd, base, sect);
12295 return true;
12298 static bool
12299 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
12301 /* Every GREG section has a STATUS section before it. Store the
12302 tid from the previous call to pass down to the next gregs
12303 function. */
12304 static long tid = 1;
12306 switch (note->type)
12308 case QNT_CORE_INFO:
12309 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
12310 case QNT_CORE_STATUS:
12311 return elfcore_grok_nto_status (abfd, note, &tid);
12312 case QNT_CORE_GREG:
12313 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
12314 case QNT_CORE_FPREG:
12315 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
12316 default:
12317 return true;
12321 static bool
12322 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
12324 char *name;
12325 asection *sect;
12326 size_t len;
12328 /* Use note name as section name. */
12329 len = note->namesz;
12330 name = (char *) bfd_alloc (abfd, len);
12331 if (name == NULL)
12332 return false;
12333 memcpy (name, note->namedata, len);
12334 name[len - 1] = '\0';
12336 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12337 if (sect == NULL)
12338 return false;
12340 sect->size = note->descsz;
12341 sect->filepos = note->descpos;
12342 sect->alignment_power = 1;
12344 return true;
12347 /* Function: elfcore_write_note
12349 Inputs:
12350 buffer to hold note, and current size of buffer
12351 name of note
12352 type of note
12353 data for note
12354 size of data for note
12356 Writes note to end of buffer. ELF64 notes are written exactly as
12357 for ELF32, despite the current (as of 2006) ELF gabi specifying
12358 that they ought to have 8-byte namesz and descsz field, and have
12359 8-byte alignment. Other writers, eg. Linux kernel, do the same.
12361 Return:
12362 Pointer to realloc'd buffer, *BUFSIZ updated. */
12364 char *
12365 elfcore_write_note (bfd *abfd,
12366 char *buf,
12367 int *bufsiz,
12368 const char *name,
12369 int type,
12370 const void *input,
12371 int size)
12373 Elf_External_Note *xnp;
12374 size_t namesz;
12375 size_t newspace;
12376 char *dest;
12378 namesz = 0;
12379 if (name != NULL)
12380 namesz = strlen (name) + 1;
12382 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
12384 buf = (char *) realloc (buf, *bufsiz + newspace);
12385 if (buf == NULL)
12386 return buf;
12387 dest = buf + *bufsiz;
12388 *bufsiz += newspace;
12389 xnp = (Elf_External_Note *) dest;
12390 H_PUT_32 (abfd, namesz, xnp->namesz);
12391 H_PUT_32 (abfd, size, xnp->descsz);
12392 H_PUT_32 (abfd, type, xnp->type);
12393 dest = xnp->name;
12394 if (name != NULL)
12396 memcpy (dest, name, namesz);
12397 dest += namesz;
12398 while (namesz & 3)
12400 *dest++ = '\0';
12401 ++namesz;
12404 memcpy (dest, input, size);
12405 dest += size;
12406 while (size & 3)
12408 *dest++ = '\0';
12409 ++size;
12411 return buf;
12414 /* gcc-8 warns (*) on all the strncpy calls in this function about
12415 possible string truncation. The "truncation" is not a bug. We
12416 have an external representation of structs with fields that are not
12417 necessarily NULL terminated and corresponding internal
12418 representation fields that are one larger so that they can always
12419 be NULL terminated.
12420 gcc versions between 4.2 and 4.6 do not allow pragma control of
12421 diagnostics inside functions, giving a hard error if you try to use
12422 the finer control available with later versions.
12423 gcc prior to 4.2 warns about diagnostic push and pop.
12424 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
12425 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
12426 (*) Depending on your system header files! */
12427 #if GCC_VERSION >= 8000
12428 # pragma GCC diagnostic push
12429 # pragma GCC diagnostic ignored "-Wstringop-truncation"
12430 #endif
12431 char *
12432 elfcore_write_prpsinfo (bfd *abfd,
12433 char *buf,
12434 int *bufsiz,
12435 const char *fname,
12436 const char *psargs)
12438 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12440 if (bed->elf_backend_write_core_note != NULL)
12442 char *ret;
12443 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12444 NT_PRPSINFO, fname, psargs);
12445 if (ret != NULL)
12446 return ret;
12449 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
12450 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
12451 if (bed->s->elfclass == ELFCLASS32)
12453 # if defined (HAVE_PSINFO32_T)
12454 psinfo32_t data;
12455 int note_type = NT_PSINFO;
12456 # else
12457 prpsinfo32_t data;
12458 int note_type = NT_PRPSINFO;
12459 # endif
12461 memset (&data, 0, sizeof (data));
12462 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12463 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12464 return elfcore_write_note (abfd, buf, bufsiz,
12465 "CORE", note_type, &data, sizeof (data));
12467 else
12468 # endif
12470 # if defined (HAVE_PSINFO_T)
12471 psinfo_t data;
12472 int note_type = NT_PSINFO;
12473 # else
12474 prpsinfo_t data;
12475 int note_type = NT_PRPSINFO;
12476 # endif
12478 memset (&data, 0, sizeof (data));
12479 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12480 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12481 return elfcore_write_note (abfd, buf, bufsiz,
12482 "CORE", note_type, &data, sizeof (data));
12484 #endif /* PSINFO_T or PRPSINFO_T */
12486 free (buf);
12487 return NULL;
12489 #if GCC_VERSION >= 8000
12490 # pragma GCC diagnostic pop
12491 #endif
12493 char *
12494 elfcore_write_linux_prpsinfo32
12495 (bfd *abfd, char *buf, int *bufsiz,
12496 const struct elf_internal_linux_prpsinfo *prpsinfo)
12498 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
12500 struct elf_external_linux_prpsinfo32_ugid16 data;
12502 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
12503 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
12504 &data, sizeof (data));
12506 else
12508 struct elf_external_linux_prpsinfo32_ugid32 data;
12510 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
12511 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
12512 &data, sizeof (data));
12516 char *
12517 elfcore_write_linux_prpsinfo64
12518 (bfd *abfd, char *buf, int *bufsiz,
12519 const struct elf_internal_linux_prpsinfo *prpsinfo)
12521 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
12523 struct elf_external_linux_prpsinfo64_ugid16 data;
12525 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
12526 return elfcore_write_note (abfd, buf, bufsiz,
12527 "CORE", NT_PRPSINFO, &data, sizeof (data));
12529 else
12531 struct elf_external_linux_prpsinfo64_ugid32 data;
12533 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
12534 return elfcore_write_note (abfd, buf, bufsiz,
12535 "CORE", NT_PRPSINFO, &data, sizeof (data));
12539 char *
12540 elfcore_write_prstatus (bfd *abfd,
12541 char *buf,
12542 int *bufsiz,
12543 long pid,
12544 int cursig,
12545 const void *gregs)
12547 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12549 if (bed->elf_backend_write_core_note != NULL)
12551 char *ret;
12552 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12553 NT_PRSTATUS,
12554 pid, cursig, gregs);
12555 if (ret != NULL)
12556 return ret;
12559 #if defined (HAVE_PRSTATUS_T)
12560 #if defined (HAVE_PRSTATUS32_T)
12561 if (bed->s->elfclass == ELFCLASS32)
12563 prstatus32_t prstat;
12565 memset (&prstat, 0, sizeof (prstat));
12566 prstat.pr_pid = pid;
12567 prstat.pr_cursig = cursig;
12568 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12569 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
12570 NT_PRSTATUS, &prstat, sizeof (prstat));
12572 else
12573 #endif
12575 prstatus_t prstat;
12577 memset (&prstat, 0, sizeof (prstat));
12578 prstat.pr_pid = pid;
12579 prstat.pr_cursig = cursig;
12580 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12581 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
12582 NT_PRSTATUS, &prstat, sizeof (prstat));
12584 #endif /* HAVE_PRSTATUS_T */
12586 free (buf);
12587 return NULL;
12590 #if defined (HAVE_LWPSTATUS_T)
12591 char *
12592 elfcore_write_lwpstatus (bfd *abfd,
12593 char *buf,
12594 int *bufsiz,
12595 long pid,
12596 int cursig,
12597 const void *gregs)
12599 lwpstatus_t lwpstat;
12600 const char *note_name = "CORE";
12602 memset (&lwpstat, 0, sizeof (lwpstat));
12603 lwpstat.pr_lwpid = pid >> 16;
12604 lwpstat.pr_cursig = cursig;
12605 #if defined (HAVE_LWPSTATUS_T_PR_REG)
12606 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
12607 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
12608 #if !defined(gregs)
12609 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
12610 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
12611 #else
12612 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
12613 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
12614 #endif
12615 #endif
12616 return elfcore_write_note (abfd, buf, bufsiz, note_name,
12617 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
12619 #endif /* HAVE_LWPSTATUS_T */
12621 #if defined (HAVE_PSTATUS_T)
12622 char *
12623 elfcore_write_pstatus (bfd *abfd,
12624 char *buf,
12625 int *bufsiz,
12626 long pid,
12627 int cursig ATTRIBUTE_UNUSED,
12628 const void *gregs ATTRIBUTE_UNUSED)
12630 const char *note_name = "CORE";
12631 #if defined (HAVE_PSTATUS32_T)
12632 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12634 if (bed->s->elfclass == ELFCLASS32)
12636 pstatus32_t pstat;
12638 memset (&pstat, 0, sizeof (pstat));
12639 pstat.pr_pid = pid & 0xffff;
12640 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
12641 NT_PSTATUS, &pstat, sizeof (pstat));
12642 return buf;
12644 else
12645 #endif
12647 pstatus_t pstat;
12649 memset (&pstat, 0, sizeof (pstat));
12650 pstat.pr_pid = pid & 0xffff;
12651 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
12652 NT_PSTATUS, &pstat, sizeof (pstat));
12653 return buf;
12656 #endif /* HAVE_PSTATUS_T */
12658 char *
12659 elfcore_write_prfpreg (bfd *abfd,
12660 char *buf,
12661 int *bufsiz,
12662 const void *fpregs,
12663 int size)
12665 const char *note_name = "CORE";
12666 return elfcore_write_note (abfd, buf, bufsiz,
12667 note_name, NT_FPREGSET, fpregs, size);
12670 char *
12671 elfcore_write_prxfpreg (bfd *abfd,
12672 char *buf,
12673 int *bufsiz,
12674 const void *xfpregs,
12675 int size)
12677 char *note_name = "LINUX";
12678 return elfcore_write_note (abfd, buf, bufsiz,
12679 note_name, NT_PRXFPREG, xfpregs, size);
12682 char *
12683 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
12684 const void *xfpregs, int size)
12686 char *note_name;
12687 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
12688 note_name = "FreeBSD";
12689 else
12690 note_name = "LINUX";
12691 return elfcore_write_note (abfd, buf, bufsiz,
12692 note_name, NT_X86_XSTATE, xfpregs, size);
12695 char *
12696 elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
12697 const void *regs, int size)
12699 char *note_name = "FreeBSD";
12700 return elfcore_write_note (abfd, buf, bufsiz,
12701 note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
12704 char *
12705 elfcore_write_ppc_vmx (bfd *abfd,
12706 char *buf,
12707 int *bufsiz,
12708 const void *ppc_vmx,
12709 int size)
12711 char *note_name = "LINUX";
12712 return elfcore_write_note (abfd, buf, bufsiz,
12713 note_name, NT_PPC_VMX, ppc_vmx, size);
12716 char *
12717 elfcore_write_ppc_vsx (bfd *abfd,
12718 char *buf,
12719 int *bufsiz,
12720 const void *ppc_vsx,
12721 int size)
12723 char *note_name = "LINUX";
12724 return elfcore_write_note (abfd, buf, bufsiz,
12725 note_name, NT_PPC_VSX, ppc_vsx, size);
12728 char *
12729 elfcore_write_ppc_tar (bfd *abfd,
12730 char *buf,
12731 int *bufsiz,
12732 const void *ppc_tar,
12733 int size)
12735 char *note_name = "LINUX";
12736 return elfcore_write_note (abfd, buf, bufsiz,
12737 note_name, NT_PPC_TAR, ppc_tar, size);
12740 char *
12741 elfcore_write_ppc_ppr (bfd *abfd,
12742 char *buf,
12743 int *bufsiz,
12744 const void *ppc_ppr,
12745 int size)
12747 char *note_name = "LINUX";
12748 return elfcore_write_note (abfd, buf, bufsiz,
12749 note_name, NT_PPC_PPR, ppc_ppr, size);
12752 char *
12753 elfcore_write_ppc_dscr (bfd *abfd,
12754 char *buf,
12755 int *bufsiz,
12756 const void *ppc_dscr,
12757 int size)
12759 char *note_name = "LINUX";
12760 return elfcore_write_note (abfd, buf, bufsiz,
12761 note_name, NT_PPC_DSCR, ppc_dscr, size);
12764 char *
12765 elfcore_write_ppc_ebb (bfd *abfd,
12766 char *buf,
12767 int *bufsiz,
12768 const void *ppc_ebb,
12769 int size)
12771 char *note_name = "LINUX";
12772 return elfcore_write_note (abfd, buf, bufsiz,
12773 note_name, NT_PPC_EBB, ppc_ebb, size);
12776 char *
12777 elfcore_write_ppc_pmu (bfd *abfd,
12778 char *buf,
12779 int *bufsiz,
12780 const void *ppc_pmu,
12781 int size)
12783 char *note_name = "LINUX";
12784 return elfcore_write_note (abfd, buf, bufsiz,
12785 note_name, NT_PPC_PMU, ppc_pmu, size);
12788 char *
12789 elfcore_write_ppc_tm_cgpr (bfd *abfd,
12790 char *buf,
12791 int *bufsiz,
12792 const void *ppc_tm_cgpr,
12793 int size)
12795 char *note_name = "LINUX";
12796 return elfcore_write_note (abfd, buf, bufsiz,
12797 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
12800 char *
12801 elfcore_write_ppc_tm_cfpr (bfd *abfd,
12802 char *buf,
12803 int *bufsiz,
12804 const void *ppc_tm_cfpr,
12805 int size)
12807 char *note_name = "LINUX";
12808 return elfcore_write_note (abfd, buf, bufsiz,
12809 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
12812 char *
12813 elfcore_write_ppc_tm_cvmx (bfd *abfd,
12814 char *buf,
12815 int *bufsiz,
12816 const void *ppc_tm_cvmx,
12817 int size)
12819 char *note_name = "LINUX";
12820 return elfcore_write_note (abfd, buf, bufsiz,
12821 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
12824 char *
12825 elfcore_write_ppc_tm_cvsx (bfd *abfd,
12826 char *buf,
12827 int *bufsiz,
12828 const void *ppc_tm_cvsx,
12829 int size)
12831 char *note_name = "LINUX";
12832 return elfcore_write_note (abfd, buf, bufsiz,
12833 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
12836 char *
12837 elfcore_write_ppc_tm_spr (bfd *abfd,
12838 char *buf,
12839 int *bufsiz,
12840 const void *ppc_tm_spr,
12841 int size)
12843 char *note_name = "LINUX";
12844 return elfcore_write_note (abfd, buf, bufsiz,
12845 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
12848 char *
12849 elfcore_write_ppc_tm_ctar (bfd *abfd,
12850 char *buf,
12851 int *bufsiz,
12852 const void *ppc_tm_ctar,
12853 int size)
12855 char *note_name = "LINUX";
12856 return elfcore_write_note (abfd, buf, bufsiz,
12857 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
12860 char *
12861 elfcore_write_ppc_tm_cppr (bfd *abfd,
12862 char *buf,
12863 int *bufsiz,
12864 const void *ppc_tm_cppr,
12865 int size)
12867 char *note_name = "LINUX";
12868 return elfcore_write_note (abfd, buf, bufsiz,
12869 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
12872 char *
12873 elfcore_write_ppc_tm_cdscr (bfd *abfd,
12874 char *buf,
12875 int *bufsiz,
12876 const void *ppc_tm_cdscr,
12877 int size)
12879 char *note_name = "LINUX";
12880 return elfcore_write_note (abfd, buf, bufsiz,
12881 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
12884 static char *
12885 elfcore_write_s390_high_gprs (bfd *abfd,
12886 char *buf,
12887 int *bufsiz,
12888 const void *s390_high_gprs,
12889 int size)
12891 char *note_name = "LINUX";
12892 return elfcore_write_note (abfd, buf, bufsiz,
12893 note_name, NT_S390_HIGH_GPRS,
12894 s390_high_gprs, size);
12897 char *
12898 elfcore_write_s390_timer (bfd *abfd,
12899 char *buf,
12900 int *bufsiz,
12901 const void *s390_timer,
12902 int size)
12904 char *note_name = "LINUX";
12905 return elfcore_write_note (abfd, buf, bufsiz,
12906 note_name, NT_S390_TIMER, s390_timer, size);
12909 char *
12910 elfcore_write_s390_todcmp (bfd *abfd,
12911 char *buf,
12912 int *bufsiz,
12913 const void *s390_todcmp,
12914 int size)
12916 char *note_name = "LINUX";
12917 return elfcore_write_note (abfd, buf, bufsiz,
12918 note_name, NT_S390_TODCMP, s390_todcmp, size);
12921 char *
12922 elfcore_write_s390_todpreg (bfd *abfd,
12923 char *buf,
12924 int *bufsiz,
12925 const void *s390_todpreg,
12926 int size)
12928 char *note_name = "LINUX";
12929 return elfcore_write_note (abfd, buf, bufsiz,
12930 note_name, NT_S390_TODPREG, s390_todpreg, size);
12933 char *
12934 elfcore_write_s390_ctrs (bfd *abfd,
12935 char *buf,
12936 int *bufsiz,
12937 const void *s390_ctrs,
12938 int size)
12940 char *note_name = "LINUX";
12941 return elfcore_write_note (abfd, buf, bufsiz,
12942 note_name, NT_S390_CTRS, s390_ctrs, size);
12945 char *
12946 elfcore_write_s390_prefix (bfd *abfd,
12947 char *buf,
12948 int *bufsiz,
12949 const void *s390_prefix,
12950 int size)
12952 char *note_name = "LINUX";
12953 return elfcore_write_note (abfd, buf, bufsiz,
12954 note_name, NT_S390_PREFIX, s390_prefix, size);
12957 char *
12958 elfcore_write_s390_last_break (bfd *abfd,
12959 char *buf,
12960 int *bufsiz,
12961 const void *s390_last_break,
12962 int size)
12964 char *note_name = "LINUX";
12965 return elfcore_write_note (abfd, buf, bufsiz,
12966 note_name, NT_S390_LAST_BREAK,
12967 s390_last_break, size);
12970 char *
12971 elfcore_write_s390_system_call (bfd *abfd,
12972 char *buf,
12973 int *bufsiz,
12974 const void *s390_system_call,
12975 int size)
12977 char *note_name = "LINUX";
12978 return elfcore_write_note (abfd, buf, bufsiz,
12979 note_name, NT_S390_SYSTEM_CALL,
12980 s390_system_call, size);
12983 char *
12984 elfcore_write_s390_tdb (bfd *abfd,
12985 char *buf,
12986 int *bufsiz,
12987 const void *s390_tdb,
12988 int size)
12990 char *note_name = "LINUX";
12991 return elfcore_write_note (abfd, buf, bufsiz,
12992 note_name, NT_S390_TDB, s390_tdb, size);
12995 char *
12996 elfcore_write_s390_vxrs_low (bfd *abfd,
12997 char *buf,
12998 int *bufsiz,
12999 const void *s390_vxrs_low,
13000 int size)
13002 char *note_name = "LINUX";
13003 return elfcore_write_note (abfd, buf, bufsiz,
13004 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
13007 char *
13008 elfcore_write_s390_vxrs_high (bfd *abfd,
13009 char *buf,
13010 int *bufsiz,
13011 const void *s390_vxrs_high,
13012 int size)
13014 char *note_name = "LINUX";
13015 return elfcore_write_note (abfd, buf, bufsiz,
13016 note_name, NT_S390_VXRS_HIGH,
13017 s390_vxrs_high, size);
13020 char *
13021 elfcore_write_s390_gs_cb (bfd *abfd,
13022 char *buf,
13023 int *bufsiz,
13024 const void *s390_gs_cb,
13025 int size)
13027 char *note_name = "LINUX";
13028 return elfcore_write_note (abfd, buf, bufsiz,
13029 note_name, NT_S390_GS_CB,
13030 s390_gs_cb, size);
13033 char *
13034 elfcore_write_s390_gs_bc (bfd *abfd,
13035 char *buf,
13036 int *bufsiz,
13037 const void *s390_gs_bc,
13038 int size)
13040 char *note_name = "LINUX";
13041 return elfcore_write_note (abfd, buf, bufsiz,
13042 note_name, NT_S390_GS_BC,
13043 s390_gs_bc, size);
13046 char *
13047 elfcore_write_arm_vfp (bfd *abfd,
13048 char *buf,
13049 int *bufsiz,
13050 const void *arm_vfp,
13051 int size)
13053 char *note_name = "LINUX";
13054 return elfcore_write_note (abfd, buf, bufsiz,
13055 note_name, NT_ARM_VFP, arm_vfp, size);
13058 char *
13059 elfcore_write_aarch_tls (bfd *abfd,
13060 char *buf,
13061 int *bufsiz,
13062 const void *aarch_tls,
13063 int size)
13065 char *note_name = "LINUX";
13066 return elfcore_write_note (abfd, buf, bufsiz,
13067 note_name, NT_ARM_TLS, aarch_tls, size);
13070 char *
13071 elfcore_write_aarch_hw_break (bfd *abfd,
13072 char *buf,
13073 int *bufsiz,
13074 const void *aarch_hw_break,
13075 int size)
13077 char *note_name = "LINUX";
13078 return elfcore_write_note (abfd, buf, bufsiz,
13079 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
13082 char *
13083 elfcore_write_aarch_hw_watch (bfd *abfd,
13084 char *buf,
13085 int *bufsiz,
13086 const void *aarch_hw_watch,
13087 int size)
13089 char *note_name = "LINUX";
13090 return elfcore_write_note (abfd, buf, bufsiz,
13091 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
13094 char *
13095 elfcore_write_aarch_sve (bfd *abfd,
13096 char *buf,
13097 int *bufsiz,
13098 const void *aarch_sve,
13099 int size)
13101 char *note_name = "LINUX";
13102 return elfcore_write_note (abfd, buf, bufsiz,
13103 note_name, NT_ARM_SVE, aarch_sve, size);
13106 char *
13107 elfcore_write_aarch_pauth (bfd *abfd,
13108 char *buf,
13109 int *bufsiz,
13110 const void *aarch_pauth,
13111 int size)
13113 char *note_name = "LINUX";
13114 return elfcore_write_note (abfd, buf, bufsiz,
13115 note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
13118 char *
13119 elfcore_write_aarch_mte (bfd *abfd,
13120 char *buf,
13121 int *bufsiz,
13122 const void *aarch_mte,
13123 int size)
13125 char *note_name = "LINUX";
13126 return elfcore_write_note (abfd, buf, bufsiz,
13127 note_name, NT_ARM_TAGGED_ADDR_CTRL,
13128 aarch_mte,
13129 size);
13132 char *
13133 elfcore_write_aarch_ssve (bfd *abfd,
13134 char *buf,
13135 int *bufsiz,
13136 const void *aarch_ssve,
13137 int size)
13139 char *note_name = "LINUX";
13140 return elfcore_write_note (abfd, buf, bufsiz,
13141 note_name, NT_ARM_SSVE,
13142 aarch_ssve,
13143 size);
13146 char *
13147 elfcore_write_aarch_za (bfd *abfd,
13148 char *buf,
13149 int *bufsiz,
13150 const void *aarch_za,
13151 int size)
13153 char *note_name = "LINUX";
13154 return elfcore_write_note (abfd, buf, bufsiz,
13155 note_name, NT_ARM_ZA,
13156 aarch_za,
13157 size);
13160 /* Write the buffer of zt register values in aarch_zt (length SIZE) into
13161 the note buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
13162 written into. Return a pointer to the new start of the note buffer, to
13163 replace BUF which may no longer be valid. */
13165 char *
13166 elfcore_write_aarch_zt (bfd *abfd,
13167 char *buf,
13168 int *bufsiz,
13169 const void *aarch_zt,
13170 int size)
13172 char *note_name = "LINUX";
13173 return elfcore_write_note (abfd, buf, bufsiz,
13174 note_name, NT_ARM_ZT,
13175 aarch_zt,
13176 size);
13179 char *
13180 elfcore_write_arc_v2 (bfd *abfd,
13181 char *buf,
13182 int *bufsiz,
13183 const void *arc_v2,
13184 int size)
13186 char *note_name = "LINUX";
13187 return elfcore_write_note (abfd, buf, bufsiz,
13188 note_name, NT_ARC_V2, arc_v2, size);
13191 char *
13192 elfcore_write_loongarch_cpucfg (bfd *abfd,
13193 char *buf,
13194 int *bufsiz,
13195 const void *loongarch_cpucfg,
13196 int size)
13198 char *note_name = "LINUX";
13199 return elfcore_write_note (abfd, buf, bufsiz,
13200 note_name, NT_LARCH_CPUCFG,
13201 loongarch_cpucfg, size);
13204 char *
13205 elfcore_write_loongarch_lbt (bfd *abfd,
13206 char *buf,
13207 int *bufsiz,
13208 const void *loongarch_lbt,
13209 int size)
13211 char *note_name = "LINUX";
13212 return elfcore_write_note (abfd, buf, bufsiz,
13213 note_name, NT_LARCH_LBT, loongarch_lbt, size);
13216 char *
13217 elfcore_write_loongarch_lsx (bfd *abfd,
13218 char *buf,
13219 int *bufsiz,
13220 const void *loongarch_lsx,
13221 int size)
13223 char *note_name = "LINUX";
13224 return elfcore_write_note (abfd, buf, bufsiz,
13225 note_name, NT_LARCH_LSX, loongarch_lsx, size);
13228 char *
13229 elfcore_write_loongarch_lasx (bfd *abfd,
13230 char *buf,
13231 int *bufsiz,
13232 const void *loongarch_lasx,
13233 int size)
13235 char *note_name = "LINUX";
13236 return elfcore_write_note (abfd, buf, bufsiz,
13237 note_name, NT_LARCH_LASX, loongarch_lasx, size);
13240 /* Write the buffer of csr values in CSRS (length SIZE) into the note
13241 buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
13242 written into. Return a pointer to the new start of the note buffer, to
13243 replace BUF which may no longer be valid. */
13245 char *
13246 elfcore_write_riscv_csr (bfd *abfd,
13247 char *buf,
13248 int *bufsiz,
13249 const void *csrs,
13250 int size)
13252 const char *note_name = "GDB";
13253 return elfcore_write_note (abfd, buf, bufsiz,
13254 note_name, NT_RISCV_CSR, csrs, size);
13257 /* Write the target description (a string) pointed to by TDESC, length
13258 SIZE, into the note buffer BUF, and update *BUFSIZ. ABFD is the bfd the
13259 note is being written into. Return a pointer to the new start of the
13260 note buffer, to replace BUF which may no longer be valid. */
13262 char *
13263 elfcore_write_gdb_tdesc (bfd *abfd,
13264 char *buf,
13265 int *bufsiz,
13266 const void *tdesc,
13267 int size)
13269 const char *note_name = "GDB";
13270 return elfcore_write_note (abfd, buf, bufsiz,
13271 note_name, NT_GDB_TDESC, tdesc, size);
13274 char *
13275 elfcore_write_register_note (bfd *abfd,
13276 char *buf,
13277 int *bufsiz,
13278 const char *section,
13279 const void *data,
13280 int size)
13282 if (strcmp (section, ".reg2") == 0)
13283 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
13284 if (strcmp (section, ".reg-xfp") == 0)
13285 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
13286 if (strcmp (section, ".reg-xstate") == 0)
13287 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
13288 if (strcmp (section, ".reg-x86-segbases") == 0)
13289 return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
13290 if (strcmp (section, ".reg-ppc-vmx") == 0)
13291 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
13292 if (strcmp (section, ".reg-ppc-vsx") == 0)
13293 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
13294 if (strcmp (section, ".reg-ppc-tar") == 0)
13295 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
13296 if (strcmp (section, ".reg-ppc-ppr") == 0)
13297 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
13298 if (strcmp (section, ".reg-ppc-dscr") == 0)
13299 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
13300 if (strcmp (section, ".reg-ppc-ebb") == 0)
13301 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
13302 if (strcmp (section, ".reg-ppc-pmu") == 0)
13303 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
13304 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
13305 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
13306 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
13307 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
13308 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
13309 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
13310 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
13311 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
13312 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
13313 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
13314 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
13315 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
13316 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
13317 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
13318 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
13319 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
13320 if (strcmp (section, ".reg-s390-high-gprs") == 0)
13321 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
13322 if (strcmp (section, ".reg-s390-timer") == 0)
13323 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
13324 if (strcmp (section, ".reg-s390-todcmp") == 0)
13325 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
13326 if (strcmp (section, ".reg-s390-todpreg") == 0)
13327 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
13328 if (strcmp (section, ".reg-s390-ctrs") == 0)
13329 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
13330 if (strcmp (section, ".reg-s390-prefix") == 0)
13331 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
13332 if (strcmp (section, ".reg-s390-last-break") == 0)
13333 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
13334 if (strcmp (section, ".reg-s390-system-call") == 0)
13335 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
13336 if (strcmp (section, ".reg-s390-tdb") == 0)
13337 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
13338 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
13339 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
13340 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
13341 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
13342 if (strcmp (section, ".reg-s390-gs-cb") == 0)
13343 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
13344 if (strcmp (section, ".reg-s390-gs-bc") == 0)
13345 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
13346 if (strcmp (section, ".reg-arm-vfp") == 0)
13347 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
13348 if (strcmp (section, ".reg-aarch-tls") == 0)
13349 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
13350 if (strcmp (section, ".reg-aarch-hw-break") == 0)
13351 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
13352 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
13353 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
13354 if (strcmp (section, ".reg-aarch-sve") == 0)
13355 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
13356 if (strcmp (section, ".reg-aarch-pauth") == 0)
13357 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
13358 if (strcmp (section, ".reg-aarch-mte") == 0)
13359 return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
13360 if (strcmp (section, ".reg-aarch-ssve") == 0)
13361 return elfcore_write_aarch_ssve (abfd, buf, bufsiz, data, size);
13362 if (strcmp (section, ".reg-aarch-za") == 0)
13363 return elfcore_write_aarch_za (abfd, buf, bufsiz, data, size);
13364 if (strcmp (section, ".reg-aarch-zt") == 0)
13365 return elfcore_write_aarch_zt (abfd, buf, bufsiz, data, size);
13366 if (strcmp (section, ".reg-arc-v2") == 0)
13367 return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
13368 if (strcmp (section, ".gdb-tdesc") == 0)
13369 return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
13370 if (strcmp (section, ".reg-riscv-csr") == 0)
13371 return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
13372 if (strcmp (section, ".reg-loongarch-cpucfg") == 0)
13373 return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size);
13374 if (strcmp (section, ".reg-loongarch-lbt") == 0)
13375 return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size);
13376 if (strcmp (section, ".reg-loongarch-lsx") == 0)
13377 return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
13378 if (strcmp (section, ".reg-loongarch-lasx") == 0)
13379 return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
13380 return NULL;
13383 char *
13384 elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
13385 const void *buf, int bufsiz)
13387 return elfcore_write_note (obfd, note_data, note_size,
13388 "CORE", NT_FILE, buf, bufsiz);
13391 static bool
13392 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
13393 size_t align)
13395 char *p;
13397 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
13398 gABI specifies that PT_NOTE alignment should be aligned to 4
13399 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
13400 align is less than 4, we use 4 byte alignment. */
13401 if (align < 4)
13402 align = 4;
13403 if (align != 4 && align != 8)
13404 return false;
13406 p = buf;
13407 while (p < buf + size)
13409 Elf_External_Note *xnp = (Elf_External_Note *) p;
13410 Elf_Internal_Note in;
13412 if (offsetof (Elf_External_Note, name) > buf - p + size)
13413 return false;
13415 in.type = H_GET_32 (abfd, xnp->type);
13417 in.namesz = H_GET_32 (abfd, xnp->namesz);
13418 in.namedata = xnp->name;
13419 if (in.namesz > buf - in.namedata + size)
13420 return false;
13422 in.descsz = H_GET_32 (abfd, xnp->descsz);
13423 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
13424 in.descpos = offset + (in.descdata - buf);
13425 if (in.descsz != 0
13426 && (in.descdata >= buf + size
13427 || in.descsz > buf - in.descdata + size))
13428 return false;
13430 switch (bfd_get_format (abfd))
13432 default:
13433 return true;
13435 case bfd_core:
13437 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
13438 struct
13440 const char * string;
13441 size_t len;
13442 bool (*func) (bfd *, Elf_Internal_Note *);
13444 grokers[] =
13446 GROKER_ELEMENT ("", elfcore_grok_note),
13447 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
13448 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
13449 GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
13450 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
13451 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
13452 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
13453 GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note)
13455 #undef GROKER_ELEMENT
13456 int i;
13458 for (i = ARRAY_SIZE (grokers); i--;)
13460 if (in.namesz >= grokers[i].len
13461 && strncmp (in.namedata, grokers[i].string,
13462 grokers[i].len) == 0)
13464 if (! grokers[i].func (abfd, & in))
13465 return false;
13466 break;
13469 break;
13472 case bfd_object:
13473 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
13475 if (! elfobj_grok_gnu_note (abfd, &in))
13476 return false;
13478 else if (in.namesz == sizeof "stapsdt"
13479 && strcmp (in.namedata, "stapsdt") == 0)
13481 if (! elfobj_grok_stapsdt_note (abfd, &in))
13482 return false;
13484 break;
13487 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
13490 return true;
13493 bool
13494 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
13495 size_t align)
13497 char *buf;
13499 if (size == 0 || (size + 1) == 0)
13500 return true;
13502 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
13503 return false;
13505 buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
13506 if (buf == NULL)
13507 return false;
13509 /* PR 17512: file: ec08f814
13510 0-termintate the buffer so that string searches will not overflow. */
13511 buf[size] = 0;
13513 if (!elf_parse_notes (abfd, buf, size, offset, align))
13515 free (buf);
13516 return false;
13519 free (buf);
13520 return true;
13523 /* Providing external access to the ELF program header table. */
13525 /* Return an upper bound on the number of bytes required to store a
13526 copy of ABFD's program header table entries. Return -1 if an error
13527 occurs; bfd_get_error will return an appropriate code. */
13529 long
13530 bfd_get_elf_phdr_upper_bound (bfd *abfd)
13532 if (abfd->xvec->flavour != bfd_target_elf_flavour)
13534 bfd_set_error (bfd_error_wrong_format);
13535 return -1;
13538 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
13541 /* Copy ABFD's program header table entries to *PHDRS. The entries
13542 will be stored as an array of Elf_Internal_Phdr structures, as
13543 defined in include/elf/internal.h. To find out how large the
13544 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
13546 Return the number of program header table entries read, or -1 if an
13547 error occurs; bfd_get_error will return an appropriate code. */
13550 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
13552 int num_phdrs;
13554 if (abfd->xvec->flavour != bfd_target_elf_flavour)
13556 bfd_set_error (bfd_error_wrong_format);
13557 return -1;
13560 num_phdrs = elf_elfheader (abfd)->e_phnum;
13561 if (num_phdrs != 0)
13562 memcpy (phdrs, elf_tdata (abfd)->phdr,
13563 num_phdrs * sizeof (Elf_Internal_Phdr));
13565 return num_phdrs;
13568 enum elf_reloc_type_class
13569 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
13570 const asection *rel_sec ATTRIBUTE_UNUSED,
13571 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
13573 return reloc_class_normal;
13576 /* For RELA architectures, return the relocation value for a
13577 relocation against a local symbol. */
13579 bfd_vma
13580 _bfd_elf_rela_local_sym (bfd *abfd,
13581 Elf_Internal_Sym *sym,
13582 asection **psec,
13583 Elf_Internal_Rela *rel)
13585 asection *sec = *psec;
13586 bfd_vma relocation;
13588 relocation = (sec->output_section->vma
13589 + sec->output_offset
13590 + sym->st_value);
13591 if ((sec->flags & SEC_MERGE)
13592 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
13593 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
13595 rel->r_addend =
13596 _bfd_merged_section_offset (abfd, psec,
13597 elf_section_data (sec)->sec_info,
13598 sym->st_value + rel->r_addend);
13599 if (sec != *psec)
13601 /* If we have changed the section, and our original section is
13602 marked with SEC_EXCLUDE, it means that the original
13603 SEC_MERGE section has been completely subsumed in some
13604 other SEC_MERGE section. In this case, we need to leave
13605 some info around for --emit-relocs. */
13606 if ((sec->flags & SEC_EXCLUDE) != 0)
13607 sec->kept_section = *psec;
13608 sec = *psec;
13610 rel->r_addend -= relocation;
13611 rel->r_addend += sec->output_section->vma + sec->output_offset;
13613 return relocation;
13616 bfd_vma
13617 _bfd_elf_rel_local_sym (bfd *abfd,
13618 Elf_Internal_Sym *sym,
13619 asection **psec,
13620 bfd_vma addend)
13622 asection *sec = *psec;
13624 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
13625 return sym->st_value + addend;
13627 return _bfd_merged_section_offset (abfd, psec,
13628 elf_section_data (sec)->sec_info,
13629 sym->st_value + addend);
13632 /* Adjust an address within a section. Given OFFSET within SEC, return
13633 the new offset within the section, based upon changes made to the
13634 section. Returns -1 if the offset is now invalid.
13635 The offset (in abnd out) is in target sized bytes, however big a
13636 byte may be. */
13638 bfd_vma
13639 _bfd_elf_section_offset (bfd *abfd,
13640 struct bfd_link_info *info,
13641 asection *sec,
13642 bfd_vma offset)
13644 switch (sec->sec_info_type)
13646 case SEC_INFO_TYPE_STABS:
13647 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
13648 offset);
13649 case SEC_INFO_TYPE_EH_FRAME:
13650 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
13652 default:
13653 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
13655 /* Reverse the offset. */
13656 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13657 bfd_size_type address_size = bed->s->arch_size / 8;
13659 /* address_size and sec->size are in octets. Convert
13660 to bytes before subtracting the original offset. */
13661 offset = ((sec->size - address_size)
13662 / bfd_octets_per_byte (abfd, sec) - offset);
13664 return offset;
13668 long
13669 _bfd_elf_get_synthetic_symtab (bfd *abfd,
13670 long symcount ATTRIBUTE_UNUSED,
13671 asymbol **syms ATTRIBUTE_UNUSED,
13672 long dynsymcount,
13673 asymbol **dynsyms,
13674 asymbol **ret)
13676 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13677 asection *relplt;
13678 asymbol *s;
13679 const char *relplt_name;
13680 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
13681 arelent *p;
13682 long count, i, n;
13683 size_t size;
13684 Elf_Internal_Shdr *hdr;
13685 char *names;
13686 asection *plt;
13688 *ret = NULL;
13690 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
13691 return 0;
13693 if (dynsymcount <= 0)
13694 return 0;
13696 if (!bed->plt_sym_val)
13697 return 0;
13699 relplt_name = bed->relplt_name;
13700 if (relplt_name == NULL)
13701 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
13702 relplt = bfd_get_section_by_name (abfd, relplt_name);
13703 if (relplt == NULL)
13704 return 0;
13706 hdr = &elf_section_data (relplt)->this_hdr;
13707 if (hdr->sh_link != elf_dynsymtab (abfd)
13708 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
13709 return 0;
13711 plt = bfd_get_section_by_name (abfd, ".plt");
13712 if (plt == NULL)
13713 return 0;
13715 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
13716 if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
13717 return -1;
13719 count = NUM_SHDR_ENTRIES (hdr);
13720 size = count * sizeof (asymbol);
13721 p = relplt->relocation;
13722 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13724 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
13725 if (p->addend != 0)
13727 #ifdef BFD64
13728 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
13729 #else
13730 size += sizeof ("+0x") - 1 + 8;
13731 #endif
13735 s = *ret = (asymbol *) bfd_malloc (size);
13736 if (s == NULL)
13737 return -1;
13739 names = (char *) (s + count);
13740 p = relplt->relocation;
13741 n = 0;
13742 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13744 size_t len;
13745 bfd_vma addr;
13747 addr = bed->plt_sym_val (i, plt, p);
13748 if (addr == (bfd_vma) -1)
13749 continue;
13751 *s = **p->sym_ptr_ptr;
13752 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
13753 we are defining a symbol, ensure one of them is set. */
13754 if ((s->flags & BSF_LOCAL) == 0)
13755 s->flags |= BSF_GLOBAL;
13756 s->flags |= BSF_SYNTHETIC;
13757 s->section = plt;
13758 s->value = addr - plt->vma;
13759 s->name = names;
13760 s->udata.p = NULL;
13761 len = strlen ((*p->sym_ptr_ptr)->name);
13762 memcpy (names, (*p->sym_ptr_ptr)->name, len);
13763 names += len;
13764 if (p->addend != 0)
13766 char buf[30], *a;
13768 memcpy (names, "+0x", sizeof ("+0x") - 1);
13769 names += sizeof ("+0x") - 1;
13770 bfd_sprintf_vma (abfd, buf, p->addend);
13771 for (a = buf; *a == '0'; ++a)
13773 len = strlen (a);
13774 memcpy (names, a, len);
13775 names += len;
13777 memcpy (names, "@plt", sizeof ("@plt"));
13778 names += sizeof ("@plt");
13779 ++s, ++n;
13782 return n;
13785 /* It is only used by x86-64 so far.
13786 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
13787 but current usage would allow all of _bfd_std_section to be zero. */
13788 static const asymbol lcomm_sym
13789 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
13790 asection _bfd_elf_large_com_section
13791 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
13792 "LARGE_COMMON", 0, SEC_IS_COMMON);
13794 bool
13795 _bfd_elf_final_write_processing (bfd *abfd)
13797 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
13799 i_ehdrp = elf_elfheader (abfd);
13801 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13802 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
13804 /* Set the osabi field to ELFOSABI_GNU if the binary contains
13805 SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13806 or STB_GNU_UNIQUE binding. */
13807 if (elf_tdata (abfd)->has_gnu_osabi != 0)
13809 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13810 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
13811 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
13812 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
13814 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
13815 _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13816 "and FreeBSD targets"));
13817 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
13818 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13819 "only by GNU and FreeBSD targets"));
13820 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
13821 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13822 "only by GNU and FreeBSD targets"));
13823 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
13824 _bfd_error_handler (_("GNU_RETAIN section is supported "
13825 "only by GNU and FreeBSD targets"));
13826 bfd_set_error (bfd_error_sorry);
13827 return false;
13830 return true;
13834 /* Return TRUE for ELF symbol types that represent functions.
13835 This is the default version of this function, which is sufficient for
13836 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
13838 bool
13839 _bfd_elf_is_function_type (unsigned int type)
13841 return (type == STT_FUNC
13842 || type == STT_GNU_IFUNC);
13845 /* If the ELF symbol SYM might be a function in SEC, return the
13846 function size and set *CODE_OFF to the function's entry point,
13847 otherwise return zero. */
13849 bfd_size_type
13850 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13851 bfd_vma *code_off)
13853 bfd_size_type size;
13854 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
13856 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
13857 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
13858 || sym->section != sec)
13859 return 0;
13861 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
13863 /* In theory we should check that the symbol's type satisfies
13864 _bfd_elf_is_function_type(), but there are some function-like
13865 symbols which would fail this test. (eg _start). Instead
13866 we check for hidden, local, notype symbols with zero size.
13867 This type of symbol is generated by the annobin plugin for gcc
13868 and clang, and should not be considered to be a function symbol. */
13869 if (size == 0
13870 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
13871 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
13872 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
13873 return 0;
13875 *code_off = sym->value;
13876 /* Do not return 0 for the function's size. */
13877 return size ? size : 1;
13880 /* Set to non-zero to enable some debug messages. */
13881 #define DEBUG_SECONDARY_RELOCS 0
13883 /* An internal-to-the-bfd-library only section type
13884 used to indicate a cached secondary reloc section. */
13885 #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
13887 /* Create a BFD section to hold a secondary reloc section. */
13889 bool
13890 _bfd_elf_init_secondary_reloc_section (bfd * abfd,
13891 Elf_Internal_Shdr *hdr,
13892 const char * name,
13893 unsigned int shindex)
13895 /* We only support RELA secondary relocs. */
13896 if (hdr->sh_type != SHT_RELA)
13897 return false;
13899 #if DEBUG_SECONDARY_RELOCS
13900 fprintf (stderr, "secondary reloc section %s encountered\n", name);
13901 #endif
13902 hdr->sh_type = SHT_SECONDARY_RELOC;
13903 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
13906 /* Read in any secondary relocs associated with SEC. */
13908 bool
13909 _bfd_elf_slurp_secondary_reloc_section (bfd * abfd,
13910 asection * sec,
13911 asymbol ** symbols,
13912 bool dynamic)
13914 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13915 asection * relsec;
13916 bool result = true;
13917 bfd_vma (*r_sym) (bfd_vma);
13918 ufile_ptr filesize;
13920 #if BFD_DEFAULT_TARGET_SIZE > 32
13921 if (bfd_arch_bits_per_address (abfd) != 32)
13922 r_sym = elf64_r_sym;
13923 else
13924 #endif
13925 r_sym = elf32_r_sym;
13927 if (!elf_section_data (sec)->has_secondary_relocs)
13928 return true;
13930 /* Discover if there are any secondary reloc sections
13931 associated with SEC. */
13932 filesize = bfd_get_file_size (abfd);
13933 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13935 Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
13937 if (hdr->sh_type == SHT_SECONDARY_RELOC
13938 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
13939 && (hdr->sh_entsize == ebd->s->sizeof_rel
13940 || hdr->sh_entsize == ebd->s->sizeof_rela))
13942 bfd_byte * native_relocs;
13943 bfd_byte * native_reloc;
13944 arelent * internal_relocs;
13945 arelent * internal_reloc;
13946 size_t i;
13947 unsigned int entsize;
13948 unsigned int symcount;
13949 bfd_size_type reloc_count;
13950 size_t amt;
13952 if (ebd->elf_info_to_howto == NULL)
13953 return false;
13955 #if DEBUG_SECONDARY_RELOCS
13956 fprintf (stderr, "read secondary relocs for %s from %s\n",
13957 sec->name, relsec->name);
13958 #endif
13959 entsize = hdr->sh_entsize;
13961 if (filesize != 0
13962 && ((ufile_ptr) hdr->sh_offset > filesize
13963 || hdr->sh_size > filesize - hdr->sh_offset))
13965 bfd_set_error (bfd_error_file_truncated);
13966 result = false;
13967 continue;
13970 native_relocs = bfd_malloc (hdr->sh_size);
13971 if (native_relocs == NULL)
13973 result = false;
13974 continue;
13977 reloc_count = NUM_SHDR_ENTRIES (hdr);
13978 if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
13980 free (native_relocs);
13981 bfd_set_error (bfd_error_file_too_big);
13982 result = false;
13983 continue;
13986 internal_relocs = (arelent *) bfd_alloc (abfd, amt);
13987 if (internal_relocs == NULL)
13989 free (native_relocs);
13990 result = false;
13991 continue;
13994 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
13995 || bfd_read (native_relocs, hdr->sh_size, abfd) != hdr->sh_size)
13997 free (native_relocs);
13998 /* The internal_relocs will be freed when
13999 the memory for the bfd is released. */
14000 result = false;
14001 continue;
14004 if (dynamic)
14005 symcount = bfd_get_dynamic_symcount (abfd);
14006 else
14007 symcount = bfd_get_symcount (abfd);
14009 for (i = 0, internal_reloc = internal_relocs,
14010 native_reloc = native_relocs;
14011 i < reloc_count;
14012 i++, internal_reloc++, native_reloc += entsize)
14014 bool res;
14015 Elf_Internal_Rela rela;
14017 if (entsize == ebd->s->sizeof_rel)
14018 ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
14019 else /* entsize == ebd->s->sizeof_rela */
14020 ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
14022 /* The address of an ELF reloc is section relative for an object
14023 file, and absolute for an executable file or shared library.
14024 The address of a normal BFD reloc is always section relative,
14025 and the address of a dynamic reloc is absolute.. */
14026 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
14027 internal_reloc->address = rela.r_offset;
14028 else
14029 internal_reloc->address = rela.r_offset - sec->vma;
14031 if (r_sym (rela.r_info) == STN_UNDEF)
14033 /* FIXME: This and the error case below mean that we
14034 have a symbol on relocs that is not elf_symbol_type. */
14035 internal_reloc->sym_ptr_ptr =
14036 bfd_abs_section_ptr->symbol_ptr_ptr;
14038 else if (r_sym (rela.r_info) > symcount)
14040 _bfd_error_handler
14041 /* xgettext:c-format */
14042 (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
14043 abfd, sec, i, (long) r_sym (rela.r_info));
14044 bfd_set_error (bfd_error_bad_value);
14045 internal_reloc->sym_ptr_ptr =
14046 bfd_abs_section_ptr->symbol_ptr_ptr;
14047 result = false;
14049 else
14051 asymbol **ps;
14053 ps = symbols + r_sym (rela.r_info) - 1;
14054 internal_reloc->sym_ptr_ptr = ps;
14055 /* Make sure that this symbol is not removed by strip. */
14056 (*ps)->flags |= BSF_KEEP;
14059 internal_reloc->addend = rela.r_addend;
14061 res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
14062 if (! res || internal_reloc->howto == NULL)
14064 #if DEBUG_SECONDARY_RELOCS
14065 fprintf (stderr,
14066 "there is no howto associated with reloc %lx\n",
14067 rela.r_info);
14068 #endif
14069 result = false;
14073 free (native_relocs);
14074 /* Store the internal relocs. */
14075 elf_section_data (relsec)->sec_info = internal_relocs;
14079 return result;
14082 /* Set the ELF section header fields of an output secondary reloc section. */
14084 bool
14085 _bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
14086 bfd *obfd ATTRIBUTE_UNUSED,
14087 const Elf_Internal_Shdr *isection,
14088 Elf_Internal_Shdr *osection)
14090 asection * isec;
14091 asection * osec;
14092 struct bfd_elf_section_data * esd;
14094 if (isection == NULL)
14095 return false;
14097 if (isection->sh_type != SHT_SECONDARY_RELOC)
14098 return true;
14100 isec = isection->bfd_section;
14101 if (isec == NULL)
14102 return false;
14104 osec = osection->bfd_section;
14105 if (osec == NULL)
14106 return false;
14108 esd = elf_section_data (osec);
14109 BFD_ASSERT (esd->sec_info == NULL);
14110 esd->sec_info = elf_section_data (isec)->sec_info;
14111 osection->sh_type = SHT_RELA;
14112 osection->sh_link = elf_onesymtab (obfd);
14113 if (osection->sh_link == 0)
14115 /* There is no symbol table - we are hosed... */
14116 _bfd_error_handler
14117 /* xgettext:c-format */
14118 (_("%pB(%pA): link section cannot be set"
14119 " because the output file does not have a symbol table"),
14120 obfd, osec);
14121 bfd_set_error (bfd_error_bad_value);
14122 return false;
14125 /* Find the output section that corresponds to the isection's
14126 sh_info link. */
14127 if (isection->sh_info == 0
14128 || isection->sh_info >= elf_numsections (ibfd))
14130 _bfd_error_handler
14131 /* xgettext:c-format */
14132 (_("%pB(%pA): info section index is invalid"),
14133 obfd, osec);
14134 bfd_set_error (bfd_error_bad_value);
14135 return false;
14138 isection = elf_elfsections (ibfd)[isection->sh_info];
14140 if (isection == NULL
14141 || isection->bfd_section == NULL
14142 || isection->bfd_section->output_section == NULL)
14144 _bfd_error_handler
14145 /* xgettext:c-format */
14146 (_("%pB(%pA): info section index cannot be set"
14147 " because the section is not in the output"),
14148 obfd, osec);
14149 bfd_set_error (bfd_error_bad_value);
14150 return false;
14153 esd = elf_section_data (isection->bfd_section->output_section);
14154 BFD_ASSERT (esd != NULL);
14155 osection->sh_info = esd->this_idx;
14156 esd->has_secondary_relocs = true;
14157 #if DEBUG_SECONDARY_RELOCS
14158 fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
14159 osec->name, osection->sh_link, osection->sh_info);
14160 fprintf (stderr, "mark section %s as having secondary relocs\n",
14161 bfd_section_name (isection->bfd_section->output_section));
14162 #endif
14164 return true;
14167 /* Write out a secondary reloc section.
14169 FIXME: Currently this function can result in a serious performance penalty
14170 for files with secondary relocs and lots of sections. The proper way to
14171 fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
14172 relocs together and then to have this function just walk that chain. */
14174 bool
14175 _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
14177 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
14178 bfd_vma addr_offset;
14179 asection * relsec;
14180 bfd_vma (*r_info) (bfd_vma, bfd_vma);
14181 bool result = true;
14183 if (sec == NULL)
14184 return false;
14186 #if BFD_DEFAULT_TARGET_SIZE > 32
14187 if (bfd_arch_bits_per_address (abfd) != 32)
14188 r_info = elf64_r_info;
14189 else
14190 #endif
14191 r_info = elf32_r_info;
14193 /* The address of an ELF reloc is section relative for an object
14194 file, and absolute for an executable file or shared library.
14195 The address of a BFD reloc is always section relative. */
14196 addr_offset = 0;
14197 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
14198 addr_offset = sec->vma;
14200 /* Discover if there are any secondary reloc sections
14201 associated with SEC. */
14202 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
14204 const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
14205 Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
14207 if (hdr->sh_type == SHT_RELA
14208 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
14210 asymbol * last_sym;
14211 int last_sym_idx;
14212 size_t reloc_count;
14213 size_t idx;
14214 bfd_size_type entsize;
14215 arelent * src_irel;
14216 bfd_byte * dst_rela;
14218 if (hdr->contents != NULL)
14220 _bfd_error_handler
14221 /* xgettext:c-format */
14222 (_("%pB(%pA): error: secondary reloc section processed twice"),
14223 abfd, relsec);
14224 bfd_set_error (bfd_error_bad_value);
14225 result = false;
14226 continue;
14229 entsize = hdr->sh_entsize;
14230 if (entsize == 0)
14232 _bfd_error_handler
14233 /* xgettext:c-format */
14234 (_("%pB(%pA): error: secondary reloc section"
14235 " has zero sized entries"),
14236 abfd, relsec);
14237 bfd_set_error (bfd_error_bad_value);
14238 result = false;
14239 continue;
14241 else if (entsize != ebd->s->sizeof_rel
14242 && entsize != ebd->s->sizeof_rela)
14244 _bfd_error_handler
14245 /* xgettext:c-format */
14246 (_("%pB(%pA): error: secondary reloc section"
14247 " has non-standard sized entries"),
14248 abfd, relsec);
14249 bfd_set_error (bfd_error_bad_value);
14250 result = false;
14251 continue;
14254 reloc_count = hdr->sh_size / entsize;
14255 hdr->sh_size = entsize * reloc_count;
14256 if (reloc_count == 0)
14258 _bfd_error_handler
14259 /* xgettext:c-format */
14260 (_("%pB(%pA): error: secondary reloc section is empty!"),
14261 abfd, relsec);
14262 bfd_set_error (bfd_error_bad_value);
14263 result = false;
14264 continue;
14267 hdr->contents = bfd_alloc (abfd, hdr->sh_size);
14268 if (hdr->contents == NULL)
14269 continue;
14271 #if DEBUG_SECONDARY_RELOCS
14272 fprintf (stderr, "write %u secondary relocs for %s from %s\n",
14273 reloc_count, sec->name, relsec->name);
14274 #endif
14275 last_sym = NULL;
14276 last_sym_idx = 0;
14277 dst_rela = hdr->contents;
14278 src_irel = (arelent *) esd->sec_info;
14279 if (src_irel == NULL)
14281 _bfd_error_handler
14282 /* xgettext:c-format */
14283 (_("%pB(%pA): error: internal relocs missing"
14284 " for secondary reloc section"),
14285 abfd, relsec);
14286 bfd_set_error (bfd_error_bad_value);
14287 result = false;
14288 continue;
14291 for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
14293 Elf_Internal_Rela src_rela;
14294 arelent *ptr;
14295 asymbol *sym;
14296 int n;
14298 ptr = src_irel + idx;
14299 if (ptr == NULL)
14301 _bfd_error_handler
14302 /* xgettext:c-format */
14303 (_("%pB(%pA): error: reloc table entry %zu is empty"),
14304 abfd, relsec, idx);
14305 bfd_set_error (bfd_error_bad_value);
14306 result = false;
14307 break;
14310 if (ptr->sym_ptr_ptr == NULL)
14312 /* FIXME: Is this an error ? */
14313 n = 0;
14315 else
14317 sym = *ptr->sym_ptr_ptr;
14319 if (sym == last_sym)
14320 n = last_sym_idx;
14321 else
14323 n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
14324 if (n < 0)
14326 _bfd_error_handler
14327 /* xgettext:c-format */
14328 (_("%pB(%pA): error: secondary reloc %zu"
14329 " references a missing symbol"),
14330 abfd, relsec, idx);
14331 bfd_set_error (bfd_error_bad_value);
14332 result = false;
14333 n = 0;
14336 last_sym = sym;
14337 last_sym_idx = n;
14340 if (sym->the_bfd != NULL
14341 && sym->the_bfd->xvec != abfd->xvec
14342 && ! _bfd_elf_validate_reloc (abfd, ptr))
14344 _bfd_error_handler
14345 /* xgettext:c-format */
14346 (_("%pB(%pA): error: secondary reloc %zu"
14347 " references a deleted symbol"),
14348 abfd, relsec, idx);
14349 bfd_set_error (bfd_error_bad_value);
14350 result = false;
14351 n = 0;
14355 src_rela.r_offset = ptr->address + addr_offset;
14356 if (ptr->howto == NULL)
14358 _bfd_error_handler
14359 /* xgettext:c-format */
14360 (_("%pB(%pA): error: secondary reloc %zu"
14361 " is of an unknown type"),
14362 abfd, relsec, idx);
14363 bfd_set_error (bfd_error_bad_value);
14364 result = false;
14365 src_rela.r_info = r_info (0, 0);
14367 else
14368 src_rela.r_info = r_info (n, ptr->howto->type);
14369 src_rela.r_addend = ptr->addend;
14371 if (entsize == ebd->s->sizeof_rel)
14372 ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
14373 else /* entsize == ebd->s->sizeof_rela */
14374 ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
14379 return result;
14382 /* Mmap in section contents. If FINAL_LINK is false, set *BUF to NULL
14383 before calling bfd_get_full_section_contents. */
14385 static bool
14386 elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf,
14387 bool final_link)
14389 #ifdef USE_MMAP
14390 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14391 if (bed->use_mmap
14392 && sec->compress_status == COMPRESS_SECTION_NONE
14393 && (sec->flags & SEC_LINKER_CREATED) == 0)
14395 /* Use mmap only if section size >= the minimum mmap section
14396 size. */
14397 size_t readsz = bfd_get_section_limit_octets (abfd, sec);
14398 size_t allocsz = bfd_get_section_alloc_size (abfd, sec);
14399 if (readsz == allocsz && readsz >= _bfd_minimum_mmap_size)
14401 if (sec->contents != NULL)
14403 if (!sec->mmapped_p)
14404 abort ();
14405 *buf = sec->contents;
14406 return true;
14408 if (sec->mmapped_p)
14409 abort ();
14410 sec->mmapped_p = 1;
14412 /* Never use the preallocated buffer if mmapp is used. */
14413 *buf = NULL;
14416 #endif
14417 /* NB: When this is called from elf_link_input_bfd, FINAL_LINK is
14418 true. If FINAL_LINK is false, *BUF is set to the preallocated
14419 buffer if USE_MMAP is undefined and *BUF is set to NULL if
14420 USE_MMAP is defined. */
14421 if (!final_link)
14422 *buf = NULL;
14423 bool ret = bfd_get_full_section_contents (abfd, sec, buf);
14424 if (ret && sec->mmapped_p)
14425 *buf = sec->contents;
14426 return ret;
14429 /* Mmap in section contents. */
14431 bool
14432 _bfd_elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
14434 return elf_mmap_section_contents (abfd, sec, buf, false);
14437 /* Mmap in the full section contents for the final link. */
14439 bool
14440 _bfd_elf_link_mmap_section_contents (bfd *abfd, sec_ptr sec,
14441 bfd_byte **buf)
14443 return elf_mmap_section_contents (abfd, sec, buf, true);
14446 /* Munmap section contents. */
14448 void
14449 _bfd_elf_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED,
14450 void *contents)
14452 /* NB: Since _bfd_elf_munmap_section_contents is called like free,
14453 CONTENTS may be NULL. */
14454 if (contents == NULL)
14455 return;
14457 #ifdef USE_MMAP
14458 if (sec->mmapped_p)
14460 /* _bfd_elf_mmap_section_contents may return the previously
14461 mapped section contents. Munmap the section contents only
14462 if they haven't been cached. */
14463 if (elf_section_data (sec)->this_hdr.contents == contents)
14464 return;
14466 /* When _bfd_elf_mmap_section_contents returns CONTENTS as
14467 malloced, CONTENTS_ADDR is set to NULL. */
14468 if (elf_section_data (sec)->contents_addr != NULL)
14470 /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid. */
14471 if (munmap (elf_section_data (sec)->contents_addr,
14472 elf_section_data (sec)->contents_size) != 0)
14473 abort ();
14474 sec->mmapped_p = 0;
14475 sec->contents = NULL;
14476 elf_section_data (sec)->contents_addr = NULL;
14477 elf_section_data (sec)->contents_size = 0;
14478 return;
14481 #endif
14483 free (contents);
14486 /* Munmap the full section contents for the final link. */
14488 void
14489 _bfd_elf_link_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED)
14491 #ifdef USE_MMAP
14492 if (sec->mmapped_p && elf_section_data (sec)->contents_addr != NULL)
14494 /* When _bfd_elf_link_mmap_section_contents returns CONTENTS as
14495 malloced, CONTENTS_ADDR is set to NULL. */
14496 /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid. */
14497 if (munmap (elf_section_data (sec)->contents_addr,
14498 elf_section_data (sec)->contents_size) != 0)
14499 abort ();
14500 sec->mmapped_p = 0;
14501 sec->contents = NULL;
14502 elf_section_data (sec)->contents_addr = NULL;
14503 elf_section_data (sec)->contents_size = 0;
14505 #endif