2007-06-14 Paolo Bonzini <bonzini@gnu.org>
[binutils.git] / bfd / elf.c
blob532e6b574926dbef9427c6f06fce0823dfbfad84
1 /* ELF executable support for BFD.
3 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 2002, 2003, 2004, 2005, 2006, 2007 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
23 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 "sysdep.h"
37 #include "bfd.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 /* DT_GNU_HASH hash function. Do not change this function; you will
210 cause invalid hash tables to be generated. */
212 unsigned long
213 bfd_elf_gnu_hash (const char *namearg)
215 const unsigned char *name = (const unsigned char *) namearg;
216 unsigned long h = 5381;
217 unsigned char ch;
219 while ((ch = *name++) != '\0')
220 h = (h << 5) + h + ch;
221 return h & 0xffffffff;
224 bfd_boolean
225 bfd_elf_mkobject (bfd *abfd)
227 if (abfd->tdata.any == NULL)
229 abfd->tdata.any = bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
230 if (abfd->tdata.any == NULL)
231 return FALSE;
234 elf_tdata (abfd)->program_header_size = (bfd_size_type) -1;
236 return TRUE;
239 bfd_boolean
240 bfd_elf_mkcorefile (bfd *abfd)
242 /* I think this can be done just like an object file. */
243 return bfd_elf_mkobject (abfd);
246 char *
247 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
249 Elf_Internal_Shdr **i_shdrp;
250 bfd_byte *shstrtab = NULL;
251 file_ptr offset;
252 bfd_size_type shstrtabsize;
254 i_shdrp = elf_elfsections (abfd);
255 if (i_shdrp == 0
256 || shindex >= elf_numsections (abfd)
257 || i_shdrp[shindex] == 0)
258 return NULL;
260 shstrtab = i_shdrp[shindex]->contents;
261 if (shstrtab == NULL)
263 /* No cached one, attempt to read, and cache what we read. */
264 offset = i_shdrp[shindex]->sh_offset;
265 shstrtabsize = i_shdrp[shindex]->sh_size;
267 /* Allocate and clear an extra byte at the end, to prevent crashes
268 in case the string table is not terminated. */
269 if (shstrtabsize + 1 == 0
270 || (shstrtab = bfd_alloc (abfd, shstrtabsize + 1)) == NULL
271 || bfd_seek (abfd, offset, SEEK_SET) != 0)
272 shstrtab = NULL;
273 else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
275 if (bfd_get_error () != bfd_error_system_call)
276 bfd_set_error (bfd_error_file_truncated);
277 shstrtab = NULL;
279 else
280 shstrtab[shstrtabsize] = '\0';
281 i_shdrp[shindex]->contents = shstrtab;
283 return (char *) shstrtab;
286 char *
287 bfd_elf_string_from_elf_section (bfd *abfd,
288 unsigned int shindex,
289 unsigned int strindex)
291 Elf_Internal_Shdr *hdr;
293 if (strindex == 0)
294 return "";
296 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
297 return NULL;
299 hdr = elf_elfsections (abfd)[shindex];
301 if (hdr->contents == NULL
302 && bfd_elf_get_str_section (abfd, shindex) == NULL)
303 return NULL;
305 if (strindex >= hdr->sh_size)
307 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
308 (*_bfd_error_handler)
309 (_("%B: invalid string offset %u >= %lu for section `%s'"),
310 abfd, strindex, (unsigned long) hdr->sh_size,
311 (shindex == shstrndx && strindex == hdr->sh_name
312 ? ".shstrtab"
313 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
314 return "";
317 return ((char *) hdr->contents) + strindex;
320 /* Read and convert symbols to internal format.
321 SYMCOUNT specifies the number of symbols to read, starting from
322 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
323 are non-NULL, they are used to store the internal symbols, external
324 symbols, and symbol section index extensions, respectively. */
326 Elf_Internal_Sym *
327 bfd_elf_get_elf_syms (bfd *ibfd,
328 Elf_Internal_Shdr *symtab_hdr,
329 size_t symcount,
330 size_t symoffset,
331 Elf_Internal_Sym *intsym_buf,
332 void *extsym_buf,
333 Elf_External_Sym_Shndx *extshndx_buf)
335 Elf_Internal_Shdr *shndx_hdr;
336 void *alloc_ext;
337 const bfd_byte *esym;
338 Elf_External_Sym_Shndx *alloc_extshndx;
339 Elf_External_Sym_Shndx *shndx;
340 Elf_Internal_Sym *isym;
341 Elf_Internal_Sym *isymend;
342 const struct elf_backend_data *bed;
343 size_t extsym_size;
344 bfd_size_type amt;
345 file_ptr pos;
347 if (symcount == 0)
348 return intsym_buf;
350 /* Normal syms might have section extension entries. */
351 shndx_hdr = NULL;
352 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
353 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
355 /* Read the symbols. */
356 alloc_ext = NULL;
357 alloc_extshndx = NULL;
358 bed = get_elf_backend_data (ibfd);
359 extsym_size = bed->s->sizeof_sym;
360 amt = symcount * extsym_size;
361 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
362 if (extsym_buf == NULL)
364 alloc_ext = bfd_malloc2 (symcount, extsym_size);
365 extsym_buf = alloc_ext;
367 if (extsym_buf == NULL
368 || bfd_seek (ibfd, pos, SEEK_SET) != 0
369 || bfd_bread (extsym_buf, amt, ibfd) != amt)
371 intsym_buf = NULL;
372 goto out;
375 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
376 extshndx_buf = NULL;
377 else
379 amt = symcount * sizeof (Elf_External_Sym_Shndx);
380 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
381 if (extshndx_buf == NULL)
383 alloc_extshndx = bfd_malloc2 (symcount,
384 sizeof (Elf_External_Sym_Shndx));
385 extshndx_buf = alloc_extshndx;
387 if (extshndx_buf == NULL
388 || bfd_seek (ibfd, pos, SEEK_SET) != 0
389 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
391 intsym_buf = NULL;
392 goto out;
396 if (intsym_buf == NULL)
398 intsym_buf = bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
399 if (intsym_buf == NULL)
400 goto out;
403 /* Convert the symbols to internal form. */
404 isymend = intsym_buf + symcount;
405 for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf;
406 isym < isymend;
407 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
408 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
410 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
411 (*_bfd_error_handler) (_("%B symbol number %lu references "
412 "nonexistent SHT_SYMTAB_SHNDX section"),
413 ibfd, (unsigned long) symoffset);
414 intsym_buf = NULL;
415 goto out;
418 out:
419 if (alloc_ext != NULL)
420 free (alloc_ext);
421 if (alloc_extshndx != NULL)
422 free (alloc_extshndx);
424 return intsym_buf;
427 /* Look up a symbol name. */
428 const char *
429 bfd_elf_sym_name (bfd *abfd,
430 Elf_Internal_Shdr *symtab_hdr,
431 Elf_Internal_Sym *isym,
432 asection *sym_sec)
434 const char *name;
435 unsigned int iname = isym->st_name;
436 unsigned int shindex = symtab_hdr->sh_link;
438 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
439 /* Check for a bogus st_shndx to avoid crashing. */
440 && isym->st_shndx < elf_numsections (abfd)
441 && !(isym->st_shndx >= SHN_LORESERVE && isym->st_shndx <= SHN_HIRESERVE))
443 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
444 shindex = elf_elfheader (abfd)->e_shstrndx;
447 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
448 if (name == NULL)
449 name = "(null)";
450 else if (sym_sec && *name == '\0')
451 name = bfd_section_name (abfd, sym_sec);
453 return name;
456 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
457 sections. The first element is the flags, the rest are section
458 pointers. */
460 typedef union elf_internal_group {
461 Elf_Internal_Shdr *shdr;
462 unsigned int flags;
463 } Elf_Internal_Group;
465 /* Return the name of the group signature symbol. Why isn't the
466 signature just a string? */
468 static const char *
469 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
471 Elf_Internal_Shdr *hdr;
472 unsigned char esym[sizeof (Elf64_External_Sym)];
473 Elf_External_Sym_Shndx eshndx;
474 Elf_Internal_Sym isym;
476 /* First we need to ensure the symbol table is available. Make sure
477 that it is a symbol table section. */
478 hdr = elf_elfsections (abfd) [ghdr->sh_link];
479 if (hdr->sh_type != SHT_SYMTAB
480 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
481 return NULL;
483 /* Go read the symbol. */
484 hdr = &elf_tdata (abfd)->symtab_hdr;
485 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
486 &isym, esym, &eshndx) == NULL)
487 return NULL;
489 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
492 /* Set next_in_group list pointer, and group name for NEWSECT. */
494 static bfd_boolean
495 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
497 unsigned int num_group = elf_tdata (abfd)->num_group;
499 /* If num_group is zero, read in all SHT_GROUP sections. The count
500 is set to -1 if there are no SHT_GROUP sections. */
501 if (num_group == 0)
503 unsigned int i, shnum;
505 /* First count the number of groups. If we have a SHT_GROUP
506 section with just a flag word (ie. sh_size is 4), ignore it. */
507 shnum = elf_numsections (abfd);
508 num_group = 0;
510 #define IS_VALID_GROUP_SECTION_HEADER(shdr) \
511 ( (shdr)->sh_type == SHT_GROUP \
512 && (shdr)->sh_size >= (2 * GRP_ENTRY_SIZE) \
513 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
514 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
516 for (i = 0; i < shnum; i++)
518 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
520 if (IS_VALID_GROUP_SECTION_HEADER (shdr))
521 num_group += 1;
524 if (num_group == 0)
526 num_group = (unsigned) -1;
527 elf_tdata (abfd)->num_group = num_group;
529 else
531 /* We keep a list of elf section headers for group sections,
532 so we can find them quickly. */
533 bfd_size_type amt;
535 elf_tdata (abfd)->num_group = num_group;
536 elf_tdata (abfd)->group_sect_ptr
537 = bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
538 if (elf_tdata (abfd)->group_sect_ptr == NULL)
539 return FALSE;
541 num_group = 0;
542 for (i = 0; i < shnum; i++)
544 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
546 if (IS_VALID_GROUP_SECTION_HEADER (shdr))
548 unsigned char *src;
549 Elf_Internal_Group *dest;
551 /* Add to list of sections. */
552 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
553 num_group += 1;
555 /* Read the raw contents. */
556 BFD_ASSERT (sizeof (*dest) >= 4);
557 amt = shdr->sh_size * sizeof (*dest) / 4;
558 shdr->contents = bfd_alloc2 (abfd, shdr->sh_size,
559 sizeof (*dest) / 4);
560 /* PR binutils/4110: Handle corrupt group headers. */
561 if (shdr->contents == NULL)
563 _bfd_error_handler
564 (_("%B: Corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size);
565 bfd_set_error (bfd_error_bad_value);
566 return FALSE;
569 memset (shdr->contents, 0, amt);
571 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
572 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
573 != shdr->sh_size))
574 return FALSE;
576 /* Translate raw contents, a flag word followed by an
577 array of elf section indices all in target byte order,
578 to the flag word followed by an array of elf section
579 pointers. */
580 src = shdr->contents + shdr->sh_size;
581 dest = (Elf_Internal_Group *) (shdr->contents + amt);
582 while (1)
584 unsigned int idx;
586 src -= 4;
587 --dest;
588 idx = H_GET_32 (abfd, src);
589 if (src == shdr->contents)
591 dest->flags = idx;
592 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
593 shdr->bfd_section->flags
594 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
595 break;
597 if (idx >= shnum)
599 ((*_bfd_error_handler)
600 (_("%B: invalid SHT_GROUP entry"), abfd));
601 idx = 0;
603 dest->shdr = elf_elfsections (abfd)[idx];
610 if (num_group != (unsigned) -1)
612 unsigned int i;
614 for (i = 0; i < num_group; i++)
616 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
617 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
618 unsigned int n_elt = shdr->sh_size / 4;
620 /* Look through this group's sections to see if current
621 section is a member. */
622 while (--n_elt != 0)
623 if ((++idx)->shdr == hdr)
625 asection *s = NULL;
627 /* We are a member of this group. Go looking through
628 other members to see if any others are linked via
629 next_in_group. */
630 idx = (Elf_Internal_Group *) shdr->contents;
631 n_elt = shdr->sh_size / 4;
632 while (--n_elt != 0)
633 if ((s = (++idx)->shdr->bfd_section) != NULL
634 && elf_next_in_group (s) != NULL)
635 break;
636 if (n_elt != 0)
638 /* Snarf the group name from other member, and
639 insert current section in circular list. */
640 elf_group_name (newsect) = elf_group_name (s);
641 elf_next_in_group (newsect) = elf_next_in_group (s);
642 elf_next_in_group (s) = newsect;
644 else
646 const char *gname;
648 gname = group_signature (abfd, shdr);
649 if (gname == NULL)
650 return FALSE;
651 elf_group_name (newsect) = gname;
653 /* Start a circular list with one element. */
654 elf_next_in_group (newsect) = newsect;
657 /* If the group section has been created, point to the
658 new member. */
659 if (shdr->bfd_section != NULL)
660 elf_next_in_group (shdr->bfd_section) = newsect;
662 i = num_group - 1;
663 break;
668 if (elf_group_name (newsect) == NULL)
670 (*_bfd_error_handler) (_("%B: no group info for section %A"),
671 abfd, newsect);
673 return TRUE;
676 bfd_boolean
677 _bfd_elf_setup_sections (bfd *abfd)
679 unsigned int i;
680 unsigned int num_group = elf_tdata (abfd)->num_group;
681 bfd_boolean result = TRUE;
682 asection *s;
684 /* Process SHF_LINK_ORDER. */
685 for (s = abfd->sections; s != NULL; s = s->next)
687 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
688 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
690 unsigned int elfsec = this_hdr->sh_link;
691 /* FIXME: The old Intel compiler and old strip/objcopy may
692 not set the sh_link or sh_info fields. Hence we could
693 get the situation where elfsec is 0. */
694 if (elfsec == 0)
696 const struct elf_backend_data *bed
697 = get_elf_backend_data (abfd);
698 if (bed->link_order_error_handler)
699 bed->link_order_error_handler
700 (_("%B: warning: sh_link not set for section `%A'"),
701 abfd, s);
703 else
705 asection *link;
707 this_hdr = elf_elfsections (abfd)[elfsec];
709 /* PR 1991, 2008:
710 Some strip/objcopy may leave an incorrect value in
711 sh_link. We don't want to proceed. */
712 link = this_hdr->bfd_section;
713 if (link == NULL)
715 (*_bfd_error_handler)
716 (_("%B: sh_link [%d] in section `%A' is incorrect"),
717 s->owner, s, elfsec);
718 result = FALSE;
721 elf_linked_to_section (s) = link;
726 /* Process section groups. */
727 if (num_group == (unsigned) -1)
728 return result;
730 for (i = 0; i < num_group; i++)
732 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
733 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
734 unsigned int n_elt = shdr->sh_size / 4;
736 while (--n_elt != 0)
737 if ((++idx)->shdr->bfd_section)
738 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
739 else if (idx->shdr->sh_type == SHT_RELA
740 || idx->shdr->sh_type == SHT_REL)
741 /* We won't include relocation sections in section groups in
742 output object files. We adjust the group section size here
743 so that relocatable link will work correctly when
744 relocation sections are in section group in input object
745 files. */
746 shdr->bfd_section->size -= 4;
747 else
749 /* There are some unknown sections in the group. */
750 (*_bfd_error_handler)
751 (_("%B: unknown [%d] section `%s' in group [%s]"),
752 abfd,
753 (unsigned int) idx->shdr->sh_type,
754 bfd_elf_string_from_elf_section (abfd,
755 (elf_elfheader (abfd)
756 ->e_shstrndx),
757 idx->shdr->sh_name),
758 shdr->bfd_section->name);
759 result = FALSE;
762 return result;
765 bfd_boolean
766 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
768 return elf_next_in_group (sec) != NULL;
771 /* Make a BFD section from an ELF section. We store a pointer to the
772 BFD section in the bfd_section field of the header. */
774 bfd_boolean
775 _bfd_elf_make_section_from_shdr (bfd *abfd,
776 Elf_Internal_Shdr *hdr,
777 const char *name,
778 int shindex)
780 asection *newsect;
781 flagword flags;
782 const struct elf_backend_data *bed;
784 if (hdr->bfd_section != NULL)
786 BFD_ASSERT (strcmp (name,
787 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
788 return TRUE;
791 newsect = bfd_make_section_anyway (abfd, name);
792 if (newsect == NULL)
793 return FALSE;
795 hdr->bfd_section = newsect;
796 elf_section_data (newsect)->this_hdr = *hdr;
797 elf_section_data (newsect)->this_idx = shindex;
799 /* Always use the real type/flags. */
800 elf_section_type (newsect) = hdr->sh_type;
801 elf_section_flags (newsect) = hdr->sh_flags;
803 newsect->filepos = hdr->sh_offset;
805 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
806 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
807 || ! bfd_set_section_alignment (abfd, newsect,
808 bfd_log2 ((bfd_vma) hdr->sh_addralign)))
809 return FALSE;
811 flags = SEC_NO_FLAGS;
812 if (hdr->sh_type != SHT_NOBITS)
813 flags |= SEC_HAS_CONTENTS;
814 if (hdr->sh_type == SHT_GROUP)
815 flags |= SEC_GROUP | SEC_EXCLUDE;
816 if ((hdr->sh_flags & SHF_ALLOC) != 0)
818 flags |= SEC_ALLOC;
819 if (hdr->sh_type != SHT_NOBITS)
820 flags |= SEC_LOAD;
822 if ((hdr->sh_flags & SHF_WRITE) == 0)
823 flags |= SEC_READONLY;
824 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
825 flags |= SEC_CODE;
826 else if ((flags & SEC_LOAD) != 0)
827 flags |= SEC_DATA;
828 if ((hdr->sh_flags & SHF_MERGE) != 0)
830 flags |= SEC_MERGE;
831 newsect->entsize = hdr->sh_entsize;
832 if ((hdr->sh_flags & SHF_STRINGS) != 0)
833 flags |= SEC_STRINGS;
835 if (hdr->sh_flags & SHF_GROUP)
836 if (!setup_group (abfd, hdr, newsect))
837 return FALSE;
838 if ((hdr->sh_flags & SHF_TLS) != 0)
839 flags |= SEC_THREAD_LOCAL;
841 if ((flags & SEC_ALLOC) == 0)
843 /* The debugging sections appear to be recognized only by name,
844 not any sort of flag. Their SEC_ALLOC bits are cleared. */
845 static const struct
847 const char *name;
848 int len;
849 } debug_sections [] =
851 { STRING_COMMA_LEN ("debug") }, /* 'd' */
852 { NULL, 0 }, /* 'e' */
853 { NULL, 0 }, /* 'f' */
854 { STRING_COMMA_LEN ("gnu.linkonce.wi.") }, /* 'g' */
855 { NULL, 0 }, /* 'h' */
856 { NULL, 0 }, /* 'i' */
857 { NULL, 0 }, /* 'j' */
858 { NULL, 0 }, /* 'k' */
859 { STRING_COMMA_LEN ("line") }, /* 'l' */
860 { NULL, 0 }, /* 'm' */
861 { NULL, 0 }, /* 'n' */
862 { NULL, 0 }, /* 'o' */
863 { NULL, 0 }, /* 'p' */
864 { NULL, 0 }, /* 'q' */
865 { NULL, 0 }, /* 'r' */
866 { STRING_COMMA_LEN ("stab") } /* 's' */
869 if (name [0] == '.')
871 int i = name [1] - 'd';
872 if (i >= 0
873 && i < (int) ARRAY_SIZE (debug_sections)
874 && debug_sections [i].name != NULL
875 && strncmp (&name [1], debug_sections [i].name,
876 debug_sections [i].len) == 0)
877 flags |= SEC_DEBUGGING;
881 /* As a GNU extension, if the name begins with .gnu.linkonce, we
882 only link a single copy of the section. This is used to support
883 g++. g++ will emit each template expansion in its own section.
884 The symbols will be defined as weak, so that multiple definitions
885 are permitted. The GNU linker extension is to actually discard
886 all but one of the sections. */
887 if (CONST_STRNEQ (name, ".gnu.linkonce")
888 && elf_next_in_group (newsect) == NULL)
889 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
891 bed = get_elf_backend_data (abfd);
892 if (bed->elf_backend_section_flags)
893 if (! bed->elf_backend_section_flags (&flags, hdr))
894 return FALSE;
896 if (! bfd_set_section_flags (abfd, newsect, flags))
897 return FALSE;
899 if ((flags & SEC_ALLOC) != 0)
901 Elf_Internal_Phdr *phdr;
902 unsigned int i;
904 /* Look through the phdrs to see if we need to adjust the lma.
905 If all the p_paddr fields are zero, we ignore them, since
906 some ELF linkers produce such output. */
907 phdr = elf_tdata (abfd)->phdr;
908 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
910 if (phdr->p_paddr != 0)
911 break;
913 if (i < elf_elfheader (abfd)->e_phnum)
915 phdr = elf_tdata (abfd)->phdr;
916 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
918 /* This section is part of this segment if its file
919 offset plus size lies within the segment's memory
920 span and, if the section is loaded, the extent of the
921 loaded data lies within the extent of the segment.
923 Note - we used to check the p_paddr field as well, and
924 refuse to set the LMA if it was 0. This is wrong
925 though, as a perfectly valid initialised segment can
926 have a p_paddr of zero. Some architectures, eg ARM,
927 place special significance on the address 0 and
928 executables need to be able to have a segment which
929 covers this address. */
930 if (phdr->p_type == PT_LOAD
931 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
932 && (hdr->sh_offset + hdr->sh_size
933 <= phdr->p_offset + phdr->p_memsz)
934 && ((flags & SEC_LOAD) == 0
935 || (hdr->sh_offset + hdr->sh_size
936 <= phdr->p_offset + phdr->p_filesz)))
938 if ((flags & SEC_LOAD) == 0)
939 newsect->lma = (phdr->p_paddr
940 + hdr->sh_addr - phdr->p_vaddr);
941 else
942 /* We used to use the same adjustment for SEC_LOAD
943 sections, but that doesn't work if the segment
944 is packed with code from multiple VMAs.
945 Instead we calculate the section LMA based on
946 the segment LMA. It is assumed that the
947 segment will contain sections with contiguous
948 LMAs, even if the VMAs are not. */
949 newsect->lma = (phdr->p_paddr
950 + hdr->sh_offset - phdr->p_offset);
952 /* With contiguous segments, we can't tell from file
953 offsets whether a section with zero size should
954 be placed at the end of one segment or the
955 beginning of the next. Decide based on vaddr. */
956 if (hdr->sh_addr >= phdr->p_vaddr
957 && (hdr->sh_addr + hdr->sh_size
958 <= phdr->p_vaddr + phdr->p_memsz))
959 break;
965 return TRUE;
969 INTERNAL_FUNCTION
970 bfd_elf_find_section
972 SYNOPSIS
973 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
975 DESCRIPTION
976 Helper functions for GDB to locate the string tables.
977 Since BFD hides string tables from callers, GDB needs to use an
978 internal hook to find them. Sun's .stabstr, in particular,
979 isn't even pointed to by the .stab section, so ordinary
980 mechanisms wouldn't work to find it, even if we had some.
983 struct elf_internal_shdr *
984 bfd_elf_find_section (bfd *abfd, char *name)
986 Elf_Internal_Shdr **i_shdrp;
987 char *shstrtab;
988 unsigned int max;
989 unsigned int i;
991 i_shdrp = elf_elfsections (abfd);
992 if (i_shdrp != NULL)
994 shstrtab = bfd_elf_get_str_section (abfd,
995 elf_elfheader (abfd)->e_shstrndx);
996 if (shstrtab != NULL)
998 max = elf_numsections (abfd);
999 for (i = 1; i < max; i++)
1000 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
1001 return i_shdrp[i];
1004 return 0;
1007 const char *const bfd_elf_section_type_names[] = {
1008 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1009 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1010 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1013 /* ELF relocs are against symbols. If we are producing relocatable
1014 output, and the reloc is against an external symbol, and nothing
1015 has given us any additional addend, the resulting reloc will also
1016 be against the same symbol. In such a case, we don't want to
1017 change anything about the way the reloc is handled, since it will
1018 all be done at final link time. Rather than put special case code
1019 into bfd_perform_relocation, all the reloc types use this howto
1020 function. It just short circuits the reloc if producing
1021 relocatable output against an external symbol. */
1023 bfd_reloc_status_type
1024 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1025 arelent *reloc_entry,
1026 asymbol *symbol,
1027 void *data ATTRIBUTE_UNUSED,
1028 asection *input_section,
1029 bfd *output_bfd,
1030 char **error_message ATTRIBUTE_UNUSED)
1032 if (output_bfd != NULL
1033 && (symbol->flags & BSF_SECTION_SYM) == 0
1034 && (! reloc_entry->howto->partial_inplace
1035 || reloc_entry->addend == 0))
1037 reloc_entry->address += input_section->output_offset;
1038 return bfd_reloc_ok;
1041 return bfd_reloc_continue;
1044 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
1046 static void
1047 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
1048 asection *sec)
1050 BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE);
1051 sec->sec_info_type = ELF_INFO_TYPE_NONE;
1054 /* Finish SHF_MERGE section merging. */
1056 bfd_boolean
1057 _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
1059 bfd *ibfd;
1060 asection *sec;
1062 if (!is_elf_hash_table (info->hash))
1063 return FALSE;
1065 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1066 if ((ibfd->flags & DYNAMIC) == 0)
1067 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
1068 if ((sec->flags & SEC_MERGE) != 0
1069 && !bfd_is_abs_section (sec->output_section))
1071 struct bfd_elf_section_data *secdata;
1073 secdata = elf_section_data (sec);
1074 if (! _bfd_add_merge_section (abfd,
1075 &elf_hash_table (info)->merge_info,
1076 sec, &secdata->sec_info))
1077 return FALSE;
1078 else if (secdata->sec_info)
1079 sec->sec_info_type = ELF_INFO_TYPE_MERGE;
1082 if (elf_hash_table (info)->merge_info != NULL)
1083 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
1084 merge_sections_remove_hook);
1085 return TRUE;
1088 void
1089 _bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
1091 sec->output_section = bfd_abs_section_ptr;
1092 sec->output_offset = sec->vma;
1093 if (!is_elf_hash_table (info->hash))
1094 return;
1096 sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
1099 /* Copy the program header and other data from one object module to
1100 another. */
1102 bfd_boolean
1103 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1105 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1106 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1107 return TRUE;
1109 BFD_ASSERT (!elf_flags_init (obfd)
1110 || (elf_elfheader (obfd)->e_flags
1111 == elf_elfheader (ibfd)->e_flags));
1113 elf_gp (obfd) = elf_gp (ibfd);
1114 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1115 elf_flags_init (obfd) = TRUE;
1116 return TRUE;
1119 static const char *
1120 get_segment_type (unsigned int p_type)
1122 const char *pt;
1123 switch (p_type)
1125 case PT_NULL: pt = "NULL"; break;
1126 case PT_LOAD: pt = "LOAD"; break;
1127 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1128 case PT_INTERP: pt = "INTERP"; break;
1129 case PT_NOTE: pt = "NOTE"; break;
1130 case PT_SHLIB: pt = "SHLIB"; break;
1131 case PT_PHDR: pt = "PHDR"; break;
1132 case PT_TLS: pt = "TLS"; break;
1133 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1134 case PT_GNU_STACK: pt = "STACK"; break;
1135 case PT_GNU_RELRO: pt = "RELRO"; break;
1136 default: pt = NULL; break;
1138 return pt;
1141 /* Print out the program headers. */
1143 bfd_boolean
1144 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1146 FILE *f = farg;
1147 Elf_Internal_Phdr *p;
1148 asection *s;
1149 bfd_byte *dynbuf = NULL;
1151 p = elf_tdata (abfd)->phdr;
1152 if (p != NULL)
1154 unsigned int i, c;
1156 fprintf (f, _("\nProgram Header:\n"));
1157 c = elf_elfheader (abfd)->e_phnum;
1158 for (i = 0; i < c; i++, p++)
1160 const char *pt = get_segment_type (p->p_type);
1161 char buf[20];
1163 if (pt == NULL)
1165 sprintf (buf, "0x%lx", p->p_type);
1166 pt = buf;
1168 fprintf (f, "%8s off 0x", pt);
1169 bfd_fprintf_vma (abfd, f, p->p_offset);
1170 fprintf (f, " vaddr 0x");
1171 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1172 fprintf (f, " paddr 0x");
1173 bfd_fprintf_vma (abfd, f, p->p_paddr);
1174 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1175 fprintf (f, " filesz 0x");
1176 bfd_fprintf_vma (abfd, f, p->p_filesz);
1177 fprintf (f, " memsz 0x");
1178 bfd_fprintf_vma (abfd, f, p->p_memsz);
1179 fprintf (f, " flags %c%c%c",
1180 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1181 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1182 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1183 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1184 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1185 fprintf (f, "\n");
1189 s = bfd_get_section_by_name (abfd, ".dynamic");
1190 if (s != NULL)
1192 int elfsec;
1193 unsigned long shlink;
1194 bfd_byte *extdyn, *extdynend;
1195 size_t extdynsize;
1196 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1198 fprintf (f, _("\nDynamic Section:\n"));
1200 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1201 goto error_return;
1203 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1204 if (elfsec == -1)
1205 goto error_return;
1206 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1208 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1209 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1211 extdyn = dynbuf;
1212 extdynend = extdyn + s->size;
1213 for (; extdyn < extdynend; extdyn += extdynsize)
1215 Elf_Internal_Dyn dyn;
1216 const char *name;
1217 char ab[20];
1218 bfd_boolean stringp;
1220 (*swap_dyn_in) (abfd, extdyn, &dyn);
1222 if (dyn.d_tag == DT_NULL)
1223 break;
1225 stringp = FALSE;
1226 switch (dyn.d_tag)
1228 default:
1229 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1230 name = ab;
1231 break;
1233 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1234 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1235 case DT_PLTGOT: name = "PLTGOT"; break;
1236 case DT_HASH: name = "HASH"; break;
1237 case DT_STRTAB: name = "STRTAB"; break;
1238 case DT_SYMTAB: name = "SYMTAB"; break;
1239 case DT_RELA: name = "RELA"; break;
1240 case DT_RELASZ: name = "RELASZ"; break;
1241 case DT_RELAENT: name = "RELAENT"; break;
1242 case DT_STRSZ: name = "STRSZ"; break;
1243 case DT_SYMENT: name = "SYMENT"; break;
1244 case DT_INIT: name = "INIT"; break;
1245 case DT_FINI: name = "FINI"; break;
1246 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1247 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1248 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1249 case DT_REL: name = "REL"; break;
1250 case DT_RELSZ: name = "RELSZ"; break;
1251 case DT_RELENT: name = "RELENT"; break;
1252 case DT_PLTREL: name = "PLTREL"; break;
1253 case DT_DEBUG: name = "DEBUG"; break;
1254 case DT_TEXTREL: name = "TEXTREL"; break;
1255 case DT_JMPREL: name = "JMPREL"; break;
1256 case DT_BIND_NOW: name = "BIND_NOW"; break;
1257 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1258 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1259 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1260 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1261 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1262 case DT_FLAGS: name = "FLAGS"; break;
1263 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1264 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1265 case DT_CHECKSUM: name = "CHECKSUM"; break;
1266 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1267 case DT_MOVEENT: name = "MOVEENT"; break;
1268 case DT_MOVESZ: name = "MOVESZ"; break;
1269 case DT_FEATURE: name = "FEATURE"; break;
1270 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1271 case DT_SYMINSZ: name = "SYMINSZ"; break;
1272 case DT_SYMINENT: name = "SYMINENT"; break;
1273 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1274 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1275 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1276 case DT_PLTPAD: name = "PLTPAD"; break;
1277 case DT_MOVETAB: name = "MOVETAB"; break;
1278 case DT_SYMINFO: name = "SYMINFO"; break;
1279 case DT_RELACOUNT: name = "RELACOUNT"; break;
1280 case DT_RELCOUNT: name = "RELCOUNT"; break;
1281 case DT_FLAGS_1: name = "FLAGS_1"; break;
1282 case DT_VERSYM: name = "VERSYM"; break;
1283 case DT_VERDEF: name = "VERDEF"; break;
1284 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1285 case DT_VERNEED: name = "VERNEED"; break;
1286 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1287 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1288 case DT_USED: name = "USED"; break;
1289 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1290 case DT_GNU_HASH: name = "GNU_HASH"; break;
1293 fprintf (f, " %-11s ", name);
1294 if (! stringp)
1295 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
1296 else
1298 const char *string;
1299 unsigned int tagv = dyn.d_un.d_val;
1301 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1302 if (string == NULL)
1303 goto error_return;
1304 fprintf (f, "%s", string);
1306 fprintf (f, "\n");
1309 free (dynbuf);
1310 dynbuf = NULL;
1313 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1314 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1316 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1317 return FALSE;
1320 if (elf_dynverdef (abfd) != 0)
1322 Elf_Internal_Verdef *t;
1324 fprintf (f, _("\nVersion definitions:\n"));
1325 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1327 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1328 t->vd_flags, t->vd_hash,
1329 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1330 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1332 Elf_Internal_Verdaux *a;
1334 fprintf (f, "\t");
1335 for (a = t->vd_auxptr->vda_nextptr;
1336 a != NULL;
1337 a = a->vda_nextptr)
1338 fprintf (f, "%s ",
1339 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1340 fprintf (f, "\n");
1345 if (elf_dynverref (abfd) != 0)
1347 Elf_Internal_Verneed *t;
1349 fprintf (f, _("\nVersion References:\n"));
1350 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1352 Elf_Internal_Vernaux *a;
1354 fprintf (f, _(" required from %s:\n"),
1355 t->vn_filename ? t->vn_filename : "<corrupt>");
1356 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1357 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1358 a->vna_flags, a->vna_other,
1359 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1363 return TRUE;
1365 error_return:
1366 if (dynbuf != NULL)
1367 free (dynbuf);
1368 return FALSE;
1371 /* Display ELF-specific fields of a symbol. */
1373 void
1374 bfd_elf_print_symbol (bfd *abfd,
1375 void *filep,
1376 asymbol *symbol,
1377 bfd_print_symbol_type how)
1379 FILE *file = filep;
1380 switch (how)
1382 case bfd_print_symbol_name:
1383 fprintf (file, "%s", symbol->name);
1384 break;
1385 case bfd_print_symbol_more:
1386 fprintf (file, "elf ");
1387 bfd_fprintf_vma (abfd, file, symbol->value);
1388 fprintf (file, " %lx", (long) symbol->flags);
1389 break;
1390 case bfd_print_symbol_all:
1392 const char *section_name;
1393 const char *name = NULL;
1394 const struct elf_backend_data *bed;
1395 unsigned char st_other;
1396 bfd_vma val;
1398 section_name = symbol->section ? symbol->section->name : "(*none*)";
1400 bed = get_elf_backend_data (abfd);
1401 if (bed->elf_backend_print_symbol_all)
1402 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1404 if (name == NULL)
1406 name = symbol->name;
1407 bfd_print_symbol_vandf (abfd, file, symbol);
1410 fprintf (file, " %s\t", section_name);
1411 /* Print the "other" value for a symbol. For common symbols,
1412 we've already printed the size; now print the alignment.
1413 For other symbols, we have no specified alignment, and
1414 we've printed the address; now print the size. */
1415 if (bfd_is_com_section (symbol->section))
1416 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1417 else
1418 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1419 bfd_fprintf_vma (abfd, file, val);
1421 /* If we have version information, print it. */
1422 if (elf_tdata (abfd)->dynversym_section != 0
1423 && (elf_tdata (abfd)->dynverdef_section != 0
1424 || elf_tdata (abfd)->dynverref_section != 0))
1426 unsigned int vernum;
1427 const char *version_string;
1429 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1431 if (vernum == 0)
1432 version_string = "";
1433 else if (vernum == 1)
1434 version_string = "Base";
1435 else if (vernum <= elf_tdata (abfd)->cverdefs)
1436 version_string =
1437 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1438 else
1440 Elf_Internal_Verneed *t;
1442 version_string = "";
1443 for (t = elf_tdata (abfd)->verref;
1444 t != NULL;
1445 t = t->vn_nextref)
1447 Elf_Internal_Vernaux *a;
1449 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1451 if (a->vna_other == vernum)
1453 version_string = a->vna_nodename;
1454 break;
1460 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1461 fprintf (file, " %-11s", version_string);
1462 else
1464 int i;
1466 fprintf (file, " (%s)", version_string);
1467 for (i = 10 - strlen (version_string); i > 0; --i)
1468 putc (' ', file);
1472 /* If the st_other field is not zero, print it. */
1473 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1475 switch (st_other)
1477 case 0: break;
1478 case STV_INTERNAL: fprintf (file, " .internal"); break;
1479 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1480 case STV_PROTECTED: fprintf (file, " .protected"); break;
1481 default:
1482 /* Some other non-defined flags are also present, so print
1483 everything hex. */
1484 fprintf (file, " 0x%02x", (unsigned int) st_other);
1487 fprintf (file, " %s", name);
1489 break;
1493 /* Create an entry in an ELF linker hash table. */
1495 struct bfd_hash_entry *
1496 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1497 struct bfd_hash_table *table,
1498 const char *string)
1500 /* Allocate the structure if it has not already been allocated by a
1501 subclass. */
1502 if (entry == NULL)
1504 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
1505 if (entry == NULL)
1506 return entry;
1509 /* Call the allocation method of the superclass. */
1510 entry = _bfd_link_hash_newfunc (entry, table, string);
1511 if (entry != NULL)
1513 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
1514 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
1516 /* Set local fields. */
1517 ret->indx = -1;
1518 ret->dynindx = -1;
1519 ret->got = htab->init_got_refcount;
1520 ret->plt = htab->init_plt_refcount;
1521 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
1522 - offsetof (struct elf_link_hash_entry, size)));
1523 /* Assume that we have been called by a non-ELF symbol reader.
1524 This flag is then reset by the code which reads an ELF input
1525 file. This ensures that a symbol created by a non-ELF symbol
1526 reader will have the flag set correctly. */
1527 ret->non_elf = 1;
1530 return entry;
1533 /* Copy data from an indirect symbol to its direct symbol, hiding the
1534 old indirect symbol. Also used for copying flags to a weakdef. */
1536 void
1537 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
1538 struct elf_link_hash_entry *dir,
1539 struct elf_link_hash_entry *ind)
1541 struct elf_link_hash_table *htab;
1543 /* Copy down any references that we may have already seen to the
1544 symbol which just became indirect. */
1546 dir->ref_dynamic |= ind->ref_dynamic;
1547 dir->ref_regular |= ind->ref_regular;
1548 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
1549 dir->non_got_ref |= ind->non_got_ref;
1550 dir->needs_plt |= ind->needs_plt;
1551 dir->pointer_equality_needed |= ind->pointer_equality_needed;
1553 if (ind->root.type != bfd_link_hash_indirect)
1554 return;
1556 /* Copy over the global and procedure linkage table refcount entries.
1557 These may have been already set up by a check_relocs routine. */
1558 htab = elf_hash_table (info);
1559 if (ind->got.refcount > htab->init_got_refcount.refcount)
1561 if (dir->got.refcount < 0)
1562 dir->got.refcount = 0;
1563 dir->got.refcount += ind->got.refcount;
1564 ind->got.refcount = htab->init_got_refcount.refcount;
1567 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
1569 if (dir->plt.refcount < 0)
1570 dir->plt.refcount = 0;
1571 dir->plt.refcount += ind->plt.refcount;
1572 ind->plt.refcount = htab->init_plt_refcount.refcount;
1575 if (ind->dynindx != -1)
1577 if (dir->dynindx != -1)
1578 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
1579 dir->dynindx = ind->dynindx;
1580 dir->dynstr_index = ind->dynstr_index;
1581 ind->dynindx = -1;
1582 ind->dynstr_index = 0;
1586 void
1587 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
1588 struct elf_link_hash_entry *h,
1589 bfd_boolean force_local)
1591 h->plt = elf_hash_table (info)->init_plt_offset;
1592 h->needs_plt = 0;
1593 if (force_local)
1595 h->forced_local = 1;
1596 if (h->dynindx != -1)
1598 h->dynindx = -1;
1599 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1600 h->dynstr_index);
1605 /* Initialize an ELF linker hash table. */
1607 bfd_boolean
1608 _bfd_elf_link_hash_table_init
1609 (struct elf_link_hash_table *table,
1610 bfd *abfd,
1611 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
1612 struct bfd_hash_table *,
1613 const char *),
1614 unsigned int entsize)
1616 bfd_boolean ret;
1617 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
1619 memset (table, 0, sizeof * table);
1620 table->init_got_refcount.refcount = can_refcount - 1;
1621 table->init_plt_refcount.refcount = can_refcount - 1;
1622 table->init_got_offset.offset = -(bfd_vma) 1;
1623 table->init_plt_offset.offset = -(bfd_vma) 1;
1624 /* The first dynamic symbol is a dummy. */
1625 table->dynsymcount = 1;
1627 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
1628 table->root.type = bfd_link_elf_hash_table;
1630 return ret;
1633 /* Create an ELF linker hash table. */
1635 struct bfd_link_hash_table *
1636 _bfd_elf_link_hash_table_create (bfd *abfd)
1638 struct elf_link_hash_table *ret;
1639 bfd_size_type amt = sizeof (struct elf_link_hash_table);
1641 ret = bfd_malloc (amt);
1642 if (ret == NULL)
1643 return NULL;
1645 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
1646 sizeof (struct elf_link_hash_entry)))
1648 free (ret);
1649 return NULL;
1652 return &ret->root;
1655 /* This is a hook for the ELF emulation code in the generic linker to
1656 tell the backend linker what file name to use for the DT_NEEDED
1657 entry for a dynamic object. */
1659 void
1660 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
1662 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1663 && bfd_get_format (abfd) == bfd_object)
1664 elf_dt_name (abfd) = name;
1668 bfd_elf_get_dyn_lib_class (bfd *abfd)
1670 int lib_class;
1671 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1672 && bfd_get_format (abfd) == bfd_object)
1673 lib_class = elf_dyn_lib_class (abfd);
1674 else
1675 lib_class = 0;
1676 return lib_class;
1679 void
1680 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
1682 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1683 && bfd_get_format (abfd) == bfd_object)
1684 elf_dyn_lib_class (abfd) = lib_class;
1687 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1688 the linker ELF emulation code. */
1690 struct bfd_link_needed_list *
1691 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
1692 struct bfd_link_info *info)
1694 if (! is_elf_hash_table (info->hash))
1695 return NULL;
1696 return elf_hash_table (info)->needed;
1699 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1700 hook for the linker ELF emulation code. */
1702 struct bfd_link_needed_list *
1703 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
1704 struct bfd_link_info *info)
1706 if (! is_elf_hash_table (info->hash))
1707 return NULL;
1708 return elf_hash_table (info)->runpath;
1711 /* Get the name actually used for a dynamic object for a link. This
1712 is the SONAME entry if there is one. Otherwise, it is the string
1713 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1715 const char *
1716 bfd_elf_get_dt_soname (bfd *abfd)
1718 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1719 && bfd_get_format (abfd) == bfd_object)
1720 return elf_dt_name (abfd);
1721 return NULL;
1724 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1725 the ELF linker emulation code. */
1727 bfd_boolean
1728 bfd_elf_get_bfd_needed_list (bfd *abfd,
1729 struct bfd_link_needed_list **pneeded)
1731 asection *s;
1732 bfd_byte *dynbuf = NULL;
1733 int elfsec;
1734 unsigned long shlink;
1735 bfd_byte *extdyn, *extdynend;
1736 size_t extdynsize;
1737 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1739 *pneeded = NULL;
1741 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1742 || bfd_get_format (abfd) != bfd_object)
1743 return TRUE;
1745 s = bfd_get_section_by_name (abfd, ".dynamic");
1746 if (s == NULL || s->size == 0)
1747 return TRUE;
1749 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1750 goto error_return;
1752 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1753 if (elfsec == -1)
1754 goto error_return;
1756 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1758 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1759 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1761 extdyn = dynbuf;
1762 extdynend = extdyn + s->size;
1763 for (; extdyn < extdynend; extdyn += extdynsize)
1765 Elf_Internal_Dyn dyn;
1767 (*swap_dyn_in) (abfd, extdyn, &dyn);
1769 if (dyn.d_tag == DT_NULL)
1770 break;
1772 if (dyn.d_tag == DT_NEEDED)
1774 const char *string;
1775 struct bfd_link_needed_list *l;
1776 unsigned int tagv = dyn.d_un.d_val;
1777 bfd_size_type amt;
1779 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1780 if (string == NULL)
1781 goto error_return;
1783 amt = sizeof *l;
1784 l = bfd_alloc (abfd, amt);
1785 if (l == NULL)
1786 goto error_return;
1788 l->by = abfd;
1789 l->name = string;
1790 l->next = *pneeded;
1791 *pneeded = l;
1795 free (dynbuf);
1797 return TRUE;
1799 error_return:
1800 if (dynbuf != NULL)
1801 free (dynbuf);
1802 return FALSE;
1805 /* Allocate an ELF string table--force the first byte to be zero. */
1807 struct bfd_strtab_hash *
1808 _bfd_elf_stringtab_init (void)
1810 struct bfd_strtab_hash *ret;
1812 ret = _bfd_stringtab_init ();
1813 if (ret != NULL)
1815 bfd_size_type loc;
1817 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1818 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1819 if (loc == (bfd_size_type) -1)
1821 _bfd_stringtab_free (ret);
1822 ret = NULL;
1825 return ret;
1828 /* ELF .o/exec file reading */
1830 /* Create a new bfd section from an ELF section header. */
1832 bfd_boolean
1833 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1835 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1836 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1837 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1838 const char *name;
1840 name = bfd_elf_string_from_elf_section (abfd,
1841 elf_elfheader (abfd)->e_shstrndx,
1842 hdr->sh_name);
1843 if (name == NULL)
1844 return FALSE;
1846 switch (hdr->sh_type)
1848 case SHT_NULL:
1849 /* Inactive section. Throw it away. */
1850 return TRUE;
1852 case SHT_PROGBITS: /* Normal section with contents. */
1853 case SHT_NOBITS: /* .bss section. */
1854 case SHT_HASH: /* .hash section. */
1855 case SHT_NOTE: /* .note section. */
1856 case SHT_INIT_ARRAY: /* .init_array section. */
1857 case SHT_FINI_ARRAY: /* .fini_array section. */
1858 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1859 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
1860 case SHT_GNU_HASH: /* .gnu.hash section. */
1861 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1863 case SHT_DYNAMIC: /* Dynamic linking information. */
1864 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1865 return FALSE;
1866 if (hdr->sh_link > elf_numsections (abfd)
1867 || elf_elfsections (abfd)[hdr->sh_link] == NULL)
1868 return FALSE;
1869 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1871 Elf_Internal_Shdr *dynsymhdr;
1873 /* The shared libraries distributed with hpux11 have a bogus
1874 sh_link field for the ".dynamic" section. Find the
1875 string table for the ".dynsym" section instead. */
1876 if (elf_dynsymtab (abfd) != 0)
1878 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1879 hdr->sh_link = dynsymhdr->sh_link;
1881 else
1883 unsigned int i, num_sec;
1885 num_sec = elf_numsections (abfd);
1886 for (i = 1; i < num_sec; i++)
1888 dynsymhdr = elf_elfsections (abfd)[i];
1889 if (dynsymhdr->sh_type == SHT_DYNSYM)
1891 hdr->sh_link = dynsymhdr->sh_link;
1892 break;
1897 break;
1899 case SHT_SYMTAB: /* A symbol table */
1900 if (elf_onesymtab (abfd) == shindex)
1901 return TRUE;
1903 if (hdr->sh_entsize != bed->s->sizeof_sym)
1904 return FALSE;
1905 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1906 elf_onesymtab (abfd) = shindex;
1907 elf_tdata (abfd)->symtab_hdr = *hdr;
1908 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1909 abfd->flags |= HAS_SYMS;
1911 /* Sometimes a shared object will map in the symbol table. If
1912 SHF_ALLOC is set, and this is a shared object, then we also
1913 treat this section as a BFD section. We can not base the
1914 decision purely on SHF_ALLOC, because that flag is sometimes
1915 set in a relocatable object file, which would confuse the
1916 linker. */
1917 if ((hdr->sh_flags & SHF_ALLOC) != 0
1918 && (abfd->flags & DYNAMIC) != 0
1919 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1920 shindex))
1921 return FALSE;
1923 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1924 can't read symbols without that section loaded as well. It
1925 is most likely specified by the next section header. */
1926 if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex)
1928 unsigned int i, num_sec;
1930 num_sec = elf_numsections (abfd);
1931 for (i = shindex + 1; i < num_sec; i++)
1933 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1934 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1935 && hdr2->sh_link == shindex)
1936 break;
1938 if (i == num_sec)
1939 for (i = 1; i < shindex; i++)
1941 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1942 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1943 && hdr2->sh_link == shindex)
1944 break;
1946 if (i != shindex)
1947 return bfd_section_from_shdr (abfd, i);
1949 return TRUE;
1951 case SHT_DYNSYM: /* A dynamic symbol table */
1952 if (elf_dynsymtab (abfd) == shindex)
1953 return TRUE;
1955 if (hdr->sh_entsize != bed->s->sizeof_sym)
1956 return FALSE;
1957 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1958 elf_dynsymtab (abfd) = shindex;
1959 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1960 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1961 abfd->flags |= HAS_SYMS;
1963 /* Besides being a symbol table, we also treat this as a regular
1964 section, so that objcopy can handle it. */
1965 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1967 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1968 if (elf_symtab_shndx (abfd) == shindex)
1969 return TRUE;
1971 BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
1972 elf_symtab_shndx (abfd) = shindex;
1973 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1974 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1975 return TRUE;
1977 case SHT_STRTAB: /* A string table */
1978 if (hdr->bfd_section != NULL)
1979 return TRUE;
1980 if (ehdr->e_shstrndx == shindex)
1982 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1983 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1984 return TRUE;
1986 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
1988 symtab_strtab:
1989 elf_tdata (abfd)->strtab_hdr = *hdr;
1990 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
1991 return TRUE;
1993 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
1995 dynsymtab_strtab:
1996 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1997 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
1998 elf_elfsections (abfd)[shindex] = hdr;
1999 /* We also treat this as a regular section, so that objcopy
2000 can handle it. */
2001 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2002 shindex);
2005 /* If the string table isn't one of the above, then treat it as a
2006 regular section. We need to scan all the headers to be sure,
2007 just in case this strtab section appeared before the above. */
2008 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2010 unsigned int i, num_sec;
2012 num_sec = elf_numsections (abfd);
2013 for (i = 1; i < num_sec; i++)
2015 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2016 if (hdr2->sh_link == shindex)
2018 /* Prevent endless recursion on broken objects. */
2019 if (i == shindex)
2020 return FALSE;
2021 if (! bfd_section_from_shdr (abfd, i))
2022 return FALSE;
2023 if (elf_onesymtab (abfd) == i)
2024 goto symtab_strtab;
2025 if (elf_dynsymtab (abfd) == i)
2026 goto dynsymtab_strtab;
2030 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2032 case SHT_REL:
2033 case SHT_RELA:
2034 /* *These* do a lot of work -- but build no sections! */
2036 asection *target_sect;
2037 Elf_Internal_Shdr *hdr2;
2038 unsigned int num_sec = elf_numsections (abfd);
2040 if (hdr->sh_entsize
2041 != (bfd_size_type) (hdr->sh_type == SHT_REL
2042 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
2043 return FALSE;
2045 /* Check for a bogus link to avoid crashing. */
2046 if ((hdr->sh_link >= SHN_LORESERVE && hdr->sh_link <= SHN_HIRESERVE)
2047 || hdr->sh_link >= num_sec)
2049 ((*_bfd_error_handler)
2050 (_("%B: invalid link %lu for reloc section %s (index %u)"),
2051 abfd, hdr->sh_link, name, shindex));
2052 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2053 shindex);
2056 /* For some incomprehensible reason Oracle distributes
2057 libraries for Solaris in which some of the objects have
2058 bogus sh_link fields. It would be nice if we could just
2059 reject them, but, unfortunately, some people need to use
2060 them. We scan through the section headers; if we find only
2061 one suitable symbol table, we clobber the sh_link to point
2062 to it. I hope this doesn't break anything. */
2063 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
2064 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
2066 unsigned int scan;
2067 int found;
2069 found = 0;
2070 for (scan = 1; scan < num_sec; scan++)
2072 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
2073 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
2075 if (found != 0)
2077 found = 0;
2078 break;
2080 found = scan;
2083 if (found != 0)
2084 hdr->sh_link = found;
2087 /* Get the symbol table. */
2088 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2089 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2090 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2091 return FALSE;
2093 /* If this reloc section does not use the main symbol table we
2094 don't treat it as a reloc section. BFD can't adequately
2095 represent such a section, so at least for now, we don't
2096 try. We just present it as a normal section. We also
2097 can't use it as a reloc section if it points to the null
2098 section, an invalid section, or another reloc section. */
2099 if (hdr->sh_link != elf_onesymtab (abfd)
2100 || hdr->sh_info == SHN_UNDEF
2101 || (hdr->sh_info >= SHN_LORESERVE && hdr->sh_info <= SHN_HIRESERVE)
2102 || hdr->sh_info >= num_sec
2103 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2104 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2105 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2106 shindex);
2108 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2109 return FALSE;
2110 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2111 if (target_sect == NULL)
2112 return FALSE;
2114 if ((target_sect->flags & SEC_RELOC) == 0
2115 || target_sect->reloc_count == 0)
2116 hdr2 = &elf_section_data (target_sect)->rel_hdr;
2117 else
2119 bfd_size_type amt;
2120 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
2121 amt = sizeof (*hdr2);
2122 hdr2 = bfd_alloc (abfd, amt);
2123 elf_section_data (target_sect)->rel_hdr2 = hdr2;
2125 *hdr2 = *hdr;
2126 elf_elfsections (abfd)[shindex] = hdr2;
2127 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
2128 target_sect->flags |= SEC_RELOC;
2129 target_sect->relocation = NULL;
2130 target_sect->rel_filepos = hdr->sh_offset;
2131 /* In the section to which the relocations apply, mark whether
2132 its relocations are of the REL or RELA variety. */
2133 if (hdr->sh_size != 0)
2134 target_sect->use_rela_p = hdr->sh_type == SHT_RELA;
2135 abfd->flags |= HAS_RELOC;
2136 return TRUE;
2139 case SHT_GNU_verdef:
2140 elf_dynverdef (abfd) = shindex;
2141 elf_tdata (abfd)->dynverdef_hdr = *hdr;
2142 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2144 case SHT_GNU_versym:
2145 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2146 return FALSE;
2147 elf_dynversym (abfd) = shindex;
2148 elf_tdata (abfd)->dynversym_hdr = *hdr;
2149 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2151 case SHT_GNU_verneed:
2152 elf_dynverref (abfd) = shindex;
2153 elf_tdata (abfd)->dynverref_hdr = *hdr;
2154 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2156 case SHT_SHLIB:
2157 return TRUE;
2159 case SHT_GROUP:
2160 /* We need a BFD section for objcopy and relocatable linking,
2161 and it's handy to have the signature available as the section
2162 name. */
2163 if (! IS_VALID_GROUP_SECTION_HEADER (hdr))
2164 return FALSE;
2165 name = group_signature (abfd, hdr);
2166 if (name == NULL)
2167 return FALSE;
2168 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2169 return FALSE;
2170 if (hdr->contents != NULL)
2172 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
2173 unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE;
2174 asection *s;
2176 if (idx->flags & GRP_COMDAT)
2177 hdr->bfd_section->flags
2178 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
2180 /* We try to keep the same section order as it comes in. */
2181 idx += n_elt;
2182 while (--n_elt != 0)
2184 --idx;
2186 if (idx->shdr != NULL
2187 && (s = idx->shdr->bfd_section) != NULL
2188 && elf_next_in_group (s) != NULL)
2190 elf_next_in_group (hdr->bfd_section) = s;
2191 break;
2195 break;
2197 default:
2198 /* Check for any processor-specific section types. */
2199 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2200 return TRUE;
2202 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2204 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2205 /* FIXME: How to properly handle allocated section reserved
2206 for applications? */
2207 (*_bfd_error_handler)
2208 (_("%B: don't know how to handle allocated, application "
2209 "specific section `%s' [0x%8x]"),
2210 abfd, name, hdr->sh_type);
2211 else
2212 /* Allow sections reserved for applications. */
2213 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2214 shindex);
2216 else if (hdr->sh_type >= SHT_LOPROC
2217 && hdr->sh_type <= SHT_HIPROC)
2218 /* FIXME: We should handle this section. */
2219 (*_bfd_error_handler)
2220 (_("%B: don't know how to handle processor specific section "
2221 "`%s' [0x%8x]"),
2222 abfd, name, hdr->sh_type);
2223 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2225 /* Unrecognised OS-specific sections. */
2226 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2227 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2228 required to correctly process the section and the file should
2229 be rejected with an error message. */
2230 (*_bfd_error_handler)
2231 (_("%B: don't know how to handle OS specific section "
2232 "`%s' [0x%8x]"),
2233 abfd, name, hdr->sh_type);
2234 else
2235 /* Otherwise it should be processed. */
2236 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2238 else
2239 /* FIXME: We should handle this section. */
2240 (*_bfd_error_handler)
2241 (_("%B: don't know how to handle section `%s' [0x%8x]"),
2242 abfd, name, hdr->sh_type);
2244 return FALSE;
2247 return TRUE;
2250 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
2251 Return SEC for sections that have no elf section, and NULL on error. */
2253 asection *
2254 bfd_section_from_r_symndx (bfd *abfd,
2255 struct sym_sec_cache *cache,
2256 asection *sec,
2257 unsigned long r_symndx)
2259 Elf_Internal_Shdr *symtab_hdr;
2260 unsigned char esym[sizeof (Elf64_External_Sym)];
2261 Elf_External_Sym_Shndx eshndx;
2262 Elf_Internal_Sym isym;
2263 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2265 if (cache->abfd == abfd && cache->indx[ent] == r_symndx)
2266 return cache->sec[ent];
2268 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2269 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2270 &isym, esym, &eshndx) == NULL)
2271 return NULL;
2273 if (cache->abfd != abfd)
2275 memset (cache->indx, -1, sizeof (cache->indx));
2276 cache->abfd = abfd;
2278 cache->indx[ent] = r_symndx;
2279 cache->sec[ent] = sec;
2280 if ((isym.st_shndx != SHN_UNDEF && isym.st_shndx < SHN_LORESERVE)
2281 || isym.st_shndx > SHN_HIRESERVE)
2283 asection *s;
2284 s = bfd_section_from_elf_index (abfd, isym.st_shndx);
2285 if (s != NULL)
2286 cache->sec[ent] = s;
2288 return cache->sec[ent];
2291 /* Given an ELF section number, retrieve the corresponding BFD
2292 section. */
2294 asection *
2295 bfd_section_from_elf_index (bfd *abfd, unsigned int index)
2297 if (index >= elf_numsections (abfd))
2298 return NULL;
2299 return elf_elfsections (abfd)[index]->bfd_section;
2302 static const struct bfd_elf_special_section special_sections_b[] =
2304 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2305 { NULL, 0, 0, 0, 0 }
2308 static const struct bfd_elf_special_section special_sections_c[] =
2310 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2311 { NULL, 0, 0, 0, 0 }
2314 static const struct bfd_elf_special_section special_sections_d[] =
2316 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2317 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2318 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2319 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2320 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2321 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2322 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2323 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2324 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2325 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2326 { NULL, 0, 0, 0, 0 }
2329 static const struct bfd_elf_special_section special_sections_f[] =
2331 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2332 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2333 { NULL, 0, 0, 0, 0 }
2336 static const struct bfd_elf_special_section special_sections_g[] =
2338 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2339 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2340 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2341 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2342 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2343 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2344 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2345 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2346 { NULL, 0, 0, 0, 0 }
2349 static const struct bfd_elf_special_section special_sections_h[] =
2351 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2352 { NULL, 0, 0, 0, 0 }
2355 static const struct bfd_elf_special_section special_sections_i[] =
2357 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2358 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2359 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2360 { NULL, 0, 0, 0, 0 }
2363 static const struct bfd_elf_special_section special_sections_l[] =
2365 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2366 { NULL, 0, 0, 0, 0 }
2369 static const struct bfd_elf_special_section special_sections_n[] =
2371 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2372 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2373 { NULL, 0, 0, 0, 0 }
2376 static const struct bfd_elf_special_section special_sections_p[] =
2378 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2379 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2380 { NULL, 0, 0, 0, 0 }
2383 static const struct bfd_elf_special_section special_sections_r[] =
2385 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2386 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2387 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2388 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2389 { NULL, 0, 0, 0, 0 }
2392 static const struct bfd_elf_special_section special_sections_s[] =
2394 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2395 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2396 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2397 /* See struct bfd_elf_special_section declaration for the semantics of
2398 this special case where .prefix_length != strlen (.prefix). */
2399 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2400 { NULL, 0, 0, 0, 0 }
2403 static const struct bfd_elf_special_section special_sections_t[] =
2405 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2406 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2407 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2408 { NULL, 0, 0, 0, 0 }
2411 static const struct bfd_elf_special_section *special_sections[] =
2413 special_sections_b, /* 'b' */
2414 special_sections_c, /* 'b' */
2415 special_sections_d, /* 'd' */
2416 NULL, /* 'e' */
2417 special_sections_f, /* 'f' */
2418 special_sections_g, /* 'g' */
2419 special_sections_h, /* 'h' */
2420 special_sections_i, /* 'i' */
2421 NULL, /* 'j' */
2422 NULL, /* 'k' */
2423 special_sections_l, /* 'l' */
2424 NULL, /* 'm' */
2425 special_sections_n, /* 'n' */
2426 NULL, /* 'o' */
2427 special_sections_p, /* 'p' */
2428 NULL, /* 'q' */
2429 special_sections_r, /* 'r' */
2430 special_sections_s, /* 's' */
2431 special_sections_t, /* 't' */
2434 const struct bfd_elf_special_section *
2435 _bfd_elf_get_special_section (const char *name,
2436 const struct bfd_elf_special_section *spec,
2437 unsigned int rela)
2439 int i;
2440 int len;
2442 len = strlen (name);
2444 for (i = 0; spec[i].prefix != NULL; i++)
2446 int suffix_len;
2447 int prefix_len = spec[i].prefix_length;
2449 if (len < prefix_len)
2450 continue;
2451 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2452 continue;
2454 suffix_len = spec[i].suffix_length;
2455 if (suffix_len <= 0)
2457 if (name[prefix_len] != 0)
2459 if (suffix_len == 0)
2460 continue;
2461 if (name[prefix_len] != '.'
2462 && (suffix_len == -2
2463 || (rela && spec[i].type == SHT_REL)))
2464 continue;
2467 else
2469 if (len < prefix_len + suffix_len)
2470 continue;
2471 if (memcmp (name + len - suffix_len,
2472 spec[i].prefix + prefix_len,
2473 suffix_len) != 0)
2474 continue;
2476 return &spec[i];
2479 return NULL;
2482 const struct bfd_elf_special_section *
2483 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2485 int i;
2486 const struct bfd_elf_special_section *spec;
2487 const struct elf_backend_data *bed;
2489 /* See if this is one of the special sections. */
2490 if (sec->name == NULL)
2491 return NULL;
2493 bed = get_elf_backend_data (abfd);
2494 spec = bed->special_sections;
2495 if (spec)
2497 spec = _bfd_elf_get_special_section (sec->name,
2498 bed->special_sections,
2499 sec->use_rela_p);
2500 if (spec != NULL)
2501 return spec;
2504 if (sec->name[0] != '.')
2505 return NULL;
2507 i = sec->name[1] - 'b';
2508 if (i < 0 || i > 't' - 'b')
2509 return NULL;
2511 spec = special_sections[i];
2513 if (spec == NULL)
2514 return NULL;
2516 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2519 bfd_boolean
2520 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2522 struct bfd_elf_section_data *sdata;
2523 const struct elf_backend_data *bed;
2524 const struct bfd_elf_special_section *ssect;
2526 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2527 if (sdata == NULL)
2529 sdata = bfd_zalloc (abfd, sizeof (*sdata));
2530 if (sdata == NULL)
2531 return FALSE;
2532 sec->used_by_bfd = sdata;
2535 /* Indicate whether or not this section should use RELA relocations. */
2536 bed = get_elf_backend_data (abfd);
2537 sec->use_rela_p = bed->default_use_rela_p;
2539 /* When we read a file, we don't need to set ELF section type and
2540 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2541 anyway. We will set ELF section type and flags for all linker
2542 created sections. If user specifies BFD section flags, we will
2543 set ELF section type and flags based on BFD section flags in
2544 elf_fake_sections. */
2545 if ((!sec->flags && abfd->direction != read_direction)
2546 || (sec->flags & SEC_LINKER_CREATED) != 0)
2548 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2549 if (ssect != NULL)
2551 elf_section_type (sec) = ssect->type;
2552 elf_section_flags (sec) = ssect->attr;
2556 return _bfd_generic_new_section_hook (abfd, sec);
2559 /* Create a new bfd section from an ELF program header.
2561 Since program segments have no names, we generate a synthetic name
2562 of the form segment<NUM>, where NUM is generally the index in the
2563 program header table. For segments that are split (see below) we
2564 generate the names segment<NUM>a and segment<NUM>b.
2566 Note that some program segments may have a file size that is different than
2567 (less than) the memory size. All this means is that at execution the
2568 system must allocate the amount of memory specified by the memory size,
2569 but only initialize it with the first "file size" bytes read from the
2570 file. This would occur for example, with program segments consisting
2571 of combined data+bss.
2573 To handle the above situation, this routine generates TWO bfd sections
2574 for the single program segment. The first has the length specified by
2575 the file size of the segment, and the second has the length specified
2576 by the difference between the two sizes. In effect, the segment is split
2577 into it's initialized and uninitialized parts.
2581 bfd_boolean
2582 _bfd_elf_make_section_from_phdr (bfd *abfd,
2583 Elf_Internal_Phdr *hdr,
2584 int index,
2585 const char *typename)
2587 asection *newsect;
2588 char *name;
2589 char namebuf[64];
2590 size_t len;
2591 int split;
2593 split = ((hdr->p_memsz > 0)
2594 && (hdr->p_filesz > 0)
2595 && (hdr->p_memsz > hdr->p_filesz));
2596 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
2597 len = strlen (namebuf) + 1;
2598 name = bfd_alloc (abfd, len);
2599 if (!name)
2600 return FALSE;
2601 memcpy (name, namebuf, len);
2602 newsect = bfd_make_section (abfd, name);
2603 if (newsect == NULL)
2604 return FALSE;
2605 newsect->vma = hdr->p_vaddr;
2606 newsect->lma = hdr->p_paddr;
2607 newsect->size = hdr->p_filesz;
2608 newsect->filepos = hdr->p_offset;
2609 newsect->flags |= SEC_HAS_CONTENTS;
2610 newsect->alignment_power = bfd_log2 (hdr->p_align);
2611 if (hdr->p_type == PT_LOAD)
2613 newsect->flags |= SEC_ALLOC;
2614 newsect->flags |= SEC_LOAD;
2615 if (hdr->p_flags & PF_X)
2617 /* FIXME: all we known is that it has execute PERMISSION,
2618 may be data. */
2619 newsect->flags |= SEC_CODE;
2622 if (!(hdr->p_flags & PF_W))
2624 newsect->flags |= SEC_READONLY;
2627 if (split)
2629 sprintf (namebuf, "%s%db", typename, index);
2630 len = strlen (namebuf) + 1;
2631 name = bfd_alloc (abfd, len);
2632 if (!name)
2633 return FALSE;
2634 memcpy (name, namebuf, len);
2635 newsect = bfd_make_section (abfd, name);
2636 if (newsect == NULL)
2637 return FALSE;
2638 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2639 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2640 newsect->size = hdr->p_memsz - hdr->p_filesz;
2641 if (hdr->p_type == PT_LOAD)
2643 newsect->flags |= SEC_ALLOC;
2644 if (hdr->p_flags & PF_X)
2645 newsect->flags |= SEC_CODE;
2647 if (!(hdr->p_flags & PF_W))
2648 newsect->flags |= SEC_READONLY;
2651 return TRUE;
2654 bfd_boolean
2655 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index)
2657 const struct elf_backend_data *bed;
2659 switch (hdr->p_type)
2661 case PT_NULL:
2662 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2664 case PT_LOAD:
2665 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2667 case PT_DYNAMIC:
2668 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2670 case PT_INTERP:
2671 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2673 case PT_NOTE:
2674 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2675 return FALSE;
2676 if (! elfcore_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2677 return FALSE;
2678 return TRUE;
2680 case PT_SHLIB:
2681 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2683 case PT_PHDR:
2684 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2686 case PT_GNU_EH_FRAME:
2687 return _bfd_elf_make_section_from_phdr (abfd, hdr, index,
2688 "eh_frame_hdr");
2690 case PT_GNU_STACK:
2691 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "stack");
2693 case PT_GNU_RELRO:
2694 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro");
2696 default:
2697 /* Check for any processor-specific program segment types. */
2698 bed = get_elf_backend_data (abfd);
2699 return bed->elf_backend_section_from_phdr (abfd, hdr, index, "proc");
2703 /* Initialize REL_HDR, the section-header for new section, containing
2704 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2705 relocations; otherwise, we use REL relocations. */
2707 bfd_boolean
2708 _bfd_elf_init_reloc_shdr (bfd *abfd,
2709 Elf_Internal_Shdr *rel_hdr,
2710 asection *asect,
2711 bfd_boolean use_rela_p)
2713 char *name;
2714 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2715 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2717 name = bfd_alloc (abfd, amt);
2718 if (name == NULL)
2719 return FALSE;
2720 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2721 rel_hdr->sh_name =
2722 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2723 FALSE);
2724 if (rel_hdr->sh_name == (unsigned int) -1)
2725 return FALSE;
2726 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2727 rel_hdr->sh_entsize = (use_rela_p
2728 ? bed->s->sizeof_rela
2729 : bed->s->sizeof_rel);
2730 rel_hdr->sh_addralign = 1 << bed->s->log_file_align;
2731 rel_hdr->sh_flags = 0;
2732 rel_hdr->sh_addr = 0;
2733 rel_hdr->sh_size = 0;
2734 rel_hdr->sh_offset = 0;
2736 return TRUE;
2739 /* Set up an ELF internal section header for a section. */
2741 static void
2742 elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg)
2744 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2745 bfd_boolean *failedptr = failedptrarg;
2746 Elf_Internal_Shdr *this_hdr;
2747 unsigned int sh_type;
2749 if (*failedptr)
2751 /* We already failed; just get out of the bfd_map_over_sections
2752 loop. */
2753 return;
2756 this_hdr = &elf_section_data (asect)->this_hdr;
2758 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2759 asect->name, FALSE);
2760 if (this_hdr->sh_name == (unsigned int) -1)
2762 *failedptr = TRUE;
2763 return;
2766 /* Don't clear sh_flags. Assembler may set additional bits. */
2768 if ((asect->flags & SEC_ALLOC) != 0
2769 || asect->user_set_vma)
2770 this_hdr->sh_addr = asect->vma;
2771 else
2772 this_hdr->sh_addr = 0;
2774 this_hdr->sh_offset = 0;
2775 this_hdr->sh_size = asect->size;
2776 this_hdr->sh_link = 0;
2777 this_hdr->sh_addralign = 1 << asect->alignment_power;
2778 /* The sh_entsize and sh_info fields may have been set already by
2779 copy_private_section_data. */
2781 this_hdr->bfd_section = asect;
2782 this_hdr->contents = NULL;
2784 /* If the section type is unspecified, we set it based on
2785 asect->flags. */
2786 if (this_hdr->sh_type == SHT_NULL)
2788 if ((asect->flags & SEC_GROUP) != 0)
2789 this_hdr->sh_type = SHT_GROUP;
2790 else if ((asect->flags & SEC_ALLOC) != 0
2791 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2792 || (asect->flags & SEC_NEVER_LOAD) != 0))
2793 this_hdr->sh_type = SHT_NOBITS;
2794 else
2795 this_hdr->sh_type = SHT_PROGBITS;
2798 switch (this_hdr->sh_type)
2800 default:
2801 break;
2803 case SHT_STRTAB:
2804 case SHT_INIT_ARRAY:
2805 case SHT_FINI_ARRAY:
2806 case SHT_PREINIT_ARRAY:
2807 case SHT_NOTE:
2808 case SHT_NOBITS:
2809 case SHT_PROGBITS:
2810 break;
2812 case SHT_HASH:
2813 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2814 break;
2816 case SHT_DYNSYM:
2817 this_hdr->sh_entsize = bed->s->sizeof_sym;
2818 break;
2820 case SHT_DYNAMIC:
2821 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2822 break;
2824 case SHT_RELA:
2825 if (get_elf_backend_data (abfd)->may_use_rela_p)
2826 this_hdr->sh_entsize = bed->s->sizeof_rela;
2827 break;
2829 case SHT_REL:
2830 if (get_elf_backend_data (abfd)->may_use_rel_p)
2831 this_hdr->sh_entsize = bed->s->sizeof_rel;
2832 break;
2834 case SHT_GNU_versym:
2835 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2836 break;
2838 case SHT_GNU_verdef:
2839 this_hdr->sh_entsize = 0;
2840 /* objcopy or strip will copy over sh_info, but may not set
2841 cverdefs. The linker will set cverdefs, but sh_info will be
2842 zero. */
2843 if (this_hdr->sh_info == 0)
2844 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2845 else
2846 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2847 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2848 break;
2850 case SHT_GNU_verneed:
2851 this_hdr->sh_entsize = 0;
2852 /* objcopy or strip will copy over sh_info, but may not set
2853 cverrefs. The linker will set cverrefs, but sh_info will be
2854 zero. */
2855 if (this_hdr->sh_info == 0)
2856 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2857 else
2858 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2859 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2860 break;
2862 case SHT_GROUP:
2863 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2864 break;
2866 case SHT_GNU_HASH:
2867 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
2868 break;
2871 if ((asect->flags & SEC_ALLOC) != 0)
2872 this_hdr->sh_flags |= SHF_ALLOC;
2873 if ((asect->flags & SEC_READONLY) == 0)
2874 this_hdr->sh_flags |= SHF_WRITE;
2875 if ((asect->flags & SEC_CODE) != 0)
2876 this_hdr->sh_flags |= SHF_EXECINSTR;
2877 if ((asect->flags & SEC_MERGE) != 0)
2879 this_hdr->sh_flags |= SHF_MERGE;
2880 this_hdr->sh_entsize = asect->entsize;
2881 if ((asect->flags & SEC_STRINGS) != 0)
2882 this_hdr->sh_flags |= SHF_STRINGS;
2884 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2885 this_hdr->sh_flags |= SHF_GROUP;
2886 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2888 this_hdr->sh_flags |= SHF_TLS;
2889 if (asect->size == 0
2890 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2892 struct bfd_link_order *o = asect->map_tail.link_order;
2894 this_hdr->sh_size = 0;
2895 if (o != NULL)
2897 this_hdr->sh_size = o->offset + o->size;
2898 if (this_hdr->sh_size != 0)
2899 this_hdr->sh_type = SHT_NOBITS;
2904 /* Check for processor-specific section types. */
2905 sh_type = this_hdr->sh_type;
2906 if (bed->elf_backend_fake_sections
2907 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2908 *failedptr = TRUE;
2910 if (sh_type == SHT_NOBITS && asect->size != 0)
2912 /* Don't change the header type from NOBITS if we are being
2913 called for objcopy --only-keep-debug. */
2914 this_hdr->sh_type = sh_type;
2917 /* If the section has relocs, set up a section header for the
2918 SHT_REL[A] section. If two relocation sections are required for
2919 this section, it is up to the processor-specific back-end to
2920 create the other. */
2921 if ((asect->flags & SEC_RELOC) != 0
2922 && !_bfd_elf_init_reloc_shdr (abfd,
2923 &elf_section_data (asect)->rel_hdr,
2924 asect,
2925 asect->use_rela_p))
2926 *failedptr = TRUE;
2929 /* Fill in the contents of a SHT_GROUP section. */
2931 void
2932 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2934 bfd_boolean *failedptr = failedptrarg;
2935 unsigned long symindx;
2936 asection *elt, *first;
2937 unsigned char *loc;
2938 bfd_boolean gas;
2940 /* Ignore linker created group section. See elfNN_ia64_object_p in
2941 elfxx-ia64.c. */
2942 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
2943 || *failedptr)
2944 return;
2946 symindx = 0;
2947 if (elf_group_id (sec) != NULL)
2948 symindx = elf_group_id (sec)->udata.i;
2950 if (symindx == 0)
2952 /* If called from the assembler, swap_out_syms will have set up
2953 elf_section_syms; If called for "ld -r", use target_index. */
2954 if (elf_section_syms (abfd) != NULL)
2955 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2956 else
2957 symindx = sec->target_index;
2959 elf_section_data (sec)->this_hdr.sh_info = symindx;
2961 /* The contents won't be allocated for "ld -r" or objcopy. */
2962 gas = TRUE;
2963 if (sec->contents == NULL)
2965 gas = FALSE;
2966 sec->contents = bfd_alloc (abfd, sec->size);
2968 /* Arrange for the section to be written out. */
2969 elf_section_data (sec)->this_hdr.contents = sec->contents;
2970 if (sec->contents == NULL)
2972 *failedptr = TRUE;
2973 return;
2977 loc = sec->contents + sec->size;
2979 /* Get the pointer to the first section in the group that gas
2980 squirreled away here. objcopy arranges for this to be set to the
2981 start of the input section group. */
2982 first = elt = elf_next_in_group (sec);
2984 /* First element is a flag word. Rest of section is elf section
2985 indices for all the sections of the group. Write them backwards
2986 just to keep the group in the same order as given in .section
2987 directives, not that it matters. */
2988 while (elt != NULL)
2990 asection *s;
2991 unsigned int idx;
2993 loc -= 4;
2994 s = elt;
2995 if (!gas)
2996 s = s->output_section;
2997 idx = 0;
2998 if (s != NULL)
2999 idx = elf_section_data (s)->this_idx;
3000 H_PUT_32 (abfd, idx, loc);
3001 elt = elf_next_in_group (elt);
3002 if (elt == first)
3003 break;
3006 if ((loc -= 4) != sec->contents)
3007 abort ();
3009 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
3012 /* Assign all ELF section numbers. The dummy first section is handled here
3013 too. The link/info pointers for the standard section types are filled
3014 in here too, while we're at it. */
3016 static bfd_boolean
3017 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
3019 struct elf_obj_tdata *t = elf_tdata (abfd);
3020 asection *sec;
3021 unsigned int section_number, secn;
3022 Elf_Internal_Shdr **i_shdrp;
3023 struct bfd_elf_section_data *d;
3025 section_number = 1;
3027 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3029 /* SHT_GROUP sections are in relocatable files only. */
3030 if (link_info == NULL || link_info->relocatable)
3032 /* Put SHT_GROUP sections first. */
3033 for (sec = abfd->sections; sec != NULL; sec = sec->next)
3035 d = elf_section_data (sec);
3037 if (d->this_hdr.sh_type == SHT_GROUP)
3039 if (sec->flags & SEC_LINKER_CREATED)
3041 /* Remove the linker created SHT_GROUP sections. */
3042 bfd_section_list_remove (abfd, sec);
3043 abfd->section_count--;
3045 else
3047 if (section_number == SHN_LORESERVE)
3048 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3049 d->this_idx = section_number++;
3055 for (sec = abfd->sections; sec; sec = sec->next)
3057 d = elf_section_data (sec);
3059 if (d->this_hdr.sh_type != SHT_GROUP)
3061 if (section_number == SHN_LORESERVE)
3062 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3063 d->this_idx = section_number++;
3065 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
3066 if ((sec->flags & SEC_RELOC) == 0)
3067 d->rel_idx = 0;
3068 else
3070 if (section_number == SHN_LORESERVE)
3071 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3072 d->rel_idx = section_number++;
3073 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
3076 if (d->rel_hdr2)
3078 if (section_number == SHN_LORESERVE)
3079 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3080 d->rel_idx2 = section_number++;
3081 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
3083 else
3084 d->rel_idx2 = 0;
3087 if (section_number == SHN_LORESERVE)
3088 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3089 t->shstrtab_section = section_number++;
3090 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3091 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
3093 if (bfd_get_symcount (abfd) > 0)
3095 if (section_number == SHN_LORESERVE)
3096 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3097 t->symtab_section = section_number++;
3098 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
3099 if (section_number > SHN_LORESERVE - 2)
3101 if (section_number == SHN_LORESERVE)
3102 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3103 t->symtab_shndx_section = section_number++;
3104 t->symtab_shndx_hdr.sh_name
3105 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3106 ".symtab_shndx", FALSE);
3107 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
3108 return FALSE;
3110 if (section_number == SHN_LORESERVE)
3111 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3112 t->strtab_section = section_number++;
3113 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
3116 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
3117 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3119 elf_numsections (abfd) = section_number;
3120 elf_elfheader (abfd)->e_shnum = section_number;
3121 if (section_number > SHN_LORESERVE)
3122 elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE;
3124 /* Set up the list of section header pointers, in agreement with the
3125 indices. */
3126 i_shdrp = bfd_zalloc2 (abfd, section_number, sizeof (Elf_Internal_Shdr *));
3127 if (i_shdrp == NULL)
3128 return FALSE;
3130 i_shdrp[0] = bfd_zalloc (abfd, sizeof (Elf_Internal_Shdr));
3131 if (i_shdrp[0] == NULL)
3133 bfd_release (abfd, i_shdrp);
3134 return FALSE;
3137 elf_elfsections (abfd) = i_shdrp;
3139 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
3140 if (bfd_get_symcount (abfd) > 0)
3142 i_shdrp[t->symtab_section] = &t->symtab_hdr;
3143 if (elf_numsections (abfd) > SHN_LORESERVE)
3145 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
3146 t->symtab_shndx_hdr.sh_link = t->symtab_section;
3148 i_shdrp[t->strtab_section] = &t->strtab_hdr;
3149 t->symtab_hdr.sh_link = t->strtab_section;
3152 for (sec = abfd->sections; sec; sec = sec->next)
3154 struct bfd_elf_section_data *d = elf_section_data (sec);
3155 asection *s;
3156 const char *name;
3158 i_shdrp[d->this_idx] = &d->this_hdr;
3159 if (d->rel_idx != 0)
3160 i_shdrp[d->rel_idx] = &d->rel_hdr;
3161 if (d->rel_idx2 != 0)
3162 i_shdrp[d->rel_idx2] = d->rel_hdr2;
3164 /* Fill in the sh_link and sh_info fields while we're at it. */
3166 /* sh_link of a reloc section is the section index of the symbol
3167 table. sh_info is the section index of the section to which
3168 the relocation entries apply. */
3169 if (d->rel_idx != 0)
3171 d->rel_hdr.sh_link = t->symtab_section;
3172 d->rel_hdr.sh_info = d->this_idx;
3174 if (d->rel_idx2 != 0)
3176 d->rel_hdr2->sh_link = t->symtab_section;
3177 d->rel_hdr2->sh_info = d->this_idx;
3180 /* We need to set up sh_link for SHF_LINK_ORDER. */
3181 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3183 s = elf_linked_to_section (sec);
3184 if (s)
3186 /* elf_linked_to_section points to the input section. */
3187 if (link_info != NULL)
3189 /* Check discarded linkonce section. */
3190 if (elf_discarded_section (s))
3192 asection *kept;
3193 (*_bfd_error_handler)
3194 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3195 abfd, d->this_hdr.bfd_section,
3196 s, s->owner);
3197 /* Point to the kept section if it has the same
3198 size as the discarded one. */
3199 kept = _bfd_elf_check_kept_section (s, link_info);
3200 if (kept == NULL)
3202 bfd_set_error (bfd_error_bad_value);
3203 return FALSE;
3205 s = kept;
3208 s = s->output_section;
3209 BFD_ASSERT (s != NULL);
3211 else
3213 /* Handle objcopy. */
3214 if (s->output_section == NULL)
3216 (*_bfd_error_handler)
3217 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3218 abfd, d->this_hdr.bfd_section, s, s->owner);
3219 bfd_set_error (bfd_error_bad_value);
3220 return FALSE;
3222 s = s->output_section;
3224 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3226 else
3228 /* PR 290:
3229 The Intel C compiler generates SHT_IA_64_UNWIND with
3230 SHF_LINK_ORDER. But it doesn't set the sh_link or
3231 sh_info fields. Hence we could get the situation
3232 where s is NULL. */
3233 const struct elf_backend_data *bed
3234 = get_elf_backend_data (abfd);
3235 if (bed->link_order_error_handler)
3236 bed->link_order_error_handler
3237 (_("%B: warning: sh_link not set for section `%A'"),
3238 abfd, sec);
3242 switch (d->this_hdr.sh_type)
3244 case SHT_REL:
3245 case SHT_RELA:
3246 /* A reloc section which we are treating as a normal BFD
3247 section. sh_link is the section index of the symbol
3248 table. sh_info is the section index of the section to
3249 which the relocation entries apply. We assume that an
3250 allocated reloc section uses the dynamic symbol table.
3251 FIXME: How can we be sure? */
3252 s = bfd_get_section_by_name (abfd, ".dynsym");
3253 if (s != NULL)
3254 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3256 /* We look up the section the relocs apply to by name. */
3257 name = sec->name;
3258 if (d->this_hdr.sh_type == SHT_REL)
3259 name += 4;
3260 else
3261 name += 5;
3262 s = bfd_get_section_by_name (abfd, name);
3263 if (s != NULL)
3264 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3265 break;
3267 case SHT_STRTAB:
3268 /* We assume that a section named .stab*str is a stabs
3269 string section. We look for a section with the same name
3270 but without the trailing ``str'', and set its sh_link
3271 field to point to this section. */
3272 if (CONST_STRNEQ (sec->name, ".stab")
3273 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3275 size_t len;
3276 char *alc;
3278 len = strlen (sec->name);
3279 alc = bfd_malloc (len - 2);
3280 if (alc == NULL)
3281 return FALSE;
3282 memcpy (alc, sec->name, len - 3);
3283 alc[len - 3] = '\0';
3284 s = bfd_get_section_by_name (abfd, alc);
3285 free (alc);
3286 if (s != NULL)
3288 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3290 /* This is a .stab section. */
3291 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3292 elf_section_data (s)->this_hdr.sh_entsize
3293 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3296 break;
3298 case SHT_DYNAMIC:
3299 case SHT_DYNSYM:
3300 case SHT_GNU_verneed:
3301 case SHT_GNU_verdef:
3302 /* sh_link is the section header index of the string table
3303 used for the dynamic entries, or the symbol table, or the
3304 version strings. */
3305 s = bfd_get_section_by_name (abfd, ".dynstr");
3306 if (s != NULL)
3307 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3308 break;
3310 case SHT_GNU_LIBLIST:
3311 /* sh_link is the section header index of the prelink library
3312 list
3313 used for the dynamic entries, or the symbol table, or the
3314 version strings. */
3315 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3316 ? ".dynstr" : ".gnu.libstr");
3317 if (s != NULL)
3318 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3319 break;
3321 case SHT_HASH:
3322 case SHT_GNU_HASH:
3323 case SHT_GNU_versym:
3324 /* sh_link is the section header index of the symbol table
3325 this hash table or version table is for. */
3326 s = bfd_get_section_by_name (abfd, ".dynsym");
3327 if (s != NULL)
3328 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3329 break;
3331 case SHT_GROUP:
3332 d->this_hdr.sh_link = t->symtab_section;
3336 for (secn = 1; secn < section_number; ++secn)
3337 if (i_shdrp[secn] == NULL)
3338 i_shdrp[secn] = i_shdrp[0];
3339 else
3340 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
3341 i_shdrp[secn]->sh_name);
3342 return TRUE;
3345 /* Map symbol from it's internal number to the external number, moving
3346 all local symbols to be at the head of the list. */
3348 static bfd_boolean
3349 sym_is_global (bfd *abfd, asymbol *sym)
3351 /* If the backend has a special mapping, use it. */
3352 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3353 if (bed->elf_backend_sym_is_global)
3354 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3356 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
3357 || bfd_is_und_section (bfd_get_section (sym))
3358 || bfd_is_com_section (bfd_get_section (sym)));
3361 /* Don't output section symbols for sections that are not going to be
3362 output. Also, don't output section symbols for reloc and other
3363 special sections. */
3365 static bfd_boolean
3366 ignore_section_sym (bfd *abfd, asymbol *sym)
3368 return ((sym->flags & BSF_SECTION_SYM) != 0
3369 && (sym->value != 0
3370 || (sym->section->owner != abfd
3371 && (sym->section->output_section->owner != abfd
3372 || sym->section->output_offset != 0))));
3375 static bfd_boolean
3376 elf_map_symbols (bfd *abfd)
3378 unsigned int symcount = bfd_get_symcount (abfd);
3379 asymbol **syms = bfd_get_outsymbols (abfd);
3380 asymbol **sect_syms;
3381 unsigned int num_locals = 0;
3382 unsigned int num_globals = 0;
3383 unsigned int num_locals2 = 0;
3384 unsigned int num_globals2 = 0;
3385 int max_index = 0;
3386 unsigned int idx;
3387 asection *asect;
3388 asymbol **new_syms;
3390 #ifdef DEBUG
3391 fprintf (stderr, "elf_map_symbols\n");
3392 fflush (stderr);
3393 #endif
3395 for (asect = abfd->sections; asect; asect = asect->next)
3397 if (max_index < asect->index)
3398 max_index = asect->index;
3401 max_index++;
3402 sect_syms = bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
3403 if (sect_syms == NULL)
3404 return FALSE;
3405 elf_section_syms (abfd) = sect_syms;
3406 elf_num_section_syms (abfd) = max_index;
3408 /* Init sect_syms entries for any section symbols we have already
3409 decided to output. */
3410 for (idx = 0; idx < symcount; idx++)
3412 asymbol *sym = syms[idx];
3414 if ((sym->flags & BSF_SECTION_SYM) != 0
3415 && !ignore_section_sym (abfd, sym))
3417 asection *sec = sym->section;
3419 if (sec->owner != abfd)
3420 sec = sec->output_section;
3422 sect_syms[sec->index] = syms[idx];
3426 /* Classify all of the symbols. */
3427 for (idx = 0; idx < symcount; idx++)
3429 if (ignore_section_sym (abfd, syms[idx]))
3430 continue;
3431 if (!sym_is_global (abfd, syms[idx]))
3432 num_locals++;
3433 else
3434 num_globals++;
3437 /* We will be adding a section symbol for each normal BFD section. Most
3438 sections will already have a section symbol in outsymbols, but
3439 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3440 at least in that case. */
3441 for (asect = abfd->sections; asect; asect = asect->next)
3443 if (sect_syms[asect->index] == NULL)
3445 if (!sym_is_global (abfd, asect->symbol))
3446 num_locals++;
3447 else
3448 num_globals++;
3452 /* Now sort the symbols so the local symbols are first. */
3453 new_syms = bfd_alloc2 (abfd, num_locals + num_globals, sizeof (asymbol *));
3455 if (new_syms == NULL)
3456 return FALSE;
3458 for (idx = 0; idx < symcount; idx++)
3460 asymbol *sym = syms[idx];
3461 unsigned int i;
3463 if (ignore_section_sym (abfd, sym))
3464 continue;
3465 if (!sym_is_global (abfd, sym))
3466 i = num_locals2++;
3467 else
3468 i = num_locals + num_globals2++;
3469 new_syms[i] = sym;
3470 sym->udata.i = i + 1;
3472 for (asect = abfd->sections; asect; asect = asect->next)
3474 if (sect_syms[asect->index] == NULL)
3476 asymbol *sym = asect->symbol;
3477 unsigned int i;
3479 sect_syms[asect->index] = sym;
3480 if (!sym_is_global (abfd, sym))
3481 i = num_locals2++;
3482 else
3483 i = num_locals + num_globals2++;
3484 new_syms[i] = sym;
3485 sym->udata.i = i + 1;
3489 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3491 elf_num_locals (abfd) = num_locals;
3492 elf_num_globals (abfd) = num_globals;
3493 return TRUE;
3496 /* Align to the maximum file alignment that could be required for any
3497 ELF data structure. */
3499 static inline file_ptr
3500 align_file_position (file_ptr off, int align)
3502 return (off + align - 1) & ~(align - 1);
3505 /* Assign a file position to a section, optionally aligning to the
3506 required section alignment. */
3508 file_ptr
3509 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3510 file_ptr offset,
3511 bfd_boolean align)
3513 if (align)
3515 unsigned int al;
3517 al = i_shdrp->sh_addralign;
3518 if (al > 1)
3519 offset = BFD_ALIGN (offset, al);
3521 i_shdrp->sh_offset = offset;
3522 if (i_shdrp->bfd_section != NULL)
3523 i_shdrp->bfd_section->filepos = offset;
3524 if (i_shdrp->sh_type != SHT_NOBITS)
3525 offset += i_shdrp->sh_size;
3526 return offset;
3529 /* Compute the file positions we are going to put the sections at, and
3530 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3531 is not NULL, this is being called by the ELF backend linker. */
3533 bfd_boolean
3534 _bfd_elf_compute_section_file_positions (bfd *abfd,
3535 struct bfd_link_info *link_info)
3537 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3538 bfd_boolean failed;
3539 struct bfd_strtab_hash *strtab = NULL;
3540 Elf_Internal_Shdr *shstrtab_hdr;
3542 if (abfd->output_has_begun)
3543 return TRUE;
3545 /* Do any elf backend specific processing first. */
3546 if (bed->elf_backend_begin_write_processing)
3547 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3549 if (! prep_headers (abfd))
3550 return FALSE;
3552 /* Post process the headers if necessary. */
3553 if (bed->elf_backend_post_process_headers)
3554 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3556 failed = FALSE;
3557 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3558 if (failed)
3559 return FALSE;
3561 if (!assign_section_numbers (abfd, link_info))
3562 return FALSE;
3564 /* The backend linker builds symbol table information itself. */
3565 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3567 /* Non-zero if doing a relocatable link. */
3568 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3570 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3571 return FALSE;
3574 if (link_info == NULL)
3576 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3577 if (failed)
3578 return FALSE;
3581 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3582 /* sh_name was set in prep_headers. */
3583 shstrtab_hdr->sh_type = SHT_STRTAB;
3584 shstrtab_hdr->sh_flags = 0;
3585 shstrtab_hdr->sh_addr = 0;
3586 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3587 shstrtab_hdr->sh_entsize = 0;
3588 shstrtab_hdr->sh_link = 0;
3589 shstrtab_hdr->sh_info = 0;
3590 /* sh_offset is set in assign_file_positions_except_relocs. */
3591 shstrtab_hdr->sh_addralign = 1;
3593 if (!assign_file_positions_except_relocs (abfd, link_info))
3594 return FALSE;
3596 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3598 file_ptr off;
3599 Elf_Internal_Shdr *hdr;
3601 off = elf_tdata (abfd)->next_file_pos;
3603 hdr = &elf_tdata (abfd)->symtab_hdr;
3604 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3606 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3607 if (hdr->sh_size != 0)
3608 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3610 hdr = &elf_tdata (abfd)->strtab_hdr;
3611 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3613 elf_tdata (abfd)->next_file_pos = off;
3615 /* Now that we know where the .strtab section goes, write it
3616 out. */
3617 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3618 || ! _bfd_stringtab_emit (abfd, strtab))
3619 return FALSE;
3620 _bfd_stringtab_free (strtab);
3623 abfd->output_has_begun = TRUE;
3625 return TRUE;
3628 /* Make an initial estimate of the size of the program header. If we
3629 get the number wrong here, we'll redo section placement. */
3631 static bfd_size_type
3632 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
3634 size_t segs;
3635 asection *s;
3636 const struct elf_backend_data *bed;
3638 /* Assume we will need exactly two PT_LOAD segments: one for text
3639 and one for data. */
3640 segs = 2;
3642 s = bfd_get_section_by_name (abfd, ".interp");
3643 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3645 /* If we have a loadable interpreter section, we need a
3646 PT_INTERP segment. In this case, assume we also need a
3647 PT_PHDR segment, although that may not be true for all
3648 targets. */
3649 segs += 2;
3652 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3654 /* We need a PT_DYNAMIC segment. */
3655 ++segs;
3657 if (elf_tdata (abfd)->relro)
3659 /* We need a PT_GNU_RELRO segment only when there is a
3660 PT_DYNAMIC segment. */
3661 ++segs;
3665 if (elf_tdata (abfd)->eh_frame_hdr)
3667 /* We need a PT_GNU_EH_FRAME segment. */
3668 ++segs;
3671 if (elf_tdata (abfd)->stack_flags)
3673 /* We need a PT_GNU_STACK segment. */
3674 ++segs;
3677 for (s = abfd->sections; s != NULL; s = s->next)
3679 if ((s->flags & SEC_LOAD) != 0
3680 && CONST_STRNEQ (s->name, ".note"))
3682 /* We need a PT_NOTE segment. */
3683 ++segs;
3687 for (s = abfd->sections; s != NULL; s = s->next)
3689 if (s->flags & SEC_THREAD_LOCAL)
3691 /* We need a PT_TLS segment. */
3692 ++segs;
3693 break;
3697 /* Let the backend count up any program headers it might need. */
3698 bed = get_elf_backend_data (abfd);
3699 if (bed->elf_backend_additional_program_headers)
3701 int a;
3703 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
3704 if (a == -1)
3705 abort ();
3706 segs += a;
3709 return segs * bed->s->sizeof_phdr;
3712 /* Create a mapping from a set of sections to a program segment. */
3714 static struct elf_segment_map *
3715 make_mapping (bfd *abfd,
3716 asection **sections,
3717 unsigned int from,
3718 unsigned int to,
3719 bfd_boolean phdr)
3721 struct elf_segment_map *m;
3722 unsigned int i;
3723 asection **hdrpp;
3724 bfd_size_type amt;
3726 amt = sizeof (struct elf_segment_map);
3727 amt += (to - from - 1) * sizeof (asection *);
3728 m = bfd_zalloc (abfd, amt);
3729 if (m == NULL)
3730 return NULL;
3731 m->next = NULL;
3732 m->p_type = PT_LOAD;
3733 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3734 m->sections[i - from] = *hdrpp;
3735 m->count = to - from;
3737 if (from == 0 && phdr)
3739 /* Include the headers in the first PT_LOAD segment. */
3740 m->includes_filehdr = 1;
3741 m->includes_phdrs = 1;
3744 return m;
3747 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3748 on failure. */
3750 struct elf_segment_map *
3751 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3753 struct elf_segment_map *m;
3755 m = bfd_zalloc (abfd, sizeof (struct elf_segment_map));
3756 if (m == NULL)
3757 return NULL;
3758 m->next = NULL;
3759 m->p_type = PT_DYNAMIC;
3760 m->count = 1;
3761 m->sections[0] = dynsec;
3763 return m;
3766 /* Possibly add or remove segments from the segment map. */
3768 static bfd_boolean
3769 elf_modify_segment_map (bfd *abfd, struct bfd_link_info *info)
3771 struct elf_segment_map **m;
3772 const struct elf_backend_data *bed;
3774 /* The placement algorithm assumes that non allocated sections are
3775 not in PT_LOAD segments. We ensure this here by removing such
3776 sections from the segment map. We also remove excluded
3777 sections. Finally, any PT_LOAD segment without sections is
3778 removed. */
3779 m = &elf_tdata (abfd)->segment_map;
3780 while (*m)
3782 unsigned int i, new_count;
3784 for (new_count = 0, i = 0; i < (*m)->count; i++)
3786 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
3787 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
3788 || (*m)->p_type != PT_LOAD))
3790 (*m)->sections[new_count] = (*m)->sections[i];
3791 new_count++;
3794 (*m)->count = new_count;
3796 if ((*m)->p_type == PT_LOAD && (*m)->count == 0)
3797 *m = (*m)->next;
3798 else
3799 m = &(*m)->next;
3802 bed = get_elf_backend_data (abfd);
3803 if (bed->elf_backend_modify_segment_map != NULL)
3805 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
3806 return FALSE;
3809 return TRUE;
3812 /* Set up a mapping from BFD sections to program segments. */
3814 bfd_boolean
3815 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
3817 unsigned int count;
3818 struct elf_segment_map *m;
3819 asection **sections = NULL;
3820 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3822 if (elf_tdata (abfd)->segment_map == NULL
3823 && bfd_count_sections (abfd) != 0)
3825 asection *s;
3826 unsigned int i;
3827 struct elf_segment_map *mfirst;
3828 struct elf_segment_map **pm;
3829 asection *last_hdr;
3830 bfd_vma last_size;
3831 unsigned int phdr_index;
3832 bfd_vma maxpagesize;
3833 asection **hdrpp;
3834 bfd_boolean phdr_in_segment = TRUE;
3835 bfd_boolean writable;
3836 int tls_count = 0;
3837 asection *first_tls = NULL;
3838 asection *dynsec, *eh_frame_hdr;
3839 bfd_size_type amt;
3841 /* Select the allocated sections, and sort them. */
3843 sections = bfd_malloc2 (bfd_count_sections (abfd), sizeof (asection *));
3844 if (sections == NULL)
3845 goto error_return;
3847 i = 0;
3848 for (s = abfd->sections; s != NULL; s = s->next)
3850 if ((s->flags & SEC_ALLOC) != 0)
3852 sections[i] = s;
3853 ++i;
3856 BFD_ASSERT (i <= bfd_count_sections (abfd));
3857 count = i;
3859 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3861 /* Build the mapping. */
3863 mfirst = NULL;
3864 pm = &mfirst;
3866 /* If we have a .interp section, then create a PT_PHDR segment for
3867 the program headers and a PT_INTERP segment for the .interp
3868 section. */
3869 s = bfd_get_section_by_name (abfd, ".interp");
3870 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3872 amt = sizeof (struct elf_segment_map);
3873 m = bfd_zalloc (abfd, amt);
3874 if (m == NULL)
3875 goto error_return;
3876 m->next = NULL;
3877 m->p_type = PT_PHDR;
3878 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3879 m->p_flags = PF_R | PF_X;
3880 m->p_flags_valid = 1;
3881 m->includes_phdrs = 1;
3883 *pm = m;
3884 pm = &m->next;
3886 amt = sizeof (struct elf_segment_map);
3887 m = bfd_zalloc (abfd, amt);
3888 if (m == NULL)
3889 goto error_return;
3890 m->next = NULL;
3891 m->p_type = PT_INTERP;
3892 m->count = 1;
3893 m->sections[0] = s;
3895 *pm = m;
3896 pm = &m->next;
3899 /* Look through the sections. We put sections in the same program
3900 segment when the start of the second section can be placed within
3901 a few bytes of the end of the first section. */
3902 last_hdr = NULL;
3903 last_size = 0;
3904 phdr_index = 0;
3905 maxpagesize = bed->maxpagesize;
3906 writable = FALSE;
3907 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3908 if (dynsec != NULL
3909 && (dynsec->flags & SEC_LOAD) == 0)
3910 dynsec = NULL;
3912 /* Deal with -Ttext or something similar such that the first section
3913 is not adjacent to the program headers. This is an
3914 approximation, since at this point we don't know exactly how many
3915 program headers we will need. */
3916 if (count > 0)
3918 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
3920 if (phdr_size == (bfd_size_type) -1)
3921 phdr_size = get_program_header_size (abfd, info);
3922 if ((abfd->flags & D_PAGED) == 0
3923 || sections[0]->lma < phdr_size
3924 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3925 phdr_in_segment = FALSE;
3928 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3930 asection *hdr;
3931 bfd_boolean new_segment;
3933 hdr = *hdrpp;
3935 /* See if this section and the last one will fit in the same
3936 segment. */
3938 if (last_hdr == NULL)
3940 /* If we don't have a segment yet, then we don't need a new
3941 one (we build the last one after this loop). */
3942 new_segment = FALSE;
3944 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3946 /* If this section has a different relation between the
3947 virtual address and the load address, then we need a new
3948 segment. */
3949 new_segment = TRUE;
3951 else if (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
3952 < BFD_ALIGN (hdr->lma, maxpagesize))
3954 /* If putting this section in this segment would force us to
3955 skip a page in the segment, then we need a new segment. */
3956 new_segment = TRUE;
3958 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3959 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
3961 /* We don't want to put a loadable section after a
3962 nonloadable section in the same segment.
3963 Consider .tbss sections as loadable for this purpose. */
3964 new_segment = TRUE;
3966 else if ((abfd->flags & D_PAGED) == 0)
3968 /* If the file is not demand paged, which means that we
3969 don't require the sections to be correctly aligned in the
3970 file, then there is no other reason for a new segment. */
3971 new_segment = FALSE;
3973 else if (! writable
3974 && (hdr->flags & SEC_READONLY) == 0
3975 && (((last_hdr->lma + last_size - 1)
3976 & ~(maxpagesize - 1))
3977 != (hdr->lma & ~(maxpagesize - 1))))
3979 /* We don't want to put a writable section in a read only
3980 segment, unless they are on the same page in memory
3981 anyhow. We already know that the last section does not
3982 bring us past the current section on the page, so the
3983 only case in which the new section is not on the same
3984 page as the previous section is when the previous section
3985 ends precisely on a page boundary. */
3986 new_segment = TRUE;
3988 else
3990 /* Otherwise, we can use the same segment. */
3991 new_segment = FALSE;
3994 /* Allow interested parties a chance to override our decision. */
3995 if (last_hdr && info->callbacks->override_segment_assignment)
3996 new_segment = info->callbacks->override_segment_assignment (info, abfd, hdr, last_hdr, new_segment);
3998 if (! new_segment)
4000 if ((hdr->flags & SEC_READONLY) == 0)
4001 writable = TRUE;
4002 last_hdr = hdr;
4003 /* .tbss sections effectively have zero size. */
4004 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
4005 != SEC_THREAD_LOCAL)
4006 last_size = hdr->size;
4007 else
4008 last_size = 0;
4009 continue;
4012 /* We need a new program segment. We must create a new program
4013 header holding all the sections from phdr_index until hdr. */
4015 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
4016 if (m == NULL)
4017 goto error_return;
4019 *pm = m;
4020 pm = &m->next;
4022 if ((hdr->flags & SEC_READONLY) == 0)
4023 writable = TRUE;
4024 else
4025 writable = FALSE;
4027 last_hdr = hdr;
4028 /* .tbss sections effectively have zero size. */
4029 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
4030 last_size = hdr->size;
4031 else
4032 last_size = 0;
4033 phdr_index = i;
4034 phdr_in_segment = FALSE;
4037 /* Create a final PT_LOAD program segment. */
4038 if (last_hdr != NULL)
4040 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
4041 if (m == NULL)
4042 goto error_return;
4044 *pm = m;
4045 pm = &m->next;
4048 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4049 if (dynsec != NULL)
4051 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4052 if (m == NULL)
4053 goto error_return;
4054 *pm = m;
4055 pm = &m->next;
4058 /* For each loadable .note section, add a PT_NOTE segment. We don't
4059 use bfd_get_section_by_name, because if we link together
4060 nonloadable .note sections and loadable .note sections, we will
4061 generate two .note sections in the output file. FIXME: Using
4062 names for section types is bogus anyhow. */
4063 for (s = abfd->sections; s != NULL; s = s->next)
4065 if ((s->flags & SEC_LOAD) != 0
4066 && CONST_STRNEQ (s->name, ".note"))
4068 amt = sizeof (struct elf_segment_map);
4069 m = bfd_zalloc (abfd, amt);
4070 if (m == NULL)
4071 goto error_return;
4072 m->next = NULL;
4073 m->p_type = PT_NOTE;
4074 m->count = 1;
4075 m->sections[0] = s;
4077 *pm = m;
4078 pm = &m->next;
4080 if (s->flags & SEC_THREAD_LOCAL)
4082 if (! tls_count)
4083 first_tls = s;
4084 tls_count++;
4088 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4089 if (tls_count > 0)
4091 int i;
4093 amt = sizeof (struct elf_segment_map);
4094 amt += (tls_count - 1) * sizeof (asection *);
4095 m = bfd_zalloc (abfd, amt);
4096 if (m == NULL)
4097 goto error_return;
4098 m->next = NULL;
4099 m->p_type = PT_TLS;
4100 m->count = tls_count;
4101 /* Mandated PF_R. */
4102 m->p_flags = PF_R;
4103 m->p_flags_valid = 1;
4104 for (i = 0; i < tls_count; ++i)
4106 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
4107 m->sections[i] = first_tls;
4108 first_tls = first_tls->next;
4111 *pm = m;
4112 pm = &m->next;
4115 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4116 segment. */
4117 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
4118 if (eh_frame_hdr != NULL
4119 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
4121 amt = sizeof (struct elf_segment_map);
4122 m = bfd_zalloc (abfd, amt);
4123 if (m == NULL)
4124 goto error_return;
4125 m->next = NULL;
4126 m->p_type = PT_GNU_EH_FRAME;
4127 m->count = 1;
4128 m->sections[0] = eh_frame_hdr->output_section;
4130 *pm = m;
4131 pm = &m->next;
4134 if (elf_tdata (abfd)->stack_flags)
4136 amt = sizeof (struct elf_segment_map);
4137 m = bfd_zalloc (abfd, amt);
4138 if (m == NULL)
4139 goto error_return;
4140 m->next = NULL;
4141 m->p_type = PT_GNU_STACK;
4142 m->p_flags = elf_tdata (abfd)->stack_flags;
4143 m->p_flags_valid = 1;
4145 *pm = m;
4146 pm = &m->next;
4149 if (dynsec != NULL && elf_tdata (abfd)->relro)
4151 /* We make a PT_GNU_RELRO segment only when there is a
4152 PT_DYNAMIC segment. */
4153 amt = sizeof (struct elf_segment_map);
4154 m = bfd_zalloc (abfd, amt);
4155 if (m == NULL)
4156 goto error_return;
4157 m->next = NULL;
4158 m->p_type = PT_GNU_RELRO;
4159 m->p_flags = PF_R;
4160 m->p_flags_valid = 1;
4162 *pm = m;
4163 pm = &m->next;
4166 free (sections);
4167 elf_tdata (abfd)->segment_map = mfirst;
4170 if (!elf_modify_segment_map (abfd, info))
4171 return FALSE;
4173 for (count = 0, m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4174 ++count;
4175 elf_tdata (abfd)->program_header_size = count * bed->s->sizeof_phdr;
4177 return TRUE;
4179 error_return:
4180 if (sections != NULL)
4181 free (sections);
4182 return FALSE;
4185 /* Sort sections by address. */
4187 static int
4188 elf_sort_sections (const void *arg1, const void *arg2)
4190 const asection *sec1 = *(const asection **) arg1;
4191 const asection *sec2 = *(const asection **) arg2;
4192 bfd_size_type size1, size2;
4194 /* Sort by LMA first, since this is the address used to
4195 place the section into a segment. */
4196 if (sec1->lma < sec2->lma)
4197 return -1;
4198 else if (sec1->lma > sec2->lma)
4199 return 1;
4201 /* Then sort by VMA. Normally the LMA and the VMA will be
4202 the same, and this will do nothing. */
4203 if (sec1->vma < sec2->vma)
4204 return -1;
4205 else if (sec1->vma > sec2->vma)
4206 return 1;
4208 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4210 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4212 if (TOEND (sec1))
4214 if (TOEND (sec2))
4216 /* If the indicies are the same, do not return 0
4217 here, but continue to try the next comparison. */
4218 if (sec1->target_index - sec2->target_index != 0)
4219 return sec1->target_index - sec2->target_index;
4221 else
4222 return 1;
4224 else if (TOEND (sec2))
4225 return -1;
4227 #undef TOEND
4229 /* Sort by size, to put zero sized sections
4230 before others at the same address. */
4232 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
4233 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
4235 if (size1 < size2)
4236 return -1;
4237 if (size1 > size2)
4238 return 1;
4240 return sec1->target_index - sec2->target_index;
4243 /* Ian Lance Taylor writes:
4245 We shouldn't be using % with a negative signed number. That's just
4246 not good. We have to make sure either that the number is not
4247 negative, or that the number has an unsigned type. When the types
4248 are all the same size they wind up as unsigned. When file_ptr is a
4249 larger signed type, the arithmetic winds up as signed long long,
4250 which is wrong.
4252 What we're trying to say here is something like ``increase OFF by
4253 the least amount that will cause it to be equal to the VMA modulo
4254 the page size.'' */
4255 /* In other words, something like:
4257 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4258 off_offset = off % bed->maxpagesize;
4259 if (vma_offset < off_offset)
4260 adjustment = vma_offset + bed->maxpagesize - off_offset;
4261 else
4262 adjustment = vma_offset - off_offset;
4264 which can can be collapsed into the expression below. */
4266 static file_ptr
4267 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
4269 return ((vma - off) % maxpagesize);
4272 /* Assign file positions to the sections based on the mapping from
4273 sections to segments. This function also sets up some fields in
4274 the file header. */
4276 static bfd_boolean
4277 assign_file_positions_for_load_sections (bfd *abfd,
4278 struct bfd_link_info *link_info)
4280 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4281 struct elf_segment_map *m;
4282 Elf_Internal_Phdr *phdrs;
4283 Elf_Internal_Phdr *p;
4284 file_ptr off;
4285 bfd_size_type maxpagesize;
4286 unsigned int alloc;
4287 unsigned int i, j;
4289 if (link_info == NULL
4290 && !elf_modify_segment_map (abfd, link_info))
4291 return FALSE;
4293 alloc = 0;
4294 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4295 ++alloc;
4297 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
4298 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
4299 elf_elfheader (abfd)->e_phnum = alloc;
4301 if (elf_tdata (abfd)->program_header_size == (bfd_size_type) -1)
4302 elf_tdata (abfd)->program_header_size = alloc * bed->s->sizeof_phdr;
4303 else
4304 BFD_ASSERT (elf_tdata (abfd)->program_header_size
4305 >= alloc * bed->s->sizeof_phdr);
4307 if (alloc == 0)
4309 elf_tdata (abfd)->next_file_pos = bed->s->sizeof_ehdr;
4310 return TRUE;
4313 phdrs = bfd_alloc2 (abfd, alloc, sizeof (Elf_Internal_Phdr));
4314 elf_tdata (abfd)->phdr = phdrs;
4315 if (phdrs == NULL)
4316 return FALSE;
4318 maxpagesize = 1;
4319 if ((abfd->flags & D_PAGED) != 0)
4320 maxpagesize = bed->maxpagesize;
4322 off = bed->s->sizeof_ehdr;
4323 off += alloc * bed->s->sizeof_phdr;
4325 for (m = elf_tdata (abfd)->segment_map, p = phdrs, j = 0;
4326 m != NULL;
4327 m = m->next, p++, j++)
4329 asection **secpp;
4331 /* If elf_segment_map is not from map_sections_to_segments, the
4332 sections may not be correctly ordered. NOTE: sorting should
4333 not be done to the PT_NOTE section of a corefile, which may
4334 contain several pseudo-sections artificially created by bfd.
4335 Sorting these pseudo-sections breaks things badly. */
4336 if (m->count > 1
4337 && !(elf_elfheader (abfd)->e_type == ET_CORE
4338 && m->p_type == PT_NOTE))
4339 qsort (m->sections, (size_t) m->count, sizeof (asection *),
4340 elf_sort_sections);
4342 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4343 number of sections with contents contributing to both p_filesz
4344 and p_memsz, followed by a number of sections with no contents
4345 that just contribute to p_memsz. In this loop, OFF tracks next
4346 available file offset for PT_LOAD and PT_NOTE segments. */
4347 p->p_type = m->p_type;
4348 p->p_flags = m->p_flags;
4350 if (m->count == 0)
4351 p->p_vaddr = 0;
4352 else
4353 p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
4355 if (m->p_paddr_valid)
4356 p->p_paddr = m->p_paddr;
4357 else if (m->count == 0)
4358 p->p_paddr = 0;
4359 else
4360 p->p_paddr = m->sections[0]->lma;
4362 if (p->p_type == PT_LOAD
4363 && (abfd->flags & D_PAGED) != 0)
4365 /* p_align in demand paged PT_LOAD segments effectively stores
4366 the maximum page size. When copying an executable with
4367 objcopy, we set m->p_align from the input file. Use this
4368 value for maxpagesize rather than bed->maxpagesize, which
4369 may be different. Note that we use maxpagesize for PT_TLS
4370 segment alignment later in this function, so we are relying
4371 on at least one PT_LOAD segment appearing before a PT_TLS
4372 segment. */
4373 if (m->p_align_valid)
4374 maxpagesize = m->p_align;
4376 p->p_align = maxpagesize;
4378 else if (m->count == 0)
4379 p->p_align = 1 << bed->s->log_file_align;
4380 else if (m->p_align_valid)
4381 p->p_align = m->p_align;
4382 else
4383 p->p_align = 0;
4385 if (p->p_type == PT_LOAD
4386 && m->count > 0)
4388 bfd_size_type align;
4389 bfd_vma adjust;
4390 unsigned int align_power = 0;
4392 if (m->p_align_valid)
4393 align = p->p_align;
4394 else
4396 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4398 unsigned int secalign;
4400 secalign = bfd_get_section_alignment (abfd, *secpp);
4401 if (secalign > align_power)
4402 align_power = secalign;
4404 align = (bfd_size_type) 1 << align_power;
4405 if (align < maxpagesize)
4406 align = maxpagesize;
4409 for (i = 0; i < m->count; i++)
4410 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
4411 /* If we aren't making room for this section, then
4412 it must be SHT_NOBITS regardless of what we've
4413 set via struct bfd_elf_special_section. */
4414 elf_section_type (m->sections[i]) = SHT_NOBITS;
4416 adjust = vma_page_aligned_bias (m->sections[0]->vma, off, align);
4417 if (adjust != 0)
4419 /* If the first section isn't loadable, the same holds
4420 for any other sections. We don't need to align the
4421 segment on disk since the segment doesn't need file
4422 space. */
4423 i = 0;
4424 while (elf_section_type (m->sections[i]) == SHT_NOBITS)
4426 /* If a segment starts with .tbss, we need to look
4427 at the next section to decide whether the segment
4428 has any loadable sections. */
4429 if ((elf_section_flags (m->sections[i]) & SHF_TLS) == 0
4430 || ++i >= m->count)
4432 adjust = 0;
4433 break;
4436 off += adjust;
4439 /* Make sure the .dynamic section is the first section in the
4440 PT_DYNAMIC segment. */
4441 else if (p->p_type == PT_DYNAMIC
4442 && m->count > 1
4443 && strcmp (m->sections[0]->name, ".dynamic") != 0)
4445 _bfd_error_handler
4446 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4447 abfd);
4448 bfd_set_error (bfd_error_bad_value);
4449 return FALSE;
4452 p->p_offset = 0;
4453 p->p_filesz = 0;
4454 p->p_memsz = 0;
4456 if (m->includes_filehdr)
4458 if (! m->p_flags_valid)
4459 p->p_flags |= PF_R;
4460 p->p_filesz = bed->s->sizeof_ehdr;
4461 p->p_memsz = bed->s->sizeof_ehdr;
4462 if (m->count > 0)
4464 BFD_ASSERT (p->p_type == PT_LOAD);
4466 if (p->p_vaddr < (bfd_vma) off)
4468 (*_bfd_error_handler)
4469 (_("%B: Not enough room for program headers, try linking with -N"),
4470 abfd);
4471 bfd_set_error (bfd_error_bad_value);
4472 return FALSE;
4475 p->p_vaddr -= off;
4476 if (! m->p_paddr_valid)
4477 p->p_paddr -= off;
4481 if (m->includes_phdrs)
4483 if (! m->p_flags_valid)
4484 p->p_flags |= PF_R;
4486 if (!m->includes_filehdr)
4488 p->p_offset = bed->s->sizeof_ehdr;
4490 if (m->count > 0)
4492 BFD_ASSERT (p->p_type == PT_LOAD);
4493 p->p_vaddr -= off - p->p_offset;
4494 if (! m->p_paddr_valid)
4495 p->p_paddr -= off - p->p_offset;
4499 p->p_filesz += alloc * bed->s->sizeof_phdr;
4500 p->p_memsz += alloc * bed->s->sizeof_phdr;
4503 if (p->p_type == PT_LOAD
4504 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4506 if (! m->includes_filehdr && ! m->includes_phdrs)
4507 p->p_offset = off;
4508 else
4510 file_ptr adjust;
4512 adjust = off - (p->p_offset + p->p_filesz);
4513 p->p_filesz += adjust;
4514 p->p_memsz += adjust;
4518 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4519 maps. Set filepos for sections in PT_LOAD segments, and in
4520 core files, for sections in PT_NOTE segments.
4521 assign_file_positions_for_non_load_sections will set filepos
4522 for other sections and update p_filesz for other segments. */
4523 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4525 asection *sec;
4526 bfd_size_type align;
4527 Elf_Internal_Shdr *this_hdr;
4529 sec = *secpp;
4530 this_hdr = &elf_section_data (sec)->this_hdr;
4531 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
4533 if (p->p_type == PT_LOAD
4534 || p->p_type == PT_TLS)
4536 bfd_signed_vma adjust = sec->lma - (p->p_paddr + p->p_memsz);
4538 if (this_hdr->sh_type != SHT_NOBITS
4539 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
4540 && ((this_hdr->sh_flags & SHF_TLS) == 0
4541 || p->p_type == PT_TLS)))
4543 if (adjust < 0)
4545 (*_bfd_error_handler)
4546 (_("%B: section %A lma 0x%lx overlaps previous sections"),
4547 abfd, sec, (unsigned long) sec->lma);
4548 adjust = 0;
4550 p->p_memsz += adjust;
4552 if (this_hdr->sh_type != SHT_NOBITS)
4554 off += adjust;
4555 p->p_filesz += adjust;
4560 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4562 /* The section at i == 0 is the one that actually contains
4563 everything. */
4564 if (i == 0)
4566 this_hdr->sh_offset = sec->filepos = off;
4567 off += sec->size;
4568 p->p_filesz = sec->size;
4569 p->p_memsz = 0;
4570 p->p_align = 1;
4572 else
4574 /* The rest are fake sections that shouldn't be written. */
4575 sec->filepos = 0;
4576 sec->size = 0;
4577 sec->flags = 0;
4578 continue;
4581 else
4583 if (p->p_type == PT_LOAD)
4585 this_hdr->sh_offset = sec->filepos = off;
4586 if (this_hdr->sh_type != SHT_NOBITS)
4587 off += sec->size;
4590 if (this_hdr->sh_type != SHT_NOBITS)
4592 p->p_filesz += sec->size;
4593 /* A load section without SHF_ALLOC is something like
4594 a note section in a PT_NOTE segment. These take
4595 file space but are not loaded into memory. */
4596 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4597 p->p_memsz += sec->size;
4600 /* .tbss is special. It doesn't contribute to p_memsz of
4601 normal segments. */
4602 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0
4603 && ((this_hdr->sh_flags & SHF_TLS) == 0
4604 || p->p_type == PT_TLS))
4605 p->p_memsz += sec->size;
4607 if (p->p_type == PT_TLS
4608 && sec->size == 0
4609 && (sec->flags & SEC_HAS_CONTENTS) == 0)
4611 struct bfd_link_order *o = sec->map_tail.link_order;
4612 if (o != NULL)
4613 p->p_memsz += o->offset + o->size;
4616 if (p->p_type == PT_GNU_RELRO)
4617 p->p_align = 1;
4618 else if (align > p->p_align
4619 && !m->p_align_valid
4620 && (p->p_type != PT_LOAD
4621 || (abfd->flags & D_PAGED) == 0))
4622 p->p_align = align;
4625 if (! m->p_flags_valid)
4627 p->p_flags |= PF_R;
4628 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
4629 p->p_flags |= PF_X;
4630 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
4631 p->p_flags |= PF_W;
4635 /* Check that all sections are in a PT_LOAD segment.
4636 Don't check funky gdb generated core files. */
4637 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
4638 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4640 Elf_Internal_Shdr *this_hdr;
4641 asection *sec;
4643 sec = *secpp;
4644 this_hdr = &(elf_section_data(sec)->this_hdr);
4645 if (this_hdr->sh_size != 0
4646 && !ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, p))
4648 (*_bfd_error_handler)
4649 (_("%B: section `%A' can't be allocated in segment %d"),
4650 abfd, sec, j);
4651 bfd_set_error (bfd_error_bad_value);
4652 return FALSE;
4657 elf_tdata (abfd)->next_file_pos = off;
4658 return TRUE;
4661 /* Assign file positions for the other sections. */
4663 static bfd_boolean
4664 assign_file_positions_for_non_load_sections (bfd *abfd,
4665 struct bfd_link_info *link_info)
4667 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4668 Elf_Internal_Shdr **i_shdrpp;
4669 Elf_Internal_Shdr **hdrpp;
4670 Elf_Internal_Phdr *phdrs;
4671 Elf_Internal_Phdr *p;
4672 struct elf_segment_map *m;
4673 bfd_vma filehdr_vaddr, filehdr_paddr;
4674 bfd_vma phdrs_vaddr, phdrs_paddr;
4675 file_ptr off;
4676 unsigned int num_sec;
4677 unsigned int i;
4678 unsigned int count;
4680 i_shdrpp = elf_elfsections (abfd);
4681 num_sec = elf_numsections (abfd);
4682 off = elf_tdata (abfd)->next_file_pos;
4683 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4685 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4686 Elf_Internal_Shdr *hdr;
4688 hdr = *hdrpp;
4689 if (hdr->bfd_section != NULL
4690 && (hdr->bfd_section->filepos != 0
4691 || (hdr->sh_type == SHT_NOBITS
4692 && hdr->contents == NULL)))
4693 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
4694 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4696 if (hdr->sh_size != 0)
4697 ((*_bfd_error_handler)
4698 (_("%B: warning: allocated section `%s' not in segment"),
4699 abfd,
4700 (hdr->bfd_section == NULL
4701 ? "*unknown*"
4702 : hdr->bfd_section->name)));
4703 /* We don't need to page align empty sections. */
4704 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
4705 off += vma_page_aligned_bias (hdr->sh_addr, off,
4706 bed->maxpagesize);
4707 else
4708 off += vma_page_aligned_bias (hdr->sh_addr, off,
4709 hdr->sh_addralign);
4710 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4711 FALSE);
4713 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4714 && hdr->bfd_section == NULL)
4715 || hdr == i_shdrpp[tdata->symtab_section]
4716 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4717 || hdr == i_shdrpp[tdata->strtab_section])
4718 hdr->sh_offset = -1;
4719 else
4720 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4722 if (i == SHN_LORESERVE - 1)
4724 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4725 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4729 /* Now that we have set the section file positions, we can set up
4730 the file positions for the non PT_LOAD segments. */
4731 count = 0;
4732 filehdr_vaddr = 0;
4733 filehdr_paddr = 0;
4734 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
4735 phdrs_paddr = 0;
4736 phdrs = elf_tdata (abfd)->phdr;
4737 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4738 m != NULL;
4739 m = m->next, p++)
4741 ++count;
4742 if (p->p_type != PT_LOAD)
4743 continue;
4745 if (m->includes_filehdr)
4747 filehdr_vaddr = p->p_vaddr;
4748 filehdr_paddr = p->p_paddr;
4750 if (m->includes_phdrs)
4752 phdrs_vaddr = p->p_vaddr;
4753 phdrs_paddr = p->p_paddr;
4754 if (m->includes_filehdr)
4756 phdrs_vaddr += bed->s->sizeof_ehdr;
4757 phdrs_paddr += bed->s->sizeof_ehdr;
4762 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4763 m != NULL;
4764 m = m->next, p++)
4766 if (m->count != 0)
4768 if (p->p_type != PT_LOAD
4769 && (p->p_type != PT_NOTE || bfd_get_format (abfd) != bfd_core))
4771 Elf_Internal_Shdr *hdr;
4772 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
4774 hdr = &elf_section_data (m->sections[m->count - 1])->this_hdr;
4775 p->p_filesz = (m->sections[m->count - 1]->filepos
4776 - m->sections[0]->filepos);
4777 if (hdr->sh_type != SHT_NOBITS)
4778 p->p_filesz += hdr->sh_size;
4780 p->p_offset = m->sections[0]->filepos;
4783 else
4785 if (m->includes_filehdr)
4787 p->p_vaddr = filehdr_vaddr;
4788 if (! m->p_paddr_valid)
4789 p->p_paddr = filehdr_paddr;
4791 else if (m->includes_phdrs)
4793 p->p_vaddr = phdrs_vaddr;
4794 if (! m->p_paddr_valid)
4795 p->p_paddr = phdrs_paddr;
4797 else if (p->p_type == PT_GNU_RELRO)
4799 Elf_Internal_Phdr *lp;
4801 for (lp = phdrs; lp < phdrs + count; ++lp)
4803 if (lp->p_type == PT_LOAD
4804 && lp->p_vaddr <= link_info->relro_end
4805 && lp->p_vaddr >= link_info->relro_start
4806 && (lp->p_vaddr + lp->p_filesz
4807 >= link_info->relro_end))
4808 break;
4811 if (lp < phdrs + count
4812 && link_info->relro_end > lp->p_vaddr)
4814 p->p_vaddr = lp->p_vaddr;
4815 p->p_paddr = lp->p_paddr;
4816 p->p_offset = lp->p_offset;
4817 p->p_filesz = link_info->relro_end - lp->p_vaddr;
4818 p->p_memsz = p->p_filesz;
4819 p->p_align = 1;
4820 p->p_flags = (lp->p_flags & ~PF_W);
4822 else
4824 memset (p, 0, sizeof *p);
4825 p->p_type = PT_NULL;
4831 elf_tdata (abfd)->next_file_pos = off;
4833 return TRUE;
4836 /* Work out the file positions of all the sections. This is called by
4837 _bfd_elf_compute_section_file_positions. All the section sizes and
4838 VMAs must be known before this is called.
4840 Reloc sections come in two flavours: Those processed specially as
4841 "side-channel" data attached to a section to which they apply, and
4842 those that bfd doesn't process as relocations. The latter sort are
4843 stored in a normal bfd section by bfd_section_from_shdr. We don't
4844 consider the former sort here, unless they form part of the loadable
4845 image. Reloc sections not assigned here will be handled later by
4846 assign_file_positions_for_relocs.
4848 We also don't set the positions of the .symtab and .strtab here. */
4850 static bfd_boolean
4851 assign_file_positions_except_relocs (bfd *abfd,
4852 struct bfd_link_info *link_info)
4854 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4855 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4856 file_ptr off;
4857 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4859 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4860 && bfd_get_format (abfd) != bfd_core)
4862 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4863 unsigned int num_sec = elf_numsections (abfd);
4864 Elf_Internal_Shdr **hdrpp;
4865 unsigned int i;
4867 /* Start after the ELF header. */
4868 off = i_ehdrp->e_ehsize;
4870 /* We are not creating an executable, which means that we are
4871 not creating a program header, and that the actual order of
4872 the sections in the file is unimportant. */
4873 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4875 Elf_Internal_Shdr *hdr;
4877 hdr = *hdrpp;
4878 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4879 && hdr->bfd_section == NULL)
4880 || i == tdata->symtab_section
4881 || i == tdata->symtab_shndx_section
4882 || i == tdata->strtab_section)
4884 hdr->sh_offset = -1;
4886 else
4887 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4889 if (i == SHN_LORESERVE - 1)
4891 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4892 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4896 else
4898 unsigned int alloc;
4900 /* Assign file positions for the loaded sections based on the
4901 assignment of sections to segments. */
4902 if (!assign_file_positions_for_load_sections (abfd, link_info))
4903 return FALSE;
4905 /* And for non-load sections. */
4906 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
4907 return FALSE;
4909 if (bed->elf_backend_modify_program_headers != NULL)
4911 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
4912 return FALSE;
4915 /* Write out the program headers. */
4916 alloc = tdata->program_header_size / bed->s->sizeof_phdr;
4917 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4918 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
4919 return FALSE;
4921 off = tdata->next_file_pos;
4924 /* Place the section headers. */
4925 off = align_file_position (off, 1 << bed->s->log_file_align);
4926 i_ehdrp->e_shoff = off;
4927 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4929 tdata->next_file_pos = off;
4931 return TRUE;
4934 static bfd_boolean
4935 prep_headers (bfd *abfd)
4937 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
4938 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4939 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
4940 struct elf_strtab_hash *shstrtab;
4941 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4943 i_ehdrp = elf_elfheader (abfd);
4944 i_shdrp = elf_elfsections (abfd);
4946 shstrtab = _bfd_elf_strtab_init ();
4947 if (shstrtab == NULL)
4948 return FALSE;
4950 elf_shstrtab (abfd) = shstrtab;
4952 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4953 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4954 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4955 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4957 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4958 i_ehdrp->e_ident[EI_DATA] =
4959 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4960 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4962 if ((abfd->flags & DYNAMIC) != 0)
4963 i_ehdrp->e_type = ET_DYN;
4964 else if ((abfd->flags & EXEC_P) != 0)
4965 i_ehdrp->e_type = ET_EXEC;
4966 else if (bfd_get_format (abfd) == bfd_core)
4967 i_ehdrp->e_type = ET_CORE;
4968 else
4969 i_ehdrp->e_type = ET_REL;
4971 switch (bfd_get_arch (abfd))
4973 case bfd_arch_unknown:
4974 i_ehdrp->e_machine = EM_NONE;
4975 break;
4977 /* There used to be a long list of cases here, each one setting
4978 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4979 in the corresponding bfd definition. To avoid duplication,
4980 the switch was removed. Machines that need special handling
4981 can generally do it in elf_backend_final_write_processing(),
4982 unless they need the information earlier than the final write.
4983 Such need can generally be supplied by replacing the tests for
4984 e_machine with the conditions used to determine it. */
4985 default:
4986 i_ehdrp->e_machine = bed->elf_machine_code;
4989 i_ehdrp->e_version = bed->s->ev_current;
4990 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4992 /* No program header, for now. */
4993 i_ehdrp->e_phoff = 0;
4994 i_ehdrp->e_phentsize = 0;
4995 i_ehdrp->e_phnum = 0;
4997 /* Each bfd section is section header entry. */
4998 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4999 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
5001 /* If we're building an executable, we'll need a program header table. */
5002 if (abfd->flags & EXEC_P)
5003 /* It all happens later. */
5005 else
5007 i_ehdrp->e_phentsize = 0;
5008 i_phdrp = 0;
5009 i_ehdrp->e_phoff = 0;
5012 elf_tdata (abfd)->symtab_hdr.sh_name =
5013 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
5014 elf_tdata (abfd)->strtab_hdr.sh_name =
5015 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
5016 elf_tdata (abfd)->shstrtab_hdr.sh_name =
5017 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
5018 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
5019 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
5020 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
5021 return FALSE;
5023 return TRUE;
5026 /* Assign file positions for all the reloc sections which are not part
5027 of the loadable file image. */
5029 void
5030 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
5032 file_ptr off;
5033 unsigned int i, num_sec;
5034 Elf_Internal_Shdr **shdrpp;
5036 off = elf_tdata (abfd)->next_file_pos;
5038 num_sec = elf_numsections (abfd);
5039 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
5041 Elf_Internal_Shdr *shdrp;
5043 shdrp = *shdrpp;
5044 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
5045 && shdrp->sh_offset == -1)
5046 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
5049 elf_tdata (abfd)->next_file_pos = off;
5052 bfd_boolean
5053 _bfd_elf_write_object_contents (bfd *abfd)
5055 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5056 Elf_Internal_Ehdr *i_ehdrp;
5057 Elf_Internal_Shdr **i_shdrp;
5058 bfd_boolean failed;
5059 unsigned int count, num_sec;
5061 if (! abfd->output_has_begun
5062 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
5063 return FALSE;
5065 i_shdrp = elf_elfsections (abfd);
5066 i_ehdrp = elf_elfheader (abfd);
5068 failed = FALSE;
5069 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
5070 if (failed)
5071 return FALSE;
5073 _bfd_elf_assign_file_positions_for_relocs (abfd);
5075 /* After writing the headers, we need to write the sections too... */
5076 num_sec = elf_numsections (abfd);
5077 for (count = 1; count < num_sec; count++)
5079 if (bed->elf_backend_section_processing)
5080 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
5081 if (i_shdrp[count]->contents)
5083 bfd_size_type amt = i_shdrp[count]->sh_size;
5085 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
5086 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
5087 return FALSE;
5089 if (count == SHN_LORESERVE - 1)
5090 count += SHN_HIRESERVE + 1 - SHN_LORESERVE;
5093 /* Write out the section header names. */
5094 if (elf_shstrtab (abfd) != NULL
5095 && (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
5096 || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
5097 return FALSE;
5099 if (bed->elf_backend_final_write_processing)
5100 (*bed->elf_backend_final_write_processing) (abfd,
5101 elf_tdata (abfd)->linker);
5103 return bed->s->write_shdrs_and_ehdr (abfd);
5106 bfd_boolean
5107 _bfd_elf_write_corefile_contents (bfd *abfd)
5109 /* Hopefully this can be done just like an object file. */
5110 return _bfd_elf_write_object_contents (abfd);
5113 /* Given a section, search the header to find them. */
5116 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
5118 const struct elf_backend_data *bed;
5119 int index;
5121 if (elf_section_data (asect) != NULL
5122 && elf_section_data (asect)->this_idx != 0)
5123 return elf_section_data (asect)->this_idx;
5125 if (bfd_is_abs_section (asect))
5126 index = SHN_ABS;
5127 else if (bfd_is_com_section (asect))
5128 index = SHN_COMMON;
5129 else if (bfd_is_und_section (asect))
5130 index = SHN_UNDEF;
5131 else
5132 index = -1;
5134 bed = get_elf_backend_data (abfd);
5135 if (bed->elf_backend_section_from_bfd_section)
5137 int retval = index;
5139 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
5140 return retval;
5143 if (index == -1)
5144 bfd_set_error (bfd_error_nonrepresentable_section);
5146 return index;
5149 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5150 on error. */
5153 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
5155 asymbol *asym_ptr = *asym_ptr_ptr;
5156 int idx;
5157 flagword flags = asym_ptr->flags;
5159 /* When gas creates relocations against local labels, it creates its
5160 own symbol for the section, but does put the symbol into the
5161 symbol chain, so udata is 0. When the linker is generating
5162 relocatable output, this section symbol may be for one of the
5163 input sections rather than the output section. */
5164 if (asym_ptr->udata.i == 0
5165 && (flags & BSF_SECTION_SYM)
5166 && asym_ptr->section)
5168 asection *sec;
5169 int indx;
5171 sec = asym_ptr->section;
5172 if (sec->owner != abfd && sec->output_section != NULL)
5173 sec = sec->output_section;
5174 if (sec->owner == abfd
5175 && (indx = sec->index) < elf_num_section_syms (abfd)
5176 && elf_section_syms (abfd)[indx] != NULL)
5177 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
5180 idx = asym_ptr->udata.i;
5182 if (idx == 0)
5184 /* This case can occur when using --strip-symbol on a symbol
5185 which is used in a relocation entry. */
5186 (*_bfd_error_handler)
5187 (_("%B: symbol `%s' required but not present"),
5188 abfd, bfd_asymbol_name (asym_ptr));
5189 bfd_set_error (bfd_error_no_symbols);
5190 return -1;
5193 #if DEBUG & 4
5195 fprintf (stderr,
5196 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
5197 (long) asym_ptr, asym_ptr->name, idx, flags,
5198 elf_symbol_flags (flags));
5199 fflush (stderr);
5201 #endif
5203 return idx;
5206 /* Rewrite program header information. */
5208 static bfd_boolean
5209 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
5211 Elf_Internal_Ehdr *iehdr;
5212 struct elf_segment_map *map;
5213 struct elf_segment_map *map_first;
5214 struct elf_segment_map **pointer_to_map;
5215 Elf_Internal_Phdr *segment;
5216 asection *section;
5217 unsigned int i;
5218 unsigned int num_segments;
5219 bfd_boolean phdr_included = FALSE;
5220 bfd_vma maxpagesize;
5221 struct elf_segment_map *phdr_adjust_seg = NULL;
5222 unsigned int phdr_adjust_num = 0;
5223 const struct elf_backend_data *bed;
5225 bed = get_elf_backend_data (ibfd);
5226 iehdr = elf_elfheader (ibfd);
5228 map_first = NULL;
5229 pointer_to_map = &map_first;
5231 num_segments = elf_elfheader (ibfd)->e_phnum;
5232 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
5234 /* Returns the end address of the segment + 1. */
5235 #define SEGMENT_END(segment, start) \
5236 (start + (segment->p_memsz > segment->p_filesz \
5237 ? segment->p_memsz : segment->p_filesz))
5239 #define SECTION_SIZE(section, segment) \
5240 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5241 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5242 ? section->size : 0)
5244 /* Returns TRUE if the given section is contained within
5245 the given segment. VMA addresses are compared. */
5246 #define IS_CONTAINED_BY_VMA(section, segment) \
5247 (section->vma >= segment->p_vaddr \
5248 && (section->vma + SECTION_SIZE (section, segment) \
5249 <= (SEGMENT_END (segment, segment->p_vaddr))))
5251 /* Returns TRUE if the given section is contained within
5252 the given segment. LMA addresses are compared. */
5253 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5254 (section->lma >= base \
5255 && (section->lma + SECTION_SIZE (section, segment) \
5256 <= SEGMENT_END (segment, base)))
5258 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
5259 #define IS_COREFILE_NOTE(p, s) \
5260 (p->p_type == PT_NOTE \
5261 && bfd_get_format (ibfd) == bfd_core \
5262 && s->vma == 0 && s->lma == 0 \
5263 && (bfd_vma) s->filepos >= p->p_offset \
5264 && ((bfd_vma) s->filepos + s->size \
5265 <= p->p_offset + p->p_filesz))
5267 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5268 linker, which generates a PT_INTERP section with p_vaddr and
5269 p_memsz set to 0. */
5270 #define IS_SOLARIS_PT_INTERP(p, s) \
5271 (p->p_vaddr == 0 \
5272 && p->p_paddr == 0 \
5273 && p->p_memsz == 0 \
5274 && p->p_filesz > 0 \
5275 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5276 && s->size > 0 \
5277 && (bfd_vma) s->filepos >= p->p_offset \
5278 && ((bfd_vma) s->filepos + s->size \
5279 <= p->p_offset + p->p_filesz))
5281 /* Decide if the given section should be included in the given segment.
5282 A section will be included if:
5283 1. It is within the address space of the segment -- we use the LMA
5284 if that is set for the segment and the VMA otherwise,
5285 2. It is an allocated segment,
5286 3. There is an output section associated with it,
5287 4. The section has not already been allocated to a previous segment.
5288 5. PT_GNU_STACK segments do not include any sections.
5289 6. PT_TLS segment includes only SHF_TLS sections.
5290 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5291 8. PT_DYNAMIC should not contain empty sections at the beginning
5292 (with the possible exception of .dynamic). */
5293 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5294 ((((segment->p_paddr \
5295 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5296 : IS_CONTAINED_BY_VMA (section, segment)) \
5297 && (section->flags & SEC_ALLOC) != 0) \
5298 || IS_COREFILE_NOTE (segment, section)) \
5299 && segment->p_type != PT_GNU_STACK \
5300 && (segment->p_type != PT_TLS \
5301 || (section->flags & SEC_THREAD_LOCAL)) \
5302 && (segment->p_type == PT_LOAD \
5303 || segment->p_type == PT_TLS \
5304 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5305 && (segment->p_type != PT_DYNAMIC \
5306 || SECTION_SIZE (section, segment) > 0 \
5307 || (segment->p_paddr \
5308 ? segment->p_paddr != section->lma \
5309 : segment->p_vaddr != section->vma) \
5310 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5311 == 0)) \
5312 && ! section->segment_mark)
5314 /* If the output section of a section in the input segment is NULL,
5315 it is removed from the corresponding output segment. */
5316 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5317 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5318 && section->output_section != NULL)
5320 /* Returns TRUE iff seg1 starts after the end of seg2. */
5321 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5322 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5324 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5325 their VMA address ranges and their LMA address ranges overlap.
5326 It is possible to have overlapping VMA ranges without overlapping LMA
5327 ranges. RedBoot images for example can have both .data and .bss mapped
5328 to the same VMA range, but with the .data section mapped to a different
5329 LMA. */
5330 #define SEGMENT_OVERLAPS(seg1, seg2) \
5331 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5332 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5333 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5334 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5336 /* Initialise the segment mark field. */
5337 for (section = ibfd->sections; section != NULL; section = section->next)
5338 section->segment_mark = FALSE;
5340 /* Scan through the segments specified in the program header
5341 of the input BFD. For this first scan we look for overlaps
5342 in the loadable segments. These can be created by weird
5343 parameters to objcopy. Also, fix some solaris weirdness. */
5344 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5345 i < num_segments;
5346 i++, segment++)
5348 unsigned int j;
5349 Elf_Internal_Phdr *segment2;
5351 if (segment->p_type == PT_INTERP)
5352 for (section = ibfd->sections; section; section = section->next)
5353 if (IS_SOLARIS_PT_INTERP (segment, section))
5355 /* Mininal change so that the normal section to segment
5356 assignment code will work. */
5357 segment->p_vaddr = section->vma;
5358 break;
5361 if (segment->p_type != PT_LOAD)
5362 continue;
5364 /* Determine if this segment overlaps any previous segments. */
5365 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
5367 bfd_signed_vma extra_length;
5369 if (segment2->p_type != PT_LOAD
5370 || ! SEGMENT_OVERLAPS (segment, segment2))
5371 continue;
5373 /* Merge the two segments together. */
5374 if (segment2->p_vaddr < segment->p_vaddr)
5376 /* Extend SEGMENT2 to include SEGMENT and then delete
5377 SEGMENT. */
5378 extra_length =
5379 SEGMENT_END (segment, segment->p_vaddr)
5380 - SEGMENT_END (segment2, segment2->p_vaddr);
5382 if (extra_length > 0)
5384 segment2->p_memsz += extra_length;
5385 segment2->p_filesz += extra_length;
5388 segment->p_type = PT_NULL;
5390 /* Since we have deleted P we must restart the outer loop. */
5391 i = 0;
5392 segment = elf_tdata (ibfd)->phdr;
5393 break;
5395 else
5397 /* Extend SEGMENT to include SEGMENT2 and then delete
5398 SEGMENT2. */
5399 extra_length =
5400 SEGMENT_END (segment2, segment2->p_vaddr)
5401 - SEGMENT_END (segment, segment->p_vaddr);
5403 if (extra_length > 0)
5405 segment->p_memsz += extra_length;
5406 segment->p_filesz += extra_length;
5409 segment2->p_type = PT_NULL;
5414 /* The second scan attempts to assign sections to segments. */
5415 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5416 i < num_segments;
5417 i ++, segment ++)
5419 unsigned int section_count;
5420 asection ** sections;
5421 asection * output_section;
5422 unsigned int isec;
5423 bfd_vma matching_lma;
5424 bfd_vma suggested_lma;
5425 unsigned int j;
5426 bfd_size_type amt;
5427 asection * first_section;
5429 if (segment->p_type == PT_NULL)
5430 continue;
5432 first_section = NULL;
5433 /* Compute how many sections might be placed into this segment. */
5434 for (section = ibfd->sections, section_count = 0;
5435 section != NULL;
5436 section = section->next)
5438 /* Find the first section in the input segment, which may be
5439 removed from the corresponding output segment. */
5440 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
5442 if (first_section == NULL)
5443 first_section = section;
5444 if (section->output_section != NULL)
5445 ++section_count;
5449 /* Allocate a segment map big enough to contain
5450 all of the sections we have selected. */
5451 amt = sizeof (struct elf_segment_map);
5452 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5453 map = bfd_zalloc (obfd, amt);
5454 if (map == NULL)
5455 return FALSE;
5457 /* Initialise the fields of the segment map. Default to
5458 using the physical address of the segment in the input BFD. */
5459 map->next = NULL;
5460 map->p_type = segment->p_type;
5461 map->p_flags = segment->p_flags;
5462 map->p_flags_valid = 1;
5464 /* If the first section in the input segment is removed, there is
5465 no need to preserve segment physical address in the corresponding
5466 output segment. */
5467 if (!first_section || first_section->output_section != NULL)
5469 map->p_paddr = segment->p_paddr;
5470 map->p_paddr_valid = 1;
5473 /* Determine if this segment contains the ELF file header
5474 and if it contains the program headers themselves. */
5475 map->includes_filehdr = (segment->p_offset == 0
5476 && segment->p_filesz >= iehdr->e_ehsize);
5478 map->includes_phdrs = 0;
5480 if (! phdr_included || segment->p_type != PT_LOAD)
5482 map->includes_phdrs =
5483 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5484 && (segment->p_offset + segment->p_filesz
5485 >= ((bfd_vma) iehdr->e_phoff
5486 + iehdr->e_phnum * iehdr->e_phentsize)));
5488 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5489 phdr_included = TRUE;
5492 if (section_count == 0)
5494 /* Special segments, such as the PT_PHDR segment, may contain
5495 no sections, but ordinary, loadable segments should contain
5496 something. They are allowed by the ELF spec however, so only
5497 a warning is produced. */
5498 if (segment->p_type == PT_LOAD)
5499 (*_bfd_error_handler)
5500 (_("%B: warning: Empty loadable segment detected, is this intentional ?\n"),
5501 ibfd);
5503 map->count = 0;
5504 *pointer_to_map = map;
5505 pointer_to_map = &map->next;
5507 continue;
5510 /* Now scan the sections in the input BFD again and attempt
5511 to add their corresponding output sections to the segment map.
5512 The problem here is how to handle an output section which has
5513 been moved (ie had its LMA changed). There are four possibilities:
5515 1. None of the sections have been moved.
5516 In this case we can continue to use the segment LMA from the
5517 input BFD.
5519 2. All of the sections have been moved by the same amount.
5520 In this case we can change the segment's LMA to match the LMA
5521 of the first section.
5523 3. Some of the sections have been moved, others have not.
5524 In this case those sections which have not been moved can be
5525 placed in the current segment which will have to have its size,
5526 and possibly its LMA changed, and a new segment or segments will
5527 have to be created to contain the other sections.
5529 4. The sections have been moved, but not by the same amount.
5530 In this case we can change the segment's LMA to match the LMA
5531 of the first section and we will have to create a new segment
5532 or segments to contain the other sections.
5534 In order to save time, we allocate an array to hold the section
5535 pointers that we are interested in. As these sections get assigned
5536 to a segment, they are removed from this array. */
5538 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
5539 to work around this long long bug. */
5540 sections = bfd_malloc2 (section_count, sizeof (asection *));
5541 if (sections == NULL)
5542 return FALSE;
5544 /* Step One: Scan for segment vs section LMA conflicts.
5545 Also add the sections to the section array allocated above.
5546 Also add the sections to the current segment. In the common
5547 case, where the sections have not been moved, this means that
5548 we have completely filled the segment, and there is nothing
5549 more to do. */
5550 isec = 0;
5551 matching_lma = 0;
5552 suggested_lma = 0;
5554 for (j = 0, section = ibfd->sections;
5555 section != NULL;
5556 section = section->next)
5558 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5560 output_section = section->output_section;
5562 sections[j ++] = section;
5564 /* The Solaris native linker always sets p_paddr to 0.
5565 We try to catch that case here, and set it to the
5566 correct value. Note - some backends require that
5567 p_paddr be left as zero. */
5568 if (segment->p_paddr == 0
5569 && segment->p_vaddr != 0
5570 && (! bed->want_p_paddr_set_to_zero)
5571 && isec == 0
5572 && output_section->lma != 0
5573 && (output_section->vma == (segment->p_vaddr
5574 + (map->includes_filehdr
5575 ? iehdr->e_ehsize
5576 : 0)
5577 + (map->includes_phdrs
5578 ? (iehdr->e_phnum
5579 * iehdr->e_phentsize)
5580 : 0))))
5581 map->p_paddr = segment->p_vaddr;
5583 /* Match up the physical address of the segment with the
5584 LMA address of the output section. */
5585 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5586 || IS_COREFILE_NOTE (segment, section)
5587 || (bed->want_p_paddr_set_to_zero &&
5588 IS_CONTAINED_BY_VMA (output_section, segment))
5591 if (matching_lma == 0)
5592 matching_lma = output_section->lma;
5594 /* We assume that if the section fits within the segment
5595 then it does not overlap any other section within that
5596 segment. */
5597 map->sections[isec ++] = output_section;
5599 else if (suggested_lma == 0)
5600 suggested_lma = output_section->lma;
5604 BFD_ASSERT (j == section_count);
5606 /* Step Two: Adjust the physical address of the current segment,
5607 if necessary. */
5608 if (isec == section_count)
5610 /* All of the sections fitted within the segment as currently
5611 specified. This is the default case. Add the segment to
5612 the list of built segments and carry on to process the next
5613 program header in the input BFD. */
5614 map->count = section_count;
5615 *pointer_to_map = map;
5616 pointer_to_map = &map->next;
5618 if (matching_lma != map->p_paddr
5619 && !map->includes_filehdr && !map->includes_phdrs)
5620 /* There is some padding before the first section in the
5621 segment. So, we must account for that in the output
5622 segment's vma. */
5623 map->p_vaddr_offset = matching_lma - map->p_paddr;
5625 free (sections);
5626 continue;
5628 else
5630 if (matching_lma != 0)
5632 /* At least one section fits inside the current segment.
5633 Keep it, but modify its physical address to match the
5634 LMA of the first section that fitted. */
5635 map->p_paddr = matching_lma;
5637 else
5639 /* None of the sections fitted inside the current segment.
5640 Change the current segment's physical address to match
5641 the LMA of the first section. */
5642 map->p_paddr = suggested_lma;
5645 /* Offset the segment physical address from the lma
5646 to allow for space taken up by elf headers. */
5647 if (map->includes_filehdr)
5648 map->p_paddr -= iehdr->e_ehsize;
5650 if (map->includes_phdrs)
5652 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5654 /* iehdr->e_phnum is just an estimate of the number
5655 of program headers that we will need. Make a note
5656 here of the number we used and the segment we chose
5657 to hold these headers, so that we can adjust the
5658 offset when we know the correct value. */
5659 phdr_adjust_num = iehdr->e_phnum;
5660 phdr_adjust_seg = map;
5664 /* Step Three: Loop over the sections again, this time assigning
5665 those that fit to the current segment and removing them from the
5666 sections array; but making sure not to leave large gaps. Once all
5667 possible sections have been assigned to the current segment it is
5668 added to the list of built segments and if sections still remain
5669 to be assigned, a new segment is constructed before repeating
5670 the loop. */
5671 isec = 0;
5674 map->count = 0;
5675 suggested_lma = 0;
5677 /* Fill the current segment with sections that fit. */
5678 for (j = 0; j < section_count; j++)
5680 section = sections[j];
5682 if (section == NULL)
5683 continue;
5685 output_section = section->output_section;
5687 BFD_ASSERT (output_section != NULL);
5689 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5690 || IS_COREFILE_NOTE (segment, section))
5692 if (map->count == 0)
5694 /* If the first section in a segment does not start at
5695 the beginning of the segment, then something is
5696 wrong. */
5697 if (output_section->lma !=
5698 (map->p_paddr
5699 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5700 + (map->includes_phdrs
5701 ? iehdr->e_phnum * iehdr->e_phentsize
5702 : 0)))
5703 abort ();
5705 else
5707 asection * prev_sec;
5709 prev_sec = map->sections[map->count - 1];
5711 /* If the gap between the end of the previous section
5712 and the start of this section is more than
5713 maxpagesize then we need to start a new segment. */
5714 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
5715 maxpagesize)
5716 < BFD_ALIGN (output_section->lma, maxpagesize))
5717 || ((prev_sec->lma + prev_sec->size)
5718 > output_section->lma))
5720 if (suggested_lma == 0)
5721 suggested_lma = output_section->lma;
5723 continue;
5727 map->sections[map->count++] = output_section;
5728 ++isec;
5729 sections[j] = NULL;
5730 section->segment_mark = TRUE;
5732 else if (suggested_lma == 0)
5733 suggested_lma = output_section->lma;
5736 BFD_ASSERT (map->count > 0);
5738 /* Add the current segment to the list of built segments. */
5739 *pointer_to_map = map;
5740 pointer_to_map = &map->next;
5742 if (isec < section_count)
5744 /* We still have not allocated all of the sections to
5745 segments. Create a new segment here, initialise it
5746 and carry on looping. */
5747 amt = sizeof (struct elf_segment_map);
5748 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5749 map = bfd_alloc (obfd, amt);
5750 if (map == NULL)
5752 free (sections);
5753 return FALSE;
5756 /* Initialise the fields of the segment map. Set the physical
5757 physical address to the LMA of the first section that has
5758 not yet been assigned. */
5759 map->next = NULL;
5760 map->p_type = segment->p_type;
5761 map->p_flags = segment->p_flags;
5762 map->p_flags_valid = 1;
5763 map->p_paddr = suggested_lma;
5764 map->p_paddr_valid = 1;
5765 map->includes_filehdr = 0;
5766 map->includes_phdrs = 0;
5769 while (isec < section_count);
5771 free (sections);
5774 /* The Solaris linker creates program headers in which all the
5775 p_paddr fields are zero. When we try to objcopy or strip such a
5776 file, we get confused. Check for this case, and if we find it
5777 reset the p_paddr_valid fields. */
5778 for (map = map_first; map != NULL; map = map->next)
5779 if (map->p_paddr != 0)
5780 break;
5781 if (map == NULL)
5782 for (map = map_first; map != NULL; map = map->next)
5783 map->p_paddr_valid = 0;
5785 elf_tdata (obfd)->segment_map = map_first;
5787 /* If we had to estimate the number of program headers that were
5788 going to be needed, then check our estimate now and adjust
5789 the offset if necessary. */
5790 if (phdr_adjust_seg != NULL)
5792 unsigned int count;
5794 for (count = 0, map = map_first; map != NULL; map = map->next)
5795 count++;
5797 if (count > phdr_adjust_num)
5798 phdr_adjust_seg->p_paddr
5799 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5802 #undef SEGMENT_END
5803 #undef SECTION_SIZE
5804 #undef IS_CONTAINED_BY_VMA
5805 #undef IS_CONTAINED_BY_LMA
5806 #undef IS_COREFILE_NOTE
5807 #undef IS_SOLARIS_PT_INTERP
5808 #undef IS_SECTION_IN_INPUT_SEGMENT
5809 #undef INCLUDE_SECTION_IN_SEGMENT
5810 #undef SEGMENT_AFTER_SEGMENT
5811 #undef SEGMENT_OVERLAPS
5812 return TRUE;
5815 /* Copy ELF program header information. */
5817 static bfd_boolean
5818 copy_elf_program_header (bfd *ibfd, bfd *obfd)
5820 Elf_Internal_Ehdr *iehdr;
5821 struct elf_segment_map *map;
5822 struct elf_segment_map *map_first;
5823 struct elf_segment_map **pointer_to_map;
5824 Elf_Internal_Phdr *segment;
5825 unsigned int i;
5826 unsigned int num_segments;
5827 bfd_boolean phdr_included = FALSE;
5829 iehdr = elf_elfheader (ibfd);
5831 map_first = NULL;
5832 pointer_to_map = &map_first;
5834 num_segments = elf_elfheader (ibfd)->e_phnum;
5835 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5836 i < num_segments;
5837 i++, segment++)
5839 asection *section;
5840 unsigned int section_count;
5841 bfd_size_type amt;
5842 Elf_Internal_Shdr *this_hdr;
5843 asection *first_section = NULL;
5845 /* FIXME: Do we need to copy PT_NULL segment? */
5846 if (segment->p_type == PT_NULL)
5847 continue;
5849 /* Compute how many sections are in this segment. */
5850 for (section = ibfd->sections, section_count = 0;
5851 section != NULL;
5852 section = section->next)
5854 this_hdr = &(elf_section_data(section)->this_hdr);
5855 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5857 if (!first_section)
5858 first_section = section;
5859 section_count++;
5863 /* Allocate a segment map big enough to contain
5864 all of the sections we have selected. */
5865 amt = sizeof (struct elf_segment_map);
5866 if (section_count != 0)
5867 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5868 map = bfd_zalloc (obfd, amt);
5869 if (map == NULL)
5870 return FALSE;
5872 /* Initialize the fields of the output segment map with the
5873 input segment. */
5874 map->next = NULL;
5875 map->p_type = segment->p_type;
5876 map->p_flags = segment->p_flags;
5877 map->p_flags_valid = 1;
5878 map->p_paddr = segment->p_paddr;
5879 map->p_paddr_valid = 1;
5880 map->p_align = segment->p_align;
5881 map->p_align_valid = 1;
5882 map->p_vaddr_offset = 0;
5884 /* Determine if this segment contains the ELF file header
5885 and if it contains the program headers themselves. */
5886 map->includes_filehdr = (segment->p_offset == 0
5887 && segment->p_filesz >= iehdr->e_ehsize);
5889 map->includes_phdrs = 0;
5890 if (! phdr_included || segment->p_type != PT_LOAD)
5892 map->includes_phdrs =
5893 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5894 && (segment->p_offset + segment->p_filesz
5895 >= ((bfd_vma) iehdr->e_phoff
5896 + iehdr->e_phnum * iehdr->e_phentsize)));
5898 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5899 phdr_included = TRUE;
5902 if (!map->includes_phdrs && !map->includes_filehdr)
5903 /* There is some other padding before the first section. */
5904 map->p_vaddr_offset = ((first_section ? first_section->lma : 0)
5905 - segment->p_paddr);
5907 if (section_count != 0)
5909 unsigned int isec = 0;
5911 for (section = first_section;
5912 section != NULL;
5913 section = section->next)
5915 this_hdr = &(elf_section_data(section)->this_hdr);
5916 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5918 map->sections[isec++] = section->output_section;
5919 if (isec == section_count)
5920 break;
5925 map->count = section_count;
5926 *pointer_to_map = map;
5927 pointer_to_map = &map->next;
5930 elf_tdata (obfd)->segment_map = map_first;
5931 return TRUE;
5934 /* Copy private BFD data. This copies or rewrites ELF program header
5935 information. */
5937 static bfd_boolean
5938 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
5940 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5941 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5942 return TRUE;
5944 if (elf_tdata (ibfd)->phdr == NULL)
5945 return TRUE;
5947 if (ibfd->xvec == obfd->xvec)
5949 /* Check to see if any sections in the input BFD
5950 covered by ELF program header have changed. */
5951 Elf_Internal_Phdr *segment;
5952 asection *section, *osec;
5953 unsigned int i, num_segments;
5954 Elf_Internal_Shdr *this_hdr;
5956 /* Initialize the segment mark field. */
5957 for (section = obfd->sections; section != NULL;
5958 section = section->next)
5959 section->segment_mark = FALSE;
5961 num_segments = elf_elfheader (ibfd)->e_phnum;
5962 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5963 i < num_segments;
5964 i++, segment++)
5966 /* PR binutils/3535. The Solaris linker always sets the p_paddr
5967 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
5968 which severly confuses things, so always regenerate the segment
5969 map in this case. */
5970 if (segment->p_paddr == 0
5971 && segment->p_memsz == 0
5972 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
5973 goto rewrite;
5975 for (section = ibfd->sections;
5976 section != NULL; section = section->next)
5978 /* We mark the output section so that we know it comes
5979 from the input BFD. */
5980 osec = section->output_section;
5981 if (osec)
5982 osec->segment_mark = TRUE;
5984 /* Check if this section is covered by the segment. */
5985 this_hdr = &(elf_section_data(section)->this_hdr);
5986 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5988 /* FIXME: Check if its output section is changed or
5989 removed. What else do we need to check? */
5990 if (osec == NULL
5991 || section->flags != osec->flags
5992 || section->lma != osec->lma
5993 || section->vma != osec->vma
5994 || section->size != osec->size
5995 || section->rawsize != osec->rawsize
5996 || section->alignment_power != osec->alignment_power)
5997 goto rewrite;
6002 /* Check to see if any output section do not come from the
6003 input BFD. */
6004 for (section = obfd->sections; section != NULL;
6005 section = section->next)
6007 if (section->segment_mark == FALSE)
6008 goto rewrite;
6009 else
6010 section->segment_mark = FALSE;
6013 return copy_elf_program_header (ibfd, obfd);
6016 rewrite:
6017 return rewrite_elf_program_header (ibfd, obfd);
6020 /* Initialize private output section information from input section. */
6022 bfd_boolean
6023 _bfd_elf_init_private_section_data (bfd *ibfd,
6024 asection *isec,
6025 bfd *obfd,
6026 asection *osec,
6027 struct bfd_link_info *link_info)
6030 Elf_Internal_Shdr *ihdr, *ohdr;
6031 bfd_boolean need_group = link_info == NULL || link_info->relocatable;
6033 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6034 || obfd->xvec->flavour != bfd_target_elf_flavour)
6035 return TRUE;
6037 /* Don't copy the output ELF section type from input if the
6038 output BFD section flags have been set to something different.
6039 elf_fake_sections will set ELF section type based on BFD
6040 section flags. */
6041 if (elf_section_type (osec) == SHT_NULL
6042 && (osec->flags == isec->flags || !osec->flags))
6043 elf_section_type (osec) = elf_section_type (isec);
6045 /* FIXME: Is this correct for all OS/PROC specific flags? */
6046 elf_section_flags (osec) |= (elf_section_flags (isec)
6047 & (SHF_MASKOS | SHF_MASKPROC));
6049 /* Set things up for objcopy and relocatable link. The output
6050 SHT_GROUP section will have its elf_next_in_group pointing back
6051 to the input group members. Ignore linker created group section.
6052 See elfNN_ia64_object_p in elfxx-ia64.c. */
6053 if (need_group)
6055 if (elf_sec_group (isec) == NULL
6056 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
6058 if (elf_section_flags (isec) & SHF_GROUP)
6059 elf_section_flags (osec) |= SHF_GROUP;
6060 elf_next_in_group (osec) = elf_next_in_group (isec);
6061 elf_group_name (osec) = elf_group_name (isec);
6065 ihdr = &elf_section_data (isec)->this_hdr;
6067 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6068 don't use the output section of the linked-to section since it
6069 may be NULL at this point. */
6070 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
6072 ohdr = &elf_section_data (osec)->this_hdr;
6073 ohdr->sh_flags |= SHF_LINK_ORDER;
6074 elf_linked_to_section (osec) = elf_linked_to_section (isec);
6077 osec->use_rela_p = isec->use_rela_p;
6079 return TRUE;
6082 /* Copy private section information. This copies over the entsize
6083 field, and sometimes the info field. */
6085 bfd_boolean
6086 _bfd_elf_copy_private_section_data (bfd *ibfd,
6087 asection *isec,
6088 bfd *obfd,
6089 asection *osec)
6091 Elf_Internal_Shdr *ihdr, *ohdr;
6093 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6094 || obfd->xvec->flavour != bfd_target_elf_flavour)
6095 return TRUE;
6097 ihdr = &elf_section_data (isec)->this_hdr;
6098 ohdr = &elf_section_data (osec)->this_hdr;
6100 ohdr->sh_entsize = ihdr->sh_entsize;
6102 if (ihdr->sh_type == SHT_SYMTAB
6103 || ihdr->sh_type == SHT_DYNSYM
6104 || ihdr->sh_type == SHT_GNU_verneed
6105 || ihdr->sh_type == SHT_GNU_verdef)
6106 ohdr->sh_info = ihdr->sh_info;
6108 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
6109 NULL);
6112 /* Copy private header information. */
6114 bfd_boolean
6115 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
6117 asection *isec;
6119 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6120 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6121 return TRUE;
6123 /* Copy over private BFD data if it has not already been copied.
6124 This must be done here, rather than in the copy_private_bfd_data
6125 entry point, because the latter is called after the section
6126 contents have been set, which means that the program headers have
6127 already been worked out. */
6128 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
6130 if (! copy_private_bfd_data (ibfd, obfd))
6131 return FALSE;
6134 /* _bfd_elf_copy_private_section_data copied over the SHF_GROUP flag
6135 but this might be wrong if we deleted the group section. */
6136 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
6137 if (elf_section_type (isec) == SHT_GROUP
6138 && isec->output_section == NULL)
6140 asection *first = elf_next_in_group (isec);
6141 asection *s = first;
6142 while (s != NULL)
6144 if (s->output_section != NULL)
6146 elf_section_flags (s->output_section) &= ~SHF_GROUP;
6147 elf_group_name (s->output_section) = NULL;
6149 s = elf_next_in_group (s);
6150 if (s == first)
6151 break;
6155 return TRUE;
6158 /* Copy private symbol information. If this symbol is in a section
6159 which we did not map into a BFD section, try to map the section
6160 index correctly. We use special macro definitions for the mapped
6161 section indices; these definitions are interpreted by the
6162 swap_out_syms function. */
6164 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6165 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6166 #define MAP_STRTAB (SHN_HIOS + 3)
6167 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6168 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6170 bfd_boolean
6171 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
6172 asymbol *isymarg,
6173 bfd *obfd,
6174 asymbol *osymarg)
6176 elf_symbol_type *isym, *osym;
6178 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6179 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6180 return TRUE;
6182 isym = elf_symbol_from (ibfd, isymarg);
6183 osym = elf_symbol_from (obfd, osymarg);
6185 if (isym != NULL
6186 && osym != NULL
6187 && bfd_is_abs_section (isym->symbol.section))
6189 unsigned int shndx;
6191 shndx = isym->internal_elf_sym.st_shndx;
6192 if (shndx == elf_onesymtab (ibfd))
6193 shndx = MAP_ONESYMTAB;
6194 else if (shndx == elf_dynsymtab (ibfd))
6195 shndx = MAP_DYNSYMTAB;
6196 else if (shndx == elf_tdata (ibfd)->strtab_section)
6197 shndx = MAP_STRTAB;
6198 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
6199 shndx = MAP_SHSTRTAB;
6200 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
6201 shndx = MAP_SYM_SHNDX;
6202 osym->internal_elf_sym.st_shndx = shndx;
6205 return TRUE;
6208 /* Swap out the symbols. */
6210 static bfd_boolean
6211 swap_out_syms (bfd *abfd,
6212 struct bfd_strtab_hash **sttp,
6213 int relocatable_p)
6215 const struct elf_backend_data *bed;
6216 int symcount;
6217 asymbol **syms;
6218 struct bfd_strtab_hash *stt;
6219 Elf_Internal_Shdr *symtab_hdr;
6220 Elf_Internal_Shdr *symtab_shndx_hdr;
6221 Elf_Internal_Shdr *symstrtab_hdr;
6222 bfd_byte *outbound_syms;
6223 bfd_byte *outbound_shndx;
6224 int idx;
6225 bfd_size_type amt;
6226 bfd_boolean name_local_sections;
6228 if (!elf_map_symbols (abfd))
6229 return FALSE;
6231 /* Dump out the symtabs. */
6232 stt = _bfd_elf_stringtab_init ();
6233 if (stt == NULL)
6234 return FALSE;
6236 bed = get_elf_backend_data (abfd);
6237 symcount = bfd_get_symcount (abfd);
6238 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6239 symtab_hdr->sh_type = SHT_SYMTAB;
6240 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
6241 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
6242 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
6243 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
6245 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
6246 symstrtab_hdr->sh_type = SHT_STRTAB;
6248 outbound_syms = bfd_alloc2 (abfd, 1 + symcount, bed->s->sizeof_sym);
6249 if (outbound_syms == NULL)
6251 _bfd_stringtab_free (stt);
6252 return FALSE;
6254 symtab_hdr->contents = outbound_syms;
6256 outbound_shndx = NULL;
6257 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
6258 if (symtab_shndx_hdr->sh_name != 0)
6260 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
6261 outbound_shndx = bfd_zalloc2 (abfd, 1 + symcount,
6262 sizeof (Elf_External_Sym_Shndx));
6263 if (outbound_shndx == NULL)
6265 _bfd_stringtab_free (stt);
6266 return FALSE;
6269 symtab_shndx_hdr->contents = outbound_shndx;
6270 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
6271 symtab_shndx_hdr->sh_size = amt;
6272 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
6273 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
6276 /* Now generate the data (for "contents"). */
6278 /* Fill in zeroth symbol and swap it out. */
6279 Elf_Internal_Sym sym;
6280 sym.st_name = 0;
6281 sym.st_value = 0;
6282 sym.st_size = 0;
6283 sym.st_info = 0;
6284 sym.st_other = 0;
6285 sym.st_shndx = SHN_UNDEF;
6286 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6287 outbound_syms += bed->s->sizeof_sym;
6288 if (outbound_shndx != NULL)
6289 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6292 name_local_sections
6293 = (bed->elf_backend_name_local_section_symbols
6294 && bed->elf_backend_name_local_section_symbols (abfd));
6296 syms = bfd_get_outsymbols (abfd);
6297 for (idx = 0; idx < symcount; idx++)
6299 Elf_Internal_Sym sym;
6300 bfd_vma value = syms[idx]->value;
6301 elf_symbol_type *type_ptr;
6302 flagword flags = syms[idx]->flags;
6303 int type;
6305 if (!name_local_sections
6306 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
6308 /* Local section symbols have no name. */
6309 sym.st_name = 0;
6311 else
6313 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
6314 syms[idx]->name,
6315 TRUE, FALSE);
6316 if (sym.st_name == (unsigned long) -1)
6318 _bfd_stringtab_free (stt);
6319 return FALSE;
6323 type_ptr = elf_symbol_from (abfd, syms[idx]);
6325 if ((flags & BSF_SECTION_SYM) == 0
6326 && bfd_is_com_section (syms[idx]->section))
6328 /* ELF common symbols put the alignment into the `value' field,
6329 and the size into the `size' field. This is backwards from
6330 how BFD handles it, so reverse it here. */
6331 sym.st_size = value;
6332 if (type_ptr == NULL
6333 || type_ptr->internal_elf_sym.st_value == 0)
6334 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
6335 else
6336 sym.st_value = type_ptr->internal_elf_sym.st_value;
6337 sym.st_shndx = _bfd_elf_section_from_bfd_section
6338 (abfd, syms[idx]->section);
6340 else
6342 asection *sec = syms[idx]->section;
6343 int shndx;
6345 if (sec->output_section)
6347 value += sec->output_offset;
6348 sec = sec->output_section;
6351 /* Don't add in the section vma for relocatable output. */
6352 if (! relocatable_p)
6353 value += sec->vma;
6354 sym.st_value = value;
6355 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
6357 if (bfd_is_abs_section (sec)
6358 && type_ptr != NULL
6359 && type_ptr->internal_elf_sym.st_shndx != 0)
6361 /* This symbol is in a real ELF section which we did
6362 not create as a BFD section. Undo the mapping done
6363 by copy_private_symbol_data. */
6364 shndx = type_ptr->internal_elf_sym.st_shndx;
6365 switch (shndx)
6367 case MAP_ONESYMTAB:
6368 shndx = elf_onesymtab (abfd);
6369 break;
6370 case MAP_DYNSYMTAB:
6371 shndx = elf_dynsymtab (abfd);
6372 break;
6373 case MAP_STRTAB:
6374 shndx = elf_tdata (abfd)->strtab_section;
6375 break;
6376 case MAP_SHSTRTAB:
6377 shndx = elf_tdata (abfd)->shstrtab_section;
6378 break;
6379 case MAP_SYM_SHNDX:
6380 shndx = elf_tdata (abfd)->symtab_shndx_section;
6381 break;
6382 default:
6383 break;
6386 else
6388 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
6390 if (shndx == -1)
6392 asection *sec2;
6394 /* Writing this would be a hell of a lot easier if
6395 we had some decent documentation on bfd, and
6396 knew what to expect of the library, and what to
6397 demand of applications. For example, it
6398 appears that `objcopy' might not set the
6399 section of a symbol to be a section that is
6400 actually in the output file. */
6401 sec2 = bfd_get_section_by_name (abfd, sec->name);
6402 if (sec2 == NULL)
6404 _bfd_error_handler (_("\
6405 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6406 syms[idx]->name ? syms[idx]->name : "<Local sym>",
6407 sec->name);
6408 bfd_set_error (bfd_error_invalid_operation);
6409 _bfd_stringtab_free (stt);
6410 return FALSE;
6413 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
6414 BFD_ASSERT (shndx != -1);
6418 sym.st_shndx = shndx;
6421 if ((flags & BSF_THREAD_LOCAL) != 0)
6422 type = STT_TLS;
6423 else if ((flags & BSF_FUNCTION) != 0)
6424 type = STT_FUNC;
6425 else if ((flags & BSF_OBJECT) != 0)
6426 type = STT_OBJECT;
6427 else if ((flags & BSF_RELC) != 0)
6428 type = STT_RELC;
6429 else if ((flags & BSF_SRELC) != 0)
6430 type = STT_SRELC;
6431 else
6432 type = STT_NOTYPE;
6434 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
6435 type = STT_TLS;
6437 /* Processor-specific types. */
6438 if (type_ptr != NULL
6439 && bed->elf_backend_get_symbol_type)
6440 type = ((*bed->elf_backend_get_symbol_type)
6441 (&type_ptr->internal_elf_sym, type));
6443 if (flags & BSF_SECTION_SYM)
6445 if (flags & BSF_GLOBAL)
6446 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6447 else
6448 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
6450 else if (bfd_is_com_section (syms[idx]->section))
6451 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
6452 else if (bfd_is_und_section (syms[idx]->section))
6453 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
6454 ? STB_WEAK
6455 : STB_GLOBAL),
6456 type);
6457 else if (flags & BSF_FILE)
6458 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
6459 else
6461 int bind = STB_LOCAL;
6463 if (flags & BSF_LOCAL)
6464 bind = STB_LOCAL;
6465 else if (flags & BSF_WEAK)
6466 bind = STB_WEAK;
6467 else if (flags & BSF_GLOBAL)
6468 bind = STB_GLOBAL;
6470 sym.st_info = ELF_ST_INFO (bind, type);
6473 if (type_ptr != NULL)
6474 sym.st_other = type_ptr->internal_elf_sym.st_other;
6475 else
6476 sym.st_other = 0;
6478 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6479 outbound_syms += bed->s->sizeof_sym;
6480 if (outbound_shndx != NULL)
6481 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6484 *sttp = stt;
6485 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
6486 symstrtab_hdr->sh_type = SHT_STRTAB;
6488 symstrtab_hdr->sh_flags = 0;
6489 symstrtab_hdr->sh_addr = 0;
6490 symstrtab_hdr->sh_entsize = 0;
6491 symstrtab_hdr->sh_link = 0;
6492 symstrtab_hdr->sh_info = 0;
6493 symstrtab_hdr->sh_addralign = 1;
6495 return TRUE;
6498 /* Return the number of bytes required to hold the symtab vector.
6500 Note that we base it on the count plus 1, since we will null terminate
6501 the vector allocated based on this size. However, the ELF symbol table
6502 always has a dummy entry as symbol #0, so it ends up even. */
6504 long
6505 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
6507 long symcount;
6508 long symtab_size;
6509 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
6511 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6512 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6513 if (symcount > 0)
6514 symtab_size -= sizeof (asymbol *);
6516 return symtab_size;
6519 long
6520 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
6522 long symcount;
6523 long symtab_size;
6524 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
6526 if (elf_dynsymtab (abfd) == 0)
6528 bfd_set_error (bfd_error_invalid_operation);
6529 return -1;
6532 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6533 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6534 if (symcount > 0)
6535 symtab_size -= sizeof (asymbol *);
6537 return symtab_size;
6540 long
6541 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
6542 sec_ptr asect)
6544 return (asect->reloc_count + 1) * sizeof (arelent *);
6547 /* Canonicalize the relocs. */
6549 long
6550 _bfd_elf_canonicalize_reloc (bfd *abfd,
6551 sec_ptr section,
6552 arelent **relptr,
6553 asymbol **symbols)
6555 arelent *tblptr;
6556 unsigned int i;
6557 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6559 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
6560 return -1;
6562 tblptr = section->relocation;
6563 for (i = 0; i < section->reloc_count; i++)
6564 *relptr++ = tblptr++;
6566 *relptr = NULL;
6568 return section->reloc_count;
6571 long
6572 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
6574 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6575 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
6577 if (symcount >= 0)
6578 bfd_get_symcount (abfd) = symcount;
6579 return symcount;
6582 long
6583 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
6584 asymbol **allocation)
6586 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6587 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
6589 if (symcount >= 0)
6590 bfd_get_dynamic_symcount (abfd) = symcount;
6591 return symcount;
6594 /* Return the size required for the dynamic reloc entries. Any loadable
6595 section that was actually installed in the BFD, and has type SHT_REL
6596 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
6597 dynamic reloc section. */
6599 long
6600 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
6602 long ret;
6603 asection *s;
6605 if (elf_dynsymtab (abfd) == 0)
6607 bfd_set_error (bfd_error_invalid_operation);
6608 return -1;
6611 ret = sizeof (arelent *);
6612 for (s = abfd->sections; s != NULL; s = s->next)
6613 if ((s->flags & SEC_LOAD) != 0
6614 && elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6615 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6616 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6617 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
6618 * sizeof (arelent *));
6620 return ret;
6623 /* Canonicalize the dynamic relocation entries. Note that we return the
6624 dynamic relocations as a single block, although they are actually
6625 associated with particular sections; the interface, which was
6626 designed for SunOS style shared libraries, expects that there is only
6627 one set of dynamic relocs. Any loadable section that was actually
6628 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
6629 dynamic symbol table, is considered to be a dynamic reloc section. */
6631 long
6632 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
6633 arelent **storage,
6634 asymbol **syms)
6636 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
6637 asection *s;
6638 long ret;
6640 if (elf_dynsymtab (abfd) == 0)
6642 bfd_set_error (bfd_error_invalid_operation);
6643 return -1;
6646 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
6647 ret = 0;
6648 for (s = abfd->sections; s != NULL; s = s->next)
6650 if ((s->flags & SEC_LOAD) != 0
6651 && elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6652 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6653 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6655 arelent *p;
6656 long count, i;
6658 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
6659 return -1;
6660 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
6661 p = s->relocation;
6662 for (i = 0; i < count; i++)
6663 *storage++ = p++;
6664 ret += count;
6668 *storage = NULL;
6670 return ret;
6673 /* Read in the version information. */
6675 bfd_boolean
6676 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
6678 bfd_byte *contents = NULL;
6679 unsigned int freeidx = 0;
6681 if (elf_dynverref (abfd) != 0)
6683 Elf_Internal_Shdr *hdr;
6684 Elf_External_Verneed *everneed;
6685 Elf_Internal_Verneed *iverneed;
6686 unsigned int i;
6687 bfd_byte *contents_end;
6689 hdr = &elf_tdata (abfd)->dynverref_hdr;
6691 elf_tdata (abfd)->verref = bfd_zalloc2 (abfd, hdr->sh_info,
6692 sizeof (Elf_Internal_Verneed));
6693 if (elf_tdata (abfd)->verref == NULL)
6694 goto error_return;
6696 elf_tdata (abfd)->cverrefs = hdr->sh_info;
6698 contents = bfd_malloc (hdr->sh_size);
6699 if (contents == NULL)
6701 error_return_verref:
6702 elf_tdata (abfd)->verref = NULL;
6703 elf_tdata (abfd)->cverrefs = 0;
6704 goto error_return;
6706 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6707 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6708 goto error_return_verref;
6710 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed))
6711 goto error_return_verref;
6713 BFD_ASSERT (sizeof (Elf_External_Verneed)
6714 == sizeof (Elf_External_Vernaux));
6715 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
6716 everneed = (Elf_External_Verneed *) contents;
6717 iverneed = elf_tdata (abfd)->verref;
6718 for (i = 0; i < hdr->sh_info; i++, iverneed++)
6720 Elf_External_Vernaux *evernaux;
6721 Elf_Internal_Vernaux *ivernaux;
6722 unsigned int j;
6724 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
6726 iverneed->vn_bfd = abfd;
6728 iverneed->vn_filename =
6729 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6730 iverneed->vn_file);
6731 if (iverneed->vn_filename == NULL)
6732 goto error_return_verref;
6734 if (iverneed->vn_cnt == 0)
6735 iverneed->vn_auxptr = NULL;
6736 else
6738 iverneed->vn_auxptr = bfd_alloc2 (abfd, iverneed->vn_cnt,
6739 sizeof (Elf_Internal_Vernaux));
6740 if (iverneed->vn_auxptr == NULL)
6741 goto error_return_verref;
6744 if (iverneed->vn_aux
6745 > (size_t) (contents_end - (bfd_byte *) everneed))
6746 goto error_return_verref;
6748 evernaux = ((Elf_External_Vernaux *)
6749 ((bfd_byte *) everneed + iverneed->vn_aux));
6750 ivernaux = iverneed->vn_auxptr;
6751 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
6753 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
6755 ivernaux->vna_nodename =
6756 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6757 ivernaux->vna_name);
6758 if (ivernaux->vna_nodename == NULL)
6759 goto error_return_verref;
6761 if (j + 1 < iverneed->vn_cnt)
6762 ivernaux->vna_nextptr = ivernaux + 1;
6763 else
6764 ivernaux->vna_nextptr = NULL;
6766 if (ivernaux->vna_next
6767 > (size_t) (contents_end - (bfd_byte *) evernaux))
6768 goto error_return_verref;
6770 evernaux = ((Elf_External_Vernaux *)
6771 ((bfd_byte *) evernaux + ivernaux->vna_next));
6773 if (ivernaux->vna_other > freeidx)
6774 freeidx = ivernaux->vna_other;
6777 if (i + 1 < hdr->sh_info)
6778 iverneed->vn_nextref = iverneed + 1;
6779 else
6780 iverneed->vn_nextref = NULL;
6782 if (iverneed->vn_next
6783 > (size_t) (contents_end - (bfd_byte *) everneed))
6784 goto error_return_verref;
6786 everneed = ((Elf_External_Verneed *)
6787 ((bfd_byte *) everneed + iverneed->vn_next));
6790 free (contents);
6791 contents = NULL;
6794 if (elf_dynverdef (abfd) != 0)
6796 Elf_Internal_Shdr *hdr;
6797 Elf_External_Verdef *everdef;
6798 Elf_Internal_Verdef *iverdef;
6799 Elf_Internal_Verdef *iverdefarr;
6800 Elf_Internal_Verdef iverdefmem;
6801 unsigned int i;
6802 unsigned int maxidx;
6803 bfd_byte *contents_end_def, *contents_end_aux;
6805 hdr = &elf_tdata (abfd)->dynverdef_hdr;
6807 contents = bfd_malloc (hdr->sh_size);
6808 if (contents == NULL)
6809 goto error_return;
6810 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6811 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6812 goto error_return;
6814 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef))
6815 goto error_return;
6817 BFD_ASSERT (sizeof (Elf_External_Verdef)
6818 >= sizeof (Elf_External_Verdaux));
6819 contents_end_def = contents + hdr->sh_size
6820 - sizeof (Elf_External_Verdef);
6821 contents_end_aux = contents + hdr->sh_size
6822 - sizeof (Elf_External_Verdaux);
6824 /* We know the number of entries in the section but not the maximum
6825 index. Therefore we have to run through all entries and find
6826 the maximum. */
6827 everdef = (Elf_External_Verdef *) contents;
6828 maxidx = 0;
6829 for (i = 0; i < hdr->sh_info; ++i)
6831 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6833 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
6834 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
6836 if (iverdefmem.vd_next
6837 > (size_t) (contents_end_def - (bfd_byte *) everdef))
6838 goto error_return;
6840 everdef = ((Elf_External_Verdef *)
6841 ((bfd_byte *) everdef + iverdefmem.vd_next));
6844 if (default_imported_symver)
6846 if (freeidx > maxidx)
6847 maxidx = ++freeidx;
6848 else
6849 freeidx = ++maxidx;
6851 elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, maxidx,
6852 sizeof (Elf_Internal_Verdef));
6853 if (elf_tdata (abfd)->verdef == NULL)
6854 goto error_return;
6856 elf_tdata (abfd)->cverdefs = maxidx;
6858 everdef = (Elf_External_Verdef *) contents;
6859 iverdefarr = elf_tdata (abfd)->verdef;
6860 for (i = 0; i < hdr->sh_info; i++)
6862 Elf_External_Verdaux *everdaux;
6863 Elf_Internal_Verdaux *iverdaux;
6864 unsigned int j;
6866 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6868 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
6870 error_return_verdef:
6871 elf_tdata (abfd)->verdef = NULL;
6872 elf_tdata (abfd)->cverdefs = 0;
6873 goto error_return;
6876 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
6877 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
6879 iverdef->vd_bfd = abfd;
6881 if (iverdef->vd_cnt == 0)
6882 iverdef->vd_auxptr = NULL;
6883 else
6885 iverdef->vd_auxptr = bfd_alloc2 (abfd, iverdef->vd_cnt,
6886 sizeof (Elf_Internal_Verdaux));
6887 if (iverdef->vd_auxptr == NULL)
6888 goto error_return_verdef;
6891 if (iverdef->vd_aux
6892 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
6893 goto error_return_verdef;
6895 everdaux = ((Elf_External_Verdaux *)
6896 ((bfd_byte *) everdef + iverdef->vd_aux));
6897 iverdaux = iverdef->vd_auxptr;
6898 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
6900 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
6902 iverdaux->vda_nodename =
6903 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6904 iverdaux->vda_name);
6905 if (iverdaux->vda_nodename == NULL)
6906 goto error_return_verdef;
6908 if (j + 1 < iverdef->vd_cnt)
6909 iverdaux->vda_nextptr = iverdaux + 1;
6910 else
6911 iverdaux->vda_nextptr = NULL;
6913 if (iverdaux->vda_next
6914 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
6915 goto error_return_verdef;
6917 everdaux = ((Elf_External_Verdaux *)
6918 ((bfd_byte *) everdaux + iverdaux->vda_next));
6921 if (iverdef->vd_cnt)
6922 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
6924 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
6925 iverdef->vd_nextdef = iverdef + 1;
6926 else
6927 iverdef->vd_nextdef = NULL;
6929 everdef = ((Elf_External_Verdef *)
6930 ((bfd_byte *) everdef + iverdef->vd_next));
6933 free (contents);
6934 contents = NULL;
6936 else if (default_imported_symver)
6938 if (freeidx < 3)
6939 freeidx = 3;
6940 else
6941 freeidx++;
6943 elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, freeidx,
6944 sizeof (Elf_Internal_Verdef));
6945 if (elf_tdata (abfd)->verdef == NULL)
6946 goto error_return;
6948 elf_tdata (abfd)->cverdefs = freeidx;
6951 /* Create a default version based on the soname. */
6952 if (default_imported_symver)
6954 Elf_Internal_Verdef *iverdef;
6955 Elf_Internal_Verdaux *iverdaux;
6957 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];;
6959 iverdef->vd_version = VER_DEF_CURRENT;
6960 iverdef->vd_flags = 0;
6961 iverdef->vd_ndx = freeidx;
6962 iverdef->vd_cnt = 1;
6964 iverdef->vd_bfd = abfd;
6966 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
6967 if (iverdef->vd_nodename == NULL)
6968 goto error_return_verdef;
6969 iverdef->vd_nextdef = NULL;
6970 iverdef->vd_auxptr = bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux));
6971 if (iverdef->vd_auxptr == NULL)
6972 goto error_return_verdef;
6974 iverdaux = iverdef->vd_auxptr;
6975 iverdaux->vda_nodename = iverdef->vd_nodename;
6976 iverdaux->vda_nextptr = NULL;
6979 return TRUE;
6981 error_return:
6982 if (contents != NULL)
6983 free (contents);
6984 return FALSE;
6987 asymbol *
6988 _bfd_elf_make_empty_symbol (bfd *abfd)
6990 elf_symbol_type *newsym;
6991 bfd_size_type amt = sizeof (elf_symbol_type);
6993 newsym = bfd_zalloc (abfd, amt);
6994 if (!newsym)
6995 return NULL;
6996 else
6998 newsym->symbol.the_bfd = abfd;
6999 return &newsym->symbol;
7003 void
7004 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
7005 asymbol *symbol,
7006 symbol_info *ret)
7008 bfd_symbol_info (symbol, ret);
7011 /* Return whether a symbol name implies a local symbol. Most targets
7012 use this function for the is_local_label_name entry point, but some
7013 override it. */
7015 bfd_boolean
7016 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
7017 const char *name)
7019 /* Normal local symbols start with ``.L''. */
7020 if (name[0] == '.' && name[1] == 'L')
7021 return TRUE;
7023 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7024 DWARF debugging symbols starting with ``..''. */
7025 if (name[0] == '.' && name[1] == '.')
7026 return TRUE;
7028 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7029 emitting DWARF debugging output. I suspect this is actually a
7030 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7031 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7032 underscore to be emitted on some ELF targets). For ease of use,
7033 we treat such symbols as local. */
7034 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
7035 return TRUE;
7037 return FALSE;
7040 alent *
7041 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
7042 asymbol *symbol ATTRIBUTE_UNUSED)
7044 abort ();
7045 return NULL;
7048 bfd_boolean
7049 _bfd_elf_set_arch_mach (bfd *abfd,
7050 enum bfd_architecture arch,
7051 unsigned long machine)
7053 /* If this isn't the right architecture for this backend, and this
7054 isn't the generic backend, fail. */
7055 if (arch != get_elf_backend_data (abfd)->arch
7056 && arch != bfd_arch_unknown
7057 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
7058 return FALSE;
7060 return bfd_default_set_arch_mach (abfd, arch, machine);
7063 /* Find the function to a particular section and offset,
7064 for error reporting. */
7066 static bfd_boolean
7067 elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
7068 asection *section,
7069 asymbol **symbols,
7070 bfd_vma offset,
7071 const char **filename_ptr,
7072 const char **functionname_ptr)
7074 const char *filename;
7075 asymbol *func, *file;
7076 bfd_vma low_func;
7077 asymbol **p;
7078 /* ??? Given multiple file symbols, it is impossible to reliably
7079 choose the right file name for global symbols. File symbols are
7080 local symbols, and thus all file symbols must sort before any
7081 global symbols. The ELF spec may be interpreted to say that a
7082 file symbol must sort before other local symbols, but currently
7083 ld -r doesn't do this. So, for ld -r output, it is possible to
7084 make a better choice of file name for local symbols by ignoring
7085 file symbols appearing after a given local symbol. */
7086 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
7088 filename = NULL;
7089 func = NULL;
7090 file = NULL;
7091 low_func = 0;
7092 state = nothing_seen;
7094 for (p = symbols; *p != NULL; p++)
7096 elf_symbol_type *q;
7098 q = (elf_symbol_type *) *p;
7100 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
7102 default:
7103 break;
7104 case STT_FILE:
7105 file = &q->symbol;
7106 if (state == symbol_seen)
7107 state = file_after_symbol_seen;
7108 continue;
7109 case STT_NOTYPE:
7110 case STT_FUNC:
7111 if (bfd_get_section (&q->symbol) == section
7112 && q->symbol.value >= low_func
7113 && q->symbol.value <= offset)
7115 func = (asymbol *) q;
7116 low_func = q->symbol.value;
7117 filename = NULL;
7118 if (file != NULL
7119 && (ELF_ST_BIND (q->internal_elf_sym.st_info) == STB_LOCAL
7120 || state != file_after_symbol_seen))
7121 filename = bfd_asymbol_name (file);
7123 break;
7125 if (state == nothing_seen)
7126 state = symbol_seen;
7129 if (func == NULL)
7130 return FALSE;
7132 if (filename_ptr)
7133 *filename_ptr = filename;
7134 if (functionname_ptr)
7135 *functionname_ptr = bfd_asymbol_name (func);
7137 return TRUE;
7140 /* Find the nearest line to a particular section and offset,
7141 for error reporting. */
7143 bfd_boolean
7144 _bfd_elf_find_nearest_line (bfd *abfd,
7145 asection *section,
7146 asymbol **symbols,
7147 bfd_vma offset,
7148 const char **filename_ptr,
7149 const char **functionname_ptr,
7150 unsigned int *line_ptr)
7152 bfd_boolean found;
7154 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
7155 filename_ptr, functionname_ptr,
7156 line_ptr))
7158 if (!*functionname_ptr)
7159 elf_find_function (abfd, section, symbols, offset,
7160 *filename_ptr ? NULL : filename_ptr,
7161 functionname_ptr);
7163 return TRUE;
7166 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
7167 filename_ptr, functionname_ptr,
7168 line_ptr, 0,
7169 &elf_tdata (abfd)->dwarf2_find_line_info))
7171 if (!*functionname_ptr)
7172 elf_find_function (abfd, section, symbols, offset,
7173 *filename_ptr ? NULL : filename_ptr,
7174 functionname_ptr);
7176 return TRUE;
7179 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
7180 &found, filename_ptr,
7181 functionname_ptr, line_ptr,
7182 &elf_tdata (abfd)->line_info))
7183 return FALSE;
7184 if (found && (*functionname_ptr || *line_ptr))
7185 return TRUE;
7187 if (symbols == NULL)
7188 return FALSE;
7190 if (! elf_find_function (abfd, section, symbols, offset,
7191 filename_ptr, functionname_ptr))
7192 return FALSE;
7194 *line_ptr = 0;
7195 return TRUE;
7198 /* Find the line for a symbol. */
7200 bfd_boolean
7201 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
7202 const char **filename_ptr, unsigned int *line_ptr)
7204 return _bfd_dwarf2_find_line (abfd, symbols, symbol,
7205 filename_ptr, line_ptr, 0,
7206 &elf_tdata (abfd)->dwarf2_find_line_info);
7209 /* After a call to bfd_find_nearest_line, successive calls to
7210 bfd_find_inliner_info can be used to get source information about
7211 each level of function inlining that terminated at the address
7212 passed to bfd_find_nearest_line. Currently this is only supported
7213 for DWARF2 with appropriate DWARF3 extensions. */
7215 bfd_boolean
7216 _bfd_elf_find_inliner_info (bfd *abfd,
7217 const char **filename_ptr,
7218 const char **functionname_ptr,
7219 unsigned int *line_ptr)
7221 bfd_boolean found;
7222 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
7223 functionname_ptr, line_ptr,
7224 & elf_tdata (abfd)->dwarf2_find_line_info);
7225 return found;
7229 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
7231 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7232 int ret = bed->s->sizeof_ehdr;
7234 if (!info->relocatable)
7236 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
7238 if (phdr_size == (bfd_size_type) -1)
7240 struct elf_segment_map *m;
7242 phdr_size = 0;
7243 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
7244 phdr_size += bed->s->sizeof_phdr;
7246 if (phdr_size == 0)
7247 phdr_size = get_program_header_size (abfd, info);
7250 elf_tdata (abfd)->program_header_size = phdr_size;
7251 ret += phdr_size;
7254 return ret;
7257 bfd_boolean
7258 _bfd_elf_set_section_contents (bfd *abfd,
7259 sec_ptr section,
7260 const void *location,
7261 file_ptr offset,
7262 bfd_size_type count)
7264 Elf_Internal_Shdr *hdr;
7265 bfd_signed_vma pos;
7267 if (! abfd->output_has_begun
7268 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7269 return FALSE;
7271 hdr = &elf_section_data (section)->this_hdr;
7272 pos = hdr->sh_offset + offset;
7273 if (bfd_seek (abfd, pos, SEEK_SET) != 0
7274 || bfd_bwrite (location, count, abfd) != count)
7275 return FALSE;
7277 return TRUE;
7280 void
7281 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
7282 arelent *cache_ptr ATTRIBUTE_UNUSED,
7283 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
7285 abort ();
7288 /* Try to convert a non-ELF reloc into an ELF one. */
7290 bfd_boolean
7291 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
7293 /* Check whether we really have an ELF howto. */
7295 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
7297 bfd_reloc_code_real_type code;
7298 reloc_howto_type *howto;
7300 /* Alien reloc: Try to determine its type to replace it with an
7301 equivalent ELF reloc. */
7303 if (areloc->howto->pc_relative)
7305 switch (areloc->howto->bitsize)
7307 case 8:
7308 code = BFD_RELOC_8_PCREL;
7309 break;
7310 case 12:
7311 code = BFD_RELOC_12_PCREL;
7312 break;
7313 case 16:
7314 code = BFD_RELOC_16_PCREL;
7315 break;
7316 case 24:
7317 code = BFD_RELOC_24_PCREL;
7318 break;
7319 case 32:
7320 code = BFD_RELOC_32_PCREL;
7321 break;
7322 case 64:
7323 code = BFD_RELOC_64_PCREL;
7324 break;
7325 default:
7326 goto fail;
7329 howto = bfd_reloc_type_lookup (abfd, code);
7331 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
7333 if (howto->pcrel_offset)
7334 areloc->addend += areloc->address;
7335 else
7336 areloc->addend -= areloc->address; /* addend is unsigned!! */
7339 else
7341 switch (areloc->howto->bitsize)
7343 case 8:
7344 code = BFD_RELOC_8;
7345 break;
7346 case 14:
7347 code = BFD_RELOC_14;
7348 break;
7349 case 16:
7350 code = BFD_RELOC_16;
7351 break;
7352 case 26:
7353 code = BFD_RELOC_26;
7354 break;
7355 case 32:
7356 code = BFD_RELOC_32;
7357 break;
7358 case 64:
7359 code = BFD_RELOC_64;
7360 break;
7361 default:
7362 goto fail;
7365 howto = bfd_reloc_type_lookup (abfd, code);
7368 if (howto)
7369 areloc->howto = howto;
7370 else
7371 goto fail;
7374 return TRUE;
7376 fail:
7377 (*_bfd_error_handler)
7378 (_("%B: unsupported relocation type %s"),
7379 abfd, areloc->howto->name);
7380 bfd_set_error (bfd_error_bad_value);
7381 return FALSE;
7384 bfd_boolean
7385 _bfd_elf_close_and_cleanup (bfd *abfd)
7387 if (bfd_get_format (abfd) == bfd_object)
7389 if (elf_tdata (abfd) != NULL && elf_shstrtab (abfd) != NULL)
7390 _bfd_elf_strtab_free (elf_shstrtab (abfd));
7391 _bfd_dwarf2_cleanup_debug_info (abfd);
7394 return _bfd_generic_close_and_cleanup (abfd);
7397 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7398 in the relocation's offset. Thus we cannot allow any sort of sanity
7399 range-checking to interfere. There is nothing else to do in processing
7400 this reloc. */
7402 bfd_reloc_status_type
7403 _bfd_elf_rel_vtable_reloc_fn
7404 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
7405 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
7406 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
7407 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
7409 return bfd_reloc_ok;
7412 /* Elf core file support. Much of this only works on native
7413 toolchains, since we rely on knowing the
7414 machine-dependent procfs structure in order to pick
7415 out details about the corefile. */
7417 #ifdef HAVE_SYS_PROCFS_H
7418 # include <sys/procfs.h>
7419 #endif
7421 /* FIXME: this is kinda wrong, but it's what gdb wants. */
7423 static int
7424 elfcore_make_pid (bfd *abfd)
7426 return ((elf_tdata (abfd)->core_lwpid << 16)
7427 + (elf_tdata (abfd)->core_pid));
7430 /* If there isn't a section called NAME, make one, using
7431 data from SECT. Note, this function will generate a
7432 reference to NAME, so you shouldn't deallocate or
7433 overwrite it. */
7435 static bfd_boolean
7436 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
7438 asection *sect2;
7440 if (bfd_get_section_by_name (abfd, name) != NULL)
7441 return TRUE;
7443 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
7444 if (sect2 == NULL)
7445 return FALSE;
7447 sect2->size = sect->size;
7448 sect2->filepos = sect->filepos;
7449 sect2->alignment_power = sect->alignment_power;
7450 return TRUE;
7453 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
7454 actually creates up to two pseudosections:
7455 - For the single-threaded case, a section named NAME, unless
7456 such a section already exists.
7457 - For the multi-threaded case, a section named "NAME/PID", where
7458 PID is elfcore_make_pid (abfd).
7459 Both pseudosections have identical contents. */
7460 bfd_boolean
7461 _bfd_elfcore_make_pseudosection (bfd *abfd,
7462 char *name,
7463 size_t size,
7464 ufile_ptr filepos)
7466 char buf[100];
7467 char *threaded_name;
7468 size_t len;
7469 asection *sect;
7471 /* Build the section name. */
7473 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
7474 len = strlen (buf) + 1;
7475 threaded_name = bfd_alloc (abfd, len);
7476 if (threaded_name == NULL)
7477 return FALSE;
7478 memcpy (threaded_name, buf, len);
7480 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
7481 SEC_HAS_CONTENTS);
7482 if (sect == NULL)
7483 return FALSE;
7484 sect->size = size;
7485 sect->filepos = filepos;
7486 sect->alignment_power = 2;
7488 return elfcore_maybe_make_sect (abfd, name, sect);
7491 /* prstatus_t exists on:
7492 solaris 2.5+
7493 linux 2.[01] + glibc
7494 unixware 4.2
7497 #if defined (HAVE_PRSTATUS_T)
7499 static bfd_boolean
7500 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
7502 size_t size;
7503 int offset;
7505 if (note->descsz == sizeof (prstatus_t))
7507 prstatus_t prstat;
7509 size = sizeof (prstat.pr_reg);
7510 offset = offsetof (prstatus_t, pr_reg);
7511 memcpy (&prstat, note->descdata, sizeof (prstat));
7513 /* Do not overwrite the core signal if it
7514 has already been set by another thread. */
7515 if (elf_tdata (abfd)->core_signal == 0)
7516 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7517 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7519 /* pr_who exists on:
7520 solaris 2.5+
7521 unixware 4.2
7522 pr_who doesn't exist on:
7523 linux 2.[01]
7525 #if defined (HAVE_PRSTATUS_T_PR_WHO)
7526 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7527 #endif
7529 #if defined (HAVE_PRSTATUS32_T)
7530 else if (note->descsz == sizeof (prstatus32_t))
7532 /* 64-bit host, 32-bit corefile */
7533 prstatus32_t prstat;
7535 size = sizeof (prstat.pr_reg);
7536 offset = offsetof (prstatus32_t, pr_reg);
7537 memcpy (&prstat, note->descdata, sizeof (prstat));
7539 /* Do not overwrite the core signal if it
7540 has already been set by another thread. */
7541 if (elf_tdata (abfd)->core_signal == 0)
7542 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7543 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7545 /* pr_who exists on:
7546 solaris 2.5+
7547 unixware 4.2
7548 pr_who doesn't exist on:
7549 linux 2.[01]
7551 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
7552 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7553 #endif
7555 #endif /* HAVE_PRSTATUS32_T */
7556 else
7558 /* Fail - we don't know how to handle any other
7559 note size (ie. data object type). */
7560 return TRUE;
7563 /* Make a ".reg/999" section and a ".reg" section. */
7564 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
7565 size, note->descpos + offset);
7567 #endif /* defined (HAVE_PRSTATUS_T) */
7569 /* Create a pseudosection containing the exact contents of NOTE. */
7570 static bfd_boolean
7571 elfcore_make_note_pseudosection (bfd *abfd,
7572 char *name,
7573 Elf_Internal_Note *note)
7575 return _bfd_elfcore_make_pseudosection (abfd, name,
7576 note->descsz, note->descpos);
7579 /* There isn't a consistent prfpregset_t across platforms,
7580 but it doesn't matter, because we don't have to pick this
7581 data structure apart. */
7583 static bfd_boolean
7584 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
7586 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7589 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
7590 type of 5 (NT_PRXFPREG). Just include the whole note's contents
7591 literally. */
7593 static bfd_boolean
7594 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
7596 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
7599 #if defined (HAVE_PRPSINFO_T)
7600 typedef prpsinfo_t elfcore_psinfo_t;
7601 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
7602 typedef prpsinfo32_t elfcore_psinfo32_t;
7603 #endif
7604 #endif
7606 #if defined (HAVE_PSINFO_T)
7607 typedef psinfo_t elfcore_psinfo_t;
7608 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
7609 typedef psinfo32_t elfcore_psinfo32_t;
7610 #endif
7611 #endif
7613 /* return a malloc'ed copy of a string at START which is at
7614 most MAX bytes long, possibly without a terminating '\0'.
7615 the copy will always have a terminating '\0'. */
7617 char *
7618 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
7620 char *dups;
7621 char *end = memchr (start, '\0', max);
7622 size_t len;
7624 if (end == NULL)
7625 len = max;
7626 else
7627 len = end - start;
7629 dups = bfd_alloc (abfd, len + 1);
7630 if (dups == NULL)
7631 return NULL;
7633 memcpy (dups, start, len);
7634 dups[len] = '\0';
7636 return dups;
7639 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7640 static bfd_boolean
7641 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
7643 if (note->descsz == sizeof (elfcore_psinfo_t))
7645 elfcore_psinfo_t psinfo;
7647 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7649 elf_tdata (abfd)->core_program
7650 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7651 sizeof (psinfo.pr_fname));
7653 elf_tdata (abfd)->core_command
7654 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7655 sizeof (psinfo.pr_psargs));
7657 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
7658 else if (note->descsz == sizeof (elfcore_psinfo32_t))
7660 /* 64-bit host, 32-bit corefile */
7661 elfcore_psinfo32_t psinfo;
7663 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7665 elf_tdata (abfd)->core_program
7666 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7667 sizeof (psinfo.pr_fname));
7669 elf_tdata (abfd)->core_command
7670 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7671 sizeof (psinfo.pr_psargs));
7673 #endif
7675 else
7677 /* Fail - we don't know how to handle any other
7678 note size (ie. data object type). */
7679 return TRUE;
7682 /* Note that for some reason, a spurious space is tacked
7683 onto the end of the args in some (at least one anyway)
7684 implementations, so strip it off if it exists. */
7687 char *command = elf_tdata (abfd)->core_command;
7688 int n = strlen (command);
7690 if (0 < n && command[n - 1] == ' ')
7691 command[n - 1] = '\0';
7694 return TRUE;
7696 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
7698 #if defined (HAVE_PSTATUS_T)
7699 static bfd_boolean
7700 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
7702 if (note->descsz == sizeof (pstatus_t)
7703 #if defined (HAVE_PXSTATUS_T)
7704 || note->descsz == sizeof (pxstatus_t)
7705 #endif
7708 pstatus_t pstat;
7710 memcpy (&pstat, note->descdata, sizeof (pstat));
7712 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7714 #if defined (HAVE_PSTATUS32_T)
7715 else if (note->descsz == sizeof (pstatus32_t))
7717 /* 64-bit host, 32-bit corefile */
7718 pstatus32_t pstat;
7720 memcpy (&pstat, note->descdata, sizeof (pstat));
7722 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7724 #endif
7725 /* Could grab some more details from the "representative"
7726 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
7727 NT_LWPSTATUS note, presumably. */
7729 return TRUE;
7731 #endif /* defined (HAVE_PSTATUS_T) */
7733 #if defined (HAVE_LWPSTATUS_T)
7734 static bfd_boolean
7735 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
7737 lwpstatus_t lwpstat;
7738 char buf[100];
7739 char *name;
7740 size_t len;
7741 asection *sect;
7743 if (note->descsz != sizeof (lwpstat)
7744 #if defined (HAVE_LWPXSTATUS_T)
7745 && note->descsz != sizeof (lwpxstatus_t)
7746 #endif
7748 return TRUE;
7750 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
7752 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
7753 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
7755 /* Make a ".reg/999" section. */
7757 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
7758 len = strlen (buf) + 1;
7759 name = bfd_alloc (abfd, len);
7760 if (name == NULL)
7761 return FALSE;
7762 memcpy (name, buf, len);
7764 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7765 if (sect == NULL)
7766 return FALSE;
7768 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7769 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
7770 sect->filepos = note->descpos
7771 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
7772 #endif
7774 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7775 sect->size = sizeof (lwpstat.pr_reg);
7776 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
7777 #endif
7779 sect->alignment_power = 2;
7781 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
7782 return FALSE;
7784 /* Make a ".reg2/999" section */
7786 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
7787 len = strlen (buf) + 1;
7788 name = bfd_alloc (abfd, len);
7789 if (name == NULL)
7790 return FALSE;
7791 memcpy (name, buf, len);
7793 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7794 if (sect == NULL)
7795 return FALSE;
7797 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7798 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
7799 sect->filepos = note->descpos
7800 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
7801 #endif
7803 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
7804 sect->size = sizeof (lwpstat.pr_fpreg);
7805 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
7806 #endif
7808 sect->alignment_power = 2;
7810 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
7812 #endif /* defined (HAVE_LWPSTATUS_T) */
7814 #if defined (HAVE_WIN32_PSTATUS_T)
7815 static bfd_boolean
7816 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
7818 char buf[30];
7819 char *name;
7820 size_t len;
7821 asection *sect;
7822 win32_pstatus_t pstatus;
7824 if (note->descsz < sizeof (pstatus))
7825 return TRUE;
7827 memcpy (&pstatus, note->descdata, sizeof (pstatus));
7829 switch (pstatus.data_type)
7831 case NOTE_INFO_PROCESS:
7832 /* FIXME: need to add ->core_command. */
7833 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
7834 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
7835 break;
7837 case NOTE_INFO_THREAD:
7838 /* Make a ".reg/999" section. */
7839 sprintf (buf, ".reg/%ld", (long) pstatus.data.thread_info.tid);
7841 len = strlen (buf) + 1;
7842 name = bfd_alloc (abfd, len);
7843 if (name == NULL)
7844 return FALSE;
7846 memcpy (name, buf, len);
7848 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7849 if (sect == NULL)
7850 return FALSE;
7852 sect->size = sizeof (pstatus.data.thread_info.thread_context);
7853 sect->filepos = (note->descpos
7854 + offsetof (struct win32_pstatus,
7855 data.thread_info.thread_context));
7856 sect->alignment_power = 2;
7858 if (pstatus.data.thread_info.is_active_thread)
7859 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
7860 return FALSE;
7861 break;
7863 case NOTE_INFO_MODULE:
7864 /* Make a ".module/xxxxxxxx" section. */
7865 sprintf (buf, ".module/%08lx",
7866 (long) pstatus.data.module_info.base_address);
7868 len = strlen (buf) + 1;
7869 name = bfd_alloc (abfd, len);
7870 if (name == NULL)
7871 return FALSE;
7873 memcpy (name, buf, len);
7875 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7877 if (sect == NULL)
7878 return FALSE;
7880 sect->size = note->descsz;
7881 sect->filepos = note->descpos;
7882 sect->alignment_power = 2;
7883 break;
7885 default:
7886 return TRUE;
7889 return TRUE;
7891 #endif /* HAVE_WIN32_PSTATUS_T */
7893 static bfd_boolean
7894 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
7896 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7898 switch (note->type)
7900 default:
7901 return TRUE;
7903 case NT_PRSTATUS:
7904 if (bed->elf_backend_grok_prstatus)
7905 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
7906 return TRUE;
7907 #if defined (HAVE_PRSTATUS_T)
7908 return elfcore_grok_prstatus (abfd, note);
7909 #else
7910 return TRUE;
7911 #endif
7913 #if defined (HAVE_PSTATUS_T)
7914 case NT_PSTATUS:
7915 return elfcore_grok_pstatus (abfd, note);
7916 #endif
7918 #if defined (HAVE_LWPSTATUS_T)
7919 case NT_LWPSTATUS:
7920 return elfcore_grok_lwpstatus (abfd, note);
7921 #endif
7923 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
7924 return elfcore_grok_prfpreg (abfd, note);
7926 #if defined (HAVE_WIN32_PSTATUS_T)
7927 case NT_WIN32PSTATUS:
7928 return elfcore_grok_win32pstatus (abfd, note);
7929 #endif
7931 case NT_PRXFPREG: /* Linux SSE extension */
7932 if (note->namesz == 6
7933 && strcmp (note->namedata, "LINUX") == 0)
7934 return elfcore_grok_prxfpreg (abfd, note);
7935 else
7936 return TRUE;
7938 case NT_PRPSINFO:
7939 case NT_PSINFO:
7940 if (bed->elf_backend_grok_psinfo)
7941 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
7942 return TRUE;
7943 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7944 return elfcore_grok_psinfo (abfd, note);
7945 #else
7946 return TRUE;
7947 #endif
7949 case NT_AUXV:
7951 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
7952 SEC_HAS_CONTENTS);
7954 if (sect == NULL)
7955 return FALSE;
7956 sect->size = note->descsz;
7957 sect->filepos = note->descpos;
7958 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
7960 return TRUE;
7965 static bfd_boolean
7966 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
7968 char *cp;
7970 cp = strchr (note->namedata, '@');
7971 if (cp != NULL)
7973 *lwpidp = atoi(cp + 1);
7974 return TRUE;
7976 return FALSE;
7979 static bfd_boolean
7980 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
7983 /* Signal number at offset 0x08. */
7984 elf_tdata (abfd)->core_signal
7985 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
7987 /* Process ID at offset 0x50. */
7988 elf_tdata (abfd)->core_pid
7989 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
7991 /* Command name at 0x7c (max 32 bytes, including nul). */
7992 elf_tdata (abfd)->core_command
7993 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
7995 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
7996 note);
7999 static bfd_boolean
8000 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
8002 int lwp;
8004 if (elfcore_netbsd_get_lwpid (note, &lwp))
8005 elf_tdata (abfd)->core_lwpid = lwp;
8007 if (note->type == NT_NETBSDCORE_PROCINFO)
8009 /* NetBSD-specific core "procinfo". Note that we expect to
8010 find this note before any of the others, which is fine,
8011 since the kernel writes this note out first when it
8012 creates a core file. */
8014 return elfcore_grok_netbsd_procinfo (abfd, note);
8017 /* As of Jan 2002 there are no other machine-independent notes
8018 defined for NetBSD core files. If the note type is less
8019 than the start of the machine-dependent note types, we don't
8020 understand it. */
8022 if (note->type < NT_NETBSDCORE_FIRSTMACH)
8023 return TRUE;
8026 switch (bfd_get_arch (abfd))
8028 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8029 PT_GETFPREGS == mach+2. */
8031 case bfd_arch_alpha:
8032 case bfd_arch_sparc:
8033 switch (note->type)
8035 case NT_NETBSDCORE_FIRSTMACH+0:
8036 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8038 case NT_NETBSDCORE_FIRSTMACH+2:
8039 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8041 default:
8042 return TRUE;
8045 /* On all other arch's, PT_GETREGS == mach+1 and
8046 PT_GETFPREGS == mach+3. */
8048 default:
8049 switch (note->type)
8051 case NT_NETBSDCORE_FIRSTMACH+1:
8052 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8054 case NT_NETBSDCORE_FIRSTMACH+3:
8055 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8057 default:
8058 return TRUE;
8061 /* NOTREACHED */
8064 static bfd_boolean
8065 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
8067 void *ddata = note->descdata;
8068 char buf[100];
8069 char *name;
8070 asection *sect;
8071 short sig;
8072 unsigned flags;
8074 /* nto_procfs_status 'pid' field is at offset 0. */
8075 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
8077 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
8078 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
8080 /* nto_procfs_status 'flags' field is at offset 8. */
8081 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
8083 /* nto_procfs_status 'what' field is at offset 14. */
8084 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
8086 elf_tdata (abfd)->core_signal = sig;
8087 elf_tdata (abfd)->core_lwpid = *tid;
8090 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
8091 do not come from signals so we make sure we set the current
8092 thread just in case. */
8093 if (flags & 0x00000080)
8094 elf_tdata (abfd)->core_lwpid = *tid;
8096 /* Make a ".qnx_core_status/%d" section. */
8097 sprintf (buf, ".qnx_core_status/%ld", *tid);
8099 name = bfd_alloc (abfd, strlen (buf) + 1);
8100 if (name == NULL)
8101 return FALSE;
8102 strcpy (name, buf);
8104 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8105 if (sect == NULL)
8106 return FALSE;
8108 sect->size = note->descsz;
8109 sect->filepos = note->descpos;
8110 sect->alignment_power = 2;
8112 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
8115 static bfd_boolean
8116 elfcore_grok_nto_regs (bfd *abfd,
8117 Elf_Internal_Note *note,
8118 long tid,
8119 char *base)
8121 char buf[100];
8122 char *name;
8123 asection *sect;
8125 /* Make a "(base)/%d" section. */
8126 sprintf (buf, "%s/%ld", base, tid);
8128 name = bfd_alloc (abfd, strlen (buf) + 1);
8129 if (name == NULL)
8130 return FALSE;
8131 strcpy (name, buf);
8133 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8134 if (sect == NULL)
8135 return FALSE;
8137 sect->size = note->descsz;
8138 sect->filepos = note->descpos;
8139 sect->alignment_power = 2;
8141 /* This is the current thread. */
8142 if (elf_tdata (abfd)->core_lwpid == tid)
8143 return elfcore_maybe_make_sect (abfd, base, sect);
8145 return TRUE;
8148 #define BFD_QNT_CORE_INFO 7
8149 #define BFD_QNT_CORE_STATUS 8
8150 #define BFD_QNT_CORE_GREG 9
8151 #define BFD_QNT_CORE_FPREG 10
8153 static bfd_boolean
8154 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
8156 /* Every GREG section has a STATUS section before it. Store the
8157 tid from the previous call to pass down to the next gregs
8158 function. */
8159 static long tid = 1;
8161 switch (note->type)
8163 case BFD_QNT_CORE_INFO:
8164 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
8165 case BFD_QNT_CORE_STATUS:
8166 return elfcore_grok_nto_status (abfd, note, &tid);
8167 case BFD_QNT_CORE_GREG:
8168 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
8169 case BFD_QNT_CORE_FPREG:
8170 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
8171 default:
8172 return TRUE;
8176 /* Function: elfcore_write_note
8178 Inputs:
8179 buffer to hold note, and current size of buffer
8180 name of note
8181 type of note
8182 data for note
8183 size of data for note
8185 Writes note to end of buffer. ELF64 notes are written exactly as
8186 for ELF32, despite the current (as of 2006) ELF gabi specifying
8187 that they ought to have 8-byte namesz and descsz field, and have
8188 8-byte alignment. Other writers, eg. Linux kernel, do the same.
8190 Return:
8191 Pointer to realloc'd buffer, *BUFSIZ updated. */
8193 char *
8194 elfcore_write_note (bfd *abfd,
8195 char *buf,
8196 int *bufsiz,
8197 const char *name,
8198 int type,
8199 const void *input,
8200 int size)
8202 Elf_External_Note *xnp;
8203 size_t namesz;
8204 size_t newspace;
8205 char *dest;
8207 namesz = 0;
8208 if (name != NULL)
8209 namesz = strlen (name) + 1;
8211 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
8213 buf = realloc (buf, *bufsiz + newspace);
8214 dest = buf + *bufsiz;
8215 *bufsiz += newspace;
8216 xnp = (Elf_External_Note *) dest;
8217 H_PUT_32 (abfd, namesz, xnp->namesz);
8218 H_PUT_32 (abfd, size, xnp->descsz);
8219 H_PUT_32 (abfd, type, xnp->type);
8220 dest = xnp->name;
8221 if (name != NULL)
8223 memcpy (dest, name, namesz);
8224 dest += namesz;
8225 while (namesz & 3)
8227 *dest++ = '\0';
8228 ++namesz;
8231 memcpy (dest, input, size);
8232 dest += size;
8233 while (size & 3)
8235 *dest++ = '\0';
8236 ++size;
8238 return buf;
8241 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8242 char *
8243 elfcore_write_prpsinfo (bfd *abfd,
8244 char *buf,
8245 int *bufsiz,
8246 const char *fname,
8247 const char *psargs)
8249 const char *note_name = "CORE";
8250 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8252 if (bed->elf_backend_write_core_note != NULL)
8254 char *ret;
8255 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8256 NT_PRPSINFO, fname, psargs);
8257 if (ret != NULL)
8258 return ret;
8261 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8262 if (bed->s->elfclass == ELFCLASS32)
8264 #if defined (HAVE_PSINFO32_T)
8265 psinfo32_t data;
8266 int note_type = NT_PSINFO;
8267 #else
8268 prpsinfo32_t data;
8269 int note_type = NT_PRPSINFO;
8270 #endif
8272 memset (&data, 0, sizeof (data));
8273 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8274 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8275 return elfcore_write_note (abfd, buf, bufsiz,
8276 note_name, note_type, &data, sizeof (data));
8278 else
8279 #endif
8281 #if defined (HAVE_PSINFO_T)
8282 psinfo_t data;
8283 int note_type = NT_PSINFO;
8284 #else
8285 prpsinfo_t data;
8286 int note_type = NT_PRPSINFO;
8287 #endif
8289 memset (&data, 0, sizeof (data));
8290 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8291 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8292 return elfcore_write_note (abfd, buf, bufsiz,
8293 note_name, note_type, &data, sizeof (data));
8296 #endif /* PSINFO_T or PRPSINFO_T */
8298 #if defined (HAVE_PRSTATUS_T)
8299 char *
8300 elfcore_write_prstatus (bfd *abfd,
8301 char *buf,
8302 int *bufsiz,
8303 long pid,
8304 int cursig,
8305 const void *gregs)
8307 const char *note_name = "CORE";
8308 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8310 if (bed->elf_backend_write_core_note != NULL)
8312 char *ret;
8313 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8314 NT_PRSTATUS,
8315 pid, cursig, gregs);
8316 if (ret != NULL)
8317 return ret;
8320 #if defined (HAVE_PRSTATUS32_T)
8321 if (bed->s->elfclass == ELFCLASS32)
8323 prstatus32_t prstat;
8325 memset (&prstat, 0, sizeof (prstat));
8326 prstat.pr_pid = pid;
8327 prstat.pr_cursig = cursig;
8328 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8329 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8330 NT_PRSTATUS, &prstat, sizeof (prstat));
8332 else
8333 #endif
8335 prstatus_t prstat;
8337 memset (&prstat, 0, sizeof (prstat));
8338 prstat.pr_pid = pid;
8339 prstat.pr_cursig = cursig;
8340 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8341 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8342 NT_PRSTATUS, &prstat, sizeof (prstat));
8345 #endif /* HAVE_PRSTATUS_T */
8347 #if defined (HAVE_LWPSTATUS_T)
8348 char *
8349 elfcore_write_lwpstatus (bfd *abfd,
8350 char *buf,
8351 int *bufsiz,
8352 long pid,
8353 int cursig,
8354 const void *gregs)
8356 lwpstatus_t lwpstat;
8357 const char *note_name = "CORE";
8359 memset (&lwpstat, 0, sizeof (lwpstat));
8360 lwpstat.pr_lwpid = pid >> 16;
8361 lwpstat.pr_cursig = cursig;
8362 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8363 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
8364 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8365 #if !defined(gregs)
8366 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
8367 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
8368 #else
8369 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
8370 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
8371 #endif
8372 #endif
8373 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8374 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
8376 #endif /* HAVE_LWPSTATUS_T */
8378 #if defined (HAVE_PSTATUS_T)
8379 char *
8380 elfcore_write_pstatus (bfd *abfd,
8381 char *buf,
8382 int *bufsiz,
8383 long pid,
8384 int cursig ATTRIBUTE_UNUSED,
8385 const void *gregs ATTRIBUTE_UNUSED)
8387 const char *note_name = "CORE";
8388 #if defined (HAVE_PSTATUS32_T)
8389 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8391 if (bed->s->elfclass == ELFCLASS32)
8393 pstatus32_t pstat;
8395 memset (&pstat, 0, sizeof (pstat));
8396 pstat.pr_pid = pid & 0xffff;
8397 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8398 NT_PSTATUS, &pstat, sizeof (pstat));
8399 return buf;
8401 else
8402 #endif
8404 pstatus_t pstat;
8406 memset (&pstat, 0, sizeof (pstat));
8407 pstat.pr_pid = pid & 0xffff;
8408 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8409 NT_PSTATUS, &pstat, sizeof (pstat));
8410 return buf;
8413 #endif /* HAVE_PSTATUS_T */
8415 char *
8416 elfcore_write_prfpreg (bfd *abfd,
8417 char *buf,
8418 int *bufsiz,
8419 const void *fpregs,
8420 int size)
8422 const char *note_name = "CORE";
8423 return elfcore_write_note (abfd, buf, bufsiz,
8424 note_name, NT_FPREGSET, fpregs, size);
8427 char *
8428 elfcore_write_prxfpreg (bfd *abfd,
8429 char *buf,
8430 int *bufsiz,
8431 const void *xfpregs,
8432 int size)
8434 char *note_name = "LINUX";
8435 return elfcore_write_note (abfd, buf, bufsiz,
8436 note_name, NT_PRXFPREG, xfpregs, size);
8439 static bfd_boolean
8440 elfcore_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
8442 char *buf;
8443 char *p;
8445 if (size <= 0)
8446 return TRUE;
8448 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
8449 return FALSE;
8451 buf = bfd_malloc (size);
8452 if (buf == NULL)
8453 return FALSE;
8455 if (bfd_bread (buf, size, abfd) != size)
8457 error:
8458 free (buf);
8459 return FALSE;
8462 p = buf;
8463 while (p < buf + size)
8465 /* FIXME: bad alignment assumption. */
8466 Elf_External_Note *xnp = (Elf_External_Note *) p;
8467 Elf_Internal_Note in;
8469 in.type = H_GET_32 (abfd, xnp->type);
8471 in.namesz = H_GET_32 (abfd, xnp->namesz);
8472 in.namedata = xnp->name;
8474 in.descsz = H_GET_32 (abfd, xnp->descsz);
8475 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
8476 in.descpos = offset + (in.descdata - buf);
8478 if (CONST_STRNEQ (in.namedata, "NetBSD-CORE"))
8480 if (! elfcore_grok_netbsd_note (abfd, &in))
8481 goto error;
8483 else if (CONST_STRNEQ (in.namedata, "QNX"))
8485 if (! elfcore_grok_nto_note (abfd, &in))
8486 goto error;
8488 else
8490 if (! elfcore_grok_note (abfd, &in))
8491 goto error;
8494 p = in.descdata + BFD_ALIGN (in.descsz, 4);
8497 free (buf);
8498 return TRUE;
8501 /* Providing external access to the ELF program header table. */
8503 /* Return an upper bound on the number of bytes required to store a
8504 copy of ABFD's program header table entries. Return -1 if an error
8505 occurs; bfd_get_error will return an appropriate code. */
8507 long
8508 bfd_get_elf_phdr_upper_bound (bfd *abfd)
8510 if (abfd->xvec->flavour != bfd_target_elf_flavour)
8512 bfd_set_error (bfd_error_wrong_format);
8513 return -1;
8516 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
8519 /* Copy ABFD's program header table entries to *PHDRS. The entries
8520 will be stored as an array of Elf_Internal_Phdr structures, as
8521 defined in include/elf/internal.h. To find out how large the
8522 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
8524 Return the number of program header table entries read, or -1 if an
8525 error occurs; bfd_get_error will return an appropriate code. */
8528 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
8530 int num_phdrs;
8532 if (abfd->xvec->flavour != bfd_target_elf_flavour)
8534 bfd_set_error (bfd_error_wrong_format);
8535 return -1;
8538 num_phdrs = elf_elfheader (abfd)->e_phnum;
8539 memcpy (phdrs, elf_tdata (abfd)->phdr,
8540 num_phdrs * sizeof (Elf_Internal_Phdr));
8542 return num_phdrs;
8545 void
8546 _bfd_elf_sprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, char *buf, bfd_vma value)
8548 #ifdef BFD64
8549 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
8551 i_ehdrp = elf_elfheader (abfd);
8552 if (i_ehdrp == NULL)
8553 sprintf_vma (buf, value);
8554 else
8556 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
8558 #if BFD_HOST_64BIT_LONG
8559 sprintf (buf, "%016lx", value);
8560 #else
8561 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
8562 _bfd_int64_low (value));
8563 #endif
8565 else
8566 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
8568 #else
8569 sprintf_vma (buf, value);
8570 #endif
8573 void
8574 _bfd_elf_fprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, void *stream, bfd_vma value)
8576 #ifdef BFD64
8577 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
8579 i_ehdrp = elf_elfheader (abfd);
8580 if (i_ehdrp == NULL)
8581 fprintf_vma ((FILE *) stream, value);
8582 else
8584 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
8586 #if BFD_HOST_64BIT_LONG
8587 fprintf ((FILE *) stream, "%016lx", value);
8588 #else
8589 fprintf ((FILE *) stream, "%08lx%08lx",
8590 _bfd_int64_high (value), _bfd_int64_low (value));
8591 #endif
8593 else
8594 fprintf ((FILE *) stream, "%08lx",
8595 (unsigned long) (value & 0xffffffff));
8597 #else
8598 fprintf_vma ((FILE *) stream, value);
8599 #endif
8602 enum elf_reloc_type_class
8603 _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
8605 return reloc_class_normal;
8608 /* For RELA architectures, return the relocation value for a
8609 relocation against a local symbol. */
8611 bfd_vma
8612 _bfd_elf_rela_local_sym (bfd *abfd,
8613 Elf_Internal_Sym *sym,
8614 asection **psec,
8615 Elf_Internal_Rela *rel)
8617 asection *sec = *psec;
8618 bfd_vma relocation;
8620 relocation = (sec->output_section->vma
8621 + sec->output_offset
8622 + sym->st_value);
8623 if ((sec->flags & SEC_MERGE)
8624 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
8625 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
8627 rel->r_addend =
8628 _bfd_merged_section_offset (abfd, psec,
8629 elf_section_data (sec)->sec_info,
8630 sym->st_value + rel->r_addend);
8631 if (sec != *psec)
8633 /* If we have changed the section, and our original section is
8634 marked with SEC_EXCLUDE, it means that the original
8635 SEC_MERGE section has been completely subsumed in some
8636 other SEC_MERGE section. In this case, we need to leave
8637 some info around for --emit-relocs. */
8638 if ((sec->flags & SEC_EXCLUDE) != 0)
8639 sec->kept_section = *psec;
8640 sec = *psec;
8642 rel->r_addend -= relocation;
8643 rel->r_addend += sec->output_section->vma + sec->output_offset;
8645 return relocation;
8648 bfd_vma
8649 _bfd_elf_rel_local_sym (bfd *abfd,
8650 Elf_Internal_Sym *sym,
8651 asection **psec,
8652 bfd_vma addend)
8654 asection *sec = *psec;
8656 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
8657 return sym->st_value + addend;
8659 return _bfd_merged_section_offset (abfd, psec,
8660 elf_section_data (sec)->sec_info,
8661 sym->st_value + addend);
8664 bfd_vma
8665 _bfd_elf_section_offset (bfd *abfd,
8666 struct bfd_link_info *info,
8667 asection *sec,
8668 bfd_vma offset)
8670 switch (sec->sec_info_type)
8672 case ELF_INFO_TYPE_STABS:
8673 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
8674 offset);
8675 case ELF_INFO_TYPE_EH_FRAME:
8676 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
8677 default:
8678 return offset;
8682 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
8683 reconstruct an ELF file by reading the segments out of remote memory
8684 based on the ELF file header at EHDR_VMA and the ELF program headers it
8685 points to. If not null, *LOADBASEP is filled in with the difference
8686 between the VMAs from which the segments were read, and the VMAs the
8687 file headers (and hence BFD's idea of each section's VMA) put them at.
8689 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
8690 remote memory at target address VMA into the local buffer at MYADDR; it
8691 should return zero on success or an `errno' code on failure. TEMPL must
8692 be a BFD for an ELF target with the word size and byte order found in
8693 the remote memory. */
8695 bfd *
8696 bfd_elf_bfd_from_remote_memory
8697 (bfd *templ,
8698 bfd_vma ehdr_vma,
8699 bfd_vma *loadbasep,
8700 int (*target_read_memory) (bfd_vma, bfd_byte *, int))
8702 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
8703 (templ, ehdr_vma, loadbasep, target_read_memory);
8706 long
8707 _bfd_elf_get_synthetic_symtab (bfd *abfd,
8708 long symcount ATTRIBUTE_UNUSED,
8709 asymbol **syms ATTRIBUTE_UNUSED,
8710 long dynsymcount,
8711 asymbol **dynsyms,
8712 asymbol **ret)
8714 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8715 asection *relplt;
8716 asymbol *s;
8717 const char *relplt_name;
8718 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
8719 arelent *p;
8720 long count, i, n;
8721 size_t size;
8722 Elf_Internal_Shdr *hdr;
8723 char *names;
8724 asection *plt;
8726 *ret = NULL;
8728 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
8729 return 0;
8731 if (dynsymcount <= 0)
8732 return 0;
8734 if (!bed->plt_sym_val)
8735 return 0;
8737 relplt_name = bed->relplt_name;
8738 if (relplt_name == NULL)
8739 relplt_name = bed->default_use_rela_p ? ".rela.plt" : ".rel.plt";
8740 relplt = bfd_get_section_by_name (abfd, relplt_name);
8741 if (relplt == NULL)
8742 return 0;
8744 hdr = &elf_section_data (relplt)->this_hdr;
8745 if (hdr->sh_link != elf_dynsymtab (abfd)
8746 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
8747 return 0;
8749 plt = bfd_get_section_by_name (abfd, ".plt");
8750 if (plt == NULL)
8751 return 0;
8753 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8754 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
8755 return -1;
8757 count = relplt->size / hdr->sh_entsize;
8758 size = count * sizeof (asymbol);
8759 p = relplt->relocation;
8760 for (i = 0; i < count; i++, s++, p++)
8761 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
8763 s = *ret = bfd_malloc (size);
8764 if (s == NULL)
8765 return -1;
8767 names = (char *) (s + count);
8768 p = relplt->relocation;
8769 n = 0;
8770 for (i = 0; i < count; i++, s++, p++)
8772 size_t len;
8773 bfd_vma addr;
8775 addr = bed->plt_sym_val (i, plt, p);
8776 if (addr == (bfd_vma) -1)
8777 continue;
8779 *s = **p->sym_ptr_ptr;
8780 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
8781 we are defining a symbol, ensure one of them is set. */
8782 if ((s->flags & BSF_LOCAL) == 0)
8783 s->flags |= BSF_GLOBAL;
8784 s->section = plt;
8785 s->value = addr - plt->vma;
8786 s->name = names;
8787 len = strlen ((*p->sym_ptr_ptr)->name);
8788 memcpy (names, (*p->sym_ptr_ptr)->name, len);
8789 names += len;
8790 memcpy (names, "@plt", sizeof ("@plt"));
8791 names += sizeof ("@plt");
8792 ++n;
8795 return n;
8798 struct elf_symbuf_symbol
8800 unsigned long st_name; /* Symbol name, index in string tbl */
8801 unsigned char st_info; /* Type and binding attributes */
8802 unsigned char st_other; /* Visibilty, and target specific */
8805 struct elf_symbuf_head
8807 struct elf_symbuf_symbol *ssym;
8808 bfd_size_type count;
8809 unsigned int st_shndx;
8812 struct elf_symbol
8814 union
8816 Elf_Internal_Sym *isym;
8817 struct elf_symbuf_symbol *ssym;
8818 } u;
8819 const char *name;
8822 /* Sort references to symbols by ascending section number. */
8824 static int
8825 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8827 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8828 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8830 return s1->st_shndx - s2->st_shndx;
8833 static int
8834 elf_sym_name_compare (const void *arg1, const void *arg2)
8836 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8837 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8838 return strcmp (s1->name, s2->name);
8841 static struct elf_symbuf_head *
8842 elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
8844 Elf_Internal_Sym **ind, **indbufend, **indbuf
8845 = bfd_malloc2 (symcount, sizeof (*indbuf));
8846 struct elf_symbuf_symbol *ssym;
8847 struct elf_symbuf_head *ssymbuf, *ssymhead;
8848 bfd_size_type i, shndx_count;
8850 if (indbuf == NULL)
8851 return NULL;
8853 for (ind = indbuf, i = 0; i < symcount; i++)
8854 if (isymbuf[i].st_shndx != SHN_UNDEF)
8855 *ind++ = &isymbuf[i];
8856 indbufend = ind;
8858 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8859 elf_sort_elf_symbol);
8861 shndx_count = 0;
8862 if (indbufend > indbuf)
8863 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8864 if (ind[0]->st_shndx != ind[1]->st_shndx)
8865 shndx_count++;
8867 ssymbuf = bfd_malloc ((shndx_count + 1) * sizeof (*ssymbuf)
8868 + (indbufend - indbuf) * sizeof (*ssymbuf));
8869 if (ssymbuf == NULL)
8871 free (indbuf);
8872 return NULL;
8875 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count);
8876 ssymbuf->ssym = NULL;
8877 ssymbuf->count = shndx_count;
8878 ssymbuf->st_shndx = 0;
8879 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8881 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8883 ssymhead++;
8884 ssymhead->ssym = ssym;
8885 ssymhead->count = 0;
8886 ssymhead->st_shndx = (*ind)->st_shndx;
8888 ssym->st_name = (*ind)->st_name;
8889 ssym->st_info = (*ind)->st_info;
8890 ssym->st_other = (*ind)->st_other;
8891 ssymhead->count++;
8893 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count);
8895 free (indbuf);
8896 return ssymbuf;
8899 /* Check if 2 sections define the same set of local and global
8900 symbols. */
8902 bfd_boolean
8903 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8904 struct bfd_link_info *info)
8906 bfd *bfd1, *bfd2;
8907 const struct elf_backend_data *bed1, *bed2;
8908 Elf_Internal_Shdr *hdr1, *hdr2;
8909 bfd_size_type symcount1, symcount2;
8910 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8911 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8912 Elf_Internal_Sym *isym, *isymend;
8913 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8914 bfd_size_type count1, count2, i;
8915 int shndx1, shndx2;
8916 bfd_boolean result;
8918 bfd1 = sec1->owner;
8919 bfd2 = sec2->owner;
8921 /* If both are .gnu.linkonce sections, they have to have the same
8922 section name. */
8923 if (CONST_STRNEQ (sec1->name, ".gnu.linkonce")
8924 && CONST_STRNEQ (sec2->name, ".gnu.linkonce"))
8925 return strcmp (sec1->name + sizeof ".gnu.linkonce",
8926 sec2->name + sizeof ".gnu.linkonce") == 0;
8928 /* Both sections have to be in ELF. */
8929 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8930 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8931 return FALSE;
8933 if (elf_section_type (sec1) != elf_section_type (sec2))
8934 return FALSE;
8936 if ((elf_section_flags (sec1) & SHF_GROUP) != 0
8937 && (elf_section_flags (sec2) & SHF_GROUP) != 0)
8939 /* If both are members of section groups, they have to have the
8940 same group name. */
8941 if (strcmp (elf_group_name (sec1), elf_group_name (sec2)) != 0)
8942 return FALSE;
8945 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8946 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8947 if (shndx1 == -1 || shndx2 == -1)
8948 return FALSE;
8950 bed1 = get_elf_backend_data (bfd1);
8951 bed2 = get_elf_backend_data (bfd2);
8952 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8953 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8954 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8955 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8957 if (symcount1 == 0 || symcount2 == 0)
8958 return FALSE;
8960 result = FALSE;
8961 isymbuf1 = NULL;
8962 isymbuf2 = NULL;
8963 ssymbuf1 = elf_tdata (bfd1)->symbuf;
8964 ssymbuf2 = elf_tdata (bfd2)->symbuf;
8966 if (ssymbuf1 == NULL)
8968 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8969 NULL, NULL, NULL);
8970 if (isymbuf1 == NULL)
8971 goto done;
8973 if (!info->reduce_memory_overheads)
8974 elf_tdata (bfd1)->symbuf = ssymbuf1
8975 = elf_create_symbuf (symcount1, isymbuf1);
8978 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8980 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8981 NULL, NULL, NULL);
8982 if (isymbuf2 == NULL)
8983 goto done;
8985 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
8986 elf_tdata (bfd2)->symbuf = ssymbuf2
8987 = elf_create_symbuf (symcount2, isymbuf2);
8990 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8992 /* Optimized faster version. */
8993 bfd_size_type lo, hi, mid;
8994 struct elf_symbol *symp;
8995 struct elf_symbuf_symbol *ssym, *ssymend;
8997 lo = 0;
8998 hi = ssymbuf1->count;
8999 ssymbuf1++;
9000 count1 = 0;
9001 while (lo < hi)
9003 mid = (lo + hi) / 2;
9004 if ((unsigned int) shndx1 < ssymbuf1[mid].st_shndx)
9005 hi = mid;
9006 else if ((unsigned int) shndx1 > ssymbuf1[mid].st_shndx)
9007 lo = mid + 1;
9008 else
9010 count1 = ssymbuf1[mid].count;
9011 ssymbuf1 += mid;
9012 break;
9016 lo = 0;
9017 hi = ssymbuf2->count;
9018 ssymbuf2++;
9019 count2 = 0;
9020 while (lo < hi)
9022 mid = (lo + hi) / 2;
9023 if ((unsigned int) shndx2 < ssymbuf2[mid].st_shndx)
9024 hi = mid;
9025 else if ((unsigned int) shndx2 > ssymbuf2[mid].st_shndx)
9026 lo = mid + 1;
9027 else
9029 count2 = ssymbuf2[mid].count;
9030 ssymbuf2 += mid;
9031 break;
9035 if (count1 == 0 || count2 == 0 || count1 != count2)
9036 goto done;
9038 symtable1 = bfd_malloc (count1 * sizeof (struct elf_symbol));
9039 symtable2 = bfd_malloc (count2 * sizeof (struct elf_symbol));
9040 if (symtable1 == NULL || symtable2 == NULL)
9041 goto done;
9043 symp = symtable1;
9044 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
9045 ssym < ssymend; ssym++, symp++)
9047 symp->u.ssym = ssym;
9048 symp->name = bfd_elf_string_from_elf_section (bfd1,
9049 hdr1->sh_link,
9050 ssym->st_name);
9053 symp = symtable2;
9054 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
9055 ssym < ssymend; ssym++, symp++)
9057 symp->u.ssym = ssym;
9058 symp->name = bfd_elf_string_from_elf_section (bfd2,
9059 hdr2->sh_link,
9060 ssym->st_name);
9063 /* Sort symbol by name. */
9064 qsort (symtable1, count1, sizeof (struct elf_symbol),
9065 elf_sym_name_compare);
9066 qsort (symtable2, count1, sizeof (struct elf_symbol),
9067 elf_sym_name_compare);
9069 for (i = 0; i < count1; i++)
9070 /* Two symbols must have the same binding, type and name. */
9071 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
9072 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
9073 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
9074 goto done;
9076 result = TRUE;
9077 goto done;
9080 symtable1 = bfd_malloc (symcount1 * sizeof (struct elf_symbol));
9081 symtable2 = bfd_malloc (symcount2 * sizeof (struct elf_symbol));
9082 if (symtable1 == NULL || symtable2 == NULL)
9083 goto done;
9085 /* Count definitions in the section. */
9086 count1 = 0;
9087 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
9088 if (isym->st_shndx == (unsigned int) shndx1)
9089 symtable1[count1++].u.isym = isym;
9091 count2 = 0;
9092 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
9093 if (isym->st_shndx == (unsigned int) shndx2)
9094 symtable2[count2++].u.isym = isym;
9096 if (count1 == 0 || count2 == 0 || count1 != count2)
9097 goto done;
9099 for (i = 0; i < count1; i++)
9100 symtable1[i].name
9101 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
9102 symtable1[i].u.isym->st_name);
9104 for (i = 0; i < count2; i++)
9105 symtable2[i].name
9106 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
9107 symtable2[i].u.isym->st_name);
9109 /* Sort symbol by name. */
9110 qsort (symtable1, count1, sizeof (struct elf_symbol),
9111 elf_sym_name_compare);
9112 qsort (symtable2, count1, sizeof (struct elf_symbol),
9113 elf_sym_name_compare);
9115 for (i = 0; i < count1; i++)
9116 /* Two symbols must have the same binding, type and name. */
9117 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
9118 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
9119 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
9120 goto done;
9122 result = TRUE;
9124 done:
9125 if (symtable1)
9126 free (symtable1);
9127 if (symtable2)
9128 free (symtable2);
9129 if (isymbuf1)
9130 free (isymbuf1);
9131 if (isymbuf2)
9132 free (isymbuf2);
9134 return result;
9137 /* It is only used by x86-64 so far. */
9138 asection _bfd_elf_large_com_section
9139 = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
9140 SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
9142 /* Return TRUE if 2 section types are compatible. */
9144 bfd_boolean
9145 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
9146 bfd *bbfd, const asection *bsec)
9148 if (asec == NULL
9149 || bsec == NULL
9150 || abfd->xvec->flavour != bfd_target_elf_flavour
9151 || bbfd->xvec->flavour != bfd_target_elf_flavour)
9152 return TRUE;
9154 return elf_section_type (asec) == elf_section_type (bsec);
9157 void
9158 _bfd_elf_set_osabi (bfd * abfd,
9159 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
9161 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
9163 i_ehdrp = elf_elfheader (abfd);
9165 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
9169 /* Return TRUE for ELF symbol types that represent functions.
9170 This is the default version of this function, which is sufficient for
9171 most targets. It returns true if TYPE is STT_FUNC. */
9173 bfd_boolean
9174 _bfd_elf_is_function_type (unsigned int type)
9176 return (type == STT_FUNC);