bfd/
[binutils/dougsmingw.git] / bfd / elf.c
bloba5a24c52997684b1ec8a000cd89f56e5856b7dc0
1 /* ELF executable support for BFD.
3 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 Free Software Foundation, Inc.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
26 SECTION
27 ELF backends
29 BFD support for ELF formats is being worked on.
30 Currently, the best supported back ends are for sparc and i386
31 (running svr4 or Solaris 2).
33 Documentation of the internals of the support code still needs
34 to be written. The code is changing quickly enough that we
35 haven't bothered yet. */
37 /* For sparc64-cross-sparc32. */
38 #define _SYSCALL32
39 #include "sysdep.h"
40 #include "bfd.h"
41 #include "bfdlink.h"
42 #include "libbfd.h"
43 #define ARCH_SIZE 0
44 #include "elf-bfd.h"
45 #include "libiberty.h"
46 #include "safe-ctype.h"
48 #ifdef CORE_HEADER
49 #include CORE_HEADER
50 #endif
52 static int elf_sort_sections (const void *, const void *);
53 static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
54 static bfd_boolean prep_headers (bfd *);
55 static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ;
56 static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ;
57 static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
58 file_ptr offset);
60 /* Swap version information in and out. The version information is
61 currently size independent. If that ever changes, this code will
62 need to move into elfcode.h. */
64 /* Swap in a Verdef structure. */
66 void
67 _bfd_elf_swap_verdef_in (bfd *abfd,
68 const Elf_External_Verdef *src,
69 Elf_Internal_Verdef *dst)
71 dst->vd_version = H_GET_16 (abfd, src->vd_version);
72 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
73 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
74 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
75 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
76 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
77 dst->vd_next = H_GET_32 (abfd, src->vd_next);
80 /* Swap out a Verdef structure. */
82 void
83 _bfd_elf_swap_verdef_out (bfd *abfd,
84 const Elf_Internal_Verdef *src,
85 Elf_External_Verdef *dst)
87 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
88 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
89 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
90 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
91 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
92 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
93 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
96 /* Swap in a Verdaux structure. */
98 void
99 _bfd_elf_swap_verdaux_in (bfd *abfd,
100 const Elf_External_Verdaux *src,
101 Elf_Internal_Verdaux *dst)
103 dst->vda_name = H_GET_32 (abfd, src->vda_name);
104 dst->vda_next = H_GET_32 (abfd, src->vda_next);
107 /* Swap out a Verdaux structure. */
109 void
110 _bfd_elf_swap_verdaux_out (bfd *abfd,
111 const Elf_Internal_Verdaux *src,
112 Elf_External_Verdaux *dst)
114 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
115 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
118 /* Swap in a Verneed structure. */
120 void
121 _bfd_elf_swap_verneed_in (bfd *abfd,
122 const Elf_External_Verneed *src,
123 Elf_Internal_Verneed *dst)
125 dst->vn_version = H_GET_16 (abfd, src->vn_version);
126 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
127 dst->vn_file = H_GET_32 (abfd, src->vn_file);
128 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
129 dst->vn_next = H_GET_32 (abfd, src->vn_next);
132 /* Swap out a Verneed structure. */
134 void
135 _bfd_elf_swap_verneed_out (bfd *abfd,
136 const Elf_Internal_Verneed *src,
137 Elf_External_Verneed *dst)
139 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
140 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
141 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
142 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
143 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
146 /* Swap in a Vernaux structure. */
148 void
149 _bfd_elf_swap_vernaux_in (bfd *abfd,
150 const Elf_External_Vernaux *src,
151 Elf_Internal_Vernaux *dst)
153 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
154 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
155 dst->vna_other = H_GET_16 (abfd, src->vna_other);
156 dst->vna_name = H_GET_32 (abfd, src->vna_name);
157 dst->vna_next = H_GET_32 (abfd, src->vna_next);
160 /* Swap out a Vernaux structure. */
162 void
163 _bfd_elf_swap_vernaux_out (bfd *abfd,
164 const Elf_Internal_Vernaux *src,
165 Elf_External_Vernaux *dst)
167 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
168 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
169 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
170 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
171 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
174 /* Swap in a Versym structure. */
176 void
177 _bfd_elf_swap_versym_in (bfd *abfd,
178 const Elf_External_Versym *src,
179 Elf_Internal_Versym *dst)
181 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
184 /* Swap out a Versym structure. */
186 void
187 _bfd_elf_swap_versym_out (bfd *abfd,
188 const Elf_Internal_Versym *src,
189 Elf_External_Versym *dst)
191 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
194 /* Standard ELF hash function. Do not change this function; you will
195 cause invalid hash tables to be generated. */
197 unsigned long
198 bfd_elf_hash (const char *namearg)
200 const unsigned char *name = (const unsigned char *) namearg;
201 unsigned long h = 0;
202 unsigned long g;
203 int ch;
205 while ((ch = *name++) != '\0')
207 h = (h << 4) + ch;
208 if ((g = (h & 0xf0000000)) != 0)
210 h ^= g >> 24;
211 /* The ELF ABI says `h &= ~g', but this is equivalent in
212 this case and on some machines one insn instead of two. */
213 h ^= g;
216 return h & 0xffffffff;
219 /* DT_GNU_HASH hash function. Do not change this function; you will
220 cause invalid hash tables to be generated. */
222 unsigned long
223 bfd_elf_gnu_hash (const char *namearg)
225 const unsigned char *name = (const unsigned char *) namearg;
226 unsigned long h = 5381;
227 unsigned char ch;
229 while ((ch = *name++) != '\0')
230 h = (h << 5) + h + ch;
231 return h & 0xffffffff;
234 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
235 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
236 bfd_boolean
237 bfd_elf_allocate_object (bfd *abfd,
238 size_t object_size,
239 enum elf_target_id object_id)
241 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
242 abfd->tdata.any = bfd_zalloc (abfd, object_size);
243 if (abfd->tdata.any == NULL)
244 return FALSE;
246 elf_object_id (abfd) = object_id;
247 elf_program_header_size (abfd) = (bfd_size_type) -1;
248 return TRUE;
252 bfd_boolean
253 bfd_elf_make_generic_object (bfd *abfd)
255 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
256 GENERIC_ELF_DATA);
259 bfd_boolean
260 bfd_elf_mkcorefile (bfd *abfd)
262 /* I think this can be done just like an object file. */
263 return bfd_elf_make_generic_object (abfd);
266 static char *
267 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
269 Elf_Internal_Shdr **i_shdrp;
270 bfd_byte *shstrtab = NULL;
271 file_ptr offset;
272 bfd_size_type shstrtabsize;
274 i_shdrp = elf_elfsections (abfd);
275 if (i_shdrp == 0
276 || shindex >= elf_numsections (abfd)
277 || i_shdrp[shindex] == 0)
278 return NULL;
280 shstrtab = i_shdrp[shindex]->contents;
281 if (shstrtab == NULL)
283 /* No cached one, attempt to read, and cache what we read. */
284 offset = i_shdrp[shindex]->sh_offset;
285 shstrtabsize = i_shdrp[shindex]->sh_size;
287 /* Allocate and clear an extra byte at the end, to prevent crashes
288 in case the string table is not terminated. */
289 if (shstrtabsize + 1 <= 1
290 || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL
291 || bfd_seek (abfd, offset, SEEK_SET) != 0)
292 shstrtab = NULL;
293 else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
295 if (bfd_get_error () != bfd_error_system_call)
296 bfd_set_error (bfd_error_file_truncated);
297 shstrtab = NULL;
298 /* Once we've failed to read it, make sure we don't keep
299 trying. Otherwise, we'll keep allocating space for
300 the string table over and over. */
301 i_shdrp[shindex]->sh_size = 0;
303 else
304 shstrtab[shstrtabsize] = '\0';
305 i_shdrp[shindex]->contents = shstrtab;
307 return (char *) shstrtab;
310 char *
311 bfd_elf_string_from_elf_section (bfd *abfd,
312 unsigned int shindex,
313 unsigned int strindex)
315 Elf_Internal_Shdr *hdr;
317 if (strindex == 0)
318 return "";
320 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
321 return NULL;
323 hdr = elf_elfsections (abfd)[shindex];
325 if (hdr->contents == NULL
326 && bfd_elf_get_str_section (abfd, shindex) == NULL)
327 return NULL;
329 if (strindex >= hdr->sh_size)
331 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
332 (*_bfd_error_handler)
333 (_("%B: invalid string offset %u >= %lu for section `%s'"),
334 abfd, strindex, (unsigned long) hdr->sh_size,
335 (shindex == shstrndx && strindex == hdr->sh_name
336 ? ".shstrtab"
337 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
338 return NULL;
341 return ((char *) hdr->contents) + strindex;
344 /* Read and convert symbols to internal format.
345 SYMCOUNT specifies the number of symbols to read, starting from
346 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
347 are non-NULL, they are used to store the internal symbols, external
348 symbols, and symbol section index extensions, respectively.
349 Returns a pointer to the internal symbol buffer (malloced if necessary)
350 or NULL if there were no symbols or some kind of problem. */
352 Elf_Internal_Sym *
353 bfd_elf_get_elf_syms (bfd *ibfd,
354 Elf_Internal_Shdr *symtab_hdr,
355 size_t symcount,
356 size_t symoffset,
357 Elf_Internal_Sym *intsym_buf,
358 void *extsym_buf,
359 Elf_External_Sym_Shndx *extshndx_buf)
361 Elf_Internal_Shdr *shndx_hdr;
362 void *alloc_ext;
363 const bfd_byte *esym;
364 Elf_External_Sym_Shndx *alloc_extshndx;
365 Elf_External_Sym_Shndx *shndx;
366 Elf_Internal_Sym *alloc_intsym;
367 Elf_Internal_Sym *isym;
368 Elf_Internal_Sym *isymend;
369 const struct elf_backend_data *bed;
370 size_t extsym_size;
371 bfd_size_type amt;
372 file_ptr pos;
374 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
375 abort ();
377 if (symcount == 0)
378 return intsym_buf;
380 /* Normal syms might have section extension entries. */
381 shndx_hdr = NULL;
382 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
383 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
385 /* Read the symbols. */
386 alloc_ext = NULL;
387 alloc_extshndx = NULL;
388 alloc_intsym = NULL;
389 bed = get_elf_backend_data (ibfd);
390 extsym_size = bed->s->sizeof_sym;
391 amt = symcount * extsym_size;
392 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
393 if (extsym_buf == NULL)
395 alloc_ext = bfd_malloc2 (symcount, extsym_size);
396 extsym_buf = alloc_ext;
398 if (extsym_buf == NULL
399 || bfd_seek (ibfd, pos, SEEK_SET) != 0
400 || bfd_bread (extsym_buf, amt, ibfd) != amt)
402 intsym_buf = NULL;
403 goto out;
406 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
407 extshndx_buf = NULL;
408 else
410 amt = symcount * sizeof (Elf_External_Sym_Shndx);
411 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
412 if (extshndx_buf == NULL)
414 alloc_extshndx = (Elf_External_Sym_Shndx *)
415 bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx));
416 extshndx_buf = alloc_extshndx;
418 if (extshndx_buf == NULL
419 || bfd_seek (ibfd, pos, SEEK_SET) != 0
420 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
422 intsym_buf = NULL;
423 goto out;
427 if (intsym_buf == NULL)
429 alloc_intsym = (Elf_Internal_Sym *)
430 bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
431 intsym_buf = alloc_intsym;
432 if (intsym_buf == NULL)
433 goto out;
436 /* Convert the symbols to internal form. */
437 isymend = intsym_buf + symcount;
438 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
439 shndx = extshndx_buf;
440 isym < isymend;
441 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
442 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
444 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
445 (*_bfd_error_handler) (_("%B symbol number %lu references "
446 "nonexistent SHT_SYMTAB_SHNDX section"),
447 ibfd, (unsigned long) symoffset);
448 if (alloc_intsym != NULL)
449 free (alloc_intsym);
450 intsym_buf = NULL;
451 goto out;
454 out:
455 if (alloc_ext != NULL)
456 free (alloc_ext);
457 if (alloc_extshndx != NULL)
458 free (alloc_extshndx);
460 return intsym_buf;
463 /* Look up a symbol name. */
464 const char *
465 bfd_elf_sym_name (bfd *abfd,
466 Elf_Internal_Shdr *symtab_hdr,
467 Elf_Internal_Sym *isym,
468 asection *sym_sec)
470 const char *name;
471 unsigned int iname = isym->st_name;
472 unsigned int shindex = symtab_hdr->sh_link;
474 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
475 /* Check for a bogus st_shndx to avoid crashing. */
476 && isym->st_shndx < elf_numsections (abfd))
478 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
479 shindex = elf_elfheader (abfd)->e_shstrndx;
482 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
483 if (name == NULL)
484 name = "(null)";
485 else if (sym_sec && *name == '\0')
486 name = bfd_section_name (abfd, sym_sec);
488 return name;
491 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
492 sections. The first element is the flags, the rest are section
493 pointers. */
495 typedef union elf_internal_group {
496 Elf_Internal_Shdr *shdr;
497 unsigned int flags;
498 } Elf_Internal_Group;
500 /* Return the name of the group signature symbol. Why isn't the
501 signature just a string? */
503 static const char *
504 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
506 Elf_Internal_Shdr *hdr;
507 unsigned char esym[sizeof (Elf64_External_Sym)];
508 Elf_External_Sym_Shndx eshndx;
509 Elf_Internal_Sym isym;
511 /* First we need to ensure the symbol table is available. Make sure
512 that it is a symbol table section. */
513 if (ghdr->sh_link >= elf_numsections (abfd))
514 return NULL;
515 hdr = elf_elfsections (abfd) [ghdr->sh_link];
516 if (hdr->sh_type != SHT_SYMTAB
517 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
518 return NULL;
520 /* Go read the symbol. */
521 hdr = &elf_tdata (abfd)->symtab_hdr;
522 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
523 &isym, esym, &eshndx) == NULL)
524 return NULL;
526 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
529 /* Set next_in_group list pointer, and group name for NEWSECT. */
531 static bfd_boolean
532 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
534 unsigned int num_group = elf_tdata (abfd)->num_group;
536 /* If num_group is zero, read in all SHT_GROUP sections. The count
537 is set to -1 if there are no SHT_GROUP sections. */
538 if (num_group == 0)
540 unsigned int i, shnum;
542 /* First count the number of groups. If we have a SHT_GROUP
543 section with just a flag word (ie. sh_size is 4), ignore it. */
544 shnum = elf_numsections (abfd);
545 num_group = 0;
547 #define IS_VALID_GROUP_SECTION_HEADER(shdr) \
548 ( (shdr)->sh_type == SHT_GROUP \
549 && (shdr)->sh_size >= (2 * GRP_ENTRY_SIZE) \
550 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
551 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
553 for (i = 0; i < shnum; i++)
555 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
557 if (IS_VALID_GROUP_SECTION_HEADER (shdr))
558 num_group += 1;
561 if (num_group == 0)
563 num_group = (unsigned) -1;
564 elf_tdata (abfd)->num_group = num_group;
566 else
568 /* We keep a list of elf section headers for group sections,
569 so we can find them quickly. */
570 bfd_size_type amt;
572 elf_tdata (abfd)->num_group = num_group;
573 elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **)
574 bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
575 if (elf_tdata (abfd)->group_sect_ptr == NULL)
576 return FALSE;
578 num_group = 0;
579 for (i = 0; i < shnum; i++)
581 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
583 if (IS_VALID_GROUP_SECTION_HEADER (shdr))
585 unsigned char *src;
586 Elf_Internal_Group *dest;
588 /* Add to list of sections. */
589 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
590 num_group += 1;
592 /* Read the raw contents. */
593 BFD_ASSERT (sizeof (*dest) >= 4);
594 amt = shdr->sh_size * sizeof (*dest) / 4;
595 shdr->contents = (unsigned char *)
596 bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4);
597 /* PR binutils/4110: Handle corrupt group headers. */
598 if (shdr->contents == NULL)
600 _bfd_error_handler
601 (_("%B: Corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size);
602 bfd_set_error (bfd_error_bad_value);
603 return FALSE;
606 memset (shdr->contents, 0, amt);
608 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
609 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
610 != shdr->sh_size))
611 return FALSE;
613 /* Translate raw contents, a flag word followed by an
614 array of elf section indices all in target byte order,
615 to the flag word followed by an array of elf section
616 pointers. */
617 src = shdr->contents + shdr->sh_size;
618 dest = (Elf_Internal_Group *) (shdr->contents + amt);
619 while (1)
621 unsigned int idx;
623 src -= 4;
624 --dest;
625 idx = H_GET_32 (abfd, src);
626 if (src == shdr->contents)
628 dest->flags = idx;
629 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
630 shdr->bfd_section->flags
631 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
632 break;
634 if (idx >= shnum)
636 ((*_bfd_error_handler)
637 (_("%B: invalid SHT_GROUP entry"), abfd));
638 idx = 0;
640 dest->shdr = elf_elfsections (abfd)[idx];
647 if (num_group != (unsigned) -1)
649 unsigned int i;
651 for (i = 0; i < num_group; i++)
653 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
654 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
655 unsigned int n_elt = shdr->sh_size / 4;
657 /* Look through this group's sections to see if current
658 section is a member. */
659 while (--n_elt != 0)
660 if ((++idx)->shdr == hdr)
662 asection *s = NULL;
664 /* We are a member of this group. Go looking through
665 other members to see if any others are linked via
666 next_in_group. */
667 idx = (Elf_Internal_Group *) shdr->contents;
668 n_elt = shdr->sh_size / 4;
669 while (--n_elt != 0)
670 if ((s = (++idx)->shdr->bfd_section) != NULL
671 && elf_next_in_group (s) != NULL)
672 break;
673 if (n_elt != 0)
675 /* Snarf the group name from other member, and
676 insert current section in circular list. */
677 elf_group_name (newsect) = elf_group_name (s);
678 elf_next_in_group (newsect) = elf_next_in_group (s);
679 elf_next_in_group (s) = newsect;
681 else
683 const char *gname;
685 gname = group_signature (abfd, shdr);
686 if (gname == NULL)
687 return FALSE;
688 elf_group_name (newsect) = gname;
690 /* Start a circular list with one element. */
691 elf_next_in_group (newsect) = newsect;
694 /* If the group section has been created, point to the
695 new member. */
696 if (shdr->bfd_section != NULL)
697 elf_next_in_group (shdr->bfd_section) = newsect;
699 i = num_group - 1;
700 break;
705 if (elf_group_name (newsect) == NULL)
707 (*_bfd_error_handler) (_("%B: no group info for section %A"),
708 abfd, newsect);
710 return TRUE;
713 bfd_boolean
714 _bfd_elf_setup_sections (bfd *abfd)
716 unsigned int i;
717 unsigned int num_group = elf_tdata (abfd)->num_group;
718 bfd_boolean result = TRUE;
719 asection *s;
721 /* Process SHF_LINK_ORDER. */
722 for (s = abfd->sections; s != NULL; s = s->next)
724 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
725 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
727 unsigned int elfsec = this_hdr->sh_link;
728 /* FIXME: The old Intel compiler and old strip/objcopy may
729 not set the sh_link or sh_info fields. Hence we could
730 get the situation where elfsec is 0. */
731 if (elfsec == 0)
733 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
734 if (bed->link_order_error_handler)
735 bed->link_order_error_handler
736 (_("%B: warning: sh_link not set for section `%A'"),
737 abfd, s);
739 else
741 asection *linksec = NULL;
743 if (elfsec < elf_numsections (abfd))
745 this_hdr = elf_elfsections (abfd)[elfsec];
746 linksec = this_hdr->bfd_section;
749 /* PR 1991, 2008:
750 Some strip/objcopy may leave an incorrect value in
751 sh_link. We don't want to proceed. */
752 if (linksec == NULL)
754 (*_bfd_error_handler)
755 (_("%B: sh_link [%d] in section `%A' is incorrect"),
756 s->owner, s, elfsec);
757 result = FALSE;
760 elf_linked_to_section (s) = linksec;
765 /* Process section groups. */
766 if (num_group == (unsigned) -1)
767 return result;
769 for (i = 0; i < num_group; i++)
771 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
772 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
773 unsigned int n_elt = shdr->sh_size / 4;
775 while (--n_elt != 0)
776 if ((++idx)->shdr->bfd_section)
777 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
778 else if (idx->shdr->sh_type == SHT_RELA
779 || idx->shdr->sh_type == SHT_REL)
780 /* We won't include relocation sections in section groups in
781 output object files. We adjust the group section size here
782 so that relocatable link will work correctly when
783 relocation sections are in section group in input object
784 files. */
785 shdr->bfd_section->size -= 4;
786 else
788 /* There are some unknown sections in the group. */
789 (*_bfd_error_handler)
790 (_("%B: unknown [%d] section `%s' in group [%s]"),
791 abfd,
792 (unsigned int) idx->shdr->sh_type,
793 bfd_elf_string_from_elf_section (abfd,
794 (elf_elfheader (abfd)
795 ->e_shstrndx),
796 idx->shdr->sh_name),
797 shdr->bfd_section->name);
798 result = FALSE;
801 return result;
804 bfd_boolean
805 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
807 return elf_next_in_group (sec) != NULL;
810 /* Make a BFD section from an ELF section. We store a pointer to the
811 BFD section in the bfd_section field of the header. */
813 bfd_boolean
814 _bfd_elf_make_section_from_shdr (bfd *abfd,
815 Elf_Internal_Shdr *hdr,
816 const char *name,
817 int shindex)
819 asection *newsect;
820 flagword flags;
821 const struct elf_backend_data *bed;
823 if (hdr->bfd_section != NULL)
825 BFD_ASSERT (strcmp (name,
826 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
827 return TRUE;
830 newsect = bfd_make_section_anyway (abfd, name);
831 if (newsect == NULL)
832 return FALSE;
834 hdr->bfd_section = newsect;
835 elf_section_data (newsect)->this_hdr = *hdr;
836 elf_section_data (newsect)->this_idx = shindex;
838 /* Always use the real type/flags. */
839 elf_section_type (newsect) = hdr->sh_type;
840 elf_section_flags (newsect) = hdr->sh_flags;
842 newsect->filepos = hdr->sh_offset;
844 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
845 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
846 || ! bfd_set_section_alignment (abfd, newsect,
847 bfd_log2 (hdr->sh_addralign)))
848 return FALSE;
850 flags = SEC_NO_FLAGS;
851 if (hdr->sh_type != SHT_NOBITS)
852 flags |= SEC_HAS_CONTENTS;
853 if (hdr->sh_type == SHT_GROUP)
854 flags |= SEC_GROUP | SEC_EXCLUDE;
855 if ((hdr->sh_flags & SHF_ALLOC) != 0)
857 flags |= SEC_ALLOC;
858 if (hdr->sh_type != SHT_NOBITS)
859 flags |= SEC_LOAD;
861 if ((hdr->sh_flags & SHF_WRITE) == 0)
862 flags |= SEC_READONLY;
863 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
864 flags |= SEC_CODE;
865 else if ((flags & SEC_LOAD) != 0)
866 flags |= SEC_DATA;
867 if ((hdr->sh_flags & SHF_MERGE) != 0)
869 flags |= SEC_MERGE;
870 newsect->entsize = hdr->sh_entsize;
871 if ((hdr->sh_flags & SHF_STRINGS) != 0)
872 flags |= SEC_STRINGS;
874 if (hdr->sh_flags & SHF_GROUP)
875 if (!setup_group (abfd, hdr, newsect))
876 return FALSE;
877 if ((hdr->sh_flags & SHF_TLS) != 0)
878 flags |= SEC_THREAD_LOCAL;
880 if ((flags & SEC_ALLOC) == 0)
882 /* The debugging sections appear to be recognized only by name,
883 not any sort of flag. Their SEC_ALLOC bits are cleared. */
884 static const struct
886 const char *name;
887 int len;
888 } debug_sections [] =
890 { STRING_COMMA_LEN ("debug") }, /* 'd' */
891 { NULL, 0 }, /* 'e' */
892 { NULL, 0 }, /* 'f' */
893 { STRING_COMMA_LEN ("gnu.linkonce.wi.") }, /* 'g' */
894 { NULL, 0 }, /* 'h' */
895 { NULL, 0 }, /* 'i' */
896 { NULL, 0 }, /* 'j' */
897 { NULL, 0 }, /* 'k' */
898 { STRING_COMMA_LEN ("line") }, /* 'l' */
899 { NULL, 0 }, /* 'm' */
900 { NULL, 0 }, /* 'n' */
901 { NULL, 0 }, /* 'o' */
902 { NULL, 0 }, /* 'p' */
903 { NULL, 0 }, /* 'q' */
904 { NULL, 0 }, /* 'r' */
905 { STRING_COMMA_LEN ("stab") }, /* 's' */
906 { NULL, 0 }, /* 't' */
907 { NULL, 0 }, /* 'u' */
908 { NULL, 0 }, /* 'v' */
909 { NULL, 0 }, /* 'w' */
910 { NULL, 0 }, /* 'x' */
911 { NULL, 0 }, /* 'y' */
912 { STRING_COMMA_LEN ("zdebug") } /* 'z' */
915 if (name [0] == '.')
917 int i = name [1] - 'd';
918 if (i >= 0
919 && i < (int) ARRAY_SIZE (debug_sections)
920 && debug_sections [i].name != NULL
921 && strncmp (&name [1], debug_sections [i].name,
922 debug_sections [i].len) == 0)
923 flags |= SEC_DEBUGGING;
927 /* As a GNU extension, if the name begins with .gnu.linkonce, we
928 only link a single copy of the section. This is used to support
929 g++. g++ will emit each template expansion in its own section.
930 The symbols will be defined as weak, so that multiple definitions
931 are permitted. The GNU linker extension is to actually discard
932 all but one of the sections. */
933 if (CONST_STRNEQ (name, ".gnu.linkonce")
934 && elf_next_in_group (newsect) == NULL)
935 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
937 bed = get_elf_backend_data (abfd);
938 if (bed->elf_backend_section_flags)
939 if (! bed->elf_backend_section_flags (&flags, hdr))
940 return FALSE;
942 if (! bfd_set_section_flags (abfd, newsect, flags))
943 return FALSE;
945 /* We do not parse the PT_NOTE segments as we are interested even in the
946 separate debug info files which may have the segments offsets corrupted.
947 PT_NOTEs from the core files are currently not parsed using BFD. */
948 if (hdr->sh_type == SHT_NOTE)
950 bfd_byte *contents;
952 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
953 return FALSE;
955 elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1);
956 free (contents);
959 if ((flags & SEC_ALLOC) != 0)
961 Elf_Internal_Phdr *phdr;
962 unsigned int i, nload;
964 /* Some ELF linkers produce binaries with all the program header
965 p_paddr fields zero. If we have such a binary with more than
966 one PT_LOAD header, then leave the section lma equal to vma
967 so that we don't create sections with overlapping lma. */
968 phdr = elf_tdata (abfd)->phdr;
969 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
970 if (phdr->p_paddr != 0)
971 break;
972 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
973 ++nload;
974 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
975 return TRUE;
977 phdr = elf_tdata (abfd)->phdr;
978 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
980 if (phdr->p_type == PT_LOAD
981 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
983 if ((flags & SEC_LOAD) == 0)
984 newsect->lma = (phdr->p_paddr
985 + hdr->sh_addr - phdr->p_vaddr);
986 else
987 /* We used to use the same adjustment for SEC_LOAD
988 sections, but that doesn't work if the segment
989 is packed with code from multiple VMAs.
990 Instead we calculate the section LMA based on
991 the segment LMA. It is assumed that the
992 segment will contain sections with contiguous
993 LMAs, even if the VMAs are not. */
994 newsect->lma = (phdr->p_paddr
995 + hdr->sh_offset - phdr->p_offset);
997 /* With contiguous segments, we can't tell from file
998 offsets whether a section with zero size should
999 be placed at the end of one segment or the
1000 beginning of the next. Decide based on vaddr. */
1001 if (hdr->sh_addr >= phdr->p_vaddr
1002 && (hdr->sh_addr + hdr->sh_size
1003 <= phdr->p_vaddr + phdr->p_memsz))
1004 break;
1009 return TRUE;
1012 const char *const bfd_elf_section_type_names[] = {
1013 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1014 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1015 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1018 /* ELF relocs are against symbols. If we are producing relocatable
1019 output, and the reloc is against an external symbol, and nothing
1020 has given us any additional addend, the resulting reloc will also
1021 be against the same symbol. In such a case, we don't want to
1022 change anything about the way the reloc is handled, since it will
1023 all be done at final link time. Rather than put special case code
1024 into bfd_perform_relocation, all the reloc types use this howto
1025 function. It just short circuits the reloc if producing
1026 relocatable output against an external symbol. */
1028 bfd_reloc_status_type
1029 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1030 arelent *reloc_entry,
1031 asymbol *symbol,
1032 void *data ATTRIBUTE_UNUSED,
1033 asection *input_section,
1034 bfd *output_bfd,
1035 char **error_message ATTRIBUTE_UNUSED)
1037 if (output_bfd != NULL
1038 && (symbol->flags & BSF_SECTION_SYM) == 0
1039 && (! reloc_entry->howto->partial_inplace
1040 || reloc_entry->addend == 0))
1042 reloc_entry->address += input_section->output_offset;
1043 return bfd_reloc_ok;
1046 return bfd_reloc_continue;
1049 /* Copy the program header and other data from one object module to
1050 another. */
1052 bfd_boolean
1053 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1055 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1056 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1057 return TRUE;
1059 BFD_ASSERT (!elf_flags_init (obfd)
1060 || (elf_elfheader (obfd)->e_flags
1061 == elf_elfheader (ibfd)->e_flags));
1063 elf_gp (obfd) = elf_gp (ibfd);
1064 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1065 elf_flags_init (obfd) = TRUE;
1067 /* Copy object attributes. */
1068 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1069 return TRUE;
1072 static const char *
1073 get_segment_type (unsigned int p_type)
1075 const char *pt;
1076 switch (p_type)
1078 case PT_NULL: pt = "NULL"; break;
1079 case PT_LOAD: pt = "LOAD"; break;
1080 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1081 case PT_INTERP: pt = "INTERP"; break;
1082 case PT_NOTE: pt = "NOTE"; break;
1083 case PT_SHLIB: pt = "SHLIB"; break;
1084 case PT_PHDR: pt = "PHDR"; break;
1085 case PT_TLS: pt = "TLS"; break;
1086 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1087 case PT_GNU_STACK: pt = "STACK"; break;
1088 case PT_GNU_RELRO: pt = "RELRO"; break;
1089 default: pt = NULL; break;
1091 return pt;
1094 /* Print out the program headers. */
1096 bfd_boolean
1097 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1099 FILE *f = (FILE *) farg;
1100 Elf_Internal_Phdr *p;
1101 asection *s;
1102 bfd_byte *dynbuf = NULL;
1104 p = elf_tdata (abfd)->phdr;
1105 if (p != NULL)
1107 unsigned int i, c;
1109 fprintf (f, _("\nProgram Header:\n"));
1110 c = elf_elfheader (abfd)->e_phnum;
1111 for (i = 0; i < c; i++, p++)
1113 const char *pt = get_segment_type (p->p_type);
1114 char buf[20];
1116 if (pt == NULL)
1118 sprintf (buf, "0x%lx", p->p_type);
1119 pt = buf;
1121 fprintf (f, "%8s off 0x", pt);
1122 bfd_fprintf_vma (abfd, f, p->p_offset);
1123 fprintf (f, " vaddr 0x");
1124 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1125 fprintf (f, " paddr 0x");
1126 bfd_fprintf_vma (abfd, f, p->p_paddr);
1127 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1128 fprintf (f, " filesz 0x");
1129 bfd_fprintf_vma (abfd, f, p->p_filesz);
1130 fprintf (f, " memsz 0x");
1131 bfd_fprintf_vma (abfd, f, p->p_memsz);
1132 fprintf (f, " flags %c%c%c",
1133 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1134 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1135 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1136 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1137 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1138 fprintf (f, "\n");
1142 s = bfd_get_section_by_name (abfd, ".dynamic");
1143 if (s != NULL)
1145 unsigned int elfsec;
1146 unsigned long shlink;
1147 bfd_byte *extdyn, *extdynend;
1148 size_t extdynsize;
1149 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1151 fprintf (f, _("\nDynamic Section:\n"));
1153 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1154 goto error_return;
1156 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1157 if (elfsec == SHN_BAD)
1158 goto error_return;
1159 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1161 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1162 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1164 extdyn = dynbuf;
1165 extdynend = extdyn + s->size;
1166 for (; extdyn < extdynend; extdyn += extdynsize)
1168 Elf_Internal_Dyn dyn;
1169 const char *name = "";
1170 char ab[20];
1171 bfd_boolean stringp;
1172 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1174 (*swap_dyn_in) (abfd, extdyn, &dyn);
1176 if (dyn.d_tag == DT_NULL)
1177 break;
1179 stringp = FALSE;
1180 switch (dyn.d_tag)
1182 default:
1183 if (bed->elf_backend_get_target_dtag)
1184 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1186 if (!strcmp (name, ""))
1188 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1189 name = ab;
1191 break;
1193 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1194 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1195 case DT_PLTGOT: name = "PLTGOT"; break;
1196 case DT_HASH: name = "HASH"; break;
1197 case DT_STRTAB: name = "STRTAB"; break;
1198 case DT_SYMTAB: name = "SYMTAB"; break;
1199 case DT_RELA: name = "RELA"; break;
1200 case DT_RELASZ: name = "RELASZ"; break;
1201 case DT_RELAENT: name = "RELAENT"; break;
1202 case DT_STRSZ: name = "STRSZ"; break;
1203 case DT_SYMENT: name = "SYMENT"; break;
1204 case DT_INIT: name = "INIT"; break;
1205 case DT_FINI: name = "FINI"; break;
1206 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1207 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1208 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1209 case DT_REL: name = "REL"; break;
1210 case DT_RELSZ: name = "RELSZ"; break;
1211 case DT_RELENT: name = "RELENT"; break;
1212 case DT_PLTREL: name = "PLTREL"; break;
1213 case DT_DEBUG: name = "DEBUG"; break;
1214 case DT_TEXTREL: name = "TEXTREL"; break;
1215 case DT_JMPREL: name = "JMPREL"; break;
1216 case DT_BIND_NOW: name = "BIND_NOW"; break;
1217 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1218 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1219 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1220 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1221 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1222 case DT_FLAGS: name = "FLAGS"; break;
1223 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1224 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1225 case DT_CHECKSUM: name = "CHECKSUM"; break;
1226 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1227 case DT_MOVEENT: name = "MOVEENT"; break;
1228 case DT_MOVESZ: name = "MOVESZ"; break;
1229 case DT_FEATURE: name = "FEATURE"; break;
1230 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1231 case DT_SYMINSZ: name = "SYMINSZ"; break;
1232 case DT_SYMINENT: name = "SYMINENT"; break;
1233 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1234 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1235 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1236 case DT_PLTPAD: name = "PLTPAD"; break;
1237 case DT_MOVETAB: name = "MOVETAB"; break;
1238 case DT_SYMINFO: name = "SYMINFO"; break;
1239 case DT_RELACOUNT: name = "RELACOUNT"; break;
1240 case DT_RELCOUNT: name = "RELCOUNT"; break;
1241 case DT_FLAGS_1: name = "FLAGS_1"; break;
1242 case DT_VERSYM: name = "VERSYM"; break;
1243 case DT_VERDEF: name = "VERDEF"; break;
1244 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1245 case DT_VERNEED: name = "VERNEED"; break;
1246 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1247 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1248 case DT_USED: name = "USED"; break;
1249 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1250 case DT_GNU_HASH: name = "GNU_HASH"; break;
1253 fprintf (f, " %-20s ", name);
1254 if (! stringp)
1256 fprintf (f, "0x");
1257 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1259 else
1261 const char *string;
1262 unsigned int tagv = dyn.d_un.d_val;
1264 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1265 if (string == NULL)
1266 goto error_return;
1267 fprintf (f, "%s", string);
1269 fprintf (f, "\n");
1272 free (dynbuf);
1273 dynbuf = NULL;
1276 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1277 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1279 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1280 return FALSE;
1283 if (elf_dynverdef (abfd) != 0)
1285 Elf_Internal_Verdef *t;
1287 fprintf (f, _("\nVersion definitions:\n"));
1288 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1290 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1291 t->vd_flags, t->vd_hash,
1292 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1293 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1295 Elf_Internal_Verdaux *a;
1297 fprintf (f, "\t");
1298 for (a = t->vd_auxptr->vda_nextptr;
1299 a != NULL;
1300 a = a->vda_nextptr)
1301 fprintf (f, "%s ",
1302 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1303 fprintf (f, "\n");
1308 if (elf_dynverref (abfd) != 0)
1310 Elf_Internal_Verneed *t;
1312 fprintf (f, _("\nVersion References:\n"));
1313 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1315 Elf_Internal_Vernaux *a;
1317 fprintf (f, _(" required from %s:\n"),
1318 t->vn_filename ? t->vn_filename : "<corrupt>");
1319 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1320 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1321 a->vna_flags, a->vna_other,
1322 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1326 return TRUE;
1328 error_return:
1329 if (dynbuf != NULL)
1330 free (dynbuf);
1331 return FALSE;
1334 /* Display ELF-specific fields of a symbol. */
1336 void
1337 bfd_elf_print_symbol (bfd *abfd,
1338 void *filep,
1339 asymbol *symbol,
1340 bfd_print_symbol_type how)
1342 FILE *file = (FILE *) filep;
1343 switch (how)
1345 case bfd_print_symbol_name:
1346 fprintf (file, "%s", symbol->name);
1347 break;
1348 case bfd_print_symbol_more:
1349 fprintf (file, "elf ");
1350 bfd_fprintf_vma (abfd, file, symbol->value);
1351 fprintf (file, " %lx", (unsigned long) symbol->flags);
1352 break;
1353 case bfd_print_symbol_all:
1355 const char *section_name;
1356 const char *name = NULL;
1357 const struct elf_backend_data *bed;
1358 unsigned char st_other;
1359 bfd_vma val;
1361 section_name = symbol->section ? symbol->section->name : "(*none*)";
1363 bed = get_elf_backend_data (abfd);
1364 if (bed->elf_backend_print_symbol_all)
1365 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1367 if (name == NULL)
1369 name = symbol->name;
1370 bfd_print_symbol_vandf (abfd, file, symbol);
1373 fprintf (file, " %s\t", section_name);
1374 /* Print the "other" value for a symbol. For common symbols,
1375 we've already printed the size; now print the alignment.
1376 For other symbols, we have no specified alignment, and
1377 we've printed the address; now print the size. */
1378 if (symbol->section && bfd_is_com_section (symbol->section))
1379 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1380 else
1381 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1382 bfd_fprintf_vma (abfd, file, val);
1384 /* If we have version information, print it. */
1385 if (elf_tdata (abfd)->dynversym_section != 0
1386 && (elf_tdata (abfd)->dynverdef_section != 0
1387 || elf_tdata (abfd)->dynverref_section != 0))
1389 unsigned int vernum;
1390 const char *version_string;
1392 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1394 if (vernum == 0)
1395 version_string = "";
1396 else if (vernum == 1)
1397 version_string = "Base";
1398 else if (vernum <= elf_tdata (abfd)->cverdefs)
1399 version_string =
1400 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1401 else
1403 Elf_Internal_Verneed *t;
1405 version_string = "";
1406 for (t = elf_tdata (abfd)->verref;
1407 t != NULL;
1408 t = t->vn_nextref)
1410 Elf_Internal_Vernaux *a;
1412 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1414 if (a->vna_other == vernum)
1416 version_string = a->vna_nodename;
1417 break;
1423 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1424 fprintf (file, " %-11s", version_string);
1425 else
1427 int i;
1429 fprintf (file, " (%s)", version_string);
1430 for (i = 10 - strlen (version_string); i > 0; --i)
1431 putc (' ', file);
1435 /* If the st_other field is not zero, print it. */
1436 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1438 switch (st_other)
1440 case 0: break;
1441 case STV_INTERNAL: fprintf (file, " .internal"); break;
1442 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1443 case STV_PROTECTED: fprintf (file, " .protected"); break;
1444 default:
1445 /* Some other non-defined flags are also present, so print
1446 everything hex. */
1447 fprintf (file, " 0x%02x", (unsigned int) st_other);
1450 fprintf (file, " %s", name);
1452 break;
1456 /* Allocate an ELF string table--force the first byte to be zero. */
1458 struct bfd_strtab_hash *
1459 _bfd_elf_stringtab_init (void)
1461 struct bfd_strtab_hash *ret;
1463 ret = _bfd_stringtab_init ();
1464 if (ret != NULL)
1466 bfd_size_type loc;
1468 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1469 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1470 if (loc == (bfd_size_type) -1)
1472 _bfd_stringtab_free (ret);
1473 ret = NULL;
1476 return ret;
1479 /* ELF .o/exec file reading */
1481 /* Create a new bfd section from an ELF section header. */
1483 bfd_boolean
1484 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1486 Elf_Internal_Shdr *hdr;
1487 Elf_Internal_Ehdr *ehdr;
1488 const struct elf_backend_data *bed;
1489 const char *name;
1491 if (shindex >= elf_numsections (abfd))
1492 return FALSE;
1494 hdr = elf_elfsections (abfd)[shindex];
1495 ehdr = elf_elfheader (abfd);
1496 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1497 hdr->sh_name);
1498 if (name == NULL)
1499 return FALSE;
1501 bed = get_elf_backend_data (abfd);
1502 switch (hdr->sh_type)
1504 case SHT_NULL:
1505 /* Inactive section. Throw it away. */
1506 return TRUE;
1508 case SHT_PROGBITS: /* Normal section with contents. */
1509 case SHT_NOBITS: /* .bss section. */
1510 case SHT_HASH: /* .hash section. */
1511 case SHT_NOTE: /* .note section. */
1512 case SHT_INIT_ARRAY: /* .init_array section. */
1513 case SHT_FINI_ARRAY: /* .fini_array section. */
1514 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1515 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
1516 case SHT_GNU_HASH: /* .gnu.hash section. */
1517 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1519 case SHT_DYNAMIC: /* Dynamic linking information. */
1520 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1521 return FALSE;
1522 if (hdr->sh_link > elf_numsections (abfd))
1524 /* PR 10478: Accept Solaris binaries with a sh_link
1525 field set to SHN_BEFORE or SHN_AFTER. */
1526 switch (bfd_get_arch (abfd))
1528 case bfd_arch_i386:
1529 case bfd_arch_sparc:
1530 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
1531 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
1532 break;
1533 /* Otherwise fall through. */
1534 default:
1535 return FALSE;
1538 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
1539 return FALSE;
1540 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1542 Elf_Internal_Shdr *dynsymhdr;
1544 /* The shared libraries distributed with hpux11 have a bogus
1545 sh_link field for the ".dynamic" section. Find the
1546 string table for the ".dynsym" section instead. */
1547 if (elf_dynsymtab (abfd) != 0)
1549 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1550 hdr->sh_link = dynsymhdr->sh_link;
1552 else
1554 unsigned int i, num_sec;
1556 num_sec = elf_numsections (abfd);
1557 for (i = 1; i < num_sec; i++)
1559 dynsymhdr = elf_elfsections (abfd)[i];
1560 if (dynsymhdr->sh_type == SHT_DYNSYM)
1562 hdr->sh_link = dynsymhdr->sh_link;
1563 break;
1568 break;
1570 case SHT_SYMTAB: /* A symbol table */
1571 if (elf_onesymtab (abfd) == shindex)
1572 return TRUE;
1574 if (hdr->sh_entsize != bed->s->sizeof_sym)
1575 return FALSE;
1576 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
1577 return FALSE;
1578 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1579 elf_onesymtab (abfd) = shindex;
1580 elf_tdata (abfd)->symtab_hdr = *hdr;
1581 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1582 abfd->flags |= HAS_SYMS;
1584 /* Sometimes a shared object will map in the symbol table. If
1585 SHF_ALLOC is set, and this is a shared object, then we also
1586 treat this section as a BFD section. We can not base the
1587 decision purely on SHF_ALLOC, because that flag is sometimes
1588 set in a relocatable object file, which would confuse the
1589 linker. */
1590 if ((hdr->sh_flags & SHF_ALLOC) != 0
1591 && (abfd->flags & DYNAMIC) != 0
1592 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1593 shindex))
1594 return FALSE;
1596 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1597 can't read symbols without that section loaded as well. It
1598 is most likely specified by the next section header. */
1599 if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex)
1601 unsigned int i, num_sec;
1603 num_sec = elf_numsections (abfd);
1604 for (i = shindex + 1; i < num_sec; i++)
1606 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1607 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1608 && hdr2->sh_link == shindex)
1609 break;
1611 if (i == num_sec)
1612 for (i = 1; i < shindex; i++)
1614 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1615 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1616 && hdr2->sh_link == shindex)
1617 break;
1619 if (i != shindex)
1620 return bfd_section_from_shdr (abfd, i);
1622 return TRUE;
1624 case SHT_DYNSYM: /* A dynamic symbol table */
1625 if (elf_dynsymtab (abfd) == shindex)
1626 return TRUE;
1628 if (hdr->sh_entsize != bed->s->sizeof_sym)
1629 return FALSE;
1630 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1631 elf_dynsymtab (abfd) = shindex;
1632 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1633 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1634 abfd->flags |= HAS_SYMS;
1636 /* Besides being a symbol table, we also treat this as a regular
1637 section, so that objcopy can handle it. */
1638 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1640 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1641 if (elf_symtab_shndx (abfd) == shindex)
1642 return TRUE;
1644 BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
1645 elf_symtab_shndx (abfd) = shindex;
1646 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1647 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1648 return TRUE;
1650 case SHT_STRTAB: /* A string table */
1651 if (hdr->bfd_section != NULL)
1652 return TRUE;
1653 if (ehdr->e_shstrndx == shindex)
1655 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1656 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1657 return TRUE;
1659 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
1661 symtab_strtab:
1662 elf_tdata (abfd)->strtab_hdr = *hdr;
1663 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
1664 return TRUE;
1666 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
1668 dynsymtab_strtab:
1669 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1670 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
1671 elf_elfsections (abfd)[shindex] = hdr;
1672 /* We also treat this as a regular section, so that objcopy
1673 can handle it. */
1674 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1675 shindex);
1678 /* If the string table isn't one of the above, then treat it as a
1679 regular section. We need to scan all the headers to be sure,
1680 just in case this strtab section appeared before the above. */
1681 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
1683 unsigned int i, num_sec;
1685 num_sec = elf_numsections (abfd);
1686 for (i = 1; i < num_sec; i++)
1688 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1689 if (hdr2->sh_link == shindex)
1691 /* Prevent endless recursion on broken objects. */
1692 if (i == shindex)
1693 return FALSE;
1694 if (! bfd_section_from_shdr (abfd, i))
1695 return FALSE;
1696 if (elf_onesymtab (abfd) == i)
1697 goto symtab_strtab;
1698 if (elf_dynsymtab (abfd) == i)
1699 goto dynsymtab_strtab;
1703 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1705 case SHT_REL:
1706 case SHT_RELA:
1707 /* *These* do a lot of work -- but build no sections! */
1709 asection *target_sect;
1710 Elf_Internal_Shdr *hdr2;
1711 unsigned int num_sec = elf_numsections (abfd);
1713 if (hdr->sh_entsize
1714 != (bfd_size_type) (hdr->sh_type == SHT_REL
1715 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
1716 return FALSE;
1718 /* Check for a bogus link to avoid crashing. */
1719 if (hdr->sh_link >= num_sec)
1721 ((*_bfd_error_handler)
1722 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1723 abfd, hdr->sh_link, name, shindex));
1724 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1725 shindex);
1728 /* For some incomprehensible reason Oracle distributes
1729 libraries for Solaris in which some of the objects have
1730 bogus sh_link fields. It would be nice if we could just
1731 reject them, but, unfortunately, some people need to use
1732 them. We scan through the section headers; if we find only
1733 one suitable symbol table, we clobber the sh_link to point
1734 to it. I hope this doesn't break anything.
1736 Don't do it on executable nor shared library. */
1737 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
1738 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1739 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1741 unsigned int scan;
1742 int found;
1744 found = 0;
1745 for (scan = 1; scan < num_sec; scan++)
1747 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1748 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1750 if (found != 0)
1752 found = 0;
1753 break;
1755 found = scan;
1758 if (found != 0)
1759 hdr->sh_link = found;
1762 /* Get the symbol table. */
1763 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1764 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
1765 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1766 return FALSE;
1768 /* If this reloc section does not use the main symbol table we
1769 don't treat it as a reloc section. BFD can't adequately
1770 represent such a section, so at least for now, we don't
1771 try. We just present it as a normal section. We also
1772 can't use it as a reloc section if it points to the null
1773 section, an invalid section, another reloc section, or its
1774 sh_link points to the null section. */
1775 if (hdr->sh_link != elf_onesymtab (abfd)
1776 || hdr->sh_link == SHN_UNDEF
1777 || hdr->sh_info == SHN_UNDEF
1778 || hdr->sh_info >= num_sec
1779 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
1780 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
1781 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1782 shindex);
1784 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1785 return FALSE;
1786 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1787 if (target_sect == NULL)
1788 return FALSE;
1790 if ((target_sect->flags & SEC_RELOC) == 0
1791 || target_sect->reloc_count == 0)
1792 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1793 else
1795 bfd_size_type amt;
1796 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1797 amt = sizeof (*hdr2);
1798 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
1799 if (hdr2 == NULL)
1800 return FALSE;
1801 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1803 *hdr2 = *hdr;
1804 elf_elfsections (abfd)[shindex] = hdr2;
1805 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1806 target_sect->flags |= SEC_RELOC;
1807 target_sect->relocation = NULL;
1808 target_sect->rel_filepos = hdr->sh_offset;
1809 /* In the section to which the relocations apply, mark whether
1810 its relocations are of the REL or RELA variety. */
1811 if (hdr->sh_size != 0)
1812 target_sect->use_rela_p = hdr->sh_type == SHT_RELA;
1813 abfd->flags |= HAS_RELOC;
1814 return TRUE;
1817 case SHT_GNU_verdef:
1818 elf_dynverdef (abfd) = shindex;
1819 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1820 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1822 case SHT_GNU_versym:
1823 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
1824 return FALSE;
1825 elf_dynversym (abfd) = shindex;
1826 elf_tdata (abfd)->dynversym_hdr = *hdr;
1827 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1829 case SHT_GNU_verneed:
1830 elf_dynverref (abfd) = shindex;
1831 elf_tdata (abfd)->dynverref_hdr = *hdr;
1832 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1834 case SHT_SHLIB:
1835 return TRUE;
1837 case SHT_GROUP:
1838 if (! IS_VALID_GROUP_SECTION_HEADER (hdr))
1839 return FALSE;
1840 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1841 return FALSE;
1842 if (hdr->contents != NULL)
1844 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1845 unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE;
1846 asection *s;
1848 if (idx->flags & GRP_COMDAT)
1849 hdr->bfd_section->flags
1850 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1852 /* We try to keep the same section order as it comes in. */
1853 idx += n_elt;
1854 while (--n_elt != 0)
1856 --idx;
1858 if (idx->shdr != NULL
1859 && (s = idx->shdr->bfd_section) != NULL
1860 && elf_next_in_group (s) != NULL)
1862 elf_next_in_group (hdr->bfd_section) = s;
1863 break;
1867 break;
1869 default:
1870 /* Possibly an attributes section. */
1871 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
1872 || hdr->sh_type == bed->obj_attrs_section_type)
1874 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1875 return FALSE;
1876 _bfd_elf_parse_attributes (abfd, hdr);
1877 return TRUE;
1880 /* Check for any processor-specific section types. */
1881 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
1882 return TRUE;
1884 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
1886 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1887 /* FIXME: How to properly handle allocated section reserved
1888 for applications? */
1889 (*_bfd_error_handler)
1890 (_("%B: don't know how to handle allocated, application "
1891 "specific section `%s' [0x%8x]"),
1892 abfd, name, hdr->sh_type);
1893 else
1894 /* Allow sections reserved for applications. */
1895 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1896 shindex);
1898 else if (hdr->sh_type >= SHT_LOPROC
1899 && hdr->sh_type <= SHT_HIPROC)
1900 /* FIXME: We should handle this section. */
1901 (*_bfd_error_handler)
1902 (_("%B: don't know how to handle processor specific section "
1903 "`%s' [0x%8x]"),
1904 abfd, name, hdr->sh_type);
1905 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
1907 /* Unrecognised OS-specific sections. */
1908 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
1909 /* SHF_OS_NONCONFORMING indicates that special knowledge is
1910 required to correctly process the section and the file should
1911 be rejected with an error message. */
1912 (*_bfd_error_handler)
1913 (_("%B: don't know how to handle OS specific section "
1914 "`%s' [0x%8x]"),
1915 abfd, name, hdr->sh_type);
1916 else
1917 /* Otherwise it should be processed. */
1918 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1920 else
1921 /* FIXME: We should handle this section. */
1922 (*_bfd_error_handler)
1923 (_("%B: don't know how to handle section `%s' [0x%8x]"),
1924 abfd, name, hdr->sh_type);
1926 return FALSE;
1929 return TRUE;
1932 /* Return the local symbol specified by ABFD, R_SYMNDX. */
1934 Elf_Internal_Sym *
1935 bfd_sym_from_r_symndx (struct sym_cache *cache,
1936 bfd *abfd,
1937 unsigned long r_symndx)
1939 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
1941 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
1943 Elf_Internal_Shdr *symtab_hdr;
1944 unsigned char esym[sizeof (Elf64_External_Sym)];
1945 Elf_External_Sym_Shndx eshndx;
1947 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1948 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
1949 &cache->sym[ent], esym, &eshndx) == NULL)
1950 return NULL;
1952 if (cache->abfd != abfd)
1954 memset (cache->indx, -1, sizeof (cache->indx));
1955 cache->abfd = abfd;
1957 cache->indx[ent] = r_symndx;
1960 return &cache->sym[ent];
1963 /* Given an ELF section number, retrieve the corresponding BFD
1964 section. */
1966 asection *
1967 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
1969 if (sec_index >= elf_numsections (abfd))
1970 return NULL;
1971 return elf_elfsections (abfd)[sec_index]->bfd_section;
1974 static const struct bfd_elf_special_section special_sections_b[] =
1976 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1977 { NULL, 0, 0, 0, 0 }
1980 static const struct bfd_elf_special_section special_sections_c[] =
1982 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
1983 { NULL, 0, 0, 0, 0 }
1986 static const struct bfd_elf_special_section special_sections_d[] =
1988 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1989 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1990 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
1991 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
1992 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
1993 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
1994 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
1995 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
1996 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
1997 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
1998 { NULL, 0, 0, 0, 0 }
2001 static const struct bfd_elf_special_section special_sections_f[] =
2003 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2004 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2005 { NULL, 0, 0, 0, 0 }
2008 static const struct bfd_elf_special_section special_sections_g[] =
2010 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2011 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2012 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2013 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2014 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2015 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2016 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2017 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2018 { NULL, 0, 0, 0, 0 }
2021 static const struct bfd_elf_special_section special_sections_h[] =
2023 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2024 { NULL, 0, 0, 0, 0 }
2027 static const struct bfd_elf_special_section special_sections_i[] =
2029 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2030 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2031 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2032 { NULL, 0, 0, 0, 0 }
2035 static const struct bfd_elf_special_section special_sections_l[] =
2037 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2038 { NULL, 0, 0, 0, 0 }
2041 static const struct bfd_elf_special_section special_sections_n[] =
2043 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2044 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2045 { NULL, 0, 0, 0, 0 }
2048 static const struct bfd_elf_special_section special_sections_p[] =
2050 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2051 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2052 { NULL, 0, 0, 0, 0 }
2055 static const struct bfd_elf_special_section special_sections_r[] =
2057 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2058 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2059 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2060 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2061 { NULL, 0, 0, 0, 0 }
2064 static const struct bfd_elf_special_section special_sections_s[] =
2066 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2067 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2068 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2069 /* See struct bfd_elf_special_section declaration for the semantics of
2070 this special case where .prefix_length != strlen (.prefix). */
2071 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2072 { NULL, 0, 0, 0, 0 }
2075 static const struct bfd_elf_special_section special_sections_t[] =
2077 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2078 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2079 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2080 { NULL, 0, 0, 0, 0 }
2083 static const struct bfd_elf_special_section special_sections_z[] =
2085 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2086 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2087 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2088 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2089 { NULL, 0, 0, 0, 0 }
2092 static const struct bfd_elf_special_section *special_sections[] =
2094 special_sections_b, /* 'b' */
2095 special_sections_c, /* 'c' */
2096 special_sections_d, /* 'd' */
2097 NULL, /* 'e' */
2098 special_sections_f, /* 'f' */
2099 special_sections_g, /* 'g' */
2100 special_sections_h, /* 'h' */
2101 special_sections_i, /* 'i' */
2102 NULL, /* 'j' */
2103 NULL, /* 'k' */
2104 special_sections_l, /* 'l' */
2105 NULL, /* 'm' */
2106 special_sections_n, /* 'n' */
2107 NULL, /* 'o' */
2108 special_sections_p, /* 'p' */
2109 NULL, /* 'q' */
2110 special_sections_r, /* 'r' */
2111 special_sections_s, /* 's' */
2112 special_sections_t, /* 't' */
2113 NULL, /* 'u' */
2114 NULL, /* 'v' */
2115 NULL, /* 'w' */
2116 NULL, /* 'x' */
2117 NULL, /* 'y' */
2118 special_sections_z /* 'z' */
2121 const struct bfd_elf_special_section *
2122 _bfd_elf_get_special_section (const char *name,
2123 const struct bfd_elf_special_section *spec,
2124 unsigned int rela)
2126 int i;
2127 int len;
2129 len = strlen (name);
2131 for (i = 0; spec[i].prefix != NULL; i++)
2133 int suffix_len;
2134 int prefix_len = spec[i].prefix_length;
2136 if (len < prefix_len)
2137 continue;
2138 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2139 continue;
2141 suffix_len = spec[i].suffix_length;
2142 if (suffix_len <= 0)
2144 if (name[prefix_len] != 0)
2146 if (suffix_len == 0)
2147 continue;
2148 if (name[prefix_len] != '.'
2149 && (suffix_len == -2
2150 || (rela && spec[i].type == SHT_REL)))
2151 continue;
2154 else
2156 if (len < prefix_len + suffix_len)
2157 continue;
2158 if (memcmp (name + len - suffix_len,
2159 spec[i].prefix + prefix_len,
2160 suffix_len) != 0)
2161 continue;
2163 return &spec[i];
2166 return NULL;
2169 const struct bfd_elf_special_section *
2170 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2172 int i;
2173 const struct bfd_elf_special_section *spec;
2174 const struct elf_backend_data *bed;
2176 /* See if this is one of the special sections. */
2177 if (sec->name == NULL)
2178 return NULL;
2180 bed = get_elf_backend_data (abfd);
2181 spec = bed->special_sections;
2182 if (spec)
2184 spec = _bfd_elf_get_special_section (sec->name,
2185 bed->special_sections,
2186 sec->use_rela_p);
2187 if (spec != NULL)
2188 return spec;
2191 if (sec->name[0] != '.')
2192 return NULL;
2194 i = sec->name[1] - 'b';
2195 if (i < 0 || i > 'z' - 'b')
2196 return NULL;
2198 spec = special_sections[i];
2200 if (spec == NULL)
2201 return NULL;
2203 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2206 bfd_boolean
2207 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2209 struct bfd_elf_section_data *sdata;
2210 const struct elf_backend_data *bed;
2211 const struct bfd_elf_special_section *ssect;
2213 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2214 if (sdata == NULL)
2216 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2217 sizeof (*sdata));
2218 if (sdata == NULL)
2219 return FALSE;
2220 sec->used_by_bfd = sdata;
2223 /* Indicate whether or not this section should use RELA relocations. */
2224 bed = get_elf_backend_data (abfd);
2225 sec->use_rela_p = bed->default_use_rela_p;
2227 /* When we read a file, we don't need to set ELF section type and
2228 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2229 anyway. We will set ELF section type and flags for all linker
2230 created sections. If user specifies BFD section flags, we will
2231 set ELF section type and flags based on BFD section flags in
2232 elf_fake_sections. */
2233 if ((!sec->flags && abfd->direction != read_direction)
2234 || (sec->flags & SEC_LINKER_CREATED) != 0)
2236 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2237 if (ssect != NULL)
2239 elf_section_type (sec) = ssect->type;
2240 elf_section_flags (sec) = ssect->attr;
2244 return _bfd_generic_new_section_hook (abfd, sec);
2247 /* Create a new bfd section from an ELF program header.
2249 Since program segments have no names, we generate a synthetic name
2250 of the form segment<NUM>, where NUM is generally the index in the
2251 program header table. For segments that are split (see below) we
2252 generate the names segment<NUM>a and segment<NUM>b.
2254 Note that some program segments may have a file size that is different than
2255 (less than) the memory size. All this means is that at execution the
2256 system must allocate the amount of memory specified by the memory size,
2257 but only initialize it with the first "file size" bytes read from the
2258 file. This would occur for example, with program segments consisting
2259 of combined data+bss.
2261 To handle the above situation, this routine generates TWO bfd sections
2262 for the single program segment. The first has the length specified by
2263 the file size of the segment, and the second has the length specified
2264 by the difference between the two sizes. In effect, the segment is split
2265 into its initialized and uninitialized parts.
2269 bfd_boolean
2270 _bfd_elf_make_section_from_phdr (bfd *abfd,
2271 Elf_Internal_Phdr *hdr,
2272 int hdr_index,
2273 const char *type_name)
2275 asection *newsect;
2276 char *name;
2277 char namebuf[64];
2278 size_t len;
2279 int split;
2281 split = ((hdr->p_memsz > 0)
2282 && (hdr->p_filesz > 0)
2283 && (hdr->p_memsz > hdr->p_filesz));
2285 if (hdr->p_filesz > 0)
2287 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
2288 len = strlen (namebuf) + 1;
2289 name = (char *) bfd_alloc (abfd, len);
2290 if (!name)
2291 return FALSE;
2292 memcpy (name, namebuf, len);
2293 newsect = bfd_make_section (abfd, name);
2294 if (newsect == NULL)
2295 return FALSE;
2296 newsect->vma = hdr->p_vaddr;
2297 newsect->lma = hdr->p_paddr;
2298 newsect->size = hdr->p_filesz;
2299 newsect->filepos = hdr->p_offset;
2300 newsect->flags |= SEC_HAS_CONTENTS;
2301 newsect->alignment_power = bfd_log2 (hdr->p_align);
2302 if (hdr->p_type == PT_LOAD)
2304 newsect->flags |= SEC_ALLOC;
2305 newsect->flags |= SEC_LOAD;
2306 if (hdr->p_flags & PF_X)
2308 /* FIXME: all we known is that it has execute PERMISSION,
2309 may be data. */
2310 newsect->flags |= SEC_CODE;
2313 if (!(hdr->p_flags & PF_W))
2315 newsect->flags |= SEC_READONLY;
2319 if (hdr->p_memsz > hdr->p_filesz)
2321 bfd_vma align;
2323 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
2324 len = strlen (namebuf) + 1;
2325 name = (char *) bfd_alloc (abfd, len);
2326 if (!name)
2327 return FALSE;
2328 memcpy (name, namebuf, len);
2329 newsect = bfd_make_section (abfd, name);
2330 if (newsect == NULL)
2331 return FALSE;
2332 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2333 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2334 newsect->size = hdr->p_memsz - hdr->p_filesz;
2335 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2336 align = newsect->vma & -newsect->vma;
2337 if (align == 0 || align > hdr->p_align)
2338 align = hdr->p_align;
2339 newsect->alignment_power = bfd_log2 (align);
2340 if (hdr->p_type == PT_LOAD)
2342 /* Hack for gdb. Segments that have not been modified do
2343 not have their contents written to a core file, on the
2344 assumption that a debugger can find the contents in the
2345 executable. We flag this case by setting the fake
2346 section size to zero. Note that "real" bss sections will
2347 always have their contents dumped to the core file. */
2348 if (bfd_get_format (abfd) == bfd_core)
2349 newsect->size = 0;
2350 newsect->flags |= SEC_ALLOC;
2351 if (hdr->p_flags & PF_X)
2352 newsect->flags |= SEC_CODE;
2354 if (!(hdr->p_flags & PF_W))
2355 newsect->flags |= SEC_READONLY;
2358 return TRUE;
2361 bfd_boolean
2362 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
2364 const struct elf_backend_data *bed;
2366 switch (hdr->p_type)
2368 case PT_NULL:
2369 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
2371 case PT_LOAD:
2372 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load");
2374 case PT_DYNAMIC:
2375 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
2377 case PT_INTERP:
2378 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
2380 case PT_NOTE:
2381 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
2382 return FALSE;
2383 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2384 return FALSE;
2385 return TRUE;
2387 case PT_SHLIB:
2388 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
2390 case PT_PHDR:
2391 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
2393 case PT_GNU_EH_FRAME:
2394 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
2395 "eh_frame_hdr");
2397 case PT_GNU_STACK:
2398 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
2400 case PT_GNU_RELRO:
2401 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
2403 default:
2404 /* Check for any processor-specific program segment types. */
2405 bed = get_elf_backend_data (abfd);
2406 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
2410 /* Initialize REL_HDR, the section-header for new section, containing
2411 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2412 relocations; otherwise, we use REL relocations. */
2414 bfd_boolean
2415 _bfd_elf_init_reloc_shdr (bfd *abfd,
2416 Elf_Internal_Shdr *rel_hdr,
2417 asection *asect,
2418 bfd_boolean use_rela_p)
2420 char *name;
2421 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2422 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2424 name = (char *) bfd_alloc (abfd, amt);
2425 if (name == NULL)
2426 return FALSE;
2427 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2428 rel_hdr->sh_name =
2429 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2430 FALSE);
2431 if (rel_hdr->sh_name == (unsigned int) -1)
2432 return FALSE;
2433 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2434 rel_hdr->sh_entsize = (use_rela_p
2435 ? bed->s->sizeof_rela
2436 : bed->s->sizeof_rel);
2437 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
2438 rel_hdr->sh_flags = 0;
2439 rel_hdr->sh_addr = 0;
2440 rel_hdr->sh_size = 0;
2441 rel_hdr->sh_offset = 0;
2443 return TRUE;
2446 /* Return the default section type based on the passed in section flags. */
2449 bfd_elf_get_default_section_type (flagword flags)
2451 if ((flags & SEC_ALLOC) != 0
2452 && ((flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0
2453 || (flags & SEC_NEVER_LOAD) != 0))
2454 return SHT_NOBITS;
2455 return SHT_PROGBITS;
2458 /* Set up an ELF internal section header for a section. */
2460 static void
2461 elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg)
2463 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2464 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
2465 Elf_Internal_Shdr *this_hdr;
2466 unsigned int sh_type;
2468 if (*failedptr)
2470 /* We already failed; just get out of the bfd_map_over_sections
2471 loop. */
2472 return;
2475 this_hdr = &elf_section_data (asect)->this_hdr;
2477 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2478 asect->name, FALSE);
2479 if (this_hdr->sh_name == (unsigned int) -1)
2481 *failedptr = TRUE;
2482 return;
2485 /* Don't clear sh_flags. Assembler may set additional bits. */
2487 if ((asect->flags & SEC_ALLOC) != 0
2488 || asect->user_set_vma)
2489 this_hdr->sh_addr = asect->vma;
2490 else
2491 this_hdr->sh_addr = 0;
2493 this_hdr->sh_offset = 0;
2494 this_hdr->sh_size = asect->size;
2495 this_hdr->sh_link = 0;
2496 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
2497 /* The sh_entsize and sh_info fields may have been set already by
2498 copy_private_section_data. */
2500 this_hdr->bfd_section = asect;
2501 this_hdr->contents = NULL;
2503 /* If the section type is unspecified, we set it based on
2504 asect->flags. */
2505 if ((asect->flags & SEC_GROUP) != 0)
2506 sh_type = SHT_GROUP;
2507 else
2508 sh_type = bfd_elf_get_default_section_type (asect->flags);
2510 if (this_hdr->sh_type == SHT_NULL)
2511 this_hdr->sh_type = sh_type;
2512 else if (this_hdr->sh_type == SHT_NOBITS
2513 && sh_type == SHT_PROGBITS
2514 && (asect->flags & SEC_ALLOC) != 0)
2516 /* Warn if we are changing a NOBITS section to PROGBITS, but
2517 allow the link to proceed. This can happen when users link
2518 non-bss input sections to bss output sections, or emit data
2519 to a bss output section via a linker script. */
2520 (*_bfd_error_handler)
2521 (_("warning: section `%A' type changed to PROGBITS"), asect);
2522 this_hdr->sh_type = sh_type;
2525 switch (this_hdr->sh_type)
2527 default:
2528 break;
2530 case SHT_STRTAB:
2531 case SHT_INIT_ARRAY:
2532 case SHT_FINI_ARRAY:
2533 case SHT_PREINIT_ARRAY:
2534 case SHT_NOTE:
2535 case SHT_NOBITS:
2536 case SHT_PROGBITS:
2537 break;
2539 case SHT_HASH:
2540 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2541 break;
2543 case SHT_DYNSYM:
2544 this_hdr->sh_entsize = bed->s->sizeof_sym;
2545 break;
2547 case SHT_DYNAMIC:
2548 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2549 break;
2551 case SHT_RELA:
2552 if (get_elf_backend_data (abfd)->may_use_rela_p)
2553 this_hdr->sh_entsize = bed->s->sizeof_rela;
2554 break;
2556 case SHT_REL:
2557 if (get_elf_backend_data (abfd)->may_use_rel_p)
2558 this_hdr->sh_entsize = bed->s->sizeof_rel;
2559 break;
2561 case SHT_GNU_versym:
2562 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2563 break;
2565 case SHT_GNU_verdef:
2566 this_hdr->sh_entsize = 0;
2567 /* objcopy or strip will copy over sh_info, but may not set
2568 cverdefs. The linker will set cverdefs, but sh_info will be
2569 zero. */
2570 if (this_hdr->sh_info == 0)
2571 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2572 else
2573 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2574 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2575 break;
2577 case SHT_GNU_verneed:
2578 this_hdr->sh_entsize = 0;
2579 /* objcopy or strip will copy over sh_info, but may not set
2580 cverrefs. The linker will set cverrefs, but sh_info will be
2581 zero. */
2582 if (this_hdr->sh_info == 0)
2583 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2584 else
2585 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2586 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2587 break;
2589 case SHT_GROUP:
2590 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2591 break;
2593 case SHT_GNU_HASH:
2594 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
2595 break;
2598 if ((asect->flags & SEC_ALLOC) != 0)
2599 this_hdr->sh_flags |= SHF_ALLOC;
2600 if ((asect->flags & SEC_READONLY) == 0)
2601 this_hdr->sh_flags |= SHF_WRITE;
2602 if ((asect->flags & SEC_CODE) != 0)
2603 this_hdr->sh_flags |= SHF_EXECINSTR;
2604 if ((asect->flags & SEC_MERGE) != 0)
2606 this_hdr->sh_flags |= SHF_MERGE;
2607 this_hdr->sh_entsize = asect->entsize;
2608 if ((asect->flags & SEC_STRINGS) != 0)
2609 this_hdr->sh_flags |= SHF_STRINGS;
2611 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2612 this_hdr->sh_flags |= SHF_GROUP;
2613 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2615 this_hdr->sh_flags |= SHF_TLS;
2616 if (asect->size == 0
2617 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2619 struct bfd_link_order *o = asect->map_tail.link_order;
2621 this_hdr->sh_size = 0;
2622 if (o != NULL)
2624 this_hdr->sh_size = o->offset + o->size;
2625 if (this_hdr->sh_size != 0)
2626 this_hdr->sh_type = SHT_NOBITS;
2631 /* Check for processor-specific section types. */
2632 sh_type = this_hdr->sh_type;
2633 if (bed->elf_backend_fake_sections
2634 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2635 *failedptr = TRUE;
2637 if (sh_type == SHT_NOBITS && asect->size != 0)
2639 /* Don't change the header type from NOBITS if we are being
2640 called for objcopy --only-keep-debug. */
2641 this_hdr->sh_type = sh_type;
2644 /* If the section has relocs, set up a section header for the
2645 SHT_REL[A] section. If two relocation sections are required for
2646 this section, it is up to the processor-specific back-end to
2647 create the other. */
2648 if ((asect->flags & SEC_RELOC) != 0
2649 && !_bfd_elf_init_reloc_shdr (abfd,
2650 &elf_section_data (asect)->rel_hdr,
2651 asect,
2652 asect->use_rela_p))
2653 *failedptr = TRUE;
2656 /* Fill in the contents of a SHT_GROUP section. Called from
2657 _bfd_elf_compute_section_file_positions for gas, objcopy, and
2658 when ELF targets use the generic linker, ld. Called for ld -r
2659 from bfd_elf_final_link. */
2661 void
2662 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2664 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
2665 asection *elt, *first;
2666 unsigned char *loc;
2667 bfd_boolean gas;
2669 /* Ignore linker created group section. See elfNN_ia64_object_p in
2670 elfxx-ia64.c. */
2671 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
2672 || *failedptr)
2673 return;
2675 if (elf_section_data (sec)->this_hdr.sh_info == 0)
2677 unsigned long symindx = 0;
2679 /* elf_group_id will have been set up by objcopy and the
2680 generic linker. */
2681 if (elf_group_id (sec) != NULL)
2682 symindx = elf_group_id (sec)->udata.i;
2684 if (symindx == 0)
2686 /* If called from the assembler, swap_out_syms will have set up
2687 elf_section_syms. */
2688 BFD_ASSERT (elf_section_syms (abfd) != NULL);
2689 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2691 elf_section_data (sec)->this_hdr.sh_info = symindx;
2693 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
2695 /* The ELF backend linker sets sh_info to -2 when the group
2696 signature symbol is global, and thus the index can't be
2697 set until all local symbols are output. */
2698 asection *igroup = elf_sec_group (elf_next_in_group (sec));
2699 struct bfd_elf_section_data *sec_data = elf_section_data (igroup);
2700 unsigned long symndx = sec_data->this_hdr.sh_info;
2701 unsigned long extsymoff = 0;
2702 struct elf_link_hash_entry *h;
2704 if (!elf_bad_symtab (igroup->owner))
2706 Elf_Internal_Shdr *symtab_hdr;
2708 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
2709 extsymoff = symtab_hdr->sh_info;
2711 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
2712 while (h->root.type == bfd_link_hash_indirect
2713 || h->root.type == bfd_link_hash_warning)
2714 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2716 elf_section_data (sec)->this_hdr.sh_info = h->indx;
2719 /* The contents won't be allocated for "ld -r" or objcopy. */
2720 gas = TRUE;
2721 if (sec->contents == NULL)
2723 gas = FALSE;
2724 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
2726 /* Arrange for the section to be written out. */
2727 elf_section_data (sec)->this_hdr.contents = sec->contents;
2728 if (sec->contents == NULL)
2730 *failedptr = TRUE;
2731 return;
2735 loc = sec->contents + sec->size;
2737 /* Get the pointer to the first section in the group that gas
2738 squirreled away here. objcopy arranges for this to be set to the
2739 start of the input section group. */
2740 first = elt = elf_next_in_group (sec);
2742 /* First element is a flag word. Rest of section is elf section
2743 indices for all the sections of the group. Write them backwards
2744 just to keep the group in the same order as given in .section
2745 directives, not that it matters. */
2746 while (elt != NULL)
2748 asection *s;
2750 s = elt;
2751 if (!gas)
2752 s = s->output_section;
2753 if (s != NULL
2754 && !bfd_is_abs_section (s))
2756 unsigned int idx = elf_section_data (s)->this_idx;
2758 loc -= 4;
2759 H_PUT_32 (abfd, idx, loc);
2761 elt = elf_next_in_group (elt);
2762 if (elt == first)
2763 break;
2766 if ((loc -= 4) != sec->contents)
2767 abort ();
2769 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2772 /* Assign all ELF section numbers. The dummy first section is handled here
2773 too. The link/info pointers for the standard section types are filled
2774 in here too, while we're at it. */
2776 static bfd_boolean
2777 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
2779 struct elf_obj_tdata *t = elf_tdata (abfd);
2780 asection *sec;
2781 unsigned int section_number, secn;
2782 Elf_Internal_Shdr **i_shdrp;
2783 struct bfd_elf_section_data *d;
2784 bfd_boolean need_symtab;
2786 section_number = 1;
2788 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2790 /* SHT_GROUP sections are in relocatable files only. */
2791 if (link_info == NULL || link_info->relocatable)
2793 /* Put SHT_GROUP sections first. */
2794 for (sec = abfd->sections; sec != NULL; sec = sec->next)
2796 d = elf_section_data (sec);
2798 if (d->this_hdr.sh_type == SHT_GROUP)
2800 if (sec->flags & SEC_LINKER_CREATED)
2802 /* Remove the linker created SHT_GROUP sections. */
2803 bfd_section_list_remove (abfd, sec);
2804 abfd->section_count--;
2806 else
2807 d->this_idx = section_number++;
2812 for (sec = abfd->sections; sec; sec = sec->next)
2814 d = elf_section_data (sec);
2816 if (d->this_hdr.sh_type != SHT_GROUP)
2817 d->this_idx = section_number++;
2818 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2819 if ((sec->flags & SEC_RELOC) == 0)
2820 d->rel_idx = 0;
2821 else
2823 d->rel_idx = section_number++;
2824 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2827 if (d->rel_hdr2)
2829 d->rel_idx2 = section_number++;
2830 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2832 else
2833 d->rel_idx2 = 0;
2836 t->shstrtab_section = section_number++;
2837 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2838 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2840 need_symtab = (bfd_get_symcount (abfd) > 0
2841 || (link_info == NULL
2842 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
2843 == HAS_RELOC)));
2844 if (need_symtab)
2846 t->symtab_section = section_number++;
2847 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2848 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
2850 t->symtab_shndx_section = section_number++;
2851 t->symtab_shndx_hdr.sh_name
2852 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2853 ".symtab_shndx", FALSE);
2854 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2855 return FALSE;
2857 t->strtab_section = section_number++;
2858 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2861 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2862 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2864 elf_numsections (abfd) = section_number;
2865 elf_elfheader (abfd)->e_shnum = section_number;
2867 /* Set up the list of section header pointers, in agreement with the
2868 indices. */
2869 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
2870 sizeof (Elf_Internal_Shdr *));
2871 if (i_shdrp == NULL)
2872 return FALSE;
2874 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
2875 sizeof (Elf_Internal_Shdr));
2876 if (i_shdrp[0] == NULL)
2878 bfd_release (abfd, i_shdrp);
2879 return FALSE;
2882 elf_elfsections (abfd) = i_shdrp;
2884 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2885 if (need_symtab)
2887 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2888 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
2890 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2891 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2893 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2894 t->symtab_hdr.sh_link = t->strtab_section;
2897 for (sec = abfd->sections; sec; sec = sec->next)
2899 asection *s;
2900 const char *name;
2902 d = elf_section_data (sec);
2904 i_shdrp[d->this_idx] = &d->this_hdr;
2905 if (d->rel_idx != 0)
2906 i_shdrp[d->rel_idx] = &d->rel_hdr;
2907 if (d->rel_idx2 != 0)
2908 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2910 /* Fill in the sh_link and sh_info fields while we're at it. */
2912 /* sh_link of a reloc section is the section index of the symbol
2913 table. sh_info is the section index of the section to which
2914 the relocation entries apply. */
2915 if (d->rel_idx != 0)
2917 d->rel_hdr.sh_link = t->symtab_section;
2918 d->rel_hdr.sh_info = d->this_idx;
2920 if (d->rel_idx2 != 0)
2922 d->rel_hdr2->sh_link = t->symtab_section;
2923 d->rel_hdr2->sh_info = d->this_idx;
2926 /* We need to set up sh_link for SHF_LINK_ORDER. */
2927 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
2929 s = elf_linked_to_section (sec);
2930 if (s)
2932 /* elf_linked_to_section points to the input section. */
2933 if (link_info != NULL)
2935 /* Check discarded linkonce section. */
2936 if (elf_discarded_section (s))
2938 asection *kept;
2939 (*_bfd_error_handler)
2940 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
2941 abfd, d->this_hdr.bfd_section,
2942 s, s->owner);
2943 /* Point to the kept section if it has the same
2944 size as the discarded one. */
2945 kept = _bfd_elf_check_kept_section (s, link_info);
2946 if (kept == NULL)
2948 bfd_set_error (bfd_error_bad_value);
2949 return FALSE;
2951 s = kept;
2954 s = s->output_section;
2955 BFD_ASSERT (s != NULL);
2957 else
2959 /* Handle objcopy. */
2960 if (s->output_section == NULL)
2962 (*_bfd_error_handler)
2963 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
2964 abfd, d->this_hdr.bfd_section, s, s->owner);
2965 bfd_set_error (bfd_error_bad_value);
2966 return FALSE;
2968 s = s->output_section;
2970 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2972 else
2974 /* PR 290:
2975 The Intel C compiler generates SHT_IA_64_UNWIND with
2976 SHF_LINK_ORDER. But it doesn't set the sh_link or
2977 sh_info fields. Hence we could get the situation
2978 where s is NULL. */
2979 const struct elf_backend_data *bed
2980 = get_elf_backend_data (abfd);
2981 if (bed->link_order_error_handler)
2982 bed->link_order_error_handler
2983 (_("%B: warning: sh_link not set for section `%A'"),
2984 abfd, sec);
2988 switch (d->this_hdr.sh_type)
2990 case SHT_REL:
2991 case SHT_RELA:
2992 /* A reloc section which we are treating as a normal BFD
2993 section. sh_link is the section index of the symbol
2994 table. sh_info is the section index of the section to
2995 which the relocation entries apply. We assume that an
2996 allocated reloc section uses the dynamic symbol table.
2997 FIXME: How can we be sure? */
2998 s = bfd_get_section_by_name (abfd, ".dynsym");
2999 if (s != NULL)
3000 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3002 /* We look up the section the relocs apply to by name. */
3003 name = sec->name;
3004 if (d->this_hdr.sh_type == SHT_REL)
3005 name += 4;
3006 else
3007 name += 5;
3008 s = bfd_get_section_by_name (abfd, name);
3009 if (s != NULL)
3010 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3011 break;
3013 case SHT_STRTAB:
3014 /* We assume that a section named .stab*str is a stabs
3015 string section. We look for a section with the same name
3016 but without the trailing ``str'', and set its sh_link
3017 field to point to this section. */
3018 if (CONST_STRNEQ (sec->name, ".stab")
3019 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3021 size_t len;
3022 char *alc;
3024 len = strlen (sec->name);
3025 alc = (char *) bfd_malloc (len - 2);
3026 if (alc == NULL)
3027 return FALSE;
3028 memcpy (alc, sec->name, len - 3);
3029 alc[len - 3] = '\0';
3030 s = bfd_get_section_by_name (abfd, alc);
3031 free (alc);
3032 if (s != NULL)
3034 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3036 /* This is a .stab section. */
3037 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3038 elf_section_data (s)->this_hdr.sh_entsize
3039 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3042 break;
3044 case SHT_DYNAMIC:
3045 case SHT_DYNSYM:
3046 case SHT_GNU_verneed:
3047 case SHT_GNU_verdef:
3048 /* sh_link is the section header index of the string table
3049 used for the dynamic entries, or the symbol table, or the
3050 version strings. */
3051 s = bfd_get_section_by_name (abfd, ".dynstr");
3052 if (s != NULL)
3053 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3054 break;
3056 case SHT_GNU_LIBLIST:
3057 /* sh_link is the section header index of the prelink library
3058 list used for the dynamic entries, or the symbol table, or
3059 the version strings. */
3060 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3061 ? ".dynstr" : ".gnu.libstr");
3062 if (s != NULL)
3063 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3064 break;
3066 case SHT_HASH:
3067 case SHT_GNU_HASH:
3068 case SHT_GNU_versym:
3069 /* sh_link is the section header index of the symbol table
3070 this hash table or version table is for. */
3071 s = bfd_get_section_by_name (abfd, ".dynsym");
3072 if (s != NULL)
3073 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3074 break;
3076 case SHT_GROUP:
3077 d->this_hdr.sh_link = t->symtab_section;
3081 for (secn = 1; secn < section_number; ++secn)
3082 if (i_shdrp[secn] == NULL)
3083 i_shdrp[secn] = i_shdrp[0];
3084 else
3085 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
3086 i_shdrp[secn]->sh_name);
3087 return TRUE;
3090 /* Map symbol from it's internal number to the external number, moving
3091 all local symbols to be at the head of the list. */
3093 static bfd_boolean
3094 sym_is_global (bfd *abfd, asymbol *sym)
3096 /* If the backend has a special mapping, use it. */
3097 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3098 if (bed->elf_backend_sym_is_global)
3099 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3101 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
3102 || bfd_is_und_section (bfd_get_section (sym))
3103 || bfd_is_com_section (bfd_get_section (sym)));
3106 /* Don't output section symbols for sections that are not going to be
3107 output. */
3109 static bfd_boolean
3110 ignore_section_sym (bfd *abfd, asymbol *sym)
3112 return ((sym->flags & BSF_SECTION_SYM) != 0
3113 && !(sym->section->owner == abfd
3114 || (sym->section->output_section->owner == abfd
3115 && sym->section->output_offset == 0)));
3118 static bfd_boolean
3119 elf_map_symbols (bfd *abfd)
3121 unsigned int symcount = bfd_get_symcount (abfd);
3122 asymbol **syms = bfd_get_outsymbols (abfd);
3123 asymbol **sect_syms;
3124 unsigned int num_locals = 0;
3125 unsigned int num_globals = 0;
3126 unsigned int num_locals2 = 0;
3127 unsigned int num_globals2 = 0;
3128 int max_index = 0;
3129 unsigned int idx;
3130 asection *asect;
3131 asymbol **new_syms;
3133 #ifdef DEBUG
3134 fprintf (stderr, "elf_map_symbols\n");
3135 fflush (stderr);
3136 #endif
3138 for (asect = abfd->sections; asect; asect = asect->next)
3140 if (max_index < asect->index)
3141 max_index = asect->index;
3144 max_index++;
3145 sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
3146 if (sect_syms == NULL)
3147 return FALSE;
3148 elf_section_syms (abfd) = sect_syms;
3149 elf_num_section_syms (abfd) = max_index;
3151 /* Init sect_syms entries for any section symbols we have already
3152 decided to output. */
3153 for (idx = 0; idx < symcount; idx++)
3155 asymbol *sym = syms[idx];
3157 if ((sym->flags & BSF_SECTION_SYM) != 0
3158 && sym->value == 0
3159 && !ignore_section_sym (abfd, sym))
3161 asection *sec = sym->section;
3163 if (sec->owner != abfd)
3164 sec = sec->output_section;
3166 sect_syms[sec->index] = syms[idx];
3170 /* Classify all of the symbols. */
3171 for (idx = 0; idx < symcount; idx++)
3173 if (ignore_section_sym (abfd, syms[idx]))
3174 continue;
3175 if (!sym_is_global (abfd, syms[idx]))
3176 num_locals++;
3177 else
3178 num_globals++;
3181 /* We will be adding a section symbol for each normal BFD section. Most
3182 sections will already have a section symbol in outsymbols, but
3183 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3184 at least in that case. */
3185 for (asect = abfd->sections; asect; asect = asect->next)
3187 if (sect_syms[asect->index] == NULL)
3189 if (!sym_is_global (abfd, asect->symbol))
3190 num_locals++;
3191 else
3192 num_globals++;
3196 /* Now sort the symbols so the local symbols are first. */
3197 new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
3198 sizeof (asymbol *));
3200 if (new_syms == NULL)
3201 return FALSE;
3203 for (idx = 0; idx < symcount; idx++)
3205 asymbol *sym = syms[idx];
3206 unsigned int i;
3208 if (ignore_section_sym (abfd, sym))
3209 continue;
3210 if (!sym_is_global (abfd, sym))
3211 i = num_locals2++;
3212 else
3213 i = num_locals + num_globals2++;
3214 new_syms[i] = sym;
3215 sym->udata.i = i + 1;
3217 for (asect = abfd->sections; asect; asect = asect->next)
3219 if (sect_syms[asect->index] == NULL)
3221 asymbol *sym = asect->symbol;
3222 unsigned int i;
3224 sect_syms[asect->index] = sym;
3225 if (!sym_is_global (abfd, sym))
3226 i = num_locals2++;
3227 else
3228 i = num_locals + num_globals2++;
3229 new_syms[i] = sym;
3230 sym->udata.i = i + 1;
3234 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3236 elf_num_locals (abfd) = num_locals;
3237 elf_num_globals (abfd) = num_globals;
3238 return TRUE;
3241 /* Align to the maximum file alignment that could be required for any
3242 ELF data structure. */
3244 static inline file_ptr
3245 align_file_position (file_ptr off, int align)
3247 return (off + align - 1) & ~(align - 1);
3250 /* Assign a file position to a section, optionally aligning to the
3251 required section alignment. */
3253 file_ptr
3254 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3255 file_ptr offset,
3256 bfd_boolean align)
3258 if (align && i_shdrp->sh_addralign > 1)
3259 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
3260 i_shdrp->sh_offset = offset;
3261 if (i_shdrp->bfd_section != NULL)
3262 i_shdrp->bfd_section->filepos = offset;
3263 if (i_shdrp->sh_type != SHT_NOBITS)
3264 offset += i_shdrp->sh_size;
3265 return offset;
3268 /* Compute the file positions we are going to put the sections at, and
3269 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3270 is not NULL, this is being called by the ELF backend linker. */
3272 bfd_boolean
3273 _bfd_elf_compute_section_file_positions (bfd *abfd,
3274 struct bfd_link_info *link_info)
3276 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3277 bfd_boolean failed;
3278 struct bfd_strtab_hash *strtab = NULL;
3279 Elf_Internal_Shdr *shstrtab_hdr;
3280 bfd_boolean need_symtab;
3282 if (abfd->output_has_begun)
3283 return TRUE;
3285 /* Do any elf backend specific processing first. */
3286 if (bed->elf_backend_begin_write_processing)
3287 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3289 if (! prep_headers (abfd))
3290 return FALSE;
3292 /* Post process the headers if necessary. */
3293 if (bed->elf_backend_post_process_headers)
3294 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3296 failed = FALSE;
3297 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3298 if (failed)
3299 return FALSE;
3301 if (!assign_section_numbers (abfd, link_info))
3302 return FALSE;
3304 /* The backend linker builds symbol table information itself. */
3305 need_symtab = (link_info == NULL
3306 && (bfd_get_symcount (abfd) > 0
3307 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3308 == HAS_RELOC)));
3309 if (need_symtab)
3311 /* Non-zero if doing a relocatable link. */
3312 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3314 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3315 return FALSE;
3318 if (link_info == NULL)
3320 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3321 if (failed)
3322 return FALSE;
3325 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3326 /* sh_name was set in prep_headers. */
3327 shstrtab_hdr->sh_type = SHT_STRTAB;
3328 shstrtab_hdr->sh_flags = 0;
3329 shstrtab_hdr->sh_addr = 0;
3330 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3331 shstrtab_hdr->sh_entsize = 0;
3332 shstrtab_hdr->sh_link = 0;
3333 shstrtab_hdr->sh_info = 0;
3334 /* sh_offset is set in assign_file_positions_except_relocs. */
3335 shstrtab_hdr->sh_addralign = 1;
3337 if (!assign_file_positions_except_relocs (abfd, link_info))
3338 return FALSE;
3340 if (need_symtab)
3342 file_ptr off;
3343 Elf_Internal_Shdr *hdr;
3345 off = elf_tdata (abfd)->next_file_pos;
3347 hdr = &elf_tdata (abfd)->symtab_hdr;
3348 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3350 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3351 if (hdr->sh_size != 0)
3352 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3354 hdr = &elf_tdata (abfd)->strtab_hdr;
3355 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3357 elf_tdata (abfd)->next_file_pos = off;
3359 /* Now that we know where the .strtab section goes, write it
3360 out. */
3361 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3362 || ! _bfd_stringtab_emit (abfd, strtab))
3363 return FALSE;
3364 _bfd_stringtab_free (strtab);
3367 abfd->output_has_begun = TRUE;
3369 return TRUE;
3372 /* Make an initial estimate of the size of the program header. If we
3373 get the number wrong here, we'll redo section placement. */
3375 static bfd_size_type
3376 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
3378 size_t segs;
3379 asection *s;
3380 const struct elf_backend_data *bed;
3382 /* Assume we will need exactly two PT_LOAD segments: one for text
3383 and one for data. */
3384 segs = 2;
3386 s = bfd_get_section_by_name (abfd, ".interp");
3387 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3389 /* If we have a loadable interpreter section, we need a
3390 PT_INTERP segment. In this case, assume we also need a
3391 PT_PHDR segment, although that may not be true for all
3392 targets. */
3393 segs += 2;
3396 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3398 /* We need a PT_DYNAMIC segment. */
3399 ++segs;
3402 if (info != NULL && info->relro)
3404 /* We need a PT_GNU_RELRO segment. */
3405 ++segs;
3408 if (elf_tdata (abfd)->eh_frame_hdr)
3410 /* We need a PT_GNU_EH_FRAME segment. */
3411 ++segs;
3414 if (elf_tdata (abfd)->stack_flags)
3416 /* We need a PT_GNU_STACK segment. */
3417 ++segs;
3420 for (s = abfd->sections; s != NULL; s = s->next)
3422 if ((s->flags & SEC_LOAD) != 0
3423 && CONST_STRNEQ (s->name, ".note"))
3425 /* We need a PT_NOTE segment. */
3426 ++segs;
3427 /* Try to create just one PT_NOTE segment
3428 for all adjacent loadable .note* sections.
3429 gABI requires that within a PT_NOTE segment
3430 (and also inside of each SHT_NOTE section)
3431 each note is padded to a multiple of 4 size,
3432 so we check whether the sections are correctly
3433 aligned. */
3434 if (s->alignment_power == 2)
3435 while (s->next != NULL
3436 && s->next->alignment_power == 2
3437 && (s->next->flags & SEC_LOAD) != 0
3438 && CONST_STRNEQ (s->next->name, ".note"))
3439 s = s->next;
3443 for (s = abfd->sections; s != NULL; s = s->next)
3445 if (s->flags & SEC_THREAD_LOCAL)
3447 /* We need a PT_TLS segment. */
3448 ++segs;
3449 break;
3453 /* Let the backend count up any program headers it might need. */
3454 bed = get_elf_backend_data (abfd);
3455 if (bed->elf_backend_additional_program_headers)
3457 int a;
3459 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
3460 if (a == -1)
3461 abort ();
3462 segs += a;
3465 return segs * bed->s->sizeof_phdr;
3468 /* Find the segment that contains the output_section of section. */
3470 Elf_Internal_Phdr *
3471 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
3473 struct elf_segment_map *m;
3474 Elf_Internal_Phdr *p;
3476 for (m = elf_tdata (abfd)->segment_map,
3477 p = elf_tdata (abfd)->phdr;
3478 m != NULL;
3479 m = m->next, p++)
3481 int i;
3483 for (i = m->count - 1; i >= 0; i--)
3484 if (m->sections[i] == section)
3485 return p;
3488 return NULL;
3491 /* Create a mapping from a set of sections to a program segment. */
3493 static struct elf_segment_map *
3494 make_mapping (bfd *abfd,
3495 asection **sections,
3496 unsigned int from,
3497 unsigned int to,
3498 bfd_boolean phdr)
3500 struct elf_segment_map *m;
3501 unsigned int i;
3502 asection **hdrpp;
3503 bfd_size_type amt;
3505 amt = sizeof (struct elf_segment_map);
3506 amt += (to - from - 1) * sizeof (asection *);
3507 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3508 if (m == NULL)
3509 return NULL;
3510 m->next = NULL;
3511 m->p_type = PT_LOAD;
3512 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3513 m->sections[i - from] = *hdrpp;
3514 m->count = to - from;
3516 if (from == 0 && phdr)
3518 /* Include the headers in the first PT_LOAD segment. */
3519 m->includes_filehdr = 1;
3520 m->includes_phdrs = 1;
3523 return m;
3526 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3527 on failure. */
3529 struct elf_segment_map *
3530 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3532 struct elf_segment_map *m;
3534 m = (struct elf_segment_map *) bfd_zalloc (abfd,
3535 sizeof (struct elf_segment_map));
3536 if (m == NULL)
3537 return NULL;
3538 m->next = NULL;
3539 m->p_type = PT_DYNAMIC;
3540 m->count = 1;
3541 m->sections[0] = dynsec;
3543 return m;
3546 /* Possibly add or remove segments from the segment map. */
3548 static bfd_boolean
3549 elf_modify_segment_map (bfd *abfd,
3550 struct bfd_link_info *info,
3551 bfd_boolean remove_empty_load)
3553 struct elf_segment_map **m;
3554 const struct elf_backend_data *bed;
3556 /* The placement algorithm assumes that non allocated sections are
3557 not in PT_LOAD segments. We ensure this here by removing such
3558 sections from the segment map. We also remove excluded
3559 sections. Finally, any PT_LOAD segment without sections is
3560 removed. */
3561 m = &elf_tdata (abfd)->segment_map;
3562 while (*m)
3564 unsigned int i, new_count;
3566 for (new_count = 0, i = 0; i < (*m)->count; i++)
3568 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
3569 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
3570 || (*m)->p_type != PT_LOAD))
3572 (*m)->sections[new_count] = (*m)->sections[i];
3573 new_count++;
3576 (*m)->count = new_count;
3578 if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0)
3579 *m = (*m)->next;
3580 else
3581 m = &(*m)->next;
3584 bed = get_elf_backend_data (abfd);
3585 if (bed->elf_backend_modify_segment_map != NULL)
3587 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
3588 return FALSE;
3591 return TRUE;
3594 /* Set up a mapping from BFD sections to program segments. */
3596 bfd_boolean
3597 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
3599 unsigned int count;
3600 struct elf_segment_map *m;
3601 asection **sections = NULL;
3602 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3603 bfd_boolean no_user_phdrs;
3605 no_user_phdrs = elf_tdata (abfd)->segment_map == NULL;
3606 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
3608 asection *s;
3609 unsigned int i;
3610 struct elf_segment_map *mfirst;
3611 struct elf_segment_map **pm;
3612 asection *last_hdr;
3613 bfd_vma last_size;
3614 unsigned int phdr_index;
3615 bfd_vma maxpagesize;
3616 asection **hdrpp;
3617 bfd_boolean phdr_in_segment = TRUE;
3618 bfd_boolean writable;
3619 int tls_count = 0;
3620 asection *first_tls = NULL;
3621 asection *dynsec, *eh_frame_hdr;
3622 bfd_size_type amt;
3624 /* Select the allocated sections, and sort them. */
3626 sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
3627 sizeof (asection *));
3628 if (sections == NULL)
3629 goto error_return;
3631 i = 0;
3632 for (s = abfd->sections; s != NULL; s = s->next)
3634 if ((s->flags & SEC_ALLOC) != 0)
3636 sections[i] = s;
3637 ++i;
3640 BFD_ASSERT (i <= bfd_count_sections (abfd));
3641 count = i;
3643 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3645 /* Build the mapping. */
3647 mfirst = NULL;
3648 pm = &mfirst;
3650 /* If we have a .interp section, then create a PT_PHDR segment for
3651 the program headers and a PT_INTERP segment for the .interp
3652 section. */
3653 s = bfd_get_section_by_name (abfd, ".interp");
3654 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3656 amt = sizeof (struct elf_segment_map);
3657 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3658 if (m == NULL)
3659 goto error_return;
3660 m->next = NULL;
3661 m->p_type = PT_PHDR;
3662 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3663 m->p_flags = PF_R | PF_X;
3664 m->p_flags_valid = 1;
3665 m->includes_phdrs = 1;
3667 *pm = m;
3668 pm = &m->next;
3670 amt = sizeof (struct elf_segment_map);
3671 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3672 if (m == NULL)
3673 goto error_return;
3674 m->next = NULL;
3675 m->p_type = PT_INTERP;
3676 m->count = 1;
3677 m->sections[0] = s;
3679 *pm = m;
3680 pm = &m->next;
3683 /* Look through the sections. We put sections in the same program
3684 segment when the start of the second section can be placed within
3685 a few bytes of the end of the first section. */
3686 last_hdr = NULL;
3687 last_size = 0;
3688 phdr_index = 0;
3689 maxpagesize = bed->maxpagesize;
3690 writable = FALSE;
3691 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3692 if (dynsec != NULL
3693 && (dynsec->flags & SEC_LOAD) == 0)
3694 dynsec = NULL;
3696 /* Deal with -Ttext or something similar such that the first section
3697 is not adjacent to the program headers. This is an
3698 approximation, since at this point we don't know exactly how many
3699 program headers we will need. */
3700 if (count > 0)
3702 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
3704 if (phdr_size == (bfd_size_type) -1)
3705 phdr_size = get_program_header_size (abfd, info);
3706 if ((abfd->flags & D_PAGED) == 0
3707 || sections[0]->lma < phdr_size
3708 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3709 phdr_in_segment = FALSE;
3712 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3714 asection *hdr;
3715 bfd_boolean new_segment;
3717 hdr = *hdrpp;
3719 /* See if this section and the last one will fit in the same
3720 segment. */
3722 if (last_hdr == NULL)
3724 /* If we don't have a segment yet, then we don't need a new
3725 one (we build the last one after this loop). */
3726 new_segment = FALSE;
3728 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3730 /* If this section has a different relation between the
3731 virtual address and the load address, then we need a new
3732 segment. */
3733 new_segment = TRUE;
3735 /* In the next test we have to be careful when last_hdr->lma is close
3736 to the end of the address space. If the aligned address wraps
3737 around to the start of the address space, then there are no more
3738 pages left in memory and it is OK to assume that the current
3739 section can be included in the current segment. */
3740 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
3741 > last_hdr->lma)
3742 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
3743 <= hdr->lma))
3745 /* If putting this section in this segment would force us to
3746 skip a page in the segment, then we need a new segment. */
3747 new_segment = TRUE;
3749 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3750 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
3752 /* We don't want to put a loadable section after a
3753 nonloadable section in the same segment.
3754 Consider .tbss sections as loadable for this purpose. */
3755 new_segment = TRUE;
3757 else if ((abfd->flags & D_PAGED) == 0)
3759 /* If the file is not demand paged, which means that we
3760 don't require the sections to be correctly aligned in the
3761 file, then there is no other reason for a new segment. */
3762 new_segment = FALSE;
3764 else if (! writable
3765 && (hdr->flags & SEC_READONLY) == 0
3766 && (((last_hdr->lma + last_size - 1)
3767 & ~(maxpagesize - 1))
3768 != (hdr->lma & ~(maxpagesize - 1))))
3770 /* We don't want to put a writable section in a read only
3771 segment, unless they are on the same page in memory
3772 anyhow. We already know that the last section does not
3773 bring us past the current section on the page, so the
3774 only case in which the new section is not on the same
3775 page as the previous section is when the previous section
3776 ends precisely on a page boundary. */
3777 new_segment = TRUE;
3779 else
3781 /* Otherwise, we can use the same segment. */
3782 new_segment = FALSE;
3785 /* Allow interested parties a chance to override our decision. */
3786 if (last_hdr != NULL
3787 && info != NULL
3788 && info->callbacks->override_segment_assignment != NULL)
3789 new_segment
3790 = info->callbacks->override_segment_assignment (info, abfd, hdr,
3791 last_hdr,
3792 new_segment);
3794 if (! new_segment)
3796 if ((hdr->flags & SEC_READONLY) == 0)
3797 writable = TRUE;
3798 last_hdr = hdr;
3799 /* .tbss sections effectively have zero size. */
3800 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
3801 != SEC_THREAD_LOCAL)
3802 last_size = hdr->size;
3803 else
3804 last_size = 0;
3805 continue;
3808 /* We need a new program segment. We must create a new program
3809 header holding all the sections from phdr_index until hdr. */
3811 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3812 if (m == NULL)
3813 goto error_return;
3815 *pm = m;
3816 pm = &m->next;
3818 if ((hdr->flags & SEC_READONLY) == 0)
3819 writable = TRUE;
3820 else
3821 writable = FALSE;
3823 last_hdr = hdr;
3824 /* .tbss sections effectively have zero size. */
3825 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3826 last_size = hdr->size;
3827 else
3828 last_size = 0;
3829 phdr_index = i;
3830 phdr_in_segment = FALSE;
3833 /* Create a final PT_LOAD program segment. */
3834 if (last_hdr != NULL)
3836 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3837 if (m == NULL)
3838 goto error_return;
3840 *pm = m;
3841 pm = &m->next;
3844 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3845 if (dynsec != NULL)
3847 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
3848 if (m == NULL)
3849 goto error_return;
3850 *pm = m;
3851 pm = &m->next;
3854 /* For each batch of consecutive loadable .note sections,
3855 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
3856 because if we link together nonloadable .note sections and
3857 loadable .note sections, we will generate two .note sections
3858 in the output file. FIXME: Using names for section types is
3859 bogus anyhow. */
3860 for (s = abfd->sections; s != NULL; s = s->next)
3862 if ((s->flags & SEC_LOAD) != 0
3863 && CONST_STRNEQ (s->name, ".note"))
3865 asection *s2;
3867 count = 1;
3868 amt = sizeof (struct elf_segment_map);
3869 if (s->alignment_power == 2)
3870 for (s2 = s; s2->next != NULL; s2 = s2->next)
3872 if (s2->next->alignment_power == 2
3873 && (s2->next->flags & SEC_LOAD) != 0
3874 && CONST_STRNEQ (s2->next->name, ".note")
3875 && align_power (s2->vma + s2->size, 2)
3876 == s2->next->vma)
3877 count++;
3878 else
3879 break;
3881 amt += (count - 1) * sizeof (asection *);
3882 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3883 if (m == NULL)
3884 goto error_return;
3885 m->next = NULL;
3886 m->p_type = PT_NOTE;
3887 m->count = count;
3888 while (count > 1)
3890 m->sections[m->count - count--] = s;
3891 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3892 s = s->next;
3894 m->sections[m->count - 1] = s;
3895 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3896 *pm = m;
3897 pm = &m->next;
3899 if (s->flags & SEC_THREAD_LOCAL)
3901 if (! tls_count)
3902 first_tls = s;
3903 tls_count++;
3907 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3908 if (tls_count > 0)
3910 amt = sizeof (struct elf_segment_map);
3911 amt += (tls_count - 1) * sizeof (asection *);
3912 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3913 if (m == NULL)
3914 goto error_return;
3915 m->next = NULL;
3916 m->p_type = PT_TLS;
3917 m->count = tls_count;
3918 /* Mandated PF_R. */
3919 m->p_flags = PF_R;
3920 m->p_flags_valid = 1;
3921 for (i = 0; i < (unsigned int) tls_count; ++i)
3923 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3924 m->sections[i] = first_tls;
3925 first_tls = first_tls->next;
3928 *pm = m;
3929 pm = &m->next;
3932 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3933 segment. */
3934 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
3935 if (eh_frame_hdr != NULL
3936 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
3938 amt = sizeof (struct elf_segment_map);
3939 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3940 if (m == NULL)
3941 goto error_return;
3942 m->next = NULL;
3943 m->p_type = PT_GNU_EH_FRAME;
3944 m->count = 1;
3945 m->sections[0] = eh_frame_hdr->output_section;
3947 *pm = m;
3948 pm = &m->next;
3951 if (elf_tdata (abfd)->stack_flags)
3953 amt = sizeof (struct elf_segment_map);
3954 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3955 if (m == NULL)
3956 goto error_return;
3957 m->next = NULL;
3958 m->p_type = PT_GNU_STACK;
3959 m->p_flags = elf_tdata (abfd)->stack_flags;
3960 m->p_flags_valid = 1;
3962 *pm = m;
3963 pm = &m->next;
3966 if (info != NULL && info->relro)
3968 for (m = mfirst; m != NULL; m = m->next)
3970 if (m->p_type == PT_LOAD)
3972 asection *last = m->sections[m->count - 1];
3973 bfd_vma vaddr = m->sections[0]->vma;
3974 bfd_vma filesz = last->vma - vaddr + last->size;
3976 if (vaddr < info->relro_end
3977 && vaddr >= info->relro_start
3978 && (vaddr + filesz) >= info->relro_end)
3979 break;
3983 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
3984 if (m != NULL)
3986 amt = sizeof (struct elf_segment_map);
3987 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3988 if (m == NULL)
3989 goto error_return;
3990 m->next = NULL;
3991 m->p_type = PT_GNU_RELRO;
3992 m->p_flags = PF_R;
3993 m->p_flags_valid = 1;
3995 *pm = m;
3996 pm = &m->next;
4000 free (sections);
4001 elf_tdata (abfd)->segment_map = mfirst;
4004 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
4005 return FALSE;
4007 for (count = 0, m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4008 ++count;
4009 elf_tdata (abfd)->program_header_size = count * bed->s->sizeof_phdr;
4011 return TRUE;
4013 error_return:
4014 if (sections != NULL)
4015 free (sections);
4016 return FALSE;
4019 /* Sort sections by address. */
4021 static int
4022 elf_sort_sections (const void *arg1, const void *arg2)
4024 const asection *sec1 = *(const asection **) arg1;
4025 const asection *sec2 = *(const asection **) arg2;
4026 bfd_size_type size1, size2;
4028 /* Sort by LMA first, since this is the address used to
4029 place the section into a segment. */
4030 if (sec1->lma < sec2->lma)
4031 return -1;
4032 else if (sec1->lma > sec2->lma)
4033 return 1;
4035 /* Then sort by VMA. Normally the LMA and the VMA will be
4036 the same, and this will do nothing. */
4037 if (sec1->vma < sec2->vma)
4038 return -1;
4039 else if (sec1->vma > sec2->vma)
4040 return 1;
4042 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4044 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4046 if (TOEND (sec1))
4048 if (TOEND (sec2))
4050 /* If the indicies are the same, do not return 0
4051 here, but continue to try the next comparison. */
4052 if (sec1->target_index - sec2->target_index != 0)
4053 return sec1->target_index - sec2->target_index;
4055 else
4056 return 1;
4058 else if (TOEND (sec2))
4059 return -1;
4061 #undef TOEND
4063 /* Sort by size, to put zero sized sections
4064 before others at the same address. */
4066 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
4067 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
4069 if (size1 < size2)
4070 return -1;
4071 if (size1 > size2)
4072 return 1;
4074 return sec1->target_index - sec2->target_index;
4077 /* Ian Lance Taylor writes:
4079 We shouldn't be using % with a negative signed number. That's just
4080 not good. We have to make sure either that the number is not
4081 negative, or that the number has an unsigned type. When the types
4082 are all the same size they wind up as unsigned. When file_ptr is a
4083 larger signed type, the arithmetic winds up as signed long long,
4084 which is wrong.
4086 What we're trying to say here is something like ``increase OFF by
4087 the least amount that will cause it to be equal to the VMA modulo
4088 the page size.'' */
4089 /* In other words, something like:
4091 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4092 off_offset = off % bed->maxpagesize;
4093 if (vma_offset < off_offset)
4094 adjustment = vma_offset + bed->maxpagesize - off_offset;
4095 else
4096 adjustment = vma_offset - off_offset;
4098 which can can be collapsed into the expression below. */
4100 static file_ptr
4101 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
4103 return ((vma - off) % maxpagesize);
4106 static void
4107 print_segment_map (const struct elf_segment_map *m)
4109 unsigned int j;
4110 const char *pt = get_segment_type (m->p_type);
4111 char buf[32];
4113 if (pt == NULL)
4115 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
4116 sprintf (buf, "LOPROC+%7.7x",
4117 (unsigned int) (m->p_type - PT_LOPROC));
4118 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
4119 sprintf (buf, "LOOS+%7.7x",
4120 (unsigned int) (m->p_type - PT_LOOS));
4121 else
4122 snprintf (buf, sizeof (buf), "%8.8x",
4123 (unsigned int) m->p_type);
4124 pt = buf;
4126 fprintf (stderr, "%s:", pt);
4127 for (j = 0; j < m->count; j++)
4128 fprintf (stderr, " %s", m->sections [j]->name);
4129 putc ('\n',stderr);
4132 static bfd_boolean
4133 write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
4135 void *buf;
4136 bfd_boolean ret;
4138 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
4139 return FALSE;
4140 buf = bfd_zmalloc (len);
4141 if (buf == NULL)
4142 return FALSE;
4143 ret = bfd_bwrite (buf, len, abfd) == len;
4144 free (buf);
4145 return ret;
4148 /* Assign file positions to the sections based on the mapping from
4149 sections to segments. This function also sets up some fields in
4150 the file header. */
4152 static bfd_boolean
4153 assign_file_positions_for_load_sections (bfd *abfd,
4154 struct bfd_link_info *link_info)
4156 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4157 struct elf_segment_map *m;
4158 Elf_Internal_Phdr *phdrs;
4159 Elf_Internal_Phdr *p;
4160 file_ptr off;
4161 bfd_size_type maxpagesize;
4162 unsigned int alloc;
4163 unsigned int i, j;
4164 bfd_vma header_pad = 0;
4166 if (link_info == NULL
4167 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
4168 return FALSE;
4170 alloc = 0;
4171 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4173 ++alloc;
4174 if (m->header_size)
4175 header_pad = m->header_size;
4178 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
4179 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
4180 elf_elfheader (abfd)->e_phnum = alloc;
4182 if (elf_tdata (abfd)->program_header_size == (bfd_size_type) -1)
4183 elf_tdata (abfd)->program_header_size = alloc * bed->s->sizeof_phdr;
4184 else
4185 BFD_ASSERT (elf_tdata (abfd)->program_header_size
4186 >= alloc * bed->s->sizeof_phdr);
4188 if (alloc == 0)
4190 elf_tdata (abfd)->next_file_pos = bed->s->sizeof_ehdr;
4191 return TRUE;
4194 /* We're writing the size in elf_tdata (abfd)->program_header_size,
4195 see assign_file_positions_except_relocs, so make sure we have
4196 that amount allocated, with trailing space cleared.
4197 The variable alloc contains the computed need, while elf_tdata
4198 (abfd)->program_header_size contains the size used for the
4199 layout.
4200 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4201 where the layout is forced to according to a larger size in the
4202 last iterations for the testcase ld-elf/header. */
4203 BFD_ASSERT (elf_tdata (abfd)->program_header_size % bed->s->sizeof_phdr
4204 == 0);
4205 phdrs = (Elf_Internal_Phdr *)
4206 bfd_zalloc2 (abfd,
4207 (elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr),
4208 sizeof (Elf_Internal_Phdr));
4209 elf_tdata (abfd)->phdr = phdrs;
4210 if (phdrs == NULL)
4211 return FALSE;
4213 maxpagesize = 1;
4214 if ((abfd->flags & D_PAGED) != 0)
4215 maxpagesize = bed->maxpagesize;
4217 off = bed->s->sizeof_ehdr;
4218 off += alloc * bed->s->sizeof_phdr;
4219 if (header_pad < (bfd_vma) off)
4220 header_pad = 0;
4221 else
4222 header_pad -= off;
4223 off += header_pad;
4225 for (m = elf_tdata (abfd)->segment_map, p = phdrs, j = 0;
4226 m != NULL;
4227 m = m->next, p++, j++)
4229 asection **secpp;
4230 bfd_vma off_adjust;
4231 bfd_boolean no_contents;
4233 /* If elf_segment_map is not from map_sections_to_segments, the
4234 sections may not be correctly ordered. NOTE: sorting should
4235 not be done to the PT_NOTE section of a corefile, which may
4236 contain several pseudo-sections artificially created by bfd.
4237 Sorting these pseudo-sections breaks things badly. */
4238 if (m->count > 1
4239 && !(elf_elfheader (abfd)->e_type == ET_CORE
4240 && m->p_type == PT_NOTE))
4241 qsort (m->sections, (size_t) m->count, sizeof (asection *),
4242 elf_sort_sections);
4244 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4245 number of sections with contents contributing to both p_filesz
4246 and p_memsz, followed by a number of sections with no contents
4247 that just contribute to p_memsz. In this loop, OFF tracks next
4248 available file offset for PT_LOAD and PT_NOTE segments. */
4249 p->p_type = m->p_type;
4250 p->p_flags = m->p_flags;
4252 if (m->count == 0)
4253 p->p_vaddr = 0;
4254 else
4255 p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
4257 if (m->p_paddr_valid)
4258 p->p_paddr = m->p_paddr;
4259 else if (m->count == 0)
4260 p->p_paddr = 0;
4261 else
4262 p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
4264 if (p->p_type == PT_LOAD
4265 && (abfd->flags & D_PAGED) != 0)
4267 /* p_align in demand paged PT_LOAD segments effectively stores
4268 the maximum page size. When copying an executable with
4269 objcopy, we set m->p_align from the input file. Use this
4270 value for maxpagesize rather than bed->maxpagesize, which
4271 may be different. Note that we use maxpagesize for PT_TLS
4272 segment alignment later in this function, so we are relying
4273 on at least one PT_LOAD segment appearing before a PT_TLS
4274 segment. */
4275 if (m->p_align_valid)
4276 maxpagesize = m->p_align;
4278 p->p_align = maxpagesize;
4280 else if (m->p_align_valid)
4281 p->p_align = m->p_align;
4282 else if (m->count == 0)
4283 p->p_align = 1 << bed->s->log_file_align;
4284 else
4285 p->p_align = 0;
4287 no_contents = FALSE;
4288 off_adjust = 0;
4289 if (p->p_type == PT_LOAD
4290 && m->count > 0)
4292 bfd_size_type align;
4293 unsigned int align_power = 0;
4295 if (m->p_align_valid)
4296 align = p->p_align;
4297 else
4299 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4301 unsigned int secalign;
4303 secalign = bfd_get_section_alignment (abfd, *secpp);
4304 if (secalign > align_power)
4305 align_power = secalign;
4307 align = (bfd_size_type) 1 << align_power;
4308 if (align < maxpagesize)
4309 align = maxpagesize;
4312 for (i = 0; i < m->count; i++)
4313 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
4314 /* If we aren't making room for this section, then
4315 it must be SHT_NOBITS regardless of what we've
4316 set via struct bfd_elf_special_section. */
4317 elf_section_type (m->sections[i]) = SHT_NOBITS;
4319 /* Find out whether this segment contains any loadable
4320 sections. */
4321 no_contents = TRUE;
4322 for (i = 0; i < m->count; i++)
4323 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
4325 no_contents = FALSE;
4326 break;
4329 off_adjust = vma_page_aligned_bias (m->sections[0]->vma, off, align);
4330 off += off_adjust;
4331 if (no_contents)
4333 /* We shouldn't need to align the segment on disk since
4334 the segment doesn't need file space, but the gABI
4335 arguably requires the alignment and glibc ld.so
4336 checks it. So to comply with the alignment
4337 requirement but not waste file space, we adjust
4338 p_offset for just this segment. (OFF_ADJUST is
4339 subtracted from OFF later.) This may put p_offset
4340 past the end of file, but that shouldn't matter. */
4342 else
4343 off_adjust = 0;
4345 /* Make sure the .dynamic section is the first section in the
4346 PT_DYNAMIC segment. */
4347 else if (p->p_type == PT_DYNAMIC
4348 && m->count > 1
4349 && strcmp (m->sections[0]->name, ".dynamic") != 0)
4351 _bfd_error_handler
4352 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4353 abfd);
4354 bfd_set_error (bfd_error_bad_value);
4355 return FALSE;
4357 /* Set the note section type to SHT_NOTE. */
4358 else if (p->p_type == PT_NOTE)
4359 for (i = 0; i < m->count; i++)
4360 elf_section_type (m->sections[i]) = SHT_NOTE;
4362 p->p_offset = 0;
4363 p->p_filesz = 0;
4364 p->p_memsz = 0;
4366 if (m->includes_filehdr)
4368 if (!m->p_flags_valid)
4369 p->p_flags |= PF_R;
4370 p->p_filesz = bed->s->sizeof_ehdr;
4371 p->p_memsz = bed->s->sizeof_ehdr;
4372 if (m->count > 0)
4374 BFD_ASSERT (p->p_type == PT_LOAD);
4376 if (p->p_vaddr < (bfd_vma) off)
4378 (*_bfd_error_handler)
4379 (_("%B: Not enough room for program headers, try linking with -N"),
4380 abfd);
4381 bfd_set_error (bfd_error_bad_value);
4382 return FALSE;
4385 p->p_vaddr -= off;
4386 if (!m->p_paddr_valid)
4387 p->p_paddr -= off;
4391 if (m->includes_phdrs)
4393 if (!m->p_flags_valid)
4394 p->p_flags |= PF_R;
4396 if (!m->includes_filehdr)
4398 p->p_offset = bed->s->sizeof_ehdr;
4400 if (m->count > 0)
4402 BFD_ASSERT (p->p_type == PT_LOAD);
4403 p->p_vaddr -= off - p->p_offset;
4404 if (!m->p_paddr_valid)
4405 p->p_paddr -= off - p->p_offset;
4409 p->p_filesz += alloc * bed->s->sizeof_phdr;
4410 p->p_memsz += alloc * bed->s->sizeof_phdr;
4411 if (m->count)
4413 p->p_filesz += header_pad;
4414 p->p_memsz += header_pad;
4418 if (p->p_type == PT_LOAD
4419 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4421 if (!m->includes_filehdr && !m->includes_phdrs)
4422 p->p_offset = off;
4423 else
4425 file_ptr adjust;
4427 adjust = off - (p->p_offset + p->p_filesz);
4428 if (!no_contents)
4429 p->p_filesz += adjust;
4430 p->p_memsz += adjust;
4434 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4435 maps. Set filepos for sections in PT_LOAD segments, and in
4436 core files, for sections in PT_NOTE segments.
4437 assign_file_positions_for_non_load_sections will set filepos
4438 for other sections and update p_filesz for other segments. */
4439 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4441 asection *sec;
4442 bfd_size_type align;
4443 Elf_Internal_Shdr *this_hdr;
4445 sec = *secpp;
4446 this_hdr = &elf_section_data (sec)->this_hdr;
4447 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
4449 if ((p->p_type == PT_LOAD
4450 || p->p_type == PT_TLS)
4451 && (this_hdr->sh_type != SHT_NOBITS
4452 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
4453 && ((this_hdr->sh_flags & SHF_TLS) == 0
4454 || p->p_type == PT_TLS))))
4456 bfd_vma adjust = sec->lma - (p->p_paddr + p->p_memsz);
4458 if (sec->lma < p->p_paddr + p->p_memsz)
4460 (*_bfd_error_handler)
4461 (_("%B: section %A lma 0x%lx overlaps previous sections"),
4462 abfd, sec, (unsigned long) sec->lma);
4463 adjust = 0;
4464 sec->lma = p->p_paddr + p->p_memsz;
4466 p->p_memsz += adjust;
4468 if (this_hdr->sh_type != SHT_NOBITS)
4470 if (p->p_filesz + adjust < p->p_memsz)
4472 /* We have a PROGBITS section following NOBITS ones.
4473 Allocate file space for the NOBITS section(s) and
4474 zero it. */
4475 adjust = p->p_memsz - p->p_filesz;
4476 if (!write_zeros (abfd, off, adjust))
4477 return FALSE;
4479 off += adjust;
4480 p->p_filesz += adjust;
4484 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4486 /* The section at i == 0 is the one that actually contains
4487 everything. */
4488 if (i == 0)
4490 this_hdr->sh_offset = sec->filepos = off;
4491 off += this_hdr->sh_size;
4492 p->p_filesz = this_hdr->sh_size;
4493 p->p_memsz = 0;
4494 p->p_align = 1;
4496 else
4498 /* The rest are fake sections that shouldn't be written. */
4499 sec->filepos = 0;
4500 sec->size = 0;
4501 sec->flags = 0;
4502 continue;
4505 else
4507 if (p->p_type == PT_LOAD)
4509 this_hdr->sh_offset = sec->filepos = off;
4510 if (this_hdr->sh_type != SHT_NOBITS)
4511 off += this_hdr->sh_size;
4514 if (this_hdr->sh_type != SHT_NOBITS)
4516 p->p_filesz += this_hdr->sh_size;
4517 /* A load section without SHF_ALLOC is something like
4518 a note section in a PT_NOTE segment. These take
4519 file space but are not loaded into memory. */
4520 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4521 p->p_memsz += this_hdr->sh_size;
4523 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4525 if (p->p_type == PT_TLS)
4526 p->p_memsz += this_hdr->sh_size;
4528 /* .tbss is special. It doesn't contribute to p_memsz of
4529 normal segments. */
4530 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
4531 p->p_memsz += this_hdr->sh_size;
4534 if (align > p->p_align
4535 && !m->p_align_valid
4536 && (p->p_type != PT_LOAD
4537 || (abfd->flags & D_PAGED) == 0))
4538 p->p_align = align;
4541 if (!m->p_flags_valid)
4543 p->p_flags |= PF_R;
4544 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
4545 p->p_flags |= PF_X;
4546 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
4547 p->p_flags |= PF_W;
4550 off -= off_adjust;
4552 /* Check that all sections are in a PT_LOAD segment.
4553 Don't check funky gdb generated core files. */
4554 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
4556 bfd_boolean check_vma = TRUE;
4558 for (i = 1; i < m->count; i++)
4559 if (m->sections[i]->vma == m->sections[i - 1]->vma
4560 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
4561 ->this_hdr), p) != 0
4562 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
4563 ->this_hdr), p) != 0)
4565 /* Looks like we have overlays packed into the segment. */
4566 check_vma = FALSE;
4567 break;
4570 for (i = 0; i < m->count; i++)
4572 Elf_Internal_Shdr *this_hdr;
4573 asection *sec;
4575 sec = m->sections[i];
4576 this_hdr = &(elf_section_data(sec)->this_hdr);
4577 if (this_hdr->sh_size != 0
4578 && !ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma))
4580 (*_bfd_error_handler)
4581 (_("%B: section `%A' can't be allocated in segment %d"),
4582 abfd, sec, j);
4583 print_segment_map (m);
4589 elf_tdata (abfd)->next_file_pos = off;
4590 return TRUE;
4593 /* Assign file positions for the other sections. */
4595 static bfd_boolean
4596 assign_file_positions_for_non_load_sections (bfd *abfd,
4597 struct bfd_link_info *link_info)
4599 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4600 Elf_Internal_Shdr **i_shdrpp;
4601 Elf_Internal_Shdr **hdrpp;
4602 Elf_Internal_Phdr *phdrs;
4603 Elf_Internal_Phdr *p;
4604 struct elf_segment_map *m;
4605 bfd_vma filehdr_vaddr, filehdr_paddr;
4606 bfd_vma phdrs_vaddr, phdrs_paddr;
4607 file_ptr off;
4608 unsigned int num_sec;
4609 unsigned int i;
4610 unsigned int count;
4612 i_shdrpp = elf_elfsections (abfd);
4613 num_sec = elf_numsections (abfd);
4614 off = elf_tdata (abfd)->next_file_pos;
4615 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4617 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4618 Elf_Internal_Shdr *hdr;
4620 hdr = *hdrpp;
4621 if (hdr->bfd_section != NULL
4622 && (hdr->bfd_section->filepos != 0
4623 || (hdr->sh_type == SHT_NOBITS
4624 && hdr->contents == NULL)))
4625 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
4626 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4628 if (hdr->sh_size != 0)
4629 ((*_bfd_error_handler)
4630 (_("%B: warning: allocated section `%s' not in segment"),
4631 abfd,
4632 (hdr->bfd_section == NULL
4633 ? "*unknown*"
4634 : hdr->bfd_section->name)));
4635 /* We don't need to page align empty sections. */
4636 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
4637 off += vma_page_aligned_bias (hdr->sh_addr, off,
4638 bed->maxpagesize);
4639 else
4640 off += vma_page_aligned_bias (hdr->sh_addr, off,
4641 hdr->sh_addralign);
4642 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4643 FALSE);
4645 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4646 && hdr->bfd_section == NULL)
4647 || hdr == i_shdrpp[tdata->symtab_section]
4648 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4649 || hdr == i_shdrpp[tdata->strtab_section])
4650 hdr->sh_offset = -1;
4651 else
4652 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4655 /* Now that we have set the section file positions, we can set up
4656 the file positions for the non PT_LOAD segments. */
4657 count = 0;
4658 filehdr_vaddr = 0;
4659 filehdr_paddr = 0;
4660 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
4661 phdrs_paddr = 0;
4662 phdrs = elf_tdata (abfd)->phdr;
4663 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4664 m != NULL;
4665 m = m->next, p++)
4667 ++count;
4668 if (p->p_type != PT_LOAD)
4669 continue;
4671 if (m->includes_filehdr)
4673 filehdr_vaddr = p->p_vaddr;
4674 filehdr_paddr = p->p_paddr;
4676 if (m->includes_phdrs)
4678 phdrs_vaddr = p->p_vaddr;
4679 phdrs_paddr = p->p_paddr;
4680 if (m->includes_filehdr)
4682 phdrs_vaddr += bed->s->sizeof_ehdr;
4683 phdrs_paddr += bed->s->sizeof_ehdr;
4688 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4689 m != NULL;
4690 m = m->next, p++)
4692 if (p->p_type == PT_GNU_RELRO)
4694 const Elf_Internal_Phdr *lp;
4696 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
4698 if (link_info != NULL)
4700 /* During linking the range of the RELRO segment is passed
4701 in link_info. */
4702 for (lp = phdrs; lp < phdrs + count; ++lp)
4704 if (lp->p_type == PT_LOAD
4705 && lp->p_vaddr >= link_info->relro_start
4706 && lp->p_vaddr < link_info->relro_end
4707 && lp->p_vaddr + lp->p_filesz >= link_info->relro_end)
4708 break;
4711 else
4713 /* Otherwise we are copying an executable or shared
4714 library, but we need to use the same linker logic. */
4715 for (lp = phdrs; lp < phdrs + count; ++lp)
4717 if (lp->p_type == PT_LOAD
4718 && lp->p_paddr == p->p_paddr)
4719 break;
4723 if (lp < phdrs + count)
4725 p->p_vaddr = lp->p_vaddr;
4726 p->p_paddr = lp->p_paddr;
4727 p->p_offset = lp->p_offset;
4728 if (link_info != NULL)
4729 p->p_filesz = link_info->relro_end - lp->p_vaddr;
4730 else if (m->p_size_valid)
4731 p->p_filesz = m->p_size;
4732 else
4733 abort ();
4734 p->p_memsz = p->p_filesz;
4735 p->p_align = 1;
4736 p->p_flags = (lp->p_flags & ~PF_W);
4738 else
4740 memset (p, 0, sizeof *p);
4741 p->p_type = PT_NULL;
4744 else if (m->count != 0)
4746 if (p->p_type != PT_LOAD
4747 && (p->p_type != PT_NOTE
4748 || bfd_get_format (abfd) != bfd_core))
4750 Elf_Internal_Shdr *hdr;
4751 asection *sect;
4753 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
4755 sect = m->sections[m->count - 1];
4756 hdr = &elf_section_data (sect)->this_hdr;
4757 p->p_filesz = sect->filepos - m->sections[0]->filepos;
4758 if (hdr->sh_type != SHT_NOBITS)
4759 p->p_filesz += hdr->sh_size;
4760 p->p_offset = m->sections[0]->filepos;
4763 else if (m->includes_filehdr)
4765 p->p_vaddr = filehdr_vaddr;
4766 if (! m->p_paddr_valid)
4767 p->p_paddr = filehdr_paddr;
4769 else if (m->includes_phdrs)
4771 p->p_vaddr = phdrs_vaddr;
4772 if (! m->p_paddr_valid)
4773 p->p_paddr = phdrs_paddr;
4777 elf_tdata (abfd)->next_file_pos = off;
4779 return TRUE;
4782 /* Work out the file positions of all the sections. This is called by
4783 _bfd_elf_compute_section_file_positions. All the section sizes and
4784 VMAs must be known before this is called.
4786 Reloc sections come in two flavours: Those processed specially as
4787 "side-channel" data attached to a section to which they apply, and
4788 those that bfd doesn't process as relocations. The latter sort are
4789 stored in a normal bfd section by bfd_section_from_shdr. We don't
4790 consider the former sort here, unless they form part of the loadable
4791 image. Reloc sections not assigned here will be handled later by
4792 assign_file_positions_for_relocs.
4794 We also don't set the positions of the .symtab and .strtab here. */
4796 static bfd_boolean
4797 assign_file_positions_except_relocs (bfd *abfd,
4798 struct bfd_link_info *link_info)
4800 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4801 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4802 file_ptr off;
4803 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4805 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4806 && bfd_get_format (abfd) != bfd_core)
4808 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4809 unsigned int num_sec = elf_numsections (abfd);
4810 Elf_Internal_Shdr **hdrpp;
4811 unsigned int i;
4813 /* Start after the ELF header. */
4814 off = i_ehdrp->e_ehsize;
4816 /* We are not creating an executable, which means that we are
4817 not creating a program header, and that the actual order of
4818 the sections in the file is unimportant. */
4819 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4821 Elf_Internal_Shdr *hdr;
4823 hdr = *hdrpp;
4824 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4825 && hdr->bfd_section == NULL)
4826 || i == tdata->symtab_section
4827 || i == tdata->symtab_shndx_section
4828 || i == tdata->strtab_section)
4830 hdr->sh_offset = -1;
4832 else
4833 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4836 else
4838 unsigned int alloc;
4840 /* Assign file positions for the loaded sections based on the
4841 assignment of sections to segments. */
4842 if (!assign_file_positions_for_load_sections (abfd, link_info))
4843 return FALSE;
4845 /* And for non-load sections. */
4846 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
4847 return FALSE;
4849 if (bed->elf_backend_modify_program_headers != NULL)
4851 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
4852 return FALSE;
4855 /* Write out the program headers. */
4856 alloc = tdata->program_header_size / bed->s->sizeof_phdr;
4857 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4858 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
4859 return FALSE;
4861 off = tdata->next_file_pos;
4864 /* Place the section headers. */
4865 off = align_file_position (off, 1 << bed->s->log_file_align);
4866 i_ehdrp->e_shoff = off;
4867 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4869 tdata->next_file_pos = off;
4871 return TRUE;
4874 static bfd_boolean
4875 prep_headers (bfd *abfd)
4877 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
4878 struct elf_strtab_hash *shstrtab;
4879 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4881 i_ehdrp = elf_elfheader (abfd);
4883 shstrtab = _bfd_elf_strtab_init ();
4884 if (shstrtab == NULL)
4885 return FALSE;
4887 elf_shstrtab (abfd) = shstrtab;
4889 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4890 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4891 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4892 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4894 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4895 i_ehdrp->e_ident[EI_DATA] =
4896 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4897 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4899 if ((abfd->flags & DYNAMIC) != 0)
4900 i_ehdrp->e_type = ET_DYN;
4901 else if ((abfd->flags & EXEC_P) != 0)
4902 i_ehdrp->e_type = ET_EXEC;
4903 else if (bfd_get_format (abfd) == bfd_core)
4904 i_ehdrp->e_type = ET_CORE;
4905 else
4906 i_ehdrp->e_type = ET_REL;
4908 switch (bfd_get_arch (abfd))
4910 case bfd_arch_unknown:
4911 i_ehdrp->e_machine = EM_NONE;
4912 break;
4914 /* There used to be a long list of cases here, each one setting
4915 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4916 in the corresponding bfd definition. To avoid duplication,
4917 the switch was removed. Machines that need special handling
4918 can generally do it in elf_backend_final_write_processing(),
4919 unless they need the information earlier than the final write.
4920 Such need can generally be supplied by replacing the tests for
4921 e_machine with the conditions used to determine it. */
4922 default:
4923 i_ehdrp->e_machine = bed->elf_machine_code;
4926 i_ehdrp->e_version = bed->s->ev_current;
4927 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4929 /* No program header, for now. */
4930 i_ehdrp->e_phoff = 0;
4931 i_ehdrp->e_phentsize = 0;
4932 i_ehdrp->e_phnum = 0;
4934 /* Each bfd section is section header entry. */
4935 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4936 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4938 /* If we're building an executable, we'll need a program header table. */
4939 if (abfd->flags & EXEC_P)
4940 /* It all happens later. */
4942 else
4944 i_ehdrp->e_phentsize = 0;
4945 i_ehdrp->e_phoff = 0;
4948 elf_tdata (abfd)->symtab_hdr.sh_name =
4949 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
4950 elf_tdata (abfd)->strtab_hdr.sh_name =
4951 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
4952 elf_tdata (abfd)->shstrtab_hdr.sh_name =
4953 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
4954 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4955 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4956 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4957 return FALSE;
4959 return TRUE;
4962 /* Assign file positions for all the reloc sections which are not part
4963 of the loadable file image. */
4965 void
4966 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
4968 file_ptr off;
4969 unsigned int i, num_sec;
4970 Elf_Internal_Shdr **shdrpp;
4972 off = elf_tdata (abfd)->next_file_pos;
4974 num_sec = elf_numsections (abfd);
4975 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
4977 Elf_Internal_Shdr *shdrp;
4979 shdrp = *shdrpp;
4980 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4981 && shdrp->sh_offset == -1)
4982 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
4985 elf_tdata (abfd)->next_file_pos = off;
4988 bfd_boolean
4989 _bfd_elf_write_object_contents (bfd *abfd)
4991 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4992 Elf_Internal_Shdr **i_shdrp;
4993 bfd_boolean failed;
4994 unsigned int count, num_sec;
4996 if (! abfd->output_has_begun
4997 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
4998 return FALSE;
5000 i_shdrp = elf_elfsections (abfd);
5002 failed = FALSE;
5003 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
5004 if (failed)
5005 return FALSE;
5007 _bfd_elf_assign_file_positions_for_relocs (abfd);
5009 /* After writing the headers, we need to write the sections too... */
5010 num_sec = elf_numsections (abfd);
5011 for (count = 1; count < num_sec; count++)
5013 if (bed->elf_backend_section_processing)
5014 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
5015 if (i_shdrp[count]->contents)
5017 bfd_size_type amt = i_shdrp[count]->sh_size;
5019 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
5020 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
5021 return FALSE;
5025 /* Write out the section header names. */
5026 if (elf_shstrtab (abfd) != NULL
5027 && (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
5028 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
5029 return FALSE;
5031 if (bed->elf_backend_final_write_processing)
5032 (*bed->elf_backend_final_write_processing) (abfd,
5033 elf_tdata (abfd)->linker);
5035 if (!bed->s->write_shdrs_and_ehdr (abfd))
5036 return FALSE;
5038 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
5039 if (elf_tdata (abfd)->after_write_object_contents)
5040 return (*elf_tdata (abfd)->after_write_object_contents) (abfd);
5042 return TRUE;
5045 bfd_boolean
5046 _bfd_elf_write_corefile_contents (bfd *abfd)
5048 /* Hopefully this can be done just like an object file. */
5049 return _bfd_elf_write_object_contents (abfd);
5052 /* Given a section, search the header to find them. */
5054 unsigned int
5055 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
5057 const struct elf_backend_data *bed;
5058 unsigned int sec_index;
5060 if (elf_section_data (asect) != NULL
5061 && elf_section_data (asect)->this_idx != 0)
5062 return elf_section_data (asect)->this_idx;
5064 if (bfd_is_abs_section (asect))
5065 sec_index = SHN_ABS;
5066 else if (bfd_is_com_section (asect))
5067 sec_index = SHN_COMMON;
5068 else if (bfd_is_und_section (asect))
5069 sec_index = SHN_UNDEF;
5070 else
5071 sec_index = SHN_BAD;
5073 bed = get_elf_backend_data (abfd);
5074 if (bed->elf_backend_section_from_bfd_section)
5076 int retval = sec_index;
5078 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
5079 return retval;
5082 if (sec_index == SHN_BAD)
5083 bfd_set_error (bfd_error_nonrepresentable_section);
5085 return sec_index;
5088 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5089 on error. */
5092 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
5094 asymbol *asym_ptr = *asym_ptr_ptr;
5095 int idx;
5096 flagword flags = asym_ptr->flags;
5098 /* When gas creates relocations against local labels, it creates its
5099 own symbol for the section, but does put the symbol into the
5100 symbol chain, so udata is 0. When the linker is generating
5101 relocatable output, this section symbol may be for one of the
5102 input sections rather than the output section. */
5103 if (asym_ptr->udata.i == 0
5104 && (flags & BSF_SECTION_SYM)
5105 && asym_ptr->section)
5107 asection *sec;
5108 int indx;
5110 sec = asym_ptr->section;
5111 if (sec->owner != abfd && sec->output_section != NULL)
5112 sec = sec->output_section;
5113 if (sec->owner == abfd
5114 && (indx = sec->index) < elf_num_section_syms (abfd)
5115 && elf_section_syms (abfd)[indx] != NULL)
5116 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
5119 idx = asym_ptr->udata.i;
5121 if (idx == 0)
5123 /* This case can occur when using --strip-symbol on a symbol
5124 which is used in a relocation entry. */
5125 (*_bfd_error_handler)
5126 (_("%B: symbol `%s' required but not present"),
5127 abfd, bfd_asymbol_name (asym_ptr));
5128 bfd_set_error (bfd_error_no_symbols);
5129 return -1;
5132 #if DEBUG & 4
5134 fprintf (stderr,
5135 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
5136 (long) asym_ptr, asym_ptr->name, idx, flags,
5137 elf_symbol_flags (flags));
5138 fflush (stderr);
5140 #endif
5142 return idx;
5145 /* Rewrite program header information. */
5147 static bfd_boolean
5148 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
5150 Elf_Internal_Ehdr *iehdr;
5151 struct elf_segment_map *map;
5152 struct elf_segment_map *map_first;
5153 struct elf_segment_map **pointer_to_map;
5154 Elf_Internal_Phdr *segment;
5155 asection *section;
5156 unsigned int i;
5157 unsigned int num_segments;
5158 bfd_boolean phdr_included = FALSE;
5159 bfd_boolean p_paddr_valid;
5160 bfd_vma maxpagesize;
5161 struct elf_segment_map *phdr_adjust_seg = NULL;
5162 unsigned int phdr_adjust_num = 0;
5163 const struct elf_backend_data *bed;
5165 bed = get_elf_backend_data (ibfd);
5166 iehdr = elf_elfheader (ibfd);
5168 map_first = NULL;
5169 pointer_to_map = &map_first;
5171 num_segments = elf_elfheader (ibfd)->e_phnum;
5172 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
5174 /* Returns the end address of the segment + 1. */
5175 #define SEGMENT_END(segment, start) \
5176 (start + (segment->p_memsz > segment->p_filesz \
5177 ? segment->p_memsz : segment->p_filesz))
5179 #define SECTION_SIZE(section, segment) \
5180 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5181 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5182 ? section->size : 0)
5184 /* Returns TRUE if the given section is contained within
5185 the given segment. VMA addresses are compared. */
5186 #define IS_CONTAINED_BY_VMA(section, segment) \
5187 (section->vma >= segment->p_vaddr \
5188 && (section->vma + SECTION_SIZE (section, segment) \
5189 <= (SEGMENT_END (segment, segment->p_vaddr))))
5191 /* Returns TRUE if the given section is contained within
5192 the given segment. LMA addresses are compared. */
5193 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5194 (section->lma >= base \
5195 && (section->lma + SECTION_SIZE (section, segment) \
5196 <= SEGMENT_END (segment, base)))
5198 /* Handle PT_NOTE segment. */
5199 #define IS_NOTE(p, s) \
5200 (p->p_type == PT_NOTE \
5201 && elf_section_type (s) == SHT_NOTE \
5202 && (bfd_vma) s->filepos >= p->p_offset \
5203 && ((bfd_vma) s->filepos + s->size \
5204 <= p->p_offset + p->p_filesz))
5206 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5207 etc. */
5208 #define IS_COREFILE_NOTE(p, s) \
5209 (IS_NOTE (p, s) \
5210 && bfd_get_format (ibfd) == bfd_core \
5211 && s->vma == 0 \
5212 && s->lma == 0)
5214 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5215 linker, which generates a PT_INTERP section with p_vaddr and
5216 p_memsz set to 0. */
5217 #define IS_SOLARIS_PT_INTERP(p, s) \
5218 (p->p_vaddr == 0 \
5219 && p->p_paddr == 0 \
5220 && p->p_memsz == 0 \
5221 && p->p_filesz > 0 \
5222 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5223 && s->size > 0 \
5224 && (bfd_vma) s->filepos >= p->p_offset \
5225 && ((bfd_vma) s->filepos + s->size \
5226 <= p->p_offset + p->p_filesz))
5228 /* Decide if the given section should be included in the given segment.
5229 A section will be included if:
5230 1. It is within the address space of the segment -- we use the LMA
5231 if that is set for the segment and the VMA otherwise,
5232 2. It is an allocated section or a NOTE section in a PT_NOTE
5233 segment.
5234 3. There is an output section associated with it,
5235 4. The section has not already been allocated to a previous segment.
5236 5. PT_GNU_STACK segments do not include any sections.
5237 6. PT_TLS segment includes only SHF_TLS sections.
5238 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5239 8. PT_DYNAMIC should not contain empty sections at the beginning
5240 (with the possible exception of .dynamic). */
5241 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5242 ((((segment->p_paddr \
5243 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5244 : IS_CONTAINED_BY_VMA (section, segment)) \
5245 && (section->flags & SEC_ALLOC) != 0) \
5246 || IS_NOTE (segment, section)) \
5247 && segment->p_type != PT_GNU_STACK \
5248 && (segment->p_type != PT_TLS \
5249 || (section->flags & SEC_THREAD_LOCAL)) \
5250 && (segment->p_type == PT_LOAD \
5251 || segment->p_type == PT_TLS \
5252 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5253 && (segment->p_type != PT_DYNAMIC \
5254 || SECTION_SIZE (section, segment) > 0 \
5255 || (segment->p_paddr \
5256 ? segment->p_paddr != section->lma \
5257 : segment->p_vaddr != section->vma) \
5258 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5259 == 0)) \
5260 && !section->segment_mark)
5262 /* If the output section of a section in the input segment is NULL,
5263 it is removed from the corresponding output segment. */
5264 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5265 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5266 && section->output_section != NULL)
5268 /* Returns TRUE iff seg1 starts after the end of seg2. */
5269 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5270 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5272 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5273 their VMA address ranges and their LMA address ranges overlap.
5274 It is possible to have overlapping VMA ranges without overlapping LMA
5275 ranges. RedBoot images for example can have both .data and .bss mapped
5276 to the same VMA range, but with the .data section mapped to a different
5277 LMA. */
5278 #define SEGMENT_OVERLAPS(seg1, seg2) \
5279 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5280 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5281 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5282 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5284 /* Initialise the segment mark field. */
5285 for (section = ibfd->sections; section != NULL; section = section->next)
5286 section->segment_mark = FALSE;
5288 /* The Solaris linker creates program headers in which all the
5289 p_paddr fields are zero. When we try to objcopy or strip such a
5290 file, we get confused. Check for this case, and if we find it
5291 don't set the p_paddr_valid fields. */
5292 p_paddr_valid = FALSE;
5293 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5294 i < num_segments;
5295 i++, segment++)
5296 if (segment->p_paddr != 0)
5298 p_paddr_valid = TRUE;
5299 break;
5302 /* Scan through the segments specified in the program header
5303 of the input BFD. For this first scan we look for overlaps
5304 in the loadable segments. These can be created by weird
5305 parameters to objcopy. Also, fix some solaris weirdness. */
5306 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5307 i < num_segments;
5308 i++, segment++)
5310 unsigned int j;
5311 Elf_Internal_Phdr *segment2;
5313 if (segment->p_type == PT_INTERP)
5314 for (section = ibfd->sections; section; section = section->next)
5315 if (IS_SOLARIS_PT_INTERP (segment, section))
5317 /* Mininal change so that the normal section to segment
5318 assignment code will work. */
5319 segment->p_vaddr = section->vma;
5320 break;
5323 if (segment->p_type != PT_LOAD)
5325 /* Remove PT_GNU_RELRO segment. */
5326 if (segment->p_type == PT_GNU_RELRO)
5327 segment->p_type = PT_NULL;
5328 continue;
5331 /* Determine if this segment overlaps any previous segments. */
5332 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
5334 bfd_signed_vma extra_length;
5336 if (segment2->p_type != PT_LOAD
5337 || !SEGMENT_OVERLAPS (segment, segment2))
5338 continue;
5340 /* Merge the two segments together. */
5341 if (segment2->p_vaddr < segment->p_vaddr)
5343 /* Extend SEGMENT2 to include SEGMENT and then delete
5344 SEGMENT. */
5345 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
5346 - SEGMENT_END (segment2, segment2->p_vaddr));
5348 if (extra_length > 0)
5350 segment2->p_memsz += extra_length;
5351 segment2->p_filesz += extra_length;
5354 segment->p_type = PT_NULL;
5356 /* Since we have deleted P we must restart the outer loop. */
5357 i = 0;
5358 segment = elf_tdata (ibfd)->phdr;
5359 break;
5361 else
5363 /* Extend SEGMENT to include SEGMENT2 and then delete
5364 SEGMENT2. */
5365 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
5366 - SEGMENT_END (segment, segment->p_vaddr));
5368 if (extra_length > 0)
5370 segment->p_memsz += extra_length;
5371 segment->p_filesz += extra_length;
5374 segment2->p_type = PT_NULL;
5379 /* The second scan attempts to assign sections to segments. */
5380 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5381 i < num_segments;
5382 i++, segment++)
5384 unsigned int section_count;
5385 asection **sections;
5386 asection *output_section;
5387 unsigned int isec;
5388 bfd_vma matching_lma;
5389 bfd_vma suggested_lma;
5390 unsigned int j;
5391 bfd_size_type amt;
5392 asection *first_section;
5393 bfd_boolean first_matching_lma;
5394 bfd_boolean first_suggested_lma;
5396 if (segment->p_type == PT_NULL)
5397 continue;
5399 first_section = NULL;
5400 /* Compute how many sections might be placed into this segment. */
5401 for (section = ibfd->sections, section_count = 0;
5402 section != NULL;
5403 section = section->next)
5405 /* Find the first section in the input segment, which may be
5406 removed from the corresponding output segment. */
5407 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
5409 if (first_section == NULL)
5410 first_section = section;
5411 if (section->output_section != NULL)
5412 ++section_count;
5416 /* Allocate a segment map big enough to contain
5417 all of the sections we have selected. */
5418 amt = sizeof (struct elf_segment_map);
5419 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5420 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
5421 if (map == NULL)
5422 return FALSE;
5424 /* Initialise the fields of the segment map. Default to
5425 using the physical address of the segment in the input BFD. */
5426 map->next = NULL;
5427 map->p_type = segment->p_type;
5428 map->p_flags = segment->p_flags;
5429 map->p_flags_valid = 1;
5431 /* If the first section in the input segment is removed, there is
5432 no need to preserve segment physical address in the corresponding
5433 output segment. */
5434 if (!first_section || first_section->output_section != NULL)
5436 map->p_paddr = segment->p_paddr;
5437 map->p_paddr_valid = p_paddr_valid;
5440 /* Determine if this segment contains the ELF file header
5441 and if it contains the program headers themselves. */
5442 map->includes_filehdr = (segment->p_offset == 0
5443 && segment->p_filesz >= iehdr->e_ehsize);
5444 map->includes_phdrs = 0;
5446 if (!phdr_included || segment->p_type != PT_LOAD)
5448 map->includes_phdrs =
5449 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5450 && (segment->p_offset + segment->p_filesz
5451 >= ((bfd_vma) iehdr->e_phoff
5452 + iehdr->e_phnum * iehdr->e_phentsize)));
5454 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5455 phdr_included = TRUE;
5458 if (section_count == 0)
5460 /* Special segments, such as the PT_PHDR segment, may contain
5461 no sections, but ordinary, loadable segments should contain
5462 something. They are allowed by the ELF spec however, so only
5463 a warning is produced. */
5464 if (segment->p_type == PT_LOAD)
5465 (*_bfd_error_handler) (_("%B: warning: Empty loadable segment"
5466 " detected, is this intentional ?\n"),
5467 ibfd);
5469 map->count = 0;
5470 *pointer_to_map = map;
5471 pointer_to_map = &map->next;
5473 continue;
5476 /* Now scan the sections in the input BFD again and attempt
5477 to add their corresponding output sections to the segment map.
5478 The problem here is how to handle an output section which has
5479 been moved (ie had its LMA changed). There are four possibilities:
5481 1. None of the sections have been moved.
5482 In this case we can continue to use the segment LMA from the
5483 input BFD.
5485 2. All of the sections have been moved by the same amount.
5486 In this case we can change the segment's LMA to match the LMA
5487 of the first section.
5489 3. Some of the sections have been moved, others have not.
5490 In this case those sections which have not been moved can be
5491 placed in the current segment which will have to have its size,
5492 and possibly its LMA changed, and a new segment or segments will
5493 have to be created to contain the other sections.
5495 4. The sections have been moved, but not by the same amount.
5496 In this case we can change the segment's LMA to match the LMA
5497 of the first section and we will have to create a new segment
5498 or segments to contain the other sections.
5500 In order to save time, we allocate an array to hold the section
5501 pointers that we are interested in. As these sections get assigned
5502 to a segment, they are removed from this array. */
5504 sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
5505 if (sections == NULL)
5506 return FALSE;
5508 /* Step One: Scan for segment vs section LMA conflicts.
5509 Also add the sections to the section array allocated above.
5510 Also add the sections to the current segment. In the common
5511 case, where the sections have not been moved, this means that
5512 we have completely filled the segment, and there is nothing
5513 more to do. */
5514 isec = 0;
5515 matching_lma = 0;
5516 suggested_lma = 0;
5517 first_matching_lma = TRUE;
5518 first_suggested_lma = TRUE;
5520 for (section = ibfd->sections;
5521 section != NULL;
5522 section = section->next)
5523 if (section == first_section)
5524 break;
5526 for (j = 0; section != NULL; section = section->next)
5528 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5530 output_section = section->output_section;
5532 sections[j++] = section;
5534 /* The Solaris native linker always sets p_paddr to 0.
5535 We try to catch that case here, and set it to the
5536 correct value. Note - some backends require that
5537 p_paddr be left as zero. */
5538 if (!p_paddr_valid
5539 && segment->p_vaddr != 0
5540 && !bed->want_p_paddr_set_to_zero
5541 && isec == 0
5542 && output_section->lma != 0
5543 && output_section->vma == (segment->p_vaddr
5544 + (map->includes_filehdr
5545 ? iehdr->e_ehsize
5546 : 0)
5547 + (map->includes_phdrs
5548 ? (iehdr->e_phnum
5549 * iehdr->e_phentsize)
5550 : 0)))
5551 map->p_paddr = segment->p_vaddr;
5553 /* Match up the physical address of the segment with the
5554 LMA address of the output section. */
5555 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5556 || IS_COREFILE_NOTE (segment, section)
5557 || (bed->want_p_paddr_set_to_zero
5558 && IS_CONTAINED_BY_VMA (output_section, segment)))
5560 if (first_matching_lma || output_section->lma < matching_lma)
5562 matching_lma = output_section->lma;
5563 first_matching_lma = FALSE;
5566 /* We assume that if the section fits within the segment
5567 then it does not overlap any other section within that
5568 segment. */
5569 map->sections[isec++] = output_section;
5571 else if (first_suggested_lma)
5573 suggested_lma = output_section->lma;
5574 first_suggested_lma = FALSE;
5577 if (j == section_count)
5578 break;
5582 BFD_ASSERT (j == section_count);
5584 /* Step Two: Adjust the physical address of the current segment,
5585 if necessary. */
5586 if (isec == section_count)
5588 /* All of the sections fitted within the segment as currently
5589 specified. This is the default case. Add the segment to
5590 the list of built segments and carry on to process the next
5591 program header in the input BFD. */
5592 map->count = section_count;
5593 *pointer_to_map = map;
5594 pointer_to_map = &map->next;
5596 if (p_paddr_valid
5597 && !bed->want_p_paddr_set_to_zero
5598 && matching_lma != map->p_paddr
5599 && !map->includes_filehdr
5600 && !map->includes_phdrs)
5601 /* There is some padding before the first section in the
5602 segment. So, we must account for that in the output
5603 segment's vma. */
5604 map->p_vaddr_offset = matching_lma - map->p_paddr;
5606 free (sections);
5607 continue;
5609 else
5611 if (!first_matching_lma)
5613 /* At least one section fits inside the current segment.
5614 Keep it, but modify its physical address to match the
5615 LMA of the first section that fitted. */
5616 map->p_paddr = matching_lma;
5618 else
5620 /* None of the sections fitted inside the current segment.
5621 Change the current segment's physical address to match
5622 the LMA of the first section. */
5623 map->p_paddr = suggested_lma;
5626 /* Offset the segment physical address from the lma
5627 to allow for space taken up by elf headers. */
5628 if (map->includes_filehdr)
5630 if (map->p_paddr >= iehdr->e_ehsize)
5631 map->p_paddr -= iehdr->e_ehsize;
5632 else
5634 map->includes_filehdr = FALSE;
5635 map->includes_phdrs = FALSE;
5639 if (map->includes_phdrs)
5641 if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize)
5643 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5645 /* iehdr->e_phnum is just an estimate of the number
5646 of program headers that we will need. Make a note
5647 here of the number we used and the segment we chose
5648 to hold these headers, so that we can adjust the
5649 offset when we know the correct value. */
5650 phdr_adjust_num = iehdr->e_phnum;
5651 phdr_adjust_seg = map;
5653 else
5654 map->includes_phdrs = FALSE;
5658 /* Step Three: Loop over the sections again, this time assigning
5659 those that fit to the current segment and removing them from the
5660 sections array; but making sure not to leave large gaps. Once all
5661 possible sections have been assigned to the current segment it is
5662 added to the list of built segments and if sections still remain
5663 to be assigned, a new segment is constructed before repeating
5664 the loop. */
5665 isec = 0;
5668 map->count = 0;
5669 suggested_lma = 0;
5670 first_suggested_lma = TRUE;
5672 /* Fill the current segment with sections that fit. */
5673 for (j = 0; j < section_count; j++)
5675 section = sections[j];
5677 if (section == NULL)
5678 continue;
5680 output_section = section->output_section;
5682 BFD_ASSERT (output_section != NULL);
5684 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5685 || IS_COREFILE_NOTE (segment, section))
5687 if (map->count == 0)
5689 /* If the first section in a segment does not start at
5690 the beginning of the segment, then something is
5691 wrong. */
5692 if (output_section->lma
5693 != (map->p_paddr
5694 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5695 + (map->includes_phdrs
5696 ? iehdr->e_phnum * iehdr->e_phentsize
5697 : 0)))
5698 abort ();
5700 else
5702 asection *prev_sec;
5704 prev_sec = map->sections[map->count - 1];
5706 /* If the gap between the end of the previous section
5707 and the start of this section is more than
5708 maxpagesize then we need to start a new segment. */
5709 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
5710 maxpagesize)
5711 < BFD_ALIGN (output_section->lma, maxpagesize))
5712 || (prev_sec->lma + prev_sec->size
5713 > output_section->lma))
5715 if (first_suggested_lma)
5717 suggested_lma = output_section->lma;
5718 first_suggested_lma = FALSE;
5721 continue;
5725 map->sections[map->count++] = output_section;
5726 ++isec;
5727 sections[j] = NULL;
5728 section->segment_mark = TRUE;
5730 else if (first_suggested_lma)
5732 suggested_lma = output_section->lma;
5733 first_suggested_lma = FALSE;
5737 BFD_ASSERT (map->count > 0);
5739 /* Add the current segment to the list of built segments. */
5740 *pointer_to_map = map;
5741 pointer_to_map = &map->next;
5743 if (isec < section_count)
5745 /* We still have not allocated all of the sections to
5746 segments. Create a new segment here, initialise it
5747 and carry on looping. */
5748 amt = sizeof (struct elf_segment_map);
5749 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5750 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
5751 if (map == NULL)
5753 free (sections);
5754 return FALSE;
5757 /* Initialise the fields of the segment map. Set the physical
5758 physical address to the LMA of the first section that has
5759 not yet been assigned. */
5760 map->next = NULL;
5761 map->p_type = segment->p_type;
5762 map->p_flags = segment->p_flags;
5763 map->p_flags_valid = 1;
5764 map->p_paddr = suggested_lma;
5765 map->p_paddr_valid = p_paddr_valid;
5766 map->includes_filehdr = 0;
5767 map->includes_phdrs = 0;
5770 while (isec < section_count);
5772 free (sections);
5775 elf_tdata (obfd)->segment_map = map_first;
5777 /* If we had to estimate the number of program headers that were
5778 going to be needed, then check our estimate now and adjust
5779 the offset if necessary. */
5780 if (phdr_adjust_seg != NULL)
5782 unsigned int count;
5784 for (count = 0, map = map_first; map != NULL; map = map->next)
5785 count++;
5787 if (count > phdr_adjust_num)
5788 phdr_adjust_seg->p_paddr
5789 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5792 #undef SEGMENT_END
5793 #undef SECTION_SIZE
5794 #undef IS_CONTAINED_BY_VMA
5795 #undef IS_CONTAINED_BY_LMA
5796 #undef IS_NOTE
5797 #undef IS_COREFILE_NOTE
5798 #undef IS_SOLARIS_PT_INTERP
5799 #undef IS_SECTION_IN_INPUT_SEGMENT
5800 #undef INCLUDE_SECTION_IN_SEGMENT
5801 #undef SEGMENT_AFTER_SEGMENT
5802 #undef SEGMENT_OVERLAPS
5803 return TRUE;
5806 /* Copy ELF program header information. */
5808 static bfd_boolean
5809 copy_elf_program_header (bfd *ibfd, bfd *obfd)
5811 Elf_Internal_Ehdr *iehdr;
5812 struct elf_segment_map *map;
5813 struct elf_segment_map *map_first;
5814 struct elf_segment_map **pointer_to_map;
5815 Elf_Internal_Phdr *segment;
5816 unsigned int i;
5817 unsigned int num_segments;
5818 bfd_boolean phdr_included = FALSE;
5819 bfd_boolean p_paddr_valid;
5821 iehdr = elf_elfheader (ibfd);
5823 map_first = NULL;
5824 pointer_to_map = &map_first;
5826 /* If all the segment p_paddr fields are zero, don't set
5827 map->p_paddr_valid. */
5828 p_paddr_valid = FALSE;
5829 num_segments = elf_elfheader (ibfd)->e_phnum;
5830 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5831 i < num_segments;
5832 i++, segment++)
5833 if (segment->p_paddr != 0)
5835 p_paddr_valid = TRUE;
5836 break;
5839 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5840 i < num_segments;
5841 i++, segment++)
5843 asection *section;
5844 unsigned int section_count;
5845 bfd_size_type amt;
5846 Elf_Internal_Shdr *this_hdr;
5847 asection *first_section = NULL;
5848 asection *lowest_section = NULL;
5850 /* Compute how many sections are in this segment. */
5851 for (section = ibfd->sections, section_count = 0;
5852 section != NULL;
5853 section = section->next)
5855 this_hdr = &(elf_section_data(section)->this_hdr);
5856 if (this_hdr->sh_size != 0
5857 && ELF_SECTION_IN_SEGMENT (this_hdr, segment))
5859 if (!first_section)
5860 first_section = lowest_section = section;
5861 if (section->lma < lowest_section->lma)
5862 lowest_section = section;
5863 section_count++;
5867 /* Allocate a segment map big enough to contain
5868 all of the sections we have selected. */
5869 amt = sizeof (struct elf_segment_map);
5870 if (section_count != 0)
5871 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5872 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
5873 if (map == NULL)
5874 return FALSE;
5876 /* Initialize the fields of the output segment map with the
5877 input segment. */
5878 map->next = NULL;
5879 map->p_type = segment->p_type;
5880 map->p_flags = segment->p_flags;
5881 map->p_flags_valid = 1;
5882 map->p_paddr = segment->p_paddr;
5883 map->p_paddr_valid = p_paddr_valid;
5884 map->p_align = segment->p_align;
5885 map->p_align_valid = 1;
5886 map->p_vaddr_offset = 0;
5888 if (map->p_type == PT_GNU_RELRO)
5890 /* The PT_GNU_RELRO segment may contain the first a few
5891 bytes in the .got.plt section even if the whole .got.plt
5892 section isn't in the PT_GNU_RELRO segment. We won't
5893 change the size of the PT_GNU_RELRO segment. */
5894 map->p_size = segment->p_memsz;
5895 map->p_size_valid = 1;
5898 /* Determine if this segment contains the ELF file header
5899 and if it contains the program headers themselves. */
5900 map->includes_filehdr = (segment->p_offset == 0
5901 && segment->p_filesz >= iehdr->e_ehsize);
5903 map->includes_phdrs = 0;
5904 if (! phdr_included || segment->p_type != PT_LOAD)
5906 map->includes_phdrs =
5907 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5908 && (segment->p_offset + segment->p_filesz
5909 >= ((bfd_vma) iehdr->e_phoff
5910 + iehdr->e_phnum * iehdr->e_phentsize)));
5912 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5913 phdr_included = TRUE;
5916 if (map->includes_filehdr && first_section)
5917 /* We need to keep the space used by the headers fixed. */
5918 map->header_size = first_section->vma - segment->p_vaddr;
5920 if (!map->includes_phdrs
5921 && !map->includes_filehdr
5922 && map->p_paddr_valid)
5923 /* There is some other padding before the first section. */
5924 map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
5925 - segment->p_paddr);
5927 if (section_count != 0)
5929 unsigned int isec = 0;
5931 for (section = first_section;
5932 section != NULL;
5933 section = section->next)
5935 this_hdr = &(elf_section_data(section)->this_hdr);
5936 if (this_hdr->sh_size != 0
5937 && ELF_SECTION_IN_SEGMENT (this_hdr, segment))
5939 map->sections[isec++] = section->output_section;
5940 if (isec == section_count)
5941 break;
5946 map->count = section_count;
5947 *pointer_to_map = map;
5948 pointer_to_map = &map->next;
5951 elf_tdata (obfd)->segment_map = map_first;
5952 return TRUE;
5955 /* Copy private BFD data. This copies or rewrites ELF program header
5956 information. */
5958 static bfd_boolean
5959 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
5961 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5962 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5963 return TRUE;
5965 if (elf_tdata (ibfd)->phdr == NULL)
5966 return TRUE;
5968 if (ibfd->xvec == obfd->xvec)
5970 /* Check to see if any sections in the input BFD
5971 covered by ELF program header have changed. */
5972 Elf_Internal_Phdr *segment;
5973 asection *section, *osec;
5974 unsigned int i, num_segments;
5975 Elf_Internal_Shdr *this_hdr;
5976 const struct elf_backend_data *bed;
5978 bed = get_elf_backend_data (ibfd);
5980 /* Regenerate the segment map if p_paddr is set to 0. */
5981 if (bed->want_p_paddr_set_to_zero)
5982 goto rewrite;
5984 /* Initialize the segment mark field. */
5985 for (section = obfd->sections; section != NULL;
5986 section = section->next)
5987 section->segment_mark = FALSE;
5989 num_segments = elf_elfheader (ibfd)->e_phnum;
5990 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5991 i < num_segments;
5992 i++, segment++)
5994 /* PR binutils/3535. The Solaris linker always sets the p_paddr
5995 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
5996 which severly confuses things, so always regenerate the segment
5997 map in this case. */
5998 if (segment->p_paddr == 0
5999 && segment->p_memsz == 0
6000 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
6001 goto rewrite;
6003 for (section = ibfd->sections;
6004 section != NULL; section = section->next)
6006 /* We mark the output section so that we know it comes
6007 from the input BFD. */
6008 osec = section->output_section;
6009 if (osec)
6010 osec->segment_mark = TRUE;
6012 /* Check if this section is covered by the segment. */
6013 this_hdr = &(elf_section_data(section)->this_hdr);
6014 if (this_hdr->sh_size != 0
6015 && ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6017 /* FIXME: Check if its output section is changed or
6018 removed. What else do we need to check? */
6019 if (osec == NULL
6020 || section->flags != osec->flags
6021 || section->lma != osec->lma
6022 || section->vma != osec->vma
6023 || section->size != osec->size
6024 || section->rawsize != osec->rawsize
6025 || section->alignment_power != osec->alignment_power)
6026 goto rewrite;
6031 /* Check to see if any output section do not come from the
6032 input BFD. */
6033 for (section = obfd->sections; section != NULL;
6034 section = section->next)
6036 if (section->segment_mark == FALSE)
6037 goto rewrite;
6038 else
6039 section->segment_mark = FALSE;
6042 return copy_elf_program_header (ibfd, obfd);
6045 rewrite:
6046 return rewrite_elf_program_header (ibfd, obfd);
6049 /* Initialize private output section information from input section. */
6051 bfd_boolean
6052 _bfd_elf_init_private_section_data (bfd *ibfd,
6053 asection *isec,
6054 bfd *obfd,
6055 asection *osec,
6056 struct bfd_link_info *link_info)
6059 Elf_Internal_Shdr *ihdr, *ohdr;
6060 bfd_boolean final_link = link_info != NULL && !link_info->relocatable;
6062 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6063 || obfd->xvec->flavour != bfd_target_elf_flavour)
6064 return TRUE;
6066 /* For objcopy and relocatable link, don't copy the output ELF
6067 section type from input if the output BFD section flags have been
6068 set to something different. For a final link allow some flags
6069 that the linker clears to differ. */
6070 if (elf_section_type (osec) == SHT_NULL
6071 && (osec->flags == isec->flags
6072 || (final_link
6073 && ((osec->flags ^ isec->flags)
6074 & ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES)) == 0)))
6075 elf_section_type (osec) = elf_section_type (isec);
6077 /* FIXME: Is this correct for all OS/PROC specific flags? */
6078 elf_section_flags (osec) |= (elf_section_flags (isec)
6079 & (SHF_MASKOS | SHF_MASKPROC));
6081 /* Set things up for objcopy and relocatable link. The output
6082 SHT_GROUP section will have its elf_next_in_group pointing back
6083 to the input group members. Ignore linker created group section.
6084 See elfNN_ia64_object_p in elfxx-ia64.c. */
6085 if (!final_link)
6087 if (elf_sec_group (isec) == NULL
6088 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
6090 if (elf_section_flags (isec) & SHF_GROUP)
6091 elf_section_flags (osec) |= SHF_GROUP;
6092 elf_next_in_group (osec) = elf_next_in_group (isec);
6093 elf_section_data (osec)->group = elf_section_data (isec)->group;
6097 ihdr = &elf_section_data (isec)->this_hdr;
6099 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6100 don't use the output section of the linked-to section since it
6101 may be NULL at this point. */
6102 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
6104 ohdr = &elf_section_data (osec)->this_hdr;
6105 ohdr->sh_flags |= SHF_LINK_ORDER;
6106 elf_linked_to_section (osec) = elf_linked_to_section (isec);
6109 osec->use_rela_p = isec->use_rela_p;
6111 return TRUE;
6114 /* Copy private section information. This copies over the entsize
6115 field, and sometimes the info field. */
6117 bfd_boolean
6118 _bfd_elf_copy_private_section_data (bfd *ibfd,
6119 asection *isec,
6120 bfd *obfd,
6121 asection *osec)
6123 Elf_Internal_Shdr *ihdr, *ohdr;
6125 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6126 || obfd->xvec->flavour != bfd_target_elf_flavour)
6127 return TRUE;
6129 ihdr = &elf_section_data (isec)->this_hdr;
6130 ohdr = &elf_section_data (osec)->this_hdr;
6132 ohdr->sh_entsize = ihdr->sh_entsize;
6134 if (ihdr->sh_type == SHT_SYMTAB
6135 || ihdr->sh_type == SHT_DYNSYM
6136 || ihdr->sh_type == SHT_GNU_verneed
6137 || ihdr->sh_type == SHT_GNU_verdef)
6138 ohdr->sh_info = ihdr->sh_info;
6140 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
6141 NULL);
6144 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
6145 necessary if we are removing either the SHT_GROUP section or any of
6146 the group member sections. DISCARDED is the value that a section's
6147 output_section has if the section will be discarded, NULL when this
6148 function is called from objcopy, bfd_abs_section_ptr when called
6149 from the linker. */
6151 bfd_boolean
6152 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
6154 asection *isec;
6156 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
6157 if (elf_section_type (isec) == SHT_GROUP)
6159 asection *first = elf_next_in_group (isec);
6160 asection *s = first;
6161 bfd_size_type removed = 0;
6163 while (s != NULL)
6165 /* If this member section is being output but the
6166 SHT_GROUP section is not, then clear the group info
6167 set up by _bfd_elf_copy_private_section_data. */
6168 if (s->output_section != discarded
6169 && isec->output_section == discarded)
6171 elf_section_flags (s->output_section) &= ~SHF_GROUP;
6172 elf_group_name (s->output_section) = NULL;
6174 /* Conversely, if the member section is not being output
6175 but the SHT_GROUP section is, then adjust its size. */
6176 else if (s->output_section == discarded
6177 && isec->output_section != discarded)
6178 removed += 4;
6179 s = elf_next_in_group (s);
6180 if (s == first)
6181 break;
6183 if (removed != 0)
6185 if (discarded != NULL)
6187 /* If we've been called for ld -r, then we need to
6188 adjust the input section size. This function may
6189 be called multiple times, so save the original
6190 size. */
6191 if (isec->rawsize == 0)
6192 isec->rawsize = isec->size;
6193 isec->size = isec->rawsize - removed;
6195 else
6197 /* Adjust the output section size when called from
6198 objcopy. */
6199 isec->output_section->size -= removed;
6204 return TRUE;
6207 /* Copy private header information. */
6209 bfd_boolean
6210 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
6212 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6213 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6214 return TRUE;
6216 /* Copy over private BFD data if it has not already been copied.
6217 This must be done here, rather than in the copy_private_bfd_data
6218 entry point, because the latter is called after the section
6219 contents have been set, which means that the program headers have
6220 already been worked out. */
6221 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
6223 if (! copy_private_bfd_data (ibfd, obfd))
6224 return FALSE;
6227 return _bfd_elf_fixup_group_sections (ibfd, NULL);
6230 /* Copy private symbol information. If this symbol is in a section
6231 which we did not map into a BFD section, try to map the section
6232 index correctly. We use special macro definitions for the mapped
6233 section indices; these definitions are interpreted by the
6234 swap_out_syms function. */
6236 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6237 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6238 #define MAP_STRTAB (SHN_HIOS + 3)
6239 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6240 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6242 bfd_boolean
6243 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
6244 asymbol *isymarg,
6245 bfd *obfd,
6246 asymbol *osymarg)
6248 elf_symbol_type *isym, *osym;
6250 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6251 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6252 return TRUE;
6254 isym = elf_symbol_from (ibfd, isymarg);
6255 osym = elf_symbol_from (obfd, osymarg);
6257 if (isym != NULL
6258 && isym->internal_elf_sym.st_shndx != 0
6259 && osym != NULL
6260 && bfd_is_abs_section (isym->symbol.section))
6262 unsigned int shndx;
6264 shndx = isym->internal_elf_sym.st_shndx;
6265 if (shndx == elf_onesymtab (ibfd))
6266 shndx = MAP_ONESYMTAB;
6267 else if (shndx == elf_dynsymtab (ibfd))
6268 shndx = MAP_DYNSYMTAB;
6269 else if (shndx == elf_tdata (ibfd)->strtab_section)
6270 shndx = MAP_STRTAB;
6271 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
6272 shndx = MAP_SHSTRTAB;
6273 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
6274 shndx = MAP_SYM_SHNDX;
6275 osym->internal_elf_sym.st_shndx = shndx;
6278 return TRUE;
6281 /* Swap out the symbols. */
6283 static bfd_boolean
6284 swap_out_syms (bfd *abfd,
6285 struct bfd_strtab_hash **sttp,
6286 int relocatable_p)
6288 const struct elf_backend_data *bed;
6289 int symcount;
6290 asymbol **syms;
6291 struct bfd_strtab_hash *stt;
6292 Elf_Internal_Shdr *symtab_hdr;
6293 Elf_Internal_Shdr *symtab_shndx_hdr;
6294 Elf_Internal_Shdr *symstrtab_hdr;
6295 bfd_byte *outbound_syms;
6296 bfd_byte *outbound_shndx;
6297 int idx;
6298 bfd_size_type amt;
6299 bfd_boolean name_local_sections;
6301 if (!elf_map_symbols (abfd))
6302 return FALSE;
6304 /* Dump out the symtabs. */
6305 stt = _bfd_elf_stringtab_init ();
6306 if (stt == NULL)
6307 return FALSE;
6309 bed = get_elf_backend_data (abfd);
6310 symcount = bfd_get_symcount (abfd);
6311 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6312 symtab_hdr->sh_type = SHT_SYMTAB;
6313 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
6314 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
6315 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
6316 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
6318 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
6319 symstrtab_hdr->sh_type = SHT_STRTAB;
6321 outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
6322 bed->s->sizeof_sym);
6323 if (outbound_syms == NULL)
6325 _bfd_stringtab_free (stt);
6326 return FALSE;
6328 symtab_hdr->contents = outbound_syms;
6330 outbound_shndx = NULL;
6331 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
6332 if (symtab_shndx_hdr->sh_name != 0)
6334 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
6335 outbound_shndx = (bfd_byte *)
6336 bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
6337 if (outbound_shndx == NULL)
6339 _bfd_stringtab_free (stt);
6340 return FALSE;
6343 symtab_shndx_hdr->contents = outbound_shndx;
6344 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
6345 symtab_shndx_hdr->sh_size = amt;
6346 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
6347 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
6350 /* Now generate the data (for "contents"). */
6352 /* Fill in zeroth symbol and swap it out. */
6353 Elf_Internal_Sym sym;
6354 sym.st_name = 0;
6355 sym.st_value = 0;
6356 sym.st_size = 0;
6357 sym.st_info = 0;
6358 sym.st_other = 0;
6359 sym.st_shndx = SHN_UNDEF;
6360 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6361 outbound_syms += bed->s->sizeof_sym;
6362 if (outbound_shndx != NULL)
6363 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6366 name_local_sections
6367 = (bed->elf_backend_name_local_section_symbols
6368 && bed->elf_backend_name_local_section_symbols (abfd));
6370 syms = bfd_get_outsymbols (abfd);
6371 for (idx = 0; idx < symcount; idx++)
6373 Elf_Internal_Sym sym;
6374 bfd_vma value = syms[idx]->value;
6375 elf_symbol_type *type_ptr;
6376 flagword flags = syms[idx]->flags;
6377 int type;
6379 if (!name_local_sections
6380 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
6382 /* Local section symbols have no name. */
6383 sym.st_name = 0;
6385 else
6387 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
6388 syms[idx]->name,
6389 TRUE, FALSE);
6390 if (sym.st_name == (unsigned long) -1)
6392 _bfd_stringtab_free (stt);
6393 return FALSE;
6397 type_ptr = elf_symbol_from (abfd, syms[idx]);
6399 if ((flags & BSF_SECTION_SYM) == 0
6400 && bfd_is_com_section (syms[idx]->section))
6402 /* ELF common symbols put the alignment into the `value' field,
6403 and the size into the `size' field. This is backwards from
6404 how BFD handles it, so reverse it here. */
6405 sym.st_size = value;
6406 if (type_ptr == NULL
6407 || type_ptr->internal_elf_sym.st_value == 0)
6408 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
6409 else
6410 sym.st_value = type_ptr->internal_elf_sym.st_value;
6411 sym.st_shndx = _bfd_elf_section_from_bfd_section
6412 (abfd, syms[idx]->section);
6414 else
6416 asection *sec = syms[idx]->section;
6417 unsigned int shndx;
6419 if (sec->output_section)
6421 value += sec->output_offset;
6422 sec = sec->output_section;
6425 /* Don't add in the section vma for relocatable output. */
6426 if (! relocatable_p)
6427 value += sec->vma;
6428 sym.st_value = value;
6429 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
6431 if (bfd_is_abs_section (sec)
6432 && type_ptr != NULL
6433 && type_ptr->internal_elf_sym.st_shndx != 0)
6435 /* This symbol is in a real ELF section which we did
6436 not create as a BFD section. Undo the mapping done
6437 by copy_private_symbol_data. */
6438 shndx = type_ptr->internal_elf_sym.st_shndx;
6439 switch (shndx)
6441 case MAP_ONESYMTAB:
6442 shndx = elf_onesymtab (abfd);
6443 break;
6444 case MAP_DYNSYMTAB:
6445 shndx = elf_dynsymtab (abfd);
6446 break;
6447 case MAP_STRTAB:
6448 shndx = elf_tdata (abfd)->strtab_section;
6449 break;
6450 case MAP_SHSTRTAB:
6451 shndx = elf_tdata (abfd)->shstrtab_section;
6452 break;
6453 case MAP_SYM_SHNDX:
6454 shndx = elf_tdata (abfd)->symtab_shndx_section;
6455 break;
6456 default:
6457 break;
6460 else
6462 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
6464 if (shndx == SHN_BAD)
6466 asection *sec2;
6468 /* Writing this would be a hell of a lot easier if
6469 we had some decent documentation on bfd, and
6470 knew what to expect of the library, and what to
6471 demand of applications. For example, it
6472 appears that `objcopy' might not set the
6473 section of a symbol to be a section that is
6474 actually in the output file. */
6475 sec2 = bfd_get_section_by_name (abfd, sec->name);
6476 if (sec2 == NULL)
6478 _bfd_error_handler (_("\
6479 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6480 syms[idx]->name ? syms[idx]->name : "<Local sym>",
6481 sec->name);
6482 bfd_set_error (bfd_error_invalid_operation);
6483 _bfd_stringtab_free (stt);
6484 return FALSE;
6487 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
6488 BFD_ASSERT (shndx != SHN_BAD);
6492 sym.st_shndx = shndx;
6495 if ((flags & BSF_THREAD_LOCAL) != 0)
6496 type = STT_TLS;
6497 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
6498 type = STT_GNU_IFUNC;
6499 else if ((flags & BSF_FUNCTION) != 0)
6500 type = STT_FUNC;
6501 else if ((flags & BSF_OBJECT) != 0)
6502 type = STT_OBJECT;
6503 else if ((flags & BSF_RELC) != 0)
6504 type = STT_RELC;
6505 else if ((flags & BSF_SRELC) != 0)
6506 type = STT_SRELC;
6507 else
6508 type = STT_NOTYPE;
6510 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
6511 type = STT_TLS;
6513 /* Processor-specific types. */
6514 if (type_ptr != NULL
6515 && bed->elf_backend_get_symbol_type)
6516 type = ((*bed->elf_backend_get_symbol_type)
6517 (&type_ptr->internal_elf_sym, type));
6519 if (flags & BSF_SECTION_SYM)
6521 if (flags & BSF_GLOBAL)
6522 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6523 else
6524 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
6526 else if (bfd_is_com_section (syms[idx]->section))
6528 #ifdef USE_STT_COMMON
6529 if (type == STT_OBJECT)
6530 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON);
6531 else
6532 #endif
6533 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
6535 else if (bfd_is_und_section (syms[idx]->section))
6536 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
6537 ? STB_WEAK
6538 : STB_GLOBAL),
6539 type);
6540 else if (flags & BSF_FILE)
6541 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
6542 else
6544 int bind = STB_LOCAL;
6546 if (flags & BSF_LOCAL)
6547 bind = STB_LOCAL;
6548 else if (flags & BSF_GNU_UNIQUE)
6549 bind = STB_GNU_UNIQUE;
6550 else if (flags & BSF_WEAK)
6551 bind = STB_WEAK;
6552 else if (flags & BSF_GLOBAL)
6553 bind = STB_GLOBAL;
6555 sym.st_info = ELF_ST_INFO (bind, type);
6558 if (type_ptr != NULL)
6559 sym.st_other = type_ptr->internal_elf_sym.st_other;
6560 else
6561 sym.st_other = 0;
6563 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6564 outbound_syms += bed->s->sizeof_sym;
6565 if (outbound_shndx != NULL)
6566 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6569 *sttp = stt;
6570 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
6571 symstrtab_hdr->sh_type = SHT_STRTAB;
6573 symstrtab_hdr->sh_flags = 0;
6574 symstrtab_hdr->sh_addr = 0;
6575 symstrtab_hdr->sh_entsize = 0;
6576 symstrtab_hdr->sh_link = 0;
6577 symstrtab_hdr->sh_info = 0;
6578 symstrtab_hdr->sh_addralign = 1;
6580 return TRUE;
6583 /* Return the number of bytes required to hold the symtab vector.
6585 Note that we base it on the count plus 1, since we will null terminate
6586 the vector allocated based on this size. However, the ELF symbol table
6587 always has a dummy entry as symbol #0, so it ends up even. */
6589 long
6590 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
6592 long symcount;
6593 long symtab_size;
6594 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
6596 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6597 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6598 if (symcount > 0)
6599 symtab_size -= sizeof (asymbol *);
6601 return symtab_size;
6604 long
6605 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
6607 long symcount;
6608 long symtab_size;
6609 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
6611 if (elf_dynsymtab (abfd) == 0)
6613 bfd_set_error (bfd_error_invalid_operation);
6614 return -1;
6617 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6618 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6619 if (symcount > 0)
6620 symtab_size -= sizeof (asymbol *);
6622 return symtab_size;
6625 long
6626 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
6627 sec_ptr asect)
6629 return (asect->reloc_count + 1) * sizeof (arelent *);
6632 /* Canonicalize the relocs. */
6634 long
6635 _bfd_elf_canonicalize_reloc (bfd *abfd,
6636 sec_ptr section,
6637 arelent **relptr,
6638 asymbol **symbols)
6640 arelent *tblptr;
6641 unsigned int i;
6642 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6644 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
6645 return -1;
6647 tblptr = section->relocation;
6648 for (i = 0; i < section->reloc_count; i++)
6649 *relptr++ = tblptr++;
6651 *relptr = NULL;
6653 return section->reloc_count;
6656 long
6657 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
6659 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6660 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
6662 if (symcount >= 0)
6663 bfd_get_symcount (abfd) = symcount;
6664 return symcount;
6667 long
6668 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
6669 asymbol **allocation)
6671 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6672 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
6674 if (symcount >= 0)
6675 bfd_get_dynamic_symcount (abfd) = symcount;
6676 return symcount;
6679 /* Return the size required for the dynamic reloc entries. Any loadable
6680 section that was actually installed in the BFD, and has type SHT_REL
6681 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
6682 dynamic reloc section. */
6684 long
6685 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
6687 long ret;
6688 asection *s;
6690 if (elf_dynsymtab (abfd) == 0)
6692 bfd_set_error (bfd_error_invalid_operation);
6693 return -1;
6696 ret = sizeof (arelent *);
6697 for (s = abfd->sections; s != NULL; s = s->next)
6698 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6699 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6700 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6701 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
6702 * sizeof (arelent *));
6704 return ret;
6707 /* Canonicalize the dynamic relocation entries. Note that we return the
6708 dynamic relocations as a single block, although they are actually
6709 associated with particular sections; the interface, which was
6710 designed for SunOS style shared libraries, expects that there is only
6711 one set of dynamic relocs. Any loadable section that was actually
6712 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
6713 dynamic symbol table, is considered to be a dynamic reloc section. */
6715 long
6716 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
6717 arelent **storage,
6718 asymbol **syms)
6720 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
6721 asection *s;
6722 long ret;
6724 if (elf_dynsymtab (abfd) == 0)
6726 bfd_set_error (bfd_error_invalid_operation);
6727 return -1;
6730 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
6731 ret = 0;
6732 for (s = abfd->sections; s != NULL; s = s->next)
6734 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6735 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6736 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6738 arelent *p;
6739 long count, i;
6741 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
6742 return -1;
6743 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
6744 p = s->relocation;
6745 for (i = 0; i < count; i++)
6746 *storage++ = p++;
6747 ret += count;
6751 *storage = NULL;
6753 return ret;
6756 /* Read in the version information. */
6758 bfd_boolean
6759 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
6761 bfd_byte *contents = NULL;
6762 unsigned int freeidx = 0;
6764 if (elf_dynverref (abfd) != 0)
6766 Elf_Internal_Shdr *hdr;
6767 Elf_External_Verneed *everneed;
6768 Elf_Internal_Verneed *iverneed;
6769 unsigned int i;
6770 bfd_byte *contents_end;
6772 hdr = &elf_tdata (abfd)->dynverref_hdr;
6774 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
6775 bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
6776 if (elf_tdata (abfd)->verref == NULL)
6777 goto error_return;
6779 elf_tdata (abfd)->cverrefs = hdr->sh_info;
6781 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
6782 if (contents == NULL)
6784 error_return_verref:
6785 elf_tdata (abfd)->verref = NULL;
6786 elf_tdata (abfd)->cverrefs = 0;
6787 goto error_return;
6789 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6790 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6791 goto error_return_verref;
6793 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed))
6794 goto error_return_verref;
6796 BFD_ASSERT (sizeof (Elf_External_Verneed)
6797 == sizeof (Elf_External_Vernaux));
6798 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
6799 everneed = (Elf_External_Verneed *) contents;
6800 iverneed = elf_tdata (abfd)->verref;
6801 for (i = 0; i < hdr->sh_info; i++, iverneed++)
6803 Elf_External_Vernaux *evernaux;
6804 Elf_Internal_Vernaux *ivernaux;
6805 unsigned int j;
6807 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
6809 iverneed->vn_bfd = abfd;
6811 iverneed->vn_filename =
6812 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6813 iverneed->vn_file);
6814 if (iverneed->vn_filename == NULL)
6815 goto error_return_verref;
6817 if (iverneed->vn_cnt == 0)
6818 iverneed->vn_auxptr = NULL;
6819 else
6821 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
6822 bfd_alloc2 (abfd, iverneed->vn_cnt,
6823 sizeof (Elf_Internal_Vernaux));
6824 if (iverneed->vn_auxptr == NULL)
6825 goto error_return_verref;
6828 if (iverneed->vn_aux
6829 > (size_t) (contents_end - (bfd_byte *) everneed))
6830 goto error_return_verref;
6832 evernaux = ((Elf_External_Vernaux *)
6833 ((bfd_byte *) everneed + iverneed->vn_aux));
6834 ivernaux = iverneed->vn_auxptr;
6835 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
6837 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
6839 ivernaux->vna_nodename =
6840 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6841 ivernaux->vna_name);
6842 if (ivernaux->vna_nodename == NULL)
6843 goto error_return_verref;
6845 if (j + 1 < iverneed->vn_cnt)
6846 ivernaux->vna_nextptr = ivernaux + 1;
6847 else
6848 ivernaux->vna_nextptr = NULL;
6850 if (ivernaux->vna_next
6851 > (size_t) (contents_end - (bfd_byte *) evernaux))
6852 goto error_return_verref;
6854 evernaux = ((Elf_External_Vernaux *)
6855 ((bfd_byte *) evernaux + ivernaux->vna_next));
6857 if (ivernaux->vna_other > freeidx)
6858 freeidx = ivernaux->vna_other;
6861 if (i + 1 < hdr->sh_info)
6862 iverneed->vn_nextref = iverneed + 1;
6863 else
6864 iverneed->vn_nextref = NULL;
6866 if (iverneed->vn_next
6867 > (size_t) (contents_end - (bfd_byte *) everneed))
6868 goto error_return_verref;
6870 everneed = ((Elf_External_Verneed *)
6871 ((bfd_byte *) everneed + iverneed->vn_next));
6874 free (contents);
6875 contents = NULL;
6878 if (elf_dynverdef (abfd) != 0)
6880 Elf_Internal_Shdr *hdr;
6881 Elf_External_Verdef *everdef;
6882 Elf_Internal_Verdef *iverdef;
6883 Elf_Internal_Verdef *iverdefarr;
6884 Elf_Internal_Verdef iverdefmem;
6885 unsigned int i;
6886 unsigned int maxidx;
6887 bfd_byte *contents_end_def, *contents_end_aux;
6889 hdr = &elf_tdata (abfd)->dynverdef_hdr;
6891 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
6892 if (contents == NULL)
6893 goto error_return;
6894 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6895 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6896 goto error_return;
6898 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef))
6899 goto error_return;
6901 BFD_ASSERT (sizeof (Elf_External_Verdef)
6902 >= sizeof (Elf_External_Verdaux));
6903 contents_end_def = contents + hdr->sh_size
6904 - sizeof (Elf_External_Verdef);
6905 contents_end_aux = contents + hdr->sh_size
6906 - sizeof (Elf_External_Verdaux);
6908 /* We know the number of entries in the section but not the maximum
6909 index. Therefore we have to run through all entries and find
6910 the maximum. */
6911 everdef = (Elf_External_Verdef *) contents;
6912 maxidx = 0;
6913 for (i = 0; i < hdr->sh_info; ++i)
6915 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6917 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
6918 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
6920 if (iverdefmem.vd_next
6921 > (size_t) (contents_end_def - (bfd_byte *) everdef))
6922 goto error_return;
6924 everdef = ((Elf_External_Verdef *)
6925 ((bfd_byte *) everdef + iverdefmem.vd_next));
6928 if (default_imported_symver)
6930 if (freeidx > maxidx)
6931 maxidx = ++freeidx;
6932 else
6933 freeidx = ++maxidx;
6935 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
6936 bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
6937 if (elf_tdata (abfd)->verdef == NULL)
6938 goto error_return;
6940 elf_tdata (abfd)->cverdefs = maxidx;
6942 everdef = (Elf_External_Verdef *) contents;
6943 iverdefarr = elf_tdata (abfd)->verdef;
6944 for (i = 0; i < hdr->sh_info; i++)
6946 Elf_External_Verdaux *everdaux;
6947 Elf_Internal_Verdaux *iverdaux;
6948 unsigned int j;
6950 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6952 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
6954 error_return_verdef:
6955 elf_tdata (abfd)->verdef = NULL;
6956 elf_tdata (abfd)->cverdefs = 0;
6957 goto error_return;
6960 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
6961 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
6963 iverdef->vd_bfd = abfd;
6965 if (iverdef->vd_cnt == 0)
6966 iverdef->vd_auxptr = NULL;
6967 else
6969 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
6970 bfd_alloc2 (abfd, iverdef->vd_cnt,
6971 sizeof (Elf_Internal_Verdaux));
6972 if (iverdef->vd_auxptr == NULL)
6973 goto error_return_verdef;
6976 if (iverdef->vd_aux
6977 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
6978 goto error_return_verdef;
6980 everdaux = ((Elf_External_Verdaux *)
6981 ((bfd_byte *) everdef + iverdef->vd_aux));
6982 iverdaux = iverdef->vd_auxptr;
6983 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
6985 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
6987 iverdaux->vda_nodename =
6988 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6989 iverdaux->vda_name);
6990 if (iverdaux->vda_nodename == NULL)
6991 goto error_return_verdef;
6993 if (j + 1 < iverdef->vd_cnt)
6994 iverdaux->vda_nextptr = iverdaux + 1;
6995 else
6996 iverdaux->vda_nextptr = NULL;
6998 if (iverdaux->vda_next
6999 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
7000 goto error_return_verdef;
7002 everdaux = ((Elf_External_Verdaux *)
7003 ((bfd_byte *) everdaux + iverdaux->vda_next));
7006 if (iverdef->vd_cnt)
7007 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
7009 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
7010 iverdef->vd_nextdef = iverdef + 1;
7011 else
7012 iverdef->vd_nextdef = NULL;
7014 everdef = ((Elf_External_Verdef *)
7015 ((bfd_byte *) everdef + iverdef->vd_next));
7018 free (contents);
7019 contents = NULL;
7021 else if (default_imported_symver)
7023 if (freeidx < 3)
7024 freeidx = 3;
7025 else
7026 freeidx++;
7028 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
7029 bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
7030 if (elf_tdata (abfd)->verdef == NULL)
7031 goto error_return;
7033 elf_tdata (abfd)->cverdefs = freeidx;
7036 /* Create a default version based on the soname. */
7037 if (default_imported_symver)
7039 Elf_Internal_Verdef *iverdef;
7040 Elf_Internal_Verdaux *iverdaux;
7042 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];;
7044 iverdef->vd_version = VER_DEF_CURRENT;
7045 iverdef->vd_flags = 0;
7046 iverdef->vd_ndx = freeidx;
7047 iverdef->vd_cnt = 1;
7049 iverdef->vd_bfd = abfd;
7051 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
7052 if (iverdef->vd_nodename == NULL)
7053 goto error_return_verdef;
7054 iverdef->vd_nextdef = NULL;
7055 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
7056 bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux));
7057 if (iverdef->vd_auxptr == NULL)
7058 goto error_return_verdef;
7060 iverdaux = iverdef->vd_auxptr;
7061 iverdaux->vda_nodename = iverdef->vd_nodename;
7062 iverdaux->vda_nextptr = NULL;
7065 return TRUE;
7067 error_return:
7068 if (contents != NULL)
7069 free (contents);
7070 return FALSE;
7073 asymbol *
7074 _bfd_elf_make_empty_symbol (bfd *abfd)
7076 elf_symbol_type *newsym;
7077 bfd_size_type amt = sizeof (elf_symbol_type);
7079 newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt);
7080 if (!newsym)
7081 return NULL;
7082 else
7084 newsym->symbol.the_bfd = abfd;
7085 return &newsym->symbol;
7089 void
7090 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
7091 asymbol *symbol,
7092 symbol_info *ret)
7094 bfd_symbol_info (symbol, ret);
7097 /* Return whether a symbol name implies a local symbol. Most targets
7098 use this function for the is_local_label_name entry point, but some
7099 override it. */
7101 bfd_boolean
7102 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
7103 const char *name)
7105 /* Normal local symbols start with ``.L''. */
7106 if (name[0] == '.' && name[1] == 'L')
7107 return TRUE;
7109 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7110 DWARF debugging symbols starting with ``..''. */
7111 if (name[0] == '.' && name[1] == '.')
7112 return TRUE;
7114 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7115 emitting DWARF debugging output. I suspect this is actually a
7116 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7117 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7118 underscore to be emitted on some ELF targets). For ease of use,
7119 we treat such symbols as local. */
7120 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
7121 return TRUE;
7123 return FALSE;
7126 alent *
7127 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
7128 asymbol *symbol ATTRIBUTE_UNUSED)
7130 abort ();
7131 return NULL;
7134 bfd_boolean
7135 _bfd_elf_set_arch_mach (bfd *abfd,
7136 enum bfd_architecture arch,
7137 unsigned long machine)
7139 /* If this isn't the right architecture for this backend, and this
7140 isn't the generic backend, fail. */
7141 if (arch != get_elf_backend_data (abfd)->arch
7142 && arch != bfd_arch_unknown
7143 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
7144 return FALSE;
7146 return bfd_default_set_arch_mach (abfd, arch, machine);
7149 /* Find the function to a particular section and offset,
7150 for error reporting. */
7152 static bfd_boolean
7153 elf_find_function (bfd *abfd,
7154 asection *section,
7155 asymbol **symbols,
7156 bfd_vma offset,
7157 const char **filename_ptr,
7158 const char **functionname_ptr)
7160 const char *filename;
7161 asymbol *func, *file;
7162 bfd_vma low_func;
7163 asymbol **p;
7164 /* ??? Given multiple file symbols, it is impossible to reliably
7165 choose the right file name for global symbols. File symbols are
7166 local symbols, and thus all file symbols must sort before any
7167 global symbols. The ELF spec may be interpreted to say that a
7168 file symbol must sort before other local symbols, but currently
7169 ld -r doesn't do this. So, for ld -r output, it is possible to
7170 make a better choice of file name for local symbols by ignoring
7171 file symbols appearing after a given local symbol. */
7172 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
7173 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7175 filename = NULL;
7176 func = NULL;
7177 file = NULL;
7178 low_func = 0;
7179 state = nothing_seen;
7181 for (p = symbols; *p != NULL; p++)
7183 elf_symbol_type *q;
7184 unsigned int type;
7186 q = (elf_symbol_type *) *p;
7188 type = ELF_ST_TYPE (q->internal_elf_sym.st_info);
7189 switch (type)
7191 case STT_FILE:
7192 file = &q->symbol;
7193 if (state == symbol_seen)
7194 state = file_after_symbol_seen;
7195 continue;
7196 default:
7197 if (!bed->is_function_type (type))
7198 break;
7199 case STT_NOTYPE:
7200 if (bfd_get_section (&q->symbol) == section
7201 && q->symbol.value >= low_func
7202 && q->symbol.value <= offset)
7204 func = (asymbol *) q;
7205 low_func = q->symbol.value;
7206 filename = NULL;
7207 if (file != NULL
7208 && (ELF_ST_BIND (q->internal_elf_sym.st_info) == STB_LOCAL
7209 || state != file_after_symbol_seen))
7210 filename = bfd_asymbol_name (file);
7212 break;
7214 if (state == nothing_seen)
7215 state = symbol_seen;
7218 if (func == NULL)
7219 return FALSE;
7221 if (filename_ptr)
7222 *filename_ptr = filename;
7223 if (functionname_ptr)
7224 *functionname_ptr = bfd_asymbol_name (func);
7226 return TRUE;
7229 /* Find the nearest line to a particular section and offset,
7230 for error reporting. */
7232 bfd_boolean
7233 _bfd_elf_find_nearest_line (bfd *abfd,
7234 asection *section,
7235 asymbol **symbols,
7236 bfd_vma offset,
7237 const char **filename_ptr,
7238 const char **functionname_ptr,
7239 unsigned int *line_ptr)
7241 bfd_boolean found;
7243 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
7244 filename_ptr, functionname_ptr,
7245 line_ptr))
7247 if (!*functionname_ptr)
7248 elf_find_function (abfd, section, symbols, offset,
7249 *filename_ptr ? NULL : filename_ptr,
7250 functionname_ptr);
7252 return TRUE;
7255 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
7256 filename_ptr, functionname_ptr,
7257 line_ptr, 0,
7258 &elf_tdata (abfd)->dwarf2_find_line_info))
7260 if (!*functionname_ptr)
7261 elf_find_function (abfd, section, symbols, offset,
7262 *filename_ptr ? NULL : filename_ptr,
7263 functionname_ptr);
7265 return TRUE;
7268 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
7269 &found, filename_ptr,
7270 functionname_ptr, line_ptr,
7271 &elf_tdata (abfd)->line_info))
7272 return FALSE;
7273 if (found && (*functionname_ptr || *line_ptr))
7274 return TRUE;
7276 if (symbols == NULL)
7277 return FALSE;
7279 if (! elf_find_function (abfd, section, symbols, offset,
7280 filename_ptr, functionname_ptr))
7281 return FALSE;
7283 *line_ptr = 0;
7284 return TRUE;
7287 /* Find the line for a symbol. */
7289 bfd_boolean
7290 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
7291 const char **filename_ptr, unsigned int *line_ptr)
7293 return _bfd_dwarf2_find_line (abfd, symbols, symbol,
7294 filename_ptr, line_ptr, 0,
7295 &elf_tdata (abfd)->dwarf2_find_line_info);
7298 /* After a call to bfd_find_nearest_line, successive calls to
7299 bfd_find_inliner_info can be used to get source information about
7300 each level of function inlining that terminated at the address
7301 passed to bfd_find_nearest_line. Currently this is only supported
7302 for DWARF2 with appropriate DWARF3 extensions. */
7304 bfd_boolean
7305 _bfd_elf_find_inliner_info (bfd *abfd,
7306 const char **filename_ptr,
7307 const char **functionname_ptr,
7308 unsigned int *line_ptr)
7310 bfd_boolean found;
7311 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
7312 functionname_ptr, line_ptr,
7313 & elf_tdata (abfd)->dwarf2_find_line_info);
7314 return found;
7318 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
7320 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7321 int ret = bed->s->sizeof_ehdr;
7323 if (!info->relocatable)
7325 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
7327 if (phdr_size == (bfd_size_type) -1)
7329 struct elf_segment_map *m;
7331 phdr_size = 0;
7332 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
7333 phdr_size += bed->s->sizeof_phdr;
7335 if (phdr_size == 0)
7336 phdr_size = get_program_header_size (abfd, info);
7339 elf_tdata (abfd)->program_header_size = phdr_size;
7340 ret += phdr_size;
7343 return ret;
7346 bfd_boolean
7347 _bfd_elf_set_section_contents (bfd *abfd,
7348 sec_ptr section,
7349 const void *location,
7350 file_ptr offset,
7351 bfd_size_type count)
7353 Elf_Internal_Shdr *hdr;
7354 bfd_signed_vma pos;
7356 if (! abfd->output_has_begun
7357 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7358 return FALSE;
7360 hdr = &elf_section_data (section)->this_hdr;
7361 pos = hdr->sh_offset + offset;
7362 if (bfd_seek (abfd, pos, SEEK_SET) != 0
7363 || bfd_bwrite (location, count, abfd) != count)
7364 return FALSE;
7366 return TRUE;
7369 void
7370 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
7371 arelent *cache_ptr ATTRIBUTE_UNUSED,
7372 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
7374 abort ();
7377 /* Try to convert a non-ELF reloc into an ELF one. */
7379 bfd_boolean
7380 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
7382 /* Check whether we really have an ELF howto. */
7384 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
7386 bfd_reloc_code_real_type code;
7387 reloc_howto_type *howto;
7389 /* Alien reloc: Try to determine its type to replace it with an
7390 equivalent ELF reloc. */
7392 if (areloc->howto->pc_relative)
7394 switch (areloc->howto->bitsize)
7396 case 8:
7397 code = BFD_RELOC_8_PCREL;
7398 break;
7399 case 12:
7400 code = BFD_RELOC_12_PCREL;
7401 break;
7402 case 16:
7403 code = BFD_RELOC_16_PCREL;
7404 break;
7405 case 24:
7406 code = BFD_RELOC_24_PCREL;
7407 break;
7408 case 32:
7409 code = BFD_RELOC_32_PCREL;
7410 break;
7411 case 64:
7412 code = BFD_RELOC_64_PCREL;
7413 break;
7414 default:
7415 goto fail;
7418 howto = bfd_reloc_type_lookup (abfd, code);
7420 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
7422 if (howto->pcrel_offset)
7423 areloc->addend += areloc->address;
7424 else
7425 areloc->addend -= areloc->address; /* addend is unsigned!! */
7428 else
7430 switch (areloc->howto->bitsize)
7432 case 8:
7433 code = BFD_RELOC_8;
7434 break;
7435 case 14:
7436 code = BFD_RELOC_14;
7437 break;
7438 case 16:
7439 code = BFD_RELOC_16;
7440 break;
7441 case 26:
7442 code = BFD_RELOC_26;
7443 break;
7444 case 32:
7445 code = BFD_RELOC_32;
7446 break;
7447 case 64:
7448 code = BFD_RELOC_64;
7449 break;
7450 default:
7451 goto fail;
7454 howto = bfd_reloc_type_lookup (abfd, code);
7457 if (howto)
7458 areloc->howto = howto;
7459 else
7460 goto fail;
7463 return TRUE;
7465 fail:
7466 (*_bfd_error_handler)
7467 (_("%B: unsupported relocation type %s"),
7468 abfd, areloc->howto->name);
7469 bfd_set_error (bfd_error_bad_value);
7470 return FALSE;
7473 bfd_boolean
7474 _bfd_elf_close_and_cleanup (bfd *abfd)
7476 if (bfd_get_format (abfd) == bfd_object)
7478 if (elf_tdata (abfd) != NULL && elf_shstrtab (abfd) != NULL)
7479 _bfd_elf_strtab_free (elf_shstrtab (abfd));
7480 _bfd_dwarf2_cleanup_debug_info (abfd);
7483 return _bfd_generic_close_and_cleanup (abfd);
7486 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7487 in the relocation's offset. Thus we cannot allow any sort of sanity
7488 range-checking to interfere. There is nothing else to do in processing
7489 this reloc. */
7491 bfd_reloc_status_type
7492 _bfd_elf_rel_vtable_reloc_fn
7493 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
7494 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
7495 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
7496 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
7498 return bfd_reloc_ok;
7501 /* Elf core file support. Much of this only works on native
7502 toolchains, since we rely on knowing the
7503 machine-dependent procfs structure in order to pick
7504 out details about the corefile. */
7506 #ifdef HAVE_SYS_PROCFS_H
7507 /* Needed for new procfs interface on sparc-solaris. */
7508 # define _STRUCTURED_PROC 1
7509 # include <sys/procfs.h>
7510 #endif
7512 /* FIXME: this is kinda wrong, but it's what gdb wants. */
7514 static int
7515 elfcore_make_pid (bfd *abfd)
7517 return ((elf_tdata (abfd)->core_lwpid << 16)
7518 + (elf_tdata (abfd)->core_pid));
7521 /* If there isn't a section called NAME, make one, using
7522 data from SECT. Note, this function will generate a
7523 reference to NAME, so you shouldn't deallocate or
7524 overwrite it. */
7526 static bfd_boolean
7527 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
7529 asection *sect2;
7531 if (bfd_get_section_by_name (abfd, name) != NULL)
7532 return TRUE;
7534 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
7535 if (sect2 == NULL)
7536 return FALSE;
7538 sect2->size = sect->size;
7539 sect2->filepos = sect->filepos;
7540 sect2->alignment_power = sect->alignment_power;
7541 return TRUE;
7544 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
7545 actually creates up to two pseudosections:
7546 - For the single-threaded case, a section named NAME, unless
7547 such a section already exists.
7548 - For the multi-threaded case, a section named "NAME/PID", where
7549 PID is elfcore_make_pid (abfd).
7550 Both pseudosections have identical contents. */
7551 bfd_boolean
7552 _bfd_elfcore_make_pseudosection (bfd *abfd,
7553 char *name,
7554 size_t size,
7555 ufile_ptr filepos)
7557 char buf[100];
7558 char *threaded_name;
7559 size_t len;
7560 asection *sect;
7562 /* Build the section name. */
7564 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
7565 len = strlen (buf) + 1;
7566 threaded_name = (char *) bfd_alloc (abfd, len);
7567 if (threaded_name == NULL)
7568 return FALSE;
7569 memcpy (threaded_name, buf, len);
7571 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
7572 SEC_HAS_CONTENTS);
7573 if (sect == NULL)
7574 return FALSE;
7575 sect->size = size;
7576 sect->filepos = filepos;
7577 sect->alignment_power = 2;
7579 return elfcore_maybe_make_sect (abfd, name, sect);
7582 /* prstatus_t exists on:
7583 solaris 2.5+
7584 linux 2.[01] + glibc
7585 unixware 4.2
7588 #if defined (HAVE_PRSTATUS_T)
7590 static bfd_boolean
7591 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
7593 size_t size;
7594 int offset;
7596 if (note->descsz == sizeof (prstatus_t))
7598 prstatus_t prstat;
7600 size = sizeof (prstat.pr_reg);
7601 offset = offsetof (prstatus_t, pr_reg);
7602 memcpy (&prstat, note->descdata, sizeof (prstat));
7604 /* Do not overwrite the core signal if it
7605 has already been set by another thread. */
7606 if (elf_tdata (abfd)->core_signal == 0)
7607 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7608 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7610 /* pr_who exists on:
7611 solaris 2.5+
7612 unixware 4.2
7613 pr_who doesn't exist on:
7614 linux 2.[01]
7616 #if defined (HAVE_PRSTATUS_T_PR_WHO)
7617 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7618 #endif
7620 #if defined (HAVE_PRSTATUS32_T)
7621 else if (note->descsz == sizeof (prstatus32_t))
7623 /* 64-bit host, 32-bit corefile */
7624 prstatus32_t prstat;
7626 size = sizeof (prstat.pr_reg);
7627 offset = offsetof (prstatus32_t, pr_reg);
7628 memcpy (&prstat, note->descdata, sizeof (prstat));
7630 /* Do not overwrite the core signal if it
7631 has already been set by another thread. */
7632 if (elf_tdata (abfd)->core_signal == 0)
7633 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7634 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7636 /* pr_who exists on:
7637 solaris 2.5+
7638 unixware 4.2
7639 pr_who doesn't exist on:
7640 linux 2.[01]
7642 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
7643 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7644 #endif
7646 #endif /* HAVE_PRSTATUS32_T */
7647 else
7649 /* Fail - we don't know how to handle any other
7650 note size (ie. data object type). */
7651 return TRUE;
7654 /* Make a ".reg/999" section and a ".reg" section. */
7655 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
7656 size, note->descpos + offset);
7658 #endif /* defined (HAVE_PRSTATUS_T) */
7660 /* Create a pseudosection containing the exact contents of NOTE. */
7661 static bfd_boolean
7662 elfcore_make_note_pseudosection (bfd *abfd,
7663 char *name,
7664 Elf_Internal_Note *note)
7666 return _bfd_elfcore_make_pseudosection (abfd, name,
7667 note->descsz, note->descpos);
7670 /* There isn't a consistent prfpregset_t across platforms,
7671 but it doesn't matter, because we don't have to pick this
7672 data structure apart. */
7674 static bfd_boolean
7675 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
7677 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7680 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
7681 type of NT_PRXFPREG. Just include the whole note's contents
7682 literally. */
7684 static bfd_boolean
7685 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
7687 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
7690 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
7691 with a note type of NT_X86_XSTATE. Just include the whole note's
7692 contents literally. */
7694 static bfd_boolean
7695 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
7697 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
7700 static bfd_boolean
7701 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
7703 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
7706 static bfd_boolean
7707 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
7709 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
7712 static bfd_boolean
7713 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
7715 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
7718 static bfd_boolean
7719 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
7721 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
7724 static bfd_boolean
7725 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
7727 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
7730 static bfd_boolean
7731 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
7733 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
7736 static bfd_boolean
7737 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
7739 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
7742 static bfd_boolean
7743 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
7745 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
7748 #if defined (HAVE_PRPSINFO_T)
7749 typedef prpsinfo_t elfcore_psinfo_t;
7750 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
7751 typedef prpsinfo32_t elfcore_psinfo32_t;
7752 #endif
7753 #endif
7755 #if defined (HAVE_PSINFO_T)
7756 typedef psinfo_t elfcore_psinfo_t;
7757 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
7758 typedef psinfo32_t elfcore_psinfo32_t;
7759 #endif
7760 #endif
7762 /* return a malloc'ed copy of a string at START which is at
7763 most MAX bytes long, possibly without a terminating '\0'.
7764 the copy will always have a terminating '\0'. */
7766 char *
7767 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
7769 char *dups;
7770 char *end = (char *) memchr (start, '\0', max);
7771 size_t len;
7773 if (end == NULL)
7774 len = max;
7775 else
7776 len = end - start;
7778 dups = (char *) bfd_alloc (abfd, len + 1);
7779 if (dups == NULL)
7780 return NULL;
7782 memcpy (dups, start, len);
7783 dups[len] = '\0';
7785 return dups;
7788 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7789 static bfd_boolean
7790 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
7792 if (note->descsz == sizeof (elfcore_psinfo_t))
7794 elfcore_psinfo_t psinfo;
7796 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7798 elf_tdata (abfd)->core_program
7799 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7800 sizeof (psinfo.pr_fname));
7802 elf_tdata (abfd)->core_command
7803 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7804 sizeof (psinfo.pr_psargs));
7806 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
7807 else if (note->descsz == sizeof (elfcore_psinfo32_t))
7809 /* 64-bit host, 32-bit corefile */
7810 elfcore_psinfo32_t psinfo;
7812 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7814 elf_tdata (abfd)->core_program
7815 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7816 sizeof (psinfo.pr_fname));
7818 elf_tdata (abfd)->core_command
7819 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7820 sizeof (psinfo.pr_psargs));
7822 #endif
7824 else
7826 /* Fail - we don't know how to handle any other
7827 note size (ie. data object type). */
7828 return TRUE;
7831 /* Note that for some reason, a spurious space is tacked
7832 onto the end of the args in some (at least one anyway)
7833 implementations, so strip it off if it exists. */
7836 char *command = elf_tdata (abfd)->core_command;
7837 int n = strlen (command);
7839 if (0 < n && command[n - 1] == ' ')
7840 command[n - 1] = '\0';
7843 return TRUE;
7845 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
7847 #if defined (HAVE_PSTATUS_T)
7848 static bfd_boolean
7849 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
7851 if (note->descsz == sizeof (pstatus_t)
7852 #if defined (HAVE_PXSTATUS_T)
7853 || note->descsz == sizeof (pxstatus_t)
7854 #endif
7857 pstatus_t pstat;
7859 memcpy (&pstat, note->descdata, sizeof (pstat));
7861 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7863 #if defined (HAVE_PSTATUS32_T)
7864 else if (note->descsz == sizeof (pstatus32_t))
7866 /* 64-bit host, 32-bit corefile */
7867 pstatus32_t pstat;
7869 memcpy (&pstat, note->descdata, sizeof (pstat));
7871 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7873 #endif
7874 /* Could grab some more details from the "representative"
7875 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
7876 NT_LWPSTATUS note, presumably. */
7878 return TRUE;
7880 #endif /* defined (HAVE_PSTATUS_T) */
7882 #if defined (HAVE_LWPSTATUS_T)
7883 static bfd_boolean
7884 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
7886 lwpstatus_t lwpstat;
7887 char buf[100];
7888 char *name;
7889 size_t len;
7890 asection *sect;
7892 if (note->descsz != sizeof (lwpstat)
7893 #if defined (HAVE_LWPXSTATUS_T)
7894 && note->descsz != sizeof (lwpxstatus_t)
7895 #endif
7897 return TRUE;
7899 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
7901 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
7902 /* Do not overwrite the core signal if it has already been set by
7903 another thread. */
7904 if (elf_tdata (abfd)->core_signal == 0)
7905 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
7907 /* Make a ".reg/999" section. */
7909 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
7910 len = strlen (buf) + 1;
7911 name = bfd_alloc (abfd, len);
7912 if (name == NULL)
7913 return FALSE;
7914 memcpy (name, buf, len);
7916 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7917 if (sect == NULL)
7918 return FALSE;
7920 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7921 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
7922 sect->filepos = note->descpos
7923 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
7924 #endif
7926 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7927 sect->size = sizeof (lwpstat.pr_reg);
7928 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
7929 #endif
7931 sect->alignment_power = 2;
7933 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
7934 return FALSE;
7936 /* Make a ".reg2/999" section */
7938 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
7939 len = strlen (buf) + 1;
7940 name = bfd_alloc (abfd, len);
7941 if (name == NULL)
7942 return FALSE;
7943 memcpy (name, buf, len);
7945 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7946 if (sect == NULL)
7947 return FALSE;
7949 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7950 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
7951 sect->filepos = note->descpos
7952 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
7953 #endif
7955 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
7956 sect->size = sizeof (lwpstat.pr_fpreg);
7957 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
7958 #endif
7960 sect->alignment_power = 2;
7962 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
7964 #endif /* defined (HAVE_LWPSTATUS_T) */
7966 static bfd_boolean
7967 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
7969 char buf[30];
7970 char *name;
7971 size_t len;
7972 asection *sect;
7973 int type;
7974 int is_active_thread;
7975 bfd_vma base_addr;
7977 if (note->descsz < 728)
7978 return TRUE;
7980 if (! CONST_STRNEQ (note->namedata, "win32"))
7981 return TRUE;
7983 type = bfd_get_32 (abfd, note->descdata);
7985 switch (type)
7987 case 1 /* NOTE_INFO_PROCESS */:
7988 /* FIXME: need to add ->core_command. */
7989 /* process_info.pid */
7990 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 8);
7991 /* process_info.signal */
7992 elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 12);
7993 break;
7995 case 2 /* NOTE_INFO_THREAD */:
7996 /* Make a ".reg/999" section. */
7997 /* thread_info.tid */
7998 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
8000 len = strlen (buf) + 1;
8001 name = (char *) bfd_alloc (abfd, len);
8002 if (name == NULL)
8003 return FALSE;
8005 memcpy (name, buf, len);
8007 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8008 if (sect == NULL)
8009 return FALSE;
8011 /* sizeof (thread_info.thread_context) */
8012 sect->size = 716;
8013 /* offsetof (thread_info.thread_context) */
8014 sect->filepos = note->descpos + 12;
8015 sect->alignment_power = 2;
8017 /* thread_info.is_active_thread */
8018 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
8020 if (is_active_thread)
8021 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
8022 return FALSE;
8023 break;
8025 case 3 /* NOTE_INFO_MODULE */:
8026 /* Make a ".module/xxxxxxxx" section. */
8027 /* module_info.base_address */
8028 base_addr = bfd_get_32 (abfd, note->descdata + 4);
8029 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
8031 len = strlen (buf) + 1;
8032 name = (char *) bfd_alloc (abfd, len);
8033 if (name == NULL)
8034 return FALSE;
8036 memcpy (name, buf, len);
8038 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8040 if (sect == NULL)
8041 return FALSE;
8043 sect->size = note->descsz;
8044 sect->filepos = note->descpos;
8045 sect->alignment_power = 2;
8046 break;
8048 default:
8049 return TRUE;
8052 return TRUE;
8055 static bfd_boolean
8056 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
8058 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8060 switch (note->type)
8062 default:
8063 return TRUE;
8065 case NT_PRSTATUS:
8066 if (bed->elf_backend_grok_prstatus)
8067 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
8068 return TRUE;
8069 #if defined (HAVE_PRSTATUS_T)
8070 return elfcore_grok_prstatus (abfd, note);
8071 #else
8072 return TRUE;
8073 #endif
8075 #if defined (HAVE_PSTATUS_T)
8076 case NT_PSTATUS:
8077 return elfcore_grok_pstatus (abfd, note);
8078 #endif
8080 #if defined (HAVE_LWPSTATUS_T)
8081 case NT_LWPSTATUS:
8082 return elfcore_grok_lwpstatus (abfd, note);
8083 #endif
8085 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
8086 return elfcore_grok_prfpreg (abfd, note);
8088 case NT_WIN32PSTATUS:
8089 return elfcore_grok_win32pstatus (abfd, note);
8091 case NT_PRXFPREG: /* Linux SSE extension */
8092 if (note->namesz == 6
8093 && strcmp (note->namedata, "LINUX") == 0)
8094 return elfcore_grok_prxfpreg (abfd, note);
8095 else
8096 return TRUE;
8098 case NT_X86_XSTATE: /* Linux XSAVE extension */
8099 if (note->namesz == 6
8100 && strcmp (note->namedata, "LINUX") == 0)
8101 return elfcore_grok_xstatereg (abfd, note);
8102 else
8103 return TRUE;
8105 case NT_PPC_VMX:
8106 if (note->namesz == 6
8107 && strcmp (note->namedata, "LINUX") == 0)
8108 return elfcore_grok_ppc_vmx (abfd, note);
8109 else
8110 return TRUE;
8112 case NT_PPC_VSX:
8113 if (note->namesz == 6
8114 && strcmp (note->namedata, "LINUX") == 0)
8115 return elfcore_grok_ppc_vsx (abfd, note);
8116 else
8117 return TRUE;
8119 case NT_S390_HIGH_GPRS:
8120 if (note->namesz == 6
8121 && strcmp (note->namedata, "LINUX") == 0)
8122 return elfcore_grok_s390_high_gprs (abfd, note);
8123 else
8124 return TRUE;
8126 case NT_S390_TIMER:
8127 if (note->namesz == 6
8128 && strcmp (note->namedata, "LINUX") == 0)
8129 return elfcore_grok_s390_timer (abfd, note);
8130 else
8131 return TRUE;
8133 case NT_S390_TODCMP:
8134 if (note->namesz == 6
8135 && strcmp (note->namedata, "LINUX") == 0)
8136 return elfcore_grok_s390_todcmp (abfd, note);
8137 else
8138 return TRUE;
8140 case NT_S390_TODPREG:
8141 if (note->namesz == 6
8142 && strcmp (note->namedata, "LINUX") == 0)
8143 return elfcore_grok_s390_todpreg (abfd, note);
8144 else
8145 return TRUE;
8147 case NT_S390_CTRS:
8148 if (note->namesz == 6
8149 && strcmp (note->namedata, "LINUX") == 0)
8150 return elfcore_grok_s390_ctrs (abfd, note);
8151 else
8152 return TRUE;
8154 case NT_S390_PREFIX:
8155 if (note->namesz == 6
8156 && strcmp (note->namedata, "LINUX") == 0)
8157 return elfcore_grok_s390_prefix (abfd, note);
8158 else
8159 return TRUE;
8161 case NT_PRPSINFO:
8162 case NT_PSINFO:
8163 if (bed->elf_backend_grok_psinfo)
8164 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
8165 return TRUE;
8166 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8167 return elfcore_grok_psinfo (abfd, note);
8168 #else
8169 return TRUE;
8170 #endif
8172 case NT_AUXV:
8174 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
8175 SEC_HAS_CONTENTS);
8177 if (sect == NULL)
8178 return FALSE;
8179 sect->size = note->descsz;
8180 sect->filepos = note->descpos;
8181 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8183 return TRUE;
8188 static bfd_boolean
8189 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
8191 elf_tdata (abfd)->build_id_size = note->descsz;
8192 elf_tdata (abfd)->build_id = (bfd_byte *) bfd_alloc (abfd, note->descsz);
8193 if (elf_tdata (abfd)->build_id == NULL)
8194 return FALSE;
8196 memcpy (elf_tdata (abfd)->build_id, note->descdata, note->descsz);
8198 return TRUE;
8201 static bfd_boolean
8202 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
8204 switch (note->type)
8206 default:
8207 return TRUE;
8209 case NT_GNU_BUILD_ID:
8210 return elfobj_grok_gnu_build_id (abfd, note);
8214 static bfd_boolean
8215 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
8217 char *cp;
8219 cp = strchr (note->namedata, '@');
8220 if (cp != NULL)
8222 *lwpidp = atoi(cp + 1);
8223 return TRUE;
8225 return FALSE;
8228 static bfd_boolean
8229 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8231 /* Signal number at offset 0x08. */
8232 elf_tdata (abfd)->core_signal
8233 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8235 /* Process ID at offset 0x50. */
8236 elf_tdata (abfd)->core_pid
8237 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
8239 /* Command name at 0x7c (max 32 bytes, including nul). */
8240 elf_tdata (abfd)->core_command
8241 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
8243 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
8244 note);
8247 static bfd_boolean
8248 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
8250 int lwp;
8252 if (elfcore_netbsd_get_lwpid (note, &lwp))
8253 elf_tdata (abfd)->core_lwpid = lwp;
8255 if (note->type == NT_NETBSDCORE_PROCINFO)
8257 /* NetBSD-specific core "procinfo". Note that we expect to
8258 find this note before any of the others, which is fine,
8259 since the kernel writes this note out first when it
8260 creates a core file. */
8262 return elfcore_grok_netbsd_procinfo (abfd, note);
8265 /* As of Jan 2002 there are no other machine-independent notes
8266 defined for NetBSD core files. If the note type is less
8267 than the start of the machine-dependent note types, we don't
8268 understand it. */
8270 if (note->type < NT_NETBSDCORE_FIRSTMACH)
8271 return TRUE;
8274 switch (bfd_get_arch (abfd))
8276 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8277 PT_GETFPREGS == mach+2. */
8279 case bfd_arch_alpha:
8280 case bfd_arch_sparc:
8281 switch (note->type)
8283 case NT_NETBSDCORE_FIRSTMACH+0:
8284 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8286 case NT_NETBSDCORE_FIRSTMACH+2:
8287 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8289 default:
8290 return TRUE;
8293 /* On all other arch's, PT_GETREGS == mach+1 and
8294 PT_GETFPREGS == mach+3. */
8296 default:
8297 switch (note->type)
8299 case NT_NETBSDCORE_FIRSTMACH+1:
8300 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8302 case NT_NETBSDCORE_FIRSTMACH+3:
8303 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8305 default:
8306 return TRUE;
8309 /* NOTREACHED */
8312 static bfd_boolean
8313 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8315 /* Signal number at offset 0x08. */
8316 elf_tdata (abfd)->core_signal
8317 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8319 /* Process ID at offset 0x20. */
8320 elf_tdata (abfd)->core_pid
8321 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
8323 /* Command name at 0x48 (max 32 bytes, including nul). */
8324 elf_tdata (abfd)->core_command
8325 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
8327 return TRUE;
8330 static bfd_boolean
8331 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
8333 if (note->type == NT_OPENBSD_PROCINFO)
8334 return elfcore_grok_openbsd_procinfo (abfd, note);
8336 if (note->type == NT_OPENBSD_REGS)
8337 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8339 if (note->type == NT_OPENBSD_FPREGS)
8340 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8342 if (note->type == NT_OPENBSD_XFPREGS)
8343 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
8345 if (note->type == NT_OPENBSD_AUXV)
8347 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
8348 SEC_HAS_CONTENTS);
8350 if (sect == NULL)
8351 return FALSE;
8352 sect->size = note->descsz;
8353 sect->filepos = note->descpos;
8354 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8356 return TRUE;
8359 if (note->type == NT_OPENBSD_WCOOKIE)
8361 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
8362 SEC_HAS_CONTENTS);
8364 if (sect == NULL)
8365 return FALSE;
8366 sect->size = note->descsz;
8367 sect->filepos = note->descpos;
8368 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8370 return TRUE;
8373 return TRUE;
8376 static bfd_boolean
8377 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
8379 void *ddata = note->descdata;
8380 char buf[100];
8381 char *name;
8382 asection *sect;
8383 short sig;
8384 unsigned flags;
8386 /* nto_procfs_status 'pid' field is at offset 0. */
8387 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
8389 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
8390 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
8392 /* nto_procfs_status 'flags' field is at offset 8. */
8393 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
8395 /* nto_procfs_status 'what' field is at offset 14. */
8396 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
8398 elf_tdata (abfd)->core_signal = sig;
8399 elf_tdata (abfd)->core_lwpid = *tid;
8402 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
8403 do not come from signals so we make sure we set the current
8404 thread just in case. */
8405 if (flags & 0x00000080)
8406 elf_tdata (abfd)->core_lwpid = *tid;
8408 /* Make a ".qnx_core_status/%d" section. */
8409 sprintf (buf, ".qnx_core_status/%ld", *tid);
8411 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
8412 if (name == NULL)
8413 return FALSE;
8414 strcpy (name, buf);
8416 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8417 if (sect == NULL)
8418 return FALSE;
8420 sect->size = note->descsz;
8421 sect->filepos = note->descpos;
8422 sect->alignment_power = 2;
8424 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
8427 static bfd_boolean
8428 elfcore_grok_nto_regs (bfd *abfd,
8429 Elf_Internal_Note *note,
8430 long tid,
8431 char *base)
8433 char buf[100];
8434 char *name;
8435 asection *sect;
8437 /* Make a "(base)/%d" section. */
8438 sprintf (buf, "%s/%ld", base, tid);
8440 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
8441 if (name == NULL)
8442 return FALSE;
8443 strcpy (name, buf);
8445 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8446 if (sect == NULL)
8447 return FALSE;
8449 sect->size = note->descsz;
8450 sect->filepos = note->descpos;
8451 sect->alignment_power = 2;
8453 /* This is the current thread. */
8454 if (elf_tdata (abfd)->core_lwpid == tid)
8455 return elfcore_maybe_make_sect (abfd, base, sect);
8457 return TRUE;
8460 #define BFD_QNT_CORE_INFO 7
8461 #define BFD_QNT_CORE_STATUS 8
8462 #define BFD_QNT_CORE_GREG 9
8463 #define BFD_QNT_CORE_FPREG 10
8465 static bfd_boolean
8466 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
8468 /* Every GREG section has a STATUS section before it. Store the
8469 tid from the previous call to pass down to the next gregs
8470 function. */
8471 static long tid = 1;
8473 switch (note->type)
8475 case BFD_QNT_CORE_INFO:
8476 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
8477 case BFD_QNT_CORE_STATUS:
8478 return elfcore_grok_nto_status (abfd, note, &tid);
8479 case BFD_QNT_CORE_GREG:
8480 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
8481 case BFD_QNT_CORE_FPREG:
8482 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
8483 default:
8484 return TRUE;
8488 static bfd_boolean
8489 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
8491 char *name;
8492 asection *sect;
8493 size_t len;
8495 /* Use note name as section name. */
8496 len = note->namesz;
8497 name = (char *) bfd_alloc (abfd, len);
8498 if (name == NULL)
8499 return FALSE;
8500 memcpy (name, note->namedata, len);
8501 name[len - 1] = '\0';
8503 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8504 if (sect == NULL)
8505 return FALSE;
8507 sect->size = note->descsz;
8508 sect->filepos = note->descpos;
8509 sect->alignment_power = 1;
8511 return TRUE;
8514 /* Function: elfcore_write_note
8516 Inputs:
8517 buffer to hold note, and current size of buffer
8518 name of note
8519 type of note
8520 data for note
8521 size of data for note
8523 Writes note to end of buffer. ELF64 notes are written exactly as
8524 for ELF32, despite the current (as of 2006) ELF gabi specifying
8525 that they ought to have 8-byte namesz and descsz field, and have
8526 8-byte alignment. Other writers, eg. Linux kernel, do the same.
8528 Return:
8529 Pointer to realloc'd buffer, *BUFSIZ updated. */
8531 char *
8532 elfcore_write_note (bfd *abfd,
8533 char *buf,
8534 int *bufsiz,
8535 const char *name,
8536 int type,
8537 const void *input,
8538 int size)
8540 Elf_External_Note *xnp;
8541 size_t namesz;
8542 size_t newspace;
8543 char *dest;
8545 namesz = 0;
8546 if (name != NULL)
8547 namesz = strlen (name) + 1;
8549 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
8551 buf = (char *) realloc (buf, *bufsiz + newspace);
8552 if (buf == NULL)
8553 return buf;
8554 dest = buf + *bufsiz;
8555 *bufsiz += newspace;
8556 xnp = (Elf_External_Note *) dest;
8557 H_PUT_32 (abfd, namesz, xnp->namesz);
8558 H_PUT_32 (abfd, size, xnp->descsz);
8559 H_PUT_32 (abfd, type, xnp->type);
8560 dest = xnp->name;
8561 if (name != NULL)
8563 memcpy (dest, name, namesz);
8564 dest += namesz;
8565 while (namesz & 3)
8567 *dest++ = '\0';
8568 ++namesz;
8571 memcpy (dest, input, size);
8572 dest += size;
8573 while (size & 3)
8575 *dest++ = '\0';
8576 ++size;
8578 return buf;
8581 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8582 char *
8583 elfcore_write_prpsinfo (bfd *abfd,
8584 char *buf,
8585 int *bufsiz,
8586 const char *fname,
8587 const char *psargs)
8589 const char *note_name = "CORE";
8590 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8592 if (bed->elf_backend_write_core_note != NULL)
8594 char *ret;
8595 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8596 NT_PRPSINFO, fname, psargs);
8597 if (ret != NULL)
8598 return ret;
8601 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8602 if (bed->s->elfclass == ELFCLASS32)
8604 #if defined (HAVE_PSINFO32_T)
8605 psinfo32_t data;
8606 int note_type = NT_PSINFO;
8607 #else
8608 prpsinfo32_t data;
8609 int note_type = NT_PRPSINFO;
8610 #endif
8612 memset (&data, 0, sizeof (data));
8613 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8614 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8615 return elfcore_write_note (abfd, buf, bufsiz,
8616 note_name, note_type, &data, sizeof (data));
8618 else
8619 #endif
8621 #if defined (HAVE_PSINFO_T)
8622 psinfo_t data;
8623 int note_type = NT_PSINFO;
8624 #else
8625 prpsinfo_t data;
8626 int note_type = NT_PRPSINFO;
8627 #endif
8629 memset (&data, 0, sizeof (data));
8630 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8631 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8632 return elfcore_write_note (abfd, buf, bufsiz,
8633 note_name, note_type, &data, sizeof (data));
8636 #endif /* PSINFO_T or PRPSINFO_T */
8638 #if defined (HAVE_PRSTATUS_T)
8639 char *
8640 elfcore_write_prstatus (bfd *abfd,
8641 char *buf,
8642 int *bufsiz,
8643 long pid,
8644 int cursig,
8645 const void *gregs)
8647 const char *note_name = "CORE";
8648 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8650 if (bed->elf_backend_write_core_note != NULL)
8652 char *ret;
8653 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8654 NT_PRSTATUS,
8655 pid, cursig, gregs);
8656 if (ret != NULL)
8657 return ret;
8660 #if defined (HAVE_PRSTATUS32_T)
8661 if (bed->s->elfclass == ELFCLASS32)
8663 prstatus32_t prstat;
8665 memset (&prstat, 0, sizeof (prstat));
8666 prstat.pr_pid = pid;
8667 prstat.pr_cursig = cursig;
8668 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8669 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8670 NT_PRSTATUS, &prstat, sizeof (prstat));
8672 else
8673 #endif
8675 prstatus_t prstat;
8677 memset (&prstat, 0, sizeof (prstat));
8678 prstat.pr_pid = pid;
8679 prstat.pr_cursig = cursig;
8680 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8681 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8682 NT_PRSTATUS, &prstat, sizeof (prstat));
8685 #endif /* HAVE_PRSTATUS_T */
8687 #if defined (HAVE_LWPSTATUS_T)
8688 char *
8689 elfcore_write_lwpstatus (bfd *abfd,
8690 char *buf,
8691 int *bufsiz,
8692 long pid,
8693 int cursig,
8694 const void *gregs)
8696 lwpstatus_t lwpstat;
8697 const char *note_name = "CORE";
8699 memset (&lwpstat, 0, sizeof (lwpstat));
8700 lwpstat.pr_lwpid = pid >> 16;
8701 lwpstat.pr_cursig = cursig;
8702 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8703 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
8704 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8705 #if !defined(gregs)
8706 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
8707 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
8708 #else
8709 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
8710 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
8711 #endif
8712 #endif
8713 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8714 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
8716 #endif /* HAVE_LWPSTATUS_T */
8718 #if defined (HAVE_PSTATUS_T)
8719 char *
8720 elfcore_write_pstatus (bfd *abfd,
8721 char *buf,
8722 int *bufsiz,
8723 long pid,
8724 int cursig ATTRIBUTE_UNUSED,
8725 const void *gregs ATTRIBUTE_UNUSED)
8727 const char *note_name = "CORE";
8728 #if defined (HAVE_PSTATUS32_T)
8729 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8731 if (bed->s->elfclass == ELFCLASS32)
8733 pstatus32_t pstat;
8735 memset (&pstat, 0, sizeof (pstat));
8736 pstat.pr_pid = pid & 0xffff;
8737 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8738 NT_PSTATUS, &pstat, sizeof (pstat));
8739 return buf;
8741 else
8742 #endif
8744 pstatus_t pstat;
8746 memset (&pstat, 0, sizeof (pstat));
8747 pstat.pr_pid = pid & 0xffff;
8748 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8749 NT_PSTATUS, &pstat, sizeof (pstat));
8750 return buf;
8753 #endif /* HAVE_PSTATUS_T */
8755 char *
8756 elfcore_write_prfpreg (bfd *abfd,
8757 char *buf,
8758 int *bufsiz,
8759 const void *fpregs,
8760 int size)
8762 const char *note_name = "CORE";
8763 return elfcore_write_note (abfd, buf, bufsiz,
8764 note_name, NT_FPREGSET, fpregs, size);
8767 char *
8768 elfcore_write_prxfpreg (bfd *abfd,
8769 char *buf,
8770 int *bufsiz,
8771 const void *xfpregs,
8772 int size)
8774 char *note_name = "LINUX";
8775 return elfcore_write_note (abfd, buf, bufsiz,
8776 note_name, NT_PRXFPREG, xfpregs, size);
8779 char *
8780 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
8781 const void *xfpregs, int size)
8783 char *note_name = "LINUX";
8784 return elfcore_write_note (abfd, buf, bufsiz,
8785 note_name, NT_X86_XSTATE, xfpregs, size);
8788 char *
8789 elfcore_write_ppc_vmx (bfd *abfd,
8790 char *buf,
8791 int *bufsiz,
8792 const void *ppc_vmx,
8793 int size)
8795 char *note_name = "LINUX";
8796 return elfcore_write_note (abfd, buf, bufsiz,
8797 note_name, NT_PPC_VMX, ppc_vmx, size);
8800 char *
8801 elfcore_write_ppc_vsx (bfd *abfd,
8802 char *buf,
8803 int *bufsiz,
8804 const void *ppc_vsx,
8805 int size)
8807 char *note_name = "LINUX";
8808 return elfcore_write_note (abfd, buf, bufsiz,
8809 note_name, NT_PPC_VSX, ppc_vsx, size);
8812 static char *
8813 elfcore_write_s390_high_gprs (bfd *abfd,
8814 char *buf,
8815 int *bufsiz,
8816 const void *s390_high_gprs,
8817 int size)
8819 char *note_name = "LINUX";
8820 return elfcore_write_note (abfd, buf, bufsiz,
8821 note_name, NT_S390_HIGH_GPRS,
8822 s390_high_gprs, size);
8825 char *
8826 elfcore_write_s390_timer (bfd *abfd,
8827 char *buf,
8828 int *bufsiz,
8829 const void *s390_timer,
8830 int size)
8832 char *note_name = "LINUX";
8833 return elfcore_write_note (abfd, buf, bufsiz,
8834 note_name, NT_S390_TIMER, s390_timer, size);
8837 char *
8838 elfcore_write_s390_todcmp (bfd *abfd,
8839 char *buf,
8840 int *bufsiz,
8841 const void *s390_todcmp,
8842 int size)
8844 char *note_name = "LINUX";
8845 return elfcore_write_note (abfd, buf, bufsiz,
8846 note_name, NT_S390_TODCMP, s390_todcmp, size);
8849 char *
8850 elfcore_write_s390_todpreg (bfd *abfd,
8851 char *buf,
8852 int *bufsiz,
8853 const void *s390_todpreg,
8854 int size)
8856 char *note_name = "LINUX";
8857 return elfcore_write_note (abfd, buf, bufsiz,
8858 note_name, NT_S390_TODPREG, s390_todpreg, size);
8861 char *
8862 elfcore_write_s390_ctrs (bfd *abfd,
8863 char *buf,
8864 int *bufsiz,
8865 const void *s390_ctrs,
8866 int size)
8868 char *note_name = "LINUX";
8869 return elfcore_write_note (abfd, buf, bufsiz,
8870 note_name, NT_S390_CTRS, s390_ctrs, size);
8873 char *
8874 elfcore_write_s390_prefix (bfd *abfd,
8875 char *buf,
8876 int *bufsiz,
8877 const void *s390_prefix,
8878 int size)
8880 char *note_name = "LINUX";
8881 return elfcore_write_note (abfd, buf, bufsiz,
8882 note_name, NT_S390_PREFIX, s390_prefix, size);
8885 char *
8886 elfcore_write_register_note (bfd *abfd,
8887 char *buf,
8888 int *bufsiz,
8889 const char *section,
8890 const void *data,
8891 int size)
8893 if (strcmp (section, ".reg2") == 0)
8894 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
8895 if (strcmp (section, ".reg-xfp") == 0)
8896 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
8897 if (strcmp (section, ".reg-xstate") == 0)
8898 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
8899 if (strcmp (section, ".reg-ppc-vmx") == 0)
8900 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
8901 if (strcmp (section, ".reg-ppc-vsx") == 0)
8902 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
8903 if (strcmp (section, ".reg-s390-high-gprs") == 0)
8904 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
8905 if (strcmp (section, ".reg-s390-timer") == 0)
8906 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
8907 if (strcmp (section, ".reg-s390-todcmp") == 0)
8908 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
8909 if (strcmp (section, ".reg-s390-todpreg") == 0)
8910 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
8911 if (strcmp (section, ".reg-s390-ctrs") == 0)
8912 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
8913 if (strcmp (section, ".reg-s390-prefix") == 0)
8914 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
8915 return NULL;
8918 static bfd_boolean
8919 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
8921 char *p;
8923 p = buf;
8924 while (p < buf + size)
8926 /* FIXME: bad alignment assumption. */
8927 Elf_External_Note *xnp = (Elf_External_Note *) p;
8928 Elf_Internal_Note in;
8930 if (offsetof (Elf_External_Note, name) > buf - p + size)
8931 return FALSE;
8933 in.type = H_GET_32 (abfd, xnp->type);
8935 in.namesz = H_GET_32 (abfd, xnp->namesz);
8936 in.namedata = xnp->name;
8937 if (in.namesz > buf - in.namedata + size)
8938 return FALSE;
8940 in.descsz = H_GET_32 (abfd, xnp->descsz);
8941 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
8942 in.descpos = offset + (in.descdata - buf);
8943 if (in.descsz != 0
8944 && (in.descdata >= buf + size
8945 || in.descsz > buf - in.descdata + size))
8946 return FALSE;
8948 switch (bfd_get_format (abfd))
8950 default:
8951 return TRUE;
8953 case bfd_core:
8954 if (CONST_STRNEQ (in.namedata, "NetBSD-CORE"))
8956 if (! elfcore_grok_netbsd_note (abfd, &in))
8957 return FALSE;
8959 else if (CONST_STRNEQ (in.namedata, "OpenBSD"))
8961 if (! elfcore_grok_openbsd_note (abfd, &in))
8962 return FALSE;
8964 else if (CONST_STRNEQ (in.namedata, "QNX"))
8966 if (! elfcore_grok_nto_note (abfd, &in))
8967 return FALSE;
8969 else if (CONST_STRNEQ (in.namedata, "SPU/"))
8971 if (! elfcore_grok_spu_note (abfd, &in))
8972 return FALSE;
8974 else
8976 if (! elfcore_grok_note (abfd, &in))
8977 return FALSE;
8979 break;
8981 case bfd_object:
8982 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
8984 if (! elfobj_grok_gnu_note (abfd, &in))
8985 return FALSE;
8987 break;
8990 p = in.descdata + BFD_ALIGN (in.descsz, 4);
8993 return TRUE;
8996 static bfd_boolean
8997 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
8999 char *buf;
9001 if (size <= 0)
9002 return TRUE;
9004 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
9005 return FALSE;
9007 buf = (char *) bfd_malloc (size);
9008 if (buf == NULL)
9009 return FALSE;
9011 if (bfd_bread (buf, size, abfd) != size
9012 || !elf_parse_notes (abfd, buf, size, offset))
9014 free (buf);
9015 return FALSE;
9018 free (buf);
9019 return TRUE;
9022 /* Providing external access to the ELF program header table. */
9024 /* Return an upper bound on the number of bytes required to store a
9025 copy of ABFD's program header table entries. Return -1 if an error
9026 occurs; bfd_get_error will return an appropriate code. */
9028 long
9029 bfd_get_elf_phdr_upper_bound (bfd *abfd)
9031 if (abfd->xvec->flavour != bfd_target_elf_flavour)
9033 bfd_set_error (bfd_error_wrong_format);
9034 return -1;
9037 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
9040 /* Copy ABFD's program header table entries to *PHDRS. The entries
9041 will be stored as an array of Elf_Internal_Phdr structures, as
9042 defined in include/elf/internal.h. To find out how large the
9043 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
9045 Return the number of program header table entries read, or -1 if an
9046 error occurs; bfd_get_error will return an appropriate code. */
9049 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
9051 int num_phdrs;
9053 if (abfd->xvec->flavour != bfd_target_elf_flavour)
9055 bfd_set_error (bfd_error_wrong_format);
9056 return -1;
9059 num_phdrs = elf_elfheader (abfd)->e_phnum;
9060 memcpy (phdrs, elf_tdata (abfd)->phdr,
9061 num_phdrs * sizeof (Elf_Internal_Phdr));
9063 return num_phdrs;
9066 enum elf_reloc_type_class
9067 _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
9069 return reloc_class_normal;
9072 /* For RELA architectures, return the relocation value for a
9073 relocation against a local symbol. */
9075 bfd_vma
9076 _bfd_elf_rela_local_sym (bfd *abfd,
9077 Elf_Internal_Sym *sym,
9078 asection **psec,
9079 Elf_Internal_Rela *rel)
9081 asection *sec = *psec;
9082 bfd_vma relocation;
9084 relocation = (sec->output_section->vma
9085 + sec->output_offset
9086 + sym->st_value);
9087 if ((sec->flags & SEC_MERGE)
9088 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
9089 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
9091 rel->r_addend =
9092 _bfd_merged_section_offset (abfd, psec,
9093 elf_section_data (sec)->sec_info,
9094 sym->st_value + rel->r_addend);
9095 if (sec != *psec)
9097 /* If we have changed the section, and our original section is
9098 marked with SEC_EXCLUDE, it means that the original
9099 SEC_MERGE section has been completely subsumed in some
9100 other SEC_MERGE section. In this case, we need to leave
9101 some info around for --emit-relocs. */
9102 if ((sec->flags & SEC_EXCLUDE) != 0)
9103 sec->kept_section = *psec;
9104 sec = *psec;
9106 rel->r_addend -= relocation;
9107 rel->r_addend += sec->output_section->vma + sec->output_offset;
9109 return relocation;
9112 bfd_vma
9113 _bfd_elf_rel_local_sym (bfd *abfd,
9114 Elf_Internal_Sym *sym,
9115 asection **psec,
9116 bfd_vma addend)
9118 asection *sec = *psec;
9120 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
9121 return sym->st_value + addend;
9123 return _bfd_merged_section_offset (abfd, psec,
9124 elf_section_data (sec)->sec_info,
9125 sym->st_value + addend);
9128 bfd_vma
9129 _bfd_elf_section_offset (bfd *abfd,
9130 struct bfd_link_info *info,
9131 asection *sec,
9132 bfd_vma offset)
9134 switch (sec->sec_info_type)
9136 case ELF_INFO_TYPE_STABS:
9137 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
9138 offset);
9139 case ELF_INFO_TYPE_EH_FRAME:
9140 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
9141 default:
9142 return offset;
9146 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
9147 reconstruct an ELF file by reading the segments out of remote memory
9148 based on the ELF file header at EHDR_VMA and the ELF program headers it
9149 points to. If not null, *LOADBASEP is filled in with the difference
9150 between the VMAs from which the segments were read, and the VMAs the
9151 file headers (and hence BFD's idea of each section's VMA) put them at.
9153 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
9154 remote memory at target address VMA into the local buffer at MYADDR; it
9155 should return zero on success or an `errno' code on failure. TEMPL must
9156 be a BFD for an ELF target with the word size and byte order found in
9157 the remote memory. */
9159 bfd *
9160 bfd_elf_bfd_from_remote_memory
9161 (bfd *templ,
9162 bfd_vma ehdr_vma,
9163 bfd_vma *loadbasep,
9164 int (*target_read_memory) (bfd_vma, bfd_byte *, int))
9166 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
9167 (templ, ehdr_vma, loadbasep, target_read_memory);
9170 long
9171 _bfd_elf_get_synthetic_symtab (bfd *abfd,
9172 long symcount ATTRIBUTE_UNUSED,
9173 asymbol **syms ATTRIBUTE_UNUSED,
9174 long dynsymcount,
9175 asymbol **dynsyms,
9176 asymbol **ret)
9178 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9179 asection *relplt;
9180 asymbol *s;
9181 const char *relplt_name;
9182 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
9183 arelent *p;
9184 long count, i, n;
9185 size_t size;
9186 Elf_Internal_Shdr *hdr;
9187 char *names;
9188 asection *plt;
9190 *ret = NULL;
9192 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
9193 return 0;
9195 if (dynsymcount <= 0)
9196 return 0;
9198 if (!bed->plt_sym_val)
9199 return 0;
9201 relplt_name = bed->relplt_name;
9202 if (relplt_name == NULL)
9203 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
9204 relplt = bfd_get_section_by_name (abfd, relplt_name);
9205 if (relplt == NULL)
9206 return 0;
9208 hdr = &elf_section_data (relplt)->this_hdr;
9209 if (hdr->sh_link != elf_dynsymtab (abfd)
9210 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
9211 return 0;
9213 plt = bfd_get_section_by_name (abfd, ".plt");
9214 if (plt == NULL)
9215 return 0;
9217 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
9218 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
9219 return -1;
9221 count = relplt->size / hdr->sh_entsize;
9222 size = count * sizeof (asymbol);
9223 p = relplt->relocation;
9224 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
9226 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
9227 if (p->addend != 0)
9229 #ifdef BFD64
9230 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
9231 #else
9232 size += sizeof ("+0x") - 1 + 8;
9233 #endif
9237 s = *ret = (asymbol *) bfd_malloc (size);
9238 if (s == NULL)
9239 return -1;
9241 names = (char *) (s + count);
9242 p = relplt->relocation;
9243 n = 0;
9244 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
9246 size_t len;
9247 bfd_vma addr;
9249 addr = bed->plt_sym_val (i, plt, p);
9250 if (addr == (bfd_vma) -1)
9251 continue;
9253 *s = **p->sym_ptr_ptr;
9254 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
9255 we are defining a symbol, ensure one of them is set. */
9256 if ((s->flags & BSF_LOCAL) == 0)
9257 s->flags |= BSF_GLOBAL;
9258 s->flags |= BSF_SYNTHETIC;
9259 s->section = plt;
9260 s->value = addr - plt->vma;
9261 s->name = names;
9262 s->udata.p = NULL;
9263 len = strlen ((*p->sym_ptr_ptr)->name);
9264 memcpy (names, (*p->sym_ptr_ptr)->name, len);
9265 names += len;
9266 if (p->addend != 0)
9268 char buf[30], *a;
9270 memcpy (names, "+0x", sizeof ("+0x") - 1);
9271 names += sizeof ("+0x") - 1;
9272 bfd_sprintf_vma (abfd, buf, p->addend);
9273 for (a = buf; *a == '0'; ++a)
9275 len = strlen (a);
9276 memcpy (names, a, len);
9277 names += len;
9279 memcpy (names, "@plt", sizeof ("@plt"));
9280 names += sizeof ("@plt");
9281 ++s, ++n;
9284 return n;
9287 /* It is only used by x86-64 so far. */
9288 asection _bfd_elf_large_com_section
9289 = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
9290 SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
9292 void
9293 _bfd_elf_set_osabi (bfd * abfd,
9294 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
9296 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
9298 i_ehdrp = elf_elfheader (abfd);
9300 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
9302 /* To make things simpler for the loader on Linux systems we set the
9303 osabi field to ELFOSABI_LINUX if the binary contains symbols of
9304 the STT_GNU_IFUNC type. */
9305 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE
9306 && elf_tdata (abfd)->has_ifunc_symbols)
9307 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_LINUX;
9311 /* Return TRUE for ELF symbol types that represent functions.
9312 This is the default version of this function, which is sufficient for
9313 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
9315 bfd_boolean
9316 _bfd_elf_is_function_type (unsigned int type)
9318 return (type == STT_FUNC
9319 || type == STT_GNU_IFUNC);