Switch sources over to use the GPL version 3
[binutils.git] / bfd / elf.c
blob62c024d6138ede00653c5d58aae6833d256ea574
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 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
25 SECTION
26 ELF backends
28 BFD support for ELF formats is being worked on.
29 Currently, the best supported back ends are for sparc and i386
30 (running svr4 or Solaris 2).
32 Documentation of the internals of the support code still needs
33 to be written. The code is changing quickly enough that we
34 haven't bothered yet. */
36 /* For sparc64-cross-sparc32. */
37 #define _SYSCALL32
38 #include "sysdep.h"
39 #include "bfd.h"
40 #include "bfdlink.h"
41 #include "libbfd.h"
42 #define ARCH_SIZE 0
43 #include "elf-bfd.h"
44 #include "libiberty.h"
46 static int elf_sort_sections (const void *, const void *);
47 static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
48 static bfd_boolean prep_headers (bfd *);
49 static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ;
50 static bfd_boolean elfcore_read_notes (bfd *, file_ptr, bfd_size_type) ;
52 /* Swap version information in and out. The version information is
53 currently size independent. If that ever changes, this code will
54 need to move into elfcode.h. */
56 /* Swap in a Verdef structure. */
58 void
59 _bfd_elf_swap_verdef_in (bfd *abfd,
60 const Elf_External_Verdef *src,
61 Elf_Internal_Verdef *dst)
63 dst->vd_version = H_GET_16 (abfd, src->vd_version);
64 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
65 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
66 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
67 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
68 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
69 dst->vd_next = H_GET_32 (abfd, src->vd_next);
72 /* Swap out a Verdef structure. */
74 void
75 _bfd_elf_swap_verdef_out (bfd *abfd,
76 const Elf_Internal_Verdef *src,
77 Elf_External_Verdef *dst)
79 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
80 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
81 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
82 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
83 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
84 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
85 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
88 /* Swap in a Verdaux structure. */
90 void
91 _bfd_elf_swap_verdaux_in (bfd *abfd,
92 const Elf_External_Verdaux *src,
93 Elf_Internal_Verdaux *dst)
95 dst->vda_name = H_GET_32 (abfd, src->vda_name);
96 dst->vda_next = H_GET_32 (abfd, src->vda_next);
99 /* Swap out a Verdaux structure. */
101 void
102 _bfd_elf_swap_verdaux_out (bfd *abfd,
103 const Elf_Internal_Verdaux *src,
104 Elf_External_Verdaux *dst)
106 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
107 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
110 /* Swap in a Verneed structure. */
112 void
113 _bfd_elf_swap_verneed_in (bfd *abfd,
114 const Elf_External_Verneed *src,
115 Elf_Internal_Verneed *dst)
117 dst->vn_version = H_GET_16 (abfd, src->vn_version);
118 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
119 dst->vn_file = H_GET_32 (abfd, src->vn_file);
120 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
121 dst->vn_next = H_GET_32 (abfd, src->vn_next);
124 /* Swap out a Verneed structure. */
126 void
127 _bfd_elf_swap_verneed_out (bfd *abfd,
128 const Elf_Internal_Verneed *src,
129 Elf_External_Verneed *dst)
131 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
132 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
133 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
134 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
135 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
138 /* Swap in a Vernaux structure. */
140 void
141 _bfd_elf_swap_vernaux_in (bfd *abfd,
142 const Elf_External_Vernaux *src,
143 Elf_Internal_Vernaux *dst)
145 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
146 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
147 dst->vna_other = H_GET_16 (abfd, src->vna_other);
148 dst->vna_name = H_GET_32 (abfd, src->vna_name);
149 dst->vna_next = H_GET_32 (abfd, src->vna_next);
152 /* Swap out a Vernaux structure. */
154 void
155 _bfd_elf_swap_vernaux_out (bfd *abfd,
156 const Elf_Internal_Vernaux *src,
157 Elf_External_Vernaux *dst)
159 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
160 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
161 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
162 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
163 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
166 /* Swap in a Versym structure. */
168 void
169 _bfd_elf_swap_versym_in (bfd *abfd,
170 const Elf_External_Versym *src,
171 Elf_Internal_Versym *dst)
173 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
176 /* Swap out a Versym structure. */
178 void
179 _bfd_elf_swap_versym_out (bfd *abfd,
180 const Elf_Internal_Versym *src,
181 Elf_External_Versym *dst)
183 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
186 /* Standard ELF hash function. Do not change this function; you will
187 cause invalid hash tables to be generated. */
189 unsigned long
190 bfd_elf_hash (const char *namearg)
192 const unsigned char *name = (const unsigned char *) namearg;
193 unsigned long h = 0;
194 unsigned long g;
195 int ch;
197 while ((ch = *name++) != '\0')
199 h = (h << 4) + ch;
200 if ((g = (h & 0xf0000000)) != 0)
202 h ^= g >> 24;
203 /* The ELF ABI says `h &= ~g', but this is equivalent in
204 this case and on some machines one insn instead of two. */
205 h ^= g;
208 return h & 0xffffffff;
211 /* DT_GNU_HASH hash function. Do not change this function; you will
212 cause invalid hash tables to be generated. */
214 unsigned long
215 bfd_elf_gnu_hash (const char *namearg)
217 const unsigned char *name = (const unsigned char *) namearg;
218 unsigned long h = 5381;
219 unsigned char ch;
221 while ((ch = *name++) != '\0')
222 h = (h << 5) + h + ch;
223 return h & 0xffffffff;
226 bfd_boolean
227 bfd_elf_mkobject (bfd *abfd)
229 if (abfd->tdata.any == NULL)
231 abfd->tdata.any = bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
232 if (abfd->tdata.any == NULL)
233 return FALSE;
236 elf_tdata (abfd)->program_header_size = (bfd_size_type) -1;
238 return TRUE;
241 bfd_boolean
242 bfd_elf_mkcorefile (bfd *abfd)
244 /* I think this can be done just like an object file. */
245 return bfd_elf_mkobject (abfd);
248 char *
249 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
251 Elf_Internal_Shdr **i_shdrp;
252 bfd_byte *shstrtab = NULL;
253 file_ptr offset;
254 bfd_size_type shstrtabsize;
256 i_shdrp = elf_elfsections (abfd);
257 if (i_shdrp == 0
258 || shindex >= elf_numsections (abfd)
259 || i_shdrp[shindex] == 0)
260 return NULL;
262 shstrtab = i_shdrp[shindex]->contents;
263 if (shstrtab == NULL)
265 /* No cached one, attempt to read, and cache what we read. */
266 offset = i_shdrp[shindex]->sh_offset;
267 shstrtabsize = i_shdrp[shindex]->sh_size;
269 /* Allocate and clear an extra byte at the end, to prevent crashes
270 in case the string table is not terminated. */
271 if (shstrtabsize + 1 == 0
272 || (shstrtab = bfd_alloc (abfd, shstrtabsize + 1)) == NULL
273 || bfd_seek (abfd, offset, SEEK_SET) != 0)
274 shstrtab = NULL;
275 else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
277 if (bfd_get_error () != bfd_error_system_call)
278 bfd_set_error (bfd_error_file_truncated);
279 shstrtab = NULL;
281 else
282 shstrtab[shstrtabsize] = '\0';
283 i_shdrp[shindex]->contents = shstrtab;
285 return (char *) shstrtab;
288 char *
289 bfd_elf_string_from_elf_section (bfd *abfd,
290 unsigned int shindex,
291 unsigned int strindex)
293 Elf_Internal_Shdr *hdr;
295 if (strindex == 0)
296 return "";
298 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
299 return NULL;
301 hdr = elf_elfsections (abfd)[shindex];
303 if (hdr->contents == NULL
304 && bfd_elf_get_str_section (abfd, shindex) == NULL)
305 return NULL;
307 if (strindex >= hdr->sh_size)
309 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
310 (*_bfd_error_handler)
311 (_("%B: invalid string offset %u >= %lu for section `%s'"),
312 abfd, strindex, (unsigned long) hdr->sh_size,
313 (shindex == shstrndx && strindex == hdr->sh_name
314 ? ".shstrtab"
315 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
316 return "";
319 return ((char *) hdr->contents) + strindex;
322 /* Read and convert symbols to internal format.
323 SYMCOUNT specifies the number of symbols to read, starting from
324 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
325 are non-NULL, they are used to store the internal symbols, external
326 symbols, and symbol section index extensions, respectively. */
328 Elf_Internal_Sym *
329 bfd_elf_get_elf_syms (bfd *ibfd,
330 Elf_Internal_Shdr *symtab_hdr,
331 size_t symcount,
332 size_t symoffset,
333 Elf_Internal_Sym *intsym_buf,
334 void *extsym_buf,
335 Elf_External_Sym_Shndx *extshndx_buf)
337 Elf_Internal_Shdr *shndx_hdr;
338 void *alloc_ext;
339 const bfd_byte *esym;
340 Elf_External_Sym_Shndx *alloc_extshndx;
341 Elf_External_Sym_Shndx *shndx;
342 Elf_Internal_Sym *isym;
343 Elf_Internal_Sym *isymend;
344 const struct elf_backend_data *bed;
345 size_t extsym_size;
346 bfd_size_type amt;
347 file_ptr pos;
349 if (symcount == 0)
350 return intsym_buf;
352 /* Normal syms might have section extension entries. */
353 shndx_hdr = NULL;
354 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
355 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
357 /* Read the symbols. */
358 alloc_ext = NULL;
359 alloc_extshndx = NULL;
360 bed = get_elf_backend_data (ibfd);
361 extsym_size = bed->s->sizeof_sym;
362 amt = symcount * extsym_size;
363 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
364 if (extsym_buf == NULL)
366 alloc_ext = bfd_malloc2 (symcount, extsym_size);
367 extsym_buf = alloc_ext;
369 if (extsym_buf == NULL
370 || bfd_seek (ibfd, pos, SEEK_SET) != 0
371 || bfd_bread (extsym_buf, amt, ibfd) != amt)
373 intsym_buf = NULL;
374 goto out;
377 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
378 extshndx_buf = NULL;
379 else
381 amt = symcount * sizeof (Elf_External_Sym_Shndx);
382 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
383 if (extshndx_buf == NULL)
385 alloc_extshndx = bfd_malloc2 (symcount,
386 sizeof (Elf_External_Sym_Shndx));
387 extshndx_buf = alloc_extshndx;
389 if (extshndx_buf == NULL
390 || bfd_seek (ibfd, pos, SEEK_SET) != 0
391 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
393 intsym_buf = NULL;
394 goto out;
398 if (intsym_buf == NULL)
400 intsym_buf = bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
401 if (intsym_buf == NULL)
402 goto out;
405 /* Convert the symbols to internal form. */
406 isymend = intsym_buf + symcount;
407 for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf;
408 isym < isymend;
409 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
410 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
412 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
413 (*_bfd_error_handler) (_("%B symbol number %lu references "
414 "nonexistent SHT_SYMTAB_SHNDX section"),
415 ibfd, (unsigned long) symoffset);
416 intsym_buf = NULL;
417 goto out;
420 out:
421 if (alloc_ext != NULL)
422 free (alloc_ext);
423 if (alloc_extshndx != NULL)
424 free (alloc_extshndx);
426 return intsym_buf;
429 /* Look up a symbol name. */
430 const char *
431 bfd_elf_sym_name (bfd *abfd,
432 Elf_Internal_Shdr *symtab_hdr,
433 Elf_Internal_Sym *isym,
434 asection *sym_sec)
436 const char *name;
437 unsigned int iname = isym->st_name;
438 unsigned int shindex = symtab_hdr->sh_link;
440 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
441 /* Check for a bogus st_shndx to avoid crashing. */
442 && isym->st_shndx < elf_numsections (abfd)
443 && !(isym->st_shndx >= SHN_LORESERVE && isym->st_shndx <= SHN_HIRESERVE))
445 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
446 shindex = elf_elfheader (abfd)->e_shstrndx;
449 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
450 if (name == NULL)
451 name = "(null)";
452 else if (sym_sec && *name == '\0')
453 name = bfd_section_name (abfd, sym_sec);
455 return name;
458 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
459 sections. The first element is the flags, the rest are section
460 pointers. */
462 typedef union elf_internal_group {
463 Elf_Internal_Shdr *shdr;
464 unsigned int flags;
465 } Elf_Internal_Group;
467 /* Return the name of the group signature symbol. Why isn't the
468 signature just a string? */
470 static const char *
471 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
473 Elf_Internal_Shdr *hdr;
474 unsigned char esym[sizeof (Elf64_External_Sym)];
475 Elf_External_Sym_Shndx eshndx;
476 Elf_Internal_Sym isym;
478 /* First we need to ensure the symbol table is available. Make sure
479 that it is a symbol table section. */
480 hdr = elf_elfsections (abfd) [ghdr->sh_link];
481 if (hdr->sh_type != SHT_SYMTAB
482 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
483 return NULL;
485 /* Go read the symbol. */
486 hdr = &elf_tdata (abfd)->symtab_hdr;
487 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
488 &isym, esym, &eshndx) == NULL)
489 return NULL;
491 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
494 /* Set next_in_group list pointer, and group name for NEWSECT. */
496 static bfd_boolean
497 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
499 unsigned int num_group = elf_tdata (abfd)->num_group;
501 /* If num_group is zero, read in all SHT_GROUP sections. The count
502 is set to -1 if there are no SHT_GROUP sections. */
503 if (num_group == 0)
505 unsigned int i, shnum;
507 /* First count the number of groups. If we have a SHT_GROUP
508 section with just a flag word (ie. sh_size is 4), ignore it. */
509 shnum = elf_numsections (abfd);
510 num_group = 0;
512 #define IS_VALID_GROUP_SECTION_HEADER(shdr) \
513 ( (shdr)->sh_type == SHT_GROUP \
514 && (shdr)->sh_size >= (2 * GRP_ENTRY_SIZE) \
515 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
516 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
518 for (i = 0; i < shnum; i++)
520 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
522 if (IS_VALID_GROUP_SECTION_HEADER (shdr))
523 num_group += 1;
526 if (num_group == 0)
528 num_group = (unsigned) -1;
529 elf_tdata (abfd)->num_group = num_group;
531 else
533 /* We keep a list of elf section headers for group sections,
534 so we can find them quickly. */
535 bfd_size_type amt;
537 elf_tdata (abfd)->num_group = num_group;
538 elf_tdata (abfd)->group_sect_ptr
539 = bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
540 if (elf_tdata (abfd)->group_sect_ptr == NULL)
541 return FALSE;
543 num_group = 0;
544 for (i = 0; i < shnum; i++)
546 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
548 if (IS_VALID_GROUP_SECTION_HEADER (shdr))
550 unsigned char *src;
551 Elf_Internal_Group *dest;
553 /* Add to list of sections. */
554 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
555 num_group += 1;
557 /* Read the raw contents. */
558 BFD_ASSERT (sizeof (*dest) >= 4);
559 amt = shdr->sh_size * sizeof (*dest) / 4;
560 shdr->contents = bfd_alloc2 (abfd, shdr->sh_size,
561 sizeof (*dest) / 4);
562 /* PR binutils/4110: Handle corrupt group headers. */
563 if (shdr->contents == NULL)
565 _bfd_error_handler
566 (_("%B: Corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size);
567 bfd_set_error (bfd_error_bad_value);
568 return FALSE;
571 memset (shdr->contents, 0, amt);
573 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
574 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
575 != shdr->sh_size))
576 return FALSE;
578 /* Translate raw contents, a flag word followed by an
579 array of elf section indices all in target byte order,
580 to the flag word followed by an array of elf section
581 pointers. */
582 src = shdr->contents + shdr->sh_size;
583 dest = (Elf_Internal_Group *) (shdr->contents + amt);
584 while (1)
586 unsigned int idx;
588 src -= 4;
589 --dest;
590 idx = H_GET_32 (abfd, src);
591 if (src == shdr->contents)
593 dest->flags = idx;
594 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
595 shdr->bfd_section->flags
596 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
597 break;
599 if (idx >= shnum)
601 ((*_bfd_error_handler)
602 (_("%B: invalid SHT_GROUP entry"), abfd));
603 idx = 0;
605 dest->shdr = elf_elfsections (abfd)[idx];
612 if (num_group != (unsigned) -1)
614 unsigned int i;
616 for (i = 0; i < num_group; i++)
618 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
619 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
620 unsigned int n_elt = shdr->sh_size / 4;
622 /* Look through this group's sections to see if current
623 section is a member. */
624 while (--n_elt != 0)
625 if ((++idx)->shdr == hdr)
627 asection *s = NULL;
629 /* We are a member of this group. Go looking through
630 other members to see if any others are linked via
631 next_in_group. */
632 idx = (Elf_Internal_Group *) shdr->contents;
633 n_elt = shdr->sh_size / 4;
634 while (--n_elt != 0)
635 if ((s = (++idx)->shdr->bfd_section) != NULL
636 && elf_next_in_group (s) != NULL)
637 break;
638 if (n_elt != 0)
640 /* Snarf the group name from other member, and
641 insert current section in circular list. */
642 elf_group_name (newsect) = elf_group_name (s);
643 elf_next_in_group (newsect) = elf_next_in_group (s);
644 elf_next_in_group (s) = newsect;
646 else
648 const char *gname;
650 gname = group_signature (abfd, shdr);
651 if (gname == NULL)
652 return FALSE;
653 elf_group_name (newsect) = gname;
655 /* Start a circular list with one element. */
656 elf_next_in_group (newsect) = newsect;
659 /* If the group section has been created, point to the
660 new member. */
661 if (shdr->bfd_section != NULL)
662 elf_next_in_group (shdr->bfd_section) = newsect;
664 i = num_group - 1;
665 break;
670 if (elf_group_name (newsect) == NULL)
672 (*_bfd_error_handler) (_("%B: no group info for section %A"),
673 abfd, newsect);
675 return TRUE;
678 bfd_boolean
679 _bfd_elf_setup_sections (bfd *abfd)
681 unsigned int i;
682 unsigned int num_group = elf_tdata (abfd)->num_group;
683 bfd_boolean result = TRUE;
684 asection *s;
686 /* Process SHF_LINK_ORDER. */
687 for (s = abfd->sections; s != NULL; s = s->next)
689 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
690 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
692 unsigned int elfsec = this_hdr->sh_link;
693 /* FIXME: The old Intel compiler and old strip/objcopy may
694 not set the sh_link or sh_info fields. Hence we could
695 get the situation where elfsec is 0. */
696 if (elfsec == 0)
698 const struct elf_backend_data *bed
699 = get_elf_backend_data (abfd);
700 if (bed->link_order_error_handler)
701 bed->link_order_error_handler
702 (_("%B: warning: sh_link not set for section `%A'"),
703 abfd, s);
705 else
707 asection *link;
709 this_hdr = elf_elfsections (abfd)[elfsec];
711 /* PR 1991, 2008:
712 Some strip/objcopy may leave an incorrect value in
713 sh_link. We don't want to proceed. */
714 link = this_hdr->bfd_section;
715 if (link == NULL)
717 (*_bfd_error_handler)
718 (_("%B: sh_link [%d] in section `%A' is incorrect"),
719 s->owner, s, elfsec);
720 result = FALSE;
723 elf_linked_to_section (s) = link;
728 /* Process section groups. */
729 if (num_group == (unsigned) -1)
730 return result;
732 for (i = 0; i < num_group; i++)
734 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
735 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
736 unsigned int n_elt = shdr->sh_size / 4;
738 while (--n_elt != 0)
739 if ((++idx)->shdr->bfd_section)
740 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
741 else if (idx->shdr->sh_type == SHT_RELA
742 || idx->shdr->sh_type == SHT_REL)
743 /* We won't include relocation sections in section groups in
744 output object files. We adjust the group section size here
745 so that relocatable link will work correctly when
746 relocation sections are in section group in input object
747 files. */
748 shdr->bfd_section->size -= 4;
749 else
751 /* There are some unknown sections in the group. */
752 (*_bfd_error_handler)
753 (_("%B: unknown [%d] section `%s' in group [%s]"),
754 abfd,
755 (unsigned int) idx->shdr->sh_type,
756 bfd_elf_string_from_elf_section (abfd,
757 (elf_elfheader (abfd)
758 ->e_shstrndx),
759 idx->shdr->sh_name),
760 shdr->bfd_section->name);
761 result = FALSE;
764 return result;
767 bfd_boolean
768 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
770 return elf_next_in_group (sec) != NULL;
773 /* Make a BFD section from an ELF section. We store a pointer to the
774 BFD section in the bfd_section field of the header. */
776 bfd_boolean
777 _bfd_elf_make_section_from_shdr (bfd *abfd,
778 Elf_Internal_Shdr *hdr,
779 const char *name,
780 int shindex)
782 asection *newsect;
783 flagword flags;
784 const struct elf_backend_data *bed;
786 if (hdr->bfd_section != NULL)
788 BFD_ASSERT (strcmp (name,
789 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
790 return TRUE;
793 newsect = bfd_make_section_anyway (abfd, name);
794 if (newsect == NULL)
795 return FALSE;
797 hdr->bfd_section = newsect;
798 elf_section_data (newsect)->this_hdr = *hdr;
799 elf_section_data (newsect)->this_idx = shindex;
801 /* Always use the real type/flags. */
802 elf_section_type (newsect) = hdr->sh_type;
803 elf_section_flags (newsect) = hdr->sh_flags;
805 newsect->filepos = hdr->sh_offset;
807 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
808 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
809 || ! bfd_set_section_alignment (abfd, newsect,
810 bfd_log2 ((bfd_vma) hdr->sh_addralign)))
811 return FALSE;
813 flags = SEC_NO_FLAGS;
814 if (hdr->sh_type != SHT_NOBITS)
815 flags |= SEC_HAS_CONTENTS;
816 if (hdr->sh_type == SHT_GROUP)
817 flags |= SEC_GROUP | SEC_EXCLUDE;
818 if ((hdr->sh_flags & SHF_ALLOC) != 0)
820 flags |= SEC_ALLOC;
821 if (hdr->sh_type != SHT_NOBITS)
822 flags |= SEC_LOAD;
824 if ((hdr->sh_flags & SHF_WRITE) == 0)
825 flags |= SEC_READONLY;
826 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
827 flags |= SEC_CODE;
828 else if ((flags & SEC_LOAD) != 0)
829 flags |= SEC_DATA;
830 if ((hdr->sh_flags & SHF_MERGE) != 0)
832 flags |= SEC_MERGE;
833 newsect->entsize = hdr->sh_entsize;
834 if ((hdr->sh_flags & SHF_STRINGS) != 0)
835 flags |= SEC_STRINGS;
837 if (hdr->sh_flags & SHF_GROUP)
838 if (!setup_group (abfd, hdr, newsect))
839 return FALSE;
840 if ((hdr->sh_flags & SHF_TLS) != 0)
841 flags |= SEC_THREAD_LOCAL;
843 if ((flags & SEC_ALLOC) == 0)
845 /* The debugging sections appear to be recognized only by name,
846 not any sort of flag. Their SEC_ALLOC bits are cleared. */
847 static const struct
849 const char *name;
850 int len;
851 } debug_sections [] =
853 { STRING_COMMA_LEN ("debug") }, /* 'd' */
854 { NULL, 0 }, /* 'e' */
855 { NULL, 0 }, /* 'f' */
856 { STRING_COMMA_LEN ("gnu.linkonce.wi.") }, /* 'g' */
857 { NULL, 0 }, /* 'h' */
858 { NULL, 0 }, /* 'i' */
859 { NULL, 0 }, /* 'j' */
860 { NULL, 0 }, /* 'k' */
861 { STRING_COMMA_LEN ("line") }, /* 'l' */
862 { NULL, 0 }, /* 'm' */
863 { NULL, 0 }, /* 'n' */
864 { NULL, 0 }, /* 'o' */
865 { NULL, 0 }, /* 'p' */
866 { NULL, 0 }, /* 'q' */
867 { NULL, 0 }, /* 'r' */
868 { STRING_COMMA_LEN ("stab") } /* 's' */
871 if (name [0] == '.')
873 int i = name [1] - 'd';
874 if (i >= 0
875 && i < (int) ARRAY_SIZE (debug_sections)
876 && debug_sections [i].name != NULL
877 && strncmp (&name [1], debug_sections [i].name,
878 debug_sections [i].len) == 0)
879 flags |= SEC_DEBUGGING;
883 /* As a GNU extension, if the name begins with .gnu.linkonce, we
884 only link a single copy of the section. This is used to support
885 g++. g++ will emit each template expansion in its own section.
886 The symbols will be defined as weak, so that multiple definitions
887 are permitted. The GNU linker extension is to actually discard
888 all but one of the sections. */
889 if (CONST_STRNEQ (name, ".gnu.linkonce")
890 && elf_next_in_group (newsect) == NULL)
891 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
893 bed = get_elf_backend_data (abfd);
894 if (bed->elf_backend_section_flags)
895 if (! bed->elf_backend_section_flags (&flags, hdr))
896 return FALSE;
898 if (! bfd_set_section_flags (abfd, newsect, flags))
899 return FALSE;
901 if ((flags & SEC_ALLOC) != 0)
903 Elf_Internal_Phdr *phdr;
904 unsigned int i;
906 /* Look through the phdrs to see if we need to adjust the lma.
907 If all the p_paddr fields are zero, we ignore them, since
908 some ELF linkers produce such output. */
909 phdr = elf_tdata (abfd)->phdr;
910 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
912 if (phdr->p_paddr != 0)
913 break;
915 if (i < elf_elfheader (abfd)->e_phnum)
917 phdr = elf_tdata (abfd)->phdr;
918 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
920 /* This section is part of this segment if its file
921 offset plus size lies within the segment's memory
922 span and, if the section is loaded, the extent of the
923 loaded data lies within the extent of the segment.
925 Note - we used to check the p_paddr field as well, and
926 refuse to set the LMA if it was 0. This is wrong
927 though, as a perfectly valid initialised segment can
928 have a p_paddr of zero. Some architectures, eg ARM,
929 place special significance on the address 0 and
930 executables need to be able to have a segment which
931 covers this address. */
932 if (phdr->p_type == PT_LOAD
933 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
934 && (hdr->sh_offset + hdr->sh_size
935 <= phdr->p_offset + phdr->p_memsz)
936 && ((flags & SEC_LOAD) == 0
937 || (hdr->sh_offset + hdr->sh_size
938 <= phdr->p_offset + phdr->p_filesz)))
940 if ((flags & SEC_LOAD) == 0)
941 newsect->lma = (phdr->p_paddr
942 + hdr->sh_addr - phdr->p_vaddr);
943 else
944 /* We used to use the same adjustment for SEC_LOAD
945 sections, but that doesn't work if the segment
946 is packed with code from multiple VMAs.
947 Instead we calculate the section LMA based on
948 the segment LMA. It is assumed that the
949 segment will contain sections with contiguous
950 LMAs, even if the VMAs are not. */
951 newsect->lma = (phdr->p_paddr
952 + hdr->sh_offset - phdr->p_offset);
954 /* With contiguous segments, we can't tell from file
955 offsets whether a section with zero size should
956 be placed at the end of one segment or the
957 beginning of the next. Decide based on vaddr. */
958 if (hdr->sh_addr >= phdr->p_vaddr
959 && (hdr->sh_addr + hdr->sh_size
960 <= phdr->p_vaddr + phdr->p_memsz))
961 break;
967 return TRUE;
971 INTERNAL_FUNCTION
972 bfd_elf_find_section
974 SYNOPSIS
975 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
977 DESCRIPTION
978 Helper functions for GDB to locate the string tables.
979 Since BFD hides string tables from callers, GDB needs to use an
980 internal hook to find them. Sun's .stabstr, in particular,
981 isn't even pointed to by the .stab section, so ordinary
982 mechanisms wouldn't work to find it, even if we had some.
985 struct elf_internal_shdr *
986 bfd_elf_find_section (bfd *abfd, char *name)
988 Elf_Internal_Shdr **i_shdrp;
989 char *shstrtab;
990 unsigned int max;
991 unsigned int i;
993 i_shdrp = elf_elfsections (abfd);
994 if (i_shdrp != NULL)
996 shstrtab = bfd_elf_get_str_section (abfd,
997 elf_elfheader (abfd)->e_shstrndx);
998 if (shstrtab != NULL)
1000 max = elf_numsections (abfd);
1001 for (i = 1; i < max; i++)
1002 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
1003 return i_shdrp[i];
1006 return 0;
1009 const char *const bfd_elf_section_type_names[] = {
1010 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1011 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1012 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1015 /* ELF relocs are against symbols. If we are producing relocatable
1016 output, and the reloc is against an external symbol, and nothing
1017 has given us any additional addend, the resulting reloc will also
1018 be against the same symbol. In such a case, we don't want to
1019 change anything about the way the reloc is handled, since it will
1020 all be done at final link time. Rather than put special case code
1021 into bfd_perform_relocation, all the reloc types use this howto
1022 function. It just short circuits the reloc if producing
1023 relocatable output against an external symbol. */
1025 bfd_reloc_status_type
1026 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1027 arelent *reloc_entry,
1028 asymbol *symbol,
1029 void *data ATTRIBUTE_UNUSED,
1030 asection *input_section,
1031 bfd *output_bfd,
1032 char **error_message ATTRIBUTE_UNUSED)
1034 if (output_bfd != NULL
1035 && (symbol->flags & BSF_SECTION_SYM) == 0
1036 && (! reloc_entry->howto->partial_inplace
1037 || reloc_entry->addend == 0))
1039 reloc_entry->address += input_section->output_offset;
1040 return bfd_reloc_ok;
1043 return bfd_reloc_continue;
1046 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
1048 static void
1049 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
1050 asection *sec)
1052 BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE);
1053 sec->sec_info_type = ELF_INFO_TYPE_NONE;
1056 /* Finish SHF_MERGE section merging. */
1058 bfd_boolean
1059 _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
1061 bfd *ibfd;
1062 asection *sec;
1064 if (!is_elf_hash_table (info->hash))
1065 return FALSE;
1067 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1068 if ((ibfd->flags & DYNAMIC) == 0)
1069 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
1070 if ((sec->flags & SEC_MERGE) != 0
1071 && !bfd_is_abs_section (sec->output_section))
1073 struct bfd_elf_section_data *secdata;
1075 secdata = elf_section_data (sec);
1076 if (! _bfd_add_merge_section (abfd,
1077 &elf_hash_table (info)->merge_info,
1078 sec, &secdata->sec_info))
1079 return FALSE;
1080 else if (secdata->sec_info)
1081 sec->sec_info_type = ELF_INFO_TYPE_MERGE;
1084 if (elf_hash_table (info)->merge_info != NULL)
1085 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
1086 merge_sections_remove_hook);
1087 return TRUE;
1090 void
1091 _bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
1093 sec->output_section = bfd_abs_section_ptr;
1094 sec->output_offset = sec->vma;
1095 if (!is_elf_hash_table (info->hash))
1096 return;
1098 sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
1101 /* Copy the program header and other data from one object module to
1102 another. */
1104 bfd_boolean
1105 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1107 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1108 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1109 return TRUE;
1111 BFD_ASSERT (!elf_flags_init (obfd)
1112 || (elf_elfheader (obfd)->e_flags
1113 == elf_elfheader (ibfd)->e_flags));
1115 elf_gp (obfd) = elf_gp (ibfd);
1116 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1117 elf_flags_init (obfd) = TRUE;
1119 /* Copy object attributes. */
1120 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1122 return TRUE;
1125 static const char *
1126 get_segment_type (unsigned int p_type)
1128 const char *pt;
1129 switch (p_type)
1131 case PT_NULL: pt = "NULL"; break;
1132 case PT_LOAD: pt = "LOAD"; break;
1133 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1134 case PT_INTERP: pt = "INTERP"; break;
1135 case PT_NOTE: pt = "NOTE"; break;
1136 case PT_SHLIB: pt = "SHLIB"; break;
1137 case PT_PHDR: pt = "PHDR"; break;
1138 case PT_TLS: pt = "TLS"; break;
1139 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1140 case PT_GNU_STACK: pt = "STACK"; break;
1141 case PT_GNU_RELRO: pt = "RELRO"; break;
1142 default: pt = NULL; break;
1144 return pt;
1147 /* Print out the program headers. */
1149 bfd_boolean
1150 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1152 FILE *f = farg;
1153 Elf_Internal_Phdr *p;
1154 asection *s;
1155 bfd_byte *dynbuf = NULL;
1157 p = elf_tdata (abfd)->phdr;
1158 if (p != NULL)
1160 unsigned int i, c;
1162 fprintf (f, _("\nProgram Header:\n"));
1163 c = elf_elfheader (abfd)->e_phnum;
1164 for (i = 0; i < c; i++, p++)
1166 const char *pt = get_segment_type (p->p_type);
1167 char buf[20];
1169 if (pt == NULL)
1171 sprintf (buf, "0x%lx", p->p_type);
1172 pt = buf;
1174 fprintf (f, "%8s off 0x", pt);
1175 bfd_fprintf_vma (abfd, f, p->p_offset);
1176 fprintf (f, " vaddr 0x");
1177 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1178 fprintf (f, " paddr 0x");
1179 bfd_fprintf_vma (abfd, f, p->p_paddr);
1180 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1181 fprintf (f, " filesz 0x");
1182 bfd_fprintf_vma (abfd, f, p->p_filesz);
1183 fprintf (f, " memsz 0x");
1184 bfd_fprintf_vma (abfd, f, p->p_memsz);
1185 fprintf (f, " flags %c%c%c",
1186 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1187 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1188 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1189 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1190 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1191 fprintf (f, "\n");
1195 s = bfd_get_section_by_name (abfd, ".dynamic");
1196 if (s != NULL)
1198 int elfsec;
1199 unsigned long shlink;
1200 bfd_byte *extdyn, *extdynend;
1201 size_t extdynsize;
1202 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1204 fprintf (f, _("\nDynamic Section:\n"));
1206 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1207 goto error_return;
1209 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1210 if (elfsec == -1)
1211 goto error_return;
1212 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1214 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1215 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1217 extdyn = dynbuf;
1218 extdynend = extdyn + s->size;
1219 for (; extdyn < extdynend; extdyn += extdynsize)
1221 Elf_Internal_Dyn dyn;
1222 const char *name;
1223 char ab[20];
1224 bfd_boolean stringp;
1226 (*swap_dyn_in) (abfd, extdyn, &dyn);
1228 if (dyn.d_tag == DT_NULL)
1229 break;
1231 stringp = FALSE;
1232 switch (dyn.d_tag)
1234 default:
1235 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1236 name = ab;
1237 break;
1239 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1240 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1241 case DT_PLTGOT: name = "PLTGOT"; break;
1242 case DT_HASH: name = "HASH"; break;
1243 case DT_STRTAB: name = "STRTAB"; break;
1244 case DT_SYMTAB: name = "SYMTAB"; break;
1245 case DT_RELA: name = "RELA"; break;
1246 case DT_RELASZ: name = "RELASZ"; break;
1247 case DT_RELAENT: name = "RELAENT"; break;
1248 case DT_STRSZ: name = "STRSZ"; break;
1249 case DT_SYMENT: name = "SYMENT"; break;
1250 case DT_INIT: name = "INIT"; break;
1251 case DT_FINI: name = "FINI"; break;
1252 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1253 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1254 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1255 case DT_REL: name = "REL"; break;
1256 case DT_RELSZ: name = "RELSZ"; break;
1257 case DT_RELENT: name = "RELENT"; break;
1258 case DT_PLTREL: name = "PLTREL"; break;
1259 case DT_DEBUG: name = "DEBUG"; break;
1260 case DT_TEXTREL: name = "TEXTREL"; break;
1261 case DT_JMPREL: name = "JMPREL"; break;
1262 case DT_BIND_NOW: name = "BIND_NOW"; break;
1263 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1264 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1265 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1266 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1267 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1268 case DT_FLAGS: name = "FLAGS"; break;
1269 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1270 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1271 case DT_CHECKSUM: name = "CHECKSUM"; break;
1272 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1273 case DT_MOVEENT: name = "MOVEENT"; break;
1274 case DT_MOVESZ: name = "MOVESZ"; break;
1275 case DT_FEATURE: name = "FEATURE"; break;
1276 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1277 case DT_SYMINSZ: name = "SYMINSZ"; break;
1278 case DT_SYMINENT: name = "SYMINENT"; break;
1279 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1280 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1281 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1282 case DT_PLTPAD: name = "PLTPAD"; break;
1283 case DT_MOVETAB: name = "MOVETAB"; break;
1284 case DT_SYMINFO: name = "SYMINFO"; break;
1285 case DT_RELACOUNT: name = "RELACOUNT"; break;
1286 case DT_RELCOUNT: name = "RELCOUNT"; break;
1287 case DT_FLAGS_1: name = "FLAGS_1"; break;
1288 case DT_VERSYM: name = "VERSYM"; break;
1289 case DT_VERDEF: name = "VERDEF"; break;
1290 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1291 case DT_VERNEED: name = "VERNEED"; break;
1292 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1293 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1294 case DT_USED: name = "USED"; break;
1295 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1296 case DT_GNU_HASH: name = "GNU_HASH"; break;
1299 fprintf (f, " %-11s ", name);
1300 if (! stringp)
1301 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
1302 else
1304 const char *string;
1305 unsigned int tagv = dyn.d_un.d_val;
1307 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1308 if (string == NULL)
1309 goto error_return;
1310 fprintf (f, "%s", string);
1312 fprintf (f, "\n");
1315 free (dynbuf);
1316 dynbuf = NULL;
1319 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1320 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1322 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1323 return FALSE;
1326 if (elf_dynverdef (abfd) != 0)
1328 Elf_Internal_Verdef *t;
1330 fprintf (f, _("\nVersion definitions:\n"));
1331 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1333 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1334 t->vd_flags, t->vd_hash,
1335 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1336 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1338 Elf_Internal_Verdaux *a;
1340 fprintf (f, "\t");
1341 for (a = t->vd_auxptr->vda_nextptr;
1342 a != NULL;
1343 a = a->vda_nextptr)
1344 fprintf (f, "%s ",
1345 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1346 fprintf (f, "\n");
1351 if (elf_dynverref (abfd) != 0)
1353 Elf_Internal_Verneed *t;
1355 fprintf (f, _("\nVersion References:\n"));
1356 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1358 Elf_Internal_Vernaux *a;
1360 fprintf (f, _(" required from %s:\n"),
1361 t->vn_filename ? t->vn_filename : "<corrupt>");
1362 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1363 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1364 a->vna_flags, a->vna_other,
1365 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1369 return TRUE;
1371 error_return:
1372 if (dynbuf != NULL)
1373 free (dynbuf);
1374 return FALSE;
1377 /* Display ELF-specific fields of a symbol. */
1379 void
1380 bfd_elf_print_symbol (bfd *abfd,
1381 void *filep,
1382 asymbol *symbol,
1383 bfd_print_symbol_type how)
1385 FILE *file = filep;
1386 switch (how)
1388 case bfd_print_symbol_name:
1389 fprintf (file, "%s", symbol->name);
1390 break;
1391 case bfd_print_symbol_more:
1392 fprintf (file, "elf ");
1393 bfd_fprintf_vma (abfd, file, symbol->value);
1394 fprintf (file, " %lx", (long) symbol->flags);
1395 break;
1396 case bfd_print_symbol_all:
1398 const char *section_name;
1399 const char *name = NULL;
1400 const struct elf_backend_data *bed;
1401 unsigned char st_other;
1402 bfd_vma val;
1404 section_name = symbol->section ? symbol->section->name : "(*none*)";
1406 bed = get_elf_backend_data (abfd);
1407 if (bed->elf_backend_print_symbol_all)
1408 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1410 if (name == NULL)
1412 name = symbol->name;
1413 bfd_print_symbol_vandf (abfd, file, symbol);
1416 fprintf (file, " %s\t", section_name);
1417 /* Print the "other" value for a symbol. For common symbols,
1418 we've already printed the size; now print the alignment.
1419 For other symbols, we have no specified alignment, and
1420 we've printed the address; now print the size. */
1421 if (bfd_is_com_section (symbol->section))
1422 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1423 else
1424 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1425 bfd_fprintf_vma (abfd, file, val);
1427 /* If we have version information, print it. */
1428 if (elf_tdata (abfd)->dynversym_section != 0
1429 && (elf_tdata (abfd)->dynverdef_section != 0
1430 || elf_tdata (abfd)->dynverref_section != 0))
1432 unsigned int vernum;
1433 const char *version_string;
1435 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1437 if (vernum == 0)
1438 version_string = "";
1439 else if (vernum == 1)
1440 version_string = "Base";
1441 else if (vernum <= elf_tdata (abfd)->cverdefs)
1442 version_string =
1443 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1444 else
1446 Elf_Internal_Verneed *t;
1448 version_string = "";
1449 for (t = elf_tdata (abfd)->verref;
1450 t != NULL;
1451 t = t->vn_nextref)
1453 Elf_Internal_Vernaux *a;
1455 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1457 if (a->vna_other == vernum)
1459 version_string = a->vna_nodename;
1460 break;
1466 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1467 fprintf (file, " %-11s", version_string);
1468 else
1470 int i;
1472 fprintf (file, " (%s)", version_string);
1473 for (i = 10 - strlen (version_string); i > 0; --i)
1474 putc (' ', file);
1478 /* If the st_other field is not zero, print it. */
1479 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1481 switch (st_other)
1483 case 0: break;
1484 case STV_INTERNAL: fprintf (file, " .internal"); break;
1485 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1486 case STV_PROTECTED: fprintf (file, " .protected"); break;
1487 default:
1488 /* Some other non-defined flags are also present, so print
1489 everything hex. */
1490 fprintf (file, " 0x%02x", (unsigned int) st_other);
1493 fprintf (file, " %s", name);
1495 break;
1499 /* Create an entry in an ELF linker hash table. */
1501 struct bfd_hash_entry *
1502 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1503 struct bfd_hash_table *table,
1504 const char *string)
1506 /* Allocate the structure if it has not already been allocated by a
1507 subclass. */
1508 if (entry == NULL)
1510 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
1511 if (entry == NULL)
1512 return entry;
1515 /* Call the allocation method of the superclass. */
1516 entry = _bfd_link_hash_newfunc (entry, table, string);
1517 if (entry != NULL)
1519 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
1520 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
1522 /* Set local fields. */
1523 ret->indx = -1;
1524 ret->dynindx = -1;
1525 ret->got = htab->init_got_refcount;
1526 ret->plt = htab->init_plt_refcount;
1527 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
1528 - offsetof (struct elf_link_hash_entry, size)));
1529 /* Assume that we have been called by a non-ELF symbol reader.
1530 This flag is then reset by the code which reads an ELF input
1531 file. This ensures that a symbol created by a non-ELF symbol
1532 reader will have the flag set correctly. */
1533 ret->non_elf = 1;
1536 return entry;
1539 /* Copy data from an indirect symbol to its direct symbol, hiding the
1540 old indirect symbol. Also used for copying flags to a weakdef. */
1542 void
1543 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
1544 struct elf_link_hash_entry *dir,
1545 struct elf_link_hash_entry *ind)
1547 struct elf_link_hash_table *htab;
1549 /* Copy down any references that we may have already seen to the
1550 symbol which just became indirect. */
1552 dir->ref_dynamic |= ind->ref_dynamic;
1553 dir->ref_regular |= ind->ref_regular;
1554 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
1555 dir->non_got_ref |= ind->non_got_ref;
1556 dir->needs_plt |= ind->needs_plt;
1557 dir->pointer_equality_needed |= ind->pointer_equality_needed;
1559 if (ind->root.type != bfd_link_hash_indirect)
1560 return;
1562 /* Copy over the global and procedure linkage table refcount entries.
1563 These may have been already set up by a check_relocs routine. */
1564 htab = elf_hash_table (info);
1565 if (ind->got.refcount > htab->init_got_refcount.refcount)
1567 if (dir->got.refcount < 0)
1568 dir->got.refcount = 0;
1569 dir->got.refcount += ind->got.refcount;
1570 ind->got.refcount = htab->init_got_refcount.refcount;
1573 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
1575 if (dir->plt.refcount < 0)
1576 dir->plt.refcount = 0;
1577 dir->plt.refcount += ind->plt.refcount;
1578 ind->plt.refcount = htab->init_plt_refcount.refcount;
1581 if (ind->dynindx != -1)
1583 if (dir->dynindx != -1)
1584 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
1585 dir->dynindx = ind->dynindx;
1586 dir->dynstr_index = ind->dynstr_index;
1587 ind->dynindx = -1;
1588 ind->dynstr_index = 0;
1592 void
1593 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
1594 struct elf_link_hash_entry *h,
1595 bfd_boolean force_local)
1597 h->plt = elf_hash_table (info)->init_plt_offset;
1598 h->needs_plt = 0;
1599 if (force_local)
1601 h->forced_local = 1;
1602 if (h->dynindx != -1)
1604 h->dynindx = -1;
1605 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1606 h->dynstr_index);
1611 /* Initialize an ELF linker hash table. */
1613 bfd_boolean
1614 _bfd_elf_link_hash_table_init
1615 (struct elf_link_hash_table *table,
1616 bfd *abfd,
1617 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
1618 struct bfd_hash_table *,
1619 const char *),
1620 unsigned int entsize)
1622 bfd_boolean ret;
1623 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
1625 memset (table, 0, sizeof * table);
1626 table->init_got_refcount.refcount = can_refcount - 1;
1627 table->init_plt_refcount.refcount = can_refcount - 1;
1628 table->init_got_offset.offset = -(bfd_vma) 1;
1629 table->init_plt_offset.offset = -(bfd_vma) 1;
1630 /* The first dynamic symbol is a dummy. */
1631 table->dynsymcount = 1;
1633 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
1634 table->root.type = bfd_link_elf_hash_table;
1636 return ret;
1639 /* Create an ELF linker hash table. */
1641 struct bfd_link_hash_table *
1642 _bfd_elf_link_hash_table_create (bfd *abfd)
1644 struct elf_link_hash_table *ret;
1645 bfd_size_type amt = sizeof (struct elf_link_hash_table);
1647 ret = bfd_malloc (amt);
1648 if (ret == NULL)
1649 return NULL;
1651 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
1652 sizeof (struct elf_link_hash_entry)))
1654 free (ret);
1655 return NULL;
1658 return &ret->root;
1661 /* This is a hook for the ELF emulation code in the generic linker to
1662 tell the backend linker what file name to use for the DT_NEEDED
1663 entry for a dynamic object. */
1665 void
1666 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
1668 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1669 && bfd_get_format (abfd) == bfd_object)
1670 elf_dt_name (abfd) = name;
1674 bfd_elf_get_dyn_lib_class (bfd *abfd)
1676 int lib_class;
1677 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1678 && bfd_get_format (abfd) == bfd_object)
1679 lib_class = elf_dyn_lib_class (abfd);
1680 else
1681 lib_class = 0;
1682 return lib_class;
1685 void
1686 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
1688 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1689 && bfd_get_format (abfd) == bfd_object)
1690 elf_dyn_lib_class (abfd) = lib_class;
1693 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1694 the linker ELF emulation code. */
1696 struct bfd_link_needed_list *
1697 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
1698 struct bfd_link_info *info)
1700 if (! is_elf_hash_table (info->hash))
1701 return NULL;
1702 return elf_hash_table (info)->needed;
1705 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1706 hook for the linker ELF emulation code. */
1708 struct bfd_link_needed_list *
1709 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
1710 struct bfd_link_info *info)
1712 if (! is_elf_hash_table (info->hash))
1713 return NULL;
1714 return elf_hash_table (info)->runpath;
1717 /* Get the name actually used for a dynamic object for a link. This
1718 is the SONAME entry if there is one. Otherwise, it is the string
1719 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1721 const char *
1722 bfd_elf_get_dt_soname (bfd *abfd)
1724 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1725 && bfd_get_format (abfd) == bfd_object)
1726 return elf_dt_name (abfd);
1727 return NULL;
1730 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1731 the ELF linker emulation code. */
1733 bfd_boolean
1734 bfd_elf_get_bfd_needed_list (bfd *abfd,
1735 struct bfd_link_needed_list **pneeded)
1737 asection *s;
1738 bfd_byte *dynbuf = NULL;
1739 int elfsec;
1740 unsigned long shlink;
1741 bfd_byte *extdyn, *extdynend;
1742 size_t extdynsize;
1743 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1745 *pneeded = NULL;
1747 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1748 || bfd_get_format (abfd) != bfd_object)
1749 return TRUE;
1751 s = bfd_get_section_by_name (abfd, ".dynamic");
1752 if (s == NULL || s->size == 0)
1753 return TRUE;
1755 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1756 goto error_return;
1758 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1759 if (elfsec == -1)
1760 goto error_return;
1762 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1764 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1765 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1767 extdyn = dynbuf;
1768 extdynend = extdyn + s->size;
1769 for (; extdyn < extdynend; extdyn += extdynsize)
1771 Elf_Internal_Dyn dyn;
1773 (*swap_dyn_in) (abfd, extdyn, &dyn);
1775 if (dyn.d_tag == DT_NULL)
1776 break;
1778 if (dyn.d_tag == DT_NEEDED)
1780 const char *string;
1781 struct bfd_link_needed_list *l;
1782 unsigned int tagv = dyn.d_un.d_val;
1783 bfd_size_type amt;
1785 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1786 if (string == NULL)
1787 goto error_return;
1789 amt = sizeof *l;
1790 l = bfd_alloc (abfd, amt);
1791 if (l == NULL)
1792 goto error_return;
1794 l->by = abfd;
1795 l->name = string;
1796 l->next = *pneeded;
1797 *pneeded = l;
1801 free (dynbuf);
1803 return TRUE;
1805 error_return:
1806 if (dynbuf != NULL)
1807 free (dynbuf);
1808 return FALSE;
1811 /* Allocate an ELF string table--force the first byte to be zero. */
1813 struct bfd_strtab_hash *
1814 _bfd_elf_stringtab_init (void)
1816 struct bfd_strtab_hash *ret;
1818 ret = _bfd_stringtab_init ();
1819 if (ret != NULL)
1821 bfd_size_type loc;
1823 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1824 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1825 if (loc == (bfd_size_type) -1)
1827 _bfd_stringtab_free (ret);
1828 ret = NULL;
1831 return ret;
1834 /* ELF .o/exec file reading */
1836 /* Create a new bfd section from an ELF section header. */
1838 bfd_boolean
1839 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1841 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1842 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1843 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1844 const char *name;
1846 name = bfd_elf_string_from_elf_section (abfd,
1847 elf_elfheader (abfd)->e_shstrndx,
1848 hdr->sh_name);
1849 if (name == NULL)
1850 return FALSE;
1852 switch (hdr->sh_type)
1854 case SHT_NULL:
1855 /* Inactive section. Throw it away. */
1856 return TRUE;
1858 case SHT_PROGBITS: /* Normal section with contents. */
1859 case SHT_NOBITS: /* .bss section. */
1860 case SHT_HASH: /* .hash section. */
1861 case SHT_NOTE: /* .note section. */
1862 case SHT_INIT_ARRAY: /* .init_array section. */
1863 case SHT_FINI_ARRAY: /* .fini_array section. */
1864 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1865 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
1866 case SHT_GNU_HASH: /* .gnu.hash section. */
1867 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1869 case SHT_DYNAMIC: /* Dynamic linking information. */
1870 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1871 return FALSE;
1872 if (hdr->sh_link > elf_numsections (abfd)
1873 || elf_elfsections (abfd)[hdr->sh_link] == NULL)
1874 return FALSE;
1875 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1877 Elf_Internal_Shdr *dynsymhdr;
1879 /* The shared libraries distributed with hpux11 have a bogus
1880 sh_link field for the ".dynamic" section. Find the
1881 string table for the ".dynsym" section instead. */
1882 if (elf_dynsymtab (abfd) != 0)
1884 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1885 hdr->sh_link = dynsymhdr->sh_link;
1887 else
1889 unsigned int i, num_sec;
1891 num_sec = elf_numsections (abfd);
1892 for (i = 1; i < num_sec; i++)
1894 dynsymhdr = elf_elfsections (abfd)[i];
1895 if (dynsymhdr->sh_type == SHT_DYNSYM)
1897 hdr->sh_link = dynsymhdr->sh_link;
1898 break;
1903 break;
1905 case SHT_SYMTAB: /* A symbol table */
1906 if (elf_onesymtab (abfd) == shindex)
1907 return TRUE;
1909 if (hdr->sh_entsize != bed->s->sizeof_sym)
1910 return FALSE;
1911 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1912 elf_onesymtab (abfd) = shindex;
1913 elf_tdata (abfd)->symtab_hdr = *hdr;
1914 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1915 abfd->flags |= HAS_SYMS;
1917 /* Sometimes a shared object will map in the symbol table. If
1918 SHF_ALLOC is set, and this is a shared object, then we also
1919 treat this section as a BFD section. We can not base the
1920 decision purely on SHF_ALLOC, because that flag is sometimes
1921 set in a relocatable object file, which would confuse the
1922 linker. */
1923 if ((hdr->sh_flags & SHF_ALLOC) != 0
1924 && (abfd->flags & DYNAMIC) != 0
1925 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1926 shindex))
1927 return FALSE;
1929 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1930 can't read symbols without that section loaded as well. It
1931 is most likely specified by the next section header. */
1932 if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex)
1934 unsigned int i, num_sec;
1936 num_sec = elf_numsections (abfd);
1937 for (i = shindex + 1; i < num_sec; i++)
1939 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1940 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1941 && hdr2->sh_link == shindex)
1942 break;
1944 if (i == num_sec)
1945 for (i = 1; i < shindex; i++)
1947 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1948 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1949 && hdr2->sh_link == shindex)
1950 break;
1952 if (i != shindex)
1953 return bfd_section_from_shdr (abfd, i);
1955 return TRUE;
1957 case SHT_DYNSYM: /* A dynamic symbol table */
1958 if (elf_dynsymtab (abfd) == shindex)
1959 return TRUE;
1961 if (hdr->sh_entsize != bed->s->sizeof_sym)
1962 return FALSE;
1963 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1964 elf_dynsymtab (abfd) = shindex;
1965 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1966 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1967 abfd->flags |= HAS_SYMS;
1969 /* Besides being a symbol table, we also treat this as a regular
1970 section, so that objcopy can handle it. */
1971 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1973 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1974 if (elf_symtab_shndx (abfd) == shindex)
1975 return TRUE;
1977 BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
1978 elf_symtab_shndx (abfd) = shindex;
1979 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1980 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1981 return TRUE;
1983 case SHT_STRTAB: /* A string table */
1984 if (hdr->bfd_section != NULL)
1985 return TRUE;
1986 if (ehdr->e_shstrndx == shindex)
1988 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1989 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1990 return TRUE;
1992 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
1994 symtab_strtab:
1995 elf_tdata (abfd)->strtab_hdr = *hdr;
1996 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
1997 return TRUE;
1999 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2001 dynsymtab_strtab:
2002 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2003 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2004 elf_elfsections (abfd)[shindex] = hdr;
2005 /* We also treat this as a regular section, so that objcopy
2006 can handle it. */
2007 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2008 shindex);
2011 /* If the string table isn't one of the above, then treat it as a
2012 regular section. We need to scan all the headers to be sure,
2013 just in case this strtab section appeared before the above. */
2014 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2016 unsigned int i, num_sec;
2018 num_sec = elf_numsections (abfd);
2019 for (i = 1; i < num_sec; i++)
2021 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2022 if (hdr2->sh_link == shindex)
2024 /* Prevent endless recursion on broken objects. */
2025 if (i == shindex)
2026 return FALSE;
2027 if (! bfd_section_from_shdr (abfd, i))
2028 return FALSE;
2029 if (elf_onesymtab (abfd) == i)
2030 goto symtab_strtab;
2031 if (elf_dynsymtab (abfd) == i)
2032 goto dynsymtab_strtab;
2036 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2038 case SHT_REL:
2039 case SHT_RELA:
2040 /* *These* do a lot of work -- but build no sections! */
2042 asection *target_sect;
2043 Elf_Internal_Shdr *hdr2;
2044 unsigned int num_sec = elf_numsections (abfd);
2046 if (hdr->sh_entsize
2047 != (bfd_size_type) (hdr->sh_type == SHT_REL
2048 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
2049 return FALSE;
2051 /* Check for a bogus link to avoid crashing. */
2052 if ((hdr->sh_link >= SHN_LORESERVE && hdr->sh_link <= SHN_HIRESERVE)
2053 || hdr->sh_link >= num_sec)
2055 ((*_bfd_error_handler)
2056 (_("%B: invalid link %lu for reloc section %s (index %u)"),
2057 abfd, hdr->sh_link, name, shindex));
2058 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2059 shindex);
2062 /* For some incomprehensible reason Oracle distributes
2063 libraries for Solaris in which some of the objects have
2064 bogus sh_link fields. It would be nice if we could just
2065 reject them, but, unfortunately, some people need to use
2066 them. We scan through the section headers; if we find only
2067 one suitable symbol table, we clobber the sh_link to point
2068 to it. I hope this doesn't break anything. */
2069 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
2070 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
2072 unsigned int scan;
2073 int found;
2075 found = 0;
2076 for (scan = 1; scan < num_sec; scan++)
2078 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
2079 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
2081 if (found != 0)
2083 found = 0;
2084 break;
2086 found = scan;
2089 if (found != 0)
2090 hdr->sh_link = found;
2093 /* Get the symbol table. */
2094 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2095 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2096 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2097 return FALSE;
2099 /* If this reloc section does not use the main symbol table we
2100 don't treat it as a reloc section. BFD can't adequately
2101 represent such a section, so at least for now, we don't
2102 try. We just present it as a normal section. We also
2103 can't use it as a reloc section if it points to the null
2104 section, an invalid section, or another reloc section. */
2105 if (hdr->sh_link != elf_onesymtab (abfd)
2106 || hdr->sh_info == SHN_UNDEF
2107 || (hdr->sh_info >= SHN_LORESERVE && hdr->sh_info <= SHN_HIRESERVE)
2108 || hdr->sh_info >= num_sec
2109 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2110 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2111 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2112 shindex);
2114 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2115 return FALSE;
2116 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2117 if (target_sect == NULL)
2118 return FALSE;
2120 if ((target_sect->flags & SEC_RELOC) == 0
2121 || target_sect->reloc_count == 0)
2122 hdr2 = &elf_section_data (target_sect)->rel_hdr;
2123 else
2125 bfd_size_type amt;
2126 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
2127 amt = sizeof (*hdr2);
2128 hdr2 = bfd_alloc (abfd, amt);
2129 elf_section_data (target_sect)->rel_hdr2 = hdr2;
2131 *hdr2 = *hdr;
2132 elf_elfsections (abfd)[shindex] = hdr2;
2133 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
2134 target_sect->flags |= SEC_RELOC;
2135 target_sect->relocation = NULL;
2136 target_sect->rel_filepos = hdr->sh_offset;
2137 /* In the section to which the relocations apply, mark whether
2138 its relocations are of the REL or RELA variety. */
2139 if (hdr->sh_size != 0)
2140 target_sect->use_rela_p = hdr->sh_type == SHT_RELA;
2141 abfd->flags |= HAS_RELOC;
2142 return TRUE;
2145 case SHT_GNU_verdef:
2146 elf_dynverdef (abfd) = shindex;
2147 elf_tdata (abfd)->dynverdef_hdr = *hdr;
2148 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2150 case SHT_GNU_versym:
2151 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2152 return FALSE;
2153 elf_dynversym (abfd) = shindex;
2154 elf_tdata (abfd)->dynversym_hdr = *hdr;
2155 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2157 case SHT_GNU_verneed:
2158 elf_dynverref (abfd) = shindex;
2159 elf_tdata (abfd)->dynverref_hdr = *hdr;
2160 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2162 case SHT_SHLIB:
2163 return TRUE;
2165 case SHT_GROUP:
2166 /* We need a BFD section for objcopy and relocatable linking,
2167 and it's handy to have the signature available as the section
2168 name. */
2169 if (! IS_VALID_GROUP_SECTION_HEADER (hdr))
2170 return FALSE;
2171 name = group_signature (abfd, hdr);
2172 if (name == NULL)
2173 return FALSE;
2174 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2175 return FALSE;
2176 if (hdr->contents != NULL)
2178 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
2179 unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE;
2180 asection *s;
2182 if (idx->flags & GRP_COMDAT)
2183 hdr->bfd_section->flags
2184 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
2186 /* We try to keep the same section order as it comes in. */
2187 idx += n_elt;
2188 while (--n_elt != 0)
2190 --idx;
2192 if (idx->shdr != NULL
2193 && (s = idx->shdr->bfd_section) != NULL
2194 && elf_next_in_group (s) != NULL)
2196 elf_next_in_group (hdr->bfd_section) = s;
2197 break;
2201 break;
2203 default:
2204 /* Possibly an attributes section. */
2205 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2206 || hdr->sh_type == bed->obj_attrs_section_type)
2208 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2209 return FALSE;
2210 _bfd_elf_parse_attributes (abfd, hdr);
2211 return TRUE;
2214 /* Check for any processor-specific section types. */
2215 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2216 return TRUE;
2218 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2220 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2221 /* FIXME: How to properly handle allocated section reserved
2222 for applications? */
2223 (*_bfd_error_handler)
2224 (_("%B: don't know how to handle allocated, application "
2225 "specific section `%s' [0x%8x]"),
2226 abfd, name, hdr->sh_type);
2227 else
2228 /* Allow sections reserved for applications. */
2229 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2230 shindex);
2232 else if (hdr->sh_type >= SHT_LOPROC
2233 && hdr->sh_type <= SHT_HIPROC)
2234 /* FIXME: We should handle this section. */
2235 (*_bfd_error_handler)
2236 (_("%B: don't know how to handle processor specific section "
2237 "`%s' [0x%8x]"),
2238 abfd, name, hdr->sh_type);
2239 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2241 /* Unrecognised OS-specific sections. */
2242 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2243 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2244 required to correctly process the section and the file should
2245 be rejected with an error message. */
2246 (*_bfd_error_handler)
2247 (_("%B: don't know how to handle OS specific section "
2248 "`%s' [0x%8x]"),
2249 abfd, name, hdr->sh_type);
2250 else
2251 /* Otherwise it should be processed. */
2252 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2254 else
2255 /* FIXME: We should handle this section. */
2256 (*_bfd_error_handler)
2257 (_("%B: don't know how to handle section `%s' [0x%8x]"),
2258 abfd, name, hdr->sh_type);
2260 return FALSE;
2263 return TRUE;
2266 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
2267 Return SEC for sections that have no elf section, and NULL on error. */
2269 asection *
2270 bfd_section_from_r_symndx (bfd *abfd,
2271 struct sym_sec_cache *cache,
2272 asection *sec,
2273 unsigned long r_symndx)
2275 Elf_Internal_Shdr *symtab_hdr;
2276 unsigned char esym[sizeof (Elf64_External_Sym)];
2277 Elf_External_Sym_Shndx eshndx;
2278 Elf_Internal_Sym isym;
2279 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2281 if (cache->abfd == abfd && cache->indx[ent] == r_symndx)
2282 return cache->sec[ent];
2284 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2285 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2286 &isym, esym, &eshndx) == NULL)
2287 return NULL;
2289 if (cache->abfd != abfd)
2291 memset (cache->indx, -1, sizeof (cache->indx));
2292 cache->abfd = abfd;
2294 cache->indx[ent] = r_symndx;
2295 cache->sec[ent] = sec;
2296 if ((isym.st_shndx != SHN_UNDEF && isym.st_shndx < SHN_LORESERVE)
2297 || isym.st_shndx > SHN_HIRESERVE)
2299 asection *s;
2300 s = bfd_section_from_elf_index (abfd, isym.st_shndx);
2301 if (s != NULL)
2302 cache->sec[ent] = s;
2304 return cache->sec[ent];
2307 /* Given an ELF section number, retrieve the corresponding BFD
2308 section. */
2310 asection *
2311 bfd_section_from_elf_index (bfd *abfd, unsigned int index)
2313 if (index >= elf_numsections (abfd))
2314 return NULL;
2315 return elf_elfsections (abfd)[index]->bfd_section;
2318 static const struct bfd_elf_special_section special_sections_b[] =
2320 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2321 { NULL, 0, 0, 0, 0 }
2324 static const struct bfd_elf_special_section special_sections_c[] =
2326 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2327 { NULL, 0, 0, 0, 0 }
2330 static const struct bfd_elf_special_section special_sections_d[] =
2332 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2333 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2334 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2335 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2336 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2337 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2338 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2339 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2340 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2341 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2342 { NULL, 0, 0, 0, 0 }
2345 static const struct bfd_elf_special_section special_sections_f[] =
2347 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2348 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2349 { NULL, 0, 0, 0, 0 }
2352 static const struct bfd_elf_special_section special_sections_g[] =
2354 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2355 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2356 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2357 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2358 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2359 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2360 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2361 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2362 { NULL, 0, 0, 0, 0 }
2365 static const struct bfd_elf_special_section special_sections_h[] =
2367 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2368 { NULL, 0, 0, 0, 0 }
2371 static const struct bfd_elf_special_section special_sections_i[] =
2373 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2374 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2375 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2376 { NULL, 0, 0, 0, 0 }
2379 static const struct bfd_elf_special_section special_sections_l[] =
2381 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2382 { NULL, 0, 0, 0, 0 }
2385 static const struct bfd_elf_special_section special_sections_n[] =
2387 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2388 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2389 { NULL, 0, 0, 0, 0 }
2392 static const struct bfd_elf_special_section special_sections_p[] =
2394 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2395 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2396 { NULL, 0, 0, 0, 0 }
2399 static const struct bfd_elf_special_section special_sections_r[] =
2401 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2402 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2403 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2404 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2405 { NULL, 0, 0, 0, 0 }
2408 static const struct bfd_elf_special_section special_sections_s[] =
2410 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2411 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2412 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2413 /* See struct bfd_elf_special_section declaration for the semantics of
2414 this special case where .prefix_length != strlen (.prefix). */
2415 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2416 { NULL, 0, 0, 0, 0 }
2419 static const struct bfd_elf_special_section special_sections_t[] =
2421 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2422 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2423 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2424 { NULL, 0, 0, 0, 0 }
2427 static const struct bfd_elf_special_section *special_sections[] =
2429 special_sections_b, /* 'b' */
2430 special_sections_c, /* 'b' */
2431 special_sections_d, /* 'd' */
2432 NULL, /* 'e' */
2433 special_sections_f, /* 'f' */
2434 special_sections_g, /* 'g' */
2435 special_sections_h, /* 'h' */
2436 special_sections_i, /* 'i' */
2437 NULL, /* 'j' */
2438 NULL, /* 'k' */
2439 special_sections_l, /* 'l' */
2440 NULL, /* 'm' */
2441 special_sections_n, /* 'n' */
2442 NULL, /* 'o' */
2443 special_sections_p, /* 'p' */
2444 NULL, /* 'q' */
2445 special_sections_r, /* 'r' */
2446 special_sections_s, /* 's' */
2447 special_sections_t, /* 't' */
2450 const struct bfd_elf_special_section *
2451 _bfd_elf_get_special_section (const char *name,
2452 const struct bfd_elf_special_section *spec,
2453 unsigned int rela)
2455 int i;
2456 int len;
2458 len = strlen (name);
2460 for (i = 0; spec[i].prefix != NULL; i++)
2462 int suffix_len;
2463 int prefix_len = spec[i].prefix_length;
2465 if (len < prefix_len)
2466 continue;
2467 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2468 continue;
2470 suffix_len = spec[i].suffix_length;
2471 if (suffix_len <= 0)
2473 if (name[prefix_len] != 0)
2475 if (suffix_len == 0)
2476 continue;
2477 if (name[prefix_len] != '.'
2478 && (suffix_len == -2
2479 || (rela && spec[i].type == SHT_REL)))
2480 continue;
2483 else
2485 if (len < prefix_len + suffix_len)
2486 continue;
2487 if (memcmp (name + len - suffix_len,
2488 spec[i].prefix + prefix_len,
2489 suffix_len) != 0)
2490 continue;
2492 return &spec[i];
2495 return NULL;
2498 const struct bfd_elf_special_section *
2499 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2501 int i;
2502 const struct bfd_elf_special_section *spec;
2503 const struct elf_backend_data *bed;
2505 /* See if this is one of the special sections. */
2506 if (sec->name == NULL)
2507 return NULL;
2509 bed = get_elf_backend_data (abfd);
2510 spec = bed->special_sections;
2511 if (spec)
2513 spec = _bfd_elf_get_special_section (sec->name,
2514 bed->special_sections,
2515 sec->use_rela_p);
2516 if (spec != NULL)
2517 return spec;
2520 if (sec->name[0] != '.')
2521 return NULL;
2523 i = sec->name[1] - 'b';
2524 if (i < 0 || i > 't' - 'b')
2525 return NULL;
2527 spec = special_sections[i];
2529 if (spec == NULL)
2530 return NULL;
2532 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2535 bfd_boolean
2536 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2538 struct bfd_elf_section_data *sdata;
2539 const struct elf_backend_data *bed;
2540 const struct bfd_elf_special_section *ssect;
2542 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2543 if (sdata == NULL)
2545 sdata = bfd_zalloc (abfd, sizeof (*sdata));
2546 if (sdata == NULL)
2547 return FALSE;
2548 sec->used_by_bfd = sdata;
2551 /* Indicate whether or not this section should use RELA relocations. */
2552 bed = get_elf_backend_data (abfd);
2553 sec->use_rela_p = bed->default_use_rela_p;
2555 /* When we read a file, we don't need to set ELF section type and
2556 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2557 anyway. We will set ELF section type and flags for all linker
2558 created sections. If user specifies BFD section flags, we will
2559 set ELF section type and flags based on BFD section flags in
2560 elf_fake_sections. */
2561 if ((!sec->flags && abfd->direction != read_direction)
2562 || (sec->flags & SEC_LINKER_CREATED) != 0)
2564 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2565 if (ssect != NULL)
2567 elf_section_type (sec) = ssect->type;
2568 elf_section_flags (sec) = ssect->attr;
2572 return _bfd_generic_new_section_hook (abfd, sec);
2575 /* Create a new bfd section from an ELF program header.
2577 Since program segments have no names, we generate a synthetic name
2578 of the form segment<NUM>, where NUM is generally the index in the
2579 program header table. For segments that are split (see below) we
2580 generate the names segment<NUM>a and segment<NUM>b.
2582 Note that some program segments may have a file size that is different than
2583 (less than) the memory size. All this means is that at execution the
2584 system must allocate the amount of memory specified by the memory size,
2585 but only initialize it with the first "file size" bytes read from the
2586 file. This would occur for example, with program segments consisting
2587 of combined data+bss.
2589 To handle the above situation, this routine generates TWO bfd sections
2590 for the single program segment. The first has the length specified by
2591 the file size of the segment, and the second has the length specified
2592 by the difference between the two sizes. In effect, the segment is split
2593 into it's initialized and uninitialized parts.
2597 bfd_boolean
2598 _bfd_elf_make_section_from_phdr (bfd *abfd,
2599 Elf_Internal_Phdr *hdr,
2600 int index,
2601 const char *typename)
2603 asection *newsect;
2604 char *name;
2605 char namebuf[64];
2606 size_t len;
2607 int split;
2609 split = ((hdr->p_memsz > 0)
2610 && (hdr->p_filesz > 0)
2611 && (hdr->p_memsz > hdr->p_filesz));
2612 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
2613 len = strlen (namebuf) + 1;
2614 name = bfd_alloc (abfd, len);
2615 if (!name)
2616 return FALSE;
2617 memcpy (name, namebuf, len);
2618 newsect = bfd_make_section (abfd, name);
2619 if (newsect == NULL)
2620 return FALSE;
2621 newsect->vma = hdr->p_vaddr;
2622 newsect->lma = hdr->p_paddr;
2623 newsect->size = hdr->p_filesz;
2624 newsect->filepos = hdr->p_offset;
2625 newsect->flags |= SEC_HAS_CONTENTS;
2626 newsect->alignment_power = bfd_log2 (hdr->p_align);
2627 if (hdr->p_type == PT_LOAD)
2629 newsect->flags |= SEC_ALLOC;
2630 newsect->flags |= SEC_LOAD;
2631 if (hdr->p_flags & PF_X)
2633 /* FIXME: all we known is that it has execute PERMISSION,
2634 may be data. */
2635 newsect->flags |= SEC_CODE;
2638 if (!(hdr->p_flags & PF_W))
2640 newsect->flags |= SEC_READONLY;
2643 if (split)
2645 sprintf (namebuf, "%s%db", typename, index);
2646 len = strlen (namebuf) + 1;
2647 name = bfd_alloc (abfd, len);
2648 if (!name)
2649 return FALSE;
2650 memcpy (name, namebuf, len);
2651 newsect = bfd_make_section (abfd, name);
2652 if (newsect == NULL)
2653 return FALSE;
2654 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2655 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2656 newsect->size = hdr->p_memsz - hdr->p_filesz;
2657 if (hdr->p_type == PT_LOAD)
2659 newsect->flags |= SEC_ALLOC;
2660 if (hdr->p_flags & PF_X)
2661 newsect->flags |= SEC_CODE;
2663 if (!(hdr->p_flags & PF_W))
2664 newsect->flags |= SEC_READONLY;
2667 return TRUE;
2670 bfd_boolean
2671 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index)
2673 const struct elf_backend_data *bed;
2675 switch (hdr->p_type)
2677 case PT_NULL:
2678 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2680 case PT_LOAD:
2681 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2683 case PT_DYNAMIC:
2684 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2686 case PT_INTERP:
2687 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2689 case PT_NOTE:
2690 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2691 return FALSE;
2692 if (! elfcore_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2693 return FALSE;
2694 return TRUE;
2696 case PT_SHLIB:
2697 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2699 case PT_PHDR:
2700 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2702 case PT_GNU_EH_FRAME:
2703 return _bfd_elf_make_section_from_phdr (abfd, hdr, index,
2704 "eh_frame_hdr");
2706 case PT_GNU_STACK:
2707 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "stack");
2709 case PT_GNU_RELRO:
2710 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro");
2712 default:
2713 /* Check for any processor-specific program segment types. */
2714 bed = get_elf_backend_data (abfd);
2715 return bed->elf_backend_section_from_phdr (abfd, hdr, index, "proc");
2719 /* Initialize REL_HDR, the section-header for new section, containing
2720 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2721 relocations; otherwise, we use REL relocations. */
2723 bfd_boolean
2724 _bfd_elf_init_reloc_shdr (bfd *abfd,
2725 Elf_Internal_Shdr *rel_hdr,
2726 asection *asect,
2727 bfd_boolean use_rela_p)
2729 char *name;
2730 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2731 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2733 name = bfd_alloc (abfd, amt);
2734 if (name == NULL)
2735 return FALSE;
2736 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2737 rel_hdr->sh_name =
2738 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2739 FALSE);
2740 if (rel_hdr->sh_name == (unsigned int) -1)
2741 return FALSE;
2742 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2743 rel_hdr->sh_entsize = (use_rela_p
2744 ? bed->s->sizeof_rela
2745 : bed->s->sizeof_rel);
2746 rel_hdr->sh_addralign = 1 << bed->s->log_file_align;
2747 rel_hdr->sh_flags = 0;
2748 rel_hdr->sh_addr = 0;
2749 rel_hdr->sh_size = 0;
2750 rel_hdr->sh_offset = 0;
2752 return TRUE;
2755 /* Set up an ELF internal section header for a section. */
2757 static void
2758 elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg)
2760 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2761 bfd_boolean *failedptr = failedptrarg;
2762 Elf_Internal_Shdr *this_hdr;
2763 unsigned int sh_type;
2765 if (*failedptr)
2767 /* We already failed; just get out of the bfd_map_over_sections
2768 loop. */
2769 return;
2772 this_hdr = &elf_section_data (asect)->this_hdr;
2774 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2775 asect->name, FALSE);
2776 if (this_hdr->sh_name == (unsigned int) -1)
2778 *failedptr = TRUE;
2779 return;
2782 /* Don't clear sh_flags. Assembler may set additional bits. */
2784 if ((asect->flags & SEC_ALLOC) != 0
2785 || asect->user_set_vma)
2786 this_hdr->sh_addr = asect->vma;
2787 else
2788 this_hdr->sh_addr = 0;
2790 this_hdr->sh_offset = 0;
2791 this_hdr->sh_size = asect->size;
2792 this_hdr->sh_link = 0;
2793 this_hdr->sh_addralign = 1 << asect->alignment_power;
2794 /* The sh_entsize and sh_info fields may have been set already by
2795 copy_private_section_data. */
2797 this_hdr->bfd_section = asect;
2798 this_hdr->contents = NULL;
2800 /* If the section type is unspecified, we set it based on
2801 asect->flags. */
2802 if (this_hdr->sh_type == SHT_NULL)
2804 if ((asect->flags & SEC_GROUP) != 0)
2805 this_hdr->sh_type = SHT_GROUP;
2806 else if ((asect->flags & SEC_ALLOC) != 0
2807 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2808 || (asect->flags & SEC_NEVER_LOAD) != 0))
2809 this_hdr->sh_type = SHT_NOBITS;
2810 else
2811 this_hdr->sh_type = SHT_PROGBITS;
2814 switch (this_hdr->sh_type)
2816 default:
2817 break;
2819 case SHT_STRTAB:
2820 case SHT_INIT_ARRAY:
2821 case SHT_FINI_ARRAY:
2822 case SHT_PREINIT_ARRAY:
2823 case SHT_NOTE:
2824 case SHT_NOBITS:
2825 case SHT_PROGBITS:
2826 break;
2828 case SHT_HASH:
2829 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2830 break;
2832 case SHT_DYNSYM:
2833 this_hdr->sh_entsize = bed->s->sizeof_sym;
2834 break;
2836 case SHT_DYNAMIC:
2837 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2838 break;
2840 case SHT_RELA:
2841 if (get_elf_backend_data (abfd)->may_use_rela_p)
2842 this_hdr->sh_entsize = bed->s->sizeof_rela;
2843 break;
2845 case SHT_REL:
2846 if (get_elf_backend_data (abfd)->may_use_rel_p)
2847 this_hdr->sh_entsize = bed->s->sizeof_rel;
2848 break;
2850 case SHT_GNU_versym:
2851 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2852 break;
2854 case SHT_GNU_verdef:
2855 this_hdr->sh_entsize = 0;
2856 /* objcopy or strip will copy over sh_info, but may not set
2857 cverdefs. The linker will set cverdefs, but sh_info will be
2858 zero. */
2859 if (this_hdr->sh_info == 0)
2860 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2861 else
2862 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2863 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2864 break;
2866 case SHT_GNU_verneed:
2867 this_hdr->sh_entsize = 0;
2868 /* objcopy or strip will copy over sh_info, but may not set
2869 cverrefs. The linker will set cverrefs, but sh_info will be
2870 zero. */
2871 if (this_hdr->sh_info == 0)
2872 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2873 else
2874 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2875 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2876 break;
2878 case SHT_GROUP:
2879 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2880 break;
2882 case SHT_GNU_HASH:
2883 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
2884 break;
2887 if ((asect->flags & SEC_ALLOC) != 0)
2888 this_hdr->sh_flags |= SHF_ALLOC;
2889 if ((asect->flags & SEC_READONLY) == 0)
2890 this_hdr->sh_flags |= SHF_WRITE;
2891 if ((asect->flags & SEC_CODE) != 0)
2892 this_hdr->sh_flags |= SHF_EXECINSTR;
2893 if ((asect->flags & SEC_MERGE) != 0)
2895 this_hdr->sh_flags |= SHF_MERGE;
2896 this_hdr->sh_entsize = asect->entsize;
2897 if ((asect->flags & SEC_STRINGS) != 0)
2898 this_hdr->sh_flags |= SHF_STRINGS;
2900 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2901 this_hdr->sh_flags |= SHF_GROUP;
2902 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2904 this_hdr->sh_flags |= SHF_TLS;
2905 if (asect->size == 0
2906 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2908 struct bfd_link_order *o = asect->map_tail.link_order;
2910 this_hdr->sh_size = 0;
2911 if (o != NULL)
2913 this_hdr->sh_size = o->offset + o->size;
2914 if (this_hdr->sh_size != 0)
2915 this_hdr->sh_type = SHT_NOBITS;
2920 /* Check for processor-specific section types. */
2921 sh_type = this_hdr->sh_type;
2922 if (bed->elf_backend_fake_sections
2923 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2924 *failedptr = TRUE;
2926 if (sh_type == SHT_NOBITS && asect->size != 0)
2928 /* Don't change the header type from NOBITS if we are being
2929 called for objcopy --only-keep-debug. */
2930 this_hdr->sh_type = sh_type;
2933 /* If the section has relocs, set up a section header for the
2934 SHT_REL[A] section. If two relocation sections are required for
2935 this section, it is up to the processor-specific back-end to
2936 create the other. */
2937 if ((asect->flags & SEC_RELOC) != 0
2938 && !_bfd_elf_init_reloc_shdr (abfd,
2939 &elf_section_data (asect)->rel_hdr,
2940 asect,
2941 asect->use_rela_p))
2942 *failedptr = TRUE;
2945 /* Fill in the contents of a SHT_GROUP section. */
2947 void
2948 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2950 bfd_boolean *failedptr = failedptrarg;
2951 unsigned long symindx;
2952 asection *elt, *first;
2953 unsigned char *loc;
2954 bfd_boolean gas;
2956 /* Ignore linker created group section. See elfNN_ia64_object_p in
2957 elfxx-ia64.c. */
2958 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
2959 || *failedptr)
2960 return;
2962 symindx = 0;
2963 if (elf_group_id (sec) != NULL)
2964 symindx = elf_group_id (sec)->udata.i;
2966 if (symindx == 0)
2968 /* If called from the assembler, swap_out_syms will have set up
2969 elf_section_syms; If called for "ld -r", use target_index. */
2970 if (elf_section_syms (abfd) != NULL)
2971 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2972 else
2973 symindx = sec->target_index;
2975 elf_section_data (sec)->this_hdr.sh_info = symindx;
2977 /* The contents won't be allocated for "ld -r" or objcopy. */
2978 gas = TRUE;
2979 if (sec->contents == NULL)
2981 gas = FALSE;
2982 sec->contents = bfd_alloc (abfd, sec->size);
2984 /* Arrange for the section to be written out. */
2985 elf_section_data (sec)->this_hdr.contents = sec->contents;
2986 if (sec->contents == NULL)
2988 *failedptr = TRUE;
2989 return;
2993 loc = sec->contents + sec->size;
2995 /* Get the pointer to the first section in the group that gas
2996 squirreled away here. objcopy arranges for this to be set to the
2997 start of the input section group. */
2998 first = elt = elf_next_in_group (sec);
3000 /* First element is a flag word. Rest of section is elf section
3001 indices for all the sections of the group. Write them backwards
3002 just to keep the group in the same order as given in .section
3003 directives, not that it matters. */
3004 while (elt != NULL)
3006 asection *s;
3007 unsigned int idx;
3009 loc -= 4;
3010 s = elt;
3011 if (!gas)
3012 s = s->output_section;
3013 idx = 0;
3014 if (s != NULL)
3015 idx = elf_section_data (s)->this_idx;
3016 H_PUT_32 (abfd, idx, loc);
3017 elt = elf_next_in_group (elt);
3018 if (elt == first)
3019 break;
3022 if ((loc -= 4) != sec->contents)
3023 abort ();
3025 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
3028 /* Assign all ELF section numbers. The dummy first section is handled here
3029 too. The link/info pointers for the standard section types are filled
3030 in here too, while we're at it. */
3032 static bfd_boolean
3033 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
3035 struct elf_obj_tdata *t = elf_tdata (abfd);
3036 asection *sec;
3037 unsigned int section_number, secn;
3038 Elf_Internal_Shdr **i_shdrp;
3039 struct bfd_elf_section_data *d;
3041 section_number = 1;
3043 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3045 /* SHT_GROUP sections are in relocatable files only. */
3046 if (link_info == NULL || link_info->relocatable)
3048 /* Put SHT_GROUP sections first. */
3049 for (sec = abfd->sections; sec != NULL; sec = sec->next)
3051 d = elf_section_data (sec);
3053 if (d->this_hdr.sh_type == SHT_GROUP)
3055 if (sec->flags & SEC_LINKER_CREATED)
3057 /* Remove the linker created SHT_GROUP sections. */
3058 bfd_section_list_remove (abfd, sec);
3059 abfd->section_count--;
3061 else
3063 if (section_number == SHN_LORESERVE)
3064 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3065 d->this_idx = section_number++;
3071 for (sec = abfd->sections; sec; sec = sec->next)
3073 d = elf_section_data (sec);
3075 if (d->this_hdr.sh_type != SHT_GROUP)
3077 if (section_number == SHN_LORESERVE)
3078 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3079 d->this_idx = section_number++;
3081 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
3082 if ((sec->flags & SEC_RELOC) == 0)
3083 d->rel_idx = 0;
3084 else
3086 if (section_number == SHN_LORESERVE)
3087 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3088 d->rel_idx = section_number++;
3089 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
3092 if (d->rel_hdr2)
3094 if (section_number == SHN_LORESERVE)
3095 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3096 d->rel_idx2 = section_number++;
3097 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
3099 else
3100 d->rel_idx2 = 0;
3103 if (section_number == SHN_LORESERVE)
3104 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3105 t->shstrtab_section = section_number++;
3106 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3107 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
3109 if (bfd_get_symcount (abfd) > 0)
3111 if (section_number == SHN_LORESERVE)
3112 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3113 t->symtab_section = section_number++;
3114 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
3115 if (section_number > SHN_LORESERVE - 2)
3117 if (section_number == SHN_LORESERVE)
3118 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3119 t->symtab_shndx_section = section_number++;
3120 t->symtab_shndx_hdr.sh_name
3121 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3122 ".symtab_shndx", FALSE);
3123 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
3124 return FALSE;
3126 if (section_number == SHN_LORESERVE)
3127 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3128 t->strtab_section = section_number++;
3129 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
3132 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
3133 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3135 elf_numsections (abfd) = section_number;
3136 elf_elfheader (abfd)->e_shnum = section_number;
3137 if (section_number > SHN_LORESERVE)
3138 elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE;
3140 /* Set up the list of section header pointers, in agreement with the
3141 indices. */
3142 i_shdrp = bfd_zalloc2 (abfd, section_number, sizeof (Elf_Internal_Shdr *));
3143 if (i_shdrp == NULL)
3144 return FALSE;
3146 i_shdrp[0] = bfd_zalloc (abfd, sizeof (Elf_Internal_Shdr));
3147 if (i_shdrp[0] == NULL)
3149 bfd_release (abfd, i_shdrp);
3150 return FALSE;
3153 elf_elfsections (abfd) = i_shdrp;
3155 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
3156 if (bfd_get_symcount (abfd) > 0)
3158 i_shdrp[t->symtab_section] = &t->symtab_hdr;
3159 if (elf_numsections (abfd) > SHN_LORESERVE)
3161 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
3162 t->symtab_shndx_hdr.sh_link = t->symtab_section;
3164 i_shdrp[t->strtab_section] = &t->strtab_hdr;
3165 t->symtab_hdr.sh_link = t->strtab_section;
3168 for (sec = abfd->sections; sec; sec = sec->next)
3170 struct bfd_elf_section_data *d = elf_section_data (sec);
3171 asection *s;
3172 const char *name;
3174 i_shdrp[d->this_idx] = &d->this_hdr;
3175 if (d->rel_idx != 0)
3176 i_shdrp[d->rel_idx] = &d->rel_hdr;
3177 if (d->rel_idx2 != 0)
3178 i_shdrp[d->rel_idx2] = d->rel_hdr2;
3180 /* Fill in the sh_link and sh_info fields while we're at it. */
3182 /* sh_link of a reloc section is the section index of the symbol
3183 table. sh_info is the section index of the section to which
3184 the relocation entries apply. */
3185 if (d->rel_idx != 0)
3187 d->rel_hdr.sh_link = t->symtab_section;
3188 d->rel_hdr.sh_info = d->this_idx;
3190 if (d->rel_idx2 != 0)
3192 d->rel_hdr2->sh_link = t->symtab_section;
3193 d->rel_hdr2->sh_info = d->this_idx;
3196 /* We need to set up sh_link for SHF_LINK_ORDER. */
3197 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3199 s = elf_linked_to_section (sec);
3200 if (s)
3202 /* elf_linked_to_section points to the input section. */
3203 if (link_info != NULL)
3205 /* Check discarded linkonce section. */
3206 if (elf_discarded_section (s))
3208 asection *kept;
3209 (*_bfd_error_handler)
3210 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3211 abfd, d->this_hdr.bfd_section,
3212 s, s->owner);
3213 /* Point to the kept section if it has the same
3214 size as the discarded one. */
3215 kept = _bfd_elf_check_kept_section (s, link_info);
3216 if (kept == NULL)
3218 bfd_set_error (bfd_error_bad_value);
3219 return FALSE;
3221 s = kept;
3224 s = s->output_section;
3225 BFD_ASSERT (s != NULL);
3227 else
3229 /* Handle objcopy. */
3230 if (s->output_section == NULL)
3232 (*_bfd_error_handler)
3233 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3234 abfd, d->this_hdr.bfd_section, s, s->owner);
3235 bfd_set_error (bfd_error_bad_value);
3236 return FALSE;
3238 s = s->output_section;
3240 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3242 else
3244 /* PR 290:
3245 The Intel C compiler generates SHT_IA_64_UNWIND with
3246 SHF_LINK_ORDER. But it doesn't set the sh_link or
3247 sh_info fields. Hence we could get the situation
3248 where s is NULL. */
3249 const struct elf_backend_data *bed
3250 = get_elf_backend_data (abfd);
3251 if (bed->link_order_error_handler)
3252 bed->link_order_error_handler
3253 (_("%B: warning: sh_link not set for section `%A'"),
3254 abfd, sec);
3258 switch (d->this_hdr.sh_type)
3260 case SHT_REL:
3261 case SHT_RELA:
3262 /* A reloc section which we are treating as a normal BFD
3263 section. sh_link is the section index of the symbol
3264 table. sh_info is the section index of the section to
3265 which the relocation entries apply. We assume that an
3266 allocated reloc section uses the dynamic symbol table.
3267 FIXME: How can we be sure? */
3268 s = bfd_get_section_by_name (abfd, ".dynsym");
3269 if (s != NULL)
3270 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3272 /* We look up the section the relocs apply to by name. */
3273 name = sec->name;
3274 if (d->this_hdr.sh_type == SHT_REL)
3275 name += 4;
3276 else
3277 name += 5;
3278 s = bfd_get_section_by_name (abfd, name);
3279 if (s != NULL)
3280 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3281 break;
3283 case SHT_STRTAB:
3284 /* We assume that a section named .stab*str is a stabs
3285 string section. We look for a section with the same name
3286 but without the trailing ``str'', and set its sh_link
3287 field to point to this section. */
3288 if (CONST_STRNEQ (sec->name, ".stab")
3289 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3291 size_t len;
3292 char *alc;
3294 len = strlen (sec->name);
3295 alc = bfd_malloc (len - 2);
3296 if (alc == NULL)
3297 return FALSE;
3298 memcpy (alc, sec->name, len - 3);
3299 alc[len - 3] = '\0';
3300 s = bfd_get_section_by_name (abfd, alc);
3301 free (alc);
3302 if (s != NULL)
3304 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3306 /* This is a .stab section. */
3307 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3308 elf_section_data (s)->this_hdr.sh_entsize
3309 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3312 break;
3314 case SHT_DYNAMIC:
3315 case SHT_DYNSYM:
3316 case SHT_GNU_verneed:
3317 case SHT_GNU_verdef:
3318 /* sh_link is the section header index of the string table
3319 used for the dynamic entries, or the symbol table, or the
3320 version strings. */
3321 s = bfd_get_section_by_name (abfd, ".dynstr");
3322 if (s != NULL)
3323 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3324 break;
3326 case SHT_GNU_LIBLIST:
3327 /* sh_link is the section header index of the prelink library
3328 list used for the dynamic entries, or the symbol table, or
3329 the version strings. */
3330 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3331 ? ".dynstr" : ".gnu.libstr");
3332 if (s != NULL)
3333 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3334 break;
3336 case SHT_HASH:
3337 case SHT_GNU_HASH:
3338 case SHT_GNU_versym:
3339 /* sh_link is the section header index of the symbol table
3340 this hash table or version table is for. */
3341 s = bfd_get_section_by_name (abfd, ".dynsym");
3342 if (s != NULL)
3343 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3344 break;
3346 case SHT_GROUP:
3347 d->this_hdr.sh_link = t->symtab_section;
3351 for (secn = 1; secn < section_number; ++secn)
3352 if (i_shdrp[secn] == NULL)
3353 i_shdrp[secn] = i_shdrp[0];
3354 else
3355 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
3356 i_shdrp[secn]->sh_name);
3357 return TRUE;
3360 /* Map symbol from it's internal number to the external number, moving
3361 all local symbols to be at the head of the list. */
3363 static bfd_boolean
3364 sym_is_global (bfd *abfd, asymbol *sym)
3366 /* If the backend has a special mapping, use it. */
3367 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3368 if (bed->elf_backend_sym_is_global)
3369 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3371 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
3372 || bfd_is_und_section (bfd_get_section (sym))
3373 || bfd_is_com_section (bfd_get_section (sym)));
3376 /* Don't output section symbols for sections that are not going to be
3377 output. Also, don't output section symbols for reloc and other
3378 special sections. */
3380 static bfd_boolean
3381 ignore_section_sym (bfd *abfd, asymbol *sym)
3383 return ((sym->flags & BSF_SECTION_SYM) != 0
3384 && (sym->value != 0
3385 || (sym->section->owner != abfd
3386 && (sym->section->output_section->owner != abfd
3387 || sym->section->output_offset != 0))));
3390 static bfd_boolean
3391 elf_map_symbols (bfd *abfd)
3393 unsigned int symcount = bfd_get_symcount (abfd);
3394 asymbol **syms = bfd_get_outsymbols (abfd);
3395 asymbol **sect_syms;
3396 unsigned int num_locals = 0;
3397 unsigned int num_globals = 0;
3398 unsigned int num_locals2 = 0;
3399 unsigned int num_globals2 = 0;
3400 int max_index = 0;
3401 unsigned int idx;
3402 asection *asect;
3403 asymbol **new_syms;
3405 #ifdef DEBUG
3406 fprintf (stderr, "elf_map_symbols\n");
3407 fflush (stderr);
3408 #endif
3410 for (asect = abfd->sections; asect; asect = asect->next)
3412 if (max_index < asect->index)
3413 max_index = asect->index;
3416 max_index++;
3417 sect_syms = bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
3418 if (sect_syms == NULL)
3419 return FALSE;
3420 elf_section_syms (abfd) = sect_syms;
3421 elf_num_section_syms (abfd) = max_index;
3423 /* Init sect_syms entries for any section symbols we have already
3424 decided to output. */
3425 for (idx = 0; idx < symcount; idx++)
3427 asymbol *sym = syms[idx];
3429 if ((sym->flags & BSF_SECTION_SYM) != 0
3430 && !ignore_section_sym (abfd, sym))
3432 asection *sec = sym->section;
3434 if (sec->owner != abfd)
3435 sec = sec->output_section;
3437 sect_syms[sec->index] = syms[idx];
3441 /* Classify all of the symbols. */
3442 for (idx = 0; idx < symcount; idx++)
3444 if (ignore_section_sym (abfd, syms[idx]))
3445 continue;
3446 if (!sym_is_global (abfd, syms[idx]))
3447 num_locals++;
3448 else
3449 num_globals++;
3452 /* We will be adding a section symbol for each normal BFD section. Most
3453 sections will already have a section symbol in outsymbols, but
3454 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3455 at least in that case. */
3456 for (asect = abfd->sections; asect; asect = asect->next)
3458 if (sect_syms[asect->index] == NULL)
3460 if (!sym_is_global (abfd, asect->symbol))
3461 num_locals++;
3462 else
3463 num_globals++;
3467 /* Now sort the symbols so the local symbols are first. */
3468 new_syms = bfd_alloc2 (abfd, num_locals + num_globals, sizeof (asymbol *));
3470 if (new_syms == NULL)
3471 return FALSE;
3473 for (idx = 0; idx < symcount; idx++)
3475 asymbol *sym = syms[idx];
3476 unsigned int i;
3478 if (ignore_section_sym (abfd, sym))
3479 continue;
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;
3487 for (asect = abfd->sections; asect; asect = asect->next)
3489 if (sect_syms[asect->index] == NULL)
3491 asymbol *sym = asect->symbol;
3492 unsigned int i;
3494 sect_syms[asect->index] = sym;
3495 if (!sym_is_global (abfd, sym))
3496 i = num_locals2++;
3497 else
3498 i = num_locals + num_globals2++;
3499 new_syms[i] = sym;
3500 sym->udata.i = i + 1;
3504 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3506 elf_num_locals (abfd) = num_locals;
3507 elf_num_globals (abfd) = num_globals;
3508 return TRUE;
3511 /* Align to the maximum file alignment that could be required for any
3512 ELF data structure. */
3514 static inline file_ptr
3515 align_file_position (file_ptr off, int align)
3517 return (off + align - 1) & ~(align - 1);
3520 /* Assign a file position to a section, optionally aligning to the
3521 required section alignment. */
3523 file_ptr
3524 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3525 file_ptr offset,
3526 bfd_boolean align)
3528 if (align)
3530 unsigned int al;
3532 al = i_shdrp->sh_addralign;
3533 if (al > 1)
3534 offset = BFD_ALIGN (offset, al);
3536 i_shdrp->sh_offset = offset;
3537 if (i_shdrp->bfd_section != NULL)
3538 i_shdrp->bfd_section->filepos = offset;
3539 if (i_shdrp->sh_type != SHT_NOBITS)
3540 offset += i_shdrp->sh_size;
3541 return offset;
3544 /* Compute the file positions we are going to put the sections at, and
3545 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3546 is not NULL, this is being called by the ELF backend linker. */
3548 bfd_boolean
3549 _bfd_elf_compute_section_file_positions (bfd *abfd,
3550 struct bfd_link_info *link_info)
3552 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3553 bfd_boolean failed;
3554 struct bfd_strtab_hash *strtab = NULL;
3555 Elf_Internal_Shdr *shstrtab_hdr;
3557 if (abfd->output_has_begun)
3558 return TRUE;
3560 /* Do any elf backend specific processing first. */
3561 if (bed->elf_backend_begin_write_processing)
3562 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3564 if (! prep_headers (abfd))
3565 return FALSE;
3567 /* Post process the headers if necessary. */
3568 if (bed->elf_backend_post_process_headers)
3569 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3571 failed = FALSE;
3572 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3573 if (failed)
3574 return FALSE;
3576 if (!assign_section_numbers (abfd, link_info))
3577 return FALSE;
3579 /* The backend linker builds symbol table information itself. */
3580 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3582 /* Non-zero if doing a relocatable link. */
3583 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3585 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3586 return FALSE;
3589 if (link_info == NULL)
3591 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3592 if (failed)
3593 return FALSE;
3596 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3597 /* sh_name was set in prep_headers. */
3598 shstrtab_hdr->sh_type = SHT_STRTAB;
3599 shstrtab_hdr->sh_flags = 0;
3600 shstrtab_hdr->sh_addr = 0;
3601 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3602 shstrtab_hdr->sh_entsize = 0;
3603 shstrtab_hdr->sh_link = 0;
3604 shstrtab_hdr->sh_info = 0;
3605 /* sh_offset is set in assign_file_positions_except_relocs. */
3606 shstrtab_hdr->sh_addralign = 1;
3608 if (!assign_file_positions_except_relocs (abfd, link_info))
3609 return FALSE;
3611 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3613 file_ptr off;
3614 Elf_Internal_Shdr *hdr;
3616 off = elf_tdata (abfd)->next_file_pos;
3618 hdr = &elf_tdata (abfd)->symtab_hdr;
3619 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3621 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3622 if (hdr->sh_size != 0)
3623 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3625 hdr = &elf_tdata (abfd)->strtab_hdr;
3626 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3628 elf_tdata (abfd)->next_file_pos = off;
3630 /* Now that we know where the .strtab section goes, write it
3631 out. */
3632 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3633 || ! _bfd_stringtab_emit (abfd, strtab))
3634 return FALSE;
3635 _bfd_stringtab_free (strtab);
3638 abfd->output_has_begun = TRUE;
3640 return TRUE;
3643 /* Make an initial estimate of the size of the program header. If we
3644 get the number wrong here, we'll redo section placement. */
3646 static bfd_size_type
3647 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
3649 size_t segs;
3650 asection *s;
3651 const struct elf_backend_data *bed;
3653 /* Assume we will need exactly two PT_LOAD segments: one for text
3654 and one for data. */
3655 segs = 2;
3657 s = bfd_get_section_by_name (abfd, ".interp");
3658 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3660 /* If we have a loadable interpreter section, we need a
3661 PT_INTERP segment. In this case, assume we also need a
3662 PT_PHDR segment, although that may not be true for all
3663 targets. */
3664 segs += 2;
3667 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3669 /* We need a PT_DYNAMIC segment. */
3670 ++segs;
3672 if (elf_tdata (abfd)->relro)
3674 /* We need a PT_GNU_RELRO segment only when there is a
3675 PT_DYNAMIC segment. */
3676 ++segs;
3680 if (elf_tdata (abfd)->eh_frame_hdr)
3682 /* We need a PT_GNU_EH_FRAME segment. */
3683 ++segs;
3686 if (elf_tdata (abfd)->stack_flags)
3688 /* We need a PT_GNU_STACK segment. */
3689 ++segs;
3692 for (s = abfd->sections; s != NULL; s = s->next)
3694 if ((s->flags & SEC_LOAD) != 0
3695 && CONST_STRNEQ (s->name, ".note"))
3697 /* We need a PT_NOTE segment. */
3698 ++segs;
3702 for (s = abfd->sections; s != NULL; s = s->next)
3704 if (s->flags & SEC_THREAD_LOCAL)
3706 /* We need a PT_TLS segment. */
3707 ++segs;
3708 break;
3712 /* Let the backend count up any program headers it might need. */
3713 bed = get_elf_backend_data (abfd);
3714 if (bed->elf_backend_additional_program_headers)
3716 int a;
3718 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
3719 if (a == -1)
3720 abort ();
3721 segs += a;
3724 return segs * bed->s->sizeof_phdr;
3727 /* Create a mapping from a set of sections to a program segment. */
3729 static struct elf_segment_map *
3730 make_mapping (bfd *abfd,
3731 asection **sections,
3732 unsigned int from,
3733 unsigned int to,
3734 bfd_boolean phdr)
3736 struct elf_segment_map *m;
3737 unsigned int i;
3738 asection **hdrpp;
3739 bfd_size_type amt;
3741 amt = sizeof (struct elf_segment_map);
3742 amt += (to - from - 1) * sizeof (asection *);
3743 m = bfd_zalloc (abfd, amt);
3744 if (m == NULL)
3745 return NULL;
3746 m->next = NULL;
3747 m->p_type = PT_LOAD;
3748 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3749 m->sections[i - from] = *hdrpp;
3750 m->count = to - from;
3752 if (from == 0 && phdr)
3754 /* Include the headers in the first PT_LOAD segment. */
3755 m->includes_filehdr = 1;
3756 m->includes_phdrs = 1;
3759 return m;
3762 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3763 on failure. */
3765 struct elf_segment_map *
3766 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3768 struct elf_segment_map *m;
3770 m = bfd_zalloc (abfd, sizeof (struct elf_segment_map));
3771 if (m == NULL)
3772 return NULL;
3773 m->next = NULL;
3774 m->p_type = PT_DYNAMIC;
3775 m->count = 1;
3776 m->sections[0] = dynsec;
3778 return m;
3781 /* Possibly add or remove segments from the segment map. */
3783 static bfd_boolean
3784 elf_modify_segment_map (bfd *abfd, struct bfd_link_info *info)
3786 struct elf_segment_map **m;
3787 const struct elf_backend_data *bed;
3789 /* The placement algorithm assumes that non allocated sections are
3790 not in PT_LOAD segments. We ensure this here by removing such
3791 sections from the segment map. We also remove excluded
3792 sections. Finally, any PT_LOAD segment without sections is
3793 removed. */
3794 m = &elf_tdata (abfd)->segment_map;
3795 while (*m)
3797 unsigned int i, new_count;
3799 for (new_count = 0, i = 0; i < (*m)->count; i++)
3801 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
3802 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
3803 || (*m)->p_type != PT_LOAD))
3805 (*m)->sections[new_count] = (*m)->sections[i];
3806 new_count++;
3809 (*m)->count = new_count;
3811 if ((*m)->p_type == PT_LOAD && (*m)->count == 0)
3812 *m = (*m)->next;
3813 else
3814 m = &(*m)->next;
3817 bed = get_elf_backend_data (abfd);
3818 if (bed->elf_backend_modify_segment_map != NULL)
3820 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
3821 return FALSE;
3824 return TRUE;
3827 /* Set up a mapping from BFD sections to program segments. */
3829 bfd_boolean
3830 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
3832 unsigned int count;
3833 struct elf_segment_map *m;
3834 asection **sections = NULL;
3835 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3837 if (elf_tdata (abfd)->segment_map == NULL
3838 && bfd_count_sections (abfd) != 0)
3840 asection *s;
3841 unsigned int i;
3842 struct elf_segment_map *mfirst;
3843 struct elf_segment_map **pm;
3844 asection *last_hdr;
3845 bfd_vma last_size;
3846 unsigned int phdr_index;
3847 bfd_vma maxpagesize;
3848 asection **hdrpp;
3849 bfd_boolean phdr_in_segment = TRUE;
3850 bfd_boolean writable;
3851 int tls_count = 0;
3852 asection *first_tls = NULL;
3853 asection *dynsec, *eh_frame_hdr;
3854 bfd_size_type amt;
3856 /* Select the allocated sections, and sort them. */
3858 sections = bfd_malloc2 (bfd_count_sections (abfd), sizeof (asection *));
3859 if (sections == NULL)
3860 goto error_return;
3862 i = 0;
3863 for (s = abfd->sections; s != NULL; s = s->next)
3865 if ((s->flags & SEC_ALLOC) != 0)
3867 sections[i] = s;
3868 ++i;
3871 BFD_ASSERT (i <= bfd_count_sections (abfd));
3872 count = i;
3874 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3876 /* Build the mapping. */
3878 mfirst = NULL;
3879 pm = &mfirst;
3881 /* If we have a .interp section, then create a PT_PHDR segment for
3882 the program headers and a PT_INTERP segment for the .interp
3883 section. */
3884 s = bfd_get_section_by_name (abfd, ".interp");
3885 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3887 amt = sizeof (struct elf_segment_map);
3888 m = bfd_zalloc (abfd, amt);
3889 if (m == NULL)
3890 goto error_return;
3891 m->next = NULL;
3892 m->p_type = PT_PHDR;
3893 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3894 m->p_flags = PF_R | PF_X;
3895 m->p_flags_valid = 1;
3896 m->includes_phdrs = 1;
3898 *pm = m;
3899 pm = &m->next;
3901 amt = sizeof (struct elf_segment_map);
3902 m = bfd_zalloc (abfd, amt);
3903 if (m == NULL)
3904 goto error_return;
3905 m->next = NULL;
3906 m->p_type = PT_INTERP;
3907 m->count = 1;
3908 m->sections[0] = s;
3910 *pm = m;
3911 pm = &m->next;
3914 /* Look through the sections. We put sections in the same program
3915 segment when the start of the second section can be placed within
3916 a few bytes of the end of the first section. */
3917 last_hdr = NULL;
3918 last_size = 0;
3919 phdr_index = 0;
3920 maxpagesize = bed->maxpagesize;
3921 writable = FALSE;
3922 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3923 if (dynsec != NULL
3924 && (dynsec->flags & SEC_LOAD) == 0)
3925 dynsec = NULL;
3927 /* Deal with -Ttext or something similar such that the first section
3928 is not adjacent to the program headers. This is an
3929 approximation, since at this point we don't know exactly how many
3930 program headers we will need. */
3931 if (count > 0)
3933 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
3935 if (phdr_size == (bfd_size_type) -1)
3936 phdr_size = get_program_header_size (abfd, info);
3937 if ((abfd->flags & D_PAGED) == 0
3938 || sections[0]->lma < phdr_size
3939 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3940 phdr_in_segment = FALSE;
3943 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3945 asection *hdr;
3946 bfd_boolean new_segment;
3948 hdr = *hdrpp;
3950 /* See if this section and the last one will fit in the same
3951 segment. */
3953 if (last_hdr == NULL)
3955 /* If we don't have a segment yet, then we don't need a new
3956 one (we build the last one after this loop). */
3957 new_segment = FALSE;
3959 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3961 /* If this section has a different relation between the
3962 virtual address and the load address, then we need a new
3963 segment. */
3964 new_segment = TRUE;
3966 else if (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
3967 < BFD_ALIGN (hdr->lma, maxpagesize))
3969 /* If putting this section in this segment would force us to
3970 skip a page in the segment, then we need a new segment. */
3971 new_segment = TRUE;
3973 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3974 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
3976 /* We don't want to put a loadable section after a
3977 nonloadable section in the same segment.
3978 Consider .tbss sections as loadable for this purpose. */
3979 new_segment = TRUE;
3981 else if ((abfd->flags & D_PAGED) == 0)
3983 /* If the file is not demand paged, which means that we
3984 don't require the sections to be correctly aligned in the
3985 file, then there is no other reason for a new segment. */
3986 new_segment = FALSE;
3988 else if (! writable
3989 && (hdr->flags & SEC_READONLY) == 0
3990 && (((last_hdr->lma + last_size - 1)
3991 & ~(maxpagesize - 1))
3992 != (hdr->lma & ~(maxpagesize - 1))))
3994 /* We don't want to put a writable section in a read only
3995 segment, unless they are on the same page in memory
3996 anyhow. We already know that the last section does not
3997 bring us past the current section on the page, so the
3998 only case in which the new section is not on the same
3999 page as the previous section is when the previous section
4000 ends precisely on a page boundary. */
4001 new_segment = TRUE;
4003 else
4005 /* Otherwise, we can use the same segment. */
4006 new_segment = FALSE;
4009 /* Allow interested parties a chance to override our decision. */
4010 if (last_hdr && info->callbacks->override_segment_assignment)
4011 new_segment = info->callbacks->override_segment_assignment (info, abfd, hdr, last_hdr, new_segment);
4013 if (! new_segment)
4015 if ((hdr->flags & SEC_READONLY) == 0)
4016 writable = TRUE;
4017 last_hdr = hdr;
4018 /* .tbss sections effectively have zero size. */
4019 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
4020 != SEC_THREAD_LOCAL)
4021 last_size = hdr->size;
4022 else
4023 last_size = 0;
4024 continue;
4027 /* We need a new program segment. We must create a new program
4028 header holding all the sections from phdr_index until hdr. */
4030 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
4031 if (m == NULL)
4032 goto error_return;
4034 *pm = m;
4035 pm = &m->next;
4037 if ((hdr->flags & SEC_READONLY) == 0)
4038 writable = TRUE;
4039 else
4040 writable = FALSE;
4042 last_hdr = hdr;
4043 /* .tbss sections effectively have zero size. */
4044 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
4045 last_size = hdr->size;
4046 else
4047 last_size = 0;
4048 phdr_index = i;
4049 phdr_in_segment = FALSE;
4052 /* Create a final PT_LOAD program segment. */
4053 if (last_hdr != NULL)
4055 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
4056 if (m == NULL)
4057 goto error_return;
4059 *pm = m;
4060 pm = &m->next;
4063 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4064 if (dynsec != NULL)
4066 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4067 if (m == NULL)
4068 goto error_return;
4069 *pm = m;
4070 pm = &m->next;
4073 /* For each loadable .note section, add a PT_NOTE segment. We don't
4074 use bfd_get_section_by_name, because if we link together
4075 nonloadable .note sections and loadable .note sections, we will
4076 generate two .note sections in the output file. FIXME: Using
4077 names for section types is bogus anyhow. */
4078 for (s = abfd->sections; s != NULL; s = s->next)
4080 if ((s->flags & SEC_LOAD) != 0
4081 && CONST_STRNEQ (s->name, ".note"))
4083 amt = sizeof (struct elf_segment_map);
4084 m = bfd_zalloc (abfd, amt);
4085 if (m == NULL)
4086 goto error_return;
4087 m->next = NULL;
4088 m->p_type = PT_NOTE;
4089 m->count = 1;
4090 m->sections[0] = s;
4092 *pm = m;
4093 pm = &m->next;
4095 if (s->flags & SEC_THREAD_LOCAL)
4097 if (! tls_count)
4098 first_tls = s;
4099 tls_count++;
4103 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4104 if (tls_count > 0)
4106 int i;
4108 amt = sizeof (struct elf_segment_map);
4109 amt += (tls_count - 1) * sizeof (asection *);
4110 m = bfd_zalloc (abfd, amt);
4111 if (m == NULL)
4112 goto error_return;
4113 m->next = NULL;
4114 m->p_type = PT_TLS;
4115 m->count = tls_count;
4116 /* Mandated PF_R. */
4117 m->p_flags = PF_R;
4118 m->p_flags_valid = 1;
4119 for (i = 0; i < tls_count; ++i)
4121 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
4122 m->sections[i] = first_tls;
4123 first_tls = first_tls->next;
4126 *pm = m;
4127 pm = &m->next;
4130 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4131 segment. */
4132 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
4133 if (eh_frame_hdr != NULL
4134 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
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_EH_FRAME;
4142 m->count = 1;
4143 m->sections[0] = eh_frame_hdr->output_section;
4145 *pm = m;
4146 pm = &m->next;
4149 if (elf_tdata (abfd)->stack_flags)
4151 amt = sizeof (struct elf_segment_map);
4152 m = bfd_zalloc (abfd, amt);
4153 if (m == NULL)
4154 goto error_return;
4155 m->next = NULL;
4156 m->p_type = PT_GNU_STACK;
4157 m->p_flags = elf_tdata (abfd)->stack_flags;
4158 m->p_flags_valid = 1;
4160 *pm = m;
4161 pm = &m->next;
4164 if (dynsec != NULL && elf_tdata (abfd)->relro)
4166 /* We make a PT_GNU_RELRO segment only when there is a
4167 PT_DYNAMIC segment. */
4168 amt = sizeof (struct elf_segment_map);
4169 m = bfd_zalloc (abfd, amt);
4170 if (m == NULL)
4171 goto error_return;
4172 m->next = NULL;
4173 m->p_type = PT_GNU_RELRO;
4174 m->p_flags = PF_R;
4175 m->p_flags_valid = 1;
4177 *pm = m;
4178 pm = &m->next;
4181 free (sections);
4182 elf_tdata (abfd)->segment_map = mfirst;
4185 if (!elf_modify_segment_map (abfd, info))
4186 return FALSE;
4188 for (count = 0, m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4189 ++count;
4190 elf_tdata (abfd)->program_header_size = count * bed->s->sizeof_phdr;
4192 return TRUE;
4194 error_return:
4195 if (sections != NULL)
4196 free (sections);
4197 return FALSE;
4200 /* Sort sections by address. */
4202 static int
4203 elf_sort_sections (const void *arg1, const void *arg2)
4205 const asection *sec1 = *(const asection **) arg1;
4206 const asection *sec2 = *(const asection **) arg2;
4207 bfd_size_type size1, size2;
4209 /* Sort by LMA first, since this is the address used to
4210 place the section into a segment. */
4211 if (sec1->lma < sec2->lma)
4212 return -1;
4213 else if (sec1->lma > sec2->lma)
4214 return 1;
4216 /* Then sort by VMA. Normally the LMA and the VMA will be
4217 the same, and this will do nothing. */
4218 if (sec1->vma < sec2->vma)
4219 return -1;
4220 else if (sec1->vma > sec2->vma)
4221 return 1;
4223 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4225 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4227 if (TOEND (sec1))
4229 if (TOEND (sec2))
4231 /* If the indicies are the same, do not return 0
4232 here, but continue to try the next comparison. */
4233 if (sec1->target_index - sec2->target_index != 0)
4234 return sec1->target_index - sec2->target_index;
4236 else
4237 return 1;
4239 else if (TOEND (sec2))
4240 return -1;
4242 #undef TOEND
4244 /* Sort by size, to put zero sized sections
4245 before others at the same address. */
4247 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
4248 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
4250 if (size1 < size2)
4251 return -1;
4252 if (size1 > size2)
4253 return 1;
4255 return sec1->target_index - sec2->target_index;
4258 /* Ian Lance Taylor writes:
4260 We shouldn't be using % with a negative signed number. That's just
4261 not good. We have to make sure either that the number is not
4262 negative, or that the number has an unsigned type. When the types
4263 are all the same size they wind up as unsigned. When file_ptr is a
4264 larger signed type, the arithmetic winds up as signed long long,
4265 which is wrong.
4267 What we're trying to say here is something like ``increase OFF by
4268 the least amount that will cause it to be equal to the VMA modulo
4269 the page size.'' */
4270 /* In other words, something like:
4272 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4273 off_offset = off % bed->maxpagesize;
4274 if (vma_offset < off_offset)
4275 adjustment = vma_offset + bed->maxpagesize - off_offset;
4276 else
4277 adjustment = vma_offset - off_offset;
4279 which can can be collapsed into the expression below. */
4281 static file_ptr
4282 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
4284 return ((vma - off) % maxpagesize);
4287 /* Assign file positions to the sections based on the mapping from
4288 sections to segments. This function also sets up some fields in
4289 the file header. */
4291 static bfd_boolean
4292 assign_file_positions_for_load_sections (bfd *abfd,
4293 struct bfd_link_info *link_info)
4295 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4296 struct elf_segment_map *m;
4297 Elf_Internal_Phdr *phdrs;
4298 Elf_Internal_Phdr *p;
4299 file_ptr off;
4300 bfd_size_type maxpagesize;
4301 unsigned int alloc;
4302 unsigned int i, j;
4304 if (link_info == NULL
4305 && !elf_modify_segment_map (abfd, link_info))
4306 return FALSE;
4308 alloc = 0;
4309 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4310 ++alloc;
4312 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
4313 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
4314 elf_elfheader (abfd)->e_phnum = alloc;
4316 if (elf_tdata (abfd)->program_header_size == (bfd_size_type) -1)
4317 elf_tdata (abfd)->program_header_size = alloc * bed->s->sizeof_phdr;
4318 else
4319 BFD_ASSERT (elf_tdata (abfd)->program_header_size
4320 >= alloc * bed->s->sizeof_phdr);
4322 if (alloc == 0)
4324 elf_tdata (abfd)->next_file_pos = bed->s->sizeof_ehdr;
4325 return TRUE;
4328 phdrs = bfd_alloc2 (abfd, alloc, sizeof (Elf_Internal_Phdr));
4329 elf_tdata (abfd)->phdr = phdrs;
4330 if (phdrs == NULL)
4331 return FALSE;
4333 maxpagesize = 1;
4334 if ((abfd->flags & D_PAGED) != 0)
4335 maxpagesize = bed->maxpagesize;
4337 off = bed->s->sizeof_ehdr;
4338 off += alloc * bed->s->sizeof_phdr;
4340 for (m = elf_tdata (abfd)->segment_map, p = phdrs, j = 0;
4341 m != NULL;
4342 m = m->next, p++, j++)
4344 asection **secpp;
4345 bfd_vma off_adjust;
4346 bfd_boolean no_contents;
4348 /* If elf_segment_map is not from map_sections_to_segments, the
4349 sections may not be correctly ordered. NOTE: sorting should
4350 not be done to the PT_NOTE section of a corefile, which may
4351 contain several pseudo-sections artificially created by bfd.
4352 Sorting these pseudo-sections breaks things badly. */
4353 if (m->count > 1
4354 && !(elf_elfheader (abfd)->e_type == ET_CORE
4355 && m->p_type == PT_NOTE))
4356 qsort (m->sections, (size_t) m->count, sizeof (asection *),
4357 elf_sort_sections);
4359 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4360 number of sections with contents contributing to both p_filesz
4361 and p_memsz, followed by a number of sections with no contents
4362 that just contribute to p_memsz. In this loop, OFF tracks next
4363 available file offset for PT_LOAD and PT_NOTE segments. */
4364 p->p_type = m->p_type;
4365 p->p_flags = m->p_flags;
4367 if (m->count == 0)
4368 p->p_vaddr = 0;
4369 else
4370 p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
4372 if (m->p_paddr_valid)
4373 p->p_paddr = m->p_paddr;
4374 else if (m->count == 0)
4375 p->p_paddr = 0;
4376 else
4377 p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
4379 if (p->p_type == PT_LOAD
4380 && (abfd->flags & D_PAGED) != 0)
4382 /* p_align in demand paged PT_LOAD segments effectively stores
4383 the maximum page size. When copying an executable with
4384 objcopy, we set m->p_align from the input file. Use this
4385 value for maxpagesize rather than bed->maxpagesize, which
4386 may be different. Note that we use maxpagesize for PT_TLS
4387 segment alignment later in this function, so we are relying
4388 on at least one PT_LOAD segment appearing before a PT_TLS
4389 segment. */
4390 if (m->p_align_valid)
4391 maxpagesize = m->p_align;
4393 p->p_align = maxpagesize;
4395 else if (m->count == 0)
4396 p->p_align = 1 << bed->s->log_file_align;
4397 else if (m->p_align_valid)
4398 p->p_align = m->p_align;
4399 else
4400 p->p_align = 0;
4402 no_contents = FALSE;
4403 off_adjust = 0;
4404 if (p->p_type == PT_LOAD
4405 && m->count > 0)
4407 bfd_size_type align;
4408 unsigned int align_power = 0;
4410 if (m->p_align_valid)
4411 align = p->p_align;
4412 else
4414 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4416 unsigned int secalign;
4418 secalign = bfd_get_section_alignment (abfd, *secpp);
4419 if (secalign > align_power)
4420 align_power = secalign;
4422 align = (bfd_size_type) 1 << align_power;
4423 if (align < maxpagesize)
4424 align = maxpagesize;
4427 for (i = 0; i < m->count; i++)
4428 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
4429 /* If we aren't making room for this section, then
4430 it must be SHT_NOBITS regardless of what we've
4431 set via struct bfd_elf_special_section. */
4432 elf_section_type (m->sections[i]) = SHT_NOBITS;
4434 /* Find out whether this segment contains any loadable
4435 sections. If the first section isn't loadable, the same
4436 holds for any other sections. */
4437 i = 0;
4438 while (elf_section_type (m->sections[i]) == SHT_NOBITS)
4440 /* If a segment starts with .tbss, we need to look
4441 at the next section to decide whether the segment
4442 has any loadable sections. */
4443 if ((elf_section_flags (m->sections[i]) & SHF_TLS) == 0
4444 || ++i >= m->count)
4446 no_contents = TRUE;
4447 break;
4451 off_adjust = vma_page_aligned_bias (m->sections[0]->vma, off, align);
4452 off += off_adjust;
4453 if (no_contents)
4455 /* We shouldn't need to align the segment on disk since
4456 the segment doesn't need file space, but the gABI
4457 arguably requires the alignment and glibc ld.so
4458 checks it. So to comply with the alignment
4459 requirement but not waste file space, we adjust
4460 p_offset for just this segment. (OFF_ADJUST is
4461 subtracted from OFF later.) This may put p_offset
4462 past the end of file, but that shouldn't matter. */
4464 else
4465 off_adjust = 0;
4467 /* Make sure the .dynamic section is the first section in the
4468 PT_DYNAMIC segment. */
4469 else if (p->p_type == PT_DYNAMIC
4470 && m->count > 1
4471 && strcmp (m->sections[0]->name, ".dynamic") != 0)
4473 _bfd_error_handler
4474 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4475 abfd);
4476 bfd_set_error (bfd_error_bad_value);
4477 return FALSE;
4480 p->p_offset = 0;
4481 p->p_filesz = 0;
4482 p->p_memsz = 0;
4484 if (m->includes_filehdr)
4486 if (!m->p_flags_valid)
4487 p->p_flags |= PF_R;
4488 p->p_filesz = bed->s->sizeof_ehdr;
4489 p->p_memsz = bed->s->sizeof_ehdr;
4490 if (m->count > 0)
4492 BFD_ASSERT (p->p_type == PT_LOAD);
4494 if (p->p_vaddr < (bfd_vma) off)
4496 (*_bfd_error_handler)
4497 (_("%B: Not enough room for program headers, try linking with -N"),
4498 abfd);
4499 bfd_set_error (bfd_error_bad_value);
4500 return FALSE;
4503 p->p_vaddr -= off;
4504 if (!m->p_paddr_valid)
4505 p->p_paddr -= off;
4509 if (m->includes_phdrs)
4511 if (!m->p_flags_valid)
4512 p->p_flags |= PF_R;
4514 if (!m->includes_filehdr)
4516 p->p_offset = bed->s->sizeof_ehdr;
4518 if (m->count > 0)
4520 BFD_ASSERT (p->p_type == PT_LOAD);
4521 p->p_vaddr -= off - p->p_offset;
4522 if (!m->p_paddr_valid)
4523 p->p_paddr -= off - p->p_offset;
4527 p->p_filesz += alloc * bed->s->sizeof_phdr;
4528 p->p_memsz += alloc * bed->s->sizeof_phdr;
4531 if (p->p_type == PT_LOAD
4532 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4534 if (!m->includes_filehdr && !m->includes_phdrs)
4535 p->p_offset = off;
4536 else
4538 file_ptr adjust;
4540 adjust = off - (p->p_offset + p->p_filesz);
4541 if (!no_contents)
4542 p->p_filesz += adjust;
4543 p->p_memsz += adjust;
4547 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4548 maps. Set filepos for sections in PT_LOAD segments, and in
4549 core files, for sections in PT_NOTE segments.
4550 assign_file_positions_for_non_load_sections will set filepos
4551 for other sections and update p_filesz for other segments. */
4552 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4554 asection *sec;
4555 bfd_size_type align;
4556 Elf_Internal_Shdr *this_hdr;
4558 sec = *secpp;
4559 this_hdr = &elf_section_data (sec)->this_hdr;
4560 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
4562 if (p->p_type == PT_LOAD
4563 || p->p_type == PT_TLS)
4565 bfd_signed_vma adjust = sec->lma - (p->p_paddr + p->p_memsz);
4567 if (this_hdr->sh_type != SHT_NOBITS
4568 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
4569 && ((this_hdr->sh_flags & SHF_TLS) == 0
4570 || p->p_type == PT_TLS)))
4572 if (adjust < 0)
4574 (*_bfd_error_handler)
4575 (_("%B: section %A lma 0x%lx overlaps previous sections"),
4576 abfd, sec, (unsigned long) sec->lma);
4577 adjust = 0;
4579 p->p_memsz += adjust;
4581 if (this_hdr->sh_type != SHT_NOBITS)
4583 off += adjust;
4584 p->p_filesz += adjust;
4589 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4591 /* The section at i == 0 is the one that actually contains
4592 everything. */
4593 if (i == 0)
4595 this_hdr->sh_offset = sec->filepos = off;
4596 off += this_hdr->sh_size;
4597 p->p_filesz = this_hdr->sh_size;
4598 p->p_memsz = 0;
4599 p->p_align = 1;
4601 else
4603 /* The rest are fake sections that shouldn't be written. */
4604 sec->filepos = 0;
4605 sec->size = 0;
4606 sec->flags = 0;
4607 continue;
4610 else
4612 if (p->p_type == PT_LOAD)
4614 this_hdr->sh_offset = sec->filepos = off;
4615 if (this_hdr->sh_type != SHT_NOBITS)
4616 off += this_hdr->sh_size;
4619 if (this_hdr->sh_type != SHT_NOBITS)
4621 p->p_filesz += this_hdr->sh_size;
4622 /* A load section without SHF_ALLOC is something like
4623 a note section in a PT_NOTE segment. These take
4624 file space but are not loaded into memory. */
4625 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4626 p->p_memsz += this_hdr->sh_size;
4628 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4630 if (p->p_type == PT_TLS)
4631 p->p_memsz += this_hdr->sh_size;
4633 /* .tbss is special. It doesn't contribute to p_memsz of
4634 normal segments. */
4635 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
4636 p->p_memsz += this_hdr->sh_size;
4639 if (p->p_type == PT_GNU_RELRO)
4640 p->p_align = 1;
4641 else if (align > p->p_align
4642 && !m->p_align_valid
4643 && (p->p_type != PT_LOAD
4644 || (abfd->flags & D_PAGED) == 0))
4645 p->p_align = align;
4648 if (!m->p_flags_valid)
4650 p->p_flags |= PF_R;
4651 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
4652 p->p_flags |= PF_X;
4653 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
4654 p->p_flags |= PF_W;
4657 off -= off_adjust;
4659 /* Check that all sections are in a PT_LOAD segment.
4660 Don't check funky gdb generated core files. */
4661 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
4662 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4664 Elf_Internal_Shdr *this_hdr;
4665 asection *sec;
4667 sec = *secpp;
4668 this_hdr = &(elf_section_data(sec)->this_hdr);
4669 if (this_hdr->sh_size != 0
4670 && !ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, p))
4672 (*_bfd_error_handler)
4673 (_("%B: section `%A' can't be allocated in segment %d"),
4674 abfd, sec, j);
4675 bfd_set_error (bfd_error_bad_value);
4676 return FALSE;
4681 elf_tdata (abfd)->next_file_pos = off;
4682 return TRUE;
4685 /* Assign file positions for the other sections. */
4687 static bfd_boolean
4688 assign_file_positions_for_non_load_sections (bfd *abfd,
4689 struct bfd_link_info *link_info)
4691 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4692 Elf_Internal_Shdr **i_shdrpp;
4693 Elf_Internal_Shdr **hdrpp;
4694 Elf_Internal_Phdr *phdrs;
4695 Elf_Internal_Phdr *p;
4696 struct elf_segment_map *m;
4697 bfd_vma filehdr_vaddr, filehdr_paddr;
4698 bfd_vma phdrs_vaddr, phdrs_paddr;
4699 file_ptr off;
4700 unsigned int num_sec;
4701 unsigned int i;
4702 unsigned int count;
4704 i_shdrpp = elf_elfsections (abfd);
4705 num_sec = elf_numsections (abfd);
4706 off = elf_tdata (abfd)->next_file_pos;
4707 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4709 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4710 Elf_Internal_Shdr *hdr;
4712 hdr = *hdrpp;
4713 if (hdr->bfd_section != NULL
4714 && (hdr->bfd_section->filepos != 0
4715 || (hdr->sh_type == SHT_NOBITS
4716 && hdr->contents == NULL)))
4717 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
4718 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4720 if (hdr->sh_size != 0)
4721 ((*_bfd_error_handler)
4722 (_("%B: warning: allocated section `%s' not in segment"),
4723 abfd,
4724 (hdr->bfd_section == NULL
4725 ? "*unknown*"
4726 : hdr->bfd_section->name)));
4727 /* We don't need to page align empty sections. */
4728 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
4729 off += vma_page_aligned_bias (hdr->sh_addr, off,
4730 bed->maxpagesize);
4731 else
4732 off += vma_page_aligned_bias (hdr->sh_addr, off,
4733 hdr->sh_addralign);
4734 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4735 FALSE);
4737 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4738 && hdr->bfd_section == NULL)
4739 || hdr == i_shdrpp[tdata->symtab_section]
4740 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4741 || hdr == i_shdrpp[tdata->strtab_section])
4742 hdr->sh_offset = -1;
4743 else
4744 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4746 if (i == SHN_LORESERVE - 1)
4748 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4749 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4753 /* Now that we have set the section file positions, we can set up
4754 the file positions for the non PT_LOAD segments. */
4755 count = 0;
4756 filehdr_vaddr = 0;
4757 filehdr_paddr = 0;
4758 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
4759 phdrs_paddr = 0;
4760 phdrs = elf_tdata (abfd)->phdr;
4761 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4762 m != NULL;
4763 m = m->next, p++)
4765 ++count;
4766 if (p->p_type != PT_LOAD)
4767 continue;
4769 if (m->includes_filehdr)
4771 filehdr_vaddr = p->p_vaddr;
4772 filehdr_paddr = p->p_paddr;
4774 if (m->includes_phdrs)
4776 phdrs_vaddr = p->p_vaddr;
4777 phdrs_paddr = p->p_paddr;
4778 if (m->includes_filehdr)
4780 phdrs_vaddr += bed->s->sizeof_ehdr;
4781 phdrs_paddr += bed->s->sizeof_ehdr;
4786 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4787 m != NULL;
4788 m = m->next, p++)
4790 if (m->count != 0)
4792 if (p->p_type != PT_LOAD
4793 && (p->p_type != PT_NOTE || bfd_get_format (abfd) != bfd_core))
4795 Elf_Internal_Shdr *hdr;
4796 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
4798 hdr = &elf_section_data (m->sections[m->count - 1])->this_hdr;
4799 p->p_filesz = (m->sections[m->count - 1]->filepos
4800 - m->sections[0]->filepos);
4801 if (hdr->sh_type != SHT_NOBITS)
4802 p->p_filesz += hdr->sh_size;
4804 p->p_offset = m->sections[0]->filepos;
4807 else
4809 if (m->includes_filehdr)
4811 p->p_vaddr = filehdr_vaddr;
4812 if (! m->p_paddr_valid)
4813 p->p_paddr = filehdr_paddr;
4815 else if (m->includes_phdrs)
4817 p->p_vaddr = phdrs_vaddr;
4818 if (! m->p_paddr_valid)
4819 p->p_paddr = phdrs_paddr;
4821 else if (p->p_type == PT_GNU_RELRO)
4823 Elf_Internal_Phdr *lp;
4825 for (lp = phdrs; lp < phdrs + count; ++lp)
4827 if (lp->p_type == PT_LOAD
4828 && lp->p_vaddr <= link_info->relro_end
4829 && lp->p_vaddr >= link_info->relro_start
4830 && (lp->p_vaddr + lp->p_filesz
4831 >= link_info->relro_end))
4832 break;
4835 if (lp < phdrs + count
4836 && link_info->relro_end > lp->p_vaddr)
4838 p->p_vaddr = lp->p_vaddr;
4839 p->p_paddr = lp->p_paddr;
4840 p->p_offset = lp->p_offset;
4841 p->p_filesz = link_info->relro_end - lp->p_vaddr;
4842 p->p_memsz = p->p_filesz;
4843 p->p_align = 1;
4844 p->p_flags = (lp->p_flags & ~PF_W);
4846 else
4848 memset (p, 0, sizeof *p);
4849 p->p_type = PT_NULL;
4855 elf_tdata (abfd)->next_file_pos = off;
4857 return TRUE;
4860 /* Work out the file positions of all the sections. This is called by
4861 _bfd_elf_compute_section_file_positions. All the section sizes and
4862 VMAs must be known before this is called.
4864 Reloc sections come in two flavours: Those processed specially as
4865 "side-channel" data attached to a section to which they apply, and
4866 those that bfd doesn't process as relocations. The latter sort are
4867 stored in a normal bfd section by bfd_section_from_shdr. We don't
4868 consider the former sort here, unless they form part of the loadable
4869 image. Reloc sections not assigned here will be handled later by
4870 assign_file_positions_for_relocs.
4872 We also don't set the positions of the .symtab and .strtab here. */
4874 static bfd_boolean
4875 assign_file_positions_except_relocs (bfd *abfd,
4876 struct bfd_link_info *link_info)
4878 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4879 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4880 file_ptr off;
4881 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4883 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4884 && bfd_get_format (abfd) != bfd_core)
4886 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4887 unsigned int num_sec = elf_numsections (abfd);
4888 Elf_Internal_Shdr **hdrpp;
4889 unsigned int i;
4891 /* Start after the ELF header. */
4892 off = i_ehdrp->e_ehsize;
4894 /* We are not creating an executable, which means that we are
4895 not creating a program header, and that the actual order of
4896 the sections in the file is unimportant. */
4897 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4899 Elf_Internal_Shdr *hdr;
4901 hdr = *hdrpp;
4902 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4903 && hdr->bfd_section == NULL)
4904 || i == tdata->symtab_section
4905 || i == tdata->symtab_shndx_section
4906 || i == tdata->strtab_section)
4908 hdr->sh_offset = -1;
4910 else
4911 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4913 if (i == SHN_LORESERVE - 1)
4915 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4916 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4920 else
4922 unsigned int alloc;
4924 /* Assign file positions for the loaded sections based on the
4925 assignment of sections to segments. */
4926 if (!assign_file_positions_for_load_sections (abfd, link_info))
4927 return FALSE;
4929 /* And for non-load sections. */
4930 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
4931 return FALSE;
4933 if (bed->elf_backend_modify_program_headers != NULL)
4935 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
4936 return FALSE;
4939 /* Write out the program headers. */
4940 alloc = tdata->program_header_size / bed->s->sizeof_phdr;
4941 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4942 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
4943 return FALSE;
4945 off = tdata->next_file_pos;
4948 /* Place the section headers. */
4949 off = align_file_position (off, 1 << bed->s->log_file_align);
4950 i_ehdrp->e_shoff = off;
4951 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4953 tdata->next_file_pos = off;
4955 return TRUE;
4958 static bfd_boolean
4959 prep_headers (bfd *abfd)
4961 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
4962 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4963 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
4964 struct elf_strtab_hash *shstrtab;
4965 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4967 i_ehdrp = elf_elfheader (abfd);
4968 i_shdrp = elf_elfsections (abfd);
4970 shstrtab = _bfd_elf_strtab_init ();
4971 if (shstrtab == NULL)
4972 return FALSE;
4974 elf_shstrtab (abfd) = shstrtab;
4976 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4977 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4978 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4979 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4981 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4982 i_ehdrp->e_ident[EI_DATA] =
4983 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4984 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4986 if ((abfd->flags & DYNAMIC) != 0)
4987 i_ehdrp->e_type = ET_DYN;
4988 else if ((abfd->flags & EXEC_P) != 0)
4989 i_ehdrp->e_type = ET_EXEC;
4990 else if (bfd_get_format (abfd) == bfd_core)
4991 i_ehdrp->e_type = ET_CORE;
4992 else
4993 i_ehdrp->e_type = ET_REL;
4995 switch (bfd_get_arch (abfd))
4997 case bfd_arch_unknown:
4998 i_ehdrp->e_machine = EM_NONE;
4999 break;
5001 /* There used to be a long list of cases here, each one setting
5002 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
5003 in the corresponding bfd definition. To avoid duplication,
5004 the switch was removed. Machines that need special handling
5005 can generally do it in elf_backend_final_write_processing(),
5006 unless they need the information earlier than the final write.
5007 Such need can generally be supplied by replacing the tests for
5008 e_machine with the conditions used to determine it. */
5009 default:
5010 i_ehdrp->e_machine = bed->elf_machine_code;
5013 i_ehdrp->e_version = bed->s->ev_current;
5014 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
5016 /* No program header, for now. */
5017 i_ehdrp->e_phoff = 0;
5018 i_ehdrp->e_phentsize = 0;
5019 i_ehdrp->e_phnum = 0;
5021 /* Each bfd section is section header entry. */
5022 i_ehdrp->e_entry = bfd_get_start_address (abfd);
5023 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
5025 /* If we're building an executable, we'll need a program header table. */
5026 if (abfd->flags & EXEC_P)
5027 /* It all happens later. */
5029 else
5031 i_ehdrp->e_phentsize = 0;
5032 i_phdrp = 0;
5033 i_ehdrp->e_phoff = 0;
5036 elf_tdata (abfd)->symtab_hdr.sh_name =
5037 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
5038 elf_tdata (abfd)->strtab_hdr.sh_name =
5039 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
5040 elf_tdata (abfd)->shstrtab_hdr.sh_name =
5041 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
5042 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
5043 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
5044 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
5045 return FALSE;
5047 return TRUE;
5050 /* Assign file positions for all the reloc sections which are not part
5051 of the loadable file image. */
5053 void
5054 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
5056 file_ptr off;
5057 unsigned int i, num_sec;
5058 Elf_Internal_Shdr **shdrpp;
5060 off = elf_tdata (abfd)->next_file_pos;
5062 num_sec = elf_numsections (abfd);
5063 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
5065 Elf_Internal_Shdr *shdrp;
5067 shdrp = *shdrpp;
5068 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
5069 && shdrp->sh_offset == -1)
5070 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
5073 elf_tdata (abfd)->next_file_pos = off;
5076 bfd_boolean
5077 _bfd_elf_write_object_contents (bfd *abfd)
5079 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5080 Elf_Internal_Ehdr *i_ehdrp;
5081 Elf_Internal_Shdr **i_shdrp;
5082 bfd_boolean failed;
5083 unsigned int count, num_sec;
5085 if (! abfd->output_has_begun
5086 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
5087 return FALSE;
5089 i_shdrp = elf_elfsections (abfd);
5090 i_ehdrp = elf_elfheader (abfd);
5092 failed = FALSE;
5093 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
5094 if (failed)
5095 return FALSE;
5097 _bfd_elf_assign_file_positions_for_relocs (abfd);
5099 /* After writing the headers, we need to write the sections too... */
5100 num_sec = elf_numsections (abfd);
5101 for (count = 1; count < num_sec; count++)
5103 if (bed->elf_backend_section_processing)
5104 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
5105 if (i_shdrp[count]->contents)
5107 bfd_size_type amt = i_shdrp[count]->sh_size;
5109 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
5110 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
5111 return FALSE;
5113 if (count == SHN_LORESERVE - 1)
5114 count += SHN_HIRESERVE + 1 - SHN_LORESERVE;
5117 /* Write out the section header names. */
5118 if (elf_shstrtab (abfd) != NULL
5119 && (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
5120 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
5121 return FALSE;
5123 if (bed->elf_backend_final_write_processing)
5124 (*bed->elf_backend_final_write_processing) (abfd,
5125 elf_tdata (abfd)->linker);
5127 return bed->s->write_shdrs_and_ehdr (abfd);
5130 bfd_boolean
5131 _bfd_elf_write_corefile_contents (bfd *abfd)
5133 /* Hopefully this can be done just like an object file. */
5134 return _bfd_elf_write_object_contents (abfd);
5137 /* Given a section, search the header to find them. */
5140 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
5142 const struct elf_backend_data *bed;
5143 int index;
5145 if (elf_section_data (asect) != NULL
5146 && elf_section_data (asect)->this_idx != 0)
5147 return elf_section_data (asect)->this_idx;
5149 if (bfd_is_abs_section (asect))
5150 index = SHN_ABS;
5151 else if (bfd_is_com_section (asect))
5152 index = SHN_COMMON;
5153 else if (bfd_is_und_section (asect))
5154 index = SHN_UNDEF;
5155 else
5156 index = -1;
5158 bed = get_elf_backend_data (abfd);
5159 if (bed->elf_backend_section_from_bfd_section)
5161 int retval = index;
5163 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
5164 return retval;
5167 if (index == -1)
5168 bfd_set_error (bfd_error_nonrepresentable_section);
5170 return index;
5173 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5174 on error. */
5177 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
5179 asymbol *asym_ptr = *asym_ptr_ptr;
5180 int idx;
5181 flagword flags = asym_ptr->flags;
5183 /* When gas creates relocations against local labels, it creates its
5184 own symbol for the section, but does put the symbol into the
5185 symbol chain, so udata is 0. When the linker is generating
5186 relocatable output, this section symbol may be for one of the
5187 input sections rather than the output section. */
5188 if (asym_ptr->udata.i == 0
5189 && (flags & BSF_SECTION_SYM)
5190 && asym_ptr->section)
5192 asection *sec;
5193 int indx;
5195 sec = asym_ptr->section;
5196 if (sec->owner != abfd && sec->output_section != NULL)
5197 sec = sec->output_section;
5198 if (sec->owner == abfd
5199 && (indx = sec->index) < elf_num_section_syms (abfd)
5200 && elf_section_syms (abfd)[indx] != NULL)
5201 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
5204 idx = asym_ptr->udata.i;
5206 if (idx == 0)
5208 /* This case can occur when using --strip-symbol on a symbol
5209 which is used in a relocation entry. */
5210 (*_bfd_error_handler)
5211 (_("%B: symbol `%s' required but not present"),
5212 abfd, bfd_asymbol_name (asym_ptr));
5213 bfd_set_error (bfd_error_no_symbols);
5214 return -1;
5217 #if DEBUG & 4
5219 fprintf (stderr,
5220 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
5221 (long) asym_ptr, asym_ptr->name, idx, flags,
5222 elf_symbol_flags (flags));
5223 fflush (stderr);
5225 #endif
5227 return idx;
5230 /* Rewrite program header information. */
5232 static bfd_boolean
5233 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
5235 Elf_Internal_Ehdr *iehdr;
5236 struct elf_segment_map *map;
5237 struct elf_segment_map *map_first;
5238 struct elf_segment_map **pointer_to_map;
5239 Elf_Internal_Phdr *segment;
5240 asection *section;
5241 unsigned int i;
5242 unsigned int num_segments;
5243 bfd_boolean phdr_included = FALSE;
5244 bfd_vma maxpagesize;
5245 struct elf_segment_map *phdr_adjust_seg = NULL;
5246 unsigned int phdr_adjust_num = 0;
5247 const struct elf_backend_data *bed;
5249 bed = get_elf_backend_data (ibfd);
5250 iehdr = elf_elfheader (ibfd);
5252 map_first = NULL;
5253 pointer_to_map = &map_first;
5255 num_segments = elf_elfheader (ibfd)->e_phnum;
5256 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
5258 /* Returns the end address of the segment + 1. */
5259 #define SEGMENT_END(segment, start) \
5260 (start + (segment->p_memsz > segment->p_filesz \
5261 ? segment->p_memsz : segment->p_filesz))
5263 #define SECTION_SIZE(section, segment) \
5264 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5265 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5266 ? section->size : 0)
5268 /* Returns TRUE if the given section is contained within
5269 the given segment. VMA addresses are compared. */
5270 #define IS_CONTAINED_BY_VMA(section, segment) \
5271 (section->vma >= segment->p_vaddr \
5272 && (section->vma + SECTION_SIZE (section, segment) \
5273 <= (SEGMENT_END (segment, segment->p_vaddr))))
5275 /* Returns TRUE if the given section is contained within
5276 the given segment. LMA addresses are compared. */
5277 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5278 (section->lma >= base \
5279 && (section->lma + SECTION_SIZE (section, segment) \
5280 <= SEGMENT_END (segment, base)))
5282 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
5283 #define IS_COREFILE_NOTE(p, s) \
5284 (p->p_type == PT_NOTE \
5285 && bfd_get_format (ibfd) == bfd_core \
5286 && s->vma == 0 && s->lma == 0 \
5287 && (bfd_vma) s->filepos >= p->p_offset \
5288 && ((bfd_vma) s->filepos + s->size \
5289 <= p->p_offset + p->p_filesz))
5291 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5292 linker, which generates a PT_INTERP section with p_vaddr and
5293 p_memsz set to 0. */
5294 #define IS_SOLARIS_PT_INTERP(p, s) \
5295 (p->p_vaddr == 0 \
5296 && p->p_paddr == 0 \
5297 && p->p_memsz == 0 \
5298 && p->p_filesz > 0 \
5299 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5300 && s->size > 0 \
5301 && (bfd_vma) s->filepos >= p->p_offset \
5302 && ((bfd_vma) s->filepos + s->size \
5303 <= p->p_offset + p->p_filesz))
5305 /* Decide if the given section should be included in the given segment.
5306 A section will be included if:
5307 1. It is within the address space of the segment -- we use the LMA
5308 if that is set for the segment and the VMA otherwise,
5309 2. It is an allocated segment,
5310 3. There is an output section associated with it,
5311 4. The section has not already been allocated to a previous segment.
5312 5. PT_GNU_STACK segments do not include any sections.
5313 6. PT_TLS segment includes only SHF_TLS sections.
5314 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5315 8. PT_DYNAMIC should not contain empty sections at the beginning
5316 (with the possible exception of .dynamic). */
5317 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5318 ((((segment->p_paddr \
5319 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5320 : IS_CONTAINED_BY_VMA (section, segment)) \
5321 && (section->flags & SEC_ALLOC) != 0) \
5322 || IS_COREFILE_NOTE (segment, section)) \
5323 && segment->p_type != PT_GNU_STACK \
5324 && (segment->p_type != PT_TLS \
5325 || (section->flags & SEC_THREAD_LOCAL)) \
5326 && (segment->p_type == PT_LOAD \
5327 || segment->p_type == PT_TLS \
5328 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5329 && (segment->p_type != PT_DYNAMIC \
5330 || SECTION_SIZE (section, segment) > 0 \
5331 || (segment->p_paddr \
5332 ? segment->p_paddr != section->lma \
5333 : segment->p_vaddr != section->vma) \
5334 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5335 == 0)) \
5336 && ! section->segment_mark)
5338 /* If the output section of a section in the input segment is NULL,
5339 it is removed from the corresponding output segment. */
5340 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5341 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5342 && section->output_section != NULL)
5344 /* Returns TRUE iff seg1 starts after the end of seg2. */
5345 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5346 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5348 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5349 their VMA address ranges and their LMA address ranges overlap.
5350 It is possible to have overlapping VMA ranges without overlapping LMA
5351 ranges. RedBoot images for example can have both .data and .bss mapped
5352 to the same VMA range, but with the .data section mapped to a different
5353 LMA. */
5354 #define SEGMENT_OVERLAPS(seg1, seg2) \
5355 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5356 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5357 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5358 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5360 /* Initialise the segment mark field. */
5361 for (section = ibfd->sections; section != NULL; section = section->next)
5362 section->segment_mark = FALSE;
5364 /* Scan through the segments specified in the program header
5365 of the input BFD. For this first scan we look for overlaps
5366 in the loadable segments. These can be created by weird
5367 parameters to objcopy. Also, fix some solaris weirdness. */
5368 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5369 i < num_segments;
5370 i++, segment++)
5372 unsigned int j;
5373 Elf_Internal_Phdr *segment2;
5375 if (segment->p_type == PT_INTERP)
5376 for (section = ibfd->sections; section; section = section->next)
5377 if (IS_SOLARIS_PT_INTERP (segment, section))
5379 /* Mininal change so that the normal section to segment
5380 assignment code will work. */
5381 segment->p_vaddr = section->vma;
5382 break;
5385 if (segment->p_type != PT_LOAD)
5386 continue;
5388 /* Determine if this segment overlaps any previous segments. */
5389 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
5391 bfd_signed_vma extra_length;
5393 if (segment2->p_type != PT_LOAD
5394 || ! SEGMENT_OVERLAPS (segment, segment2))
5395 continue;
5397 /* Merge the two segments together. */
5398 if (segment2->p_vaddr < segment->p_vaddr)
5400 /* Extend SEGMENT2 to include SEGMENT and then delete
5401 SEGMENT. */
5402 extra_length =
5403 SEGMENT_END (segment, segment->p_vaddr)
5404 - SEGMENT_END (segment2, segment2->p_vaddr);
5406 if (extra_length > 0)
5408 segment2->p_memsz += extra_length;
5409 segment2->p_filesz += extra_length;
5412 segment->p_type = PT_NULL;
5414 /* Since we have deleted P we must restart the outer loop. */
5415 i = 0;
5416 segment = elf_tdata (ibfd)->phdr;
5417 break;
5419 else
5421 /* Extend SEGMENT to include SEGMENT2 and then delete
5422 SEGMENT2. */
5423 extra_length =
5424 SEGMENT_END (segment2, segment2->p_vaddr)
5425 - SEGMENT_END (segment, segment->p_vaddr);
5427 if (extra_length > 0)
5429 segment->p_memsz += extra_length;
5430 segment->p_filesz += extra_length;
5433 segment2->p_type = PT_NULL;
5438 /* The second scan attempts to assign sections to segments. */
5439 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5440 i < num_segments;
5441 i ++, segment ++)
5443 unsigned int section_count;
5444 asection ** sections;
5445 asection * output_section;
5446 unsigned int isec;
5447 bfd_vma matching_lma;
5448 bfd_vma suggested_lma;
5449 unsigned int j;
5450 bfd_size_type amt;
5451 asection * first_section;
5453 if (segment->p_type == PT_NULL)
5454 continue;
5456 first_section = NULL;
5457 /* Compute how many sections might be placed into this segment. */
5458 for (section = ibfd->sections, section_count = 0;
5459 section != NULL;
5460 section = section->next)
5462 /* Find the first section in the input segment, which may be
5463 removed from the corresponding output segment. */
5464 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
5466 if (first_section == NULL)
5467 first_section = section;
5468 if (section->output_section != NULL)
5469 ++section_count;
5473 /* Allocate a segment map big enough to contain
5474 all of the sections we have selected. */
5475 amt = sizeof (struct elf_segment_map);
5476 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5477 map = bfd_zalloc (obfd, amt);
5478 if (map == NULL)
5479 return FALSE;
5481 /* Initialise the fields of the segment map. Default to
5482 using the physical address of the segment in the input BFD. */
5483 map->next = NULL;
5484 map->p_type = segment->p_type;
5485 map->p_flags = segment->p_flags;
5486 map->p_flags_valid = 1;
5488 /* If the first section in the input segment is removed, there is
5489 no need to preserve segment physical address in the corresponding
5490 output segment. */
5491 if (!first_section || first_section->output_section != NULL)
5493 map->p_paddr = segment->p_paddr;
5494 map->p_paddr_valid = 1;
5497 /* Determine if this segment contains the ELF file header
5498 and if it contains the program headers themselves. */
5499 map->includes_filehdr = (segment->p_offset == 0
5500 && segment->p_filesz >= iehdr->e_ehsize);
5502 map->includes_phdrs = 0;
5504 if (! phdr_included || segment->p_type != PT_LOAD)
5506 map->includes_phdrs =
5507 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5508 && (segment->p_offset + segment->p_filesz
5509 >= ((bfd_vma) iehdr->e_phoff
5510 + iehdr->e_phnum * iehdr->e_phentsize)));
5512 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5513 phdr_included = TRUE;
5516 if (section_count == 0)
5518 /* Special segments, such as the PT_PHDR segment, may contain
5519 no sections, but ordinary, loadable segments should contain
5520 something. They are allowed by the ELF spec however, so only
5521 a warning is produced. */
5522 if (segment->p_type == PT_LOAD)
5523 (*_bfd_error_handler)
5524 (_("%B: warning: Empty loadable segment detected, is this intentional ?\n"),
5525 ibfd);
5527 map->count = 0;
5528 *pointer_to_map = map;
5529 pointer_to_map = &map->next;
5531 continue;
5534 /* Now scan the sections in the input BFD again and attempt
5535 to add their corresponding output sections to the segment map.
5536 The problem here is how to handle an output section which has
5537 been moved (ie had its LMA changed). There are four possibilities:
5539 1. None of the sections have been moved.
5540 In this case we can continue to use the segment LMA from the
5541 input BFD.
5543 2. All of the sections have been moved by the same amount.
5544 In this case we can change the segment's LMA to match the LMA
5545 of the first section.
5547 3. Some of the sections have been moved, others have not.
5548 In this case those sections which have not been moved can be
5549 placed in the current segment which will have to have its size,
5550 and possibly its LMA changed, and a new segment or segments will
5551 have to be created to contain the other sections.
5553 4. The sections have been moved, but not by the same amount.
5554 In this case we can change the segment's LMA to match the LMA
5555 of the first section and we will have to create a new segment
5556 or segments to contain the other sections.
5558 In order to save time, we allocate an array to hold the section
5559 pointers that we are interested in. As these sections get assigned
5560 to a segment, they are removed from this array. */
5562 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
5563 to work around this long long bug. */
5564 sections = bfd_malloc2 (section_count, sizeof (asection *));
5565 if (sections == NULL)
5566 return FALSE;
5568 /* Step One: Scan for segment vs section LMA conflicts.
5569 Also add the sections to the section array allocated above.
5570 Also add the sections to the current segment. In the common
5571 case, where the sections have not been moved, this means that
5572 we have completely filled the segment, and there is nothing
5573 more to do. */
5574 isec = 0;
5575 matching_lma = 0;
5576 suggested_lma = 0;
5578 for (j = 0, section = ibfd->sections;
5579 section != NULL;
5580 section = section->next)
5582 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5584 output_section = section->output_section;
5586 sections[j ++] = section;
5588 /* The Solaris native linker always sets p_paddr to 0.
5589 We try to catch that case here, and set it to the
5590 correct value. Note - some backends require that
5591 p_paddr be left as zero. */
5592 if (segment->p_paddr == 0
5593 && segment->p_vaddr != 0
5594 && (! bed->want_p_paddr_set_to_zero)
5595 && isec == 0
5596 && output_section->lma != 0
5597 && (output_section->vma == (segment->p_vaddr
5598 + (map->includes_filehdr
5599 ? iehdr->e_ehsize
5600 : 0)
5601 + (map->includes_phdrs
5602 ? (iehdr->e_phnum
5603 * iehdr->e_phentsize)
5604 : 0))))
5605 map->p_paddr = segment->p_vaddr;
5607 /* Match up the physical address of the segment with the
5608 LMA address of the output section. */
5609 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5610 || IS_COREFILE_NOTE (segment, section)
5611 || (bed->want_p_paddr_set_to_zero &&
5612 IS_CONTAINED_BY_VMA (output_section, segment)))
5614 if (matching_lma == 0)
5615 matching_lma = output_section->lma;
5617 /* We assume that if the section fits within the segment
5618 then it does not overlap any other section within that
5619 segment. */
5620 map->sections[isec ++] = output_section;
5622 else if (suggested_lma == 0)
5623 suggested_lma = output_section->lma;
5627 BFD_ASSERT (j == section_count);
5629 /* Step Two: Adjust the physical address of the current segment,
5630 if necessary. */
5631 if (isec == section_count)
5633 /* All of the sections fitted within the segment as currently
5634 specified. This is the default case. Add the segment to
5635 the list of built segments and carry on to process the next
5636 program header in the input BFD. */
5637 map->count = section_count;
5638 *pointer_to_map = map;
5639 pointer_to_map = &map->next;
5641 if (matching_lma != map->p_paddr
5642 && !map->includes_filehdr && !map->includes_phdrs)
5643 /* There is some padding before the first section in the
5644 segment. So, we must account for that in the output
5645 segment's vma. */
5646 map->p_vaddr_offset = matching_lma - map->p_paddr;
5648 free (sections);
5649 continue;
5651 else
5653 if (matching_lma != 0)
5655 /* At least one section fits inside the current segment.
5656 Keep it, but modify its physical address to match the
5657 LMA of the first section that fitted. */
5658 map->p_paddr = matching_lma;
5660 else
5662 /* None of the sections fitted inside the current segment.
5663 Change the current segment's physical address to match
5664 the LMA of the first section. */
5665 map->p_paddr = suggested_lma;
5668 /* Offset the segment physical address from the lma
5669 to allow for space taken up by elf headers. */
5670 if (map->includes_filehdr)
5671 map->p_paddr -= iehdr->e_ehsize;
5673 if (map->includes_phdrs)
5675 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5677 /* iehdr->e_phnum is just an estimate of the number
5678 of program headers that we will need. Make a note
5679 here of the number we used and the segment we chose
5680 to hold these headers, so that we can adjust the
5681 offset when we know the correct value. */
5682 phdr_adjust_num = iehdr->e_phnum;
5683 phdr_adjust_seg = map;
5687 /* Step Three: Loop over the sections again, this time assigning
5688 those that fit to the current segment and removing them from the
5689 sections array; but making sure not to leave large gaps. Once all
5690 possible sections have been assigned to the current segment it is
5691 added to the list of built segments and if sections still remain
5692 to be assigned, a new segment is constructed before repeating
5693 the loop. */
5694 isec = 0;
5697 map->count = 0;
5698 suggested_lma = 0;
5700 /* Fill the current segment with sections that fit. */
5701 for (j = 0; j < section_count; j++)
5703 section = sections[j];
5705 if (section == NULL)
5706 continue;
5708 output_section = section->output_section;
5710 BFD_ASSERT (output_section != NULL);
5712 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5713 || IS_COREFILE_NOTE (segment, section))
5715 if (map->count == 0)
5717 /* If the first section in a segment does not start at
5718 the beginning of the segment, then something is
5719 wrong. */
5720 if (output_section->lma !=
5721 (map->p_paddr
5722 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5723 + (map->includes_phdrs
5724 ? iehdr->e_phnum * iehdr->e_phentsize
5725 : 0)))
5726 abort ();
5728 else
5730 asection * prev_sec;
5732 prev_sec = map->sections[map->count - 1];
5734 /* If the gap between the end of the previous section
5735 and the start of this section is more than
5736 maxpagesize then we need to start a new segment. */
5737 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
5738 maxpagesize)
5739 < BFD_ALIGN (output_section->lma, maxpagesize))
5740 || ((prev_sec->lma + prev_sec->size)
5741 > output_section->lma))
5743 if (suggested_lma == 0)
5744 suggested_lma = output_section->lma;
5746 continue;
5750 map->sections[map->count++] = output_section;
5751 ++isec;
5752 sections[j] = NULL;
5753 section->segment_mark = TRUE;
5755 else if (suggested_lma == 0)
5756 suggested_lma = output_section->lma;
5759 BFD_ASSERT (map->count > 0);
5761 /* Add the current segment to the list of built segments. */
5762 *pointer_to_map = map;
5763 pointer_to_map = &map->next;
5765 if (isec < section_count)
5767 /* We still have not allocated all of the sections to
5768 segments. Create a new segment here, initialise it
5769 and carry on looping. */
5770 amt = sizeof (struct elf_segment_map);
5771 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5772 map = bfd_alloc (obfd, amt);
5773 if (map == NULL)
5775 free (sections);
5776 return FALSE;
5779 /* Initialise the fields of the segment map. Set the physical
5780 physical address to the LMA of the first section that has
5781 not yet been assigned. */
5782 map->next = NULL;
5783 map->p_type = segment->p_type;
5784 map->p_flags = segment->p_flags;
5785 map->p_flags_valid = 1;
5786 map->p_paddr = suggested_lma;
5787 map->p_paddr_valid = 1;
5788 map->includes_filehdr = 0;
5789 map->includes_phdrs = 0;
5792 while (isec < section_count);
5794 free (sections);
5797 /* The Solaris linker creates program headers in which all the
5798 p_paddr fields are zero. When we try to objcopy or strip such a
5799 file, we get confused. Check for this case, and if we find it
5800 reset the p_paddr_valid fields. */
5801 for (map = map_first; map != NULL; map = map->next)
5802 if (map->p_paddr != 0)
5803 break;
5804 if (map == NULL)
5805 for (map = map_first; map != NULL; map = map->next)
5806 map->p_paddr_valid = 0;
5808 elf_tdata (obfd)->segment_map = map_first;
5810 /* If we had to estimate the number of program headers that were
5811 going to be needed, then check our estimate now and adjust
5812 the offset if necessary. */
5813 if (phdr_adjust_seg != NULL)
5815 unsigned int count;
5817 for (count = 0, map = map_first; map != NULL; map = map->next)
5818 count++;
5820 if (count > phdr_adjust_num)
5821 phdr_adjust_seg->p_paddr
5822 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5825 #undef SEGMENT_END
5826 #undef SECTION_SIZE
5827 #undef IS_CONTAINED_BY_VMA
5828 #undef IS_CONTAINED_BY_LMA
5829 #undef IS_COREFILE_NOTE
5830 #undef IS_SOLARIS_PT_INTERP
5831 #undef IS_SECTION_IN_INPUT_SEGMENT
5832 #undef INCLUDE_SECTION_IN_SEGMENT
5833 #undef SEGMENT_AFTER_SEGMENT
5834 #undef SEGMENT_OVERLAPS
5835 return TRUE;
5838 /* Copy ELF program header information. */
5840 static bfd_boolean
5841 copy_elf_program_header (bfd *ibfd, bfd *obfd)
5843 Elf_Internal_Ehdr *iehdr;
5844 struct elf_segment_map *map;
5845 struct elf_segment_map *map_first;
5846 struct elf_segment_map **pointer_to_map;
5847 Elf_Internal_Phdr *segment;
5848 unsigned int i;
5849 unsigned int num_segments;
5850 bfd_boolean phdr_included = FALSE;
5852 iehdr = elf_elfheader (ibfd);
5854 map_first = NULL;
5855 pointer_to_map = &map_first;
5857 num_segments = elf_elfheader (ibfd)->e_phnum;
5858 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5859 i < num_segments;
5860 i++, segment++)
5862 asection *section;
5863 unsigned int section_count;
5864 bfd_size_type amt;
5865 Elf_Internal_Shdr *this_hdr;
5866 asection *first_section = NULL;
5868 /* FIXME: Do we need to copy PT_NULL segment? */
5869 if (segment->p_type == PT_NULL)
5870 continue;
5872 /* Compute how many sections are in this segment. */
5873 for (section = ibfd->sections, section_count = 0;
5874 section != NULL;
5875 section = section->next)
5877 this_hdr = &(elf_section_data(section)->this_hdr);
5878 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5880 if (!first_section)
5881 first_section = section;
5882 section_count++;
5886 /* Allocate a segment map big enough to contain
5887 all of the sections we have selected. */
5888 amt = sizeof (struct elf_segment_map);
5889 if (section_count != 0)
5890 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5891 map = bfd_zalloc (obfd, amt);
5892 if (map == NULL)
5893 return FALSE;
5895 /* Initialize the fields of the output segment map with the
5896 input segment. */
5897 map->next = NULL;
5898 map->p_type = segment->p_type;
5899 map->p_flags = segment->p_flags;
5900 map->p_flags_valid = 1;
5901 map->p_paddr = segment->p_paddr;
5902 map->p_paddr_valid = 1;
5903 map->p_align = segment->p_align;
5904 map->p_align_valid = 1;
5905 map->p_vaddr_offset = 0;
5907 /* Determine if this segment contains the ELF file header
5908 and if it contains the program headers themselves. */
5909 map->includes_filehdr = (segment->p_offset == 0
5910 && segment->p_filesz >= iehdr->e_ehsize);
5912 map->includes_phdrs = 0;
5913 if (! phdr_included || segment->p_type != PT_LOAD)
5915 map->includes_phdrs =
5916 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5917 && (segment->p_offset + segment->p_filesz
5918 >= ((bfd_vma) iehdr->e_phoff
5919 + iehdr->e_phnum * iehdr->e_phentsize)));
5921 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5922 phdr_included = TRUE;
5925 if (!map->includes_phdrs && !map->includes_filehdr)
5926 /* There is some other padding before the first section. */
5927 map->p_vaddr_offset = ((first_section ? first_section->lma : 0)
5928 - segment->p_paddr);
5930 if (section_count != 0)
5932 unsigned int isec = 0;
5934 for (section = first_section;
5935 section != NULL;
5936 section = section->next)
5938 this_hdr = &(elf_section_data(section)->this_hdr);
5939 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5941 map->sections[isec++] = section->output_section;
5942 if (isec == section_count)
5943 break;
5948 map->count = section_count;
5949 *pointer_to_map = map;
5950 pointer_to_map = &map->next;
5953 elf_tdata (obfd)->segment_map = map_first;
5954 return TRUE;
5957 /* Copy private BFD data. This copies or rewrites ELF program header
5958 information. */
5960 static bfd_boolean
5961 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
5963 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5964 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5965 return TRUE;
5967 if (elf_tdata (ibfd)->phdr == NULL)
5968 return TRUE;
5970 if (ibfd->xvec == obfd->xvec)
5972 /* Check to see if any sections in the input BFD
5973 covered by ELF program header have changed. */
5974 Elf_Internal_Phdr *segment;
5975 asection *section, *osec;
5976 unsigned int i, num_segments;
5977 Elf_Internal_Shdr *this_hdr;
5979 /* Initialize the segment mark field. */
5980 for (section = obfd->sections; section != NULL;
5981 section = section->next)
5982 section->segment_mark = FALSE;
5984 num_segments = elf_elfheader (ibfd)->e_phnum;
5985 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5986 i < num_segments;
5987 i++, segment++)
5989 /* PR binutils/3535. The Solaris linker always sets the p_paddr
5990 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
5991 which severly confuses things, so always regenerate the segment
5992 map in this case. */
5993 if (segment->p_paddr == 0
5994 && segment->p_memsz == 0
5995 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
5996 goto rewrite;
5998 for (section = ibfd->sections;
5999 section != NULL; section = section->next)
6001 /* We mark the output section so that we know it comes
6002 from the input BFD. */
6003 osec = section->output_section;
6004 if (osec)
6005 osec->segment_mark = TRUE;
6007 /* Check if this section is covered by the segment. */
6008 this_hdr = &(elf_section_data(section)->this_hdr);
6009 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
6011 /* FIXME: Check if its output section is changed or
6012 removed. What else do we need to check? */
6013 if (osec == NULL
6014 || section->flags != osec->flags
6015 || section->lma != osec->lma
6016 || section->vma != osec->vma
6017 || section->size != osec->size
6018 || section->rawsize != osec->rawsize
6019 || section->alignment_power != osec->alignment_power)
6020 goto rewrite;
6025 /* Check to see if any output section do not come from the
6026 input BFD. */
6027 for (section = obfd->sections; section != NULL;
6028 section = section->next)
6030 if (section->segment_mark == FALSE)
6031 goto rewrite;
6032 else
6033 section->segment_mark = FALSE;
6036 return copy_elf_program_header (ibfd, obfd);
6039 rewrite:
6040 return rewrite_elf_program_header (ibfd, obfd);
6043 /* Initialize private output section information from input section. */
6045 bfd_boolean
6046 _bfd_elf_init_private_section_data (bfd *ibfd,
6047 asection *isec,
6048 bfd *obfd,
6049 asection *osec,
6050 struct bfd_link_info *link_info)
6053 Elf_Internal_Shdr *ihdr, *ohdr;
6054 bfd_boolean need_group = link_info == NULL || link_info->relocatable;
6056 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6057 || obfd->xvec->flavour != bfd_target_elf_flavour)
6058 return TRUE;
6060 /* Don't copy the output ELF section type from input if the
6061 output BFD section flags have been set to something different.
6062 elf_fake_sections will set ELF section type based on BFD
6063 section flags. */
6064 if (elf_section_type (osec) == SHT_NULL
6065 && (osec->flags == isec->flags || !osec->flags))
6066 elf_section_type (osec) = elf_section_type (isec);
6068 /* FIXME: Is this correct for all OS/PROC specific flags? */
6069 elf_section_flags (osec) |= (elf_section_flags (isec)
6070 & (SHF_MASKOS | SHF_MASKPROC));
6072 /* Set things up for objcopy and relocatable link. The output
6073 SHT_GROUP section will have its elf_next_in_group pointing back
6074 to the input group members. Ignore linker created group section.
6075 See elfNN_ia64_object_p in elfxx-ia64.c. */
6076 if (need_group)
6078 if (elf_sec_group (isec) == NULL
6079 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
6081 if (elf_section_flags (isec) & SHF_GROUP)
6082 elf_section_flags (osec) |= SHF_GROUP;
6083 elf_next_in_group (osec) = elf_next_in_group (isec);
6084 elf_group_name (osec) = elf_group_name (isec);
6088 ihdr = &elf_section_data (isec)->this_hdr;
6090 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6091 don't use the output section of the linked-to section since it
6092 may be NULL at this point. */
6093 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
6095 ohdr = &elf_section_data (osec)->this_hdr;
6096 ohdr->sh_flags |= SHF_LINK_ORDER;
6097 elf_linked_to_section (osec) = elf_linked_to_section (isec);
6100 osec->use_rela_p = isec->use_rela_p;
6102 return TRUE;
6105 /* Copy private section information. This copies over the entsize
6106 field, and sometimes the info field. */
6108 bfd_boolean
6109 _bfd_elf_copy_private_section_data (bfd *ibfd,
6110 asection *isec,
6111 bfd *obfd,
6112 asection *osec)
6114 Elf_Internal_Shdr *ihdr, *ohdr;
6116 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6117 || obfd->xvec->flavour != bfd_target_elf_flavour)
6118 return TRUE;
6120 ihdr = &elf_section_data (isec)->this_hdr;
6121 ohdr = &elf_section_data (osec)->this_hdr;
6123 ohdr->sh_entsize = ihdr->sh_entsize;
6125 if (ihdr->sh_type == SHT_SYMTAB
6126 || ihdr->sh_type == SHT_DYNSYM
6127 || ihdr->sh_type == SHT_GNU_verneed
6128 || ihdr->sh_type == SHT_GNU_verdef)
6129 ohdr->sh_info = ihdr->sh_info;
6131 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
6132 NULL);
6135 /* Copy private header information. */
6137 bfd_boolean
6138 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
6140 asection *isec;
6142 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6143 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6144 return TRUE;
6146 /* Copy over private BFD data if it has not already been copied.
6147 This must be done here, rather than in the copy_private_bfd_data
6148 entry point, because the latter is called after the section
6149 contents have been set, which means that the program headers have
6150 already been worked out. */
6151 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
6153 if (! copy_private_bfd_data (ibfd, obfd))
6154 return FALSE;
6157 /* _bfd_elf_copy_private_section_data copied over the SHF_GROUP flag
6158 but this might be wrong if we deleted the group section. */
6159 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
6160 if (elf_section_type (isec) == SHT_GROUP
6161 && isec->output_section == NULL)
6163 asection *first = elf_next_in_group (isec);
6164 asection *s = first;
6165 while (s != NULL)
6167 if (s->output_section != NULL)
6169 elf_section_flags (s->output_section) &= ~SHF_GROUP;
6170 elf_group_name (s->output_section) = NULL;
6172 s = elf_next_in_group (s);
6173 if (s == first)
6174 break;
6178 return TRUE;
6181 /* Copy private symbol information. If this symbol is in a section
6182 which we did not map into a BFD section, try to map the section
6183 index correctly. We use special macro definitions for the mapped
6184 section indices; these definitions are interpreted by the
6185 swap_out_syms function. */
6187 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6188 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6189 #define MAP_STRTAB (SHN_HIOS + 3)
6190 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6191 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6193 bfd_boolean
6194 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
6195 asymbol *isymarg,
6196 bfd *obfd,
6197 asymbol *osymarg)
6199 elf_symbol_type *isym, *osym;
6201 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6202 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6203 return TRUE;
6205 isym = elf_symbol_from (ibfd, isymarg);
6206 osym = elf_symbol_from (obfd, osymarg);
6208 if (isym != NULL
6209 && osym != NULL
6210 && bfd_is_abs_section (isym->symbol.section))
6212 unsigned int shndx;
6214 shndx = isym->internal_elf_sym.st_shndx;
6215 if (shndx == elf_onesymtab (ibfd))
6216 shndx = MAP_ONESYMTAB;
6217 else if (shndx == elf_dynsymtab (ibfd))
6218 shndx = MAP_DYNSYMTAB;
6219 else if (shndx == elf_tdata (ibfd)->strtab_section)
6220 shndx = MAP_STRTAB;
6221 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
6222 shndx = MAP_SHSTRTAB;
6223 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
6224 shndx = MAP_SYM_SHNDX;
6225 osym->internal_elf_sym.st_shndx = shndx;
6228 return TRUE;
6231 /* Swap out the symbols. */
6233 static bfd_boolean
6234 swap_out_syms (bfd *abfd,
6235 struct bfd_strtab_hash **sttp,
6236 int relocatable_p)
6238 const struct elf_backend_data *bed;
6239 int symcount;
6240 asymbol **syms;
6241 struct bfd_strtab_hash *stt;
6242 Elf_Internal_Shdr *symtab_hdr;
6243 Elf_Internal_Shdr *symtab_shndx_hdr;
6244 Elf_Internal_Shdr *symstrtab_hdr;
6245 bfd_byte *outbound_syms;
6246 bfd_byte *outbound_shndx;
6247 int idx;
6248 bfd_size_type amt;
6249 bfd_boolean name_local_sections;
6251 if (!elf_map_symbols (abfd))
6252 return FALSE;
6254 /* Dump out the symtabs. */
6255 stt = _bfd_elf_stringtab_init ();
6256 if (stt == NULL)
6257 return FALSE;
6259 bed = get_elf_backend_data (abfd);
6260 symcount = bfd_get_symcount (abfd);
6261 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6262 symtab_hdr->sh_type = SHT_SYMTAB;
6263 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
6264 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
6265 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
6266 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
6268 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
6269 symstrtab_hdr->sh_type = SHT_STRTAB;
6271 outbound_syms = bfd_alloc2 (abfd, 1 + symcount, bed->s->sizeof_sym);
6272 if (outbound_syms == NULL)
6274 _bfd_stringtab_free (stt);
6275 return FALSE;
6277 symtab_hdr->contents = outbound_syms;
6279 outbound_shndx = NULL;
6280 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
6281 if (symtab_shndx_hdr->sh_name != 0)
6283 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
6284 outbound_shndx = bfd_zalloc2 (abfd, 1 + symcount,
6285 sizeof (Elf_External_Sym_Shndx));
6286 if (outbound_shndx == NULL)
6288 _bfd_stringtab_free (stt);
6289 return FALSE;
6292 symtab_shndx_hdr->contents = outbound_shndx;
6293 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
6294 symtab_shndx_hdr->sh_size = amt;
6295 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
6296 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
6299 /* Now generate the data (for "contents"). */
6301 /* Fill in zeroth symbol and swap it out. */
6302 Elf_Internal_Sym sym;
6303 sym.st_name = 0;
6304 sym.st_value = 0;
6305 sym.st_size = 0;
6306 sym.st_info = 0;
6307 sym.st_other = 0;
6308 sym.st_shndx = SHN_UNDEF;
6309 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6310 outbound_syms += bed->s->sizeof_sym;
6311 if (outbound_shndx != NULL)
6312 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6315 name_local_sections
6316 = (bed->elf_backend_name_local_section_symbols
6317 && bed->elf_backend_name_local_section_symbols (abfd));
6319 syms = bfd_get_outsymbols (abfd);
6320 for (idx = 0; idx < symcount; idx++)
6322 Elf_Internal_Sym sym;
6323 bfd_vma value = syms[idx]->value;
6324 elf_symbol_type *type_ptr;
6325 flagword flags = syms[idx]->flags;
6326 int type;
6328 if (!name_local_sections
6329 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
6331 /* Local section symbols have no name. */
6332 sym.st_name = 0;
6334 else
6336 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
6337 syms[idx]->name,
6338 TRUE, FALSE);
6339 if (sym.st_name == (unsigned long) -1)
6341 _bfd_stringtab_free (stt);
6342 return FALSE;
6346 type_ptr = elf_symbol_from (abfd, syms[idx]);
6348 if ((flags & BSF_SECTION_SYM) == 0
6349 && bfd_is_com_section (syms[idx]->section))
6351 /* ELF common symbols put the alignment into the `value' field,
6352 and the size into the `size' field. This is backwards from
6353 how BFD handles it, so reverse it here. */
6354 sym.st_size = value;
6355 if (type_ptr == NULL
6356 || type_ptr->internal_elf_sym.st_value == 0)
6357 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
6358 else
6359 sym.st_value = type_ptr->internal_elf_sym.st_value;
6360 sym.st_shndx = _bfd_elf_section_from_bfd_section
6361 (abfd, syms[idx]->section);
6363 else
6365 asection *sec = syms[idx]->section;
6366 int shndx;
6368 if (sec->output_section)
6370 value += sec->output_offset;
6371 sec = sec->output_section;
6374 /* Don't add in the section vma for relocatable output. */
6375 if (! relocatable_p)
6376 value += sec->vma;
6377 sym.st_value = value;
6378 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
6380 if (bfd_is_abs_section (sec)
6381 && type_ptr != NULL
6382 && type_ptr->internal_elf_sym.st_shndx != 0)
6384 /* This symbol is in a real ELF section which we did
6385 not create as a BFD section. Undo the mapping done
6386 by copy_private_symbol_data. */
6387 shndx = type_ptr->internal_elf_sym.st_shndx;
6388 switch (shndx)
6390 case MAP_ONESYMTAB:
6391 shndx = elf_onesymtab (abfd);
6392 break;
6393 case MAP_DYNSYMTAB:
6394 shndx = elf_dynsymtab (abfd);
6395 break;
6396 case MAP_STRTAB:
6397 shndx = elf_tdata (abfd)->strtab_section;
6398 break;
6399 case MAP_SHSTRTAB:
6400 shndx = elf_tdata (abfd)->shstrtab_section;
6401 break;
6402 case MAP_SYM_SHNDX:
6403 shndx = elf_tdata (abfd)->symtab_shndx_section;
6404 break;
6405 default:
6406 break;
6409 else
6411 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
6413 if (shndx == -1)
6415 asection *sec2;
6417 /* Writing this would be a hell of a lot easier if
6418 we had some decent documentation on bfd, and
6419 knew what to expect of the library, and what to
6420 demand of applications. For example, it
6421 appears that `objcopy' might not set the
6422 section of a symbol to be a section that is
6423 actually in the output file. */
6424 sec2 = bfd_get_section_by_name (abfd, sec->name);
6425 if (sec2 == NULL)
6427 _bfd_error_handler (_("\
6428 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6429 syms[idx]->name ? syms[idx]->name : "<Local sym>",
6430 sec->name);
6431 bfd_set_error (bfd_error_invalid_operation);
6432 _bfd_stringtab_free (stt);
6433 return FALSE;
6436 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
6437 BFD_ASSERT (shndx != -1);
6441 sym.st_shndx = shndx;
6444 if ((flags & BSF_THREAD_LOCAL) != 0)
6445 type = STT_TLS;
6446 else if ((flags & BSF_FUNCTION) != 0)
6447 type = STT_FUNC;
6448 else if ((flags & BSF_OBJECT) != 0)
6449 type = STT_OBJECT;
6450 else if ((flags & BSF_RELC) != 0)
6451 type = STT_RELC;
6452 else if ((flags & BSF_SRELC) != 0)
6453 type = STT_SRELC;
6454 else
6455 type = STT_NOTYPE;
6457 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
6458 type = STT_TLS;
6460 /* Processor-specific types. */
6461 if (type_ptr != NULL
6462 && bed->elf_backend_get_symbol_type)
6463 type = ((*bed->elf_backend_get_symbol_type)
6464 (&type_ptr->internal_elf_sym, type));
6466 if (flags & BSF_SECTION_SYM)
6468 if (flags & BSF_GLOBAL)
6469 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6470 else
6471 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
6473 else if (bfd_is_com_section (syms[idx]->section))
6474 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
6475 else if (bfd_is_und_section (syms[idx]->section))
6476 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
6477 ? STB_WEAK
6478 : STB_GLOBAL),
6479 type);
6480 else if (flags & BSF_FILE)
6481 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
6482 else
6484 int bind = STB_LOCAL;
6486 if (flags & BSF_LOCAL)
6487 bind = STB_LOCAL;
6488 else if (flags & BSF_WEAK)
6489 bind = STB_WEAK;
6490 else if (flags & BSF_GLOBAL)
6491 bind = STB_GLOBAL;
6493 sym.st_info = ELF_ST_INFO (bind, type);
6496 if (type_ptr != NULL)
6497 sym.st_other = type_ptr->internal_elf_sym.st_other;
6498 else
6499 sym.st_other = 0;
6501 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6502 outbound_syms += bed->s->sizeof_sym;
6503 if (outbound_shndx != NULL)
6504 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6507 *sttp = stt;
6508 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
6509 symstrtab_hdr->sh_type = SHT_STRTAB;
6511 symstrtab_hdr->sh_flags = 0;
6512 symstrtab_hdr->sh_addr = 0;
6513 symstrtab_hdr->sh_entsize = 0;
6514 symstrtab_hdr->sh_link = 0;
6515 symstrtab_hdr->sh_info = 0;
6516 symstrtab_hdr->sh_addralign = 1;
6518 return TRUE;
6521 /* Return the number of bytes required to hold the symtab vector.
6523 Note that we base it on the count plus 1, since we will null terminate
6524 the vector allocated based on this size. However, the ELF symbol table
6525 always has a dummy entry as symbol #0, so it ends up even. */
6527 long
6528 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
6530 long symcount;
6531 long symtab_size;
6532 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
6534 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6535 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6536 if (symcount > 0)
6537 symtab_size -= sizeof (asymbol *);
6539 return symtab_size;
6542 long
6543 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
6545 long symcount;
6546 long symtab_size;
6547 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
6549 if (elf_dynsymtab (abfd) == 0)
6551 bfd_set_error (bfd_error_invalid_operation);
6552 return -1;
6555 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6556 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6557 if (symcount > 0)
6558 symtab_size -= sizeof (asymbol *);
6560 return symtab_size;
6563 long
6564 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
6565 sec_ptr asect)
6567 return (asect->reloc_count + 1) * sizeof (arelent *);
6570 /* Canonicalize the relocs. */
6572 long
6573 _bfd_elf_canonicalize_reloc (bfd *abfd,
6574 sec_ptr section,
6575 arelent **relptr,
6576 asymbol **symbols)
6578 arelent *tblptr;
6579 unsigned int i;
6580 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6582 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
6583 return -1;
6585 tblptr = section->relocation;
6586 for (i = 0; i < section->reloc_count; i++)
6587 *relptr++ = tblptr++;
6589 *relptr = NULL;
6591 return section->reloc_count;
6594 long
6595 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
6597 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6598 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
6600 if (symcount >= 0)
6601 bfd_get_symcount (abfd) = symcount;
6602 return symcount;
6605 long
6606 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
6607 asymbol **allocation)
6609 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6610 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
6612 if (symcount >= 0)
6613 bfd_get_dynamic_symcount (abfd) = symcount;
6614 return symcount;
6617 /* Return the size required for the dynamic reloc entries. Any loadable
6618 section that was actually installed in the BFD, and has type SHT_REL
6619 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
6620 dynamic reloc section. */
6622 long
6623 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
6625 long ret;
6626 asection *s;
6628 if (elf_dynsymtab (abfd) == 0)
6630 bfd_set_error (bfd_error_invalid_operation);
6631 return -1;
6634 ret = sizeof (arelent *);
6635 for (s = abfd->sections; s != NULL; s = s->next)
6636 if ((s->flags & SEC_LOAD) != 0
6637 && elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6638 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6639 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6640 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
6641 * sizeof (arelent *));
6643 return ret;
6646 /* Canonicalize the dynamic relocation entries. Note that we return the
6647 dynamic relocations as a single block, although they are actually
6648 associated with particular sections; the interface, which was
6649 designed for SunOS style shared libraries, expects that there is only
6650 one set of dynamic relocs. Any loadable section that was actually
6651 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
6652 dynamic symbol table, is considered to be a dynamic reloc section. */
6654 long
6655 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
6656 arelent **storage,
6657 asymbol **syms)
6659 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
6660 asection *s;
6661 long ret;
6663 if (elf_dynsymtab (abfd) == 0)
6665 bfd_set_error (bfd_error_invalid_operation);
6666 return -1;
6669 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
6670 ret = 0;
6671 for (s = abfd->sections; s != NULL; s = s->next)
6673 if ((s->flags & SEC_LOAD) != 0
6674 && elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6675 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6676 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6678 arelent *p;
6679 long count, i;
6681 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
6682 return -1;
6683 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
6684 p = s->relocation;
6685 for (i = 0; i < count; i++)
6686 *storage++ = p++;
6687 ret += count;
6691 *storage = NULL;
6693 return ret;
6696 /* Read in the version information. */
6698 bfd_boolean
6699 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
6701 bfd_byte *contents = NULL;
6702 unsigned int freeidx = 0;
6704 if (elf_dynverref (abfd) != 0)
6706 Elf_Internal_Shdr *hdr;
6707 Elf_External_Verneed *everneed;
6708 Elf_Internal_Verneed *iverneed;
6709 unsigned int i;
6710 bfd_byte *contents_end;
6712 hdr = &elf_tdata (abfd)->dynverref_hdr;
6714 elf_tdata (abfd)->verref = bfd_zalloc2 (abfd, hdr->sh_info,
6715 sizeof (Elf_Internal_Verneed));
6716 if (elf_tdata (abfd)->verref == NULL)
6717 goto error_return;
6719 elf_tdata (abfd)->cverrefs = hdr->sh_info;
6721 contents = bfd_malloc (hdr->sh_size);
6722 if (contents == NULL)
6724 error_return_verref:
6725 elf_tdata (abfd)->verref = NULL;
6726 elf_tdata (abfd)->cverrefs = 0;
6727 goto error_return;
6729 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6730 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6731 goto error_return_verref;
6733 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed))
6734 goto error_return_verref;
6736 BFD_ASSERT (sizeof (Elf_External_Verneed)
6737 == sizeof (Elf_External_Vernaux));
6738 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
6739 everneed = (Elf_External_Verneed *) contents;
6740 iverneed = elf_tdata (abfd)->verref;
6741 for (i = 0; i < hdr->sh_info; i++, iverneed++)
6743 Elf_External_Vernaux *evernaux;
6744 Elf_Internal_Vernaux *ivernaux;
6745 unsigned int j;
6747 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
6749 iverneed->vn_bfd = abfd;
6751 iverneed->vn_filename =
6752 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6753 iverneed->vn_file);
6754 if (iverneed->vn_filename == NULL)
6755 goto error_return_verref;
6757 if (iverneed->vn_cnt == 0)
6758 iverneed->vn_auxptr = NULL;
6759 else
6761 iverneed->vn_auxptr = bfd_alloc2 (abfd, iverneed->vn_cnt,
6762 sizeof (Elf_Internal_Vernaux));
6763 if (iverneed->vn_auxptr == NULL)
6764 goto error_return_verref;
6767 if (iverneed->vn_aux
6768 > (size_t) (contents_end - (bfd_byte *) everneed))
6769 goto error_return_verref;
6771 evernaux = ((Elf_External_Vernaux *)
6772 ((bfd_byte *) everneed + iverneed->vn_aux));
6773 ivernaux = iverneed->vn_auxptr;
6774 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
6776 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
6778 ivernaux->vna_nodename =
6779 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6780 ivernaux->vna_name);
6781 if (ivernaux->vna_nodename == NULL)
6782 goto error_return_verref;
6784 if (j + 1 < iverneed->vn_cnt)
6785 ivernaux->vna_nextptr = ivernaux + 1;
6786 else
6787 ivernaux->vna_nextptr = NULL;
6789 if (ivernaux->vna_next
6790 > (size_t) (contents_end - (bfd_byte *) evernaux))
6791 goto error_return_verref;
6793 evernaux = ((Elf_External_Vernaux *)
6794 ((bfd_byte *) evernaux + ivernaux->vna_next));
6796 if (ivernaux->vna_other > freeidx)
6797 freeidx = ivernaux->vna_other;
6800 if (i + 1 < hdr->sh_info)
6801 iverneed->vn_nextref = iverneed + 1;
6802 else
6803 iverneed->vn_nextref = NULL;
6805 if (iverneed->vn_next
6806 > (size_t) (contents_end - (bfd_byte *) everneed))
6807 goto error_return_verref;
6809 everneed = ((Elf_External_Verneed *)
6810 ((bfd_byte *) everneed + iverneed->vn_next));
6813 free (contents);
6814 contents = NULL;
6817 if (elf_dynverdef (abfd) != 0)
6819 Elf_Internal_Shdr *hdr;
6820 Elf_External_Verdef *everdef;
6821 Elf_Internal_Verdef *iverdef;
6822 Elf_Internal_Verdef *iverdefarr;
6823 Elf_Internal_Verdef iverdefmem;
6824 unsigned int i;
6825 unsigned int maxidx;
6826 bfd_byte *contents_end_def, *contents_end_aux;
6828 hdr = &elf_tdata (abfd)->dynverdef_hdr;
6830 contents = bfd_malloc (hdr->sh_size);
6831 if (contents == NULL)
6832 goto error_return;
6833 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6834 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6835 goto error_return;
6837 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef))
6838 goto error_return;
6840 BFD_ASSERT (sizeof (Elf_External_Verdef)
6841 >= sizeof (Elf_External_Verdaux));
6842 contents_end_def = contents + hdr->sh_size
6843 - sizeof (Elf_External_Verdef);
6844 contents_end_aux = contents + hdr->sh_size
6845 - sizeof (Elf_External_Verdaux);
6847 /* We know the number of entries in the section but not the maximum
6848 index. Therefore we have to run through all entries and find
6849 the maximum. */
6850 everdef = (Elf_External_Verdef *) contents;
6851 maxidx = 0;
6852 for (i = 0; i < hdr->sh_info; ++i)
6854 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6856 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
6857 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
6859 if (iverdefmem.vd_next
6860 > (size_t) (contents_end_def - (bfd_byte *) everdef))
6861 goto error_return;
6863 everdef = ((Elf_External_Verdef *)
6864 ((bfd_byte *) everdef + iverdefmem.vd_next));
6867 if (default_imported_symver)
6869 if (freeidx > maxidx)
6870 maxidx = ++freeidx;
6871 else
6872 freeidx = ++maxidx;
6874 elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, maxidx,
6875 sizeof (Elf_Internal_Verdef));
6876 if (elf_tdata (abfd)->verdef == NULL)
6877 goto error_return;
6879 elf_tdata (abfd)->cverdefs = maxidx;
6881 everdef = (Elf_External_Verdef *) contents;
6882 iverdefarr = elf_tdata (abfd)->verdef;
6883 for (i = 0; i < hdr->sh_info; i++)
6885 Elf_External_Verdaux *everdaux;
6886 Elf_Internal_Verdaux *iverdaux;
6887 unsigned int j;
6889 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6891 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
6893 error_return_verdef:
6894 elf_tdata (abfd)->verdef = NULL;
6895 elf_tdata (abfd)->cverdefs = 0;
6896 goto error_return;
6899 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
6900 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
6902 iverdef->vd_bfd = abfd;
6904 if (iverdef->vd_cnt == 0)
6905 iverdef->vd_auxptr = NULL;
6906 else
6908 iverdef->vd_auxptr = bfd_alloc2 (abfd, iverdef->vd_cnt,
6909 sizeof (Elf_Internal_Verdaux));
6910 if (iverdef->vd_auxptr == NULL)
6911 goto error_return_verdef;
6914 if (iverdef->vd_aux
6915 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
6916 goto error_return_verdef;
6918 everdaux = ((Elf_External_Verdaux *)
6919 ((bfd_byte *) everdef + iverdef->vd_aux));
6920 iverdaux = iverdef->vd_auxptr;
6921 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
6923 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
6925 iverdaux->vda_nodename =
6926 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6927 iverdaux->vda_name);
6928 if (iverdaux->vda_nodename == NULL)
6929 goto error_return_verdef;
6931 if (j + 1 < iverdef->vd_cnt)
6932 iverdaux->vda_nextptr = iverdaux + 1;
6933 else
6934 iverdaux->vda_nextptr = NULL;
6936 if (iverdaux->vda_next
6937 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
6938 goto error_return_verdef;
6940 everdaux = ((Elf_External_Verdaux *)
6941 ((bfd_byte *) everdaux + iverdaux->vda_next));
6944 if (iverdef->vd_cnt)
6945 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
6947 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
6948 iverdef->vd_nextdef = iverdef + 1;
6949 else
6950 iverdef->vd_nextdef = NULL;
6952 everdef = ((Elf_External_Verdef *)
6953 ((bfd_byte *) everdef + iverdef->vd_next));
6956 free (contents);
6957 contents = NULL;
6959 else if (default_imported_symver)
6961 if (freeidx < 3)
6962 freeidx = 3;
6963 else
6964 freeidx++;
6966 elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, freeidx,
6967 sizeof (Elf_Internal_Verdef));
6968 if (elf_tdata (abfd)->verdef == NULL)
6969 goto error_return;
6971 elf_tdata (abfd)->cverdefs = freeidx;
6974 /* Create a default version based on the soname. */
6975 if (default_imported_symver)
6977 Elf_Internal_Verdef *iverdef;
6978 Elf_Internal_Verdaux *iverdaux;
6980 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];;
6982 iverdef->vd_version = VER_DEF_CURRENT;
6983 iverdef->vd_flags = 0;
6984 iverdef->vd_ndx = freeidx;
6985 iverdef->vd_cnt = 1;
6987 iverdef->vd_bfd = abfd;
6989 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
6990 if (iverdef->vd_nodename == NULL)
6991 goto error_return_verdef;
6992 iverdef->vd_nextdef = NULL;
6993 iverdef->vd_auxptr = bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux));
6994 if (iverdef->vd_auxptr == NULL)
6995 goto error_return_verdef;
6997 iverdaux = iverdef->vd_auxptr;
6998 iverdaux->vda_nodename = iverdef->vd_nodename;
6999 iverdaux->vda_nextptr = NULL;
7002 return TRUE;
7004 error_return:
7005 if (contents != NULL)
7006 free (contents);
7007 return FALSE;
7010 asymbol *
7011 _bfd_elf_make_empty_symbol (bfd *abfd)
7013 elf_symbol_type *newsym;
7014 bfd_size_type amt = sizeof (elf_symbol_type);
7016 newsym = bfd_zalloc (abfd, amt);
7017 if (!newsym)
7018 return NULL;
7019 else
7021 newsym->symbol.the_bfd = abfd;
7022 return &newsym->symbol;
7026 void
7027 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
7028 asymbol *symbol,
7029 symbol_info *ret)
7031 bfd_symbol_info (symbol, ret);
7034 /* Return whether a symbol name implies a local symbol. Most targets
7035 use this function for the is_local_label_name entry point, but some
7036 override it. */
7038 bfd_boolean
7039 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
7040 const char *name)
7042 /* Normal local symbols start with ``.L''. */
7043 if (name[0] == '.' && name[1] == 'L')
7044 return TRUE;
7046 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7047 DWARF debugging symbols starting with ``..''. */
7048 if (name[0] == '.' && name[1] == '.')
7049 return TRUE;
7051 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7052 emitting DWARF debugging output. I suspect this is actually a
7053 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7054 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7055 underscore to be emitted on some ELF targets). For ease of use,
7056 we treat such symbols as local. */
7057 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
7058 return TRUE;
7060 return FALSE;
7063 alent *
7064 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
7065 asymbol *symbol ATTRIBUTE_UNUSED)
7067 abort ();
7068 return NULL;
7071 bfd_boolean
7072 _bfd_elf_set_arch_mach (bfd *abfd,
7073 enum bfd_architecture arch,
7074 unsigned long machine)
7076 /* If this isn't the right architecture for this backend, and this
7077 isn't the generic backend, fail. */
7078 if (arch != get_elf_backend_data (abfd)->arch
7079 && arch != bfd_arch_unknown
7080 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
7081 return FALSE;
7083 return bfd_default_set_arch_mach (abfd, arch, machine);
7086 /* Find the function to a particular section and offset,
7087 for error reporting. */
7089 static bfd_boolean
7090 elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
7091 asection *section,
7092 asymbol **symbols,
7093 bfd_vma offset,
7094 const char **filename_ptr,
7095 const char **functionname_ptr)
7097 const char *filename;
7098 asymbol *func, *file;
7099 bfd_vma low_func;
7100 asymbol **p;
7101 /* ??? Given multiple file symbols, it is impossible to reliably
7102 choose the right file name for global symbols. File symbols are
7103 local symbols, and thus all file symbols must sort before any
7104 global symbols. The ELF spec may be interpreted to say that a
7105 file symbol must sort before other local symbols, but currently
7106 ld -r doesn't do this. So, for ld -r output, it is possible to
7107 make a better choice of file name for local symbols by ignoring
7108 file symbols appearing after a given local symbol. */
7109 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
7111 filename = NULL;
7112 func = NULL;
7113 file = NULL;
7114 low_func = 0;
7115 state = nothing_seen;
7117 for (p = symbols; *p != NULL; p++)
7119 elf_symbol_type *q;
7121 q = (elf_symbol_type *) *p;
7123 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
7125 default:
7126 break;
7127 case STT_FILE:
7128 file = &q->symbol;
7129 if (state == symbol_seen)
7130 state = file_after_symbol_seen;
7131 continue;
7132 case STT_NOTYPE:
7133 case STT_FUNC:
7134 if (bfd_get_section (&q->symbol) == section
7135 && q->symbol.value >= low_func
7136 && q->symbol.value <= offset)
7138 func = (asymbol *) q;
7139 low_func = q->symbol.value;
7140 filename = NULL;
7141 if (file != NULL
7142 && (ELF_ST_BIND (q->internal_elf_sym.st_info) == STB_LOCAL
7143 || state != file_after_symbol_seen))
7144 filename = bfd_asymbol_name (file);
7146 break;
7148 if (state == nothing_seen)
7149 state = symbol_seen;
7152 if (func == NULL)
7153 return FALSE;
7155 if (filename_ptr)
7156 *filename_ptr = filename;
7157 if (functionname_ptr)
7158 *functionname_ptr = bfd_asymbol_name (func);
7160 return TRUE;
7163 /* Find the nearest line to a particular section and offset,
7164 for error reporting. */
7166 bfd_boolean
7167 _bfd_elf_find_nearest_line (bfd *abfd,
7168 asection *section,
7169 asymbol **symbols,
7170 bfd_vma offset,
7171 const char **filename_ptr,
7172 const char **functionname_ptr,
7173 unsigned int *line_ptr)
7175 bfd_boolean found;
7177 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
7178 filename_ptr, functionname_ptr,
7179 line_ptr))
7181 if (!*functionname_ptr)
7182 elf_find_function (abfd, section, symbols, offset,
7183 *filename_ptr ? NULL : filename_ptr,
7184 functionname_ptr);
7186 return TRUE;
7189 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
7190 filename_ptr, functionname_ptr,
7191 line_ptr, 0,
7192 &elf_tdata (abfd)->dwarf2_find_line_info))
7194 if (!*functionname_ptr)
7195 elf_find_function (abfd, section, symbols, offset,
7196 *filename_ptr ? NULL : filename_ptr,
7197 functionname_ptr);
7199 return TRUE;
7202 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
7203 &found, filename_ptr,
7204 functionname_ptr, line_ptr,
7205 &elf_tdata (abfd)->line_info))
7206 return FALSE;
7207 if (found && (*functionname_ptr || *line_ptr))
7208 return TRUE;
7210 if (symbols == NULL)
7211 return FALSE;
7213 if (! elf_find_function (abfd, section, symbols, offset,
7214 filename_ptr, functionname_ptr))
7215 return FALSE;
7217 *line_ptr = 0;
7218 return TRUE;
7221 /* Find the line for a symbol. */
7223 bfd_boolean
7224 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
7225 const char **filename_ptr, unsigned int *line_ptr)
7227 return _bfd_dwarf2_find_line (abfd, symbols, symbol,
7228 filename_ptr, line_ptr, 0,
7229 &elf_tdata (abfd)->dwarf2_find_line_info);
7232 /* After a call to bfd_find_nearest_line, successive calls to
7233 bfd_find_inliner_info can be used to get source information about
7234 each level of function inlining that terminated at the address
7235 passed to bfd_find_nearest_line. Currently this is only supported
7236 for DWARF2 with appropriate DWARF3 extensions. */
7238 bfd_boolean
7239 _bfd_elf_find_inliner_info (bfd *abfd,
7240 const char **filename_ptr,
7241 const char **functionname_ptr,
7242 unsigned int *line_ptr)
7244 bfd_boolean found;
7245 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
7246 functionname_ptr, line_ptr,
7247 & elf_tdata (abfd)->dwarf2_find_line_info);
7248 return found;
7252 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
7254 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7255 int ret = bed->s->sizeof_ehdr;
7257 if (!info->relocatable)
7259 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
7261 if (phdr_size == (bfd_size_type) -1)
7263 struct elf_segment_map *m;
7265 phdr_size = 0;
7266 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
7267 phdr_size += bed->s->sizeof_phdr;
7269 if (phdr_size == 0)
7270 phdr_size = get_program_header_size (abfd, info);
7273 elf_tdata (abfd)->program_header_size = phdr_size;
7274 ret += phdr_size;
7277 return ret;
7280 bfd_boolean
7281 _bfd_elf_set_section_contents (bfd *abfd,
7282 sec_ptr section,
7283 const void *location,
7284 file_ptr offset,
7285 bfd_size_type count)
7287 Elf_Internal_Shdr *hdr;
7288 bfd_signed_vma pos;
7290 if (! abfd->output_has_begun
7291 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7292 return FALSE;
7294 hdr = &elf_section_data (section)->this_hdr;
7295 pos = hdr->sh_offset + offset;
7296 if (bfd_seek (abfd, pos, SEEK_SET) != 0
7297 || bfd_bwrite (location, count, abfd) != count)
7298 return FALSE;
7300 return TRUE;
7303 void
7304 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
7305 arelent *cache_ptr ATTRIBUTE_UNUSED,
7306 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
7308 abort ();
7311 /* Try to convert a non-ELF reloc into an ELF one. */
7313 bfd_boolean
7314 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
7316 /* Check whether we really have an ELF howto. */
7318 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
7320 bfd_reloc_code_real_type code;
7321 reloc_howto_type *howto;
7323 /* Alien reloc: Try to determine its type to replace it with an
7324 equivalent ELF reloc. */
7326 if (areloc->howto->pc_relative)
7328 switch (areloc->howto->bitsize)
7330 case 8:
7331 code = BFD_RELOC_8_PCREL;
7332 break;
7333 case 12:
7334 code = BFD_RELOC_12_PCREL;
7335 break;
7336 case 16:
7337 code = BFD_RELOC_16_PCREL;
7338 break;
7339 case 24:
7340 code = BFD_RELOC_24_PCREL;
7341 break;
7342 case 32:
7343 code = BFD_RELOC_32_PCREL;
7344 break;
7345 case 64:
7346 code = BFD_RELOC_64_PCREL;
7347 break;
7348 default:
7349 goto fail;
7352 howto = bfd_reloc_type_lookup (abfd, code);
7354 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
7356 if (howto->pcrel_offset)
7357 areloc->addend += areloc->address;
7358 else
7359 areloc->addend -= areloc->address; /* addend is unsigned!! */
7362 else
7364 switch (areloc->howto->bitsize)
7366 case 8:
7367 code = BFD_RELOC_8;
7368 break;
7369 case 14:
7370 code = BFD_RELOC_14;
7371 break;
7372 case 16:
7373 code = BFD_RELOC_16;
7374 break;
7375 case 26:
7376 code = BFD_RELOC_26;
7377 break;
7378 case 32:
7379 code = BFD_RELOC_32;
7380 break;
7381 case 64:
7382 code = BFD_RELOC_64;
7383 break;
7384 default:
7385 goto fail;
7388 howto = bfd_reloc_type_lookup (abfd, code);
7391 if (howto)
7392 areloc->howto = howto;
7393 else
7394 goto fail;
7397 return TRUE;
7399 fail:
7400 (*_bfd_error_handler)
7401 (_("%B: unsupported relocation type %s"),
7402 abfd, areloc->howto->name);
7403 bfd_set_error (bfd_error_bad_value);
7404 return FALSE;
7407 bfd_boolean
7408 _bfd_elf_close_and_cleanup (bfd *abfd)
7410 if (bfd_get_format (abfd) == bfd_object)
7412 if (elf_tdata (abfd) != NULL && elf_shstrtab (abfd) != NULL)
7413 _bfd_elf_strtab_free (elf_shstrtab (abfd));
7414 _bfd_dwarf2_cleanup_debug_info (abfd);
7417 return _bfd_generic_close_and_cleanup (abfd);
7420 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7421 in the relocation's offset. Thus we cannot allow any sort of sanity
7422 range-checking to interfere. There is nothing else to do in processing
7423 this reloc. */
7425 bfd_reloc_status_type
7426 _bfd_elf_rel_vtable_reloc_fn
7427 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
7428 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
7429 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
7430 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
7432 return bfd_reloc_ok;
7435 /* Elf core file support. Much of this only works on native
7436 toolchains, since we rely on knowing the
7437 machine-dependent procfs structure in order to pick
7438 out details about the corefile. */
7440 #ifdef HAVE_SYS_PROCFS_H
7441 # include <sys/procfs.h>
7442 #endif
7444 /* FIXME: this is kinda wrong, but it's what gdb wants. */
7446 static int
7447 elfcore_make_pid (bfd *abfd)
7449 return ((elf_tdata (abfd)->core_lwpid << 16)
7450 + (elf_tdata (abfd)->core_pid));
7453 /* If there isn't a section called NAME, make one, using
7454 data from SECT. Note, this function will generate a
7455 reference to NAME, so you shouldn't deallocate or
7456 overwrite it. */
7458 static bfd_boolean
7459 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
7461 asection *sect2;
7463 if (bfd_get_section_by_name (abfd, name) != NULL)
7464 return TRUE;
7466 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
7467 if (sect2 == NULL)
7468 return FALSE;
7470 sect2->size = sect->size;
7471 sect2->filepos = sect->filepos;
7472 sect2->alignment_power = sect->alignment_power;
7473 return TRUE;
7476 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
7477 actually creates up to two pseudosections:
7478 - For the single-threaded case, a section named NAME, unless
7479 such a section already exists.
7480 - For the multi-threaded case, a section named "NAME/PID", where
7481 PID is elfcore_make_pid (abfd).
7482 Both pseudosections have identical contents. */
7483 bfd_boolean
7484 _bfd_elfcore_make_pseudosection (bfd *abfd,
7485 char *name,
7486 size_t size,
7487 ufile_ptr filepos)
7489 char buf[100];
7490 char *threaded_name;
7491 size_t len;
7492 asection *sect;
7494 /* Build the section name. */
7496 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
7497 len = strlen (buf) + 1;
7498 threaded_name = bfd_alloc (abfd, len);
7499 if (threaded_name == NULL)
7500 return FALSE;
7501 memcpy (threaded_name, buf, len);
7503 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
7504 SEC_HAS_CONTENTS);
7505 if (sect == NULL)
7506 return FALSE;
7507 sect->size = size;
7508 sect->filepos = filepos;
7509 sect->alignment_power = 2;
7511 return elfcore_maybe_make_sect (abfd, name, sect);
7514 /* prstatus_t exists on:
7515 solaris 2.5+
7516 linux 2.[01] + glibc
7517 unixware 4.2
7520 #if defined (HAVE_PRSTATUS_T)
7522 static bfd_boolean
7523 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
7525 size_t size;
7526 int offset;
7528 if (note->descsz == sizeof (prstatus_t))
7530 prstatus_t prstat;
7532 size = sizeof (prstat.pr_reg);
7533 offset = offsetof (prstatus_t, pr_reg);
7534 memcpy (&prstat, note->descdata, sizeof (prstat));
7536 /* Do not overwrite the core signal if it
7537 has already been set by another thread. */
7538 if (elf_tdata (abfd)->core_signal == 0)
7539 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7540 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7542 /* pr_who exists on:
7543 solaris 2.5+
7544 unixware 4.2
7545 pr_who doesn't exist on:
7546 linux 2.[01]
7548 #if defined (HAVE_PRSTATUS_T_PR_WHO)
7549 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7550 #endif
7552 #if defined (HAVE_PRSTATUS32_T)
7553 else if (note->descsz == sizeof (prstatus32_t))
7555 /* 64-bit host, 32-bit corefile */
7556 prstatus32_t prstat;
7558 size = sizeof (prstat.pr_reg);
7559 offset = offsetof (prstatus32_t, pr_reg);
7560 memcpy (&prstat, note->descdata, sizeof (prstat));
7562 /* Do not overwrite the core signal if it
7563 has already been set by another thread. */
7564 if (elf_tdata (abfd)->core_signal == 0)
7565 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7566 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7568 /* pr_who exists on:
7569 solaris 2.5+
7570 unixware 4.2
7571 pr_who doesn't exist on:
7572 linux 2.[01]
7574 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
7575 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7576 #endif
7578 #endif /* HAVE_PRSTATUS32_T */
7579 else
7581 /* Fail - we don't know how to handle any other
7582 note size (ie. data object type). */
7583 return TRUE;
7586 /* Make a ".reg/999" section and a ".reg" section. */
7587 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
7588 size, note->descpos + offset);
7590 #endif /* defined (HAVE_PRSTATUS_T) */
7592 /* Create a pseudosection containing the exact contents of NOTE. */
7593 static bfd_boolean
7594 elfcore_make_note_pseudosection (bfd *abfd,
7595 char *name,
7596 Elf_Internal_Note *note)
7598 return _bfd_elfcore_make_pseudosection (abfd, name,
7599 note->descsz, note->descpos);
7602 /* There isn't a consistent prfpregset_t across platforms,
7603 but it doesn't matter, because we don't have to pick this
7604 data structure apart. */
7606 static bfd_boolean
7607 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
7609 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7612 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
7613 type of 5 (NT_PRXFPREG). Just include the whole note's contents
7614 literally. */
7616 static bfd_boolean
7617 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
7619 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
7622 #if defined (HAVE_PRPSINFO_T)
7623 typedef prpsinfo_t elfcore_psinfo_t;
7624 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
7625 typedef prpsinfo32_t elfcore_psinfo32_t;
7626 #endif
7627 #endif
7629 #if defined (HAVE_PSINFO_T)
7630 typedef psinfo_t elfcore_psinfo_t;
7631 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
7632 typedef psinfo32_t elfcore_psinfo32_t;
7633 #endif
7634 #endif
7636 /* return a malloc'ed copy of a string at START which is at
7637 most MAX bytes long, possibly without a terminating '\0'.
7638 the copy will always have a terminating '\0'. */
7640 char *
7641 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
7643 char *dups;
7644 char *end = memchr (start, '\0', max);
7645 size_t len;
7647 if (end == NULL)
7648 len = max;
7649 else
7650 len = end - start;
7652 dups = bfd_alloc (abfd, len + 1);
7653 if (dups == NULL)
7654 return NULL;
7656 memcpy (dups, start, len);
7657 dups[len] = '\0';
7659 return dups;
7662 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7663 static bfd_boolean
7664 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
7666 if (note->descsz == sizeof (elfcore_psinfo_t))
7668 elfcore_psinfo_t psinfo;
7670 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7672 elf_tdata (abfd)->core_program
7673 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7674 sizeof (psinfo.pr_fname));
7676 elf_tdata (abfd)->core_command
7677 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7678 sizeof (psinfo.pr_psargs));
7680 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
7681 else if (note->descsz == sizeof (elfcore_psinfo32_t))
7683 /* 64-bit host, 32-bit corefile */
7684 elfcore_psinfo32_t psinfo;
7686 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7688 elf_tdata (abfd)->core_program
7689 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7690 sizeof (psinfo.pr_fname));
7692 elf_tdata (abfd)->core_command
7693 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7694 sizeof (psinfo.pr_psargs));
7696 #endif
7698 else
7700 /* Fail - we don't know how to handle any other
7701 note size (ie. data object type). */
7702 return TRUE;
7705 /* Note that for some reason, a spurious space is tacked
7706 onto the end of the args in some (at least one anyway)
7707 implementations, so strip it off if it exists. */
7710 char *command = elf_tdata (abfd)->core_command;
7711 int n = strlen (command);
7713 if (0 < n && command[n - 1] == ' ')
7714 command[n - 1] = '\0';
7717 return TRUE;
7719 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
7721 #if defined (HAVE_PSTATUS_T)
7722 static bfd_boolean
7723 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
7725 if (note->descsz == sizeof (pstatus_t)
7726 #if defined (HAVE_PXSTATUS_T)
7727 || note->descsz == sizeof (pxstatus_t)
7728 #endif
7731 pstatus_t pstat;
7733 memcpy (&pstat, note->descdata, sizeof (pstat));
7735 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7737 #if defined (HAVE_PSTATUS32_T)
7738 else if (note->descsz == sizeof (pstatus32_t))
7740 /* 64-bit host, 32-bit corefile */
7741 pstatus32_t pstat;
7743 memcpy (&pstat, note->descdata, sizeof (pstat));
7745 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7747 #endif
7748 /* Could grab some more details from the "representative"
7749 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
7750 NT_LWPSTATUS note, presumably. */
7752 return TRUE;
7754 #endif /* defined (HAVE_PSTATUS_T) */
7756 #if defined (HAVE_LWPSTATUS_T)
7757 static bfd_boolean
7758 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
7760 lwpstatus_t lwpstat;
7761 char buf[100];
7762 char *name;
7763 size_t len;
7764 asection *sect;
7766 if (note->descsz != sizeof (lwpstat)
7767 #if defined (HAVE_LWPXSTATUS_T)
7768 && note->descsz != sizeof (lwpxstatus_t)
7769 #endif
7771 return TRUE;
7773 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
7775 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
7776 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
7778 /* Make a ".reg/999" section. */
7780 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
7781 len = strlen (buf) + 1;
7782 name = bfd_alloc (abfd, len);
7783 if (name == NULL)
7784 return FALSE;
7785 memcpy (name, buf, len);
7787 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7788 if (sect == NULL)
7789 return FALSE;
7791 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7792 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
7793 sect->filepos = note->descpos
7794 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
7795 #endif
7797 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7798 sect->size = sizeof (lwpstat.pr_reg);
7799 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
7800 #endif
7802 sect->alignment_power = 2;
7804 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
7805 return FALSE;
7807 /* Make a ".reg2/999" section */
7809 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
7810 len = strlen (buf) + 1;
7811 name = bfd_alloc (abfd, len);
7812 if (name == NULL)
7813 return FALSE;
7814 memcpy (name, buf, len);
7816 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7817 if (sect == NULL)
7818 return FALSE;
7820 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7821 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
7822 sect->filepos = note->descpos
7823 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
7824 #endif
7826 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
7827 sect->size = sizeof (lwpstat.pr_fpreg);
7828 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
7829 #endif
7831 sect->alignment_power = 2;
7833 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
7835 #endif /* defined (HAVE_LWPSTATUS_T) */
7837 #if defined (HAVE_WIN32_PSTATUS_T)
7838 static bfd_boolean
7839 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
7841 char buf[30];
7842 char *name;
7843 size_t len;
7844 asection *sect;
7845 win32_pstatus_t pstatus;
7847 if (note->descsz < sizeof (pstatus))
7848 return TRUE;
7850 memcpy (&pstatus, note->descdata, sizeof (pstatus));
7852 switch (pstatus.data_type)
7854 case NOTE_INFO_PROCESS:
7855 /* FIXME: need to add ->core_command. */
7856 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
7857 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
7858 break;
7860 case NOTE_INFO_THREAD:
7861 /* Make a ".reg/999" section. */
7862 sprintf (buf, ".reg/%ld", (long) pstatus.data.thread_info.tid);
7864 len = strlen (buf) + 1;
7865 name = bfd_alloc (abfd, len);
7866 if (name == NULL)
7867 return FALSE;
7869 memcpy (name, buf, len);
7871 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7872 if (sect == NULL)
7873 return FALSE;
7875 sect->size = sizeof (pstatus.data.thread_info.thread_context);
7876 sect->filepos = (note->descpos
7877 + offsetof (struct win32_pstatus,
7878 data.thread_info.thread_context));
7879 sect->alignment_power = 2;
7881 if (pstatus.data.thread_info.is_active_thread)
7882 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
7883 return FALSE;
7884 break;
7886 case NOTE_INFO_MODULE:
7887 /* Make a ".module/xxxxxxxx" section. */
7888 sprintf (buf, ".module/%08lx",
7889 (long) pstatus.data.module_info.base_address);
7891 len = strlen (buf) + 1;
7892 name = bfd_alloc (abfd, len);
7893 if (name == NULL)
7894 return FALSE;
7896 memcpy (name, buf, len);
7898 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7900 if (sect == NULL)
7901 return FALSE;
7903 sect->size = note->descsz;
7904 sect->filepos = note->descpos;
7905 sect->alignment_power = 2;
7906 break;
7908 default:
7909 return TRUE;
7912 return TRUE;
7914 #endif /* HAVE_WIN32_PSTATUS_T */
7916 static bfd_boolean
7917 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
7919 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7921 switch (note->type)
7923 default:
7924 return TRUE;
7926 case NT_PRSTATUS:
7927 if (bed->elf_backend_grok_prstatus)
7928 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
7929 return TRUE;
7930 #if defined (HAVE_PRSTATUS_T)
7931 return elfcore_grok_prstatus (abfd, note);
7932 #else
7933 return TRUE;
7934 #endif
7936 #if defined (HAVE_PSTATUS_T)
7937 case NT_PSTATUS:
7938 return elfcore_grok_pstatus (abfd, note);
7939 #endif
7941 #if defined (HAVE_LWPSTATUS_T)
7942 case NT_LWPSTATUS:
7943 return elfcore_grok_lwpstatus (abfd, note);
7944 #endif
7946 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
7947 return elfcore_grok_prfpreg (abfd, note);
7949 #if defined (HAVE_WIN32_PSTATUS_T)
7950 case NT_WIN32PSTATUS:
7951 return elfcore_grok_win32pstatus (abfd, note);
7952 #endif
7954 case NT_PRXFPREG: /* Linux SSE extension */
7955 if (note->namesz == 6
7956 && strcmp (note->namedata, "LINUX") == 0)
7957 return elfcore_grok_prxfpreg (abfd, note);
7958 else
7959 return TRUE;
7961 case NT_PRPSINFO:
7962 case NT_PSINFO:
7963 if (bed->elf_backend_grok_psinfo)
7964 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
7965 return TRUE;
7966 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7967 return elfcore_grok_psinfo (abfd, note);
7968 #else
7969 return TRUE;
7970 #endif
7972 case NT_AUXV:
7974 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
7975 SEC_HAS_CONTENTS);
7977 if (sect == NULL)
7978 return FALSE;
7979 sect->size = note->descsz;
7980 sect->filepos = note->descpos;
7981 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
7983 return TRUE;
7988 static bfd_boolean
7989 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
7991 char *cp;
7993 cp = strchr (note->namedata, '@');
7994 if (cp != NULL)
7996 *lwpidp = atoi(cp + 1);
7997 return TRUE;
7999 return FALSE;
8002 static bfd_boolean
8003 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8005 /* Signal number at offset 0x08. */
8006 elf_tdata (abfd)->core_signal
8007 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8009 /* Process ID at offset 0x50. */
8010 elf_tdata (abfd)->core_pid
8011 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
8013 /* Command name at 0x7c (max 32 bytes, including nul). */
8014 elf_tdata (abfd)->core_command
8015 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
8017 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
8018 note);
8021 static bfd_boolean
8022 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
8024 int lwp;
8026 if (elfcore_netbsd_get_lwpid (note, &lwp))
8027 elf_tdata (abfd)->core_lwpid = lwp;
8029 if (note->type == NT_NETBSDCORE_PROCINFO)
8031 /* NetBSD-specific core "procinfo". Note that we expect to
8032 find this note before any of the others, which is fine,
8033 since the kernel writes this note out first when it
8034 creates a core file. */
8036 return elfcore_grok_netbsd_procinfo (abfd, note);
8039 /* As of Jan 2002 there are no other machine-independent notes
8040 defined for NetBSD core files. If the note type is less
8041 than the start of the machine-dependent note types, we don't
8042 understand it. */
8044 if (note->type < NT_NETBSDCORE_FIRSTMACH)
8045 return TRUE;
8048 switch (bfd_get_arch (abfd))
8050 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8051 PT_GETFPREGS == mach+2. */
8053 case bfd_arch_alpha:
8054 case bfd_arch_sparc:
8055 switch (note->type)
8057 case NT_NETBSDCORE_FIRSTMACH+0:
8058 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8060 case NT_NETBSDCORE_FIRSTMACH+2:
8061 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8063 default:
8064 return TRUE;
8067 /* On all other arch's, PT_GETREGS == mach+1 and
8068 PT_GETFPREGS == mach+3. */
8070 default:
8071 switch (note->type)
8073 case NT_NETBSDCORE_FIRSTMACH+1:
8074 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8076 case NT_NETBSDCORE_FIRSTMACH+3:
8077 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8079 default:
8080 return TRUE;
8083 /* NOTREACHED */
8086 static bfd_boolean
8087 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
8089 void *ddata = note->descdata;
8090 char buf[100];
8091 char *name;
8092 asection *sect;
8093 short sig;
8094 unsigned flags;
8096 /* nto_procfs_status 'pid' field is at offset 0. */
8097 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
8099 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
8100 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
8102 /* nto_procfs_status 'flags' field is at offset 8. */
8103 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
8105 /* nto_procfs_status 'what' field is at offset 14. */
8106 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
8108 elf_tdata (abfd)->core_signal = sig;
8109 elf_tdata (abfd)->core_lwpid = *tid;
8112 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
8113 do not come from signals so we make sure we set the current
8114 thread just in case. */
8115 if (flags & 0x00000080)
8116 elf_tdata (abfd)->core_lwpid = *tid;
8118 /* Make a ".qnx_core_status/%d" section. */
8119 sprintf (buf, ".qnx_core_status/%ld", *tid);
8121 name = bfd_alloc (abfd, strlen (buf) + 1);
8122 if (name == NULL)
8123 return FALSE;
8124 strcpy (name, buf);
8126 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8127 if (sect == NULL)
8128 return FALSE;
8130 sect->size = note->descsz;
8131 sect->filepos = note->descpos;
8132 sect->alignment_power = 2;
8134 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
8137 static bfd_boolean
8138 elfcore_grok_nto_regs (bfd *abfd,
8139 Elf_Internal_Note *note,
8140 long tid,
8141 char *base)
8143 char buf[100];
8144 char *name;
8145 asection *sect;
8147 /* Make a "(base)/%d" section. */
8148 sprintf (buf, "%s/%ld", base, tid);
8150 name = bfd_alloc (abfd, strlen (buf) + 1);
8151 if (name == NULL)
8152 return FALSE;
8153 strcpy (name, buf);
8155 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8156 if (sect == NULL)
8157 return FALSE;
8159 sect->size = note->descsz;
8160 sect->filepos = note->descpos;
8161 sect->alignment_power = 2;
8163 /* This is the current thread. */
8164 if (elf_tdata (abfd)->core_lwpid == tid)
8165 return elfcore_maybe_make_sect (abfd, base, sect);
8167 return TRUE;
8170 #define BFD_QNT_CORE_INFO 7
8171 #define BFD_QNT_CORE_STATUS 8
8172 #define BFD_QNT_CORE_GREG 9
8173 #define BFD_QNT_CORE_FPREG 10
8175 static bfd_boolean
8176 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
8178 /* Every GREG section has a STATUS section before it. Store the
8179 tid from the previous call to pass down to the next gregs
8180 function. */
8181 static long tid = 1;
8183 switch (note->type)
8185 case BFD_QNT_CORE_INFO:
8186 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
8187 case BFD_QNT_CORE_STATUS:
8188 return elfcore_grok_nto_status (abfd, note, &tid);
8189 case BFD_QNT_CORE_GREG:
8190 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
8191 case BFD_QNT_CORE_FPREG:
8192 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
8193 default:
8194 return TRUE;
8198 /* Function: elfcore_write_note
8200 Inputs:
8201 buffer to hold note, and current size of buffer
8202 name of note
8203 type of note
8204 data for note
8205 size of data for note
8207 Writes note to end of buffer. ELF64 notes are written exactly as
8208 for ELF32, despite the current (as of 2006) ELF gabi specifying
8209 that they ought to have 8-byte namesz and descsz field, and have
8210 8-byte alignment. Other writers, eg. Linux kernel, do the same.
8212 Return:
8213 Pointer to realloc'd buffer, *BUFSIZ updated. */
8215 char *
8216 elfcore_write_note (bfd *abfd,
8217 char *buf,
8218 int *bufsiz,
8219 const char *name,
8220 int type,
8221 const void *input,
8222 int size)
8224 Elf_External_Note *xnp;
8225 size_t namesz;
8226 size_t newspace;
8227 char *dest;
8229 namesz = 0;
8230 if (name != NULL)
8231 namesz = strlen (name) + 1;
8233 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
8235 buf = realloc (buf, *bufsiz + newspace);
8236 dest = buf + *bufsiz;
8237 *bufsiz += newspace;
8238 xnp = (Elf_External_Note *) dest;
8239 H_PUT_32 (abfd, namesz, xnp->namesz);
8240 H_PUT_32 (abfd, size, xnp->descsz);
8241 H_PUT_32 (abfd, type, xnp->type);
8242 dest = xnp->name;
8243 if (name != NULL)
8245 memcpy (dest, name, namesz);
8246 dest += namesz;
8247 while (namesz & 3)
8249 *dest++ = '\0';
8250 ++namesz;
8253 memcpy (dest, input, size);
8254 dest += size;
8255 while (size & 3)
8257 *dest++ = '\0';
8258 ++size;
8260 return buf;
8263 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8264 char *
8265 elfcore_write_prpsinfo (bfd *abfd,
8266 char *buf,
8267 int *bufsiz,
8268 const char *fname,
8269 const char *psargs)
8271 const char *note_name = "CORE";
8272 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8274 if (bed->elf_backend_write_core_note != NULL)
8276 char *ret;
8277 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8278 NT_PRPSINFO, fname, psargs);
8279 if (ret != NULL)
8280 return ret;
8283 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8284 if (bed->s->elfclass == ELFCLASS32)
8286 #if defined (HAVE_PSINFO32_T)
8287 psinfo32_t data;
8288 int note_type = NT_PSINFO;
8289 #else
8290 prpsinfo32_t data;
8291 int note_type = NT_PRPSINFO;
8292 #endif
8294 memset (&data, 0, sizeof (data));
8295 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8296 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8297 return elfcore_write_note (abfd, buf, bufsiz,
8298 note_name, note_type, &data, sizeof (data));
8300 else
8301 #endif
8303 #if defined (HAVE_PSINFO_T)
8304 psinfo_t data;
8305 int note_type = NT_PSINFO;
8306 #else
8307 prpsinfo_t data;
8308 int note_type = NT_PRPSINFO;
8309 #endif
8311 memset (&data, 0, sizeof (data));
8312 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8313 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8314 return elfcore_write_note (abfd, buf, bufsiz,
8315 note_name, note_type, &data, sizeof (data));
8318 #endif /* PSINFO_T or PRPSINFO_T */
8320 #if defined (HAVE_PRSTATUS_T)
8321 char *
8322 elfcore_write_prstatus (bfd *abfd,
8323 char *buf,
8324 int *bufsiz,
8325 long pid,
8326 int cursig,
8327 const void *gregs)
8329 const char *note_name = "CORE";
8330 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8332 if (bed->elf_backend_write_core_note != NULL)
8334 char *ret;
8335 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8336 NT_PRSTATUS,
8337 pid, cursig, gregs);
8338 if (ret != NULL)
8339 return ret;
8342 #if defined (HAVE_PRSTATUS32_T)
8343 if (bed->s->elfclass == ELFCLASS32)
8345 prstatus32_t prstat;
8347 memset (&prstat, 0, sizeof (prstat));
8348 prstat.pr_pid = pid;
8349 prstat.pr_cursig = cursig;
8350 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8351 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8352 NT_PRSTATUS, &prstat, sizeof (prstat));
8354 else
8355 #endif
8357 prstatus_t prstat;
8359 memset (&prstat, 0, sizeof (prstat));
8360 prstat.pr_pid = pid;
8361 prstat.pr_cursig = cursig;
8362 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8363 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8364 NT_PRSTATUS, &prstat, sizeof (prstat));
8367 #endif /* HAVE_PRSTATUS_T */
8369 #if defined (HAVE_LWPSTATUS_T)
8370 char *
8371 elfcore_write_lwpstatus (bfd *abfd,
8372 char *buf,
8373 int *bufsiz,
8374 long pid,
8375 int cursig,
8376 const void *gregs)
8378 lwpstatus_t lwpstat;
8379 const char *note_name = "CORE";
8381 memset (&lwpstat, 0, sizeof (lwpstat));
8382 lwpstat.pr_lwpid = pid >> 16;
8383 lwpstat.pr_cursig = cursig;
8384 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8385 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
8386 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8387 #if !defined(gregs)
8388 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
8389 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
8390 #else
8391 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
8392 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
8393 #endif
8394 #endif
8395 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8396 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
8398 #endif /* HAVE_LWPSTATUS_T */
8400 #if defined (HAVE_PSTATUS_T)
8401 char *
8402 elfcore_write_pstatus (bfd *abfd,
8403 char *buf,
8404 int *bufsiz,
8405 long pid,
8406 int cursig ATTRIBUTE_UNUSED,
8407 const void *gregs ATTRIBUTE_UNUSED)
8409 const char *note_name = "CORE";
8410 #if defined (HAVE_PSTATUS32_T)
8411 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8413 if (bed->s->elfclass == ELFCLASS32)
8415 pstatus32_t pstat;
8417 memset (&pstat, 0, sizeof (pstat));
8418 pstat.pr_pid = pid & 0xffff;
8419 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8420 NT_PSTATUS, &pstat, sizeof (pstat));
8421 return buf;
8423 else
8424 #endif
8426 pstatus_t pstat;
8428 memset (&pstat, 0, sizeof (pstat));
8429 pstat.pr_pid = pid & 0xffff;
8430 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8431 NT_PSTATUS, &pstat, sizeof (pstat));
8432 return buf;
8435 #endif /* HAVE_PSTATUS_T */
8437 char *
8438 elfcore_write_prfpreg (bfd *abfd,
8439 char *buf,
8440 int *bufsiz,
8441 const void *fpregs,
8442 int size)
8444 const char *note_name = "CORE";
8445 return elfcore_write_note (abfd, buf, bufsiz,
8446 note_name, NT_FPREGSET, fpregs, size);
8449 char *
8450 elfcore_write_prxfpreg (bfd *abfd,
8451 char *buf,
8452 int *bufsiz,
8453 const void *xfpregs,
8454 int size)
8456 char *note_name = "LINUX";
8457 return elfcore_write_note (abfd, buf, bufsiz,
8458 note_name, NT_PRXFPREG, xfpregs, size);
8461 static bfd_boolean
8462 elfcore_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
8464 char *buf;
8465 char *p;
8467 if (size <= 0)
8468 return TRUE;
8470 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
8471 return FALSE;
8473 buf = bfd_malloc (size);
8474 if (buf == NULL)
8475 return FALSE;
8477 if (bfd_bread (buf, size, abfd) != size)
8479 error:
8480 free (buf);
8481 return FALSE;
8484 p = buf;
8485 while (p < buf + size)
8487 /* FIXME: bad alignment assumption. */
8488 Elf_External_Note *xnp = (Elf_External_Note *) p;
8489 Elf_Internal_Note in;
8491 in.type = H_GET_32 (abfd, xnp->type);
8493 in.namesz = H_GET_32 (abfd, xnp->namesz);
8494 in.namedata = xnp->name;
8496 in.descsz = H_GET_32 (abfd, xnp->descsz);
8497 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
8498 in.descpos = offset + (in.descdata - buf);
8500 if (CONST_STRNEQ (in.namedata, "NetBSD-CORE"))
8502 if (! elfcore_grok_netbsd_note (abfd, &in))
8503 goto error;
8505 else if (CONST_STRNEQ (in.namedata, "QNX"))
8507 if (! elfcore_grok_nto_note (abfd, &in))
8508 goto error;
8510 else
8512 if (! elfcore_grok_note (abfd, &in))
8513 goto error;
8516 p = in.descdata + BFD_ALIGN (in.descsz, 4);
8519 free (buf);
8520 return TRUE;
8523 /* Providing external access to the ELF program header table. */
8525 /* Return an upper bound on the number of bytes required to store a
8526 copy of ABFD's program header table entries. Return -1 if an error
8527 occurs; bfd_get_error will return an appropriate code. */
8529 long
8530 bfd_get_elf_phdr_upper_bound (bfd *abfd)
8532 if (abfd->xvec->flavour != bfd_target_elf_flavour)
8534 bfd_set_error (bfd_error_wrong_format);
8535 return -1;
8538 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
8541 /* Copy ABFD's program header table entries to *PHDRS. The entries
8542 will be stored as an array of Elf_Internal_Phdr structures, as
8543 defined in include/elf/internal.h. To find out how large the
8544 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
8546 Return the number of program header table entries read, or -1 if an
8547 error occurs; bfd_get_error will return an appropriate code. */
8550 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
8552 int num_phdrs;
8554 if (abfd->xvec->flavour != bfd_target_elf_flavour)
8556 bfd_set_error (bfd_error_wrong_format);
8557 return -1;
8560 num_phdrs = elf_elfheader (abfd)->e_phnum;
8561 memcpy (phdrs, elf_tdata (abfd)->phdr,
8562 num_phdrs * sizeof (Elf_Internal_Phdr));
8564 return num_phdrs;
8567 void
8568 _bfd_elf_sprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, char *buf, bfd_vma value)
8570 #ifdef BFD64
8571 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
8573 i_ehdrp = elf_elfheader (abfd);
8574 if (i_ehdrp == NULL)
8575 sprintf_vma (buf, value);
8576 else
8578 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
8580 #if BFD_HOST_64BIT_LONG
8581 sprintf (buf, "%016lx", value);
8582 #else
8583 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
8584 _bfd_int64_low (value));
8585 #endif
8587 else
8588 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
8590 #else
8591 sprintf_vma (buf, value);
8592 #endif
8595 void
8596 _bfd_elf_fprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, void *stream, bfd_vma value)
8598 #ifdef BFD64
8599 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
8601 i_ehdrp = elf_elfheader (abfd);
8602 if (i_ehdrp == NULL)
8603 fprintf_vma ((FILE *) stream, value);
8604 else
8606 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
8608 #if BFD_HOST_64BIT_LONG
8609 fprintf ((FILE *) stream, "%016lx", value);
8610 #else
8611 fprintf ((FILE *) stream, "%08lx%08lx",
8612 _bfd_int64_high (value), _bfd_int64_low (value));
8613 #endif
8615 else
8616 fprintf ((FILE *) stream, "%08lx",
8617 (unsigned long) (value & 0xffffffff));
8619 #else
8620 fprintf_vma ((FILE *) stream, value);
8621 #endif
8624 enum elf_reloc_type_class
8625 _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
8627 return reloc_class_normal;
8630 /* For RELA architectures, return the relocation value for a
8631 relocation against a local symbol. */
8633 bfd_vma
8634 _bfd_elf_rela_local_sym (bfd *abfd,
8635 Elf_Internal_Sym *sym,
8636 asection **psec,
8637 Elf_Internal_Rela *rel)
8639 asection *sec = *psec;
8640 bfd_vma relocation;
8642 relocation = (sec->output_section->vma
8643 + sec->output_offset
8644 + sym->st_value);
8645 if ((sec->flags & SEC_MERGE)
8646 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
8647 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
8649 rel->r_addend =
8650 _bfd_merged_section_offset (abfd, psec,
8651 elf_section_data (sec)->sec_info,
8652 sym->st_value + rel->r_addend);
8653 if (sec != *psec)
8655 /* If we have changed the section, and our original section is
8656 marked with SEC_EXCLUDE, it means that the original
8657 SEC_MERGE section has been completely subsumed in some
8658 other SEC_MERGE section. In this case, we need to leave
8659 some info around for --emit-relocs. */
8660 if ((sec->flags & SEC_EXCLUDE) != 0)
8661 sec->kept_section = *psec;
8662 sec = *psec;
8664 rel->r_addend -= relocation;
8665 rel->r_addend += sec->output_section->vma + sec->output_offset;
8667 return relocation;
8670 bfd_vma
8671 _bfd_elf_rel_local_sym (bfd *abfd,
8672 Elf_Internal_Sym *sym,
8673 asection **psec,
8674 bfd_vma addend)
8676 asection *sec = *psec;
8678 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
8679 return sym->st_value + addend;
8681 return _bfd_merged_section_offset (abfd, psec,
8682 elf_section_data (sec)->sec_info,
8683 sym->st_value + addend);
8686 bfd_vma
8687 _bfd_elf_section_offset (bfd *abfd,
8688 struct bfd_link_info *info,
8689 asection *sec,
8690 bfd_vma offset)
8692 switch (sec->sec_info_type)
8694 case ELF_INFO_TYPE_STABS:
8695 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
8696 offset);
8697 case ELF_INFO_TYPE_EH_FRAME:
8698 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
8699 default:
8700 return offset;
8704 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
8705 reconstruct an ELF file by reading the segments out of remote memory
8706 based on the ELF file header at EHDR_VMA and the ELF program headers it
8707 points to. If not null, *LOADBASEP is filled in with the difference
8708 between the VMAs from which the segments were read, and the VMAs the
8709 file headers (and hence BFD's idea of each section's VMA) put them at.
8711 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
8712 remote memory at target address VMA into the local buffer at MYADDR; it
8713 should return zero on success or an `errno' code on failure. TEMPL must
8714 be a BFD for an ELF target with the word size and byte order found in
8715 the remote memory. */
8717 bfd *
8718 bfd_elf_bfd_from_remote_memory
8719 (bfd *templ,
8720 bfd_vma ehdr_vma,
8721 bfd_vma *loadbasep,
8722 int (*target_read_memory) (bfd_vma, bfd_byte *, int))
8724 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
8725 (templ, ehdr_vma, loadbasep, target_read_memory);
8728 long
8729 _bfd_elf_get_synthetic_symtab (bfd *abfd,
8730 long symcount ATTRIBUTE_UNUSED,
8731 asymbol **syms ATTRIBUTE_UNUSED,
8732 long dynsymcount,
8733 asymbol **dynsyms,
8734 asymbol **ret)
8736 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8737 asection *relplt;
8738 asymbol *s;
8739 const char *relplt_name;
8740 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
8741 arelent *p;
8742 long count, i, n;
8743 size_t size;
8744 Elf_Internal_Shdr *hdr;
8745 char *names;
8746 asection *plt;
8748 *ret = NULL;
8750 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
8751 return 0;
8753 if (dynsymcount <= 0)
8754 return 0;
8756 if (!bed->plt_sym_val)
8757 return 0;
8759 relplt_name = bed->relplt_name;
8760 if (relplt_name == NULL)
8761 relplt_name = bed->default_use_rela_p ? ".rela.plt" : ".rel.plt";
8762 relplt = bfd_get_section_by_name (abfd, relplt_name);
8763 if (relplt == NULL)
8764 return 0;
8766 hdr = &elf_section_data (relplt)->this_hdr;
8767 if (hdr->sh_link != elf_dynsymtab (abfd)
8768 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
8769 return 0;
8771 plt = bfd_get_section_by_name (abfd, ".plt");
8772 if (plt == NULL)
8773 return 0;
8775 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8776 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
8777 return -1;
8779 count = relplt->size / hdr->sh_entsize;
8780 size = count * sizeof (asymbol);
8781 p = relplt->relocation;
8782 for (i = 0; i < count; i++, s++, p++)
8783 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
8785 s = *ret = bfd_malloc (size);
8786 if (s == NULL)
8787 return -1;
8789 names = (char *) (s + count);
8790 p = relplt->relocation;
8791 n = 0;
8792 for (i = 0; i < count; i++, s++, p++)
8794 size_t len;
8795 bfd_vma addr;
8797 addr = bed->plt_sym_val (i, plt, p);
8798 if (addr == (bfd_vma) -1)
8799 continue;
8801 *s = **p->sym_ptr_ptr;
8802 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
8803 we are defining a symbol, ensure one of them is set. */
8804 if ((s->flags & BSF_LOCAL) == 0)
8805 s->flags |= BSF_GLOBAL;
8806 s->section = plt;
8807 s->value = addr - plt->vma;
8808 s->name = names;
8809 len = strlen ((*p->sym_ptr_ptr)->name);
8810 memcpy (names, (*p->sym_ptr_ptr)->name, len);
8811 names += len;
8812 memcpy (names, "@plt", sizeof ("@plt"));
8813 names += sizeof ("@plt");
8814 ++n;
8817 return n;
8820 struct elf_symbuf_symbol
8822 unsigned long st_name; /* Symbol name, index in string tbl */
8823 unsigned char st_info; /* Type and binding attributes */
8824 unsigned char st_other; /* Visibilty, and target specific */
8827 struct elf_symbuf_head
8829 struct elf_symbuf_symbol *ssym;
8830 bfd_size_type count;
8831 unsigned int st_shndx;
8834 struct elf_symbol
8836 union
8838 Elf_Internal_Sym *isym;
8839 struct elf_symbuf_symbol *ssym;
8840 } u;
8841 const char *name;
8844 /* Sort references to symbols by ascending section number. */
8846 static int
8847 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8849 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8850 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8852 return s1->st_shndx - s2->st_shndx;
8855 static int
8856 elf_sym_name_compare (const void *arg1, const void *arg2)
8858 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8859 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8860 return strcmp (s1->name, s2->name);
8863 static struct elf_symbuf_head *
8864 elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
8866 Elf_Internal_Sym **ind, **indbufend, **indbuf
8867 = bfd_malloc2 (symcount, sizeof (*indbuf));
8868 struct elf_symbuf_symbol *ssym;
8869 struct elf_symbuf_head *ssymbuf, *ssymhead;
8870 bfd_size_type i, shndx_count;
8872 if (indbuf == NULL)
8873 return NULL;
8875 for (ind = indbuf, i = 0; i < symcount; i++)
8876 if (isymbuf[i].st_shndx != SHN_UNDEF)
8877 *ind++ = &isymbuf[i];
8878 indbufend = ind;
8880 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8881 elf_sort_elf_symbol);
8883 shndx_count = 0;
8884 if (indbufend > indbuf)
8885 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8886 if (ind[0]->st_shndx != ind[1]->st_shndx)
8887 shndx_count++;
8889 ssymbuf = bfd_malloc ((shndx_count + 1) * sizeof (*ssymbuf)
8890 + (indbufend - indbuf) * sizeof (*ssymbuf));
8891 if (ssymbuf == NULL)
8893 free (indbuf);
8894 return NULL;
8897 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count);
8898 ssymbuf->ssym = NULL;
8899 ssymbuf->count = shndx_count;
8900 ssymbuf->st_shndx = 0;
8901 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8903 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8905 ssymhead++;
8906 ssymhead->ssym = ssym;
8907 ssymhead->count = 0;
8908 ssymhead->st_shndx = (*ind)->st_shndx;
8910 ssym->st_name = (*ind)->st_name;
8911 ssym->st_info = (*ind)->st_info;
8912 ssym->st_other = (*ind)->st_other;
8913 ssymhead->count++;
8915 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count);
8917 free (indbuf);
8918 return ssymbuf;
8921 /* Check if 2 sections define the same set of local and global
8922 symbols. */
8924 bfd_boolean
8925 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8926 struct bfd_link_info *info)
8928 bfd *bfd1, *bfd2;
8929 const struct elf_backend_data *bed1, *bed2;
8930 Elf_Internal_Shdr *hdr1, *hdr2;
8931 bfd_size_type symcount1, symcount2;
8932 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8933 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8934 Elf_Internal_Sym *isym, *isymend;
8935 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8936 bfd_size_type count1, count2, i;
8937 int shndx1, shndx2;
8938 bfd_boolean result;
8940 bfd1 = sec1->owner;
8941 bfd2 = sec2->owner;
8943 /* If both are .gnu.linkonce sections, they have to have the same
8944 section name. */
8945 if (CONST_STRNEQ (sec1->name, ".gnu.linkonce")
8946 && CONST_STRNEQ (sec2->name, ".gnu.linkonce"))
8947 return strcmp (sec1->name + sizeof ".gnu.linkonce",
8948 sec2->name + sizeof ".gnu.linkonce") == 0;
8950 /* Both sections have to be in ELF. */
8951 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8952 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8953 return FALSE;
8955 if (elf_section_type (sec1) != elf_section_type (sec2))
8956 return FALSE;
8958 if ((elf_section_flags (sec1) & SHF_GROUP) != 0
8959 && (elf_section_flags (sec2) & SHF_GROUP) != 0)
8961 /* If both are members of section groups, they have to have the
8962 same group name. */
8963 if (strcmp (elf_group_name (sec1), elf_group_name (sec2)) != 0)
8964 return FALSE;
8967 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8968 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8969 if (shndx1 == -1 || shndx2 == -1)
8970 return FALSE;
8972 bed1 = get_elf_backend_data (bfd1);
8973 bed2 = get_elf_backend_data (bfd2);
8974 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8975 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8976 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8977 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8979 if (symcount1 == 0 || symcount2 == 0)
8980 return FALSE;
8982 result = FALSE;
8983 isymbuf1 = NULL;
8984 isymbuf2 = NULL;
8985 ssymbuf1 = elf_tdata (bfd1)->symbuf;
8986 ssymbuf2 = elf_tdata (bfd2)->symbuf;
8988 if (ssymbuf1 == NULL)
8990 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8991 NULL, NULL, NULL);
8992 if (isymbuf1 == NULL)
8993 goto done;
8995 if (!info->reduce_memory_overheads)
8996 elf_tdata (bfd1)->symbuf = ssymbuf1
8997 = elf_create_symbuf (symcount1, isymbuf1);
9000 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
9002 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
9003 NULL, NULL, NULL);
9004 if (isymbuf2 == NULL)
9005 goto done;
9007 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
9008 elf_tdata (bfd2)->symbuf = ssymbuf2
9009 = elf_create_symbuf (symcount2, isymbuf2);
9012 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
9014 /* Optimized faster version. */
9015 bfd_size_type lo, hi, mid;
9016 struct elf_symbol *symp;
9017 struct elf_symbuf_symbol *ssym, *ssymend;
9019 lo = 0;
9020 hi = ssymbuf1->count;
9021 ssymbuf1++;
9022 count1 = 0;
9023 while (lo < hi)
9025 mid = (lo + hi) / 2;
9026 if ((unsigned int) shndx1 < ssymbuf1[mid].st_shndx)
9027 hi = mid;
9028 else if ((unsigned int) shndx1 > ssymbuf1[mid].st_shndx)
9029 lo = mid + 1;
9030 else
9032 count1 = ssymbuf1[mid].count;
9033 ssymbuf1 += mid;
9034 break;
9038 lo = 0;
9039 hi = ssymbuf2->count;
9040 ssymbuf2++;
9041 count2 = 0;
9042 while (lo < hi)
9044 mid = (lo + hi) / 2;
9045 if ((unsigned int) shndx2 < ssymbuf2[mid].st_shndx)
9046 hi = mid;
9047 else if ((unsigned int) shndx2 > ssymbuf2[mid].st_shndx)
9048 lo = mid + 1;
9049 else
9051 count2 = ssymbuf2[mid].count;
9052 ssymbuf2 += mid;
9053 break;
9057 if (count1 == 0 || count2 == 0 || count1 != count2)
9058 goto done;
9060 symtable1 = bfd_malloc (count1 * sizeof (struct elf_symbol));
9061 symtable2 = bfd_malloc (count2 * sizeof (struct elf_symbol));
9062 if (symtable1 == NULL || symtable2 == NULL)
9063 goto done;
9065 symp = symtable1;
9066 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
9067 ssym < ssymend; ssym++, symp++)
9069 symp->u.ssym = ssym;
9070 symp->name = bfd_elf_string_from_elf_section (bfd1,
9071 hdr1->sh_link,
9072 ssym->st_name);
9075 symp = symtable2;
9076 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
9077 ssym < ssymend; ssym++, symp++)
9079 symp->u.ssym = ssym;
9080 symp->name = bfd_elf_string_from_elf_section (bfd2,
9081 hdr2->sh_link,
9082 ssym->st_name);
9085 /* Sort symbol by name. */
9086 qsort (symtable1, count1, sizeof (struct elf_symbol),
9087 elf_sym_name_compare);
9088 qsort (symtable2, count1, sizeof (struct elf_symbol),
9089 elf_sym_name_compare);
9091 for (i = 0; i < count1; i++)
9092 /* Two symbols must have the same binding, type and name. */
9093 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
9094 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
9095 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
9096 goto done;
9098 result = TRUE;
9099 goto done;
9102 symtable1 = bfd_malloc (symcount1 * sizeof (struct elf_symbol));
9103 symtable2 = bfd_malloc (symcount2 * sizeof (struct elf_symbol));
9104 if (symtable1 == NULL || symtable2 == NULL)
9105 goto done;
9107 /* Count definitions in the section. */
9108 count1 = 0;
9109 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
9110 if (isym->st_shndx == (unsigned int) shndx1)
9111 symtable1[count1++].u.isym = isym;
9113 count2 = 0;
9114 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
9115 if (isym->st_shndx == (unsigned int) shndx2)
9116 symtable2[count2++].u.isym = isym;
9118 if (count1 == 0 || count2 == 0 || count1 != count2)
9119 goto done;
9121 for (i = 0; i < count1; i++)
9122 symtable1[i].name
9123 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
9124 symtable1[i].u.isym->st_name);
9126 for (i = 0; i < count2; i++)
9127 symtable2[i].name
9128 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
9129 symtable2[i].u.isym->st_name);
9131 /* Sort symbol by name. */
9132 qsort (symtable1, count1, sizeof (struct elf_symbol),
9133 elf_sym_name_compare);
9134 qsort (symtable2, count1, sizeof (struct elf_symbol),
9135 elf_sym_name_compare);
9137 for (i = 0; i < count1; i++)
9138 /* Two symbols must have the same binding, type and name. */
9139 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
9140 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
9141 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
9142 goto done;
9144 result = TRUE;
9146 done:
9147 if (symtable1)
9148 free (symtable1);
9149 if (symtable2)
9150 free (symtable2);
9151 if (isymbuf1)
9152 free (isymbuf1);
9153 if (isymbuf2)
9154 free (isymbuf2);
9156 return result;
9159 /* It is only used by x86-64 so far. */
9160 asection _bfd_elf_large_com_section
9161 = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
9162 SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
9164 /* Return TRUE if 2 section types are compatible. */
9166 bfd_boolean
9167 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
9168 bfd *bbfd, const asection *bsec)
9170 if (asec == NULL
9171 || bsec == NULL
9172 || abfd->xvec->flavour != bfd_target_elf_flavour
9173 || bbfd->xvec->flavour != bfd_target_elf_flavour)
9174 return TRUE;
9176 return elf_section_type (asec) == elf_section_type (bsec);
9179 void
9180 _bfd_elf_set_osabi (bfd * abfd,
9181 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
9183 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
9185 i_ehdrp = elf_elfheader (abfd);
9187 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
9191 /* Return TRUE for ELF symbol types that represent functions.
9192 This is the default version of this function, which is sufficient for
9193 most targets. It returns true if TYPE is STT_FUNC. */
9195 bfd_boolean
9196 _bfd_elf_is_function_type (unsigned int type)
9198 return (type == STT_FUNC);