PR 7023
[binutils.git] / bfd / elf.c
blobcb7d2fc3e447b50da53ad3958cc6bd5d8a9968d0
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 Free Software Foundation, Inc.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
25 SECTION
26 ELF backends
28 BFD support for ELF formats is being worked on.
29 Currently, the best supported back ends are for sparc and i386
30 (running svr4 or Solaris 2).
32 Documentation of the internals of the support code still needs
33 to be written. The code is changing quickly enough that we
34 haven't bothered yet. */
36 /* For sparc64-cross-sparc32. */
37 #define _SYSCALL32
38 #include "sysdep.h"
39 #include "bfd.h"
40 #include "bfdlink.h"
41 #include "libbfd.h"
42 #define ARCH_SIZE 0
43 #include "elf-bfd.h"
44 #include "libiberty.h"
45 #include "safe-ctype.h"
47 static int elf_sort_sections (const void *, const void *);
48 static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
49 static bfd_boolean prep_headers (bfd *);
50 static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ;
51 static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ;
52 static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
53 file_ptr offset);
55 /* Swap version information in and out. The version information is
56 currently size independent. If that ever changes, this code will
57 need to move into elfcode.h. */
59 /* Swap in a Verdef structure. */
61 void
62 _bfd_elf_swap_verdef_in (bfd *abfd,
63 const Elf_External_Verdef *src,
64 Elf_Internal_Verdef *dst)
66 dst->vd_version = H_GET_16 (abfd, src->vd_version);
67 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
68 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
69 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
70 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
71 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
72 dst->vd_next = H_GET_32 (abfd, src->vd_next);
75 /* Swap out a Verdef structure. */
77 void
78 _bfd_elf_swap_verdef_out (bfd *abfd,
79 const Elf_Internal_Verdef *src,
80 Elf_External_Verdef *dst)
82 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
83 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
84 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
85 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
86 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
87 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
88 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
91 /* Swap in a Verdaux structure. */
93 void
94 _bfd_elf_swap_verdaux_in (bfd *abfd,
95 const Elf_External_Verdaux *src,
96 Elf_Internal_Verdaux *dst)
98 dst->vda_name = H_GET_32 (abfd, src->vda_name);
99 dst->vda_next = H_GET_32 (abfd, src->vda_next);
102 /* Swap out a Verdaux structure. */
104 void
105 _bfd_elf_swap_verdaux_out (bfd *abfd,
106 const Elf_Internal_Verdaux *src,
107 Elf_External_Verdaux *dst)
109 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
110 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
113 /* Swap in a Verneed structure. */
115 void
116 _bfd_elf_swap_verneed_in (bfd *abfd,
117 const Elf_External_Verneed *src,
118 Elf_Internal_Verneed *dst)
120 dst->vn_version = H_GET_16 (abfd, src->vn_version);
121 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
122 dst->vn_file = H_GET_32 (abfd, src->vn_file);
123 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
124 dst->vn_next = H_GET_32 (abfd, src->vn_next);
127 /* Swap out a Verneed structure. */
129 void
130 _bfd_elf_swap_verneed_out (bfd *abfd,
131 const Elf_Internal_Verneed *src,
132 Elf_External_Verneed *dst)
134 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
135 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
136 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
137 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
138 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
141 /* Swap in a Vernaux structure. */
143 void
144 _bfd_elf_swap_vernaux_in (bfd *abfd,
145 const Elf_External_Vernaux *src,
146 Elf_Internal_Vernaux *dst)
148 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
149 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
150 dst->vna_other = H_GET_16 (abfd, src->vna_other);
151 dst->vna_name = H_GET_32 (abfd, src->vna_name);
152 dst->vna_next = H_GET_32 (abfd, src->vna_next);
155 /* Swap out a Vernaux structure. */
157 void
158 _bfd_elf_swap_vernaux_out (bfd *abfd,
159 const Elf_Internal_Vernaux *src,
160 Elf_External_Vernaux *dst)
162 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
163 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
164 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
165 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
166 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
169 /* Swap in a Versym structure. */
171 void
172 _bfd_elf_swap_versym_in (bfd *abfd,
173 const Elf_External_Versym *src,
174 Elf_Internal_Versym *dst)
176 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
179 /* Swap out a Versym structure. */
181 void
182 _bfd_elf_swap_versym_out (bfd *abfd,
183 const Elf_Internal_Versym *src,
184 Elf_External_Versym *dst)
186 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
189 /* Standard ELF hash function. Do not change this function; you will
190 cause invalid hash tables to be generated. */
192 unsigned long
193 bfd_elf_hash (const char *namearg)
195 const unsigned char *name = (const unsigned char *) namearg;
196 unsigned long h = 0;
197 unsigned long g;
198 int ch;
200 while ((ch = *name++) != '\0')
202 h = (h << 4) + ch;
203 if ((g = (h & 0xf0000000)) != 0)
205 h ^= g >> 24;
206 /* The ELF ABI says `h &= ~g', but this is equivalent in
207 this case and on some machines one insn instead of two. */
208 h ^= g;
211 return h & 0xffffffff;
214 /* DT_GNU_HASH hash function. Do not change this function; you will
215 cause invalid hash tables to be generated. */
217 unsigned long
218 bfd_elf_gnu_hash (const char *namearg)
220 const unsigned char *name = (const unsigned char *) namearg;
221 unsigned long h = 5381;
222 unsigned char ch;
224 while ((ch = *name++) != '\0')
225 h = (h << 5) + h + ch;
226 return h & 0xffffffff;
229 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
230 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
231 bfd_boolean
232 bfd_elf_allocate_object (bfd *abfd,
233 size_t object_size,
234 enum elf_object_id object_id)
236 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
237 abfd->tdata.any = bfd_zalloc (abfd, object_size);
238 if (abfd->tdata.any == NULL)
239 return FALSE;
241 elf_object_id (abfd) = object_id;
242 elf_program_header_size (abfd) = (bfd_size_type) -1;
243 return TRUE;
247 bfd_boolean
248 bfd_elf_make_generic_object (bfd *abfd)
250 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
251 GENERIC_ELF_TDATA);
254 bfd_boolean
255 bfd_elf_mkcorefile (bfd *abfd)
257 /* I think this can be done just like an object file. */
258 return bfd_elf_make_generic_object (abfd);
261 char *
262 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
264 Elf_Internal_Shdr **i_shdrp;
265 bfd_byte *shstrtab = NULL;
266 file_ptr offset;
267 bfd_size_type shstrtabsize;
269 i_shdrp = elf_elfsections (abfd);
270 if (i_shdrp == 0
271 || shindex >= elf_numsections (abfd)
272 || i_shdrp[shindex] == 0)
273 return NULL;
275 shstrtab = i_shdrp[shindex]->contents;
276 if (shstrtab == NULL)
278 /* No cached one, attempt to read, and cache what we read. */
279 offset = i_shdrp[shindex]->sh_offset;
280 shstrtabsize = i_shdrp[shindex]->sh_size;
282 /* Allocate and clear an extra byte at the end, to prevent crashes
283 in case the string table is not terminated. */
284 if (shstrtabsize + 1 <= 1
285 || (shstrtab = bfd_alloc (abfd, shstrtabsize + 1)) == NULL
286 || bfd_seek (abfd, offset, SEEK_SET) != 0)
287 shstrtab = NULL;
288 else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
290 if (bfd_get_error () != bfd_error_system_call)
291 bfd_set_error (bfd_error_file_truncated);
292 shstrtab = NULL;
293 /* Once we've failed to read it, make sure we don't keep
294 trying. Otherwise, we'll keep allocating space for
295 the string table over and over. */
296 i_shdrp[shindex]->sh_size = 0;
298 else
299 shstrtab[shstrtabsize] = '\0';
300 i_shdrp[shindex]->contents = shstrtab;
302 return (char *) shstrtab;
305 char *
306 bfd_elf_string_from_elf_section (bfd *abfd,
307 unsigned int shindex,
308 unsigned int strindex)
310 Elf_Internal_Shdr *hdr;
312 if (strindex == 0)
313 return "";
315 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
316 return NULL;
318 hdr = elf_elfsections (abfd)[shindex];
320 if (hdr->contents == NULL
321 && bfd_elf_get_str_section (abfd, shindex) == NULL)
322 return NULL;
324 if (strindex >= hdr->sh_size)
326 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
327 (*_bfd_error_handler)
328 (_("%B: invalid string offset %u >= %lu for section `%s'"),
329 abfd, strindex, (unsigned long) hdr->sh_size,
330 (shindex == shstrndx && strindex == hdr->sh_name
331 ? ".shstrtab"
332 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
333 return "";
336 return ((char *) hdr->contents) + strindex;
339 /* Read and convert symbols to internal format.
340 SYMCOUNT specifies the number of symbols to read, starting from
341 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
342 are non-NULL, they are used to store the internal symbols, external
343 symbols, and symbol section index extensions, respectively.
344 Returns a pointer to the internal symbol buffer (malloced if necessary)
345 or NULL if there were no symbols or some kind of problem. */
347 Elf_Internal_Sym *
348 bfd_elf_get_elf_syms (bfd *ibfd,
349 Elf_Internal_Shdr *symtab_hdr,
350 size_t symcount,
351 size_t symoffset,
352 Elf_Internal_Sym *intsym_buf,
353 void *extsym_buf,
354 Elf_External_Sym_Shndx *extshndx_buf)
356 Elf_Internal_Shdr *shndx_hdr;
357 void *alloc_ext;
358 const bfd_byte *esym;
359 Elf_External_Sym_Shndx *alloc_extshndx;
360 Elf_External_Sym_Shndx *shndx;
361 Elf_Internal_Sym *alloc_intsym;
362 Elf_Internal_Sym *isym;
363 Elf_Internal_Sym *isymend;
364 const struct elf_backend_data *bed;
365 size_t extsym_size;
366 bfd_size_type amt;
367 file_ptr pos;
369 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
370 abort ();
372 if (symcount == 0)
373 return intsym_buf;
375 /* Normal syms might have section extension entries. */
376 shndx_hdr = NULL;
377 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
378 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
380 /* Read the symbols. */
381 alloc_ext = NULL;
382 alloc_extshndx = NULL;
383 alloc_intsym = NULL;
384 bed = get_elf_backend_data (ibfd);
385 extsym_size = bed->s->sizeof_sym;
386 amt = symcount * extsym_size;
387 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
388 if (extsym_buf == NULL)
390 alloc_ext = bfd_malloc2 (symcount, extsym_size);
391 extsym_buf = alloc_ext;
393 if (extsym_buf == NULL
394 || bfd_seek (ibfd, pos, SEEK_SET) != 0
395 || bfd_bread (extsym_buf, amt, ibfd) != amt)
397 intsym_buf = NULL;
398 goto out;
401 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
402 extshndx_buf = NULL;
403 else
405 amt = symcount * sizeof (Elf_External_Sym_Shndx);
406 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
407 if (extshndx_buf == NULL)
409 alloc_extshndx = bfd_malloc2 (symcount,
410 sizeof (Elf_External_Sym_Shndx));
411 extshndx_buf = alloc_extshndx;
413 if (extshndx_buf == NULL
414 || bfd_seek (ibfd, pos, SEEK_SET) != 0
415 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
417 intsym_buf = NULL;
418 goto out;
422 if (intsym_buf == NULL)
424 alloc_intsym = bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
425 intsym_buf = alloc_intsym;
426 if (intsym_buf == NULL)
427 goto out;
430 /* Convert the symbols to internal form. */
431 isymend = intsym_buf + symcount;
432 for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf;
433 isym < isymend;
434 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
435 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
437 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
438 (*_bfd_error_handler) (_("%B symbol number %lu references "
439 "nonexistent SHT_SYMTAB_SHNDX section"),
440 ibfd, (unsigned long) symoffset);
441 if (alloc_intsym != NULL)
442 free (alloc_intsym);
443 intsym_buf = NULL;
444 goto out;
447 out:
448 if (alloc_ext != NULL)
449 free (alloc_ext);
450 if (alloc_extshndx != NULL)
451 free (alloc_extshndx);
453 return intsym_buf;
456 /* Look up a symbol name. */
457 const char *
458 bfd_elf_sym_name (bfd *abfd,
459 Elf_Internal_Shdr *symtab_hdr,
460 Elf_Internal_Sym *isym,
461 asection *sym_sec)
463 const char *name;
464 unsigned int iname = isym->st_name;
465 unsigned int shindex = symtab_hdr->sh_link;
467 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
468 /* Check for a bogus st_shndx to avoid crashing. */
469 && isym->st_shndx < elf_numsections (abfd))
471 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
472 shindex = elf_elfheader (abfd)->e_shstrndx;
475 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
476 if (name == NULL)
477 name = "(null)";
478 else if (sym_sec && *name == '\0')
479 name = bfd_section_name (abfd, sym_sec);
481 return name;
484 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
485 sections. The first element is the flags, the rest are section
486 pointers. */
488 typedef union elf_internal_group {
489 Elf_Internal_Shdr *shdr;
490 unsigned int flags;
491 } Elf_Internal_Group;
493 /* Return the name of the group signature symbol. Why isn't the
494 signature just a string? */
496 static const char *
497 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
499 Elf_Internal_Shdr *hdr;
500 unsigned char esym[sizeof (Elf64_External_Sym)];
501 Elf_External_Sym_Shndx eshndx;
502 Elf_Internal_Sym isym;
504 /* First we need to ensure the symbol table is available. Make sure
505 that it is a symbol table section. */
506 if (ghdr->sh_link >= elf_numsections (abfd))
507 return NULL;
508 hdr = elf_elfsections (abfd) [ghdr->sh_link];
509 if (hdr->sh_type != SHT_SYMTAB
510 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
511 return NULL;
513 /* Go read the symbol. */
514 hdr = &elf_tdata (abfd)->symtab_hdr;
515 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
516 &isym, esym, &eshndx) == NULL)
517 return NULL;
519 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
522 /* Set next_in_group list pointer, and group name for NEWSECT. */
524 static bfd_boolean
525 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
527 unsigned int num_group = elf_tdata (abfd)->num_group;
529 /* If num_group is zero, read in all SHT_GROUP sections. The count
530 is set to -1 if there are no SHT_GROUP sections. */
531 if (num_group == 0)
533 unsigned int i, shnum;
535 /* First count the number of groups. If we have a SHT_GROUP
536 section with just a flag word (ie. sh_size is 4), ignore it. */
537 shnum = elf_numsections (abfd);
538 num_group = 0;
540 #define IS_VALID_GROUP_SECTION_HEADER(shdr) \
541 ( (shdr)->sh_type == SHT_GROUP \
542 && (shdr)->sh_size >= (2 * GRP_ENTRY_SIZE) \
543 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
544 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
546 for (i = 0; i < shnum; i++)
548 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
550 if (IS_VALID_GROUP_SECTION_HEADER (shdr))
551 num_group += 1;
554 if (num_group == 0)
556 num_group = (unsigned) -1;
557 elf_tdata (abfd)->num_group = num_group;
559 else
561 /* We keep a list of elf section headers for group sections,
562 so we can find them quickly. */
563 bfd_size_type amt;
565 elf_tdata (abfd)->num_group = num_group;
566 elf_tdata (abfd)->group_sect_ptr
567 = bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
568 if (elf_tdata (abfd)->group_sect_ptr == NULL)
569 return FALSE;
571 num_group = 0;
572 for (i = 0; i < shnum; i++)
574 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
576 if (IS_VALID_GROUP_SECTION_HEADER (shdr))
578 unsigned char *src;
579 Elf_Internal_Group *dest;
581 /* Add to list of sections. */
582 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
583 num_group += 1;
585 /* Read the raw contents. */
586 BFD_ASSERT (sizeof (*dest) >= 4);
587 amt = shdr->sh_size * sizeof (*dest) / 4;
588 shdr->contents = bfd_alloc2 (abfd, shdr->sh_size,
589 sizeof (*dest) / 4);
590 /* PR binutils/4110: Handle corrupt group headers. */
591 if (shdr->contents == NULL)
593 _bfd_error_handler
594 (_("%B: Corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size);
595 bfd_set_error (bfd_error_bad_value);
596 return FALSE;
599 memset (shdr->contents, 0, amt);
601 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
602 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
603 != shdr->sh_size))
604 return FALSE;
606 /* Translate raw contents, a flag word followed by an
607 array of elf section indices all in target byte order,
608 to the flag word followed by an array of elf section
609 pointers. */
610 src = shdr->contents + shdr->sh_size;
611 dest = (Elf_Internal_Group *) (shdr->contents + amt);
612 while (1)
614 unsigned int idx;
616 src -= 4;
617 --dest;
618 idx = H_GET_32 (abfd, src);
619 if (src == shdr->contents)
621 dest->flags = idx;
622 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
623 shdr->bfd_section->flags
624 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
625 break;
627 if (idx >= shnum)
629 ((*_bfd_error_handler)
630 (_("%B: invalid SHT_GROUP entry"), abfd));
631 idx = 0;
633 dest->shdr = elf_elfsections (abfd)[idx];
640 if (num_group != (unsigned) -1)
642 unsigned int i;
644 for (i = 0; i < num_group; i++)
646 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
647 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
648 unsigned int n_elt = shdr->sh_size / 4;
650 /* Look through this group's sections to see if current
651 section is a member. */
652 while (--n_elt != 0)
653 if ((++idx)->shdr == hdr)
655 asection *s = NULL;
657 /* We are a member of this group. Go looking through
658 other members to see if any others are linked via
659 next_in_group. */
660 idx = (Elf_Internal_Group *) shdr->contents;
661 n_elt = shdr->sh_size / 4;
662 while (--n_elt != 0)
663 if ((s = (++idx)->shdr->bfd_section) != NULL
664 && elf_next_in_group (s) != NULL)
665 break;
666 if (n_elt != 0)
668 /* Snarf the group name from other member, and
669 insert current section in circular list. */
670 elf_group_name (newsect) = elf_group_name (s);
671 elf_next_in_group (newsect) = elf_next_in_group (s);
672 elf_next_in_group (s) = newsect;
674 else
676 const char *gname;
678 gname = group_signature (abfd, shdr);
679 if (gname == NULL)
680 return FALSE;
681 elf_group_name (newsect) = gname;
683 /* Start a circular list with one element. */
684 elf_next_in_group (newsect) = newsect;
687 /* If the group section has been created, point to the
688 new member. */
689 if (shdr->bfd_section != NULL)
690 elf_next_in_group (shdr->bfd_section) = newsect;
692 i = num_group - 1;
693 break;
698 if (elf_group_name (newsect) == NULL)
700 (*_bfd_error_handler) (_("%B: no group info for section %A"),
701 abfd, newsect);
703 return TRUE;
706 bfd_boolean
707 _bfd_elf_setup_sections (bfd *abfd)
709 unsigned int i;
710 unsigned int num_group = elf_tdata (abfd)->num_group;
711 bfd_boolean result = TRUE;
712 asection *s;
714 /* Process SHF_LINK_ORDER. */
715 for (s = abfd->sections; s != NULL; s = s->next)
717 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
718 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
720 unsigned int elfsec = this_hdr->sh_link;
721 /* FIXME: The old Intel compiler and old strip/objcopy may
722 not set the sh_link or sh_info fields. Hence we could
723 get the situation where elfsec is 0. */
724 if (elfsec == 0)
726 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
727 if (bed->link_order_error_handler)
728 bed->link_order_error_handler
729 (_("%B: warning: sh_link not set for section `%A'"),
730 abfd, s);
732 else
734 asection *link = NULL;
736 if (elfsec < elf_numsections (abfd))
738 this_hdr = elf_elfsections (abfd)[elfsec];
739 link = this_hdr->bfd_section;
742 /* PR 1991, 2008:
743 Some strip/objcopy may leave an incorrect value in
744 sh_link. We don't want to proceed. */
745 if (link == NULL)
747 (*_bfd_error_handler)
748 (_("%B: sh_link [%d] in section `%A' is incorrect"),
749 s->owner, s, elfsec);
750 result = FALSE;
753 elf_linked_to_section (s) = link;
758 /* Process section groups. */
759 if (num_group == (unsigned) -1)
760 return result;
762 for (i = 0; i < num_group; i++)
764 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
765 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
766 unsigned int n_elt = shdr->sh_size / 4;
768 while (--n_elt != 0)
769 if ((++idx)->shdr->bfd_section)
770 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
771 else if (idx->shdr->sh_type == SHT_RELA
772 || idx->shdr->sh_type == SHT_REL)
773 /* We won't include relocation sections in section groups in
774 output object files. We adjust the group section size here
775 so that relocatable link will work correctly when
776 relocation sections are in section group in input object
777 files. */
778 shdr->bfd_section->size -= 4;
779 else
781 /* There are some unknown sections in the group. */
782 (*_bfd_error_handler)
783 (_("%B: unknown [%d] section `%s' in group [%s]"),
784 abfd,
785 (unsigned int) idx->shdr->sh_type,
786 bfd_elf_string_from_elf_section (abfd,
787 (elf_elfheader (abfd)
788 ->e_shstrndx),
789 idx->shdr->sh_name),
790 shdr->bfd_section->name);
791 result = FALSE;
794 return result;
797 bfd_boolean
798 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
800 return elf_next_in_group (sec) != NULL;
803 /* Make a BFD section from an ELF section. We store a pointer to the
804 BFD section in the bfd_section field of the header. */
806 bfd_boolean
807 _bfd_elf_make_section_from_shdr (bfd *abfd,
808 Elf_Internal_Shdr *hdr,
809 const char *name,
810 int shindex)
812 asection *newsect;
813 flagword flags;
814 const struct elf_backend_data *bed;
816 if (hdr->bfd_section != NULL)
818 BFD_ASSERT (strcmp (name,
819 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
820 return TRUE;
823 newsect = bfd_make_section_anyway (abfd, name);
824 if (newsect == NULL)
825 return FALSE;
827 hdr->bfd_section = newsect;
828 elf_section_data (newsect)->this_hdr = *hdr;
829 elf_section_data (newsect)->this_idx = shindex;
831 /* Always use the real type/flags. */
832 elf_section_type (newsect) = hdr->sh_type;
833 elf_section_flags (newsect) = hdr->sh_flags;
835 newsect->filepos = hdr->sh_offset;
837 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
838 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
839 || ! bfd_set_section_alignment (abfd, newsect,
840 bfd_log2 (hdr->sh_addralign)))
841 return FALSE;
843 flags = SEC_NO_FLAGS;
844 if (hdr->sh_type != SHT_NOBITS)
845 flags |= SEC_HAS_CONTENTS;
846 if (hdr->sh_type == SHT_GROUP)
847 flags |= SEC_GROUP | SEC_EXCLUDE;
848 if ((hdr->sh_flags & SHF_ALLOC) != 0)
850 flags |= SEC_ALLOC;
851 if (hdr->sh_type != SHT_NOBITS)
852 flags |= SEC_LOAD;
854 if ((hdr->sh_flags & SHF_WRITE) == 0)
855 flags |= SEC_READONLY;
856 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
857 flags |= SEC_CODE;
858 else if ((flags & SEC_LOAD) != 0)
859 flags |= SEC_DATA;
860 if ((hdr->sh_flags & SHF_MERGE) != 0)
862 flags |= SEC_MERGE;
863 newsect->entsize = hdr->sh_entsize;
864 if ((hdr->sh_flags & SHF_STRINGS) != 0)
865 flags |= SEC_STRINGS;
867 if (hdr->sh_flags & SHF_GROUP)
868 if (!setup_group (abfd, hdr, newsect))
869 return FALSE;
870 if ((hdr->sh_flags & SHF_TLS) != 0)
871 flags |= SEC_THREAD_LOCAL;
873 if ((flags & SEC_ALLOC) == 0)
875 /* The debugging sections appear to be recognized only by name,
876 not any sort of flag. Their SEC_ALLOC bits are cleared. */
877 static const struct
879 const char *name;
880 int len;
881 } debug_sections [] =
883 { STRING_COMMA_LEN ("debug") }, /* 'd' */
884 { NULL, 0 }, /* 'e' */
885 { NULL, 0 }, /* 'f' */
886 { STRING_COMMA_LEN ("gnu.linkonce.wi.") }, /* 'g' */
887 { NULL, 0 }, /* 'h' */
888 { NULL, 0 }, /* 'i' */
889 { NULL, 0 }, /* 'j' */
890 { NULL, 0 }, /* 'k' */
891 { STRING_COMMA_LEN ("line") }, /* 'l' */
892 { NULL, 0 }, /* 'm' */
893 { NULL, 0 }, /* 'n' */
894 { NULL, 0 }, /* 'o' */
895 { NULL, 0 }, /* 'p' */
896 { NULL, 0 }, /* 'q' */
897 { NULL, 0 }, /* 'r' */
898 { STRING_COMMA_LEN ("stab") }, /* 's' */
899 { NULL, 0 }, /* 't' */
900 { NULL, 0 }, /* 'u' */
901 { NULL, 0 }, /* 'v' */
902 { NULL, 0 }, /* 'w' */
903 { NULL, 0 }, /* 'x' */
904 { NULL, 0 }, /* 'y' */
905 { STRING_COMMA_LEN ("zdebug") } /* 'z' */
908 if (name [0] == '.')
910 int i = name [1] - 'd';
911 if (i >= 0
912 && i < (int) ARRAY_SIZE (debug_sections)
913 && debug_sections [i].name != NULL
914 && strncmp (&name [1], debug_sections [i].name,
915 debug_sections [i].len) == 0)
916 flags |= SEC_DEBUGGING;
920 /* As a GNU extension, if the name begins with .gnu.linkonce, we
921 only link a single copy of the section. This is used to support
922 g++. g++ will emit each template expansion in its own section.
923 The symbols will be defined as weak, so that multiple definitions
924 are permitted. The GNU linker extension is to actually discard
925 all but one of the sections. */
926 if (CONST_STRNEQ (name, ".gnu.linkonce")
927 && elf_next_in_group (newsect) == NULL)
928 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
930 bed = get_elf_backend_data (abfd);
931 if (bed->elf_backend_section_flags)
932 if (! bed->elf_backend_section_flags (&flags, hdr))
933 return FALSE;
935 if (! bfd_set_section_flags (abfd, newsect, flags))
936 return FALSE;
938 /* We do not parse the PT_NOTE segments as we are interested even in the
939 separate debug info files which may have the segments offsets corrupted.
940 PT_NOTEs from the core files are currently not parsed using BFD. */
941 if (hdr->sh_type == SHT_NOTE)
943 bfd_byte *contents;
945 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
946 return FALSE;
948 elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1);
949 free (contents);
952 if ((flags & SEC_ALLOC) != 0)
954 Elf_Internal_Phdr *phdr;
955 unsigned int i, nload;
957 /* Some ELF linkers produce binaries with all the program header
958 p_paddr fields zero. If we have such a binary with more than
959 one PT_LOAD header, then leave the section lma equal to vma
960 so that we don't create sections with overlapping lma. */
961 phdr = elf_tdata (abfd)->phdr;
962 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
963 if (phdr->p_paddr != 0)
964 break;
965 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
966 ++nload;
967 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
968 return TRUE;
970 phdr = elf_tdata (abfd)->phdr;
971 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
973 /* This section is part of this segment if its file
974 offset plus size lies within the segment's memory
975 span and, if the section is loaded, the extent of the
976 loaded data lies within the extent of the segment.
978 Note - we used to check the p_paddr field as well, and
979 refuse to set the LMA if it was 0. This is wrong
980 though, as a perfectly valid initialised segment can
981 have a p_paddr of zero. Some architectures, eg ARM,
982 place special significance on the address 0 and
983 executables need to be able to have a segment which
984 covers this address. */
985 if (phdr->p_type == PT_LOAD
986 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
987 && (hdr->sh_offset + hdr->sh_size
988 <= phdr->p_offset + phdr->p_memsz)
989 && ((flags & SEC_LOAD) == 0
990 || (hdr->sh_offset + hdr->sh_size
991 <= phdr->p_offset + phdr->p_filesz)))
993 if ((flags & SEC_LOAD) == 0)
994 newsect->lma = (phdr->p_paddr
995 + hdr->sh_addr - phdr->p_vaddr);
996 else
997 /* We used to use the same adjustment for SEC_LOAD
998 sections, but that doesn't work if the segment
999 is packed with code from multiple VMAs.
1000 Instead we calculate the section LMA based on
1001 the segment LMA. It is assumed that the
1002 segment will contain sections with contiguous
1003 LMAs, even if the VMAs are not. */
1004 newsect->lma = (phdr->p_paddr
1005 + hdr->sh_offset - phdr->p_offset);
1007 /* With contiguous segments, we can't tell from file
1008 offsets whether a section with zero size should
1009 be placed at the end of one segment or the
1010 beginning of the next. Decide based on vaddr. */
1011 if (hdr->sh_addr >= phdr->p_vaddr
1012 && (hdr->sh_addr + hdr->sh_size
1013 <= phdr->p_vaddr + phdr->p_memsz))
1014 break;
1019 return TRUE;
1023 INTERNAL_FUNCTION
1024 bfd_elf_find_section
1026 SYNOPSIS
1027 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
1029 DESCRIPTION
1030 Helper functions for GDB to locate the string tables.
1031 Since BFD hides string tables from callers, GDB needs to use an
1032 internal hook to find them. Sun's .stabstr, in particular,
1033 isn't even pointed to by the .stab section, so ordinary
1034 mechanisms wouldn't work to find it, even if we had some.
1037 struct elf_internal_shdr *
1038 bfd_elf_find_section (bfd *abfd, char *name)
1040 Elf_Internal_Shdr **i_shdrp;
1041 char *shstrtab;
1042 unsigned int max;
1043 unsigned int i;
1045 i_shdrp = elf_elfsections (abfd);
1046 if (i_shdrp != NULL)
1048 shstrtab = bfd_elf_get_str_section (abfd,
1049 elf_elfheader (abfd)->e_shstrndx);
1050 if (shstrtab != NULL)
1052 max = elf_numsections (abfd);
1053 for (i = 1; i < max; i++)
1054 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
1055 return i_shdrp[i];
1058 return 0;
1061 const char *const bfd_elf_section_type_names[] = {
1062 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1063 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1064 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1067 /* ELF relocs are against symbols. If we are producing relocatable
1068 output, and the reloc is against an external symbol, and nothing
1069 has given us any additional addend, the resulting reloc will also
1070 be against the same symbol. In such a case, we don't want to
1071 change anything about the way the reloc is handled, since it will
1072 all be done at final link time. Rather than put special case code
1073 into bfd_perform_relocation, all the reloc types use this howto
1074 function. It just short circuits the reloc if producing
1075 relocatable output against an external symbol. */
1077 bfd_reloc_status_type
1078 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1079 arelent *reloc_entry,
1080 asymbol *symbol,
1081 void *data ATTRIBUTE_UNUSED,
1082 asection *input_section,
1083 bfd *output_bfd,
1084 char **error_message ATTRIBUTE_UNUSED)
1086 if (output_bfd != NULL
1087 && (symbol->flags & BSF_SECTION_SYM) == 0
1088 && (! reloc_entry->howto->partial_inplace
1089 || reloc_entry->addend == 0))
1091 reloc_entry->address += input_section->output_offset;
1092 return bfd_reloc_ok;
1095 return bfd_reloc_continue;
1098 /* Copy the program header and other data from one object module to
1099 another. */
1101 bfd_boolean
1102 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1104 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1105 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1106 return TRUE;
1108 BFD_ASSERT (!elf_flags_init (obfd)
1109 || (elf_elfheader (obfd)->e_flags
1110 == elf_elfheader (ibfd)->e_flags));
1112 elf_gp (obfd) = elf_gp (ibfd);
1113 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1114 elf_flags_init (obfd) = TRUE;
1116 /* Copy object attributes. */
1117 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1119 return TRUE;
1122 static const char *
1123 get_segment_type (unsigned int p_type)
1125 const char *pt;
1126 switch (p_type)
1128 case PT_NULL: pt = "NULL"; break;
1129 case PT_LOAD: pt = "LOAD"; break;
1130 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1131 case PT_INTERP: pt = "INTERP"; break;
1132 case PT_NOTE: pt = "NOTE"; break;
1133 case PT_SHLIB: pt = "SHLIB"; break;
1134 case PT_PHDR: pt = "PHDR"; break;
1135 case PT_TLS: pt = "TLS"; break;
1136 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1137 case PT_GNU_STACK: pt = "STACK"; break;
1138 case PT_GNU_RELRO: pt = "RELRO"; break;
1139 default: pt = NULL; break;
1141 return pt;
1144 /* Print out the program headers. */
1146 bfd_boolean
1147 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1149 FILE *f = farg;
1150 Elf_Internal_Phdr *p;
1151 asection *s;
1152 bfd_byte *dynbuf = NULL;
1154 p = elf_tdata (abfd)->phdr;
1155 if (p != NULL)
1157 unsigned int i, c;
1159 fprintf (f, _("\nProgram Header:\n"));
1160 c = elf_elfheader (abfd)->e_phnum;
1161 for (i = 0; i < c; i++, p++)
1163 const char *pt = get_segment_type (p->p_type);
1164 char buf[20];
1166 if (pt == NULL)
1168 sprintf (buf, "0x%lx", p->p_type);
1169 pt = buf;
1171 fprintf (f, "%8s off 0x", pt);
1172 bfd_fprintf_vma (abfd, f, p->p_offset);
1173 fprintf (f, " vaddr 0x");
1174 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1175 fprintf (f, " paddr 0x");
1176 bfd_fprintf_vma (abfd, f, p->p_paddr);
1177 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1178 fprintf (f, " filesz 0x");
1179 bfd_fprintf_vma (abfd, f, p->p_filesz);
1180 fprintf (f, " memsz 0x");
1181 bfd_fprintf_vma (abfd, f, p->p_memsz);
1182 fprintf (f, " flags %c%c%c",
1183 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1184 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1185 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1186 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1187 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1188 fprintf (f, "\n");
1192 s = bfd_get_section_by_name (abfd, ".dynamic");
1193 if (s != NULL)
1195 unsigned int elfsec;
1196 unsigned long shlink;
1197 bfd_byte *extdyn, *extdynend;
1198 size_t extdynsize;
1199 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1201 fprintf (f, _("\nDynamic Section:\n"));
1203 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1204 goto error_return;
1206 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1207 if (elfsec == SHN_BAD)
1208 goto error_return;
1209 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1211 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1212 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1214 extdyn = dynbuf;
1215 extdynend = extdyn + s->size;
1216 for (; extdyn < extdynend; extdyn += extdynsize)
1218 Elf_Internal_Dyn dyn;
1219 const char *name = "";
1220 char ab[20];
1221 bfd_boolean stringp;
1222 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1224 (*swap_dyn_in) (abfd, extdyn, &dyn);
1226 if (dyn.d_tag == DT_NULL)
1227 break;
1229 stringp = FALSE;
1230 switch (dyn.d_tag)
1232 default:
1233 if (bed->elf_backend_get_target_dtag)
1234 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1236 if (!strcmp (name, ""))
1238 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1239 name = ab;
1241 break;
1243 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1244 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1245 case DT_PLTGOT: name = "PLTGOT"; break;
1246 case DT_HASH: name = "HASH"; break;
1247 case DT_STRTAB: name = "STRTAB"; break;
1248 case DT_SYMTAB: name = "SYMTAB"; break;
1249 case DT_RELA: name = "RELA"; break;
1250 case DT_RELASZ: name = "RELASZ"; break;
1251 case DT_RELAENT: name = "RELAENT"; break;
1252 case DT_STRSZ: name = "STRSZ"; break;
1253 case DT_SYMENT: name = "SYMENT"; break;
1254 case DT_INIT: name = "INIT"; break;
1255 case DT_FINI: name = "FINI"; break;
1256 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1257 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1258 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1259 case DT_REL: name = "REL"; break;
1260 case DT_RELSZ: name = "RELSZ"; break;
1261 case DT_RELENT: name = "RELENT"; break;
1262 case DT_PLTREL: name = "PLTREL"; break;
1263 case DT_DEBUG: name = "DEBUG"; break;
1264 case DT_TEXTREL: name = "TEXTREL"; break;
1265 case DT_JMPREL: name = "JMPREL"; break;
1266 case DT_BIND_NOW: name = "BIND_NOW"; break;
1267 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1268 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1269 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1270 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1271 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1272 case DT_FLAGS: name = "FLAGS"; break;
1273 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1274 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1275 case DT_CHECKSUM: name = "CHECKSUM"; break;
1276 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1277 case DT_MOVEENT: name = "MOVEENT"; break;
1278 case DT_MOVESZ: name = "MOVESZ"; break;
1279 case DT_FEATURE: name = "FEATURE"; break;
1280 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1281 case DT_SYMINSZ: name = "SYMINSZ"; break;
1282 case DT_SYMINENT: name = "SYMINENT"; break;
1283 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1284 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1285 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1286 case DT_PLTPAD: name = "PLTPAD"; break;
1287 case DT_MOVETAB: name = "MOVETAB"; break;
1288 case DT_SYMINFO: name = "SYMINFO"; break;
1289 case DT_RELACOUNT: name = "RELACOUNT"; break;
1290 case DT_RELCOUNT: name = "RELCOUNT"; break;
1291 case DT_FLAGS_1: name = "FLAGS_1"; break;
1292 case DT_VERSYM: name = "VERSYM"; break;
1293 case DT_VERDEF: name = "VERDEF"; break;
1294 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1295 case DT_VERNEED: name = "VERNEED"; break;
1296 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1297 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1298 case DT_USED: name = "USED"; break;
1299 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1300 case DT_GNU_HASH: name = "GNU_HASH"; break;
1303 fprintf (f, " %-20s ", name);
1304 if (! stringp)
1306 fprintf (f, "0x");
1307 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1309 else
1311 const char *string;
1312 unsigned int tagv = dyn.d_un.d_val;
1314 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1315 if (string == NULL)
1316 goto error_return;
1317 fprintf (f, "%s", string);
1319 fprintf (f, "\n");
1322 free (dynbuf);
1323 dynbuf = NULL;
1326 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1327 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1329 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1330 return FALSE;
1333 if (elf_dynverdef (abfd) != 0)
1335 Elf_Internal_Verdef *t;
1337 fprintf (f, _("\nVersion definitions:\n"));
1338 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1340 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1341 t->vd_flags, t->vd_hash,
1342 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1343 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1345 Elf_Internal_Verdaux *a;
1347 fprintf (f, "\t");
1348 for (a = t->vd_auxptr->vda_nextptr;
1349 a != NULL;
1350 a = a->vda_nextptr)
1351 fprintf (f, "%s ",
1352 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1353 fprintf (f, "\n");
1358 if (elf_dynverref (abfd) != 0)
1360 Elf_Internal_Verneed *t;
1362 fprintf (f, _("\nVersion References:\n"));
1363 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1365 Elf_Internal_Vernaux *a;
1367 fprintf (f, _(" required from %s:\n"),
1368 t->vn_filename ? t->vn_filename : "<corrupt>");
1369 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1370 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1371 a->vna_flags, a->vna_other,
1372 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1376 return TRUE;
1378 error_return:
1379 if (dynbuf != NULL)
1380 free (dynbuf);
1381 return FALSE;
1384 /* Display ELF-specific fields of a symbol. */
1386 void
1387 bfd_elf_print_symbol (bfd *abfd,
1388 void *filep,
1389 asymbol *symbol,
1390 bfd_print_symbol_type how)
1392 FILE *file = filep;
1393 switch (how)
1395 case bfd_print_symbol_name:
1396 fprintf (file, "%s", symbol->name);
1397 break;
1398 case bfd_print_symbol_more:
1399 fprintf (file, "elf ");
1400 bfd_fprintf_vma (abfd, file, symbol->value);
1401 fprintf (file, " %lx", (unsigned long) symbol->flags);
1402 break;
1403 case bfd_print_symbol_all:
1405 const char *section_name;
1406 const char *name = NULL;
1407 const struct elf_backend_data *bed;
1408 unsigned char st_other;
1409 bfd_vma val;
1411 section_name = symbol->section ? symbol->section->name : "(*none*)";
1413 bed = get_elf_backend_data (abfd);
1414 if (bed->elf_backend_print_symbol_all)
1415 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1417 if (name == NULL)
1419 name = symbol->name;
1420 bfd_print_symbol_vandf (abfd, file, symbol);
1423 fprintf (file, " %s\t", section_name);
1424 /* Print the "other" value for a symbol. For common symbols,
1425 we've already printed the size; now print the alignment.
1426 For other symbols, we have no specified alignment, and
1427 we've printed the address; now print the size. */
1428 if (symbol->section && bfd_is_com_section (symbol->section))
1429 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1430 else
1431 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1432 bfd_fprintf_vma (abfd, file, val);
1434 /* If we have version information, print it. */
1435 if (elf_tdata (abfd)->dynversym_section != 0
1436 && (elf_tdata (abfd)->dynverdef_section != 0
1437 || elf_tdata (abfd)->dynverref_section != 0))
1439 unsigned int vernum;
1440 const char *version_string;
1442 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1444 if (vernum == 0)
1445 version_string = "";
1446 else if (vernum == 1)
1447 version_string = "Base";
1448 else if (vernum <= elf_tdata (abfd)->cverdefs)
1449 version_string =
1450 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1451 else
1453 Elf_Internal_Verneed *t;
1455 version_string = "";
1456 for (t = elf_tdata (abfd)->verref;
1457 t != NULL;
1458 t = t->vn_nextref)
1460 Elf_Internal_Vernaux *a;
1462 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1464 if (a->vna_other == vernum)
1466 version_string = a->vna_nodename;
1467 break;
1473 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1474 fprintf (file, " %-11s", version_string);
1475 else
1477 int i;
1479 fprintf (file, " (%s)", version_string);
1480 for (i = 10 - strlen (version_string); i > 0; --i)
1481 putc (' ', file);
1485 /* If the st_other field is not zero, print it. */
1486 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1488 switch (st_other)
1490 case 0: break;
1491 case STV_INTERNAL: fprintf (file, " .internal"); break;
1492 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1493 case STV_PROTECTED: fprintf (file, " .protected"); break;
1494 default:
1495 /* Some other non-defined flags are also present, so print
1496 everything hex. */
1497 fprintf (file, " 0x%02x", (unsigned int) st_other);
1500 fprintf (file, " %s", name);
1502 break;
1506 /* Allocate an ELF string table--force the first byte to be zero. */
1508 struct bfd_strtab_hash *
1509 _bfd_elf_stringtab_init (void)
1511 struct bfd_strtab_hash *ret;
1513 ret = _bfd_stringtab_init ();
1514 if (ret != NULL)
1516 bfd_size_type loc;
1518 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1519 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1520 if (loc == (bfd_size_type) -1)
1522 _bfd_stringtab_free (ret);
1523 ret = NULL;
1526 return ret;
1529 /* ELF .o/exec file reading */
1531 /* Create a new bfd section from an ELF section header. */
1533 bfd_boolean
1534 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1536 Elf_Internal_Shdr *hdr;
1537 Elf_Internal_Ehdr *ehdr;
1538 const struct elf_backend_data *bed;
1539 const char *name;
1541 if (shindex >= elf_numsections (abfd))
1542 return FALSE;
1544 hdr = elf_elfsections (abfd)[shindex];
1545 ehdr = elf_elfheader (abfd);
1546 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1547 hdr->sh_name);
1548 if (name == NULL)
1549 return FALSE;
1551 bed = get_elf_backend_data (abfd);
1552 switch (hdr->sh_type)
1554 case SHT_NULL:
1555 /* Inactive section. Throw it away. */
1556 return TRUE;
1558 case SHT_PROGBITS: /* Normal section with contents. */
1559 case SHT_NOBITS: /* .bss section. */
1560 case SHT_HASH: /* .hash section. */
1561 case SHT_NOTE: /* .note section. */
1562 case SHT_INIT_ARRAY: /* .init_array section. */
1563 case SHT_FINI_ARRAY: /* .fini_array section. */
1564 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1565 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
1566 case SHT_GNU_HASH: /* .gnu.hash section. */
1567 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1569 case SHT_DYNAMIC: /* Dynamic linking information. */
1570 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1571 return FALSE;
1572 if (hdr->sh_link > elf_numsections (abfd)
1573 || elf_elfsections (abfd)[hdr->sh_link] == NULL)
1574 return FALSE;
1575 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1577 Elf_Internal_Shdr *dynsymhdr;
1579 /* The shared libraries distributed with hpux11 have a bogus
1580 sh_link field for the ".dynamic" section. Find the
1581 string table for the ".dynsym" section instead. */
1582 if (elf_dynsymtab (abfd) != 0)
1584 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1585 hdr->sh_link = dynsymhdr->sh_link;
1587 else
1589 unsigned int i, num_sec;
1591 num_sec = elf_numsections (abfd);
1592 for (i = 1; i < num_sec; i++)
1594 dynsymhdr = elf_elfsections (abfd)[i];
1595 if (dynsymhdr->sh_type == SHT_DYNSYM)
1597 hdr->sh_link = dynsymhdr->sh_link;
1598 break;
1603 break;
1605 case SHT_SYMTAB: /* A symbol table */
1606 if (elf_onesymtab (abfd) == shindex)
1607 return TRUE;
1609 if (hdr->sh_entsize != bed->s->sizeof_sym)
1610 return FALSE;
1611 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
1612 return FALSE;
1613 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1614 elf_onesymtab (abfd) = shindex;
1615 elf_tdata (abfd)->symtab_hdr = *hdr;
1616 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1617 abfd->flags |= HAS_SYMS;
1619 /* Sometimes a shared object will map in the symbol table. If
1620 SHF_ALLOC is set, and this is a shared object, then we also
1621 treat this section as a BFD section. We can not base the
1622 decision purely on SHF_ALLOC, because that flag is sometimes
1623 set in a relocatable object file, which would confuse the
1624 linker. */
1625 if ((hdr->sh_flags & SHF_ALLOC) != 0
1626 && (abfd->flags & DYNAMIC) != 0
1627 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1628 shindex))
1629 return FALSE;
1631 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1632 can't read symbols without that section loaded as well. It
1633 is most likely specified by the next section header. */
1634 if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex)
1636 unsigned int i, num_sec;
1638 num_sec = elf_numsections (abfd);
1639 for (i = shindex + 1; i < num_sec; i++)
1641 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1642 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1643 && hdr2->sh_link == shindex)
1644 break;
1646 if (i == num_sec)
1647 for (i = 1; i < shindex; i++)
1649 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1650 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1651 && hdr2->sh_link == shindex)
1652 break;
1654 if (i != shindex)
1655 return bfd_section_from_shdr (abfd, i);
1657 return TRUE;
1659 case SHT_DYNSYM: /* A dynamic symbol table */
1660 if (elf_dynsymtab (abfd) == shindex)
1661 return TRUE;
1663 if (hdr->sh_entsize != bed->s->sizeof_sym)
1664 return FALSE;
1665 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1666 elf_dynsymtab (abfd) = shindex;
1667 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1668 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1669 abfd->flags |= HAS_SYMS;
1671 /* Besides being a symbol table, we also treat this as a regular
1672 section, so that objcopy can handle it. */
1673 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1675 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1676 if (elf_symtab_shndx (abfd) == shindex)
1677 return TRUE;
1679 BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
1680 elf_symtab_shndx (abfd) = shindex;
1681 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1682 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1683 return TRUE;
1685 case SHT_STRTAB: /* A string table */
1686 if (hdr->bfd_section != NULL)
1687 return TRUE;
1688 if (ehdr->e_shstrndx == shindex)
1690 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1691 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1692 return TRUE;
1694 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
1696 symtab_strtab:
1697 elf_tdata (abfd)->strtab_hdr = *hdr;
1698 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
1699 return TRUE;
1701 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
1703 dynsymtab_strtab:
1704 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1705 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
1706 elf_elfsections (abfd)[shindex] = hdr;
1707 /* We also treat this as a regular section, so that objcopy
1708 can handle it. */
1709 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1710 shindex);
1713 /* If the string table isn't one of the above, then treat it as a
1714 regular section. We need to scan all the headers to be sure,
1715 just in case this strtab section appeared before the above. */
1716 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
1718 unsigned int i, num_sec;
1720 num_sec = elf_numsections (abfd);
1721 for (i = 1; i < num_sec; i++)
1723 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1724 if (hdr2->sh_link == shindex)
1726 /* Prevent endless recursion on broken objects. */
1727 if (i == shindex)
1728 return FALSE;
1729 if (! bfd_section_from_shdr (abfd, i))
1730 return FALSE;
1731 if (elf_onesymtab (abfd) == i)
1732 goto symtab_strtab;
1733 if (elf_dynsymtab (abfd) == i)
1734 goto dynsymtab_strtab;
1738 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1740 case SHT_REL:
1741 case SHT_RELA:
1742 /* *These* do a lot of work -- but build no sections! */
1744 asection *target_sect;
1745 Elf_Internal_Shdr *hdr2;
1746 unsigned int num_sec = elf_numsections (abfd);
1748 if (hdr->sh_entsize
1749 != (bfd_size_type) (hdr->sh_type == SHT_REL
1750 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
1751 return FALSE;
1753 /* Check for a bogus link to avoid crashing. */
1754 if (hdr->sh_link >= num_sec)
1756 ((*_bfd_error_handler)
1757 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1758 abfd, hdr->sh_link, name, shindex));
1759 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1760 shindex);
1763 /* For some incomprehensible reason Oracle distributes
1764 libraries for Solaris in which some of the objects have
1765 bogus sh_link fields. It would be nice if we could just
1766 reject them, but, unfortunately, some people need to use
1767 them. We scan through the section headers; if we find only
1768 one suitable symbol table, we clobber the sh_link to point
1769 to it. I hope this doesn't break anything. */
1770 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1771 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1773 unsigned int scan;
1774 int found;
1776 found = 0;
1777 for (scan = 1; scan < num_sec; scan++)
1779 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1780 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1782 if (found != 0)
1784 found = 0;
1785 break;
1787 found = scan;
1790 if (found != 0)
1791 hdr->sh_link = found;
1794 /* Get the symbol table. */
1795 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1796 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
1797 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1798 return FALSE;
1800 /* If this reloc section does not use the main symbol table we
1801 don't treat it as a reloc section. BFD can't adequately
1802 represent such a section, so at least for now, we don't
1803 try. We just present it as a normal section. We also
1804 can't use it as a reloc section if it points to the null
1805 section, an invalid section, or another reloc section. */
1806 if (hdr->sh_link != elf_onesymtab (abfd)
1807 || hdr->sh_info == SHN_UNDEF
1808 || hdr->sh_info >= num_sec
1809 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
1810 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
1811 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1812 shindex);
1814 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1815 return FALSE;
1816 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1817 if (target_sect == NULL)
1818 return FALSE;
1820 if ((target_sect->flags & SEC_RELOC) == 0
1821 || target_sect->reloc_count == 0)
1822 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1823 else
1825 bfd_size_type amt;
1826 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1827 amt = sizeof (*hdr2);
1828 hdr2 = bfd_alloc (abfd, amt);
1829 if (hdr2 == NULL)
1830 return FALSE;
1831 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1833 *hdr2 = *hdr;
1834 elf_elfsections (abfd)[shindex] = hdr2;
1835 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1836 target_sect->flags |= SEC_RELOC;
1837 target_sect->relocation = NULL;
1838 target_sect->rel_filepos = hdr->sh_offset;
1839 /* In the section to which the relocations apply, mark whether
1840 its relocations are of the REL or RELA variety. */
1841 if (hdr->sh_size != 0)
1842 target_sect->use_rela_p = hdr->sh_type == SHT_RELA;
1843 abfd->flags |= HAS_RELOC;
1844 return TRUE;
1847 case SHT_GNU_verdef:
1848 elf_dynverdef (abfd) = shindex;
1849 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1850 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1852 case SHT_GNU_versym:
1853 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
1854 return FALSE;
1855 elf_dynversym (abfd) = shindex;
1856 elf_tdata (abfd)->dynversym_hdr = *hdr;
1857 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1859 case SHT_GNU_verneed:
1860 elf_dynverref (abfd) = shindex;
1861 elf_tdata (abfd)->dynverref_hdr = *hdr;
1862 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1864 case SHT_SHLIB:
1865 return TRUE;
1867 case SHT_GROUP:
1868 if (! IS_VALID_GROUP_SECTION_HEADER (hdr))
1869 return FALSE;
1870 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1871 return FALSE;
1872 if (hdr->contents != NULL)
1874 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1875 unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE;
1876 asection *s;
1878 if (idx->flags & GRP_COMDAT)
1879 hdr->bfd_section->flags
1880 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1882 /* We try to keep the same section order as it comes in. */
1883 idx += n_elt;
1884 while (--n_elt != 0)
1886 --idx;
1888 if (idx->shdr != NULL
1889 && (s = idx->shdr->bfd_section) != NULL
1890 && elf_next_in_group (s) != NULL)
1892 elf_next_in_group (hdr->bfd_section) = s;
1893 break;
1897 break;
1899 default:
1900 /* Possibly an attributes section. */
1901 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
1902 || hdr->sh_type == bed->obj_attrs_section_type)
1904 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1905 return FALSE;
1906 _bfd_elf_parse_attributes (abfd, hdr);
1907 return TRUE;
1910 /* Check for any processor-specific section types. */
1911 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
1912 return TRUE;
1914 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
1916 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1917 /* FIXME: How to properly handle allocated section reserved
1918 for applications? */
1919 (*_bfd_error_handler)
1920 (_("%B: don't know how to handle allocated, application "
1921 "specific section `%s' [0x%8x]"),
1922 abfd, name, hdr->sh_type);
1923 else
1924 /* Allow sections reserved for applications. */
1925 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1926 shindex);
1928 else if (hdr->sh_type >= SHT_LOPROC
1929 && hdr->sh_type <= SHT_HIPROC)
1930 /* FIXME: We should handle this section. */
1931 (*_bfd_error_handler)
1932 (_("%B: don't know how to handle processor specific section "
1933 "`%s' [0x%8x]"),
1934 abfd, name, hdr->sh_type);
1935 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
1937 /* Unrecognised OS-specific sections. */
1938 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
1939 /* SHF_OS_NONCONFORMING indicates that special knowledge is
1940 required to correctly process the section and the file should
1941 be rejected with an error message. */
1942 (*_bfd_error_handler)
1943 (_("%B: don't know how to handle OS specific section "
1944 "`%s' [0x%8x]"),
1945 abfd, name, hdr->sh_type);
1946 else
1947 /* Otherwise it should be processed. */
1948 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1950 else
1951 /* FIXME: We should handle this section. */
1952 (*_bfd_error_handler)
1953 (_("%B: don't know how to handle section `%s' [0x%8x]"),
1954 abfd, name, hdr->sh_type);
1956 return FALSE;
1959 return TRUE;
1962 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1963 Return SEC for sections that have no elf section, and NULL on error. */
1965 asection *
1966 bfd_section_from_r_symndx (bfd *abfd,
1967 struct sym_sec_cache *cache,
1968 asection *sec,
1969 unsigned long r_symndx)
1971 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
1972 asection *s;
1974 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
1976 Elf_Internal_Shdr *symtab_hdr;
1977 unsigned char esym[sizeof (Elf64_External_Sym)];
1978 Elf_External_Sym_Shndx eshndx;
1979 Elf_Internal_Sym isym;
1981 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1982 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
1983 &isym, esym, &eshndx) == NULL)
1984 return NULL;
1986 if (cache->abfd != abfd)
1988 memset (cache->indx, -1, sizeof (cache->indx));
1989 cache->abfd = abfd;
1991 cache->indx[ent] = r_symndx;
1992 cache->shndx[ent] = isym.st_shndx;
1995 s = bfd_section_from_elf_index (abfd, cache->shndx[ent]);
1996 if (s != NULL)
1997 return s;
1999 return sec;
2002 /* Given an ELF section number, retrieve the corresponding BFD
2003 section. */
2005 asection *
2006 bfd_section_from_elf_index (bfd *abfd, unsigned int index)
2008 if (index >= elf_numsections (abfd))
2009 return NULL;
2010 return elf_elfsections (abfd)[index]->bfd_section;
2013 static const struct bfd_elf_special_section special_sections_b[] =
2015 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2016 { NULL, 0, 0, 0, 0 }
2019 static const struct bfd_elf_special_section special_sections_c[] =
2021 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2022 { NULL, 0, 0, 0, 0 }
2025 static const struct bfd_elf_special_section special_sections_d[] =
2027 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2028 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2029 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2030 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2031 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2032 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2033 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2034 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2035 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2036 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2037 { NULL, 0, 0, 0, 0 }
2040 static const struct bfd_elf_special_section special_sections_f[] =
2042 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2043 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2044 { NULL, 0, 0, 0, 0 }
2047 static const struct bfd_elf_special_section special_sections_g[] =
2049 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2050 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2051 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2052 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2053 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2054 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2055 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2056 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2057 { NULL, 0, 0, 0, 0 }
2060 static const struct bfd_elf_special_section special_sections_h[] =
2062 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2063 { NULL, 0, 0, 0, 0 }
2066 static const struct bfd_elf_special_section special_sections_i[] =
2068 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2069 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2070 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2071 { NULL, 0, 0, 0, 0 }
2074 static const struct bfd_elf_special_section special_sections_l[] =
2076 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2077 { NULL, 0, 0, 0, 0 }
2080 static const struct bfd_elf_special_section special_sections_n[] =
2082 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2083 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2084 { NULL, 0, 0, 0, 0 }
2087 static const struct bfd_elf_special_section special_sections_p[] =
2089 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2090 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2091 { NULL, 0, 0, 0, 0 }
2094 static const struct bfd_elf_special_section special_sections_r[] =
2096 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2097 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2098 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2099 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2100 { NULL, 0, 0, 0, 0 }
2103 static const struct bfd_elf_special_section special_sections_s[] =
2105 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2106 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2107 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2108 /* See struct bfd_elf_special_section declaration for the semantics of
2109 this special case where .prefix_length != strlen (.prefix). */
2110 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2111 { NULL, 0, 0, 0, 0 }
2114 static const struct bfd_elf_special_section special_sections_t[] =
2116 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2117 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2118 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2119 { NULL, 0, 0, 0, 0 }
2122 static const struct bfd_elf_special_section special_sections_z[] =
2124 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2125 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2126 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2127 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2128 { NULL, 0, 0, 0, 0 }
2131 static const struct bfd_elf_special_section *special_sections[] =
2133 special_sections_b, /* 'b' */
2134 special_sections_c, /* 'c' */
2135 special_sections_d, /* 'd' */
2136 NULL, /* 'e' */
2137 special_sections_f, /* 'f' */
2138 special_sections_g, /* 'g' */
2139 special_sections_h, /* 'h' */
2140 special_sections_i, /* 'i' */
2141 NULL, /* 'j' */
2142 NULL, /* 'k' */
2143 special_sections_l, /* 'l' */
2144 NULL, /* 'm' */
2145 special_sections_n, /* 'n' */
2146 NULL, /* 'o' */
2147 special_sections_p, /* 'p' */
2148 NULL, /* 'q' */
2149 special_sections_r, /* 'r' */
2150 special_sections_s, /* 's' */
2151 special_sections_t, /* 't' */
2152 NULL, /* 'u' */
2153 NULL, /* 'v' */
2154 NULL, /* 'w' */
2155 NULL, /* 'x' */
2156 NULL, /* 'y' */
2157 special_sections_z /* 'z' */
2160 const struct bfd_elf_special_section *
2161 _bfd_elf_get_special_section (const char *name,
2162 const struct bfd_elf_special_section *spec,
2163 unsigned int rela)
2165 int i;
2166 int len;
2168 len = strlen (name);
2170 for (i = 0; spec[i].prefix != NULL; i++)
2172 int suffix_len;
2173 int prefix_len = spec[i].prefix_length;
2175 if (len < prefix_len)
2176 continue;
2177 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2178 continue;
2180 suffix_len = spec[i].suffix_length;
2181 if (suffix_len <= 0)
2183 if (name[prefix_len] != 0)
2185 if (suffix_len == 0)
2186 continue;
2187 if (name[prefix_len] != '.'
2188 && (suffix_len == -2
2189 || (rela && spec[i].type == SHT_REL)))
2190 continue;
2193 else
2195 if (len < prefix_len + suffix_len)
2196 continue;
2197 if (memcmp (name + len - suffix_len,
2198 spec[i].prefix + prefix_len,
2199 suffix_len) != 0)
2200 continue;
2202 return &spec[i];
2205 return NULL;
2208 const struct bfd_elf_special_section *
2209 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2211 int i;
2212 const struct bfd_elf_special_section *spec;
2213 const struct elf_backend_data *bed;
2215 /* See if this is one of the special sections. */
2216 if (sec->name == NULL)
2217 return NULL;
2219 bed = get_elf_backend_data (abfd);
2220 spec = bed->special_sections;
2221 if (spec)
2223 spec = _bfd_elf_get_special_section (sec->name,
2224 bed->special_sections,
2225 sec->use_rela_p);
2226 if (spec != NULL)
2227 return spec;
2230 if (sec->name[0] != '.')
2231 return NULL;
2233 i = sec->name[1] - 'b';
2234 if (i < 0 || i > 'z' - 'b')
2235 return NULL;
2237 spec = special_sections[i];
2239 if (spec == NULL)
2240 return NULL;
2242 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2245 bfd_boolean
2246 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2248 struct bfd_elf_section_data *sdata;
2249 const struct elf_backend_data *bed;
2250 const struct bfd_elf_special_section *ssect;
2252 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2253 if (sdata == NULL)
2255 sdata = bfd_zalloc (abfd, sizeof (*sdata));
2256 if (sdata == NULL)
2257 return FALSE;
2258 sec->used_by_bfd = sdata;
2261 /* Indicate whether or not this section should use RELA relocations. */
2262 bed = get_elf_backend_data (abfd);
2263 sec->use_rela_p = bed->default_use_rela_p;
2265 /* When we read a file, we don't need to set ELF section type and
2266 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2267 anyway. We will set ELF section type and flags for all linker
2268 created sections. If user specifies BFD section flags, we will
2269 set ELF section type and flags based on BFD section flags in
2270 elf_fake_sections. */
2271 if ((!sec->flags && abfd->direction != read_direction)
2272 || (sec->flags & SEC_LINKER_CREATED) != 0)
2274 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2275 if (ssect != NULL)
2277 elf_section_type (sec) = ssect->type;
2278 elf_section_flags (sec) = ssect->attr;
2282 return _bfd_generic_new_section_hook (abfd, sec);
2285 /* Create a new bfd section from an ELF program header.
2287 Since program segments have no names, we generate a synthetic name
2288 of the form segment<NUM>, where NUM is generally the index in the
2289 program header table. For segments that are split (see below) we
2290 generate the names segment<NUM>a and segment<NUM>b.
2292 Note that some program segments may have a file size that is different than
2293 (less than) the memory size. All this means is that at execution the
2294 system must allocate the amount of memory specified by the memory size,
2295 but only initialize it with the first "file size" bytes read from the
2296 file. This would occur for example, with program segments consisting
2297 of combined data+bss.
2299 To handle the above situation, this routine generates TWO bfd sections
2300 for the single program segment. The first has the length specified by
2301 the file size of the segment, and the second has the length specified
2302 by the difference between the two sizes. In effect, the segment is split
2303 into its initialized and uninitialized parts.
2307 bfd_boolean
2308 _bfd_elf_make_section_from_phdr (bfd *abfd,
2309 Elf_Internal_Phdr *hdr,
2310 int index,
2311 const char *typename)
2313 asection *newsect;
2314 char *name;
2315 char namebuf[64];
2316 size_t len;
2317 int split;
2319 split = ((hdr->p_memsz > 0)
2320 && (hdr->p_filesz > 0)
2321 && (hdr->p_memsz > hdr->p_filesz));
2323 if (hdr->p_filesz > 0)
2325 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
2326 len = strlen (namebuf) + 1;
2327 name = 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;
2335 newsect->lma = hdr->p_paddr;
2336 newsect->size = hdr->p_filesz;
2337 newsect->filepos = hdr->p_offset;
2338 newsect->flags |= SEC_HAS_CONTENTS;
2339 newsect->alignment_power = bfd_log2 (hdr->p_align);
2340 if (hdr->p_type == PT_LOAD)
2342 newsect->flags |= SEC_ALLOC;
2343 newsect->flags |= SEC_LOAD;
2344 if (hdr->p_flags & PF_X)
2346 /* FIXME: all we known is that it has execute PERMISSION,
2347 may be data. */
2348 newsect->flags |= SEC_CODE;
2351 if (!(hdr->p_flags & PF_W))
2353 newsect->flags |= SEC_READONLY;
2357 if (hdr->p_memsz > hdr->p_filesz)
2359 bfd_vma align;
2361 sprintf (namebuf, "%s%d%s", typename, index, split ? "b" : "");
2362 len = strlen (namebuf) + 1;
2363 name = bfd_alloc (abfd, len);
2364 if (!name)
2365 return FALSE;
2366 memcpy (name, namebuf, len);
2367 newsect = bfd_make_section (abfd, name);
2368 if (newsect == NULL)
2369 return FALSE;
2370 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2371 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2372 newsect->size = hdr->p_memsz - hdr->p_filesz;
2373 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2374 align = newsect->vma & -newsect->vma;
2375 if (align == 0 || align > hdr->p_align)
2376 align = hdr->p_align;
2377 newsect->alignment_power = bfd_log2 (align);
2378 if (hdr->p_type == PT_LOAD)
2380 /* Hack for gdb. Segments that have not been modified do
2381 not have their contents written to a core file, on the
2382 assumption that a debugger can find the contents in the
2383 executable. We flag this case by setting the fake
2384 section size to zero. Note that "real" bss sections will
2385 always have their contents dumped to the core file. */
2386 if (bfd_get_format (abfd) == bfd_core)
2387 newsect->size = 0;
2388 newsect->flags |= SEC_ALLOC;
2389 if (hdr->p_flags & PF_X)
2390 newsect->flags |= SEC_CODE;
2392 if (!(hdr->p_flags & PF_W))
2393 newsect->flags |= SEC_READONLY;
2396 return TRUE;
2399 bfd_boolean
2400 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index)
2402 const struct elf_backend_data *bed;
2404 switch (hdr->p_type)
2406 case PT_NULL:
2407 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2409 case PT_LOAD:
2410 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2412 case PT_DYNAMIC:
2413 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2415 case PT_INTERP:
2416 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2418 case PT_NOTE:
2419 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2420 return FALSE;
2421 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2422 return FALSE;
2423 return TRUE;
2425 case PT_SHLIB:
2426 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2428 case PT_PHDR:
2429 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2431 case PT_GNU_EH_FRAME:
2432 return _bfd_elf_make_section_from_phdr (abfd, hdr, index,
2433 "eh_frame_hdr");
2435 case PT_GNU_STACK:
2436 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "stack");
2438 case PT_GNU_RELRO:
2439 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro");
2441 default:
2442 /* Check for any processor-specific program segment types. */
2443 bed = get_elf_backend_data (abfd);
2444 return bed->elf_backend_section_from_phdr (abfd, hdr, index, "proc");
2448 /* Initialize REL_HDR, the section-header for new section, containing
2449 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2450 relocations; otherwise, we use REL relocations. */
2452 bfd_boolean
2453 _bfd_elf_init_reloc_shdr (bfd *abfd,
2454 Elf_Internal_Shdr *rel_hdr,
2455 asection *asect,
2456 bfd_boolean use_rela_p)
2458 char *name;
2459 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2460 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2462 name = bfd_alloc (abfd, amt);
2463 if (name == NULL)
2464 return FALSE;
2465 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2466 rel_hdr->sh_name =
2467 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2468 FALSE);
2469 if (rel_hdr->sh_name == (unsigned int) -1)
2470 return FALSE;
2471 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2472 rel_hdr->sh_entsize = (use_rela_p
2473 ? bed->s->sizeof_rela
2474 : bed->s->sizeof_rel);
2475 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
2476 rel_hdr->sh_flags = 0;
2477 rel_hdr->sh_addr = 0;
2478 rel_hdr->sh_size = 0;
2479 rel_hdr->sh_offset = 0;
2481 return TRUE;
2484 /* Set up an ELF internal section header for a section. */
2486 static void
2487 elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg)
2489 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2490 bfd_boolean *failedptr = failedptrarg;
2491 Elf_Internal_Shdr *this_hdr;
2492 unsigned int sh_type;
2494 if (*failedptr)
2496 /* We already failed; just get out of the bfd_map_over_sections
2497 loop. */
2498 return;
2501 this_hdr = &elf_section_data (asect)->this_hdr;
2503 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2504 asect->name, FALSE);
2505 if (this_hdr->sh_name == (unsigned int) -1)
2507 *failedptr = TRUE;
2508 return;
2511 /* Don't clear sh_flags. Assembler may set additional bits. */
2513 if ((asect->flags & SEC_ALLOC) != 0
2514 || asect->user_set_vma)
2515 this_hdr->sh_addr = asect->vma;
2516 else
2517 this_hdr->sh_addr = 0;
2519 this_hdr->sh_offset = 0;
2520 this_hdr->sh_size = asect->size;
2521 this_hdr->sh_link = 0;
2522 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
2523 /* The sh_entsize and sh_info fields may have been set already by
2524 copy_private_section_data. */
2526 this_hdr->bfd_section = asect;
2527 this_hdr->contents = NULL;
2529 /* If the section type is unspecified, we set it based on
2530 asect->flags. */
2531 if ((asect->flags & SEC_GROUP) != 0)
2532 sh_type = SHT_GROUP;
2533 else if ((asect->flags & SEC_ALLOC) != 0
2534 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2535 || (asect->flags & SEC_NEVER_LOAD) != 0))
2536 sh_type = SHT_NOBITS;
2537 else
2538 sh_type = SHT_PROGBITS;
2540 if (this_hdr->sh_type == SHT_NULL)
2541 this_hdr->sh_type = sh_type;
2542 else if (this_hdr->sh_type == SHT_NOBITS
2543 && sh_type == SHT_PROGBITS
2544 && (asect->flags & SEC_ALLOC) != 0)
2546 /* Warn if we are changing a NOBITS section to PROGBITS, but
2547 allow the link to proceed. This can happen when users link
2548 non-bss input sections to bss output sections, or emit data
2549 to a bss output section via a linker script. */
2550 (*_bfd_error_handler)
2551 (_("warning: section `%A' type changed to PROGBITS"), asect);
2552 this_hdr->sh_type = sh_type;
2555 switch (this_hdr->sh_type)
2557 default:
2558 break;
2560 case SHT_STRTAB:
2561 case SHT_INIT_ARRAY:
2562 case SHT_FINI_ARRAY:
2563 case SHT_PREINIT_ARRAY:
2564 case SHT_NOTE:
2565 case SHT_NOBITS:
2566 case SHT_PROGBITS:
2567 break;
2569 case SHT_HASH:
2570 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2571 break;
2573 case SHT_DYNSYM:
2574 this_hdr->sh_entsize = bed->s->sizeof_sym;
2575 break;
2577 case SHT_DYNAMIC:
2578 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2579 break;
2581 case SHT_RELA:
2582 if (get_elf_backend_data (abfd)->may_use_rela_p)
2583 this_hdr->sh_entsize = bed->s->sizeof_rela;
2584 break;
2586 case SHT_REL:
2587 if (get_elf_backend_data (abfd)->may_use_rel_p)
2588 this_hdr->sh_entsize = bed->s->sizeof_rel;
2589 break;
2591 case SHT_GNU_versym:
2592 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2593 break;
2595 case SHT_GNU_verdef:
2596 this_hdr->sh_entsize = 0;
2597 /* objcopy or strip will copy over sh_info, but may not set
2598 cverdefs. The linker will set cverdefs, but sh_info will be
2599 zero. */
2600 if (this_hdr->sh_info == 0)
2601 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2602 else
2603 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2604 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2605 break;
2607 case SHT_GNU_verneed:
2608 this_hdr->sh_entsize = 0;
2609 /* objcopy or strip will copy over sh_info, but may not set
2610 cverrefs. The linker will set cverrefs, but sh_info will be
2611 zero. */
2612 if (this_hdr->sh_info == 0)
2613 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2614 else
2615 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2616 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2617 break;
2619 case SHT_GROUP:
2620 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2621 break;
2623 case SHT_GNU_HASH:
2624 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
2625 break;
2628 if ((asect->flags & SEC_ALLOC) != 0)
2629 this_hdr->sh_flags |= SHF_ALLOC;
2630 if ((asect->flags & SEC_READONLY) == 0)
2631 this_hdr->sh_flags |= SHF_WRITE;
2632 if ((asect->flags & SEC_CODE) != 0)
2633 this_hdr->sh_flags |= SHF_EXECINSTR;
2634 if ((asect->flags & SEC_MERGE) != 0)
2636 this_hdr->sh_flags |= SHF_MERGE;
2637 this_hdr->sh_entsize = asect->entsize;
2638 if ((asect->flags & SEC_STRINGS) != 0)
2639 this_hdr->sh_flags |= SHF_STRINGS;
2641 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2642 this_hdr->sh_flags |= SHF_GROUP;
2643 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2645 this_hdr->sh_flags |= SHF_TLS;
2646 if (asect->size == 0
2647 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2649 struct bfd_link_order *o = asect->map_tail.link_order;
2651 this_hdr->sh_size = 0;
2652 if (o != NULL)
2654 this_hdr->sh_size = o->offset + o->size;
2655 if (this_hdr->sh_size != 0)
2656 this_hdr->sh_type = SHT_NOBITS;
2661 /* Check for processor-specific section types. */
2662 sh_type = this_hdr->sh_type;
2663 if (bed->elf_backend_fake_sections
2664 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2665 *failedptr = TRUE;
2667 if (sh_type == SHT_NOBITS && asect->size != 0)
2669 /* Don't change the header type from NOBITS if we are being
2670 called for objcopy --only-keep-debug. */
2671 this_hdr->sh_type = sh_type;
2674 /* If the section has relocs, set up a section header for the
2675 SHT_REL[A] section. If two relocation sections are required for
2676 this section, it is up to the processor-specific back-end to
2677 create the other. */
2678 if ((asect->flags & SEC_RELOC) != 0
2679 && !_bfd_elf_init_reloc_shdr (abfd,
2680 &elf_section_data (asect)->rel_hdr,
2681 asect,
2682 asect->use_rela_p))
2683 *failedptr = TRUE;
2686 /* Fill in the contents of a SHT_GROUP section. Called from
2687 _bfd_elf_compute_section_file_positions for gas, objcopy, and
2688 when ELF targets use the generic linker, ld. Called for ld -r
2689 from bfd_elf_final_link. */
2691 void
2692 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2694 bfd_boolean *failedptr = failedptrarg;
2695 asection *elt, *first;
2696 unsigned char *loc;
2697 bfd_boolean gas;
2699 /* Ignore linker created group section. See elfNN_ia64_object_p in
2700 elfxx-ia64.c. */
2701 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
2702 || *failedptr)
2703 return;
2705 if (elf_section_data (sec)->this_hdr.sh_info == 0)
2707 unsigned long symindx = 0;
2709 /* elf_group_id will have been set up by objcopy and the
2710 generic linker. */
2711 if (elf_group_id (sec) != NULL)
2712 symindx = elf_group_id (sec)->udata.i;
2714 if (symindx == 0)
2716 /* If called from the assembler, swap_out_syms will have set up
2717 elf_section_syms. */
2718 BFD_ASSERT (elf_section_syms (abfd) != NULL);
2719 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2721 elf_section_data (sec)->this_hdr.sh_info = symindx;
2723 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
2725 /* The ELF backend linker sets sh_info to -2 when the group
2726 signature symbol is global, and thus the index can't be
2727 set until all local symbols are output. */
2728 asection *igroup = elf_sec_group (elf_next_in_group (sec));
2729 struct bfd_elf_section_data *sec_data = elf_section_data (igroup);
2730 unsigned long symndx = sec_data->this_hdr.sh_info;
2731 unsigned long extsymoff = 0;
2732 struct elf_link_hash_entry *h;
2734 if (!elf_bad_symtab (igroup->owner))
2736 Elf_Internal_Shdr *symtab_hdr;
2738 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
2739 extsymoff = symtab_hdr->sh_info;
2741 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
2742 while (h->root.type == bfd_link_hash_indirect
2743 || h->root.type == bfd_link_hash_warning)
2744 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2746 elf_section_data (sec)->this_hdr.sh_info = h->indx;
2749 /* The contents won't be allocated for "ld -r" or objcopy. */
2750 gas = TRUE;
2751 if (sec->contents == NULL)
2753 gas = FALSE;
2754 sec->contents = bfd_alloc (abfd, sec->size);
2756 /* Arrange for the section to be written out. */
2757 elf_section_data (sec)->this_hdr.contents = sec->contents;
2758 if (sec->contents == NULL)
2760 *failedptr = TRUE;
2761 return;
2765 loc = sec->contents + sec->size;
2767 /* Get the pointer to the first section in the group that gas
2768 squirreled away here. objcopy arranges for this to be set to the
2769 start of the input section group. */
2770 first = elt = elf_next_in_group (sec);
2772 /* First element is a flag word. Rest of section is elf section
2773 indices for all the sections of the group. Write them backwards
2774 just to keep the group in the same order as given in .section
2775 directives, not that it matters. */
2776 while (elt != NULL)
2778 asection *s;
2779 unsigned int idx;
2781 loc -= 4;
2782 s = elt;
2783 if (!gas)
2784 s = s->output_section;
2785 idx = 0;
2786 if (s != NULL)
2787 idx = elf_section_data (s)->this_idx;
2788 H_PUT_32 (abfd, idx, loc);
2789 elt = elf_next_in_group (elt);
2790 if (elt == first)
2791 break;
2794 if ((loc -= 4) != sec->contents)
2795 abort ();
2797 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2800 /* Assign all ELF section numbers. The dummy first section is handled here
2801 too. The link/info pointers for the standard section types are filled
2802 in here too, while we're at it. */
2804 static bfd_boolean
2805 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
2807 struct elf_obj_tdata *t = elf_tdata (abfd);
2808 asection *sec;
2809 unsigned int section_number, secn;
2810 Elf_Internal_Shdr **i_shdrp;
2811 struct bfd_elf_section_data *d;
2813 section_number = 1;
2815 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2817 /* SHT_GROUP sections are in relocatable files only. */
2818 if (link_info == NULL || link_info->relocatable)
2820 /* Put SHT_GROUP sections first. */
2821 for (sec = abfd->sections; sec != NULL; sec = sec->next)
2823 d = elf_section_data (sec);
2825 if (d->this_hdr.sh_type == SHT_GROUP)
2827 if (sec->flags & SEC_LINKER_CREATED)
2829 /* Remove the linker created SHT_GROUP sections. */
2830 bfd_section_list_remove (abfd, sec);
2831 abfd->section_count--;
2833 else
2834 d->this_idx = section_number++;
2839 for (sec = abfd->sections; sec; sec = sec->next)
2841 d = elf_section_data (sec);
2843 if (d->this_hdr.sh_type != SHT_GROUP)
2844 d->this_idx = section_number++;
2845 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2846 if ((sec->flags & SEC_RELOC) == 0)
2847 d->rel_idx = 0;
2848 else
2850 d->rel_idx = section_number++;
2851 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2854 if (d->rel_hdr2)
2856 d->rel_idx2 = section_number++;
2857 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2859 else
2860 d->rel_idx2 = 0;
2863 t->shstrtab_section = section_number++;
2864 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2865 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2867 if (bfd_get_symcount (abfd) > 0)
2869 t->symtab_section = section_number++;
2870 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2871 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
2873 t->symtab_shndx_section = section_number++;
2874 t->symtab_shndx_hdr.sh_name
2875 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2876 ".symtab_shndx", FALSE);
2877 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2878 return FALSE;
2880 t->strtab_section = section_number++;
2881 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2884 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2885 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2887 elf_numsections (abfd) = section_number;
2888 elf_elfheader (abfd)->e_shnum = section_number;
2890 /* Set up the list of section header pointers, in agreement with the
2891 indices. */
2892 i_shdrp = bfd_zalloc2 (abfd, section_number, sizeof (Elf_Internal_Shdr *));
2893 if (i_shdrp == NULL)
2894 return FALSE;
2896 i_shdrp[0] = bfd_zalloc (abfd, sizeof (Elf_Internal_Shdr));
2897 if (i_shdrp[0] == NULL)
2899 bfd_release (abfd, i_shdrp);
2900 return FALSE;
2903 elf_elfsections (abfd) = i_shdrp;
2905 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2906 if (bfd_get_symcount (abfd) > 0)
2908 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2909 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
2911 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2912 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2914 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2915 t->symtab_hdr.sh_link = t->strtab_section;
2918 for (sec = abfd->sections; sec; sec = sec->next)
2920 struct bfd_elf_section_data *d = elf_section_data (sec);
2921 asection *s;
2922 const char *name;
2924 i_shdrp[d->this_idx] = &d->this_hdr;
2925 if (d->rel_idx != 0)
2926 i_shdrp[d->rel_idx] = &d->rel_hdr;
2927 if (d->rel_idx2 != 0)
2928 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2930 /* Fill in the sh_link and sh_info fields while we're at it. */
2932 /* sh_link of a reloc section is the section index of the symbol
2933 table. sh_info is the section index of the section to which
2934 the relocation entries apply. */
2935 if (d->rel_idx != 0)
2937 d->rel_hdr.sh_link = t->symtab_section;
2938 d->rel_hdr.sh_info = d->this_idx;
2940 if (d->rel_idx2 != 0)
2942 d->rel_hdr2->sh_link = t->symtab_section;
2943 d->rel_hdr2->sh_info = d->this_idx;
2946 /* We need to set up sh_link for SHF_LINK_ORDER. */
2947 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
2949 s = elf_linked_to_section (sec);
2950 if (s)
2952 /* elf_linked_to_section points to the input section. */
2953 if (link_info != NULL)
2955 /* Check discarded linkonce section. */
2956 if (elf_discarded_section (s))
2958 asection *kept;
2959 (*_bfd_error_handler)
2960 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
2961 abfd, d->this_hdr.bfd_section,
2962 s, s->owner);
2963 /* Point to the kept section if it has the same
2964 size as the discarded one. */
2965 kept = _bfd_elf_check_kept_section (s, link_info);
2966 if (kept == NULL)
2968 bfd_set_error (bfd_error_bad_value);
2969 return FALSE;
2971 s = kept;
2974 s = s->output_section;
2975 BFD_ASSERT (s != NULL);
2977 else
2979 /* Handle objcopy. */
2980 if (s->output_section == NULL)
2982 (*_bfd_error_handler)
2983 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
2984 abfd, d->this_hdr.bfd_section, s, s->owner);
2985 bfd_set_error (bfd_error_bad_value);
2986 return FALSE;
2988 s = s->output_section;
2990 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2992 else
2994 /* PR 290:
2995 The Intel C compiler generates SHT_IA_64_UNWIND with
2996 SHF_LINK_ORDER. But it doesn't set the sh_link or
2997 sh_info fields. Hence we could get the situation
2998 where s is NULL. */
2999 const struct elf_backend_data *bed
3000 = get_elf_backend_data (abfd);
3001 if (bed->link_order_error_handler)
3002 bed->link_order_error_handler
3003 (_("%B: warning: sh_link not set for section `%A'"),
3004 abfd, sec);
3008 switch (d->this_hdr.sh_type)
3010 case SHT_REL:
3011 case SHT_RELA:
3012 /* A reloc section which we are treating as a normal BFD
3013 section. sh_link is the section index of the symbol
3014 table. sh_info is the section index of the section to
3015 which the relocation entries apply. We assume that an
3016 allocated reloc section uses the dynamic symbol table.
3017 FIXME: How can we be sure? */
3018 s = bfd_get_section_by_name (abfd, ".dynsym");
3019 if (s != NULL)
3020 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3022 /* We look up the section the relocs apply to by name. */
3023 name = sec->name;
3024 if (d->this_hdr.sh_type == SHT_REL)
3025 name += 4;
3026 else
3027 name += 5;
3028 s = bfd_get_section_by_name (abfd, name);
3029 if (s != NULL)
3030 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3031 break;
3033 case SHT_STRTAB:
3034 /* We assume that a section named .stab*str is a stabs
3035 string section. We look for a section with the same name
3036 but without the trailing ``str'', and set its sh_link
3037 field to point to this section. */
3038 if (CONST_STRNEQ (sec->name, ".stab")
3039 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3041 size_t len;
3042 char *alc;
3044 len = strlen (sec->name);
3045 alc = bfd_malloc (len - 2);
3046 if (alc == NULL)
3047 return FALSE;
3048 memcpy (alc, sec->name, len - 3);
3049 alc[len - 3] = '\0';
3050 s = bfd_get_section_by_name (abfd, alc);
3051 free (alc);
3052 if (s != NULL)
3054 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3056 /* This is a .stab section. */
3057 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3058 elf_section_data (s)->this_hdr.sh_entsize
3059 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3062 break;
3064 case SHT_DYNAMIC:
3065 case SHT_DYNSYM:
3066 case SHT_GNU_verneed:
3067 case SHT_GNU_verdef:
3068 /* sh_link is the section header index of the string table
3069 used for the dynamic entries, or the symbol table, or the
3070 version strings. */
3071 s = bfd_get_section_by_name (abfd, ".dynstr");
3072 if (s != NULL)
3073 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3074 break;
3076 case SHT_GNU_LIBLIST:
3077 /* sh_link is the section header index of the prelink library
3078 list used for the dynamic entries, or the symbol table, or
3079 the version strings. */
3080 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3081 ? ".dynstr" : ".gnu.libstr");
3082 if (s != NULL)
3083 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3084 break;
3086 case SHT_HASH:
3087 case SHT_GNU_HASH:
3088 case SHT_GNU_versym:
3089 /* sh_link is the section header index of the symbol table
3090 this hash table or version table is for. */
3091 s = bfd_get_section_by_name (abfd, ".dynsym");
3092 if (s != NULL)
3093 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3094 break;
3096 case SHT_GROUP:
3097 d->this_hdr.sh_link = t->symtab_section;
3101 for (secn = 1; secn < section_number; ++secn)
3102 if (i_shdrp[secn] == NULL)
3103 i_shdrp[secn] = i_shdrp[0];
3104 else
3105 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
3106 i_shdrp[secn]->sh_name);
3107 return TRUE;
3110 /* Map symbol from it's internal number to the external number, moving
3111 all local symbols to be at the head of the list. */
3113 static bfd_boolean
3114 sym_is_global (bfd *abfd, asymbol *sym)
3116 /* If the backend has a special mapping, use it. */
3117 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3118 if (bed->elf_backend_sym_is_global)
3119 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3121 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
3122 || bfd_is_und_section (bfd_get_section (sym))
3123 || bfd_is_com_section (bfd_get_section (sym)));
3126 /* Don't output section symbols for sections that are not going to be
3127 output. */
3129 static bfd_boolean
3130 ignore_section_sym (bfd *abfd, asymbol *sym)
3132 return ((sym->flags & BSF_SECTION_SYM) != 0
3133 && !(sym->section->owner == abfd
3134 || (sym->section->output_section->owner == abfd
3135 && sym->section->output_offset == 0)));
3138 static bfd_boolean
3139 elf_map_symbols (bfd *abfd)
3141 unsigned int symcount = bfd_get_symcount (abfd);
3142 asymbol **syms = bfd_get_outsymbols (abfd);
3143 asymbol **sect_syms;
3144 unsigned int num_locals = 0;
3145 unsigned int num_globals = 0;
3146 unsigned int num_locals2 = 0;
3147 unsigned int num_globals2 = 0;
3148 int max_index = 0;
3149 unsigned int idx;
3150 asection *asect;
3151 asymbol **new_syms;
3153 #ifdef DEBUG
3154 fprintf (stderr, "elf_map_symbols\n");
3155 fflush (stderr);
3156 #endif
3158 for (asect = abfd->sections; asect; asect = asect->next)
3160 if (max_index < asect->index)
3161 max_index = asect->index;
3164 max_index++;
3165 sect_syms = bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
3166 if (sect_syms == NULL)
3167 return FALSE;
3168 elf_section_syms (abfd) = sect_syms;
3169 elf_num_section_syms (abfd) = max_index;
3171 /* Init sect_syms entries for any section symbols we have already
3172 decided to output. */
3173 for (idx = 0; idx < symcount; idx++)
3175 asymbol *sym = syms[idx];
3177 if ((sym->flags & BSF_SECTION_SYM) != 0
3178 && sym->value == 0
3179 && !ignore_section_sym (abfd, sym))
3181 asection *sec = sym->section;
3183 if (sec->owner != abfd)
3184 sec = sec->output_section;
3186 sect_syms[sec->index] = syms[idx];
3190 /* Classify all of the symbols. */
3191 for (idx = 0; idx < symcount; idx++)
3193 if (ignore_section_sym (abfd, syms[idx]))
3194 continue;
3195 if (!sym_is_global (abfd, syms[idx]))
3196 num_locals++;
3197 else
3198 num_globals++;
3201 /* We will be adding a section symbol for each normal BFD section. Most
3202 sections will already have a section symbol in outsymbols, but
3203 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3204 at least in that case. */
3205 for (asect = abfd->sections; asect; asect = asect->next)
3207 if (sect_syms[asect->index] == NULL)
3209 if (!sym_is_global (abfd, asect->symbol))
3210 num_locals++;
3211 else
3212 num_globals++;
3216 /* Now sort the symbols so the local symbols are first. */
3217 new_syms = bfd_alloc2 (abfd, num_locals + num_globals, sizeof (asymbol *));
3219 if (new_syms == NULL)
3220 return FALSE;
3222 for (idx = 0; idx < symcount; idx++)
3224 asymbol *sym = syms[idx];
3225 unsigned int i;
3227 if (ignore_section_sym (abfd, sym))
3228 continue;
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;
3236 for (asect = abfd->sections; asect; asect = asect->next)
3238 if (sect_syms[asect->index] == NULL)
3240 asymbol *sym = asect->symbol;
3241 unsigned int i;
3243 sect_syms[asect->index] = sym;
3244 if (!sym_is_global (abfd, sym))
3245 i = num_locals2++;
3246 else
3247 i = num_locals + num_globals2++;
3248 new_syms[i] = sym;
3249 sym->udata.i = i + 1;
3253 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3255 elf_num_locals (abfd) = num_locals;
3256 elf_num_globals (abfd) = num_globals;
3257 return TRUE;
3260 /* Align to the maximum file alignment that could be required for any
3261 ELF data structure. */
3263 static inline file_ptr
3264 align_file_position (file_ptr off, int align)
3266 return (off + align - 1) & ~(align - 1);
3269 /* Assign a file position to a section, optionally aligning to the
3270 required section alignment. */
3272 file_ptr
3273 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3274 file_ptr offset,
3275 bfd_boolean align)
3277 if (align && i_shdrp->sh_addralign > 1)
3278 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
3279 i_shdrp->sh_offset = offset;
3280 if (i_shdrp->bfd_section != NULL)
3281 i_shdrp->bfd_section->filepos = offset;
3282 if (i_shdrp->sh_type != SHT_NOBITS)
3283 offset += i_shdrp->sh_size;
3284 return offset;
3287 /* Compute the file positions we are going to put the sections at, and
3288 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3289 is not NULL, this is being called by the ELF backend linker. */
3291 bfd_boolean
3292 _bfd_elf_compute_section_file_positions (bfd *abfd,
3293 struct bfd_link_info *link_info)
3295 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3296 bfd_boolean failed;
3297 struct bfd_strtab_hash *strtab = NULL;
3298 Elf_Internal_Shdr *shstrtab_hdr;
3300 if (abfd->output_has_begun)
3301 return TRUE;
3303 /* Do any elf backend specific processing first. */
3304 if (bed->elf_backend_begin_write_processing)
3305 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3307 if (! prep_headers (abfd))
3308 return FALSE;
3310 /* Post process the headers if necessary. */
3311 if (bed->elf_backend_post_process_headers)
3312 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3314 failed = FALSE;
3315 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3316 if (failed)
3317 return FALSE;
3319 if (!assign_section_numbers (abfd, link_info))
3320 return FALSE;
3322 /* The backend linker builds symbol table information itself. */
3323 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3325 /* Non-zero if doing a relocatable link. */
3326 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3328 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3329 return FALSE;
3332 if (link_info == NULL)
3334 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3335 if (failed)
3336 return FALSE;
3339 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3340 /* sh_name was set in prep_headers. */
3341 shstrtab_hdr->sh_type = SHT_STRTAB;
3342 shstrtab_hdr->sh_flags = 0;
3343 shstrtab_hdr->sh_addr = 0;
3344 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3345 shstrtab_hdr->sh_entsize = 0;
3346 shstrtab_hdr->sh_link = 0;
3347 shstrtab_hdr->sh_info = 0;
3348 /* sh_offset is set in assign_file_positions_except_relocs. */
3349 shstrtab_hdr->sh_addralign = 1;
3351 if (!assign_file_positions_except_relocs (abfd, link_info))
3352 return FALSE;
3354 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3356 file_ptr off;
3357 Elf_Internal_Shdr *hdr;
3359 off = elf_tdata (abfd)->next_file_pos;
3361 hdr = &elf_tdata (abfd)->symtab_hdr;
3362 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3364 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3365 if (hdr->sh_size != 0)
3366 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3368 hdr = &elf_tdata (abfd)->strtab_hdr;
3369 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3371 elf_tdata (abfd)->next_file_pos = off;
3373 /* Now that we know where the .strtab section goes, write it
3374 out. */
3375 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3376 || ! _bfd_stringtab_emit (abfd, strtab))
3377 return FALSE;
3378 _bfd_stringtab_free (strtab);
3381 abfd->output_has_begun = TRUE;
3383 return TRUE;
3386 /* Make an initial estimate of the size of the program header. If we
3387 get the number wrong here, we'll redo section placement. */
3389 static bfd_size_type
3390 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
3392 size_t segs;
3393 asection *s;
3394 const struct elf_backend_data *bed;
3396 /* Assume we will need exactly two PT_LOAD segments: one for text
3397 and one for data. */
3398 segs = 2;
3400 s = bfd_get_section_by_name (abfd, ".interp");
3401 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3403 /* If we have a loadable interpreter section, we need a
3404 PT_INTERP segment. In this case, assume we also need a
3405 PT_PHDR segment, although that may not be true for all
3406 targets. */
3407 segs += 2;
3410 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3412 /* We need a PT_DYNAMIC segment. */
3413 ++segs;
3416 if (info != NULL && info->relro)
3418 /* We need a PT_GNU_RELRO segment. */
3419 ++segs;
3422 if (elf_tdata (abfd)->eh_frame_hdr)
3424 /* We need a PT_GNU_EH_FRAME segment. */
3425 ++segs;
3428 if (elf_tdata (abfd)->stack_flags)
3430 /* We need a PT_GNU_STACK segment. */
3431 ++segs;
3434 for (s = abfd->sections; s != NULL; s = s->next)
3436 if ((s->flags & SEC_LOAD) != 0
3437 && CONST_STRNEQ (s->name, ".note"))
3439 /* We need a PT_NOTE segment. */
3440 ++segs;
3441 /* Try to create just one PT_NOTE segment
3442 for all adjacent loadable .note* sections.
3443 gABI requires that within a PT_NOTE segment
3444 (and also inside of each SHT_NOTE section)
3445 each note is padded to a multiple of 4 size,
3446 so we check whether the sections are correctly
3447 aligned. */
3448 if (s->alignment_power == 2)
3449 while (s->next != NULL
3450 && s->next->alignment_power == 2
3451 && (s->next->flags & SEC_LOAD) != 0
3452 && CONST_STRNEQ (s->next->name, ".note"))
3453 s = s->next;
3457 for (s = abfd->sections; s != NULL; s = s->next)
3459 if (s->flags & SEC_THREAD_LOCAL)
3461 /* We need a PT_TLS segment. */
3462 ++segs;
3463 break;
3467 /* Let the backend count up any program headers it might need. */
3468 bed = get_elf_backend_data (abfd);
3469 if (bed->elf_backend_additional_program_headers)
3471 int a;
3473 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
3474 if (a == -1)
3475 abort ();
3476 segs += a;
3479 return segs * bed->s->sizeof_phdr;
3482 /* Find the segment that contains the output_section of section. */
3484 Elf_Internal_Phdr *
3485 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
3487 struct elf_segment_map *m;
3488 Elf_Internal_Phdr *p;
3490 for (m = elf_tdata (abfd)->segment_map,
3491 p = elf_tdata (abfd)->phdr;
3492 m != NULL;
3493 m = m->next, p++)
3495 int i;
3497 for (i = m->count - 1; i >= 0; i--)
3498 if (m->sections[i] == section)
3499 return p;
3502 return NULL;
3505 /* Create a mapping from a set of sections to a program segment. */
3507 static struct elf_segment_map *
3508 make_mapping (bfd *abfd,
3509 asection **sections,
3510 unsigned int from,
3511 unsigned int to,
3512 bfd_boolean phdr)
3514 struct elf_segment_map *m;
3515 unsigned int i;
3516 asection **hdrpp;
3517 bfd_size_type amt;
3519 amt = sizeof (struct elf_segment_map);
3520 amt += (to - from - 1) * sizeof (asection *);
3521 m = bfd_zalloc (abfd, amt);
3522 if (m == NULL)
3523 return NULL;
3524 m->next = NULL;
3525 m->p_type = PT_LOAD;
3526 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3527 m->sections[i - from] = *hdrpp;
3528 m->count = to - from;
3530 if (from == 0 && phdr)
3532 /* Include the headers in the first PT_LOAD segment. */
3533 m->includes_filehdr = 1;
3534 m->includes_phdrs = 1;
3537 return m;
3540 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3541 on failure. */
3543 struct elf_segment_map *
3544 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3546 struct elf_segment_map *m;
3548 m = bfd_zalloc (abfd, sizeof (struct elf_segment_map));
3549 if (m == NULL)
3550 return NULL;
3551 m->next = NULL;
3552 m->p_type = PT_DYNAMIC;
3553 m->count = 1;
3554 m->sections[0] = dynsec;
3556 return m;
3559 /* Possibly add or remove segments from the segment map. */
3561 static bfd_boolean
3562 elf_modify_segment_map (bfd *abfd,
3563 struct bfd_link_info *info,
3564 bfd_boolean remove_empty_load)
3566 struct elf_segment_map **m;
3567 const struct elf_backend_data *bed;
3569 /* The placement algorithm assumes that non allocated sections are
3570 not in PT_LOAD segments. We ensure this here by removing such
3571 sections from the segment map. We also remove excluded
3572 sections. Finally, any PT_LOAD segment without sections is
3573 removed. */
3574 m = &elf_tdata (abfd)->segment_map;
3575 while (*m)
3577 unsigned int i, new_count;
3579 for (new_count = 0, i = 0; i < (*m)->count; i++)
3581 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
3582 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
3583 || (*m)->p_type != PT_LOAD))
3585 (*m)->sections[new_count] = (*m)->sections[i];
3586 new_count++;
3589 (*m)->count = new_count;
3591 if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0)
3592 *m = (*m)->next;
3593 else
3594 m = &(*m)->next;
3597 bed = get_elf_backend_data (abfd);
3598 if (bed->elf_backend_modify_segment_map != NULL)
3600 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
3601 return FALSE;
3604 return TRUE;
3607 /* Set up a mapping from BFD sections to program segments. */
3609 bfd_boolean
3610 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
3612 unsigned int count;
3613 struct elf_segment_map *m;
3614 asection **sections = NULL;
3615 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3616 bfd_boolean no_user_phdrs;
3618 no_user_phdrs = elf_tdata (abfd)->segment_map == NULL;
3619 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
3621 asection *s;
3622 unsigned int i;
3623 struct elf_segment_map *mfirst;
3624 struct elf_segment_map **pm;
3625 asection *last_hdr;
3626 bfd_vma last_size;
3627 unsigned int phdr_index;
3628 bfd_vma maxpagesize;
3629 asection **hdrpp;
3630 bfd_boolean phdr_in_segment = TRUE;
3631 bfd_boolean writable;
3632 int tls_count = 0;
3633 asection *first_tls = NULL;
3634 asection *dynsec, *eh_frame_hdr;
3635 bfd_size_type amt;
3637 /* Select the allocated sections, and sort them. */
3639 sections = bfd_malloc2 (bfd_count_sections (abfd), sizeof (asection *));
3640 if (sections == NULL)
3641 goto error_return;
3643 i = 0;
3644 for (s = abfd->sections; s != NULL; s = s->next)
3646 if ((s->flags & SEC_ALLOC) != 0)
3648 sections[i] = s;
3649 ++i;
3652 BFD_ASSERT (i <= bfd_count_sections (abfd));
3653 count = i;
3655 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3657 /* Build the mapping. */
3659 mfirst = NULL;
3660 pm = &mfirst;
3662 /* If we have a .interp section, then create a PT_PHDR segment for
3663 the program headers and a PT_INTERP segment for the .interp
3664 section. */
3665 s = bfd_get_section_by_name (abfd, ".interp");
3666 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3668 amt = sizeof (struct elf_segment_map);
3669 m = bfd_zalloc (abfd, amt);
3670 if (m == NULL)
3671 goto error_return;
3672 m->next = NULL;
3673 m->p_type = PT_PHDR;
3674 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3675 m->p_flags = PF_R | PF_X;
3676 m->p_flags_valid = 1;
3677 m->includes_phdrs = 1;
3679 *pm = m;
3680 pm = &m->next;
3682 amt = sizeof (struct elf_segment_map);
3683 m = bfd_zalloc (abfd, amt);
3684 if (m == NULL)
3685 goto error_return;
3686 m->next = NULL;
3687 m->p_type = PT_INTERP;
3688 m->count = 1;
3689 m->sections[0] = s;
3691 *pm = m;
3692 pm = &m->next;
3695 /* Look through the sections. We put sections in the same program
3696 segment when the start of the second section can be placed within
3697 a few bytes of the end of the first section. */
3698 last_hdr = NULL;
3699 last_size = 0;
3700 phdr_index = 0;
3701 maxpagesize = bed->maxpagesize;
3702 writable = FALSE;
3703 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3704 if (dynsec != NULL
3705 && (dynsec->flags & SEC_LOAD) == 0)
3706 dynsec = NULL;
3708 /* Deal with -Ttext or something similar such that the first section
3709 is not adjacent to the program headers. This is an
3710 approximation, since at this point we don't know exactly how many
3711 program headers we will need. */
3712 if (count > 0)
3714 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
3716 if (phdr_size == (bfd_size_type) -1)
3717 phdr_size = get_program_header_size (abfd, info);
3718 if ((abfd->flags & D_PAGED) == 0
3719 || sections[0]->lma < phdr_size
3720 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3721 phdr_in_segment = FALSE;
3724 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3726 asection *hdr;
3727 bfd_boolean new_segment;
3729 hdr = *hdrpp;
3731 /* See if this section and the last one will fit in the same
3732 segment. */
3734 if (last_hdr == NULL)
3736 /* If we don't have a segment yet, then we don't need a new
3737 one (we build the last one after this loop). */
3738 new_segment = FALSE;
3740 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3742 /* If this section has a different relation between the
3743 virtual address and the load address, then we need a new
3744 segment. */
3745 new_segment = TRUE;
3747 /* In the next test we have to be careful when last_hdr->lma is close
3748 to the end of the address space. If the aligned address wraps
3749 around to the start of the address space, then there are no more
3750 pages left in memory and it is OK to assume that the current
3751 section can be included in the current segment. */
3752 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
3753 > last_hdr->lma)
3754 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
3755 <= hdr->lma))
3757 /* If putting this section in this segment would force us to
3758 skip a page in the segment, then we need a new segment. */
3759 new_segment = TRUE;
3761 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3762 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
3764 /* We don't want to put a loadable section after a
3765 nonloadable section in the same segment.
3766 Consider .tbss sections as loadable for this purpose. */
3767 new_segment = TRUE;
3769 else if ((abfd->flags & D_PAGED) == 0)
3771 /* If the file is not demand paged, which means that we
3772 don't require the sections to be correctly aligned in the
3773 file, then there is no other reason for a new segment. */
3774 new_segment = FALSE;
3776 else if (! writable
3777 && (hdr->flags & SEC_READONLY) == 0
3778 && (((last_hdr->lma + last_size - 1)
3779 & ~(maxpagesize - 1))
3780 != (hdr->lma & ~(maxpagesize - 1))))
3782 /* We don't want to put a writable section in a read only
3783 segment, unless they are on the same page in memory
3784 anyhow. We already know that the last section does not
3785 bring us past the current section on the page, so the
3786 only case in which the new section is not on the same
3787 page as the previous section is when the previous section
3788 ends precisely on a page boundary. */
3789 new_segment = TRUE;
3791 else
3793 /* Otherwise, we can use the same segment. */
3794 new_segment = FALSE;
3797 /* Allow interested parties a chance to override our decision. */
3798 if (last_hdr != NULL
3799 && info != NULL
3800 && info->callbacks->override_segment_assignment != NULL)
3801 new_segment
3802 = info->callbacks->override_segment_assignment (info, abfd, hdr,
3803 last_hdr,
3804 new_segment);
3806 if (! new_segment)
3808 if ((hdr->flags & SEC_READONLY) == 0)
3809 writable = TRUE;
3810 last_hdr = hdr;
3811 /* .tbss sections effectively have zero size. */
3812 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
3813 != SEC_THREAD_LOCAL)
3814 last_size = hdr->size;
3815 else
3816 last_size = 0;
3817 continue;
3820 /* We need a new program segment. We must create a new program
3821 header holding all the sections from phdr_index until hdr. */
3823 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3824 if (m == NULL)
3825 goto error_return;
3827 *pm = m;
3828 pm = &m->next;
3830 if ((hdr->flags & SEC_READONLY) == 0)
3831 writable = TRUE;
3832 else
3833 writable = FALSE;
3835 last_hdr = hdr;
3836 /* .tbss sections effectively have zero size. */
3837 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3838 last_size = hdr->size;
3839 else
3840 last_size = 0;
3841 phdr_index = i;
3842 phdr_in_segment = FALSE;
3845 /* Create a final PT_LOAD program segment. */
3846 if (last_hdr != NULL)
3848 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3849 if (m == NULL)
3850 goto error_return;
3852 *pm = m;
3853 pm = &m->next;
3856 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3857 if (dynsec != NULL)
3859 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
3860 if (m == NULL)
3861 goto error_return;
3862 *pm = m;
3863 pm = &m->next;
3866 /* For each batch of consecutive loadable .note sections,
3867 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
3868 because if we link together nonloadable .note sections and
3869 loadable .note sections, we will generate two .note sections
3870 in the output file. FIXME: Using names for section types is
3871 bogus anyhow. */
3872 for (s = abfd->sections; s != NULL; s = s->next)
3874 if ((s->flags & SEC_LOAD) != 0
3875 && CONST_STRNEQ (s->name, ".note"))
3877 asection *s2;
3878 unsigned count = 1;
3879 amt = sizeof (struct elf_segment_map);
3880 if (s->alignment_power == 2)
3881 for (s2 = s; s2->next != NULL; s2 = s2->next)
3883 if (s2->next->alignment_power == 2
3884 && (s2->next->flags & SEC_LOAD) != 0
3885 && CONST_STRNEQ (s2->next->name, ".note")
3886 && align_power (s2->vma + s2->size, 2)
3887 == s2->next->vma)
3888 count++;
3889 else
3890 break;
3892 amt += (count - 1) * sizeof (asection *);
3893 m = bfd_zalloc (abfd, amt);
3894 if (m == NULL)
3895 goto error_return;
3896 m->next = NULL;
3897 m->p_type = PT_NOTE;
3898 m->count = count;
3899 while (count > 1)
3901 m->sections[m->count - count--] = s;
3902 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3903 s = s->next;
3905 m->sections[m->count - 1] = s;
3906 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3907 *pm = m;
3908 pm = &m->next;
3910 if (s->flags & SEC_THREAD_LOCAL)
3912 if (! tls_count)
3913 first_tls = s;
3914 tls_count++;
3918 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3919 if (tls_count > 0)
3921 int i;
3923 amt = sizeof (struct elf_segment_map);
3924 amt += (tls_count - 1) * sizeof (asection *);
3925 m = bfd_zalloc (abfd, amt);
3926 if (m == NULL)
3927 goto error_return;
3928 m->next = NULL;
3929 m->p_type = PT_TLS;
3930 m->count = tls_count;
3931 /* Mandated PF_R. */
3932 m->p_flags = PF_R;
3933 m->p_flags_valid = 1;
3934 for (i = 0; i < tls_count; ++i)
3936 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3937 m->sections[i] = first_tls;
3938 first_tls = first_tls->next;
3941 *pm = m;
3942 pm = &m->next;
3945 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3946 segment. */
3947 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
3948 if (eh_frame_hdr != NULL
3949 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
3951 amt = sizeof (struct elf_segment_map);
3952 m = bfd_zalloc (abfd, amt);
3953 if (m == NULL)
3954 goto error_return;
3955 m->next = NULL;
3956 m->p_type = PT_GNU_EH_FRAME;
3957 m->count = 1;
3958 m->sections[0] = eh_frame_hdr->output_section;
3960 *pm = m;
3961 pm = &m->next;
3964 if (elf_tdata (abfd)->stack_flags)
3966 amt = sizeof (struct elf_segment_map);
3967 m = bfd_zalloc (abfd, amt);
3968 if (m == NULL)
3969 goto error_return;
3970 m->next = NULL;
3971 m->p_type = PT_GNU_STACK;
3972 m->p_flags = elf_tdata (abfd)->stack_flags;
3973 m->p_flags_valid = 1;
3975 *pm = m;
3976 pm = &m->next;
3979 if (info != NULL && info->relro)
3981 for (m = mfirst; m != NULL; m = m->next)
3983 if (m->p_type == PT_LOAD)
3985 asection *last = m->sections[m->count - 1];
3986 bfd_vma vaddr = m->sections[0]->vma;
3987 bfd_vma filesz = last->vma - vaddr + last->size;
3989 if (vaddr < info->relro_end
3990 && vaddr >= info->relro_start
3991 && (vaddr + filesz) >= info->relro_end)
3992 break;
3996 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
3997 if (m != NULL)
3999 amt = sizeof (struct elf_segment_map);
4000 m = bfd_zalloc (abfd, amt);
4001 if (m == NULL)
4002 goto error_return;
4003 m->next = NULL;
4004 m->p_type = PT_GNU_RELRO;
4005 m->p_flags = PF_R;
4006 m->p_flags_valid = 1;
4008 *pm = m;
4009 pm = &m->next;
4013 free (sections);
4014 elf_tdata (abfd)->segment_map = mfirst;
4017 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
4018 return FALSE;
4020 for (count = 0, m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4021 ++count;
4022 elf_tdata (abfd)->program_header_size = count * bed->s->sizeof_phdr;
4024 return TRUE;
4026 error_return:
4027 if (sections != NULL)
4028 free (sections);
4029 return FALSE;
4032 /* Sort sections by address. */
4034 static int
4035 elf_sort_sections (const void *arg1, const void *arg2)
4037 const asection *sec1 = *(const asection **) arg1;
4038 const asection *sec2 = *(const asection **) arg2;
4039 bfd_size_type size1, size2;
4041 /* Sort by LMA first, since this is the address used to
4042 place the section into a segment. */
4043 if (sec1->lma < sec2->lma)
4044 return -1;
4045 else if (sec1->lma > sec2->lma)
4046 return 1;
4048 /* Then sort by VMA. Normally the LMA and the VMA will be
4049 the same, and this will do nothing. */
4050 if (sec1->vma < sec2->vma)
4051 return -1;
4052 else if (sec1->vma > sec2->vma)
4053 return 1;
4055 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4057 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4059 if (TOEND (sec1))
4061 if (TOEND (sec2))
4063 /* If the indicies are the same, do not return 0
4064 here, but continue to try the next comparison. */
4065 if (sec1->target_index - sec2->target_index != 0)
4066 return sec1->target_index - sec2->target_index;
4068 else
4069 return 1;
4071 else if (TOEND (sec2))
4072 return -1;
4074 #undef TOEND
4076 /* Sort by size, to put zero sized sections
4077 before others at the same address. */
4079 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
4080 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
4082 if (size1 < size2)
4083 return -1;
4084 if (size1 > size2)
4085 return 1;
4087 return sec1->target_index - sec2->target_index;
4090 /* Ian Lance Taylor writes:
4092 We shouldn't be using % with a negative signed number. That's just
4093 not good. We have to make sure either that the number is not
4094 negative, or that the number has an unsigned type. When the types
4095 are all the same size they wind up as unsigned. When file_ptr is a
4096 larger signed type, the arithmetic winds up as signed long long,
4097 which is wrong.
4099 What we're trying to say here is something like ``increase OFF by
4100 the least amount that will cause it to be equal to the VMA modulo
4101 the page size.'' */
4102 /* In other words, something like:
4104 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4105 off_offset = off % bed->maxpagesize;
4106 if (vma_offset < off_offset)
4107 adjustment = vma_offset + bed->maxpagesize - off_offset;
4108 else
4109 adjustment = vma_offset - off_offset;
4111 which can can be collapsed into the expression below. */
4113 static file_ptr
4114 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
4116 return ((vma - off) % maxpagesize);
4119 static void
4120 print_segment_map (const struct elf_segment_map *m)
4122 unsigned int j;
4123 const char *pt = get_segment_type (m->p_type);
4124 char buf[32];
4126 if (pt == NULL)
4128 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
4129 sprintf (buf, "LOPROC+%7.7x",
4130 (unsigned int) (m->p_type - PT_LOPROC));
4131 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
4132 sprintf (buf, "LOOS+%7.7x",
4133 (unsigned int) (m->p_type - PT_LOOS));
4134 else
4135 snprintf (buf, sizeof (buf), "%8.8x",
4136 (unsigned int) m->p_type);
4137 pt = buf;
4139 fprintf (stderr, "%s:", pt);
4140 for (j = 0; j < m->count; j++)
4141 fprintf (stderr, " %s", m->sections [j]->name);
4142 putc ('\n',stderr);
4145 /* Assign file positions to the sections based on the mapping from
4146 sections to segments. This function also sets up some fields in
4147 the file header. */
4149 static bfd_boolean
4150 assign_file_positions_for_load_sections (bfd *abfd,
4151 struct bfd_link_info *link_info)
4153 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4154 struct elf_segment_map *m;
4155 Elf_Internal_Phdr *phdrs;
4156 Elf_Internal_Phdr *p;
4157 file_ptr off;
4158 bfd_size_type maxpagesize;
4159 unsigned int alloc;
4160 unsigned int i, j;
4162 if (link_info == NULL
4163 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
4164 return FALSE;
4166 alloc = 0;
4167 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4168 ++alloc;
4170 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
4171 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
4172 elf_elfheader (abfd)->e_phnum = alloc;
4174 if (elf_tdata (abfd)->program_header_size == (bfd_size_type) -1)
4175 elf_tdata (abfd)->program_header_size = alloc * bed->s->sizeof_phdr;
4176 else
4177 BFD_ASSERT (elf_tdata (abfd)->program_header_size
4178 >= alloc * bed->s->sizeof_phdr);
4180 if (alloc == 0)
4182 elf_tdata (abfd)->next_file_pos = bed->s->sizeof_ehdr;
4183 return TRUE;
4186 phdrs = bfd_alloc2 (abfd, alloc, sizeof (Elf_Internal_Phdr));
4187 elf_tdata (abfd)->phdr = phdrs;
4188 if (phdrs == NULL)
4189 return FALSE;
4191 maxpagesize = 1;
4192 if ((abfd->flags & D_PAGED) != 0)
4193 maxpagesize = bed->maxpagesize;
4195 off = bed->s->sizeof_ehdr;
4196 off += alloc * bed->s->sizeof_phdr;
4198 for (m = elf_tdata (abfd)->segment_map, p = phdrs, j = 0;
4199 m != NULL;
4200 m = m->next, p++, j++)
4202 asection **secpp;
4203 bfd_vma off_adjust;
4204 bfd_boolean no_contents;
4206 /* If elf_segment_map is not from map_sections_to_segments, the
4207 sections may not be correctly ordered. NOTE: sorting should
4208 not be done to the PT_NOTE section of a corefile, which may
4209 contain several pseudo-sections artificially created by bfd.
4210 Sorting these pseudo-sections breaks things badly. */
4211 if (m->count > 1
4212 && !(elf_elfheader (abfd)->e_type == ET_CORE
4213 && m->p_type == PT_NOTE))
4214 qsort (m->sections, (size_t) m->count, sizeof (asection *),
4215 elf_sort_sections);
4217 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4218 number of sections with contents contributing to both p_filesz
4219 and p_memsz, followed by a number of sections with no contents
4220 that just contribute to p_memsz. In this loop, OFF tracks next
4221 available file offset for PT_LOAD and PT_NOTE segments. */
4222 p->p_type = m->p_type;
4223 p->p_flags = m->p_flags;
4225 if (m->count == 0)
4226 p->p_vaddr = 0;
4227 else
4228 p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
4230 if (m->p_paddr_valid)
4231 p->p_paddr = m->p_paddr;
4232 else if (m->count == 0)
4233 p->p_paddr = 0;
4234 else
4235 p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
4237 if (p->p_type == PT_LOAD
4238 && (abfd->flags & D_PAGED) != 0)
4240 /* p_align in demand paged PT_LOAD segments effectively stores
4241 the maximum page size. When copying an executable with
4242 objcopy, we set m->p_align from the input file. Use this
4243 value for maxpagesize rather than bed->maxpagesize, which
4244 may be different. Note that we use maxpagesize for PT_TLS
4245 segment alignment later in this function, so we are relying
4246 on at least one PT_LOAD segment appearing before a PT_TLS
4247 segment. */
4248 if (m->p_align_valid)
4249 maxpagesize = m->p_align;
4251 p->p_align = maxpagesize;
4253 else if (m->p_align_valid)
4254 p->p_align = m->p_align;
4255 else if (m->count == 0)
4256 p->p_align = 1 << bed->s->log_file_align;
4257 else
4258 p->p_align = 0;
4260 no_contents = FALSE;
4261 off_adjust = 0;
4262 if (p->p_type == PT_LOAD
4263 && m->count > 0)
4265 bfd_size_type align;
4266 unsigned int align_power = 0;
4268 if (m->p_align_valid)
4269 align = p->p_align;
4270 else
4272 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4274 unsigned int secalign;
4276 secalign = bfd_get_section_alignment (abfd, *secpp);
4277 if (secalign > align_power)
4278 align_power = secalign;
4280 align = (bfd_size_type) 1 << align_power;
4281 if (align < maxpagesize)
4282 align = maxpagesize;
4285 for (i = 0; i < m->count; i++)
4286 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
4287 /* If we aren't making room for this section, then
4288 it must be SHT_NOBITS regardless of what we've
4289 set via struct bfd_elf_special_section. */
4290 elf_section_type (m->sections[i]) = SHT_NOBITS;
4292 /* Find out whether this segment contains any loadable
4293 sections. */
4294 no_contents = TRUE;
4295 for (i = 0; i < m->count; i++)
4296 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
4298 no_contents = FALSE;
4299 break;
4302 off_adjust = vma_page_aligned_bias (m->sections[0]->vma, off, align);
4303 off += off_adjust;
4304 if (no_contents)
4306 /* We shouldn't need to align the segment on disk since
4307 the segment doesn't need file space, but the gABI
4308 arguably requires the alignment and glibc ld.so
4309 checks it. So to comply with the alignment
4310 requirement but not waste file space, we adjust
4311 p_offset for just this segment. (OFF_ADJUST is
4312 subtracted from OFF later.) This may put p_offset
4313 past the end of file, but that shouldn't matter. */
4315 else
4316 off_adjust = 0;
4318 /* Make sure the .dynamic section is the first section in the
4319 PT_DYNAMIC segment. */
4320 else if (p->p_type == PT_DYNAMIC
4321 && m->count > 1
4322 && strcmp (m->sections[0]->name, ".dynamic") != 0)
4324 _bfd_error_handler
4325 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4326 abfd);
4327 bfd_set_error (bfd_error_bad_value);
4328 return FALSE;
4330 /* Set the note section type to SHT_NOTE. */
4331 else if (p->p_type == PT_NOTE)
4332 for (i = 0; i < m->count; i++)
4333 elf_section_type (m->sections[i]) = SHT_NOTE;
4335 p->p_offset = 0;
4336 p->p_filesz = 0;
4337 p->p_memsz = 0;
4339 if (m->includes_filehdr)
4341 if (!m->p_flags_valid)
4342 p->p_flags |= PF_R;
4343 p->p_filesz = bed->s->sizeof_ehdr;
4344 p->p_memsz = bed->s->sizeof_ehdr;
4345 if (m->count > 0)
4347 BFD_ASSERT (p->p_type == PT_LOAD);
4349 if (p->p_vaddr < (bfd_vma) off)
4351 (*_bfd_error_handler)
4352 (_("%B: Not enough room for program headers, try linking with -N"),
4353 abfd);
4354 bfd_set_error (bfd_error_bad_value);
4355 return FALSE;
4358 p->p_vaddr -= off;
4359 if (!m->p_paddr_valid)
4360 p->p_paddr -= off;
4364 if (m->includes_phdrs)
4366 if (!m->p_flags_valid)
4367 p->p_flags |= PF_R;
4369 if (!m->includes_filehdr)
4371 p->p_offset = bed->s->sizeof_ehdr;
4373 if (m->count > 0)
4375 BFD_ASSERT (p->p_type == PT_LOAD);
4376 p->p_vaddr -= off - p->p_offset;
4377 if (!m->p_paddr_valid)
4378 p->p_paddr -= off - p->p_offset;
4382 p->p_filesz += alloc * bed->s->sizeof_phdr;
4383 p->p_memsz += alloc * bed->s->sizeof_phdr;
4386 if (p->p_type == PT_LOAD
4387 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4389 if (!m->includes_filehdr && !m->includes_phdrs)
4390 p->p_offset = off;
4391 else
4393 file_ptr adjust;
4395 adjust = off - (p->p_offset + p->p_filesz);
4396 if (!no_contents)
4397 p->p_filesz += adjust;
4398 p->p_memsz += adjust;
4402 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4403 maps. Set filepos for sections in PT_LOAD segments, and in
4404 core files, for sections in PT_NOTE segments.
4405 assign_file_positions_for_non_load_sections will set filepos
4406 for other sections and update p_filesz for other segments. */
4407 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4409 asection *sec;
4410 bfd_size_type align;
4411 Elf_Internal_Shdr *this_hdr;
4413 sec = *secpp;
4414 this_hdr = &elf_section_data (sec)->this_hdr;
4415 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
4417 if ((p->p_type == PT_LOAD
4418 || p->p_type == PT_TLS)
4419 && (this_hdr->sh_type != SHT_NOBITS
4420 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
4421 && ((this_hdr->sh_flags & SHF_TLS) == 0
4422 || p->p_type == PT_TLS))))
4424 bfd_signed_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
4426 if (adjust < 0)
4428 (*_bfd_error_handler)
4429 (_("%B: section %A vma 0x%lx overlaps previous sections"),
4430 abfd, sec, (unsigned long) sec->vma);
4431 adjust = 0;
4433 p->p_memsz += adjust;
4435 if (this_hdr->sh_type != SHT_NOBITS)
4437 off += adjust;
4438 p->p_filesz += adjust;
4442 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4444 /* The section at i == 0 is the one that actually contains
4445 everything. */
4446 if (i == 0)
4448 this_hdr->sh_offset = sec->filepos = off;
4449 off += this_hdr->sh_size;
4450 p->p_filesz = this_hdr->sh_size;
4451 p->p_memsz = 0;
4452 p->p_align = 1;
4454 else
4456 /* The rest are fake sections that shouldn't be written. */
4457 sec->filepos = 0;
4458 sec->size = 0;
4459 sec->flags = 0;
4460 continue;
4463 else
4465 if (p->p_type == PT_LOAD)
4467 this_hdr->sh_offset = sec->filepos = off;
4468 if (this_hdr->sh_type != SHT_NOBITS)
4469 off += this_hdr->sh_size;
4472 if (this_hdr->sh_type != SHT_NOBITS)
4474 p->p_filesz += this_hdr->sh_size;
4475 /* A load section without SHF_ALLOC is something like
4476 a note section in a PT_NOTE segment. These take
4477 file space but are not loaded into memory. */
4478 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4479 p->p_memsz += this_hdr->sh_size;
4481 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4483 if (p->p_type == PT_TLS)
4484 p->p_memsz += this_hdr->sh_size;
4486 /* .tbss is special. It doesn't contribute to p_memsz of
4487 normal segments. */
4488 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
4489 p->p_memsz += this_hdr->sh_size;
4492 if (align > p->p_align
4493 && !m->p_align_valid
4494 && (p->p_type != PT_LOAD
4495 || (abfd->flags & D_PAGED) == 0))
4496 p->p_align = align;
4499 if (!m->p_flags_valid)
4501 p->p_flags |= PF_R;
4502 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
4503 p->p_flags |= PF_X;
4504 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
4505 p->p_flags |= PF_W;
4508 off -= off_adjust;
4510 /* Check that all sections are in a PT_LOAD segment.
4511 Don't check funky gdb generated core files. */
4512 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
4513 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4515 Elf_Internal_Shdr *this_hdr;
4516 asection *sec;
4518 sec = *secpp;
4519 this_hdr = &(elf_section_data(sec)->this_hdr);
4520 if (this_hdr->sh_size != 0
4521 && !ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, p))
4523 (*_bfd_error_handler)
4524 (_("%B: section `%A' can't be allocated in segment %d"),
4525 abfd, sec, j);
4526 print_segment_map (m);
4527 bfd_set_error (bfd_error_bad_value);
4528 return FALSE;
4533 elf_tdata (abfd)->next_file_pos = off;
4534 return TRUE;
4537 /* Assign file positions for the other sections. */
4539 static bfd_boolean
4540 assign_file_positions_for_non_load_sections (bfd *abfd,
4541 struct bfd_link_info *link_info)
4543 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4544 Elf_Internal_Shdr **i_shdrpp;
4545 Elf_Internal_Shdr **hdrpp;
4546 Elf_Internal_Phdr *phdrs;
4547 Elf_Internal_Phdr *p;
4548 struct elf_segment_map *m;
4549 bfd_vma filehdr_vaddr, filehdr_paddr;
4550 bfd_vma phdrs_vaddr, phdrs_paddr;
4551 file_ptr off;
4552 unsigned int num_sec;
4553 unsigned int i;
4554 unsigned int count;
4556 i_shdrpp = elf_elfsections (abfd);
4557 num_sec = elf_numsections (abfd);
4558 off = elf_tdata (abfd)->next_file_pos;
4559 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4561 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4562 Elf_Internal_Shdr *hdr;
4564 hdr = *hdrpp;
4565 if (hdr->bfd_section != NULL
4566 && (hdr->bfd_section->filepos != 0
4567 || (hdr->sh_type == SHT_NOBITS
4568 && hdr->contents == NULL)))
4569 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
4570 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4572 if (hdr->sh_size != 0)
4573 ((*_bfd_error_handler)
4574 (_("%B: warning: allocated section `%s' not in segment"),
4575 abfd,
4576 (hdr->bfd_section == NULL
4577 ? "*unknown*"
4578 : hdr->bfd_section->name)));
4579 /* We don't need to page align empty sections. */
4580 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
4581 off += vma_page_aligned_bias (hdr->sh_addr, off,
4582 bed->maxpagesize);
4583 else
4584 off += vma_page_aligned_bias (hdr->sh_addr, off,
4585 hdr->sh_addralign);
4586 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4587 FALSE);
4589 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4590 && hdr->bfd_section == NULL)
4591 || hdr == i_shdrpp[tdata->symtab_section]
4592 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4593 || hdr == i_shdrpp[tdata->strtab_section])
4594 hdr->sh_offset = -1;
4595 else
4596 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4599 /* Now that we have set the section file positions, we can set up
4600 the file positions for the non PT_LOAD segments. */
4601 count = 0;
4602 filehdr_vaddr = 0;
4603 filehdr_paddr = 0;
4604 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
4605 phdrs_paddr = 0;
4606 phdrs = elf_tdata (abfd)->phdr;
4607 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4608 m != NULL;
4609 m = m->next, p++)
4611 ++count;
4612 if (p->p_type != PT_LOAD)
4613 continue;
4615 if (m->includes_filehdr)
4617 filehdr_vaddr = p->p_vaddr;
4618 filehdr_paddr = p->p_paddr;
4620 if (m->includes_phdrs)
4622 phdrs_vaddr = p->p_vaddr;
4623 phdrs_paddr = p->p_paddr;
4624 if (m->includes_filehdr)
4626 phdrs_vaddr += bed->s->sizeof_ehdr;
4627 phdrs_paddr += bed->s->sizeof_ehdr;
4632 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4633 m != NULL;
4634 m = m->next, p++)
4636 if (p->p_type == PT_GNU_RELRO)
4638 const Elf_Internal_Phdr *lp;
4640 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
4642 if (link_info != NULL)
4644 /* During linking the range of the RELRO segment is passed
4645 in link_info. */
4646 for (lp = phdrs; lp < phdrs + count; ++lp)
4648 if (lp->p_type == PT_LOAD
4649 && lp->p_vaddr >= link_info->relro_start
4650 && lp->p_vaddr < link_info->relro_end
4651 && lp->p_vaddr + lp->p_filesz >= link_info->relro_end)
4652 break;
4655 else
4657 /* Otherwise we are copying an executable or shared
4658 library, but we need to use the same linker logic. */
4659 for (lp = phdrs; lp < phdrs + count; ++lp)
4661 if (lp->p_type == PT_LOAD
4662 && lp->p_paddr == p->p_paddr)
4663 break;
4667 if (lp < phdrs + count)
4669 p->p_vaddr = lp->p_vaddr;
4670 p->p_paddr = lp->p_paddr;
4671 p->p_offset = lp->p_offset;
4672 if (link_info != NULL)
4673 p->p_filesz = link_info->relro_end - lp->p_vaddr;
4674 else if (m->p_size_valid)
4675 p->p_filesz = m->p_size;
4676 else
4677 abort ();
4678 p->p_memsz = p->p_filesz;
4679 p->p_align = 1;
4680 p->p_flags = (lp->p_flags & ~PF_W);
4682 else if (link_info != NULL)
4684 memset (p, 0, sizeof *p);
4685 p->p_type = PT_NULL;
4687 else
4688 abort ();
4690 else if (m->count != 0)
4692 if (p->p_type != PT_LOAD
4693 && (p->p_type != PT_NOTE
4694 || bfd_get_format (abfd) != bfd_core))
4696 Elf_Internal_Shdr *hdr;
4697 asection *sect;
4699 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
4701 sect = m->sections[m->count - 1];
4702 hdr = &elf_section_data (sect)->this_hdr;
4703 p->p_filesz = sect->filepos - m->sections[0]->filepos;
4704 if (hdr->sh_type != SHT_NOBITS)
4705 p->p_filesz += hdr->sh_size;
4706 p->p_offset = m->sections[0]->filepos;
4709 else if (m->includes_filehdr)
4711 p->p_vaddr = filehdr_vaddr;
4712 if (! m->p_paddr_valid)
4713 p->p_paddr = filehdr_paddr;
4715 else if (m->includes_phdrs)
4717 p->p_vaddr = phdrs_vaddr;
4718 if (! m->p_paddr_valid)
4719 p->p_paddr = phdrs_paddr;
4723 elf_tdata (abfd)->next_file_pos = off;
4725 return TRUE;
4728 /* Work out the file positions of all the sections. This is called by
4729 _bfd_elf_compute_section_file_positions. All the section sizes and
4730 VMAs must be known before this is called.
4732 Reloc sections come in two flavours: Those processed specially as
4733 "side-channel" data attached to a section to which they apply, and
4734 those that bfd doesn't process as relocations. The latter sort are
4735 stored in a normal bfd section by bfd_section_from_shdr. We don't
4736 consider the former sort here, unless they form part of the loadable
4737 image. Reloc sections not assigned here will be handled later by
4738 assign_file_positions_for_relocs.
4740 We also don't set the positions of the .symtab and .strtab here. */
4742 static bfd_boolean
4743 assign_file_positions_except_relocs (bfd *abfd,
4744 struct bfd_link_info *link_info)
4746 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4747 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4748 file_ptr off;
4749 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4751 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4752 && bfd_get_format (abfd) != bfd_core)
4754 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4755 unsigned int num_sec = elf_numsections (abfd);
4756 Elf_Internal_Shdr **hdrpp;
4757 unsigned int i;
4759 /* Start after the ELF header. */
4760 off = i_ehdrp->e_ehsize;
4762 /* We are not creating an executable, which means that we are
4763 not creating a program header, and that the actual order of
4764 the sections in the file is unimportant. */
4765 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4767 Elf_Internal_Shdr *hdr;
4769 hdr = *hdrpp;
4770 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4771 && hdr->bfd_section == NULL)
4772 || i == tdata->symtab_section
4773 || i == tdata->symtab_shndx_section
4774 || i == tdata->strtab_section)
4776 hdr->sh_offset = -1;
4778 else
4779 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4782 else
4784 unsigned int alloc;
4786 /* Assign file positions for the loaded sections based on the
4787 assignment of sections to segments. */
4788 if (!assign_file_positions_for_load_sections (abfd, link_info))
4789 return FALSE;
4791 /* And for non-load sections. */
4792 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
4793 return FALSE;
4795 if (bed->elf_backend_modify_program_headers != NULL)
4797 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
4798 return FALSE;
4801 /* Write out the program headers. */
4802 alloc = tdata->program_header_size / bed->s->sizeof_phdr;
4803 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4804 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
4805 return FALSE;
4807 off = tdata->next_file_pos;
4810 /* Place the section headers. */
4811 off = align_file_position (off, 1 << bed->s->log_file_align);
4812 i_ehdrp->e_shoff = off;
4813 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4815 tdata->next_file_pos = off;
4817 return TRUE;
4820 static bfd_boolean
4821 prep_headers (bfd *abfd)
4823 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
4824 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4825 struct elf_strtab_hash *shstrtab;
4826 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4828 i_ehdrp = elf_elfheader (abfd);
4830 shstrtab = _bfd_elf_strtab_init ();
4831 if (shstrtab == NULL)
4832 return FALSE;
4834 elf_shstrtab (abfd) = shstrtab;
4836 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4837 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4838 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4839 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4841 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4842 i_ehdrp->e_ident[EI_DATA] =
4843 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4844 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4846 if ((abfd->flags & DYNAMIC) != 0)
4847 i_ehdrp->e_type = ET_DYN;
4848 else if ((abfd->flags & EXEC_P) != 0)
4849 i_ehdrp->e_type = ET_EXEC;
4850 else if (bfd_get_format (abfd) == bfd_core)
4851 i_ehdrp->e_type = ET_CORE;
4852 else
4853 i_ehdrp->e_type = ET_REL;
4855 switch (bfd_get_arch (abfd))
4857 case bfd_arch_unknown:
4858 i_ehdrp->e_machine = EM_NONE;
4859 break;
4861 /* There used to be a long list of cases here, each one setting
4862 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4863 in the corresponding bfd definition. To avoid duplication,
4864 the switch was removed. Machines that need special handling
4865 can generally do it in elf_backend_final_write_processing(),
4866 unless they need the information earlier than the final write.
4867 Such need can generally be supplied by replacing the tests for
4868 e_machine with the conditions used to determine it. */
4869 default:
4870 i_ehdrp->e_machine = bed->elf_machine_code;
4873 i_ehdrp->e_version = bed->s->ev_current;
4874 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4876 /* No program header, for now. */
4877 i_ehdrp->e_phoff = 0;
4878 i_ehdrp->e_phentsize = 0;
4879 i_ehdrp->e_phnum = 0;
4881 /* Each bfd section is section header entry. */
4882 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4883 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4885 /* If we're building an executable, we'll need a program header table. */
4886 if (abfd->flags & EXEC_P)
4887 /* It all happens later. */
4889 else
4891 i_ehdrp->e_phentsize = 0;
4892 i_phdrp = 0;
4893 i_ehdrp->e_phoff = 0;
4896 elf_tdata (abfd)->symtab_hdr.sh_name =
4897 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
4898 elf_tdata (abfd)->strtab_hdr.sh_name =
4899 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
4900 elf_tdata (abfd)->shstrtab_hdr.sh_name =
4901 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
4902 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4903 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4904 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4905 return FALSE;
4907 return TRUE;
4910 /* Assign file positions for all the reloc sections which are not part
4911 of the loadable file image. */
4913 void
4914 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
4916 file_ptr off;
4917 unsigned int i, num_sec;
4918 Elf_Internal_Shdr **shdrpp;
4920 off = elf_tdata (abfd)->next_file_pos;
4922 num_sec = elf_numsections (abfd);
4923 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
4925 Elf_Internal_Shdr *shdrp;
4927 shdrp = *shdrpp;
4928 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4929 && shdrp->sh_offset == -1)
4930 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
4933 elf_tdata (abfd)->next_file_pos = off;
4936 bfd_boolean
4937 _bfd_elf_write_object_contents (bfd *abfd)
4939 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4940 Elf_Internal_Ehdr *i_ehdrp;
4941 Elf_Internal_Shdr **i_shdrp;
4942 bfd_boolean failed;
4943 unsigned int count, num_sec;
4945 if (! abfd->output_has_begun
4946 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
4947 return FALSE;
4949 i_shdrp = elf_elfsections (abfd);
4950 i_ehdrp = elf_elfheader (abfd);
4952 failed = FALSE;
4953 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4954 if (failed)
4955 return FALSE;
4957 _bfd_elf_assign_file_positions_for_relocs (abfd);
4959 /* After writing the headers, we need to write the sections too... */
4960 num_sec = elf_numsections (abfd);
4961 for (count = 1; count < num_sec; count++)
4963 if (bed->elf_backend_section_processing)
4964 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4965 if (i_shdrp[count]->contents)
4967 bfd_size_type amt = i_shdrp[count]->sh_size;
4969 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
4970 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
4971 return FALSE;
4975 /* Write out the section header names. */
4976 if (elf_shstrtab (abfd) != NULL
4977 && (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
4978 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
4979 return FALSE;
4981 if (bed->elf_backend_final_write_processing)
4982 (*bed->elf_backend_final_write_processing) (abfd,
4983 elf_tdata (abfd)->linker);
4985 if (!bed->s->write_shdrs_and_ehdr (abfd))
4986 return FALSE;
4988 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
4989 if (elf_tdata (abfd)->after_write_object_contents)
4990 return (*elf_tdata (abfd)->after_write_object_contents) (abfd);
4992 return TRUE;
4995 bfd_boolean
4996 _bfd_elf_write_corefile_contents (bfd *abfd)
4998 /* Hopefully this can be done just like an object file. */
4999 return _bfd_elf_write_object_contents (abfd);
5002 /* Given a section, search the header to find them. */
5004 unsigned int
5005 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
5007 const struct elf_backend_data *bed;
5008 unsigned int index;
5010 if (elf_section_data (asect) != NULL
5011 && elf_section_data (asect)->this_idx != 0)
5012 return elf_section_data (asect)->this_idx;
5014 if (bfd_is_abs_section (asect))
5015 index = SHN_ABS;
5016 else if (bfd_is_com_section (asect))
5017 index = SHN_COMMON;
5018 else if (bfd_is_und_section (asect))
5019 index = SHN_UNDEF;
5020 else
5021 index = SHN_BAD;
5023 bed = get_elf_backend_data (abfd);
5024 if (bed->elf_backend_section_from_bfd_section)
5026 int retval = index;
5028 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
5029 return retval;
5032 if (index == SHN_BAD)
5033 bfd_set_error (bfd_error_nonrepresentable_section);
5035 return index;
5038 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5039 on error. */
5042 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
5044 asymbol *asym_ptr = *asym_ptr_ptr;
5045 int idx;
5046 flagword flags = asym_ptr->flags;
5048 /* When gas creates relocations against local labels, it creates its
5049 own symbol for the section, but does put the symbol into the
5050 symbol chain, so udata is 0. When the linker is generating
5051 relocatable output, this section symbol may be for one of the
5052 input sections rather than the output section. */
5053 if (asym_ptr->udata.i == 0
5054 && (flags & BSF_SECTION_SYM)
5055 && asym_ptr->section)
5057 asection *sec;
5058 int indx;
5060 sec = asym_ptr->section;
5061 if (sec->owner != abfd && sec->output_section != NULL)
5062 sec = sec->output_section;
5063 if (sec->owner == abfd
5064 && (indx = sec->index) < elf_num_section_syms (abfd)
5065 && elf_section_syms (abfd)[indx] != NULL)
5066 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
5069 idx = asym_ptr->udata.i;
5071 if (idx == 0)
5073 /* This case can occur when using --strip-symbol on a symbol
5074 which is used in a relocation entry. */
5075 (*_bfd_error_handler)
5076 (_("%B: symbol `%s' required but not present"),
5077 abfd, bfd_asymbol_name (asym_ptr));
5078 bfd_set_error (bfd_error_no_symbols);
5079 return -1;
5082 #if DEBUG & 4
5084 fprintf (stderr,
5085 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
5086 (long) asym_ptr, asym_ptr->name, idx, flags,
5087 elf_symbol_flags (flags));
5088 fflush (stderr);
5090 #endif
5092 return idx;
5095 /* Rewrite program header information. */
5097 static bfd_boolean
5098 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
5100 Elf_Internal_Ehdr *iehdr;
5101 struct elf_segment_map *map;
5102 struct elf_segment_map *map_first;
5103 struct elf_segment_map **pointer_to_map;
5104 Elf_Internal_Phdr *segment;
5105 asection *section;
5106 unsigned int i;
5107 unsigned int num_segments;
5108 bfd_boolean phdr_included = FALSE;
5109 bfd_boolean p_paddr_valid;
5110 bfd_vma maxpagesize;
5111 struct elf_segment_map *phdr_adjust_seg = NULL;
5112 unsigned int phdr_adjust_num = 0;
5113 const struct elf_backend_data *bed;
5115 bed = get_elf_backend_data (ibfd);
5116 iehdr = elf_elfheader (ibfd);
5118 map_first = NULL;
5119 pointer_to_map = &map_first;
5121 num_segments = elf_elfheader (ibfd)->e_phnum;
5122 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
5124 /* Returns the end address of the segment + 1. */
5125 #define SEGMENT_END(segment, start) \
5126 (start + (segment->p_memsz > segment->p_filesz \
5127 ? segment->p_memsz : segment->p_filesz))
5129 #define SECTION_SIZE(section, segment) \
5130 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5131 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5132 ? section->size : 0)
5134 /* Returns TRUE if the given section is contained within
5135 the given segment. VMA addresses are compared. */
5136 #define IS_CONTAINED_BY_VMA(section, segment) \
5137 (section->vma >= segment->p_vaddr \
5138 && (section->vma + SECTION_SIZE (section, segment) \
5139 <= (SEGMENT_END (segment, segment->p_vaddr))))
5141 /* Returns TRUE if the given section is contained within
5142 the given segment. LMA addresses are compared. */
5143 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5144 (section->lma >= base \
5145 && (section->lma + SECTION_SIZE (section, segment) \
5146 <= SEGMENT_END (segment, base)))
5148 /* Handle PT_NOTE segment. */
5149 #define IS_NOTE(p, s) \
5150 (p->p_type == PT_NOTE \
5151 && elf_section_type (s) == SHT_NOTE \
5152 && (bfd_vma) s->filepos >= p->p_offset \
5153 && ((bfd_vma) s->filepos + s->size \
5154 <= p->p_offset + p->p_filesz))
5156 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5157 etc. */
5158 #define IS_COREFILE_NOTE(p, s) \
5159 (IS_NOTE (p, s) \
5160 && bfd_get_format (ibfd) == bfd_core \
5161 && s->vma == 0 \
5162 && s->lma == 0)
5164 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5165 linker, which generates a PT_INTERP section with p_vaddr and
5166 p_memsz set to 0. */
5167 #define IS_SOLARIS_PT_INTERP(p, s) \
5168 (p->p_vaddr == 0 \
5169 && p->p_paddr == 0 \
5170 && p->p_memsz == 0 \
5171 && p->p_filesz > 0 \
5172 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5173 && s->size > 0 \
5174 && (bfd_vma) s->filepos >= p->p_offset \
5175 && ((bfd_vma) s->filepos + s->size \
5176 <= p->p_offset + p->p_filesz))
5178 /* Decide if the given section should be included in the given segment.
5179 A section will be included if:
5180 1. It is within the address space of the segment -- we use the LMA
5181 if that is set for the segment and the VMA otherwise,
5182 2. It is an allocated section or a NOTE section in a PT_NOTE
5183 segment.
5184 3. There is an output section associated with it,
5185 4. The section has not already been allocated to a previous segment.
5186 5. PT_GNU_STACK segments do not include any sections.
5187 6. PT_TLS segment includes only SHF_TLS sections.
5188 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5189 8. PT_DYNAMIC should not contain empty sections at the beginning
5190 (with the possible exception of .dynamic). */
5191 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5192 ((((segment->p_paddr \
5193 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5194 : IS_CONTAINED_BY_VMA (section, segment)) \
5195 && (section->flags & SEC_ALLOC) != 0) \
5196 || IS_NOTE (segment, section)) \
5197 && segment->p_type != PT_GNU_STACK \
5198 && (segment->p_type != PT_TLS \
5199 || (section->flags & SEC_THREAD_LOCAL)) \
5200 && (segment->p_type == PT_LOAD \
5201 || segment->p_type == PT_TLS \
5202 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5203 && (segment->p_type != PT_DYNAMIC \
5204 || SECTION_SIZE (section, segment) > 0 \
5205 || (segment->p_paddr \
5206 ? segment->p_paddr != section->lma \
5207 : segment->p_vaddr != section->vma) \
5208 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5209 == 0)) \
5210 && !section->segment_mark)
5212 /* If the output section of a section in the input segment is NULL,
5213 it is removed from the corresponding output segment. */
5214 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5215 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5216 && section->output_section != NULL)
5218 /* Returns TRUE iff seg1 starts after the end of seg2. */
5219 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5220 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5222 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5223 their VMA address ranges and their LMA address ranges overlap.
5224 It is possible to have overlapping VMA ranges without overlapping LMA
5225 ranges. RedBoot images for example can have both .data and .bss mapped
5226 to the same VMA range, but with the .data section mapped to a different
5227 LMA. */
5228 #define SEGMENT_OVERLAPS(seg1, seg2) \
5229 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5230 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5231 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5232 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5234 /* Initialise the segment mark field. */
5235 for (section = ibfd->sections; section != NULL; section = section->next)
5236 section->segment_mark = FALSE;
5238 /* The Solaris linker creates program headers in which all the
5239 p_paddr fields are zero. When we try to objcopy or strip such a
5240 file, we get confused. Check for this case, and if we find it
5241 don't set the p_paddr_valid fields. */
5242 p_paddr_valid = FALSE;
5243 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5244 i < num_segments;
5245 i++, segment++)
5246 if (segment->p_paddr != 0)
5248 p_paddr_valid = TRUE;
5249 break;
5252 /* Scan through the segments specified in the program header
5253 of the input BFD. For this first scan we look for overlaps
5254 in the loadable segments. These can be created by weird
5255 parameters to objcopy. Also, fix some solaris weirdness. */
5256 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5257 i < num_segments;
5258 i++, segment++)
5260 unsigned int j;
5261 Elf_Internal_Phdr *segment2;
5263 if (segment->p_type == PT_INTERP)
5264 for (section = ibfd->sections; section; section = section->next)
5265 if (IS_SOLARIS_PT_INTERP (segment, section))
5267 /* Mininal change so that the normal section to segment
5268 assignment code will work. */
5269 segment->p_vaddr = section->vma;
5270 break;
5273 if (segment->p_type != PT_LOAD)
5275 /* Remove PT_GNU_RELRO segment. */
5276 if (segment->p_type == PT_GNU_RELRO)
5277 segment->p_type = PT_NULL;
5278 continue;
5281 /* Determine if this segment overlaps any previous segments. */
5282 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
5284 bfd_signed_vma extra_length;
5286 if (segment2->p_type != PT_LOAD
5287 || !SEGMENT_OVERLAPS (segment, segment2))
5288 continue;
5290 /* Merge the two segments together. */
5291 if (segment2->p_vaddr < segment->p_vaddr)
5293 /* Extend SEGMENT2 to include SEGMENT and then delete
5294 SEGMENT. */
5295 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
5296 - SEGMENT_END (segment2, segment2->p_vaddr));
5298 if (extra_length > 0)
5300 segment2->p_memsz += extra_length;
5301 segment2->p_filesz += extra_length;
5304 segment->p_type = PT_NULL;
5306 /* Since we have deleted P we must restart the outer loop. */
5307 i = 0;
5308 segment = elf_tdata (ibfd)->phdr;
5309 break;
5311 else
5313 /* Extend SEGMENT to include SEGMENT2 and then delete
5314 SEGMENT2. */
5315 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
5316 - SEGMENT_END (segment, segment->p_vaddr));
5318 if (extra_length > 0)
5320 segment->p_memsz += extra_length;
5321 segment->p_filesz += extra_length;
5324 segment2->p_type = PT_NULL;
5329 /* The second scan attempts to assign sections to segments. */
5330 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5331 i < num_segments;
5332 i++, segment++)
5334 unsigned int section_count;
5335 asection **sections;
5336 asection *output_section;
5337 unsigned int isec;
5338 bfd_vma matching_lma;
5339 bfd_vma suggested_lma;
5340 unsigned int j;
5341 bfd_size_type amt;
5342 asection *first_section;
5343 bfd_boolean first_matching_lma;
5344 bfd_boolean first_suggested_lma;
5346 if (segment->p_type == PT_NULL)
5347 continue;
5349 first_section = NULL;
5350 /* Compute how many sections might be placed into this segment. */
5351 for (section = ibfd->sections, section_count = 0;
5352 section != NULL;
5353 section = section->next)
5355 /* Find the first section in the input segment, which may be
5356 removed from the corresponding output segment. */
5357 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
5359 if (first_section == NULL)
5360 first_section = section;
5361 if (section->output_section != NULL)
5362 ++section_count;
5366 /* Allocate a segment map big enough to contain
5367 all of the sections we have selected. */
5368 amt = sizeof (struct elf_segment_map);
5369 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5370 map = bfd_zalloc (obfd, amt);
5371 if (map == NULL)
5372 return FALSE;
5374 /* Initialise the fields of the segment map. Default to
5375 using the physical address of the segment in the input BFD. */
5376 map->next = NULL;
5377 map->p_type = segment->p_type;
5378 map->p_flags = segment->p_flags;
5379 map->p_flags_valid = 1;
5381 /* If the first section in the input segment is removed, there is
5382 no need to preserve segment physical address in the corresponding
5383 output segment. */
5384 if (!first_section || first_section->output_section != NULL)
5386 map->p_paddr = segment->p_paddr;
5387 map->p_paddr_valid = p_paddr_valid;
5390 /* Determine if this segment contains the ELF file header
5391 and if it contains the program headers themselves. */
5392 map->includes_filehdr = (segment->p_offset == 0
5393 && segment->p_filesz >= iehdr->e_ehsize);
5394 map->includes_phdrs = 0;
5396 if (!phdr_included || segment->p_type != PT_LOAD)
5398 map->includes_phdrs =
5399 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5400 && (segment->p_offset + segment->p_filesz
5401 >= ((bfd_vma) iehdr->e_phoff
5402 + iehdr->e_phnum * iehdr->e_phentsize)));
5404 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5405 phdr_included = TRUE;
5408 if (section_count == 0)
5410 /* Special segments, such as the PT_PHDR segment, may contain
5411 no sections, but ordinary, loadable segments should contain
5412 something. They are allowed by the ELF spec however, so only
5413 a warning is produced. */
5414 if (segment->p_type == PT_LOAD)
5415 (*_bfd_error_handler) (_("%B: warning: Empty loadable segment"
5416 " detected, is this intentional ?\n"),
5417 ibfd);
5419 map->count = 0;
5420 *pointer_to_map = map;
5421 pointer_to_map = &map->next;
5423 continue;
5426 /* Now scan the sections in the input BFD again and attempt
5427 to add their corresponding output sections to the segment map.
5428 The problem here is how to handle an output section which has
5429 been moved (ie had its LMA changed). There are four possibilities:
5431 1. None of the sections have been moved.
5432 In this case we can continue to use the segment LMA from the
5433 input BFD.
5435 2. All of the sections have been moved by the same amount.
5436 In this case we can change the segment's LMA to match the LMA
5437 of the first section.
5439 3. Some of the sections have been moved, others have not.
5440 In this case those sections which have not been moved can be
5441 placed in the current segment which will have to have its size,
5442 and possibly its LMA changed, and a new segment or segments will
5443 have to be created to contain the other sections.
5445 4. The sections have been moved, but not by the same amount.
5446 In this case we can change the segment's LMA to match the LMA
5447 of the first section and we will have to create a new segment
5448 or segments to contain the other sections.
5450 In order to save time, we allocate an array to hold the section
5451 pointers that we are interested in. As these sections get assigned
5452 to a segment, they are removed from this array. */
5454 sections = bfd_malloc2 (section_count, sizeof (asection *));
5455 if (sections == NULL)
5456 return FALSE;
5458 /* Step One: Scan for segment vs section LMA conflicts.
5459 Also add the sections to the section array allocated above.
5460 Also add the sections to the current segment. In the common
5461 case, where the sections have not been moved, this means that
5462 we have completely filled the segment, and there is nothing
5463 more to do. */
5464 isec = 0;
5465 matching_lma = 0;
5466 suggested_lma = 0;
5467 first_matching_lma = TRUE;
5468 first_suggested_lma = TRUE;
5470 for (section = ibfd->sections;
5471 section != NULL;
5472 section = section->next)
5473 if (section == first_section)
5474 break;
5476 for (j = 0; section != NULL; section = section->next)
5478 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5480 output_section = section->output_section;
5482 sections[j++] = section;
5484 /* The Solaris native linker always sets p_paddr to 0.
5485 We try to catch that case here, and set it to the
5486 correct value. Note - some backends require that
5487 p_paddr be left as zero. */
5488 if (!p_paddr_valid
5489 && segment->p_vaddr != 0
5490 && !bed->want_p_paddr_set_to_zero
5491 && isec == 0
5492 && output_section->lma != 0
5493 && output_section->vma == (segment->p_vaddr
5494 + (map->includes_filehdr
5495 ? iehdr->e_ehsize
5496 : 0)
5497 + (map->includes_phdrs
5498 ? (iehdr->e_phnum
5499 * iehdr->e_phentsize)
5500 : 0)))
5501 map->p_paddr = segment->p_vaddr;
5503 /* Match up the physical address of the segment with the
5504 LMA address of the output section. */
5505 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5506 || IS_COREFILE_NOTE (segment, section)
5507 || (bed->want_p_paddr_set_to_zero
5508 && IS_CONTAINED_BY_VMA (output_section, segment)))
5510 if (first_matching_lma || output_section->lma < matching_lma)
5512 matching_lma = output_section->lma;
5513 first_matching_lma = FALSE;
5516 /* We assume that if the section fits within the segment
5517 then it does not overlap any other section within that
5518 segment. */
5519 map->sections[isec++] = output_section;
5521 else if (first_suggested_lma)
5523 suggested_lma = output_section->lma;
5524 first_suggested_lma = FALSE;
5527 if (j == section_count)
5528 break;
5532 BFD_ASSERT (j == section_count);
5534 /* Step Two: Adjust the physical address of the current segment,
5535 if necessary. */
5536 if (isec == section_count)
5538 /* All of the sections fitted within the segment as currently
5539 specified. This is the default case. Add the segment to
5540 the list of built segments and carry on to process the next
5541 program header in the input BFD. */
5542 map->count = section_count;
5543 *pointer_to_map = map;
5544 pointer_to_map = &map->next;
5546 if (p_paddr_valid
5547 && !bed->want_p_paddr_set_to_zero
5548 && matching_lma != map->p_paddr
5549 && !map->includes_filehdr
5550 && !map->includes_phdrs)
5551 /* There is some padding before the first section in the
5552 segment. So, we must account for that in the output
5553 segment's vma. */
5554 map->p_vaddr_offset = matching_lma - map->p_paddr;
5556 free (sections);
5557 continue;
5559 else
5561 if (!first_matching_lma)
5563 /* At least one section fits inside the current segment.
5564 Keep it, but modify its physical address to match the
5565 LMA of the first section that fitted. */
5566 map->p_paddr = matching_lma;
5568 else
5570 /* None of the sections fitted inside the current segment.
5571 Change the current segment's physical address to match
5572 the LMA of the first section. */
5573 map->p_paddr = suggested_lma;
5576 /* Offset the segment physical address from the lma
5577 to allow for space taken up by elf headers. */
5578 if (map->includes_filehdr)
5580 if (map->p_paddr >= iehdr->e_ehsize)
5581 map->p_paddr -= iehdr->e_ehsize;
5582 else
5584 map->includes_filehdr = FALSE;
5585 map->includes_phdrs = FALSE;
5589 if (map->includes_phdrs)
5591 if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize)
5593 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5595 /* iehdr->e_phnum is just an estimate of the number
5596 of program headers that we will need. Make a note
5597 here of the number we used and the segment we chose
5598 to hold these headers, so that we can adjust the
5599 offset when we know the correct value. */
5600 phdr_adjust_num = iehdr->e_phnum;
5601 phdr_adjust_seg = map;
5603 else
5604 map->includes_phdrs = FALSE;
5608 /* Step Three: Loop over the sections again, this time assigning
5609 those that fit to the current segment and removing them from the
5610 sections array; but making sure not to leave large gaps. Once all
5611 possible sections have been assigned to the current segment it is
5612 added to the list of built segments and if sections still remain
5613 to be assigned, a new segment is constructed before repeating
5614 the loop. */
5615 isec = 0;
5618 map->count = 0;
5619 suggested_lma = 0;
5620 first_suggested_lma = TRUE;
5622 /* Fill the current segment with sections that fit. */
5623 for (j = 0; j < section_count; j++)
5625 section = sections[j];
5627 if (section == NULL)
5628 continue;
5630 output_section = section->output_section;
5632 BFD_ASSERT (output_section != NULL);
5634 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5635 || IS_COREFILE_NOTE (segment, section))
5637 if (map->count == 0)
5639 /* If the first section in a segment does not start at
5640 the beginning of the segment, then something is
5641 wrong. */
5642 if (output_section->lma
5643 != (map->p_paddr
5644 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5645 + (map->includes_phdrs
5646 ? iehdr->e_phnum * iehdr->e_phentsize
5647 : 0)))
5648 abort ();
5650 else
5652 asection *prev_sec;
5654 prev_sec = map->sections[map->count - 1];
5656 /* If the gap between the end of the previous section
5657 and the start of this section is more than
5658 maxpagesize then we need to start a new segment. */
5659 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
5660 maxpagesize)
5661 < BFD_ALIGN (output_section->lma, maxpagesize))
5662 || (prev_sec->lma + prev_sec->size
5663 > output_section->lma))
5665 if (first_suggested_lma)
5667 suggested_lma = output_section->lma;
5668 first_suggested_lma = FALSE;
5671 continue;
5675 map->sections[map->count++] = output_section;
5676 ++isec;
5677 sections[j] = NULL;
5678 section->segment_mark = TRUE;
5680 else if (first_suggested_lma)
5682 suggested_lma = output_section->lma;
5683 first_suggested_lma = FALSE;
5687 BFD_ASSERT (map->count > 0);
5689 /* Add the current segment to the list of built segments. */
5690 *pointer_to_map = map;
5691 pointer_to_map = &map->next;
5693 if (isec < section_count)
5695 /* We still have not allocated all of the sections to
5696 segments. Create a new segment here, initialise it
5697 and carry on looping. */
5698 amt = sizeof (struct elf_segment_map);
5699 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5700 map = bfd_alloc (obfd, amt);
5701 if (map == NULL)
5703 free (sections);
5704 return FALSE;
5707 /* Initialise the fields of the segment map. Set the physical
5708 physical address to the LMA of the first section that has
5709 not yet been assigned. */
5710 map->next = NULL;
5711 map->p_type = segment->p_type;
5712 map->p_flags = segment->p_flags;
5713 map->p_flags_valid = 1;
5714 map->p_paddr = suggested_lma;
5715 map->p_paddr_valid = p_paddr_valid;
5716 map->includes_filehdr = 0;
5717 map->includes_phdrs = 0;
5720 while (isec < section_count);
5722 free (sections);
5725 elf_tdata (obfd)->segment_map = map_first;
5727 /* If we had to estimate the number of program headers that were
5728 going to be needed, then check our estimate now and adjust
5729 the offset if necessary. */
5730 if (phdr_adjust_seg != NULL)
5732 unsigned int count;
5734 for (count = 0, map = map_first; map != NULL; map = map->next)
5735 count++;
5737 if (count > phdr_adjust_num)
5738 phdr_adjust_seg->p_paddr
5739 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5742 #undef SEGMENT_END
5743 #undef SECTION_SIZE
5744 #undef IS_CONTAINED_BY_VMA
5745 #undef IS_CONTAINED_BY_LMA
5746 #undef IS_NOTE
5747 #undef IS_COREFILE_NOTE
5748 #undef IS_SOLARIS_PT_INTERP
5749 #undef IS_SECTION_IN_INPUT_SEGMENT
5750 #undef INCLUDE_SECTION_IN_SEGMENT
5751 #undef SEGMENT_AFTER_SEGMENT
5752 #undef SEGMENT_OVERLAPS
5753 return TRUE;
5756 /* Copy ELF program header information. */
5758 static bfd_boolean
5759 copy_elf_program_header (bfd *ibfd, bfd *obfd)
5761 Elf_Internal_Ehdr *iehdr;
5762 struct elf_segment_map *map;
5763 struct elf_segment_map *map_first;
5764 struct elf_segment_map **pointer_to_map;
5765 Elf_Internal_Phdr *segment;
5766 unsigned int i;
5767 unsigned int num_segments;
5768 bfd_boolean phdr_included = FALSE;
5769 bfd_boolean p_paddr_valid;
5771 iehdr = elf_elfheader (ibfd);
5773 map_first = NULL;
5774 pointer_to_map = &map_first;
5776 /* If all the segment p_paddr fields are zero, don't set
5777 map->p_paddr_valid. */
5778 p_paddr_valid = FALSE;
5779 num_segments = elf_elfheader (ibfd)->e_phnum;
5780 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5781 i < num_segments;
5782 i++, segment++)
5783 if (segment->p_paddr != 0)
5785 p_paddr_valid = TRUE;
5786 break;
5789 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5790 i < num_segments;
5791 i++, segment++)
5793 asection *section;
5794 unsigned int section_count;
5795 bfd_size_type amt;
5796 Elf_Internal_Shdr *this_hdr;
5797 asection *first_section = NULL;
5798 asection *lowest_section = NULL;
5800 /* Compute how many sections are in this segment. */
5801 for (section = ibfd->sections, section_count = 0;
5802 section != NULL;
5803 section = section->next)
5805 this_hdr = &(elf_section_data(section)->this_hdr);
5806 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5808 if (!first_section)
5809 first_section = lowest_section = section;
5810 if (section->lma < lowest_section->lma)
5811 lowest_section = section;
5812 section_count++;
5816 /* Allocate a segment map big enough to contain
5817 all of the sections we have selected. */
5818 amt = sizeof (struct elf_segment_map);
5819 if (section_count != 0)
5820 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5821 map = bfd_zalloc (obfd, amt);
5822 if (map == NULL)
5823 return FALSE;
5825 /* Initialize the fields of the output segment map with the
5826 input segment. */
5827 map->next = NULL;
5828 map->p_type = segment->p_type;
5829 map->p_flags = segment->p_flags;
5830 map->p_flags_valid = 1;
5831 map->p_paddr = segment->p_paddr;
5832 map->p_paddr_valid = p_paddr_valid;
5833 map->p_align = segment->p_align;
5834 map->p_align_valid = 1;
5835 map->p_vaddr_offset = 0;
5837 if (map->p_type == PT_GNU_RELRO
5838 && segment->p_filesz == segment->p_memsz)
5840 /* The PT_GNU_RELRO segment may contain the first a few
5841 bytes in the .got.plt section even if the whole .got.plt
5842 section isn't in the PT_GNU_RELRO segment. We won't
5843 change the size of the PT_GNU_RELRO segment. */
5844 map->p_size = segment->p_filesz;
5845 map->p_size_valid = 1;
5848 /* Determine if this segment contains the ELF file header
5849 and if it contains the program headers themselves. */
5850 map->includes_filehdr = (segment->p_offset == 0
5851 && segment->p_filesz >= iehdr->e_ehsize);
5853 map->includes_phdrs = 0;
5854 if (! phdr_included || segment->p_type != PT_LOAD)
5856 map->includes_phdrs =
5857 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5858 && (segment->p_offset + segment->p_filesz
5859 >= ((bfd_vma) iehdr->e_phoff
5860 + iehdr->e_phnum * iehdr->e_phentsize)));
5862 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5863 phdr_included = TRUE;
5866 if (!map->includes_phdrs
5867 && !map->includes_filehdr
5868 && map->p_paddr_valid)
5869 /* There is some other padding before the first section. */
5870 map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
5871 - segment->p_paddr);
5873 if (section_count != 0)
5875 unsigned int isec = 0;
5877 for (section = first_section;
5878 section != NULL;
5879 section = section->next)
5881 this_hdr = &(elf_section_data(section)->this_hdr);
5882 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5884 map->sections[isec++] = section->output_section;
5885 if (isec == section_count)
5886 break;
5891 map->count = section_count;
5892 *pointer_to_map = map;
5893 pointer_to_map = &map->next;
5896 elf_tdata (obfd)->segment_map = map_first;
5897 return TRUE;
5900 /* Copy private BFD data. This copies or rewrites ELF program header
5901 information. */
5903 static bfd_boolean
5904 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
5906 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5907 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5908 return TRUE;
5910 if (elf_tdata (ibfd)->phdr == NULL)
5911 return TRUE;
5913 if (ibfd->xvec == obfd->xvec)
5915 /* Check to see if any sections in the input BFD
5916 covered by ELF program header have changed. */
5917 Elf_Internal_Phdr *segment;
5918 asection *section, *osec;
5919 unsigned int i, num_segments;
5920 Elf_Internal_Shdr *this_hdr;
5921 const struct elf_backend_data *bed;
5923 bed = get_elf_backend_data (ibfd);
5925 /* Regenerate the segment map if p_paddr is set to 0. */
5926 if (bed->want_p_paddr_set_to_zero)
5927 goto rewrite;
5929 /* Initialize the segment mark field. */
5930 for (section = obfd->sections; section != NULL;
5931 section = section->next)
5932 section->segment_mark = FALSE;
5934 num_segments = elf_elfheader (ibfd)->e_phnum;
5935 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5936 i < num_segments;
5937 i++, segment++)
5939 /* PR binutils/3535. The Solaris linker always sets the p_paddr
5940 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
5941 which severly confuses things, so always regenerate the segment
5942 map in this case. */
5943 if (segment->p_paddr == 0
5944 && segment->p_memsz == 0
5945 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
5946 goto rewrite;
5948 for (section = ibfd->sections;
5949 section != NULL; section = section->next)
5951 /* We mark the output section so that we know it comes
5952 from the input BFD. */
5953 osec = section->output_section;
5954 if (osec)
5955 osec->segment_mark = TRUE;
5957 /* Check if this section is covered by the segment. */
5958 this_hdr = &(elf_section_data(section)->this_hdr);
5959 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5961 /* FIXME: Check if its output section is changed or
5962 removed. What else do we need to check? */
5963 if (osec == NULL
5964 || section->flags != osec->flags
5965 || section->lma != osec->lma
5966 || section->vma != osec->vma
5967 || section->size != osec->size
5968 || section->rawsize != osec->rawsize
5969 || section->alignment_power != osec->alignment_power)
5970 goto rewrite;
5975 /* Check to see if any output section do not come from the
5976 input BFD. */
5977 for (section = obfd->sections; section != NULL;
5978 section = section->next)
5980 if (section->segment_mark == FALSE)
5981 goto rewrite;
5982 else
5983 section->segment_mark = FALSE;
5986 return copy_elf_program_header (ibfd, obfd);
5989 rewrite:
5990 return rewrite_elf_program_header (ibfd, obfd);
5993 /* Initialize private output section information from input section. */
5995 bfd_boolean
5996 _bfd_elf_init_private_section_data (bfd *ibfd,
5997 asection *isec,
5998 bfd *obfd,
5999 asection *osec,
6000 struct bfd_link_info *link_info)
6003 Elf_Internal_Shdr *ihdr, *ohdr;
6004 bfd_boolean need_group = link_info == NULL || link_info->relocatable;
6006 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6007 || obfd->xvec->flavour != bfd_target_elf_flavour)
6008 return TRUE;
6010 /* Don't copy the output ELF section type from input if the
6011 output BFD section flags have been set to something different.
6012 elf_fake_sections will set ELF section type based on BFD
6013 section flags. */
6014 if (elf_section_type (osec) == SHT_NULL
6015 && (osec->flags == isec->flags || !osec->flags))
6016 elf_section_type (osec) = elf_section_type (isec);
6018 /* FIXME: Is this correct for all OS/PROC specific flags? */
6019 elf_section_flags (osec) |= (elf_section_flags (isec)
6020 & (SHF_MASKOS | SHF_MASKPROC));
6022 /* Set things up for objcopy and relocatable link. The output
6023 SHT_GROUP section will have its elf_next_in_group pointing back
6024 to the input group members. Ignore linker created group section.
6025 See elfNN_ia64_object_p in elfxx-ia64.c. */
6026 if (need_group)
6028 if (elf_sec_group (isec) == NULL
6029 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
6031 if (elf_section_flags (isec) & SHF_GROUP)
6032 elf_section_flags (osec) |= SHF_GROUP;
6033 elf_next_in_group (osec) = elf_next_in_group (isec);
6034 elf_section_data (osec)->group = elf_section_data (isec)->group;
6038 ihdr = &elf_section_data (isec)->this_hdr;
6040 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6041 don't use the output section of the linked-to section since it
6042 may be NULL at this point. */
6043 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
6045 ohdr = &elf_section_data (osec)->this_hdr;
6046 ohdr->sh_flags |= SHF_LINK_ORDER;
6047 elf_linked_to_section (osec) = elf_linked_to_section (isec);
6050 osec->use_rela_p = isec->use_rela_p;
6052 return TRUE;
6055 /* Copy private section information. This copies over the entsize
6056 field, and sometimes the info field. */
6058 bfd_boolean
6059 _bfd_elf_copy_private_section_data (bfd *ibfd,
6060 asection *isec,
6061 bfd *obfd,
6062 asection *osec)
6064 Elf_Internal_Shdr *ihdr, *ohdr;
6066 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6067 || obfd->xvec->flavour != bfd_target_elf_flavour)
6068 return TRUE;
6070 ihdr = &elf_section_data (isec)->this_hdr;
6071 ohdr = &elf_section_data (osec)->this_hdr;
6073 ohdr->sh_entsize = ihdr->sh_entsize;
6075 if (ihdr->sh_type == SHT_SYMTAB
6076 || ihdr->sh_type == SHT_DYNSYM
6077 || ihdr->sh_type == SHT_GNU_verneed
6078 || ihdr->sh_type == SHT_GNU_verdef)
6079 ohdr->sh_info = ihdr->sh_info;
6081 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
6082 NULL);
6085 /* Copy private header information. */
6087 bfd_boolean
6088 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
6090 asection *isec;
6092 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6093 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6094 return TRUE;
6096 /* Copy over private BFD data if it has not already been copied.
6097 This must be done here, rather than in the copy_private_bfd_data
6098 entry point, because the latter is called after the section
6099 contents have been set, which means that the program headers have
6100 already been worked out. */
6101 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
6103 if (! copy_private_bfd_data (ibfd, obfd))
6104 return FALSE;
6107 /* _bfd_elf_copy_private_section_data copied over the SHF_GROUP flag
6108 but this might be wrong if we deleted the group section. */
6109 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
6110 if (elf_section_type (isec) == SHT_GROUP
6111 && isec->output_section == NULL)
6113 asection *first = elf_next_in_group (isec);
6114 asection *s = first;
6115 while (s != NULL)
6117 if (s->output_section != NULL)
6119 elf_section_flags (s->output_section) &= ~SHF_GROUP;
6120 elf_group_name (s->output_section) = NULL;
6122 s = elf_next_in_group (s);
6123 if (s == first)
6124 break;
6128 return TRUE;
6131 /* Copy private symbol information. If this symbol is in a section
6132 which we did not map into a BFD section, try to map the section
6133 index correctly. We use special macro definitions for the mapped
6134 section indices; these definitions are interpreted by the
6135 swap_out_syms function. */
6137 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6138 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6139 #define MAP_STRTAB (SHN_HIOS + 3)
6140 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6141 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6143 bfd_boolean
6144 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
6145 asymbol *isymarg,
6146 bfd *obfd,
6147 asymbol *osymarg)
6149 elf_symbol_type *isym, *osym;
6151 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6152 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6153 return TRUE;
6155 isym = elf_symbol_from (ibfd, isymarg);
6156 osym = elf_symbol_from (obfd, osymarg);
6158 if (isym != NULL
6159 && isym->internal_elf_sym.st_shndx != 0
6160 && osym != NULL
6161 && bfd_is_abs_section (isym->symbol.section))
6163 unsigned int shndx;
6165 shndx = isym->internal_elf_sym.st_shndx;
6166 if (shndx == elf_onesymtab (ibfd))
6167 shndx = MAP_ONESYMTAB;
6168 else if (shndx == elf_dynsymtab (ibfd))
6169 shndx = MAP_DYNSYMTAB;
6170 else if (shndx == elf_tdata (ibfd)->strtab_section)
6171 shndx = MAP_STRTAB;
6172 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
6173 shndx = MAP_SHSTRTAB;
6174 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
6175 shndx = MAP_SYM_SHNDX;
6176 osym->internal_elf_sym.st_shndx = shndx;
6179 return TRUE;
6182 /* Swap out the symbols. */
6184 static bfd_boolean
6185 swap_out_syms (bfd *abfd,
6186 struct bfd_strtab_hash **sttp,
6187 int relocatable_p)
6189 const struct elf_backend_data *bed;
6190 int symcount;
6191 asymbol **syms;
6192 struct bfd_strtab_hash *stt;
6193 Elf_Internal_Shdr *symtab_hdr;
6194 Elf_Internal_Shdr *symtab_shndx_hdr;
6195 Elf_Internal_Shdr *symstrtab_hdr;
6196 bfd_byte *outbound_syms;
6197 bfd_byte *outbound_shndx;
6198 int idx;
6199 bfd_size_type amt;
6200 bfd_boolean name_local_sections;
6202 if (!elf_map_symbols (abfd))
6203 return FALSE;
6205 /* Dump out the symtabs. */
6206 stt = _bfd_elf_stringtab_init ();
6207 if (stt == NULL)
6208 return FALSE;
6210 bed = get_elf_backend_data (abfd);
6211 symcount = bfd_get_symcount (abfd);
6212 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6213 symtab_hdr->sh_type = SHT_SYMTAB;
6214 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
6215 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
6216 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
6217 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
6219 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
6220 symstrtab_hdr->sh_type = SHT_STRTAB;
6222 outbound_syms = bfd_alloc2 (abfd, 1 + symcount, bed->s->sizeof_sym);
6223 if (outbound_syms == NULL)
6225 _bfd_stringtab_free (stt);
6226 return FALSE;
6228 symtab_hdr->contents = outbound_syms;
6230 outbound_shndx = NULL;
6231 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
6232 if (symtab_shndx_hdr->sh_name != 0)
6234 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
6235 outbound_shndx = bfd_zalloc2 (abfd, 1 + symcount,
6236 sizeof (Elf_External_Sym_Shndx));
6237 if (outbound_shndx == NULL)
6239 _bfd_stringtab_free (stt);
6240 return FALSE;
6243 symtab_shndx_hdr->contents = outbound_shndx;
6244 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
6245 symtab_shndx_hdr->sh_size = amt;
6246 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
6247 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
6250 /* Now generate the data (for "contents"). */
6252 /* Fill in zeroth symbol and swap it out. */
6253 Elf_Internal_Sym sym;
6254 sym.st_name = 0;
6255 sym.st_value = 0;
6256 sym.st_size = 0;
6257 sym.st_info = 0;
6258 sym.st_other = 0;
6259 sym.st_shndx = SHN_UNDEF;
6260 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6261 outbound_syms += bed->s->sizeof_sym;
6262 if (outbound_shndx != NULL)
6263 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6266 name_local_sections
6267 = (bed->elf_backend_name_local_section_symbols
6268 && bed->elf_backend_name_local_section_symbols (abfd));
6270 syms = bfd_get_outsymbols (abfd);
6271 for (idx = 0; idx < symcount; idx++)
6273 Elf_Internal_Sym sym;
6274 bfd_vma value = syms[idx]->value;
6275 elf_symbol_type *type_ptr;
6276 flagword flags = syms[idx]->flags;
6277 int type;
6279 if (!name_local_sections
6280 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
6282 /* Local section symbols have no name. */
6283 sym.st_name = 0;
6285 else
6287 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
6288 syms[idx]->name,
6289 TRUE, FALSE);
6290 if (sym.st_name == (unsigned long) -1)
6292 _bfd_stringtab_free (stt);
6293 return FALSE;
6297 type_ptr = elf_symbol_from (abfd, syms[idx]);
6299 if ((flags & BSF_SECTION_SYM) == 0
6300 && bfd_is_com_section (syms[idx]->section))
6302 /* ELF common symbols put the alignment into the `value' field,
6303 and the size into the `size' field. This is backwards from
6304 how BFD handles it, so reverse it here. */
6305 sym.st_size = value;
6306 if (type_ptr == NULL
6307 || type_ptr->internal_elf_sym.st_value == 0)
6308 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
6309 else
6310 sym.st_value = type_ptr->internal_elf_sym.st_value;
6311 sym.st_shndx = _bfd_elf_section_from_bfd_section
6312 (abfd, syms[idx]->section);
6314 else
6316 asection *sec = syms[idx]->section;
6317 unsigned int shndx;
6319 if (sec->output_section)
6321 value += sec->output_offset;
6322 sec = sec->output_section;
6325 /* Don't add in the section vma for relocatable output. */
6326 if (! relocatable_p)
6327 value += sec->vma;
6328 sym.st_value = value;
6329 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
6331 if (bfd_is_abs_section (sec)
6332 && type_ptr != NULL
6333 && type_ptr->internal_elf_sym.st_shndx != 0)
6335 /* This symbol is in a real ELF section which we did
6336 not create as a BFD section. Undo the mapping done
6337 by copy_private_symbol_data. */
6338 shndx = type_ptr->internal_elf_sym.st_shndx;
6339 switch (shndx)
6341 case MAP_ONESYMTAB:
6342 shndx = elf_onesymtab (abfd);
6343 break;
6344 case MAP_DYNSYMTAB:
6345 shndx = elf_dynsymtab (abfd);
6346 break;
6347 case MAP_STRTAB:
6348 shndx = elf_tdata (abfd)->strtab_section;
6349 break;
6350 case MAP_SHSTRTAB:
6351 shndx = elf_tdata (abfd)->shstrtab_section;
6352 break;
6353 case MAP_SYM_SHNDX:
6354 shndx = elf_tdata (abfd)->symtab_shndx_section;
6355 break;
6356 default:
6357 break;
6360 else
6362 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
6364 if (shndx == SHN_BAD)
6366 asection *sec2;
6368 /* Writing this would be a hell of a lot easier if
6369 we had some decent documentation on bfd, and
6370 knew what to expect of the library, and what to
6371 demand of applications. For example, it
6372 appears that `objcopy' might not set the
6373 section of a symbol to be a section that is
6374 actually in the output file. */
6375 sec2 = bfd_get_section_by_name (abfd, sec->name);
6376 if (sec2 == NULL)
6378 _bfd_error_handler (_("\
6379 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6380 syms[idx]->name ? syms[idx]->name : "<Local sym>",
6381 sec->name);
6382 bfd_set_error (bfd_error_invalid_operation);
6383 _bfd_stringtab_free (stt);
6384 return FALSE;
6387 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
6388 BFD_ASSERT (shndx != SHN_BAD);
6392 sym.st_shndx = shndx;
6395 if ((flags & BSF_THREAD_LOCAL) != 0)
6396 type = STT_TLS;
6397 else if ((flags & BSF_FUNCTION) != 0)
6398 type = STT_FUNC;
6399 else if ((flags & BSF_OBJECT) != 0)
6400 type = STT_OBJECT;
6401 else if ((flags & BSF_RELC) != 0)
6402 type = STT_RELC;
6403 else if ((flags & BSF_SRELC) != 0)
6404 type = STT_SRELC;
6405 else
6406 type = STT_NOTYPE;
6408 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
6409 type = STT_TLS;
6411 /* Processor-specific types. */
6412 if (type_ptr != NULL
6413 && bed->elf_backend_get_symbol_type)
6414 type = ((*bed->elf_backend_get_symbol_type)
6415 (&type_ptr->internal_elf_sym, type));
6417 if (flags & BSF_SECTION_SYM)
6419 if (flags & BSF_GLOBAL)
6420 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6421 else
6422 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
6424 else if (bfd_is_com_section (syms[idx]->section))
6426 #ifdef USE_STT_COMMON
6427 if (type == STT_OBJECT)
6428 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON);
6429 else
6430 #else
6431 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
6432 #endif
6434 else if (bfd_is_und_section (syms[idx]->section))
6435 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
6436 ? STB_WEAK
6437 : STB_GLOBAL),
6438 type);
6439 else if (flags & BSF_FILE)
6440 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
6441 else
6443 int bind = STB_LOCAL;
6445 if (flags & BSF_LOCAL)
6446 bind = STB_LOCAL;
6447 else if (flags & BSF_WEAK)
6448 bind = STB_WEAK;
6449 else if (flags & BSF_GLOBAL)
6450 bind = STB_GLOBAL;
6452 sym.st_info = ELF_ST_INFO (bind, type);
6455 if (type_ptr != NULL)
6456 sym.st_other = type_ptr->internal_elf_sym.st_other;
6457 else
6458 sym.st_other = 0;
6460 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6461 outbound_syms += bed->s->sizeof_sym;
6462 if (outbound_shndx != NULL)
6463 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6466 *sttp = stt;
6467 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
6468 symstrtab_hdr->sh_type = SHT_STRTAB;
6470 symstrtab_hdr->sh_flags = 0;
6471 symstrtab_hdr->sh_addr = 0;
6472 symstrtab_hdr->sh_entsize = 0;
6473 symstrtab_hdr->sh_link = 0;
6474 symstrtab_hdr->sh_info = 0;
6475 symstrtab_hdr->sh_addralign = 1;
6477 return TRUE;
6480 /* Return the number of bytes required to hold the symtab vector.
6482 Note that we base it on the count plus 1, since we will null terminate
6483 the vector allocated based on this size. However, the ELF symbol table
6484 always has a dummy entry as symbol #0, so it ends up even. */
6486 long
6487 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
6489 long symcount;
6490 long symtab_size;
6491 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
6493 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6494 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6495 if (symcount > 0)
6496 symtab_size -= sizeof (asymbol *);
6498 return symtab_size;
6501 long
6502 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
6504 long symcount;
6505 long symtab_size;
6506 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
6508 if (elf_dynsymtab (abfd) == 0)
6510 bfd_set_error (bfd_error_invalid_operation);
6511 return -1;
6514 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6515 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6516 if (symcount > 0)
6517 symtab_size -= sizeof (asymbol *);
6519 return symtab_size;
6522 long
6523 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
6524 sec_ptr asect)
6526 return (asect->reloc_count + 1) * sizeof (arelent *);
6529 /* Canonicalize the relocs. */
6531 long
6532 _bfd_elf_canonicalize_reloc (bfd *abfd,
6533 sec_ptr section,
6534 arelent **relptr,
6535 asymbol **symbols)
6537 arelent *tblptr;
6538 unsigned int i;
6539 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6541 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
6542 return -1;
6544 tblptr = section->relocation;
6545 for (i = 0; i < section->reloc_count; i++)
6546 *relptr++ = tblptr++;
6548 *relptr = NULL;
6550 return section->reloc_count;
6553 long
6554 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
6556 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6557 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
6559 if (symcount >= 0)
6560 bfd_get_symcount (abfd) = symcount;
6561 return symcount;
6564 long
6565 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
6566 asymbol **allocation)
6568 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6569 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
6571 if (symcount >= 0)
6572 bfd_get_dynamic_symcount (abfd) = symcount;
6573 return symcount;
6576 /* Return the size required for the dynamic reloc entries. Any loadable
6577 section that was actually installed in the BFD, and has type SHT_REL
6578 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
6579 dynamic reloc section. */
6581 long
6582 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
6584 long ret;
6585 asection *s;
6587 if (elf_dynsymtab (abfd) == 0)
6589 bfd_set_error (bfd_error_invalid_operation);
6590 return -1;
6593 ret = sizeof (arelent *);
6594 for (s = abfd->sections; s != NULL; s = s->next)
6595 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6596 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6597 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6598 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
6599 * sizeof (arelent *));
6601 return ret;
6604 /* Canonicalize the dynamic relocation entries. Note that we return the
6605 dynamic relocations as a single block, although they are actually
6606 associated with particular sections; the interface, which was
6607 designed for SunOS style shared libraries, expects that there is only
6608 one set of dynamic relocs. Any loadable section that was actually
6609 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
6610 dynamic symbol table, is considered to be a dynamic reloc section. */
6612 long
6613 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
6614 arelent **storage,
6615 asymbol **syms)
6617 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
6618 asection *s;
6619 long ret;
6621 if (elf_dynsymtab (abfd) == 0)
6623 bfd_set_error (bfd_error_invalid_operation);
6624 return -1;
6627 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
6628 ret = 0;
6629 for (s = abfd->sections; s != NULL; s = s->next)
6631 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6632 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6633 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6635 arelent *p;
6636 long count, i;
6638 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
6639 return -1;
6640 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
6641 p = s->relocation;
6642 for (i = 0; i < count; i++)
6643 *storage++ = p++;
6644 ret += count;
6648 *storage = NULL;
6650 return ret;
6653 /* Read in the version information. */
6655 bfd_boolean
6656 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
6658 bfd_byte *contents = NULL;
6659 unsigned int freeidx = 0;
6661 if (elf_dynverref (abfd) != 0)
6663 Elf_Internal_Shdr *hdr;
6664 Elf_External_Verneed *everneed;
6665 Elf_Internal_Verneed *iverneed;
6666 unsigned int i;
6667 bfd_byte *contents_end;
6669 hdr = &elf_tdata (abfd)->dynverref_hdr;
6671 elf_tdata (abfd)->verref = bfd_zalloc2 (abfd, hdr->sh_info,
6672 sizeof (Elf_Internal_Verneed));
6673 if (elf_tdata (abfd)->verref == NULL)
6674 goto error_return;
6676 elf_tdata (abfd)->cverrefs = hdr->sh_info;
6678 contents = bfd_malloc (hdr->sh_size);
6679 if (contents == NULL)
6681 error_return_verref:
6682 elf_tdata (abfd)->verref = NULL;
6683 elf_tdata (abfd)->cverrefs = 0;
6684 goto error_return;
6686 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6687 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6688 goto error_return_verref;
6690 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed))
6691 goto error_return_verref;
6693 BFD_ASSERT (sizeof (Elf_External_Verneed)
6694 == sizeof (Elf_External_Vernaux));
6695 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
6696 everneed = (Elf_External_Verneed *) contents;
6697 iverneed = elf_tdata (abfd)->verref;
6698 for (i = 0; i < hdr->sh_info; i++, iverneed++)
6700 Elf_External_Vernaux *evernaux;
6701 Elf_Internal_Vernaux *ivernaux;
6702 unsigned int j;
6704 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
6706 iverneed->vn_bfd = abfd;
6708 iverneed->vn_filename =
6709 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6710 iverneed->vn_file);
6711 if (iverneed->vn_filename == NULL)
6712 goto error_return_verref;
6714 if (iverneed->vn_cnt == 0)
6715 iverneed->vn_auxptr = NULL;
6716 else
6718 iverneed->vn_auxptr = bfd_alloc2 (abfd, iverneed->vn_cnt,
6719 sizeof (Elf_Internal_Vernaux));
6720 if (iverneed->vn_auxptr == NULL)
6721 goto error_return_verref;
6724 if (iverneed->vn_aux
6725 > (size_t) (contents_end - (bfd_byte *) everneed))
6726 goto error_return_verref;
6728 evernaux = ((Elf_External_Vernaux *)
6729 ((bfd_byte *) everneed + iverneed->vn_aux));
6730 ivernaux = iverneed->vn_auxptr;
6731 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
6733 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
6735 ivernaux->vna_nodename =
6736 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6737 ivernaux->vna_name);
6738 if (ivernaux->vna_nodename == NULL)
6739 goto error_return_verref;
6741 if (j + 1 < iverneed->vn_cnt)
6742 ivernaux->vna_nextptr = ivernaux + 1;
6743 else
6744 ivernaux->vna_nextptr = NULL;
6746 if (ivernaux->vna_next
6747 > (size_t) (contents_end - (bfd_byte *) evernaux))
6748 goto error_return_verref;
6750 evernaux = ((Elf_External_Vernaux *)
6751 ((bfd_byte *) evernaux + ivernaux->vna_next));
6753 if (ivernaux->vna_other > freeidx)
6754 freeidx = ivernaux->vna_other;
6757 if (i + 1 < hdr->sh_info)
6758 iverneed->vn_nextref = iverneed + 1;
6759 else
6760 iverneed->vn_nextref = NULL;
6762 if (iverneed->vn_next
6763 > (size_t) (contents_end - (bfd_byte *) everneed))
6764 goto error_return_verref;
6766 everneed = ((Elf_External_Verneed *)
6767 ((bfd_byte *) everneed + iverneed->vn_next));
6770 free (contents);
6771 contents = NULL;
6774 if (elf_dynverdef (abfd) != 0)
6776 Elf_Internal_Shdr *hdr;
6777 Elf_External_Verdef *everdef;
6778 Elf_Internal_Verdef *iverdef;
6779 Elf_Internal_Verdef *iverdefarr;
6780 Elf_Internal_Verdef iverdefmem;
6781 unsigned int i;
6782 unsigned int maxidx;
6783 bfd_byte *contents_end_def, *contents_end_aux;
6785 hdr = &elf_tdata (abfd)->dynverdef_hdr;
6787 contents = bfd_malloc (hdr->sh_size);
6788 if (contents == NULL)
6789 goto error_return;
6790 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6791 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6792 goto error_return;
6794 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef))
6795 goto error_return;
6797 BFD_ASSERT (sizeof (Elf_External_Verdef)
6798 >= sizeof (Elf_External_Verdaux));
6799 contents_end_def = contents + hdr->sh_size
6800 - sizeof (Elf_External_Verdef);
6801 contents_end_aux = contents + hdr->sh_size
6802 - sizeof (Elf_External_Verdaux);
6804 /* We know the number of entries in the section but not the maximum
6805 index. Therefore we have to run through all entries and find
6806 the maximum. */
6807 everdef = (Elf_External_Verdef *) contents;
6808 maxidx = 0;
6809 for (i = 0; i < hdr->sh_info; ++i)
6811 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6813 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
6814 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
6816 if (iverdefmem.vd_next
6817 > (size_t) (contents_end_def - (bfd_byte *) everdef))
6818 goto error_return;
6820 everdef = ((Elf_External_Verdef *)
6821 ((bfd_byte *) everdef + iverdefmem.vd_next));
6824 if (default_imported_symver)
6826 if (freeidx > maxidx)
6827 maxidx = ++freeidx;
6828 else
6829 freeidx = ++maxidx;
6831 elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, maxidx,
6832 sizeof (Elf_Internal_Verdef));
6833 if (elf_tdata (abfd)->verdef == NULL)
6834 goto error_return;
6836 elf_tdata (abfd)->cverdefs = maxidx;
6838 everdef = (Elf_External_Verdef *) contents;
6839 iverdefarr = elf_tdata (abfd)->verdef;
6840 for (i = 0; i < hdr->sh_info; i++)
6842 Elf_External_Verdaux *everdaux;
6843 Elf_Internal_Verdaux *iverdaux;
6844 unsigned int j;
6846 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6848 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
6850 error_return_verdef:
6851 elf_tdata (abfd)->verdef = NULL;
6852 elf_tdata (abfd)->cverdefs = 0;
6853 goto error_return;
6856 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
6857 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
6859 iverdef->vd_bfd = abfd;
6861 if (iverdef->vd_cnt == 0)
6862 iverdef->vd_auxptr = NULL;
6863 else
6865 iverdef->vd_auxptr = bfd_alloc2 (abfd, iverdef->vd_cnt,
6866 sizeof (Elf_Internal_Verdaux));
6867 if (iverdef->vd_auxptr == NULL)
6868 goto error_return_verdef;
6871 if (iverdef->vd_aux
6872 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
6873 goto error_return_verdef;
6875 everdaux = ((Elf_External_Verdaux *)
6876 ((bfd_byte *) everdef + iverdef->vd_aux));
6877 iverdaux = iverdef->vd_auxptr;
6878 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
6880 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
6882 iverdaux->vda_nodename =
6883 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6884 iverdaux->vda_name);
6885 if (iverdaux->vda_nodename == NULL)
6886 goto error_return_verdef;
6888 if (j + 1 < iverdef->vd_cnt)
6889 iverdaux->vda_nextptr = iverdaux + 1;
6890 else
6891 iverdaux->vda_nextptr = NULL;
6893 if (iverdaux->vda_next
6894 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
6895 goto error_return_verdef;
6897 everdaux = ((Elf_External_Verdaux *)
6898 ((bfd_byte *) everdaux + iverdaux->vda_next));
6901 if (iverdef->vd_cnt)
6902 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
6904 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
6905 iverdef->vd_nextdef = iverdef + 1;
6906 else
6907 iverdef->vd_nextdef = NULL;
6909 everdef = ((Elf_External_Verdef *)
6910 ((bfd_byte *) everdef + iverdef->vd_next));
6913 free (contents);
6914 contents = NULL;
6916 else if (default_imported_symver)
6918 if (freeidx < 3)
6919 freeidx = 3;
6920 else
6921 freeidx++;
6923 elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, freeidx,
6924 sizeof (Elf_Internal_Verdef));
6925 if (elf_tdata (abfd)->verdef == NULL)
6926 goto error_return;
6928 elf_tdata (abfd)->cverdefs = freeidx;
6931 /* Create a default version based on the soname. */
6932 if (default_imported_symver)
6934 Elf_Internal_Verdef *iverdef;
6935 Elf_Internal_Verdaux *iverdaux;
6937 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];;
6939 iverdef->vd_version = VER_DEF_CURRENT;
6940 iverdef->vd_flags = 0;
6941 iverdef->vd_ndx = freeidx;
6942 iverdef->vd_cnt = 1;
6944 iverdef->vd_bfd = abfd;
6946 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
6947 if (iverdef->vd_nodename == NULL)
6948 goto error_return_verdef;
6949 iverdef->vd_nextdef = NULL;
6950 iverdef->vd_auxptr = bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux));
6951 if (iverdef->vd_auxptr == NULL)
6952 goto error_return_verdef;
6954 iverdaux = iverdef->vd_auxptr;
6955 iverdaux->vda_nodename = iverdef->vd_nodename;
6956 iverdaux->vda_nextptr = NULL;
6959 return TRUE;
6961 error_return:
6962 if (contents != NULL)
6963 free (contents);
6964 return FALSE;
6967 asymbol *
6968 _bfd_elf_make_empty_symbol (bfd *abfd)
6970 elf_symbol_type *newsym;
6971 bfd_size_type amt = sizeof (elf_symbol_type);
6973 newsym = bfd_zalloc (abfd, amt);
6974 if (!newsym)
6975 return NULL;
6976 else
6978 newsym->symbol.the_bfd = abfd;
6979 return &newsym->symbol;
6983 void
6984 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
6985 asymbol *symbol,
6986 symbol_info *ret)
6988 bfd_symbol_info (symbol, ret);
6991 /* Return whether a symbol name implies a local symbol. Most targets
6992 use this function for the is_local_label_name entry point, but some
6993 override it. */
6995 bfd_boolean
6996 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
6997 const char *name)
6999 /* Normal local symbols start with ``.L''. */
7000 if (name[0] == '.' && name[1] == 'L')
7001 return TRUE;
7003 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7004 DWARF debugging symbols starting with ``..''. */
7005 if (name[0] == '.' && name[1] == '.')
7006 return TRUE;
7008 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7009 emitting DWARF debugging output. I suspect this is actually a
7010 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7011 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7012 underscore to be emitted on some ELF targets). For ease of use,
7013 we treat such symbols as local. */
7014 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
7015 return TRUE;
7017 return FALSE;
7020 alent *
7021 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
7022 asymbol *symbol ATTRIBUTE_UNUSED)
7024 abort ();
7025 return NULL;
7028 bfd_boolean
7029 _bfd_elf_set_arch_mach (bfd *abfd,
7030 enum bfd_architecture arch,
7031 unsigned long machine)
7033 /* If this isn't the right architecture for this backend, and this
7034 isn't the generic backend, fail. */
7035 if (arch != get_elf_backend_data (abfd)->arch
7036 && arch != bfd_arch_unknown
7037 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
7038 return FALSE;
7040 return bfd_default_set_arch_mach (abfd, arch, machine);
7043 /* Find the function to a particular section and offset,
7044 for error reporting. */
7046 static bfd_boolean
7047 elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
7048 asection *section,
7049 asymbol **symbols,
7050 bfd_vma offset,
7051 const char **filename_ptr,
7052 const char **functionname_ptr)
7054 const char *filename;
7055 asymbol *func, *file;
7056 bfd_vma low_func;
7057 asymbol **p;
7058 /* ??? Given multiple file symbols, it is impossible to reliably
7059 choose the right file name for global symbols. File symbols are
7060 local symbols, and thus all file symbols must sort before any
7061 global symbols. The ELF spec may be interpreted to say that a
7062 file symbol must sort before other local symbols, but currently
7063 ld -r doesn't do this. So, for ld -r output, it is possible to
7064 make a better choice of file name for local symbols by ignoring
7065 file symbols appearing after a given local symbol. */
7066 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
7068 filename = NULL;
7069 func = NULL;
7070 file = NULL;
7071 low_func = 0;
7072 state = nothing_seen;
7074 for (p = symbols; *p != NULL; p++)
7076 elf_symbol_type *q;
7078 q = (elf_symbol_type *) *p;
7080 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
7082 default:
7083 break;
7084 case STT_FILE:
7085 file = &q->symbol;
7086 if (state == symbol_seen)
7087 state = file_after_symbol_seen;
7088 continue;
7089 case STT_NOTYPE:
7090 case STT_FUNC:
7091 if (bfd_get_section (&q->symbol) == section
7092 && q->symbol.value >= low_func
7093 && q->symbol.value <= offset)
7095 func = (asymbol *) q;
7096 low_func = q->symbol.value;
7097 filename = NULL;
7098 if (file != NULL
7099 && (ELF_ST_BIND (q->internal_elf_sym.st_info) == STB_LOCAL
7100 || state != file_after_symbol_seen))
7101 filename = bfd_asymbol_name (file);
7103 break;
7105 if (state == nothing_seen)
7106 state = symbol_seen;
7109 if (func == NULL)
7110 return FALSE;
7112 if (filename_ptr)
7113 *filename_ptr = filename;
7114 if (functionname_ptr)
7115 *functionname_ptr = bfd_asymbol_name (func);
7117 return TRUE;
7120 /* Find the nearest line to a particular section and offset,
7121 for error reporting. */
7123 bfd_boolean
7124 _bfd_elf_find_nearest_line (bfd *abfd,
7125 asection *section,
7126 asymbol **symbols,
7127 bfd_vma offset,
7128 const char **filename_ptr,
7129 const char **functionname_ptr,
7130 unsigned int *line_ptr)
7132 bfd_boolean found;
7134 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
7135 filename_ptr, functionname_ptr,
7136 line_ptr))
7138 if (!*functionname_ptr)
7139 elf_find_function (abfd, section, symbols, offset,
7140 *filename_ptr ? NULL : filename_ptr,
7141 functionname_ptr);
7143 return TRUE;
7146 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
7147 filename_ptr, functionname_ptr,
7148 line_ptr, 0,
7149 &elf_tdata (abfd)->dwarf2_find_line_info))
7151 if (!*functionname_ptr)
7152 elf_find_function (abfd, section, symbols, offset,
7153 *filename_ptr ? NULL : filename_ptr,
7154 functionname_ptr);
7156 return TRUE;
7159 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
7160 &found, filename_ptr,
7161 functionname_ptr, line_ptr,
7162 &elf_tdata (abfd)->line_info))
7163 return FALSE;
7164 if (found && (*functionname_ptr || *line_ptr))
7165 return TRUE;
7167 if (symbols == NULL)
7168 return FALSE;
7170 if (! elf_find_function (abfd, section, symbols, offset,
7171 filename_ptr, functionname_ptr))
7172 return FALSE;
7174 *line_ptr = 0;
7175 return TRUE;
7178 /* Find the line for a symbol. */
7180 bfd_boolean
7181 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
7182 const char **filename_ptr, unsigned int *line_ptr)
7184 return _bfd_dwarf2_find_line (abfd, symbols, symbol,
7185 filename_ptr, line_ptr, 0,
7186 &elf_tdata (abfd)->dwarf2_find_line_info);
7189 /* After a call to bfd_find_nearest_line, successive calls to
7190 bfd_find_inliner_info can be used to get source information about
7191 each level of function inlining that terminated at the address
7192 passed to bfd_find_nearest_line. Currently this is only supported
7193 for DWARF2 with appropriate DWARF3 extensions. */
7195 bfd_boolean
7196 _bfd_elf_find_inliner_info (bfd *abfd,
7197 const char **filename_ptr,
7198 const char **functionname_ptr,
7199 unsigned int *line_ptr)
7201 bfd_boolean found;
7202 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
7203 functionname_ptr, line_ptr,
7204 & elf_tdata (abfd)->dwarf2_find_line_info);
7205 return found;
7209 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
7211 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7212 int ret = bed->s->sizeof_ehdr;
7214 if (!info->relocatable)
7216 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
7218 if (phdr_size == (bfd_size_type) -1)
7220 struct elf_segment_map *m;
7222 phdr_size = 0;
7223 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
7224 phdr_size += bed->s->sizeof_phdr;
7226 if (phdr_size == 0)
7227 phdr_size = get_program_header_size (abfd, info);
7230 elf_tdata (abfd)->program_header_size = phdr_size;
7231 ret += phdr_size;
7234 return ret;
7237 bfd_boolean
7238 _bfd_elf_set_section_contents (bfd *abfd,
7239 sec_ptr section,
7240 const void *location,
7241 file_ptr offset,
7242 bfd_size_type count)
7244 Elf_Internal_Shdr *hdr;
7245 bfd_signed_vma pos;
7247 if (! abfd->output_has_begun
7248 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7249 return FALSE;
7251 hdr = &elf_section_data (section)->this_hdr;
7252 pos = hdr->sh_offset + offset;
7253 if (bfd_seek (abfd, pos, SEEK_SET) != 0
7254 || bfd_bwrite (location, count, abfd) != count)
7255 return FALSE;
7257 return TRUE;
7260 void
7261 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
7262 arelent *cache_ptr ATTRIBUTE_UNUSED,
7263 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
7265 abort ();
7268 /* Try to convert a non-ELF reloc into an ELF one. */
7270 bfd_boolean
7271 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
7273 /* Check whether we really have an ELF howto. */
7275 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
7277 bfd_reloc_code_real_type code;
7278 reloc_howto_type *howto;
7280 /* Alien reloc: Try to determine its type to replace it with an
7281 equivalent ELF reloc. */
7283 if (areloc->howto->pc_relative)
7285 switch (areloc->howto->bitsize)
7287 case 8:
7288 code = BFD_RELOC_8_PCREL;
7289 break;
7290 case 12:
7291 code = BFD_RELOC_12_PCREL;
7292 break;
7293 case 16:
7294 code = BFD_RELOC_16_PCREL;
7295 break;
7296 case 24:
7297 code = BFD_RELOC_24_PCREL;
7298 break;
7299 case 32:
7300 code = BFD_RELOC_32_PCREL;
7301 break;
7302 case 64:
7303 code = BFD_RELOC_64_PCREL;
7304 break;
7305 default:
7306 goto fail;
7309 howto = bfd_reloc_type_lookup (abfd, code);
7311 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
7313 if (howto->pcrel_offset)
7314 areloc->addend += areloc->address;
7315 else
7316 areloc->addend -= areloc->address; /* addend is unsigned!! */
7319 else
7321 switch (areloc->howto->bitsize)
7323 case 8:
7324 code = BFD_RELOC_8;
7325 break;
7326 case 14:
7327 code = BFD_RELOC_14;
7328 break;
7329 case 16:
7330 code = BFD_RELOC_16;
7331 break;
7332 case 26:
7333 code = BFD_RELOC_26;
7334 break;
7335 case 32:
7336 code = BFD_RELOC_32;
7337 break;
7338 case 64:
7339 code = BFD_RELOC_64;
7340 break;
7341 default:
7342 goto fail;
7345 howto = bfd_reloc_type_lookup (abfd, code);
7348 if (howto)
7349 areloc->howto = howto;
7350 else
7351 goto fail;
7354 return TRUE;
7356 fail:
7357 (*_bfd_error_handler)
7358 (_("%B: unsupported relocation type %s"),
7359 abfd, areloc->howto->name);
7360 bfd_set_error (bfd_error_bad_value);
7361 return FALSE;
7364 bfd_boolean
7365 _bfd_elf_close_and_cleanup (bfd *abfd)
7367 if (bfd_get_format (abfd) == bfd_object)
7369 if (elf_tdata (abfd) != NULL && elf_shstrtab (abfd) != NULL)
7370 _bfd_elf_strtab_free (elf_shstrtab (abfd));
7371 _bfd_dwarf2_cleanup_debug_info (abfd);
7374 return _bfd_generic_close_and_cleanup (abfd);
7377 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7378 in the relocation's offset. Thus we cannot allow any sort of sanity
7379 range-checking to interfere. There is nothing else to do in processing
7380 this reloc. */
7382 bfd_reloc_status_type
7383 _bfd_elf_rel_vtable_reloc_fn
7384 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
7385 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
7386 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
7387 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
7389 return bfd_reloc_ok;
7392 /* Elf core file support. Much of this only works on native
7393 toolchains, since we rely on knowing the
7394 machine-dependent procfs structure in order to pick
7395 out details about the corefile. */
7397 #ifdef HAVE_SYS_PROCFS_H
7398 # include <sys/procfs.h>
7399 #endif
7401 /* FIXME: this is kinda wrong, but it's what gdb wants. */
7403 static int
7404 elfcore_make_pid (bfd *abfd)
7406 return ((elf_tdata (abfd)->core_lwpid << 16)
7407 + (elf_tdata (abfd)->core_pid));
7410 /* If there isn't a section called NAME, make one, using
7411 data from SECT. Note, this function will generate a
7412 reference to NAME, so you shouldn't deallocate or
7413 overwrite it. */
7415 static bfd_boolean
7416 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
7418 asection *sect2;
7420 if (bfd_get_section_by_name (abfd, name) != NULL)
7421 return TRUE;
7423 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
7424 if (sect2 == NULL)
7425 return FALSE;
7427 sect2->size = sect->size;
7428 sect2->filepos = sect->filepos;
7429 sect2->alignment_power = sect->alignment_power;
7430 return TRUE;
7433 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
7434 actually creates up to two pseudosections:
7435 - For the single-threaded case, a section named NAME, unless
7436 such a section already exists.
7437 - For the multi-threaded case, a section named "NAME/PID", where
7438 PID is elfcore_make_pid (abfd).
7439 Both pseudosections have identical contents. */
7440 bfd_boolean
7441 _bfd_elfcore_make_pseudosection (bfd *abfd,
7442 char *name,
7443 size_t size,
7444 ufile_ptr filepos)
7446 char buf[100];
7447 char *threaded_name;
7448 size_t len;
7449 asection *sect;
7451 /* Build the section name. */
7453 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
7454 len = strlen (buf) + 1;
7455 threaded_name = bfd_alloc (abfd, len);
7456 if (threaded_name == NULL)
7457 return FALSE;
7458 memcpy (threaded_name, buf, len);
7460 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
7461 SEC_HAS_CONTENTS);
7462 if (sect == NULL)
7463 return FALSE;
7464 sect->size = size;
7465 sect->filepos = filepos;
7466 sect->alignment_power = 2;
7468 return elfcore_maybe_make_sect (abfd, name, sect);
7471 /* prstatus_t exists on:
7472 solaris 2.5+
7473 linux 2.[01] + glibc
7474 unixware 4.2
7477 #if defined (HAVE_PRSTATUS_T)
7479 static bfd_boolean
7480 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
7482 size_t size;
7483 int offset;
7485 if (note->descsz == sizeof (prstatus_t))
7487 prstatus_t prstat;
7489 size = sizeof (prstat.pr_reg);
7490 offset = offsetof (prstatus_t, pr_reg);
7491 memcpy (&prstat, note->descdata, sizeof (prstat));
7493 /* Do not overwrite the core signal if it
7494 has already been set by another thread. */
7495 if (elf_tdata (abfd)->core_signal == 0)
7496 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7497 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7499 /* pr_who exists on:
7500 solaris 2.5+
7501 unixware 4.2
7502 pr_who doesn't exist on:
7503 linux 2.[01]
7505 #if defined (HAVE_PRSTATUS_T_PR_WHO)
7506 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7507 #endif
7509 #if defined (HAVE_PRSTATUS32_T)
7510 else if (note->descsz == sizeof (prstatus32_t))
7512 /* 64-bit host, 32-bit corefile */
7513 prstatus32_t prstat;
7515 size = sizeof (prstat.pr_reg);
7516 offset = offsetof (prstatus32_t, pr_reg);
7517 memcpy (&prstat, note->descdata, sizeof (prstat));
7519 /* Do not overwrite the core signal if it
7520 has already been set by another thread. */
7521 if (elf_tdata (abfd)->core_signal == 0)
7522 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7523 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7525 /* pr_who exists on:
7526 solaris 2.5+
7527 unixware 4.2
7528 pr_who doesn't exist on:
7529 linux 2.[01]
7531 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
7532 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7533 #endif
7535 #endif /* HAVE_PRSTATUS32_T */
7536 else
7538 /* Fail - we don't know how to handle any other
7539 note size (ie. data object type). */
7540 return TRUE;
7543 /* Make a ".reg/999" section and a ".reg" section. */
7544 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
7545 size, note->descpos + offset);
7547 #endif /* defined (HAVE_PRSTATUS_T) */
7549 /* Create a pseudosection containing the exact contents of NOTE. */
7550 static bfd_boolean
7551 elfcore_make_note_pseudosection (bfd *abfd,
7552 char *name,
7553 Elf_Internal_Note *note)
7555 return _bfd_elfcore_make_pseudosection (abfd, name,
7556 note->descsz, note->descpos);
7559 /* There isn't a consistent prfpregset_t across platforms,
7560 but it doesn't matter, because we don't have to pick this
7561 data structure apart. */
7563 static bfd_boolean
7564 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
7566 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7569 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
7570 type of NT_PRXFPREG. Just include the whole note's contents
7571 literally. */
7573 static bfd_boolean
7574 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
7576 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
7579 static bfd_boolean
7580 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
7582 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
7585 static bfd_boolean
7586 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
7588 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
7591 #if defined (HAVE_PRPSINFO_T)
7592 typedef prpsinfo_t elfcore_psinfo_t;
7593 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
7594 typedef prpsinfo32_t elfcore_psinfo32_t;
7595 #endif
7596 #endif
7598 #if defined (HAVE_PSINFO_T)
7599 typedef psinfo_t elfcore_psinfo_t;
7600 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
7601 typedef psinfo32_t elfcore_psinfo32_t;
7602 #endif
7603 #endif
7605 /* return a malloc'ed copy of a string at START which is at
7606 most MAX bytes long, possibly without a terminating '\0'.
7607 the copy will always have a terminating '\0'. */
7609 char *
7610 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
7612 char *dups;
7613 char *end = memchr (start, '\0', max);
7614 size_t len;
7616 if (end == NULL)
7617 len = max;
7618 else
7619 len = end - start;
7621 dups = bfd_alloc (abfd, len + 1);
7622 if (dups == NULL)
7623 return NULL;
7625 memcpy (dups, start, len);
7626 dups[len] = '\0';
7628 return dups;
7631 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7632 static bfd_boolean
7633 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
7635 if (note->descsz == sizeof (elfcore_psinfo_t))
7637 elfcore_psinfo_t psinfo;
7639 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7641 elf_tdata (abfd)->core_program
7642 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7643 sizeof (psinfo.pr_fname));
7645 elf_tdata (abfd)->core_command
7646 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7647 sizeof (psinfo.pr_psargs));
7649 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
7650 else if (note->descsz == sizeof (elfcore_psinfo32_t))
7652 /* 64-bit host, 32-bit corefile */
7653 elfcore_psinfo32_t psinfo;
7655 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7657 elf_tdata (abfd)->core_program
7658 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7659 sizeof (psinfo.pr_fname));
7661 elf_tdata (abfd)->core_command
7662 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7663 sizeof (psinfo.pr_psargs));
7665 #endif
7667 else
7669 /* Fail - we don't know how to handle any other
7670 note size (ie. data object type). */
7671 return TRUE;
7674 /* Note that for some reason, a spurious space is tacked
7675 onto the end of the args in some (at least one anyway)
7676 implementations, so strip it off if it exists. */
7679 char *command = elf_tdata (abfd)->core_command;
7680 int n = strlen (command);
7682 if (0 < n && command[n - 1] == ' ')
7683 command[n - 1] = '\0';
7686 return TRUE;
7688 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
7690 #if defined (HAVE_PSTATUS_T)
7691 static bfd_boolean
7692 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
7694 if (note->descsz == sizeof (pstatus_t)
7695 #if defined (HAVE_PXSTATUS_T)
7696 || note->descsz == sizeof (pxstatus_t)
7697 #endif
7700 pstatus_t pstat;
7702 memcpy (&pstat, note->descdata, sizeof (pstat));
7704 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7706 #if defined (HAVE_PSTATUS32_T)
7707 else if (note->descsz == sizeof (pstatus32_t))
7709 /* 64-bit host, 32-bit corefile */
7710 pstatus32_t pstat;
7712 memcpy (&pstat, note->descdata, sizeof (pstat));
7714 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7716 #endif
7717 /* Could grab some more details from the "representative"
7718 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
7719 NT_LWPSTATUS note, presumably. */
7721 return TRUE;
7723 #endif /* defined (HAVE_PSTATUS_T) */
7725 #if defined (HAVE_LWPSTATUS_T)
7726 static bfd_boolean
7727 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
7729 lwpstatus_t lwpstat;
7730 char buf[100];
7731 char *name;
7732 size_t len;
7733 asection *sect;
7735 if (note->descsz != sizeof (lwpstat)
7736 #if defined (HAVE_LWPXSTATUS_T)
7737 && note->descsz != sizeof (lwpxstatus_t)
7738 #endif
7740 return TRUE;
7742 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
7744 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
7745 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
7747 /* Make a ".reg/999" section. */
7749 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
7750 len = strlen (buf) + 1;
7751 name = bfd_alloc (abfd, len);
7752 if (name == NULL)
7753 return FALSE;
7754 memcpy (name, buf, len);
7756 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7757 if (sect == NULL)
7758 return FALSE;
7760 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7761 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
7762 sect->filepos = note->descpos
7763 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
7764 #endif
7766 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7767 sect->size = sizeof (lwpstat.pr_reg);
7768 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
7769 #endif
7771 sect->alignment_power = 2;
7773 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
7774 return FALSE;
7776 /* Make a ".reg2/999" section */
7778 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
7779 len = strlen (buf) + 1;
7780 name = bfd_alloc (abfd, len);
7781 if (name == NULL)
7782 return FALSE;
7783 memcpy (name, buf, len);
7785 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7786 if (sect == NULL)
7787 return FALSE;
7789 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7790 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
7791 sect->filepos = note->descpos
7792 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
7793 #endif
7795 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
7796 sect->size = sizeof (lwpstat.pr_fpreg);
7797 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
7798 #endif
7800 sect->alignment_power = 2;
7802 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
7804 #endif /* defined (HAVE_LWPSTATUS_T) */
7806 static bfd_boolean
7807 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
7809 char buf[30];
7810 char *name;
7811 size_t len;
7812 asection *sect;
7813 int type;
7814 int is_active_thread;
7815 bfd_vma base_addr;
7817 if (note->descsz < 728)
7818 return TRUE;
7820 if (! CONST_STRNEQ (note->namedata, "win32"))
7821 return TRUE;
7823 type = bfd_get_32 (abfd, note->descdata);
7825 switch (type)
7827 case 1 /* NOTE_INFO_PROCESS */:
7828 /* FIXME: need to add ->core_command. */
7829 /* process_info.pid */
7830 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 8);
7831 /* process_info.signal */
7832 elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 12);
7833 break;
7835 case 2 /* NOTE_INFO_THREAD */:
7836 /* Make a ".reg/999" section. */
7837 /* thread_info.tid */
7838 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
7840 len = strlen (buf) + 1;
7841 name = bfd_alloc (abfd, len);
7842 if (name == NULL)
7843 return FALSE;
7845 memcpy (name, buf, len);
7847 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7848 if (sect == NULL)
7849 return FALSE;
7851 /* sizeof (thread_info.thread_context) */
7852 sect->size = 716;
7853 /* offsetof (thread_info.thread_context) */
7854 sect->filepos = note->descpos + 12;
7855 sect->alignment_power = 2;
7857 /* thread_info.is_active_thread */
7858 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
7860 if (is_active_thread)
7861 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
7862 return FALSE;
7863 break;
7865 case 3 /* NOTE_INFO_MODULE */:
7866 /* Make a ".module/xxxxxxxx" section. */
7867 /* module_info.base_address */
7868 base_addr = bfd_get_32 (abfd, note->descdata + 4);
7869 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
7871 len = strlen (buf) + 1;
7872 name = bfd_alloc (abfd, len);
7873 if (name == NULL)
7874 return FALSE;
7876 memcpy (name, buf, len);
7878 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7880 if (sect == NULL)
7881 return FALSE;
7883 sect->size = note->descsz;
7884 sect->filepos = note->descpos;
7885 sect->alignment_power = 2;
7886 break;
7888 default:
7889 return TRUE;
7892 return TRUE;
7895 static bfd_boolean
7896 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
7898 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7900 switch (note->type)
7902 default:
7903 return TRUE;
7905 case NT_PRSTATUS:
7906 if (bed->elf_backend_grok_prstatus)
7907 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
7908 return TRUE;
7909 #if defined (HAVE_PRSTATUS_T)
7910 return elfcore_grok_prstatus (abfd, note);
7911 #else
7912 return TRUE;
7913 #endif
7915 #if defined (HAVE_PSTATUS_T)
7916 case NT_PSTATUS:
7917 return elfcore_grok_pstatus (abfd, note);
7918 #endif
7920 #if defined (HAVE_LWPSTATUS_T)
7921 case NT_LWPSTATUS:
7922 return elfcore_grok_lwpstatus (abfd, note);
7923 #endif
7925 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
7926 return elfcore_grok_prfpreg (abfd, note);
7928 case NT_WIN32PSTATUS:
7929 return elfcore_grok_win32pstatus (abfd, note);
7931 case NT_PRXFPREG: /* Linux SSE extension */
7932 if (note->namesz == 6
7933 && strcmp (note->namedata, "LINUX") == 0)
7934 return elfcore_grok_prxfpreg (abfd, note);
7935 else
7936 return TRUE;
7938 case NT_PPC_VMX:
7939 if (note->namesz == 6
7940 && strcmp (note->namedata, "LINUX") == 0)
7941 return elfcore_grok_ppc_vmx (abfd, note);
7942 else
7943 return TRUE;
7945 case NT_PPC_VSX:
7946 if (note->namesz == 6
7947 && strcmp (note->namedata, "LINUX") == 0)
7948 return elfcore_grok_ppc_vsx (abfd, note);
7949 else
7950 return TRUE;
7952 case NT_PRPSINFO:
7953 case NT_PSINFO:
7954 if (bed->elf_backend_grok_psinfo)
7955 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
7956 return TRUE;
7957 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7958 return elfcore_grok_psinfo (abfd, note);
7959 #else
7960 return TRUE;
7961 #endif
7963 case NT_AUXV:
7965 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
7966 SEC_HAS_CONTENTS);
7968 if (sect == NULL)
7969 return FALSE;
7970 sect->size = note->descsz;
7971 sect->filepos = note->descpos;
7972 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
7974 return TRUE;
7979 static bfd_boolean
7980 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
7982 elf_tdata (abfd)->build_id_size = note->descsz;
7983 elf_tdata (abfd)->build_id = bfd_alloc (abfd, note->descsz);
7984 if (elf_tdata (abfd)->build_id == NULL)
7985 return FALSE;
7987 memcpy (elf_tdata (abfd)->build_id, note->descdata, note->descsz);
7989 return TRUE;
7992 static bfd_boolean
7993 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
7995 switch (note->type)
7997 default:
7998 return TRUE;
8000 case NT_GNU_BUILD_ID:
8001 return elfobj_grok_gnu_build_id (abfd, note);
8005 static bfd_boolean
8006 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
8008 char *cp;
8010 cp = strchr (note->namedata, '@');
8011 if (cp != NULL)
8013 *lwpidp = atoi(cp + 1);
8014 return TRUE;
8016 return FALSE;
8019 static bfd_boolean
8020 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8022 /* Signal number at offset 0x08. */
8023 elf_tdata (abfd)->core_signal
8024 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8026 /* Process ID at offset 0x50. */
8027 elf_tdata (abfd)->core_pid
8028 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
8030 /* Command name at 0x7c (max 32 bytes, including nul). */
8031 elf_tdata (abfd)->core_command
8032 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
8034 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
8035 note);
8038 static bfd_boolean
8039 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
8041 int lwp;
8043 if (elfcore_netbsd_get_lwpid (note, &lwp))
8044 elf_tdata (abfd)->core_lwpid = lwp;
8046 if (note->type == NT_NETBSDCORE_PROCINFO)
8048 /* NetBSD-specific core "procinfo". Note that we expect to
8049 find this note before any of the others, which is fine,
8050 since the kernel writes this note out first when it
8051 creates a core file. */
8053 return elfcore_grok_netbsd_procinfo (abfd, note);
8056 /* As of Jan 2002 there are no other machine-independent notes
8057 defined for NetBSD core files. If the note type is less
8058 than the start of the machine-dependent note types, we don't
8059 understand it. */
8061 if (note->type < NT_NETBSDCORE_FIRSTMACH)
8062 return TRUE;
8065 switch (bfd_get_arch (abfd))
8067 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8068 PT_GETFPREGS == mach+2. */
8070 case bfd_arch_alpha:
8071 case bfd_arch_sparc:
8072 switch (note->type)
8074 case NT_NETBSDCORE_FIRSTMACH+0:
8075 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8077 case NT_NETBSDCORE_FIRSTMACH+2:
8078 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8080 default:
8081 return TRUE;
8084 /* On all other arch's, PT_GETREGS == mach+1 and
8085 PT_GETFPREGS == mach+3. */
8087 default:
8088 switch (note->type)
8090 case NT_NETBSDCORE_FIRSTMACH+1:
8091 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8093 case NT_NETBSDCORE_FIRSTMACH+3:
8094 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8096 default:
8097 return TRUE;
8100 /* NOTREACHED */
8103 static bfd_boolean
8104 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
8106 void *ddata = note->descdata;
8107 char buf[100];
8108 char *name;
8109 asection *sect;
8110 short sig;
8111 unsigned flags;
8113 /* nto_procfs_status 'pid' field is at offset 0. */
8114 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
8116 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
8117 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
8119 /* nto_procfs_status 'flags' field is at offset 8. */
8120 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
8122 /* nto_procfs_status 'what' field is at offset 14. */
8123 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
8125 elf_tdata (abfd)->core_signal = sig;
8126 elf_tdata (abfd)->core_lwpid = *tid;
8129 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
8130 do not come from signals so we make sure we set the current
8131 thread just in case. */
8132 if (flags & 0x00000080)
8133 elf_tdata (abfd)->core_lwpid = *tid;
8135 /* Make a ".qnx_core_status/%d" section. */
8136 sprintf (buf, ".qnx_core_status/%ld", *tid);
8138 name = bfd_alloc (abfd, strlen (buf) + 1);
8139 if (name == NULL)
8140 return FALSE;
8141 strcpy (name, buf);
8143 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8144 if (sect == NULL)
8145 return FALSE;
8147 sect->size = note->descsz;
8148 sect->filepos = note->descpos;
8149 sect->alignment_power = 2;
8151 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
8154 static bfd_boolean
8155 elfcore_grok_nto_regs (bfd *abfd,
8156 Elf_Internal_Note *note,
8157 long tid,
8158 char *base)
8160 char buf[100];
8161 char *name;
8162 asection *sect;
8164 /* Make a "(base)/%d" section. */
8165 sprintf (buf, "%s/%ld", base, tid);
8167 name = bfd_alloc (abfd, strlen (buf) + 1);
8168 if (name == NULL)
8169 return FALSE;
8170 strcpy (name, buf);
8172 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8173 if (sect == NULL)
8174 return FALSE;
8176 sect->size = note->descsz;
8177 sect->filepos = note->descpos;
8178 sect->alignment_power = 2;
8180 /* This is the current thread. */
8181 if (elf_tdata (abfd)->core_lwpid == tid)
8182 return elfcore_maybe_make_sect (abfd, base, sect);
8184 return TRUE;
8187 #define BFD_QNT_CORE_INFO 7
8188 #define BFD_QNT_CORE_STATUS 8
8189 #define BFD_QNT_CORE_GREG 9
8190 #define BFD_QNT_CORE_FPREG 10
8192 static bfd_boolean
8193 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
8195 /* Every GREG section has a STATUS section before it. Store the
8196 tid from the previous call to pass down to the next gregs
8197 function. */
8198 static long tid = 1;
8200 switch (note->type)
8202 case BFD_QNT_CORE_INFO:
8203 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
8204 case BFD_QNT_CORE_STATUS:
8205 return elfcore_grok_nto_status (abfd, note, &tid);
8206 case BFD_QNT_CORE_GREG:
8207 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
8208 case BFD_QNT_CORE_FPREG:
8209 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
8210 default:
8211 return TRUE;
8215 static bfd_boolean
8216 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
8218 char *name;
8219 asection *sect;
8220 size_t len;
8222 /* Use note name as section name. */
8223 len = note->namesz;
8224 name = bfd_alloc (abfd, len);
8225 if (name == NULL)
8226 return FALSE;
8227 memcpy (name, note->namedata, len);
8228 name[len - 1] = '\0';
8230 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8231 if (sect == NULL)
8232 return FALSE;
8234 sect->size = note->descsz;
8235 sect->filepos = note->descpos;
8236 sect->alignment_power = 1;
8238 return TRUE;
8241 /* Function: elfcore_write_note
8243 Inputs:
8244 buffer to hold note, and current size of buffer
8245 name of note
8246 type of note
8247 data for note
8248 size of data for note
8250 Writes note to end of buffer. ELF64 notes are written exactly as
8251 for ELF32, despite the current (as of 2006) ELF gabi specifying
8252 that they ought to have 8-byte namesz and descsz field, and have
8253 8-byte alignment. Other writers, eg. Linux kernel, do the same.
8255 Return:
8256 Pointer to realloc'd buffer, *BUFSIZ updated. */
8258 char *
8259 elfcore_write_note (bfd *abfd,
8260 char *buf,
8261 int *bufsiz,
8262 const char *name,
8263 int type,
8264 const void *input,
8265 int size)
8267 Elf_External_Note *xnp;
8268 size_t namesz;
8269 size_t newspace;
8270 char *dest;
8272 namesz = 0;
8273 if (name != NULL)
8274 namesz = strlen (name) + 1;
8276 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
8278 buf = realloc (buf, *bufsiz + newspace);
8279 if (buf == NULL)
8280 return buf;
8281 dest = buf + *bufsiz;
8282 *bufsiz += newspace;
8283 xnp = (Elf_External_Note *) dest;
8284 H_PUT_32 (abfd, namesz, xnp->namesz);
8285 H_PUT_32 (abfd, size, xnp->descsz);
8286 H_PUT_32 (abfd, type, xnp->type);
8287 dest = xnp->name;
8288 if (name != NULL)
8290 memcpy (dest, name, namesz);
8291 dest += namesz;
8292 while (namesz & 3)
8294 *dest++ = '\0';
8295 ++namesz;
8298 memcpy (dest, input, size);
8299 dest += size;
8300 while (size & 3)
8302 *dest++ = '\0';
8303 ++size;
8305 return buf;
8308 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8309 char *
8310 elfcore_write_prpsinfo (bfd *abfd,
8311 char *buf,
8312 int *bufsiz,
8313 const char *fname,
8314 const char *psargs)
8316 const char *note_name = "CORE";
8317 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8319 if (bed->elf_backend_write_core_note != NULL)
8321 char *ret;
8322 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8323 NT_PRPSINFO, fname, psargs);
8324 if (ret != NULL)
8325 return ret;
8328 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8329 if (bed->s->elfclass == ELFCLASS32)
8331 #if defined (HAVE_PSINFO32_T)
8332 psinfo32_t data;
8333 int note_type = NT_PSINFO;
8334 #else
8335 prpsinfo32_t data;
8336 int note_type = NT_PRPSINFO;
8337 #endif
8339 memset (&data, 0, sizeof (data));
8340 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8341 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8342 return elfcore_write_note (abfd, buf, bufsiz,
8343 note_name, note_type, &data, sizeof (data));
8345 else
8346 #endif
8348 #if defined (HAVE_PSINFO_T)
8349 psinfo_t data;
8350 int note_type = NT_PSINFO;
8351 #else
8352 prpsinfo_t data;
8353 int note_type = NT_PRPSINFO;
8354 #endif
8356 memset (&data, 0, sizeof (data));
8357 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8358 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8359 return elfcore_write_note (abfd, buf, bufsiz,
8360 note_name, note_type, &data, sizeof (data));
8363 #endif /* PSINFO_T or PRPSINFO_T */
8365 #if defined (HAVE_PRSTATUS_T)
8366 char *
8367 elfcore_write_prstatus (bfd *abfd,
8368 char *buf,
8369 int *bufsiz,
8370 long pid,
8371 int cursig,
8372 const void *gregs)
8374 const char *note_name = "CORE";
8375 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8377 if (bed->elf_backend_write_core_note != NULL)
8379 char *ret;
8380 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8381 NT_PRSTATUS,
8382 pid, cursig, gregs);
8383 if (ret != NULL)
8384 return ret;
8387 #if defined (HAVE_PRSTATUS32_T)
8388 if (bed->s->elfclass == ELFCLASS32)
8390 prstatus32_t prstat;
8392 memset (&prstat, 0, sizeof (prstat));
8393 prstat.pr_pid = pid;
8394 prstat.pr_cursig = cursig;
8395 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8396 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8397 NT_PRSTATUS, &prstat, sizeof (prstat));
8399 else
8400 #endif
8402 prstatus_t prstat;
8404 memset (&prstat, 0, sizeof (prstat));
8405 prstat.pr_pid = pid;
8406 prstat.pr_cursig = cursig;
8407 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8408 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8409 NT_PRSTATUS, &prstat, sizeof (prstat));
8412 #endif /* HAVE_PRSTATUS_T */
8414 #if defined (HAVE_LWPSTATUS_T)
8415 char *
8416 elfcore_write_lwpstatus (bfd *abfd,
8417 char *buf,
8418 int *bufsiz,
8419 long pid,
8420 int cursig,
8421 const void *gregs)
8423 lwpstatus_t lwpstat;
8424 const char *note_name = "CORE";
8426 memset (&lwpstat, 0, sizeof (lwpstat));
8427 lwpstat.pr_lwpid = pid >> 16;
8428 lwpstat.pr_cursig = cursig;
8429 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8430 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
8431 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8432 #if !defined(gregs)
8433 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
8434 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
8435 #else
8436 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
8437 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
8438 #endif
8439 #endif
8440 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8441 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
8443 #endif /* HAVE_LWPSTATUS_T */
8445 #if defined (HAVE_PSTATUS_T)
8446 char *
8447 elfcore_write_pstatus (bfd *abfd,
8448 char *buf,
8449 int *bufsiz,
8450 long pid,
8451 int cursig ATTRIBUTE_UNUSED,
8452 const void *gregs ATTRIBUTE_UNUSED)
8454 const char *note_name = "CORE";
8455 #if defined (HAVE_PSTATUS32_T)
8456 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8458 if (bed->s->elfclass == ELFCLASS32)
8460 pstatus32_t pstat;
8462 memset (&pstat, 0, sizeof (pstat));
8463 pstat.pr_pid = pid & 0xffff;
8464 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8465 NT_PSTATUS, &pstat, sizeof (pstat));
8466 return buf;
8468 else
8469 #endif
8471 pstatus_t pstat;
8473 memset (&pstat, 0, sizeof (pstat));
8474 pstat.pr_pid = pid & 0xffff;
8475 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8476 NT_PSTATUS, &pstat, sizeof (pstat));
8477 return buf;
8480 #endif /* HAVE_PSTATUS_T */
8482 char *
8483 elfcore_write_prfpreg (bfd *abfd,
8484 char *buf,
8485 int *bufsiz,
8486 const void *fpregs,
8487 int size)
8489 const char *note_name = "CORE";
8490 return elfcore_write_note (abfd, buf, bufsiz,
8491 note_name, NT_FPREGSET, fpregs, size);
8494 char *
8495 elfcore_write_prxfpreg (bfd *abfd,
8496 char *buf,
8497 int *bufsiz,
8498 const void *xfpregs,
8499 int size)
8501 char *note_name = "LINUX";
8502 return elfcore_write_note (abfd, buf, bufsiz,
8503 note_name, NT_PRXFPREG, xfpregs, size);
8506 char *
8507 elfcore_write_ppc_vmx (bfd *abfd,
8508 char *buf,
8509 int *bufsiz,
8510 const void *ppc_vmx,
8511 int size)
8513 char *note_name = "LINUX";
8514 return elfcore_write_note (abfd, buf, bufsiz,
8515 note_name, NT_PPC_VMX, ppc_vmx, size);
8518 char *
8519 elfcore_write_ppc_vsx (bfd *abfd,
8520 char *buf,
8521 int *bufsiz,
8522 const void *ppc_vsx,
8523 int size)
8525 char *note_name = "LINUX";
8526 return elfcore_write_note (abfd, buf, bufsiz,
8527 note_name, NT_PPC_VSX, ppc_vsx, size);
8530 char *
8531 elfcore_write_register_note (bfd *abfd,
8532 char *buf,
8533 int *bufsiz,
8534 const char *section,
8535 const void *data,
8536 int size)
8538 if (strcmp (section, ".reg2") == 0)
8539 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
8540 if (strcmp (section, ".reg-xfp") == 0)
8541 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
8542 if (strcmp (section, ".reg-ppc-vmx") == 0)
8543 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
8544 if (strcmp (section, ".reg-ppc-vsx") == 0)
8545 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
8546 return NULL;
8549 static bfd_boolean
8550 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
8552 char *p;
8554 p = buf;
8555 while (p < buf + size)
8557 /* FIXME: bad alignment assumption. */
8558 Elf_External_Note *xnp = (Elf_External_Note *) p;
8559 Elf_Internal_Note in;
8561 if (offsetof (Elf_External_Note, name) > buf - p + size)
8562 return FALSE;
8564 in.type = H_GET_32 (abfd, xnp->type);
8566 in.namesz = H_GET_32 (abfd, xnp->namesz);
8567 in.namedata = xnp->name;
8568 if (in.namesz > buf - in.namedata + size)
8569 return FALSE;
8571 in.descsz = H_GET_32 (abfd, xnp->descsz);
8572 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
8573 in.descpos = offset + (in.descdata - buf);
8574 if (in.descsz != 0
8575 && (in.descdata >= buf + size
8576 || in.descsz > buf - in.descdata + size))
8577 return FALSE;
8579 switch (bfd_get_format (abfd))
8581 default:
8582 return TRUE;
8584 case bfd_core:
8585 if (CONST_STRNEQ (in.namedata, "NetBSD-CORE"))
8587 if (! elfcore_grok_netbsd_note (abfd, &in))
8588 return FALSE;
8590 else if (CONST_STRNEQ (in.namedata, "QNX"))
8592 if (! elfcore_grok_nto_note (abfd, &in))
8593 return FALSE;
8595 else if (CONST_STRNEQ (in.namedata, "SPU/"))
8597 if (! elfcore_grok_spu_note (abfd, &in))
8598 return FALSE;
8600 else
8602 if (! elfcore_grok_note (abfd, &in))
8603 return FALSE;
8605 break;
8607 case bfd_object:
8608 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
8610 if (! elfobj_grok_gnu_note (abfd, &in))
8611 return FALSE;
8613 break;
8616 p = in.descdata + BFD_ALIGN (in.descsz, 4);
8619 return TRUE;
8622 static bfd_boolean
8623 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
8625 char *buf;
8627 if (size <= 0)
8628 return TRUE;
8630 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
8631 return FALSE;
8633 buf = bfd_malloc (size);
8634 if (buf == NULL)
8635 return FALSE;
8637 if (bfd_bread (buf, size, abfd) != size
8638 || !elf_parse_notes (abfd, buf, size, offset))
8640 free (buf);
8641 return FALSE;
8644 free (buf);
8645 return TRUE;
8648 /* Providing external access to the ELF program header table. */
8650 /* Return an upper bound on the number of bytes required to store a
8651 copy of ABFD's program header table entries. Return -1 if an error
8652 occurs; bfd_get_error will return an appropriate code. */
8654 long
8655 bfd_get_elf_phdr_upper_bound (bfd *abfd)
8657 if (abfd->xvec->flavour != bfd_target_elf_flavour)
8659 bfd_set_error (bfd_error_wrong_format);
8660 return -1;
8663 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
8666 /* Copy ABFD's program header table entries to *PHDRS. The entries
8667 will be stored as an array of Elf_Internal_Phdr structures, as
8668 defined in include/elf/internal.h. To find out how large the
8669 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
8671 Return the number of program header table entries read, or -1 if an
8672 error occurs; bfd_get_error will return an appropriate code. */
8675 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
8677 int num_phdrs;
8679 if (abfd->xvec->flavour != bfd_target_elf_flavour)
8681 bfd_set_error (bfd_error_wrong_format);
8682 return -1;
8685 num_phdrs = elf_elfheader (abfd)->e_phnum;
8686 memcpy (phdrs, elf_tdata (abfd)->phdr,
8687 num_phdrs * sizeof (Elf_Internal_Phdr));
8689 return num_phdrs;
8692 enum elf_reloc_type_class
8693 _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
8695 return reloc_class_normal;
8698 /* For RELA architectures, return the relocation value for a
8699 relocation against a local symbol. */
8701 bfd_vma
8702 _bfd_elf_rela_local_sym (bfd *abfd,
8703 Elf_Internal_Sym *sym,
8704 asection **psec,
8705 Elf_Internal_Rela *rel)
8707 asection *sec = *psec;
8708 bfd_vma relocation;
8710 relocation = (sec->output_section->vma
8711 + sec->output_offset
8712 + sym->st_value);
8713 if ((sec->flags & SEC_MERGE)
8714 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
8715 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
8717 rel->r_addend =
8718 _bfd_merged_section_offset (abfd, psec,
8719 elf_section_data (sec)->sec_info,
8720 sym->st_value + rel->r_addend);
8721 if (sec != *psec)
8723 /* If we have changed the section, and our original section is
8724 marked with SEC_EXCLUDE, it means that the original
8725 SEC_MERGE section has been completely subsumed in some
8726 other SEC_MERGE section. In this case, we need to leave
8727 some info around for --emit-relocs. */
8728 if ((sec->flags & SEC_EXCLUDE) != 0)
8729 sec->kept_section = *psec;
8730 sec = *psec;
8732 rel->r_addend -= relocation;
8733 rel->r_addend += sec->output_section->vma + sec->output_offset;
8735 return relocation;
8738 bfd_vma
8739 _bfd_elf_rel_local_sym (bfd *abfd,
8740 Elf_Internal_Sym *sym,
8741 asection **psec,
8742 bfd_vma addend)
8744 asection *sec = *psec;
8746 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
8747 return sym->st_value + addend;
8749 return _bfd_merged_section_offset (abfd, psec,
8750 elf_section_data (sec)->sec_info,
8751 sym->st_value + addend);
8754 bfd_vma
8755 _bfd_elf_section_offset (bfd *abfd,
8756 struct bfd_link_info *info,
8757 asection *sec,
8758 bfd_vma offset)
8760 switch (sec->sec_info_type)
8762 case ELF_INFO_TYPE_STABS:
8763 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
8764 offset);
8765 case ELF_INFO_TYPE_EH_FRAME:
8766 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
8767 default:
8768 return offset;
8772 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
8773 reconstruct an ELF file by reading the segments out of remote memory
8774 based on the ELF file header at EHDR_VMA and the ELF program headers it
8775 points to. If not null, *LOADBASEP is filled in with the difference
8776 between the VMAs from which the segments were read, and the VMAs the
8777 file headers (and hence BFD's idea of each section's VMA) put them at.
8779 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
8780 remote memory at target address VMA into the local buffer at MYADDR; it
8781 should return zero on success or an `errno' code on failure. TEMPL must
8782 be a BFD for an ELF target with the word size and byte order found in
8783 the remote memory. */
8785 bfd *
8786 bfd_elf_bfd_from_remote_memory
8787 (bfd *templ,
8788 bfd_vma ehdr_vma,
8789 bfd_vma *loadbasep,
8790 int (*target_read_memory) (bfd_vma, bfd_byte *, int))
8792 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
8793 (templ, ehdr_vma, loadbasep, target_read_memory);
8796 long
8797 _bfd_elf_get_synthetic_symtab (bfd *abfd,
8798 long symcount ATTRIBUTE_UNUSED,
8799 asymbol **syms ATTRIBUTE_UNUSED,
8800 long dynsymcount,
8801 asymbol **dynsyms,
8802 asymbol **ret)
8804 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8805 asection *relplt;
8806 asymbol *s;
8807 const char *relplt_name;
8808 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
8809 arelent *p;
8810 long count, i, n;
8811 size_t size;
8812 Elf_Internal_Shdr *hdr;
8813 char *names;
8814 asection *plt;
8816 *ret = NULL;
8818 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
8819 return 0;
8821 if (dynsymcount <= 0)
8822 return 0;
8824 if (!bed->plt_sym_val)
8825 return 0;
8827 relplt_name = bed->relplt_name;
8828 if (relplt_name == NULL)
8829 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
8830 relplt = bfd_get_section_by_name (abfd, relplt_name);
8831 if (relplt == NULL)
8832 return 0;
8834 hdr = &elf_section_data (relplt)->this_hdr;
8835 if (hdr->sh_link != elf_dynsymtab (abfd)
8836 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
8837 return 0;
8839 plt = bfd_get_section_by_name (abfd, ".plt");
8840 if (plt == NULL)
8841 return 0;
8843 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8844 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
8845 return -1;
8847 count = relplt->size / hdr->sh_entsize;
8848 size = count * sizeof (asymbol);
8849 p = relplt->relocation;
8850 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
8851 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
8853 s = *ret = bfd_malloc (size);
8854 if (s == NULL)
8855 return -1;
8857 names = (char *) (s + count);
8858 p = relplt->relocation;
8859 n = 0;
8860 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
8862 size_t len;
8863 bfd_vma addr;
8865 addr = bed->plt_sym_val (i, plt, p);
8866 if (addr == (bfd_vma) -1)
8867 continue;
8869 *s = **p->sym_ptr_ptr;
8870 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
8871 we are defining a symbol, ensure one of them is set. */
8872 if ((s->flags & BSF_LOCAL) == 0)
8873 s->flags |= BSF_GLOBAL;
8874 s->flags |= BSF_SYNTHETIC;
8875 s->section = plt;
8876 s->value = addr - plt->vma;
8877 s->name = names;
8878 s->udata.p = NULL;
8879 len = strlen ((*p->sym_ptr_ptr)->name);
8880 memcpy (names, (*p->sym_ptr_ptr)->name, len);
8881 names += len;
8882 memcpy (names, "@plt", sizeof ("@plt"));
8883 names += sizeof ("@plt");
8884 ++s, ++n;
8887 return n;
8890 /* It is only used by x86-64 so far. */
8891 asection _bfd_elf_large_com_section
8892 = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
8893 SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
8895 void
8896 _bfd_elf_set_osabi (bfd * abfd,
8897 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
8899 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
8901 i_ehdrp = elf_elfheader (abfd);
8903 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
8907 /* Return TRUE for ELF symbol types that represent functions.
8908 This is the default version of this function, which is sufficient for
8909 most targets. It returns true if TYPE is STT_FUNC. */
8911 bfd_boolean
8912 _bfd_elf_is_function_type (unsigned int type)
8914 return (type == STT_FUNC);