* configure.tgt (mips64*el-*-linux-gnu*): Define targ_extra_libpath
[binutils.git] / bfd / elf.c
blobfb587d51281bf0933ee51886c948deaf0d86f1ca
1 /* ELF executable support for BFD.
3 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 2002, 2003, 2004, 2005 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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* SECTION
24 ELF backends
26 BFD support for ELF formats is being worked on.
27 Currently, the best supported back ends are for sparc and i386
28 (running svr4 or Solaris 2).
30 Documentation of the internals of the support code still needs
31 to be written. The code is changing quickly enough that we
32 haven't bothered yet. */
34 /* For sparc64-cross-sparc32. */
35 #define _SYSCALL32
36 #include "bfd.h"
37 #include "sysdep.h"
38 #include "bfdlink.h"
39 #include "libbfd.h"
40 #define ARCH_SIZE 0
41 #include "elf-bfd.h"
42 #include "libiberty.h"
44 static int elf_sort_sections (const void *, const void *);
45 static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
46 static bfd_boolean prep_headers (bfd *);
47 static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ;
48 static bfd_boolean elfcore_read_notes (bfd *, file_ptr, bfd_size_type) ;
50 /* Swap version information in and out. The version information is
51 currently size independent. If that ever changes, this code will
52 need to move into elfcode.h. */
54 /* Swap in a Verdef structure. */
56 void
57 _bfd_elf_swap_verdef_in (bfd *abfd,
58 const Elf_External_Verdef *src,
59 Elf_Internal_Verdef *dst)
61 dst->vd_version = H_GET_16 (abfd, src->vd_version);
62 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
63 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
64 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
65 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
66 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
67 dst->vd_next = H_GET_32 (abfd, src->vd_next);
70 /* Swap out a Verdef structure. */
72 void
73 _bfd_elf_swap_verdef_out (bfd *abfd,
74 const Elf_Internal_Verdef *src,
75 Elf_External_Verdef *dst)
77 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
78 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
79 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
80 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
81 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
82 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
83 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
86 /* Swap in a Verdaux structure. */
88 void
89 _bfd_elf_swap_verdaux_in (bfd *abfd,
90 const Elf_External_Verdaux *src,
91 Elf_Internal_Verdaux *dst)
93 dst->vda_name = H_GET_32 (abfd, src->vda_name);
94 dst->vda_next = H_GET_32 (abfd, src->vda_next);
97 /* Swap out a Verdaux structure. */
99 void
100 _bfd_elf_swap_verdaux_out (bfd *abfd,
101 const Elf_Internal_Verdaux *src,
102 Elf_External_Verdaux *dst)
104 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
105 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
108 /* Swap in a Verneed structure. */
110 void
111 _bfd_elf_swap_verneed_in (bfd *abfd,
112 const Elf_External_Verneed *src,
113 Elf_Internal_Verneed *dst)
115 dst->vn_version = H_GET_16 (abfd, src->vn_version);
116 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
117 dst->vn_file = H_GET_32 (abfd, src->vn_file);
118 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
119 dst->vn_next = H_GET_32 (abfd, src->vn_next);
122 /* Swap out a Verneed structure. */
124 void
125 _bfd_elf_swap_verneed_out (bfd *abfd,
126 const Elf_Internal_Verneed *src,
127 Elf_External_Verneed *dst)
129 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
130 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
131 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
132 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
133 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
136 /* Swap in a Vernaux structure. */
138 void
139 _bfd_elf_swap_vernaux_in (bfd *abfd,
140 const Elf_External_Vernaux *src,
141 Elf_Internal_Vernaux *dst)
143 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
144 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
145 dst->vna_other = H_GET_16 (abfd, src->vna_other);
146 dst->vna_name = H_GET_32 (abfd, src->vna_name);
147 dst->vna_next = H_GET_32 (abfd, src->vna_next);
150 /* Swap out a Vernaux structure. */
152 void
153 _bfd_elf_swap_vernaux_out (bfd *abfd,
154 const Elf_Internal_Vernaux *src,
155 Elf_External_Vernaux *dst)
157 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
158 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
159 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
160 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
161 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
164 /* Swap in a Versym structure. */
166 void
167 _bfd_elf_swap_versym_in (bfd *abfd,
168 const Elf_External_Versym *src,
169 Elf_Internal_Versym *dst)
171 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
174 /* Swap out a Versym structure. */
176 void
177 _bfd_elf_swap_versym_out (bfd *abfd,
178 const Elf_Internal_Versym *src,
179 Elf_External_Versym *dst)
181 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
184 /* Standard ELF hash function. Do not change this function; you will
185 cause invalid hash tables to be generated. */
187 unsigned long
188 bfd_elf_hash (const char *namearg)
190 const unsigned char *name = (const unsigned char *) namearg;
191 unsigned long h = 0;
192 unsigned long g;
193 int ch;
195 while ((ch = *name++) != '\0')
197 h = (h << 4) + ch;
198 if ((g = (h & 0xf0000000)) != 0)
200 h ^= g >> 24;
201 /* The ELF ABI says `h &= ~g', but this is equivalent in
202 this case and on some machines one insn instead of two. */
203 h ^= g;
206 return h & 0xffffffff;
209 /* Read a specified number of bytes at a specified offset in an ELF
210 file, into a newly allocated buffer, and return a pointer to the
211 buffer. */
213 static char *
214 elf_read (bfd *abfd, file_ptr offset, bfd_size_type size)
216 char *buf;
218 if ((buf = bfd_alloc (abfd, size)) == NULL)
219 return NULL;
220 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
221 return NULL;
222 if (bfd_bread (buf, size, abfd) != size)
224 if (bfd_get_error () != bfd_error_system_call)
225 bfd_set_error (bfd_error_file_truncated);
226 return NULL;
228 return buf;
231 bfd_boolean
232 bfd_elf_mkobject (bfd *abfd)
234 /* This just does initialization. */
235 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
236 elf_tdata (abfd) = bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
237 if (elf_tdata (abfd) == 0)
238 return FALSE;
239 /* Since everything is done at close time, do we need any
240 initialization? */
242 return TRUE;
245 bfd_boolean
246 bfd_elf_mkcorefile (bfd *abfd)
248 /* I think this can be done just like an object file. */
249 return bfd_elf_mkobject (abfd);
252 char *
253 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
255 Elf_Internal_Shdr **i_shdrp;
256 char *shstrtab = NULL;
257 file_ptr offset;
258 bfd_size_type shstrtabsize;
260 i_shdrp = elf_elfsections (abfd);
261 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
262 return 0;
264 shstrtab = (char *) i_shdrp[shindex]->contents;
265 if (shstrtab == NULL)
267 /* No cached one, attempt to read, and cache what we read. */
268 offset = i_shdrp[shindex]->sh_offset;
269 shstrtabsize = i_shdrp[shindex]->sh_size;
270 shstrtab = elf_read (abfd, offset, shstrtabsize);
271 i_shdrp[shindex]->contents = shstrtab;
273 return shstrtab;
276 char *
277 bfd_elf_string_from_elf_section (bfd *abfd,
278 unsigned int shindex,
279 unsigned int strindex)
281 Elf_Internal_Shdr *hdr;
283 if (strindex == 0)
284 return "";
286 hdr = elf_elfsections (abfd)[shindex];
288 if (hdr->contents == NULL
289 && bfd_elf_get_str_section (abfd, shindex) == NULL)
290 return NULL;
292 if (strindex >= hdr->sh_size)
294 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
295 (*_bfd_error_handler)
296 (_("%B: invalid string offset %u >= %lu for section `%s'"),
297 abfd, strindex, (unsigned long) hdr->sh_size,
298 (shindex == shstrndx && strindex == hdr->sh_name
299 ? ".shstrtab"
300 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
301 return "";
304 return ((char *) hdr->contents) + strindex;
307 /* Read and convert symbols to internal format.
308 SYMCOUNT specifies the number of symbols to read, starting from
309 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
310 are non-NULL, they are used to store the internal symbols, external
311 symbols, and symbol section index extensions, respectively. */
313 Elf_Internal_Sym *
314 bfd_elf_get_elf_syms (bfd *ibfd,
315 Elf_Internal_Shdr *symtab_hdr,
316 size_t symcount,
317 size_t symoffset,
318 Elf_Internal_Sym *intsym_buf,
319 void *extsym_buf,
320 Elf_External_Sym_Shndx *extshndx_buf)
322 Elf_Internal_Shdr *shndx_hdr;
323 void *alloc_ext;
324 const bfd_byte *esym;
325 Elf_External_Sym_Shndx *alloc_extshndx;
326 Elf_External_Sym_Shndx *shndx;
327 Elf_Internal_Sym *isym;
328 Elf_Internal_Sym *isymend;
329 const struct elf_backend_data *bed;
330 size_t extsym_size;
331 bfd_size_type amt;
332 file_ptr pos;
334 if (symcount == 0)
335 return intsym_buf;
337 /* Normal syms might have section extension entries. */
338 shndx_hdr = NULL;
339 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
340 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
342 /* Read the symbols. */
343 alloc_ext = NULL;
344 alloc_extshndx = NULL;
345 bed = get_elf_backend_data (ibfd);
346 extsym_size = bed->s->sizeof_sym;
347 amt = symcount * extsym_size;
348 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
349 if (extsym_buf == NULL)
351 alloc_ext = bfd_malloc (amt);
352 extsym_buf = alloc_ext;
354 if (extsym_buf == NULL
355 || bfd_seek (ibfd, pos, SEEK_SET) != 0
356 || bfd_bread (extsym_buf, amt, ibfd) != amt)
358 intsym_buf = NULL;
359 goto out;
362 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
363 extshndx_buf = NULL;
364 else
366 amt = symcount * sizeof (Elf_External_Sym_Shndx);
367 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
368 if (extshndx_buf == NULL)
370 alloc_extshndx = bfd_malloc (amt);
371 extshndx_buf = alloc_extshndx;
373 if (extshndx_buf == NULL
374 || bfd_seek (ibfd, pos, SEEK_SET) != 0
375 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
377 intsym_buf = NULL;
378 goto out;
382 if (intsym_buf == NULL)
384 bfd_size_type amt = symcount * sizeof (Elf_Internal_Sym);
385 intsym_buf = bfd_malloc (amt);
386 if (intsym_buf == NULL)
387 goto out;
390 /* Convert the symbols to internal form. */
391 isymend = intsym_buf + symcount;
392 for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf;
393 isym < isymend;
394 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
395 (*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym);
397 out:
398 if (alloc_ext != NULL)
399 free (alloc_ext);
400 if (alloc_extshndx != NULL)
401 free (alloc_extshndx);
403 return intsym_buf;
406 /* Look up a symbol name. */
407 const char *
408 bfd_elf_sym_name (bfd *abfd,
409 Elf_Internal_Shdr *symtab_hdr,
410 Elf_Internal_Sym *isym)
412 unsigned int iname = isym->st_name;
413 unsigned int shindex = symtab_hdr->sh_link;
414 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
415 /* Check for a bogus st_shndx to avoid crashing. */
416 && isym->st_shndx < elf_numsections (abfd)
417 && !(isym->st_shndx >= SHN_LORESERVE && isym->st_shndx <= SHN_HIRESERVE))
419 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
420 shindex = elf_elfheader (abfd)->e_shstrndx;
423 return bfd_elf_string_from_elf_section (abfd, shindex, iname);
426 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
427 sections. The first element is the flags, the rest are section
428 pointers. */
430 typedef union elf_internal_group {
431 Elf_Internal_Shdr *shdr;
432 unsigned int flags;
433 } Elf_Internal_Group;
435 /* Return the name of the group signature symbol. Why isn't the
436 signature just a string? */
438 static const char *
439 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
441 Elf_Internal_Shdr *hdr;
442 unsigned char esym[sizeof (Elf64_External_Sym)];
443 Elf_External_Sym_Shndx eshndx;
444 Elf_Internal_Sym isym;
446 /* First we need to ensure the symbol table is available. */
447 if (! bfd_section_from_shdr (abfd, ghdr->sh_link))
448 return NULL;
450 /* Go read the symbol. */
451 hdr = &elf_tdata (abfd)->symtab_hdr;
452 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
453 &isym, esym, &eshndx) == NULL)
454 return NULL;
456 return bfd_elf_sym_name (abfd, hdr, &isym);
459 /* Set next_in_group list pointer, and group name for NEWSECT. */
461 static bfd_boolean
462 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
464 unsigned int num_group = elf_tdata (abfd)->num_group;
466 /* If num_group is zero, read in all SHT_GROUP sections. The count
467 is set to -1 if there are no SHT_GROUP sections. */
468 if (num_group == 0)
470 unsigned int i, shnum;
472 /* First count the number of groups. If we have a SHT_GROUP
473 section with just a flag word (ie. sh_size is 4), ignore it. */
474 shnum = elf_numsections (abfd);
475 num_group = 0;
476 for (i = 0; i < shnum; i++)
478 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
479 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
480 num_group += 1;
483 if (num_group == 0)
484 num_group = (unsigned) -1;
485 elf_tdata (abfd)->num_group = num_group;
487 if (num_group > 0)
489 /* We keep a list of elf section headers for group sections,
490 so we can find them quickly. */
491 bfd_size_type amt = num_group * sizeof (Elf_Internal_Shdr *);
492 elf_tdata (abfd)->group_sect_ptr = bfd_alloc (abfd, amt);
493 if (elf_tdata (abfd)->group_sect_ptr == NULL)
494 return FALSE;
496 num_group = 0;
497 for (i = 0; i < shnum; i++)
499 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
500 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
502 unsigned char *src;
503 Elf_Internal_Group *dest;
505 /* Add to list of sections. */
506 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
507 num_group += 1;
509 /* Read the raw contents. */
510 BFD_ASSERT (sizeof (*dest) >= 4);
511 amt = shdr->sh_size * sizeof (*dest) / 4;
512 shdr->contents = bfd_alloc (abfd, amt);
513 if (shdr->contents == NULL
514 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
515 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
516 != shdr->sh_size))
517 return FALSE;
519 /* Translate raw contents, a flag word followed by an
520 array of elf section indices all in target byte order,
521 to the flag word followed by an array of elf section
522 pointers. */
523 src = shdr->contents + shdr->sh_size;
524 dest = (Elf_Internal_Group *) (shdr->contents + amt);
525 while (1)
527 unsigned int idx;
529 src -= 4;
530 --dest;
531 idx = H_GET_32 (abfd, src);
532 if (src == shdr->contents)
534 dest->flags = idx;
535 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
536 shdr->bfd_section->flags
537 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
538 break;
540 if (idx >= shnum)
542 ((*_bfd_error_handler)
543 (_("%B: invalid SHT_GROUP entry"), abfd));
544 idx = 0;
546 dest->shdr = elf_elfsections (abfd)[idx];
553 if (num_group != (unsigned) -1)
555 unsigned int i;
557 for (i = 0; i < num_group; i++)
559 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
560 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
561 unsigned int n_elt = shdr->sh_size / 4;
563 /* Look through this group's sections to see if current
564 section is a member. */
565 while (--n_elt != 0)
566 if ((++idx)->shdr == hdr)
568 asection *s = NULL;
570 /* We are a member of this group. Go looking through
571 other members to see if any others are linked via
572 next_in_group. */
573 idx = (Elf_Internal_Group *) shdr->contents;
574 n_elt = shdr->sh_size / 4;
575 while (--n_elt != 0)
576 if ((s = (++idx)->shdr->bfd_section) != NULL
577 && elf_next_in_group (s) != NULL)
578 break;
579 if (n_elt != 0)
581 /* Snarf the group name from other member, and
582 insert current section in circular list. */
583 elf_group_name (newsect) = elf_group_name (s);
584 elf_next_in_group (newsect) = elf_next_in_group (s);
585 elf_next_in_group (s) = newsect;
587 else
589 const char *gname;
591 gname = group_signature (abfd, shdr);
592 if (gname == NULL)
593 return FALSE;
594 elf_group_name (newsect) = gname;
596 /* Start a circular list with one element. */
597 elf_next_in_group (newsect) = newsect;
600 /* If the group section has been created, point to the
601 new member. */
602 if (shdr->bfd_section != NULL)
603 elf_next_in_group (shdr->bfd_section) = newsect;
605 i = num_group - 1;
606 break;
611 if (elf_group_name (newsect) == NULL)
613 (*_bfd_error_handler) (_("%B: no group info for section %A"),
614 abfd, newsect);
616 return TRUE;
619 bfd_boolean
620 _bfd_elf_setup_group_pointers (bfd *abfd)
622 unsigned int i;
623 unsigned int num_group = elf_tdata (abfd)->num_group;
624 bfd_boolean result = TRUE;
626 if (num_group == (unsigned) -1)
627 return result;
629 for (i = 0; i < num_group; i++)
631 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
632 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
633 unsigned int n_elt = shdr->sh_size / 4;
635 while (--n_elt != 0)
636 if ((++idx)->shdr->bfd_section)
637 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
638 else if (idx->shdr->sh_type == SHT_RELA
639 || idx->shdr->sh_type == SHT_REL)
640 /* We won't include relocation sections in section groups in
641 output object files. We adjust the group section size here
642 so that relocatable link will work correctly when
643 relocation sections are in section group in input object
644 files. */
645 shdr->bfd_section->size -= 4;
646 else
648 /* There are some unknown sections in the group. */
649 (*_bfd_error_handler)
650 (_("%B: unknown [%d] section `%s' in group [%s]"),
651 abfd,
652 (unsigned int) idx->shdr->sh_type,
653 bfd_elf_string_from_elf_section (abfd,
654 (elf_elfheader (abfd)
655 ->e_shstrndx),
656 idx->shdr->sh_name),
657 shdr->bfd_section->name);
658 result = FALSE;
661 return result;
664 bfd_boolean
665 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
667 return elf_next_in_group (sec) != NULL;
670 /* Make a BFD section from an ELF section. We store a pointer to the
671 BFD section in the bfd_section field of the header. */
673 bfd_boolean
674 _bfd_elf_make_section_from_shdr (bfd *abfd,
675 Elf_Internal_Shdr *hdr,
676 const char *name)
678 asection *newsect;
679 flagword flags;
680 const struct elf_backend_data *bed;
682 if (hdr->bfd_section != NULL)
684 BFD_ASSERT (strcmp (name,
685 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
686 return TRUE;
689 newsect = bfd_make_section_anyway (abfd, name);
690 if (newsect == NULL)
691 return FALSE;
693 hdr->bfd_section = newsect;
694 elf_section_data (newsect)->this_hdr = *hdr;
696 /* Always use the real type/flags. */
697 elf_section_type (newsect) = hdr->sh_type;
698 elf_section_flags (newsect) = hdr->sh_flags;
700 newsect->filepos = hdr->sh_offset;
702 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
703 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
704 || ! bfd_set_section_alignment (abfd, newsect,
705 bfd_log2 ((bfd_vma) hdr->sh_addralign)))
706 return FALSE;
708 flags = SEC_NO_FLAGS;
709 if (hdr->sh_type != SHT_NOBITS)
710 flags |= SEC_HAS_CONTENTS;
711 if (hdr->sh_type == SHT_GROUP)
712 flags |= SEC_GROUP | SEC_EXCLUDE;
713 if ((hdr->sh_flags & SHF_ALLOC) != 0)
715 flags |= SEC_ALLOC;
716 if (hdr->sh_type != SHT_NOBITS)
717 flags |= SEC_LOAD;
719 if ((hdr->sh_flags & SHF_WRITE) == 0)
720 flags |= SEC_READONLY;
721 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
722 flags |= SEC_CODE;
723 else if ((flags & SEC_LOAD) != 0)
724 flags |= SEC_DATA;
725 if ((hdr->sh_flags & SHF_MERGE) != 0)
727 flags |= SEC_MERGE;
728 newsect->entsize = hdr->sh_entsize;
729 if ((hdr->sh_flags & SHF_STRINGS) != 0)
730 flags |= SEC_STRINGS;
732 if (hdr->sh_flags & SHF_GROUP)
733 if (!setup_group (abfd, hdr, newsect))
734 return FALSE;
735 if ((hdr->sh_flags & SHF_TLS) != 0)
736 flags |= SEC_THREAD_LOCAL;
738 /* The debugging sections appear to be recognized only by name, not
739 any sort of flag. */
741 static const char *debug_sec_names [] =
743 ".debug",
744 ".gnu.linkonce.wi.",
745 ".line",
746 ".stab"
748 int i;
750 for (i = ARRAY_SIZE (debug_sec_names); i--;)
751 if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0)
752 break;
754 if (i >= 0)
755 flags |= SEC_DEBUGGING;
758 /* As a GNU extension, if the name begins with .gnu.linkonce, we
759 only link a single copy of the section. This is used to support
760 g++. g++ will emit each template expansion in its own section.
761 The symbols will be defined as weak, so that multiple definitions
762 are permitted. The GNU linker extension is to actually discard
763 all but one of the sections. */
764 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0
765 && elf_next_in_group (newsect) == NULL)
766 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
768 bed = get_elf_backend_data (abfd);
769 if (bed->elf_backend_section_flags)
770 if (! bed->elf_backend_section_flags (&flags, hdr))
771 return FALSE;
773 if (! bfd_set_section_flags (abfd, newsect, flags))
774 return FALSE;
776 if ((flags & SEC_ALLOC) != 0)
778 Elf_Internal_Phdr *phdr;
779 unsigned int i;
781 /* Look through the phdrs to see if we need to adjust the lma.
782 If all the p_paddr fields are zero, we ignore them, since
783 some ELF linkers produce such output. */
784 phdr = elf_tdata (abfd)->phdr;
785 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
787 if (phdr->p_paddr != 0)
788 break;
790 if (i < elf_elfheader (abfd)->e_phnum)
792 phdr = elf_tdata (abfd)->phdr;
793 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
795 /* This section is part of this segment if its file
796 offset plus size lies within the segment's memory
797 span and, if the section is loaded, the extent of the
798 loaded data lies within the extent of the segment.
800 Note - we used to check the p_paddr field as well, and
801 refuse to set the LMA if it was 0. This is wrong
802 though, as a perfectly valid initialised segment can
803 have a p_paddr of zero. Some architectures, eg ARM,
804 place special significance on the address 0 and
805 executables need to be able to have a segment which
806 covers this address. */
807 if (phdr->p_type == PT_LOAD
808 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
809 && (hdr->sh_offset + hdr->sh_size
810 <= phdr->p_offset + phdr->p_memsz)
811 && ((flags & SEC_LOAD) == 0
812 || (hdr->sh_offset + hdr->sh_size
813 <= phdr->p_offset + phdr->p_filesz)))
815 if ((flags & SEC_LOAD) == 0)
816 newsect->lma = (phdr->p_paddr
817 + hdr->sh_addr - phdr->p_vaddr);
818 else
819 /* We used to use the same adjustment for SEC_LOAD
820 sections, but that doesn't work if the segment
821 is packed with code from multiple VMAs.
822 Instead we calculate the section LMA based on
823 the segment LMA. It is assumed that the
824 segment will contain sections with contiguous
825 LMAs, even if the VMAs are not. */
826 newsect->lma = (phdr->p_paddr
827 + hdr->sh_offset - phdr->p_offset);
829 /* With contiguous segments, we can't tell from file
830 offsets whether a section with zero size should
831 be placed at the end of one segment or the
832 beginning of the next. Decide based on vaddr. */
833 if (hdr->sh_addr >= phdr->p_vaddr
834 && (hdr->sh_addr + hdr->sh_size
835 <= phdr->p_vaddr + phdr->p_memsz))
836 break;
842 return TRUE;
846 INTERNAL_FUNCTION
847 bfd_elf_find_section
849 SYNOPSIS
850 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
852 DESCRIPTION
853 Helper functions for GDB to locate the string tables.
854 Since BFD hides string tables from callers, GDB needs to use an
855 internal hook to find them. Sun's .stabstr, in particular,
856 isn't even pointed to by the .stab section, so ordinary
857 mechanisms wouldn't work to find it, even if we had some.
860 struct elf_internal_shdr *
861 bfd_elf_find_section (bfd *abfd, char *name)
863 Elf_Internal_Shdr **i_shdrp;
864 char *shstrtab;
865 unsigned int max;
866 unsigned int i;
868 i_shdrp = elf_elfsections (abfd);
869 if (i_shdrp != NULL)
871 shstrtab = bfd_elf_get_str_section (abfd,
872 elf_elfheader (abfd)->e_shstrndx);
873 if (shstrtab != NULL)
875 max = elf_numsections (abfd);
876 for (i = 1; i < max; i++)
877 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
878 return i_shdrp[i];
881 return 0;
884 const char *const bfd_elf_section_type_names[] = {
885 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
886 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
887 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
890 /* ELF relocs are against symbols. If we are producing relocatable
891 output, and the reloc is against an external symbol, and nothing
892 has given us any additional addend, the resulting reloc will also
893 be against the same symbol. In such a case, we don't want to
894 change anything about the way the reloc is handled, since it will
895 all be done at final link time. Rather than put special case code
896 into bfd_perform_relocation, all the reloc types use this howto
897 function. It just short circuits the reloc if producing
898 relocatable output against an external symbol. */
900 bfd_reloc_status_type
901 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
902 arelent *reloc_entry,
903 asymbol *symbol,
904 void *data ATTRIBUTE_UNUSED,
905 asection *input_section,
906 bfd *output_bfd,
907 char **error_message ATTRIBUTE_UNUSED)
909 if (output_bfd != NULL
910 && (symbol->flags & BSF_SECTION_SYM) == 0
911 && (! reloc_entry->howto->partial_inplace
912 || reloc_entry->addend == 0))
914 reloc_entry->address += input_section->output_offset;
915 return bfd_reloc_ok;
918 return bfd_reloc_continue;
921 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
923 static void
924 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
925 asection *sec)
927 BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE);
928 sec->sec_info_type = ELF_INFO_TYPE_NONE;
931 /* Finish SHF_MERGE section merging. */
933 bfd_boolean
934 _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
936 bfd *ibfd;
937 asection *sec;
939 if (!is_elf_hash_table (info->hash))
940 return FALSE;
942 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
943 if ((ibfd->flags & DYNAMIC) == 0)
944 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
945 if ((sec->flags & SEC_MERGE) != 0
946 && !bfd_is_abs_section (sec->output_section))
948 struct bfd_elf_section_data *secdata;
950 secdata = elf_section_data (sec);
951 if (! _bfd_add_merge_section (abfd,
952 &elf_hash_table (info)->merge_info,
953 sec, &secdata->sec_info))
954 return FALSE;
955 else if (secdata->sec_info)
956 sec->sec_info_type = ELF_INFO_TYPE_MERGE;
959 if (elf_hash_table (info)->merge_info != NULL)
960 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
961 merge_sections_remove_hook);
962 return TRUE;
965 void
966 _bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
968 sec->output_section = bfd_abs_section_ptr;
969 sec->output_offset = sec->vma;
970 if (!is_elf_hash_table (info->hash))
971 return;
973 sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
976 /* Copy the program header and other data from one object module to
977 another. */
979 bfd_boolean
980 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
982 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
983 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
984 return TRUE;
986 BFD_ASSERT (!elf_flags_init (obfd)
987 || (elf_elfheader (obfd)->e_flags
988 == elf_elfheader (ibfd)->e_flags));
990 elf_gp (obfd) = elf_gp (ibfd);
991 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
992 elf_flags_init (obfd) = TRUE;
993 return TRUE;
996 /* Print out the program headers. */
998 bfd_boolean
999 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1001 FILE *f = farg;
1002 Elf_Internal_Phdr *p;
1003 asection *s;
1004 bfd_byte *dynbuf = NULL;
1006 p = elf_tdata (abfd)->phdr;
1007 if (p != NULL)
1009 unsigned int i, c;
1011 fprintf (f, _("\nProgram Header:\n"));
1012 c = elf_elfheader (abfd)->e_phnum;
1013 for (i = 0; i < c; i++, p++)
1015 const char *pt;
1016 char buf[20];
1018 switch (p->p_type)
1020 case PT_NULL: pt = "NULL"; break;
1021 case PT_LOAD: pt = "LOAD"; break;
1022 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1023 case PT_INTERP: pt = "INTERP"; break;
1024 case PT_NOTE: pt = "NOTE"; break;
1025 case PT_SHLIB: pt = "SHLIB"; break;
1026 case PT_PHDR: pt = "PHDR"; break;
1027 case PT_TLS: pt = "TLS"; break;
1028 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1029 case PT_GNU_STACK: pt = "STACK"; break;
1030 case PT_GNU_RELRO: pt = "RELRO"; break;
1031 default: sprintf (buf, "0x%lx", p->p_type); pt = buf; break;
1033 fprintf (f, "%8s off 0x", pt);
1034 bfd_fprintf_vma (abfd, f, p->p_offset);
1035 fprintf (f, " vaddr 0x");
1036 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1037 fprintf (f, " paddr 0x");
1038 bfd_fprintf_vma (abfd, f, p->p_paddr);
1039 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1040 fprintf (f, " filesz 0x");
1041 bfd_fprintf_vma (abfd, f, p->p_filesz);
1042 fprintf (f, " memsz 0x");
1043 bfd_fprintf_vma (abfd, f, p->p_memsz);
1044 fprintf (f, " flags %c%c%c",
1045 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1046 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1047 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1048 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1049 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1050 fprintf (f, "\n");
1054 s = bfd_get_section_by_name (abfd, ".dynamic");
1055 if (s != NULL)
1057 int elfsec;
1058 unsigned long shlink;
1059 bfd_byte *extdyn, *extdynend;
1060 size_t extdynsize;
1061 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1063 fprintf (f, _("\nDynamic Section:\n"));
1065 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1066 goto error_return;
1068 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1069 if (elfsec == -1)
1070 goto error_return;
1071 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1073 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1074 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1076 extdyn = dynbuf;
1077 extdynend = extdyn + s->size;
1078 for (; extdyn < extdynend; extdyn += extdynsize)
1080 Elf_Internal_Dyn dyn;
1081 const char *name;
1082 char ab[20];
1083 bfd_boolean stringp;
1085 (*swap_dyn_in) (abfd, extdyn, &dyn);
1087 if (dyn.d_tag == DT_NULL)
1088 break;
1090 stringp = FALSE;
1091 switch (dyn.d_tag)
1093 default:
1094 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1095 name = ab;
1096 break;
1098 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1099 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1100 case DT_PLTGOT: name = "PLTGOT"; break;
1101 case DT_HASH: name = "HASH"; break;
1102 case DT_STRTAB: name = "STRTAB"; break;
1103 case DT_SYMTAB: name = "SYMTAB"; break;
1104 case DT_RELA: name = "RELA"; break;
1105 case DT_RELASZ: name = "RELASZ"; break;
1106 case DT_RELAENT: name = "RELAENT"; break;
1107 case DT_STRSZ: name = "STRSZ"; break;
1108 case DT_SYMENT: name = "SYMENT"; break;
1109 case DT_INIT: name = "INIT"; break;
1110 case DT_FINI: name = "FINI"; break;
1111 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1112 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1113 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1114 case DT_REL: name = "REL"; break;
1115 case DT_RELSZ: name = "RELSZ"; break;
1116 case DT_RELENT: name = "RELENT"; break;
1117 case DT_PLTREL: name = "PLTREL"; break;
1118 case DT_DEBUG: name = "DEBUG"; break;
1119 case DT_TEXTREL: name = "TEXTREL"; break;
1120 case DT_JMPREL: name = "JMPREL"; break;
1121 case DT_BIND_NOW: name = "BIND_NOW"; break;
1122 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1123 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1124 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1125 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1126 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1127 case DT_FLAGS: name = "FLAGS"; break;
1128 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1129 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1130 case DT_CHECKSUM: name = "CHECKSUM"; break;
1131 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1132 case DT_MOVEENT: name = "MOVEENT"; break;
1133 case DT_MOVESZ: name = "MOVESZ"; break;
1134 case DT_FEATURE: name = "FEATURE"; break;
1135 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1136 case DT_SYMINSZ: name = "SYMINSZ"; break;
1137 case DT_SYMINENT: name = "SYMINENT"; break;
1138 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1139 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1140 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1141 case DT_PLTPAD: name = "PLTPAD"; break;
1142 case DT_MOVETAB: name = "MOVETAB"; break;
1143 case DT_SYMINFO: name = "SYMINFO"; break;
1144 case DT_RELACOUNT: name = "RELACOUNT"; break;
1145 case DT_RELCOUNT: name = "RELCOUNT"; break;
1146 case DT_FLAGS_1: name = "FLAGS_1"; break;
1147 case DT_VERSYM: name = "VERSYM"; break;
1148 case DT_VERDEF: name = "VERDEF"; break;
1149 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1150 case DT_VERNEED: name = "VERNEED"; break;
1151 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1152 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1153 case DT_USED: name = "USED"; break;
1154 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1157 fprintf (f, " %-11s ", name);
1158 if (! stringp)
1159 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
1160 else
1162 const char *string;
1163 unsigned int tagv = dyn.d_un.d_val;
1165 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1166 if (string == NULL)
1167 goto error_return;
1168 fprintf (f, "%s", string);
1170 fprintf (f, "\n");
1173 free (dynbuf);
1174 dynbuf = NULL;
1177 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1178 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1180 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1181 return FALSE;
1184 if (elf_dynverdef (abfd) != 0)
1186 Elf_Internal_Verdef *t;
1188 fprintf (f, _("\nVersion definitions:\n"));
1189 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1191 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1192 t->vd_flags, t->vd_hash, t->vd_nodename);
1193 if (t->vd_auxptr->vda_nextptr != NULL)
1195 Elf_Internal_Verdaux *a;
1197 fprintf (f, "\t");
1198 for (a = t->vd_auxptr->vda_nextptr;
1199 a != NULL;
1200 a = a->vda_nextptr)
1201 fprintf (f, "%s ", a->vda_nodename);
1202 fprintf (f, "\n");
1207 if (elf_dynverref (abfd) != 0)
1209 Elf_Internal_Verneed *t;
1211 fprintf (f, _("\nVersion References:\n"));
1212 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1214 Elf_Internal_Vernaux *a;
1216 fprintf (f, _(" required from %s:\n"), t->vn_filename);
1217 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1218 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1219 a->vna_flags, a->vna_other, a->vna_nodename);
1223 return TRUE;
1225 error_return:
1226 if (dynbuf != NULL)
1227 free (dynbuf);
1228 return FALSE;
1231 /* Display ELF-specific fields of a symbol. */
1233 void
1234 bfd_elf_print_symbol (bfd *abfd,
1235 void *filep,
1236 asymbol *symbol,
1237 bfd_print_symbol_type how)
1239 FILE *file = filep;
1240 switch (how)
1242 case bfd_print_symbol_name:
1243 fprintf (file, "%s", symbol->name);
1244 break;
1245 case bfd_print_symbol_more:
1246 fprintf (file, "elf ");
1247 bfd_fprintf_vma (abfd, file, symbol->value);
1248 fprintf (file, " %lx", (long) symbol->flags);
1249 break;
1250 case bfd_print_symbol_all:
1252 const char *section_name;
1253 const char *name = NULL;
1254 const struct elf_backend_data *bed;
1255 unsigned char st_other;
1256 bfd_vma val;
1258 section_name = symbol->section ? symbol->section->name : "(*none*)";
1260 bed = get_elf_backend_data (abfd);
1261 if (bed->elf_backend_print_symbol_all)
1262 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1264 if (name == NULL)
1266 name = symbol->name;
1267 bfd_print_symbol_vandf (abfd, file, symbol);
1270 fprintf (file, " %s\t", section_name);
1271 /* Print the "other" value for a symbol. For common symbols,
1272 we've already printed the size; now print the alignment.
1273 For other symbols, we have no specified alignment, and
1274 we've printed the address; now print the size. */
1275 if (bfd_is_com_section (symbol->section))
1276 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1277 else
1278 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1279 bfd_fprintf_vma (abfd, file, val);
1281 /* If we have version information, print it. */
1282 if (elf_tdata (abfd)->dynversym_section != 0
1283 && (elf_tdata (abfd)->dynverdef_section != 0
1284 || elf_tdata (abfd)->dynverref_section != 0))
1286 unsigned int vernum;
1287 const char *version_string;
1289 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1291 if (vernum == 0)
1292 version_string = "";
1293 else if (vernum == 1)
1294 version_string = "Base";
1295 else if (vernum <= elf_tdata (abfd)->cverdefs)
1296 version_string =
1297 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1298 else
1300 Elf_Internal_Verneed *t;
1302 version_string = "";
1303 for (t = elf_tdata (abfd)->verref;
1304 t != NULL;
1305 t = t->vn_nextref)
1307 Elf_Internal_Vernaux *a;
1309 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1311 if (a->vna_other == vernum)
1313 version_string = a->vna_nodename;
1314 break;
1320 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1321 fprintf (file, " %-11s", version_string);
1322 else
1324 int i;
1326 fprintf (file, " (%s)", version_string);
1327 for (i = 10 - strlen (version_string); i > 0; --i)
1328 putc (' ', file);
1332 /* If the st_other field is not zero, print it. */
1333 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1335 switch (st_other)
1337 case 0: break;
1338 case STV_INTERNAL: fprintf (file, " .internal"); break;
1339 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1340 case STV_PROTECTED: fprintf (file, " .protected"); break;
1341 default:
1342 /* Some other non-defined flags are also present, so print
1343 everything hex. */
1344 fprintf (file, " 0x%02x", (unsigned int) st_other);
1347 fprintf (file, " %s", name);
1349 break;
1353 /* Create an entry in an ELF linker hash table. */
1355 struct bfd_hash_entry *
1356 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1357 struct bfd_hash_table *table,
1358 const char *string)
1360 /* Allocate the structure if it has not already been allocated by a
1361 subclass. */
1362 if (entry == NULL)
1364 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
1365 if (entry == NULL)
1366 return entry;
1369 /* Call the allocation method of the superclass. */
1370 entry = _bfd_link_hash_newfunc (entry, table, string);
1371 if (entry != NULL)
1373 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
1374 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
1376 /* Set local fields. */
1377 ret->indx = -1;
1378 ret->dynindx = -1;
1379 ret->got = ret->plt = htab->init_refcount;
1380 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
1381 - offsetof (struct elf_link_hash_entry, size)));
1382 /* Assume that we have been called by a non-ELF symbol reader.
1383 This flag is then reset by the code which reads an ELF input
1384 file. This ensures that a symbol created by a non-ELF symbol
1385 reader will have the flag set correctly. */
1386 ret->non_elf = 1;
1389 return entry;
1392 /* Copy data from an indirect symbol to its direct symbol, hiding the
1393 old indirect symbol. Also used for copying flags to a weakdef. */
1395 void
1396 _bfd_elf_link_hash_copy_indirect (const struct elf_backend_data *bed,
1397 struct elf_link_hash_entry *dir,
1398 struct elf_link_hash_entry *ind)
1400 bfd_signed_vma tmp;
1401 bfd_signed_vma lowest_valid = bed->can_refcount;
1403 /* Copy down any references that we may have already seen to the
1404 symbol which just became indirect. */
1406 dir->ref_dynamic |= ind->ref_dynamic;
1407 dir->ref_regular |= ind->ref_regular;
1408 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
1409 dir->non_got_ref |= ind->non_got_ref;
1410 dir->needs_plt |= ind->needs_plt;
1411 dir->pointer_equality_needed |= ind->pointer_equality_needed;
1413 if (ind->root.type != bfd_link_hash_indirect)
1414 return;
1416 /* Copy over the global and procedure linkage table refcount entries.
1417 These may have been already set up by a check_relocs routine. */
1418 tmp = dir->got.refcount;
1419 if (tmp < lowest_valid)
1421 dir->got.refcount = ind->got.refcount;
1422 ind->got.refcount = tmp;
1424 else
1425 BFD_ASSERT (ind->got.refcount < lowest_valid);
1427 tmp = dir->plt.refcount;
1428 if (tmp < lowest_valid)
1430 dir->plt.refcount = ind->plt.refcount;
1431 ind->plt.refcount = tmp;
1433 else
1434 BFD_ASSERT (ind->plt.refcount < lowest_valid);
1436 if (dir->dynindx == -1)
1438 dir->dynindx = ind->dynindx;
1439 dir->dynstr_index = ind->dynstr_index;
1440 ind->dynindx = -1;
1441 ind->dynstr_index = 0;
1443 else
1444 BFD_ASSERT (ind->dynindx == -1);
1447 void
1448 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
1449 struct elf_link_hash_entry *h,
1450 bfd_boolean force_local)
1452 h->plt = elf_hash_table (info)->init_offset;
1453 h->needs_plt = 0;
1454 if (force_local)
1456 h->forced_local = 1;
1457 if (h->dynindx != -1)
1459 h->dynindx = -1;
1460 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1461 h->dynstr_index);
1466 /* Initialize an ELF linker hash table. */
1468 bfd_boolean
1469 _bfd_elf_link_hash_table_init
1470 (struct elf_link_hash_table *table,
1471 bfd *abfd,
1472 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
1473 struct bfd_hash_table *,
1474 const char *))
1476 bfd_boolean ret;
1478 table->dynamic_sections_created = FALSE;
1479 table->dynobj = NULL;
1480 /* Make sure can_refcount is extended to the width and signedness of
1481 init_refcount before we subtract one from it. */
1482 table->init_refcount.refcount = get_elf_backend_data (abfd)->can_refcount;
1483 table->init_refcount.refcount -= 1;
1484 table->init_offset.offset = -(bfd_vma) 1;
1485 /* The first dynamic symbol is a dummy. */
1486 table->dynsymcount = 1;
1487 table->dynstr = NULL;
1488 table->bucketcount = 0;
1489 table->needed = NULL;
1490 table->hgot = NULL;
1491 table->merge_info = NULL;
1492 memset (&table->stab_info, 0, sizeof (table->stab_info));
1493 memset (&table->eh_info, 0, sizeof (table->eh_info));
1494 table->dynlocal = NULL;
1495 table->runpath = NULL;
1496 table->tls_sec = NULL;
1497 table->tls_size = 0;
1498 table->loaded = NULL;
1500 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc);
1501 table->root.type = bfd_link_elf_hash_table;
1503 return ret;
1506 /* Create an ELF linker hash table. */
1508 struct bfd_link_hash_table *
1509 _bfd_elf_link_hash_table_create (bfd *abfd)
1511 struct elf_link_hash_table *ret;
1512 bfd_size_type amt = sizeof (struct elf_link_hash_table);
1514 ret = bfd_malloc (amt);
1515 if (ret == NULL)
1516 return NULL;
1518 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
1520 free (ret);
1521 return NULL;
1524 return &ret->root;
1527 /* This is a hook for the ELF emulation code in the generic linker to
1528 tell the backend linker what file name to use for the DT_NEEDED
1529 entry for a dynamic object. */
1531 void
1532 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
1534 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1535 && bfd_get_format (abfd) == bfd_object)
1536 elf_dt_name (abfd) = name;
1540 bfd_elf_get_dyn_lib_class (bfd *abfd)
1542 int lib_class;
1543 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1544 && bfd_get_format (abfd) == bfd_object)
1545 lib_class = elf_dyn_lib_class (abfd);
1546 else
1547 lib_class = 0;
1548 return lib_class;
1551 void
1552 bfd_elf_set_dyn_lib_class (bfd *abfd, int lib_class)
1554 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1555 && bfd_get_format (abfd) == bfd_object)
1556 elf_dyn_lib_class (abfd) = lib_class;
1559 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1560 the linker ELF emulation code. */
1562 struct bfd_link_needed_list *
1563 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
1564 struct bfd_link_info *info)
1566 if (! is_elf_hash_table (info->hash))
1567 return NULL;
1568 return elf_hash_table (info)->needed;
1571 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1572 hook for the linker ELF emulation code. */
1574 struct bfd_link_needed_list *
1575 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
1576 struct bfd_link_info *info)
1578 if (! is_elf_hash_table (info->hash))
1579 return NULL;
1580 return elf_hash_table (info)->runpath;
1583 /* Get the name actually used for a dynamic object for a link. This
1584 is the SONAME entry if there is one. Otherwise, it is the string
1585 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1587 const char *
1588 bfd_elf_get_dt_soname (bfd *abfd)
1590 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1591 && bfd_get_format (abfd) == bfd_object)
1592 return elf_dt_name (abfd);
1593 return NULL;
1596 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1597 the ELF linker emulation code. */
1599 bfd_boolean
1600 bfd_elf_get_bfd_needed_list (bfd *abfd,
1601 struct bfd_link_needed_list **pneeded)
1603 asection *s;
1604 bfd_byte *dynbuf = NULL;
1605 int elfsec;
1606 unsigned long shlink;
1607 bfd_byte *extdyn, *extdynend;
1608 size_t extdynsize;
1609 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1611 *pneeded = NULL;
1613 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1614 || bfd_get_format (abfd) != bfd_object)
1615 return TRUE;
1617 s = bfd_get_section_by_name (abfd, ".dynamic");
1618 if (s == NULL || s->size == 0)
1619 return TRUE;
1621 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1622 goto error_return;
1624 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1625 if (elfsec == -1)
1626 goto error_return;
1628 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1630 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1631 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1633 extdyn = dynbuf;
1634 extdynend = extdyn + s->size;
1635 for (; extdyn < extdynend; extdyn += extdynsize)
1637 Elf_Internal_Dyn dyn;
1639 (*swap_dyn_in) (abfd, extdyn, &dyn);
1641 if (dyn.d_tag == DT_NULL)
1642 break;
1644 if (dyn.d_tag == DT_NEEDED)
1646 const char *string;
1647 struct bfd_link_needed_list *l;
1648 unsigned int tagv = dyn.d_un.d_val;
1649 bfd_size_type amt;
1651 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1652 if (string == NULL)
1653 goto error_return;
1655 amt = sizeof *l;
1656 l = bfd_alloc (abfd, amt);
1657 if (l == NULL)
1658 goto error_return;
1660 l->by = abfd;
1661 l->name = string;
1662 l->next = *pneeded;
1663 *pneeded = l;
1667 free (dynbuf);
1669 return TRUE;
1671 error_return:
1672 if (dynbuf != NULL)
1673 free (dynbuf);
1674 return FALSE;
1677 /* Allocate an ELF string table--force the first byte to be zero. */
1679 struct bfd_strtab_hash *
1680 _bfd_elf_stringtab_init (void)
1682 struct bfd_strtab_hash *ret;
1684 ret = _bfd_stringtab_init ();
1685 if (ret != NULL)
1687 bfd_size_type loc;
1689 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1690 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1691 if (loc == (bfd_size_type) -1)
1693 _bfd_stringtab_free (ret);
1694 ret = NULL;
1697 return ret;
1700 /* ELF .o/exec file reading */
1702 /* Create a new bfd section from an ELF section header. */
1704 bfd_boolean
1705 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1707 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1708 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1709 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1710 const char *name;
1712 name = bfd_elf_string_from_elf_section (abfd,
1713 elf_elfheader (abfd)->e_shstrndx,
1714 hdr->sh_name);
1716 switch (hdr->sh_type)
1718 case SHT_NULL:
1719 /* Inactive section. Throw it away. */
1720 return TRUE;
1722 case SHT_PROGBITS: /* Normal section with contents. */
1723 case SHT_NOBITS: /* .bss section. */
1724 case SHT_HASH: /* .hash section. */
1725 case SHT_NOTE: /* .note section. */
1726 case SHT_INIT_ARRAY: /* .init_array section. */
1727 case SHT_FINI_ARRAY: /* .fini_array section. */
1728 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1729 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
1730 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1732 case SHT_DYNAMIC: /* Dynamic linking information. */
1733 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1734 return FALSE;
1735 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1737 Elf_Internal_Shdr *dynsymhdr;
1739 /* The shared libraries distributed with hpux11 have a bogus
1740 sh_link field for the ".dynamic" section. Find the
1741 string table for the ".dynsym" section instead. */
1742 if (elf_dynsymtab (abfd) != 0)
1744 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1745 hdr->sh_link = dynsymhdr->sh_link;
1747 else
1749 unsigned int i, num_sec;
1751 num_sec = elf_numsections (abfd);
1752 for (i = 1; i < num_sec; i++)
1754 dynsymhdr = elf_elfsections (abfd)[i];
1755 if (dynsymhdr->sh_type == SHT_DYNSYM)
1757 hdr->sh_link = dynsymhdr->sh_link;
1758 break;
1763 break;
1765 case SHT_SYMTAB: /* A symbol table */
1766 if (elf_onesymtab (abfd) == shindex)
1767 return TRUE;
1769 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1770 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1771 elf_onesymtab (abfd) = shindex;
1772 elf_tdata (abfd)->symtab_hdr = *hdr;
1773 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1774 abfd->flags |= HAS_SYMS;
1776 /* Sometimes a shared object will map in the symbol table. If
1777 SHF_ALLOC is set, and this is a shared object, then we also
1778 treat this section as a BFD section. We can not base the
1779 decision purely on SHF_ALLOC, because that flag is sometimes
1780 set in a relocatable object file, which would confuse the
1781 linker. */
1782 if ((hdr->sh_flags & SHF_ALLOC) != 0
1783 && (abfd->flags & DYNAMIC) != 0
1784 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1785 return FALSE;
1787 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1788 can't read symbols without that section loaded as well. It
1789 is most likely specified by the next section header. */
1790 if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex)
1792 unsigned int i, num_sec;
1794 num_sec = elf_numsections (abfd);
1795 for (i = shindex + 1; i < num_sec; i++)
1797 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1798 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1799 && hdr2->sh_link == shindex)
1800 break;
1802 if (i == num_sec)
1803 for (i = 1; i < shindex; i++)
1805 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1806 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1807 && hdr2->sh_link == shindex)
1808 break;
1810 if (i != shindex)
1811 return bfd_section_from_shdr (abfd, i);
1813 return TRUE;
1815 case SHT_DYNSYM: /* A dynamic symbol table */
1816 if (elf_dynsymtab (abfd) == shindex)
1817 return TRUE;
1819 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1820 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1821 elf_dynsymtab (abfd) = shindex;
1822 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1823 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1824 abfd->flags |= HAS_SYMS;
1826 /* Besides being a symbol table, we also treat this as a regular
1827 section, so that objcopy can handle it. */
1828 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1830 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1831 if (elf_symtab_shndx (abfd) == shindex)
1832 return TRUE;
1834 BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
1835 elf_symtab_shndx (abfd) = shindex;
1836 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1837 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1838 return TRUE;
1840 case SHT_STRTAB: /* A string table */
1841 if (hdr->bfd_section != NULL)
1842 return TRUE;
1843 if (ehdr->e_shstrndx == shindex)
1845 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1846 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1847 return TRUE;
1849 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
1851 symtab_strtab:
1852 elf_tdata (abfd)->strtab_hdr = *hdr;
1853 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
1854 return TRUE;
1856 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
1858 dynsymtab_strtab:
1859 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1860 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
1861 elf_elfsections (abfd)[shindex] = hdr;
1862 /* We also treat this as a regular section, so that objcopy
1863 can handle it. */
1864 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1867 /* If the string table isn't one of the above, then treat it as a
1868 regular section. We need to scan all the headers to be sure,
1869 just in case this strtab section appeared before the above. */
1870 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
1872 unsigned int i, num_sec;
1874 num_sec = elf_numsections (abfd);
1875 for (i = 1; i < num_sec; i++)
1877 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1878 if (hdr2->sh_link == shindex)
1880 if (! bfd_section_from_shdr (abfd, i))
1881 return FALSE;
1882 if (elf_onesymtab (abfd) == i)
1883 goto symtab_strtab;
1884 if (elf_dynsymtab (abfd) == i)
1885 goto dynsymtab_strtab;
1889 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1891 case SHT_REL:
1892 case SHT_RELA:
1893 /* *These* do a lot of work -- but build no sections! */
1895 asection *target_sect;
1896 Elf_Internal_Shdr *hdr2;
1897 unsigned int num_sec = elf_numsections (abfd);
1899 /* Check for a bogus link to avoid crashing. */
1900 if ((hdr->sh_link >= SHN_LORESERVE && hdr->sh_link <= SHN_HIRESERVE)
1901 || hdr->sh_link >= num_sec)
1903 ((*_bfd_error_handler)
1904 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1905 abfd, hdr->sh_link, name, shindex));
1906 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1909 /* For some incomprehensible reason Oracle distributes
1910 libraries for Solaris in which some of the objects have
1911 bogus sh_link fields. It would be nice if we could just
1912 reject them, but, unfortunately, some people need to use
1913 them. We scan through the section headers; if we find only
1914 one suitable symbol table, we clobber the sh_link to point
1915 to it. I hope this doesn't break anything. */
1916 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1917 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1919 unsigned int scan;
1920 int found;
1922 found = 0;
1923 for (scan = 1; scan < num_sec; scan++)
1925 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1926 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1928 if (found != 0)
1930 found = 0;
1931 break;
1933 found = scan;
1936 if (found != 0)
1937 hdr->sh_link = found;
1940 /* Get the symbol table. */
1941 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1942 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
1943 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1944 return FALSE;
1946 /* If this reloc section does not use the main symbol table we
1947 don't treat it as a reloc section. BFD can't adequately
1948 represent such a section, so at least for now, we don't
1949 try. We just present it as a normal section. We also
1950 can't use it as a reloc section if it points to the null
1951 section. */
1952 if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
1953 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1955 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1956 return FALSE;
1957 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1958 if (target_sect == NULL)
1959 return FALSE;
1961 if ((target_sect->flags & SEC_RELOC) == 0
1962 || target_sect->reloc_count == 0)
1963 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1964 else
1966 bfd_size_type amt;
1967 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1968 amt = sizeof (*hdr2);
1969 hdr2 = bfd_alloc (abfd, amt);
1970 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1972 *hdr2 = *hdr;
1973 elf_elfsections (abfd)[shindex] = hdr2;
1974 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1975 target_sect->flags |= SEC_RELOC;
1976 target_sect->relocation = NULL;
1977 target_sect->rel_filepos = hdr->sh_offset;
1978 /* In the section to which the relocations apply, mark whether
1979 its relocations are of the REL or RELA variety. */
1980 if (hdr->sh_size != 0)
1981 target_sect->use_rela_p = hdr->sh_type == SHT_RELA;
1982 abfd->flags |= HAS_RELOC;
1983 return TRUE;
1985 break;
1987 case SHT_GNU_verdef:
1988 elf_dynverdef (abfd) = shindex;
1989 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1990 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1991 break;
1993 case SHT_GNU_versym:
1994 elf_dynversym (abfd) = shindex;
1995 elf_tdata (abfd)->dynversym_hdr = *hdr;
1996 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1997 break;
1999 case SHT_GNU_verneed:
2000 elf_dynverref (abfd) = shindex;
2001 elf_tdata (abfd)->dynverref_hdr = *hdr;
2002 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
2003 break;
2005 case SHT_SHLIB:
2006 return TRUE;
2008 case SHT_GROUP:
2009 /* We need a BFD section for objcopy and relocatable linking,
2010 and it's handy to have the signature available as the section
2011 name. */
2012 name = group_signature (abfd, hdr);
2013 if (name == NULL)
2014 return FALSE;
2015 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name))
2016 return FALSE;
2017 if (hdr->contents != NULL)
2019 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
2020 unsigned int n_elt = hdr->sh_size / 4;
2021 asection *s;
2023 if (idx->flags & GRP_COMDAT)
2024 hdr->bfd_section->flags
2025 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
2027 /* We try to keep the same section order as it comes in. */
2028 idx += n_elt;
2029 while (--n_elt != 0)
2030 if ((s = (--idx)->shdr->bfd_section) != NULL
2031 && elf_next_in_group (s) != NULL)
2033 elf_next_in_group (hdr->bfd_section) = s;
2034 break;
2037 break;
2039 default:
2040 /* Check for any processor-specific section types. */
2041 if (bed->elf_backend_section_from_shdr)
2042 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
2043 break;
2046 return TRUE;
2049 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
2050 Return SEC for sections that have no elf section, and NULL on error. */
2052 asection *
2053 bfd_section_from_r_symndx (bfd *abfd,
2054 struct sym_sec_cache *cache,
2055 asection *sec,
2056 unsigned long r_symndx)
2058 Elf_Internal_Shdr *symtab_hdr;
2059 unsigned char esym[sizeof (Elf64_External_Sym)];
2060 Elf_External_Sym_Shndx eshndx;
2061 Elf_Internal_Sym isym;
2062 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2064 if (cache->abfd == abfd && cache->indx[ent] == r_symndx)
2065 return cache->sec[ent];
2067 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2068 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2069 &isym, esym, &eshndx) == NULL)
2070 return NULL;
2072 if (cache->abfd != abfd)
2074 memset (cache->indx, -1, sizeof (cache->indx));
2075 cache->abfd = abfd;
2077 cache->indx[ent] = r_symndx;
2078 cache->sec[ent] = sec;
2079 if ((isym.st_shndx != SHN_UNDEF && isym.st_shndx < SHN_LORESERVE)
2080 || isym.st_shndx > SHN_HIRESERVE)
2082 asection *s;
2083 s = bfd_section_from_elf_index (abfd, isym.st_shndx);
2084 if (s != NULL)
2085 cache->sec[ent] = s;
2087 return cache->sec[ent];
2090 /* Given an ELF section number, retrieve the corresponding BFD
2091 section. */
2093 asection *
2094 bfd_section_from_elf_index (bfd *abfd, unsigned int index)
2096 if (index >= elf_numsections (abfd))
2097 return NULL;
2098 return elf_elfsections (abfd)[index]->bfd_section;
2101 static struct bfd_elf_special_section const special_sections[] =
2103 { ".bss", 4, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2104 { ".gnu.linkonce.b",15, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2105 { ".comment", 8, 0, SHT_PROGBITS, 0 },
2106 { ".data", 5, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2107 { ".data1", 6, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2108 { ".debug", 6, 0, SHT_PROGBITS, 0 },
2109 { ".fini", 5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2110 { ".init", 5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2111 { ".line", 5, 0, SHT_PROGBITS, 0 },
2112 { ".rodata", 7, -2, SHT_PROGBITS, SHF_ALLOC },
2113 { ".rodata1", 8, 0, SHT_PROGBITS, SHF_ALLOC },
2114 { ".tbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2115 { ".tdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2116 { ".text", 5, -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2117 { ".init_array", 11, 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2118 { ".fini_array", 11, 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2119 { ".preinit_array", 14, 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2120 { ".debug_line", 11, 0, SHT_PROGBITS, 0 },
2121 { ".debug_info", 11, 0, SHT_PROGBITS, 0 },
2122 { ".debug_abbrev", 13, 0, SHT_PROGBITS, 0 },
2123 { ".debug_aranges", 14, 0, SHT_PROGBITS, 0 },
2124 { ".dynamic", 8, 0, SHT_DYNAMIC, SHF_ALLOC },
2125 { ".dynstr", 7, 0, SHT_STRTAB, SHF_ALLOC },
2126 { ".dynsym", 7, 0, SHT_DYNSYM, SHF_ALLOC },
2127 { ".got", 4, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2128 { ".hash", 5, 0, SHT_HASH, SHF_ALLOC },
2129 { ".interp", 7, 0, SHT_PROGBITS, 0 },
2130 { ".plt", 4, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2131 { ".shstrtab", 9, 0, SHT_STRTAB, 0 },
2132 { ".strtab", 7, 0, SHT_STRTAB, 0 },
2133 { ".symtab", 7, 0, SHT_SYMTAB, 0 },
2134 { ".gnu.version", 12, 0, SHT_GNU_versym, 0 },
2135 { ".gnu.version_d", 14, 0, SHT_GNU_verdef, 0 },
2136 { ".gnu.version_r", 14, 0, SHT_GNU_verneed, 0 },
2137 { ".note.GNU-stack",15, 0, SHT_PROGBITS, 0 },
2138 { ".note", 5, -1, SHT_NOTE, 0 },
2139 { ".rela", 5, -1, SHT_RELA, 0 },
2140 { ".rel", 4, -1, SHT_REL, 0 },
2141 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2142 { ".gnu.liblist", 12, 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2143 { ".gnu.conflict", 13, 0, SHT_RELA, SHF_ALLOC },
2144 { NULL, 0, 0, 0, 0 }
2147 static const struct bfd_elf_special_section *
2148 get_special_section (const char *name,
2149 const struct bfd_elf_special_section *special_sections,
2150 unsigned int rela)
2152 int i;
2153 int len = strlen (name);
2155 for (i = 0; special_sections[i].prefix != NULL; i++)
2157 int suffix_len;
2158 int prefix_len = special_sections[i].prefix_length;
2160 if (len < prefix_len)
2161 continue;
2162 if (memcmp (name, special_sections[i].prefix, prefix_len) != 0)
2163 continue;
2165 suffix_len = special_sections[i].suffix_length;
2166 if (suffix_len <= 0)
2168 if (name[prefix_len] != 0)
2170 if (suffix_len == 0)
2171 continue;
2172 if (name[prefix_len] != '.'
2173 && (suffix_len == -2
2174 || (rela && special_sections[i].type == SHT_REL)))
2175 continue;
2178 else
2180 if (len < prefix_len + suffix_len)
2181 continue;
2182 if (memcmp (name + len - suffix_len,
2183 special_sections[i].prefix + prefix_len,
2184 suffix_len) != 0)
2185 continue;
2187 return &special_sections[i];
2190 return NULL;
2193 const struct bfd_elf_special_section *
2194 _bfd_elf_get_sec_type_attr (bfd *abfd, const char *name)
2196 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2197 const struct bfd_elf_special_section *ssect = NULL;
2199 /* See if this is one of the special sections. */
2200 if (name)
2202 unsigned int rela = bed->default_use_rela_p;
2204 if (bed->special_sections)
2205 ssect = get_special_section (name, bed->special_sections, rela);
2207 if (! ssect)
2208 ssect = get_special_section (name, special_sections, rela);
2211 return ssect;
2214 bfd_boolean
2215 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2217 struct bfd_elf_section_data *sdata;
2218 const struct bfd_elf_special_section *ssect;
2220 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2221 if (sdata == NULL)
2223 sdata = bfd_zalloc (abfd, sizeof (*sdata));
2224 if (sdata == NULL)
2225 return FALSE;
2226 sec->used_by_bfd = sdata;
2229 elf_section_type (sec) = SHT_NULL;
2230 ssect = _bfd_elf_get_sec_type_attr (abfd, sec->name);
2231 if (ssect != NULL)
2233 elf_section_type (sec) = ssect->type;
2234 elf_section_flags (sec) = ssect->attr;
2237 /* Indicate whether or not this section should use RELA relocations. */
2238 sec->use_rela_p = get_elf_backend_data (abfd)->default_use_rela_p;
2240 return TRUE;
2243 /* Create a new bfd section from an ELF program header.
2245 Since program segments have no names, we generate a synthetic name
2246 of the form segment<NUM>, where NUM is generally the index in the
2247 program header table. For segments that are split (see below) we
2248 generate the names segment<NUM>a and segment<NUM>b.
2250 Note that some program segments may have a file size that is different than
2251 (less than) the memory size. All this means is that at execution the
2252 system must allocate the amount of memory specified by the memory size,
2253 but only initialize it with the first "file size" bytes read from the
2254 file. This would occur for example, with program segments consisting
2255 of combined data+bss.
2257 To handle the above situation, this routine generates TWO bfd sections
2258 for the single program segment. The first has the length specified by
2259 the file size of the segment, and the second has the length specified
2260 by the difference between the two sizes. In effect, the segment is split
2261 into it's initialized and uninitialized parts.
2265 bfd_boolean
2266 _bfd_elf_make_section_from_phdr (bfd *abfd,
2267 Elf_Internal_Phdr *hdr,
2268 int index,
2269 const char *typename)
2271 asection *newsect;
2272 char *name;
2273 char namebuf[64];
2274 size_t len;
2275 int split;
2277 split = ((hdr->p_memsz > 0)
2278 && (hdr->p_filesz > 0)
2279 && (hdr->p_memsz > hdr->p_filesz));
2280 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
2281 len = strlen (namebuf) + 1;
2282 name = bfd_alloc (abfd, len);
2283 if (!name)
2284 return FALSE;
2285 memcpy (name, namebuf, len);
2286 newsect = bfd_make_section (abfd, name);
2287 if (newsect == NULL)
2288 return FALSE;
2289 newsect->vma = hdr->p_vaddr;
2290 newsect->lma = hdr->p_paddr;
2291 newsect->size = hdr->p_filesz;
2292 newsect->filepos = hdr->p_offset;
2293 newsect->flags |= SEC_HAS_CONTENTS;
2294 newsect->alignment_power = bfd_log2 (hdr->p_align);
2295 if (hdr->p_type == PT_LOAD)
2297 newsect->flags |= SEC_ALLOC;
2298 newsect->flags |= SEC_LOAD;
2299 if (hdr->p_flags & PF_X)
2301 /* FIXME: all we known is that it has execute PERMISSION,
2302 may be data. */
2303 newsect->flags |= SEC_CODE;
2306 if (!(hdr->p_flags & PF_W))
2308 newsect->flags |= SEC_READONLY;
2311 if (split)
2313 sprintf (namebuf, "%s%db", typename, index);
2314 len = strlen (namebuf) + 1;
2315 name = bfd_alloc (abfd, len);
2316 if (!name)
2317 return FALSE;
2318 memcpy (name, namebuf, len);
2319 newsect = bfd_make_section (abfd, name);
2320 if (newsect == NULL)
2321 return FALSE;
2322 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2323 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2324 newsect->size = hdr->p_memsz - hdr->p_filesz;
2325 if (hdr->p_type == PT_LOAD)
2327 newsect->flags |= SEC_ALLOC;
2328 if (hdr->p_flags & PF_X)
2329 newsect->flags |= SEC_CODE;
2331 if (!(hdr->p_flags & PF_W))
2332 newsect->flags |= SEC_READONLY;
2335 return TRUE;
2338 bfd_boolean
2339 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index)
2341 const struct elf_backend_data *bed;
2343 switch (hdr->p_type)
2345 case PT_NULL:
2346 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2348 case PT_LOAD:
2349 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2351 case PT_DYNAMIC:
2352 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2354 case PT_INTERP:
2355 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2357 case PT_NOTE:
2358 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2359 return FALSE;
2360 if (! elfcore_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2361 return FALSE;
2362 return TRUE;
2364 case PT_SHLIB:
2365 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2367 case PT_PHDR:
2368 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2370 case PT_GNU_EH_FRAME:
2371 return _bfd_elf_make_section_from_phdr (abfd, hdr, index,
2372 "eh_frame_hdr");
2374 case PT_GNU_STACK:
2375 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "stack");
2377 case PT_GNU_RELRO:
2378 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro");
2380 default:
2381 /* Check for any processor-specific program segment types.
2382 If no handler for them, default to making "segment" sections. */
2383 bed = get_elf_backend_data (abfd);
2384 if (bed->elf_backend_section_from_phdr)
2385 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
2386 else
2387 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
2391 /* Initialize REL_HDR, the section-header for new section, containing
2392 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2393 relocations; otherwise, we use REL relocations. */
2395 bfd_boolean
2396 _bfd_elf_init_reloc_shdr (bfd *abfd,
2397 Elf_Internal_Shdr *rel_hdr,
2398 asection *asect,
2399 bfd_boolean use_rela_p)
2401 char *name;
2402 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2403 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2405 name = bfd_alloc (abfd, amt);
2406 if (name == NULL)
2407 return FALSE;
2408 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2409 rel_hdr->sh_name =
2410 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2411 FALSE);
2412 if (rel_hdr->sh_name == (unsigned int) -1)
2413 return FALSE;
2414 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2415 rel_hdr->sh_entsize = (use_rela_p
2416 ? bed->s->sizeof_rela
2417 : bed->s->sizeof_rel);
2418 rel_hdr->sh_addralign = 1 << bed->s->log_file_align;
2419 rel_hdr->sh_flags = 0;
2420 rel_hdr->sh_addr = 0;
2421 rel_hdr->sh_size = 0;
2422 rel_hdr->sh_offset = 0;
2424 return TRUE;
2427 /* Set up an ELF internal section header for a section. */
2429 static void
2430 elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg)
2432 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2433 bfd_boolean *failedptr = failedptrarg;
2434 Elf_Internal_Shdr *this_hdr;
2436 if (*failedptr)
2438 /* We already failed; just get out of the bfd_map_over_sections
2439 loop. */
2440 return;
2443 this_hdr = &elf_section_data (asect)->this_hdr;
2445 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2446 asect->name, FALSE);
2447 if (this_hdr->sh_name == (unsigned int) -1)
2449 *failedptr = TRUE;
2450 return;
2453 this_hdr->sh_flags = 0;
2455 if ((asect->flags & SEC_ALLOC) != 0
2456 || asect->user_set_vma)
2457 this_hdr->sh_addr = asect->vma;
2458 else
2459 this_hdr->sh_addr = 0;
2461 this_hdr->sh_offset = 0;
2462 this_hdr->sh_size = asect->size;
2463 this_hdr->sh_link = 0;
2464 this_hdr->sh_addralign = 1 << asect->alignment_power;
2465 /* The sh_entsize and sh_info fields may have been set already by
2466 copy_private_section_data. */
2468 this_hdr->bfd_section = asect;
2469 this_hdr->contents = NULL;
2471 /* If the section type is unspecified, we set it based on
2472 asect->flags. */
2473 if (this_hdr->sh_type == SHT_NULL)
2475 if ((asect->flags & SEC_GROUP) != 0)
2477 /* We also need to mark SHF_GROUP here for relocatable
2478 link. */
2479 struct bfd_link_order *l;
2480 asection *elt;
2482 for (l = asect->link_order_head; l != NULL; l = l->next)
2483 if (l->type == bfd_indirect_link_order
2484 && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
2487 /* The name is not important. Anything will do. */
2488 elf_group_name (elt->output_section) = "G";
2489 elf_section_flags (elt->output_section) |= SHF_GROUP;
2491 elt = elf_next_in_group (elt);
2492 /* During a relocatable link, the lists are
2493 circular. */
2495 while (elt != elf_next_in_group (l->u.indirect.section));
2497 this_hdr->sh_type = SHT_GROUP;
2499 else if ((asect->flags & SEC_ALLOC) != 0
2500 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2501 || (asect->flags & SEC_NEVER_LOAD) != 0))
2502 this_hdr->sh_type = SHT_NOBITS;
2503 else
2504 this_hdr->sh_type = SHT_PROGBITS;
2507 switch (this_hdr->sh_type)
2509 default:
2510 break;
2512 case SHT_STRTAB:
2513 case SHT_INIT_ARRAY:
2514 case SHT_FINI_ARRAY:
2515 case SHT_PREINIT_ARRAY:
2516 case SHT_NOTE:
2517 case SHT_NOBITS:
2518 case SHT_PROGBITS:
2519 break;
2521 case SHT_HASH:
2522 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2523 break;
2525 case SHT_DYNSYM:
2526 this_hdr->sh_entsize = bed->s->sizeof_sym;
2527 break;
2529 case SHT_DYNAMIC:
2530 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2531 break;
2533 case SHT_RELA:
2534 if (get_elf_backend_data (abfd)->may_use_rela_p)
2535 this_hdr->sh_entsize = bed->s->sizeof_rela;
2536 break;
2538 case SHT_REL:
2539 if (get_elf_backend_data (abfd)->may_use_rel_p)
2540 this_hdr->sh_entsize = bed->s->sizeof_rel;
2541 break;
2543 case SHT_GNU_versym:
2544 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2545 break;
2547 case SHT_GNU_verdef:
2548 this_hdr->sh_entsize = 0;
2549 /* objcopy or strip will copy over sh_info, but may not set
2550 cverdefs. The linker will set cverdefs, but sh_info will be
2551 zero. */
2552 if (this_hdr->sh_info == 0)
2553 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2554 else
2555 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2556 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2557 break;
2559 case SHT_GNU_verneed:
2560 this_hdr->sh_entsize = 0;
2561 /* objcopy or strip will copy over sh_info, but may not set
2562 cverrefs. The linker will set cverrefs, but sh_info will be
2563 zero. */
2564 if (this_hdr->sh_info == 0)
2565 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2566 else
2567 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2568 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2569 break;
2571 case SHT_GROUP:
2572 this_hdr->sh_entsize = 4;
2573 break;
2576 if ((asect->flags & SEC_ALLOC) != 0)
2577 this_hdr->sh_flags |= SHF_ALLOC;
2578 if ((asect->flags & SEC_READONLY) == 0)
2579 this_hdr->sh_flags |= SHF_WRITE;
2580 if ((asect->flags & SEC_CODE) != 0)
2581 this_hdr->sh_flags |= SHF_EXECINSTR;
2582 if ((asect->flags & SEC_MERGE) != 0)
2584 this_hdr->sh_flags |= SHF_MERGE;
2585 this_hdr->sh_entsize = asect->entsize;
2586 if ((asect->flags & SEC_STRINGS) != 0)
2587 this_hdr->sh_flags |= SHF_STRINGS;
2589 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2590 this_hdr->sh_flags |= SHF_GROUP;
2591 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2593 this_hdr->sh_flags |= SHF_TLS;
2594 if (asect->size == 0 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2596 struct bfd_link_order *o;
2598 this_hdr->sh_size = 0;
2599 for (o = asect->link_order_head; o != NULL; o = o->next)
2600 if (this_hdr->sh_size < o->offset + o->size)
2601 this_hdr->sh_size = o->offset + o->size;
2602 if (this_hdr->sh_size)
2603 this_hdr->sh_type = SHT_NOBITS;
2607 /* Check for processor-specific section types. */
2608 if (bed->elf_backend_fake_sections
2609 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2610 *failedptr = TRUE;
2612 /* If the section has relocs, set up a section header for the
2613 SHT_REL[A] section. If two relocation sections are required for
2614 this section, it is up to the processor-specific back-end to
2615 create the other. */
2616 if ((asect->flags & SEC_RELOC) != 0
2617 && !_bfd_elf_init_reloc_shdr (abfd,
2618 &elf_section_data (asect)->rel_hdr,
2619 asect,
2620 asect->use_rela_p))
2621 *failedptr = TRUE;
2624 /* Fill in the contents of a SHT_GROUP section. */
2626 void
2627 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2629 bfd_boolean *failedptr = failedptrarg;
2630 unsigned long symindx;
2631 asection *elt, *first;
2632 unsigned char *loc;
2633 struct bfd_link_order *l;
2634 bfd_boolean gas;
2636 if (elf_section_data (sec)->this_hdr.sh_type != SHT_GROUP
2637 || *failedptr)
2638 return;
2640 symindx = 0;
2641 if (elf_group_id (sec) != NULL)
2642 symindx = elf_group_id (sec)->udata.i;
2644 if (symindx == 0)
2646 /* If called from the assembler, swap_out_syms will have set up
2647 elf_section_syms; If called for "ld -r", use target_index. */
2648 if (elf_section_syms (abfd) != NULL)
2649 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2650 else
2651 symindx = sec->target_index;
2653 elf_section_data (sec)->this_hdr.sh_info = symindx;
2655 /* The contents won't be allocated for "ld -r" or objcopy. */
2656 gas = TRUE;
2657 if (sec->contents == NULL)
2659 gas = FALSE;
2660 sec->contents = bfd_alloc (abfd, sec->size);
2662 /* Arrange for the section to be written out. */
2663 elf_section_data (sec)->this_hdr.contents = sec->contents;
2664 if (sec->contents == NULL)
2666 *failedptr = TRUE;
2667 return;
2671 loc = sec->contents + sec->size;
2673 /* Get the pointer to the first section in the group that gas
2674 squirreled away here. objcopy arranges for this to be set to the
2675 start of the input section group. */
2676 first = elt = elf_next_in_group (sec);
2678 /* First element is a flag word. Rest of section is elf section
2679 indices for all the sections of the group. Write them backwards
2680 just to keep the group in the same order as given in .section
2681 directives, not that it matters. */
2682 while (elt != NULL)
2684 asection *s;
2685 unsigned int idx;
2687 loc -= 4;
2688 s = elt;
2689 if (!gas)
2690 s = s->output_section;
2691 idx = 0;
2692 if (s != NULL)
2693 idx = elf_section_data (s)->this_idx;
2694 H_PUT_32 (abfd, idx, loc);
2695 elt = elf_next_in_group (elt);
2696 if (elt == first)
2697 break;
2700 /* If this is a relocatable link, then the above did nothing because
2701 SEC is the output section. Look through the input sections
2702 instead. */
2703 for (l = sec->link_order_head; l != NULL; l = l->next)
2704 if (l->type == bfd_indirect_link_order
2705 && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
2708 loc -= 4;
2709 H_PUT_32 (abfd,
2710 elf_section_data (elt->output_section)->this_idx, loc);
2711 elt = elf_next_in_group (elt);
2712 /* During a relocatable link, the lists are circular. */
2714 while (elt != elf_next_in_group (l->u.indirect.section));
2716 if ((loc -= 4) != sec->contents)
2717 abort ();
2719 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2722 /* Assign all ELF section numbers. The dummy first section is handled here
2723 too. The link/info pointers for the standard section types are filled
2724 in here too, while we're at it. */
2726 static bfd_boolean
2727 assign_section_numbers (bfd *abfd)
2729 struct elf_obj_tdata *t = elf_tdata (abfd);
2730 asection *sec;
2731 unsigned int section_number, secn;
2732 Elf_Internal_Shdr **i_shdrp;
2733 bfd_size_type amt;
2734 struct bfd_elf_section_data *d;
2736 section_number = 1;
2738 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2740 /* Put SHT_GROUP sections first. */
2741 for (sec = abfd->sections; sec; sec = sec->next)
2743 d = elf_section_data (sec);
2745 if (d->this_hdr.sh_type == SHT_GROUP)
2747 if (section_number == SHN_LORESERVE)
2748 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2749 d->this_idx = section_number++;
2753 for (sec = abfd->sections; sec; sec = sec->next)
2755 d = elf_section_data (sec);
2757 if (d->this_hdr.sh_type != SHT_GROUP)
2759 if (section_number == SHN_LORESERVE)
2760 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2761 d->this_idx = section_number++;
2763 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2764 if ((sec->flags & SEC_RELOC) == 0)
2765 d->rel_idx = 0;
2766 else
2768 if (section_number == SHN_LORESERVE)
2769 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2770 d->rel_idx = section_number++;
2771 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2774 if (d->rel_hdr2)
2776 if (section_number == SHN_LORESERVE)
2777 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2778 d->rel_idx2 = section_number++;
2779 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2781 else
2782 d->rel_idx2 = 0;
2785 if (section_number == SHN_LORESERVE)
2786 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2787 t->shstrtab_section = section_number++;
2788 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2789 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2791 if (bfd_get_symcount (abfd) > 0)
2793 if (section_number == SHN_LORESERVE)
2794 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2795 t->symtab_section = section_number++;
2796 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2797 if (section_number > SHN_LORESERVE - 2)
2799 if (section_number == SHN_LORESERVE)
2800 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2801 t->symtab_shndx_section = section_number++;
2802 t->symtab_shndx_hdr.sh_name
2803 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2804 ".symtab_shndx", FALSE);
2805 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2806 return FALSE;
2808 if (section_number == SHN_LORESERVE)
2809 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2810 t->strtab_section = section_number++;
2811 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2814 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2815 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2817 elf_numsections (abfd) = section_number;
2818 elf_elfheader (abfd)->e_shnum = section_number;
2819 if (section_number > SHN_LORESERVE)
2820 elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE;
2822 /* Set up the list of section header pointers, in agreement with the
2823 indices. */
2824 amt = section_number * sizeof (Elf_Internal_Shdr *);
2825 i_shdrp = bfd_zalloc (abfd, amt);
2826 if (i_shdrp == NULL)
2827 return FALSE;
2829 amt = sizeof (Elf_Internal_Shdr);
2830 i_shdrp[0] = bfd_zalloc (abfd, amt);
2831 if (i_shdrp[0] == NULL)
2833 bfd_release (abfd, i_shdrp);
2834 return FALSE;
2837 elf_elfsections (abfd) = i_shdrp;
2839 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2840 if (bfd_get_symcount (abfd) > 0)
2842 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2843 if (elf_numsections (abfd) > SHN_LORESERVE)
2845 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2846 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2848 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2849 t->symtab_hdr.sh_link = t->strtab_section;
2852 for (sec = abfd->sections; sec; sec = sec->next)
2854 struct bfd_elf_section_data *d = elf_section_data (sec);
2855 asection *s;
2856 const char *name;
2858 i_shdrp[d->this_idx] = &d->this_hdr;
2859 if (d->rel_idx != 0)
2860 i_shdrp[d->rel_idx] = &d->rel_hdr;
2861 if (d->rel_idx2 != 0)
2862 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2864 /* Fill in the sh_link and sh_info fields while we're at it. */
2866 /* sh_link of a reloc section is the section index of the symbol
2867 table. sh_info is the section index of the section to which
2868 the relocation entries apply. */
2869 if (d->rel_idx != 0)
2871 d->rel_hdr.sh_link = t->symtab_section;
2872 d->rel_hdr.sh_info = d->this_idx;
2874 if (d->rel_idx2 != 0)
2876 d->rel_hdr2->sh_link = t->symtab_section;
2877 d->rel_hdr2->sh_info = d->this_idx;
2880 /* We need to set up sh_link for SHF_LINK_ORDER. */
2881 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
2883 s = elf_linked_to_section (sec);
2884 if (s)
2885 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2886 else
2888 struct bfd_link_order *p;
2890 /* Find out what the corresponding section in output
2891 is. */
2892 for (p = sec->link_order_head; p != NULL; p = p->next)
2894 s = p->u.indirect.section;
2895 if (p->type == bfd_indirect_link_order
2896 && (bfd_get_flavour (s->owner)
2897 == bfd_target_elf_flavour))
2899 Elf_Internal_Shdr ** const elf_shdrp
2900 = elf_elfsections (s->owner);
2901 int elfsec
2902 = _bfd_elf_section_from_bfd_section (s->owner, s);
2903 elfsec = elf_shdrp[elfsec]->sh_link;
2904 /* PR 290:
2905 The Intel C compiler generates SHT_IA_64_UNWIND with
2906 SHF_LINK_ORDER. But it doesn't set theh sh_link or
2907 sh_info fields. Hence we could get the situation
2908 where elfsec is 0. */
2909 if (elfsec == 0)
2911 const struct elf_backend_data *bed
2912 = get_elf_backend_data (abfd);
2913 if (bed->link_order_error_handler)
2914 bed->link_order_error_handler
2915 (_("%B: warning: sh_link not set for section `%S'"),
2916 abfd, s);
2918 else
2920 s = elf_shdrp[elfsec]->bfd_section->output_section;
2921 BFD_ASSERT (s != NULL);
2922 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2924 break;
2930 switch (d->this_hdr.sh_type)
2932 case SHT_REL:
2933 case SHT_RELA:
2934 /* A reloc section which we are treating as a normal BFD
2935 section. sh_link is the section index of the symbol
2936 table. sh_info is the section index of the section to
2937 which the relocation entries apply. We assume that an
2938 allocated reloc section uses the dynamic symbol table.
2939 FIXME: How can we be sure? */
2940 s = bfd_get_section_by_name (abfd, ".dynsym");
2941 if (s != NULL)
2942 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2944 /* We look up the section the relocs apply to by name. */
2945 name = sec->name;
2946 if (d->this_hdr.sh_type == SHT_REL)
2947 name += 4;
2948 else
2949 name += 5;
2950 s = bfd_get_section_by_name (abfd, name);
2951 if (s != NULL)
2952 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
2953 break;
2955 case SHT_STRTAB:
2956 /* We assume that a section named .stab*str is a stabs
2957 string section. We look for a section with the same name
2958 but without the trailing ``str'', and set its sh_link
2959 field to point to this section. */
2960 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
2961 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
2963 size_t len;
2964 char *alc;
2966 len = strlen (sec->name);
2967 alc = bfd_malloc (len - 2);
2968 if (alc == NULL)
2969 return FALSE;
2970 memcpy (alc, sec->name, len - 3);
2971 alc[len - 3] = '\0';
2972 s = bfd_get_section_by_name (abfd, alc);
2973 free (alc);
2974 if (s != NULL)
2976 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
2978 /* This is a .stab section. */
2979 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
2980 elf_section_data (s)->this_hdr.sh_entsize
2981 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
2984 break;
2986 case SHT_DYNAMIC:
2987 case SHT_DYNSYM:
2988 case SHT_GNU_verneed:
2989 case SHT_GNU_verdef:
2990 /* sh_link is the section header index of the string table
2991 used for the dynamic entries, or the symbol table, or the
2992 version strings. */
2993 s = bfd_get_section_by_name (abfd, ".dynstr");
2994 if (s != NULL)
2995 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2996 break;
2998 case SHT_GNU_LIBLIST:
2999 /* sh_link is the section header index of the prelink library
3000 list
3001 used for the dynamic entries, or the symbol table, or the
3002 version strings. */
3003 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3004 ? ".dynstr" : ".gnu.libstr");
3005 if (s != NULL)
3006 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3007 break;
3009 case SHT_HASH:
3010 case SHT_GNU_versym:
3011 /* sh_link is the section header index of the symbol table
3012 this hash table or version table is for. */
3013 s = bfd_get_section_by_name (abfd, ".dynsym");
3014 if (s != NULL)
3015 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3016 break;
3018 case SHT_GROUP:
3019 d->this_hdr.sh_link = t->symtab_section;
3023 for (secn = 1; secn < section_number; ++secn)
3024 if (i_shdrp[secn] == NULL)
3025 i_shdrp[secn] = i_shdrp[0];
3026 else
3027 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
3028 i_shdrp[secn]->sh_name);
3029 return TRUE;
3032 /* Map symbol from it's internal number to the external number, moving
3033 all local symbols to be at the head of the list. */
3035 static int
3036 sym_is_global (bfd *abfd, asymbol *sym)
3038 /* If the backend has a special mapping, use it. */
3039 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3040 if (bed->elf_backend_sym_is_global)
3041 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3043 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
3044 || bfd_is_und_section (bfd_get_section (sym))
3045 || bfd_is_com_section (bfd_get_section (sym)));
3048 static bfd_boolean
3049 elf_map_symbols (bfd *abfd)
3051 unsigned int symcount = bfd_get_symcount (abfd);
3052 asymbol **syms = bfd_get_outsymbols (abfd);
3053 asymbol **sect_syms;
3054 unsigned int num_locals = 0;
3055 unsigned int num_globals = 0;
3056 unsigned int num_locals2 = 0;
3057 unsigned int num_globals2 = 0;
3058 int max_index = 0;
3059 unsigned int idx;
3060 asection *asect;
3061 asymbol **new_syms;
3062 bfd_size_type amt;
3064 #ifdef DEBUG
3065 fprintf (stderr, "elf_map_symbols\n");
3066 fflush (stderr);
3067 #endif
3069 for (asect = abfd->sections; asect; asect = asect->next)
3071 if (max_index < asect->index)
3072 max_index = asect->index;
3075 max_index++;
3076 amt = max_index * sizeof (asymbol *);
3077 sect_syms = bfd_zalloc (abfd, amt);
3078 if (sect_syms == NULL)
3079 return FALSE;
3080 elf_section_syms (abfd) = sect_syms;
3081 elf_num_section_syms (abfd) = max_index;
3083 /* Init sect_syms entries for any section symbols we have already
3084 decided to output. */
3085 for (idx = 0; idx < symcount; idx++)
3087 asymbol *sym = syms[idx];
3089 if ((sym->flags & BSF_SECTION_SYM) != 0
3090 && sym->value == 0)
3092 asection *sec;
3094 sec = sym->section;
3096 if (sec->owner != NULL)
3098 if (sec->owner != abfd)
3100 if (sec->output_offset != 0)
3101 continue;
3103 sec = sec->output_section;
3105 /* Empty sections in the input files may have had a
3106 section symbol created for them. (See the comment
3107 near the end of _bfd_generic_link_output_symbols in
3108 linker.c). If the linker script discards such
3109 sections then we will reach this point. Since we know
3110 that we cannot avoid this case, we detect it and skip
3111 the abort and the assignment to the sect_syms array.
3112 To reproduce this particular case try running the
3113 linker testsuite test ld-scripts/weak.exp for an ELF
3114 port that uses the generic linker. */
3115 if (sec->owner == NULL)
3116 continue;
3118 BFD_ASSERT (sec->owner == abfd);
3120 sect_syms[sec->index] = syms[idx];
3125 /* Classify all of the symbols. */
3126 for (idx = 0; idx < symcount; idx++)
3128 if (!sym_is_global (abfd, syms[idx]))
3129 num_locals++;
3130 else
3131 num_globals++;
3134 /* We will be adding a section symbol for each BFD section. Most normal
3135 sections will already have a section symbol in outsymbols, but
3136 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3137 at least in that case. */
3138 for (asect = abfd->sections; asect; asect = asect->next)
3140 if (sect_syms[asect->index] == NULL)
3142 if (!sym_is_global (abfd, asect->symbol))
3143 num_locals++;
3144 else
3145 num_globals++;
3149 /* Now sort the symbols so the local symbols are first. */
3150 amt = (num_locals + num_globals) * sizeof (asymbol *);
3151 new_syms = bfd_alloc (abfd, amt);
3153 if (new_syms == NULL)
3154 return FALSE;
3156 for (idx = 0; idx < symcount; idx++)
3158 asymbol *sym = syms[idx];
3159 unsigned int i;
3161 if (!sym_is_global (abfd, sym))
3162 i = num_locals2++;
3163 else
3164 i = num_locals + num_globals2++;
3165 new_syms[i] = sym;
3166 sym->udata.i = i + 1;
3168 for (asect = abfd->sections; asect; asect = asect->next)
3170 if (sect_syms[asect->index] == NULL)
3172 asymbol *sym = asect->symbol;
3173 unsigned int i;
3175 sect_syms[asect->index] = sym;
3176 if (!sym_is_global (abfd, sym))
3177 i = num_locals2++;
3178 else
3179 i = num_locals + num_globals2++;
3180 new_syms[i] = sym;
3181 sym->udata.i = i + 1;
3185 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3187 elf_num_locals (abfd) = num_locals;
3188 elf_num_globals (abfd) = num_globals;
3189 return TRUE;
3192 /* Align to the maximum file alignment that could be required for any
3193 ELF data structure. */
3195 static inline file_ptr
3196 align_file_position (file_ptr off, int align)
3198 return (off + align - 1) & ~(align - 1);
3201 /* Assign a file position to a section, optionally aligning to the
3202 required section alignment. */
3204 file_ptr
3205 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3206 file_ptr offset,
3207 bfd_boolean align)
3209 if (align)
3211 unsigned int al;
3213 al = i_shdrp->sh_addralign;
3214 if (al > 1)
3215 offset = BFD_ALIGN (offset, al);
3217 i_shdrp->sh_offset = offset;
3218 if (i_shdrp->bfd_section != NULL)
3219 i_shdrp->bfd_section->filepos = offset;
3220 if (i_shdrp->sh_type != SHT_NOBITS)
3221 offset += i_shdrp->sh_size;
3222 return offset;
3225 /* Compute the file positions we are going to put the sections at, and
3226 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3227 is not NULL, this is being called by the ELF backend linker. */
3229 bfd_boolean
3230 _bfd_elf_compute_section_file_positions (bfd *abfd,
3231 struct bfd_link_info *link_info)
3233 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3234 bfd_boolean failed;
3235 struct bfd_strtab_hash *strtab;
3236 Elf_Internal_Shdr *shstrtab_hdr;
3238 if (abfd->output_has_begun)
3239 return TRUE;
3241 /* Do any elf backend specific processing first. */
3242 if (bed->elf_backend_begin_write_processing)
3243 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3245 if (! prep_headers (abfd))
3246 return FALSE;
3248 /* Post process the headers if necessary. */
3249 if (bed->elf_backend_post_process_headers)
3250 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3252 failed = FALSE;
3253 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3254 if (failed)
3255 return FALSE;
3257 if (!assign_section_numbers (abfd))
3258 return FALSE;
3260 /* The backend linker builds symbol table information itself. */
3261 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3263 /* Non-zero if doing a relocatable link. */
3264 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3266 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3267 return FALSE;
3270 if (link_info == NULL)
3272 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3273 if (failed)
3274 return FALSE;
3277 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3278 /* sh_name was set in prep_headers. */
3279 shstrtab_hdr->sh_type = SHT_STRTAB;
3280 shstrtab_hdr->sh_flags = 0;
3281 shstrtab_hdr->sh_addr = 0;
3282 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3283 shstrtab_hdr->sh_entsize = 0;
3284 shstrtab_hdr->sh_link = 0;
3285 shstrtab_hdr->sh_info = 0;
3286 /* sh_offset is set in assign_file_positions_except_relocs. */
3287 shstrtab_hdr->sh_addralign = 1;
3289 if (!assign_file_positions_except_relocs (abfd, link_info))
3290 return FALSE;
3292 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3294 file_ptr off;
3295 Elf_Internal_Shdr *hdr;
3297 off = elf_tdata (abfd)->next_file_pos;
3299 hdr = &elf_tdata (abfd)->symtab_hdr;
3300 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3302 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3303 if (hdr->sh_size != 0)
3304 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3306 hdr = &elf_tdata (abfd)->strtab_hdr;
3307 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3309 elf_tdata (abfd)->next_file_pos = off;
3311 /* Now that we know where the .strtab section goes, write it
3312 out. */
3313 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3314 || ! _bfd_stringtab_emit (abfd, strtab))
3315 return FALSE;
3316 _bfd_stringtab_free (strtab);
3319 abfd->output_has_begun = TRUE;
3321 return TRUE;
3324 /* Create a mapping from a set of sections to a program segment. */
3326 static struct elf_segment_map *
3327 make_mapping (bfd *abfd,
3328 asection **sections,
3329 unsigned int from,
3330 unsigned int to,
3331 bfd_boolean phdr)
3333 struct elf_segment_map *m;
3334 unsigned int i;
3335 asection **hdrpp;
3336 bfd_size_type amt;
3338 amt = sizeof (struct elf_segment_map);
3339 amt += (to - from - 1) * sizeof (asection *);
3340 m = bfd_zalloc (abfd, amt);
3341 if (m == NULL)
3342 return NULL;
3343 m->next = NULL;
3344 m->p_type = PT_LOAD;
3345 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3346 m->sections[i - from] = *hdrpp;
3347 m->count = to - from;
3349 if (from == 0 && phdr)
3351 /* Include the headers in the first PT_LOAD segment. */
3352 m->includes_filehdr = 1;
3353 m->includes_phdrs = 1;
3356 return m;
3359 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3360 on failure. */
3362 struct elf_segment_map *
3363 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3365 struct elf_segment_map *m;
3367 m = bfd_zalloc (abfd, sizeof (struct elf_segment_map));
3368 if (m == NULL)
3369 return NULL;
3370 m->next = NULL;
3371 m->p_type = PT_DYNAMIC;
3372 m->count = 1;
3373 m->sections[0] = dynsec;
3375 return m;
3378 /* Set up a mapping from BFD sections to program segments. */
3380 static bfd_boolean
3381 map_sections_to_segments (bfd *abfd)
3383 asection **sections = NULL;
3384 asection *s;
3385 unsigned int i;
3386 unsigned int count;
3387 struct elf_segment_map *mfirst;
3388 struct elf_segment_map **pm;
3389 struct elf_segment_map *m;
3390 asection *last_hdr;
3391 bfd_vma last_size;
3392 unsigned int phdr_index;
3393 bfd_vma maxpagesize;
3394 asection **hdrpp;
3395 bfd_boolean phdr_in_segment = TRUE;
3396 bfd_boolean writable;
3397 int tls_count = 0;
3398 asection *first_tls = NULL;
3399 asection *dynsec, *eh_frame_hdr;
3400 bfd_size_type amt;
3402 if (elf_tdata (abfd)->segment_map != NULL)
3403 return TRUE;
3405 if (bfd_count_sections (abfd) == 0)
3406 return TRUE;
3408 /* Select the allocated sections, and sort them. */
3410 amt = bfd_count_sections (abfd) * sizeof (asection *);
3411 sections = bfd_malloc (amt);
3412 if (sections == NULL)
3413 goto error_return;
3415 i = 0;
3416 for (s = abfd->sections; s != NULL; s = s->next)
3418 if ((s->flags & SEC_ALLOC) != 0)
3420 sections[i] = s;
3421 ++i;
3424 BFD_ASSERT (i <= bfd_count_sections (abfd));
3425 count = i;
3427 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3429 /* Build the mapping. */
3431 mfirst = NULL;
3432 pm = &mfirst;
3434 /* If we have a .interp section, then create a PT_PHDR segment for
3435 the program headers and a PT_INTERP segment for the .interp
3436 section. */
3437 s = bfd_get_section_by_name (abfd, ".interp");
3438 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3440 amt = sizeof (struct elf_segment_map);
3441 m = bfd_zalloc (abfd, amt);
3442 if (m == NULL)
3443 goto error_return;
3444 m->next = NULL;
3445 m->p_type = PT_PHDR;
3446 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3447 m->p_flags = PF_R | PF_X;
3448 m->p_flags_valid = 1;
3449 m->includes_phdrs = 1;
3451 *pm = m;
3452 pm = &m->next;
3454 amt = sizeof (struct elf_segment_map);
3455 m = bfd_zalloc (abfd, amt);
3456 if (m == NULL)
3457 goto error_return;
3458 m->next = NULL;
3459 m->p_type = PT_INTERP;
3460 m->count = 1;
3461 m->sections[0] = s;
3463 *pm = m;
3464 pm = &m->next;
3467 /* Look through the sections. We put sections in the same program
3468 segment when the start of the second section can be placed within
3469 a few bytes of the end of the first section. */
3470 last_hdr = NULL;
3471 last_size = 0;
3472 phdr_index = 0;
3473 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
3474 writable = FALSE;
3475 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3476 if (dynsec != NULL
3477 && (dynsec->flags & SEC_LOAD) == 0)
3478 dynsec = NULL;
3480 /* Deal with -Ttext or something similar such that the first section
3481 is not adjacent to the program headers. This is an
3482 approximation, since at this point we don't know exactly how many
3483 program headers we will need. */
3484 if (count > 0)
3486 bfd_size_type phdr_size;
3488 phdr_size = elf_tdata (abfd)->program_header_size;
3489 if (phdr_size == 0)
3490 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
3491 if ((abfd->flags & D_PAGED) == 0
3492 || sections[0]->lma < phdr_size
3493 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3494 phdr_in_segment = FALSE;
3497 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3499 asection *hdr;
3500 bfd_boolean new_segment;
3502 hdr = *hdrpp;
3504 /* See if this section and the last one will fit in the same
3505 segment. */
3507 if (last_hdr == NULL)
3509 /* If we don't have a segment yet, then we don't need a new
3510 one (we build the last one after this loop). */
3511 new_segment = FALSE;
3513 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3515 /* If this section has a different relation between the
3516 virtual address and the load address, then we need a new
3517 segment. */
3518 new_segment = TRUE;
3520 else if (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
3521 < BFD_ALIGN (hdr->lma, maxpagesize))
3523 /* If putting this section in this segment would force us to
3524 skip a page in the segment, then we need a new segment. */
3525 new_segment = TRUE;
3527 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3528 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
3530 /* We don't want to put a loadable section after a
3531 nonloadable section in the same segment.
3532 Consider .tbss sections as loadable for this purpose. */
3533 new_segment = TRUE;
3535 else if ((abfd->flags & D_PAGED) == 0)
3537 /* If the file is not demand paged, which means that we
3538 don't require the sections to be correctly aligned in the
3539 file, then there is no other reason for a new segment. */
3540 new_segment = FALSE;
3542 else if (! writable
3543 && (hdr->flags & SEC_READONLY) == 0
3544 && (((last_hdr->lma + last_size - 1)
3545 & ~(maxpagesize - 1))
3546 != (hdr->lma & ~(maxpagesize - 1))))
3548 /* We don't want to put a writable section in a read only
3549 segment, unless they are on the same page in memory
3550 anyhow. We already know that the last section does not
3551 bring us past the current section on the page, so the
3552 only case in which the new section is not on the same
3553 page as the previous section is when the previous section
3554 ends precisely on a page boundary. */
3555 new_segment = TRUE;
3557 else
3559 /* Otherwise, we can use the same segment. */
3560 new_segment = FALSE;
3563 if (! new_segment)
3565 if ((hdr->flags & SEC_READONLY) == 0)
3566 writable = TRUE;
3567 last_hdr = hdr;
3568 /* .tbss sections effectively have zero size. */
3569 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3570 last_size = hdr->size;
3571 else
3572 last_size = 0;
3573 continue;
3576 /* We need a new program segment. We must create a new program
3577 header holding all the sections from phdr_index until hdr. */
3579 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3580 if (m == NULL)
3581 goto error_return;
3583 *pm = m;
3584 pm = &m->next;
3586 if ((hdr->flags & SEC_READONLY) == 0)
3587 writable = TRUE;
3588 else
3589 writable = FALSE;
3591 last_hdr = hdr;
3592 /* .tbss sections effectively have zero size. */
3593 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3594 last_size = hdr->size;
3595 else
3596 last_size = 0;
3597 phdr_index = i;
3598 phdr_in_segment = FALSE;
3601 /* Create a final PT_LOAD program segment. */
3602 if (last_hdr != NULL)
3604 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3605 if (m == NULL)
3606 goto error_return;
3608 *pm = m;
3609 pm = &m->next;
3612 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3613 if (dynsec != NULL)
3615 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
3616 if (m == NULL)
3617 goto error_return;
3618 *pm = m;
3619 pm = &m->next;
3622 /* For each loadable .note section, add a PT_NOTE segment. We don't
3623 use bfd_get_section_by_name, because if we link together
3624 nonloadable .note sections and loadable .note sections, we will
3625 generate two .note sections in the output file. FIXME: Using
3626 names for section types is bogus anyhow. */
3627 for (s = abfd->sections; s != NULL; s = s->next)
3629 if ((s->flags & SEC_LOAD) != 0
3630 && strncmp (s->name, ".note", 5) == 0)
3632 amt = sizeof (struct elf_segment_map);
3633 m = bfd_zalloc (abfd, amt);
3634 if (m == NULL)
3635 goto error_return;
3636 m->next = NULL;
3637 m->p_type = PT_NOTE;
3638 m->count = 1;
3639 m->sections[0] = s;
3641 *pm = m;
3642 pm = &m->next;
3644 if (s->flags & SEC_THREAD_LOCAL)
3646 if (! tls_count)
3647 first_tls = s;
3648 tls_count++;
3652 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3653 if (tls_count > 0)
3655 int i;
3657 amt = sizeof (struct elf_segment_map);
3658 amt += (tls_count - 1) * sizeof (asection *);
3659 m = bfd_zalloc (abfd, amt);
3660 if (m == NULL)
3661 goto error_return;
3662 m->next = NULL;
3663 m->p_type = PT_TLS;
3664 m->count = tls_count;
3665 /* Mandated PF_R. */
3666 m->p_flags = PF_R;
3667 m->p_flags_valid = 1;
3668 for (i = 0; i < tls_count; ++i)
3670 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3671 m->sections[i] = first_tls;
3672 first_tls = first_tls->next;
3675 *pm = m;
3676 pm = &m->next;
3679 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3680 segment. */
3681 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
3682 if (eh_frame_hdr != NULL
3683 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
3685 amt = sizeof (struct elf_segment_map);
3686 m = bfd_zalloc (abfd, amt);
3687 if (m == NULL)
3688 goto error_return;
3689 m->next = NULL;
3690 m->p_type = PT_GNU_EH_FRAME;
3691 m->count = 1;
3692 m->sections[0] = eh_frame_hdr->output_section;
3694 *pm = m;
3695 pm = &m->next;
3698 if (elf_tdata (abfd)->stack_flags)
3700 amt = sizeof (struct elf_segment_map);
3701 m = bfd_zalloc (abfd, amt);
3702 if (m == NULL)
3703 goto error_return;
3704 m->next = NULL;
3705 m->p_type = PT_GNU_STACK;
3706 m->p_flags = elf_tdata (abfd)->stack_flags;
3707 m->p_flags_valid = 1;
3709 *pm = m;
3710 pm = &m->next;
3713 if (elf_tdata (abfd)->relro)
3715 amt = sizeof (struct elf_segment_map);
3716 m = bfd_zalloc (abfd, amt);
3717 if (m == NULL)
3718 goto error_return;
3719 m->next = NULL;
3720 m->p_type = PT_GNU_RELRO;
3721 m->p_flags = PF_R;
3722 m->p_flags_valid = 1;
3724 *pm = m;
3725 pm = &m->next;
3728 free (sections);
3729 sections = NULL;
3731 elf_tdata (abfd)->segment_map = mfirst;
3732 return TRUE;
3734 error_return:
3735 if (sections != NULL)
3736 free (sections);
3737 return FALSE;
3740 /* Sort sections by address. */
3742 static int
3743 elf_sort_sections (const void *arg1, const void *arg2)
3745 const asection *sec1 = *(const asection **) arg1;
3746 const asection *sec2 = *(const asection **) arg2;
3747 bfd_size_type size1, size2;
3749 /* Sort by LMA first, since this is the address used to
3750 place the section into a segment. */
3751 if (sec1->lma < sec2->lma)
3752 return -1;
3753 else if (sec1->lma > sec2->lma)
3754 return 1;
3756 /* Then sort by VMA. Normally the LMA and the VMA will be
3757 the same, and this will do nothing. */
3758 if (sec1->vma < sec2->vma)
3759 return -1;
3760 else if (sec1->vma > sec2->vma)
3761 return 1;
3763 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3765 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
3767 if (TOEND (sec1))
3769 if (TOEND (sec2))
3771 /* If the indicies are the same, do not return 0
3772 here, but continue to try the next comparison. */
3773 if (sec1->target_index - sec2->target_index != 0)
3774 return sec1->target_index - sec2->target_index;
3776 else
3777 return 1;
3779 else if (TOEND (sec2))
3780 return -1;
3782 #undef TOEND
3784 /* Sort by size, to put zero sized sections
3785 before others at the same address. */
3787 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
3788 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
3790 if (size1 < size2)
3791 return -1;
3792 if (size1 > size2)
3793 return 1;
3795 return sec1->target_index - sec2->target_index;
3798 /* Ian Lance Taylor writes:
3800 We shouldn't be using % with a negative signed number. That's just
3801 not good. We have to make sure either that the number is not
3802 negative, or that the number has an unsigned type. When the types
3803 are all the same size they wind up as unsigned. When file_ptr is a
3804 larger signed type, the arithmetic winds up as signed long long,
3805 which is wrong.
3807 What we're trying to say here is something like ``increase OFF by
3808 the least amount that will cause it to be equal to the VMA modulo
3809 the page size.'' */
3810 /* In other words, something like:
3812 vma_offset = m->sections[0]->vma % bed->maxpagesize;
3813 off_offset = off % bed->maxpagesize;
3814 if (vma_offset < off_offset)
3815 adjustment = vma_offset + bed->maxpagesize - off_offset;
3816 else
3817 adjustment = vma_offset - off_offset;
3819 which can can be collapsed into the expression below. */
3821 static file_ptr
3822 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
3824 return ((vma - off) % maxpagesize);
3827 /* Assign file positions to the sections based on the mapping from
3828 sections to segments. This function also sets up some fields in
3829 the file header, and writes out the program headers. */
3831 static bfd_boolean
3832 assign_file_positions_for_segments (bfd *abfd, struct bfd_link_info *link_info)
3834 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3835 unsigned int count;
3836 struct elf_segment_map *m;
3837 unsigned int alloc;
3838 Elf_Internal_Phdr *phdrs;
3839 file_ptr off, voff;
3840 bfd_vma filehdr_vaddr, filehdr_paddr;
3841 bfd_vma phdrs_vaddr, phdrs_paddr;
3842 Elf_Internal_Phdr *p;
3843 bfd_size_type amt;
3845 if (elf_tdata (abfd)->segment_map == NULL)
3847 if (! map_sections_to_segments (abfd))
3848 return FALSE;
3850 else
3852 /* The placement algorithm assumes that non allocated sections are
3853 not in PT_LOAD segments. We ensure this here by removing such
3854 sections from the segment map. */
3855 for (m = elf_tdata (abfd)->segment_map;
3856 m != NULL;
3857 m = m->next)
3859 unsigned int new_count;
3860 unsigned int i;
3862 if (m->p_type != PT_LOAD)
3863 continue;
3865 new_count = 0;
3866 for (i = 0; i < m->count; i ++)
3868 if ((m->sections[i]->flags & SEC_ALLOC) != 0)
3870 if (i != new_count)
3871 m->sections[new_count] = m->sections[i];
3873 new_count ++;
3877 if (new_count != m->count)
3878 m->count = new_count;
3882 if (bed->elf_backend_modify_segment_map)
3884 if (! (*bed->elf_backend_modify_segment_map) (abfd, link_info))
3885 return FALSE;
3888 count = 0;
3889 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3890 ++count;
3892 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
3893 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
3894 elf_elfheader (abfd)->e_phnum = count;
3896 if (count == 0)
3898 elf_tdata (abfd)->next_file_pos = bed->s->sizeof_ehdr;
3899 return TRUE;
3902 /* If we already counted the number of program segments, make sure
3903 that we allocated enough space. This happens when SIZEOF_HEADERS
3904 is used in a linker script. */
3905 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
3906 if (alloc != 0 && count > alloc)
3908 ((*_bfd_error_handler)
3909 (_("%B: Not enough room for program headers (allocated %u, need %u)"),
3910 abfd, alloc, count));
3911 bfd_set_error (bfd_error_bad_value);
3912 return FALSE;
3915 if (alloc == 0)
3916 alloc = count;
3918 amt = alloc * sizeof (Elf_Internal_Phdr);
3919 phdrs = bfd_alloc (abfd, amt);
3920 if (phdrs == NULL)
3921 return FALSE;
3923 off = bed->s->sizeof_ehdr;
3924 off += alloc * bed->s->sizeof_phdr;
3926 filehdr_vaddr = 0;
3927 filehdr_paddr = 0;
3928 phdrs_vaddr = 0;
3929 phdrs_paddr = 0;
3931 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3932 m != NULL;
3933 m = m->next, p++)
3935 unsigned int i;
3936 asection **secpp;
3938 /* If elf_segment_map is not from map_sections_to_segments, the
3939 sections may not be correctly ordered. NOTE: sorting should
3940 not be done to the PT_NOTE section of a corefile, which may
3941 contain several pseudo-sections artificially created by bfd.
3942 Sorting these pseudo-sections breaks things badly. */
3943 if (m->count > 1
3944 && !(elf_elfheader (abfd)->e_type == ET_CORE
3945 && m->p_type == PT_NOTE))
3946 qsort (m->sections, (size_t) m->count, sizeof (asection *),
3947 elf_sort_sections);
3949 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
3950 number of sections with contents contributing to both p_filesz
3951 and p_memsz, followed by a number of sections with no contents
3952 that just contribute to p_memsz. In this loop, OFF tracks next
3953 available file offset for PT_LOAD and PT_NOTE segments. VOFF is
3954 an adjustment we use for segments that have no file contents
3955 but need zero filled memory allocation. */
3956 voff = 0;
3957 p->p_type = m->p_type;
3958 p->p_flags = m->p_flags;
3960 if (p->p_type == PT_LOAD
3961 && m->count > 0)
3963 bfd_size_type align;
3964 bfd_vma adjust;
3966 if ((abfd->flags & D_PAGED) != 0)
3967 align = bed->maxpagesize;
3968 else
3970 unsigned int align_power = 0;
3971 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3973 unsigned int secalign;
3975 secalign = bfd_get_section_alignment (abfd, *secpp);
3976 if (secalign > align_power)
3977 align_power = secalign;
3979 align = (bfd_size_type) 1 << align_power;
3982 adjust = vma_page_aligned_bias (m->sections[0]->vma, off, align);
3983 off += adjust;
3984 if (adjust != 0
3985 && !m->includes_filehdr
3986 && !m->includes_phdrs
3987 && (ufile_ptr) off >= align)
3989 /* If the first section isn't loadable, the same holds for
3990 any other sections. Since the segment won't need file
3991 space, we can make p_offset overlap some prior segment.
3992 However, .tbss is special. If a segment starts with
3993 .tbss, we need to look at the next section to decide
3994 whether the segment has any loadable sections. */
3995 i = 0;
3996 while ((m->sections[i]->flags & SEC_LOAD) == 0)
3998 if ((m->sections[i]->flags & SEC_THREAD_LOCAL) == 0
3999 || ++i >= m->count)
4001 off -= adjust;
4002 voff = adjust - align;
4003 break;
4008 /* Make sure the .dynamic section is the first section in the
4009 PT_DYNAMIC segment. */
4010 else if (p->p_type == PT_DYNAMIC
4011 && m->count > 1
4012 && strcmp (m->sections[0]->name, ".dynamic") != 0)
4014 _bfd_error_handler
4015 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4016 abfd);
4017 bfd_set_error (bfd_error_bad_value);
4018 return FALSE;
4021 if (m->count == 0)
4022 p->p_vaddr = 0;
4023 else
4024 p->p_vaddr = m->sections[0]->vma;
4026 if (m->p_paddr_valid)
4027 p->p_paddr = m->p_paddr;
4028 else if (m->count == 0)
4029 p->p_paddr = 0;
4030 else
4031 p->p_paddr = m->sections[0]->lma;
4033 if (p->p_type == PT_LOAD
4034 && (abfd->flags & D_PAGED) != 0)
4035 p->p_align = bed->maxpagesize;
4036 else if (m->count == 0)
4037 p->p_align = 1 << bed->s->log_file_align;
4038 else
4039 p->p_align = 0;
4041 p->p_offset = 0;
4042 p->p_filesz = 0;
4043 p->p_memsz = 0;
4045 if (m->includes_filehdr)
4047 if (! m->p_flags_valid)
4048 p->p_flags |= PF_R;
4049 p->p_offset = 0;
4050 p->p_filesz = bed->s->sizeof_ehdr;
4051 p->p_memsz = bed->s->sizeof_ehdr;
4052 if (m->count > 0)
4054 BFD_ASSERT (p->p_type == PT_LOAD);
4056 if (p->p_vaddr < (bfd_vma) off)
4058 (*_bfd_error_handler)
4059 (_("%B: Not enough room for program headers, try linking with -N"),
4060 abfd);
4061 bfd_set_error (bfd_error_bad_value);
4062 return FALSE;
4065 p->p_vaddr -= off;
4066 if (! m->p_paddr_valid)
4067 p->p_paddr -= off;
4069 if (p->p_type == PT_LOAD)
4071 filehdr_vaddr = p->p_vaddr;
4072 filehdr_paddr = p->p_paddr;
4076 if (m->includes_phdrs)
4078 if (! m->p_flags_valid)
4079 p->p_flags |= PF_R;
4081 if (m->includes_filehdr)
4083 if (p->p_type == PT_LOAD)
4085 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
4086 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
4089 else
4091 p->p_offset = bed->s->sizeof_ehdr;
4093 if (m->count > 0)
4095 BFD_ASSERT (p->p_type == PT_LOAD);
4096 p->p_vaddr -= off - p->p_offset;
4097 if (! m->p_paddr_valid)
4098 p->p_paddr -= off - p->p_offset;
4101 if (p->p_type == PT_LOAD)
4103 phdrs_vaddr = p->p_vaddr;
4104 phdrs_paddr = p->p_paddr;
4106 else
4107 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
4110 p->p_filesz += alloc * bed->s->sizeof_phdr;
4111 p->p_memsz += alloc * bed->s->sizeof_phdr;
4114 if (p->p_type == PT_LOAD
4115 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4117 if (! m->includes_filehdr && ! m->includes_phdrs)
4118 p->p_offset = off + voff;
4119 else
4121 file_ptr adjust;
4123 adjust = off - (p->p_offset + p->p_filesz);
4124 p->p_filesz += adjust;
4125 p->p_memsz += adjust;
4129 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4131 asection *sec;
4132 flagword flags;
4133 bfd_size_type align;
4135 sec = *secpp;
4136 flags = sec->flags;
4137 align = 1 << bfd_get_section_alignment (abfd, sec);
4139 if (p->p_type == PT_LOAD
4140 || p->p_type == PT_TLS)
4142 bfd_signed_vma adjust;
4144 if ((flags & SEC_LOAD) != 0)
4146 adjust = sec->lma - (p->p_paddr + p->p_filesz);
4147 if (adjust < 0)
4149 (*_bfd_error_handler)
4150 (_("%B: section %A lma 0x%lx overlaps previous sections"),
4151 abfd, sec, (unsigned long) sec->lma);
4152 adjust = 0;
4154 off += adjust;
4155 p->p_filesz += adjust;
4156 p->p_memsz += adjust;
4158 /* .tbss is special. It doesn't contribute to p_memsz of
4159 normal segments. */
4160 else if ((flags & SEC_THREAD_LOCAL) == 0
4161 || p->p_type == PT_TLS)
4163 /* The section VMA must equal the file position
4164 modulo the page size. */
4165 bfd_size_type page = align;
4166 if ((abfd->flags & D_PAGED) != 0)
4167 page = bed->maxpagesize;
4168 adjust = vma_page_aligned_bias (sec->vma,
4169 p->p_vaddr + p->p_memsz,
4170 page);
4171 p->p_memsz += adjust;
4175 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4177 /* The section at i == 0 is the one that actually contains
4178 everything. */
4179 if (i == 0)
4181 sec->filepos = off;
4182 off += sec->size;
4183 p->p_filesz = sec->size;
4184 p->p_memsz = 0;
4185 p->p_align = 1;
4187 else
4189 /* The rest are fake sections that shouldn't be written. */
4190 sec->filepos = 0;
4191 sec->size = 0;
4192 sec->flags = 0;
4193 continue;
4196 else
4198 if (p->p_type == PT_LOAD)
4200 sec->filepos = off;
4201 /* FIXME: The SEC_HAS_CONTENTS test here dates back to
4202 1997, and the exact reason for it isn't clear. One
4203 plausible explanation is that it is to work around
4204 a problem we have with linker scripts using data
4205 statements in NOLOAD sections. I don't think it
4206 makes a great deal of sense to have such a section
4207 assigned to a PT_LOAD segment, but apparently
4208 people do this. The data statement results in a
4209 bfd_data_link_order being built, and these need
4210 section contents to write into. Eventually, we get
4211 to _bfd_elf_write_object_contents which writes any
4212 section with contents to the output. Make room
4213 here for the write, so that following segments are
4214 not trashed. */
4215 if ((flags & SEC_LOAD) != 0
4216 || (flags & SEC_HAS_CONTENTS) != 0)
4217 off += sec->size;
4220 if ((flags & SEC_LOAD) != 0)
4222 p->p_filesz += sec->size;
4223 p->p_memsz += sec->size;
4225 /* .tbss is special. It doesn't contribute to p_memsz of
4226 normal segments. */
4227 else if ((flags & SEC_THREAD_LOCAL) == 0
4228 || p->p_type == PT_TLS)
4229 p->p_memsz += sec->size;
4231 if (p->p_type == PT_TLS
4232 && sec->size == 0
4233 && (sec->flags & SEC_HAS_CONTENTS) == 0)
4235 struct bfd_link_order *o;
4236 bfd_vma tbss_size = 0;
4238 for (o = sec->link_order_head; o != NULL; o = o->next)
4239 if (tbss_size < o->offset + o->size)
4240 tbss_size = o->offset + o->size;
4242 p->p_memsz += tbss_size;
4245 if (align > p->p_align
4246 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
4247 p->p_align = align;
4250 if (! m->p_flags_valid)
4252 p->p_flags |= PF_R;
4253 if ((flags & SEC_CODE) != 0)
4254 p->p_flags |= PF_X;
4255 if ((flags & SEC_READONLY) == 0)
4256 p->p_flags |= PF_W;
4261 /* Now that we have set the section file positions, we can set up
4262 the file positions for the non PT_LOAD segments. */
4263 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4264 m != NULL;
4265 m = m->next, p++)
4267 if (p->p_type != PT_LOAD && m->count > 0)
4269 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
4270 /* If the section has not yet been assigned a file position,
4271 do so now. The ARM BPABI requires that .dynamic section
4272 not be marked SEC_ALLOC because it is not part of any
4273 PT_LOAD segment, so it will not be processed above. */
4274 if (p->p_type == PT_DYNAMIC && m->sections[0]->filepos == 0)
4276 unsigned int i;
4277 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4279 i = 1;
4280 while (i_shdrpp[i]->bfd_section != m->sections[0])
4281 ++i;
4282 off = (_bfd_elf_assign_file_position_for_section
4283 (i_shdrpp[i], off, TRUE));
4284 p->p_filesz = m->sections[0]->size;
4286 p->p_offset = m->sections[0]->filepos;
4288 if (m->count == 0)
4290 if (m->includes_filehdr)
4292 p->p_vaddr = filehdr_vaddr;
4293 if (! m->p_paddr_valid)
4294 p->p_paddr = filehdr_paddr;
4296 else if (m->includes_phdrs)
4298 p->p_vaddr = phdrs_vaddr;
4299 if (! m->p_paddr_valid)
4300 p->p_paddr = phdrs_paddr;
4302 else if (p->p_type == PT_GNU_RELRO)
4304 Elf_Internal_Phdr *lp;
4306 for (lp = phdrs; lp < phdrs + count; ++lp)
4308 if (lp->p_type == PT_LOAD
4309 && lp->p_vaddr <= link_info->relro_end
4310 && lp->p_vaddr >= link_info->relro_start
4311 && lp->p_vaddr + lp->p_filesz
4312 >= link_info->relro_end)
4313 break;
4316 if (lp < phdrs + count
4317 && link_info->relro_end > lp->p_vaddr)
4319 p->p_vaddr = lp->p_vaddr;
4320 p->p_paddr = lp->p_paddr;
4321 p->p_offset = lp->p_offset;
4322 p->p_filesz = link_info->relro_end - lp->p_vaddr;
4323 p->p_memsz = p->p_filesz;
4324 p->p_align = 1;
4325 p->p_flags = (lp->p_flags & ~PF_W);
4327 else
4329 memset (p, 0, sizeof *p);
4330 p->p_type = PT_NULL;
4336 /* Clear out any program headers we allocated but did not use. */
4337 for (; count < alloc; count++, p++)
4339 memset (p, 0, sizeof *p);
4340 p->p_type = PT_NULL;
4343 elf_tdata (abfd)->phdr = phdrs;
4345 elf_tdata (abfd)->next_file_pos = off;
4347 /* Write out the program headers. */
4348 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4349 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
4350 return FALSE;
4352 return TRUE;
4355 /* Get the size of the program header.
4357 If this is called by the linker before any of the section VMA's are set, it
4358 can't calculate the correct value for a strange memory layout. This only
4359 happens when SIZEOF_HEADERS is used in a linker script. In this case,
4360 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
4361 data segment (exclusive of .interp and .dynamic).
4363 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
4364 will be two segments. */
4366 static bfd_size_type
4367 get_program_header_size (bfd *abfd)
4369 size_t segs;
4370 asection *s;
4371 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4373 /* We can't return a different result each time we're called. */
4374 if (elf_tdata (abfd)->program_header_size != 0)
4375 return elf_tdata (abfd)->program_header_size;
4377 if (elf_tdata (abfd)->segment_map != NULL)
4379 struct elf_segment_map *m;
4381 segs = 0;
4382 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4383 ++segs;
4384 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
4385 return elf_tdata (abfd)->program_header_size;
4388 /* Assume we will need exactly two PT_LOAD segments: one for text
4389 and one for data. */
4390 segs = 2;
4392 s = bfd_get_section_by_name (abfd, ".interp");
4393 if (s != NULL && (s->flags & SEC_LOAD) != 0)
4395 /* If we have a loadable interpreter section, we need a
4396 PT_INTERP segment. In this case, assume we also need a
4397 PT_PHDR segment, although that may not be true for all
4398 targets. */
4399 segs += 2;
4402 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4404 /* We need a PT_DYNAMIC segment. */
4405 ++segs;
4408 if (elf_tdata (abfd)->eh_frame_hdr)
4410 /* We need a PT_GNU_EH_FRAME segment. */
4411 ++segs;
4414 if (elf_tdata (abfd)->stack_flags)
4416 /* We need a PT_GNU_STACK segment. */
4417 ++segs;
4420 if (elf_tdata (abfd)->relro)
4422 /* We need a PT_GNU_RELRO segment. */
4423 ++segs;
4426 for (s = abfd->sections; s != NULL; s = s->next)
4428 if ((s->flags & SEC_LOAD) != 0
4429 && strncmp (s->name, ".note", 5) == 0)
4431 /* We need a PT_NOTE segment. */
4432 ++segs;
4436 for (s = abfd->sections; s != NULL; s = s->next)
4438 if (s->flags & SEC_THREAD_LOCAL)
4440 /* We need a PT_TLS segment. */
4441 ++segs;
4442 break;
4446 /* Let the backend count up any program headers it might need. */
4447 if (bed->elf_backend_additional_program_headers)
4449 int a;
4451 a = (*bed->elf_backend_additional_program_headers) (abfd);
4452 if (a == -1)
4453 abort ();
4454 segs += a;
4457 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
4458 return elf_tdata (abfd)->program_header_size;
4461 /* Work out the file positions of all the sections. This is called by
4462 _bfd_elf_compute_section_file_positions. All the section sizes and
4463 VMAs must be known before this is called.
4465 Reloc sections come in two flavours: Those processed specially as
4466 "side-channel" data attached to a section to which they apply, and
4467 those that bfd doesn't process as relocations. The latter sort are
4468 stored in a normal bfd section by bfd_section_from_shdr. We don't
4469 consider the former sort here, unless they form part of the loadable
4470 image. Reloc sections not assigned here will be handled later by
4471 assign_file_positions_for_relocs.
4473 We also don't set the positions of the .symtab and .strtab here. */
4475 static bfd_boolean
4476 assign_file_positions_except_relocs (bfd *abfd,
4477 struct bfd_link_info *link_info)
4479 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
4480 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
4481 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4482 unsigned int num_sec = elf_numsections (abfd);
4483 file_ptr off;
4484 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4486 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4487 && bfd_get_format (abfd) != bfd_core)
4489 Elf_Internal_Shdr **hdrpp;
4490 unsigned int i;
4492 /* Start after the ELF header. */
4493 off = i_ehdrp->e_ehsize;
4495 /* We are not creating an executable, which means that we are
4496 not creating a program header, and that the actual order of
4497 the sections in the file is unimportant. */
4498 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4500 Elf_Internal_Shdr *hdr;
4502 hdr = *hdrpp;
4503 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4504 && hdr->bfd_section == NULL)
4505 || i == tdata->symtab_section
4506 || i == tdata->symtab_shndx_section
4507 || i == tdata->strtab_section)
4509 hdr->sh_offset = -1;
4511 else
4512 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4514 if (i == SHN_LORESERVE - 1)
4516 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4517 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4521 else
4523 unsigned int i;
4524 Elf_Internal_Shdr **hdrpp;
4526 /* Assign file positions for the loaded sections based on the
4527 assignment of sections to segments. */
4528 if (! assign_file_positions_for_segments (abfd, link_info))
4529 return FALSE;
4531 /* Assign file positions for the other sections. */
4533 off = elf_tdata (abfd)->next_file_pos;
4534 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4536 Elf_Internal_Shdr *hdr;
4538 hdr = *hdrpp;
4539 if (hdr->bfd_section != NULL
4540 && hdr->bfd_section->filepos != 0)
4541 hdr->sh_offset = hdr->bfd_section->filepos;
4542 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4544 ((*_bfd_error_handler)
4545 (_("%B: warning: allocated section `%s' not in segment"),
4546 abfd,
4547 (hdr->bfd_section == NULL
4548 ? "*unknown*"
4549 : hdr->bfd_section->name)));
4550 if ((abfd->flags & D_PAGED) != 0)
4551 off += vma_page_aligned_bias (hdr->sh_addr, off,
4552 bed->maxpagesize);
4553 else
4554 off += vma_page_aligned_bias (hdr->sh_addr, off,
4555 hdr->sh_addralign);
4556 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4557 FALSE);
4559 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4560 && hdr->bfd_section == NULL)
4561 || hdr == i_shdrpp[tdata->symtab_section]
4562 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4563 || hdr == i_shdrpp[tdata->strtab_section])
4564 hdr->sh_offset = -1;
4565 else
4566 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4568 if (i == SHN_LORESERVE - 1)
4570 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4571 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4576 /* Place the section headers. */
4577 off = align_file_position (off, 1 << bed->s->log_file_align);
4578 i_ehdrp->e_shoff = off;
4579 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4581 elf_tdata (abfd)->next_file_pos = off;
4583 return TRUE;
4586 static bfd_boolean
4587 prep_headers (bfd *abfd)
4589 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
4590 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4591 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
4592 struct elf_strtab_hash *shstrtab;
4593 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4595 i_ehdrp = elf_elfheader (abfd);
4596 i_shdrp = elf_elfsections (abfd);
4598 shstrtab = _bfd_elf_strtab_init ();
4599 if (shstrtab == NULL)
4600 return FALSE;
4602 elf_shstrtab (abfd) = shstrtab;
4604 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4605 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4606 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4607 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4609 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4610 i_ehdrp->e_ident[EI_DATA] =
4611 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4612 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4614 if ((abfd->flags & DYNAMIC) != 0)
4615 i_ehdrp->e_type = ET_DYN;
4616 else if ((abfd->flags & EXEC_P) != 0)
4617 i_ehdrp->e_type = ET_EXEC;
4618 else if (bfd_get_format (abfd) == bfd_core)
4619 i_ehdrp->e_type = ET_CORE;
4620 else
4621 i_ehdrp->e_type = ET_REL;
4623 switch (bfd_get_arch (abfd))
4625 case bfd_arch_unknown:
4626 i_ehdrp->e_machine = EM_NONE;
4627 break;
4629 /* There used to be a long list of cases here, each one setting
4630 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4631 in the corresponding bfd definition. To avoid duplication,
4632 the switch was removed. Machines that need special handling
4633 can generally do it in elf_backend_final_write_processing(),
4634 unless they need the information earlier than the final write.
4635 Such need can generally be supplied by replacing the tests for
4636 e_machine with the conditions used to determine it. */
4637 default:
4638 i_ehdrp->e_machine = bed->elf_machine_code;
4641 i_ehdrp->e_version = bed->s->ev_current;
4642 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4644 /* No program header, for now. */
4645 i_ehdrp->e_phoff = 0;
4646 i_ehdrp->e_phentsize = 0;
4647 i_ehdrp->e_phnum = 0;
4649 /* Each bfd section is section header entry. */
4650 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4651 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4653 /* If we're building an executable, we'll need a program header table. */
4654 if (abfd->flags & EXEC_P)
4655 /* It all happens later. */
4657 else
4659 i_ehdrp->e_phentsize = 0;
4660 i_phdrp = 0;
4661 i_ehdrp->e_phoff = 0;
4664 elf_tdata (abfd)->symtab_hdr.sh_name =
4665 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
4666 elf_tdata (abfd)->strtab_hdr.sh_name =
4667 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
4668 elf_tdata (abfd)->shstrtab_hdr.sh_name =
4669 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
4670 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4671 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4672 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4673 return FALSE;
4675 return TRUE;
4678 /* Assign file positions for all the reloc sections which are not part
4679 of the loadable file image. */
4681 void
4682 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
4684 file_ptr off;
4685 unsigned int i, num_sec;
4686 Elf_Internal_Shdr **shdrpp;
4688 off = elf_tdata (abfd)->next_file_pos;
4690 num_sec = elf_numsections (abfd);
4691 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
4693 Elf_Internal_Shdr *shdrp;
4695 shdrp = *shdrpp;
4696 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4697 && shdrp->sh_offset == -1)
4698 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
4701 elf_tdata (abfd)->next_file_pos = off;
4704 bfd_boolean
4705 _bfd_elf_write_object_contents (bfd *abfd)
4707 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4708 Elf_Internal_Ehdr *i_ehdrp;
4709 Elf_Internal_Shdr **i_shdrp;
4710 bfd_boolean failed;
4711 unsigned int count, num_sec;
4713 if (! abfd->output_has_begun
4714 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
4715 return FALSE;
4717 i_shdrp = elf_elfsections (abfd);
4718 i_ehdrp = elf_elfheader (abfd);
4720 failed = FALSE;
4721 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4722 if (failed)
4723 return FALSE;
4725 _bfd_elf_assign_file_positions_for_relocs (abfd);
4727 /* After writing the headers, we need to write the sections too... */
4728 num_sec = elf_numsections (abfd);
4729 for (count = 1; count < num_sec; count++)
4731 if (bed->elf_backend_section_processing)
4732 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4733 if (i_shdrp[count]->contents)
4735 bfd_size_type amt = i_shdrp[count]->sh_size;
4737 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
4738 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
4739 return FALSE;
4741 if (count == SHN_LORESERVE - 1)
4742 count += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4745 /* Write out the section header names. */
4746 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
4747 || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))
4748 return FALSE;
4750 if (bed->elf_backend_final_write_processing)
4751 (*bed->elf_backend_final_write_processing) (abfd,
4752 elf_tdata (abfd)->linker);
4754 return bed->s->write_shdrs_and_ehdr (abfd);
4757 bfd_boolean
4758 _bfd_elf_write_corefile_contents (bfd *abfd)
4760 /* Hopefully this can be done just like an object file. */
4761 return _bfd_elf_write_object_contents (abfd);
4764 /* Given a section, search the header to find them. */
4767 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
4769 const struct elf_backend_data *bed;
4770 int index;
4772 if (elf_section_data (asect) != NULL
4773 && elf_section_data (asect)->this_idx != 0)
4774 return elf_section_data (asect)->this_idx;
4776 if (bfd_is_abs_section (asect))
4777 index = SHN_ABS;
4778 else if (bfd_is_com_section (asect))
4779 index = SHN_COMMON;
4780 else if (bfd_is_und_section (asect))
4781 index = SHN_UNDEF;
4782 else
4784 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
4785 int maxindex = elf_numsections (abfd);
4787 for (index = 1; index < maxindex; index++)
4789 Elf_Internal_Shdr *hdr = i_shdrp[index];
4791 if (hdr != NULL && hdr->bfd_section == asect)
4792 return index;
4794 index = -1;
4797 bed = get_elf_backend_data (abfd);
4798 if (bed->elf_backend_section_from_bfd_section)
4800 int retval = index;
4802 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
4803 return retval;
4806 if (index == -1)
4807 bfd_set_error (bfd_error_nonrepresentable_section);
4809 return index;
4812 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4813 on error. */
4816 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
4818 asymbol *asym_ptr = *asym_ptr_ptr;
4819 int idx;
4820 flagword flags = asym_ptr->flags;
4822 /* When gas creates relocations against local labels, it creates its
4823 own symbol for the section, but does put the symbol into the
4824 symbol chain, so udata is 0. When the linker is generating
4825 relocatable output, this section symbol may be for one of the
4826 input sections rather than the output section. */
4827 if (asym_ptr->udata.i == 0
4828 && (flags & BSF_SECTION_SYM)
4829 && asym_ptr->section)
4831 int indx;
4833 if (asym_ptr->section->output_section != NULL)
4834 indx = asym_ptr->section->output_section->index;
4835 else
4836 indx = asym_ptr->section->index;
4837 if (indx < elf_num_section_syms (abfd)
4838 && elf_section_syms (abfd)[indx] != NULL)
4839 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
4842 idx = asym_ptr->udata.i;
4844 if (idx == 0)
4846 /* This case can occur when using --strip-symbol on a symbol
4847 which is used in a relocation entry. */
4848 (*_bfd_error_handler)
4849 (_("%B: symbol `%s' required but not present"),
4850 abfd, bfd_asymbol_name (asym_ptr));
4851 bfd_set_error (bfd_error_no_symbols);
4852 return -1;
4855 #if DEBUG & 4
4857 fprintf (stderr,
4858 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
4859 (long) asym_ptr, asym_ptr->name, idx, flags,
4860 elf_symbol_flags (flags));
4861 fflush (stderr);
4863 #endif
4865 return idx;
4868 /* Copy private BFD data. This copies any program header information. */
4870 static bfd_boolean
4871 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
4873 Elf_Internal_Ehdr *iehdr;
4874 struct elf_segment_map *map;
4875 struct elf_segment_map *map_first;
4876 struct elf_segment_map **pointer_to_map;
4877 Elf_Internal_Phdr *segment;
4878 asection *section;
4879 unsigned int i;
4880 unsigned int num_segments;
4881 bfd_boolean phdr_included = FALSE;
4882 bfd_vma maxpagesize;
4883 struct elf_segment_map *phdr_adjust_seg = NULL;
4884 unsigned int phdr_adjust_num = 0;
4885 const struct elf_backend_data *bed;
4887 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4888 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4889 return TRUE;
4891 if (elf_tdata (ibfd)->phdr == NULL)
4892 return TRUE;
4894 bed = get_elf_backend_data (ibfd);
4895 iehdr = elf_elfheader (ibfd);
4897 map_first = NULL;
4898 pointer_to_map = &map_first;
4900 num_segments = elf_elfheader (ibfd)->e_phnum;
4901 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
4903 /* Returns the end address of the segment + 1. */
4904 #define SEGMENT_END(segment, start) \
4905 (start + (segment->p_memsz > segment->p_filesz \
4906 ? segment->p_memsz : segment->p_filesz))
4908 #define SECTION_SIZE(section, segment) \
4909 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
4910 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
4911 ? section->size : 0)
4913 /* Returns TRUE if the given section is contained within
4914 the given segment. VMA addresses are compared. */
4915 #define IS_CONTAINED_BY_VMA(section, segment) \
4916 (section->vma >= segment->p_vaddr \
4917 && (section->vma + SECTION_SIZE (section, segment) \
4918 <= (SEGMENT_END (segment, segment->p_vaddr))))
4920 /* Returns TRUE if the given section is contained within
4921 the given segment. LMA addresses are compared. */
4922 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4923 (section->lma >= base \
4924 && (section->lma + SECTION_SIZE (section, segment) \
4925 <= SEGMENT_END (segment, base)))
4927 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4928 #define IS_COREFILE_NOTE(p, s) \
4929 (p->p_type == PT_NOTE \
4930 && bfd_get_format (ibfd) == bfd_core \
4931 && s->vma == 0 && s->lma == 0 \
4932 && (bfd_vma) s->filepos >= p->p_offset \
4933 && ((bfd_vma) s->filepos + s->size \
4934 <= p->p_offset + p->p_filesz))
4936 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4937 linker, which generates a PT_INTERP section with p_vaddr and
4938 p_memsz set to 0. */
4939 #define IS_SOLARIS_PT_INTERP(p, s) \
4940 (p->p_vaddr == 0 \
4941 && p->p_paddr == 0 \
4942 && p->p_memsz == 0 \
4943 && p->p_filesz > 0 \
4944 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4945 && s->size > 0 \
4946 && (bfd_vma) s->filepos >= p->p_offset \
4947 && ((bfd_vma) s->filepos + s->size \
4948 <= p->p_offset + p->p_filesz))
4950 /* Decide if the given section should be included in the given segment.
4951 A section will be included if:
4952 1. It is within the address space of the segment -- we use the LMA
4953 if that is set for the segment and the VMA otherwise,
4954 2. It is an allocated segment,
4955 3. There is an output section associated with it,
4956 4. The section has not already been allocated to a previous segment.
4957 5. PT_GNU_STACK segments do not include any sections.
4958 6. PT_TLS segment includes only SHF_TLS sections.
4959 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
4960 8. PT_DYNAMIC should not contain empty sections at the beginning
4961 (with the possible exception of .dynamic). */
4962 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
4963 ((((segment->p_paddr \
4964 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4965 : IS_CONTAINED_BY_VMA (section, segment)) \
4966 && (section->flags & SEC_ALLOC) != 0) \
4967 || IS_COREFILE_NOTE (segment, section)) \
4968 && section->output_section != NULL \
4969 && segment->p_type != PT_GNU_STACK \
4970 && (segment->p_type != PT_TLS \
4971 || (section->flags & SEC_THREAD_LOCAL)) \
4972 && (segment->p_type == PT_LOAD \
4973 || segment->p_type == PT_TLS \
4974 || (section->flags & SEC_THREAD_LOCAL) == 0) \
4975 && (segment->p_type != PT_DYNAMIC \
4976 || SECTION_SIZE (section, segment) > 0 \
4977 || (segment->p_paddr \
4978 ? segment->p_paddr != section->lma \
4979 : segment->p_vaddr != section->vma) \
4980 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
4981 == 0)) \
4982 && ! section->segment_mark)
4984 /* Returns TRUE iff seg1 starts after the end of seg2. */
4985 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
4986 (seg1->field >= SEGMENT_END (seg2, seg2->field))
4988 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
4989 their VMA address ranges and their LMA address ranges overlap.
4990 It is possible to have overlapping VMA ranges without overlapping LMA
4991 ranges. RedBoot images for example can have both .data and .bss mapped
4992 to the same VMA range, but with the .data section mapped to a different
4993 LMA. */
4994 #define SEGMENT_OVERLAPS(seg1, seg2) \
4995 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
4996 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
4997 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
4998 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5000 /* Initialise the segment mark field. */
5001 for (section = ibfd->sections; section != NULL; section = section->next)
5002 section->segment_mark = FALSE;
5004 /* Scan through the segments specified in the program header
5005 of the input BFD. For this first scan we look for overlaps
5006 in the loadable segments. These can be created by weird
5007 parameters to objcopy. Also, fix some solaris weirdness. */
5008 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5009 i < num_segments;
5010 i++, segment++)
5012 unsigned int j;
5013 Elf_Internal_Phdr *segment2;
5015 if (segment->p_type == PT_INTERP)
5016 for (section = ibfd->sections; section; section = section->next)
5017 if (IS_SOLARIS_PT_INTERP (segment, section))
5019 /* Mininal change so that the normal section to segment
5020 assignment code will work. */
5021 segment->p_vaddr = section->vma;
5022 break;
5025 if (segment->p_type != PT_LOAD)
5026 continue;
5028 /* Determine if this segment overlaps any previous segments. */
5029 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
5031 bfd_signed_vma extra_length;
5033 if (segment2->p_type != PT_LOAD
5034 || ! SEGMENT_OVERLAPS (segment, segment2))
5035 continue;
5037 /* Merge the two segments together. */
5038 if (segment2->p_vaddr < segment->p_vaddr)
5040 /* Extend SEGMENT2 to include SEGMENT and then delete
5041 SEGMENT. */
5042 extra_length =
5043 SEGMENT_END (segment, segment->p_vaddr)
5044 - SEGMENT_END (segment2, segment2->p_vaddr);
5046 if (extra_length > 0)
5048 segment2->p_memsz += extra_length;
5049 segment2->p_filesz += extra_length;
5052 segment->p_type = PT_NULL;
5054 /* Since we have deleted P we must restart the outer loop. */
5055 i = 0;
5056 segment = elf_tdata (ibfd)->phdr;
5057 break;
5059 else
5061 /* Extend SEGMENT to include SEGMENT2 and then delete
5062 SEGMENT2. */
5063 extra_length =
5064 SEGMENT_END (segment2, segment2->p_vaddr)
5065 - SEGMENT_END (segment, segment->p_vaddr);
5067 if (extra_length > 0)
5069 segment->p_memsz += extra_length;
5070 segment->p_filesz += extra_length;
5073 segment2->p_type = PT_NULL;
5078 /* The second scan attempts to assign sections to segments. */
5079 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5080 i < num_segments;
5081 i ++, segment ++)
5083 unsigned int section_count;
5084 asection ** sections;
5085 asection * output_section;
5086 unsigned int isec;
5087 bfd_vma matching_lma;
5088 bfd_vma suggested_lma;
5089 unsigned int j;
5090 bfd_size_type amt;
5092 if (segment->p_type == PT_NULL)
5093 continue;
5095 /* Compute how many sections might be placed into this segment. */
5096 for (section = ibfd->sections, section_count = 0;
5097 section != NULL;
5098 section = section->next)
5099 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5100 ++section_count;
5102 /* Allocate a segment map big enough to contain
5103 all of the sections we have selected. */
5104 amt = sizeof (struct elf_segment_map);
5105 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5106 map = bfd_alloc (obfd, amt);
5107 if (map == NULL)
5108 return FALSE;
5110 /* Initialise the fields of the segment map. Default to
5111 using the physical address of the segment in the input BFD. */
5112 map->next = NULL;
5113 map->p_type = segment->p_type;
5114 map->p_flags = segment->p_flags;
5115 map->p_flags_valid = 1;
5116 map->p_paddr = segment->p_paddr;
5117 map->p_paddr_valid = 1;
5119 /* Determine if this segment contains the ELF file header
5120 and if it contains the program headers themselves. */
5121 map->includes_filehdr = (segment->p_offset == 0
5122 && segment->p_filesz >= iehdr->e_ehsize);
5124 map->includes_phdrs = 0;
5126 if (! phdr_included || segment->p_type != PT_LOAD)
5128 map->includes_phdrs =
5129 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5130 && (segment->p_offset + segment->p_filesz
5131 >= ((bfd_vma) iehdr->e_phoff
5132 + iehdr->e_phnum * iehdr->e_phentsize)));
5134 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5135 phdr_included = TRUE;
5138 if (section_count == 0)
5140 /* Special segments, such as the PT_PHDR segment, may contain
5141 no sections, but ordinary, loadable segments should contain
5142 something. They are allowed by the ELF spec however, so only
5143 a warning is produced. */
5144 if (segment->p_type == PT_LOAD)
5145 (*_bfd_error_handler)
5146 (_("%B: warning: Empty loadable segment detected, is this intentional ?\n"),
5147 ibfd);
5149 map->count = 0;
5150 *pointer_to_map = map;
5151 pointer_to_map = &map->next;
5153 continue;
5156 /* Now scan the sections in the input BFD again and attempt
5157 to add their corresponding output sections to the segment map.
5158 The problem here is how to handle an output section which has
5159 been moved (ie had its LMA changed). There are four possibilities:
5161 1. None of the sections have been moved.
5162 In this case we can continue to use the segment LMA from the
5163 input BFD.
5165 2. All of the sections have been moved by the same amount.
5166 In this case we can change the segment's LMA to match the LMA
5167 of the first section.
5169 3. Some of the sections have been moved, others have not.
5170 In this case those sections which have not been moved can be
5171 placed in the current segment which will have to have its size,
5172 and possibly its LMA changed, and a new segment or segments will
5173 have to be created to contain the other sections.
5175 4. The sections have been moved, but not by the same amount.
5176 In this case we can change the segment's LMA to match the LMA
5177 of the first section and we will have to create a new segment
5178 or segments to contain the other sections.
5180 In order to save time, we allocate an array to hold the section
5181 pointers that we are interested in. As these sections get assigned
5182 to a segment, they are removed from this array. */
5184 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
5185 to work around this long long bug. */
5186 amt = section_count * sizeof (asection *);
5187 sections = bfd_malloc (amt);
5188 if (sections == NULL)
5189 return FALSE;
5191 /* Step One: Scan for segment vs section LMA conflicts.
5192 Also add the sections to the section array allocated above.
5193 Also add the sections to the current segment. In the common
5194 case, where the sections have not been moved, this means that
5195 we have completely filled the segment, and there is nothing
5196 more to do. */
5197 isec = 0;
5198 matching_lma = 0;
5199 suggested_lma = 0;
5201 for (j = 0, section = ibfd->sections;
5202 section != NULL;
5203 section = section->next)
5205 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5207 output_section = section->output_section;
5209 sections[j ++] = section;
5211 /* The Solaris native linker always sets p_paddr to 0.
5212 We try to catch that case here, and set it to the
5213 correct value. Note - some backends require that
5214 p_paddr be left as zero. */
5215 if (segment->p_paddr == 0
5216 && segment->p_vaddr != 0
5217 && (! bed->want_p_paddr_set_to_zero)
5218 && isec == 0
5219 && output_section->lma != 0
5220 && (output_section->vma == (segment->p_vaddr
5221 + (map->includes_filehdr
5222 ? iehdr->e_ehsize
5223 : 0)
5224 + (map->includes_phdrs
5225 ? (iehdr->e_phnum
5226 * iehdr->e_phentsize)
5227 : 0))))
5228 map->p_paddr = segment->p_vaddr;
5230 /* Match up the physical address of the segment with the
5231 LMA address of the output section. */
5232 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5233 || IS_COREFILE_NOTE (segment, section)
5234 || (bed->want_p_paddr_set_to_zero &&
5235 IS_CONTAINED_BY_VMA (output_section, segment))
5238 if (matching_lma == 0)
5239 matching_lma = output_section->lma;
5241 /* We assume that if the section fits within the segment
5242 then it does not overlap any other section within that
5243 segment. */
5244 map->sections[isec ++] = output_section;
5246 else if (suggested_lma == 0)
5247 suggested_lma = output_section->lma;
5251 BFD_ASSERT (j == section_count);
5253 /* Step Two: Adjust the physical address of the current segment,
5254 if necessary. */
5255 if (isec == section_count)
5257 /* All of the sections fitted within the segment as currently
5258 specified. This is the default case. Add the segment to
5259 the list of built segments and carry on to process the next
5260 program header in the input BFD. */
5261 map->count = section_count;
5262 *pointer_to_map = map;
5263 pointer_to_map = &map->next;
5265 free (sections);
5266 continue;
5268 else
5270 if (matching_lma != 0)
5272 /* At least one section fits inside the current segment.
5273 Keep it, but modify its physical address to match the
5274 LMA of the first section that fitted. */
5275 map->p_paddr = matching_lma;
5277 else
5279 /* None of the sections fitted inside the current segment.
5280 Change the current segment's physical address to match
5281 the LMA of the first section. */
5282 map->p_paddr = suggested_lma;
5285 /* Offset the segment physical address from the lma
5286 to allow for space taken up by elf headers. */
5287 if (map->includes_filehdr)
5288 map->p_paddr -= iehdr->e_ehsize;
5290 if (map->includes_phdrs)
5292 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5294 /* iehdr->e_phnum is just an estimate of the number
5295 of program headers that we will need. Make a note
5296 here of the number we used and the segment we chose
5297 to hold these headers, so that we can adjust the
5298 offset when we know the correct value. */
5299 phdr_adjust_num = iehdr->e_phnum;
5300 phdr_adjust_seg = map;
5304 /* Step Three: Loop over the sections again, this time assigning
5305 those that fit to the current segment and removing them from the
5306 sections array; but making sure not to leave large gaps. Once all
5307 possible sections have been assigned to the current segment it is
5308 added to the list of built segments and if sections still remain
5309 to be assigned, a new segment is constructed before repeating
5310 the loop. */
5311 isec = 0;
5314 map->count = 0;
5315 suggested_lma = 0;
5317 /* Fill the current segment with sections that fit. */
5318 for (j = 0; j < section_count; j++)
5320 section = sections[j];
5322 if (section == NULL)
5323 continue;
5325 output_section = section->output_section;
5327 BFD_ASSERT (output_section != NULL);
5329 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5330 || IS_COREFILE_NOTE (segment, section))
5332 if (map->count == 0)
5334 /* If the first section in a segment does not start at
5335 the beginning of the segment, then something is
5336 wrong. */
5337 if (output_section->lma !=
5338 (map->p_paddr
5339 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5340 + (map->includes_phdrs
5341 ? iehdr->e_phnum * iehdr->e_phentsize
5342 : 0)))
5343 abort ();
5345 else
5347 asection * prev_sec;
5349 prev_sec = map->sections[map->count - 1];
5351 /* If the gap between the end of the previous section
5352 and the start of this section is more than
5353 maxpagesize then we need to start a new segment. */
5354 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
5355 maxpagesize)
5356 < BFD_ALIGN (output_section->lma, maxpagesize))
5357 || ((prev_sec->lma + prev_sec->size)
5358 > output_section->lma))
5360 if (suggested_lma == 0)
5361 suggested_lma = output_section->lma;
5363 continue;
5367 map->sections[map->count++] = output_section;
5368 ++isec;
5369 sections[j] = NULL;
5370 section->segment_mark = TRUE;
5372 else if (suggested_lma == 0)
5373 suggested_lma = output_section->lma;
5376 BFD_ASSERT (map->count > 0);
5378 /* Add the current segment to the list of built segments. */
5379 *pointer_to_map = map;
5380 pointer_to_map = &map->next;
5382 if (isec < section_count)
5384 /* We still have not allocated all of the sections to
5385 segments. Create a new segment here, initialise it
5386 and carry on looping. */
5387 amt = sizeof (struct elf_segment_map);
5388 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5389 map = bfd_alloc (obfd, amt);
5390 if (map == NULL)
5392 free (sections);
5393 return FALSE;
5396 /* Initialise the fields of the segment map. Set the physical
5397 physical address to the LMA of the first section that has
5398 not yet been assigned. */
5399 map->next = NULL;
5400 map->p_type = segment->p_type;
5401 map->p_flags = segment->p_flags;
5402 map->p_flags_valid = 1;
5403 map->p_paddr = suggested_lma;
5404 map->p_paddr_valid = 1;
5405 map->includes_filehdr = 0;
5406 map->includes_phdrs = 0;
5409 while (isec < section_count);
5411 free (sections);
5414 /* The Solaris linker creates program headers in which all the
5415 p_paddr fields are zero. When we try to objcopy or strip such a
5416 file, we get confused. Check for this case, and if we find it
5417 reset the p_paddr_valid fields. */
5418 for (map = map_first; map != NULL; map = map->next)
5419 if (map->p_paddr != 0)
5420 break;
5421 if (map == NULL)
5422 for (map = map_first; map != NULL; map = map->next)
5423 map->p_paddr_valid = 0;
5425 elf_tdata (obfd)->segment_map = map_first;
5427 /* If we had to estimate the number of program headers that were
5428 going to be needed, then check our estimate now and adjust
5429 the offset if necessary. */
5430 if (phdr_adjust_seg != NULL)
5432 unsigned int count;
5434 for (count = 0, map = map_first; map != NULL; map = map->next)
5435 count++;
5437 if (count > phdr_adjust_num)
5438 phdr_adjust_seg->p_paddr
5439 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5442 #undef SEGMENT_END
5443 #undef SECTION_SIZE
5444 #undef IS_CONTAINED_BY_VMA
5445 #undef IS_CONTAINED_BY_LMA
5446 #undef IS_COREFILE_NOTE
5447 #undef IS_SOLARIS_PT_INTERP
5448 #undef INCLUDE_SECTION_IN_SEGMENT
5449 #undef SEGMENT_AFTER_SEGMENT
5450 #undef SEGMENT_OVERLAPS
5451 return TRUE;
5454 /* Copy private section information. This copies over the entsize
5455 field, and sometimes the info field. */
5457 bfd_boolean
5458 _bfd_elf_copy_private_section_data (bfd *ibfd,
5459 asection *isec,
5460 bfd *obfd,
5461 asection *osec)
5463 Elf_Internal_Shdr *ihdr, *ohdr;
5465 if (ibfd->xvec->flavour != bfd_target_elf_flavour
5466 || obfd->xvec->flavour != bfd_target_elf_flavour)
5467 return TRUE;
5469 ihdr = &elf_section_data (isec)->this_hdr;
5470 ohdr = &elf_section_data (osec)->this_hdr;
5472 ohdr->sh_entsize = ihdr->sh_entsize;
5474 if (ihdr->sh_type == SHT_SYMTAB
5475 || ihdr->sh_type == SHT_DYNSYM
5476 || ihdr->sh_type == SHT_GNU_verneed
5477 || ihdr->sh_type == SHT_GNU_verdef)
5478 ohdr->sh_info = ihdr->sh_info;
5480 /* Set things up for objcopy. The output SHT_GROUP section will
5481 have its elf_next_in_group pointing back to the input group
5482 members. */
5483 elf_next_in_group (osec) = elf_next_in_group (isec);
5484 elf_group_name (osec) = elf_group_name (isec);
5486 osec->use_rela_p = isec->use_rela_p;
5488 return TRUE;
5491 /* Copy private header information. */
5493 bfd_boolean
5494 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
5496 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5497 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5498 return TRUE;
5500 /* Copy over private BFD data if it has not already been copied.
5501 This must be done here, rather than in the copy_private_bfd_data
5502 entry point, because the latter is called after the section
5503 contents have been set, which means that the program headers have
5504 already been worked out. */
5505 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
5507 if (! copy_private_bfd_data (ibfd, obfd))
5508 return FALSE;
5511 return TRUE;
5514 /* Copy private symbol information. If this symbol is in a section
5515 which we did not map into a BFD section, try to map the section
5516 index correctly. We use special macro definitions for the mapped
5517 section indices; these definitions are interpreted by the
5518 swap_out_syms function. */
5520 #define MAP_ONESYMTAB (SHN_HIOS + 1)
5521 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
5522 #define MAP_STRTAB (SHN_HIOS + 3)
5523 #define MAP_SHSTRTAB (SHN_HIOS + 4)
5524 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
5526 bfd_boolean
5527 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
5528 asymbol *isymarg,
5529 bfd *obfd,
5530 asymbol *osymarg)
5532 elf_symbol_type *isym, *osym;
5534 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5535 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5536 return TRUE;
5538 isym = elf_symbol_from (ibfd, isymarg);
5539 osym = elf_symbol_from (obfd, osymarg);
5541 if (isym != NULL
5542 && osym != NULL
5543 && bfd_is_abs_section (isym->symbol.section))
5545 unsigned int shndx;
5547 shndx = isym->internal_elf_sym.st_shndx;
5548 if (shndx == elf_onesymtab (ibfd))
5549 shndx = MAP_ONESYMTAB;
5550 else if (shndx == elf_dynsymtab (ibfd))
5551 shndx = MAP_DYNSYMTAB;
5552 else if (shndx == elf_tdata (ibfd)->strtab_section)
5553 shndx = MAP_STRTAB;
5554 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
5555 shndx = MAP_SHSTRTAB;
5556 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
5557 shndx = MAP_SYM_SHNDX;
5558 osym->internal_elf_sym.st_shndx = shndx;
5561 return TRUE;
5564 /* Swap out the symbols. */
5566 static bfd_boolean
5567 swap_out_syms (bfd *abfd,
5568 struct bfd_strtab_hash **sttp,
5569 int relocatable_p)
5571 const struct elf_backend_data *bed;
5572 int symcount;
5573 asymbol **syms;
5574 struct bfd_strtab_hash *stt;
5575 Elf_Internal_Shdr *symtab_hdr;
5576 Elf_Internal_Shdr *symtab_shndx_hdr;
5577 Elf_Internal_Shdr *symstrtab_hdr;
5578 char *outbound_syms;
5579 char *outbound_shndx;
5580 int idx;
5581 bfd_size_type amt;
5582 bfd_boolean name_local_sections;
5584 if (!elf_map_symbols (abfd))
5585 return FALSE;
5587 /* Dump out the symtabs. */
5588 stt = _bfd_elf_stringtab_init ();
5589 if (stt == NULL)
5590 return FALSE;
5592 bed = get_elf_backend_data (abfd);
5593 symcount = bfd_get_symcount (abfd);
5594 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5595 symtab_hdr->sh_type = SHT_SYMTAB;
5596 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
5597 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
5598 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
5599 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
5601 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5602 symstrtab_hdr->sh_type = SHT_STRTAB;
5604 amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym;
5605 outbound_syms = bfd_alloc (abfd, amt);
5606 if (outbound_syms == NULL)
5608 _bfd_stringtab_free (stt);
5609 return FALSE;
5611 symtab_hdr->contents = outbound_syms;
5613 outbound_shndx = NULL;
5614 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
5615 if (symtab_shndx_hdr->sh_name != 0)
5617 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
5618 outbound_shndx = bfd_zalloc (abfd, amt);
5619 if (outbound_shndx == NULL)
5621 _bfd_stringtab_free (stt);
5622 return FALSE;
5625 symtab_shndx_hdr->contents = outbound_shndx;
5626 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
5627 symtab_shndx_hdr->sh_size = amt;
5628 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
5629 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
5632 /* Now generate the data (for "contents"). */
5634 /* Fill in zeroth symbol and swap it out. */
5635 Elf_Internal_Sym sym;
5636 sym.st_name = 0;
5637 sym.st_value = 0;
5638 sym.st_size = 0;
5639 sym.st_info = 0;
5640 sym.st_other = 0;
5641 sym.st_shndx = SHN_UNDEF;
5642 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5643 outbound_syms += bed->s->sizeof_sym;
5644 if (outbound_shndx != NULL)
5645 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5648 name_local_sections
5649 = (bed->elf_backend_name_local_section_symbols
5650 && bed->elf_backend_name_local_section_symbols (abfd));
5652 syms = bfd_get_outsymbols (abfd);
5653 for (idx = 0; idx < symcount; idx++)
5655 Elf_Internal_Sym sym;
5656 bfd_vma value = syms[idx]->value;
5657 elf_symbol_type *type_ptr;
5658 flagword flags = syms[idx]->flags;
5659 int type;
5661 if (!name_local_sections
5662 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
5664 /* Local section symbols have no name. */
5665 sym.st_name = 0;
5667 else
5669 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
5670 syms[idx]->name,
5671 TRUE, FALSE);
5672 if (sym.st_name == (unsigned long) -1)
5674 _bfd_stringtab_free (stt);
5675 return FALSE;
5679 type_ptr = elf_symbol_from (abfd, syms[idx]);
5681 if ((flags & BSF_SECTION_SYM) == 0
5682 && bfd_is_com_section (syms[idx]->section))
5684 /* ELF common symbols put the alignment into the `value' field,
5685 and the size into the `size' field. This is backwards from
5686 how BFD handles it, so reverse it here. */
5687 sym.st_size = value;
5688 if (type_ptr == NULL
5689 || type_ptr->internal_elf_sym.st_value == 0)
5690 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
5691 else
5692 sym.st_value = type_ptr->internal_elf_sym.st_value;
5693 sym.st_shndx = _bfd_elf_section_from_bfd_section
5694 (abfd, syms[idx]->section);
5696 else
5698 asection *sec = syms[idx]->section;
5699 int shndx;
5701 if (sec->output_section)
5703 value += sec->output_offset;
5704 sec = sec->output_section;
5707 /* Don't add in the section vma for relocatable output. */
5708 if (! relocatable_p)
5709 value += sec->vma;
5710 sym.st_value = value;
5711 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
5713 if (bfd_is_abs_section (sec)
5714 && type_ptr != NULL
5715 && type_ptr->internal_elf_sym.st_shndx != 0)
5717 /* This symbol is in a real ELF section which we did
5718 not create as a BFD section. Undo the mapping done
5719 by copy_private_symbol_data. */
5720 shndx = type_ptr->internal_elf_sym.st_shndx;
5721 switch (shndx)
5723 case MAP_ONESYMTAB:
5724 shndx = elf_onesymtab (abfd);
5725 break;
5726 case MAP_DYNSYMTAB:
5727 shndx = elf_dynsymtab (abfd);
5728 break;
5729 case MAP_STRTAB:
5730 shndx = elf_tdata (abfd)->strtab_section;
5731 break;
5732 case MAP_SHSTRTAB:
5733 shndx = elf_tdata (abfd)->shstrtab_section;
5734 break;
5735 case MAP_SYM_SHNDX:
5736 shndx = elf_tdata (abfd)->symtab_shndx_section;
5737 break;
5738 default:
5739 break;
5742 else
5744 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
5746 if (shndx == -1)
5748 asection *sec2;
5750 /* Writing this would be a hell of a lot easier if
5751 we had some decent documentation on bfd, and
5752 knew what to expect of the library, and what to
5753 demand of applications. For example, it
5754 appears that `objcopy' might not set the
5755 section of a symbol to be a section that is
5756 actually in the output file. */
5757 sec2 = bfd_get_section_by_name (abfd, sec->name);
5758 if (sec2 == NULL)
5760 _bfd_error_handler (_("\
5761 Unable to find equivalent output section for symbol '%s' from section '%s'"),
5762 syms[idx]->name ? syms[idx]->name : "<Local sym>",
5763 sec->name);
5764 bfd_set_error (bfd_error_invalid_operation);
5765 _bfd_stringtab_free (stt);
5766 return FALSE;
5769 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
5770 BFD_ASSERT (shndx != -1);
5774 sym.st_shndx = shndx;
5777 if ((flags & BSF_THREAD_LOCAL) != 0)
5778 type = STT_TLS;
5779 else if ((flags & BSF_FUNCTION) != 0)
5780 type = STT_FUNC;
5781 else if ((flags & BSF_OBJECT) != 0)
5782 type = STT_OBJECT;
5783 else
5784 type = STT_NOTYPE;
5786 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
5787 type = STT_TLS;
5789 /* Processor-specific types. */
5790 if (type_ptr != NULL
5791 && bed->elf_backend_get_symbol_type)
5792 type = ((*bed->elf_backend_get_symbol_type)
5793 (&type_ptr->internal_elf_sym, type));
5795 if (flags & BSF_SECTION_SYM)
5797 if (flags & BSF_GLOBAL)
5798 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5799 else
5800 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5802 else if (bfd_is_com_section (syms[idx]->section))
5803 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
5804 else if (bfd_is_und_section (syms[idx]->section))
5805 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
5806 ? STB_WEAK
5807 : STB_GLOBAL),
5808 type);
5809 else if (flags & BSF_FILE)
5810 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5811 else
5813 int bind = STB_LOCAL;
5815 if (flags & BSF_LOCAL)
5816 bind = STB_LOCAL;
5817 else if (flags & BSF_WEAK)
5818 bind = STB_WEAK;
5819 else if (flags & BSF_GLOBAL)
5820 bind = STB_GLOBAL;
5822 sym.st_info = ELF_ST_INFO (bind, type);
5825 if (type_ptr != NULL)
5826 sym.st_other = type_ptr->internal_elf_sym.st_other;
5827 else
5828 sym.st_other = 0;
5830 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5831 outbound_syms += bed->s->sizeof_sym;
5832 if (outbound_shndx != NULL)
5833 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5836 *sttp = stt;
5837 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
5838 symstrtab_hdr->sh_type = SHT_STRTAB;
5840 symstrtab_hdr->sh_flags = 0;
5841 symstrtab_hdr->sh_addr = 0;
5842 symstrtab_hdr->sh_entsize = 0;
5843 symstrtab_hdr->sh_link = 0;
5844 symstrtab_hdr->sh_info = 0;
5845 symstrtab_hdr->sh_addralign = 1;
5847 return TRUE;
5850 /* Return the number of bytes required to hold the symtab vector.
5852 Note that we base it on the count plus 1, since we will null terminate
5853 the vector allocated based on this size. However, the ELF symbol table
5854 always has a dummy entry as symbol #0, so it ends up even. */
5856 long
5857 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
5859 long symcount;
5860 long symtab_size;
5861 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
5863 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5864 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5865 if (symcount > 0)
5866 symtab_size -= sizeof (asymbol *);
5868 return symtab_size;
5871 long
5872 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
5874 long symcount;
5875 long symtab_size;
5876 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
5878 if (elf_dynsymtab (abfd) == 0)
5880 bfd_set_error (bfd_error_invalid_operation);
5881 return -1;
5884 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5885 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5886 if (symcount > 0)
5887 symtab_size -= sizeof (asymbol *);
5889 return symtab_size;
5892 long
5893 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
5894 sec_ptr asect)
5896 return (asect->reloc_count + 1) * sizeof (arelent *);
5899 /* Canonicalize the relocs. */
5901 long
5902 _bfd_elf_canonicalize_reloc (bfd *abfd,
5903 sec_ptr section,
5904 arelent **relptr,
5905 asymbol **symbols)
5907 arelent *tblptr;
5908 unsigned int i;
5909 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5911 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
5912 return -1;
5914 tblptr = section->relocation;
5915 for (i = 0; i < section->reloc_count; i++)
5916 *relptr++ = tblptr++;
5918 *relptr = NULL;
5920 return section->reloc_count;
5923 long
5924 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
5926 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5927 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
5929 if (symcount >= 0)
5930 bfd_get_symcount (abfd) = symcount;
5931 return symcount;
5934 long
5935 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
5936 asymbol **allocation)
5938 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5939 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
5941 if (symcount >= 0)
5942 bfd_get_dynamic_symcount (abfd) = symcount;
5943 return symcount;
5946 /* Return the size required for the dynamic reloc entries. Any loadable
5947 section that was actually installed in the BFD, and has type SHT_REL
5948 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
5949 dynamic reloc section. */
5951 long
5952 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
5954 long ret;
5955 asection *s;
5957 if (elf_dynsymtab (abfd) == 0)
5959 bfd_set_error (bfd_error_invalid_operation);
5960 return -1;
5963 ret = sizeof (arelent *);
5964 for (s = abfd->sections; s != NULL; s = s->next)
5965 if ((s->flags & SEC_LOAD) != 0
5966 && elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5967 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5968 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5969 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
5970 * sizeof (arelent *));
5972 return ret;
5975 /* Canonicalize the dynamic relocation entries. Note that we return the
5976 dynamic relocations as a single block, although they are actually
5977 associated with particular sections; the interface, which was
5978 designed for SunOS style shared libraries, expects that there is only
5979 one set of dynamic relocs. Any loadable section that was actually
5980 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
5981 dynamic symbol table, is considered to be a dynamic reloc section. */
5983 long
5984 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
5985 arelent **storage,
5986 asymbol **syms)
5988 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
5989 asection *s;
5990 long ret;
5992 if (elf_dynsymtab (abfd) == 0)
5994 bfd_set_error (bfd_error_invalid_operation);
5995 return -1;
5998 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
5999 ret = 0;
6000 for (s = abfd->sections; s != NULL; s = s->next)
6002 if ((s->flags & SEC_LOAD) != 0
6003 && elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6004 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6005 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6007 arelent *p;
6008 long count, i;
6010 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
6011 return -1;
6012 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
6013 p = s->relocation;
6014 for (i = 0; i < count; i++)
6015 *storage++ = p++;
6016 ret += count;
6020 *storage = NULL;
6022 return ret;
6025 /* Read in the version information. */
6027 bfd_boolean
6028 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
6030 bfd_byte *contents = NULL;
6031 bfd_size_type amt;
6032 unsigned int freeidx = 0;
6034 if (elf_dynverref (abfd) != 0)
6036 Elf_Internal_Shdr *hdr;
6037 Elf_External_Verneed *everneed;
6038 Elf_Internal_Verneed *iverneed;
6039 unsigned int i;
6041 hdr = &elf_tdata (abfd)->dynverref_hdr;
6043 amt = (bfd_size_type) hdr->sh_info * sizeof (Elf_Internal_Verneed);
6044 elf_tdata (abfd)->verref = bfd_zalloc (abfd, amt);
6045 if (elf_tdata (abfd)->verref == NULL)
6046 goto error_return;
6048 elf_tdata (abfd)->cverrefs = hdr->sh_info;
6050 contents = bfd_malloc (hdr->sh_size);
6051 if (contents == NULL)
6052 goto error_return;
6053 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6054 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6055 goto error_return;
6057 everneed = (Elf_External_Verneed *) contents;
6058 iverneed = elf_tdata (abfd)->verref;
6059 for (i = 0; i < hdr->sh_info; i++, iverneed++)
6061 Elf_External_Vernaux *evernaux;
6062 Elf_Internal_Vernaux *ivernaux;
6063 unsigned int j;
6065 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
6067 iverneed->vn_bfd = abfd;
6069 iverneed->vn_filename =
6070 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6071 iverneed->vn_file);
6072 if (iverneed->vn_filename == NULL)
6073 goto error_return;
6075 amt = iverneed->vn_cnt;
6076 amt *= sizeof (Elf_Internal_Vernaux);
6077 iverneed->vn_auxptr = bfd_alloc (abfd, amt);
6079 evernaux = ((Elf_External_Vernaux *)
6080 ((bfd_byte *) everneed + iverneed->vn_aux));
6081 ivernaux = iverneed->vn_auxptr;
6082 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
6084 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
6086 ivernaux->vna_nodename =
6087 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6088 ivernaux->vna_name);
6089 if (ivernaux->vna_nodename == NULL)
6090 goto error_return;
6092 if (j + 1 < iverneed->vn_cnt)
6093 ivernaux->vna_nextptr = ivernaux + 1;
6094 else
6095 ivernaux->vna_nextptr = NULL;
6097 evernaux = ((Elf_External_Vernaux *)
6098 ((bfd_byte *) evernaux + ivernaux->vna_next));
6100 if (ivernaux->vna_other > freeidx)
6101 freeidx = ivernaux->vna_other;
6104 if (i + 1 < hdr->sh_info)
6105 iverneed->vn_nextref = iverneed + 1;
6106 else
6107 iverneed->vn_nextref = NULL;
6109 everneed = ((Elf_External_Verneed *)
6110 ((bfd_byte *) everneed + iverneed->vn_next));
6113 free (contents);
6114 contents = NULL;
6117 if (elf_dynverdef (abfd) != 0)
6119 Elf_Internal_Shdr *hdr;
6120 Elf_External_Verdef *everdef;
6121 Elf_Internal_Verdef *iverdef;
6122 Elf_Internal_Verdef *iverdefarr;
6123 Elf_Internal_Verdef iverdefmem;
6124 unsigned int i;
6125 unsigned int maxidx;
6127 hdr = &elf_tdata (abfd)->dynverdef_hdr;
6129 contents = bfd_malloc (hdr->sh_size);
6130 if (contents == NULL)
6131 goto error_return;
6132 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6133 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6134 goto error_return;
6136 /* We know the number of entries in the section but not the maximum
6137 index. Therefore we have to run through all entries and find
6138 the maximum. */
6139 everdef = (Elf_External_Verdef *) contents;
6140 maxidx = 0;
6141 for (i = 0; i < hdr->sh_info; ++i)
6143 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6145 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
6146 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
6148 everdef = ((Elf_External_Verdef *)
6149 ((bfd_byte *) everdef + iverdefmem.vd_next));
6152 if (default_imported_symver)
6154 if (freeidx > maxidx)
6155 maxidx = ++freeidx;
6156 else
6157 freeidx = ++maxidx;
6159 amt = (bfd_size_type) maxidx * sizeof (Elf_Internal_Verdef);
6160 elf_tdata (abfd)->verdef = bfd_zalloc (abfd, amt);
6161 if (elf_tdata (abfd)->verdef == NULL)
6162 goto error_return;
6164 elf_tdata (abfd)->cverdefs = maxidx;
6166 everdef = (Elf_External_Verdef *) contents;
6167 iverdefarr = elf_tdata (abfd)->verdef;
6168 for (i = 0; i < hdr->sh_info; i++)
6170 Elf_External_Verdaux *everdaux;
6171 Elf_Internal_Verdaux *iverdaux;
6172 unsigned int j;
6174 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6176 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
6177 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
6179 iverdef->vd_bfd = abfd;
6181 amt = (bfd_size_type) iverdef->vd_cnt * sizeof (Elf_Internal_Verdaux);
6182 iverdef->vd_auxptr = bfd_alloc (abfd, amt);
6183 if (iverdef->vd_auxptr == NULL)
6184 goto error_return;
6186 everdaux = ((Elf_External_Verdaux *)
6187 ((bfd_byte *) everdef + iverdef->vd_aux));
6188 iverdaux = iverdef->vd_auxptr;
6189 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
6191 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
6193 iverdaux->vda_nodename =
6194 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6195 iverdaux->vda_name);
6196 if (iverdaux->vda_nodename == NULL)
6197 goto error_return;
6199 if (j + 1 < iverdef->vd_cnt)
6200 iverdaux->vda_nextptr = iverdaux + 1;
6201 else
6202 iverdaux->vda_nextptr = NULL;
6204 everdaux = ((Elf_External_Verdaux *)
6205 ((bfd_byte *) everdaux + iverdaux->vda_next));
6208 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
6210 if (i + 1 < hdr->sh_info)
6211 iverdef->vd_nextdef = iverdef + 1;
6212 else
6213 iverdef->vd_nextdef = NULL;
6215 everdef = ((Elf_External_Verdef *)
6216 ((bfd_byte *) everdef + iverdef->vd_next));
6219 free (contents);
6220 contents = NULL;
6222 else if (default_imported_symver)
6224 if (freeidx < 3)
6225 freeidx = 3;
6226 else
6227 freeidx++;
6229 amt = (bfd_size_type) freeidx * sizeof (Elf_Internal_Verdef);
6230 elf_tdata (abfd)->verdef = bfd_zalloc (abfd, amt);
6231 if (elf_tdata (abfd)->verdef == NULL)
6232 goto error_return;
6234 elf_tdata (abfd)->cverdefs = freeidx;
6237 /* Create a default version based on the soname. */
6238 if (default_imported_symver)
6240 Elf_Internal_Verdef *iverdef;
6241 Elf_Internal_Verdaux *iverdaux;
6243 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];;
6245 iverdef->vd_version = VER_DEF_CURRENT;
6246 iverdef->vd_flags = 0;
6247 iverdef->vd_ndx = freeidx;
6248 iverdef->vd_cnt = 1;
6250 iverdef->vd_bfd = abfd;
6252 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
6253 if (iverdef->vd_nodename == NULL)
6254 goto error_return;
6255 iverdef->vd_nextdef = NULL;
6256 amt = (bfd_size_type) sizeof (Elf_Internal_Verdaux);
6257 iverdef->vd_auxptr = bfd_alloc (abfd, amt);
6259 iverdaux = iverdef->vd_auxptr;
6260 iverdaux->vda_nodename = iverdef->vd_nodename;
6261 iverdaux->vda_nextptr = NULL;
6264 return TRUE;
6266 error_return:
6267 if (contents != NULL)
6268 free (contents);
6269 return FALSE;
6272 asymbol *
6273 _bfd_elf_make_empty_symbol (bfd *abfd)
6275 elf_symbol_type *newsym;
6276 bfd_size_type amt = sizeof (elf_symbol_type);
6278 newsym = bfd_zalloc (abfd, amt);
6279 if (!newsym)
6280 return NULL;
6281 else
6283 newsym->symbol.the_bfd = abfd;
6284 return &newsym->symbol;
6288 void
6289 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
6290 asymbol *symbol,
6291 symbol_info *ret)
6293 bfd_symbol_info (symbol, ret);
6296 /* Return whether a symbol name implies a local symbol. Most targets
6297 use this function for the is_local_label_name entry point, but some
6298 override it. */
6300 bfd_boolean
6301 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
6302 const char *name)
6304 /* Normal local symbols start with ``.L''. */
6305 if (name[0] == '.' && name[1] == 'L')
6306 return TRUE;
6308 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
6309 DWARF debugging symbols starting with ``..''. */
6310 if (name[0] == '.' && name[1] == '.')
6311 return TRUE;
6313 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
6314 emitting DWARF debugging output. I suspect this is actually a
6315 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
6316 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
6317 underscore to be emitted on some ELF targets). For ease of use,
6318 we treat such symbols as local. */
6319 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
6320 return TRUE;
6322 return FALSE;
6325 alent *
6326 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
6327 asymbol *symbol ATTRIBUTE_UNUSED)
6329 abort ();
6330 return NULL;
6333 bfd_boolean
6334 _bfd_elf_set_arch_mach (bfd *abfd,
6335 enum bfd_architecture arch,
6336 unsigned long machine)
6338 /* If this isn't the right architecture for this backend, and this
6339 isn't the generic backend, fail. */
6340 if (arch != get_elf_backend_data (abfd)->arch
6341 && arch != bfd_arch_unknown
6342 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
6343 return FALSE;
6345 return bfd_default_set_arch_mach (abfd, arch, machine);
6348 /* Find the function to a particular section and offset,
6349 for error reporting. */
6351 static bfd_boolean
6352 elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
6353 asection *section,
6354 asymbol **symbols,
6355 bfd_vma offset,
6356 const char **filename_ptr,
6357 const char **functionname_ptr)
6359 const char *filename;
6360 asymbol *func, *file;
6361 bfd_vma low_func;
6362 asymbol **p;
6363 /* ??? Given multiple file symbols, it is impossible to reliably
6364 choose the right file name for global symbols. File symbols are
6365 local symbols, and thus all file symbols must sort before any
6366 global symbols. The ELF spec may be interpreted to say that a
6367 file symbol must sort before other local symbols, but currently
6368 ld -r doesn't do this. So, for ld -r output, it is possible to
6369 make a better choice of file name for local symbols by ignoring
6370 file symbols appearing after a given local symbol. */
6371 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
6373 filename = NULL;
6374 func = NULL;
6375 file = NULL;
6376 low_func = 0;
6377 state = nothing_seen;
6379 for (p = symbols; *p != NULL; p++)
6381 elf_symbol_type *q;
6383 q = (elf_symbol_type *) *p;
6385 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
6387 default:
6388 break;
6389 case STT_FILE:
6390 file = &q->symbol;
6391 if (state == symbol_seen)
6392 state = file_after_symbol_seen;
6393 continue;
6394 case STT_SECTION:
6395 continue;
6396 case STT_NOTYPE:
6397 case STT_FUNC:
6398 if (bfd_get_section (&q->symbol) == section
6399 && q->symbol.value >= low_func
6400 && q->symbol.value <= offset)
6402 func = (asymbol *) q;
6403 low_func = q->symbol.value;
6404 if (file == NULL)
6405 filename = NULL;
6406 else if (ELF_ST_BIND (q->internal_elf_sym.st_info) != STB_LOCAL
6407 && state == file_after_symbol_seen)
6408 filename = NULL;
6409 else
6410 filename = bfd_asymbol_name (file);
6412 break;
6414 if (state == nothing_seen)
6415 state = symbol_seen;
6418 if (func == NULL)
6419 return FALSE;
6421 if (filename_ptr)
6422 *filename_ptr = filename;
6423 if (functionname_ptr)
6424 *functionname_ptr = bfd_asymbol_name (func);
6426 return TRUE;
6429 /* Find the nearest line to a particular section and offset,
6430 for error reporting. */
6432 bfd_boolean
6433 _bfd_elf_find_nearest_line (bfd *abfd,
6434 asection *section,
6435 asymbol **symbols,
6436 bfd_vma offset,
6437 const char **filename_ptr,
6438 const char **functionname_ptr,
6439 unsigned int *line_ptr)
6441 bfd_boolean found;
6443 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
6444 filename_ptr, functionname_ptr,
6445 line_ptr))
6447 if (!*functionname_ptr)
6448 elf_find_function (abfd, section, symbols, offset,
6449 *filename_ptr ? NULL : filename_ptr,
6450 functionname_ptr);
6452 return TRUE;
6455 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
6456 filename_ptr, functionname_ptr,
6457 line_ptr, 0,
6458 &elf_tdata (abfd)->dwarf2_find_line_info))
6460 if (!*functionname_ptr)
6461 elf_find_function (abfd, section, symbols, offset,
6462 *filename_ptr ? NULL : filename_ptr,
6463 functionname_ptr);
6465 return TRUE;
6468 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
6469 &found, filename_ptr,
6470 functionname_ptr, line_ptr,
6471 &elf_tdata (abfd)->line_info))
6472 return FALSE;
6473 if (found && (*functionname_ptr || *line_ptr))
6474 return TRUE;
6476 if (symbols == NULL)
6477 return FALSE;
6479 if (! elf_find_function (abfd, section, symbols, offset,
6480 filename_ptr, functionname_ptr))
6481 return FALSE;
6483 *line_ptr = 0;
6484 return TRUE;
6488 _bfd_elf_sizeof_headers (bfd *abfd, bfd_boolean reloc)
6490 int ret;
6492 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
6493 if (! reloc)
6494 ret += get_program_header_size (abfd);
6495 return ret;
6498 bfd_boolean
6499 _bfd_elf_set_section_contents (bfd *abfd,
6500 sec_ptr section,
6501 const void *location,
6502 file_ptr offset,
6503 bfd_size_type count)
6505 Elf_Internal_Shdr *hdr;
6506 bfd_signed_vma pos;
6508 if (! abfd->output_has_begun
6509 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
6510 return FALSE;
6512 hdr = &elf_section_data (section)->this_hdr;
6513 pos = hdr->sh_offset + offset;
6514 if (bfd_seek (abfd, pos, SEEK_SET) != 0
6515 || bfd_bwrite (location, count, abfd) != count)
6516 return FALSE;
6518 return TRUE;
6521 void
6522 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
6523 arelent *cache_ptr ATTRIBUTE_UNUSED,
6524 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
6526 abort ();
6529 /* Try to convert a non-ELF reloc into an ELF one. */
6531 bfd_boolean
6532 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
6534 /* Check whether we really have an ELF howto. */
6536 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
6538 bfd_reloc_code_real_type code;
6539 reloc_howto_type *howto;
6541 /* Alien reloc: Try to determine its type to replace it with an
6542 equivalent ELF reloc. */
6544 if (areloc->howto->pc_relative)
6546 switch (areloc->howto->bitsize)
6548 case 8:
6549 code = BFD_RELOC_8_PCREL;
6550 break;
6551 case 12:
6552 code = BFD_RELOC_12_PCREL;
6553 break;
6554 case 16:
6555 code = BFD_RELOC_16_PCREL;
6556 break;
6557 case 24:
6558 code = BFD_RELOC_24_PCREL;
6559 break;
6560 case 32:
6561 code = BFD_RELOC_32_PCREL;
6562 break;
6563 case 64:
6564 code = BFD_RELOC_64_PCREL;
6565 break;
6566 default:
6567 goto fail;
6570 howto = bfd_reloc_type_lookup (abfd, code);
6572 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
6574 if (howto->pcrel_offset)
6575 areloc->addend += areloc->address;
6576 else
6577 areloc->addend -= areloc->address; /* addend is unsigned!! */
6580 else
6582 switch (areloc->howto->bitsize)
6584 case 8:
6585 code = BFD_RELOC_8;
6586 break;
6587 case 14:
6588 code = BFD_RELOC_14;
6589 break;
6590 case 16:
6591 code = BFD_RELOC_16;
6592 break;
6593 case 26:
6594 code = BFD_RELOC_26;
6595 break;
6596 case 32:
6597 code = BFD_RELOC_32;
6598 break;
6599 case 64:
6600 code = BFD_RELOC_64;
6601 break;
6602 default:
6603 goto fail;
6606 howto = bfd_reloc_type_lookup (abfd, code);
6609 if (howto)
6610 areloc->howto = howto;
6611 else
6612 goto fail;
6615 return TRUE;
6617 fail:
6618 (*_bfd_error_handler)
6619 (_("%B: unsupported relocation type %s"),
6620 abfd, areloc->howto->name);
6621 bfd_set_error (bfd_error_bad_value);
6622 return FALSE;
6625 bfd_boolean
6626 _bfd_elf_close_and_cleanup (bfd *abfd)
6628 if (bfd_get_format (abfd) == bfd_object)
6630 if (elf_shstrtab (abfd) != NULL)
6631 _bfd_elf_strtab_free (elf_shstrtab (abfd));
6634 return _bfd_generic_close_and_cleanup (abfd);
6637 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
6638 in the relocation's offset. Thus we cannot allow any sort of sanity
6639 range-checking to interfere. There is nothing else to do in processing
6640 this reloc. */
6642 bfd_reloc_status_type
6643 _bfd_elf_rel_vtable_reloc_fn
6644 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
6645 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
6646 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
6647 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
6649 return bfd_reloc_ok;
6652 /* Elf core file support. Much of this only works on native
6653 toolchains, since we rely on knowing the
6654 machine-dependent procfs structure in order to pick
6655 out details about the corefile. */
6657 #ifdef HAVE_SYS_PROCFS_H
6658 # include <sys/procfs.h>
6659 #endif
6661 /* FIXME: this is kinda wrong, but it's what gdb wants. */
6663 static int
6664 elfcore_make_pid (bfd *abfd)
6666 return ((elf_tdata (abfd)->core_lwpid << 16)
6667 + (elf_tdata (abfd)->core_pid));
6670 /* If there isn't a section called NAME, make one, using
6671 data from SECT. Note, this function will generate a
6672 reference to NAME, so you shouldn't deallocate or
6673 overwrite it. */
6675 static bfd_boolean
6676 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
6678 asection *sect2;
6680 if (bfd_get_section_by_name (abfd, name) != NULL)
6681 return TRUE;
6683 sect2 = bfd_make_section (abfd, name);
6684 if (sect2 == NULL)
6685 return FALSE;
6687 sect2->size = sect->size;
6688 sect2->filepos = sect->filepos;
6689 sect2->flags = sect->flags;
6690 sect2->alignment_power = sect->alignment_power;
6691 return TRUE;
6694 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
6695 actually creates up to two pseudosections:
6696 - For the single-threaded case, a section named NAME, unless
6697 such a section already exists.
6698 - For the multi-threaded case, a section named "NAME/PID", where
6699 PID is elfcore_make_pid (abfd).
6700 Both pseudosections have identical contents. */
6701 bfd_boolean
6702 _bfd_elfcore_make_pseudosection (bfd *abfd,
6703 char *name,
6704 size_t size,
6705 ufile_ptr filepos)
6707 char buf[100];
6708 char *threaded_name;
6709 size_t len;
6710 asection *sect;
6712 /* Build the section name. */
6714 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
6715 len = strlen (buf) + 1;
6716 threaded_name = bfd_alloc (abfd, len);
6717 if (threaded_name == NULL)
6718 return FALSE;
6719 memcpy (threaded_name, buf, len);
6721 sect = bfd_make_section_anyway (abfd, threaded_name);
6722 if (sect == NULL)
6723 return FALSE;
6724 sect->size = size;
6725 sect->filepos = filepos;
6726 sect->flags = SEC_HAS_CONTENTS;
6727 sect->alignment_power = 2;
6729 return elfcore_maybe_make_sect (abfd, name, sect);
6732 /* prstatus_t exists on:
6733 solaris 2.5+
6734 linux 2.[01] + glibc
6735 unixware 4.2
6738 #if defined (HAVE_PRSTATUS_T)
6740 static bfd_boolean
6741 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
6743 size_t size;
6744 int offset;
6746 if (note->descsz == sizeof (prstatus_t))
6748 prstatus_t prstat;
6750 size = sizeof (prstat.pr_reg);
6751 offset = offsetof (prstatus_t, pr_reg);
6752 memcpy (&prstat, note->descdata, sizeof (prstat));
6754 /* Do not overwrite the core signal if it
6755 has already been set by another thread. */
6756 if (elf_tdata (abfd)->core_signal == 0)
6757 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
6758 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6760 /* pr_who exists on:
6761 solaris 2.5+
6762 unixware 4.2
6763 pr_who doesn't exist on:
6764 linux 2.[01]
6766 #if defined (HAVE_PRSTATUS_T_PR_WHO)
6767 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6768 #endif
6770 #if defined (HAVE_PRSTATUS32_T)
6771 else if (note->descsz == sizeof (prstatus32_t))
6773 /* 64-bit host, 32-bit corefile */
6774 prstatus32_t prstat;
6776 size = sizeof (prstat.pr_reg);
6777 offset = offsetof (prstatus32_t, pr_reg);
6778 memcpy (&prstat, note->descdata, sizeof (prstat));
6780 /* Do not overwrite the core signal if it
6781 has already been set by another thread. */
6782 if (elf_tdata (abfd)->core_signal == 0)
6783 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
6784 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6786 /* pr_who exists on:
6787 solaris 2.5+
6788 unixware 4.2
6789 pr_who doesn't exist on:
6790 linux 2.[01]
6792 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
6793 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6794 #endif
6796 #endif /* HAVE_PRSTATUS32_T */
6797 else
6799 /* Fail - we don't know how to handle any other
6800 note size (ie. data object type). */
6801 return TRUE;
6804 /* Make a ".reg/999" section and a ".reg" section. */
6805 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
6806 size, note->descpos + offset);
6808 #endif /* defined (HAVE_PRSTATUS_T) */
6810 /* Create a pseudosection containing the exact contents of NOTE. */
6811 static bfd_boolean
6812 elfcore_make_note_pseudosection (bfd *abfd,
6813 char *name,
6814 Elf_Internal_Note *note)
6816 return _bfd_elfcore_make_pseudosection (abfd, name,
6817 note->descsz, note->descpos);
6820 /* There isn't a consistent prfpregset_t across platforms,
6821 but it doesn't matter, because we don't have to pick this
6822 data structure apart. */
6824 static bfd_boolean
6825 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
6827 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6830 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6831 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6832 literally. */
6834 static bfd_boolean
6835 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
6837 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
6840 #if defined (HAVE_PRPSINFO_T)
6841 typedef prpsinfo_t elfcore_psinfo_t;
6842 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6843 typedef prpsinfo32_t elfcore_psinfo32_t;
6844 #endif
6845 #endif
6847 #if defined (HAVE_PSINFO_T)
6848 typedef psinfo_t elfcore_psinfo_t;
6849 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6850 typedef psinfo32_t elfcore_psinfo32_t;
6851 #endif
6852 #endif
6854 /* return a malloc'ed copy of a string at START which is at
6855 most MAX bytes long, possibly without a terminating '\0'.
6856 the copy will always have a terminating '\0'. */
6858 char *
6859 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
6861 char *dups;
6862 char *end = memchr (start, '\0', max);
6863 size_t len;
6865 if (end == NULL)
6866 len = max;
6867 else
6868 len = end - start;
6870 dups = bfd_alloc (abfd, len + 1);
6871 if (dups == NULL)
6872 return NULL;
6874 memcpy (dups, start, len);
6875 dups[len] = '\0';
6877 return dups;
6880 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6881 static bfd_boolean
6882 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
6884 if (note->descsz == sizeof (elfcore_psinfo_t))
6886 elfcore_psinfo_t psinfo;
6888 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6890 elf_tdata (abfd)->core_program
6891 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6892 sizeof (psinfo.pr_fname));
6894 elf_tdata (abfd)->core_command
6895 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6896 sizeof (psinfo.pr_psargs));
6898 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6899 else if (note->descsz == sizeof (elfcore_psinfo32_t))
6901 /* 64-bit host, 32-bit corefile */
6902 elfcore_psinfo32_t psinfo;
6904 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6906 elf_tdata (abfd)->core_program
6907 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6908 sizeof (psinfo.pr_fname));
6910 elf_tdata (abfd)->core_command
6911 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6912 sizeof (psinfo.pr_psargs));
6914 #endif
6916 else
6918 /* Fail - we don't know how to handle any other
6919 note size (ie. data object type). */
6920 return TRUE;
6923 /* Note that for some reason, a spurious space is tacked
6924 onto the end of the args in some (at least one anyway)
6925 implementations, so strip it off if it exists. */
6928 char *command = elf_tdata (abfd)->core_command;
6929 int n = strlen (command);
6931 if (0 < n && command[n - 1] == ' ')
6932 command[n - 1] = '\0';
6935 return TRUE;
6937 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6939 #if defined (HAVE_PSTATUS_T)
6940 static bfd_boolean
6941 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
6943 if (note->descsz == sizeof (pstatus_t)
6944 #if defined (HAVE_PXSTATUS_T)
6945 || note->descsz == sizeof (pxstatus_t)
6946 #endif
6949 pstatus_t pstat;
6951 memcpy (&pstat, note->descdata, sizeof (pstat));
6953 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6955 #if defined (HAVE_PSTATUS32_T)
6956 else if (note->descsz == sizeof (pstatus32_t))
6958 /* 64-bit host, 32-bit corefile */
6959 pstatus32_t pstat;
6961 memcpy (&pstat, note->descdata, sizeof (pstat));
6963 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6965 #endif
6966 /* Could grab some more details from the "representative"
6967 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6968 NT_LWPSTATUS note, presumably. */
6970 return TRUE;
6972 #endif /* defined (HAVE_PSTATUS_T) */
6974 #if defined (HAVE_LWPSTATUS_T)
6975 static bfd_boolean
6976 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
6978 lwpstatus_t lwpstat;
6979 char buf[100];
6980 char *name;
6981 size_t len;
6982 asection *sect;
6984 if (note->descsz != sizeof (lwpstat)
6985 #if defined (HAVE_LWPXSTATUS_T)
6986 && note->descsz != sizeof (lwpxstatus_t)
6987 #endif
6989 return TRUE;
6991 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
6993 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
6994 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
6996 /* Make a ".reg/999" section. */
6998 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
6999 len = strlen (buf) + 1;
7000 name = bfd_alloc (abfd, len);
7001 if (name == NULL)
7002 return FALSE;
7003 memcpy (name, buf, len);
7005 sect = bfd_make_section_anyway (abfd, name);
7006 if (sect == NULL)
7007 return FALSE;
7009 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7010 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
7011 sect->filepos = note->descpos
7012 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
7013 #endif
7015 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7016 sect->size = sizeof (lwpstat.pr_reg);
7017 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
7018 #endif
7020 sect->flags = SEC_HAS_CONTENTS;
7021 sect->alignment_power = 2;
7023 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
7024 return FALSE;
7026 /* Make a ".reg2/999" section */
7028 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
7029 len = strlen (buf) + 1;
7030 name = bfd_alloc (abfd, len);
7031 if (name == NULL)
7032 return FALSE;
7033 memcpy (name, buf, len);
7035 sect = bfd_make_section_anyway (abfd, name);
7036 if (sect == NULL)
7037 return FALSE;
7039 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7040 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
7041 sect->filepos = note->descpos
7042 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
7043 #endif
7045 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
7046 sect->size = sizeof (lwpstat.pr_fpreg);
7047 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
7048 #endif
7050 sect->flags = SEC_HAS_CONTENTS;
7051 sect->alignment_power = 2;
7053 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
7055 #endif /* defined (HAVE_LWPSTATUS_T) */
7057 #if defined (HAVE_WIN32_PSTATUS_T)
7058 static bfd_boolean
7059 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
7061 char buf[30];
7062 char *name;
7063 size_t len;
7064 asection *sect;
7065 win32_pstatus_t pstatus;
7067 if (note->descsz < sizeof (pstatus))
7068 return TRUE;
7070 memcpy (&pstatus, note->descdata, sizeof (pstatus));
7072 switch (pstatus.data_type)
7074 case NOTE_INFO_PROCESS:
7075 /* FIXME: need to add ->core_command. */
7076 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
7077 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
7078 break;
7080 case NOTE_INFO_THREAD:
7081 /* Make a ".reg/999" section. */
7082 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
7084 len = strlen (buf) + 1;
7085 name = bfd_alloc (abfd, len);
7086 if (name == NULL)
7087 return FALSE;
7089 memcpy (name, buf, len);
7091 sect = bfd_make_section_anyway (abfd, name);
7092 if (sect == NULL)
7093 return FALSE;
7095 sect->size = sizeof (pstatus.data.thread_info.thread_context);
7096 sect->filepos = (note->descpos
7097 + offsetof (struct win32_pstatus,
7098 data.thread_info.thread_context));
7099 sect->flags = SEC_HAS_CONTENTS;
7100 sect->alignment_power = 2;
7102 if (pstatus.data.thread_info.is_active_thread)
7103 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
7104 return FALSE;
7105 break;
7107 case NOTE_INFO_MODULE:
7108 /* Make a ".module/xxxxxxxx" section. */
7109 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
7111 len = strlen (buf) + 1;
7112 name = bfd_alloc (abfd, len);
7113 if (name == NULL)
7114 return FALSE;
7116 memcpy (name, buf, len);
7118 sect = bfd_make_section_anyway (abfd, name);
7120 if (sect == NULL)
7121 return FALSE;
7123 sect->size = note->descsz;
7124 sect->filepos = note->descpos;
7125 sect->flags = SEC_HAS_CONTENTS;
7126 sect->alignment_power = 2;
7127 break;
7129 default:
7130 return TRUE;
7133 return TRUE;
7135 #endif /* HAVE_WIN32_PSTATUS_T */
7137 static bfd_boolean
7138 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
7140 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7142 switch (note->type)
7144 default:
7145 return TRUE;
7147 case NT_PRSTATUS:
7148 if (bed->elf_backend_grok_prstatus)
7149 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
7150 return TRUE;
7151 #if defined (HAVE_PRSTATUS_T)
7152 return elfcore_grok_prstatus (abfd, note);
7153 #else
7154 return TRUE;
7155 #endif
7157 #if defined (HAVE_PSTATUS_T)
7158 case NT_PSTATUS:
7159 return elfcore_grok_pstatus (abfd, note);
7160 #endif
7162 #if defined (HAVE_LWPSTATUS_T)
7163 case NT_LWPSTATUS:
7164 return elfcore_grok_lwpstatus (abfd, note);
7165 #endif
7167 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
7168 return elfcore_grok_prfpreg (abfd, note);
7170 #if defined (HAVE_WIN32_PSTATUS_T)
7171 case NT_WIN32PSTATUS:
7172 return elfcore_grok_win32pstatus (abfd, note);
7173 #endif
7175 case NT_PRXFPREG: /* Linux SSE extension */
7176 if (note->namesz == 6
7177 && strcmp (note->namedata, "LINUX") == 0)
7178 return elfcore_grok_prxfpreg (abfd, note);
7179 else
7180 return TRUE;
7182 case NT_PRPSINFO:
7183 case NT_PSINFO:
7184 if (bed->elf_backend_grok_psinfo)
7185 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
7186 return TRUE;
7187 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7188 return elfcore_grok_psinfo (abfd, note);
7189 #else
7190 return TRUE;
7191 #endif
7193 case NT_AUXV:
7195 asection *sect = bfd_make_section_anyway (abfd, ".auxv");
7197 if (sect == NULL)
7198 return FALSE;
7199 sect->size = note->descsz;
7200 sect->filepos = note->descpos;
7201 sect->flags = SEC_HAS_CONTENTS;
7202 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
7204 return TRUE;
7209 static bfd_boolean
7210 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
7212 char *cp;
7214 cp = strchr (note->namedata, '@');
7215 if (cp != NULL)
7217 *lwpidp = atoi(cp + 1);
7218 return TRUE;
7220 return FALSE;
7223 static bfd_boolean
7224 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
7227 /* Signal number at offset 0x08. */
7228 elf_tdata (abfd)->core_signal
7229 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
7231 /* Process ID at offset 0x50. */
7232 elf_tdata (abfd)->core_pid
7233 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
7235 /* Command name at 0x7c (max 32 bytes, including nul). */
7236 elf_tdata (abfd)->core_command
7237 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
7239 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
7240 note);
7243 static bfd_boolean
7244 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
7246 int lwp;
7248 if (elfcore_netbsd_get_lwpid (note, &lwp))
7249 elf_tdata (abfd)->core_lwpid = lwp;
7251 if (note->type == NT_NETBSDCORE_PROCINFO)
7253 /* NetBSD-specific core "procinfo". Note that we expect to
7254 find this note before any of the others, which is fine,
7255 since the kernel writes this note out first when it
7256 creates a core file. */
7258 return elfcore_grok_netbsd_procinfo (abfd, note);
7261 /* As of Jan 2002 there are no other machine-independent notes
7262 defined for NetBSD core files. If the note type is less
7263 than the start of the machine-dependent note types, we don't
7264 understand it. */
7266 if (note->type < NT_NETBSDCORE_FIRSTMACH)
7267 return TRUE;
7270 switch (bfd_get_arch (abfd))
7272 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
7273 PT_GETFPREGS == mach+2. */
7275 case bfd_arch_alpha:
7276 case bfd_arch_sparc:
7277 switch (note->type)
7279 case NT_NETBSDCORE_FIRSTMACH+0:
7280 return elfcore_make_note_pseudosection (abfd, ".reg", note);
7282 case NT_NETBSDCORE_FIRSTMACH+2:
7283 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7285 default:
7286 return TRUE;
7289 /* On all other arch's, PT_GETREGS == mach+1 and
7290 PT_GETFPREGS == mach+3. */
7292 default:
7293 switch (note->type)
7295 case NT_NETBSDCORE_FIRSTMACH+1:
7296 return elfcore_make_note_pseudosection (abfd, ".reg", note);
7298 case NT_NETBSDCORE_FIRSTMACH+3:
7299 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7301 default:
7302 return TRUE;
7305 /* NOTREACHED */
7308 static bfd_boolean
7309 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, pid_t *tid)
7311 void *ddata = note->descdata;
7312 char buf[100];
7313 char *name;
7314 asection *sect;
7315 short sig;
7316 unsigned flags;
7318 /* nto_procfs_status 'pid' field is at offset 0. */
7319 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
7321 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
7322 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
7324 /* nto_procfs_status 'flags' field is at offset 8. */
7325 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
7327 /* nto_procfs_status 'what' field is at offset 14. */
7328 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
7330 elf_tdata (abfd)->core_signal = sig;
7331 elf_tdata (abfd)->core_lwpid = *tid;
7334 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
7335 do not come from signals so we make sure we set the current
7336 thread just in case. */
7337 if (flags & 0x00000080)
7338 elf_tdata (abfd)->core_lwpid = *tid;
7340 /* Make a ".qnx_core_status/%d" section. */
7341 sprintf (buf, ".qnx_core_status/%d", *tid);
7343 name = bfd_alloc (abfd, strlen (buf) + 1);
7344 if (name == NULL)
7345 return FALSE;
7346 strcpy (name, buf);
7348 sect = bfd_make_section_anyway (abfd, name);
7349 if (sect == NULL)
7350 return FALSE;
7352 sect->size = note->descsz;
7353 sect->filepos = note->descpos;
7354 sect->flags = SEC_HAS_CONTENTS;
7355 sect->alignment_power = 2;
7357 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
7360 static bfd_boolean
7361 elfcore_grok_nto_regs (bfd *abfd,
7362 Elf_Internal_Note *note,
7363 pid_t tid,
7364 char *base)
7366 char buf[100];
7367 char *name;
7368 asection *sect;
7370 /* Make a "(base)/%d" section. */
7371 sprintf (buf, "%s/%d", base, tid);
7373 name = bfd_alloc (abfd, strlen (buf) + 1);
7374 if (name == NULL)
7375 return FALSE;
7376 strcpy (name, buf);
7378 sect = bfd_make_section_anyway (abfd, name);
7379 if (sect == NULL)
7380 return FALSE;
7382 sect->size = note->descsz;
7383 sect->filepos = note->descpos;
7384 sect->flags = SEC_HAS_CONTENTS;
7385 sect->alignment_power = 2;
7387 /* This is the current thread. */
7388 if (elf_tdata (abfd)->core_lwpid == tid)
7389 return elfcore_maybe_make_sect (abfd, base, sect);
7391 return TRUE;
7394 #define BFD_QNT_CORE_INFO 7
7395 #define BFD_QNT_CORE_STATUS 8
7396 #define BFD_QNT_CORE_GREG 9
7397 #define BFD_QNT_CORE_FPREG 10
7399 static bfd_boolean
7400 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
7402 /* Every GREG section has a STATUS section before it. Store the
7403 tid from the previous call to pass down to the next gregs
7404 function. */
7405 static pid_t tid = 1;
7407 switch (note->type)
7409 case BFD_QNT_CORE_INFO:
7410 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
7411 case BFD_QNT_CORE_STATUS:
7412 return elfcore_grok_nto_status (abfd, note, &tid);
7413 case BFD_QNT_CORE_GREG:
7414 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
7415 case BFD_QNT_CORE_FPREG:
7416 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
7417 default:
7418 return TRUE;
7422 /* Function: elfcore_write_note
7424 Inputs:
7425 buffer to hold note
7426 name of note
7427 type of note
7428 data for note
7429 size of data for note
7431 Return:
7432 End of buffer containing note. */
7434 char *
7435 elfcore_write_note (bfd *abfd,
7436 char *buf,
7437 int *bufsiz,
7438 const char *name,
7439 int type,
7440 const void *input,
7441 int size)
7443 Elf_External_Note *xnp;
7444 size_t namesz;
7445 size_t pad;
7446 size_t newspace;
7447 char *p, *dest;
7449 namesz = 0;
7450 pad = 0;
7451 if (name != NULL)
7453 const struct elf_backend_data *bed;
7455 namesz = strlen (name) + 1;
7456 bed = get_elf_backend_data (abfd);
7457 pad = -namesz & ((1 << bed->s->log_file_align) - 1);
7460 newspace = 12 + namesz + pad + size;
7462 p = realloc (buf, *bufsiz + newspace);
7463 dest = p + *bufsiz;
7464 *bufsiz += newspace;
7465 xnp = (Elf_External_Note *) dest;
7466 H_PUT_32 (abfd, namesz, xnp->namesz);
7467 H_PUT_32 (abfd, size, xnp->descsz);
7468 H_PUT_32 (abfd, type, xnp->type);
7469 dest = xnp->name;
7470 if (name != NULL)
7472 memcpy (dest, name, namesz);
7473 dest += namesz;
7474 while (pad != 0)
7476 *dest++ = '\0';
7477 --pad;
7480 memcpy (dest, input, size);
7481 return p;
7484 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7485 char *
7486 elfcore_write_prpsinfo (bfd *abfd,
7487 char *buf,
7488 int *bufsiz,
7489 const char *fname,
7490 const char *psargs)
7492 int note_type;
7493 char *note_name = "CORE";
7495 #if defined (HAVE_PSINFO_T)
7496 psinfo_t data;
7497 note_type = NT_PSINFO;
7498 #else
7499 prpsinfo_t data;
7500 note_type = NT_PRPSINFO;
7501 #endif
7503 memset (&data, 0, sizeof (data));
7504 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
7505 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
7506 return elfcore_write_note (abfd, buf, bufsiz,
7507 note_name, note_type, &data, sizeof (data));
7509 #endif /* PSINFO_T or PRPSINFO_T */
7511 #if defined (HAVE_PRSTATUS_T)
7512 char *
7513 elfcore_write_prstatus (bfd *abfd,
7514 char *buf,
7515 int *bufsiz,
7516 long pid,
7517 int cursig,
7518 const void *gregs)
7520 prstatus_t prstat;
7521 char *note_name = "CORE";
7523 memset (&prstat, 0, sizeof (prstat));
7524 prstat.pr_pid = pid;
7525 prstat.pr_cursig = cursig;
7526 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
7527 return elfcore_write_note (abfd, buf, bufsiz,
7528 note_name, NT_PRSTATUS, &prstat, sizeof (prstat));
7530 #endif /* HAVE_PRSTATUS_T */
7532 #if defined (HAVE_LWPSTATUS_T)
7533 char *
7534 elfcore_write_lwpstatus (bfd *abfd,
7535 char *buf,
7536 int *bufsiz,
7537 long pid,
7538 int cursig,
7539 const void *gregs)
7541 lwpstatus_t lwpstat;
7542 char *note_name = "CORE";
7544 memset (&lwpstat, 0, sizeof (lwpstat));
7545 lwpstat.pr_lwpid = pid >> 16;
7546 lwpstat.pr_cursig = cursig;
7547 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7548 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
7549 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7550 #if !defined(gregs)
7551 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
7552 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
7553 #else
7554 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
7555 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
7556 #endif
7557 #endif
7558 return elfcore_write_note (abfd, buf, bufsiz, note_name,
7559 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
7561 #endif /* HAVE_LWPSTATUS_T */
7563 #if defined (HAVE_PSTATUS_T)
7564 char *
7565 elfcore_write_pstatus (bfd *abfd,
7566 char *buf,
7567 int *bufsiz,
7568 long pid,
7569 int cursig,
7570 const void *gregs)
7572 pstatus_t pstat;
7573 char *note_name = "CORE";
7575 memset (&pstat, 0, sizeof (pstat));
7576 pstat.pr_pid = pid & 0xffff;
7577 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
7578 NT_PSTATUS, &pstat, sizeof (pstat));
7579 return buf;
7581 #endif /* HAVE_PSTATUS_T */
7583 char *
7584 elfcore_write_prfpreg (bfd *abfd,
7585 char *buf,
7586 int *bufsiz,
7587 const void *fpregs,
7588 int size)
7590 char *note_name = "CORE";
7591 return elfcore_write_note (abfd, buf, bufsiz,
7592 note_name, NT_FPREGSET, fpregs, size);
7595 char *
7596 elfcore_write_prxfpreg (bfd *abfd,
7597 char *buf,
7598 int *bufsiz,
7599 const void *xfpregs,
7600 int size)
7602 char *note_name = "LINUX";
7603 return elfcore_write_note (abfd, buf, bufsiz,
7604 note_name, NT_PRXFPREG, xfpregs, size);
7607 static bfd_boolean
7608 elfcore_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
7610 char *buf;
7611 char *p;
7613 if (size <= 0)
7614 return TRUE;
7616 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
7617 return FALSE;
7619 buf = bfd_malloc (size);
7620 if (buf == NULL)
7621 return FALSE;
7623 if (bfd_bread (buf, size, abfd) != size)
7625 error:
7626 free (buf);
7627 return FALSE;
7630 p = buf;
7631 while (p < buf + size)
7633 /* FIXME: bad alignment assumption. */
7634 Elf_External_Note *xnp = (Elf_External_Note *) p;
7635 Elf_Internal_Note in;
7637 in.type = H_GET_32 (abfd, xnp->type);
7639 in.namesz = H_GET_32 (abfd, xnp->namesz);
7640 in.namedata = xnp->name;
7642 in.descsz = H_GET_32 (abfd, xnp->descsz);
7643 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
7644 in.descpos = offset + (in.descdata - buf);
7646 if (strncmp (in.namedata, "NetBSD-CORE", 11) == 0)
7648 if (! elfcore_grok_netbsd_note (abfd, &in))
7649 goto error;
7651 else if (strncmp (in.namedata, "QNX", 3) == 0)
7653 if (! elfcore_grok_nto_note (abfd, &in))
7654 goto error;
7656 else
7658 if (! elfcore_grok_note (abfd, &in))
7659 goto error;
7662 p = in.descdata + BFD_ALIGN (in.descsz, 4);
7665 free (buf);
7666 return TRUE;
7669 /* Providing external access to the ELF program header table. */
7671 /* Return an upper bound on the number of bytes required to store a
7672 copy of ABFD's program header table entries. Return -1 if an error
7673 occurs; bfd_get_error will return an appropriate code. */
7675 long
7676 bfd_get_elf_phdr_upper_bound (bfd *abfd)
7678 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7680 bfd_set_error (bfd_error_wrong_format);
7681 return -1;
7684 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
7687 /* Copy ABFD's program header table entries to *PHDRS. The entries
7688 will be stored as an array of Elf_Internal_Phdr structures, as
7689 defined in include/elf/internal.h. To find out how large the
7690 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
7692 Return the number of program header table entries read, or -1 if an
7693 error occurs; bfd_get_error will return an appropriate code. */
7696 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
7698 int num_phdrs;
7700 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7702 bfd_set_error (bfd_error_wrong_format);
7703 return -1;
7706 num_phdrs = elf_elfheader (abfd)->e_phnum;
7707 memcpy (phdrs, elf_tdata (abfd)->phdr,
7708 num_phdrs * sizeof (Elf_Internal_Phdr));
7710 return num_phdrs;
7713 void
7714 _bfd_elf_sprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, char *buf, bfd_vma value)
7716 #ifdef BFD64
7717 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7719 i_ehdrp = elf_elfheader (abfd);
7720 if (i_ehdrp == NULL)
7721 sprintf_vma (buf, value);
7722 else
7724 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
7726 #if BFD_HOST_64BIT_LONG
7727 sprintf (buf, "%016lx", value);
7728 #else
7729 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
7730 _bfd_int64_low (value));
7731 #endif
7733 else
7734 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
7736 #else
7737 sprintf_vma (buf, value);
7738 #endif
7741 void
7742 _bfd_elf_fprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, void *stream, bfd_vma value)
7744 #ifdef BFD64
7745 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7747 i_ehdrp = elf_elfheader (abfd);
7748 if (i_ehdrp == NULL)
7749 fprintf_vma ((FILE *) stream, value);
7750 else
7752 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
7754 #if BFD_HOST_64BIT_LONG
7755 fprintf ((FILE *) stream, "%016lx", value);
7756 #else
7757 fprintf ((FILE *) stream, "%08lx%08lx",
7758 _bfd_int64_high (value), _bfd_int64_low (value));
7759 #endif
7761 else
7762 fprintf ((FILE *) stream, "%08lx",
7763 (unsigned long) (value & 0xffffffff));
7765 #else
7766 fprintf_vma ((FILE *) stream, value);
7767 #endif
7770 enum elf_reloc_type_class
7771 _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
7773 return reloc_class_normal;
7776 /* For RELA architectures, return the relocation value for a
7777 relocation against a local symbol. */
7779 bfd_vma
7780 _bfd_elf_rela_local_sym (bfd *abfd,
7781 Elf_Internal_Sym *sym,
7782 asection **psec,
7783 Elf_Internal_Rela *rel)
7785 asection *sec = *psec;
7786 bfd_vma relocation;
7788 relocation = (sec->output_section->vma
7789 + sec->output_offset
7790 + sym->st_value);
7791 if ((sec->flags & SEC_MERGE)
7792 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
7793 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
7795 rel->r_addend =
7796 _bfd_merged_section_offset (abfd, psec,
7797 elf_section_data (sec)->sec_info,
7798 sym->st_value + rel->r_addend);
7799 if (sec != *psec)
7801 /* If we have changed the section, and our original section is
7802 marked with SEC_EXCLUDE, it means that the original
7803 SEC_MERGE section has been completely subsumed in some
7804 other SEC_MERGE section. In this case, we need to leave
7805 some info around for --emit-relocs. */
7806 if ((sec->flags & SEC_EXCLUDE) != 0)
7807 sec->kept_section = *psec;
7808 sec = *psec;
7810 rel->r_addend -= relocation;
7811 rel->r_addend += sec->output_section->vma + sec->output_offset;
7813 return relocation;
7816 bfd_vma
7817 _bfd_elf_rel_local_sym (bfd *abfd,
7818 Elf_Internal_Sym *sym,
7819 asection **psec,
7820 bfd_vma addend)
7822 asection *sec = *psec;
7824 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
7825 return sym->st_value + addend;
7827 return _bfd_merged_section_offset (abfd, psec,
7828 elf_section_data (sec)->sec_info,
7829 sym->st_value + addend);
7832 bfd_vma
7833 _bfd_elf_section_offset (bfd *abfd,
7834 struct bfd_link_info *info,
7835 asection *sec,
7836 bfd_vma offset)
7838 switch (sec->sec_info_type)
7840 case ELF_INFO_TYPE_STABS:
7841 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
7842 offset);
7843 case ELF_INFO_TYPE_EH_FRAME:
7844 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
7845 default:
7846 return offset;
7850 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
7851 reconstruct an ELF file by reading the segments out of remote memory
7852 based on the ELF file header at EHDR_VMA and the ELF program headers it
7853 points to. If not null, *LOADBASEP is filled in with the difference
7854 between the VMAs from which the segments were read, and the VMAs the
7855 file headers (and hence BFD's idea of each section's VMA) put them at.
7857 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
7858 remote memory at target address VMA into the local buffer at MYADDR; it
7859 should return zero on success or an `errno' code on failure. TEMPL must
7860 be a BFD for an ELF target with the word size and byte order found in
7861 the remote memory. */
7863 bfd *
7864 bfd_elf_bfd_from_remote_memory
7865 (bfd *templ,
7866 bfd_vma ehdr_vma,
7867 bfd_vma *loadbasep,
7868 int (*target_read_memory) (bfd_vma, char *, int))
7870 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
7871 (templ, ehdr_vma, loadbasep, target_read_memory);
7874 long
7875 _bfd_elf_get_synthetic_symtab (bfd *abfd,
7876 long symcount ATTRIBUTE_UNUSED,
7877 asymbol **syms ATTRIBUTE_UNUSED,
7878 long dynsymcount,
7879 asymbol **dynsyms,
7880 asymbol **ret)
7882 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7883 asection *relplt;
7884 asymbol *s;
7885 const char *relplt_name;
7886 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
7887 arelent *p;
7888 long count, i, n;
7889 size_t size;
7890 Elf_Internal_Shdr *hdr;
7891 char *names;
7892 asection *plt;
7894 *ret = NULL;
7896 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
7897 return 0;
7899 if (dynsymcount <= 0)
7900 return 0;
7902 if (!bed->plt_sym_val)
7903 return 0;
7905 relplt_name = bed->relplt_name;
7906 if (relplt_name == NULL)
7907 relplt_name = bed->default_use_rela_p ? ".rela.plt" : ".rel.plt";
7908 relplt = bfd_get_section_by_name (abfd, relplt_name);
7909 if (relplt == NULL)
7910 return 0;
7912 hdr = &elf_section_data (relplt)->this_hdr;
7913 if (hdr->sh_link != elf_dynsymtab (abfd)
7914 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
7915 return 0;
7917 plt = bfd_get_section_by_name (abfd, ".plt");
7918 if (plt == NULL)
7919 return 0;
7921 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
7922 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
7923 return -1;
7925 count = relplt->size / hdr->sh_entsize;
7926 size = count * sizeof (asymbol);
7927 p = relplt->relocation;
7928 for (i = 0; i < count; i++, s++, p++)
7929 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
7931 s = *ret = bfd_malloc (size);
7932 if (s == NULL)
7933 return -1;
7935 names = (char *) (s + count);
7936 p = relplt->relocation;
7937 n = 0;
7938 for (i = 0; i < count; i++, s++, p++)
7940 size_t len;
7941 bfd_vma addr;
7943 addr = bed->plt_sym_val (i, plt, p);
7944 if (addr == (bfd_vma) -1)
7945 continue;
7947 *s = **p->sym_ptr_ptr;
7948 s->section = plt;
7949 s->value = addr - plt->vma;
7950 s->name = names;
7951 len = strlen ((*p->sym_ptr_ptr)->name);
7952 memcpy (names, (*p->sym_ptr_ptr)->name, len);
7953 names += len;
7954 memcpy (names, "@plt", sizeof ("@plt"));
7955 names += sizeof ("@plt");
7956 ++n;
7959 return n;
7962 /* Sort symbol by binding and section. We want to put definitions
7963 sorted by section at the beginning. */
7965 static int
7966 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7968 const Elf_Internal_Sym *s1;
7969 const Elf_Internal_Sym *s2;
7970 int shndx;
7972 /* Make sure that undefined symbols are at the end. */
7973 s1 = (const Elf_Internal_Sym *) arg1;
7974 if (s1->st_shndx == SHN_UNDEF)
7975 return 1;
7976 s2 = (const Elf_Internal_Sym *) arg2;
7977 if (s2->st_shndx == SHN_UNDEF)
7978 return -1;
7980 /* Sorted by section index. */
7981 shndx = s1->st_shndx - s2->st_shndx;
7982 if (shndx != 0)
7983 return shndx;
7985 /* Sorted by binding. */
7986 return ELF_ST_BIND (s1->st_info) - ELF_ST_BIND (s2->st_info);
7989 struct elf_symbol
7991 Elf_Internal_Sym *sym;
7992 const char *name;
7995 static int
7996 elf_sym_name_compare (const void *arg1, const void *arg2)
7998 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7999 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8000 return strcmp (s1->name, s2->name);
8003 /* Check if 2 sections define the same set of local and global
8004 symbols. */
8006 bfd_boolean
8007 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2)
8009 bfd *bfd1, *bfd2;
8010 const struct elf_backend_data *bed1, *bed2;
8011 Elf_Internal_Shdr *hdr1, *hdr2;
8012 bfd_size_type symcount1, symcount2;
8013 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8014 Elf_Internal_Sym *isymstart1 = NULL, *isymstart2 = NULL, *isym;
8015 Elf_Internal_Sym *isymend;
8016 struct elf_symbol *symp, *symtable1 = NULL, *symtable2 = NULL;
8017 bfd_size_type count1, count2, i;
8018 int shndx1, shndx2;
8019 bfd_boolean result;
8021 bfd1 = sec1->owner;
8022 bfd2 = sec2->owner;
8024 /* If both are .gnu.linkonce sections, they have to have the same
8025 section name. */
8026 if (strncmp (sec1->name, ".gnu.linkonce",
8027 sizeof ".gnu.linkonce" - 1) == 0
8028 && strncmp (sec2->name, ".gnu.linkonce",
8029 sizeof ".gnu.linkonce" - 1) == 0)
8030 return strcmp (sec1->name + sizeof ".gnu.linkonce",
8031 sec2->name + sizeof ".gnu.linkonce") == 0;
8033 /* Both sections have to be in ELF. */
8034 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8035 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8036 return FALSE;
8038 if (elf_section_type (sec1) != elf_section_type (sec2))
8039 return FALSE;
8041 if ((elf_section_flags (sec1) & SHF_GROUP) != 0
8042 && (elf_section_flags (sec2) & SHF_GROUP) != 0)
8044 /* If both are members of section groups, they have to have the
8045 same group name. */
8046 if (strcmp (elf_group_name (sec1), elf_group_name (sec2)) != 0)
8047 return FALSE;
8050 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8051 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8052 if (shndx1 == -1 || shndx2 == -1)
8053 return FALSE;
8055 bed1 = get_elf_backend_data (bfd1);
8056 bed2 = get_elf_backend_data (bfd2);
8057 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8058 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8059 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8060 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8062 if (symcount1 == 0 || symcount2 == 0)
8063 return FALSE;
8065 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8066 NULL, NULL, NULL);
8067 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8068 NULL, NULL, NULL);
8070 result = FALSE;
8071 if (isymbuf1 == NULL || isymbuf2 == NULL)
8072 goto done;
8074 /* Sort symbols by binding and section. Global definitions are at
8075 the beginning. */
8076 qsort (isymbuf1, symcount1, sizeof (Elf_Internal_Sym),
8077 elf_sort_elf_symbol);
8078 qsort (isymbuf2, symcount2, sizeof (Elf_Internal_Sym),
8079 elf_sort_elf_symbol);
8081 /* Count definitions in the section. */
8082 count1 = 0;
8083 for (isym = isymbuf1, isymend = isym + symcount1;
8084 isym < isymend; isym++)
8086 if (isym->st_shndx == (unsigned int) shndx1)
8088 if (count1 == 0)
8089 isymstart1 = isym;
8090 count1++;
8093 if (count1 && isym->st_shndx != (unsigned int) shndx1)
8094 break;
8097 count2 = 0;
8098 for (isym = isymbuf2, isymend = isym + symcount2;
8099 isym < isymend; isym++)
8101 if (isym->st_shndx == (unsigned int) shndx2)
8103 if (count2 == 0)
8104 isymstart2 = isym;
8105 count2++;
8108 if (count2 && isym->st_shndx != (unsigned int) shndx2)
8109 break;
8112 if (count1 == 0 || count2 == 0 || count1 != count2)
8113 goto done;
8115 symtable1 = bfd_malloc (count1 * sizeof (struct elf_symbol));
8116 symtable2 = bfd_malloc (count1 * sizeof (struct elf_symbol));
8118 if (symtable1 == NULL || symtable2 == NULL)
8119 goto done;
8121 symp = symtable1;
8122 for (isym = isymstart1, isymend = isym + count1;
8123 isym < isymend; isym++)
8125 symp->sym = isym;
8126 symp->name = bfd_elf_string_from_elf_section (bfd1,
8127 hdr1->sh_link,
8128 isym->st_name);
8129 symp++;
8132 symp = symtable2;
8133 for (isym = isymstart2, isymend = isym + count1;
8134 isym < isymend; isym++)
8136 symp->sym = isym;
8137 symp->name = bfd_elf_string_from_elf_section (bfd2,
8138 hdr2->sh_link,
8139 isym->st_name);
8140 symp++;
8143 /* Sort symbol by name. */
8144 qsort (symtable1, count1, sizeof (struct elf_symbol),
8145 elf_sym_name_compare);
8146 qsort (symtable2, count1, sizeof (struct elf_symbol),
8147 elf_sym_name_compare);
8149 for (i = 0; i < count1; i++)
8150 /* Two symbols must have the same binding, type and name. */
8151 if (symtable1 [i].sym->st_info != symtable2 [i].sym->st_info
8152 || symtable1 [i].sym->st_other != symtable2 [i].sym->st_other
8153 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8154 goto done;
8156 result = TRUE;
8158 done:
8159 if (symtable1)
8160 free (symtable1);
8161 if (symtable2)
8162 free (symtable2);
8163 if (isymbuf1)
8164 free (isymbuf1);
8165 if (isymbuf2)
8166 free (isymbuf2);
8168 return result;