* elf.c (_bfd_elf_map_sections_to_segments): Don't load program
[binutils.git] / bfd / elf.c
blob9c56e2e7b1f7c4f0bacad9f32c715a526d70f574
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;
879 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
880 flags |= SEC_EXCLUDE;
882 if ((flags & SEC_ALLOC) == 0)
884 /* The debugging sections appear to be recognized only by name,
885 not any sort of flag. Their SEC_ALLOC bits are cleared. */
886 static const struct
888 const char *name;
889 int len;
890 } debug_sections [] =
892 { STRING_COMMA_LEN ("debug") }, /* 'd' */
893 { NULL, 0 }, /* 'e' */
894 { NULL, 0 }, /* 'f' */
895 { STRING_COMMA_LEN ("gnu.linkonce.wi.") }, /* 'g' */
896 { NULL, 0 }, /* 'h' */
897 { NULL, 0 }, /* 'i' */
898 { NULL, 0 }, /* 'j' */
899 { NULL, 0 }, /* 'k' */
900 { STRING_COMMA_LEN ("line") }, /* 'l' */
901 { NULL, 0 }, /* 'm' */
902 { NULL, 0 }, /* 'n' */
903 { NULL, 0 }, /* 'o' */
904 { NULL, 0 }, /* 'p' */
905 { NULL, 0 }, /* 'q' */
906 { NULL, 0 }, /* 'r' */
907 { STRING_COMMA_LEN ("stab") }, /* 's' */
908 { NULL, 0 }, /* 't' */
909 { NULL, 0 }, /* 'u' */
910 { NULL, 0 }, /* 'v' */
911 { NULL, 0 }, /* 'w' */
912 { NULL, 0 }, /* 'x' */
913 { NULL, 0 }, /* 'y' */
914 { STRING_COMMA_LEN ("zdebug") } /* 'z' */
917 if (name [0] == '.')
919 int i = name [1] - 'd';
920 if (i >= 0
921 && i < (int) ARRAY_SIZE (debug_sections)
922 && debug_sections [i].name != NULL
923 && strncmp (&name [1], debug_sections [i].name,
924 debug_sections [i].len) == 0)
925 flags |= SEC_DEBUGGING;
929 /* As a GNU extension, if the name begins with .gnu.linkonce, we
930 only link a single copy of the section. This is used to support
931 g++. g++ will emit each template expansion in its own section.
932 The symbols will be defined as weak, so that multiple definitions
933 are permitted. The GNU linker extension is to actually discard
934 all but one of the sections. */
935 if (CONST_STRNEQ (name, ".gnu.linkonce")
936 && elf_next_in_group (newsect) == NULL)
937 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
939 bed = get_elf_backend_data (abfd);
940 if (bed->elf_backend_section_flags)
941 if (! bed->elf_backend_section_flags (&flags, hdr))
942 return FALSE;
944 if (! bfd_set_section_flags (abfd, newsect, flags))
945 return FALSE;
947 /* We do not parse the PT_NOTE segments as we are interested even in the
948 separate debug info files which may have the segments offsets corrupted.
949 PT_NOTEs from the core files are currently not parsed using BFD. */
950 if (hdr->sh_type == SHT_NOTE)
952 bfd_byte *contents;
954 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
955 return FALSE;
957 elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1);
958 free (contents);
961 if ((flags & SEC_ALLOC) != 0)
963 Elf_Internal_Phdr *phdr;
964 unsigned int i, nload;
966 /* Some ELF linkers produce binaries with all the program header
967 p_paddr fields zero. If we have such a binary with more than
968 one PT_LOAD header, then leave the section lma equal to vma
969 so that we don't create sections with overlapping lma. */
970 phdr = elf_tdata (abfd)->phdr;
971 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
972 if (phdr->p_paddr != 0)
973 break;
974 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
975 ++nload;
976 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
977 return TRUE;
979 phdr = elf_tdata (abfd)->phdr;
980 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
982 if (phdr->p_type == PT_LOAD
983 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
985 if ((flags & SEC_LOAD) == 0)
986 newsect->lma = (phdr->p_paddr
987 + hdr->sh_addr - phdr->p_vaddr);
988 else
989 /* We used to use the same adjustment for SEC_LOAD
990 sections, but that doesn't work if the segment
991 is packed with code from multiple VMAs.
992 Instead we calculate the section LMA based on
993 the segment LMA. It is assumed that the
994 segment will contain sections with contiguous
995 LMAs, even if the VMAs are not. */
996 newsect->lma = (phdr->p_paddr
997 + hdr->sh_offset - phdr->p_offset);
999 /* With contiguous segments, we can't tell from file
1000 offsets whether a section with zero size should
1001 be placed at the end of one segment or the
1002 beginning of the next. Decide based on vaddr. */
1003 if (hdr->sh_addr >= phdr->p_vaddr
1004 && (hdr->sh_addr + hdr->sh_size
1005 <= phdr->p_vaddr + phdr->p_memsz))
1006 break;
1011 return TRUE;
1014 const char *const bfd_elf_section_type_names[] = {
1015 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1016 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1017 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1020 /* ELF relocs are against symbols. If we are producing relocatable
1021 output, and the reloc is against an external symbol, and nothing
1022 has given us any additional addend, the resulting reloc will also
1023 be against the same symbol. In such a case, we don't want to
1024 change anything about the way the reloc is handled, since it will
1025 all be done at final link time. Rather than put special case code
1026 into bfd_perform_relocation, all the reloc types use this howto
1027 function. It just short circuits the reloc if producing
1028 relocatable output against an external symbol. */
1030 bfd_reloc_status_type
1031 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1032 arelent *reloc_entry,
1033 asymbol *symbol,
1034 void *data ATTRIBUTE_UNUSED,
1035 asection *input_section,
1036 bfd *output_bfd,
1037 char **error_message ATTRIBUTE_UNUSED)
1039 if (output_bfd != NULL
1040 && (symbol->flags & BSF_SECTION_SYM) == 0
1041 && (! reloc_entry->howto->partial_inplace
1042 || reloc_entry->addend == 0))
1044 reloc_entry->address += input_section->output_offset;
1045 return bfd_reloc_ok;
1048 return bfd_reloc_continue;
1051 /* Copy the program header and other data from one object module to
1052 another. */
1054 bfd_boolean
1055 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1057 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1058 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1059 return TRUE;
1061 BFD_ASSERT (!elf_flags_init (obfd)
1062 || (elf_elfheader (obfd)->e_flags
1063 == elf_elfheader (ibfd)->e_flags));
1065 elf_gp (obfd) = elf_gp (ibfd);
1066 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1067 elf_flags_init (obfd) = TRUE;
1069 /* Copy object attributes. */
1070 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1071 return TRUE;
1074 static const char *
1075 get_segment_type (unsigned int p_type)
1077 const char *pt;
1078 switch (p_type)
1080 case PT_NULL: pt = "NULL"; break;
1081 case PT_LOAD: pt = "LOAD"; break;
1082 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1083 case PT_INTERP: pt = "INTERP"; break;
1084 case PT_NOTE: pt = "NOTE"; break;
1085 case PT_SHLIB: pt = "SHLIB"; break;
1086 case PT_PHDR: pt = "PHDR"; break;
1087 case PT_TLS: pt = "TLS"; break;
1088 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1089 case PT_GNU_STACK: pt = "STACK"; break;
1090 case PT_GNU_RELRO: pt = "RELRO"; break;
1091 default: pt = NULL; break;
1093 return pt;
1096 /* Print out the program headers. */
1098 bfd_boolean
1099 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1101 FILE *f = (FILE *) farg;
1102 Elf_Internal_Phdr *p;
1103 asection *s;
1104 bfd_byte *dynbuf = NULL;
1106 p = elf_tdata (abfd)->phdr;
1107 if (p != NULL)
1109 unsigned int i, c;
1111 fprintf (f, _("\nProgram Header:\n"));
1112 c = elf_elfheader (abfd)->e_phnum;
1113 for (i = 0; i < c; i++, p++)
1115 const char *pt = get_segment_type (p->p_type);
1116 char buf[20];
1118 if (pt == NULL)
1120 sprintf (buf, "0x%lx", p->p_type);
1121 pt = buf;
1123 fprintf (f, "%8s off 0x", pt);
1124 bfd_fprintf_vma (abfd, f, p->p_offset);
1125 fprintf (f, " vaddr 0x");
1126 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1127 fprintf (f, " paddr 0x");
1128 bfd_fprintf_vma (abfd, f, p->p_paddr);
1129 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1130 fprintf (f, " filesz 0x");
1131 bfd_fprintf_vma (abfd, f, p->p_filesz);
1132 fprintf (f, " memsz 0x");
1133 bfd_fprintf_vma (abfd, f, p->p_memsz);
1134 fprintf (f, " flags %c%c%c",
1135 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1136 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1137 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1138 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1139 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1140 fprintf (f, "\n");
1144 s = bfd_get_section_by_name (abfd, ".dynamic");
1145 if (s != NULL)
1147 unsigned int elfsec;
1148 unsigned long shlink;
1149 bfd_byte *extdyn, *extdynend;
1150 size_t extdynsize;
1151 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1153 fprintf (f, _("\nDynamic Section:\n"));
1155 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1156 goto error_return;
1158 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1159 if (elfsec == SHN_BAD)
1160 goto error_return;
1161 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1163 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1164 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1166 extdyn = dynbuf;
1167 extdynend = extdyn + s->size;
1168 for (; extdyn < extdynend; extdyn += extdynsize)
1170 Elf_Internal_Dyn dyn;
1171 const char *name = "";
1172 char ab[20];
1173 bfd_boolean stringp;
1174 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1176 (*swap_dyn_in) (abfd, extdyn, &dyn);
1178 if (dyn.d_tag == DT_NULL)
1179 break;
1181 stringp = FALSE;
1182 switch (dyn.d_tag)
1184 default:
1185 if (bed->elf_backend_get_target_dtag)
1186 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1188 if (!strcmp (name, ""))
1190 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1191 name = ab;
1193 break;
1195 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1196 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1197 case DT_PLTGOT: name = "PLTGOT"; break;
1198 case DT_HASH: name = "HASH"; break;
1199 case DT_STRTAB: name = "STRTAB"; break;
1200 case DT_SYMTAB: name = "SYMTAB"; break;
1201 case DT_RELA: name = "RELA"; break;
1202 case DT_RELASZ: name = "RELASZ"; break;
1203 case DT_RELAENT: name = "RELAENT"; break;
1204 case DT_STRSZ: name = "STRSZ"; break;
1205 case DT_SYMENT: name = "SYMENT"; break;
1206 case DT_INIT: name = "INIT"; break;
1207 case DT_FINI: name = "FINI"; break;
1208 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1209 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1210 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1211 case DT_REL: name = "REL"; break;
1212 case DT_RELSZ: name = "RELSZ"; break;
1213 case DT_RELENT: name = "RELENT"; break;
1214 case DT_PLTREL: name = "PLTREL"; break;
1215 case DT_DEBUG: name = "DEBUG"; break;
1216 case DT_TEXTREL: name = "TEXTREL"; break;
1217 case DT_JMPREL: name = "JMPREL"; break;
1218 case DT_BIND_NOW: name = "BIND_NOW"; break;
1219 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1220 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1221 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1222 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1223 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1224 case DT_FLAGS: name = "FLAGS"; break;
1225 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1226 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1227 case DT_CHECKSUM: name = "CHECKSUM"; break;
1228 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1229 case DT_MOVEENT: name = "MOVEENT"; break;
1230 case DT_MOVESZ: name = "MOVESZ"; break;
1231 case DT_FEATURE: name = "FEATURE"; break;
1232 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1233 case DT_SYMINSZ: name = "SYMINSZ"; break;
1234 case DT_SYMINENT: name = "SYMINENT"; break;
1235 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1236 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1237 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1238 case DT_PLTPAD: name = "PLTPAD"; break;
1239 case DT_MOVETAB: name = "MOVETAB"; break;
1240 case DT_SYMINFO: name = "SYMINFO"; break;
1241 case DT_RELACOUNT: name = "RELACOUNT"; break;
1242 case DT_RELCOUNT: name = "RELCOUNT"; break;
1243 case DT_FLAGS_1: name = "FLAGS_1"; break;
1244 case DT_VERSYM: name = "VERSYM"; break;
1245 case DT_VERDEF: name = "VERDEF"; break;
1246 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1247 case DT_VERNEED: name = "VERNEED"; break;
1248 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1249 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1250 case DT_USED: name = "USED"; break;
1251 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1252 case DT_GNU_HASH: name = "GNU_HASH"; break;
1255 fprintf (f, " %-20s ", name);
1256 if (! stringp)
1258 fprintf (f, "0x");
1259 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1261 else
1263 const char *string;
1264 unsigned int tagv = dyn.d_un.d_val;
1266 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1267 if (string == NULL)
1268 goto error_return;
1269 fprintf (f, "%s", string);
1271 fprintf (f, "\n");
1274 free (dynbuf);
1275 dynbuf = NULL;
1278 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1279 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1281 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1282 return FALSE;
1285 if (elf_dynverdef (abfd) != 0)
1287 Elf_Internal_Verdef *t;
1289 fprintf (f, _("\nVersion definitions:\n"));
1290 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1292 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1293 t->vd_flags, t->vd_hash,
1294 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1295 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1297 Elf_Internal_Verdaux *a;
1299 fprintf (f, "\t");
1300 for (a = t->vd_auxptr->vda_nextptr;
1301 a != NULL;
1302 a = a->vda_nextptr)
1303 fprintf (f, "%s ",
1304 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1305 fprintf (f, "\n");
1310 if (elf_dynverref (abfd) != 0)
1312 Elf_Internal_Verneed *t;
1314 fprintf (f, _("\nVersion References:\n"));
1315 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1317 Elf_Internal_Vernaux *a;
1319 fprintf (f, _(" required from %s:\n"),
1320 t->vn_filename ? t->vn_filename : "<corrupt>");
1321 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1322 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1323 a->vna_flags, a->vna_other,
1324 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1328 return TRUE;
1330 error_return:
1331 if (dynbuf != NULL)
1332 free (dynbuf);
1333 return FALSE;
1336 /* Display ELF-specific fields of a symbol. */
1338 void
1339 bfd_elf_print_symbol (bfd *abfd,
1340 void *filep,
1341 asymbol *symbol,
1342 bfd_print_symbol_type how)
1344 FILE *file = (FILE *) filep;
1345 switch (how)
1347 case bfd_print_symbol_name:
1348 fprintf (file, "%s", symbol->name);
1349 break;
1350 case bfd_print_symbol_more:
1351 fprintf (file, "elf ");
1352 bfd_fprintf_vma (abfd, file, symbol->value);
1353 fprintf (file, " %lx", (unsigned long) symbol->flags);
1354 break;
1355 case bfd_print_symbol_all:
1357 const char *section_name;
1358 const char *name = NULL;
1359 const struct elf_backend_data *bed;
1360 unsigned char st_other;
1361 bfd_vma val;
1363 section_name = symbol->section ? symbol->section->name : "(*none*)";
1365 bed = get_elf_backend_data (abfd);
1366 if (bed->elf_backend_print_symbol_all)
1367 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1369 if (name == NULL)
1371 name = symbol->name;
1372 bfd_print_symbol_vandf (abfd, file, symbol);
1375 fprintf (file, " %s\t", section_name);
1376 /* Print the "other" value for a symbol. For common symbols,
1377 we've already printed the size; now print the alignment.
1378 For other symbols, we have no specified alignment, and
1379 we've printed the address; now print the size. */
1380 if (symbol->section && bfd_is_com_section (symbol->section))
1381 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1382 else
1383 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1384 bfd_fprintf_vma (abfd, file, val);
1386 /* If we have version information, print it. */
1387 if (elf_tdata (abfd)->dynversym_section != 0
1388 && (elf_tdata (abfd)->dynverdef_section != 0
1389 || elf_tdata (abfd)->dynverref_section != 0))
1391 unsigned int vernum;
1392 const char *version_string;
1394 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1396 if (vernum == 0)
1397 version_string = "";
1398 else if (vernum == 1)
1399 version_string = "Base";
1400 else if (vernum <= elf_tdata (abfd)->cverdefs)
1401 version_string =
1402 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1403 else
1405 Elf_Internal_Verneed *t;
1407 version_string = "";
1408 for (t = elf_tdata (abfd)->verref;
1409 t != NULL;
1410 t = t->vn_nextref)
1412 Elf_Internal_Vernaux *a;
1414 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1416 if (a->vna_other == vernum)
1418 version_string = a->vna_nodename;
1419 break;
1425 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1426 fprintf (file, " %-11s", version_string);
1427 else
1429 int i;
1431 fprintf (file, " (%s)", version_string);
1432 for (i = 10 - strlen (version_string); i > 0; --i)
1433 putc (' ', file);
1437 /* If the st_other field is not zero, print it. */
1438 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1440 switch (st_other)
1442 case 0: break;
1443 case STV_INTERNAL: fprintf (file, " .internal"); break;
1444 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1445 case STV_PROTECTED: fprintf (file, " .protected"); break;
1446 default:
1447 /* Some other non-defined flags are also present, so print
1448 everything hex. */
1449 fprintf (file, " 0x%02x", (unsigned int) st_other);
1452 fprintf (file, " %s", name);
1454 break;
1458 /* Allocate an ELF string table--force the first byte to be zero. */
1460 struct bfd_strtab_hash *
1461 _bfd_elf_stringtab_init (void)
1463 struct bfd_strtab_hash *ret;
1465 ret = _bfd_stringtab_init ();
1466 if (ret != NULL)
1468 bfd_size_type loc;
1470 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1471 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1472 if (loc == (bfd_size_type) -1)
1474 _bfd_stringtab_free (ret);
1475 ret = NULL;
1478 return ret;
1481 /* ELF .o/exec file reading */
1483 /* Create a new bfd section from an ELF section header. */
1485 bfd_boolean
1486 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1488 Elf_Internal_Shdr *hdr;
1489 Elf_Internal_Ehdr *ehdr;
1490 const struct elf_backend_data *bed;
1491 const char *name;
1493 if (shindex >= elf_numsections (abfd))
1494 return FALSE;
1496 hdr = elf_elfsections (abfd)[shindex];
1497 ehdr = elf_elfheader (abfd);
1498 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1499 hdr->sh_name);
1500 if (name == NULL)
1501 return FALSE;
1503 bed = get_elf_backend_data (abfd);
1504 switch (hdr->sh_type)
1506 case SHT_NULL:
1507 /* Inactive section. Throw it away. */
1508 return TRUE;
1510 case SHT_PROGBITS: /* Normal section with contents. */
1511 case SHT_NOBITS: /* .bss section. */
1512 case SHT_HASH: /* .hash section. */
1513 case SHT_NOTE: /* .note section. */
1514 case SHT_INIT_ARRAY: /* .init_array section. */
1515 case SHT_FINI_ARRAY: /* .fini_array section. */
1516 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1517 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
1518 case SHT_GNU_HASH: /* .gnu.hash section. */
1519 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1521 case SHT_DYNAMIC: /* Dynamic linking information. */
1522 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1523 return FALSE;
1524 if (hdr->sh_link > elf_numsections (abfd))
1526 /* PR 10478: Accept Solaris binaries with a sh_link
1527 field set to SHN_BEFORE or SHN_AFTER. */
1528 switch (bfd_get_arch (abfd))
1530 case bfd_arch_i386:
1531 case bfd_arch_sparc:
1532 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
1533 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
1534 break;
1535 /* Otherwise fall through. */
1536 default:
1537 return FALSE;
1540 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
1541 return FALSE;
1542 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1544 Elf_Internal_Shdr *dynsymhdr;
1546 /* The shared libraries distributed with hpux11 have a bogus
1547 sh_link field for the ".dynamic" section. Find the
1548 string table for the ".dynsym" section instead. */
1549 if (elf_dynsymtab (abfd) != 0)
1551 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1552 hdr->sh_link = dynsymhdr->sh_link;
1554 else
1556 unsigned int i, num_sec;
1558 num_sec = elf_numsections (abfd);
1559 for (i = 1; i < num_sec; i++)
1561 dynsymhdr = elf_elfsections (abfd)[i];
1562 if (dynsymhdr->sh_type == SHT_DYNSYM)
1564 hdr->sh_link = dynsymhdr->sh_link;
1565 break;
1570 break;
1572 case SHT_SYMTAB: /* A symbol table */
1573 if (elf_onesymtab (abfd) == shindex)
1574 return TRUE;
1576 if (hdr->sh_entsize != bed->s->sizeof_sym)
1577 return FALSE;
1578 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
1579 return FALSE;
1580 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1581 elf_onesymtab (abfd) = shindex;
1582 elf_tdata (abfd)->symtab_hdr = *hdr;
1583 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1584 abfd->flags |= HAS_SYMS;
1586 /* Sometimes a shared object will map in the symbol table. If
1587 SHF_ALLOC is set, and this is a shared object, then we also
1588 treat this section as a BFD section. We can not base the
1589 decision purely on SHF_ALLOC, because that flag is sometimes
1590 set in a relocatable object file, which would confuse the
1591 linker. */
1592 if ((hdr->sh_flags & SHF_ALLOC) != 0
1593 && (abfd->flags & DYNAMIC) != 0
1594 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1595 shindex))
1596 return FALSE;
1598 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1599 can't read symbols without that section loaded as well. It
1600 is most likely specified by the next section header. */
1601 if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex)
1603 unsigned int i, num_sec;
1605 num_sec = elf_numsections (abfd);
1606 for (i = shindex + 1; i < num_sec; i++)
1608 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1609 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1610 && hdr2->sh_link == shindex)
1611 break;
1613 if (i == num_sec)
1614 for (i = 1; i < shindex; i++)
1616 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1617 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1618 && hdr2->sh_link == shindex)
1619 break;
1621 if (i != shindex)
1622 return bfd_section_from_shdr (abfd, i);
1624 return TRUE;
1626 case SHT_DYNSYM: /* A dynamic symbol table */
1627 if (elf_dynsymtab (abfd) == shindex)
1628 return TRUE;
1630 if (hdr->sh_entsize != bed->s->sizeof_sym)
1631 return FALSE;
1632 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1633 elf_dynsymtab (abfd) = shindex;
1634 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1635 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1636 abfd->flags |= HAS_SYMS;
1638 /* Besides being a symbol table, we also treat this as a regular
1639 section, so that objcopy can handle it. */
1640 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1642 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1643 if (elf_symtab_shndx (abfd) == shindex)
1644 return TRUE;
1646 BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
1647 elf_symtab_shndx (abfd) = shindex;
1648 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1649 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1650 return TRUE;
1652 case SHT_STRTAB: /* A string table */
1653 if (hdr->bfd_section != NULL)
1654 return TRUE;
1655 if (ehdr->e_shstrndx == shindex)
1657 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1658 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1659 return TRUE;
1661 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
1663 symtab_strtab:
1664 elf_tdata (abfd)->strtab_hdr = *hdr;
1665 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
1666 return TRUE;
1668 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
1670 dynsymtab_strtab:
1671 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1672 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
1673 elf_elfsections (abfd)[shindex] = hdr;
1674 /* We also treat this as a regular section, so that objcopy
1675 can handle it. */
1676 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1677 shindex);
1680 /* If the string table isn't one of the above, then treat it as a
1681 regular section. We need to scan all the headers to be sure,
1682 just in case this strtab section appeared before the above. */
1683 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
1685 unsigned int i, num_sec;
1687 num_sec = elf_numsections (abfd);
1688 for (i = 1; i < num_sec; i++)
1690 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1691 if (hdr2->sh_link == shindex)
1693 /* Prevent endless recursion on broken objects. */
1694 if (i == shindex)
1695 return FALSE;
1696 if (! bfd_section_from_shdr (abfd, i))
1697 return FALSE;
1698 if (elf_onesymtab (abfd) == i)
1699 goto symtab_strtab;
1700 if (elf_dynsymtab (abfd) == i)
1701 goto dynsymtab_strtab;
1705 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1707 case SHT_REL:
1708 case SHT_RELA:
1709 /* *These* do a lot of work -- but build no sections! */
1711 asection *target_sect;
1712 Elf_Internal_Shdr *hdr2;
1713 unsigned int num_sec = elf_numsections (abfd);
1715 if (hdr->sh_entsize
1716 != (bfd_size_type) (hdr->sh_type == SHT_REL
1717 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
1718 return FALSE;
1720 /* Check for a bogus link to avoid crashing. */
1721 if (hdr->sh_link >= num_sec)
1723 ((*_bfd_error_handler)
1724 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1725 abfd, hdr->sh_link, name, shindex));
1726 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1727 shindex);
1730 /* For some incomprehensible reason Oracle distributes
1731 libraries for Solaris in which some of the objects have
1732 bogus sh_link fields. It would be nice if we could just
1733 reject them, but, unfortunately, some people need to use
1734 them. We scan through the section headers; if we find only
1735 one suitable symbol table, we clobber the sh_link to point
1736 to it. I hope this doesn't break anything.
1738 Don't do it on executable nor shared library. */
1739 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
1740 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1741 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1743 unsigned int scan;
1744 int found;
1746 found = 0;
1747 for (scan = 1; scan < num_sec; scan++)
1749 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1750 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1752 if (found != 0)
1754 found = 0;
1755 break;
1757 found = scan;
1760 if (found != 0)
1761 hdr->sh_link = found;
1764 /* Get the symbol table. */
1765 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1766 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
1767 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1768 return FALSE;
1770 /* If this reloc section does not use the main symbol table we
1771 don't treat it as a reloc section. BFD can't adequately
1772 represent such a section, so at least for now, we don't
1773 try. We just present it as a normal section. We also
1774 can't use it as a reloc section if it points to the null
1775 section, an invalid section, another reloc section, or its
1776 sh_link points to the null section. */
1777 if (hdr->sh_link != elf_onesymtab (abfd)
1778 || hdr->sh_link == SHN_UNDEF
1779 || hdr->sh_info == SHN_UNDEF
1780 || hdr->sh_info >= num_sec
1781 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
1782 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
1783 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1784 shindex);
1786 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1787 return FALSE;
1788 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1789 if (target_sect == NULL)
1790 return FALSE;
1792 if ((target_sect->flags & SEC_RELOC) == 0
1793 || target_sect->reloc_count == 0)
1794 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1795 else
1797 bfd_size_type amt;
1798 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1799 amt = sizeof (*hdr2);
1800 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
1801 if (hdr2 == NULL)
1802 return FALSE;
1803 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1805 *hdr2 = *hdr;
1806 elf_elfsections (abfd)[shindex] = hdr2;
1807 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1808 target_sect->flags |= SEC_RELOC;
1809 target_sect->relocation = NULL;
1810 target_sect->rel_filepos = hdr->sh_offset;
1811 /* In the section to which the relocations apply, mark whether
1812 its relocations are of the REL or RELA variety. */
1813 if (hdr->sh_size != 0)
1814 target_sect->use_rela_p = hdr->sh_type == SHT_RELA;
1815 abfd->flags |= HAS_RELOC;
1816 return TRUE;
1819 case SHT_GNU_verdef:
1820 elf_dynverdef (abfd) = shindex;
1821 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1822 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1824 case SHT_GNU_versym:
1825 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
1826 return FALSE;
1827 elf_dynversym (abfd) = shindex;
1828 elf_tdata (abfd)->dynversym_hdr = *hdr;
1829 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1831 case SHT_GNU_verneed:
1832 elf_dynverref (abfd) = shindex;
1833 elf_tdata (abfd)->dynverref_hdr = *hdr;
1834 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1836 case SHT_SHLIB:
1837 return TRUE;
1839 case SHT_GROUP:
1840 if (! IS_VALID_GROUP_SECTION_HEADER (hdr))
1841 return FALSE;
1842 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1843 return FALSE;
1844 if (hdr->contents != NULL)
1846 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1847 unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE;
1848 asection *s;
1850 if (idx->flags & GRP_COMDAT)
1851 hdr->bfd_section->flags
1852 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1854 /* We try to keep the same section order as it comes in. */
1855 idx += n_elt;
1856 while (--n_elt != 0)
1858 --idx;
1860 if (idx->shdr != NULL
1861 && (s = idx->shdr->bfd_section) != NULL
1862 && elf_next_in_group (s) != NULL)
1864 elf_next_in_group (hdr->bfd_section) = s;
1865 break;
1869 break;
1871 default:
1872 /* Possibly an attributes section. */
1873 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
1874 || hdr->sh_type == bed->obj_attrs_section_type)
1876 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1877 return FALSE;
1878 _bfd_elf_parse_attributes (abfd, hdr);
1879 return TRUE;
1882 /* Check for any processor-specific section types. */
1883 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
1884 return TRUE;
1886 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
1888 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1889 /* FIXME: How to properly handle allocated section reserved
1890 for applications? */
1891 (*_bfd_error_handler)
1892 (_("%B: don't know how to handle allocated, application "
1893 "specific section `%s' [0x%8x]"),
1894 abfd, name, hdr->sh_type);
1895 else
1896 /* Allow sections reserved for applications. */
1897 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1898 shindex);
1900 else if (hdr->sh_type >= SHT_LOPROC
1901 && hdr->sh_type <= SHT_HIPROC)
1902 /* FIXME: We should handle this section. */
1903 (*_bfd_error_handler)
1904 (_("%B: don't know how to handle processor specific section "
1905 "`%s' [0x%8x]"),
1906 abfd, name, hdr->sh_type);
1907 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
1909 /* Unrecognised OS-specific sections. */
1910 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
1911 /* SHF_OS_NONCONFORMING indicates that special knowledge is
1912 required to correctly process the section and the file should
1913 be rejected with an error message. */
1914 (*_bfd_error_handler)
1915 (_("%B: don't know how to handle OS specific section "
1916 "`%s' [0x%8x]"),
1917 abfd, name, hdr->sh_type);
1918 else
1919 /* Otherwise it should be processed. */
1920 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1922 else
1923 /* FIXME: We should handle this section. */
1924 (*_bfd_error_handler)
1925 (_("%B: don't know how to handle section `%s' [0x%8x]"),
1926 abfd, name, hdr->sh_type);
1928 return FALSE;
1931 return TRUE;
1934 /* Return the local symbol specified by ABFD, R_SYMNDX. */
1936 Elf_Internal_Sym *
1937 bfd_sym_from_r_symndx (struct sym_cache *cache,
1938 bfd *abfd,
1939 unsigned long r_symndx)
1941 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
1943 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
1945 Elf_Internal_Shdr *symtab_hdr;
1946 unsigned char esym[sizeof (Elf64_External_Sym)];
1947 Elf_External_Sym_Shndx eshndx;
1949 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1950 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
1951 &cache->sym[ent], esym, &eshndx) == NULL)
1952 return NULL;
1954 if (cache->abfd != abfd)
1956 memset (cache->indx, -1, sizeof (cache->indx));
1957 cache->abfd = abfd;
1959 cache->indx[ent] = r_symndx;
1962 return &cache->sym[ent];
1965 /* Given an ELF section number, retrieve the corresponding BFD
1966 section. */
1968 asection *
1969 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
1971 if (sec_index >= elf_numsections (abfd))
1972 return NULL;
1973 return elf_elfsections (abfd)[sec_index]->bfd_section;
1976 static const struct bfd_elf_special_section special_sections_b[] =
1978 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1979 { NULL, 0, 0, 0, 0 }
1982 static const struct bfd_elf_special_section special_sections_c[] =
1984 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
1985 { NULL, 0, 0, 0, 0 }
1988 static const struct bfd_elf_special_section special_sections_d[] =
1990 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1991 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1992 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
1993 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
1994 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
1995 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
1996 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
1997 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
1998 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
1999 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2000 { NULL, 0, 0, 0, 0 }
2003 static const struct bfd_elf_special_section special_sections_f[] =
2005 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2006 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2007 { NULL, 0, 0, 0, 0 }
2010 static const struct bfd_elf_special_section special_sections_g[] =
2012 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2013 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2014 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2015 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2016 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2017 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2018 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2019 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2020 { NULL, 0, 0, 0, 0 }
2023 static const struct bfd_elf_special_section special_sections_h[] =
2025 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2026 { NULL, 0, 0, 0, 0 }
2029 static const struct bfd_elf_special_section special_sections_i[] =
2031 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2032 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2033 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2034 { NULL, 0, 0, 0, 0 }
2037 static const struct bfd_elf_special_section special_sections_l[] =
2039 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2040 { NULL, 0, 0, 0, 0 }
2043 static const struct bfd_elf_special_section special_sections_n[] =
2045 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2046 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2047 { NULL, 0, 0, 0, 0 }
2050 static const struct bfd_elf_special_section special_sections_p[] =
2052 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2053 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2054 { NULL, 0, 0, 0, 0 }
2057 static const struct bfd_elf_special_section special_sections_r[] =
2059 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2060 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2061 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2062 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2063 { NULL, 0, 0, 0, 0 }
2066 static const struct bfd_elf_special_section special_sections_s[] =
2068 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2069 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2070 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2071 /* See struct bfd_elf_special_section declaration for the semantics of
2072 this special case where .prefix_length != strlen (.prefix). */
2073 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2074 { NULL, 0, 0, 0, 0 }
2077 static const struct bfd_elf_special_section special_sections_t[] =
2079 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2080 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2081 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2082 { NULL, 0, 0, 0, 0 }
2085 static const struct bfd_elf_special_section special_sections_z[] =
2087 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2088 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2089 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2090 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2091 { NULL, 0, 0, 0, 0 }
2094 static const struct bfd_elf_special_section *special_sections[] =
2096 special_sections_b, /* 'b' */
2097 special_sections_c, /* 'c' */
2098 special_sections_d, /* 'd' */
2099 NULL, /* 'e' */
2100 special_sections_f, /* 'f' */
2101 special_sections_g, /* 'g' */
2102 special_sections_h, /* 'h' */
2103 special_sections_i, /* 'i' */
2104 NULL, /* 'j' */
2105 NULL, /* 'k' */
2106 special_sections_l, /* 'l' */
2107 NULL, /* 'm' */
2108 special_sections_n, /* 'n' */
2109 NULL, /* 'o' */
2110 special_sections_p, /* 'p' */
2111 NULL, /* 'q' */
2112 special_sections_r, /* 'r' */
2113 special_sections_s, /* 's' */
2114 special_sections_t, /* 't' */
2115 NULL, /* 'u' */
2116 NULL, /* 'v' */
2117 NULL, /* 'w' */
2118 NULL, /* 'x' */
2119 NULL, /* 'y' */
2120 special_sections_z /* 'z' */
2123 const struct bfd_elf_special_section *
2124 _bfd_elf_get_special_section (const char *name,
2125 const struct bfd_elf_special_section *spec,
2126 unsigned int rela)
2128 int i;
2129 int len;
2131 len = strlen (name);
2133 for (i = 0; spec[i].prefix != NULL; i++)
2135 int suffix_len;
2136 int prefix_len = spec[i].prefix_length;
2138 if (len < prefix_len)
2139 continue;
2140 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2141 continue;
2143 suffix_len = spec[i].suffix_length;
2144 if (suffix_len <= 0)
2146 if (name[prefix_len] != 0)
2148 if (suffix_len == 0)
2149 continue;
2150 if (name[prefix_len] != '.'
2151 && (suffix_len == -2
2152 || (rela && spec[i].type == SHT_REL)))
2153 continue;
2156 else
2158 if (len < prefix_len + suffix_len)
2159 continue;
2160 if (memcmp (name + len - suffix_len,
2161 spec[i].prefix + prefix_len,
2162 suffix_len) != 0)
2163 continue;
2165 return &spec[i];
2168 return NULL;
2171 const struct bfd_elf_special_section *
2172 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2174 int i;
2175 const struct bfd_elf_special_section *spec;
2176 const struct elf_backend_data *bed;
2178 /* See if this is one of the special sections. */
2179 if (sec->name == NULL)
2180 return NULL;
2182 bed = get_elf_backend_data (abfd);
2183 spec = bed->special_sections;
2184 if (spec)
2186 spec = _bfd_elf_get_special_section (sec->name,
2187 bed->special_sections,
2188 sec->use_rela_p);
2189 if (spec != NULL)
2190 return spec;
2193 if (sec->name[0] != '.')
2194 return NULL;
2196 i = sec->name[1] - 'b';
2197 if (i < 0 || i > 'z' - 'b')
2198 return NULL;
2200 spec = special_sections[i];
2202 if (spec == NULL)
2203 return NULL;
2205 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2208 bfd_boolean
2209 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2211 struct bfd_elf_section_data *sdata;
2212 const struct elf_backend_data *bed;
2213 const struct bfd_elf_special_section *ssect;
2215 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2216 if (sdata == NULL)
2218 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2219 sizeof (*sdata));
2220 if (sdata == NULL)
2221 return FALSE;
2222 sec->used_by_bfd = sdata;
2225 /* Indicate whether or not this section should use RELA relocations. */
2226 bed = get_elf_backend_data (abfd);
2227 sec->use_rela_p = bed->default_use_rela_p;
2229 /* When we read a file, we don't need to set ELF section type and
2230 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2231 anyway. We will set ELF section type and flags for all linker
2232 created sections. If user specifies BFD section flags, we will
2233 set ELF section type and flags based on BFD section flags in
2234 elf_fake_sections. */
2235 if ((!sec->flags && abfd->direction != read_direction)
2236 || (sec->flags & SEC_LINKER_CREATED) != 0)
2238 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2239 if (ssect != NULL)
2241 elf_section_type (sec) = ssect->type;
2242 elf_section_flags (sec) = ssect->attr;
2246 return _bfd_generic_new_section_hook (abfd, sec);
2249 /* Create a new bfd section from an ELF program header.
2251 Since program segments have no names, we generate a synthetic name
2252 of the form segment<NUM>, where NUM is generally the index in the
2253 program header table. For segments that are split (see below) we
2254 generate the names segment<NUM>a and segment<NUM>b.
2256 Note that some program segments may have a file size that is different than
2257 (less than) the memory size. All this means is that at execution the
2258 system must allocate the amount of memory specified by the memory size,
2259 but only initialize it with the first "file size" bytes read from the
2260 file. This would occur for example, with program segments consisting
2261 of combined data+bss.
2263 To handle the above situation, this routine generates TWO bfd sections
2264 for the single program segment. The first has the length specified by
2265 the file size of the segment, and the second has the length specified
2266 by the difference between the two sizes. In effect, the segment is split
2267 into its initialized and uninitialized parts.
2271 bfd_boolean
2272 _bfd_elf_make_section_from_phdr (bfd *abfd,
2273 Elf_Internal_Phdr *hdr,
2274 int hdr_index,
2275 const char *type_name)
2277 asection *newsect;
2278 char *name;
2279 char namebuf[64];
2280 size_t len;
2281 int split;
2283 split = ((hdr->p_memsz > 0)
2284 && (hdr->p_filesz > 0)
2285 && (hdr->p_memsz > hdr->p_filesz));
2287 if (hdr->p_filesz > 0)
2289 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
2290 len = strlen (namebuf) + 1;
2291 name = (char *) bfd_alloc (abfd, len);
2292 if (!name)
2293 return FALSE;
2294 memcpy (name, namebuf, len);
2295 newsect = bfd_make_section (abfd, name);
2296 if (newsect == NULL)
2297 return FALSE;
2298 newsect->vma = hdr->p_vaddr;
2299 newsect->lma = hdr->p_paddr;
2300 newsect->size = hdr->p_filesz;
2301 newsect->filepos = hdr->p_offset;
2302 newsect->flags |= SEC_HAS_CONTENTS;
2303 newsect->alignment_power = bfd_log2 (hdr->p_align);
2304 if (hdr->p_type == PT_LOAD)
2306 newsect->flags |= SEC_ALLOC;
2307 newsect->flags |= SEC_LOAD;
2308 if (hdr->p_flags & PF_X)
2310 /* FIXME: all we known is that it has execute PERMISSION,
2311 may be data. */
2312 newsect->flags |= SEC_CODE;
2315 if (!(hdr->p_flags & PF_W))
2317 newsect->flags |= SEC_READONLY;
2321 if (hdr->p_memsz > hdr->p_filesz)
2323 bfd_vma align;
2325 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
2326 len = strlen (namebuf) + 1;
2327 name = (char *) bfd_alloc (abfd, len);
2328 if (!name)
2329 return FALSE;
2330 memcpy (name, namebuf, len);
2331 newsect = bfd_make_section (abfd, name);
2332 if (newsect == NULL)
2333 return FALSE;
2334 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2335 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2336 newsect->size = hdr->p_memsz - hdr->p_filesz;
2337 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2338 align = newsect->vma & -newsect->vma;
2339 if (align == 0 || align > hdr->p_align)
2340 align = hdr->p_align;
2341 newsect->alignment_power = bfd_log2 (align);
2342 if (hdr->p_type == PT_LOAD)
2344 /* Hack for gdb. Segments that have not been modified do
2345 not have their contents written to a core file, on the
2346 assumption that a debugger can find the contents in the
2347 executable. We flag this case by setting the fake
2348 section size to zero. Note that "real" bss sections will
2349 always have their contents dumped to the core file. */
2350 if (bfd_get_format (abfd) == bfd_core)
2351 newsect->size = 0;
2352 newsect->flags |= SEC_ALLOC;
2353 if (hdr->p_flags & PF_X)
2354 newsect->flags |= SEC_CODE;
2356 if (!(hdr->p_flags & PF_W))
2357 newsect->flags |= SEC_READONLY;
2360 return TRUE;
2363 bfd_boolean
2364 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
2366 const struct elf_backend_data *bed;
2368 switch (hdr->p_type)
2370 case PT_NULL:
2371 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
2373 case PT_LOAD:
2374 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load");
2376 case PT_DYNAMIC:
2377 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
2379 case PT_INTERP:
2380 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
2382 case PT_NOTE:
2383 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
2384 return FALSE;
2385 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2386 return FALSE;
2387 return TRUE;
2389 case PT_SHLIB:
2390 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
2392 case PT_PHDR:
2393 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
2395 case PT_GNU_EH_FRAME:
2396 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
2397 "eh_frame_hdr");
2399 case PT_GNU_STACK:
2400 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
2402 case PT_GNU_RELRO:
2403 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
2405 default:
2406 /* Check for any processor-specific program segment types. */
2407 bed = get_elf_backend_data (abfd);
2408 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
2412 /* Initialize REL_HDR, the section-header for new section, containing
2413 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2414 relocations; otherwise, we use REL relocations. */
2416 bfd_boolean
2417 _bfd_elf_init_reloc_shdr (bfd *abfd,
2418 Elf_Internal_Shdr *rel_hdr,
2419 asection *asect,
2420 bfd_boolean use_rela_p)
2422 char *name;
2423 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2424 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2426 name = (char *) bfd_alloc (abfd, amt);
2427 if (name == NULL)
2428 return FALSE;
2429 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2430 rel_hdr->sh_name =
2431 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2432 FALSE);
2433 if (rel_hdr->sh_name == (unsigned int) -1)
2434 return FALSE;
2435 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2436 rel_hdr->sh_entsize = (use_rela_p
2437 ? bed->s->sizeof_rela
2438 : bed->s->sizeof_rel);
2439 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
2440 rel_hdr->sh_flags = 0;
2441 rel_hdr->sh_addr = 0;
2442 rel_hdr->sh_size = 0;
2443 rel_hdr->sh_offset = 0;
2445 return TRUE;
2448 /* Return the default section type based on the passed in section flags. */
2451 bfd_elf_get_default_section_type (flagword flags)
2453 if ((flags & SEC_ALLOC) != 0
2454 && ((flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0
2455 || (flags & SEC_NEVER_LOAD) != 0))
2456 return SHT_NOBITS;
2457 return SHT_PROGBITS;
2460 /* Set up an ELF internal section header for a section. */
2462 static void
2463 elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg)
2465 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2466 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
2467 Elf_Internal_Shdr *this_hdr;
2468 unsigned int sh_type;
2470 if (*failedptr)
2472 /* We already failed; just get out of the bfd_map_over_sections
2473 loop. */
2474 return;
2477 this_hdr = &elf_section_data (asect)->this_hdr;
2479 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2480 asect->name, FALSE);
2481 if (this_hdr->sh_name == (unsigned int) -1)
2483 *failedptr = TRUE;
2484 return;
2487 /* Don't clear sh_flags. Assembler may set additional bits. */
2489 if ((asect->flags & SEC_ALLOC) != 0
2490 || asect->user_set_vma)
2491 this_hdr->sh_addr = asect->vma;
2492 else
2493 this_hdr->sh_addr = 0;
2495 this_hdr->sh_offset = 0;
2496 this_hdr->sh_size = asect->size;
2497 this_hdr->sh_link = 0;
2498 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
2499 /* The sh_entsize and sh_info fields may have been set already by
2500 copy_private_section_data. */
2502 this_hdr->bfd_section = asect;
2503 this_hdr->contents = NULL;
2505 /* If the section type is unspecified, we set it based on
2506 asect->flags. */
2507 if ((asect->flags & SEC_GROUP) != 0)
2508 sh_type = SHT_GROUP;
2509 else
2510 sh_type = bfd_elf_get_default_section_type (asect->flags);
2512 if (this_hdr->sh_type == SHT_NULL)
2513 this_hdr->sh_type = sh_type;
2514 else if (this_hdr->sh_type == SHT_NOBITS
2515 && sh_type == SHT_PROGBITS
2516 && (asect->flags & SEC_ALLOC) != 0)
2518 /* Warn if we are changing a NOBITS section to PROGBITS, but
2519 allow the link to proceed. This can happen when users link
2520 non-bss input sections to bss output sections, or emit data
2521 to a bss output section via a linker script. */
2522 (*_bfd_error_handler)
2523 (_("warning: section `%A' type changed to PROGBITS"), asect);
2524 this_hdr->sh_type = sh_type;
2527 switch (this_hdr->sh_type)
2529 default:
2530 break;
2532 case SHT_STRTAB:
2533 case SHT_INIT_ARRAY:
2534 case SHT_FINI_ARRAY:
2535 case SHT_PREINIT_ARRAY:
2536 case SHT_NOTE:
2537 case SHT_NOBITS:
2538 case SHT_PROGBITS:
2539 break;
2541 case SHT_HASH:
2542 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2543 break;
2545 case SHT_DYNSYM:
2546 this_hdr->sh_entsize = bed->s->sizeof_sym;
2547 break;
2549 case SHT_DYNAMIC:
2550 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2551 break;
2553 case SHT_RELA:
2554 if (get_elf_backend_data (abfd)->may_use_rela_p)
2555 this_hdr->sh_entsize = bed->s->sizeof_rela;
2556 break;
2558 case SHT_REL:
2559 if (get_elf_backend_data (abfd)->may_use_rel_p)
2560 this_hdr->sh_entsize = bed->s->sizeof_rel;
2561 break;
2563 case SHT_GNU_versym:
2564 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2565 break;
2567 case SHT_GNU_verdef:
2568 this_hdr->sh_entsize = 0;
2569 /* objcopy or strip will copy over sh_info, but may not set
2570 cverdefs. The linker will set cverdefs, but sh_info will be
2571 zero. */
2572 if (this_hdr->sh_info == 0)
2573 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2574 else
2575 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2576 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2577 break;
2579 case SHT_GNU_verneed:
2580 this_hdr->sh_entsize = 0;
2581 /* objcopy or strip will copy over sh_info, but may not set
2582 cverrefs. The linker will set cverrefs, but sh_info will be
2583 zero. */
2584 if (this_hdr->sh_info == 0)
2585 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2586 else
2587 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2588 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2589 break;
2591 case SHT_GROUP:
2592 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2593 break;
2595 case SHT_GNU_HASH:
2596 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
2597 break;
2600 if ((asect->flags & SEC_ALLOC) != 0)
2601 this_hdr->sh_flags |= SHF_ALLOC;
2602 if ((asect->flags & SEC_READONLY) == 0)
2603 this_hdr->sh_flags |= SHF_WRITE;
2604 if ((asect->flags & SEC_CODE) != 0)
2605 this_hdr->sh_flags |= SHF_EXECINSTR;
2606 if ((asect->flags & SEC_MERGE) != 0)
2608 this_hdr->sh_flags |= SHF_MERGE;
2609 this_hdr->sh_entsize = asect->entsize;
2610 if ((asect->flags & SEC_STRINGS) != 0)
2611 this_hdr->sh_flags |= SHF_STRINGS;
2613 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2614 this_hdr->sh_flags |= SHF_GROUP;
2615 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2617 this_hdr->sh_flags |= SHF_TLS;
2618 if (asect->size == 0
2619 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2621 struct bfd_link_order *o = asect->map_tail.link_order;
2623 this_hdr->sh_size = 0;
2624 if (o != NULL)
2626 this_hdr->sh_size = o->offset + o->size;
2627 if (this_hdr->sh_size != 0)
2628 this_hdr->sh_type = SHT_NOBITS;
2632 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
2633 this_hdr->sh_flags |= SHF_EXCLUDE;
2635 /* Check for processor-specific section types. */
2636 sh_type = this_hdr->sh_type;
2637 if (bed->elf_backend_fake_sections
2638 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2639 *failedptr = TRUE;
2641 if (sh_type == SHT_NOBITS && asect->size != 0)
2643 /* Don't change the header type from NOBITS if we are being
2644 called for objcopy --only-keep-debug. */
2645 this_hdr->sh_type = sh_type;
2648 /* If the section has relocs, set up a section header for the
2649 SHT_REL[A] section. If two relocation sections are required for
2650 this section, it is up to the processor-specific back-end to
2651 create the other. */
2652 if ((asect->flags & SEC_RELOC) != 0
2653 && !_bfd_elf_init_reloc_shdr (abfd,
2654 &elf_section_data (asect)->rel_hdr,
2655 asect,
2656 asect->use_rela_p))
2657 *failedptr = TRUE;
2660 /* Fill in the contents of a SHT_GROUP section. Called from
2661 _bfd_elf_compute_section_file_positions for gas, objcopy, and
2662 when ELF targets use the generic linker, ld. Called for ld -r
2663 from bfd_elf_final_link. */
2665 void
2666 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2668 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
2669 asection *elt, *first;
2670 unsigned char *loc;
2671 bfd_boolean gas;
2673 /* Ignore linker created group section. See elfNN_ia64_object_p in
2674 elfxx-ia64.c. */
2675 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
2676 || *failedptr)
2677 return;
2679 if (elf_section_data (sec)->this_hdr.sh_info == 0)
2681 unsigned long symindx = 0;
2683 /* elf_group_id will have been set up by objcopy and the
2684 generic linker. */
2685 if (elf_group_id (sec) != NULL)
2686 symindx = elf_group_id (sec)->udata.i;
2688 if (symindx == 0)
2690 /* If called from the assembler, swap_out_syms will have set up
2691 elf_section_syms. */
2692 BFD_ASSERT (elf_section_syms (abfd) != NULL);
2693 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2695 elf_section_data (sec)->this_hdr.sh_info = symindx;
2697 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
2699 /* The ELF backend linker sets sh_info to -2 when the group
2700 signature symbol is global, and thus the index can't be
2701 set until all local symbols are output. */
2702 asection *igroup = elf_sec_group (elf_next_in_group (sec));
2703 struct bfd_elf_section_data *sec_data = elf_section_data (igroup);
2704 unsigned long symndx = sec_data->this_hdr.sh_info;
2705 unsigned long extsymoff = 0;
2706 struct elf_link_hash_entry *h;
2708 if (!elf_bad_symtab (igroup->owner))
2710 Elf_Internal_Shdr *symtab_hdr;
2712 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
2713 extsymoff = symtab_hdr->sh_info;
2715 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
2716 while (h->root.type == bfd_link_hash_indirect
2717 || h->root.type == bfd_link_hash_warning)
2718 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2720 elf_section_data (sec)->this_hdr.sh_info = h->indx;
2723 /* The contents won't be allocated for "ld -r" or objcopy. */
2724 gas = TRUE;
2725 if (sec->contents == NULL)
2727 gas = FALSE;
2728 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
2730 /* Arrange for the section to be written out. */
2731 elf_section_data (sec)->this_hdr.contents = sec->contents;
2732 if (sec->contents == NULL)
2734 *failedptr = TRUE;
2735 return;
2739 loc = sec->contents + sec->size;
2741 /* Get the pointer to the first section in the group that gas
2742 squirreled away here. objcopy arranges for this to be set to the
2743 start of the input section group. */
2744 first = elt = elf_next_in_group (sec);
2746 /* First element is a flag word. Rest of section is elf section
2747 indices for all the sections of the group. Write them backwards
2748 just to keep the group in the same order as given in .section
2749 directives, not that it matters. */
2750 while (elt != NULL)
2752 asection *s;
2754 s = elt;
2755 if (!gas)
2756 s = s->output_section;
2757 if (s != NULL
2758 && !bfd_is_abs_section (s))
2760 unsigned int idx = elf_section_data (s)->this_idx;
2762 loc -= 4;
2763 H_PUT_32 (abfd, idx, loc);
2765 elt = elf_next_in_group (elt);
2766 if (elt == first)
2767 break;
2770 if ((loc -= 4) != sec->contents)
2771 abort ();
2773 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2776 /* Assign all ELF section numbers. The dummy first section is handled here
2777 too. The link/info pointers for the standard section types are filled
2778 in here too, while we're at it. */
2780 static bfd_boolean
2781 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
2783 struct elf_obj_tdata *t = elf_tdata (abfd);
2784 asection *sec;
2785 unsigned int section_number, secn;
2786 Elf_Internal_Shdr **i_shdrp;
2787 struct bfd_elf_section_data *d;
2788 bfd_boolean need_symtab;
2790 section_number = 1;
2792 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2794 /* SHT_GROUP sections are in relocatable files only. */
2795 if (link_info == NULL || link_info->relocatable)
2797 /* Put SHT_GROUP sections first. */
2798 for (sec = abfd->sections; sec != NULL; sec = sec->next)
2800 d = elf_section_data (sec);
2802 if (d->this_hdr.sh_type == SHT_GROUP)
2804 if (sec->flags & SEC_LINKER_CREATED)
2806 /* Remove the linker created SHT_GROUP sections. */
2807 bfd_section_list_remove (abfd, sec);
2808 abfd->section_count--;
2810 else
2811 d->this_idx = section_number++;
2816 for (sec = abfd->sections; sec; sec = sec->next)
2818 d = elf_section_data (sec);
2820 if (d->this_hdr.sh_type != SHT_GROUP)
2821 d->this_idx = section_number++;
2822 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2823 if ((sec->flags & SEC_RELOC) == 0)
2824 d->rel_idx = 0;
2825 else
2827 d->rel_idx = section_number++;
2828 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2831 if (d->rel_hdr2)
2833 d->rel_idx2 = section_number++;
2834 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2836 else
2837 d->rel_idx2 = 0;
2840 t->shstrtab_section = section_number++;
2841 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2842 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2844 need_symtab = (bfd_get_symcount (abfd) > 0
2845 || (link_info == NULL
2846 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
2847 == HAS_RELOC)));
2848 if (need_symtab)
2850 t->symtab_section = section_number++;
2851 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2852 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
2854 t->symtab_shndx_section = section_number++;
2855 t->symtab_shndx_hdr.sh_name
2856 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2857 ".symtab_shndx", FALSE);
2858 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2859 return FALSE;
2861 t->strtab_section = section_number++;
2862 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2865 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2866 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2868 elf_numsections (abfd) = section_number;
2869 elf_elfheader (abfd)->e_shnum = section_number;
2871 /* Set up the list of section header pointers, in agreement with the
2872 indices. */
2873 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
2874 sizeof (Elf_Internal_Shdr *));
2875 if (i_shdrp == NULL)
2876 return FALSE;
2878 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
2879 sizeof (Elf_Internal_Shdr));
2880 if (i_shdrp[0] == NULL)
2882 bfd_release (abfd, i_shdrp);
2883 return FALSE;
2886 elf_elfsections (abfd) = i_shdrp;
2888 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2889 if (need_symtab)
2891 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2892 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
2894 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2895 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2897 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2898 t->symtab_hdr.sh_link = t->strtab_section;
2901 for (sec = abfd->sections; sec; sec = sec->next)
2903 asection *s;
2904 const char *name;
2906 d = elf_section_data (sec);
2908 i_shdrp[d->this_idx] = &d->this_hdr;
2909 if (d->rel_idx != 0)
2910 i_shdrp[d->rel_idx] = &d->rel_hdr;
2911 if (d->rel_idx2 != 0)
2912 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2914 /* Fill in the sh_link and sh_info fields while we're at it. */
2916 /* sh_link of a reloc section is the section index of the symbol
2917 table. sh_info is the section index of the section to which
2918 the relocation entries apply. */
2919 if (d->rel_idx != 0)
2921 d->rel_hdr.sh_link = t->symtab_section;
2922 d->rel_hdr.sh_info = d->this_idx;
2924 if (d->rel_idx2 != 0)
2926 d->rel_hdr2->sh_link = t->symtab_section;
2927 d->rel_hdr2->sh_info = d->this_idx;
2930 /* We need to set up sh_link for SHF_LINK_ORDER. */
2931 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
2933 s = elf_linked_to_section (sec);
2934 if (s)
2936 /* elf_linked_to_section points to the input section. */
2937 if (link_info != NULL)
2939 /* Check discarded linkonce section. */
2940 if (elf_discarded_section (s))
2942 asection *kept;
2943 (*_bfd_error_handler)
2944 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
2945 abfd, d->this_hdr.bfd_section,
2946 s, s->owner);
2947 /* Point to the kept section if it has the same
2948 size as the discarded one. */
2949 kept = _bfd_elf_check_kept_section (s, link_info);
2950 if (kept == NULL)
2952 bfd_set_error (bfd_error_bad_value);
2953 return FALSE;
2955 s = kept;
2958 s = s->output_section;
2959 BFD_ASSERT (s != NULL);
2961 else
2963 /* Handle objcopy. */
2964 if (s->output_section == NULL)
2966 (*_bfd_error_handler)
2967 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
2968 abfd, d->this_hdr.bfd_section, s, s->owner);
2969 bfd_set_error (bfd_error_bad_value);
2970 return FALSE;
2972 s = s->output_section;
2974 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2976 else
2978 /* PR 290:
2979 The Intel C compiler generates SHT_IA_64_UNWIND with
2980 SHF_LINK_ORDER. But it doesn't set the sh_link or
2981 sh_info fields. Hence we could get the situation
2982 where s is NULL. */
2983 const struct elf_backend_data *bed
2984 = get_elf_backend_data (abfd);
2985 if (bed->link_order_error_handler)
2986 bed->link_order_error_handler
2987 (_("%B: warning: sh_link not set for section `%A'"),
2988 abfd, sec);
2992 switch (d->this_hdr.sh_type)
2994 case SHT_REL:
2995 case SHT_RELA:
2996 /* A reloc section which we are treating as a normal BFD
2997 section. sh_link is the section index of the symbol
2998 table. sh_info is the section index of the section to
2999 which the relocation entries apply. We assume that an
3000 allocated reloc section uses the dynamic symbol table.
3001 FIXME: How can we be sure? */
3002 s = bfd_get_section_by_name (abfd, ".dynsym");
3003 if (s != NULL)
3004 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3006 /* We look up the section the relocs apply to by name. */
3007 name = sec->name;
3008 if (d->this_hdr.sh_type == SHT_REL)
3009 name += 4;
3010 else
3011 name += 5;
3012 s = bfd_get_section_by_name (abfd, name);
3013 if (s != NULL)
3014 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3015 break;
3017 case SHT_STRTAB:
3018 /* We assume that a section named .stab*str is a stabs
3019 string section. We look for a section with the same name
3020 but without the trailing ``str'', and set its sh_link
3021 field to point to this section. */
3022 if (CONST_STRNEQ (sec->name, ".stab")
3023 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3025 size_t len;
3026 char *alc;
3028 len = strlen (sec->name);
3029 alc = (char *) bfd_malloc (len - 2);
3030 if (alc == NULL)
3031 return FALSE;
3032 memcpy (alc, sec->name, len - 3);
3033 alc[len - 3] = '\0';
3034 s = bfd_get_section_by_name (abfd, alc);
3035 free (alc);
3036 if (s != NULL)
3038 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3040 /* This is a .stab section. */
3041 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3042 elf_section_data (s)->this_hdr.sh_entsize
3043 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3046 break;
3048 case SHT_DYNAMIC:
3049 case SHT_DYNSYM:
3050 case SHT_GNU_verneed:
3051 case SHT_GNU_verdef:
3052 /* sh_link is the section header index of the string table
3053 used for the dynamic entries, or the symbol table, or the
3054 version strings. */
3055 s = bfd_get_section_by_name (abfd, ".dynstr");
3056 if (s != NULL)
3057 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3058 break;
3060 case SHT_GNU_LIBLIST:
3061 /* sh_link is the section header index of the prelink library
3062 list used for the dynamic entries, or the symbol table, or
3063 the version strings. */
3064 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3065 ? ".dynstr" : ".gnu.libstr");
3066 if (s != NULL)
3067 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3068 break;
3070 case SHT_HASH:
3071 case SHT_GNU_HASH:
3072 case SHT_GNU_versym:
3073 /* sh_link is the section header index of the symbol table
3074 this hash table or version table is for. */
3075 s = bfd_get_section_by_name (abfd, ".dynsym");
3076 if (s != NULL)
3077 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3078 break;
3080 case SHT_GROUP:
3081 d->this_hdr.sh_link = t->symtab_section;
3085 for (secn = 1; secn < section_number; ++secn)
3086 if (i_shdrp[secn] == NULL)
3087 i_shdrp[secn] = i_shdrp[0];
3088 else
3089 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
3090 i_shdrp[secn]->sh_name);
3091 return TRUE;
3094 /* Map symbol from it's internal number to the external number, moving
3095 all local symbols to be at the head of the list. */
3097 static bfd_boolean
3098 sym_is_global (bfd *abfd, asymbol *sym)
3100 /* If the backend has a special mapping, use it. */
3101 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3102 if (bed->elf_backend_sym_is_global)
3103 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3105 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
3106 || bfd_is_und_section (bfd_get_section (sym))
3107 || bfd_is_com_section (bfd_get_section (sym)));
3110 /* Don't output section symbols for sections that are not going to be
3111 output. */
3113 static bfd_boolean
3114 ignore_section_sym (bfd *abfd, asymbol *sym)
3116 return ((sym->flags & BSF_SECTION_SYM) != 0
3117 && !(sym->section->owner == abfd
3118 || (sym->section->output_section->owner == abfd
3119 && sym->section->output_offset == 0)));
3122 static bfd_boolean
3123 elf_map_symbols (bfd *abfd)
3125 unsigned int symcount = bfd_get_symcount (abfd);
3126 asymbol **syms = bfd_get_outsymbols (abfd);
3127 asymbol **sect_syms;
3128 unsigned int num_locals = 0;
3129 unsigned int num_globals = 0;
3130 unsigned int num_locals2 = 0;
3131 unsigned int num_globals2 = 0;
3132 int max_index = 0;
3133 unsigned int idx;
3134 asection *asect;
3135 asymbol **new_syms;
3137 #ifdef DEBUG
3138 fprintf (stderr, "elf_map_symbols\n");
3139 fflush (stderr);
3140 #endif
3142 for (asect = abfd->sections; asect; asect = asect->next)
3144 if (max_index < asect->index)
3145 max_index = asect->index;
3148 max_index++;
3149 sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
3150 if (sect_syms == NULL)
3151 return FALSE;
3152 elf_section_syms (abfd) = sect_syms;
3153 elf_num_section_syms (abfd) = max_index;
3155 /* Init sect_syms entries for any section symbols we have already
3156 decided to output. */
3157 for (idx = 0; idx < symcount; idx++)
3159 asymbol *sym = syms[idx];
3161 if ((sym->flags & BSF_SECTION_SYM) != 0
3162 && sym->value == 0
3163 && !ignore_section_sym (abfd, sym))
3165 asection *sec = sym->section;
3167 if (sec->owner != abfd)
3168 sec = sec->output_section;
3170 sect_syms[sec->index] = syms[idx];
3174 /* Classify all of the symbols. */
3175 for (idx = 0; idx < symcount; idx++)
3177 if (ignore_section_sym (abfd, syms[idx]))
3178 continue;
3179 if (!sym_is_global (abfd, syms[idx]))
3180 num_locals++;
3181 else
3182 num_globals++;
3185 /* We will be adding a section symbol for each normal BFD section. Most
3186 sections will already have a section symbol in outsymbols, but
3187 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3188 at least in that case. */
3189 for (asect = abfd->sections; asect; asect = asect->next)
3191 if (sect_syms[asect->index] == NULL)
3193 if (!sym_is_global (abfd, asect->symbol))
3194 num_locals++;
3195 else
3196 num_globals++;
3200 /* Now sort the symbols so the local symbols are first. */
3201 new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
3202 sizeof (asymbol *));
3204 if (new_syms == NULL)
3205 return FALSE;
3207 for (idx = 0; idx < symcount; idx++)
3209 asymbol *sym = syms[idx];
3210 unsigned int i;
3212 if (ignore_section_sym (abfd, sym))
3213 continue;
3214 if (!sym_is_global (abfd, sym))
3215 i = num_locals2++;
3216 else
3217 i = num_locals + num_globals2++;
3218 new_syms[i] = sym;
3219 sym->udata.i = i + 1;
3221 for (asect = abfd->sections; asect; asect = asect->next)
3223 if (sect_syms[asect->index] == NULL)
3225 asymbol *sym = asect->symbol;
3226 unsigned int i;
3228 sect_syms[asect->index] = sym;
3229 if (!sym_is_global (abfd, sym))
3230 i = num_locals2++;
3231 else
3232 i = num_locals + num_globals2++;
3233 new_syms[i] = sym;
3234 sym->udata.i = i + 1;
3238 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3240 elf_num_locals (abfd) = num_locals;
3241 elf_num_globals (abfd) = num_globals;
3242 return TRUE;
3245 /* Align to the maximum file alignment that could be required for any
3246 ELF data structure. */
3248 static inline file_ptr
3249 align_file_position (file_ptr off, int align)
3251 return (off + align - 1) & ~(align - 1);
3254 /* Assign a file position to a section, optionally aligning to the
3255 required section alignment. */
3257 file_ptr
3258 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3259 file_ptr offset,
3260 bfd_boolean align)
3262 if (align && i_shdrp->sh_addralign > 1)
3263 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
3264 i_shdrp->sh_offset = offset;
3265 if (i_shdrp->bfd_section != NULL)
3266 i_shdrp->bfd_section->filepos = offset;
3267 if (i_shdrp->sh_type != SHT_NOBITS)
3268 offset += i_shdrp->sh_size;
3269 return offset;
3272 /* Compute the file positions we are going to put the sections at, and
3273 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3274 is not NULL, this is being called by the ELF backend linker. */
3276 bfd_boolean
3277 _bfd_elf_compute_section_file_positions (bfd *abfd,
3278 struct bfd_link_info *link_info)
3280 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3281 bfd_boolean failed;
3282 struct bfd_strtab_hash *strtab = NULL;
3283 Elf_Internal_Shdr *shstrtab_hdr;
3284 bfd_boolean need_symtab;
3286 if (abfd->output_has_begun)
3287 return TRUE;
3289 /* Do any elf backend specific processing first. */
3290 if (bed->elf_backend_begin_write_processing)
3291 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3293 if (! prep_headers (abfd))
3294 return FALSE;
3296 /* Post process the headers if necessary. */
3297 if (bed->elf_backend_post_process_headers)
3298 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3300 failed = FALSE;
3301 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3302 if (failed)
3303 return FALSE;
3305 if (!assign_section_numbers (abfd, link_info))
3306 return FALSE;
3308 /* The backend linker builds symbol table information itself. */
3309 need_symtab = (link_info == NULL
3310 && (bfd_get_symcount (abfd) > 0
3311 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3312 == HAS_RELOC)));
3313 if (need_symtab)
3315 /* Non-zero if doing a relocatable link. */
3316 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3318 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3319 return FALSE;
3322 if (link_info == NULL)
3324 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3325 if (failed)
3326 return FALSE;
3329 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3330 /* sh_name was set in prep_headers. */
3331 shstrtab_hdr->sh_type = SHT_STRTAB;
3332 shstrtab_hdr->sh_flags = 0;
3333 shstrtab_hdr->sh_addr = 0;
3334 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3335 shstrtab_hdr->sh_entsize = 0;
3336 shstrtab_hdr->sh_link = 0;
3337 shstrtab_hdr->sh_info = 0;
3338 /* sh_offset is set in assign_file_positions_except_relocs. */
3339 shstrtab_hdr->sh_addralign = 1;
3341 if (!assign_file_positions_except_relocs (abfd, link_info))
3342 return FALSE;
3344 if (need_symtab)
3346 file_ptr off;
3347 Elf_Internal_Shdr *hdr;
3349 off = elf_tdata (abfd)->next_file_pos;
3351 hdr = &elf_tdata (abfd)->symtab_hdr;
3352 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3354 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3355 if (hdr->sh_size != 0)
3356 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3358 hdr = &elf_tdata (abfd)->strtab_hdr;
3359 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3361 elf_tdata (abfd)->next_file_pos = off;
3363 /* Now that we know where the .strtab section goes, write it
3364 out. */
3365 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3366 || ! _bfd_stringtab_emit (abfd, strtab))
3367 return FALSE;
3368 _bfd_stringtab_free (strtab);
3371 abfd->output_has_begun = TRUE;
3373 return TRUE;
3376 /* Make an initial estimate of the size of the program header. If we
3377 get the number wrong here, we'll redo section placement. */
3379 static bfd_size_type
3380 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
3382 size_t segs;
3383 asection *s;
3384 const struct elf_backend_data *bed;
3386 /* Assume we will need exactly two PT_LOAD segments: one for text
3387 and one for data. */
3388 segs = 2;
3390 s = bfd_get_section_by_name (abfd, ".interp");
3391 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3393 /* If we have a loadable interpreter section, we need a
3394 PT_INTERP segment. In this case, assume we also need a
3395 PT_PHDR segment, although that may not be true for all
3396 targets. */
3397 segs += 2;
3400 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3402 /* We need a PT_DYNAMIC segment. */
3403 ++segs;
3406 if (info != NULL && info->relro)
3408 /* We need a PT_GNU_RELRO segment. */
3409 ++segs;
3412 if (elf_tdata (abfd)->eh_frame_hdr)
3414 /* We need a PT_GNU_EH_FRAME segment. */
3415 ++segs;
3418 if (elf_tdata (abfd)->stack_flags)
3420 /* We need a PT_GNU_STACK segment. */
3421 ++segs;
3424 for (s = abfd->sections; s != NULL; s = s->next)
3426 if ((s->flags & SEC_LOAD) != 0
3427 && CONST_STRNEQ (s->name, ".note"))
3429 /* We need a PT_NOTE segment. */
3430 ++segs;
3431 /* Try to create just one PT_NOTE segment
3432 for all adjacent loadable .note* sections.
3433 gABI requires that within a PT_NOTE segment
3434 (and also inside of each SHT_NOTE section)
3435 each note is padded to a multiple of 4 size,
3436 so we check whether the sections are correctly
3437 aligned. */
3438 if (s->alignment_power == 2)
3439 while (s->next != NULL
3440 && s->next->alignment_power == 2
3441 && (s->next->flags & SEC_LOAD) != 0
3442 && CONST_STRNEQ (s->next->name, ".note"))
3443 s = s->next;
3447 for (s = abfd->sections; s != NULL; s = s->next)
3449 if (s->flags & SEC_THREAD_LOCAL)
3451 /* We need a PT_TLS segment. */
3452 ++segs;
3453 break;
3457 /* Let the backend count up any program headers it might need. */
3458 bed = get_elf_backend_data (abfd);
3459 if (bed->elf_backend_additional_program_headers)
3461 int a;
3463 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
3464 if (a == -1)
3465 abort ();
3466 segs += a;
3469 return segs * bed->s->sizeof_phdr;
3472 /* Find the segment that contains the output_section of section. */
3474 Elf_Internal_Phdr *
3475 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
3477 struct elf_segment_map *m;
3478 Elf_Internal_Phdr *p;
3480 for (m = elf_tdata (abfd)->segment_map,
3481 p = elf_tdata (abfd)->phdr;
3482 m != NULL;
3483 m = m->next, p++)
3485 int i;
3487 for (i = m->count - 1; i >= 0; i--)
3488 if (m->sections[i] == section)
3489 return p;
3492 return NULL;
3495 /* Create a mapping from a set of sections to a program segment. */
3497 static struct elf_segment_map *
3498 make_mapping (bfd *abfd,
3499 asection **sections,
3500 unsigned int from,
3501 unsigned int to,
3502 bfd_boolean phdr)
3504 struct elf_segment_map *m;
3505 unsigned int i;
3506 asection **hdrpp;
3507 bfd_size_type amt;
3509 amt = sizeof (struct elf_segment_map);
3510 amt += (to - from - 1) * sizeof (asection *);
3511 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3512 if (m == NULL)
3513 return NULL;
3514 m->next = NULL;
3515 m->p_type = PT_LOAD;
3516 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3517 m->sections[i - from] = *hdrpp;
3518 m->count = to - from;
3520 if (from == 0 && phdr)
3522 /* Include the headers in the first PT_LOAD segment. */
3523 m->includes_filehdr = 1;
3524 m->includes_phdrs = 1;
3527 return m;
3530 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3531 on failure. */
3533 struct elf_segment_map *
3534 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3536 struct elf_segment_map *m;
3538 m = (struct elf_segment_map *) bfd_zalloc (abfd,
3539 sizeof (struct elf_segment_map));
3540 if (m == NULL)
3541 return NULL;
3542 m->next = NULL;
3543 m->p_type = PT_DYNAMIC;
3544 m->count = 1;
3545 m->sections[0] = dynsec;
3547 return m;
3550 /* Possibly add or remove segments from the segment map. */
3552 static bfd_boolean
3553 elf_modify_segment_map (bfd *abfd,
3554 struct bfd_link_info *info,
3555 bfd_boolean remove_empty_load)
3557 struct elf_segment_map **m;
3558 const struct elf_backend_data *bed;
3560 /* The placement algorithm assumes that non allocated sections are
3561 not in PT_LOAD segments. We ensure this here by removing such
3562 sections from the segment map. We also remove excluded
3563 sections. Finally, any PT_LOAD segment without sections is
3564 removed. */
3565 m = &elf_tdata (abfd)->segment_map;
3566 while (*m)
3568 unsigned int i, new_count;
3570 for (new_count = 0, i = 0; i < (*m)->count; i++)
3572 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
3573 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
3574 || (*m)->p_type != PT_LOAD))
3576 (*m)->sections[new_count] = (*m)->sections[i];
3577 new_count++;
3580 (*m)->count = new_count;
3582 if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0)
3583 *m = (*m)->next;
3584 else
3585 m = &(*m)->next;
3588 bed = get_elf_backend_data (abfd);
3589 if (bed->elf_backend_modify_segment_map != NULL)
3591 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
3592 return FALSE;
3595 return TRUE;
3598 /* Set up a mapping from BFD sections to program segments. */
3600 bfd_boolean
3601 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
3603 unsigned int count;
3604 struct elf_segment_map *m;
3605 asection **sections = NULL;
3606 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3607 bfd_boolean no_user_phdrs;
3609 no_user_phdrs = elf_tdata (abfd)->segment_map == NULL;
3610 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
3612 asection *s;
3613 unsigned int i;
3614 struct elf_segment_map *mfirst;
3615 struct elf_segment_map **pm;
3616 asection *last_hdr;
3617 bfd_vma last_size;
3618 unsigned int phdr_index;
3619 bfd_vma maxpagesize;
3620 asection **hdrpp;
3621 bfd_boolean phdr_in_segment = TRUE;
3622 bfd_boolean writable;
3623 int tls_count = 0;
3624 asection *first_tls = NULL;
3625 asection *dynsec, *eh_frame_hdr;
3626 bfd_size_type amt;
3627 bfd_vma addr_mask, wrap_to = 0;
3629 /* Select the allocated sections, and sort them. */
3631 sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
3632 sizeof (asection *));
3633 if (sections == NULL)
3634 goto error_return;
3636 /* Calculate top address, avoiding undefined behaviour of shift
3637 left operator when shift count is equal to size of type
3638 being shifted. */
3639 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
3640 addr_mask = (addr_mask << 1) + 1;
3642 i = 0;
3643 for (s = abfd->sections; s != NULL; s = s->next)
3645 if ((s->flags & SEC_ALLOC) != 0)
3647 sections[i] = s;
3648 ++i;
3649 /* A wrapping section potentially clashes with header. */
3650 if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask))
3651 wrap_to = (s->lma + s->size) & addr_mask;
3654 BFD_ASSERT (i <= bfd_count_sections (abfd));
3655 count = i;
3657 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3659 /* Build the mapping. */
3661 mfirst = NULL;
3662 pm = &mfirst;
3664 /* If we have a .interp section, then create a PT_PHDR segment for
3665 the program headers and a PT_INTERP segment for the .interp
3666 section. */
3667 s = bfd_get_section_by_name (abfd, ".interp");
3668 if (s != NULL && (s->flags & SEC_LOAD) != 0)
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_PHDR;
3676 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3677 m->p_flags = PF_R | PF_X;
3678 m->p_flags_valid = 1;
3679 m->includes_phdrs = 1;
3681 *pm = m;
3682 pm = &m->next;
3684 amt = sizeof (struct elf_segment_map);
3685 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3686 if (m == NULL)
3687 goto error_return;
3688 m->next = NULL;
3689 m->p_type = PT_INTERP;
3690 m->count = 1;
3691 m->sections[0] = s;
3693 *pm = m;
3694 pm = &m->next;
3697 /* Look through the sections. We put sections in the same program
3698 segment when the start of the second section can be placed within
3699 a few bytes of the end of the first section. */
3700 last_hdr = NULL;
3701 last_size = 0;
3702 phdr_index = 0;
3703 maxpagesize = bed->maxpagesize;
3704 writable = FALSE;
3705 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3706 if (dynsec != NULL
3707 && (dynsec->flags & SEC_LOAD) == 0)
3708 dynsec = NULL;
3710 /* Deal with -Ttext or something similar such that the first section
3711 is not adjacent to the program headers. This is an
3712 approximation, since at this point we don't know exactly how many
3713 program headers we will need. */
3714 if (count > 0)
3716 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
3718 if (phdr_size == (bfd_size_type) -1)
3719 phdr_size = get_program_header_size (abfd, info);
3720 if ((abfd->flags & D_PAGED) == 0
3721 || (sections[0]->lma & addr_mask) < phdr_size
3722 || ((sections[0]->lma & addr_mask) % maxpagesize
3723 < phdr_size % maxpagesize)
3724 || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to)
3725 phdr_in_segment = FALSE;
3728 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3730 asection *hdr;
3731 bfd_boolean new_segment;
3733 hdr = *hdrpp;
3735 /* See if this section and the last one will fit in the same
3736 segment. */
3738 if (last_hdr == NULL)
3740 /* If we don't have a segment yet, then we don't need a new
3741 one (we build the last one after this loop). */
3742 new_segment = FALSE;
3744 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3746 /* If this section has a different relation between the
3747 virtual address and the load address, then we need a new
3748 segment. */
3749 new_segment = TRUE;
3751 else if (hdr->lma < last_hdr->lma + last_size
3752 || last_hdr->lma + last_size < last_hdr->lma)
3754 /* If this section has a load address that makes it overlap
3755 the previous section, then we need a new segment. */
3756 new_segment = TRUE;
3758 /* In the next test we have to be careful when last_hdr->lma is close
3759 to the end of the address space. If the aligned address wraps
3760 around to the start of the address space, then there are no more
3761 pages left in memory and it is OK to assume that the current
3762 section can be included in the current segment. */
3763 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
3764 > last_hdr->lma)
3765 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
3766 <= hdr->lma))
3768 /* If putting this section in this segment would force us to
3769 skip a page in the segment, then we need a new segment. */
3770 new_segment = TRUE;
3772 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3773 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
3775 /* We don't want to put a loadable section after a
3776 nonloadable section in the same segment.
3777 Consider .tbss sections as loadable for this purpose. */
3778 new_segment = TRUE;
3780 else if ((abfd->flags & D_PAGED) == 0)
3782 /* If the file is not demand paged, which means that we
3783 don't require the sections to be correctly aligned in the
3784 file, then there is no other reason for a new segment. */
3785 new_segment = FALSE;
3787 else if (! writable
3788 && (hdr->flags & SEC_READONLY) == 0
3789 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
3790 != (hdr->lma & -maxpagesize)))
3792 /* We don't want to put a writable section in a read only
3793 segment, unless they are on the same page in memory
3794 anyhow. We already know that the last section does not
3795 bring us past the current section on the page, so the
3796 only case in which the new section is not on the same
3797 page as the previous section is when the previous section
3798 ends precisely on a page boundary. */
3799 new_segment = TRUE;
3801 else
3803 /* Otherwise, we can use the same segment. */
3804 new_segment = FALSE;
3807 /* Allow interested parties a chance to override our decision. */
3808 if (last_hdr != NULL
3809 && info != NULL
3810 && info->callbacks->override_segment_assignment != NULL)
3811 new_segment
3812 = info->callbacks->override_segment_assignment (info, abfd, hdr,
3813 last_hdr,
3814 new_segment);
3816 if (! new_segment)
3818 if ((hdr->flags & SEC_READONLY) == 0)
3819 writable = TRUE;
3820 last_hdr = hdr;
3821 /* .tbss sections effectively have zero size. */
3822 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
3823 != SEC_THREAD_LOCAL)
3824 last_size = hdr->size;
3825 else
3826 last_size = 0;
3827 continue;
3830 /* We need a new program segment. We must create a new program
3831 header holding all the sections from phdr_index until hdr. */
3833 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3834 if (m == NULL)
3835 goto error_return;
3837 *pm = m;
3838 pm = &m->next;
3840 if ((hdr->flags & SEC_READONLY) == 0)
3841 writable = TRUE;
3842 else
3843 writable = FALSE;
3845 last_hdr = hdr;
3846 /* .tbss sections effectively have zero size. */
3847 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3848 last_size = hdr->size;
3849 else
3850 last_size = 0;
3851 phdr_index = i;
3852 phdr_in_segment = FALSE;
3855 /* Create a final PT_LOAD program segment. */
3856 if (last_hdr != NULL)
3858 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3859 if (m == NULL)
3860 goto error_return;
3862 *pm = m;
3863 pm = &m->next;
3866 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3867 if (dynsec != NULL)
3869 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
3870 if (m == NULL)
3871 goto error_return;
3872 *pm = m;
3873 pm = &m->next;
3876 /* For each batch of consecutive loadable .note sections,
3877 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
3878 because if we link together nonloadable .note sections and
3879 loadable .note sections, we will generate two .note sections
3880 in the output file. FIXME: Using names for section types is
3881 bogus anyhow. */
3882 for (s = abfd->sections; s != NULL; s = s->next)
3884 if ((s->flags & SEC_LOAD) != 0
3885 && CONST_STRNEQ (s->name, ".note"))
3887 asection *s2;
3889 count = 1;
3890 amt = sizeof (struct elf_segment_map);
3891 if (s->alignment_power == 2)
3892 for (s2 = s; s2->next != NULL; s2 = s2->next)
3894 if (s2->next->alignment_power == 2
3895 && (s2->next->flags & SEC_LOAD) != 0
3896 && CONST_STRNEQ (s2->next->name, ".note")
3897 && align_power (s2->lma + s2->size, 2)
3898 == s2->next->lma)
3899 count++;
3900 else
3901 break;
3903 amt += (count - 1) * sizeof (asection *);
3904 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3905 if (m == NULL)
3906 goto error_return;
3907 m->next = NULL;
3908 m->p_type = PT_NOTE;
3909 m->count = count;
3910 while (count > 1)
3912 m->sections[m->count - count--] = s;
3913 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3914 s = s->next;
3916 m->sections[m->count - 1] = s;
3917 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3918 *pm = m;
3919 pm = &m->next;
3921 if (s->flags & SEC_THREAD_LOCAL)
3923 if (! tls_count)
3924 first_tls = s;
3925 tls_count++;
3929 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3930 if (tls_count > 0)
3932 amt = sizeof (struct elf_segment_map);
3933 amt += (tls_count - 1) * sizeof (asection *);
3934 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3935 if (m == NULL)
3936 goto error_return;
3937 m->next = NULL;
3938 m->p_type = PT_TLS;
3939 m->count = tls_count;
3940 /* Mandated PF_R. */
3941 m->p_flags = PF_R;
3942 m->p_flags_valid = 1;
3943 for (i = 0; i < (unsigned int) tls_count; ++i)
3945 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3946 m->sections[i] = first_tls;
3947 first_tls = first_tls->next;
3950 *pm = m;
3951 pm = &m->next;
3954 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3955 segment. */
3956 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
3957 if (eh_frame_hdr != NULL
3958 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
3960 amt = sizeof (struct elf_segment_map);
3961 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3962 if (m == NULL)
3963 goto error_return;
3964 m->next = NULL;
3965 m->p_type = PT_GNU_EH_FRAME;
3966 m->count = 1;
3967 m->sections[0] = eh_frame_hdr->output_section;
3969 *pm = m;
3970 pm = &m->next;
3973 if (elf_tdata (abfd)->stack_flags)
3975 amt = sizeof (struct elf_segment_map);
3976 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3977 if (m == NULL)
3978 goto error_return;
3979 m->next = NULL;
3980 m->p_type = PT_GNU_STACK;
3981 m->p_flags = elf_tdata (abfd)->stack_flags;
3982 m->p_flags_valid = 1;
3984 *pm = m;
3985 pm = &m->next;
3988 if (info != NULL && info->relro)
3990 for (m = mfirst; m != NULL; m = m->next)
3992 if (m->p_type == PT_LOAD)
3994 asection *last = m->sections[m->count - 1];
3995 bfd_vma vaddr = m->sections[0]->vma;
3996 bfd_vma filesz = last->vma - vaddr + last->size;
3998 if (vaddr < info->relro_end
3999 && vaddr >= info->relro_start
4000 && (vaddr + filesz) >= info->relro_end)
4001 break;
4005 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
4006 if (m != NULL)
4008 amt = sizeof (struct elf_segment_map);
4009 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4010 if (m == NULL)
4011 goto error_return;
4012 m->next = NULL;
4013 m->p_type = PT_GNU_RELRO;
4014 m->p_flags = PF_R;
4015 m->p_flags_valid = 1;
4017 *pm = m;
4018 pm = &m->next;
4022 free (sections);
4023 elf_tdata (abfd)->segment_map = mfirst;
4026 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
4027 return FALSE;
4029 for (count = 0, m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4030 ++count;
4031 elf_tdata (abfd)->program_header_size = count * bed->s->sizeof_phdr;
4033 return TRUE;
4035 error_return:
4036 if (sections != NULL)
4037 free (sections);
4038 return FALSE;
4041 /* Sort sections by address. */
4043 static int
4044 elf_sort_sections (const void *arg1, const void *arg2)
4046 const asection *sec1 = *(const asection **) arg1;
4047 const asection *sec2 = *(const asection **) arg2;
4048 bfd_size_type size1, size2;
4050 /* Sort by LMA first, since this is the address used to
4051 place the section into a segment. */
4052 if (sec1->lma < sec2->lma)
4053 return -1;
4054 else if (sec1->lma > sec2->lma)
4055 return 1;
4057 /* Then sort by VMA. Normally the LMA and the VMA will be
4058 the same, and this will do nothing. */
4059 if (sec1->vma < sec2->vma)
4060 return -1;
4061 else if (sec1->vma > sec2->vma)
4062 return 1;
4064 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4066 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4068 if (TOEND (sec1))
4070 if (TOEND (sec2))
4072 /* If the indicies are the same, do not return 0
4073 here, but continue to try the next comparison. */
4074 if (sec1->target_index - sec2->target_index != 0)
4075 return sec1->target_index - sec2->target_index;
4077 else
4078 return 1;
4080 else if (TOEND (sec2))
4081 return -1;
4083 #undef TOEND
4085 /* Sort by size, to put zero sized sections
4086 before others at the same address. */
4088 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
4089 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
4091 if (size1 < size2)
4092 return -1;
4093 if (size1 > size2)
4094 return 1;
4096 return sec1->target_index - sec2->target_index;
4099 /* Ian Lance Taylor writes:
4101 We shouldn't be using % with a negative signed number. That's just
4102 not good. We have to make sure either that the number is not
4103 negative, or that the number has an unsigned type. When the types
4104 are all the same size they wind up as unsigned. When file_ptr is a
4105 larger signed type, the arithmetic winds up as signed long long,
4106 which is wrong.
4108 What we're trying to say here is something like ``increase OFF by
4109 the least amount that will cause it to be equal to the VMA modulo
4110 the page size.'' */
4111 /* In other words, something like:
4113 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4114 off_offset = off % bed->maxpagesize;
4115 if (vma_offset < off_offset)
4116 adjustment = vma_offset + bed->maxpagesize - off_offset;
4117 else
4118 adjustment = vma_offset - off_offset;
4120 which can can be collapsed into the expression below. */
4122 static file_ptr
4123 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
4125 return ((vma - off) % maxpagesize);
4128 static void
4129 print_segment_map (const struct elf_segment_map *m)
4131 unsigned int j;
4132 const char *pt = get_segment_type (m->p_type);
4133 char buf[32];
4135 if (pt == NULL)
4137 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
4138 sprintf (buf, "LOPROC+%7.7x",
4139 (unsigned int) (m->p_type - PT_LOPROC));
4140 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
4141 sprintf (buf, "LOOS+%7.7x",
4142 (unsigned int) (m->p_type - PT_LOOS));
4143 else
4144 snprintf (buf, sizeof (buf), "%8.8x",
4145 (unsigned int) m->p_type);
4146 pt = buf;
4148 fprintf (stderr, "%s:", pt);
4149 for (j = 0; j < m->count; j++)
4150 fprintf (stderr, " %s", m->sections [j]->name);
4151 putc ('\n',stderr);
4154 static bfd_boolean
4155 write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
4157 void *buf;
4158 bfd_boolean ret;
4160 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
4161 return FALSE;
4162 buf = bfd_zmalloc (len);
4163 if (buf == NULL)
4164 return FALSE;
4165 ret = bfd_bwrite (buf, len, abfd) == len;
4166 free (buf);
4167 return ret;
4170 /* Assign file positions to the sections based on the mapping from
4171 sections to segments. This function also sets up some fields in
4172 the file header. */
4174 static bfd_boolean
4175 assign_file_positions_for_load_sections (bfd *abfd,
4176 struct bfd_link_info *link_info)
4178 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4179 struct elf_segment_map *m;
4180 Elf_Internal_Phdr *phdrs;
4181 Elf_Internal_Phdr *p;
4182 file_ptr off;
4183 bfd_size_type maxpagesize;
4184 unsigned int alloc;
4185 unsigned int i, j;
4186 bfd_vma header_pad = 0;
4188 if (link_info == NULL
4189 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
4190 return FALSE;
4192 alloc = 0;
4193 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4195 ++alloc;
4196 if (m->header_size)
4197 header_pad = m->header_size;
4200 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
4201 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
4202 elf_elfheader (abfd)->e_phnum = alloc;
4204 if (elf_tdata (abfd)->program_header_size == (bfd_size_type) -1)
4205 elf_tdata (abfd)->program_header_size = alloc * bed->s->sizeof_phdr;
4206 else
4207 BFD_ASSERT (elf_tdata (abfd)->program_header_size
4208 >= alloc * bed->s->sizeof_phdr);
4210 if (alloc == 0)
4212 elf_tdata (abfd)->next_file_pos = bed->s->sizeof_ehdr;
4213 return TRUE;
4216 /* We're writing the size in elf_tdata (abfd)->program_header_size,
4217 see assign_file_positions_except_relocs, so make sure we have
4218 that amount allocated, with trailing space cleared.
4219 The variable alloc contains the computed need, while elf_tdata
4220 (abfd)->program_header_size contains the size used for the
4221 layout.
4222 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4223 where the layout is forced to according to a larger size in the
4224 last iterations for the testcase ld-elf/header. */
4225 BFD_ASSERT (elf_tdata (abfd)->program_header_size % bed->s->sizeof_phdr
4226 == 0);
4227 phdrs = (Elf_Internal_Phdr *)
4228 bfd_zalloc2 (abfd,
4229 (elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr),
4230 sizeof (Elf_Internal_Phdr));
4231 elf_tdata (abfd)->phdr = phdrs;
4232 if (phdrs == NULL)
4233 return FALSE;
4235 maxpagesize = 1;
4236 if ((abfd->flags & D_PAGED) != 0)
4237 maxpagesize = bed->maxpagesize;
4239 off = bed->s->sizeof_ehdr;
4240 off += alloc * bed->s->sizeof_phdr;
4241 if (header_pad < (bfd_vma) off)
4242 header_pad = 0;
4243 else
4244 header_pad -= off;
4245 off += header_pad;
4247 for (m = elf_tdata (abfd)->segment_map, p = phdrs, j = 0;
4248 m != NULL;
4249 m = m->next, p++, j++)
4251 asection **secpp;
4252 bfd_vma off_adjust;
4253 bfd_boolean no_contents;
4255 /* If elf_segment_map is not from map_sections_to_segments, the
4256 sections may not be correctly ordered. NOTE: sorting should
4257 not be done to the PT_NOTE section of a corefile, which may
4258 contain several pseudo-sections artificially created by bfd.
4259 Sorting these pseudo-sections breaks things badly. */
4260 if (m->count > 1
4261 && !(elf_elfheader (abfd)->e_type == ET_CORE
4262 && m->p_type == PT_NOTE))
4263 qsort (m->sections, (size_t) m->count, sizeof (asection *),
4264 elf_sort_sections);
4266 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4267 number of sections with contents contributing to both p_filesz
4268 and p_memsz, followed by a number of sections with no contents
4269 that just contribute to p_memsz. In this loop, OFF tracks next
4270 available file offset for PT_LOAD and PT_NOTE segments. */
4271 p->p_type = m->p_type;
4272 p->p_flags = m->p_flags;
4274 if (m->count == 0)
4275 p->p_vaddr = 0;
4276 else
4277 p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
4279 if (m->p_paddr_valid)
4280 p->p_paddr = m->p_paddr;
4281 else if (m->count == 0)
4282 p->p_paddr = 0;
4283 else
4284 p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
4286 if (p->p_type == PT_LOAD
4287 && (abfd->flags & D_PAGED) != 0)
4289 /* p_align in demand paged PT_LOAD segments effectively stores
4290 the maximum page size. When copying an executable with
4291 objcopy, we set m->p_align from the input file. Use this
4292 value for maxpagesize rather than bed->maxpagesize, which
4293 may be different. Note that we use maxpagesize for PT_TLS
4294 segment alignment later in this function, so we are relying
4295 on at least one PT_LOAD segment appearing before a PT_TLS
4296 segment. */
4297 if (m->p_align_valid)
4298 maxpagesize = m->p_align;
4300 p->p_align = maxpagesize;
4302 else if (m->p_align_valid)
4303 p->p_align = m->p_align;
4304 else if (m->count == 0)
4305 p->p_align = 1 << bed->s->log_file_align;
4306 else
4307 p->p_align = 0;
4309 no_contents = FALSE;
4310 off_adjust = 0;
4311 if (p->p_type == PT_LOAD
4312 && m->count > 0)
4314 bfd_size_type align;
4315 unsigned int align_power = 0;
4317 if (m->p_align_valid)
4318 align = p->p_align;
4319 else
4321 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4323 unsigned int secalign;
4325 secalign = bfd_get_section_alignment (abfd, *secpp);
4326 if (secalign > align_power)
4327 align_power = secalign;
4329 align = (bfd_size_type) 1 << align_power;
4330 if (align < maxpagesize)
4331 align = maxpagesize;
4334 for (i = 0; i < m->count; i++)
4335 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
4336 /* If we aren't making room for this section, then
4337 it must be SHT_NOBITS regardless of what we've
4338 set via struct bfd_elf_special_section. */
4339 elf_section_type (m->sections[i]) = SHT_NOBITS;
4341 /* Find out whether this segment contains any loadable
4342 sections. */
4343 no_contents = TRUE;
4344 for (i = 0; i < m->count; i++)
4345 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
4347 no_contents = FALSE;
4348 break;
4351 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align);
4352 off += off_adjust;
4353 if (no_contents)
4355 /* We shouldn't need to align the segment on disk since
4356 the segment doesn't need file space, but the gABI
4357 arguably requires the alignment and glibc ld.so
4358 checks it. So to comply with the alignment
4359 requirement but not waste file space, we adjust
4360 p_offset for just this segment. (OFF_ADJUST is
4361 subtracted from OFF later.) This may put p_offset
4362 past the end of file, but that shouldn't matter. */
4364 else
4365 off_adjust = 0;
4367 /* Make sure the .dynamic section is the first section in the
4368 PT_DYNAMIC segment. */
4369 else if (p->p_type == PT_DYNAMIC
4370 && m->count > 1
4371 && strcmp (m->sections[0]->name, ".dynamic") != 0)
4373 _bfd_error_handler
4374 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4375 abfd);
4376 bfd_set_error (bfd_error_bad_value);
4377 return FALSE;
4379 /* Set the note section type to SHT_NOTE. */
4380 else if (p->p_type == PT_NOTE)
4381 for (i = 0; i < m->count; i++)
4382 elf_section_type (m->sections[i]) = SHT_NOTE;
4384 p->p_offset = 0;
4385 p->p_filesz = 0;
4386 p->p_memsz = 0;
4388 if (m->includes_filehdr)
4390 if (!m->p_flags_valid)
4391 p->p_flags |= PF_R;
4392 p->p_filesz = bed->s->sizeof_ehdr;
4393 p->p_memsz = bed->s->sizeof_ehdr;
4394 if (m->count > 0)
4396 BFD_ASSERT (p->p_type == PT_LOAD);
4398 if (p->p_vaddr < (bfd_vma) off)
4400 (*_bfd_error_handler)
4401 (_("%B: Not enough room for program headers, try linking with -N"),
4402 abfd);
4403 bfd_set_error (bfd_error_bad_value);
4404 return FALSE;
4407 p->p_vaddr -= off;
4408 if (!m->p_paddr_valid)
4409 p->p_paddr -= off;
4413 if (m->includes_phdrs)
4415 if (!m->p_flags_valid)
4416 p->p_flags |= PF_R;
4418 if (!m->includes_filehdr)
4420 p->p_offset = bed->s->sizeof_ehdr;
4422 if (m->count > 0)
4424 BFD_ASSERT (p->p_type == PT_LOAD);
4425 p->p_vaddr -= off - p->p_offset;
4426 if (!m->p_paddr_valid)
4427 p->p_paddr -= off - p->p_offset;
4431 p->p_filesz += alloc * bed->s->sizeof_phdr;
4432 p->p_memsz += alloc * bed->s->sizeof_phdr;
4433 if (m->count)
4435 p->p_filesz += header_pad;
4436 p->p_memsz += header_pad;
4440 if (p->p_type == PT_LOAD
4441 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4443 if (!m->includes_filehdr && !m->includes_phdrs)
4444 p->p_offset = off;
4445 else
4447 file_ptr adjust;
4449 adjust = off - (p->p_offset + p->p_filesz);
4450 if (!no_contents)
4451 p->p_filesz += adjust;
4452 p->p_memsz += adjust;
4456 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4457 maps. Set filepos for sections in PT_LOAD segments, and in
4458 core files, for sections in PT_NOTE segments.
4459 assign_file_positions_for_non_load_sections will set filepos
4460 for other sections and update p_filesz for other segments. */
4461 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4463 asection *sec;
4464 bfd_size_type align;
4465 Elf_Internal_Shdr *this_hdr;
4467 sec = *secpp;
4468 this_hdr = &elf_section_data (sec)->this_hdr;
4469 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
4471 if ((p->p_type == PT_LOAD
4472 || p->p_type == PT_TLS)
4473 && (this_hdr->sh_type != SHT_NOBITS
4474 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
4475 && ((this_hdr->sh_flags & SHF_TLS) == 0
4476 || p->p_type == PT_TLS))))
4478 bfd_vma p_start = p->p_paddr;
4479 bfd_vma p_end = p_start + p->p_memsz;
4480 bfd_vma s_start = sec->lma;
4481 bfd_vma adjust = s_start - p_end;
4483 if (s_start < p_end
4484 || p_end < p_start)
4486 (*_bfd_error_handler)
4487 (_("%B: section %A lma %#lx adjusted to %#lx"), abfd, sec,
4488 (unsigned long) s_start, (unsigned long) p_end);
4489 adjust = 0;
4490 sec->lma = p_end;
4492 p->p_memsz += adjust;
4494 if (this_hdr->sh_type != SHT_NOBITS)
4496 if (p->p_filesz + adjust < p->p_memsz)
4498 /* We have a PROGBITS section following NOBITS ones.
4499 Allocate file space for the NOBITS section(s) and
4500 zero it. */
4501 adjust = p->p_memsz - p->p_filesz;
4502 if (!write_zeros (abfd, off, adjust))
4503 return FALSE;
4505 off += adjust;
4506 p->p_filesz += adjust;
4510 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4512 /* The section at i == 0 is the one that actually contains
4513 everything. */
4514 if (i == 0)
4516 this_hdr->sh_offset = sec->filepos = off;
4517 off += this_hdr->sh_size;
4518 p->p_filesz = this_hdr->sh_size;
4519 p->p_memsz = 0;
4520 p->p_align = 1;
4522 else
4524 /* The rest are fake sections that shouldn't be written. */
4525 sec->filepos = 0;
4526 sec->size = 0;
4527 sec->flags = 0;
4528 continue;
4531 else
4533 if (p->p_type == PT_LOAD)
4535 this_hdr->sh_offset = sec->filepos = off;
4536 if (this_hdr->sh_type != SHT_NOBITS)
4537 off += this_hdr->sh_size;
4540 if (this_hdr->sh_type != SHT_NOBITS)
4542 p->p_filesz += this_hdr->sh_size;
4543 /* A load section without SHF_ALLOC is something like
4544 a note section in a PT_NOTE segment. These take
4545 file space but are not loaded into memory. */
4546 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4547 p->p_memsz += this_hdr->sh_size;
4549 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4551 if (p->p_type == PT_TLS)
4552 p->p_memsz += this_hdr->sh_size;
4554 /* .tbss is special. It doesn't contribute to p_memsz of
4555 normal segments. */
4556 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
4557 p->p_memsz += this_hdr->sh_size;
4560 if (align > p->p_align
4561 && !m->p_align_valid
4562 && (p->p_type != PT_LOAD
4563 || (abfd->flags & D_PAGED) == 0))
4564 p->p_align = align;
4567 if (!m->p_flags_valid)
4569 p->p_flags |= PF_R;
4570 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
4571 p->p_flags |= PF_X;
4572 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
4573 p->p_flags |= PF_W;
4576 off -= off_adjust;
4578 /* Check that all sections are in a PT_LOAD segment.
4579 Don't check funky gdb generated core files. */
4580 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
4582 bfd_boolean check_vma = TRUE;
4584 for (i = 1; i < m->count; i++)
4585 if (m->sections[i]->vma == m->sections[i - 1]->vma
4586 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
4587 ->this_hdr), p) != 0
4588 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
4589 ->this_hdr), p) != 0)
4591 /* Looks like we have overlays packed into the segment. */
4592 check_vma = FALSE;
4593 break;
4596 for (i = 0; i < m->count; i++)
4598 Elf_Internal_Shdr *this_hdr;
4599 asection *sec;
4601 sec = m->sections[i];
4602 this_hdr = &(elf_section_data(sec)->this_hdr);
4603 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0))
4605 (*_bfd_error_handler)
4606 (_("%B: section `%A' can't be allocated in segment %d"),
4607 abfd, sec, j);
4608 print_segment_map (m);
4614 elf_tdata (abfd)->next_file_pos = off;
4615 return TRUE;
4618 /* Assign file positions for the other sections. */
4620 static bfd_boolean
4621 assign_file_positions_for_non_load_sections (bfd *abfd,
4622 struct bfd_link_info *link_info)
4624 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4625 Elf_Internal_Shdr **i_shdrpp;
4626 Elf_Internal_Shdr **hdrpp;
4627 Elf_Internal_Phdr *phdrs;
4628 Elf_Internal_Phdr *p;
4629 struct elf_segment_map *m;
4630 bfd_vma filehdr_vaddr, filehdr_paddr;
4631 bfd_vma phdrs_vaddr, phdrs_paddr;
4632 file_ptr off;
4633 unsigned int num_sec;
4634 unsigned int i;
4635 unsigned int count;
4637 i_shdrpp = elf_elfsections (abfd);
4638 num_sec = elf_numsections (abfd);
4639 off = elf_tdata (abfd)->next_file_pos;
4640 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4642 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4643 Elf_Internal_Shdr *hdr;
4645 hdr = *hdrpp;
4646 if (hdr->bfd_section != NULL
4647 && (hdr->bfd_section->filepos != 0
4648 || (hdr->sh_type == SHT_NOBITS
4649 && hdr->contents == NULL)))
4650 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
4651 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4653 (*_bfd_error_handler)
4654 (_("%B: warning: allocated section `%s' not in segment"),
4655 abfd,
4656 (hdr->bfd_section == NULL
4657 ? "*unknown*"
4658 : hdr->bfd_section->name));
4659 /* We don't need to page align empty sections. */
4660 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
4661 off += vma_page_aligned_bias (hdr->sh_addr, off,
4662 bed->maxpagesize);
4663 else
4664 off += vma_page_aligned_bias (hdr->sh_addr, off,
4665 hdr->sh_addralign);
4666 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4667 FALSE);
4669 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4670 && hdr->bfd_section == NULL)
4671 || hdr == i_shdrpp[tdata->symtab_section]
4672 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4673 || hdr == i_shdrpp[tdata->strtab_section])
4674 hdr->sh_offset = -1;
4675 else
4676 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4679 /* Now that we have set the section file positions, we can set up
4680 the file positions for the non PT_LOAD segments. */
4681 count = 0;
4682 filehdr_vaddr = 0;
4683 filehdr_paddr = 0;
4684 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
4685 phdrs_paddr = 0;
4686 phdrs = elf_tdata (abfd)->phdr;
4687 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4688 m != NULL;
4689 m = m->next, p++)
4691 ++count;
4692 if (p->p_type != PT_LOAD)
4693 continue;
4695 if (m->includes_filehdr)
4697 filehdr_vaddr = p->p_vaddr;
4698 filehdr_paddr = p->p_paddr;
4700 if (m->includes_phdrs)
4702 phdrs_vaddr = p->p_vaddr;
4703 phdrs_paddr = p->p_paddr;
4704 if (m->includes_filehdr)
4706 phdrs_vaddr += bed->s->sizeof_ehdr;
4707 phdrs_paddr += bed->s->sizeof_ehdr;
4712 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4713 m != NULL;
4714 m = m->next, p++)
4716 if (p->p_type == PT_GNU_RELRO)
4718 const Elf_Internal_Phdr *lp;
4720 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
4722 if (link_info != NULL)
4724 /* During linking the range of the RELRO segment is passed
4725 in link_info. */
4726 for (lp = phdrs; lp < phdrs + count; ++lp)
4728 if (lp->p_type == PT_LOAD
4729 && lp->p_vaddr >= link_info->relro_start
4730 && lp->p_vaddr < link_info->relro_end
4731 && lp->p_vaddr + lp->p_filesz >= link_info->relro_end)
4732 break;
4735 else
4737 /* Otherwise we are copying an executable or shared
4738 library, but we need to use the same linker logic. */
4739 for (lp = phdrs; lp < phdrs + count; ++lp)
4741 if (lp->p_type == PT_LOAD
4742 && lp->p_paddr == p->p_paddr)
4743 break;
4747 if (lp < phdrs + count)
4749 p->p_vaddr = lp->p_vaddr;
4750 p->p_paddr = lp->p_paddr;
4751 p->p_offset = lp->p_offset;
4752 if (link_info != NULL)
4753 p->p_filesz = link_info->relro_end - lp->p_vaddr;
4754 else if (m->p_size_valid)
4755 p->p_filesz = m->p_size;
4756 else
4757 abort ();
4758 p->p_memsz = p->p_filesz;
4759 p->p_align = 1;
4760 p->p_flags = (lp->p_flags & ~PF_W);
4762 else
4764 memset (p, 0, sizeof *p);
4765 p->p_type = PT_NULL;
4768 else if (m->count != 0)
4770 if (p->p_type != PT_LOAD
4771 && (p->p_type != PT_NOTE
4772 || bfd_get_format (abfd) != bfd_core))
4774 Elf_Internal_Shdr *hdr;
4775 asection *sect;
4777 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
4779 sect = m->sections[m->count - 1];
4780 hdr = &elf_section_data (sect)->this_hdr;
4781 p->p_filesz = sect->filepos - m->sections[0]->filepos;
4782 if (hdr->sh_type != SHT_NOBITS)
4783 p->p_filesz += hdr->sh_size;
4784 p->p_offset = m->sections[0]->filepos;
4787 else if (m->includes_filehdr)
4789 p->p_vaddr = filehdr_vaddr;
4790 if (! m->p_paddr_valid)
4791 p->p_paddr = filehdr_paddr;
4793 else if (m->includes_phdrs)
4795 p->p_vaddr = phdrs_vaddr;
4796 if (! m->p_paddr_valid)
4797 p->p_paddr = phdrs_paddr;
4801 elf_tdata (abfd)->next_file_pos = off;
4803 return TRUE;
4806 /* Work out the file positions of all the sections. This is called by
4807 _bfd_elf_compute_section_file_positions. All the section sizes and
4808 VMAs must be known before this is called.
4810 Reloc sections come in two flavours: Those processed specially as
4811 "side-channel" data attached to a section to which they apply, and
4812 those that bfd doesn't process as relocations. The latter sort are
4813 stored in a normal bfd section by bfd_section_from_shdr. We don't
4814 consider the former sort here, unless they form part of the loadable
4815 image. Reloc sections not assigned here will be handled later by
4816 assign_file_positions_for_relocs.
4818 We also don't set the positions of the .symtab and .strtab here. */
4820 static bfd_boolean
4821 assign_file_positions_except_relocs (bfd *abfd,
4822 struct bfd_link_info *link_info)
4824 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4825 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4826 file_ptr off;
4827 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4829 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4830 && bfd_get_format (abfd) != bfd_core)
4832 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4833 unsigned int num_sec = elf_numsections (abfd);
4834 Elf_Internal_Shdr **hdrpp;
4835 unsigned int i;
4837 /* Start after the ELF header. */
4838 off = i_ehdrp->e_ehsize;
4840 /* We are not creating an executable, which means that we are
4841 not creating a program header, and that the actual order of
4842 the sections in the file is unimportant. */
4843 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4845 Elf_Internal_Shdr *hdr;
4847 hdr = *hdrpp;
4848 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4849 && hdr->bfd_section == NULL)
4850 || i == tdata->symtab_section
4851 || i == tdata->symtab_shndx_section
4852 || i == tdata->strtab_section)
4854 hdr->sh_offset = -1;
4856 else
4857 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4860 else
4862 unsigned int alloc;
4864 /* Assign file positions for the loaded sections based on the
4865 assignment of sections to segments. */
4866 if (!assign_file_positions_for_load_sections (abfd, link_info))
4867 return FALSE;
4869 /* And for non-load sections. */
4870 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
4871 return FALSE;
4873 if (bed->elf_backend_modify_program_headers != NULL)
4875 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
4876 return FALSE;
4879 /* Write out the program headers. */
4880 alloc = tdata->program_header_size / bed->s->sizeof_phdr;
4881 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4882 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
4883 return FALSE;
4885 off = tdata->next_file_pos;
4888 /* Place the section headers. */
4889 off = align_file_position (off, 1 << bed->s->log_file_align);
4890 i_ehdrp->e_shoff = off;
4891 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4893 tdata->next_file_pos = off;
4895 return TRUE;
4898 static bfd_boolean
4899 prep_headers (bfd *abfd)
4901 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
4902 struct elf_strtab_hash *shstrtab;
4903 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4905 i_ehdrp = elf_elfheader (abfd);
4907 shstrtab = _bfd_elf_strtab_init ();
4908 if (shstrtab == NULL)
4909 return FALSE;
4911 elf_shstrtab (abfd) = shstrtab;
4913 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4914 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4915 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4916 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4918 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4919 i_ehdrp->e_ident[EI_DATA] =
4920 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4921 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4923 if ((abfd->flags & DYNAMIC) != 0)
4924 i_ehdrp->e_type = ET_DYN;
4925 else if ((abfd->flags & EXEC_P) != 0)
4926 i_ehdrp->e_type = ET_EXEC;
4927 else if (bfd_get_format (abfd) == bfd_core)
4928 i_ehdrp->e_type = ET_CORE;
4929 else
4930 i_ehdrp->e_type = ET_REL;
4932 switch (bfd_get_arch (abfd))
4934 case bfd_arch_unknown:
4935 i_ehdrp->e_machine = EM_NONE;
4936 break;
4938 /* There used to be a long list of cases here, each one setting
4939 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4940 in the corresponding bfd definition. To avoid duplication,
4941 the switch was removed. Machines that need special handling
4942 can generally do it in elf_backend_final_write_processing(),
4943 unless they need the information earlier than the final write.
4944 Such need can generally be supplied by replacing the tests for
4945 e_machine with the conditions used to determine it. */
4946 default:
4947 i_ehdrp->e_machine = bed->elf_machine_code;
4950 i_ehdrp->e_version = bed->s->ev_current;
4951 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4953 /* No program header, for now. */
4954 i_ehdrp->e_phoff = 0;
4955 i_ehdrp->e_phentsize = 0;
4956 i_ehdrp->e_phnum = 0;
4958 /* Each bfd section is section header entry. */
4959 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4960 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4962 /* If we're building an executable, we'll need a program header table. */
4963 if (abfd->flags & EXEC_P)
4964 /* It all happens later. */
4966 else
4968 i_ehdrp->e_phentsize = 0;
4969 i_ehdrp->e_phoff = 0;
4972 elf_tdata (abfd)->symtab_hdr.sh_name =
4973 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
4974 elf_tdata (abfd)->strtab_hdr.sh_name =
4975 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
4976 elf_tdata (abfd)->shstrtab_hdr.sh_name =
4977 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
4978 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4979 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4980 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4981 return FALSE;
4983 return TRUE;
4986 /* Assign file positions for all the reloc sections which are not part
4987 of the loadable file image. */
4989 void
4990 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
4992 file_ptr off;
4993 unsigned int i, num_sec;
4994 Elf_Internal_Shdr **shdrpp;
4996 off = elf_tdata (abfd)->next_file_pos;
4998 num_sec = elf_numsections (abfd);
4999 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
5001 Elf_Internal_Shdr *shdrp;
5003 shdrp = *shdrpp;
5004 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
5005 && shdrp->sh_offset == -1)
5006 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
5009 elf_tdata (abfd)->next_file_pos = off;
5012 bfd_boolean
5013 _bfd_elf_write_object_contents (bfd *abfd)
5015 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5016 Elf_Internal_Shdr **i_shdrp;
5017 bfd_boolean failed;
5018 unsigned int count, num_sec;
5020 if (! abfd->output_has_begun
5021 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
5022 return FALSE;
5024 i_shdrp = elf_elfsections (abfd);
5026 failed = FALSE;
5027 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
5028 if (failed)
5029 return FALSE;
5031 _bfd_elf_assign_file_positions_for_relocs (abfd);
5033 /* After writing the headers, we need to write the sections too... */
5034 num_sec = elf_numsections (abfd);
5035 for (count = 1; count < num_sec; count++)
5037 if (bed->elf_backend_section_processing)
5038 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
5039 if (i_shdrp[count]->contents)
5041 bfd_size_type amt = i_shdrp[count]->sh_size;
5043 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
5044 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
5045 return FALSE;
5049 /* Write out the section header names. */
5050 if (elf_shstrtab (abfd) != NULL
5051 && (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
5052 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
5053 return FALSE;
5055 if (bed->elf_backend_final_write_processing)
5056 (*bed->elf_backend_final_write_processing) (abfd,
5057 elf_tdata (abfd)->linker);
5059 if (!bed->s->write_shdrs_and_ehdr (abfd))
5060 return FALSE;
5062 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
5063 if (elf_tdata (abfd)->after_write_object_contents)
5064 return (*elf_tdata (abfd)->after_write_object_contents) (abfd);
5066 return TRUE;
5069 bfd_boolean
5070 _bfd_elf_write_corefile_contents (bfd *abfd)
5072 /* Hopefully this can be done just like an object file. */
5073 return _bfd_elf_write_object_contents (abfd);
5076 /* Given a section, search the header to find them. */
5078 unsigned int
5079 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
5081 const struct elf_backend_data *bed;
5082 unsigned int sec_index;
5084 if (elf_section_data (asect) != NULL
5085 && elf_section_data (asect)->this_idx != 0)
5086 return elf_section_data (asect)->this_idx;
5088 if (bfd_is_abs_section (asect))
5089 sec_index = SHN_ABS;
5090 else if (bfd_is_com_section (asect))
5091 sec_index = SHN_COMMON;
5092 else if (bfd_is_und_section (asect))
5093 sec_index = SHN_UNDEF;
5094 else
5095 sec_index = SHN_BAD;
5097 bed = get_elf_backend_data (abfd);
5098 if (bed->elf_backend_section_from_bfd_section)
5100 int retval = sec_index;
5102 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
5103 return retval;
5106 if (sec_index == SHN_BAD)
5107 bfd_set_error (bfd_error_nonrepresentable_section);
5109 return sec_index;
5112 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5113 on error. */
5116 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
5118 asymbol *asym_ptr = *asym_ptr_ptr;
5119 int idx;
5120 flagword flags = asym_ptr->flags;
5122 /* When gas creates relocations against local labels, it creates its
5123 own symbol for the section, but does put the symbol into the
5124 symbol chain, so udata is 0. When the linker is generating
5125 relocatable output, this section symbol may be for one of the
5126 input sections rather than the output section. */
5127 if (asym_ptr->udata.i == 0
5128 && (flags & BSF_SECTION_SYM)
5129 && asym_ptr->section)
5131 asection *sec;
5132 int indx;
5134 sec = asym_ptr->section;
5135 if (sec->owner != abfd && sec->output_section != NULL)
5136 sec = sec->output_section;
5137 if (sec->owner == abfd
5138 && (indx = sec->index) < elf_num_section_syms (abfd)
5139 && elf_section_syms (abfd)[indx] != NULL)
5140 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
5143 idx = asym_ptr->udata.i;
5145 if (idx == 0)
5147 /* This case can occur when using --strip-symbol on a symbol
5148 which is used in a relocation entry. */
5149 (*_bfd_error_handler)
5150 (_("%B: symbol `%s' required but not present"),
5151 abfd, bfd_asymbol_name (asym_ptr));
5152 bfd_set_error (bfd_error_no_symbols);
5153 return -1;
5156 #if DEBUG & 4
5158 fprintf (stderr,
5159 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
5160 (long) asym_ptr, asym_ptr->name, idx, flags,
5161 elf_symbol_flags (flags));
5162 fflush (stderr);
5164 #endif
5166 return idx;
5169 /* Rewrite program header information. */
5171 static bfd_boolean
5172 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
5174 Elf_Internal_Ehdr *iehdr;
5175 struct elf_segment_map *map;
5176 struct elf_segment_map *map_first;
5177 struct elf_segment_map **pointer_to_map;
5178 Elf_Internal_Phdr *segment;
5179 asection *section;
5180 unsigned int i;
5181 unsigned int num_segments;
5182 bfd_boolean phdr_included = FALSE;
5183 bfd_boolean p_paddr_valid;
5184 bfd_vma maxpagesize;
5185 struct elf_segment_map *phdr_adjust_seg = NULL;
5186 unsigned int phdr_adjust_num = 0;
5187 const struct elf_backend_data *bed;
5189 bed = get_elf_backend_data (ibfd);
5190 iehdr = elf_elfheader (ibfd);
5192 map_first = NULL;
5193 pointer_to_map = &map_first;
5195 num_segments = elf_elfheader (ibfd)->e_phnum;
5196 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
5198 /* Returns the end address of the segment + 1. */
5199 #define SEGMENT_END(segment, start) \
5200 (start + (segment->p_memsz > segment->p_filesz \
5201 ? segment->p_memsz : segment->p_filesz))
5203 #define SECTION_SIZE(section, segment) \
5204 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5205 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5206 ? section->size : 0)
5208 /* Returns TRUE if the given section is contained within
5209 the given segment. VMA addresses are compared. */
5210 #define IS_CONTAINED_BY_VMA(section, segment) \
5211 (section->vma >= segment->p_vaddr \
5212 && (section->vma + SECTION_SIZE (section, segment) \
5213 <= (SEGMENT_END (segment, segment->p_vaddr))))
5215 /* Returns TRUE if the given section is contained within
5216 the given segment. LMA addresses are compared. */
5217 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5218 (section->lma >= base \
5219 && (section->lma + SECTION_SIZE (section, segment) \
5220 <= SEGMENT_END (segment, base)))
5222 /* Handle PT_NOTE segment. */
5223 #define IS_NOTE(p, s) \
5224 (p->p_type == PT_NOTE \
5225 && elf_section_type (s) == SHT_NOTE \
5226 && (bfd_vma) s->filepos >= p->p_offset \
5227 && ((bfd_vma) s->filepos + s->size \
5228 <= p->p_offset + p->p_filesz))
5230 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5231 etc. */
5232 #define IS_COREFILE_NOTE(p, s) \
5233 (IS_NOTE (p, s) \
5234 && bfd_get_format (ibfd) == bfd_core \
5235 && s->vma == 0 \
5236 && s->lma == 0)
5238 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5239 linker, which generates a PT_INTERP section with p_vaddr and
5240 p_memsz set to 0. */
5241 #define IS_SOLARIS_PT_INTERP(p, s) \
5242 (p->p_vaddr == 0 \
5243 && p->p_paddr == 0 \
5244 && p->p_memsz == 0 \
5245 && p->p_filesz > 0 \
5246 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5247 && s->size > 0 \
5248 && (bfd_vma) s->filepos >= p->p_offset \
5249 && ((bfd_vma) s->filepos + s->size \
5250 <= p->p_offset + p->p_filesz))
5252 /* Decide if the given section should be included in the given segment.
5253 A section will be included if:
5254 1. It is within the address space of the segment -- we use the LMA
5255 if that is set for the segment and the VMA otherwise,
5256 2. It is an allocated section or a NOTE section in a PT_NOTE
5257 segment.
5258 3. There is an output section associated with it,
5259 4. The section has not already been allocated to a previous segment.
5260 5. PT_GNU_STACK segments do not include any sections.
5261 6. PT_TLS segment includes only SHF_TLS sections.
5262 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5263 8. PT_DYNAMIC should not contain empty sections at the beginning
5264 (with the possible exception of .dynamic). */
5265 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5266 ((((segment->p_paddr \
5267 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5268 : IS_CONTAINED_BY_VMA (section, segment)) \
5269 && (section->flags & SEC_ALLOC) != 0) \
5270 || IS_NOTE (segment, section)) \
5271 && segment->p_type != PT_GNU_STACK \
5272 && (segment->p_type != PT_TLS \
5273 || (section->flags & SEC_THREAD_LOCAL)) \
5274 && (segment->p_type == PT_LOAD \
5275 || segment->p_type == PT_TLS \
5276 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5277 && (segment->p_type != PT_DYNAMIC \
5278 || SECTION_SIZE (section, segment) > 0 \
5279 || (segment->p_paddr \
5280 ? segment->p_paddr != section->lma \
5281 : segment->p_vaddr != section->vma) \
5282 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5283 == 0)) \
5284 && !section->segment_mark)
5286 /* If the output section of a section in the input segment is NULL,
5287 it is removed from the corresponding output segment. */
5288 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5289 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5290 && section->output_section != NULL)
5292 /* Returns TRUE iff seg1 starts after the end of seg2. */
5293 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5294 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5296 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5297 their VMA address ranges and their LMA address ranges overlap.
5298 It is possible to have overlapping VMA ranges without overlapping LMA
5299 ranges. RedBoot images for example can have both .data and .bss mapped
5300 to the same VMA range, but with the .data section mapped to a different
5301 LMA. */
5302 #define SEGMENT_OVERLAPS(seg1, seg2) \
5303 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5304 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5305 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5306 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5308 /* Initialise the segment mark field. */
5309 for (section = ibfd->sections; section != NULL; section = section->next)
5310 section->segment_mark = FALSE;
5312 /* The Solaris linker creates program headers in which all the
5313 p_paddr fields are zero. When we try to objcopy or strip such a
5314 file, we get confused. Check for this case, and if we find it
5315 don't set the p_paddr_valid fields. */
5316 p_paddr_valid = FALSE;
5317 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5318 i < num_segments;
5319 i++, segment++)
5320 if (segment->p_paddr != 0)
5322 p_paddr_valid = TRUE;
5323 break;
5326 /* Scan through the segments specified in the program header
5327 of the input BFD. For this first scan we look for overlaps
5328 in the loadable segments. These can be created by weird
5329 parameters to objcopy. Also, fix some solaris weirdness. */
5330 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5331 i < num_segments;
5332 i++, segment++)
5334 unsigned int j;
5335 Elf_Internal_Phdr *segment2;
5337 if (segment->p_type == PT_INTERP)
5338 for (section = ibfd->sections; section; section = section->next)
5339 if (IS_SOLARIS_PT_INTERP (segment, section))
5341 /* Mininal change so that the normal section to segment
5342 assignment code will work. */
5343 segment->p_vaddr = section->vma;
5344 break;
5347 if (segment->p_type != PT_LOAD)
5349 /* Remove PT_GNU_RELRO segment. */
5350 if (segment->p_type == PT_GNU_RELRO)
5351 segment->p_type = PT_NULL;
5352 continue;
5355 /* Determine if this segment overlaps any previous segments. */
5356 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
5358 bfd_signed_vma extra_length;
5360 if (segment2->p_type != PT_LOAD
5361 || !SEGMENT_OVERLAPS (segment, segment2))
5362 continue;
5364 /* Merge the two segments together. */
5365 if (segment2->p_vaddr < segment->p_vaddr)
5367 /* Extend SEGMENT2 to include SEGMENT and then delete
5368 SEGMENT. */
5369 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
5370 - SEGMENT_END (segment2, segment2->p_vaddr));
5372 if (extra_length > 0)
5374 segment2->p_memsz += extra_length;
5375 segment2->p_filesz += extra_length;
5378 segment->p_type = PT_NULL;
5380 /* Since we have deleted P we must restart the outer loop. */
5381 i = 0;
5382 segment = elf_tdata (ibfd)->phdr;
5383 break;
5385 else
5387 /* Extend SEGMENT to include SEGMENT2 and then delete
5388 SEGMENT2. */
5389 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
5390 - SEGMENT_END (segment, segment->p_vaddr));
5392 if (extra_length > 0)
5394 segment->p_memsz += extra_length;
5395 segment->p_filesz += extra_length;
5398 segment2->p_type = PT_NULL;
5403 /* The second scan attempts to assign sections to segments. */
5404 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5405 i < num_segments;
5406 i++, segment++)
5408 unsigned int section_count;
5409 asection **sections;
5410 asection *output_section;
5411 unsigned int isec;
5412 bfd_vma matching_lma;
5413 bfd_vma suggested_lma;
5414 unsigned int j;
5415 bfd_size_type amt;
5416 asection *first_section;
5417 bfd_boolean first_matching_lma;
5418 bfd_boolean first_suggested_lma;
5420 if (segment->p_type == PT_NULL)
5421 continue;
5423 first_section = NULL;
5424 /* Compute how many sections might be placed into this segment. */
5425 for (section = ibfd->sections, section_count = 0;
5426 section != NULL;
5427 section = section->next)
5429 /* Find the first section in the input segment, which may be
5430 removed from the corresponding output segment. */
5431 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
5433 if (first_section == NULL)
5434 first_section = section;
5435 if (section->output_section != NULL)
5436 ++section_count;
5440 /* Allocate a segment map big enough to contain
5441 all of the sections we have selected. */
5442 amt = sizeof (struct elf_segment_map);
5443 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5444 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
5445 if (map == NULL)
5446 return FALSE;
5448 /* Initialise the fields of the segment map. Default to
5449 using the physical address of the segment in the input BFD. */
5450 map->next = NULL;
5451 map->p_type = segment->p_type;
5452 map->p_flags = segment->p_flags;
5453 map->p_flags_valid = 1;
5455 /* If the first section in the input segment is removed, there is
5456 no need to preserve segment physical address in the corresponding
5457 output segment. */
5458 if (!first_section || first_section->output_section != NULL)
5460 map->p_paddr = segment->p_paddr;
5461 map->p_paddr_valid = p_paddr_valid;
5464 /* Determine if this segment contains the ELF file header
5465 and if it contains the program headers themselves. */
5466 map->includes_filehdr = (segment->p_offset == 0
5467 && segment->p_filesz >= iehdr->e_ehsize);
5468 map->includes_phdrs = 0;
5470 if (!phdr_included || segment->p_type != PT_LOAD)
5472 map->includes_phdrs =
5473 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5474 && (segment->p_offset + segment->p_filesz
5475 >= ((bfd_vma) iehdr->e_phoff
5476 + iehdr->e_phnum * iehdr->e_phentsize)));
5478 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5479 phdr_included = TRUE;
5482 if (section_count == 0)
5484 /* Special segments, such as the PT_PHDR segment, may contain
5485 no sections, but ordinary, loadable segments should contain
5486 something. They are allowed by the ELF spec however, so only
5487 a warning is produced. */
5488 if (segment->p_type == PT_LOAD)
5489 (*_bfd_error_handler) (_("%B: warning: Empty loadable segment"
5490 " detected, is this intentional ?\n"),
5491 ibfd);
5493 map->count = 0;
5494 *pointer_to_map = map;
5495 pointer_to_map = &map->next;
5497 continue;
5500 /* Now scan the sections in the input BFD again and attempt
5501 to add their corresponding output sections to the segment map.
5502 The problem here is how to handle an output section which has
5503 been moved (ie had its LMA changed). There are four possibilities:
5505 1. None of the sections have been moved.
5506 In this case we can continue to use the segment LMA from the
5507 input BFD.
5509 2. All of the sections have been moved by the same amount.
5510 In this case we can change the segment's LMA to match the LMA
5511 of the first section.
5513 3. Some of the sections have been moved, others have not.
5514 In this case those sections which have not been moved can be
5515 placed in the current segment which will have to have its size,
5516 and possibly its LMA changed, and a new segment or segments will
5517 have to be created to contain the other sections.
5519 4. The sections have been moved, but not by the same amount.
5520 In this case we can change the segment's LMA to match the LMA
5521 of the first section and we will have to create a new segment
5522 or segments to contain the other sections.
5524 In order to save time, we allocate an array to hold the section
5525 pointers that we are interested in. As these sections get assigned
5526 to a segment, they are removed from this array. */
5528 sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
5529 if (sections == NULL)
5530 return FALSE;
5532 /* Step One: Scan for segment vs section LMA conflicts.
5533 Also add the sections to the section array allocated above.
5534 Also add the sections to the current segment. In the common
5535 case, where the sections have not been moved, this means that
5536 we have completely filled the segment, and there is nothing
5537 more to do. */
5538 isec = 0;
5539 matching_lma = 0;
5540 suggested_lma = 0;
5541 first_matching_lma = TRUE;
5542 first_suggested_lma = TRUE;
5544 for (section = ibfd->sections;
5545 section != NULL;
5546 section = section->next)
5547 if (section == first_section)
5548 break;
5550 for (j = 0; section != NULL; section = section->next)
5552 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5554 output_section = section->output_section;
5556 sections[j++] = section;
5558 /* The Solaris native linker always sets p_paddr to 0.
5559 We try to catch that case here, and set it to the
5560 correct value. Note - some backends require that
5561 p_paddr be left as zero. */
5562 if (!p_paddr_valid
5563 && segment->p_vaddr != 0
5564 && !bed->want_p_paddr_set_to_zero
5565 && isec == 0
5566 && output_section->lma != 0
5567 && output_section->vma == (segment->p_vaddr
5568 + (map->includes_filehdr
5569 ? iehdr->e_ehsize
5570 : 0)
5571 + (map->includes_phdrs
5572 ? (iehdr->e_phnum
5573 * iehdr->e_phentsize)
5574 : 0)))
5575 map->p_paddr = segment->p_vaddr;
5577 /* Match up the physical address of the segment with the
5578 LMA address of the output section. */
5579 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5580 || IS_COREFILE_NOTE (segment, section)
5581 || (bed->want_p_paddr_set_to_zero
5582 && IS_CONTAINED_BY_VMA (output_section, segment)))
5584 if (first_matching_lma || output_section->lma < matching_lma)
5586 matching_lma = output_section->lma;
5587 first_matching_lma = FALSE;
5590 /* We assume that if the section fits within the segment
5591 then it does not overlap any other section within that
5592 segment. */
5593 map->sections[isec++] = output_section;
5595 else if (first_suggested_lma)
5597 suggested_lma = output_section->lma;
5598 first_suggested_lma = FALSE;
5601 if (j == section_count)
5602 break;
5606 BFD_ASSERT (j == section_count);
5608 /* Step Two: Adjust the physical address of the current segment,
5609 if necessary. */
5610 if (isec == section_count)
5612 /* All of the sections fitted within the segment as currently
5613 specified. This is the default case. Add the segment to
5614 the list of built segments and carry on to process the next
5615 program header in the input BFD. */
5616 map->count = section_count;
5617 *pointer_to_map = map;
5618 pointer_to_map = &map->next;
5620 if (p_paddr_valid
5621 && !bed->want_p_paddr_set_to_zero
5622 && matching_lma != map->p_paddr
5623 && !map->includes_filehdr
5624 && !map->includes_phdrs)
5625 /* There is some padding before the first section in the
5626 segment. So, we must account for that in the output
5627 segment's vma. */
5628 map->p_vaddr_offset = matching_lma - map->p_paddr;
5630 free (sections);
5631 continue;
5633 else
5635 if (!first_matching_lma)
5637 /* At least one section fits inside the current segment.
5638 Keep it, but modify its physical address to match the
5639 LMA of the first section that fitted. */
5640 map->p_paddr = matching_lma;
5642 else
5644 /* None of the sections fitted inside the current segment.
5645 Change the current segment's physical address to match
5646 the LMA of the first section. */
5647 map->p_paddr = suggested_lma;
5650 /* Offset the segment physical address from the lma
5651 to allow for space taken up by elf headers. */
5652 if (map->includes_filehdr)
5654 if (map->p_paddr >= iehdr->e_ehsize)
5655 map->p_paddr -= iehdr->e_ehsize;
5656 else
5658 map->includes_filehdr = FALSE;
5659 map->includes_phdrs = FALSE;
5663 if (map->includes_phdrs)
5665 if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize)
5667 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5669 /* iehdr->e_phnum is just an estimate of the number
5670 of program headers that we will need. Make a note
5671 here of the number we used and the segment we chose
5672 to hold these headers, so that we can adjust the
5673 offset when we know the correct value. */
5674 phdr_adjust_num = iehdr->e_phnum;
5675 phdr_adjust_seg = map;
5677 else
5678 map->includes_phdrs = FALSE;
5682 /* Step Three: Loop over the sections again, this time assigning
5683 those that fit to the current segment and removing them from the
5684 sections array; but making sure not to leave large gaps. Once all
5685 possible sections have been assigned to the current segment it is
5686 added to the list of built segments and if sections still remain
5687 to be assigned, a new segment is constructed before repeating
5688 the loop. */
5689 isec = 0;
5692 map->count = 0;
5693 suggested_lma = 0;
5694 first_suggested_lma = TRUE;
5696 /* Fill the current segment with sections that fit. */
5697 for (j = 0; j < section_count; j++)
5699 section = sections[j];
5701 if (section == NULL)
5702 continue;
5704 output_section = section->output_section;
5706 BFD_ASSERT (output_section != NULL);
5708 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5709 || IS_COREFILE_NOTE (segment, section))
5711 if (map->count == 0)
5713 /* If the first section in a segment does not start at
5714 the beginning of the segment, then something is
5715 wrong. */
5716 if (output_section->lma
5717 != (map->p_paddr
5718 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5719 + (map->includes_phdrs
5720 ? iehdr->e_phnum * iehdr->e_phentsize
5721 : 0)))
5722 abort ();
5724 else
5726 asection *prev_sec;
5728 prev_sec = map->sections[map->count - 1];
5730 /* If the gap between the end of the previous section
5731 and the start of this section is more than
5732 maxpagesize then we need to start a new segment. */
5733 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
5734 maxpagesize)
5735 < BFD_ALIGN (output_section->lma, maxpagesize))
5736 || (prev_sec->lma + prev_sec->size
5737 > output_section->lma))
5739 if (first_suggested_lma)
5741 suggested_lma = output_section->lma;
5742 first_suggested_lma = FALSE;
5745 continue;
5749 map->sections[map->count++] = output_section;
5750 ++isec;
5751 sections[j] = NULL;
5752 section->segment_mark = TRUE;
5754 else if (first_suggested_lma)
5756 suggested_lma = output_section->lma;
5757 first_suggested_lma = FALSE;
5761 BFD_ASSERT (map->count > 0);
5763 /* Add the current segment to the list of built segments. */
5764 *pointer_to_map = map;
5765 pointer_to_map = &map->next;
5767 if (isec < section_count)
5769 /* We still have not allocated all of the sections to
5770 segments. Create a new segment here, initialise it
5771 and carry on looping. */
5772 amt = sizeof (struct elf_segment_map);
5773 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5774 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
5775 if (map == NULL)
5777 free (sections);
5778 return FALSE;
5781 /* Initialise the fields of the segment map. Set the physical
5782 physical address to the LMA of the first section that has
5783 not yet been assigned. */
5784 map->next = NULL;
5785 map->p_type = segment->p_type;
5786 map->p_flags = segment->p_flags;
5787 map->p_flags_valid = 1;
5788 map->p_paddr = suggested_lma;
5789 map->p_paddr_valid = p_paddr_valid;
5790 map->includes_filehdr = 0;
5791 map->includes_phdrs = 0;
5794 while (isec < section_count);
5796 free (sections);
5799 elf_tdata (obfd)->segment_map = map_first;
5801 /* If we had to estimate the number of program headers that were
5802 going to be needed, then check our estimate now and adjust
5803 the offset if necessary. */
5804 if (phdr_adjust_seg != NULL)
5806 unsigned int count;
5808 for (count = 0, map = map_first; map != NULL; map = map->next)
5809 count++;
5811 if (count > phdr_adjust_num)
5812 phdr_adjust_seg->p_paddr
5813 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5816 #undef SEGMENT_END
5817 #undef SECTION_SIZE
5818 #undef IS_CONTAINED_BY_VMA
5819 #undef IS_CONTAINED_BY_LMA
5820 #undef IS_NOTE
5821 #undef IS_COREFILE_NOTE
5822 #undef IS_SOLARIS_PT_INTERP
5823 #undef IS_SECTION_IN_INPUT_SEGMENT
5824 #undef INCLUDE_SECTION_IN_SEGMENT
5825 #undef SEGMENT_AFTER_SEGMENT
5826 #undef SEGMENT_OVERLAPS
5827 return TRUE;
5830 /* Copy ELF program header information. */
5832 static bfd_boolean
5833 copy_elf_program_header (bfd *ibfd, bfd *obfd)
5835 Elf_Internal_Ehdr *iehdr;
5836 struct elf_segment_map *map;
5837 struct elf_segment_map *map_first;
5838 struct elf_segment_map **pointer_to_map;
5839 Elf_Internal_Phdr *segment;
5840 unsigned int i;
5841 unsigned int num_segments;
5842 bfd_boolean phdr_included = FALSE;
5843 bfd_boolean p_paddr_valid;
5845 iehdr = elf_elfheader (ibfd);
5847 map_first = NULL;
5848 pointer_to_map = &map_first;
5850 /* If all the segment p_paddr fields are zero, don't set
5851 map->p_paddr_valid. */
5852 p_paddr_valid = FALSE;
5853 num_segments = elf_elfheader (ibfd)->e_phnum;
5854 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5855 i < num_segments;
5856 i++, segment++)
5857 if (segment->p_paddr != 0)
5859 p_paddr_valid = TRUE;
5860 break;
5863 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5864 i < num_segments;
5865 i++, segment++)
5867 asection *section;
5868 unsigned int section_count;
5869 bfd_size_type amt;
5870 Elf_Internal_Shdr *this_hdr;
5871 asection *first_section = NULL;
5872 asection *lowest_section = NULL;
5874 /* Compute how many sections are in this segment. */
5875 for (section = ibfd->sections, section_count = 0;
5876 section != NULL;
5877 section = section->next)
5879 this_hdr = &(elf_section_data(section)->this_hdr);
5880 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
5882 if (!first_section)
5883 first_section = lowest_section = section;
5884 if (section->lma < lowest_section->lma)
5885 lowest_section = section;
5886 section_count++;
5890 /* Allocate a segment map big enough to contain
5891 all of the sections we have selected. */
5892 amt = sizeof (struct elf_segment_map);
5893 if (section_count != 0)
5894 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5895 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
5896 if (map == NULL)
5897 return FALSE;
5899 /* Initialize the fields of the output segment map with the
5900 input segment. */
5901 map->next = NULL;
5902 map->p_type = segment->p_type;
5903 map->p_flags = segment->p_flags;
5904 map->p_flags_valid = 1;
5905 map->p_paddr = segment->p_paddr;
5906 map->p_paddr_valid = p_paddr_valid;
5907 map->p_align = segment->p_align;
5908 map->p_align_valid = 1;
5909 map->p_vaddr_offset = 0;
5911 if (map->p_type == PT_GNU_RELRO)
5913 /* The PT_GNU_RELRO segment may contain the first a few
5914 bytes in the .got.plt section even if the whole .got.plt
5915 section isn't in the PT_GNU_RELRO segment. We won't
5916 change the size of the PT_GNU_RELRO segment. */
5917 map->p_size = segment->p_memsz;
5918 map->p_size_valid = 1;
5921 /* Determine if this segment contains the ELF file header
5922 and if it contains the program headers themselves. */
5923 map->includes_filehdr = (segment->p_offset == 0
5924 && segment->p_filesz >= iehdr->e_ehsize);
5926 map->includes_phdrs = 0;
5927 if (! phdr_included || segment->p_type != PT_LOAD)
5929 map->includes_phdrs =
5930 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5931 && (segment->p_offset + segment->p_filesz
5932 >= ((bfd_vma) iehdr->e_phoff
5933 + iehdr->e_phnum * iehdr->e_phentsize)));
5935 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5936 phdr_included = TRUE;
5939 if (map->includes_filehdr && first_section)
5940 /* We need to keep the space used by the headers fixed. */
5941 map->header_size = first_section->vma - segment->p_vaddr;
5943 if (!map->includes_phdrs
5944 && !map->includes_filehdr
5945 && map->p_paddr_valid)
5946 /* There is some other padding before the first section. */
5947 map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
5948 - segment->p_paddr);
5950 if (section_count != 0)
5952 unsigned int isec = 0;
5954 for (section = first_section;
5955 section != NULL;
5956 section = section->next)
5958 this_hdr = &(elf_section_data(section)->this_hdr);
5959 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
5961 map->sections[isec++] = section->output_section;
5962 if (isec == section_count)
5963 break;
5968 map->count = section_count;
5969 *pointer_to_map = map;
5970 pointer_to_map = &map->next;
5973 elf_tdata (obfd)->segment_map = map_first;
5974 return TRUE;
5977 /* Copy private BFD data. This copies or rewrites ELF program header
5978 information. */
5980 static bfd_boolean
5981 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
5983 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5984 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5985 return TRUE;
5987 if (elf_tdata (ibfd)->phdr == NULL)
5988 return TRUE;
5990 if (ibfd->xvec == obfd->xvec)
5992 /* Check to see if any sections in the input BFD
5993 covered by ELF program header have changed. */
5994 Elf_Internal_Phdr *segment;
5995 asection *section, *osec;
5996 unsigned int i, num_segments;
5997 Elf_Internal_Shdr *this_hdr;
5998 const struct elf_backend_data *bed;
6000 bed = get_elf_backend_data (ibfd);
6002 /* Regenerate the segment map if p_paddr is set to 0. */
6003 if (bed->want_p_paddr_set_to_zero)
6004 goto rewrite;
6006 /* Initialize the segment mark field. */
6007 for (section = obfd->sections; section != NULL;
6008 section = section->next)
6009 section->segment_mark = FALSE;
6011 num_segments = elf_elfheader (ibfd)->e_phnum;
6012 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6013 i < num_segments;
6014 i++, segment++)
6016 /* PR binutils/3535. The Solaris linker always sets the p_paddr
6017 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
6018 which severly confuses things, so always regenerate the segment
6019 map in this case. */
6020 if (segment->p_paddr == 0
6021 && segment->p_memsz == 0
6022 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
6023 goto rewrite;
6025 for (section = ibfd->sections;
6026 section != NULL; section = section->next)
6028 /* We mark the output section so that we know it comes
6029 from the input BFD. */
6030 osec = section->output_section;
6031 if (osec)
6032 osec->segment_mark = TRUE;
6034 /* Check if this section is covered by the segment. */
6035 this_hdr = &(elf_section_data(section)->this_hdr);
6036 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6038 /* FIXME: Check if its output section is changed or
6039 removed. What else do we need to check? */
6040 if (osec == NULL
6041 || section->flags != osec->flags
6042 || section->lma != osec->lma
6043 || section->vma != osec->vma
6044 || section->size != osec->size
6045 || section->rawsize != osec->rawsize
6046 || section->alignment_power != osec->alignment_power)
6047 goto rewrite;
6052 /* Check to see if any output section do not come from the
6053 input BFD. */
6054 for (section = obfd->sections; section != NULL;
6055 section = section->next)
6057 if (section->segment_mark == FALSE)
6058 goto rewrite;
6059 else
6060 section->segment_mark = FALSE;
6063 return copy_elf_program_header (ibfd, obfd);
6066 rewrite:
6067 return rewrite_elf_program_header (ibfd, obfd);
6070 /* Initialize private output section information from input section. */
6072 bfd_boolean
6073 _bfd_elf_init_private_section_data (bfd *ibfd,
6074 asection *isec,
6075 bfd *obfd,
6076 asection *osec,
6077 struct bfd_link_info *link_info)
6080 Elf_Internal_Shdr *ihdr, *ohdr;
6081 bfd_boolean final_link = link_info != NULL && !link_info->relocatable;
6083 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6084 || obfd->xvec->flavour != bfd_target_elf_flavour)
6085 return TRUE;
6087 /* For objcopy and relocatable link, don't copy the output ELF
6088 section type from input if the output BFD section flags have been
6089 set to something different. For a final link allow some flags
6090 that the linker clears to differ. */
6091 if (elf_section_type (osec) == SHT_NULL
6092 && (osec->flags == isec->flags
6093 || (final_link
6094 && ((osec->flags ^ isec->flags)
6095 & ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES)) == 0)))
6096 elf_section_type (osec) = elf_section_type (isec);
6098 /* FIXME: Is this correct for all OS/PROC specific flags? */
6099 elf_section_flags (osec) |= (elf_section_flags (isec)
6100 & (SHF_MASKOS | SHF_MASKPROC));
6102 /* Set things up for objcopy and relocatable link. The output
6103 SHT_GROUP section will have its elf_next_in_group pointing back
6104 to the input group members. Ignore linker created group section.
6105 See elfNN_ia64_object_p in elfxx-ia64.c. */
6106 if (!final_link)
6108 if (elf_sec_group (isec) == NULL
6109 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
6111 if (elf_section_flags (isec) & SHF_GROUP)
6112 elf_section_flags (osec) |= SHF_GROUP;
6113 elf_next_in_group (osec) = elf_next_in_group (isec);
6114 elf_section_data (osec)->group = elf_section_data (isec)->group;
6118 ihdr = &elf_section_data (isec)->this_hdr;
6120 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6121 don't use the output section of the linked-to section since it
6122 may be NULL at this point. */
6123 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
6125 ohdr = &elf_section_data (osec)->this_hdr;
6126 ohdr->sh_flags |= SHF_LINK_ORDER;
6127 elf_linked_to_section (osec) = elf_linked_to_section (isec);
6130 osec->use_rela_p = isec->use_rela_p;
6132 return TRUE;
6135 /* Copy private section information. This copies over the entsize
6136 field, and sometimes the info field. */
6138 bfd_boolean
6139 _bfd_elf_copy_private_section_data (bfd *ibfd,
6140 asection *isec,
6141 bfd *obfd,
6142 asection *osec)
6144 Elf_Internal_Shdr *ihdr, *ohdr;
6146 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6147 || obfd->xvec->flavour != bfd_target_elf_flavour)
6148 return TRUE;
6150 ihdr = &elf_section_data (isec)->this_hdr;
6151 ohdr = &elf_section_data (osec)->this_hdr;
6153 ohdr->sh_entsize = ihdr->sh_entsize;
6155 if (ihdr->sh_type == SHT_SYMTAB
6156 || ihdr->sh_type == SHT_DYNSYM
6157 || ihdr->sh_type == SHT_GNU_verneed
6158 || ihdr->sh_type == SHT_GNU_verdef)
6159 ohdr->sh_info = ihdr->sh_info;
6161 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
6162 NULL);
6165 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
6166 necessary if we are removing either the SHT_GROUP section or any of
6167 the group member sections. DISCARDED is the value that a section's
6168 output_section has if the section will be discarded, NULL when this
6169 function is called from objcopy, bfd_abs_section_ptr when called
6170 from the linker. */
6172 bfd_boolean
6173 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
6175 asection *isec;
6177 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
6178 if (elf_section_type (isec) == SHT_GROUP)
6180 asection *first = elf_next_in_group (isec);
6181 asection *s = first;
6182 bfd_size_type removed = 0;
6184 while (s != NULL)
6186 /* If this member section is being output but the
6187 SHT_GROUP section is not, then clear the group info
6188 set up by _bfd_elf_copy_private_section_data. */
6189 if (s->output_section != discarded
6190 && isec->output_section == discarded)
6192 elf_section_flags (s->output_section) &= ~SHF_GROUP;
6193 elf_group_name (s->output_section) = NULL;
6195 /* Conversely, if the member section is not being output
6196 but the SHT_GROUP section is, then adjust its size. */
6197 else if (s->output_section == discarded
6198 && isec->output_section != discarded)
6199 removed += 4;
6200 s = elf_next_in_group (s);
6201 if (s == first)
6202 break;
6204 if (removed != 0)
6206 if (discarded != NULL)
6208 /* If we've been called for ld -r, then we need to
6209 adjust the input section size. This function may
6210 be called multiple times, so save the original
6211 size. */
6212 if (isec->rawsize == 0)
6213 isec->rawsize = isec->size;
6214 isec->size = isec->rawsize - removed;
6216 else
6218 /* Adjust the output section size when called from
6219 objcopy. */
6220 isec->output_section->size -= removed;
6225 return TRUE;
6228 /* Copy private header information. */
6230 bfd_boolean
6231 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
6233 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6234 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6235 return TRUE;
6237 /* Copy over private BFD data if it has not already been copied.
6238 This must be done here, rather than in the copy_private_bfd_data
6239 entry point, because the latter is called after the section
6240 contents have been set, which means that the program headers have
6241 already been worked out. */
6242 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
6244 if (! copy_private_bfd_data (ibfd, obfd))
6245 return FALSE;
6248 return _bfd_elf_fixup_group_sections (ibfd, NULL);
6251 /* Copy private symbol information. If this symbol is in a section
6252 which we did not map into a BFD section, try to map the section
6253 index correctly. We use special macro definitions for the mapped
6254 section indices; these definitions are interpreted by the
6255 swap_out_syms function. */
6257 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6258 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6259 #define MAP_STRTAB (SHN_HIOS + 3)
6260 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6261 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6263 bfd_boolean
6264 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
6265 asymbol *isymarg,
6266 bfd *obfd,
6267 asymbol *osymarg)
6269 elf_symbol_type *isym, *osym;
6271 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6272 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6273 return TRUE;
6275 isym = elf_symbol_from (ibfd, isymarg);
6276 osym = elf_symbol_from (obfd, osymarg);
6278 if (isym != NULL
6279 && isym->internal_elf_sym.st_shndx != 0
6280 && osym != NULL
6281 && bfd_is_abs_section (isym->symbol.section))
6283 unsigned int shndx;
6285 shndx = isym->internal_elf_sym.st_shndx;
6286 if (shndx == elf_onesymtab (ibfd))
6287 shndx = MAP_ONESYMTAB;
6288 else if (shndx == elf_dynsymtab (ibfd))
6289 shndx = MAP_DYNSYMTAB;
6290 else if (shndx == elf_tdata (ibfd)->strtab_section)
6291 shndx = MAP_STRTAB;
6292 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
6293 shndx = MAP_SHSTRTAB;
6294 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
6295 shndx = MAP_SYM_SHNDX;
6296 osym->internal_elf_sym.st_shndx = shndx;
6299 return TRUE;
6302 /* Swap out the symbols. */
6304 static bfd_boolean
6305 swap_out_syms (bfd *abfd,
6306 struct bfd_strtab_hash **sttp,
6307 int relocatable_p)
6309 const struct elf_backend_data *bed;
6310 int symcount;
6311 asymbol **syms;
6312 struct bfd_strtab_hash *stt;
6313 Elf_Internal_Shdr *symtab_hdr;
6314 Elf_Internal_Shdr *symtab_shndx_hdr;
6315 Elf_Internal_Shdr *symstrtab_hdr;
6316 bfd_byte *outbound_syms;
6317 bfd_byte *outbound_shndx;
6318 int idx;
6319 bfd_size_type amt;
6320 bfd_boolean name_local_sections;
6322 if (!elf_map_symbols (abfd))
6323 return FALSE;
6325 /* Dump out the symtabs. */
6326 stt = _bfd_elf_stringtab_init ();
6327 if (stt == NULL)
6328 return FALSE;
6330 bed = get_elf_backend_data (abfd);
6331 symcount = bfd_get_symcount (abfd);
6332 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6333 symtab_hdr->sh_type = SHT_SYMTAB;
6334 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
6335 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
6336 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
6337 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
6339 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
6340 symstrtab_hdr->sh_type = SHT_STRTAB;
6342 outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
6343 bed->s->sizeof_sym);
6344 if (outbound_syms == NULL)
6346 _bfd_stringtab_free (stt);
6347 return FALSE;
6349 symtab_hdr->contents = outbound_syms;
6351 outbound_shndx = NULL;
6352 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
6353 if (symtab_shndx_hdr->sh_name != 0)
6355 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
6356 outbound_shndx = (bfd_byte *)
6357 bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
6358 if (outbound_shndx == NULL)
6360 _bfd_stringtab_free (stt);
6361 return FALSE;
6364 symtab_shndx_hdr->contents = outbound_shndx;
6365 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
6366 symtab_shndx_hdr->sh_size = amt;
6367 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
6368 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
6371 /* Now generate the data (for "contents"). */
6373 /* Fill in zeroth symbol and swap it out. */
6374 Elf_Internal_Sym sym;
6375 sym.st_name = 0;
6376 sym.st_value = 0;
6377 sym.st_size = 0;
6378 sym.st_info = 0;
6379 sym.st_other = 0;
6380 sym.st_shndx = SHN_UNDEF;
6381 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6382 outbound_syms += bed->s->sizeof_sym;
6383 if (outbound_shndx != NULL)
6384 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6387 name_local_sections
6388 = (bed->elf_backend_name_local_section_symbols
6389 && bed->elf_backend_name_local_section_symbols (abfd));
6391 syms = bfd_get_outsymbols (abfd);
6392 for (idx = 0; idx < symcount; idx++)
6394 Elf_Internal_Sym sym;
6395 bfd_vma value = syms[idx]->value;
6396 elf_symbol_type *type_ptr;
6397 flagword flags = syms[idx]->flags;
6398 int type;
6400 if (!name_local_sections
6401 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
6403 /* Local section symbols have no name. */
6404 sym.st_name = 0;
6406 else
6408 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
6409 syms[idx]->name,
6410 TRUE, FALSE);
6411 if (sym.st_name == (unsigned long) -1)
6413 _bfd_stringtab_free (stt);
6414 return FALSE;
6418 type_ptr = elf_symbol_from (abfd, syms[idx]);
6420 if ((flags & BSF_SECTION_SYM) == 0
6421 && bfd_is_com_section (syms[idx]->section))
6423 /* ELF common symbols put the alignment into the `value' field,
6424 and the size into the `size' field. This is backwards from
6425 how BFD handles it, so reverse it here. */
6426 sym.st_size = value;
6427 if (type_ptr == NULL
6428 || type_ptr->internal_elf_sym.st_value == 0)
6429 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
6430 else
6431 sym.st_value = type_ptr->internal_elf_sym.st_value;
6432 sym.st_shndx = _bfd_elf_section_from_bfd_section
6433 (abfd, syms[idx]->section);
6435 else
6437 asection *sec = syms[idx]->section;
6438 unsigned int shndx;
6440 if (sec->output_section)
6442 value += sec->output_offset;
6443 sec = sec->output_section;
6446 /* Don't add in the section vma for relocatable output. */
6447 if (! relocatable_p)
6448 value += sec->vma;
6449 sym.st_value = value;
6450 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
6452 if (bfd_is_abs_section (sec)
6453 && type_ptr != NULL
6454 && type_ptr->internal_elf_sym.st_shndx != 0)
6456 /* This symbol is in a real ELF section which we did
6457 not create as a BFD section. Undo the mapping done
6458 by copy_private_symbol_data. */
6459 shndx = type_ptr->internal_elf_sym.st_shndx;
6460 switch (shndx)
6462 case MAP_ONESYMTAB:
6463 shndx = elf_onesymtab (abfd);
6464 break;
6465 case MAP_DYNSYMTAB:
6466 shndx = elf_dynsymtab (abfd);
6467 break;
6468 case MAP_STRTAB:
6469 shndx = elf_tdata (abfd)->strtab_section;
6470 break;
6471 case MAP_SHSTRTAB:
6472 shndx = elf_tdata (abfd)->shstrtab_section;
6473 break;
6474 case MAP_SYM_SHNDX:
6475 shndx = elf_tdata (abfd)->symtab_shndx_section;
6476 break;
6477 default:
6478 break;
6481 else
6483 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
6485 if (shndx == SHN_BAD)
6487 asection *sec2;
6489 /* Writing this would be a hell of a lot easier if
6490 we had some decent documentation on bfd, and
6491 knew what to expect of the library, and what to
6492 demand of applications. For example, it
6493 appears that `objcopy' might not set the
6494 section of a symbol to be a section that is
6495 actually in the output file. */
6496 sec2 = bfd_get_section_by_name (abfd, sec->name);
6497 if (sec2 == NULL)
6499 _bfd_error_handler (_("\
6500 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6501 syms[idx]->name ? syms[idx]->name : "<Local sym>",
6502 sec->name);
6503 bfd_set_error (bfd_error_invalid_operation);
6504 _bfd_stringtab_free (stt);
6505 return FALSE;
6508 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
6509 BFD_ASSERT (shndx != SHN_BAD);
6513 sym.st_shndx = shndx;
6516 if ((flags & BSF_THREAD_LOCAL) != 0)
6517 type = STT_TLS;
6518 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
6519 type = STT_GNU_IFUNC;
6520 else if ((flags & BSF_FUNCTION) != 0)
6521 type = STT_FUNC;
6522 else if ((flags & BSF_OBJECT) != 0)
6523 type = STT_OBJECT;
6524 else if ((flags & BSF_RELC) != 0)
6525 type = STT_RELC;
6526 else if ((flags & BSF_SRELC) != 0)
6527 type = STT_SRELC;
6528 else
6529 type = STT_NOTYPE;
6531 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
6532 type = STT_TLS;
6534 /* Processor-specific types. */
6535 if (type_ptr != NULL
6536 && bed->elf_backend_get_symbol_type)
6537 type = ((*bed->elf_backend_get_symbol_type)
6538 (&type_ptr->internal_elf_sym, type));
6540 if (flags & BSF_SECTION_SYM)
6542 if (flags & BSF_GLOBAL)
6543 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6544 else
6545 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
6547 else if (bfd_is_com_section (syms[idx]->section))
6549 #ifdef USE_STT_COMMON
6550 if (type == STT_OBJECT)
6551 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON);
6552 else
6553 #endif
6554 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
6556 else if (bfd_is_und_section (syms[idx]->section))
6557 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
6558 ? STB_WEAK
6559 : STB_GLOBAL),
6560 type);
6561 else if (flags & BSF_FILE)
6562 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
6563 else
6565 int bind = STB_LOCAL;
6567 if (flags & BSF_LOCAL)
6568 bind = STB_LOCAL;
6569 else if (flags & BSF_GNU_UNIQUE)
6570 bind = STB_GNU_UNIQUE;
6571 else if (flags & BSF_WEAK)
6572 bind = STB_WEAK;
6573 else if (flags & BSF_GLOBAL)
6574 bind = STB_GLOBAL;
6576 sym.st_info = ELF_ST_INFO (bind, type);
6579 if (type_ptr != NULL)
6580 sym.st_other = type_ptr->internal_elf_sym.st_other;
6581 else
6582 sym.st_other = 0;
6584 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6585 outbound_syms += bed->s->sizeof_sym;
6586 if (outbound_shndx != NULL)
6587 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6590 *sttp = stt;
6591 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
6592 symstrtab_hdr->sh_type = SHT_STRTAB;
6594 symstrtab_hdr->sh_flags = 0;
6595 symstrtab_hdr->sh_addr = 0;
6596 symstrtab_hdr->sh_entsize = 0;
6597 symstrtab_hdr->sh_link = 0;
6598 symstrtab_hdr->sh_info = 0;
6599 symstrtab_hdr->sh_addralign = 1;
6601 return TRUE;
6604 /* Return the number of bytes required to hold the symtab vector.
6606 Note that we base it on the count plus 1, since we will null terminate
6607 the vector allocated based on this size. However, the ELF symbol table
6608 always has a dummy entry as symbol #0, so it ends up even. */
6610 long
6611 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
6613 long symcount;
6614 long symtab_size;
6615 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
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_dynamic_symtab_upper_bound (bfd *abfd)
6628 long symcount;
6629 long symtab_size;
6630 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
6632 if (elf_dynsymtab (abfd) == 0)
6634 bfd_set_error (bfd_error_invalid_operation);
6635 return -1;
6638 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6639 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6640 if (symcount > 0)
6641 symtab_size -= sizeof (asymbol *);
6643 return symtab_size;
6646 long
6647 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
6648 sec_ptr asect)
6650 return (asect->reloc_count + 1) * sizeof (arelent *);
6653 /* Canonicalize the relocs. */
6655 long
6656 _bfd_elf_canonicalize_reloc (bfd *abfd,
6657 sec_ptr section,
6658 arelent **relptr,
6659 asymbol **symbols)
6661 arelent *tblptr;
6662 unsigned int i;
6663 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6665 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
6666 return -1;
6668 tblptr = section->relocation;
6669 for (i = 0; i < section->reloc_count; i++)
6670 *relptr++ = tblptr++;
6672 *relptr = NULL;
6674 return section->reloc_count;
6677 long
6678 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
6680 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6681 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
6683 if (symcount >= 0)
6684 bfd_get_symcount (abfd) = symcount;
6685 return symcount;
6688 long
6689 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
6690 asymbol **allocation)
6692 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6693 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
6695 if (symcount >= 0)
6696 bfd_get_dynamic_symcount (abfd) = symcount;
6697 return symcount;
6700 /* Return the size required for the dynamic reloc entries. Any loadable
6701 section that was actually installed in the BFD, and has type SHT_REL
6702 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
6703 dynamic reloc section. */
6705 long
6706 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
6708 long ret;
6709 asection *s;
6711 if (elf_dynsymtab (abfd) == 0)
6713 bfd_set_error (bfd_error_invalid_operation);
6714 return -1;
6717 ret = sizeof (arelent *);
6718 for (s = abfd->sections; s != NULL; s = s->next)
6719 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6720 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6721 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6722 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
6723 * sizeof (arelent *));
6725 return ret;
6728 /* Canonicalize the dynamic relocation entries. Note that we return the
6729 dynamic relocations as a single block, although they are actually
6730 associated with particular sections; the interface, which was
6731 designed for SunOS style shared libraries, expects that there is only
6732 one set of dynamic relocs. Any loadable section that was actually
6733 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
6734 dynamic symbol table, is considered to be a dynamic reloc section. */
6736 long
6737 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
6738 arelent **storage,
6739 asymbol **syms)
6741 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
6742 asection *s;
6743 long ret;
6745 if (elf_dynsymtab (abfd) == 0)
6747 bfd_set_error (bfd_error_invalid_operation);
6748 return -1;
6751 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
6752 ret = 0;
6753 for (s = abfd->sections; s != NULL; s = s->next)
6755 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6756 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6757 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6759 arelent *p;
6760 long count, i;
6762 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
6763 return -1;
6764 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
6765 p = s->relocation;
6766 for (i = 0; i < count; i++)
6767 *storage++ = p++;
6768 ret += count;
6772 *storage = NULL;
6774 return ret;
6777 /* Read in the version information. */
6779 bfd_boolean
6780 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
6782 bfd_byte *contents = NULL;
6783 unsigned int freeidx = 0;
6785 if (elf_dynverref (abfd) != 0)
6787 Elf_Internal_Shdr *hdr;
6788 Elf_External_Verneed *everneed;
6789 Elf_Internal_Verneed *iverneed;
6790 unsigned int i;
6791 bfd_byte *contents_end;
6793 hdr = &elf_tdata (abfd)->dynverref_hdr;
6795 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
6796 bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
6797 if (elf_tdata (abfd)->verref == NULL)
6798 goto error_return;
6800 elf_tdata (abfd)->cverrefs = hdr->sh_info;
6802 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
6803 if (contents == NULL)
6805 error_return_verref:
6806 elf_tdata (abfd)->verref = NULL;
6807 elf_tdata (abfd)->cverrefs = 0;
6808 goto error_return;
6810 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6811 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6812 goto error_return_verref;
6814 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed))
6815 goto error_return_verref;
6817 BFD_ASSERT (sizeof (Elf_External_Verneed)
6818 == sizeof (Elf_External_Vernaux));
6819 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
6820 everneed = (Elf_External_Verneed *) contents;
6821 iverneed = elf_tdata (abfd)->verref;
6822 for (i = 0; i < hdr->sh_info; i++, iverneed++)
6824 Elf_External_Vernaux *evernaux;
6825 Elf_Internal_Vernaux *ivernaux;
6826 unsigned int j;
6828 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
6830 iverneed->vn_bfd = abfd;
6832 iverneed->vn_filename =
6833 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6834 iverneed->vn_file);
6835 if (iverneed->vn_filename == NULL)
6836 goto error_return_verref;
6838 if (iverneed->vn_cnt == 0)
6839 iverneed->vn_auxptr = NULL;
6840 else
6842 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
6843 bfd_alloc2 (abfd, iverneed->vn_cnt,
6844 sizeof (Elf_Internal_Vernaux));
6845 if (iverneed->vn_auxptr == NULL)
6846 goto error_return_verref;
6849 if (iverneed->vn_aux
6850 > (size_t) (contents_end - (bfd_byte *) everneed))
6851 goto error_return_verref;
6853 evernaux = ((Elf_External_Vernaux *)
6854 ((bfd_byte *) everneed + iverneed->vn_aux));
6855 ivernaux = iverneed->vn_auxptr;
6856 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
6858 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
6860 ivernaux->vna_nodename =
6861 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6862 ivernaux->vna_name);
6863 if (ivernaux->vna_nodename == NULL)
6864 goto error_return_verref;
6866 if (j + 1 < iverneed->vn_cnt)
6867 ivernaux->vna_nextptr = ivernaux + 1;
6868 else
6869 ivernaux->vna_nextptr = NULL;
6871 if (ivernaux->vna_next
6872 > (size_t) (contents_end - (bfd_byte *) evernaux))
6873 goto error_return_verref;
6875 evernaux = ((Elf_External_Vernaux *)
6876 ((bfd_byte *) evernaux + ivernaux->vna_next));
6878 if (ivernaux->vna_other > freeidx)
6879 freeidx = ivernaux->vna_other;
6882 if (i + 1 < hdr->sh_info)
6883 iverneed->vn_nextref = iverneed + 1;
6884 else
6885 iverneed->vn_nextref = NULL;
6887 if (iverneed->vn_next
6888 > (size_t) (contents_end - (bfd_byte *) everneed))
6889 goto error_return_verref;
6891 everneed = ((Elf_External_Verneed *)
6892 ((bfd_byte *) everneed + iverneed->vn_next));
6895 free (contents);
6896 contents = NULL;
6899 if (elf_dynverdef (abfd) != 0)
6901 Elf_Internal_Shdr *hdr;
6902 Elf_External_Verdef *everdef;
6903 Elf_Internal_Verdef *iverdef;
6904 Elf_Internal_Verdef *iverdefarr;
6905 Elf_Internal_Verdef iverdefmem;
6906 unsigned int i;
6907 unsigned int maxidx;
6908 bfd_byte *contents_end_def, *contents_end_aux;
6910 hdr = &elf_tdata (abfd)->dynverdef_hdr;
6912 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
6913 if (contents == NULL)
6914 goto error_return;
6915 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6916 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6917 goto error_return;
6919 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef))
6920 goto error_return;
6922 BFD_ASSERT (sizeof (Elf_External_Verdef)
6923 >= sizeof (Elf_External_Verdaux));
6924 contents_end_def = contents + hdr->sh_size
6925 - sizeof (Elf_External_Verdef);
6926 contents_end_aux = contents + hdr->sh_size
6927 - sizeof (Elf_External_Verdaux);
6929 /* We know the number of entries in the section but not the maximum
6930 index. Therefore we have to run through all entries and find
6931 the maximum. */
6932 everdef = (Elf_External_Verdef *) contents;
6933 maxidx = 0;
6934 for (i = 0; i < hdr->sh_info; ++i)
6936 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6938 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
6939 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
6941 if (iverdefmem.vd_next
6942 > (size_t) (contents_end_def - (bfd_byte *) everdef))
6943 goto error_return;
6945 everdef = ((Elf_External_Verdef *)
6946 ((bfd_byte *) everdef + iverdefmem.vd_next));
6949 if (default_imported_symver)
6951 if (freeidx > maxidx)
6952 maxidx = ++freeidx;
6953 else
6954 freeidx = ++maxidx;
6956 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
6957 bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
6958 if (elf_tdata (abfd)->verdef == NULL)
6959 goto error_return;
6961 elf_tdata (abfd)->cverdefs = maxidx;
6963 everdef = (Elf_External_Verdef *) contents;
6964 iverdefarr = elf_tdata (abfd)->verdef;
6965 for (i = 0; i < hdr->sh_info; i++)
6967 Elf_External_Verdaux *everdaux;
6968 Elf_Internal_Verdaux *iverdaux;
6969 unsigned int j;
6971 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6973 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
6975 error_return_verdef:
6976 elf_tdata (abfd)->verdef = NULL;
6977 elf_tdata (abfd)->cverdefs = 0;
6978 goto error_return;
6981 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
6982 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
6984 iverdef->vd_bfd = abfd;
6986 if (iverdef->vd_cnt == 0)
6987 iverdef->vd_auxptr = NULL;
6988 else
6990 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
6991 bfd_alloc2 (abfd, iverdef->vd_cnt,
6992 sizeof (Elf_Internal_Verdaux));
6993 if (iverdef->vd_auxptr == NULL)
6994 goto error_return_verdef;
6997 if (iverdef->vd_aux
6998 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
6999 goto error_return_verdef;
7001 everdaux = ((Elf_External_Verdaux *)
7002 ((bfd_byte *) everdef + iverdef->vd_aux));
7003 iverdaux = iverdef->vd_auxptr;
7004 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
7006 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
7008 iverdaux->vda_nodename =
7009 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7010 iverdaux->vda_name);
7011 if (iverdaux->vda_nodename == NULL)
7012 goto error_return_verdef;
7014 if (j + 1 < iverdef->vd_cnt)
7015 iverdaux->vda_nextptr = iverdaux + 1;
7016 else
7017 iverdaux->vda_nextptr = NULL;
7019 if (iverdaux->vda_next
7020 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
7021 goto error_return_verdef;
7023 everdaux = ((Elf_External_Verdaux *)
7024 ((bfd_byte *) everdaux + iverdaux->vda_next));
7027 if (iverdef->vd_cnt)
7028 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
7030 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
7031 iverdef->vd_nextdef = iverdef + 1;
7032 else
7033 iverdef->vd_nextdef = NULL;
7035 everdef = ((Elf_External_Verdef *)
7036 ((bfd_byte *) everdef + iverdef->vd_next));
7039 free (contents);
7040 contents = NULL;
7042 else if (default_imported_symver)
7044 if (freeidx < 3)
7045 freeidx = 3;
7046 else
7047 freeidx++;
7049 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
7050 bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
7051 if (elf_tdata (abfd)->verdef == NULL)
7052 goto error_return;
7054 elf_tdata (abfd)->cverdefs = freeidx;
7057 /* Create a default version based on the soname. */
7058 if (default_imported_symver)
7060 Elf_Internal_Verdef *iverdef;
7061 Elf_Internal_Verdaux *iverdaux;
7063 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];;
7065 iverdef->vd_version = VER_DEF_CURRENT;
7066 iverdef->vd_flags = 0;
7067 iverdef->vd_ndx = freeidx;
7068 iverdef->vd_cnt = 1;
7070 iverdef->vd_bfd = abfd;
7072 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
7073 if (iverdef->vd_nodename == NULL)
7074 goto error_return_verdef;
7075 iverdef->vd_nextdef = NULL;
7076 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
7077 bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux));
7078 if (iverdef->vd_auxptr == NULL)
7079 goto error_return_verdef;
7081 iverdaux = iverdef->vd_auxptr;
7082 iverdaux->vda_nodename = iverdef->vd_nodename;
7083 iverdaux->vda_nextptr = NULL;
7086 return TRUE;
7088 error_return:
7089 if (contents != NULL)
7090 free (contents);
7091 return FALSE;
7094 asymbol *
7095 _bfd_elf_make_empty_symbol (bfd *abfd)
7097 elf_symbol_type *newsym;
7098 bfd_size_type amt = sizeof (elf_symbol_type);
7100 newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt);
7101 if (!newsym)
7102 return NULL;
7103 else
7105 newsym->symbol.the_bfd = abfd;
7106 return &newsym->symbol;
7110 void
7111 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
7112 asymbol *symbol,
7113 symbol_info *ret)
7115 bfd_symbol_info (symbol, ret);
7118 /* Return whether a symbol name implies a local symbol. Most targets
7119 use this function for the is_local_label_name entry point, but some
7120 override it. */
7122 bfd_boolean
7123 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
7124 const char *name)
7126 /* Normal local symbols start with ``.L''. */
7127 if (name[0] == '.' && name[1] == 'L')
7128 return TRUE;
7130 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7131 DWARF debugging symbols starting with ``..''. */
7132 if (name[0] == '.' && name[1] == '.')
7133 return TRUE;
7135 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7136 emitting DWARF debugging output. I suspect this is actually a
7137 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7138 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7139 underscore to be emitted on some ELF targets). For ease of use,
7140 we treat such symbols as local. */
7141 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
7142 return TRUE;
7144 return FALSE;
7147 alent *
7148 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
7149 asymbol *symbol ATTRIBUTE_UNUSED)
7151 abort ();
7152 return NULL;
7155 bfd_boolean
7156 _bfd_elf_set_arch_mach (bfd *abfd,
7157 enum bfd_architecture arch,
7158 unsigned long machine)
7160 /* If this isn't the right architecture for this backend, and this
7161 isn't the generic backend, fail. */
7162 if (arch != get_elf_backend_data (abfd)->arch
7163 && arch != bfd_arch_unknown
7164 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
7165 return FALSE;
7167 return bfd_default_set_arch_mach (abfd, arch, machine);
7170 /* Find the function to a particular section and offset,
7171 for error reporting. */
7173 static bfd_boolean
7174 elf_find_function (bfd *abfd,
7175 asection *section,
7176 asymbol **symbols,
7177 bfd_vma offset,
7178 const char **filename_ptr,
7179 const char **functionname_ptr)
7181 const char *filename;
7182 asymbol *func, *file;
7183 bfd_vma low_func;
7184 asymbol **p;
7185 /* ??? Given multiple file symbols, it is impossible to reliably
7186 choose the right file name for global symbols. File symbols are
7187 local symbols, and thus all file symbols must sort before any
7188 global symbols. The ELF spec may be interpreted to say that a
7189 file symbol must sort before other local symbols, but currently
7190 ld -r doesn't do this. So, for ld -r output, it is possible to
7191 make a better choice of file name for local symbols by ignoring
7192 file symbols appearing after a given local symbol. */
7193 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
7194 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7196 filename = NULL;
7197 func = NULL;
7198 file = NULL;
7199 low_func = 0;
7200 state = nothing_seen;
7202 for (p = symbols; *p != NULL; p++)
7204 elf_symbol_type *q;
7205 unsigned int type;
7207 q = (elf_symbol_type *) *p;
7209 type = ELF_ST_TYPE (q->internal_elf_sym.st_info);
7210 switch (type)
7212 case STT_FILE:
7213 file = &q->symbol;
7214 if (state == symbol_seen)
7215 state = file_after_symbol_seen;
7216 continue;
7217 default:
7218 if (!bed->is_function_type (type))
7219 break;
7220 case STT_NOTYPE:
7221 if (bfd_get_section (&q->symbol) == section
7222 && q->symbol.value >= low_func
7223 && q->symbol.value <= offset)
7225 func = (asymbol *) q;
7226 low_func = q->symbol.value;
7227 filename = NULL;
7228 if (file != NULL
7229 && (ELF_ST_BIND (q->internal_elf_sym.st_info) == STB_LOCAL
7230 || state != file_after_symbol_seen))
7231 filename = bfd_asymbol_name (file);
7233 break;
7235 if (state == nothing_seen)
7236 state = symbol_seen;
7239 if (func == NULL)
7240 return FALSE;
7242 if (filename_ptr)
7243 *filename_ptr = filename;
7244 if (functionname_ptr)
7245 *functionname_ptr = bfd_asymbol_name (func);
7247 return TRUE;
7250 /* Find the nearest line to a particular section and offset,
7251 for error reporting. */
7253 bfd_boolean
7254 _bfd_elf_find_nearest_line (bfd *abfd,
7255 asection *section,
7256 asymbol **symbols,
7257 bfd_vma offset,
7258 const char **filename_ptr,
7259 const char **functionname_ptr,
7260 unsigned int *line_ptr)
7262 bfd_boolean found;
7264 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
7265 filename_ptr, functionname_ptr,
7266 line_ptr))
7268 if (!*functionname_ptr)
7269 elf_find_function (abfd, section, symbols, offset,
7270 *filename_ptr ? NULL : filename_ptr,
7271 functionname_ptr);
7273 return TRUE;
7276 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
7277 filename_ptr, functionname_ptr,
7278 line_ptr, 0,
7279 &elf_tdata (abfd)->dwarf2_find_line_info))
7281 if (!*functionname_ptr)
7282 elf_find_function (abfd, section, symbols, offset,
7283 *filename_ptr ? NULL : filename_ptr,
7284 functionname_ptr);
7286 return TRUE;
7289 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
7290 &found, filename_ptr,
7291 functionname_ptr, line_ptr,
7292 &elf_tdata (abfd)->line_info))
7293 return FALSE;
7294 if (found && (*functionname_ptr || *line_ptr))
7295 return TRUE;
7297 if (symbols == NULL)
7298 return FALSE;
7300 if (! elf_find_function (abfd, section, symbols, offset,
7301 filename_ptr, functionname_ptr))
7302 return FALSE;
7304 *line_ptr = 0;
7305 return TRUE;
7308 /* Find the line for a symbol. */
7310 bfd_boolean
7311 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
7312 const char **filename_ptr, unsigned int *line_ptr)
7314 return _bfd_dwarf2_find_line (abfd, symbols, symbol,
7315 filename_ptr, line_ptr, 0,
7316 &elf_tdata (abfd)->dwarf2_find_line_info);
7319 /* After a call to bfd_find_nearest_line, successive calls to
7320 bfd_find_inliner_info can be used to get source information about
7321 each level of function inlining that terminated at the address
7322 passed to bfd_find_nearest_line. Currently this is only supported
7323 for DWARF2 with appropriate DWARF3 extensions. */
7325 bfd_boolean
7326 _bfd_elf_find_inliner_info (bfd *abfd,
7327 const char **filename_ptr,
7328 const char **functionname_ptr,
7329 unsigned int *line_ptr)
7331 bfd_boolean found;
7332 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
7333 functionname_ptr, line_ptr,
7334 & elf_tdata (abfd)->dwarf2_find_line_info);
7335 return found;
7339 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
7341 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7342 int ret = bed->s->sizeof_ehdr;
7344 if (!info->relocatable)
7346 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
7348 if (phdr_size == (bfd_size_type) -1)
7350 struct elf_segment_map *m;
7352 phdr_size = 0;
7353 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
7354 phdr_size += bed->s->sizeof_phdr;
7356 if (phdr_size == 0)
7357 phdr_size = get_program_header_size (abfd, info);
7360 elf_tdata (abfd)->program_header_size = phdr_size;
7361 ret += phdr_size;
7364 return ret;
7367 bfd_boolean
7368 _bfd_elf_set_section_contents (bfd *abfd,
7369 sec_ptr section,
7370 const void *location,
7371 file_ptr offset,
7372 bfd_size_type count)
7374 Elf_Internal_Shdr *hdr;
7375 bfd_signed_vma pos;
7377 if (! abfd->output_has_begun
7378 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7379 return FALSE;
7381 hdr = &elf_section_data (section)->this_hdr;
7382 pos = hdr->sh_offset + offset;
7383 if (bfd_seek (abfd, pos, SEEK_SET) != 0
7384 || bfd_bwrite (location, count, abfd) != count)
7385 return FALSE;
7387 return TRUE;
7390 void
7391 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
7392 arelent *cache_ptr ATTRIBUTE_UNUSED,
7393 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
7395 abort ();
7398 /* Try to convert a non-ELF reloc into an ELF one. */
7400 bfd_boolean
7401 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
7403 /* Check whether we really have an ELF howto. */
7405 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
7407 bfd_reloc_code_real_type code;
7408 reloc_howto_type *howto;
7410 /* Alien reloc: Try to determine its type to replace it with an
7411 equivalent ELF reloc. */
7413 if (areloc->howto->pc_relative)
7415 switch (areloc->howto->bitsize)
7417 case 8:
7418 code = BFD_RELOC_8_PCREL;
7419 break;
7420 case 12:
7421 code = BFD_RELOC_12_PCREL;
7422 break;
7423 case 16:
7424 code = BFD_RELOC_16_PCREL;
7425 break;
7426 case 24:
7427 code = BFD_RELOC_24_PCREL;
7428 break;
7429 case 32:
7430 code = BFD_RELOC_32_PCREL;
7431 break;
7432 case 64:
7433 code = BFD_RELOC_64_PCREL;
7434 break;
7435 default:
7436 goto fail;
7439 howto = bfd_reloc_type_lookup (abfd, code);
7441 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
7443 if (howto->pcrel_offset)
7444 areloc->addend += areloc->address;
7445 else
7446 areloc->addend -= areloc->address; /* addend is unsigned!! */
7449 else
7451 switch (areloc->howto->bitsize)
7453 case 8:
7454 code = BFD_RELOC_8;
7455 break;
7456 case 14:
7457 code = BFD_RELOC_14;
7458 break;
7459 case 16:
7460 code = BFD_RELOC_16;
7461 break;
7462 case 26:
7463 code = BFD_RELOC_26;
7464 break;
7465 case 32:
7466 code = BFD_RELOC_32;
7467 break;
7468 case 64:
7469 code = BFD_RELOC_64;
7470 break;
7471 default:
7472 goto fail;
7475 howto = bfd_reloc_type_lookup (abfd, code);
7478 if (howto)
7479 areloc->howto = howto;
7480 else
7481 goto fail;
7484 return TRUE;
7486 fail:
7487 (*_bfd_error_handler)
7488 (_("%B: unsupported relocation type %s"),
7489 abfd, areloc->howto->name);
7490 bfd_set_error (bfd_error_bad_value);
7491 return FALSE;
7494 bfd_boolean
7495 _bfd_elf_close_and_cleanup (bfd *abfd)
7497 if (bfd_get_format (abfd) == bfd_object)
7499 if (elf_tdata (abfd) != NULL && elf_shstrtab (abfd) != NULL)
7500 _bfd_elf_strtab_free (elf_shstrtab (abfd));
7501 _bfd_dwarf2_cleanup_debug_info (abfd);
7504 return _bfd_generic_close_and_cleanup (abfd);
7507 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7508 in the relocation's offset. Thus we cannot allow any sort of sanity
7509 range-checking to interfere. There is nothing else to do in processing
7510 this reloc. */
7512 bfd_reloc_status_type
7513 _bfd_elf_rel_vtable_reloc_fn
7514 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
7515 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
7516 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
7517 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
7519 return bfd_reloc_ok;
7522 /* Elf core file support. Much of this only works on native
7523 toolchains, since we rely on knowing the
7524 machine-dependent procfs structure in order to pick
7525 out details about the corefile. */
7527 #ifdef HAVE_SYS_PROCFS_H
7528 /* Needed for new procfs interface on sparc-solaris. */
7529 # define _STRUCTURED_PROC 1
7530 # include <sys/procfs.h>
7531 #endif
7533 /* Return a PID that identifies a "thread" for threaded cores, or the
7534 PID of the main process for non-threaded cores. */
7536 static int
7537 elfcore_make_pid (bfd *abfd)
7539 int pid;
7541 pid = elf_tdata (abfd)->core_lwpid;
7542 if (pid == 0)
7543 pid = elf_tdata (abfd)->core_pid;
7545 return pid;
7548 /* If there isn't a section called NAME, make one, using
7549 data from SECT. Note, this function will generate a
7550 reference to NAME, so you shouldn't deallocate or
7551 overwrite it. */
7553 static bfd_boolean
7554 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
7556 asection *sect2;
7558 if (bfd_get_section_by_name (abfd, name) != NULL)
7559 return TRUE;
7561 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
7562 if (sect2 == NULL)
7563 return FALSE;
7565 sect2->size = sect->size;
7566 sect2->filepos = sect->filepos;
7567 sect2->alignment_power = sect->alignment_power;
7568 return TRUE;
7571 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
7572 actually creates up to two pseudosections:
7573 - For the single-threaded case, a section named NAME, unless
7574 such a section already exists.
7575 - For the multi-threaded case, a section named "NAME/PID", where
7576 PID is elfcore_make_pid (abfd).
7577 Both pseudosections have identical contents. */
7578 bfd_boolean
7579 _bfd_elfcore_make_pseudosection (bfd *abfd,
7580 char *name,
7581 size_t size,
7582 ufile_ptr filepos)
7584 char buf[100];
7585 char *threaded_name;
7586 size_t len;
7587 asection *sect;
7589 /* Build the section name. */
7591 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
7592 len = strlen (buf) + 1;
7593 threaded_name = (char *) bfd_alloc (abfd, len);
7594 if (threaded_name == NULL)
7595 return FALSE;
7596 memcpy (threaded_name, buf, len);
7598 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
7599 SEC_HAS_CONTENTS);
7600 if (sect == NULL)
7601 return FALSE;
7602 sect->size = size;
7603 sect->filepos = filepos;
7604 sect->alignment_power = 2;
7606 return elfcore_maybe_make_sect (abfd, name, sect);
7609 /* prstatus_t exists on:
7610 solaris 2.5+
7611 linux 2.[01] + glibc
7612 unixware 4.2
7615 #if defined (HAVE_PRSTATUS_T)
7617 static bfd_boolean
7618 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
7620 size_t size;
7621 int offset;
7623 if (note->descsz == sizeof (prstatus_t))
7625 prstatus_t prstat;
7627 size = sizeof (prstat.pr_reg);
7628 offset = offsetof (prstatus_t, pr_reg);
7629 memcpy (&prstat, note->descdata, sizeof (prstat));
7631 /* Do not overwrite the core signal if it
7632 has already been set by another thread. */
7633 if (elf_tdata (abfd)->core_signal == 0)
7634 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7635 if (elf_tdata (abfd)->core_pid == 0)
7636 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7638 /* pr_who exists on:
7639 solaris 2.5+
7640 unixware 4.2
7641 pr_who doesn't exist on:
7642 linux 2.[01]
7644 #if defined (HAVE_PRSTATUS_T_PR_WHO)
7645 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7646 #else
7647 elf_tdata (abfd)->core_lwpid = prstat.pr_pid;
7648 #endif
7650 #if defined (HAVE_PRSTATUS32_T)
7651 else if (note->descsz == sizeof (prstatus32_t))
7653 /* 64-bit host, 32-bit corefile */
7654 prstatus32_t prstat;
7656 size = sizeof (prstat.pr_reg);
7657 offset = offsetof (prstatus32_t, pr_reg);
7658 memcpy (&prstat, note->descdata, sizeof (prstat));
7660 /* Do not overwrite the core signal if it
7661 has already been set by another thread. */
7662 if (elf_tdata (abfd)->core_signal == 0)
7663 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7664 if (elf_tdata (abfd)->core_pid == 0)
7665 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7667 /* pr_who exists on:
7668 solaris 2.5+
7669 unixware 4.2
7670 pr_who doesn't exist on:
7671 linux 2.[01]
7673 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
7674 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7675 #else
7676 elf_tdata (abfd)->core_lwpid = prstat.pr_pid;
7677 #endif
7679 #endif /* HAVE_PRSTATUS32_T */
7680 else
7682 /* Fail - we don't know how to handle any other
7683 note size (ie. data object type). */
7684 return TRUE;
7687 /* Make a ".reg/999" section and a ".reg" section. */
7688 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
7689 size, note->descpos + offset);
7691 #endif /* defined (HAVE_PRSTATUS_T) */
7693 /* Create a pseudosection containing the exact contents of NOTE. */
7694 static bfd_boolean
7695 elfcore_make_note_pseudosection (bfd *abfd,
7696 char *name,
7697 Elf_Internal_Note *note)
7699 return _bfd_elfcore_make_pseudosection (abfd, name,
7700 note->descsz, note->descpos);
7703 /* There isn't a consistent prfpregset_t across platforms,
7704 but it doesn't matter, because we don't have to pick this
7705 data structure apart. */
7707 static bfd_boolean
7708 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
7710 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7713 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
7714 type of NT_PRXFPREG. Just include the whole note's contents
7715 literally. */
7717 static bfd_boolean
7718 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
7720 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
7723 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
7724 with a note type of NT_X86_XSTATE. Just include the whole note's
7725 contents literally. */
7727 static bfd_boolean
7728 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
7730 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
7733 static bfd_boolean
7734 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
7736 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
7739 static bfd_boolean
7740 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
7742 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
7745 static bfd_boolean
7746 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
7748 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
7751 static bfd_boolean
7752 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
7754 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
7757 static bfd_boolean
7758 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
7760 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
7763 static bfd_boolean
7764 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
7766 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
7769 static bfd_boolean
7770 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
7772 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
7775 static bfd_boolean
7776 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
7778 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
7781 #if defined (HAVE_PRPSINFO_T)
7782 typedef prpsinfo_t elfcore_psinfo_t;
7783 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
7784 typedef prpsinfo32_t elfcore_psinfo32_t;
7785 #endif
7786 #endif
7788 #if defined (HAVE_PSINFO_T)
7789 typedef psinfo_t elfcore_psinfo_t;
7790 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
7791 typedef psinfo32_t elfcore_psinfo32_t;
7792 #endif
7793 #endif
7795 /* return a malloc'ed copy of a string at START which is at
7796 most MAX bytes long, possibly without a terminating '\0'.
7797 the copy will always have a terminating '\0'. */
7799 char *
7800 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
7802 char *dups;
7803 char *end = (char *) memchr (start, '\0', max);
7804 size_t len;
7806 if (end == NULL)
7807 len = max;
7808 else
7809 len = end - start;
7811 dups = (char *) bfd_alloc (abfd, len + 1);
7812 if (dups == NULL)
7813 return NULL;
7815 memcpy (dups, start, len);
7816 dups[len] = '\0';
7818 return dups;
7821 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7822 static bfd_boolean
7823 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
7825 if (note->descsz == sizeof (elfcore_psinfo_t))
7827 elfcore_psinfo_t psinfo;
7829 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7831 elf_tdata (abfd)->core_program
7832 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7833 sizeof (psinfo.pr_fname));
7835 elf_tdata (abfd)->core_command
7836 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7837 sizeof (psinfo.pr_psargs));
7839 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
7840 else if (note->descsz == sizeof (elfcore_psinfo32_t))
7842 /* 64-bit host, 32-bit corefile */
7843 elfcore_psinfo32_t psinfo;
7845 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7847 elf_tdata (abfd)->core_program
7848 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7849 sizeof (psinfo.pr_fname));
7851 elf_tdata (abfd)->core_command
7852 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7853 sizeof (psinfo.pr_psargs));
7855 #endif
7857 else
7859 /* Fail - we don't know how to handle any other
7860 note size (ie. data object type). */
7861 return TRUE;
7864 /* Note that for some reason, a spurious space is tacked
7865 onto the end of the args in some (at least one anyway)
7866 implementations, so strip it off if it exists. */
7869 char *command = elf_tdata (abfd)->core_command;
7870 int n = strlen (command);
7872 if (0 < n && command[n - 1] == ' ')
7873 command[n - 1] = '\0';
7876 return TRUE;
7878 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
7880 #if defined (HAVE_PSTATUS_T)
7881 static bfd_boolean
7882 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
7884 if (note->descsz == sizeof (pstatus_t)
7885 #if defined (HAVE_PXSTATUS_T)
7886 || note->descsz == sizeof (pxstatus_t)
7887 #endif
7890 pstatus_t pstat;
7892 memcpy (&pstat, note->descdata, sizeof (pstat));
7894 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7896 #if defined (HAVE_PSTATUS32_T)
7897 else if (note->descsz == sizeof (pstatus32_t))
7899 /* 64-bit host, 32-bit corefile */
7900 pstatus32_t pstat;
7902 memcpy (&pstat, note->descdata, sizeof (pstat));
7904 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7906 #endif
7907 /* Could grab some more details from the "representative"
7908 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
7909 NT_LWPSTATUS note, presumably. */
7911 return TRUE;
7913 #endif /* defined (HAVE_PSTATUS_T) */
7915 #if defined (HAVE_LWPSTATUS_T)
7916 static bfd_boolean
7917 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
7919 lwpstatus_t lwpstat;
7920 char buf[100];
7921 char *name;
7922 size_t len;
7923 asection *sect;
7925 if (note->descsz != sizeof (lwpstat)
7926 #if defined (HAVE_LWPXSTATUS_T)
7927 && note->descsz != sizeof (lwpxstatus_t)
7928 #endif
7930 return TRUE;
7932 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
7934 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
7935 /* Do not overwrite the core signal if it has already been set by
7936 another thread. */
7937 if (elf_tdata (abfd)->core_signal == 0)
7938 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
7940 /* Make a ".reg/999" section. */
7942 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
7943 len = strlen (buf) + 1;
7944 name = bfd_alloc (abfd, len);
7945 if (name == NULL)
7946 return FALSE;
7947 memcpy (name, buf, len);
7949 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7950 if (sect == NULL)
7951 return FALSE;
7953 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7954 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
7955 sect->filepos = note->descpos
7956 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
7957 #endif
7959 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7960 sect->size = sizeof (lwpstat.pr_reg);
7961 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
7962 #endif
7964 sect->alignment_power = 2;
7966 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
7967 return FALSE;
7969 /* Make a ".reg2/999" section */
7971 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
7972 len = strlen (buf) + 1;
7973 name = bfd_alloc (abfd, len);
7974 if (name == NULL)
7975 return FALSE;
7976 memcpy (name, buf, len);
7978 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7979 if (sect == NULL)
7980 return FALSE;
7982 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7983 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
7984 sect->filepos = note->descpos
7985 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
7986 #endif
7988 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
7989 sect->size = sizeof (lwpstat.pr_fpreg);
7990 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
7991 #endif
7993 sect->alignment_power = 2;
7995 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
7997 #endif /* defined (HAVE_LWPSTATUS_T) */
7999 static bfd_boolean
8000 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
8002 char buf[30];
8003 char *name;
8004 size_t len;
8005 asection *sect;
8006 int type;
8007 int is_active_thread;
8008 bfd_vma base_addr;
8010 if (note->descsz < 728)
8011 return TRUE;
8013 if (! CONST_STRNEQ (note->namedata, "win32"))
8014 return TRUE;
8016 type = bfd_get_32 (abfd, note->descdata);
8018 switch (type)
8020 case 1 /* NOTE_INFO_PROCESS */:
8021 /* FIXME: need to add ->core_command. */
8022 /* process_info.pid */
8023 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 8);
8024 /* process_info.signal */
8025 elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 12);
8026 break;
8028 case 2 /* NOTE_INFO_THREAD */:
8029 /* Make a ".reg/999" section. */
8030 /* thread_info.tid */
8031 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
8033 len = strlen (buf) + 1;
8034 name = (char *) bfd_alloc (abfd, len);
8035 if (name == NULL)
8036 return FALSE;
8038 memcpy (name, buf, len);
8040 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8041 if (sect == NULL)
8042 return FALSE;
8044 /* sizeof (thread_info.thread_context) */
8045 sect->size = 716;
8046 /* offsetof (thread_info.thread_context) */
8047 sect->filepos = note->descpos + 12;
8048 sect->alignment_power = 2;
8050 /* thread_info.is_active_thread */
8051 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
8053 if (is_active_thread)
8054 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
8055 return FALSE;
8056 break;
8058 case 3 /* NOTE_INFO_MODULE */:
8059 /* Make a ".module/xxxxxxxx" section. */
8060 /* module_info.base_address */
8061 base_addr = bfd_get_32 (abfd, note->descdata + 4);
8062 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
8064 len = strlen (buf) + 1;
8065 name = (char *) bfd_alloc (abfd, len);
8066 if (name == NULL)
8067 return FALSE;
8069 memcpy (name, buf, len);
8071 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8073 if (sect == NULL)
8074 return FALSE;
8076 sect->size = note->descsz;
8077 sect->filepos = note->descpos;
8078 sect->alignment_power = 2;
8079 break;
8081 default:
8082 return TRUE;
8085 return TRUE;
8088 static bfd_boolean
8089 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
8091 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8093 switch (note->type)
8095 default:
8096 return TRUE;
8098 case NT_PRSTATUS:
8099 if (bed->elf_backend_grok_prstatus)
8100 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
8101 return TRUE;
8102 #if defined (HAVE_PRSTATUS_T)
8103 return elfcore_grok_prstatus (abfd, note);
8104 #else
8105 return TRUE;
8106 #endif
8108 #if defined (HAVE_PSTATUS_T)
8109 case NT_PSTATUS:
8110 return elfcore_grok_pstatus (abfd, note);
8111 #endif
8113 #if defined (HAVE_LWPSTATUS_T)
8114 case NT_LWPSTATUS:
8115 return elfcore_grok_lwpstatus (abfd, note);
8116 #endif
8118 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
8119 return elfcore_grok_prfpreg (abfd, note);
8121 case NT_WIN32PSTATUS:
8122 return elfcore_grok_win32pstatus (abfd, note);
8124 case NT_PRXFPREG: /* Linux SSE extension */
8125 if (note->namesz == 6
8126 && strcmp (note->namedata, "LINUX") == 0)
8127 return elfcore_grok_prxfpreg (abfd, note);
8128 else
8129 return TRUE;
8131 case NT_X86_XSTATE: /* Linux XSAVE extension */
8132 if (note->namesz == 6
8133 && strcmp (note->namedata, "LINUX") == 0)
8134 return elfcore_grok_xstatereg (abfd, note);
8135 else
8136 return TRUE;
8138 case NT_PPC_VMX:
8139 if (note->namesz == 6
8140 && strcmp (note->namedata, "LINUX") == 0)
8141 return elfcore_grok_ppc_vmx (abfd, note);
8142 else
8143 return TRUE;
8145 case NT_PPC_VSX:
8146 if (note->namesz == 6
8147 && strcmp (note->namedata, "LINUX") == 0)
8148 return elfcore_grok_ppc_vsx (abfd, note);
8149 else
8150 return TRUE;
8152 case NT_S390_HIGH_GPRS:
8153 if (note->namesz == 6
8154 && strcmp (note->namedata, "LINUX") == 0)
8155 return elfcore_grok_s390_high_gprs (abfd, note);
8156 else
8157 return TRUE;
8159 case NT_S390_TIMER:
8160 if (note->namesz == 6
8161 && strcmp (note->namedata, "LINUX") == 0)
8162 return elfcore_grok_s390_timer (abfd, note);
8163 else
8164 return TRUE;
8166 case NT_S390_TODCMP:
8167 if (note->namesz == 6
8168 && strcmp (note->namedata, "LINUX") == 0)
8169 return elfcore_grok_s390_todcmp (abfd, note);
8170 else
8171 return TRUE;
8173 case NT_S390_TODPREG:
8174 if (note->namesz == 6
8175 && strcmp (note->namedata, "LINUX") == 0)
8176 return elfcore_grok_s390_todpreg (abfd, note);
8177 else
8178 return TRUE;
8180 case NT_S390_CTRS:
8181 if (note->namesz == 6
8182 && strcmp (note->namedata, "LINUX") == 0)
8183 return elfcore_grok_s390_ctrs (abfd, note);
8184 else
8185 return TRUE;
8187 case NT_S390_PREFIX:
8188 if (note->namesz == 6
8189 && strcmp (note->namedata, "LINUX") == 0)
8190 return elfcore_grok_s390_prefix (abfd, note);
8191 else
8192 return TRUE;
8194 case NT_PRPSINFO:
8195 case NT_PSINFO:
8196 if (bed->elf_backend_grok_psinfo)
8197 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
8198 return TRUE;
8199 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8200 return elfcore_grok_psinfo (abfd, note);
8201 #else
8202 return TRUE;
8203 #endif
8205 case NT_AUXV:
8207 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
8208 SEC_HAS_CONTENTS);
8210 if (sect == NULL)
8211 return FALSE;
8212 sect->size = note->descsz;
8213 sect->filepos = note->descpos;
8214 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8216 return TRUE;
8221 static bfd_boolean
8222 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
8224 elf_tdata (abfd)->build_id_size = note->descsz;
8225 elf_tdata (abfd)->build_id = (bfd_byte *) bfd_alloc (abfd, note->descsz);
8226 if (elf_tdata (abfd)->build_id == NULL)
8227 return FALSE;
8229 memcpy (elf_tdata (abfd)->build_id, note->descdata, note->descsz);
8231 return TRUE;
8234 static bfd_boolean
8235 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
8237 switch (note->type)
8239 default:
8240 return TRUE;
8242 case NT_GNU_BUILD_ID:
8243 return elfobj_grok_gnu_build_id (abfd, note);
8247 static bfd_boolean
8248 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
8250 char *cp;
8252 cp = strchr (note->namedata, '@');
8253 if (cp != NULL)
8255 *lwpidp = atoi(cp + 1);
8256 return TRUE;
8258 return FALSE;
8261 static bfd_boolean
8262 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8264 /* Signal number at offset 0x08. */
8265 elf_tdata (abfd)->core_signal
8266 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8268 /* Process ID at offset 0x50. */
8269 elf_tdata (abfd)->core_pid
8270 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
8272 /* Command name at 0x7c (max 32 bytes, including nul). */
8273 elf_tdata (abfd)->core_command
8274 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
8276 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
8277 note);
8280 static bfd_boolean
8281 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
8283 int lwp;
8285 if (elfcore_netbsd_get_lwpid (note, &lwp))
8286 elf_tdata (abfd)->core_lwpid = lwp;
8288 if (note->type == NT_NETBSDCORE_PROCINFO)
8290 /* NetBSD-specific core "procinfo". Note that we expect to
8291 find this note before any of the others, which is fine,
8292 since the kernel writes this note out first when it
8293 creates a core file. */
8295 return elfcore_grok_netbsd_procinfo (abfd, note);
8298 /* As of Jan 2002 there are no other machine-independent notes
8299 defined for NetBSD core files. If the note type is less
8300 than the start of the machine-dependent note types, we don't
8301 understand it. */
8303 if (note->type < NT_NETBSDCORE_FIRSTMACH)
8304 return TRUE;
8307 switch (bfd_get_arch (abfd))
8309 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8310 PT_GETFPREGS == mach+2. */
8312 case bfd_arch_alpha:
8313 case bfd_arch_sparc:
8314 switch (note->type)
8316 case NT_NETBSDCORE_FIRSTMACH+0:
8317 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8319 case NT_NETBSDCORE_FIRSTMACH+2:
8320 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8322 default:
8323 return TRUE;
8326 /* On all other arch's, PT_GETREGS == mach+1 and
8327 PT_GETFPREGS == mach+3. */
8329 default:
8330 switch (note->type)
8332 case NT_NETBSDCORE_FIRSTMACH+1:
8333 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8335 case NT_NETBSDCORE_FIRSTMACH+3:
8336 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8338 default:
8339 return TRUE;
8342 /* NOTREACHED */
8345 static bfd_boolean
8346 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8348 /* Signal number at offset 0x08. */
8349 elf_tdata (abfd)->core_signal
8350 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8352 /* Process ID at offset 0x20. */
8353 elf_tdata (abfd)->core_pid
8354 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
8356 /* Command name at 0x48 (max 32 bytes, including nul). */
8357 elf_tdata (abfd)->core_command
8358 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
8360 return TRUE;
8363 static bfd_boolean
8364 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
8366 if (note->type == NT_OPENBSD_PROCINFO)
8367 return elfcore_grok_openbsd_procinfo (abfd, note);
8369 if (note->type == NT_OPENBSD_REGS)
8370 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8372 if (note->type == NT_OPENBSD_FPREGS)
8373 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8375 if (note->type == NT_OPENBSD_XFPREGS)
8376 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
8378 if (note->type == NT_OPENBSD_AUXV)
8380 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
8381 SEC_HAS_CONTENTS);
8383 if (sect == NULL)
8384 return FALSE;
8385 sect->size = note->descsz;
8386 sect->filepos = note->descpos;
8387 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8389 return TRUE;
8392 if (note->type == NT_OPENBSD_WCOOKIE)
8394 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
8395 SEC_HAS_CONTENTS);
8397 if (sect == NULL)
8398 return FALSE;
8399 sect->size = note->descsz;
8400 sect->filepos = note->descpos;
8401 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8403 return TRUE;
8406 return TRUE;
8409 static bfd_boolean
8410 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
8412 void *ddata = note->descdata;
8413 char buf[100];
8414 char *name;
8415 asection *sect;
8416 short sig;
8417 unsigned flags;
8419 /* nto_procfs_status 'pid' field is at offset 0. */
8420 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
8422 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
8423 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
8425 /* nto_procfs_status 'flags' field is at offset 8. */
8426 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
8428 /* nto_procfs_status 'what' field is at offset 14. */
8429 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
8431 elf_tdata (abfd)->core_signal = sig;
8432 elf_tdata (abfd)->core_lwpid = *tid;
8435 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
8436 do not come from signals so we make sure we set the current
8437 thread just in case. */
8438 if (flags & 0x00000080)
8439 elf_tdata (abfd)->core_lwpid = *tid;
8441 /* Make a ".qnx_core_status/%d" section. */
8442 sprintf (buf, ".qnx_core_status/%ld", *tid);
8444 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
8445 if (name == NULL)
8446 return FALSE;
8447 strcpy (name, buf);
8449 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8450 if (sect == NULL)
8451 return FALSE;
8453 sect->size = note->descsz;
8454 sect->filepos = note->descpos;
8455 sect->alignment_power = 2;
8457 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
8460 static bfd_boolean
8461 elfcore_grok_nto_regs (bfd *abfd,
8462 Elf_Internal_Note *note,
8463 long tid,
8464 char *base)
8466 char buf[100];
8467 char *name;
8468 asection *sect;
8470 /* Make a "(base)/%d" section. */
8471 sprintf (buf, "%s/%ld", base, tid);
8473 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
8474 if (name == NULL)
8475 return FALSE;
8476 strcpy (name, buf);
8478 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8479 if (sect == NULL)
8480 return FALSE;
8482 sect->size = note->descsz;
8483 sect->filepos = note->descpos;
8484 sect->alignment_power = 2;
8486 /* This is the current thread. */
8487 if (elf_tdata (abfd)->core_lwpid == tid)
8488 return elfcore_maybe_make_sect (abfd, base, sect);
8490 return TRUE;
8493 #define BFD_QNT_CORE_INFO 7
8494 #define BFD_QNT_CORE_STATUS 8
8495 #define BFD_QNT_CORE_GREG 9
8496 #define BFD_QNT_CORE_FPREG 10
8498 static bfd_boolean
8499 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
8501 /* Every GREG section has a STATUS section before it. Store the
8502 tid from the previous call to pass down to the next gregs
8503 function. */
8504 static long tid = 1;
8506 switch (note->type)
8508 case BFD_QNT_CORE_INFO:
8509 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
8510 case BFD_QNT_CORE_STATUS:
8511 return elfcore_grok_nto_status (abfd, note, &tid);
8512 case BFD_QNT_CORE_GREG:
8513 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
8514 case BFD_QNT_CORE_FPREG:
8515 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
8516 default:
8517 return TRUE;
8521 static bfd_boolean
8522 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
8524 char *name;
8525 asection *sect;
8526 size_t len;
8528 /* Use note name as section name. */
8529 len = note->namesz;
8530 name = (char *) bfd_alloc (abfd, len);
8531 if (name == NULL)
8532 return FALSE;
8533 memcpy (name, note->namedata, len);
8534 name[len - 1] = '\0';
8536 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8537 if (sect == NULL)
8538 return FALSE;
8540 sect->size = note->descsz;
8541 sect->filepos = note->descpos;
8542 sect->alignment_power = 1;
8544 return TRUE;
8547 /* Function: elfcore_write_note
8549 Inputs:
8550 buffer to hold note, and current size of buffer
8551 name of note
8552 type of note
8553 data for note
8554 size of data for note
8556 Writes note to end of buffer. ELF64 notes are written exactly as
8557 for ELF32, despite the current (as of 2006) ELF gabi specifying
8558 that they ought to have 8-byte namesz and descsz field, and have
8559 8-byte alignment. Other writers, eg. Linux kernel, do the same.
8561 Return:
8562 Pointer to realloc'd buffer, *BUFSIZ updated. */
8564 char *
8565 elfcore_write_note (bfd *abfd,
8566 char *buf,
8567 int *bufsiz,
8568 const char *name,
8569 int type,
8570 const void *input,
8571 int size)
8573 Elf_External_Note *xnp;
8574 size_t namesz;
8575 size_t newspace;
8576 char *dest;
8578 namesz = 0;
8579 if (name != NULL)
8580 namesz = strlen (name) + 1;
8582 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
8584 buf = (char *) realloc (buf, *bufsiz + newspace);
8585 if (buf == NULL)
8586 return buf;
8587 dest = buf + *bufsiz;
8588 *bufsiz += newspace;
8589 xnp = (Elf_External_Note *) dest;
8590 H_PUT_32 (abfd, namesz, xnp->namesz);
8591 H_PUT_32 (abfd, size, xnp->descsz);
8592 H_PUT_32 (abfd, type, xnp->type);
8593 dest = xnp->name;
8594 if (name != NULL)
8596 memcpy (dest, name, namesz);
8597 dest += namesz;
8598 while (namesz & 3)
8600 *dest++ = '\0';
8601 ++namesz;
8604 memcpy (dest, input, size);
8605 dest += size;
8606 while (size & 3)
8608 *dest++ = '\0';
8609 ++size;
8611 return buf;
8614 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8615 char *
8616 elfcore_write_prpsinfo (bfd *abfd,
8617 char *buf,
8618 int *bufsiz,
8619 const char *fname,
8620 const char *psargs)
8622 const char *note_name = "CORE";
8623 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8625 if (bed->elf_backend_write_core_note != NULL)
8627 char *ret;
8628 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8629 NT_PRPSINFO, fname, psargs);
8630 if (ret != NULL)
8631 return ret;
8634 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8635 if (bed->s->elfclass == ELFCLASS32)
8637 #if defined (HAVE_PSINFO32_T)
8638 psinfo32_t data;
8639 int note_type = NT_PSINFO;
8640 #else
8641 prpsinfo32_t data;
8642 int note_type = NT_PRPSINFO;
8643 #endif
8645 memset (&data, 0, sizeof (data));
8646 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8647 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8648 return elfcore_write_note (abfd, buf, bufsiz,
8649 note_name, note_type, &data, sizeof (data));
8651 else
8652 #endif
8654 #if defined (HAVE_PSINFO_T)
8655 psinfo_t data;
8656 int note_type = NT_PSINFO;
8657 #else
8658 prpsinfo_t data;
8659 int note_type = NT_PRPSINFO;
8660 #endif
8662 memset (&data, 0, sizeof (data));
8663 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8664 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8665 return elfcore_write_note (abfd, buf, bufsiz,
8666 note_name, note_type, &data, sizeof (data));
8669 #endif /* PSINFO_T or PRPSINFO_T */
8671 #if defined (HAVE_PRSTATUS_T)
8672 char *
8673 elfcore_write_prstatus (bfd *abfd,
8674 char *buf,
8675 int *bufsiz,
8676 long pid,
8677 int cursig,
8678 const void *gregs)
8680 const char *note_name = "CORE";
8681 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8683 if (bed->elf_backend_write_core_note != NULL)
8685 char *ret;
8686 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8687 NT_PRSTATUS,
8688 pid, cursig, gregs);
8689 if (ret != NULL)
8690 return ret;
8693 #if defined (HAVE_PRSTATUS32_T)
8694 if (bed->s->elfclass == ELFCLASS32)
8696 prstatus32_t prstat;
8698 memset (&prstat, 0, sizeof (prstat));
8699 prstat.pr_pid = pid;
8700 prstat.pr_cursig = cursig;
8701 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8702 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8703 NT_PRSTATUS, &prstat, sizeof (prstat));
8705 else
8706 #endif
8708 prstatus_t prstat;
8710 memset (&prstat, 0, sizeof (prstat));
8711 prstat.pr_pid = pid;
8712 prstat.pr_cursig = cursig;
8713 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8714 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8715 NT_PRSTATUS, &prstat, sizeof (prstat));
8718 #endif /* HAVE_PRSTATUS_T */
8720 #if defined (HAVE_LWPSTATUS_T)
8721 char *
8722 elfcore_write_lwpstatus (bfd *abfd,
8723 char *buf,
8724 int *bufsiz,
8725 long pid,
8726 int cursig,
8727 const void *gregs)
8729 lwpstatus_t lwpstat;
8730 const char *note_name = "CORE";
8732 memset (&lwpstat, 0, sizeof (lwpstat));
8733 lwpstat.pr_lwpid = pid >> 16;
8734 lwpstat.pr_cursig = cursig;
8735 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8736 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
8737 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8738 #if !defined(gregs)
8739 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
8740 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
8741 #else
8742 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
8743 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
8744 #endif
8745 #endif
8746 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8747 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
8749 #endif /* HAVE_LWPSTATUS_T */
8751 #if defined (HAVE_PSTATUS_T)
8752 char *
8753 elfcore_write_pstatus (bfd *abfd,
8754 char *buf,
8755 int *bufsiz,
8756 long pid,
8757 int cursig ATTRIBUTE_UNUSED,
8758 const void *gregs ATTRIBUTE_UNUSED)
8760 const char *note_name = "CORE";
8761 #if defined (HAVE_PSTATUS32_T)
8762 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8764 if (bed->s->elfclass == ELFCLASS32)
8766 pstatus32_t pstat;
8768 memset (&pstat, 0, sizeof (pstat));
8769 pstat.pr_pid = pid & 0xffff;
8770 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8771 NT_PSTATUS, &pstat, sizeof (pstat));
8772 return buf;
8774 else
8775 #endif
8777 pstatus_t pstat;
8779 memset (&pstat, 0, sizeof (pstat));
8780 pstat.pr_pid = pid & 0xffff;
8781 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8782 NT_PSTATUS, &pstat, sizeof (pstat));
8783 return buf;
8786 #endif /* HAVE_PSTATUS_T */
8788 char *
8789 elfcore_write_prfpreg (bfd *abfd,
8790 char *buf,
8791 int *bufsiz,
8792 const void *fpregs,
8793 int size)
8795 const char *note_name = "CORE";
8796 return elfcore_write_note (abfd, buf, bufsiz,
8797 note_name, NT_FPREGSET, fpregs, size);
8800 char *
8801 elfcore_write_prxfpreg (bfd *abfd,
8802 char *buf,
8803 int *bufsiz,
8804 const void *xfpregs,
8805 int size)
8807 char *note_name = "LINUX";
8808 return elfcore_write_note (abfd, buf, bufsiz,
8809 note_name, NT_PRXFPREG, xfpregs, size);
8812 char *
8813 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
8814 const void *xfpregs, int size)
8816 char *note_name = "LINUX";
8817 return elfcore_write_note (abfd, buf, bufsiz,
8818 note_name, NT_X86_XSTATE, xfpregs, size);
8821 char *
8822 elfcore_write_ppc_vmx (bfd *abfd,
8823 char *buf,
8824 int *bufsiz,
8825 const void *ppc_vmx,
8826 int size)
8828 char *note_name = "LINUX";
8829 return elfcore_write_note (abfd, buf, bufsiz,
8830 note_name, NT_PPC_VMX, ppc_vmx, size);
8833 char *
8834 elfcore_write_ppc_vsx (bfd *abfd,
8835 char *buf,
8836 int *bufsiz,
8837 const void *ppc_vsx,
8838 int size)
8840 char *note_name = "LINUX";
8841 return elfcore_write_note (abfd, buf, bufsiz,
8842 note_name, NT_PPC_VSX, ppc_vsx, size);
8845 static char *
8846 elfcore_write_s390_high_gprs (bfd *abfd,
8847 char *buf,
8848 int *bufsiz,
8849 const void *s390_high_gprs,
8850 int size)
8852 char *note_name = "LINUX";
8853 return elfcore_write_note (abfd, buf, bufsiz,
8854 note_name, NT_S390_HIGH_GPRS,
8855 s390_high_gprs, size);
8858 char *
8859 elfcore_write_s390_timer (bfd *abfd,
8860 char *buf,
8861 int *bufsiz,
8862 const void *s390_timer,
8863 int size)
8865 char *note_name = "LINUX";
8866 return elfcore_write_note (abfd, buf, bufsiz,
8867 note_name, NT_S390_TIMER, s390_timer, size);
8870 char *
8871 elfcore_write_s390_todcmp (bfd *abfd,
8872 char *buf,
8873 int *bufsiz,
8874 const void *s390_todcmp,
8875 int size)
8877 char *note_name = "LINUX";
8878 return elfcore_write_note (abfd, buf, bufsiz,
8879 note_name, NT_S390_TODCMP, s390_todcmp, size);
8882 char *
8883 elfcore_write_s390_todpreg (bfd *abfd,
8884 char *buf,
8885 int *bufsiz,
8886 const void *s390_todpreg,
8887 int size)
8889 char *note_name = "LINUX";
8890 return elfcore_write_note (abfd, buf, bufsiz,
8891 note_name, NT_S390_TODPREG, s390_todpreg, size);
8894 char *
8895 elfcore_write_s390_ctrs (bfd *abfd,
8896 char *buf,
8897 int *bufsiz,
8898 const void *s390_ctrs,
8899 int size)
8901 char *note_name = "LINUX";
8902 return elfcore_write_note (abfd, buf, bufsiz,
8903 note_name, NT_S390_CTRS, s390_ctrs, size);
8906 char *
8907 elfcore_write_s390_prefix (bfd *abfd,
8908 char *buf,
8909 int *bufsiz,
8910 const void *s390_prefix,
8911 int size)
8913 char *note_name = "LINUX";
8914 return elfcore_write_note (abfd, buf, bufsiz,
8915 note_name, NT_S390_PREFIX, s390_prefix, size);
8918 char *
8919 elfcore_write_register_note (bfd *abfd,
8920 char *buf,
8921 int *bufsiz,
8922 const char *section,
8923 const void *data,
8924 int size)
8926 if (strcmp (section, ".reg2") == 0)
8927 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
8928 if (strcmp (section, ".reg-xfp") == 0)
8929 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
8930 if (strcmp (section, ".reg-xstate") == 0)
8931 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
8932 if (strcmp (section, ".reg-ppc-vmx") == 0)
8933 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
8934 if (strcmp (section, ".reg-ppc-vsx") == 0)
8935 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
8936 if (strcmp (section, ".reg-s390-high-gprs") == 0)
8937 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
8938 if (strcmp (section, ".reg-s390-timer") == 0)
8939 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
8940 if (strcmp (section, ".reg-s390-todcmp") == 0)
8941 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
8942 if (strcmp (section, ".reg-s390-todpreg") == 0)
8943 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
8944 if (strcmp (section, ".reg-s390-ctrs") == 0)
8945 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
8946 if (strcmp (section, ".reg-s390-prefix") == 0)
8947 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
8948 return NULL;
8951 static bfd_boolean
8952 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
8954 char *p;
8956 p = buf;
8957 while (p < buf + size)
8959 /* FIXME: bad alignment assumption. */
8960 Elf_External_Note *xnp = (Elf_External_Note *) p;
8961 Elf_Internal_Note in;
8963 if (offsetof (Elf_External_Note, name) > buf - p + size)
8964 return FALSE;
8966 in.type = H_GET_32 (abfd, xnp->type);
8968 in.namesz = H_GET_32 (abfd, xnp->namesz);
8969 in.namedata = xnp->name;
8970 if (in.namesz > buf - in.namedata + size)
8971 return FALSE;
8973 in.descsz = H_GET_32 (abfd, xnp->descsz);
8974 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
8975 in.descpos = offset + (in.descdata - buf);
8976 if (in.descsz != 0
8977 && (in.descdata >= buf + size
8978 || in.descsz > buf - in.descdata + size))
8979 return FALSE;
8981 switch (bfd_get_format (abfd))
8983 default:
8984 return TRUE;
8986 case bfd_core:
8987 if (CONST_STRNEQ (in.namedata, "NetBSD-CORE"))
8989 if (! elfcore_grok_netbsd_note (abfd, &in))
8990 return FALSE;
8992 else if (CONST_STRNEQ (in.namedata, "OpenBSD"))
8994 if (! elfcore_grok_openbsd_note (abfd, &in))
8995 return FALSE;
8997 else if (CONST_STRNEQ (in.namedata, "QNX"))
8999 if (! elfcore_grok_nto_note (abfd, &in))
9000 return FALSE;
9002 else if (CONST_STRNEQ (in.namedata, "SPU/"))
9004 if (! elfcore_grok_spu_note (abfd, &in))
9005 return FALSE;
9007 else
9009 if (! elfcore_grok_note (abfd, &in))
9010 return FALSE;
9012 break;
9014 case bfd_object:
9015 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
9017 if (! elfobj_grok_gnu_note (abfd, &in))
9018 return FALSE;
9020 break;
9023 p = in.descdata + BFD_ALIGN (in.descsz, 4);
9026 return TRUE;
9029 static bfd_boolean
9030 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
9032 char *buf;
9034 if (size <= 0)
9035 return TRUE;
9037 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
9038 return FALSE;
9040 buf = (char *) bfd_malloc (size);
9041 if (buf == NULL)
9042 return FALSE;
9044 if (bfd_bread (buf, size, abfd) != size
9045 || !elf_parse_notes (abfd, buf, size, offset))
9047 free (buf);
9048 return FALSE;
9051 free (buf);
9052 return TRUE;
9055 /* Providing external access to the ELF program header table. */
9057 /* Return an upper bound on the number of bytes required to store a
9058 copy of ABFD's program header table entries. Return -1 if an error
9059 occurs; bfd_get_error will return an appropriate code. */
9061 long
9062 bfd_get_elf_phdr_upper_bound (bfd *abfd)
9064 if (abfd->xvec->flavour != bfd_target_elf_flavour)
9066 bfd_set_error (bfd_error_wrong_format);
9067 return -1;
9070 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
9073 /* Copy ABFD's program header table entries to *PHDRS. The entries
9074 will be stored as an array of Elf_Internal_Phdr structures, as
9075 defined in include/elf/internal.h. To find out how large the
9076 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
9078 Return the number of program header table entries read, or -1 if an
9079 error occurs; bfd_get_error will return an appropriate code. */
9082 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
9084 int num_phdrs;
9086 if (abfd->xvec->flavour != bfd_target_elf_flavour)
9088 bfd_set_error (bfd_error_wrong_format);
9089 return -1;
9092 num_phdrs = elf_elfheader (abfd)->e_phnum;
9093 memcpy (phdrs, elf_tdata (abfd)->phdr,
9094 num_phdrs * sizeof (Elf_Internal_Phdr));
9096 return num_phdrs;
9099 enum elf_reloc_type_class
9100 _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
9102 return reloc_class_normal;
9105 /* For RELA architectures, return the relocation value for a
9106 relocation against a local symbol. */
9108 bfd_vma
9109 _bfd_elf_rela_local_sym (bfd *abfd,
9110 Elf_Internal_Sym *sym,
9111 asection **psec,
9112 Elf_Internal_Rela *rel)
9114 asection *sec = *psec;
9115 bfd_vma relocation;
9117 relocation = (sec->output_section->vma
9118 + sec->output_offset
9119 + sym->st_value);
9120 if ((sec->flags & SEC_MERGE)
9121 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
9122 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
9124 rel->r_addend =
9125 _bfd_merged_section_offset (abfd, psec,
9126 elf_section_data (sec)->sec_info,
9127 sym->st_value + rel->r_addend);
9128 if (sec != *psec)
9130 /* If we have changed the section, and our original section is
9131 marked with SEC_EXCLUDE, it means that the original
9132 SEC_MERGE section has been completely subsumed in some
9133 other SEC_MERGE section. In this case, we need to leave
9134 some info around for --emit-relocs. */
9135 if ((sec->flags & SEC_EXCLUDE) != 0)
9136 sec->kept_section = *psec;
9137 sec = *psec;
9139 rel->r_addend -= relocation;
9140 rel->r_addend += sec->output_section->vma + sec->output_offset;
9142 return relocation;
9145 bfd_vma
9146 _bfd_elf_rel_local_sym (bfd *abfd,
9147 Elf_Internal_Sym *sym,
9148 asection **psec,
9149 bfd_vma addend)
9151 asection *sec = *psec;
9153 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
9154 return sym->st_value + addend;
9156 return _bfd_merged_section_offset (abfd, psec,
9157 elf_section_data (sec)->sec_info,
9158 sym->st_value + addend);
9161 bfd_vma
9162 _bfd_elf_section_offset (bfd *abfd,
9163 struct bfd_link_info *info,
9164 asection *sec,
9165 bfd_vma offset)
9167 switch (sec->sec_info_type)
9169 case ELF_INFO_TYPE_STABS:
9170 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
9171 offset);
9172 case ELF_INFO_TYPE_EH_FRAME:
9173 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
9174 default:
9175 return offset;
9179 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
9180 reconstruct an ELF file by reading the segments out of remote memory
9181 based on the ELF file header at EHDR_VMA and the ELF program headers it
9182 points to. If not null, *LOADBASEP is filled in with the difference
9183 between the VMAs from which the segments were read, and the VMAs the
9184 file headers (and hence BFD's idea of each section's VMA) put them at.
9186 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
9187 remote memory at target address VMA into the local buffer at MYADDR; it
9188 should return zero on success or an `errno' code on failure. TEMPL must
9189 be a BFD for an ELF target with the word size and byte order found in
9190 the remote memory. */
9192 bfd *
9193 bfd_elf_bfd_from_remote_memory
9194 (bfd *templ,
9195 bfd_vma ehdr_vma,
9196 bfd_vma *loadbasep,
9197 int (*target_read_memory) (bfd_vma, bfd_byte *, int))
9199 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
9200 (templ, ehdr_vma, loadbasep, target_read_memory);
9203 long
9204 _bfd_elf_get_synthetic_symtab (bfd *abfd,
9205 long symcount ATTRIBUTE_UNUSED,
9206 asymbol **syms ATTRIBUTE_UNUSED,
9207 long dynsymcount,
9208 asymbol **dynsyms,
9209 asymbol **ret)
9211 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9212 asection *relplt;
9213 asymbol *s;
9214 const char *relplt_name;
9215 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
9216 arelent *p;
9217 long count, i, n;
9218 size_t size;
9219 Elf_Internal_Shdr *hdr;
9220 char *names;
9221 asection *plt;
9223 *ret = NULL;
9225 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
9226 return 0;
9228 if (dynsymcount <= 0)
9229 return 0;
9231 if (!bed->plt_sym_val)
9232 return 0;
9234 relplt_name = bed->relplt_name;
9235 if (relplt_name == NULL)
9236 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
9237 relplt = bfd_get_section_by_name (abfd, relplt_name);
9238 if (relplt == NULL)
9239 return 0;
9241 hdr = &elf_section_data (relplt)->this_hdr;
9242 if (hdr->sh_link != elf_dynsymtab (abfd)
9243 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
9244 return 0;
9246 plt = bfd_get_section_by_name (abfd, ".plt");
9247 if (plt == NULL)
9248 return 0;
9250 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
9251 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
9252 return -1;
9254 count = relplt->size / hdr->sh_entsize;
9255 size = count * sizeof (asymbol);
9256 p = relplt->relocation;
9257 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
9259 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
9260 if (p->addend != 0)
9262 #ifdef BFD64
9263 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
9264 #else
9265 size += sizeof ("+0x") - 1 + 8;
9266 #endif
9270 s = *ret = (asymbol *) bfd_malloc (size);
9271 if (s == NULL)
9272 return -1;
9274 names = (char *) (s + count);
9275 p = relplt->relocation;
9276 n = 0;
9277 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
9279 size_t len;
9280 bfd_vma addr;
9282 addr = bed->plt_sym_val (i, plt, p);
9283 if (addr == (bfd_vma) -1)
9284 continue;
9286 *s = **p->sym_ptr_ptr;
9287 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
9288 we are defining a symbol, ensure one of them is set. */
9289 if ((s->flags & BSF_LOCAL) == 0)
9290 s->flags |= BSF_GLOBAL;
9291 s->flags |= BSF_SYNTHETIC;
9292 s->section = plt;
9293 s->value = addr - plt->vma;
9294 s->name = names;
9295 s->udata.p = NULL;
9296 len = strlen ((*p->sym_ptr_ptr)->name);
9297 memcpy (names, (*p->sym_ptr_ptr)->name, len);
9298 names += len;
9299 if (p->addend != 0)
9301 char buf[30], *a;
9303 memcpy (names, "+0x", sizeof ("+0x") - 1);
9304 names += sizeof ("+0x") - 1;
9305 bfd_sprintf_vma (abfd, buf, p->addend);
9306 for (a = buf; *a == '0'; ++a)
9308 len = strlen (a);
9309 memcpy (names, a, len);
9310 names += len;
9312 memcpy (names, "@plt", sizeof ("@plt"));
9313 names += sizeof ("@plt");
9314 ++s, ++n;
9317 return n;
9320 /* It is only used by x86-64 so far. */
9321 asection _bfd_elf_large_com_section
9322 = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
9323 SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
9325 void
9326 _bfd_elf_set_osabi (bfd * abfd,
9327 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
9329 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
9331 i_ehdrp = elf_elfheader (abfd);
9333 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
9335 /* To make things simpler for the loader on Linux systems we set the
9336 osabi field to ELFOSABI_LINUX if the binary contains symbols of
9337 the STT_GNU_IFUNC type. */
9338 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE
9339 && elf_tdata (abfd)->has_ifunc_symbols)
9340 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_LINUX;
9344 /* Return TRUE for ELF symbol types that represent functions.
9345 This is the default version of this function, which is sufficient for
9346 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
9348 bfd_boolean
9349 _bfd_elf_is_function_type (unsigned int type)
9351 return (type == STT_FUNC
9352 || type == STT_GNU_IFUNC);