bfd/
[binutils.git] / bfd / elf.c
blob4371f15869da4bded996eac80788c72fe4d0eb7b
1 /* ELF executable support for BFD.
3 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 2002, 2003, 2004 Free Software Foundation, Inc.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* SECTION
24 ELF backends
26 BFD support for ELF formats is being worked on.
27 Currently, the best supported back ends are for sparc and i386
28 (running svr4 or Solaris 2).
30 Documentation of the internals of the support code still needs
31 to be written. The code is changing quickly enough that we
32 haven't bothered yet. */
34 /* For sparc64-cross-sparc32. */
35 #define _SYSCALL32
36 #include "bfd.h"
37 #include "sysdep.h"
38 #include "bfdlink.h"
39 #include "libbfd.h"
40 #define ARCH_SIZE 0
41 #include "elf-bfd.h"
42 #include "libiberty.h"
44 static int elf_sort_sections (const void *, const void *);
45 static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
46 static bfd_boolean prep_headers (bfd *);
47 static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ;
48 static bfd_boolean elfcore_read_notes (bfd *, file_ptr, bfd_size_type) ;
50 /* Swap version information in and out. The version information is
51 currently size independent. If that ever changes, this code will
52 need to move into elfcode.h. */
54 /* Swap in a Verdef structure. */
56 void
57 _bfd_elf_swap_verdef_in (bfd *abfd,
58 const Elf_External_Verdef *src,
59 Elf_Internal_Verdef *dst)
61 dst->vd_version = H_GET_16 (abfd, src->vd_version);
62 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
63 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
64 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
65 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
66 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
67 dst->vd_next = H_GET_32 (abfd, src->vd_next);
70 /* Swap out a Verdef structure. */
72 void
73 _bfd_elf_swap_verdef_out (bfd *abfd,
74 const Elf_Internal_Verdef *src,
75 Elf_External_Verdef *dst)
77 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
78 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
79 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
80 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
81 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
82 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
83 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
86 /* Swap in a Verdaux structure. */
88 void
89 _bfd_elf_swap_verdaux_in (bfd *abfd,
90 const Elf_External_Verdaux *src,
91 Elf_Internal_Verdaux *dst)
93 dst->vda_name = H_GET_32 (abfd, src->vda_name);
94 dst->vda_next = H_GET_32 (abfd, src->vda_next);
97 /* Swap out a Verdaux structure. */
99 void
100 _bfd_elf_swap_verdaux_out (bfd *abfd,
101 const Elf_Internal_Verdaux *src,
102 Elf_External_Verdaux *dst)
104 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
105 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
108 /* Swap in a Verneed structure. */
110 void
111 _bfd_elf_swap_verneed_in (bfd *abfd,
112 const Elf_External_Verneed *src,
113 Elf_Internal_Verneed *dst)
115 dst->vn_version = H_GET_16 (abfd, src->vn_version);
116 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
117 dst->vn_file = H_GET_32 (abfd, src->vn_file);
118 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
119 dst->vn_next = H_GET_32 (abfd, src->vn_next);
122 /* Swap out a Verneed structure. */
124 void
125 _bfd_elf_swap_verneed_out (bfd *abfd,
126 const Elf_Internal_Verneed *src,
127 Elf_External_Verneed *dst)
129 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
130 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
131 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
132 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
133 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
136 /* Swap in a Vernaux structure. */
138 void
139 _bfd_elf_swap_vernaux_in (bfd *abfd,
140 const Elf_External_Vernaux *src,
141 Elf_Internal_Vernaux *dst)
143 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
144 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
145 dst->vna_other = H_GET_16 (abfd, src->vna_other);
146 dst->vna_name = H_GET_32 (abfd, src->vna_name);
147 dst->vna_next = H_GET_32 (abfd, src->vna_next);
150 /* Swap out a Vernaux structure. */
152 void
153 _bfd_elf_swap_vernaux_out (bfd *abfd,
154 const Elf_Internal_Vernaux *src,
155 Elf_External_Vernaux *dst)
157 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
158 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
159 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
160 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
161 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
164 /* Swap in a Versym structure. */
166 void
167 _bfd_elf_swap_versym_in (bfd *abfd,
168 const Elf_External_Versym *src,
169 Elf_Internal_Versym *dst)
171 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
174 /* Swap out a Versym structure. */
176 void
177 _bfd_elf_swap_versym_out (bfd *abfd,
178 const Elf_Internal_Versym *src,
179 Elf_External_Versym *dst)
181 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
184 /* Standard ELF hash function. Do not change this function; you will
185 cause invalid hash tables to be generated. */
187 unsigned long
188 bfd_elf_hash (const char *namearg)
190 const unsigned char *name = (const unsigned char *) namearg;
191 unsigned long h = 0;
192 unsigned long g;
193 int ch;
195 while ((ch = *name++) != '\0')
197 h = (h << 4) + ch;
198 if ((g = (h & 0xf0000000)) != 0)
200 h ^= g >> 24;
201 /* The ELF ABI says `h &= ~g', but this is equivalent in
202 this case and on some machines one insn instead of two. */
203 h ^= g;
206 return h & 0xffffffff;
209 /* Read a specified number of bytes at a specified offset in an ELF
210 file, into a newly allocated buffer, and return a pointer to the
211 buffer. */
213 static char *
214 elf_read (bfd *abfd, file_ptr offset, bfd_size_type size)
216 char *buf;
218 if ((buf = bfd_alloc (abfd, size)) == NULL)
219 return NULL;
220 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
221 return NULL;
222 if (bfd_bread (buf, size, abfd) != size)
224 if (bfd_get_error () != bfd_error_system_call)
225 bfd_set_error (bfd_error_file_truncated);
226 return NULL;
228 return buf;
231 bfd_boolean
232 bfd_elf_mkobject (bfd *abfd)
234 /* This just does initialization. */
235 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
236 elf_tdata (abfd) = bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
237 if (elf_tdata (abfd) == 0)
238 return FALSE;
239 /* Since everything is done at close time, do we need any
240 initialization? */
242 return TRUE;
245 bfd_boolean
246 bfd_elf_mkcorefile (bfd *abfd)
248 /* I think this can be done just like an object file. */
249 return bfd_elf_mkobject (abfd);
252 char *
253 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
255 Elf_Internal_Shdr **i_shdrp;
256 char *shstrtab = NULL;
257 file_ptr offset;
258 bfd_size_type shstrtabsize;
260 i_shdrp = elf_elfsections (abfd);
261 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
262 return 0;
264 shstrtab = (char *) i_shdrp[shindex]->contents;
265 if (shstrtab == NULL)
267 /* No cached one, attempt to read, and cache what we read. */
268 offset = i_shdrp[shindex]->sh_offset;
269 shstrtabsize = i_shdrp[shindex]->sh_size;
270 shstrtab = elf_read (abfd, offset, shstrtabsize);
271 i_shdrp[shindex]->contents = shstrtab;
273 return shstrtab;
276 char *
277 bfd_elf_string_from_elf_section (bfd *abfd,
278 unsigned int shindex,
279 unsigned int strindex)
281 Elf_Internal_Shdr *hdr;
283 if (strindex == 0)
284 return "";
286 hdr = elf_elfsections (abfd)[shindex];
288 if (hdr->contents == NULL
289 && bfd_elf_get_str_section (abfd, shindex) == NULL)
290 return NULL;
292 if (strindex >= hdr->sh_size)
294 (*_bfd_error_handler)
295 (_("%s: invalid string offset %u >= %lu for section `%s'"),
296 bfd_archive_filename (abfd), strindex, (unsigned long) hdr->sh_size,
297 ((shindex == elf_elfheader(abfd)->e_shstrndx
298 && strindex == hdr->sh_name)
299 ? ".shstrtab"
300 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
301 return "";
304 return ((char *) hdr->contents) + strindex;
307 /* Read and convert symbols to internal format.
308 SYMCOUNT specifies the number of symbols to read, starting from
309 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
310 are non-NULL, they are used to store the internal symbols, external
311 symbols, and symbol section index extensions, respectively. */
313 Elf_Internal_Sym *
314 bfd_elf_get_elf_syms (bfd *ibfd,
315 Elf_Internal_Shdr *symtab_hdr,
316 size_t symcount,
317 size_t symoffset,
318 Elf_Internal_Sym *intsym_buf,
319 void *extsym_buf,
320 Elf_External_Sym_Shndx *extshndx_buf)
322 Elf_Internal_Shdr *shndx_hdr;
323 void *alloc_ext;
324 const bfd_byte *esym;
325 Elf_External_Sym_Shndx *alloc_extshndx;
326 Elf_External_Sym_Shndx *shndx;
327 Elf_Internal_Sym *isym;
328 Elf_Internal_Sym *isymend;
329 const struct elf_backend_data *bed;
330 size_t extsym_size;
331 bfd_size_type amt;
332 file_ptr pos;
334 if (symcount == 0)
335 return intsym_buf;
337 /* Normal syms might have section extension entries. */
338 shndx_hdr = NULL;
339 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
340 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
342 /* Read the symbols. */
343 alloc_ext = NULL;
344 alloc_extshndx = NULL;
345 bed = get_elf_backend_data (ibfd);
346 extsym_size = bed->s->sizeof_sym;
347 amt = symcount * extsym_size;
348 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
349 if (extsym_buf == NULL)
351 alloc_ext = bfd_malloc (amt);
352 extsym_buf = alloc_ext;
354 if (extsym_buf == NULL
355 || bfd_seek (ibfd, pos, SEEK_SET) != 0
356 || bfd_bread (extsym_buf, amt, ibfd) != amt)
358 intsym_buf = NULL;
359 goto out;
362 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
363 extshndx_buf = NULL;
364 else
366 amt = symcount * sizeof (Elf_External_Sym_Shndx);
367 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
368 if (extshndx_buf == NULL)
370 alloc_extshndx = bfd_malloc (amt);
371 extshndx_buf = alloc_extshndx;
373 if (extshndx_buf == NULL
374 || bfd_seek (ibfd, pos, SEEK_SET) != 0
375 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
377 intsym_buf = NULL;
378 goto out;
382 if (intsym_buf == NULL)
384 bfd_size_type amt = symcount * sizeof (Elf_Internal_Sym);
385 intsym_buf = bfd_malloc (amt);
386 if (intsym_buf == NULL)
387 goto out;
390 /* Convert the symbols to internal form. */
391 isymend = intsym_buf + symcount;
392 for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf;
393 isym < isymend;
394 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
395 (*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym);
397 out:
398 if (alloc_ext != NULL)
399 free (alloc_ext);
400 if (alloc_extshndx != NULL)
401 free (alloc_extshndx);
403 return intsym_buf;
406 /* Look up a symbol name. */
407 const char *
408 bfd_elf_local_sym_name (bfd *abfd, Elf_Internal_Sym *isym)
410 unsigned int iname = isym->st_name;
411 unsigned int shindex = elf_tdata (abfd)->symtab_hdr.sh_link;
412 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION)
414 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
415 shindex = elf_elfheader (abfd)->e_shstrndx;
418 return bfd_elf_string_from_elf_section (abfd, shindex, iname);
421 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
422 sections. The first element is the flags, the rest are section
423 pointers. */
425 typedef union elf_internal_group {
426 Elf_Internal_Shdr *shdr;
427 unsigned int flags;
428 } Elf_Internal_Group;
430 /* Return the name of the group signature symbol. Why isn't the
431 signature just a string? */
433 static const char *
434 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
436 Elf_Internal_Shdr *hdr;
437 unsigned char esym[sizeof (Elf64_External_Sym)];
438 Elf_External_Sym_Shndx eshndx;
439 Elf_Internal_Sym isym;
441 /* First we need to ensure the symbol table is available. */
442 if (! bfd_section_from_shdr (abfd, ghdr->sh_link))
443 return NULL;
445 /* Go read the symbol. */
446 hdr = &elf_tdata (abfd)->symtab_hdr;
447 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
448 &isym, esym, &eshndx) == NULL)
449 return NULL;
451 return bfd_elf_local_sym_name (abfd, &isym);
454 /* Set next_in_group list pointer, and group name for NEWSECT. */
456 static bfd_boolean
457 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
459 unsigned int num_group = elf_tdata (abfd)->num_group;
461 /* If num_group is zero, read in all SHT_GROUP sections. The count
462 is set to -1 if there are no SHT_GROUP sections. */
463 if (num_group == 0)
465 unsigned int i, shnum;
467 /* First count the number of groups. If we have a SHT_GROUP
468 section with just a flag word (ie. sh_size is 4), ignore it. */
469 shnum = elf_numsections (abfd);
470 num_group = 0;
471 for (i = 0; i < shnum; i++)
473 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
474 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
475 num_group += 1;
478 if (num_group == 0)
479 num_group = (unsigned) -1;
480 elf_tdata (abfd)->num_group = num_group;
482 if (num_group > 0)
484 /* We keep a list of elf section headers for group sections,
485 so we can find them quickly. */
486 bfd_size_type amt = num_group * sizeof (Elf_Internal_Shdr *);
487 elf_tdata (abfd)->group_sect_ptr = bfd_alloc (abfd, amt);
488 if (elf_tdata (abfd)->group_sect_ptr == NULL)
489 return FALSE;
491 num_group = 0;
492 for (i = 0; i < shnum; i++)
494 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
495 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
497 unsigned char *src;
498 Elf_Internal_Group *dest;
500 /* Add to list of sections. */
501 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
502 num_group += 1;
504 /* Read the raw contents. */
505 BFD_ASSERT (sizeof (*dest) >= 4);
506 amt = shdr->sh_size * sizeof (*dest) / 4;
507 shdr->contents = bfd_alloc (abfd, amt);
508 if (shdr->contents == NULL
509 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
510 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
511 != shdr->sh_size))
512 return FALSE;
514 /* Translate raw contents, a flag word followed by an
515 array of elf section indices all in target byte order,
516 to the flag word followed by an array of elf section
517 pointers. */
518 src = shdr->contents + shdr->sh_size;
519 dest = (Elf_Internal_Group *) (shdr->contents + amt);
520 while (1)
522 unsigned int idx;
524 src -= 4;
525 --dest;
526 idx = H_GET_32 (abfd, src);
527 if (src == shdr->contents)
529 dest->flags = idx;
530 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
531 shdr->bfd_section->flags
532 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
533 break;
535 if (idx >= shnum)
537 ((*_bfd_error_handler)
538 (_("%s: invalid SHT_GROUP entry"),
539 bfd_archive_filename (abfd)));
540 idx = 0;
542 dest->shdr = elf_elfsections (abfd)[idx];
549 if (num_group != (unsigned) -1)
551 unsigned int i;
553 for (i = 0; i < num_group; i++)
555 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
556 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
557 unsigned int n_elt = shdr->sh_size / 4;
559 /* Look through this group's sections to see if current
560 section is a member. */
561 while (--n_elt != 0)
562 if ((++idx)->shdr == hdr)
564 asection *s = NULL;
566 /* We are a member of this group. Go looking through
567 other members to see if any others are linked via
568 next_in_group. */
569 idx = (Elf_Internal_Group *) shdr->contents;
570 n_elt = shdr->sh_size / 4;
571 while (--n_elt != 0)
572 if ((s = (++idx)->shdr->bfd_section) != NULL
573 && elf_next_in_group (s) != NULL)
574 break;
575 if (n_elt != 0)
577 /* Snarf the group name from other member, and
578 insert current section in circular list. */
579 elf_group_name (newsect) = elf_group_name (s);
580 elf_next_in_group (newsect) = elf_next_in_group (s);
581 elf_next_in_group (s) = newsect;
583 else
585 const char *gname;
587 gname = group_signature (abfd, shdr);
588 if (gname == NULL)
589 return FALSE;
590 elf_group_name (newsect) = gname;
592 /* Start a circular list with one element. */
593 elf_next_in_group (newsect) = newsect;
596 /* If the group section has been created, point to the
597 new member. */
598 if (shdr->bfd_section != NULL)
599 elf_next_in_group (shdr->bfd_section) = newsect;
601 i = num_group - 1;
602 break;
607 if (elf_group_name (newsect) == NULL)
609 (*_bfd_error_handler) (_("%s: no group info for section %s"),
610 bfd_archive_filename (abfd), newsect->name);
612 return TRUE;
615 bfd_boolean
616 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
618 return elf_next_in_group (sec) != NULL;
621 bfd_boolean
622 bfd_elf_discard_group (bfd *abfd ATTRIBUTE_UNUSED, asection *group)
624 asection *first = elf_next_in_group (group);
625 asection *s = first;
627 while (s != NULL)
629 s->output_section = bfd_abs_section_ptr;
630 s = elf_next_in_group (s);
631 /* These lists are circular. */
632 if (s == first)
633 break;
635 return TRUE;
638 /* Make a BFD section from an ELF section. We store a pointer to the
639 BFD section in the bfd_section field of the header. */
641 bfd_boolean
642 _bfd_elf_make_section_from_shdr (bfd *abfd,
643 Elf_Internal_Shdr *hdr,
644 const char *name)
646 asection *newsect;
647 flagword flags;
648 const struct elf_backend_data *bed;
650 if (hdr->bfd_section != NULL)
652 BFD_ASSERT (strcmp (name,
653 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
654 return TRUE;
657 newsect = bfd_make_section_anyway (abfd, name);
658 if (newsect == NULL)
659 return FALSE;
661 hdr->bfd_section = newsect;
662 elf_section_data (newsect)->this_hdr = *hdr;
664 /* Always use the real type/flags. */
665 elf_section_type (newsect) = hdr->sh_type;
666 elf_section_flags (newsect) = hdr->sh_flags;
668 newsect->filepos = hdr->sh_offset;
670 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
671 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
672 || ! bfd_set_section_alignment (abfd, newsect,
673 bfd_log2 ((bfd_vma) hdr->sh_addralign)))
674 return FALSE;
676 flags = SEC_NO_FLAGS;
677 if (hdr->sh_type != SHT_NOBITS)
678 flags |= SEC_HAS_CONTENTS;
679 if (hdr->sh_type == SHT_GROUP)
680 flags |= SEC_GROUP | SEC_EXCLUDE;
681 if ((hdr->sh_flags & SHF_ALLOC) != 0)
683 flags |= SEC_ALLOC;
684 if (hdr->sh_type != SHT_NOBITS)
685 flags |= SEC_LOAD;
687 if ((hdr->sh_flags & SHF_WRITE) == 0)
688 flags |= SEC_READONLY;
689 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
690 flags |= SEC_CODE;
691 else if ((flags & SEC_LOAD) != 0)
692 flags |= SEC_DATA;
693 if ((hdr->sh_flags & SHF_MERGE) != 0)
695 flags |= SEC_MERGE;
696 newsect->entsize = hdr->sh_entsize;
697 if ((hdr->sh_flags & SHF_STRINGS) != 0)
698 flags |= SEC_STRINGS;
700 if (hdr->sh_flags & SHF_GROUP)
701 if (!setup_group (abfd, hdr, newsect))
702 return FALSE;
703 if ((hdr->sh_flags & SHF_TLS) != 0)
704 flags |= SEC_THREAD_LOCAL;
706 /* The debugging sections appear to be recognized only by name, not
707 any sort of flag. */
709 static const char *debug_sec_names [] =
711 ".debug",
712 ".gnu.linkonce.wi.",
713 ".line",
714 ".stab"
716 int i;
718 for (i = ARRAY_SIZE (debug_sec_names); i--;)
719 if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0)
720 break;
722 if (i >= 0)
723 flags |= SEC_DEBUGGING;
726 /* As a GNU extension, if the name begins with .gnu.linkonce, we
727 only link a single copy of the section. This is used to support
728 g++. g++ will emit each template expansion in its own section.
729 The symbols will be defined as weak, so that multiple definitions
730 are permitted. The GNU linker extension is to actually discard
731 all but one of the sections. */
732 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0
733 && elf_next_in_group (newsect) == NULL)
734 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
736 bed = get_elf_backend_data (abfd);
737 if (bed->elf_backend_section_flags)
738 if (! bed->elf_backend_section_flags (&flags, hdr))
739 return FALSE;
741 if (! bfd_set_section_flags (abfd, newsect, flags))
742 return FALSE;
744 if ((flags & SEC_ALLOC) != 0)
746 Elf_Internal_Phdr *phdr;
747 unsigned int i;
749 /* Look through the phdrs to see if we need to adjust the lma.
750 If all the p_paddr fields are zero, we ignore them, since
751 some ELF linkers produce such output. */
752 phdr = elf_tdata (abfd)->phdr;
753 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
755 if (phdr->p_paddr != 0)
756 break;
758 if (i < elf_elfheader (abfd)->e_phnum)
760 phdr = elf_tdata (abfd)->phdr;
761 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
763 /* This section is part of this segment if its file
764 offset plus size lies within the segment's memory
765 span and, if the section is loaded, the extent of the
766 loaded data lies within the extent of the segment.
768 Note - we used to check the p_paddr field as well, and
769 refuse to set the LMA if it was 0. This is wrong
770 though, as a perfectly valid initialised segment can
771 have a p_paddr of zero. Some architectures, eg ARM,
772 place special significance on the address 0 and
773 executables need to be able to have a segment which
774 covers this address. */
775 if (phdr->p_type == PT_LOAD
776 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
777 && (hdr->sh_offset + hdr->sh_size
778 <= phdr->p_offset + phdr->p_memsz)
779 && ((flags & SEC_LOAD) == 0
780 || (hdr->sh_offset + hdr->sh_size
781 <= phdr->p_offset + phdr->p_filesz)))
783 if ((flags & SEC_LOAD) == 0)
784 newsect->lma = (phdr->p_paddr
785 + hdr->sh_addr - phdr->p_vaddr);
786 else
787 /* We used to use the same adjustment for SEC_LOAD
788 sections, but that doesn't work if the segment
789 is packed with code from multiple VMAs.
790 Instead we calculate the section LMA based on
791 the segment LMA. It is assumed that the
792 segment will contain sections with contiguous
793 LMAs, even if the VMAs are not. */
794 newsect->lma = (phdr->p_paddr
795 + hdr->sh_offset - phdr->p_offset);
797 /* With contiguous segments, we can't tell from file
798 offsets whether a section with zero size should
799 be placed at the end of one segment or the
800 beginning of the next. Decide based on vaddr. */
801 if (hdr->sh_addr >= phdr->p_vaddr
802 && (hdr->sh_addr + hdr->sh_size
803 <= phdr->p_vaddr + phdr->p_memsz))
804 break;
810 return TRUE;
814 INTERNAL_FUNCTION
815 bfd_elf_find_section
817 SYNOPSIS
818 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
820 DESCRIPTION
821 Helper functions for GDB to locate the string tables.
822 Since BFD hides string tables from callers, GDB needs to use an
823 internal hook to find them. Sun's .stabstr, in particular,
824 isn't even pointed to by the .stab section, so ordinary
825 mechanisms wouldn't work to find it, even if we had some.
828 struct elf_internal_shdr *
829 bfd_elf_find_section (bfd *abfd, char *name)
831 Elf_Internal_Shdr **i_shdrp;
832 char *shstrtab;
833 unsigned int max;
834 unsigned int i;
836 i_shdrp = elf_elfsections (abfd);
837 if (i_shdrp != NULL)
839 shstrtab = bfd_elf_get_str_section (abfd,
840 elf_elfheader (abfd)->e_shstrndx);
841 if (shstrtab != NULL)
843 max = elf_numsections (abfd);
844 for (i = 1; i < max; i++)
845 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
846 return i_shdrp[i];
849 return 0;
852 const char *const bfd_elf_section_type_names[] = {
853 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
854 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
855 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
858 /* ELF relocs are against symbols. If we are producing relocatable
859 output, and the reloc is against an external symbol, and nothing
860 has given us any additional addend, the resulting reloc will also
861 be against the same symbol. In such a case, we don't want to
862 change anything about the way the reloc is handled, since it will
863 all be done at final link time. Rather than put special case code
864 into bfd_perform_relocation, all the reloc types use this howto
865 function. It just short circuits the reloc if producing
866 relocatable output against an external symbol. */
868 bfd_reloc_status_type
869 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
870 arelent *reloc_entry,
871 asymbol *symbol,
872 void *data ATTRIBUTE_UNUSED,
873 asection *input_section,
874 bfd *output_bfd,
875 char **error_message ATTRIBUTE_UNUSED)
877 if (output_bfd != NULL
878 && (symbol->flags & BSF_SECTION_SYM) == 0
879 && (! reloc_entry->howto->partial_inplace
880 || reloc_entry->addend == 0))
882 reloc_entry->address += input_section->output_offset;
883 return bfd_reloc_ok;
886 return bfd_reloc_continue;
889 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
891 static void
892 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
893 asection *sec)
895 BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE);
896 sec->sec_info_type = ELF_INFO_TYPE_NONE;
899 /* Finish SHF_MERGE section merging. */
901 bfd_boolean
902 _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
904 bfd *ibfd;
905 asection *sec;
907 if (!is_elf_hash_table (info->hash))
908 return FALSE;
910 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
911 if ((ibfd->flags & DYNAMIC) == 0)
912 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
913 if ((sec->flags & SEC_MERGE) != 0
914 && !bfd_is_abs_section (sec->output_section))
916 struct bfd_elf_section_data *secdata;
918 secdata = elf_section_data (sec);
919 if (! _bfd_add_merge_section (abfd,
920 &elf_hash_table (info)->merge_info,
921 sec, &secdata->sec_info))
922 return FALSE;
923 else if (secdata->sec_info)
924 sec->sec_info_type = ELF_INFO_TYPE_MERGE;
927 if (elf_hash_table (info)->merge_info != NULL)
928 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
929 merge_sections_remove_hook);
930 return TRUE;
933 void
934 _bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
936 sec->output_section = bfd_abs_section_ptr;
937 sec->output_offset = sec->vma;
938 if (!is_elf_hash_table (info->hash))
939 return;
941 sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
944 /* Copy the program header and other data from one object module to
945 another. */
947 bfd_boolean
948 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
950 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
951 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
952 return TRUE;
954 BFD_ASSERT (!elf_flags_init (obfd)
955 || (elf_elfheader (obfd)->e_flags
956 == elf_elfheader (ibfd)->e_flags));
958 elf_gp (obfd) = elf_gp (ibfd);
959 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
960 elf_flags_init (obfd) = TRUE;
961 return TRUE;
964 /* Print out the program headers. */
966 bfd_boolean
967 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
969 FILE *f = farg;
970 Elf_Internal_Phdr *p;
971 asection *s;
972 bfd_byte *dynbuf = NULL;
974 p = elf_tdata (abfd)->phdr;
975 if (p != NULL)
977 unsigned int i, c;
979 fprintf (f, _("\nProgram Header:\n"));
980 c = elf_elfheader (abfd)->e_phnum;
981 for (i = 0; i < c; i++, p++)
983 const char *pt;
984 char buf[20];
986 switch (p->p_type)
988 case PT_NULL: pt = "NULL"; break;
989 case PT_LOAD: pt = "LOAD"; break;
990 case PT_DYNAMIC: pt = "DYNAMIC"; break;
991 case PT_INTERP: pt = "INTERP"; break;
992 case PT_NOTE: pt = "NOTE"; break;
993 case PT_SHLIB: pt = "SHLIB"; break;
994 case PT_PHDR: pt = "PHDR"; break;
995 case PT_TLS: pt = "TLS"; break;
996 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
997 case PT_GNU_STACK: pt = "STACK"; break;
998 case PT_GNU_RELRO: pt = "RELRO"; break;
999 default: sprintf (buf, "0x%lx", p->p_type); pt = buf; break;
1001 fprintf (f, "%8s off 0x", pt);
1002 bfd_fprintf_vma (abfd, f, p->p_offset);
1003 fprintf (f, " vaddr 0x");
1004 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1005 fprintf (f, " paddr 0x");
1006 bfd_fprintf_vma (abfd, f, p->p_paddr);
1007 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1008 fprintf (f, " filesz 0x");
1009 bfd_fprintf_vma (abfd, f, p->p_filesz);
1010 fprintf (f, " memsz 0x");
1011 bfd_fprintf_vma (abfd, f, p->p_memsz);
1012 fprintf (f, " flags %c%c%c",
1013 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1014 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1015 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1016 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1017 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1018 fprintf (f, "\n");
1022 s = bfd_get_section_by_name (abfd, ".dynamic");
1023 if (s != NULL)
1025 int elfsec;
1026 unsigned long shlink;
1027 bfd_byte *extdyn, *extdynend;
1028 size_t extdynsize;
1029 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1031 fprintf (f, _("\nDynamic Section:\n"));
1033 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1034 goto error_return;
1036 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1037 if (elfsec == -1)
1038 goto error_return;
1039 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1041 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1042 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1044 extdyn = dynbuf;
1045 extdynend = extdyn + s->size;
1046 for (; extdyn < extdynend; extdyn += extdynsize)
1048 Elf_Internal_Dyn dyn;
1049 const char *name;
1050 char ab[20];
1051 bfd_boolean stringp;
1053 (*swap_dyn_in) (abfd, extdyn, &dyn);
1055 if (dyn.d_tag == DT_NULL)
1056 break;
1058 stringp = FALSE;
1059 switch (dyn.d_tag)
1061 default:
1062 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1063 name = ab;
1064 break;
1066 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1067 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1068 case DT_PLTGOT: name = "PLTGOT"; break;
1069 case DT_HASH: name = "HASH"; break;
1070 case DT_STRTAB: name = "STRTAB"; break;
1071 case DT_SYMTAB: name = "SYMTAB"; break;
1072 case DT_RELA: name = "RELA"; break;
1073 case DT_RELASZ: name = "RELASZ"; break;
1074 case DT_RELAENT: name = "RELAENT"; break;
1075 case DT_STRSZ: name = "STRSZ"; break;
1076 case DT_SYMENT: name = "SYMENT"; break;
1077 case DT_INIT: name = "INIT"; break;
1078 case DT_FINI: name = "FINI"; break;
1079 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1080 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1081 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1082 case DT_REL: name = "REL"; break;
1083 case DT_RELSZ: name = "RELSZ"; break;
1084 case DT_RELENT: name = "RELENT"; break;
1085 case DT_PLTREL: name = "PLTREL"; break;
1086 case DT_DEBUG: name = "DEBUG"; break;
1087 case DT_TEXTREL: name = "TEXTREL"; break;
1088 case DT_JMPREL: name = "JMPREL"; break;
1089 case DT_BIND_NOW: name = "BIND_NOW"; break;
1090 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1091 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1092 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1093 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1094 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1095 case DT_FLAGS: name = "FLAGS"; break;
1096 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1097 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1098 case DT_CHECKSUM: name = "CHECKSUM"; break;
1099 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1100 case DT_MOVEENT: name = "MOVEENT"; break;
1101 case DT_MOVESZ: name = "MOVESZ"; break;
1102 case DT_FEATURE: name = "FEATURE"; break;
1103 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1104 case DT_SYMINSZ: name = "SYMINSZ"; break;
1105 case DT_SYMINENT: name = "SYMINENT"; break;
1106 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1107 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1108 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1109 case DT_PLTPAD: name = "PLTPAD"; break;
1110 case DT_MOVETAB: name = "MOVETAB"; break;
1111 case DT_SYMINFO: name = "SYMINFO"; break;
1112 case DT_RELACOUNT: name = "RELACOUNT"; break;
1113 case DT_RELCOUNT: name = "RELCOUNT"; break;
1114 case DT_FLAGS_1: name = "FLAGS_1"; break;
1115 case DT_VERSYM: name = "VERSYM"; break;
1116 case DT_VERDEF: name = "VERDEF"; break;
1117 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1118 case DT_VERNEED: name = "VERNEED"; break;
1119 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1120 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1121 case DT_USED: name = "USED"; break;
1122 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1125 fprintf (f, " %-11s ", name);
1126 if (! stringp)
1127 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
1128 else
1130 const char *string;
1131 unsigned int tagv = dyn.d_un.d_val;
1133 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1134 if (string == NULL)
1135 goto error_return;
1136 fprintf (f, "%s", string);
1138 fprintf (f, "\n");
1141 free (dynbuf);
1142 dynbuf = NULL;
1145 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1146 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1148 if (! _bfd_elf_slurp_version_tables (abfd))
1149 return FALSE;
1152 if (elf_dynverdef (abfd) != 0)
1154 Elf_Internal_Verdef *t;
1156 fprintf (f, _("\nVersion definitions:\n"));
1157 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1159 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1160 t->vd_flags, t->vd_hash, t->vd_nodename);
1161 if (t->vd_auxptr->vda_nextptr != NULL)
1163 Elf_Internal_Verdaux *a;
1165 fprintf (f, "\t");
1166 for (a = t->vd_auxptr->vda_nextptr;
1167 a != NULL;
1168 a = a->vda_nextptr)
1169 fprintf (f, "%s ", a->vda_nodename);
1170 fprintf (f, "\n");
1175 if (elf_dynverref (abfd) != 0)
1177 Elf_Internal_Verneed *t;
1179 fprintf (f, _("\nVersion References:\n"));
1180 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1182 Elf_Internal_Vernaux *a;
1184 fprintf (f, _(" required from %s:\n"), t->vn_filename);
1185 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1186 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1187 a->vna_flags, a->vna_other, a->vna_nodename);
1191 return TRUE;
1193 error_return:
1194 if (dynbuf != NULL)
1195 free (dynbuf);
1196 return FALSE;
1199 /* Display ELF-specific fields of a symbol. */
1201 void
1202 bfd_elf_print_symbol (bfd *abfd,
1203 void *filep,
1204 asymbol *symbol,
1205 bfd_print_symbol_type how)
1207 FILE *file = filep;
1208 switch (how)
1210 case bfd_print_symbol_name:
1211 fprintf (file, "%s", symbol->name);
1212 break;
1213 case bfd_print_symbol_more:
1214 fprintf (file, "elf ");
1215 bfd_fprintf_vma (abfd, file, symbol->value);
1216 fprintf (file, " %lx", (long) symbol->flags);
1217 break;
1218 case bfd_print_symbol_all:
1220 const char *section_name;
1221 const char *name = NULL;
1222 const struct elf_backend_data *bed;
1223 unsigned char st_other;
1224 bfd_vma val;
1226 section_name = symbol->section ? symbol->section->name : "(*none*)";
1228 bed = get_elf_backend_data (abfd);
1229 if (bed->elf_backend_print_symbol_all)
1230 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1232 if (name == NULL)
1234 name = symbol->name;
1235 bfd_print_symbol_vandf (abfd, file, symbol);
1238 fprintf (file, " %s\t", section_name);
1239 /* Print the "other" value for a symbol. For common symbols,
1240 we've already printed the size; now print the alignment.
1241 For other symbols, we have no specified alignment, and
1242 we've printed the address; now print the size. */
1243 if (bfd_is_com_section (symbol->section))
1244 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1245 else
1246 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1247 bfd_fprintf_vma (abfd, file, val);
1249 /* If we have version information, print it. */
1250 if (elf_tdata (abfd)->dynversym_section != 0
1251 && (elf_tdata (abfd)->dynverdef_section != 0
1252 || elf_tdata (abfd)->dynverref_section != 0))
1254 unsigned int vernum;
1255 const char *version_string;
1257 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1259 if (vernum == 0)
1260 version_string = "";
1261 else if (vernum == 1)
1262 version_string = "Base";
1263 else if (vernum <= elf_tdata (abfd)->cverdefs)
1264 version_string =
1265 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1266 else
1268 Elf_Internal_Verneed *t;
1270 version_string = "";
1271 for (t = elf_tdata (abfd)->verref;
1272 t != NULL;
1273 t = t->vn_nextref)
1275 Elf_Internal_Vernaux *a;
1277 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1279 if (a->vna_other == vernum)
1281 version_string = a->vna_nodename;
1282 break;
1288 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1289 fprintf (file, " %-11s", version_string);
1290 else
1292 int i;
1294 fprintf (file, " (%s)", version_string);
1295 for (i = 10 - strlen (version_string); i > 0; --i)
1296 putc (' ', file);
1300 /* If the st_other field is not zero, print it. */
1301 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1303 switch (st_other)
1305 case 0: break;
1306 case STV_INTERNAL: fprintf (file, " .internal"); break;
1307 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1308 case STV_PROTECTED: fprintf (file, " .protected"); break;
1309 default:
1310 /* Some other non-defined flags are also present, so print
1311 everything hex. */
1312 fprintf (file, " 0x%02x", (unsigned int) st_other);
1315 fprintf (file, " %s", name);
1317 break;
1321 /* Create an entry in an ELF linker hash table. */
1323 struct bfd_hash_entry *
1324 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1325 struct bfd_hash_table *table,
1326 const char *string)
1328 /* Allocate the structure if it has not already been allocated by a
1329 subclass. */
1330 if (entry == NULL)
1332 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
1333 if (entry == NULL)
1334 return entry;
1337 /* Call the allocation method of the superclass. */
1338 entry = _bfd_link_hash_newfunc (entry, table, string);
1339 if (entry != NULL)
1341 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
1342 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
1344 /* Set local fields. */
1345 ret->indx = -1;
1346 ret->dynindx = -1;
1347 ret->dynstr_index = 0;
1348 ret->elf_hash_value = 0;
1349 ret->weakdef = NULL;
1350 ret->verinfo.verdef = NULL;
1351 ret->vtable_entries_size = 0;
1352 ret->vtable_entries_used = NULL;
1353 ret->vtable_parent = NULL;
1354 ret->got = htab->init_refcount;
1355 ret->plt = htab->init_refcount;
1356 ret->size = 0;
1357 ret->type = STT_NOTYPE;
1358 ret->other = 0;
1359 /* Assume that we have been called by a non-ELF symbol reader.
1360 This flag is then reset by the code which reads an ELF input
1361 file. This ensures that a symbol created by a non-ELF symbol
1362 reader will have the flag set correctly. */
1363 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
1366 return entry;
1369 /* Copy data from an indirect symbol to its direct symbol, hiding the
1370 old indirect symbol. Also used for copying flags to a weakdef. */
1372 void
1373 _bfd_elf_link_hash_copy_indirect (const struct elf_backend_data *bed,
1374 struct elf_link_hash_entry *dir,
1375 struct elf_link_hash_entry *ind)
1377 bfd_signed_vma tmp;
1378 bfd_signed_vma lowest_valid = bed->can_refcount;
1380 /* Copy down any references that we may have already seen to the
1381 symbol which just became indirect. */
1383 dir->elf_link_hash_flags
1384 |= ind->elf_link_hash_flags & (ELF_LINK_HASH_REF_DYNAMIC
1385 | ELF_LINK_HASH_REF_REGULAR
1386 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1387 | ELF_LINK_NON_GOT_REF
1388 | ELF_LINK_HASH_NEEDS_PLT
1389 | ELF_LINK_POINTER_EQUALITY_NEEDED);
1391 if (ind->root.type != bfd_link_hash_indirect)
1392 return;
1394 /* Copy over the global and procedure linkage table refcount entries.
1395 These may have been already set up by a check_relocs routine. */
1396 tmp = dir->got.refcount;
1397 if (tmp < lowest_valid)
1399 dir->got.refcount = ind->got.refcount;
1400 ind->got.refcount = tmp;
1402 else
1403 BFD_ASSERT (ind->got.refcount < lowest_valid);
1405 tmp = dir->plt.refcount;
1406 if (tmp < lowest_valid)
1408 dir->plt.refcount = ind->plt.refcount;
1409 ind->plt.refcount = tmp;
1411 else
1412 BFD_ASSERT (ind->plt.refcount < lowest_valid);
1414 if (dir->dynindx == -1)
1416 dir->dynindx = ind->dynindx;
1417 dir->dynstr_index = ind->dynstr_index;
1418 ind->dynindx = -1;
1419 ind->dynstr_index = 0;
1421 else
1422 BFD_ASSERT (ind->dynindx == -1);
1425 void
1426 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
1427 struct elf_link_hash_entry *h,
1428 bfd_boolean force_local)
1430 h->plt = elf_hash_table (info)->init_offset;
1431 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1432 if (force_local)
1434 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
1435 if (h->dynindx != -1)
1437 h->dynindx = -1;
1438 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1439 h->dynstr_index);
1444 /* Initialize an ELF linker hash table. */
1446 bfd_boolean
1447 _bfd_elf_link_hash_table_init
1448 (struct elf_link_hash_table *table,
1449 bfd *abfd,
1450 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
1451 struct bfd_hash_table *,
1452 const char *))
1454 bfd_boolean ret;
1456 table->dynamic_sections_created = FALSE;
1457 table->dynobj = NULL;
1458 /* Make sure can_refcount is extended to the width and signedness of
1459 init_refcount before we subtract one from it. */
1460 table->init_refcount.refcount = get_elf_backend_data (abfd)->can_refcount;
1461 table->init_refcount.refcount -= 1;
1462 table->init_offset.offset = -(bfd_vma) 1;
1463 /* The first dynamic symbol is a dummy. */
1464 table->dynsymcount = 1;
1465 table->dynstr = NULL;
1466 table->bucketcount = 0;
1467 table->needed = NULL;
1468 table->hgot = NULL;
1469 table->merge_info = NULL;
1470 memset (&table->stab_info, 0, sizeof (table->stab_info));
1471 memset (&table->eh_info, 0, sizeof (table->eh_info));
1472 table->dynlocal = NULL;
1473 table->runpath = NULL;
1474 table->tls_sec = NULL;
1475 table->tls_size = 0;
1476 table->loaded = NULL;
1478 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc);
1479 table->root.type = bfd_link_elf_hash_table;
1481 return ret;
1484 /* Create an ELF linker hash table. */
1486 struct bfd_link_hash_table *
1487 _bfd_elf_link_hash_table_create (bfd *abfd)
1489 struct elf_link_hash_table *ret;
1490 bfd_size_type amt = sizeof (struct elf_link_hash_table);
1492 ret = bfd_malloc (amt);
1493 if (ret == NULL)
1494 return NULL;
1496 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
1498 free (ret);
1499 return NULL;
1502 return &ret->root;
1505 /* This is a hook for the ELF emulation code in the generic linker to
1506 tell the backend linker what file name to use for the DT_NEEDED
1507 entry for a dynamic object. */
1509 void
1510 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
1512 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1513 && bfd_get_format (abfd) == bfd_object)
1514 elf_dt_name (abfd) = name;
1518 bfd_elf_get_dyn_lib_class (bfd *abfd)
1520 int lib_class;
1521 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1522 && bfd_get_format (abfd) == bfd_object)
1523 lib_class = elf_dyn_lib_class (abfd);
1524 else
1525 lib_class = 0;
1526 return lib_class;
1529 void
1530 bfd_elf_set_dyn_lib_class (bfd *abfd, int lib_class)
1532 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1533 && bfd_get_format (abfd) == bfd_object)
1534 elf_dyn_lib_class (abfd) = lib_class;
1537 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1538 the linker ELF emulation code. */
1540 struct bfd_link_needed_list *
1541 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
1542 struct bfd_link_info *info)
1544 if (! is_elf_hash_table (info->hash))
1545 return NULL;
1546 return elf_hash_table (info)->needed;
1549 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1550 hook for the linker ELF emulation code. */
1552 struct bfd_link_needed_list *
1553 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
1554 struct bfd_link_info *info)
1556 if (! is_elf_hash_table (info->hash))
1557 return NULL;
1558 return elf_hash_table (info)->runpath;
1561 /* Get the name actually used for a dynamic object for a link. This
1562 is the SONAME entry if there is one. Otherwise, it is the string
1563 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1565 const char *
1566 bfd_elf_get_dt_soname (bfd *abfd)
1568 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1569 && bfd_get_format (abfd) == bfd_object)
1570 return elf_dt_name (abfd);
1571 return NULL;
1574 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1575 the ELF linker emulation code. */
1577 bfd_boolean
1578 bfd_elf_get_bfd_needed_list (bfd *abfd,
1579 struct bfd_link_needed_list **pneeded)
1581 asection *s;
1582 bfd_byte *dynbuf = NULL;
1583 int elfsec;
1584 unsigned long shlink;
1585 bfd_byte *extdyn, *extdynend;
1586 size_t extdynsize;
1587 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1589 *pneeded = NULL;
1591 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1592 || bfd_get_format (abfd) != bfd_object)
1593 return TRUE;
1595 s = bfd_get_section_by_name (abfd, ".dynamic");
1596 if (s == NULL || s->size == 0)
1597 return TRUE;
1599 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1600 goto error_return;
1602 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1603 if (elfsec == -1)
1604 goto error_return;
1606 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1608 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1609 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1611 extdyn = dynbuf;
1612 extdynend = extdyn + s->size;
1613 for (; extdyn < extdynend; extdyn += extdynsize)
1615 Elf_Internal_Dyn dyn;
1617 (*swap_dyn_in) (abfd, extdyn, &dyn);
1619 if (dyn.d_tag == DT_NULL)
1620 break;
1622 if (dyn.d_tag == DT_NEEDED)
1624 const char *string;
1625 struct bfd_link_needed_list *l;
1626 unsigned int tagv = dyn.d_un.d_val;
1627 bfd_size_type amt;
1629 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1630 if (string == NULL)
1631 goto error_return;
1633 amt = sizeof *l;
1634 l = bfd_alloc (abfd, amt);
1635 if (l == NULL)
1636 goto error_return;
1638 l->by = abfd;
1639 l->name = string;
1640 l->next = *pneeded;
1641 *pneeded = l;
1645 free (dynbuf);
1647 return TRUE;
1649 error_return:
1650 if (dynbuf != NULL)
1651 free (dynbuf);
1652 return FALSE;
1655 /* Allocate an ELF string table--force the first byte to be zero. */
1657 struct bfd_strtab_hash *
1658 _bfd_elf_stringtab_init (void)
1660 struct bfd_strtab_hash *ret;
1662 ret = _bfd_stringtab_init ();
1663 if (ret != NULL)
1665 bfd_size_type loc;
1667 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1668 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1669 if (loc == (bfd_size_type) -1)
1671 _bfd_stringtab_free (ret);
1672 ret = NULL;
1675 return ret;
1678 /* ELF .o/exec file reading */
1680 /* Create a new bfd section from an ELF section header. */
1682 bfd_boolean
1683 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1685 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1686 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1687 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1688 const char *name;
1690 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1692 switch (hdr->sh_type)
1694 case SHT_NULL:
1695 /* Inactive section. Throw it away. */
1696 return TRUE;
1698 case SHT_PROGBITS: /* Normal section with contents. */
1699 case SHT_NOBITS: /* .bss section. */
1700 case SHT_HASH: /* .hash section. */
1701 case SHT_NOTE: /* .note section. */
1702 case SHT_INIT_ARRAY: /* .init_array section. */
1703 case SHT_FINI_ARRAY: /* .fini_array section. */
1704 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1705 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1707 case SHT_DYNAMIC: /* Dynamic linking information. */
1708 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1709 return FALSE;
1710 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1712 Elf_Internal_Shdr *dynsymhdr;
1714 /* The shared libraries distributed with hpux11 have a bogus
1715 sh_link field for the ".dynamic" section. Find the
1716 string table for the ".dynsym" section instead. */
1717 if (elf_dynsymtab (abfd) != 0)
1719 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1720 hdr->sh_link = dynsymhdr->sh_link;
1722 else
1724 unsigned int i, num_sec;
1726 num_sec = elf_numsections (abfd);
1727 for (i = 1; i < num_sec; i++)
1729 dynsymhdr = elf_elfsections (abfd)[i];
1730 if (dynsymhdr->sh_type == SHT_DYNSYM)
1732 hdr->sh_link = dynsymhdr->sh_link;
1733 break;
1738 break;
1740 case SHT_SYMTAB: /* A symbol table */
1741 if (elf_onesymtab (abfd) == shindex)
1742 return TRUE;
1744 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1745 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1746 elf_onesymtab (abfd) = shindex;
1747 elf_tdata (abfd)->symtab_hdr = *hdr;
1748 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1749 abfd->flags |= HAS_SYMS;
1751 /* Sometimes a shared object will map in the symbol table. If
1752 SHF_ALLOC is set, and this is a shared object, then we also
1753 treat this section as a BFD section. We can not base the
1754 decision purely on SHF_ALLOC, because that flag is sometimes
1755 set in a relocatable object file, which would confuse the
1756 linker. */
1757 if ((hdr->sh_flags & SHF_ALLOC) != 0
1758 && (abfd->flags & DYNAMIC) != 0
1759 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1760 return FALSE;
1762 return TRUE;
1764 case SHT_DYNSYM: /* A dynamic symbol table */
1765 if (elf_dynsymtab (abfd) == shindex)
1766 return TRUE;
1768 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1769 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1770 elf_dynsymtab (abfd) = shindex;
1771 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1772 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1773 abfd->flags |= HAS_SYMS;
1775 /* Besides being a symbol table, we also treat this as a regular
1776 section, so that objcopy can handle it. */
1777 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1779 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1780 if (elf_symtab_shndx (abfd) == shindex)
1781 return TRUE;
1783 /* Get the associated symbol table. */
1784 if (! bfd_section_from_shdr (abfd, hdr->sh_link)
1785 || hdr->sh_link != elf_onesymtab (abfd))
1786 return FALSE;
1788 elf_symtab_shndx (abfd) = shindex;
1789 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1790 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1791 return TRUE;
1793 case SHT_STRTAB: /* A string table */
1794 if (hdr->bfd_section != NULL)
1795 return TRUE;
1796 if (ehdr->e_shstrndx == shindex)
1798 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1799 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1800 return TRUE;
1803 unsigned int i, num_sec;
1805 num_sec = elf_numsections (abfd);
1806 for (i = 1; i < num_sec; i++)
1808 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1809 if (hdr2->sh_link == shindex)
1811 if (! bfd_section_from_shdr (abfd, i))
1812 return FALSE;
1813 if (elf_onesymtab (abfd) == i)
1815 elf_tdata (abfd)->strtab_hdr = *hdr;
1816 elf_elfsections (abfd)[shindex] =
1817 &elf_tdata (abfd)->strtab_hdr;
1818 return TRUE;
1820 if (elf_dynsymtab (abfd) == i)
1822 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1823 elf_elfsections (abfd)[shindex] = hdr =
1824 &elf_tdata (abfd)->dynstrtab_hdr;
1825 /* We also treat this as a regular section, so
1826 that objcopy can handle it. */
1827 break;
1829 #if 0 /* Not handling other string tables specially right now. */
1830 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1831 /* We have a strtab for some random other section. */
1832 newsect = (asection *) hdr2->bfd_section;
1833 if (!newsect)
1834 break;
1835 hdr->bfd_section = newsect;
1836 hdr2 = &elf_section_data (newsect)->str_hdr;
1837 *hdr2 = *hdr;
1838 elf_elfsections (abfd)[shindex] = hdr2;
1839 #endif
1844 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1846 case SHT_REL:
1847 case SHT_RELA:
1848 /* *These* do a lot of work -- but build no sections! */
1850 asection *target_sect;
1851 Elf_Internal_Shdr *hdr2;
1852 unsigned int num_sec = elf_numsections (abfd);
1854 /* Check for a bogus link to avoid crashing. */
1855 if ((hdr->sh_link >= SHN_LORESERVE && hdr->sh_link <= SHN_HIRESERVE)
1856 || hdr->sh_link >= num_sec)
1858 ((*_bfd_error_handler)
1859 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1860 bfd_archive_filename (abfd), hdr->sh_link, name, shindex));
1861 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1864 /* For some incomprehensible reason Oracle distributes
1865 libraries for Solaris in which some of the objects have
1866 bogus sh_link fields. It would be nice if we could just
1867 reject them, but, unfortunately, some people need to use
1868 them. We scan through the section headers; if we find only
1869 one suitable symbol table, we clobber the sh_link to point
1870 to it. I hope this doesn't break anything. */
1871 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1872 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1874 unsigned int scan;
1875 int found;
1877 found = 0;
1878 for (scan = 1; scan < num_sec; scan++)
1880 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1881 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1883 if (found != 0)
1885 found = 0;
1886 break;
1888 found = scan;
1891 if (found != 0)
1892 hdr->sh_link = found;
1895 /* Get the symbol table. */
1896 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1897 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1898 return FALSE;
1900 /* If this reloc section does not use the main symbol table we
1901 don't treat it as a reloc section. BFD can't adequately
1902 represent such a section, so at least for now, we don't
1903 try. We just present it as a normal section. We also
1904 can't use it as a reloc section if it points to the null
1905 section. */
1906 if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
1907 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1909 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1910 return FALSE;
1911 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1912 if (target_sect == NULL)
1913 return FALSE;
1915 if ((target_sect->flags & SEC_RELOC) == 0
1916 || target_sect->reloc_count == 0)
1917 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1918 else
1920 bfd_size_type amt;
1921 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1922 amt = sizeof (*hdr2);
1923 hdr2 = bfd_alloc (abfd, amt);
1924 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1926 *hdr2 = *hdr;
1927 elf_elfsections (abfd)[shindex] = hdr2;
1928 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1929 target_sect->flags |= SEC_RELOC;
1930 target_sect->relocation = NULL;
1931 target_sect->rel_filepos = hdr->sh_offset;
1932 /* In the section to which the relocations apply, mark whether
1933 its relocations are of the REL or RELA variety. */
1934 if (hdr->sh_size != 0)
1935 target_sect->use_rela_p = hdr->sh_type == SHT_RELA;
1936 abfd->flags |= HAS_RELOC;
1937 return TRUE;
1939 break;
1941 case SHT_GNU_verdef:
1942 elf_dynverdef (abfd) = shindex;
1943 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1944 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1945 break;
1947 case SHT_GNU_versym:
1948 elf_dynversym (abfd) = shindex;
1949 elf_tdata (abfd)->dynversym_hdr = *hdr;
1950 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1951 break;
1953 case SHT_GNU_verneed:
1954 elf_dynverref (abfd) = shindex;
1955 elf_tdata (abfd)->dynverref_hdr = *hdr;
1956 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1957 break;
1959 case SHT_SHLIB:
1960 return TRUE;
1962 case SHT_GROUP:
1963 /* We need a BFD section for objcopy and relocatable linking,
1964 and it's handy to have the signature available as the section
1965 name. */
1966 name = group_signature (abfd, hdr);
1967 if (name == NULL)
1968 return FALSE;
1969 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name))
1970 return FALSE;
1971 if (hdr->contents != NULL)
1973 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1974 unsigned int n_elt = hdr->sh_size / 4;
1975 asection *s;
1977 if (idx->flags & GRP_COMDAT)
1978 hdr->bfd_section->flags
1979 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1981 /* We try to keep the same section order as it comes in. */
1982 idx += n_elt;
1983 while (--n_elt != 0)
1984 if ((s = (--idx)->shdr->bfd_section) != NULL
1985 && elf_next_in_group (s) != NULL)
1987 elf_next_in_group (hdr->bfd_section) = s;
1988 break;
1991 break;
1993 default:
1994 /* Check for any processor-specific section types. */
1996 if (bed->elf_backend_section_from_shdr)
1997 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1999 break;
2002 return TRUE;
2005 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
2006 Return SEC for sections that have no elf section, and NULL on error. */
2008 asection *
2009 bfd_section_from_r_symndx (bfd *abfd,
2010 struct sym_sec_cache *cache,
2011 asection *sec,
2012 unsigned long r_symndx)
2014 Elf_Internal_Shdr *symtab_hdr;
2015 unsigned char esym[sizeof (Elf64_External_Sym)];
2016 Elf_External_Sym_Shndx eshndx;
2017 Elf_Internal_Sym isym;
2018 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2020 if (cache->abfd == abfd && cache->indx[ent] == r_symndx)
2021 return cache->sec[ent];
2023 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2024 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2025 &isym, esym, &eshndx) == NULL)
2026 return NULL;
2028 if (cache->abfd != abfd)
2030 memset (cache->indx, -1, sizeof (cache->indx));
2031 cache->abfd = abfd;
2033 cache->indx[ent] = r_symndx;
2034 cache->sec[ent] = sec;
2035 if ((isym.st_shndx != SHN_UNDEF && isym.st_shndx < SHN_LORESERVE)
2036 || isym.st_shndx > SHN_HIRESERVE)
2038 asection *s;
2039 s = bfd_section_from_elf_index (abfd, isym.st_shndx);
2040 if (s != NULL)
2041 cache->sec[ent] = s;
2043 return cache->sec[ent];
2046 /* Given an ELF section number, retrieve the corresponding BFD
2047 section. */
2049 asection *
2050 bfd_section_from_elf_index (bfd *abfd, unsigned int index)
2052 if (index >= elf_numsections (abfd))
2053 return NULL;
2054 return elf_elfsections (abfd)[index]->bfd_section;
2057 static struct bfd_elf_special_section const special_sections[] =
2059 { ".bss", 4, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2060 { ".comment", 8, 0, SHT_PROGBITS, 0 },
2061 { ".data", 5, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2062 { ".data1", 6, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2063 { ".debug", 6, 0, SHT_PROGBITS, 0 },
2064 { ".fini", 5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2065 { ".init", 5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2066 { ".line", 5, 0, SHT_PROGBITS, 0 },
2067 { ".rodata", 7, -2, SHT_PROGBITS, SHF_ALLOC },
2068 { ".rodata1", 8, 0, SHT_PROGBITS, SHF_ALLOC },
2069 { ".tbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2070 { ".tdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2071 { ".text", 5, -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2072 { ".init_array", 11, 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2073 { ".fini_array", 11, 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2074 { ".preinit_array", 14, 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2075 { ".debug_line", 11, 0, SHT_PROGBITS, 0 },
2076 { ".debug_info", 11, 0, SHT_PROGBITS, 0 },
2077 { ".debug_abbrev", 13, 0, SHT_PROGBITS, 0 },
2078 { ".debug_aranges", 14, 0, SHT_PROGBITS, 0 },
2079 { ".dynamic", 8, 0, SHT_DYNAMIC, SHF_ALLOC },
2080 { ".dynstr", 7, 0, SHT_STRTAB, SHF_ALLOC },
2081 { ".dynsym", 7, 0, SHT_DYNSYM, SHF_ALLOC },
2082 { ".got", 4, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2083 { ".hash", 5, 0, SHT_HASH, SHF_ALLOC },
2084 { ".interp", 7, 0, SHT_PROGBITS, 0 },
2085 { ".plt", 4, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2086 { ".shstrtab", 9, 0, SHT_STRTAB, 0 },
2087 { ".strtab", 7, 0, SHT_STRTAB, 0 },
2088 { ".symtab", 7, 0, SHT_SYMTAB, 0 },
2089 { ".gnu.version", 12, 0, SHT_GNU_versym, 0 },
2090 { ".gnu.version_d", 14, 0, SHT_GNU_verdef, 0 },
2091 { ".gnu.version_r", 14, 0, SHT_GNU_verneed, 0 },
2092 { ".note.GNU-stack",15, 0, SHT_PROGBITS, 0 },
2093 { ".note", 5, -1, SHT_NOTE, 0 },
2094 { ".rela", 5, -1, SHT_RELA, 0 },
2095 { ".rel", 4, -1, SHT_REL, 0 },
2096 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2097 { NULL, 0, 0, 0, 0 }
2100 static const struct bfd_elf_special_section *
2101 get_special_section (const char *name,
2102 const struct bfd_elf_special_section *special_sections,
2103 unsigned int rela)
2105 int i;
2106 int len = strlen (name);
2108 for (i = 0; special_sections[i].prefix != NULL; i++)
2110 int suffix_len;
2111 int prefix_len = special_sections[i].prefix_length;
2113 if (len < prefix_len)
2114 continue;
2115 if (memcmp (name, special_sections[i].prefix, prefix_len) != 0)
2116 continue;
2118 suffix_len = special_sections[i].suffix_length;
2119 if (suffix_len <= 0)
2121 if (name[prefix_len] != 0)
2123 if (suffix_len == 0)
2124 continue;
2125 if (name[prefix_len] != '.'
2126 && (suffix_len == -2
2127 || (rela && special_sections[i].type == SHT_REL)))
2128 continue;
2131 else
2133 if (len < prefix_len + suffix_len)
2134 continue;
2135 if (memcmp (name + len - suffix_len,
2136 special_sections[i].prefix + prefix_len,
2137 suffix_len) != 0)
2138 continue;
2140 return &special_sections[i];
2143 return NULL;
2146 const struct bfd_elf_special_section *
2147 _bfd_elf_get_sec_type_attr (bfd *abfd, const char *name)
2149 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2150 const struct bfd_elf_special_section *ssect = NULL;
2152 /* See if this is one of the special sections. */
2153 if (name)
2155 unsigned int rela = bed->default_use_rela_p;
2157 if (bed->special_sections)
2158 ssect = get_special_section (name, bed->special_sections, rela);
2160 if (! ssect)
2161 ssect = get_special_section (name, special_sections, rela);
2164 return ssect;
2167 bfd_boolean
2168 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2170 struct bfd_elf_section_data *sdata;
2171 const struct bfd_elf_special_section *ssect;
2173 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2174 if (sdata == NULL)
2176 sdata = bfd_zalloc (abfd, sizeof (*sdata));
2177 if (sdata == NULL)
2178 return FALSE;
2179 sec->used_by_bfd = sdata;
2182 elf_section_type (sec) = SHT_NULL;
2183 ssect = _bfd_elf_get_sec_type_attr (abfd, sec->name);
2184 if (ssect != NULL)
2186 elf_section_type (sec) = ssect->type;
2187 elf_section_flags (sec) = ssect->attr;
2190 /* Indicate whether or not this section should use RELA relocations. */
2191 sec->use_rela_p = get_elf_backend_data (abfd)->default_use_rela_p;
2193 return TRUE;
2196 /* Create a new bfd section from an ELF program header.
2198 Since program segments have no names, we generate a synthetic name
2199 of the form segment<NUM>, where NUM is generally the index in the
2200 program header table. For segments that are split (see below) we
2201 generate the names segment<NUM>a and segment<NUM>b.
2203 Note that some program segments may have a file size that is different than
2204 (less than) the memory size. All this means is that at execution the
2205 system must allocate the amount of memory specified by the memory size,
2206 but only initialize it with the first "file size" bytes read from the
2207 file. This would occur for example, with program segments consisting
2208 of combined data+bss.
2210 To handle the above situation, this routine generates TWO bfd sections
2211 for the single program segment. The first has the length specified by
2212 the file size of the segment, and the second has the length specified
2213 by the difference between the two sizes. In effect, the segment is split
2214 into it's initialized and uninitialized parts.
2218 bfd_boolean
2219 _bfd_elf_make_section_from_phdr (bfd *abfd,
2220 Elf_Internal_Phdr *hdr,
2221 int index,
2222 const char *typename)
2224 asection *newsect;
2225 char *name;
2226 char namebuf[64];
2227 size_t len;
2228 int split;
2230 split = ((hdr->p_memsz > 0)
2231 && (hdr->p_filesz > 0)
2232 && (hdr->p_memsz > hdr->p_filesz));
2233 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
2234 len = strlen (namebuf) + 1;
2235 name = bfd_alloc (abfd, len);
2236 if (!name)
2237 return FALSE;
2238 memcpy (name, namebuf, len);
2239 newsect = bfd_make_section (abfd, name);
2240 if (newsect == NULL)
2241 return FALSE;
2242 newsect->vma = hdr->p_vaddr;
2243 newsect->lma = hdr->p_paddr;
2244 newsect->size = hdr->p_filesz;
2245 newsect->filepos = hdr->p_offset;
2246 newsect->flags |= SEC_HAS_CONTENTS;
2247 newsect->alignment_power = bfd_log2 (hdr->p_align);
2248 if (hdr->p_type == PT_LOAD)
2250 newsect->flags |= SEC_ALLOC;
2251 newsect->flags |= SEC_LOAD;
2252 if (hdr->p_flags & PF_X)
2254 /* FIXME: all we known is that it has execute PERMISSION,
2255 may be data. */
2256 newsect->flags |= SEC_CODE;
2259 if (!(hdr->p_flags & PF_W))
2261 newsect->flags |= SEC_READONLY;
2264 if (split)
2266 sprintf (namebuf, "%s%db", typename, index);
2267 len = strlen (namebuf) + 1;
2268 name = bfd_alloc (abfd, len);
2269 if (!name)
2270 return FALSE;
2271 memcpy (name, namebuf, len);
2272 newsect = bfd_make_section (abfd, name);
2273 if (newsect == NULL)
2274 return FALSE;
2275 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2276 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2277 newsect->size = hdr->p_memsz - hdr->p_filesz;
2278 if (hdr->p_type == PT_LOAD)
2280 newsect->flags |= SEC_ALLOC;
2281 if (hdr->p_flags & PF_X)
2282 newsect->flags |= SEC_CODE;
2284 if (!(hdr->p_flags & PF_W))
2285 newsect->flags |= SEC_READONLY;
2288 return TRUE;
2291 bfd_boolean
2292 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index)
2294 const struct elf_backend_data *bed;
2296 switch (hdr->p_type)
2298 case PT_NULL:
2299 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2301 case PT_LOAD:
2302 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2304 case PT_DYNAMIC:
2305 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2307 case PT_INTERP:
2308 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2310 case PT_NOTE:
2311 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2312 return FALSE;
2313 if (! elfcore_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2314 return FALSE;
2315 return TRUE;
2317 case PT_SHLIB:
2318 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2320 case PT_PHDR:
2321 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2323 case PT_GNU_EH_FRAME:
2324 return _bfd_elf_make_section_from_phdr (abfd, hdr, index,
2325 "eh_frame_hdr");
2327 case PT_GNU_STACK:
2328 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "stack");
2330 case PT_GNU_RELRO:
2331 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro");
2333 default:
2334 /* Check for any processor-specific program segment types.
2335 If no handler for them, default to making "segment" sections. */
2336 bed = get_elf_backend_data (abfd);
2337 if (bed->elf_backend_section_from_phdr)
2338 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
2339 else
2340 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
2344 /* Initialize REL_HDR, the section-header for new section, containing
2345 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2346 relocations; otherwise, we use REL relocations. */
2348 bfd_boolean
2349 _bfd_elf_init_reloc_shdr (bfd *abfd,
2350 Elf_Internal_Shdr *rel_hdr,
2351 asection *asect,
2352 bfd_boolean use_rela_p)
2354 char *name;
2355 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2356 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2358 name = bfd_alloc (abfd, amt);
2359 if (name == NULL)
2360 return FALSE;
2361 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2362 rel_hdr->sh_name =
2363 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2364 FALSE);
2365 if (rel_hdr->sh_name == (unsigned int) -1)
2366 return FALSE;
2367 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2368 rel_hdr->sh_entsize = (use_rela_p
2369 ? bed->s->sizeof_rela
2370 : bed->s->sizeof_rel);
2371 rel_hdr->sh_addralign = 1 << bed->s->log_file_align;
2372 rel_hdr->sh_flags = 0;
2373 rel_hdr->sh_addr = 0;
2374 rel_hdr->sh_size = 0;
2375 rel_hdr->sh_offset = 0;
2377 return TRUE;
2380 /* Set up an ELF internal section header for a section. */
2382 static void
2383 elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg)
2385 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2386 bfd_boolean *failedptr = failedptrarg;
2387 Elf_Internal_Shdr *this_hdr;
2389 if (*failedptr)
2391 /* We already failed; just get out of the bfd_map_over_sections
2392 loop. */
2393 return;
2396 this_hdr = &elf_section_data (asect)->this_hdr;
2398 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2399 asect->name, FALSE);
2400 if (this_hdr->sh_name == (unsigned int) -1)
2402 *failedptr = TRUE;
2403 return;
2406 this_hdr->sh_flags = 0;
2408 if ((asect->flags & SEC_ALLOC) != 0
2409 || asect->user_set_vma)
2410 this_hdr->sh_addr = asect->vma;
2411 else
2412 this_hdr->sh_addr = 0;
2414 this_hdr->sh_offset = 0;
2415 this_hdr->sh_size = asect->size;
2416 this_hdr->sh_link = 0;
2417 this_hdr->sh_addralign = 1 << asect->alignment_power;
2418 /* The sh_entsize and sh_info fields may have been set already by
2419 copy_private_section_data. */
2421 this_hdr->bfd_section = asect;
2422 this_hdr->contents = NULL;
2424 /* If the section type is unspecified, we set it based on
2425 asect->flags. */
2426 if (this_hdr->sh_type == SHT_NULL)
2428 if ((asect->flags & SEC_GROUP) != 0)
2430 /* We also need to mark SHF_GROUP here for relocatable
2431 link. */
2432 struct bfd_link_order *l;
2433 asection *elt;
2435 for (l = asect->link_order_head; l != NULL; l = l->next)
2436 if (l->type == bfd_indirect_link_order
2437 && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
2440 /* The name is not important. Anything will do. */
2441 elf_group_name (elt->output_section) = "G";
2442 elf_section_flags (elt->output_section) |= SHF_GROUP;
2444 elt = elf_next_in_group (elt);
2445 /* During a relocatable link, the lists are
2446 circular. */
2448 while (elt != elf_next_in_group (l->u.indirect.section));
2450 this_hdr->sh_type = SHT_GROUP;
2452 else if ((asect->flags & SEC_ALLOC) != 0
2453 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2454 || (asect->flags & SEC_NEVER_LOAD) != 0))
2455 this_hdr->sh_type = SHT_NOBITS;
2456 else
2457 this_hdr->sh_type = SHT_PROGBITS;
2460 switch (this_hdr->sh_type)
2462 default:
2463 break;
2465 case SHT_STRTAB:
2466 case SHT_INIT_ARRAY:
2467 case SHT_FINI_ARRAY:
2468 case SHT_PREINIT_ARRAY:
2469 case SHT_NOTE:
2470 case SHT_NOBITS:
2471 case SHT_PROGBITS:
2472 break;
2474 case SHT_HASH:
2475 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2476 break;
2478 case SHT_DYNSYM:
2479 this_hdr->sh_entsize = bed->s->sizeof_sym;
2480 break;
2482 case SHT_DYNAMIC:
2483 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2484 break;
2486 case SHT_RELA:
2487 if (get_elf_backend_data (abfd)->may_use_rela_p)
2488 this_hdr->sh_entsize = bed->s->sizeof_rela;
2489 break;
2491 case SHT_REL:
2492 if (get_elf_backend_data (abfd)->may_use_rel_p)
2493 this_hdr->sh_entsize = bed->s->sizeof_rel;
2494 break;
2496 case SHT_GNU_versym:
2497 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2498 break;
2500 case SHT_GNU_verdef:
2501 this_hdr->sh_entsize = 0;
2502 /* objcopy or strip will copy over sh_info, but may not set
2503 cverdefs. The linker will set cverdefs, but sh_info will be
2504 zero. */
2505 if (this_hdr->sh_info == 0)
2506 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2507 else
2508 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2509 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2510 break;
2512 case SHT_GNU_verneed:
2513 this_hdr->sh_entsize = 0;
2514 /* objcopy or strip will copy over sh_info, but may not set
2515 cverrefs. The linker will set cverrefs, but sh_info will be
2516 zero. */
2517 if (this_hdr->sh_info == 0)
2518 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2519 else
2520 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2521 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2522 break;
2524 case SHT_GROUP:
2525 this_hdr->sh_entsize = 4;
2526 break;
2529 if ((asect->flags & SEC_ALLOC) != 0)
2530 this_hdr->sh_flags |= SHF_ALLOC;
2531 if ((asect->flags & SEC_READONLY) == 0)
2532 this_hdr->sh_flags |= SHF_WRITE;
2533 if ((asect->flags & SEC_CODE) != 0)
2534 this_hdr->sh_flags |= SHF_EXECINSTR;
2535 if ((asect->flags & SEC_MERGE) != 0)
2537 this_hdr->sh_flags |= SHF_MERGE;
2538 this_hdr->sh_entsize = asect->entsize;
2539 if ((asect->flags & SEC_STRINGS) != 0)
2540 this_hdr->sh_flags |= SHF_STRINGS;
2542 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2543 this_hdr->sh_flags |= SHF_GROUP;
2544 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2546 this_hdr->sh_flags |= SHF_TLS;
2547 if (asect->size == 0 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2549 struct bfd_link_order *o;
2551 this_hdr->sh_size = 0;
2552 for (o = asect->link_order_head; o != NULL; o = o->next)
2553 if (this_hdr->sh_size < o->offset + o->size)
2554 this_hdr->sh_size = o->offset + o->size;
2555 if (this_hdr->sh_size)
2556 this_hdr->sh_type = SHT_NOBITS;
2560 /* Check for processor-specific section types. */
2561 if (bed->elf_backend_fake_sections
2562 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2563 *failedptr = TRUE;
2565 /* If the section has relocs, set up a section header for the
2566 SHT_REL[A] section. If two relocation sections are required for
2567 this section, it is up to the processor-specific back-end to
2568 create the other. */
2569 if ((asect->flags & SEC_RELOC) != 0
2570 && !_bfd_elf_init_reloc_shdr (abfd,
2571 &elf_section_data (asect)->rel_hdr,
2572 asect,
2573 asect->use_rela_p))
2574 *failedptr = TRUE;
2577 /* Fill in the contents of a SHT_GROUP section. */
2579 void
2580 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2582 bfd_boolean *failedptr = failedptrarg;
2583 unsigned long symindx;
2584 asection *elt, *first;
2585 unsigned char *loc;
2586 struct bfd_link_order *l;
2587 bfd_boolean gas;
2589 if (elf_section_data (sec)->this_hdr.sh_type != SHT_GROUP
2590 || *failedptr)
2591 return;
2593 symindx = 0;
2594 if (elf_group_id (sec) != NULL)
2595 symindx = elf_group_id (sec)->udata.i;
2597 if (symindx == 0)
2599 /* If called from the assembler, swap_out_syms will have set up
2600 elf_section_syms; If called for "ld -r", use target_index. */
2601 if (elf_section_syms (abfd) != NULL)
2602 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2603 else
2604 symindx = sec->target_index;
2606 elf_section_data (sec)->this_hdr.sh_info = symindx;
2608 /* The contents won't be allocated for "ld -r" or objcopy. */
2609 gas = TRUE;
2610 if (sec->contents == NULL)
2612 gas = FALSE;
2613 sec->contents = bfd_alloc (abfd, sec->size);
2615 /* Arrange for the section to be written out. */
2616 elf_section_data (sec)->this_hdr.contents = sec->contents;
2617 if (sec->contents == NULL)
2619 *failedptr = TRUE;
2620 return;
2624 loc = sec->contents + sec->size;
2626 /* Get the pointer to the first section in the group that gas
2627 squirreled away here. objcopy arranges for this to be set to the
2628 start of the input section group. */
2629 first = elt = elf_next_in_group (sec);
2631 /* First element is a flag word. Rest of section is elf section
2632 indices for all the sections of the group. Write them backwards
2633 just to keep the group in the same order as given in .section
2634 directives, not that it matters. */
2635 while (elt != NULL)
2637 asection *s;
2638 unsigned int idx;
2640 loc -= 4;
2641 s = elt;
2642 if (!gas)
2643 s = s->output_section;
2644 idx = 0;
2645 if (s != NULL)
2646 idx = elf_section_data (s)->this_idx;
2647 H_PUT_32 (abfd, idx, loc);
2648 elt = elf_next_in_group (elt);
2649 if (elt == first)
2650 break;
2653 /* If this is a relocatable link, then the above did nothing because
2654 SEC is the output section. Look through the input sections
2655 instead. */
2656 for (l = sec->link_order_head; l != NULL; l = l->next)
2657 if (l->type == bfd_indirect_link_order
2658 && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
2661 loc -= 4;
2662 H_PUT_32 (abfd,
2663 elf_section_data (elt->output_section)->this_idx, loc);
2664 elt = elf_next_in_group (elt);
2665 /* During a relocatable link, the lists are circular. */
2667 while (elt != elf_next_in_group (l->u.indirect.section));
2669 /* With ld -r, merging SHT_GROUP sections results in wasted space
2670 due to allowing for the flag word on each input. We may well
2671 duplicate entries too. */
2672 while ((loc -= 4) > sec->contents)
2673 H_PUT_32 (abfd, 0, loc);
2675 if (loc != sec->contents)
2676 abort ();
2678 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2681 /* Assign all ELF section numbers. The dummy first section is handled here
2682 too. The link/info pointers for the standard section types are filled
2683 in here too, while we're at it. */
2685 static bfd_boolean
2686 assign_section_numbers (bfd *abfd)
2688 struct elf_obj_tdata *t = elf_tdata (abfd);
2689 asection *sec;
2690 unsigned int section_number, secn;
2691 Elf_Internal_Shdr **i_shdrp;
2692 bfd_size_type amt;
2694 section_number = 1;
2696 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2698 for (sec = abfd->sections; sec; sec = sec->next)
2700 struct bfd_elf_section_data *d = elf_section_data (sec);
2702 if (section_number == SHN_LORESERVE)
2703 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2704 d->this_idx = section_number++;
2705 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2706 if ((sec->flags & SEC_RELOC) == 0)
2707 d->rel_idx = 0;
2708 else
2710 if (section_number == SHN_LORESERVE)
2711 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2712 d->rel_idx = section_number++;
2713 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2716 if (d->rel_hdr2)
2718 if (section_number == SHN_LORESERVE)
2719 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2720 d->rel_idx2 = section_number++;
2721 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2723 else
2724 d->rel_idx2 = 0;
2727 if (section_number == SHN_LORESERVE)
2728 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2729 t->shstrtab_section = section_number++;
2730 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2731 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2733 if (bfd_get_symcount (abfd) > 0)
2735 if (section_number == SHN_LORESERVE)
2736 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2737 t->symtab_section = section_number++;
2738 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2739 if (section_number > SHN_LORESERVE - 2)
2741 if (section_number == SHN_LORESERVE)
2742 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2743 t->symtab_shndx_section = section_number++;
2744 t->symtab_shndx_hdr.sh_name
2745 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2746 ".symtab_shndx", FALSE);
2747 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2748 return FALSE;
2750 if (section_number == SHN_LORESERVE)
2751 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2752 t->strtab_section = section_number++;
2753 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2756 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2757 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2759 elf_numsections (abfd) = section_number;
2760 elf_elfheader (abfd)->e_shnum = section_number;
2761 if (section_number > SHN_LORESERVE)
2762 elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE;
2764 /* Set up the list of section header pointers, in agreement with the
2765 indices. */
2766 amt = section_number * sizeof (Elf_Internal_Shdr *);
2767 i_shdrp = bfd_zalloc (abfd, amt);
2768 if (i_shdrp == NULL)
2769 return FALSE;
2771 amt = sizeof (Elf_Internal_Shdr);
2772 i_shdrp[0] = bfd_zalloc (abfd, amt);
2773 if (i_shdrp[0] == NULL)
2775 bfd_release (abfd, i_shdrp);
2776 return FALSE;
2779 elf_elfsections (abfd) = i_shdrp;
2781 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2782 if (bfd_get_symcount (abfd) > 0)
2784 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2785 if (elf_numsections (abfd) > SHN_LORESERVE)
2787 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2788 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2790 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2791 t->symtab_hdr.sh_link = t->strtab_section;
2793 for (sec = abfd->sections; sec; sec = sec->next)
2795 struct bfd_elf_section_data *d = elf_section_data (sec);
2796 asection *s;
2797 const char *name;
2799 i_shdrp[d->this_idx] = &d->this_hdr;
2800 if (d->rel_idx != 0)
2801 i_shdrp[d->rel_idx] = &d->rel_hdr;
2802 if (d->rel_idx2 != 0)
2803 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2805 /* Fill in the sh_link and sh_info fields while we're at it. */
2807 /* sh_link of a reloc section is the section index of the symbol
2808 table. sh_info is the section index of the section to which
2809 the relocation entries apply. */
2810 if (d->rel_idx != 0)
2812 d->rel_hdr.sh_link = t->symtab_section;
2813 d->rel_hdr.sh_info = d->this_idx;
2815 if (d->rel_idx2 != 0)
2817 d->rel_hdr2->sh_link = t->symtab_section;
2818 d->rel_hdr2->sh_info = d->this_idx;
2821 switch (d->this_hdr.sh_type)
2823 case SHT_REL:
2824 case SHT_RELA:
2825 /* A reloc section which we are treating as a normal BFD
2826 section. sh_link is the section index of the symbol
2827 table. sh_info is the section index of the section to
2828 which the relocation entries apply. We assume that an
2829 allocated reloc section uses the dynamic symbol table.
2830 FIXME: How can we be sure? */
2831 s = bfd_get_section_by_name (abfd, ".dynsym");
2832 if (s != NULL)
2833 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2835 /* We look up the section the relocs apply to by name. */
2836 name = sec->name;
2837 if (d->this_hdr.sh_type == SHT_REL)
2838 name += 4;
2839 else
2840 name += 5;
2841 s = bfd_get_section_by_name (abfd, name);
2842 if (s != NULL)
2843 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
2844 break;
2846 case SHT_STRTAB:
2847 /* We assume that a section named .stab*str is a stabs
2848 string section. We look for a section with the same name
2849 but without the trailing ``str'', and set its sh_link
2850 field to point to this section. */
2851 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
2852 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
2854 size_t len;
2855 char *alc;
2857 len = strlen (sec->name);
2858 alc = bfd_malloc (len - 2);
2859 if (alc == NULL)
2860 return FALSE;
2861 memcpy (alc, sec->name, len - 3);
2862 alc[len - 3] = '\0';
2863 s = bfd_get_section_by_name (abfd, alc);
2864 free (alc);
2865 if (s != NULL)
2867 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
2869 /* This is a .stab section. */
2870 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
2871 elf_section_data (s)->this_hdr.sh_entsize
2872 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
2875 break;
2877 case SHT_DYNAMIC:
2878 case SHT_DYNSYM:
2879 case SHT_GNU_verneed:
2880 case SHT_GNU_verdef:
2881 /* sh_link is the section header index of the string table
2882 used for the dynamic entries, or the symbol table, or the
2883 version strings. */
2884 s = bfd_get_section_by_name (abfd, ".dynstr");
2885 if (s != NULL)
2886 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2887 break;
2889 case SHT_HASH:
2890 case SHT_GNU_versym:
2891 /* sh_link is the section header index of the symbol table
2892 this hash table or version table is for. */
2893 s = bfd_get_section_by_name (abfd, ".dynsym");
2894 if (s != NULL)
2895 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2896 break;
2898 case SHT_GROUP:
2899 d->this_hdr.sh_link = t->symtab_section;
2903 for (secn = 1; secn < section_number; ++secn)
2904 if (i_shdrp[secn] == NULL)
2905 i_shdrp[secn] = i_shdrp[0];
2906 else
2907 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
2908 i_shdrp[secn]->sh_name);
2909 return TRUE;
2912 /* Map symbol from it's internal number to the external number, moving
2913 all local symbols to be at the head of the list. */
2915 static int
2916 sym_is_global (bfd *abfd, asymbol *sym)
2918 /* If the backend has a special mapping, use it. */
2919 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2920 if (bed->elf_backend_sym_is_global)
2921 return (*bed->elf_backend_sym_is_global) (abfd, sym);
2923 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2924 || bfd_is_und_section (bfd_get_section (sym))
2925 || bfd_is_com_section (bfd_get_section (sym)));
2928 static bfd_boolean
2929 elf_map_symbols (bfd *abfd)
2931 unsigned int symcount = bfd_get_symcount (abfd);
2932 asymbol **syms = bfd_get_outsymbols (abfd);
2933 asymbol **sect_syms;
2934 unsigned int num_locals = 0;
2935 unsigned int num_globals = 0;
2936 unsigned int num_locals2 = 0;
2937 unsigned int num_globals2 = 0;
2938 int max_index = 0;
2939 unsigned int idx;
2940 asection *asect;
2941 asymbol **new_syms;
2942 bfd_size_type amt;
2944 #ifdef DEBUG
2945 fprintf (stderr, "elf_map_symbols\n");
2946 fflush (stderr);
2947 #endif
2949 for (asect = abfd->sections; asect; asect = asect->next)
2951 if (max_index < asect->index)
2952 max_index = asect->index;
2955 max_index++;
2956 amt = max_index * sizeof (asymbol *);
2957 sect_syms = bfd_zalloc (abfd, amt);
2958 if (sect_syms == NULL)
2959 return FALSE;
2960 elf_section_syms (abfd) = sect_syms;
2961 elf_num_section_syms (abfd) = max_index;
2963 /* Init sect_syms entries for any section symbols we have already
2964 decided to output. */
2965 for (idx = 0; idx < symcount; idx++)
2967 asymbol *sym = syms[idx];
2969 if ((sym->flags & BSF_SECTION_SYM) != 0
2970 && sym->value == 0)
2972 asection *sec;
2974 sec = sym->section;
2976 if (sec->owner != NULL)
2978 if (sec->owner != abfd)
2980 if (sec->output_offset != 0)
2981 continue;
2983 sec = sec->output_section;
2985 /* Empty sections in the input files may have had a
2986 section symbol created for them. (See the comment
2987 near the end of _bfd_generic_link_output_symbols in
2988 linker.c). If the linker script discards such
2989 sections then we will reach this point. Since we know
2990 that we cannot avoid this case, we detect it and skip
2991 the abort and the assignment to the sect_syms array.
2992 To reproduce this particular case try running the
2993 linker testsuite test ld-scripts/weak.exp for an ELF
2994 port that uses the generic linker. */
2995 if (sec->owner == NULL)
2996 continue;
2998 BFD_ASSERT (sec->owner == abfd);
3000 sect_syms[sec->index] = syms[idx];
3005 /* Classify all of the symbols. */
3006 for (idx = 0; idx < symcount; idx++)
3008 if (!sym_is_global (abfd, syms[idx]))
3009 num_locals++;
3010 else
3011 num_globals++;
3014 /* We will be adding a section symbol for each BFD section. Most normal
3015 sections will already have a section symbol in outsymbols, but
3016 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3017 at least in that case. */
3018 for (asect = abfd->sections; asect; asect = asect->next)
3020 if (sect_syms[asect->index] == NULL)
3022 if (!sym_is_global (abfd, asect->symbol))
3023 num_locals++;
3024 else
3025 num_globals++;
3029 /* Now sort the symbols so the local symbols are first. */
3030 amt = (num_locals + num_globals) * sizeof (asymbol *);
3031 new_syms = bfd_alloc (abfd, amt);
3033 if (new_syms == NULL)
3034 return FALSE;
3036 for (idx = 0; idx < symcount; idx++)
3038 asymbol *sym = syms[idx];
3039 unsigned int i;
3041 if (!sym_is_global (abfd, sym))
3042 i = num_locals2++;
3043 else
3044 i = num_locals + num_globals2++;
3045 new_syms[i] = sym;
3046 sym->udata.i = i + 1;
3048 for (asect = abfd->sections; asect; asect = asect->next)
3050 if (sect_syms[asect->index] == NULL)
3052 asymbol *sym = asect->symbol;
3053 unsigned int i;
3055 sect_syms[asect->index] = sym;
3056 if (!sym_is_global (abfd, sym))
3057 i = num_locals2++;
3058 else
3059 i = num_locals + num_globals2++;
3060 new_syms[i] = sym;
3061 sym->udata.i = i + 1;
3065 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3067 elf_num_locals (abfd) = num_locals;
3068 elf_num_globals (abfd) = num_globals;
3069 return TRUE;
3072 /* Align to the maximum file alignment that could be required for any
3073 ELF data structure. */
3075 static inline file_ptr
3076 align_file_position (file_ptr off, int align)
3078 return (off + align - 1) & ~(align - 1);
3081 /* Assign a file position to a section, optionally aligning to the
3082 required section alignment. */
3084 file_ptr
3085 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3086 file_ptr offset,
3087 bfd_boolean align)
3089 if (align)
3091 unsigned int al;
3093 al = i_shdrp->sh_addralign;
3094 if (al > 1)
3095 offset = BFD_ALIGN (offset, al);
3097 i_shdrp->sh_offset = offset;
3098 if (i_shdrp->bfd_section != NULL)
3099 i_shdrp->bfd_section->filepos = offset;
3100 if (i_shdrp->sh_type != SHT_NOBITS)
3101 offset += i_shdrp->sh_size;
3102 return offset;
3105 /* Compute the file positions we are going to put the sections at, and
3106 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3107 is not NULL, this is being called by the ELF backend linker. */
3109 bfd_boolean
3110 _bfd_elf_compute_section_file_positions (bfd *abfd,
3111 struct bfd_link_info *link_info)
3113 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3114 bfd_boolean failed;
3115 struct bfd_strtab_hash *strtab;
3116 Elf_Internal_Shdr *shstrtab_hdr;
3118 if (abfd->output_has_begun)
3119 return TRUE;
3121 /* Do any elf backend specific processing first. */
3122 if (bed->elf_backend_begin_write_processing)
3123 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3125 if (! prep_headers (abfd))
3126 return FALSE;
3128 /* Post process the headers if necessary. */
3129 if (bed->elf_backend_post_process_headers)
3130 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3132 failed = FALSE;
3133 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3134 if (failed)
3135 return FALSE;
3137 if (!assign_section_numbers (abfd))
3138 return FALSE;
3140 /* The backend linker builds symbol table information itself. */
3141 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3143 /* Non-zero if doing a relocatable link. */
3144 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3146 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3147 return FALSE;
3150 if (link_info == NULL)
3152 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3153 if (failed)
3154 return FALSE;
3157 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3158 /* sh_name was set in prep_headers. */
3159 shstrtab_hdr->sh_type = SHT_STRTAB;
3160 shstrtab_hdr->sh_flags = 0;
3161 shstrtab_hdr->sh_addr = 0;
3162 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3163 shstrtab_hdr->sh_entsize = 0;
3164 shstrtab_hdr->sh_link = 0;
3165 shstrtab_hdr->sh_info = 0;
3166 /* sh_offset is set in assign_file_positions_except_relocs. */
3167 shstrtab_hdr->sh_addralign = 1;
3169 if (!assign_file_positions_except_relocs (abfd, link_info))
3170 return FALSE;
3172 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3174 file_ptr off;
3175 Elf_Internal_Shdr *hdr;
3177 off = elf_tdata (abfd)->next_file_pos;
3179 hdr = &elf_tdata (abfd)->symtab_hdr;
3180 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3182 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3183 if (hdr->sh_size != 0)
3184 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3186 hdr = &elf_tdata (abfd)->strtab_hdr;
3187 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3189 elf_tdata (abfd)->next_file_pos = off;
3191 /* Now that we know where the .strtab section goes, write it
3192 out. */
3193 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3194 || ! _bfd_stringtab_emit (abfd, strtab))
3195 return FALSE;
3196 _bfd_stringtab_free (strtab);
3199 abfd->output_has_begun = TRUE;
3201 return TRUE;
3204 /* Create a mapping from a set of sections to a program segment. */
3206 static struct elf_segment_map *
3207 make_mapping (bfd *abfd,
3208 asection **sections,
3209 unsigned int from,
3210 unsigned int to,
3211 bfd_boolean phdr)
3213 struct elf_segment_map *m;
3214 unsigned int i;
3215 asection **hdrpp;
3216 bfd_size_type amt;
3218 amt = sizeof (struct elf_segment_map);
3219 amt += (to - from - 1) * sizeof (asection *);
3220 m = bfd_zalloc (abfd, amt);
3221 if (m == NULL)
3222 return NULL;
3223 m->next = NULL;
3224 m->p_type = PT_LOAD;
3225 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3226 m->sections[i - from] = *hdrpp;
3227 m->count = to - from;
3229 if (from == 0 && phdr)
3231 /* Include the headers in the first PT_LOAD segment. */
3232 m->includes_filehdr = 1;
3233 m->includes_phdrs = 1;
3236 return m;
3239 /* Set up a mapping from BFD sections to program segments. */
3241 static bfd_boolean
3242 map_sections_to_segments (bfd *abfd)
3244 asection **sections = NULL;
3245 asection *s;
3246 unsigned int i;
3247 unsigned int count;
3248 struct elf_segment_map *mfirst;
3249 struct elf_segment_map **pm;
3250 struct elf_segment_map *m;
3251 asection *last_hdr;
3252 bfd_vma last_size;
3253 unsigned int phdr_index;
3254 bfd_vma maxpagesize;
3255 asection **hdrpp;
3256 bfd_boolean phdr_in_segment = TRUE;
3257 bfd_boolean writable;
3258 int tls_count = 0;
3259 asection *first_tls = NULL;
3260 asection *dynsec, *eh_frame_hdr;
3261 bfd_size_type amt;
3263 if (elf_tdata (abfd)->segment_map != NULL)
3264 return TRUE;
3266 if (bfd_count_sections (abfd) == 0)
3267 return TRUE;
3269 /* Select the allocated sections, and sort them. */
3271 amt = bfd_count_sections (abfd) * sizeof (asection *);
3272 sections = bfd_malloc (amt);
3273 if (sections == NULL)
3274 goto error_return;
3276 i = 0;
3277 for (s = abfd->sections; s != NULL; s = s->next)
3279 if ((s->flags & SEC_ALLOC) != 0)
3281 sections[i] = s;
3282 ++i;
3285 BFD_ASSERT (i <= bfd_count_sections (abfd));
3286 count = i;
3288 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3290 /* Build the mapping. */
3292 mfirst = NULL;
3293 pm = &mfirst;
3295 /* If we have a .interp section, then create a PT_PHDR segment for
3296 the program headers and a PT_INTERP segment for the .interp
3297 section. */
3298 s = bfd_get_section_by_name (abfd, ".interp");
3299 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3301 amt = sizeof (struct elf_segment_map);
3302 m = bfd_zalloc (abfd, amt);
3303 if (m == NULL)
3304 goto error_return;
3305 m->next = NULL;
3306 m->p_type = PT_PHDR;
3307 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3308 m->p_flags = PF_R | PF_X;
3309 m->p_flags_valid = 1;
3310 m->includes_phdrs = 1;
3312 *pm = m;
3313 pm = &m->next;
3315 amt = sizeof (struct elf_segment_map);
3316 m = bfd_zalloc (abfd, amt);
3317 if (m == NULL)
3318 goto error_return;
3319 m->next = NULL;
3320 m->p_type = PT_INTERP;
3321 m->count = 1;
3322 m->sections[0] = s;
3324 *pm = m;
3325 pm = &m->next;
3328 /* Look through the sections. We put sections in the same program
3329 segment when the start of the second section can be placed within
3330 a few bytes of the end of the first section. */
3331 last_hdr = NULL;
3332 last_size = 0;
3333 phdr_index = 0;
3334 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
3335 writable = FALSE;
3336 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3337 if (dynsec != NULL
3338 && (dynsec->flags & SEC_LOAD) == 0)
3339 dynsec = NULL;
3341 /* Deal with -Ttext or something similar such that the first section
3342 is not adjacent to the program headers. This is an
3343 approximation, since at this point we don't know exactly how many
3344 program headers we will need. */
3345 if (count > 0)
3347 bfd_size_type phdr_size;
3349 phdr_size = elf_tdata (abfd)->program_header_size;
3350 if (phdr_size == 0)
3351 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
3352 if ((abfd->flags & D_PAGED) == 0
3353 || sections[0]->lma < phdr_size
3354 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3355 phdr_in_segment = FALSE;
3358 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3360 asection *hdr;
3361 bfd_boolean new_segment;
3363 hdr = *hdrpp;
3365 /* See if this section and the last one will fit in the same
3366 segment. */
3368 if (last_hdr == NULL)
3370 /* If we don't have a segment yet, then we don't need a new
3371 one (we build the last one after this loop). */
3372 new_segment = FALSE;
3374 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3376 /* If this section has a different relation between the
3377 virtual address and the load address, then we need a new
3378 segment. */
3379 new_segment = TRUE;
3381 else if (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
3382 < BFD_ALIGN (hdr->lma, maxpagesize))
3384 /* If putting this section in this segment would force us to
3385 skip a page in the segment, then we need a new segment. */
3386 new_segment = TRUE;
3388 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3389 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
3391 /* We don't want to put a loadable section after a
3392 nonloadable section in the same segment.
3393 Consider .tbss sections as loadable for this purpose. */
3394 new_segment = TRUE;
3396 else if ((abfd->flags & D_PAGED) == 0)
3398 /* If the file is not demand paged, which means that we
3399 don't require the sections to be correctly aligned in the
3400 file, then there is no other reason for a new segment. */
3401 new_segment = FALSE;
3403 else if (! writable
3404 && (hdr->flags & SEC_READONLY) == 0
3405 && (((last_hdr->lma + last_size - 1)
3406 & ~(maxpagesize - 1))
3407 != (hdr->lma & ~(maxpagesize - 1))))
3409 /* We don't want to put a writable section in a read only
3410 segment, unless they are on the same page in memory
3411 anyhow. We already know that the last section does not
3412 bring us past the current section on the page, so the
3413 only case in which the new section is not on the same
3414 page as the previous section is when the previous section
3415 ends precisely on a page boundary. */
3416 new_segment = TRUE;
3418 else
3420 /* Otherwise, we can use the same segment. */
3421 new_segment = FALSE;
3424 if (! new_segment)
3426 if ((hdr->flags & SEC_READONLY) == 0)
3427 writable = TRUE;
3428 last_hdr = hdr;
3429 /* .tbss sections effectively have zero size. */
3430 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3431 last_size = hdr->size;
3432 else
3433 last_size = 0;
3434 continue;
3437 /* We need a new program segment. We must create a new program
3438 header holding all the sections from phdr_index until hdr. */
3440 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3441 if (m == NULL)
3442 goto error_return;
3444 *pm = m;
3445 pm = &m->next;
3447 if ((hdr->flags & SEC_READONLY) == 0)
3448 writable = TRUE;
3449 else
3450 writable = FALSE;
3452 last_hdr = hdr;
3453 /* .tbss sections effectively have zero size. */
3454 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3455 last_size = hdr->size;
3456 else
3457 last_size = 0;
3458 phdr_index = i;
3459 phdr_in_segment = FALSE;
3462 /* Create a final PT_LOAD program segment. */
3463 if (last_hdr != NULL)
3465 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3466 if (m == NULL)
3467 goto error_return;
3469 *pm = m;
3470 pm = &m->next;
3473 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3474 if (dynsec != NULL)
3476 amt = sizeof (struct elf_segment_map);
3477 m = bfd_zalloc (abfd, amt);
3478 if (m == NULL)
3479 goto error_return;
3480 m->next = NULL;
3481 m->p_type = PT_DYNAMIC;
3482 m->count = 1;
3483 m->sections[0] = dynsec;
3485 *pm = m;
3486 pm = &m->next;
3489 /* For each loadable .note section, add a PT_NOTE segment. We don't
3490 use bfd_get_section_by_name, because if we link together
3491 nonloadable .note sections and loadable .note sections, we will
3492 generate two .note sections in the output file. FIXME: Using
3493 names for section types is bogus anyhow. */
3494 for (s = abfd->sections; s != NULL; s = s->next)
3496 if ((s->flags & SEC_LOAD) != 0
3497 && strncmp (s->name, ".note", 5) == 0)
3499 amt = sizeof (struct elf_segment_map);
3500 m = bfd_zalloc (abfd, amt);
3501 if (m == NULL)
3502 goto error_return;
3503 m->next = NULL;
3504 m->p_type = PT_NOTE;
3505 m->count = 1;
3506 m->sections[0] = s;
3508 *pm = m;
3509 pm = &m->next;
3511 if (s->flags & SEC_THREAD_LOCAL)
3513 if (! tls_count)
3514 first_tls = s;
3515 tls_count++;
3519 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3520 if (tls_count > 0)
3522 int i;
3524 amt = sizeof (struct elf_segment_map);
3525 amt += (tls_count - 1) * sizeof (asection *);
3526 m = bfd_zalloc (abfd, amt);
3527 if (m == NULL)
3528 goto error_return;
3529 m->next = NULL;
3530 m->p_type = PT_TLS;
3531 m->count = tls_count;
3532 /* Mandated PF_R. */
3533 m->p_flags = PF_R;
3534 m->p_flags_valid = 1;
3535 for (i = 0; i < tls_count; ++i)
3537 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3538 m->sections[i] = first_tls;
3539 first_tls = first_tls->next;
3542 *pm = m;
3543 pm = &m->next;
3546 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3547 segment. */
3548 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
3549 if (eh_frame_hdr != NULL
3550 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
3552 amt = sizeof (struct elf_segment_map);
3553 m = bfd_zalloc (abfd, amt);
3554 if (m == NULL)
3555 goto error_return;
3556 m->next = NULL;
3557 m->p_type = PT_GNU_EH_FRAME;
3558 m->count = 1;
3559 m->sections[0] = eh_frame_hdr->output_section;
3561 *pm = m;
3562 pm = &m->next;
3565 if (elf_tdata (abfd)->stack_flags)
3567 amt = sizeof (struct elf_segment_map);
3568 m = bfd_zalloc (abfd, amt);
3569 if (m == NULL)
3570 goto error_return;
3571 m->next = NULL;
3572 m->p_type = PT_GNU_STACK;
3573 m->p_flags = elf_tdata (abfd)->stack_flags;
3574 m->p_flags_valid = 1;
3576 *pm = m;
3577 pm = &m->next;
3580 if (elf_tdata (abfd)->relro)
3582 amt = sizeof (struct elf_segment_map);
3583 m = bfd_zalloc (abfd, amt);
3584 if (m == NULL)
3585 goto error_return;
3586 m->next = NULL;
3587 m->p_type = PT_GNU_RELRO;
3588 m->p_flags = PF_R;
3589 m->p_flags_valid = 1;
3591 *pm = m;
3592 pm = &m->next;
3595 free (sections);
3596 sections = NULL;
3598 elf_tdata (abfd)->segment_map = mfirst;
3599 return TRUE;
3601 error_return:
3602 if (sections != NULL)
3603 free (sections);
3604 return FALSE;
3607 /* Sort sections by address. */
3609 static int
3610 elf_sort_sections (const void *arg1, const void *arg2)
3612 const asection *sec1 = *(const asection **) arg1;
3613 const asection *sec2 = *(const asection **) arg2;
3614 bfd_size_type size1, size2;
3616 /* Sort by LMA first, since this is the address used to
3617 place the section into a segment. */
3618 if (sec1->lma < sec2->lma)
3619 return -1;
3620 else if (sec1->lma > sec2->lma)
3621 return 1;
3623 /* Then sort by VMA. Normally the LMA and the VMA will be
3624 the same, and this will do nothing. */
3625 if (sec1->vma < sec2->vma)
3626 return -1;
3627 else if (sec1->vma > sec2->vma)
3628 return 1;
3630 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3632 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
3634 if (TOEND (sec1))
3636 if (TOEND (sec2))
3638 /* If the indicies are the same, do not return 0
3639 here, but continue to try the next comparison. */
3640 if (sec1->target_index - sec2->target_index != 0)
3641 return sec1->target_index - sec2->target_index;
3643 else
3644 return 1;
3646 else if (TOEND (sec2))
3647 return -1;
3649 #undef TOEND
3651 /* Sort by size, to put zero sized sections
3652 before others at the same address. */
3654 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
3655 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
3657 if (size1 < size2)
3658 return -1;
3659 if (size1 > size2)
3660 return 1;
3662 return sec1->target_index - sec2->target_index;
3665 /* Ian Lance Taylor writes:
3667 We shouldn't be using % with a negative signed number. That's just
3668 not good. We have to make sure either that the number is not
3669 negative, or that the number has an unsigned type. When the types
3670 are all the same size they wind up as unsigned. When file_ptr is a
3671 larger signed type, the arithmetic winds up as signed long long,
3672 which is wrong.
3674 What we're trying to say here is something like ``increase OFF by
3675 the least amount that will cause it to be equal to the VMA modulo
3676 the page size.'' */
3677 /* In other words, something like:
3679 vma_offset = m->sections[0]->vma % bed->maxpagesize;
3680 off_offset = off % bed->maxpagesize;
3681 if (vma_offset < off_offset)
3682 adjustment = vma_offset + bed->maxpagesize - off_offset;
3683 else
3684 adjustment = vma_offset - off_offset;
3686 which can can be collapsed into the expression below. */
3688 static file_ptr
3689 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
3691 return ((vma - off) % maxpagesize);
3694 /* Assign file positions to the sections based on the mapping from
3695 sections to segments. This function also sets up some fields in
3696 the file header, and writes out the program headers. */
3698 static bfd_boolean
3699 assign_file_positions_for_segments (bfd *abfd, struct bfd_link_info *link_info)
3701 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3702 unsigned int count;
3703 struct elf_segment_map *m;
3704 unsigned int alloc;
3705 Elf_Internal_Phdr *phdrs;
3706 file_ptr off, voff;
3707 bfd_vma filehdr_vaddr, filehdr_paddr;
3708 bfd_vma phdrs_vaddr, phdrs_paddr;
3709 Elf_Internal_Phdr *p;
3710 bfd_size_type amt;
3712 if (elf_tdata (abfd)->segment_map == NULL)
3714 if (! map_sections_to_segments (abfd))
3715 return FALSE;
3717 else
3719 /* The placement algorithm assumes that non allocated sections are
3720 not in PT_LOAD segments. We ensure this here by removing such
3721 sections from the segment map. */
3722 for (m = elf_tdata (abfd)->segment_map;
3723 m != NULL;
3724 m = m->next)
3726 unsigned int new_count;
3727 unsigned int i;
3729 if (m->p_type != PT_LOAD)
3730 continue;
3732 new_count = 0;
3733 for (i = 0; i < m->count; i ++)
3735 if ((m->sections[i]->flags & SEC_ALLOC) != 0)
3737 if (i != new_count)
3738 m->sections[new_count] = m->sections[i];
3740 new_count ++;
3744 if (new_count != m->count)
3745 m->count = new_count;
3749 if (bed->elf_backend_modify_segment_map)
3751 if (! (*bed->elf_backend_modify_segment_map) (abfd, link_info))
3752 return FALSE;
3755 count = 0;
3756 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3757 ++count;
3759 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
3760 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
3761 elf_elfheader (abfd)->e_phnum = count;
3763 if (count == 0)
3764 return TRUE;
3766 /* If we already counted the number of program segments, make sure
3767 that we allocated enough space. This happens when SIZEOF_HEADERS
3768 is used in a linker script. */
3769 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
3770 if (alloc != 0 && count > alloc)
3772 ((*_bfd_error_handler)
3773 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3774 bfd_get_filename (abfd), alloc, count));
3775 bfd_set_error (bfd_error_bad_value);
3776 return FALSE;
3779 if (alloc == 0)
3780 alloc = count;
3782 amt = alloc * sizeof (Elf_Internal_Phdr);
3783 phdrs = bfd_alloc (abfd, amt);
3784 if (phdrs == NULL)
3785 return FALSE;
3787 off = bed->s->sizeof_ehdr;
3788 off += alloc * bed->s->sizeof_phdr;
3790 filehdr_vaddr = 0;
3791 filehdr_paddr = 0;
3792 phdrs_vaddr = 0;
3793 phdrs_paddr = 0;
3795 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3796 m != NULL;
3797 m = m->next, p++)
3799 unsigned int i;
3800 asection **secpp;
3802 /* If elf_segment_map is not from map_sections_to_segments, the
3803 sections may not be correctly ordered. NOTE: sorting should
3804 not be done to the PT_NOTE section of a corefile, which may
3805 contain several pseudo-sections artificially created by bfd.
3806 Sorting these pseudo-sections breaks things badly. */
3807 if (m->count > 1
3808 && !(elf_elfheader (abfd)->e_type == ET_CORE
3809 && m->p_type == PT_NOTE))
3810 qsort (m->sections, (size_t) m->count, sizeof (asection *),
3811 elf_sort_sections);
3813 p->p_type = m->p_type;
3814 p->p_flags = m->p_flags;
3816 if (p->p_type == PT_LOAD
3817 && m->count > 0
3818 && (m->sections[0]->flags & SEC_ALLOC) != 0)
3820 if ((abfd->flags & D_PAGED) != 0)
3821 off += vma_page_aligned_bias (m->sections[0]->vma, off,
3822 bed->maxpagesize);
3823 else
3825 bfd_size_type align;
3827 align = 0;
3828 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3830 bfd_size_type secalign;
3832 secalign = bfd_get_section_alignment (abfd, *secpp);
3833 if (secalign > align)
3834 align = secalign;
3837 off += vma_page_aligned_bias (m->sections[0]->vma, off,
3838 1 << align);
3841 /* Make sure the .dynamic section is the first section in the
3842 PT_DYNAMIC segment. */
3843 else if (p->p_type == PT_DYNAMIC
3844 && m->count > 1
3845 && strcmp (m->sections[0]->name, ".dynamic") != 0)
3847 _bfd_error_handler
3848 (_("%s: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
3849 bfd_get_filename (abfd));
3850 bfd_set_error (bfd_error_bad_value);
3851 return FALSE;
3854 if (m->count == 0)
3855 p->p_vaddr = 0;
3856 else
3857 p->p_vaddr = m->sections[0]->vma;
3859 if (m->p_paddr_valid)
3860 p->p_paddr = m->p_paddr;
3861 else if (m->count == 0)
3862 p->p_paddr = 0;
3863 else
3864 p->p_paddr = m->sections[0]->lma;
3866 if (p->p_type == PT_LOAD
3867 && (abfd->flags & D_PAGED) != 0)
3868 p->p_align = bed->maxpagesize;
3869 else if (m->count == 0)
3870 p->p_align = 1 << bed->s->log_file_align;
3871 else
3872 p->p_align = 0;
3874 p->p_offset = 0;
3875 p->p_filesz = 0;
3876 p->p_memsz = 0;
3878 if (m->includes_filehdr)
3880 if (! m->p_flags_valid)
3881 p->p_flags |= PF_R;
3882 p->p_offset = 0;
3883 p->p_filesz = bed->s->sizeof_ehdr;
3884 p->p_memsz = bed->s->sizeof_ehdr;
3885 if (m->count > 0)
3887 BFD_ASSERT (p->p_type == PT_LOAD);
3889 if (p->p_vaddr < (bfd_vma) off)
3891 (*_bfd_error_handler)
3892 (_("%s: Not enough room for program headers, try linking with -N"),
3893 bfd_get_filename (abfd));
3894 bfd_set_error (bfd_error_bad_value);
3895 return FALSE;
3898 p->p_vaddr -= off;
3899 if (! m->p_paddr_valid)
3900 p->p_paddr -= off;
3902 if (p->p_type == PT_LOAD)
3904 filehdr_vaddr = p->p_vaddr;
3905 filehdr_paddr = p->p_paddr;
3909 if (m->includes_phdrs)
3911 if (! m->p_flags_valid)
3912 p->p_flags |= PF_R;
3914 if (m->includes_filehdr)
3916 if (p->p_type == PT_LOAD)
3918 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
3919 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
3922 else
3924 p->p_offset = bed->s->sizeof_ehdr;
3926 if (m->count > 0)
3928 BFD_ASSERT (p->p_type == PT_LOAD);
3929 p->p_vaddr -= off - p->p_offset;
3930 if (! m->p_paddr_valid)
3931 p->p_paddr -= off - p->p_offset;
3934 if (p->p_type == PT_LOAD)
3936 phdrs_vaddr = p->p_vaddr;
3937 phdrs_paddr = p->p_paddr;
3939 else
3940 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
3943 p->p_filesz += alloc * bed->s->sizeof_phdr;
3944 p->p_memsz += alloc * bed->s->sizeof_phdr;
3947 if (p->p_type == PT_LOAD
3948 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
3950 if (! m->includes_filehdr && ! m->includes_phdrs)
3951 p->p_offset = off;
3952 else
3954 file_ptr adjust;
3956 adjust = off - (p->p_offset + p->p_filesz);
3957 p->p_filesz += adjust;
3958 p->p_memsz += adjust;
3962 voff = off;
3964 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3966 asection *sec;
3967 flagword flags;
3968 bfd_size_type align;
3970 sec = *secpp;
3971 flags = sec->flags;
3972 align = 1 << bfd_get_section_alignment (abfd, sec);
3974 /* The section may have artificial alignment forced by a
3975 link script. Notice this case by the gap between the
3976 cumulative phdr lma and the section's lma. */
3977 if (p->p_paddr + p->p_memsz < sec->lma)
3979 bfd_vma adjust = sec->lma - (p->p_paddr + p->p_memsz);
3981 p->p_memsz += adjust;
3982 if (p->p_type == PT_LOAD
3983 || (p->p_type == PT_NOTE
3984 && bfd_get_format (abfd) == bfd_core))
3986 off += adjust;
3987 voff += adjust;
3989 if ((flags & SEC_LOAD) != 0
3990 || (flags & SEC_THREAD_LOCAL) != 0)
3991 p->p_filesz += adjust;
3994 if (p->p_type == PT_LOAD)
3996 bfd_signed_vma adjust;
3998 if ((flags & SEC_LOAD) != 0)
4000 adjust = sec->lma - (p->p_paddr + p->p_memsz);
4001 if (adjust < 0)
4002 adjust = 0;
4004 else if ((flags & SEC_ALLOC) != 0)
4006 /* The section VMA must equal the file position
4007 modulo the page size. FIXME: I'm not sure if
4008 this adjustment is really necessary. We used to
4009 not have the SEC_LOAD case just above, and then
4010 this was necessary, but now I'm not sure. */
4011 if ((abfd->flags & D_PAGED) != 0)
4012 adjust = vma_page_aligned_bias (sec->vma, voff,
4013 bed->maxpagesize);
4014 else
4015 adjust = vma_page_aligned_bias (sec->vma, voff,
4016 align);
4018 else
4019 adjust = 0;
4021 if (adjust != 0)
4023 if (i == 0)
4025 (* _bfd_error_handler) (_("\
4026 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
4027 bfd_section_name (abfd, sec),
4028 sec->lma,
4029 p->p_paddr);
4030 return FALSE;
4032 p->p_memsz += adjust;
4033 off += adjust;
4034 voff += adjust;
4035 if ((flags & SEC_LOAD) != 0)
4036 p->p_filesz += adjust;
4039 sec->filepos = off;
4041 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
4042 used in a linker script we may have a section with
4043 SEC_LOAD clear but which is supposed to have
4044 contents. */
4045 if ((flags & SEC_LOAD) != 0
4046 || (flags & SEC_HAS_CONTENTS) != 0)
4047 off += sec->size;
4049 if ((flags & SEC_ALLOC) != 0
4050 && ((flags & SEC_LOAD) != 0
4051 || (flags & SEC_THREAD_LOCAL) == 0))
4052 voff += sec->size;
4055 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4057 /* The actual "note" segment has i == 0.
4058 This is the one that actually contains everything. */
4059 if (i == 0)
4061 sec->filepos = off;
4062 p->p_filesz = sec->size;
4063 off += sec->size;
4064 voff = off;
4066 else
4068 /* Fake sections -- don't need to be written. */
4069 sec->filepos = 0;
4070 sec->size = 0;
4071 flags = sec->flags = 0;
4073 p->p_memsz = 0;
4074 p->p_align = 1;
4076 else
4078 if ((sec->flags & SEC_LOAD) != 0
4079 || (sec->flags & SEC_THREAD_LOCAL) == 0
4080 || p->p_type == PT_TLS)
4081 p->p_memsz += sec->size;
4083 if ((flags & SEC_LOAD) != 0)
4084 p->p_filesz += sec->size;
4086 if (p->p_type == PT_TLS
4087 && sec->size == 0
4088 && (sec->flags & SEC_HAS_CONTENTS) == 0)
4090 struct bfd_link_order *o;
4091 bfd_vma tbss_size = 0;
4093 for (o = sec->link_order_head; o != NULL; o = o->next)
4094 if (tbss_size < o->offset + o->size)
4095 tbss_size = o->offset + o->size;
4097 p->p_memsz += tbss_size;
4100 if (align > p->p_align
4101 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
4102 p->p_align = align;
4105 if (! m->p_flags_valid)
4107 p->p_flags |= PF_R;
4108 if ((flags & SEC_CODE) != 0)
4109 p->p_flags |= PF_X;
4110 if ((flags & SEC_READONLY) == 0)
4111 p->p_flags |= PF_W;
4116 /* Now that we have set the section file positions, we can set up
4117 the file positions for the non PT_LOAD segments. */
4118 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4119 m != NULL;
4120 m = m->next, p++)
4122 if (p->p_type != PT_LOAD && m->count > 0)
4124 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
4125 p->p_offset = m->sections[0]->filepos;
4127 if (m->count == 0)
4129 if (m->includes_filehdr)
4131 p->p_vaddr = filehdr_vaddr;
4132 if (! m->p_paddr_valid)
4133 p->p_paddr = filehdr_paddr;
4135 else if (m->includes_phdrs)
4137 p->p_vaddr = phdrs_vaddr;
4138 if (! m->p_paddr_valid)
4139 p->p_paddr = phdrs_paddr;
4141 else if (p->p_type == PT_GNU_RELRO)
4143 Elf_Internal_Phdr *lp;
4145 for (lp = phdrs; lp < phdrs + count; ++lp)
4147 if (lp->p_type == PT_LOAD
4148 && lp->p_vaddr <= link_info->relro_end
4149 && lp->p_vaddr >= link_info->relro_start
4150 && lp->p_vaddr + lp->p_filesz
4151 >= link_info->relro_end)
4152 break;
4155 if (lp < phdrs + count
4156 && link_info->relro_end > lp->p_vaddr)
4158 p->p_vaddr = lp->p_vaddr;
4159 p->p_paddr = lp->p_paddr;
4160 p->p_offset = lp->p_offset;
4161 p->p_filesz = link_info->relro_end - lp->p_vaddr;
4162 p->p_memsz = p->p_filesz;
4163 p->p_align = 1;
4164 p->p_flags = (lp->p_flags & ~PF_W);
4166 else
4168 memset (p, 0, sizeof *p);
4169 p->p_type = PT_NULL;
4175 /* Clear out any program headers we allocated but did not use. */
4176 for (; count < alloc; count++, p++)
4178 memset (p, 0, sizeof *p);
4179 p->p_type = PT_NULL;
4182 elf_tdata (abfd)->phdr = phdrs;
4184 elf_tdata (abfd)->next_file_pos = off;
4186 /* Write out the program headers. */
4187 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4188 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
4189 return FALSE;
4191 return TRUE;
4194 /* Get the size of the program header.
4196 If this is called by the linker before any of the section VMA's are set, it
4197 can't calculate the correct value for a strange memory layout. This only
4198 happens when SIZEOF_HEADERS is used in a linker script. In this case,
4199 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
4200 data segment (exclusive of .interp and .dynamic).
4202 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
4203 will be two segments. */
4205 static bfd_size_type
4206 get_program_header_size (bfd *abfd)
4208 size_t segs;
4209 asection *s;
4210 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4212 /* We can't return a different result each time we're called. */
4213 if (elf_tdata (abfd)->program_header_size != 0)
4214 return elf_tdata (abfd)->program_header_size;
4216 if (elf_tdata (abfd)->segment_map != NULL)
4218 struct elf_segment_map *m;
4220 segs = 0;
4221 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4222 ++segs;
4223 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
4224 return elf_tdata (abfd)->program_header_size;
4227 /* Assume we will need exactly two PT_LOAD segments: one for text
4228 and one for data. */
4229 segs = 2;
4231 s = bfd_get_section_by_name (abfd, ".interp");
4232 if (s != NULL && (s->flags & SEC_LOAD) != 0)
4234 /* If we have a loadable interpreter section, we need a
4235 PT_INTERP segment. In this case, assume we also need a
4236 PT_PHDR segment, although that may not be true for all
4237 targets. */
4238 segs += 2;
4241 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4243 /* We need a PT_DYNAMIC segment. */
4244 ++segs;
4247 if (elf_tdata (abfd)->eh_frame_hdr)
4249 /* We need a PT_GNU_EH_FRAME segment. */
4250 ++segs;
4253 if (elf_tdata (abfd)->stack_flags)
4255 /* We need a PT_GNU_STACK segment. */
4256 ++segs;
4259 if (elf_tdata (abfd)->relro)
4261 /* We need a PT_GNU_RELRO segment. */
4262 ++segs;
4265 for (s = abfd->sections; s != NULL; s = s->next)
4267 if ((s->flags & SEC_LOAD) != 0
4268 && strncmp (s->name, ".note", 5) == 0)
4270 /* We need a PT_NOTE segment. */
4271 ++segs;
4275 for (s = abfd->sections; s != NULL; s = s->next)
4277 if (s->flags & SEC_THREAD_LOCAL)
4279 /* We need a PT_TLS segment. */
4280 ++segs;
4281 break;
4285 /* Let the backend count up any program headers it might need. */
4286 if (bed->elf_backend_additional_program_headers)
4288 int a;
4290 a = (*bed->elf_backend_additional_program_headers) (abfd);
4291 if (a == -1)
4292 abort ();
4293 segs += a;
4296 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
4297 return elf_tdata (abfd)->program_header_size;
4300 /* Work out the file positions of all the sections. This is called by
4301 _bfd_elf_compute_section_file_positions. All the section sizes and
4302 VMAs must be known before this is called.
4304 We do not consider reloc sections at this point, unless they form
4305 part of the loadable image. Reloc sections are assigned file
4306 positions in assign_file_positions_for_relocs, which is called by
4307 write_object_contents and final_link.
4309 We also don't set the positions of the .symtab and .strtab here. */
4311 static bfd_boolean
4312 assign_file_positions_except_relocs (bfd *abfd,
4313 struct bfd_link_info *link_info)
4315 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
4316 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
4317 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4318 unsigned int num_sec = elf_numsections (abfd);
4319 file_ptr off;
4320 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4322 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4323 && bfd_get_format (abfd) != bfd_core)
4325 Elf_Internal_Shdr **hdrpp;
4326 unsigned int i;
4328 /* Start after the ELF header. */
4329 off = i_ehdrp->e_ehsize;
4331 /* We are not creating an executable, which means that we are
4332 not creating a program header, and that the actual order of
4333 the sections in the file is unimportant. */
4334 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4336 Elf_Internal_Shdr *hdr;
4338 hdr = *hdrpp;
4339 if (hdr->sh_type == SHT_REL
4340 || hdr->sh_type == SHT_RELA
4341 || i == tdata->symtab_section
4342 || i == tdata->symtab_shndx_section
4343 || i == tdata->strtab_section)
4345 hdr->sh_offset = -1;
4347 else
4348 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4350 if (i == SHN_LORESERVE - 1)
4352 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4353 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4357 else
4359 unsigned int i;
4360 Elf_Internal_Shdr **hdrpp;
4362 /* Assign file positions for the loaded sections based on the
4363 assignment of sections to segments. */
4364 if (! assign_file_positions_for_segments (abfd, link_info))
4365 return FALSE;
4367 /* Assign file positions for the other sections. */
4369 off = elf_tdata (abfd)->next_file_pos;
4370 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4372 Elf_Internal_Shdr *hdr;
4374 hdr = *hdrpp;
4375 if (hdr->bfd_section != NULL
4376 && hdr->bfd_section->filepos != 0)
4377 hdr->sh_offset = hdr->bfd_section->filepos;
4378 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4380 ((*_bfd_error_handler)
4381 (_("%s: warning: allocated section `%s' not in segment"),
4382 bfd_get_filename (abfd),
4383 (hdr->bfd_section == NULL
4384 ? "*unknown*"
4385 : hdr->bfd_section->name)));
4386 if ((abfd->flags & D_PAGED) != 0)
4387 off += vma_page_aligned_bias (hdr->sh_addr, off,
4388 bed->maxpagesize);
4389 else
4390 off += vma_page_aligned_bias (hdr->sh_addr, off,
4391 hdr->sh_addralign);
4392 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4393 FALSE);
4395 else if (hdr->sh_type == SHT_REL
4396 || hdr->sh_type == SHT_RELA
4397 || hdr == i_shdrpp[tdata->symtab_section]
4398 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4399 || hdr == i_shdrpp[tdata->strtab_section])
4400 hdr->sh_offset = -1;
4401 else
4402 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4404 if (i == SHN_LORESERVE - 1)
4406 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4407 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4412 /* Place the section headers. */
4413 off = align_file_position (off, 1 << bed->s->log_file_align);
4414 i_ehdrp->e_shoff = off;
4415 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4417 elf_tdata (abfd)->next_file_pos = off;
4419 return TRUE;
4422 static bfd_boolean
4423 prep_headers (bfd *abfd)
4425 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
4426 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4427 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
4428 struct elf_strtab_hash *shstrtab;
4429 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4431 i_ehdrp = elf_elfheader (abfd);
4432 i_shdrp = elf_elfsections (abfd);
4434 shstrtab = _bfd_elf_strtab_init ();
4435 if (shstrtab == NULL)
4436 return FALSE;
4438 elf_shstrtab (abfd) = shstrtab;
4440 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4441 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4442 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4443 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4445 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4446 i_ehdrp->e_ident[EI_DATA] =
4447 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4448 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4450 if ((abfd->flags & DYNAMIC) != 0)
4451 i_ehdrp->e_type = ET_DYN;
4452 else if ((abfd->flags & EXEC_P) != 0)
4453 i_ehdrp->e_type = ET_EXEC;
4454 else if (bfd_get_format (abfd) == bfd_core)
4455 i_ehdrp->e_type = ET_CORE;
4456 else
4457 i_ehdrp->e_type = ET_REL;
4459 switch (bfd_get_arch (abfd))
4461 case bfd_arch_unknown:
4462 i_ehdrp->e_machine = EM_NONE;
4463 break;
4465 /* There used to be a long list of cases here, each one setting
4466 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4467 in the corresponding bfd definition. To avoid duplication,
4468 the switch was removed. Machines that need special handling
4469 can generally do it in elf_backend_final_write_processing(),
4470 unless they need the information earlier than the final write.
4471 Such need can generally be supplied by replacing the tests for
4472 e_machine with the conditions used to determine it. */
4473 default:
4474 i_ehdrp->e_machine = bed->elf_machine_code;
4477 i_ehdrp->e_version = bed->s->ev_current;
4478 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4480 /* No program header, for now. */
4481 i_ehdrp->e_phoff = 0;
4482 i_ehdrp->e_phentsize = 0;
4483 i_ehdrp->e_phnum = 0;
4485 /* Each bfd section is section header entry. */
4486 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4487 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4489 /* If we're building an executable, we'll need a program header table. */
4490 if (abfd->flags & EXEC_P)
4492 /* It all happens later. */
4493 #if 0
4494 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
4496 /* elf_build_phdrs() returns a (NULL-terminated) array of
4497 Elf_Internal_Phdrs. */
4498 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
4499 i_ehdrp->e_phoff = outbase;
4500 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
4501 #endif
4503 else
4505 i_ehdrp->e_phentsize = 0;
4506 i_phdrp = 0;
4507 i_ehdrp->e_phoff = 0;
4510 elf_tdata (abfd)->symtab_hdr.sh_name =
4511 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
4512 elf_tdata (abfd)->strtab_hdr.sh_name =
4513 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
4514 elf_tdata (abfd)->shstrtab_hdr.sh_name =
4515 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
4516 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4517 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4518 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4519 return FALSE;
4521 return TRUE;
4524 /* Assign file positions for all the reloc sections which are not part
4525 of the loadable file image. */
4527 void
4528 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
4530 file_ptr off;
4531 unsigned int i, num_sec;
4532 Elf_Internal_Shdr **shdrpp;
4534 off = elf_tdata (abfd)->next_file_pos;
4536 num_sec = elf_numsections (abfd);
4537 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
4539 Elf_Internal_Shdr *shdrp;
4541 shdrp = *shdrpp;
4542 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4543 && shdrp->sh_offset == -1)
4544 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
4547 elf_tdata (abfd)->next_file_pos = off;
4550 bfd_boolean
4551 _bfd_elf_write_object_contents (bfd *abfd)
4553 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4554 Elf_Internal_Ehdr *i_ehdrp;
4555 Elf_Internal_Shdr **i_shdrp;
4556 bfd_boolean failed;
4557 unsigned int count, num_sec;
4559 if (! abfd->output_has_begun
4560 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
4561 return FALSE;
4563 i_shdrp = elf_elfsections (abfd);
4564 i_ehdrp = elf_elfheader (abfd);
4566 failed = FALSE;
4567 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4568 if (failed)
4569 return FALSE;
4571 _bfd_elf_assign_file_positions_for_relocs (abfd);
4573 /* After writing the headers, we need to write the sections too... */
4574 num_sec = elf_numsections (abfd);
4575 for (count = 1; count < num_sec; count++)
4577 if (bed->elf_backend_section_processing)
4578 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4579 if (i_shdrp[count]->contents)
4581 bfd_size_type amt = i_shdrp[count]->sh_size;
4583 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
4584 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
4585 return FALSE;
4587 if (count == SHN_LORESERVE - 1)
4588 count += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4591 /* Write out the section header names. */
4592 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
4593 || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))
4594 return FALSE;
4596 if (bed->elf_backend_final_write_processing)
4597 (*bed->elf_backend_final_write_processing) (abfd,
4598 elf_tdata (abfd)->linker);
4600 return bed->s->write_shdrs_and_ehdr (abfd);
4603 bfd_boolean
4604 _bfd_elf_write_corefile_contents (bfd *abfd)
4606 /* Hopefully this can be done just like an object file. */
4607 return _bfd_elf_write_object_contents (abfd);
4610 /* Given a section, search the header to find them. */
4613 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
4615 const struct elf_backend_data *bed;
4616 int index;
4618 if (elf_section_data (asect) != NULL
4619 && elf_section_data (asect)->this_idx != 0)
4620 return elf_section_data (asect)->this_idx;
4622 if (bfd_is_abs_section (asect))
4623 index = SHN_ABS;
4624 else if (bfd_is_com_section (asect))
4625 index = SHN_COMMON;
4626 else if (bfd_is_und_section (asect))
4627 index = SHN_UNDEF;
4628 else
4630 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
4631 int maxindex = elf_numsections (abfd);
4633 for (index = 1; index < maxindex; index++)
4635 Elf_Internal_Shdr *hdr = i_shdrp[index];
4637 if (hdr != NULL && hdr->bfd_section == asect)
4638 return index;
4640 index = -1;
4643 bed = get_elf_backend_data (abfd);
4644 if (bed->elf_backend_section_from_bfd_section)
4646 int retval = index;
4648 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
4649 return retval;
4652 if (index == -1)
4653 bfd_set_error (bfd_error_nonrepresentable_section);
4655 return index;
4658 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4659 on error. */
4662 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
4664 asymbol *asym_ptr = *asym_ptr_ptr;
4665 int idx;
4666 flagword flags = asym_ptr->flags;
4668 /* When gas creates relocations against local labels, it creates its
4669 own symbol for the section, but does put the symbol into the
4670 symbol chain, so udata is 0. When the linker is generating
4671 relocatable output, this section symbol may be for one of the
4672 input sections rather than the output section. */
4673 if (asym_ptr->udata.i == 0
4674 && (flags & BSF_SECTION_SYM)
4675 && asym_ptr->section)
4677 int indx;
4679 if (asym_ptr->section->output_section != NULL)
4680 indx = asym_ptr->section->output_section->index;
4681 else
4682 indx = asym_ptr->section->index;
4683 if (indx < elf_num_section_syms (abfd)
4684 && elf_section_syms (abfd)[indx] != NULL)
4685 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
4688 idx = asym_ptr->udata.i;
4690 if (idx == 0)
4692 /* This case can occur when using --strip-symbol on a symbol
4693 which is used in a relocation entry. */
4694 (*_bfd_error_handler)
4695 (_("%s: symbol `%s' required but not present"),
4696 bfd_archive_filename (abfd), bfd_asymbol_name (asym_ptr));
4697 bfd_set_error (bfd_error_no_symbols);
4698 return -1;
4701 #if DEBUG & 4
4703 fprintf (stderr,
4704 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
4705 (long) asym_ptr, asym_ptr->name, idx, flags,
4706 elf_symbol_flags (flags));
4707 fflush (stderr);
4709 #endif
4711 return idx;
4714 /* Copy private BFD data. This copies any program header information. */
4716 static bfd_boolean
4717 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
4719 Elf_Internal_Ehdr *iehdr;
4720 struct elf_segment_map *map;
4721 struct elf_segment_map *map_first;
4722 struct elf_segment_map **pointer_to_map;
4723 Elf_Internal_Phdr *segment;
4724 asection *section;
4725 unsigned int i;
4726 unsigned int num_segments;
4727 bfd_boolean phdr_included = FALSE;
4728 bfd_vma maxpagesize;
4729 struct elf_segment_map *phdr_adjust_seg = NULL;
4730 unsigned int phdr_adjust_num = 0;
4731 const struct elf_backend_data *bed;
4733 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4734 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4735 return TRUE;
4737 if (elf_tdata (ibfd)->phdr == NULL)
4738 return TRUE;
4740 bed = get_elf_backend_data (ibfd);
4741 iehdr = elf_elfheader (ibfd);
4743 map_first = NULL;
4744 pointer_to_map = &map_first;
4746 num_segments = elf_elfheader (ibfd)->e_phnum;
4747 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
4749 /* Returns the end address of the segment + 1. */
4750 #define SEGMENT_END(segment, start) \
4751 (start + (segment->p_memsz > segment->p_filesz \
4752 ? segment->p_memsz : segment->p_filesz))
4754 #define SECTION_SIZE(section, segment) \
4755 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
4756 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
4757 ? section->size : 0)
4759 /* Returns TRUE if the given section is contained within
4760 the given segment. VMA addresses are compared. */
4761 #define IS_CONTAINED_BY_VMA(section, segment) \
4762 (section->vma >= segment->p_vaddr \
4763 && (section->vma + SECTION_SIZE (section, segment) \
4764 <= (SEGMENT_END (segment, segment->p_vaddr))))
4766 /* Returns TRUE if the given section is contained within
4767 the given segment. LMA addresses are compared. */
4768 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4769 (section->lma >= base \
4770 && (section->lma + SECTION_SIZE (section, segment) \
4771 <= SEGMENT_END (segment, base)))
4773 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4774 #define IS_COREFILE_NOTE(p, s) \
4775 (p->p_type == PT_NOTE \
4776 && bfd_get_format (ibfd) == bfd_core \
4777 && s->vma == 0 && s->lma == 0 \
4778 && (bfd_vma) s->filepos >= p->p_offset \
4779 && ((bfd_vma) s->filepos + s->size \
4780 <= p->p_offset + p->p_filesz))
4782 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4783 linker, which generates a PT_INTERP section with p_vaddr and
4784 p_memsz set to 0. */
4785 #define IS_SOLARIS_PT_INTERP(p, s) \
4786 (p->p_vaddr == 0 \
4787 && p->p_paddr == 0 \
4788 && p->p_memsz == 0 \
4789 && p->p_filesz > 0 \
4790 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4791 && s->size > 0 \
4792 && (bfd_vma) s->filepos >= p->p_offset \
4793 && ((bfd_vma) s->filepos + s->size \
4794 <= p->p_offset + p->p_filesz))
4796 /* Decide if the given section should be included in the given segment.
4797 A section will be included if:
4798 1. It is within the address space of the segment -- we use the LMA
4799 if that is set for the segment and the VMA otherwise,
4800 2. It is an allocated segment,
4801 3. There is an output section associated with it,
4802 4. The section has not already been allocated to a previous segment.
4803 5. PT_GNU_STACK segments do not include any sections.
4804 6. PT_TLS segment includes only SHF_TLS sections.
4805 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. */
4806 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
4807 ((((segment->p_paddr \
4808 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4809 : IS_CONTAINED_BY_VMA (section, segment)) \
4810 && (section->flags & SEC_ALLOC) != 0) \
4811 || IS_COREFILE_NOTE (segment, section)) \
4812 && section->output_section != NULL \
4813 && segment->p_type != PT_GNU_STACK \
4814 && (segment->p_type != PT_TLS \
4815 || (section->flags & SEC_THREAD_LOCAL)) \
4816 && (segment->p_type == PT_LOAD \
4817 || segment->p_type == PT_TLS \
4818 || (section->flags & SEC_THREAD_LOCAL) == 0) \
4819 && ! section->segment_mark)
4821 /* Returns TRUE iff seg1 starts after the end of seg2. */
4822 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
4823 (seg1->field >= SEGMENT_END (seg2, seg2->field))
4825 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
4826 their VMA address ranges and their LMA address ranges overlap.
4827 It is possible to have overlapping VMA ranges without overlapping LMA
4828 ranges. RedBoot images for example can have both .data and .bss mapped
4829 to the same VMA range, but with the .data section mapped to a different
4830 LMA. */
4831 #define SEGMENT_OVERLAPS(seg1, seg2) \
4832 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
4833 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
4834 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
4835 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
4837 /* Initialise the segment mark field. */
4838 for (section = ibfd->sections; section != NULL; section = section->next)
4839 section->segment_mark = FALSE;
4841 /* Scan through the segments specified in the program header
4842 of the input BFD. For this first scan we look for overlaps
4843 in the loadable segments. These can be created by weird
4844 parameters to objcopy. Also, fix some solaris weirdness. */
4845 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4846 i < num_segments;
4847 i++, segment++)
4849 unsigned int j;
4850 Elf_Internal_Phdr *segment2;
4852 if (segment->p_type == PT_INTERP)
4853 for (section = ibfd->sections; section; section = section->next)
4854 if (IS_SOLARIS_PT_INTERP (segment, section))
4856 /* Mininal change so that the normal section to segment
4857 assignment code will work. */
4858 segment->p_vaddr = section->vma;
4859 break;
4862 if (segment->p_type != PT_LOAD)
4863 continue;
4865 /* Determine if this segment overlaps any previous segments. */
4866 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
4868 bfd_signed_vma extra_length;
4870 if (segment2->p_type != PT_LOAD
4871 || ! SEGMENT_OVERLAPS (segment, segment2))
4872 continue;
4874 /* Merge the two segments together. */
4875 if (segment2->p_vaddr < segment->p_vaddr)
4877 /* Extend SEGMENT2 to include SEGMENT and then delete
4878 SEGMENT. */
4879 extra_length =
4880 SEGMENT_END (segment, segment->p_vaddr)
4881 - SEGMENT_END (segment2, segment2->p_vaddr);
4883 if (extra_length > 0)
4885 segment2->p_memsz += extra_length;
4886 segment2->p_filesz += extra_length;
4889 segment->p_type = PT_NULL;
4891 /* Since we have deleted P we must restart the outer loop. */
4892 i = 0;
4893 segment = elf_tdata (ibfd)->phdr;
4894 break;
4896 else
4898 /* Extend SEGMENT to include SEGMENT2 and then delete
4899 SEGMENT2. */
4900 extra_length =
4901 SEGMENT_END (segment2, segment2->p_vaddr)
4902 - SEGMENT_END (segment, segment->p_vaddr);
4904 if (extra_length > 0)
4906 segment->p_memsz += extra_length;
4907 segment->p_filesz += extra_length;
4910 segment2->p_type = PT_NULL;
4915 /* The second scan attempts to assign sections to segments. */
4916 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4917 i < num_segments;
4918 i ++, segment ++)
4920 unsigned int section_count;
4921 asection ** sections;
4922 asection * output_section;
4923 unsigned int isec;
4924 bfd_vma matching_lma;
4925 bfd_vma suggested_lma;
4926 unsigned int j;
4927 bfd_size_type amt;
4929 if (segment->p_type == PT_NULL)
4930 continue;
4932 /* Compute how many sections might be placed into this segment. */
4933 for (section = ibfd->sections, section_count = 0;
4934 section != NULL;
4935 section = section->next)
4936 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
4937 ++section_count;
4939 /* Allocate a segment map big enough to contain
4940 all of the sections we have selected. */
4941 amt = sizeof (struct elf_segment_map);
4942 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
4943 map = bfd_alloc (obfd, amt);
4944 if (map == NULL)
4945 return FALSE;
4947 /* Initialise the fields of the segment map. Default to
4948 using the physical address of the segment in the input BFD. */
4949 map->next = NULL;
4950 map->p_type = segment->p_type;
4951 map->p_flags = segment->p_flags;
4952 map->p_flags_valid = 1;
4953 map->p_paddr = segment->p_paddr;
4954 map->p_paddr_valid = 1;
4956 /* Determine if this segment contains the ELF file header
4957 and if it contains the program headers themselves. */
4958 map->includes_filehdr = (segment->p_offset == 0
4959 && segment->p_filesz >= iehdr->e_ehsize);
4961 map->includes_phdrs = 0;
4963 if (! phdr_included || segment->p_type != PT_LOAD)
4965 map->includes_phdrs =
4966 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
4967 && (segment->p_offset + segment->p_filesz
4968 >= ((bfd_vma) iehdr->e_phoff
4969 + iehdr->e_phnum * iehdr->e_phentsize)));
4971 if (segment->p_type == PT_LOAD && map->includes_phdrs)
4972 phdr_included = TRUE;
4975 if (section_count == 0)
4977 /* Special segments, such as the PT_PHDR segment, may contain
4978 no sections, but ordinary, loadable segments should contain
4979 something. They are allowed by the ELF spec however, so only
4980 a warning is produced. */
4981 if (segment->p_type == PT_LOAD)
4982 (*_bfd_error_handler)
4983 (_("%s: warning: Empty loadable segment detected, is this intentional ?\n"),
4984 bfd_archive_filename (ibfd));
4986 map->count = 0;
4987 *pointer_to_map = map;
4988 pointer_to_map = &map->next;
4990 continue;
4993 /* Now scan the sections in the input BFD again and attempt
4994 to add their corresponding output sections to the segment map.
4995 The problem here is how to handle an output section which has
4996 been moved (ie had its LMA changed). There are four possibilities:
4998 1. None of the sections have been moved.
4999 In this case we can continue to use the segment LMA from the
5000 input BFD.
5002 2. All of the sections have been moved by the same amount.
5003 In this case we can change the segment's LMA to match the LMA
5004 of the first section.
5006 3. Some of the sections have been moved, others have not.
5007 In this case those sections which have not been moved can be
5008 placed in the current segment which will have to have its size,
5009 and possibly its LMA changed, and a new segment or segments will
5010 have to be created to contain the other sections.
5012 4. The sections have been moved, but not by the same amount.
5013 In this case we can change the segment's LMA to match the LMA
5014 of the first section and we will have to create a new segment
5015 or segments to contain the other sections.
5017 In order to save time, we allocate an array to hold the section
5018 pointers that we are interested in. As these sections get assigned
5019 to a segment, they are removed from this array. */
5021 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
5022 to work around this long long bug. */
5023 amt = section_count * sizeof (asection *);
5024 sections = bfd_malloc (amt);
5025 if (sections == NULL)
5026 return FALSE;
5028 /* Step One: Scan for segment vs section LMA conflicts.
5029 Also add the sections to the section array allocated above.
5030 Also add the sections to the current segment. In the common
5031 case, where the sections have not been moved, this means that
5032 we have completely filled the segment, and there is nothing
5033 more to do. */
5034 isec = 0;
5035 matching_lma = 0;
5036 suggested_lma = 0;
5038 for (j = 0, section = ibfd->sections;
5039 section != NULL;
5040 section = section->next)
5042 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5044 output_section = section->output_section;
5046 sections[j ++] = section;
5048 /* The Solaris native linker always sets p_paddr to 0.
5049 We try to catch that case here, and set it to the
5050 correct value. Note - some backends require that
5051 p_paddr be left as zero. */
5052 if (segment->p_paddr == 0
5053 && segment->p_vaddr != 0
5054 && (! bed->want_p_paddr_set_to_zero)
5055 && isec == 0
5056 && output_section->lma != 0
5057 && (output_section->vma == (segment->p_vaddr
5058 + (map->includes_filehdr
5059 ? iehdr->e_ehsize
5060 : 0)
5061 + (map->includes_phdrs
5062 ? (iehdr->e_phnum
5063 * iehdr->e_phentsize)
5064 : 0))))
5065 map->p_paddr = segment->p_vaddr;
5067 /* Match up the physical address of the segment with the
5068 LMA address of the output section. */
5069 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5070 || IS_COREFILE_NOTE (segment, section)
5071 || (bed->want_p_paddr_set_to_zero &&
5072 IS_CONTAINED_BY_VMA (output_section, segment))
5075 if (matching_lma == 0)
5076 matching_lma = output_section->lma;
5078 /* We assume that if the section fits within the segment
5079 then it does not overlap any other section within that
5080 segment. */
5081 map->sections[isec ++] = output_section;
5083 else if (suggested_lma == 0)
5084 suggested_lma = output_section->lma;
5088 BFD_ASSERT (j == section_count);
5090 /* Step Two: Adjust the physical address of the current segment,
5091 if necessary. */
5092 if (isec == section_count)
5094 /* All of the sections fitted within the segment as currently
5095 specified. This is the default case. Add the segment to
5096 the list of built segments and carry on to process the next
5097 program header in the input BFD. */
5098 map->count = section_count;
5099 *pointer_to_map = map;
5100 pointer_to_map = &map->next;
5102 free (sections);
5103 continue;
5105 else
5107 if (matching_lma != 0)
5109 /* At least one section fits inside the current segment.
5110 Keep it, but modify its physical address to match the
5111 LMA of the first section that fitted. */
5112 map->p_paddr = matching_lma;
5114 else
5116 /* None of the sections fitted inside the current segment.
5117 Change the current segment's physical address to match
5118 the LMA of the first section. */
5119 map->p_paddr = suggested_lma;
5122 /* Offset the segment physical address from the lma
5123 to allow for space taken up by elf headers. */
5124 if (map->includes_filehdr)
5125 map->p_paddr -= iehdr->e_ehsize;
5127 if (map->includes_phdrs)
5129 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5131 /* iehdr->e_phnum is just an estimate of the number
5132 of program headers that we will need. Make a note
5133 here of the number we used and the segment we chose
5134 to hold these headers, so that we can adjust the
5135 offset when we know the correct value. */
5136 phdr_adjust_num = iehdr->e_phnum;
5137 phdr_adjust_seg = map;
5141 /* Step Three: Loop over the sections again, this time assigning
5142 those that fit to the current segment and removing them from the
5143 sections array; but making sure not to leave large gaps. Once all
5144 possible sections have been assigned to the current segment it is
5145 added to the list of built segments and if sections still remain
5146 to be assigned, a new segment is constructed before repeating
5147 the loop. */
5148 isec = 0;
5151 map->count = 0;
5152 suggested_lma = 0;
5154 /* Fill the current segment with sections that fit. */
5155 for (j = 0; j < section_count; j++)
5157 section = sections[j];
5159 if (section == NULL)
5160 continue;
5162 output_section = section->output_section;
5164 BFD_ASSERT (output_section != NULL);
5166 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5167 || IS_COREFILE_NOTE (segment, section))
5169 if (map->count == 0)
5171 /* If the first section in a segment does not start at
5172 the beginning of the segment, then something is
5173 wrong. */
5174 if (output_section->lma !=
5175 (map->p_paddr
5176 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5177 + (map->includes_phdrs
5178 ? iehdr->e_phnum * iehdr->e_phentsize
5179 : 0)))
5180 abort ();
5182 else
5184 asection * prev_sec;
5186 prev_sec = map->sections[map->count - 1];
5188 /* If the gap between the end of the previous section
5189 and the start of this section is more than
5190 maxpagesize then we need to start a new segment. */
5191 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
5192 maxpagesize)
5193 < BFD_ALIGN (output_section->lma, maxpagesize))
5194 || ((prev_sec->lma + prev_sec->size)
5195 > output_section->lma))
5197 if (suggested_lma == 0)
5198 suggested_lma = output_section->lma;
5200 continue;
5204 map->sections[map->count++] = output_section;
5205 ++isec;
5206 sections[j] = NULL;
5207 section->segment_mark = TRUE;
5209 else if (suggested_lma == 0)
5210 suggested_lma = output_section->lma;
5213 BFD_ASSERT (map->count > 0);
5215 /* Add the current segment to the list of built segments. */
5216 *pointer_to_map = map;
5217 pointer_to_map = &map->next;
5219 if (isec < section_count)
5221 /* We still have not allocated all of the sections to
5222 segments. Create a new segment here, initialise it
5223 and carry on looping. */
5224 amt = sizeof (struct elf_segment_map);
5225 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5226 map = bfd_alloc (obfd, amt);
5227 if (map == NULL)
5229 free (sections);
5230 return FALSE;
5233 /* Initialise the fields of the segment map. Set the physical
5234 physical address to the LMA of the first section that has
5235 not yet been assigned. */
5236 map->next = NULL;
5237 map->p_type = segment->p_type;
5238 map->p_flags = segment->p_flags;
5239 map->p_flags_valid = 1;
5240 map->p_paddr = suggested_lma;
5241 map->p_paddr_valid = 1;
5242 map->includes_filehdr = 0;
5243 map->includes_phdrs = 0;
5246 while (isec < section_count);
5248 free (sections);
5251 /* The Solaris linker creates program headers in which all the
5252 p_paddr fields are zero. When we try to objcopy or strip such a
5253 file, we get confused. Check for this case, and if we find it
5254 reset the p_paddr_valid fields. */
5255 for (map = map_first; map != NULL; map = map->next)
5256 if (map->p_paddr != 0)
5257 break;
5258 if (map == NULL)
5259 for (map = map_first; map != NULL; map = map->next)
5260 map->p_paddr_valid = 0;
5262 elf_tdata (obfd)->segment_map = map_first;
5264 /* If we had to estimate the number of program headers that were
5265 going to be needed, then check our estimate now and adjust
5266 the offset if necessary. */
5267 if (phdr_adjust_seg != NULL)
5269 unsigned int count;
5271 for (count = 0, map = map_first; map != NULL; map = map->next)
5272 count++;
5274 if (count > phdr_adjust_num)
5275 phdr_adjust_seg->p_paddr
5276 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5279 #if 0
5280 /* Final Step: Sort the segments into ascending order of physical
5281 address. */
5282 if (map_first != NULL)
5284 struct elf_segment_map *prev;
5286 prev = map_first;
5287 for (map = map_first->next; map != NULL; prev = map, map = map->next)
5289 /* Yes I know - its a bubble sort.... */
5290 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
5292 /* Swap map and map->next. */
5293 prev->next = map->next;
5294 map->next = map->next->next;
5295 prev->next->next = map;
5297 /* Restart loop. */
5298 map = map_first;
5302 #endif
5304 #undef SEGMENT_END
5305 #undef SECTION_SIZE
5306 #undef IS_CONTAINED_BY_VMA
5307 #undef IS_CONTAINED_BY_LMA
5308 #undef IS_COREFILE_NOTE
5309 #undef IS_SOLARIS_PT_INTERP
5310 #undef INCLUDE_SECTION_IN_SEGMENT
5311 #undef SEGMENT_AFTER_SEGMENT
5312 #undef SEGMENT_OVERLAPS
5313 return TRUE;
5316 /* Copy private section information. This copies over the entsize
5317 field, and sometimes the info field. */
5319 bfd_boolean
5320 _bfd_elf_copy_private_section_data (bfd *ibfd,
5321 asection *isec,
5322 bfd *obfd,
5323 asection *osec)
5325 Elf_Internal_Shdr *ihdr, *ohdr;
5327 if (ibfd->xvec->flavour != bfd_target_elf_flavour
5328 || obfd->xvec->flavour != bfd_target_elf_flavour)
5329 return TRUE;
5331 ihdr = &elf_section_data (isec)->this_hdr;
5332 ohdr = &elf_section_data (osec)->this_hdr;
5334 ohdr->sh_entsize = ihdr->sh_entsize;
5336 if (ihdr->sh_type == SHT_SYMTAB
5337 || ihdr->sh_type == SHT_DYNSYM
5338 || ihdr->sh_type == SHT_GNU_verneed
5339 || ihdr->sh_type == SHT_GNU_verdef)
5340 ohdr->sh_info = ihdr->sh_info;
5342 /* Set things up for objcopy. The output SHT_GROUP section will
5343 have its elf_next_in_group pointing back to the input group
5344 members. */
5345 elf_next_in_group (osec) = elf_next_in_group (isec);
5346 elf_group_name (osec) = elf_group_name (isec);
5348 osec->use_rela_p = isec->use_rela_p;
5350 return TRUE;
5353 /* Copy private header information. */
5355 bfd_boolean
5356 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
5358 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5359 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5360 return TRUE;
5362 /* Copy over private BFD data if it has not already been copied.
5363 This must be done here, rather than in the copy_private_bfd_data
5364 entry point, because the latter is called after the section
5365 contents have been set, which means that the program headers have
5366 already been worked out. */
5367 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
5369 if (! copy_private_bfd_data (ibfd, obfd))
5370 return FALSE;
5373 return TRUE;
5376 /* Copy private symbol information. If this symbol is in a section
5377 which we did not map into a BFD section, try to map the section
5378 index correctly. We use special macro definitions for the mapped
5379 section indices; these definitions are interpreted by the
5380 swap_out_syms function. */
5382 #define MAP_ONESYMTAB (SHN_HIOS + 1)
5383 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
5384 #define MAP_STRTAB (SHN_HIOS + 3)
5385 #define MAP_SHSTRTAB (SHN_HIOS + 4)
5386 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
5388 bfd_boolean
5389 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
5390 asymbol *isymarg,
5391 bfd *obfd,
5392 asymbol *osymarg)
5394 elf_symbol_type *isym, *osym;
5396 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5397 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5398 return TRUE;
5400 isym = elf_symbol_from (ibfd, isymarg);
5401 osym = elf_symbol_from (obfd, osymarg);
5403 if (isym != NULL
5404 && osym != NULL
5405 && bfd_is_abs_section (isym->symbol.section))
5407 unsigned int shndx;
5409 shndx = isym->internal_elf_sym.st_shndx;
5410 if (shndx == elf_onesymtab (ibfd))
5411 shndx = MAP_ONESYMTAB;
5412 else if (shndx == elf_dynsymtab (ibfd))
5413 shndx = MAP_DYNSYMTAB;
5414 else if (shndx == elf_tdata (ibfd)->strtab_section)
5415 shndx = MAP_STRTAB;
5416 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
5417 shndx = MAP_SHSTRTAB;
5418 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
5419 shndx = MAP_SYM_SHNDX;
5420 osym->internal_elf_sym.st_shndx = shndx;
5423 return TRUE;
5426 /* Swap out the symbols. */
5428 static bfd_boolean
5429 swap_out_syms (bfd *abfd,
5430 struct bfd_strtab_hash **sttp,
5431 int relocatable_p)
5433 const struct elf_backend_data *bed;
5434 int symcount;
5435 asymbol **syms;
5436 struct bfd_strtab_hash *stt;
5437 Elf_Internal_Shdr *symtab_hdr;
5438 Elf_Internal_Shdr *symtab_shndx_hdr;
5439 Elf_Internal_Shdr *symstrtab_hdr;
5440 char *outbound_syms;
5441 char *outbound_shndx;
5442 int idx;
5443 bfd_size_type amt;
5444 bfd_boolean name_local_sections;
5446 if (!elf_map_symbols (abfd))
5447 return FALSE;
5449 /* Dump out the symtabs. */
5450 stt = _bfd_elf_stringtab_init ();
5451 if (stt == NULL)
5452 return FALSE;
5454 bed = get_elf_backend_data (abfd);
5455 symcount = bfd_get_symcount (abfd);
5456 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5457 symtab_hdr->sh_type = SHT_SYMTAB;
5458 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
5459 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
5460 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
5461 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
5463 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5464 symstrtab_hdr->sh_type = SHT_STRTAB;
5466 amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym;
5467 outbound_syms = bfd_alloc (abfd, amt);
5468 if (outbound_syms == NULL)
5470 _bfd_stringtab_free (stt);
5471 return FALSE;
5473 symtab_hdr->contents = outbound_syms;
5475 outbound_shndx = NULL;
5476 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
5477 if (symtab_shndx_hdr->sh_name != 0)
5479 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
5480 outbound_shndx = bfd_zalloc (abfd, amt);
5481 if (outbound_shndx == NULL)
5483 _bfd_stringtab_free (stt);
5484 return FALSE;
5487 symtab_shndx_hdr->contents = outbound_shndx;
5488 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
5489 symtab_shndx_hdr->sh_size = amt;
5490 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
5491 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
5494 /* Now generate the data (for "contents"). */
5496 /* Fill in zeroth symbol and swap it out. */
5497 Elf_Internal_Sym sym;
5498 sym.st_name = 0;
5499 sym.st_value = 0;
5500 sym.st_size = 0;
5501 sym.st_info = 0;
5502 sym.st_other = 0;
5503 sym.st_shndx = SHN_UNDEF;
5504 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5505 outbound_syms += bed->s->sizeof_sym;
5506 if (outbound_shndx != NULL)
5507 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5510 name_local_sections
5511 = (bed->elf_backend_name_local_section_symbols
5512 && bed->elf_backend_name_local_section_symbols (abfd));
5514 syms = bfd_get_outsymbols (abfd);
5515 for (idx = 0; idx < symcount; idx++)
5517 Elf_Internal_Sym sym;
5518 bfd_vma value = syms[idx]->value;
5519 elf_symbol_type *type_ptr;
5520 flagword flags = syms[idx]->flags;
5521 int type;
5523 if (!name_local_sections
5524 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
5526 /* Local section symbols have no name. */
5527 sym.st_name = 0;
5529 else
5531 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
5532 syms[idx]->name,
5533 TRUE, FALSE);
5534 if (sym.st_name == (unsigned long) -1)
5536 _bfd_stringtab_free (stt);
5537 return FALSE;
5541 type_ptr = elf_symbol_from (abfd, syms[idx]);
5543 if ((flags & BSF_SECTION_SYM) == 0
5544 && bfd_is_com_section (syms[idx]->section))
5546 /* ELF common symbols put the alignment into the `value' field,
5547 and the size into the `size' field. This is backwards from
5548 how BFD handles it, so reverse it here. */
5549 sym.st_size = value;
5550 if (type_ptr == NULL
5551 || type_ptr->internal_elf_sym.st_value == 0)
5552 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
5553 else
5554 sym.st_value = type_ptr->internal_elf_sym.st_value;
5555 sym.st_shndx = _bfd_elf_section_from_bfd_section
5556 (abfd, syms[idx]->section);
5558 else
5560 asection *sec = syms[idx]->section;
5561 int shndx;
5563 if (sec->output_section)
5565 value += sec->output_offset;
5566 sec = sec->output_section;
5569 /* Don't add in the section vma for relocatable output. */
5570 if (! relocatable_p)
5571 value += sec->vma;
5572 sym.st_value = value;
5573 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
5575 if (bfd_is_abs_section (sec)
5576 && type_ptr != NULL
5577 && type_ptr->internal_elf_sym.st_shndx != 0)
5579 /* This symbol is in a real ELF section which we did
5580 not create as a BFD section. Undo the mapping done
5581 by copy_private_symbol_data. */
5582 shndx = type_ptr->internal_elf_sym.st_shndx;
5583 switch (shndx)
5585 case MAP_ONESYMTAB:
5586 shndx = elf_onesymtab (abfd);
5587 break;
5588 case MAP_DYNSYMTAB:
5589 shndx = elf_dynsymtab (abfd);
5590 break;
5591 case MAP_STRTAB:
5592 shndx = elf_tdata (abfd)->strtab_section;
5593 break;
5594 case MAP_SHSTRTAB:
5595 shndx = elf_tdata (abfd)->shstrtab_section;
5596 break;
5597 case MAP_SYM_SHNDX:
5598 shndx = elf_tdata (abfd)->symtab_shndx_section;
5599 break;
5600 default:
5601 break;
5604 else
5606 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
5608 if (shndx == -1)
5610 asection *sec2;
5612 /* Writing this would be a hell of a lot easier if
5613 we had some decent documentation on bfd, and
5614 knew what to expect of the library, and what to
5615 demand of applications. For example, it
5616 appears that `objcopy' might not set the
5617 section of a symbol to be a section that is
5618 actually in the output file. */
5619 sec2 = bfd_get_section_by_name (abfd, sec->name);
5620 if (sec2 == NULL)
5622 _bfd_error_handler (_("\
5623 Unable to find equivalent output section for symbol '%s' from section '%s'"),
5624 syms[idx]->name ? syms[idx]->name : "<Local sym>",
5625 sec->name);
5626 bfd_set_error (bfd_error_invalid_operation);
5627 _bfd_stringtab_free (stt);
5628 return FALSE;
5631 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
5632 BFD_ASSERT (shndx != -1);
5636 sym.st_shndx = shndx;
5639 if ((flags & BSF_THREAD_LOCAL) != 0)
5640 type = STT_TLS;
5641 else if ((flags & BSF_FUNCTION) != 0)
5642 type = STT_FUNC;
5643 else if ((flags & BSF_OBJECT) != 0)
5644 type = STT_OBJECT;
5645 else
5646 type = STT_NOTYPE;
5648 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
5649 type = STT_TLS;
5651 /* Processor-specific types. */
5652 if (type_ptr != NULL
5653 && bed->elf_backend_get_symbol_type)
5654 type = ((*bed->elf_backend_get_symbol_type)
5655 (&type_ptr->internal_elf_sym, type));
5657 if (flags & BSF_SECTION_SYM)
5659 if (flags & BSF_GLOBAL)
5660 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5661 else
5662 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5664 else if (bfd_is_com_section (syms[idx]->section))
5665 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
5666 else if (bfd_is_und_section (syms[idx]->section))
5667 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
5668 ? STB_WEAK
5669 : STB_GLOBAL),
5670 type);
5671 else if (flags & BSF_FILE)
5672 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5673 else
5675 int bind = STB_LOCAL;
5677 if (flags & BSF_LOCAL)
5678 bind = STB_LOCAL;
5679 else if (flags & BSF_WEAK)
5680 bind = STB_WEAK;
5681 else if (flags & BSF_GLOBAL)
5682 bind = STB_GLOBAL;
5684 sym.st_info = ELF_ST_INFO (bind, type);
5687 if (type_ptr != NULL)
5688 sym.st_other = type_ptr->internal_elf_sym.st_other;
5689 else
5690 sym.st_other = 0;
5692 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5693 outbound_syms += bed->s->sizeof_sym;
5694 if (outbound_shndx != NULL)
5695 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5698 *sttp = stt;
5699 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
5700 symstrtab_hdr->sh_type = SHT_STRTAB;
5702 symstrtab_hdr->sh_flags = 0;
5703 symstrtab_hdr->sh_addr = 0;
5704 symstrtab_hdr->sh_entsize = 0;
5705 symstrtab_hdr->sh_link = 0;
5706 symstrtab_hdr->sh_info = 0;
5707 symstrtab_hdr->sh_addralign = 1;
5709 return TRUE;
5712 /* Return the number of bytes required to hold the symtab vector.
5714 Note that we base it on the count plus 1, since we will null terminate
5715 the vector allocated based on this size. However, the ELF symbol table
5716 always has a dummy entry as symbol #0, so it ends up even. */
5718 long
5719 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
5721 long symcount;
5722 long symtab_size;
5723 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
5725 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5726 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5727 if (symcount > 0)
5728 symtab_size -= sizeof (asymbol *);
5730 return symtab_size;
5733 long
5734 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
5736 long symcount;
5737 long symtab_size;
5738 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
5740 if (elf_dynsymtab (abfd) == 0)
5742 bfd_set_error (bfd_error_invalid_operation);
5743 return -1;
5746 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5747 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5748 if (symcount > 0)
5749 symtab_size -= sizeof (asymbol *);
5751 return symtab_size;
5754 long
5755 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
5756 sec_ptr asect)
5758 return (asect->reloc_count + 1) * sizeof (arelent *);
5761 /* Canonicalize the relocs. */
5763 long
5764 _bfd_elf_canonicalize_reloc (bfd *abfd,
5765 sec_ptr section,
5766 arelent **relptr,
5767 asymbol **symbols)
5769 arelent *tblptr;
5770 unsigned int i;
5771 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5773 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
5774 return -1;
5776 tblptr = section->relocation;
5777 for (i = 0; i < section->reloc_count; i++)
5778 *relptr++ = tblptr++;
5780 *relptr = NULL;
5782 return section->reloc_count;
5785 long
5786 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
5788 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5789 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
5791 if (symcount >= 0)
5792 bfd_get_symcount (abfd) = symcount;
5793 return symcount;
5796 long
5797 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
5798 asymbol **allocation)
5800 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5801 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
5803 if (symcount >= 0)
5804 bfd_get_dynamic_symcount (abfd) = symcount;
5805 return symcount;
5808 /* Return the size required for the dynamic reloc entries. Any
5809 section that was actually installed in the BFD, and has type
5810 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5811 considered to be a dynamic reloc section. */
5813 long
5814 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
5816 long ret;
5817 asection *s;
5819 if (elf_dynsymtab (abfd) == 0)
5821 bfd_set_error (bfd_error_invalid_operation);
5822 return -1;
5825 ret = sizeof (arelent *);
5826 for (s = abfd->sections; s != NULL; s = s->next)
5827 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5828 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5829 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5830 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
5831 * sizeof (arelent *));
5833 return ret;
5836 /* Canonicalize the dynamic relocation entries. Note that we return
5837 the dynamic relocations as a single block, although they are
5838 actually associated with particular sections; the interface, which
5839 was designed for SunOS style shared libraries, expects that there
5840 is only one set of dynamic relocs. Any section that was actually
5841 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5842 the dynamic symbol table, is considered to be a dynamic reloc
5843 section. */
5845 long
5846 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
5847 arelent **storage,
5848 asymbol **syms)
5850 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
5851 asection *s;
5852 long ret;
5854 if (elf_dynsymtab (abfd) == 0)
5856 bfd_set_error (bfd_error_invalid_operation);
5857 return -1;
5860 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
5861 ret = 0;
5862 for (s = abfd->sections; s != NULL; s = s->next)
5864 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5865 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5866 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5868 arelent *p;
5869 long count, i;
5871 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
5872 return -1;
5873 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
5874 p = s->relocation;
5875 for (i = 0; i < count; i++)
5876 *storage++ = p++;
5877 ret += count;
5881 *storage = NULL;
5883 return ret;
5886 /* Read in the version information. */
5888 bfd_boolean
5889 _bfd_elf_slurp_version_tables (bfd *abfd)
5891 bfd_byte *contents = NULL;
5892 bfd_size_type amt;
5894 if (elf_dynverdef (abfd) != 0)
5896 Elf_Internal_Shdr *hdr;
5897 Elf_External_Verdef *everdef;
5898 Elf_Internal_Verdef *iverdef;
5899 Elf_Internal_Verdef *iverdefarr;
5900 Elf_Internal_Verdef iverdefmem;
5901 unsigned int i;
5902 unsigned int maxidx;
5904 hdr = &elf_tdata (abfd)->dynverdef_hdr;
5906 contents = bfd_malloc (hdr->sh_size);
5907 if (contents == NULL)
5908 goto error_return;
5909 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5910 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
5911 goto error_return;
5913 /* We know the number of entries in the section but not the maximum
5914 index. Therefore we have to run through all entries and find
5915 the maximum. */
5916 everdef = (Elf_External_Verdef *) contents;
5917 maxidx = 0;
5918 for (i = 0; i < hdr->sh_info; ++i)
5920 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5922 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
5923 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
5925 everdef = ((Elf_External_Verdef *)
5926 ((bfd_byte *) everdef + iverdefmem.vd_next));
5929 amt = (bfd_size_type) maxidx * sizeof (Elf_Internal_Verdef);
5930 elf_tdata (abfd)->verdef = bfd_zalloc (abfd, amt);
5931 if (elf_tdata (abfd)->verdef == NULL)
5932 goto error_return;
5934 elf_tdata (abfd)->cverdefs = maxidx;
5936 everdef = (Elf_External_Verdef *) contents;
5937 iverdefarr = elf_tdata (abfd)->verdef;
5938 for (i = 0; i < hdr->sh_info; i++)
5940 Elf_External_Verdaux *everdaux;
5941 Elf_Internal_Verdaux *iverdaux;
5942 unsigned int j;
5944 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5946 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
5947 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
5949 iverdef->vd_bfd = abfd;
5951 amt = (bfd_size_type) iverdef->vd_cnt * sizeof (Elf_Internal_Verdaux);
5952 iverdef->vd_auxptr = bfd_alloc (abfd, amt);
5953 if (iverdef->vd_auxptr == NULL)
5954 goto error_return;
5956 everdaux = ((Elf_External_Verdaux *)
5957 ((bfd_byte *) everdef + iverdef->vd_aux));
5958 iverdaux = iverdef->vd_auxptr;
5959 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
5961 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
5963 iverdaux->vda_nodename =
5964 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5965 iverdaux->vda_name);
5966 if (iverdaux->vda_nodename == NULL)
5967 goto error_return;
5969 if (j + 1 < iverdef->vd_cnt)
5970 iverdaux->vda_nextptr = iverdaux + 1;
5971 else
5972 iverdaux->vda_nextptr = NULL;
5974 everdaux = ((Elf_External_Verdaux *)
5975 ((bfd_byte *) everdaux + iverdaux->vda_next));
5978 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
5980 if (i + 1 < hdr->sh_info)
5981 iverdef->vd_nextdef = iverdef + 1;
5982 else
5983 iverdef->vd_nextdef = NULL;
5985 everdef = ((Elf_External_Verdef *)
5986 ((bfd_byte *) everdef + iverdef->vd_next));
5989 free (contents);
5990 contents = NULL;
5993 if (elf_dynverref (abfd) != 0)
5995 Elf_Internal_Shdr *hdr;
5996 Elf_External_Verneed *everneed;
5997 Elf_Internal_Verneed *iverneed;
5998 unsigned int i;
6000 hdr = &elf_tdata (abfd)->dynverref_hdr;
6002 amt = (bfd_size_type) hdr->sh_info * sizeof (Elf_Internal_Verneed);
6003 elf_tdata (abfd)->verref = bfd_zalloc (abfd, amt);
6004 if (elf_tdata (abfd)->verref == NULL)
6005 goto error_return;
6007 elf_tdata (abfd)->cverrefs = hdr->sh_info;
6009 contents = bfd_malloc (hdr->sh_size);
6010 if (contents == NULL)
6011 goto error_return;
6012 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6013 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6014 goto error_return;
6016 everneed = (Elf_External_Verneed *) contents;
6017 iverneed = elf_tdata (abfd)->verref;
6018 for (i = 0; i < hdr->sh_info; i++, iverneed++)
6020 Elf_External_Vernaux *evernaux;
6021 Elf_Internal_Vernaux *ivernaux;
6022 unsigned int j;
6024 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
6026 iverneed->vn_bfd = abfd;
6028 iverneed->vn_filename =
6029 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6030 iverneed->vn_file);
6031 if (iverneed->vn_filename == NULL)
6032 goto error_return;
6034 amt = iverneed->vn_cnt;
6035 amt *= sizeof (Elf_Internal_Vernaux);
6036 iverneed->vn_auxptr = bfd_alloc (abfd, amt);
6038 evernaux = ((Elf_External_Vernaux *)
6039 ((bfd_byte *) everneed + iverneed->vn_aux));
6040 ivernaux = iverneed->vn_auxptr;
6041 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
6043 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
6045 ivernaux->vna_nodename =
6046 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6047 ivernaux->vna_name);
6048 if (ivernaux->vna_nodename == NULL)
6049 goto error_return;
6051 if (j + 1 < iverneed->vn_cnt)
6052 ivernaux->vna_nextptr = ivernaux + 1;
6053 else
6054 ivernaux->vna_nextptr = NULL;
6056 evernaux = ((Elf_External_Vernaux *)
6057 ((bfd_byte *) evernaux + ivernaux->vna_next));
6060 if (i + 1 < hdr->sh_info)
6061 iverneed->vn_nextref = iverneed + 1;
6062 else
6063 iverneed->vn_nextref = NULL;
6065 everneed = ((Elf_External_Verneed *)
6066 ((bfd_byte *) everneed + iverneed->vn_next));
6069 free (contents);
6070 contents = NULL;
6073 return TRUE;
6075 error_return:
6076 if (contents != NULL)
6077 free (contents);
6078 return FALSE;
6081 asymbol *
6082 _bfd_elf_make_empty_symbol (bfd *abfd)
6084 elf_symbol_type *newsym;
6085 bfd_size_type amt = sizeof (elf_symbol_type);
6087 newsym = bfd_zalloc (abfd, amt);
6088 if (!newsym)
6089 return NULL;
6090 else
6092 newsym->symbol.the_bfd = abfd;
6093 return &newsym->symbol;
6097 void
6098 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
6099 asymbol *symbol,
6100 symbol_info *ret)
6102 bfd_symbol_info (symbol, ret);
6105 /* Return whether a symbol name implies a local symbol. Most targets
6106 use this function for the is_local_label_name entry point, but some
6107 override it. */
6109 bfd_boolean
6110 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
6111 const char *name)
6113 /* Normal local symbols start with ``.L''. */
6114 if (name[0] == '.' && name[1] == 'L')
6115 return TRUE;
6117 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
6118 DWARF debugging symbols starting with ``..''. */
6119 if (name[0] == '.' && name[1] == '.')
6120 return TRUE;
6122 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
6123 emitting DWARF debugging output. I suspect this is actually a
6124 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
6125 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
6126 underscore to be emitted on some ELF targets). For ease of use,
6127 we treat such symbols as local. */
6128 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
6129 return TRUE;
6131 return FALSE;
6134 alent *
6135 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
6136 asymbol *symbol ATTRIBUTE_UNUSED)
6138 abort ();
6139 return NULL;
6142 bfd_boolean
6143 _bfd_elf_set_arch_mach (bfd *abfd,
6144 enum bfd_architecture arch,
6145 unsigned long machine)
6147 /* If this isn't the right architecture for this backend, and this
6148 isn't the generic backend, fail. */
6149 if (arch != get_elf_backend_data (abfd)->arch
6150 && arch != bfd_arch_unknown
6151 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
6152 return FALSE;
6154 return bfd_default_set_arch_mach (abfd, arch, machine);
6157 /* Find the function to a particular section and offset,
6158 for error reporting. */
6160 static bfd_boolean
6161 elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
6162 asection *section,
6163 asymbol **symbols,
6164 bfd_vma offset,
6165 const char **filename_ptr,
6166 const char **functionname_ptr)
6168 const char *filename;
6169 asymbol *func;
6170 bfd_vma low_func;
6171 asymbol **p;
6173 filename = NULL;
6174 func = NULL;
6175 low_func = 0;
6177 for (p = symbols; *p != NULL; p++)
6179 elf_symbol_type *q;
6181 q = (elf_symbol_type *) *p;
6183 if (bfd_get_section (&q->symbol) != section)
6184 continue;
6186 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
6188 default:
6189 break;
6190 case STT_FILE:
6191 filename = bfd_asymbol_name (&q->symbol);
6192 break;
6193 case STT_NOTYPE:
6194 case STT_FUNC:
6195 if (q->symbol.section == section
6196 && q->symbol.value >= low_func
6197 && q->symbol.value <= offset)
6199 func = (asymbol *) q;
6200 low_func = q->symbol.value;
6202 break;
6206 if (func == NULL)
6207 return FALSE;
6209 if (filename_ptr)
6210 *filename_ptr = filename;
6211 if (functionname_ptr)
6212 *functionname_ptr = bfd_asymbol_name (func);
6214 return TRUE;
6217 /* Find the nearest line to a particular section and offset,
6218 for error reporting. */
6220 bfd_boolean
6221 _bfd_elf_find_nearest_line (bfd *abfd,
6222 asection *section,
6223 asymbol **symbols,
6224 bfd_vma offset,
6225 const char **filename_ptr,
6226 const char **functionname_ptr,
6227 unsigned int *line_ptr)
6229 bfd_boolean found;
6231 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
6232 filename_ptr, functionname_ptr,
6233 line_ptr))
6235 if (!*functionname_ptr)
6236 elf_find_function (abfd, section, symbols, offset,
6237 *filename_ptr ? NULL : filename_ptr,
6238 functionname_ptr);
6240 return TRUE;
6243 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
6244 filename_ptr, functionname_ptr,
6245 line_ptr, 0,
6246 &elf_tdata (abfd)->dwarf2_find_line_info))
6248 if (!*functionname_ptr)
6249 elf_find_function (abfd, section, symbols, offset,
6250 *filename_ptr ? NULL : filename_ptr,
6251 functionname_ptr);
6253 return TRUE;
6256 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
6257 &found, filename_ptr,
6258 functionname_ptr, line_ptr,
6259 &elf_tdata (abfd)->line_info))
6260 return FALSE;
6261 if (found && (*functionname_ptr || *line_ptr))
6262 return TRUE;
6264 if (symbols == NULL)
6265 return FALSE;
6267 if (! elf_find_function (abfd, section, symbols, offset,
6268 filename_ptr, functionname_ptr))
6269 return FALSE;
6271 *line_ptr = 0;
6272 return TRUE;
6276 _bfd_elf_sizeof_headers (bfd *abfd, bfd_boolean reloc)
6278 int ret;
6280 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
6281 if (! reloc)
6282 ret += get_program_header_size (abfd);
6283 return ret;
6286 bfd_boolean
6287 _bfd_elf_set_section_contents (bfd *abfd,
6288 sec_ptr section,
6289 const void *location,
6290 file_ptr offset,
6291 bfd_size_type count)
6293 Elf_Internal_Shdr *hdr;
6294 bfd_signed_vma pos;
6296 if (! abfd->output_has_begun
6297 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
6298 return FALSE;
6300 hdr = &elf_section_data (section)->this_hdr;
6301 pos = hdr->sh_offset + offset;
6302 if (bfd_seek (abfd, pos, SEEK_SET) != 0
6303 || bfd_bwrite (location, count, abfd) != count)
6304 return FALSE;
6306 return TRUE;
6309 void
6310 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
6311 arelent *cache_ptr ATTRIBUTE_UNUSED,
6312 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
6314 abort ();
6317 /* Try to convert a non-ELF reloc into an ELF one. */
6319 bfd_boolean
6320 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
6322 /* Check whether we really have an ELF howto. */
6324 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
6326 bfd_reloc_code_real_type code;
6327 reloc_howto_type *howto;
6329 /* Alien reloc: Try to determine its type to replace it with an
6330 equivalent ELF reloc. */
6332 if (areloc->howto->pc_relative)
6334 switch (areloc->howto->bitsize)
6336 case 8:
6337 code = BFD_RELOC_8_PCREL;
6338 break;
6339 case 12:
6340 code = BFD_RELOC_12_PCREL;
6341 break;
6342 case 16:
6343 code = BFD_RELOC_16_PCREL;
6344 break;
6345 case 24:
6346 code = BFD_RELOC_24_PCREL;
6347 break;
6348 case 32:
6349 code = BFD_RELOC_32_PCREL;
6350 break;
6351 case 64:
6352 code = BFD_RELOC_64_PCREL;
6353 break;
6354 default:
6355 goto fail;
6358 howto = bfd_reloc_type_lookup (abfd, code);
6360 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
6362 if (howto->pcrel_offset)
6363 areloc->addend += areloc->address;
6364 else
6365 areloc->addend -= areloc->address; /* addend is unsigned!! */
6368 else
6370 switch (areloc->howto->bitsize)
6372 case 8:
6373 code = BFD_RELOC_8;
6374 break;
6375 case 14:
6376 code = BFD_RELOC_14;
6377 break;
6378 case 16:
6379 code = BFD_RELOC_16;
6380 break;
6381 case 26:
6382 code = BFD_RELOC_26;
6383 break;
6384 case 32:
6385 code = BFD_RELOC_32;
6386 break;
6387 case 64:
6388 code = BFD_RELOC_64;
6389 break;
6390 default:
6391 goto fail;
6394 howto = bfd_reloc_type_lookup (abfd, code);
6397 if (howto)
6398 areloc->howto = howto;
6399 else
6400 goto fail;
6403 return TRUE;
6405 fail:
6406 (*_bfd_error_handler)
6407 (_("%s: unsupported relocation type %s"),
6408 bfd_archive_filename (abfd), areloc->howto->name);
6409 bfd_set_error (bfd_error_bad_value);
6410 return FALSE;
6413 bfd_boolean
6414 _bfd_elf_close_and_cleanup (bfd *abfd)
6416 if (bfd_get_format (abfd) == bfd_object)
6418 if (elf_shstrtab (abfd) != NULL)
6419 _bfd_elf_strtab_free (elf_shstrtab (abfd));
6422 return _bfd_generic_close_and_cleanup (abfd);
6425 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
6426 in the relocation's offset. Thus we cannot allow any sort of sanity
6427 range-checking to interfere. There is nothing else to do in processing
6428 this reloc. */
6430 bfd_reloc_status_type
6431 _bfd_elf_rel_vtable_reloc_fn
6432 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
6433 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
6434 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
6435 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
6437 return bfd_reloc_ok;
6440 /* Elf core file support. Much of this only works on native
6441 toolchains, since we rely on knowing the
6442 machine-dependent procfs structure in order to pick
6443 out details about the corefile. */
6445 #ifdef HAVE_SYS_PROCFS_H
6446 # include <sys/procfs.h>
6447 #endif
6449 /* FIXME: this is kinda wrong, but it's what gdb wants. */
6451 static int
6452 elfcore_make_pid (bfd *abfd)
6454 return ((elf_tdata (abfd)->core_lwpid << 16)
6455 + (elf_tdata (abfd)->core_pid));
6458 /* If there isn't a section called NAME, make one, using
6459 data from SECT. Note, this function will generate a
6460 reference to NAME, so you shouldn't deallocate or
6461 overwrite it. */
6463 static bfd_boolean
6464 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
6466 asection *sect2;
6468 if (bfd_get_section_by_name (abfd, name) != NULL)
6469 return TRUE;
6471 sect2 = bfd_make_section (abfd, name);
6472 if (sect2 == NULL)
6473 return FALSE;
6475 sect2->size = sect->size;
6476 sect2->filepos = sect->filepos;
6477 sect2->flags = sect->flags;
6478 sect2->alignment_power = sect->alignment_power;
6479 return TRUE;
6482 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
6483 actually creates up to two pseudosections:
6484 - For the single-threaded case, a section named NAME, unless
6485 such a section already exists.
6486 - For the multi-threaded case, a section named "NAME/PID", where
6487 PID is elfcore_make_pid (abfd).
6488 Both pseudosections have identical contents. */
6489 bfd_boolean
6490 _bfd_elfcore_make_pseudosection (bfd *abfd,
6491 char *name,
6492 size_t size,
6493 ufile_ptr filepos)
6495 char buf[100];
6496 char *threaded_name;
6497 size_t len;
6498 asection *sect;
6500 /* Build the section name. */
6502 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
6503 len = strlen (buf) + 1;
6504 threaded_name = bfd_alloc (abfd, len);
6505 if (threaded_name == NULL)
6506 return FALSE;
6507 memcpy (threaded_name, buf, len);
6509 sect = bfd_make_section_anyway (abfd, threaded_name);
6510 if (sect == NULL)
6511 return FALSE;
6512 sect->size = size;
6513 sect->filepos = filepos;
6514 sect->flags = SEC_HAS_CONTENTS;
6515 sect->alignment_power = 2;
6517 return elfcore_maybe_make_sect (abfd, name, sect);
6520 /* prstatus_t exists on:
6521 solaris 2.5+
6522 linux 2.[01] + glibc
6523 unixware 4.2
6526 #if defined (HAVE_PRSTATUS_T)
6528 static bfd_boolean
6529 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
6531 size_t size;
6532 int offset;
6534 if (note->descsz == sizeof (prstatus_t))
6536 prstatus_t prstat;
6538 size = sizeof (prstat.pr_reg);
6539 offset = offsetof (prstatus_t, pr_reg);
6540 memcpy (&prstat, note->descdata, sizeof (prstat));
6542 /* Do not overwrite the core signal if it
6543 has already been set by another thread. */
6544 if (elf_tdata (abfd)->core_signal == 0)
6545 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
6546 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6548 /* pr_who exists on:
6549 solaris 2.5+
6550 unixware 4.2
6551 pr_who doesn't exist on:
6552 linux 2.[01]
6554 #if defined (HAVE_PRSTATUS_T_PR_WHO)
6555 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6556 #endif
6558 #if defined (HAVE_PRSTATUS32_T)
6559 else if (note->descsz == sizeof (prstatus32_t))
6561 /* 64-bit host, 32-bit corefile */
6562 prstatus32_t prstat;
6564 size = sizeof (prstat.pr_reg);
6565 offset = offsetof (prstatus32_t, pr_reg);
6566 memcpy (&prstat, note->descdata, sizeof (prstat));
6568 /* Do not overwrite the core signal if it
6569 has already been set by another thread. */
6570 if (elf_tdata (abfd)->core_signal == 0)
6571 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
6572 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6574 /* pr_who exists on:
6575 solaris 2.5+
6576 unixware 4.2
6577 pr_who doesn't exist on:
6578 linux 2.[01]
6580 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
6581 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6582 #endif
6584 #endif /* HAVE_PRSTATUS32_T */
6585 else
6587 /* Fail - we don't know how to handle any other
6588 note size (ie. data object type). */
6589 return TRUE;
6592 /* Make a ".reg/999" section and a ".reg" section. */
6593 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
6594 size, note->descpos + offset);
6596 #endif /* defined (HAVE_PRSTATUS_T) */
6598 /* Create a pseudosection containing the exact contents of NOTE. */
6599 static bfd_boolean
6600 elfcore_make_note_pseudosection (bfd *abfd,
6601 char *name,
6602 Elf_Internal_Note *note)
6604 return _bfd_elfcore_make_pseudosection (abfd, name,
6605 note->descsz, note->descpos);
6608 /* There isn't a consistent prfpregset_t across platforms,
6609 but it doesn't matter, because we don't have to pick this
6610 data structure apart. */
6612 static bfd_boolean
6613 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
6615 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6618 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6619 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6620 literally. */
6622 static bfd_boolean
6623 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
6625 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
6628 #if defined (HAVE_PRPSINFO_T)
6629 typedef prpsinfo_t elfcore_psinfo_t;
6630 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6631 typedef prpsinfo32_t elfcore_psinfo32_t;
6632 #endif
6633 #endif
6635 #if defined (HAVE_PSINFO_T)
6636 typedef psinfo_t elfcore_psinfo_t;
6637 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6638 typedef psinfo32_t elfcore_psinfo32_t;
6639 #endif
6640 #endif
6642 /* return a malloc'ed copy of a string at START which is at
6643 most MAX bytes long, possibly without a terminating '\0'.
6644 the copy will always have a terminating '\0'. */
6646 char *
6647 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
6649 char *dups;
6650 char *end = memchr (start, '\0', max);
6651 size_t len;
6653 if (end == NULL)
6654 len = max;
6655 else
6656 len = end - start;
6658 dups = bfd_alloc (abfd, len + 1);
6659 if (dups == NULL)
6660 return NULL;
6662 memcpy (dups, start, len);
6663 dups[len] = '\0';
6665 return dups;
6668 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6669 static bfd_boolean
6670 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
6672 if (note->descsz == sizeof (elfcore_psinfo_t))
6674 elfcore_psinfo_t psinfo;
6676 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6678 elf_tdata (abfd)->core_program
6679 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6680 sizeof (psinfo.pr_fname));
6682 elf_tdata (abfd)->core_command
6683 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6684 sizeof (psinfo.pr_psargs));
6686 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6687 else if (note->descsz == sizeof (elfcore_psinfo32_t))
6689 /* 64-bit host, 32-bit corefile */
6690 elfcore_psinfo32_t psinfo;
6692 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6694 elf_tdata (abfd)->core_program
6695 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6696 sizeof (psinfo.pr_fname));
6698 elf_tdata (abfd)->core_command
6699 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6700 sizeof (psinfo.pr_psargs));
6702 #endif
6704 else
6706 /* Fail - we don't know how to handle any other
6707 note size (ie. data object type). */
6708 return TRUE;
6711 /* Note that for some reason, a spurious space is tacked
6712 onto the end of the args in some (at least one anyway)
6713 implementations, so strip it off if it exists. */
6716 char *command = elf_tdata (abfd)->core_command;
6717 int n = strlen (command);
6719 if (0 < n && command[n - 1] == ' ')
6720 command[n - 1] = '\0';
6723 return TRUE;
6725 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6727 #if defined (HAVE_PSTATUS_T)
6728 static bfd_boolean
6729 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
6731 if (note->descsz == sizeof (pstatus_t)
6732 #if defined (HAVE_PXSTATUS_T)
6733 || note->descsz == sizeof (pxstatus_t)
6734 #endif
6737 pstatus_t pstat;
6739 memcpy (&pstat, note->descdata, sizeof (pstat));
6741 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6743 #if defined (HAVE_PSTATUS32_T)
6744 else if (note->descsz == sizeof (pstatus32_t))
6746 /* 64-bit host, 32-bit corefile */
6747 pstatus32_t pstat;
6749 memcpy (&pstat, note->descdata, sizeof (pstat));
6751 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6753 #endif
6754 /* Could grab some more details from the "representative"
6755 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6756 NT_LWPSTATUS note, presumably. */
6758 return TRUE;
6760 #endif /* defined (HAVE_PSTATUS_T) */
6762 #if defined (HAVE_LWPSTATUS_T)
6763 static bfd_boolean
6764 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
6766 lwpstatus_t lwpstat;
6767 char buf[100];
6768 char *name;
6769 size_t len;
6770 asection *sect;
6772 if (note->descsz != sizeof (lwpstat)
6773 #if defined (HAVE_LWPXSTATUS_T)
6774 && note->descsz != sizeof (lwpxstatus_t)
6775 #endif
6777 return TRUE;
6779 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
6781 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
6782 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
6784 /* Make a ".reg/999" section. */
6786 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
6787 len = strlen (buf) + 1;
6788 name = bfd_alloc (abfd, len);
6789 if (name == NULL)
6790 return FALSE;
6791 memcpy (name, buf, len);
6793 sect = bfd_make_section_anyway (abfd, name);
6794 if (sect == NULL)
6795 return FALSE;
6797 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6798 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
6799 sect->filepos = note->descpos
6800 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
6801 #endif
6803 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6804 sect->size = sizeof (lwpstat.pr_reg);
6805 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
6806 #endif
6808 sect->flags = SEC_HAS_CONTENTS;
6809 sect->alignment_power = 2;
6811 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
6812 return FALSE;
6814 /* Make a ".reg2/999" section */
6816 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
6817 len = strlen (buf) + 1;
6818 name = bfd_alloc (abfd, len);
6819 if (name == NULL)
6820 return FALSE;
6821 memcpy (name, buf, len);
6823 sect = bfd_make_section_anyway (abfd, name);
6824 if (sect == NULL)
6825 return FALSE;
6827 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6828 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
6829 sect->filepos = note->descpos
6830 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
6831 #endif
6833 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6834 sect->size = sizeof (lwpstat.pr_fpreg);
6835 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
6836 #endif
6838 sect->flags = SEC_HAS_CONTENTS;
6839 sect->alignment_power = 2;
6841 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
6843 #endif /* defined (HAVE_LWPSTATUS_T) */
6845 #if defined (HAVE_WIN32_PSTATUS_T)
6846 static bfd_boolean
6847 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
6849 char buf[30];
6850 char *name;
6851 size_t len;
6852 asection *sect;
6853 win32_pstatus_t pstatus;
6855 if (note->descsz < sizeof (pstatus))
6856 return TRUE;
6858 memcpy (&pstatus, note->descdata, sizeof (pstatus));
6860 switch (pstatus.data_type)
6862 case NOTE_INFO_PROCESS:
6863 /* FIXME: need to add ->core_command. */
6864 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
6865 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
6866 break;
6868 case NOTE_INFO_THREAD:
6869 /* Make a ".reg/999" section. */
6870 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
6872 len = strlen (buf) + 1;
6873 name = bfd_alloc (abfd, len);
6874 if (name == NULL)
6875 return FALSE;
6877 memcpy (name, buf, len);
6879 sect = bfd_make_section_anyway (abfd, name);
6880 if (sect == NULL)
6881 return FALSE;
6883 sect->size = sizeof (pstatus.data.thread_info.thread_context);
6884 sect->filepos = (note->descpos
6885 + offsetof (struct win32_pstatus,
6886 data.thread_info.thread_context));
6887 sect->flags = SEC_HAS_CONTENTS;
6888 sect->alignment_power = 2;
6890 if (pstatus.data.thread_info.is_active_thread)
6891 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
6892 return FALSE;
6893 break;
6895 case NOTE_INFO_MODULE:
6896 /* Make a ".module/xxxxxxxx" section. */
6897 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
6899 len = strlen (buf) + 1;
6900 name = bfd_alloc (abfd, len);
6901 if (name == NULL)
6902 return FALSE;
6904 memcpy (name, buf, len);
6906 sect = bfd_make_section_anyway (abfd, name);
6908 if (sect == NULL)
6909 return FALSE;
6911 sect->size = note->descsz;
6912 sect->filepos = note->descpos;
6913 sect->flags = SEC_HAS_CONTENTS;
6914 sect->alignment_power = 2;
6915 break;
6917 default:
6918 return TRUE;
6921 return TRUE;
6923 #endif /* HAVE_WIN32_PSTATUS_T */
6925 static bfd_boolean
6926 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
6928 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6930 switch (note->type)
6932 default:
6933 return TRUE;
6935 case NT_PRSTATUS:
6936 if (bed->elf_backend_grok_prstatus)
6937 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
6938 return TRUE;
6939 #if defined (HAVE_PRSTATUS_T)
6940 return elfcore_grok_prstatus (abfd, note);
6941 #else
6942 return TRUE;
6943 #endif
6945 #if defined (HAVE_PSTATUS_T)
6946 case NT_PSTATUS:
6947 return elfcore_grok_pstatus (abfd, note);
6948 #endif
6950 #if defined (HAVE_LWPSTATUS_T)
6951 case NT_LWPSTATUS:
6952 return elfcore_grok_lwpstatus (abfd, note);
6953 #endif
6955 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
6956 return elfcore_grok_prfpreg (abfd, note);
6958 #if defined (HAVE_WIN32_PSTATUS_T)
6959 case NT_WIN32PSTATUS:
6960 return elfcore_grok_win32pstatus (abfd, note);
6961 #endif
6963 case NT_PRXFPREG: /* Linux SSE extension */
6964 if (note->namesz == 6
6965 && strcmp (note->namedata, "LINUX") == 0)
6966 return elfcore_grok_prxfpreg (abfd, note);
6967 else
6968 return TRUE;
6970 case NT_PRPSINFO:
6971 case NT_PSINFO:
6972 if (bed->elf_backend_grok_psinfo)
6973 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
6974 return TRUE;
6975 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6976 return elfcore_grok_psinfo (abfd, note);
6977 #else
6978 return TRUE;
6979 #endif
6981 case NT_AUXV:
6983 asection *sect = bfd_make_section_anyway (abfd, ".auxv");
6985 if (sect == NULL)
6986 return FALSE;
6987 sect->size = note->descsz;
6988 sect->filepos = note->descpos;
6989 sect->flags = SEC_HAS_CONTENTS;
6990 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
6992 return TRUE;
6997 static bfd_boolean
6998 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
7000 char *cp;
7002 cp = strchr (note->namedata, '@');
7003 if (cp != NULL)
7005 *lwpidp = atoi(cp + 1);
7006 return TRUE;
7008 return FALSE;
7011 static bfd_boolean
7012 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
7015 /* Signal number at offset 0x08. */
7016 elf_tdata (abfd)->core_signal
7017 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
7019 /* Process ID at offset 0x50. */
7020 elf_tdata (abfd)->core_pid
7021 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
7023 /* Command name at 0x7c (max 32 bytes, including nul). */
7024 elf_tdata (abfd)->core_command
7025 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
7027 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
7028 note);
7031 static bfd_boolean
7032 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
7034 int lwp;
7036 if (elfcore_netbsd_get_lwpid (note, &lwp))
7037 elf_tdata (abfd)->core_lwpid = lwp;
7039 if (note->type == NT_NETBSDCORE_PROCINFO)
7041 /* NetBSD-specific core "procinfo". Note that we expect to
7042 find this note before any of the others, which is fine,
7043 since the kernel writes this note out first when it
7044 creates a core file. */
7046 return elfcore_grok_netbsd_procinfo (abfd, note);
7049 /* As of Jan 2002 there are no other machine-independent notes
7050 defined for NetBSD core files. If the note type is less
7051 than the start of the machine-dependent note types, we don't
7052 understand it. */
7054 if (note->type < NT_NETBSDCORE_FIRSTMACH)
7055 return TRUE;
7058 switch (bfd_get_arch (abfd))
7060 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
7061 PT_GETFPREGS == mach+2. */
7063 case bfd_arch_alpha:
7064 case bfd_arch_sparc:
7065 switch (note->type)
7067 case NT_NETBSDCORE_FIRSTMACH+0:
7068 return elfcore_make_note_pseudosection (abfd, ".reg", note);
7070 case NT_NETBSDCORE_FIRSTMACH+2:
7071 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7073 default:
7074 return TRUE;
7077 /* On all other arch's, PT_GETREGS == mach+1 and
7078 PT_GETFPREGS == mach+3. */
7080 default:
7081 switch (note->type)
7083 case NT_NETBSDCORE_FIRSTMACH+1:
7084 return elfcore_make_note_pseudosection (abfd, ".reg", note);
7086 case NT_NETBSDCORE_FIRSTMACH+3:
7087 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7089 default:
7090 return TRUE;
7093 /* NOTREACHED */
7096 static bfd_boolean
7097 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, pid_t *tid)
7099 void *ddata = note->descdata;
7100 char buf[100];
7101 char *name;
7102 asection *sect;
7103 short sig;
7104 unsigned flags;
7106 /* nto_procfs_status 'pid' field is at offset 0. */
7107 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
7109 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
7110 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
7112 /* nto_procfs_status 'flags' field is at offset 8. */
7113 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
7115 /* nto_procfs_status 'what' field is at offset 14. */
7116 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
7118 elf_tdata (abfd)->core_signal = sig;
7119 elf_tdata (abfd)->core_lwpid = *tid;
7122 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
7123 do not come from signals so we make sure we set the current
7124 thread just in case. */
7125 if (flags & 0x00000080)
7126 elf_tdata (abfd)->core_lwpid = *tid;
7128 /* Make a ".qnx_core_status/%d" section. */
7129 sprintf (buf, ".qnx_core_status/%d", *tid);
7131 name = bfd_alloc (abfd, strlen (buf) + 1);
7132 if (name == NULL)
7133 return FALSE;
7134 strcpy (name, buf);
7136 sect = bfd_make_section_anyway (abfd, name);
7137 if (sect == NULL)
7138 return FALSE;
7140 sect->size = note->descsz;
7141 sect->filepos = note->descpos;
7142 sect->flags = SEC_HAS_CONTENTS;
7143 sect->alignment_power = 2;
7145 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
7148 static bfd_boolean
7149 elfcore_grok_nto_gregs (bfd *abfd, Elf_Internal_Note *note, pid_t tid)
7151 char buf[100];
7152 char *name;
7153 asection *sect;
7155 /* Make a ".reg/%d" section. */
7156 sprintf (buf, ".reg/%d", tid);
7158 name = bfd_alloc (abfd, strlen (buf) + 1);
7159 if (name == NULL)
7160 return FALSE;
7161 strcpy (name, buf);
7163 sect = bfd_make_section_anyway (abfd, name);
7164 if (sect == NULL)
7165 return FALSE;
7167 sect->size = note->descsz;
7168 sect->filepos = note->descpos;
7169 sect->flags = SEC_HAS_CONTENTS;
7170 sect->alignment_power = 2;
7172 /* This is the current thread. */
7173 if (elf_tdata (abfd)->core_lwpid == tid)
7174 return elfcore_maybe_make_sect (abfd, ".reg", sect);
7176 return TRUE;
7179 #define BFD_QNT_CORE_INFO 7
7180 #define BFD_QNT_CORE_STATUS 8
7181 #define BFD_QNT_CORE_GREG 9
7182 #define BFD_QNT_CORE_FPREG 10
7184 static bfd_boolean
7185 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
7187 /* Every GREG section has a STATUS section before it. Store the
7188 tid from the previous call to pass down to the next gregs
7189 function. */
7190 static pid_t tid = 1;
7192 switch (note->type)
7194 case BFD_QNT_CORE_INFO: return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
7195 case BFD_QNT_CORE_STATUS: return elfcore_grok_nto_status (abfd, note, &tid);
7196 case BFD_QNT_CORE_GREG: return elfcore_grok_nto_gregs (abfd, note, tid);
7197 case BFD_QNT_CORE_FPREG: return elfcore_grok_prfpreg (abfd, note);
7198 default: return TRUE;
7202 /* Function: elfcore_write_note
7204 Inputs:
7205 buffer to hold note
7206 name of note
7207 type of note
7208 data for note
7209 size of data for note
7211 Return:
7212 End of buffer containing note. */
7214 char *
7215 elfcore_write_note (bfd *abfd,
7216 char *buf,
7217 int *bufsiz,
7218 const char *name,
7219 int type,
7220 const void *input,
7221 int size)
7223 Elf_External_Note *xnp;
7224 size_t namesz;
7225 size_t pad;
7226 size_t newspace;
7227 char *p, *dest;
7229 namesz = 0;
7230 pad = 0;
7231 if (name != NULL)
7233 const struct elf_backend_data *bed;
7235 namesz = strlen (name) + 1;
7236 bed = get_elf_backend_data (abfd);
7237 pad = -namesz & ((1 << bed->s->log_file_align) - 1);
7240 newspace = 12 + namesz + pad + size;
7242 p = realloc (buf, *bufsiz + newspace);
7243 dest = p + *bufsiz;
7244 *bufsiz += newspace;
7245 xnp = (Elf_External_Note *) dest;
7246 H_PUT_32 (abfd, namesz, xnp->namesz);
7247 H_PUT_32 (abfd, size, xnp->descsz);
7248 H_PUT_32 (abfd, type, xnp->type);
7249 dest = xnp->name;
7250 if (name != NULL)
7252 memcpy (dest, name, namesz);
7253 dest += namesz;
7254 while (pad != 0)
7256 *dest++ = '\0';
7257 --pad;
7260 memcpy (dest, input, size);
7261 return p;
7264 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7265 char *
7266 elfcore_write_prpsinfo (bfd *abfd,
7267 char *buf,
7268 int *bufsiz,
7269 const char *fname,
7270 const char *psargs)
7272 int note_type;
7273 char *note_name = "CORE";
7275 #if defined (HAVE_PSINFO_T)
7276 psinfo_t data;
7277 note_type = NT_PSINFO;
7278 #else
7279 prpsinfo_t data;
7280 note_type = NT_PRPSINFO;
7281 #endif
7283 memset (&data, 0, sizeof (data));
7284 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
7285 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
7286 return elfcore_write_note (abfd, buf, bufsiz,
7287 note_name, note_type, &data, sizeof (data));
7289 #endif /* PSINFO_T or PRPSINFO_T */
7291 #if defined (HAVE_PRSTATUS_T)
7292 char *
7293 elfcore_write_prstatus (bfd *abfd,
7294 char *buf,
7295 int *bufsiz,
7296 long pid,
7297 int cursig,
7298 const void *gregs)
7300 prstatus_t prstat;
7301 char *note_name = "CORE";
7303 memset (&prstat, 0, sizeof (prstat));
7304 prstat.pr_pid = pid;
7305 prstat.pr_cursig = cursig;
7306 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
7307 return elfcore_write_note (abfd, buf, bufsiz,
7308 note_name, NT_PRSTATUS, &prstat, sizeof (prstat));
7310 #endif /* HAVE_PRSTATUS_T */
7312 #if defined (HAVE_LWPSTATUS_T)
7313 char *
7314 elfcore_write_lwpstatus (bfd *abfd,
7315 char *buf,
7316 int *bufsiz,
7317 long pid,
7318 int cursig,
7319 const void *gregs)
7321 lwpstatus_t lwpstat;
7322 char *note_name = "CORE";
7324 memset (&lwpstat, 0, sizeof (lwpstat));
7325 lwpstat.pr_lwpid = pid >> 16;
7326 lwpstat.pr_cursig = cursig;
7327 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7328 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
7329 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7330 #if !defined(gregs)
7331 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
7332 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
7333 #else
7334 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
7335 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
7336 #endif
7337 #endif
7338 return elfcore_write_note (abfd, buf, bufsiz, note_name,
7339 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
7341 #endif /* HAVE_LWPSTATUS_T */
7343 #if defined (HAVE_PSTATUS_T)
7344 char *
7345 elfcore_write_pstatus (bfd *abfd,
7346 char *buf,
7347 int *bufsiz,
7348 long pid,
7349 int cursig,
7350 const void *gregs)
7352 pstatus_t pstat;
7353 char *note_name = "CORE";
7355 memset (&pstat, 0, sizeof (pstat));
7356 pstat.pr_pid = pid & 0xffff;
7357 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
7358 NT_PSTATUS, &pstat, sizeof (pstat));
7359 return buf;
7361 #endif /* HAVE_PSTATUS_T */
7363 char *
7364 elfcore_write_prfpreg (bfd *abfd,
7365 char *buf,
7366 int *bufsiz,
7367 const void *fpregs,
7368 int size)
7370 char *note_name = "CORE";
7371 return elfcore_write_note (abfd, buf, bufsiz,
7372 note_name, NT_FPREGSET, fpregs, size);
7375 char *
7376 elfcore_write_prxfpreg (bfd *abfd,
7377 char *buf,
7378 int *bufsiz,
7379 const void *xfpregs,
7380 int size)
7382 char *note_name = "LINUX";
7383 return elfcore_write_note (abfd, buf, bufsiz,
7384 note_name, NT_PRXFPREG, xfpregs, size);
7387 static bfd_boolean
7388 elfcore_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
7390 char *buf;
7391 char *p;
7393 if (size <= 0)
7394 return TRUE;
7396 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
7397 return FALSE;
7399 buf = bfd_malloc (size);
7400 if (buf == NULL)
7401 return FALSE;
7403 if (bfd_bread (buf, size, abfd) != size)
7405 error:
7406 free (buf);
7407 return FALSE;
7410 p = buf;
7411 while (p < buf + size)
7413 /* FIXME: bad alignment assumption. */
7414 Elf_External_Note *xnp = (Elf_External_Note *) p;
7415 Elf_Internal_Note in;
7417 in.type = H_GET_32 (abfd, xnp->type);
7419 in.namesz = H_GET_32 (abfd, xnp->namesz);
7420 in.namedata = xnp->name;
7422 in.descsz = H_GET_32 (abfd, xnp->descsz);
7423 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
7424 in.descpos = offset + (in.descdata - buf);
7426 if (strncmp (in.namedata, "NetBSD-CORE", 11) == 0)
7428 if (! elfcore_grok_netbsd_note (abfd, &in))
7429 goto error;
7431 else if (strncmp (in.namedata, "QNX", 3) == 0)
7433 if (! elfcore_grok_nto_note (abfd, &in))
7434 goto error;
7436 else
7438 if (! elfcore_grok_note (abfd, &in))
7439 goto error;
7442 p = in.descdata + BFD_ALIGN (in.descsz, 4);
7445 free (buf);
7446 return TRUE;
7449 /* Providing external access to the ELF program header table. */
7451 /* Return an upper bound on the number of bytes required to store a
7452 copy of ABFD's program header table entries. Return -1 if an error
7453 occurs; bfd_get_error will return an appropriate code. */
7455 long
7456 bfd_get_elf_phdr_upper_bound (bfd *abfd)
7458 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7460 bfd_set_error (bfd_error_wrong_format);
7461 return -1;
7464 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
7467 /* Copy ABFD's program header table entries to *PHDRS. The entries
7468 will be stored as an array of Elf_Internal_Phdr structures, as
7469 defined in include/elf/internal.h. To find out how large the
7470 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
7472 Return the number of program header table entries read, or -1 if an
7473 error occurs; bfd_get_error will return an appropriate code. */
7476 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
7478 int num_phdrs;
7480 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7482 bfd_set_error (bfd_error_wrong_format);
7483 return -1;
7486 num_phdrs = elf_elfheader (abfd)->e_phnum;
7487 memcpy (phdrs, elf_tdata (abfd)->phdr,
7488 num_phdrs * sizeof (Elf_Internal_Phdr));
7490 return num_phdrs;
7493 void
7494 _bfd_elf_sprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, char *buf, bfd_vma value)
7496 #ifdef BFD64
7497 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7499 i_ehdrp = elf_elfheader (abfd);
7500 if (i_ehdrp == NULL)
7501 sprintf_vma (buf, value);
7502 else
7504 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
7506 #if BFD_HOST_64BIT_LONG
7507 sprintf (buf, "%016lx", value);
7508 #else
7509 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
7510 _bfd_int64_low (value));
7511 #endif
7513 else
7514 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
7516 #else
7517 sprintf_vma (buf, value);
7518 #endif
7521 void
7522 _bfd_elf_fprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, void *stream, bfd_vma value)
7524 #ifdef BFD64
7525 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7527 i_ehdrp = elf_elfheader (abfd);
7528 if (i_ehdrp == NULL)
7529 fprintf_vma ((FILE *) stream, value);
7530 else
7532 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
7534 #if BFD_HOST_64BIT_LONG
7535 fprintf ((FILE *) stream, "%016lx", value);
7536 #else
7537 fprintf ((FILE *) stream, "%08lx%08lx",
7538 _bfd_int64_high (value), _bfd_int64_low (value));
7539 #endif
7541 else
7542 fprintf ((FILE *) stream, "%08lx",
7543 (unsigned long) (value & 0xffffffff));
7545 #else
7546 fprintf_vma ((FILE *) stream, value);
7547 #endif
7550 enum elf_reloc_type_class
7551 _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
7553 return reloc_class_normal;
7556 /* For RELA architectures, return the relocation value for a
7557 relocation against a local symbol. */
7559 bfd_vma
7560 _bfd_elf_rela_local_sym (bfd *abfd,
7561 Elf_Internal_Sym *sym,
7562 asection **psec,
7563 Elf_Internal_Rela *rel)
7565 asection *sec = *psec;
7566 bfd_vma relocation;
7568 relocation = (sec->output_section->vma
7569 + sec->output_offset
7570 + sym->st_value);
7571 if ((sec->flags & SEC_MERGE)
7572 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
7573 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
7575 rel->r_addend =
7576 _bfd_merged_section_offset (abfd, psec,
7577 elf_section_data (sec)->sec_info,
7578 sym->st_value + rel->r_addend);
7579 if (sec != *psec)
7581 /* If we have changed the section, and our original section is
7582 marked with SEC_EXCLUDE, it means that the original
7583 SEC_MERGE section has been completely subsumed in some
7584 other SEC_MERGE section. In this case, we need to leave
7585 some info around for --emit-relocs. */
7586 if ((sec->flags & SEC_EXCLUDE) != 0)
7587 sec->kept_section = *psec;
7588 sec = *psec;
7590 rel->r_addend -= relocation;
7591 rel->r_addend += sec->output_section->vma + sec->output_offset;
7593 return relocation;
7596 bfd_vma
7597 _bfd_elf_rel_local_sym (bfd *abfd,
7598 Elf_Internal_Sym *sym,
7599 asection **psec,
7600 bfd_vma addend)
7602 asection *sec = *psec;
7604 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
7605 return sym->st_value + addend;
7607 return _bfd_merged_section_offset (abfd, psec,
7608 elf_section_data (sec)->sec_info,
7609 sym->st_value + addend);
7612 bfd_vma
7613 _bfd_elf_section_offset (bfd *abfd,
7614 struct bfd_link_info *info ATTRIBUTE_UNUSED,
7615 asection *sec,
7616 bfd_vma offset)
7618 switch (sec->sec_info_type)
7620 case ELF_INFO_TYPE_STABS:
7621 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
7622 offset);
7623 case ELF_INFO_TYPE_EH_FRAME:
7624 return _bfd_elf_eh_frame_section_offset (abfd, sec, offset);
7625 default:
7626 return offset;
7630 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
7631 reconstruct an ELF file by reading the segments out of remote memory
7632 based on the ELF file header at EHDR_VMA and the ELF program headers it
7633 points to. If not null, *LOADBASEP is filled in with the difference
7634 between the VMAs from which the segments were read, and the VMAs the
7635 file headers (and hence BFD's idea of each section's VMA) put them at.
7637 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
7638 remote memory at target address VMA into the local buffer at MYADDR; it
7639 should return zero on success or an `errno' code on failure. TEMPL must
7640 be a BFD for an ELF target with the word size and byte order found in
7641 the remote memory. */
7643 bfd *
7644 bfd_elf_bfd_from_remote_memory
7645 (bfd *templ,
7646 bfd_vma ehdr_vma,
7647 bfd_vma *loadbasep,
7648 int (*target_read_memory) (bfd_vma, char *, int))
7650 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
7651 (templ, ehdr_vma, loadbasep, target_read_memory);
7654 long
7655 _bfd_elf_get_synthetic_symtab (bfd *abfd, asymbol **dynsyms, asymbol **ret)
7657 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7658 asection *relplt;
7659 asymbol *s;
7660 const char *relplt_name;
7661 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
7662 arelent *p;
7663 long count, i, n;
7664 size_t size;
7665 Elf_Internal_Shdr *hdr;
7666 char *names;
7667 asection *plt;
7669 *ret = NULL;
7670 if (!bed->plt_sym_val)
7671 return 0;
7673 relplt_name = bed->relplt_name;
7674 if (relplt_name == NULL)
7675 relplt_name = bed->default_use_rela_p ? ".rela.plt" : ".rel.plt";
7676 relplt = bfd_get_section_by_name (abfd, relplt_name);
7677 if (relplt == NULL)
7678 return 0;
7680 hdr = &elf_section_data (relplt)->this_hdr;
7681 if (hdr->sh_link != elf_dynsymtab (abfd)
7682 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
7683 return 0;
7685 plt = bfd_get_section_by_name (abfd, ".plt");
7686 if (plt == NULL)
7687 return 0;
7689 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
7690 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
7691 return -1;
7693 count = relplt->size / hdr->sh_entsize;
7694 size = count * sizeof (asymbol);
7695 p = relplt->relocation;
7696 for (i = 0; i < count; i++, s++, p++)
7697 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
7699 s = *ret = bfd_malloc (size);
7700 if (s == NULL)
7701 return -1;
7703 names = (char *) (s + count);
7704 p = relplt->relocation;
7705 n = 0;
7706 for (i = 0; i < count; i++, s++, p++)
7708 size_t len;
7709 bfd_vma addr;
7711 addr = bed->plt_sym_val (i, plt, p);
7712 if (addr == (bfd_vma) -1)
7713 continue;
7715 *s = **p->sym_ptr_ptr;
7716 s->section = plt;
7717 s->value = addr - plt->vma;
7718 s->name = names;
7719 len = strlen ((*p->sym_ptr_ptr)->name);
7720 memcpy (names, (*p->sym_ptr_ptr)->name, len);
7721 names += len;
7722 memcpy (names, "@plt", sizeof ("@plt"));
7723 names += sizeof ("@plt");
7724 ++n;
7727 return n;