* bfd-in.h (struct stab_info): Move from stabs.c.
[binutils.git] / bfd / elf.c
blob1525fa55d62ed69ab0e2501e8870adaf51b8d03a
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;
1517 void
1518 bfd_elf_set_dyn_lib_class (bfd *abfd, int lib_class)
1520 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1521 && bfd_get_format (abfd) == bfd_object)
1522 elf_dyn_lib_class (abfd) = lib_class;
1525 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1526 the linker ELF emulation code. */
1528 struct bfd_link_needed_list *
1529 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
1530 struct bfd_link_info *info)
1532 if (! is_elf_hash_table (info->hash))
1533 return NULL;
1534 return elf_hash_table (info)->needed;
1537 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1538 hook for the linker ELF emulation code. */
1540 struct bfd_link_needed_list *
1541 bfd_elf_get_runpath_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)->runpath;
1549 /* Get the name actually used for a dynamic object for a link. This
1550 is the SONAME entry if there is one. Otherwise, it is the string
1551 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1553 const char *
1554 bfd_elf_get_dt_soname (bfd *abfd)
1556 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1557 && bfd_get_format (abfd) == bfd_object)
1558 return elf_dt_name (abfd);
1559 return NULL;
1562 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1563 the ELF linker emulation code. */
1565 bfd_boolean
1566 bfd_elf_get_bfd_needed_list (bfd *abfd,
1567 struct bfd_link_needed_list **pneeded)
1569 asection *s;
1570 bfd_byte *dynbuf = NULL;
1571 int elfsec;
1572 unsigned long shlink;
1573 bfd_byte *extdyn, *extdynend;
1574 size_t extdynsize;
1575 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1577 *pneeded = NULL;
1579 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1580 || bfd_get_format (abfd) != bfd_object)
1581 return TRUE;
1583 s = bfd_get_section_by_name (abfd, ".dynamic");
1584 if (s == NULL || s->size == 0)
1585 return TRUE;
1587 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1588 goto error_return;
1590 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1591 if (elfsec == -1)
1592 goto error_return;
1594 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1596 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1597 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1599 extdyn = dynbuf;
1600 extdynend = extdyn + s->size;
1601 for (; extdyn < extdynend; extdyn += extdynsize)
1603 Elf_Internal_Dyn dyn;
1605 (*swap_dyn_in) (abfd, extdyn, &dyn);
1607 if (dyn.d_tag == DT_NULL)
1608 break;
1610 if (dyn.d_tag == DT_NEEDED)
1612 const char *string;
1613 struct bfd_link_needed_list *l;
1614 unsigned int tagv = dyn.d_un.d_val;
1615 bfd_size_type amt;
1617 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1618 if (string == NULL)
1619 goto error_return;
1621 amt = sizeof *l;
1622 l = bfd_alloc (abfd, amt);
1623 if (l == NULL)
1624 goto error_return;
1626 l->by = abfd;
1627 l->name = string;
1628 l->next = *pneeded;
1629 *pneeded = l;
1633 free (dynbuf);
1635 return TRUE;
1637 error_return:
1638 if (dynbuf != NULL)
1639 free (dynbuf);
1640 return FALSE;
1643 /* Allocate an ELF string table--force the first byte to be zero. */
1645 struct bfd_strtab_hash *
1646 _bfd_elf_stringtab_init (void)
1648 struct bfd_strtab_hash *ret;
1650 ret = _bfd_stringtab_init ();
1651 if (ret != NULL)
1653 bfd_size_type loc;
1655 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1656 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1657 if (loc == (bfd_size_type) -1)
1659 _bfd_stringtab_free (ret);
1660 ret = NULL;
1663 return ret;
1666 /* ELF .o/exec file reading */
1668 /* Create a new bfd section from an ELF section header. */
1670 bfd_boolean
1671 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1673 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1674 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1675 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1676 const char *name;
1678 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1680 switch (hdr->sh_type)
1682 case SHT_NULL:
1683 /* Inactive section. Throw it away. */
1684 return TRUE;
1686 case SHT_PROGBITS: /* Normal section with contents. */
1687 case SHT_NOBITS: /* .bss section. */
1688 case SHT_HASH: /* .hash section. */
1689 case SHT_NOTE: /* .note section. */
1690 case SHT_INIT_ARRAY: /* .init_array section. */
1691 case SHT_FINI_ARRAY: /* .fini_array section. */
1692 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1693 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1695 case SHT_DYNAMIC: /* Dynamic linking information. */
1696 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1697 return FALSE;
1698 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1700 Elf_Internal_Shdr *dynsymhdr;
1702 /* The shared libraries distributed with hpux11 have a bogus
1703 sh_link field for the ".dynamic" section. Find the
1704 string table for the ".dynsym" section instead. */
1705 if (elf_dynsymtab (abfd) != 0)
1707 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1708 hdr->sh_link = dynsymhdr->sh_link;
1710 else
1712 unsigned int i, num_sec;
1714 num_sec = elf_numsections (abfd);
1715 for (i = 1; i < num_sec; i++)
1717 dynsymhdr = elf_elfsections (abfd)[i];
1718 if (dynsymhdr->sh_type == SHT_DYNSYM)
1720 hdr->sh_link = dynsymhdr->sh_link;
1721 break;
1726 break;
1728 case SHT_SYMTAB: /* A symbol table */
1729 if (elf_onesymtab (abfd) == shindex)
1730 return TRUE;
1732 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1733 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1734 elf_onesymtab (abfd) = shindex;
1735 elf_tdata (abfd)->symtab_hdr = *hdr;
1736 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1737 abfd->flags |= HAS_SYMS;
1739 /* Sometimes a shared object will map in the symbol table. If
1740 SHF_ALLOC is set, and this is a shared object, then we also
1741 treat this section as a BFD section. We can not base the
1742 decision purely on SHF_ALLOC, because that flag is sometimes
1743 set in a relocatable object file, which would confuse the
1744 linker. */
1745 if ((hdr->sh_flags & SHF_ALLOC) != 0
1746 && (abfd->flags & DYNAMIC) != 0
1747 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1748 return FALSE;
1750 return TRUE;
1752 case SHT_DYNSYM: /* A dynamic symbol table */
1753 if (elf_dynsymtab (abfd) == shindex)
1754 return TRUE;
1756 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1757 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1758 elf_dynsymtab (abfd) = shindex;
1759 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1760 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1761 abfd->flags |= HAS_SYMS;
1763 /* Besides being a symbol table, we also treat this as a regular
1764 section, so that objcopy can handle it. */
1765 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1767 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1768 if (elf_symtab_shndx (abfd) == shindex)
1769 return TRUE;
1771 /* Get the associated symbol table. */
1772 if (! bfd_section_from_shdr (abfd, hdr->sh_link)
1773 || hdr->sh_link != elf_onesymtab (abfd))
1774 return FALSE;
1776 elf_symtab_shndx (abfd) = shindex;
1777 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1778 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1779 return TRUE;
1781 case SHT_STRTAB: /* A string table */
1782 if (hdr->bfd_section != NULL)
1783 return TRUE;
1784 if (ehdr->e_shstrndx == shindex)
1786 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1787 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1788 return TRUE;
1791 unsigned int i, num_sec;
1793 num_sec = elf_numsections (abfd);
1794 for (i = 1; i < num_sec; i++)
1796 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1797 if (hdr2->sh_link == shindex)
1799 if (! bfd_section_from_shdr (abfd, i))
1800 return FALSE;
1801 if (elf_onesymtab (abfd) == i)
1803 elf_tdata (abfd)->strtab_hdr = *hdr;
1804 elf_elfsections (abfd)[shindex] =
1805 &elf_tdata (abfd)->strtab_hdr;
1806 return TRUE;
1808 if (elf_dynsymtab (abfd) == i)
1810 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1811 elf_elfsections (abfd)[shindex] = hdr =
1812 &elf_tdata (abfd)->dynstrtab_hdr;
1813 /* We also treat this as a regular section, so
1814 that objcopy can handle it. */
1815 break;
1817 #if 0 /* Not handling other string tables specially right now. */
1818 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1819 /* We have a strtab for some random other section. */
1820 newsect = (asection *) hdr2->bfd_section;
1821 if (!newsect)
1822 break;
1823 hdr->bfd_section = newsect;
1824 hdr2 = &elf_section_data (newsect)->str_hdr;
1825 *hdr2 = *hdr;
1826 elf_elfsections (abfd)[shindex] = hdr2;
1827 #endif
1832 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1834 case SHT_REL:
1835 case SHT_RELA:
1836 /* *These* do a lot of work -- but build no sections! */
1838 asection *target_sect;
1839 Elf_Internal_Shdr *hdr2;
1840 unsigned int num_sec = elf_numsections (abfd);
1842 /* Check for a bogus link to avoid crashing. */
1843 if ((hdr->sh_link >= SHN_LORESERVE && hdr->sh_link <= SHN_HIRESERVE)
1844 || hdr->sh_link >= num_sec)
1846 ((*_bfd_error_handler)
1847 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1848 bfd_archive_filename (abfd), hdr->sh_link, name, shindex));
1849 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1852 /* For some incomprehensible reason Oracle distributes
1853 libraries for Solaris in which some of the objects have
1854 bogus sh_link fields. It would be nice if we could just
1855 reject them, but, unfortunately, some people need to use
1856 them. We scan through the section headers; if we find only
1857 one suitable symbol table, we clobber the sh_link to point
1858 to it. I hope this doesn't break anything. */
1859 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1860 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1862 unsigned int scan;
1863 int found;
1865 found = 0;
1866 for (scan = 1; scan < num_sec; scan++)
1868 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1869 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1871 if (found != 0)
1873 found = 0;
1874 break;
1876 found = scan;
1879 if (found != 0)
1880 hdr->sh_link = found;
1883 /* Get the symbol table. */
1884 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1885 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1886 return FALSE;
1888 /* If this reloc section does not use the main symbol table we
1889 don't treat it as a reloc section. BFD can't adequately
1890 represent such a section, so at least for now, we don't
1891 try. We just present it as a normal section. We also
1892 can't use it as a reloc section if it points to the null
1893 section. */
1894 if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
1895 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1897 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1898 return FALSE;
1899 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1900 if (target_sect == NULL)
1901 return FALSE;
1903 if ((target_sect->flags & SEC_RELOC) == 0
1904 || target_sect->reloc_count == 0)
1905 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1906 else
1908 bfd_size_type amt;
1909 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1910 amt = sizeof (*hdr2);
1911 hdr2 = bfd_alloc (abfd, amt);
1912 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1914 *hdr2 = *hdr;
1915 elf_elfsections (abfd)[shindex] = hdr2;
1916 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1917 target_sect->flags |= SEC_RELOC;
1918 target_sect->relocation = NULL;
1919 target_sect->rel_filepos = hdr->sh_offset;
1920 /* In the section to which the relocations apply, mark whether
1921 its relocations are of the REL or RELA variety. */
1922 if (hdr->sh_size != 0)
1923 target_sect->use_rela_p = hdr->sh_type == SHT_RELA;
1924 abfd->flags |= HAS_RELOC;
1925 return TRUE;
1927 break;
1929 case SHT_GNU_verdef:
1930 elf_dynverdef (abfd) = shindex;
1931 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1932 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1933 break;
1935 case SHT_GNU_versym:
1936 elf_dynversym (abfd) = shindex;
1937 elf_tdata (abfd)->dynversym_hdr = *hdr;
1938 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1939 break;
1941 case SHT_GNU_verneed:
1942 elf_dynverref (abfd) = shindex;
1943 elf_tdata (abfd)->dynverref_hdr = *hdr;
1944 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1945 break;
1947 case SHT_SHLIB:
1948 return TRUE;
1950 case SHT_GROUP:
1951 /* We need a BFD section for objcopy and relocatable linking,
1952 and it's handy to have the signature available as the section
1953 name. */
1954 name = group_signature (abfd, hdr);
1955 if (name == NULL)
1956 return FALSE;
1957 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name))
1958 return FALSE;
1959 if (hdr->contents != NULL)
1961 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1962 unsigned int n_elt = hdr->sh_size / 4;
1963 asection *s;
1965 if (idx->flags & GRP_COMDAT)
1966 hdr->bfd_section->flags
1967 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1969 /* We try to keep the same section order as it comes in. */
1970 idx += n_elt;
1971 while (--n_elt != 0)
1972 if ((s = (--idx)->shdr->bfd_section) != NULL
1973 && elf_next_in_group (s) != NULL)
1975 elf_next_in_group (hdr->bfd_section) = s;
1976 break;
1979 break;
1981 default:
1982 /* Check for any processor-specific section types. */
1984 if (bed->elf_backend_section_from_shdr)
1985 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1987 break;
1990 return TRUE;
1993 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1994 Return SEC for sections that have no elf section, and NULL on error. */
1996 asection *
1997 bfd_section_from_r_symndx (bfd *abfd,
1998 struct sym_sec_cache *cache,
1999 asection *sec,
2000 unsigned long r_symndx)
2002 Elf_Internal_Shdr *symtab_hdr;
2003 unsigned char esym[sizeof (Elf64_External_Sym)];
2004 Elf_External_Sym_Shndx eshndx;
2005 Elf_Internal_Sym isym;
2006 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2008 if (cache->abfd == abfd && cache->indx[ent] == r_symndx)
2009 return cache->sec[ent];
2011 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2012 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2013 &isym, esym, &eshndx) == NULL)
2014 return NULL;
2016 if (cache->abfd != abfd)
2018 memset (cache->indx, -1, sizeof (cache->indx));
2019 cache->abfd = abfd;
2021 cache->indx[ent] = r_symndx;
2022 cache->sec[ent] = sec;
2023 if ((isym.st_shndx != SHN_UNDEF && isym.st_shndx < SHN_LORESERVE)
2024 || isym.st_shndx > SHN_HIRESERVE)
2026 asection *s;
2027 s = bfd_section_from_elf_index (abfd, isym.st_shndx);
2028 if (s != NULL)
2029 cache->sec[ent] = s;
2031 return cache->sec[ent];
2034 /* Given an ELF section number, retrieve the corresponding BFD
2035 section. */
2037 asection *
2038 bfd_section_from_elf_index (bfd *abfd, unsigned int index)
2040 if (index >= elf_numsections (abfd))
2041 return NULL;
2042 return elf_elfsections (abfd)[index]->bfd_section;
2045 static struct bfd_elf_special_section const special_sections[] =
2047 { ".bss", 4, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2048 { ".comment", 8, 0, SHT_PROGBITS, 0 },
2049 { ".data", 5, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2050 { ".data1", 6, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2051 { ".debug", 6, 0, SHT_PROGBITS, 0 },
2052 { ".fini", 5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2053 { ".init", 5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2054 { ".line", 5, 0, SHT_PROGBITS, 0 },
2055 { ".rodata", 7, -2, SHT_PROGBITS, SHF_ALLOC },
2056 { ".rodata1", 8, 0, SHT_PROGBITS, SHF_ALLOC },
2057 { ".tbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2058 { ".tdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2059 { ".text", 5, -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2060 { ".init_array", 11, 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2061 { ".fini_array", 11, 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2062 { ".preinit_array", 14, 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2063 { ".debug_line", 11, 0, SHT_PROGBITS, 0 },
2064 { ".debug_info", 11, 0, SHT_PROGBITS, 0 },
2065 { ".debug_abbrev", 13, 0, SHT_PROGBITS, 0 },
2066 { ".debug_aranges", 14, 0, SHT_PROGBITS, 0 },
2067 { ".dynamic", 8, 0, SHT_DYNAMIC, SHF_ALLOC },
2068 { ".dynstr", 7, 0, SHT_STRTAB, SHF_ALLOC },
2069 { ".dynsym", 7, 0, SHT_DYNSYM, SHF_ALLOC },
2070 { ".got", 4, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2071 { ".hash", 5, 0, SHT_HASH, SHF_ALLOC },
2072 { ".interp", 7, 0, SHT_PROGBITS, 0 },
2073 { ".plt", 4, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2074 { ".shstrtab", 9, 0, SHT_STRTAB, 0 },
2075 { ".strtab", 7, 0, SHT_STRTAB, 0 },
2076 { ".symtab", 7, 0, SHT_SYMTAB, 0 },
2077 { ".gnu.version", 12, 0, SHT_GNU_versym, 0 },
2078 { ".gnu.version_d", 14, 0, SHT_GNU_verdef, 0 },
2079 { ".gnu.version_r", 14, 0, SHT_GNU_verneed, 0 },
2080 { ".note.GNU-stack",15, 0, SHT_PROGBITS, 0 },
2081 { ".note", 5, -1, SHT_NOTE, 0 },
2082 { ".rela", 5, -1, SHT_RELA, 0 },
2083 { ".rel", 4, -1, SHT_REL, 0 },
2084 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2085 { NULL, 0, 0, 0, 0 }
2088 static const struct bfd_elf_special_section *
2089 get_special_section (const char *name,
2090 const struct bfd_elf_special_section *special_sections,
2091 unsigned int rela)
2093 int i;
2094 int len = strlen (name);
2096 for (i = 0; special_sections[i].prefix != NULL; i++)
2098 int suffix_len;
2099 int prefix_len = special_sections[i].prefix_length;
2101 if (len < prefix_len)
2102 continue;
2103 if (memcmp (name, special_sections[i].prefix, prefix_len) != 0)
2104 continue;
2106 suffix_len = special_sections[i].suffix_length;
2107 if (suffix_len <= 0)
2109 if (name[prefix_len] != 0)
2111 if (suffix_len == 0)
2112 continue;
2113 if (name[prefix_len] != '.'
2114 && (suffix_len == -2
2115 || (rela && special_sections[i].type == SHT_REL)))
2116 continue;
2119 else
2121 if (len < prefix_len + suffix_len)
2122 continue;
2123 if (memcmp (name + len - suffix_len,
2124 special_sections[i].prefix + prefix_len,
2125 suffix_len) != 0)
2126 continue;
2128 return &special_sections[i];
2131 return NULL;
2134 const struct bfd_elf_special_section *
2135 _bfd_elf_get_sec_type_attr (bfd *abfd, const char *name)
2137 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2138 const struct bfd_elf_special_section *ssect = NULL;
2140 /* See if this is one of the special sections. */
2141 if (name)
2143 unsigned int rela = bed->default_use_rela_p;
2145 if (bed->special_sections)
2146 ssect = get_special_section (name, bed->special_sections, rela);
2148 if (! ssect)
2149 ssect = get_special_section (name, special_sections, rela);
2152 return ssect;
2155 bfd_boolean
2156 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2158 struct bfd_elf_section_data *sdata;
2159 const struct bfd_elf_special_section *ssect;
2161 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2162 if (sdata == NULL)
2164 sdata = bfd_zalloc (abfd, sizeof (*sdata));
2165 if (sdata == NULL)
2166 return FALSE;
2167 sec->used_by_bfd = sdata;
2170 elf_section_type (sec) = SHT_NULL;
2171 ssect = _bfd_elf_get_sec_type_attr (abfd, sec->name);
2172 if (ssect != NULL)
2174 elf_section_type (sec) = ssect->type;
2175 elf_section_flags (sec) = ssect->attr;
2178 /* Indicate whether or not this section should use RELA relocations. */
2179 sec->use_rela_p = get_elf_backend_data (abfd)->default_use_rela_p;
2181 return TRUE;
2184 /* Create a new bfd section from an ELF program header.
2186 Since program segments have no names, we generate a synthetic name
2187 of the form segment<NUM>, where NUM is generally the index in the
2188 program header table. For segments that are split (see below) we
2189 generate the names segment<NUM>a and segment<NUM>b.
2191 Note that some program segments may have a file size that is different than
2192 (less than) the memory size. All this means is that at execution the
2193 system must allocate the amount of memory specified by the memory size,
2194 but only initialize it with the first "file size" bytes read from the
2195 file. This would occur for example, with program segments consisting
2196 of combined data+bss.
2198 To handle the above situation, this routine generates TWO bfd sections
2199 for the single program segment. The first has the length specified by
2200 the file size of the segment, and the second has the length specified
2201 by the difference between the two sizes. In effect, the segment is split
2202 into it's initialized and uninitialized parts.
2206 bfd_boolean
2207 _bfd_elf_make_section_from_phdr (bfd *abfd,
2208 Elf_Internal_Phdr *hdr,
2209 int index,
2210 const char *typename)
2212 asection *newsect;
2213 char *name;
2214 char namebuf[64];
2215 size_t len;
2216 int split;
2218 split = ((hdr->p_memsz > 0)
2219 && (hdr->p_filesz > 0)
2220 && (hdr->p_memsz > hdr->p_filesz));
2221 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
2222 len = strlen (namebuf) + 1;
2223 name = bfd_alloc (abfd, len);
2224 if (!name)
2225 return FALSE;
2226 memcpy (name, namebuf, len);
2227 newsect = bfd_make_section (abfd, name);
2228 if (newsect == NULL)
2229 return FALSE;
2230 newsect->vma = hdr->p_vaddr;
2231 newsect->lma = hdr->p_paddr;
2232 newsect->size = hdr->p_filesz;
2233 newsect->filepos = hdr->p_offset;
2234 newsect->flags |= SEC_HAS_CONTENTS;
2235 newsect->alignment_power = bfd_log2 (hdr->p_align);
2236 if (hdr->p_type == PT_LOAD)
2238 newsect->flags |= SEC_ALLOC;
2239 newsect->flags |= SEC_LOAD;
2240 if (hdr->p_flags & PF_X)
2242 /* FIXME: all we known is that it has execute PERMISSION,
2243 may be data. */
2244 newsect->flags |= SEC_CODE;
2247 if (!(hdr->p_flags & PF_W))
2249 newsect->flags |= SEC_READONLY;
2252 if (split)
2254 sprintf (namebuf, "%s%db", typename, index);
2255 len = strlen (namebuf) + 1;
2256 name = bfd_alloc (abfd, len);
2257 if (!name)
2258 return FALSE;
2259 memcpy (name, namebuf, len);
2260 newsect = bfd_make_section (abfd, name);
2261 if (newsect == NULL)
2262 return FALSE;
2263 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2264 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2265 newsect->size = hdr->p_memsz - hdr->p_filesz;
2266 if (hdr->p_type == PT_LOAD)
2268 newsect->flags |= SEC_ALLOC;
2269 if (hdr->p_flags & PF_X)
2270 newsect->flags |= SEC_CODE;
2272 if (!(hdr->p_flags & PF_W))
2273 newsect->flags |= SEC_READONLY;
2276 return TRUE;
2279 bfd_boolean
2280 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index)
2282 const struct elf_backend_data *bed;
2284 switch (hdr->p_type)
2286 case PT_NULL:
2287 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2289 case PT_LOAD:
2290 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2292 case PT_DYNAMIC:
2293 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2295 case PT_INTERP:
2296 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2298 case PT_NOTE:
2299 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2300 return FALSE;
2301 if (! elfcore_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2302 return FALSE;
2303 return TRUE;
2305 case PT_SHLIB:
2306 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2308 case PT_PHDR:
2309 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2311 case PT_GNU_EH_FRAME:
2312 return _bfd_elf_make_section_from_phdr (abfd, hdr, index,
2313 "eh_frame_hdr");
2315 case PT_GNU_STACK:
2316 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "stack");
2318 case PT_GNU_RELRO:
2319 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro");
2321 default:
2322 /* Check for any processor-specific program segment types.
2323 If no handler for them, default to making "segment" sections. */
2324 bed = get_elf_backend_data (abfd);
2325 if (bed->elf_backend_section_from_phdr)
2326 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
2327 else
2328 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
2332 /* Initialize REL_HDR, the section-header for new section, containing
2333 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2334 relocations; otherwise, we use REL relocations. */
2336 bfd_boolean
2337 _bfd_elf_init_reloc_shdr (bfd *abfd,
2338 Elf_Internal_Shdr *rel_hdr,
2339 asection *asect,
2340 bfd_boolean use_rela_p)
2342 char *name;
2343 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2344 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2346 name = bfd_alloc (abfd, amt);
2347 if (name == NULL)
2348 return FALSE;
2349 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2350 rel_hdr->sh_name =
2351 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2352 FALSE);
2353 if (rel_hdr->sh_name == (unsigned int) -1)
2354 return FALSE;
2355 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2356 rel_hdr->sh_entsize = (use_rela_p
2357 ? bed->s->sizeof_rela
2358 : bed->s->sizeof_rel);
2359 rel_hdr->sh_addralign = 1 << bed->s->log_file_align;
2360 rel_hdr->sh_flags = 0;
2361 rel_hdr->sh_addr = 0;
2362 rel_hdr->sh_size = 0;
2363 rel_hdr->sh_offset = 0;
2365 return TRUE;
2368 /* Set up an ELF internal section header for a section. */
2370 static void
2371 elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg)
2373 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2374 bfd_boolean *failedptr = failedptrarg;
2375 Elf_Internal_Shdr *this_hdr;
2377 if (*failedptr)
2379 /* We already failed; just get out of the bfd_map_over_sections
2380 loop. */
2381 return;
2384 this_hdr = &elf_section_data (asect)->this_hdr;
2386 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2387 asect->name, FALSE);
2388 if (this_hdr->sh_name == (unsigned int) -1)
2390 *failedptr = TRUE;
2391 return;
2394 this_hdr->sh_flags = 0;
2396 if ((asect->flags & SEC_ALLOC) != 0
2397 || asect->user_set_vma)
2398 this_hdr->sh_addr = asect->vma;
2399 else
2400 this_hdr->sh_addr = 0;
2402 this_hdr->sh_offset = 0;
2403 this_hdr->sh_size = asect->size;
2404 this_hdr->sh_link = 0;
2405 this_hdr->sh_addralign = 1 << asect->alignment_power;
2406 /* The sh_entsize and sh_info fields may have been set already by
2407 copy_private_section_data. */
2409 this_hdr->bfd_section = asect;
2410 this_hdr->contents = NULL;
2412 /* If the section type is unspecified, we set it based on
2413 asect->flags. */
2414 if (this_hdr->sh_type == SHT_NULL)
2416 if ((asect->flags & SEC_GROUP) != 0)
2418 /* We also need to mark SHF_GROUP here for relocatable
2419 link. */
2420 struct bfd_link_order *l;
2421 asection *elt;
2423 for (l = asect->link_order_head; l != NULL; l = l->next)
2424 if (l->type == bfd_indirect_link_order
2425 && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
2428 /* The name is not important. Anything will do. */
2429 elf_group_name (elt->output_section) = "G";
2430 elf_section_flags (elt->output_section) |= SHF_GROUP;
2432 elt = elf_next_in_group (elt);
2433 /* During a relocatable link, the lists are
2434 circular. */
2436 while (elt != elf_next_in_group (l->u.indirect.section));
2438 this_hdr->sh_type = SHT_GROUP;
2440 else if ((asect->flags & SEC_ALLOC) != 0
2441 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2442 || (asect->flags & SEC_NEVER_LOAD) != 0))
2443 this_hdr->sh_type = SHT_NOBITS;
2444 else
2445 this_hdr->sh_type = SHT_PROGBITS;
2448 switch (this_hdr->sh_type)
2450 default:
2451 break;
2453 case SHT_STRTAB:
2454 case SHT_INIT_ARRAY:
2455 case SHT_FINI_ARRAY:
2456 case SHT_PREINIT_ARRAY:
2457 case SHT_NOTE:
2458 case SHT_NOBITS:
2459 case SHT_PROGBITS:
2460 break;
2462 case SHT_HASH:
2463 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2464 break;
2466 case SHT_DYNSYM:
2467 this_hdr->sh_entsize = bed->s->sizeof_sym;
2468 break;
2470 case SHT_DYNAMIC:
2471 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2472 break;
2474 case SHT_RELA:
2475 if (get_elf_backend_data (abfd)->may_use_rela_p)
2476 this_hdr->sh_entsize = bed->s->sizeof_rela;
2477 break;
2479 case SHT_REL:
2480 if (get_elf_backend_data (abfd)->may_use_rel_p)
2481 this_hdr->sh_entsize = bed->s->sizeof_rel;
2482 break;
2484 case SHT_GNU_versym:
2485 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2486 break;
2488 case SHT_GNU_verdef:
2489 this_hdr->sh_entsize = 0;
2490 /* objcopy or strip will copy over sh_info, but may not set
2491 cverdefs. The linker will set cverdefs, but sh_info will be
2492 zero. */
2493 if (this_hdr->sh_info == 0)
2494 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2495 else
2496 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2497 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2498 break;
2500 case SHT_GNU_verneed:
2501 this_hdr->sh_entsize = 0;
2502 /* objcopy or strip will copy over sh_info, but may not set
2503 cverrefs. The linker will set cverrefs, but sh_info will be
2504 zero. */
2505 if (this_hdr->sh_info == 0)
2506 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2507 else
2508 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2509 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2510 break;
2512 case SHT_GROUP:
2513 this_hdr->sh_entsize = 4;
2514 break;
2517 if ((asect->flags & SEC_ALLOC) != 0)
2518 this_hdr->sh_flags |= SHF_ALLOC;
2519 if ((asect->flags & SEC_READONLY) == 0)
2520 this_hdr->sh_flags |= SHF_WRITE;
2521 if ((asect->flags & SEC_CODE) != 0)
2522 this_hdr->sh_flags |= SHF_EXECINSTR;
2523 if ((asect->flags & SEC_MERGE) != 0)
2525 this_hdr->sh_flags |= SHF_MERGE;
2526 this_hdr->sh_entsize = asect->entsize;
2527 if ((asect->flags & SEC_STRINGS) != 0)
2528 this_hdr->sh_flags |= SHF_STRINGS;
2530 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2531 this_hdr->sh_flags |= SHF_GROUP;
2532 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2534 this_hdr->sh_flags |= SHF_TLS;
2535 if (asect->size == 0 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2537 struct bfd_link_order *o;
2539 this_hdr->sh_size = 0;
2540 for (o = asect->link_order_head; o != NULL; o = o->next)
2541 if (this_hdr->sh_size < o->offset + o->size)
2542 this_hdr->sh_size = o->offset + o->size;
2543 if (this_hdr->sh_size)
2544 this_hdr->sh_type = SHT_NOBITS;
2548 /* Check for processor-specific section types. */
2549 if (bed->elf_backend_fake_sections
2550 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2551 *failedptr = TRUE;
2553 /* If the section has relocs, set up a section header for the
2554 SHT_REL[A] section. If two relocation sections are required for
2555 this section, it is up to the processor-specific back-end to
2556 create the other. */
2557 if ((asect->flags & SEC_RELOC) != 0
2558 && !_bfd_elf_init_reloc_shdr (abfd,
2559 &elf_section_data (asect)->rel_hdr,
2560 asect,
2561 asect->use_rela_p))
2562 *failedptr = TRUE;
2565 /* Fill in the contents of a SHT_GROUP section. */
2567 void
2568 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2570 bfd_boolean *failedptr = failedptrarg;
2571 unsigned long symindx;
2572 asection *elt, *first;
2573 unsigned char *loc;
2574 struct bfd_link_order *l;
2575 bfd_boolean gas;
2577 if (elf_section_data (sec)->this_hdr.sh_type != SHT_GROUP
2578 || *failedptr)
2579 return;
2581 symindx = 0;
2582 if (elf_group_id (sec) != NULL)
2583 symindx = elf_group_id (sec)->udata.i;
2585 if (symindx == 0)
2587 /* If called from the assembler, swap_out_syms will have set up
2588 elf_section_syms; If called for "ld -r", use target_index. */
2589 if (elf_section_syms (abfd) != NULL)
2590 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2591 else
2592 symindx = sec->target_index;
2594 elf_section_data (sec)->this_hdr.sh_info = symindx;
2596 /* The contents won't be allocated for "ld -r" or objcopy. */
2597 gas = TRUE;
2598 if (sec->contents == NULL)
2600 gas = FALSE;
2601 sec->contents = bfd_alloc (abfd, sec->size);
2603 /* Arrange for the section to be written out. */
2604 elf_section_data (sec)->this_hdr.contents = sec->contents;
2605 if (sec->contents == NULL)
2607 *failedptr = TRUE;
2608 return;
2612 loc = sec->contents + sec->size;
2614 /* Get the pointer to the first section in the group that gas
2615 squirreled away here. objcopy arranges for this to be set to the
2616 start of the input section group. */
2617 first = elt = elf_next_in_group (sec);
2619 /* First element is a flag word. Rest of section is elf section
2620 indices for all the sections of the group. Write them backwards
2621 just to keep the group in the same order as given in .section
2622 directives, not that it matters. */
2623 while (elt != NULL)
2625 asection *s;
2626 unsigned int idx;
2628 loc -= 4;
2629 s = elt;
2630 if (!gas)
2631 s = s->output_section;
2632 idx = 0;
2633 if (s != NULL)
2634 idx = elf_section_data (s)->this_idx;
2635 H_PUT_32 (abfd, idx, loc);
2636 elt = elf_next_in_group (elt);
2637 if (elt == first)
2638 break;
2641 /* If this is a relocatable link, then the above did nothing because
2642 SEC is the output section. Look through the input sections
2643 instead. */
2644 for (l = sec->link_order_head; l != NULL; l = l->next)
2645 if (l->type == bfd_indirect_link_order
2646 && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
2649 loc -= 4;
2650 H_PUT_32 (abfd,
2651 elf_section_data (elt->output_section)->this_idx, loc);
2652 elt = elf_next_in_group (elt);
2653 /* During a relocatable link, the lists are circular. */
2655 while (elt != elf_next_in_group (l->u.indirect.section));
2657 /* With ld -r, merging SHT_GROUP sections results in wasted space
2658 due to allowing for the flag word on each input. We may well
2659 duplicate entries too. */
2660 while ((loc -= 4) > sec->contents)
2661 H_PUT_32 (abfd, 0, loc);
2663 if (loc != sec->contents)
2664 abort ();
2666 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2669 /* Assign all ELF section numbers. The dummy first section is handled here
2670 too. The link/info pointers for the standard section types are filled
2671 in here too, while we're at it. */
2673 static bfd_boolean
2674 assign_section_numbers (bfd *abfd)
2676 struct elf_obj_tdata *t = elf_tdata (abfd);
2677 asection *sec;
2678 unsigned int section_number, secn;
2679 Elf_Internal_Shdr **i_shdrp;
2680 bfd_size_type amt;
2682 section_number = 1;
2684 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2686 for (sec = abfd->sections; sec; sec = sec->next)
2688 struct bfd_elf_section_data *d = elf_section_data (sec);
2690 if (section_number == SHN_LORESERVE)
2691 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2692 d->this_idx = section_number++;
2693 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2694 if ((sec->flags & SEC_RELOC) == 0)
2695 d->rel_idx = 0;
2696 else
2698 if (section_number == SHN_LORESERVE)
2699 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2700 d->rel_idx = section_number++;
2701 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2704 if (d->rel_hdr2)
2706 if (section_number == SHN_LORESERVE)
2707 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2708 d->rel_idx2 = section_number++;
2709 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2711 else
2712 d->rel_idx2 = 0;
2715 if (section_number == SHN_LORESERVE)
2716 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2717 t->shstrtab_section = section_number++;
2718 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2719 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2721 if (bfd_get_symcount (abfd) > 0)
2723 if (section_number == SHN_LORESERVE)
2724 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2725 t->symtab_section = section_number++;
2726 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2727 if (section_number > SHN_LORESERVE - 2)
2729 if (section_number == SHN_LORESERVE)
2730 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2731 t->symtab_shndx_section = section_number++;
2732 t->symtab_shndx_hdr.sh_name
2733 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2734 ".symtab_shndx", FALSE);
2735 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2736 return FALSE;
2738 if (section_number == SHN_LORESERVE)
2739 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2740 t->strtab_section = section_number++;
2741 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2744 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2745 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2747 elf_numsections (abfd) = section_number;
2748 elf_elfheader (abfd)->e_shnum = section_number;
2749 if (section_number > SHN_LORESERVE)
2750 elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE;
2752 /* Set up the list of section header pointers, in agreement with the
2753 indices. */
2754 amt = section_number * sizeof (Elf_Internal_Shdr *);
2755 i_shdrp = bfd_zalloc (abfd, amt);
2756 if (i_shdrp == NULL)
2757 return FALSE;
2759 amt = sizeof (Elf_Internal_Shdr);
2760 i_shdrp[0] = bfd_zalloc (abfd, amt);
2761 if (i_shdrp[0] == NULL)
2763 bfd_release (abfd, i_shdrp);
2764 return FALSE;
2767 elf_elfsections (abfd) = i_shdrp;
2769 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2770 if (bfd_get_symcount (abfd) > 0)
2772 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2773 if (elf_numsections (abfd) > SHN_LORESERVE)
2775 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2776 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2778 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2779 t->symtab_hdr.sh_link = t->strtab_section;
2781 for (sec = abfd->sections; sec; sec = sec->next)
2783 struct bfd_elf_section_data *d = elf_section_data (sec);
2784 asection *s;
2785 const char *name;
2787 i_shdrp[d->this_idx] = &d->this_hdr;
2788 if (d->rel_idx != 0)
2789 i_shdrp[d->rel_idx] = &d->rel_hdr;
2790 if (d->rel_idx2 != 0)
2791 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2793 /* Fill in the sh_link and sh_info fields while we're at it. */
2795 /* sh_link of a reloc section is the section index of the symbol
2796 table. sh_info is the section index of the section to which
2797 the relocation entries apply. */
2798 if (d->rel_idx != 0)
2800 d->rel_hdr.sh_link = t->symtab_section;
2801 d->rel_hdr.sh_info = d->this_idx;
2803 if (d->rel_idx2 != 0)
2805 d->rel_hdr2->sh_link = t->symtab_section;
2806 d->rel_hdr2->sh_info = d->this_idx;
2809 switch (d->this_hdr.sh_type)
2811 case SHT_REL:
2812 case SHT_RELA:
2813 /* A reloc section which we are treating as a normal BFD
2814 section. sh_link is the section index of the symbol
2815 table. sh_info is the section index of the section to
2816 which the relocation entries apply. We assume that an
2817 allocated reloc section uses the dynamic symbol table.
2818 FIXME: How can we be sure? */
2819 s = bfd_get_section_by_name (abfd, ".dynsym");
2820 if (s != NULL)
2821 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2823 /* We look up the section the relocs apply to by name. */
2824 name = sec->name;
2825 if (d->this_hdr.sh_type == SHT_REL)
2826 name += 4;
2827 else
2828 name += 5;
2829 s = bfd_get_section_by_name (abfd, name);
2830 if (s != NULL)
2831 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
2832 break;
2834 case SHT_STRTAB:
2835 /* We assume that a section named .stab*str is a stabs
2836 string section. We look for a section with the same name
2837 but without the trailing ``str'', and set its sh_link
2838 field to point to this section. */
2839 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
2840 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
2842 size_t len;
2843 char *alc;
2845 len = strlen (sec->name);
2846 alc = bfd_malloc (len - 2);
2847 if (alc == NULL)
2848 return FALSE;
2849 memcpy (alc, sec->name, len - 3);
2850 alc[len - 3] = '\0';
2851 s = bfd_get_section_by_name (abfd, alc);
2852 free (alc);
2853 if (s != NULL)
2855 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
2857 /* This is a .stab section. */
2858 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
2859 elf_section_data (s)->this_hdr.sh_entsize
2860 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
2863 break;
2865 case SHT_DYNAMIC:
2866 case SHT_DYNSYM:
2867 case SHT_GNU_verneed:
2868 case SHT_GNU_verdef:
2869 /* sh_link is the section header index of the string table
2870 used for the dynamic entries, or the symbol table, or the
2871 version strings. */
2872 s = bfd_get_section_by_name (abfd, ".dynstr");
2873 if (s != NULL)
2874 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2875 break;
2877 case SHT_HASH:
2878 case SHT_GNU_versym:
2879 /* sh_link is the section header index of the symbol table
2880 this hash table or version table is for. */
2881 s = bfd_get_section_by_name (abfd, ".dynsym");
2882 if (s != NULL)
2883 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2884 break;
2886 case SHT_GROUP:
2887 d->this_hdr.sh_link = t->symtab_section;
2891 for (secn = 1; secn < section_number; ++secn)
2892 if (i_shdrp[secn] == NULL)
2893 i_shdrp[secn] = i_shdrp[0];
2894 else
2895 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
2896 i_shdrp[secn]->sh_name);
2897 return TRUE;
2900 /* Map symbol from it's internal number to the external number, moving
2901 all local symbols to be at the head of the list. */
2903 static int
2904 sym_is_global (bfd *abfd, asymbol *sym)
2906 /* If the backend has a special mapping, use it. */
2907 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2908 if (bed->elf_backend_sym_is_global)
2909 return (*bed->elf_backend_sym_is_global) (abfd, sym);
2911 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2912 || bfd_is_und_section (bfd_get_section (sym))
2913 || bfd_is_com_section (bfd_get_section (sym)));
2916 static bfd_boolean
2917 elf_map_symbols (bfd *abfd)
2919 unsigned int symcount = bfd_get_symcount (abfd);
2920 asymbol **syms = bfd_get_outsymbols (abfd);
2921 asymbol **sect_syms;
2922 unsigned int num_locals = 0;
2923 unsigned int num_globals = 0;
2924 unsigned int num_locals2 = 0;
2925 unsigned int num_globals2 = 0;
2926 int max_index = 0;
2927 unsigned int idx;
2928 asection *asect;
2929 asymbol **new_syms;
2930 bfd_size_type amt;
2932 #ifdef DEBUG
2933 fprintf (stderr, "elf_map_symbols\n");
2934 fflush (stderr);
2935 #endif
2937 for (asect = abfd->sections; asect; asect = asect->next)
2939 if (max_index < asect->index)
2940 max_index = asect->index;
2943 max_index++;
2944 amt = max_index * sizeof (asymbol *);
2945 sect_syms = bfd_zalloc (abfd, amt);
2946 if (sect_syms == NULL)
2947 return FALSE;
2948 elf_section_syms (abfd) = sect_syms;
2949 elf_num_section_syms (abfd) = max_index;
2951 /* Init sect_syms entries for any section symbols we have already
2952 decided to output. */
2953 for (idx = 0; idx < symcount; idx++)
2955 asymbol *sym = syms[idx];
2957 if ((sym->flags & BSF_SECTION_SYM) != 0
2958 && sym->value == 0)
2960 asection *sec;
2962 sec = sym->section;
2964 if (sec->owner != NULL)
2966 if (sec->owner != abfd)
2968 if (sec->output_offset != 0)
2969 continue;
2971 sec = sec->output_section;
2973 /* Empty sections in the input files may have had a
2974 section symbol created for them. (See the comment
2975 near the end of _bfd_generic_link_output_symbols in
2976 linker.c). If the linker script discards such
2977 sections then we will reach this point. Since we know
2978 that we cannot avoid this case, we detect it and skip
2979 the abort and the assignment to the sect_syms array.
2980 To reproduce this particular case try running the
2981 linker testsuite test ld-scripts/weak.exp for an ELF
2982 port that uses the generic linker. */
2983 if (sec->owner == NULL)
2984 continue;
2986 BFD_ASSERT (sec->owner == abfd);
2988 sect_syms[sec->index] = syms[idx];
2993 /* Classify all of the symbols. */
2994 for (idx = 0; idx < symcount; idx++)
2996 if (!sym_is_global (abfd, syms[idx]))
2997 num_locals++;
2998 else
2999 num_globals++;
3002 /* We will be adding a section symbol for each BFD section. Most normal
3003 sections will already have a section symbol in outsymbols, but
3004 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3005 at least in that case. */
3006 for (asect = abfd->sections; asect; asect = asect->next)
3008 if (sect_syms[asect->index] == NULL)
3010 if (!sym_is_global (abfd, asect->symbol))
3011 num_locals++;
3012 else
3013 num_globals++;
3017 /* Now sort the symbols so the local symbols are first. */
3018 amt = (num_locals + num_globals) * sizeof (asymbol *);
3019 new_syms = bfd_alloc (abfd, amt);
3021 if (new_syms == NULL)
3022 return FALSE;
3024 for (idx = 0; idx < symcount; idx++)
3026 asymbol *sym = syms[idx];
3027 unsigned int i;
3029 if (!sym_is_global (abfd, sym))
3030 i = num_locals2++;
3031 else
3032 i = num_locals + num_globals2++;
3033 new_syms[i] = sym;
3034 sym->udata.i = i + 1;
3036 for (asect = abfd->sections; asect; asect = asect->next)
3038 if (sect_syms[asect->index] == NULL)
3040 asymbol *sym = asect->symbol;
3041 unsigned int i;
3043 sect_syms[asect->index] = sym;
3044 if (!sym_is_global (abfd, sym))
3045 i = num_locals2++;
3046 else
3047 i = num_locals + num_globals2++;
3048 new_syms[i] = sym;
3049 sym->udata.i = i + 1;
3053 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3055 elf_num_locals (abfd) = num_locals;
3056 elf_num_globals (abfd) = num_globals;
3057 return TRUE;
3060 /* Align to the maximum file alignment that could be required for any
3061 ELF data structure. */
3063 static inline file_ptr
3064 align_file_position (file_ptr off, int align)
3066 return (off + align - 1) & ~(align - 1);
3069 /* Assign a file position to a section, optionally aligning to the
3070 required section alignment. */
3072 file_ptr
3073 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3074 file_ptr offset,
3075 bfd_boolean align)
3077 if (align)
3079 unsigned int al;
3081 al = i_shdrp->sh_addralign;
3082 if (al > 1)
3083 offset = BFD_ALIGN (offset, al);
3085 i_shdrp->sh_offset = offset;
3086 if (i_shdrp->bfd_section != NULL)
3087 i_shdrp->bfd_section->filepos = offset;
3088 if (i_shdrp->sh_type != SHT_NOBITS)
3089 offset += i_shdrp->sh_size;
3090 return offset;
3093 /* Compute the file positions we are going to put the sections at, and
3094 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3095 is not NULL, this is being called by the ELF backend linker. */
3097 bfd_boolean
3098 _bfd_elf_compute_section_file_positions (bfd *abfd,
3099 struct bfd_link_info *link_info)
3101 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3102 bfd_boolean failed;
3103 struct bfd_strtab_hash *strtab;
3104 Elf_Internal_Shdr *shstrtab_hdr;
3106 if (abfd->output_has_begun)
3107 return TRUE;
3109 /* Do any elf backend specific processing first. */
3110 if (bed->elf_backend_begin_write_processing)
3111 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3113 if (! prep_headers (abfd))
3114 return FALSE;
3116 /* Post process the headers if necessary. */
3117 if (bed->elf_backend_post_process_headers)
3118 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3120 failed = FALSE;
3121 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3122 if (failed)
3123 return FALSE;
3125 if (!assign_section_numbers (abfd))
3126 return FALSE;
3128 /* The backend linker builds symbol table information itself. */
3129 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3131 /* Non-zero if doing a relocatable link. */
3132 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3134 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3135 return FALSE;
3138 if (link_info == NULL)
3140 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3141 if (failed)
3142 return FALSE;
3145 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3146 /* sh_name was set in prep_headers. */
3147 shstrtab_hdr->sh_type = SHT_STRTAB;
3148 shstrtab_hdr->sh_flags = 0;
3149 shstrtab_hdr->sh_addr = 0;
3150 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3151 shstrtab_hdr->sh_entsize = 0;
3152 shstrtab_hdr->sh_link = 0;
3153 shstrtab_hdr->sh_info = 0;
3154 /* sh_offset is set in assign_file_positions_except_relocs. */
3155 shstrtab_hdr->sh_addralign = 1;
3157 if (!assign_file_positions_except_relocs (abfd, link_info))
3158 return FALSE;
3160 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3162 file_ptr off;
3163 Elf_Internal_Shdr *hdr;
3165 off = elf_tdata (abfd)->next_file_pos;
3167 hdr = &elf_tdata (abfd)->symtab_hdr;
3168 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3170 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3171 if (hdr->sh_size != 0)
3172 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3174 hdr = &elf_tdata (abfd)->strtab_hdr;
3175 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3177 elf_tdata (abfd)->next_file_pos = off;
3179 /* Now that we know where the .strtab section goes, write it
3180 out. */
3181 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3182 || ! _bfd_stringtab_emit (abfd, strtab))
3183 return FALSE;
3184 _bfd_stringtab_free (strtab);
3187 abfd->output_has_begun = TRUE;
3189 return TRUE;
3192 /* Create a mapping from a set of sections to a program segment. */
3194 static struct elf_segment_map *
3195 make_mapping (bfd *abfd,
3196 asection **sections,
3197 unsigned int from,
3198 unsigned int to,
3199 bfd_boolean phdr)
3201 struct elf_segment_map *m;
3202 unsigned int i;
3203 asection **hdrpp;
3204 bfd_size_type amt;
3206 amt = sizeof (struct elf_segment_map);
3207 amt += (to - from - 1) * sizeof (asection *);
3208 m = bfd_zalloc (abfd, amt);
3209 if (m == NULL)
3210 return NULL;
3211 m->next = NULL;
3212 m->p_type = PT_LOAD;
3213 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3214 m->sections[i - from] = *hdrpp;
3215 m->count = to - from;
3217 if (from == 0 && phdr)
3219 /* Include the headers in the first PT_LOAD segment. */
3220 m->includes_filehdr = 1;
3221 m->includes_phdrs = 1;
3224 return m;
3227 /* Set up a mapping from BFD sections to program segments. */
3229 static bfd_boolean
3230 map_sections_to_segments (bfd *abfd)
3232 asection **sections = NULL;
3233 asection *s;
3234 unsigned int i;
3235 unsigned int count;
3236 struct elf_segment_map *mfirst;
3237 struct elf_segment_map **pm;
3238 struct elf_segment_map *m;
3239 asection *last_hdr;
3240 bfd_vma last_size;
3241 unsigned int phdr_index;
3242 bfd_vma maxpagesize;
3243 asection **hdrpp;
3244 bfd_boolean phdr_in_segment = TRUE;
3245 bfd_boolean writable;
3246 int tls_count = 0;
3247 asection *first_tls = NULL;
3248 asection *dynsec, *eh_frame_hdr;
3249 bfd_size_type amt;
3251 if (elf_tdata (abfd)->segment_map != NULL)
3252 return TRUE;
3254 if (bfd_count_sections (abfd) == 0)
3255 return TRUE;
3257 /* Select the allocated sections, and sort them. */
3259 amt = bfd_count_sections (abfd) * sizeof (asection *);
3260 sections = bfd_malloc (amt);
3261 if (sections == NULL)
3262 goto error_return;
3264 i = 0;
3265 for (s = abfd->sections; s != NULL; s = s->next)
3267 if ((s->flags & SEC_ALLOC) != 0)
3269 sections[i] = s;
3270 ++i;
3273 BFD_ASSERT (i <= bfd_count_sections (abfd));
3274 count = i;
3276 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3278 /* Build the mapping. */
3280 mfirst = NULL;
3281 pm = &mfirst;
3283 /* If we have a .interp section, then create a PT_PHDR segment for
3284 the program headers and a PT_INTERP segment for the .interp
3285 section. */
3286 s = bfd_get_section_by_name (abfd, ".interp");
3287 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3289 amt = sizeof (struct elf_segment_map);
3290 m = bfd_zalloc (abfd, amt);
3291 if (m == NULL)
3292 goto error_return;
3293 m->next = NULL;
3294 m->p_type = PT_PHDR;
3295 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3296 m->p_flags = PF_R | PF_X;
3297 m->p_flags_valid = 1;
3298 m->includes_phdrs = 1;
3300 *pm = m;
3301 pm = &m->next;
3303 amt = sizeof (struct elf_segment_map);
3304 m = bfd_zalloc (abfd, amt);
3305 if (m == NULL)
3306 goto error_return;
3307 m->next = NULL;
3308 m->p_type = PT_INTERP;
3309 m->count = 1;
3310 m->sections[0] = s;
3312 *pm = m;
3313 pm = &m->next;
3316 /* Look through the sections. We put sections in the same program
3317 segment when the start of the second section can be placed within
3318 a few bytes of the end of the first section. */
3319 last_hdr = NULL;
3320 last_size = 0;
3321 phdr_index = 0;
3322 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
3323 writable = FALSE;
3324 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3325 if (dynsec != NULL
3326 && (dynsec->flags & SEC_LOAD) == 0)
3327 dynsec = NULL;
3329 /* Deal with -Ttext or something similar such that the first section
3330 is not adjacent to the program headers. This is an
3331 approximation, since at this point we don't know exactly how many
3332 program headers we will need. */
3333 if (count > 0)
3335 bfd_size_type phdr_size;
3337 phdr_size = elf_tdata (abfd)->program_header_size;
3338 if (phdr_size == 0)
3339 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
3340 if ((abfd->flags & D_PAGED) == 0
3341 || sections[0]->lma < phdr_size
3342 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3343 phdr_in_segment = FALSE;
3346 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3348 asection *hdr;
3349 bfd_boolean new_segment;
3351 hdr = *hdrpp;
3353 /* See if this section and the last one will fit in the same
3354 segment. */
3356 if (last_hdr == NULL)
3358 /* If we don't have a segment yet, then we don't need a new
3359 one (we build the last one after this loop). */
3360 new_segment = FALSE;
3362 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3364 /* If this section has a different relation between the
3365 virtual address and the load address, then we need a new
3366 segment. */
3367 new_segment = TRUE;
3369 else if (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
3370 < BFD_ALIGN (hdr->lma, maxpagesize))
3372 /* If putting this section in this segment would force us to
3373 skip a page in the segment, then we need a new segment. */
3374 new_segment = TRUE;
3376 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3377 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
3379 /* We don't want to put a loadable section after a
3380 nonloadable section in the same segment.
3381 Consider .tbss sections as loadable for this purpose. */
3382 new_segment = TRUE;
3384 else if ((abfd->flags & D_PAGED) == 0)
3386 /* If the file is not demand paged, which means that we
3387 don't require the sections to be correctly aligned in the
3388 file, then there is no other reason for a new segment. */
3389 new_segment = FALSE;
3391 else if (! writable
3392 && (hdr->flags & SEC_READONLY) == 0
3393 && (((last_hdr->lma + last_size - 1)
3394 & ~(maxpagesize - 1))
3395 != (hdr->lma & ~(maxpagesize - 1))))
3397 /* We don't want to put a writable section in a read only
3398 segment, unless they are on the same page in memory
3399 anyhow. We already know that the last section does not
3400 bring us past the current section on the page, so the
3401 only case in which the new section is not on the same
3402 page as the previous section is when the previous section
3403 ends precisely on a page boundary. */
3404 new_segment = TRUE;
3406 else
3408 /* Otherwise, we can use the same segment. */
3409 new_segment = FALSE;
3412 if (! new_segment)
3414 if ((hdr->flags & SEC_READONLY) == 0)
3415 writable = TRUE;
3416 last_hdr = hdr;
3417 /* .tbss sections effectively have zero size. */
3418 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3419 last_size = hdr->size;
3420 else
3421 last_size = 0;
3422 continue;
3425 /* We need a new program segment. We must create a new program
3426 header holding all the sections from phdr_index until hdr. */
3428 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3429 if (m == NULL)
3430 goto error_return;
3432 *pm = m;
3433 pm = &m->next;
3435 if ((hdr->flags & SEC_READONLY) == 0)
3436 writable = TRUE;
3437 else
3438 writable = FALSE;
3440 last_hdr = hdr;
3441 /* .tbss sections effectively have zero size. */
3442 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3443 last_size = hdr->size;
3444 else
3445 last_size = 0;
3446 phdr_index = i;
3447 phdr_in_segment = FALSE;
3450 /* Create a final PT_LOAD program segment. */
3451 if (last_hdr != NULL)
3453 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3454 if (m == NULL)
3455 goto error_return;
3457 *pm = m;
3458 pm = &m->next;
3461 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3462 if (dynsec != NULL)
3464 amt = sizeof (struct elf_segment_map);
3465 m = bfd_zalloc (abfd, amt);
3466 if (m == NULL)
3467 goto error_return;
3468 m->next = NULL;
3469 m->p_type = PT_DYNAMIC;
3470 m->count = 1;
3471 m->sections[0] = dynsec;
3473 *pm = m;
3474 pm = &m->next;
3477 /* For each loadable .note section, add a PT_NOTE segment. We don't
3478 use bfd_get_section_by_name, because if we link together
3479 nonloadable .note sections and loadable .note sections, we will
3480 generate two .note sections in the output file. FIXME: Using
3481 names for section types is bogus anyhow. */
3482 for (s = abfd->sections; s != NULL; s = s->next)
3484 if ((s->flags & SEC_LOAD) != 0
3485 && strncmp (s->name, ".note", 5) == 0)
3487 amt = sizeof (struct elf_segment_map);
3488 m = bfd_zalloc (abfd, amt);
3489 if (m == NULL)
3490 goto error_return;
3491 m->next = NULL;
3492 m->p_type = PT_NOTE;
3493 m->count = 1;
3494 m->sections[0] = s;
3496 *pm = m;
3497 pm = &m->next;
3499 if (s->flags & SEC_THREAD_LOCAL)
3501 if (! tls_count)
3502 first_tls = s;
3503 tls_count++;
3507 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3508 if (tls_count > 0)
3510 int i;
3512 amt = sizeof (struct elf_segment_map);
3513 amt += (tls_count - 1) * sizeof (asection *);
3514 m = bfd_zalloc (abfd, amt);
3515 if (m == NULL)
3516 goto error_return;
3517 m->next = NULL;
3518 m->p_type = PT_TLS;
3519 m->count = tls_count;
3520 /* Mandated PF_R. */
3521 m->p_flags = PF_R;
3522 m->p_flags_valid = 1;
3523 for (i = 0; i < tls_count; ++i)
3525 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3526 m->sections[i] = first_tls;
3527 first_tls = first_tls->next;
3530 *pm = m;
3531 pm = &m->next;
3534 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3535 segment. */
3536 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
3537 if (eh_frame_hdr != NULL
3538 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
3540 amt = sizeof (struct elf_segment_map);
3541 m = bfd_zalloc (abfd, amt);
3542 if (m == NULL)
3543 goto error_return;
3544 m->next = NULL;
3545 m->p_type = PT_GNU_EH_FRAME;
3546 m->count = 1;
3547 m->sections[0] = eh_frame_hdr->output_section;
3549 *pm = m;
3550 pm = &m->next;
3553 if (elf_tdata (abfd)->stack_flags)
3555 amt = sizeof (struct elf_segment_map);
3556 m = bfd_zalloc (abfd, amt);
3557 if (m == NULL)
3558 goto error_return;
3559 m->next = NULL;
3560 m->p_type = PT_GNU_STACK;
3561 m->p_flags = elf_tdata (abfd)->stack_flags;
3562 m->p_flags_valid = 1;
3564 *pm = m;
3565 pm = &m->next;
3568 if (elf_tdata (abfd)->relro)
3570 amt = sizeof (struct elf_segment_map);
3571 m = bfd_zalloc (abfd, amt);
3572 if (m == NULL)
3573 goto error_return;
3574 m->next = NULL;
3575 m->p_type = PT_GNU_RELRO;
3576 m->p_flags = PF_R;
3577 m->p_flags_valid = 1;
3579 *pm = m;
3580 pm = &m->next;
3583 free (sections);
3584 sections = NULL;
3586 elf_tdata (abfd)->segment_map = mfirst;
3587 return TRUE;
3589 error_return:
3590 if (sections != NULL)
3591 free (sections);
3592 return FALSE;
3595 /* Sort sections by address. */
3597 static int
3598 elf_sort_sections (const void *arg1, const void *arg2)
3600 const asection *sec1 = *(const asection **) arg1;
3601 const asection *sec2 = *(const asection **) arg2;
3602 bfd_size_type size1, size2;
3604 /* Sort by LMA first, since this is the address used to
3605 place the section into a segment. */
3606 if (sec1->lma < sec2->lma)
3607 return -1;
3608 else if (sec1->lma > sec2->lma)
3609 return 1;
3611 /* Then sort by VMA. Normally the LMA and the VMA will be
3612 the same, and this will do nothing. */
3613 if (sec1->vma < sec2->vma)
3614 return -1;
3615 else if (sec1->vma > sec2->vma)
3616 return 1;
3618 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3620 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
3622 if (TOEND (sec1))
3624 if (TOEND (sec2))
3626 /* If the indicies are the same, do not return 0
3627 here, but continue to try the next comparison. */
3628 if (sec1->target_index - sec2->target_index != 0)
3629 return sec1->target_index - sec2->target_index;
3631 else
3632 return 1;
3634 else if (TOEND (sec2))
3635 return -1;
3637 #undef TOEND
3639 /* Sort by size, to put zero sized sections
3640 before others at the same address. */
3642 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
3643 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
3645 if (size1 < size2)
3646 return -1;
3647 if (size1 > size2)
3648 return 1;
3650 return sec1->target_index - sec2->target_index;
3653 /* Ian Lance Taylor writes:
3655 We shouldn't be using % with a negative signed number. That's just
3656 not good. We have to make sure either that the number is not
3657 negative, or that the number has an unsigned type. When the types
3658 are all the same size they wind up as unsigned. When file_ptr is a
3659 larger signed type, the arithmetic winds up as signed long long,
3660 which is wrong.
3662 What we're trying to say here is something like ``increase OFF by
3663 the least amount that will cause it to be equal to the VMA modulo
3664 the page size.'' */
3665 /* In other words, something like:
3667 vma_offset = m->sections[0]->vma % bed->maxpagesize;
3668 off_offset = off % bed->maxpagesize;
3669 if (vma_offset < off_offset)
3670 adjustment = vma_offset + bed->maxpagesize - off_offset;
3671 else
3672 adjustment = vma_offset - off_offset;
3674 which can can be collapsed into the expression below. */
3676 static file_ptr
3677 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
3679 return ((vma - off) % maxpagesize);
3682 /* Assign file positions to the sections based on the mapping from
3683 sections to segments. This function also sets up some fields in
3684 the file header, and writes out the program headers. */
3686 static bfd_boolean
3687 assign_file_positions_for_segments (bfd *abfd, struct bfd_link_info *link_info)
3689 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3690 unsigned int count;
3691 struct elf_segment_map *m;
3692 unsigned int alloc;
3693 Elf_Internal_Phdr *phdrs;
3694 file_ptr off, voff;
3695 bfd_vma filehdr_vaddr, filehdr_paddr;
3696 bfd_vma phdrs_vaddr, phdrs_paddr;
3697 Elf_Internal_Phdr *p;
3698 bfd_size_type amt;
3700 if (elf_tdata (abfd)->segment_map == NULL)
3702 if (! map_sections_to_segments (abfd))
3703 return FALSE;
3705 else
3707 /* The placement algorithm assumes that non allocated sections are
3708 not in PT_LOAD segments. We ensure this here by removing such
3709 sections from the segment map. */
3710 for (m = elf_tdata (abfd)->segment_map;
3711 m != NULL;
3712 m = m->next)
3714 unsigned int new_count;
3715 unsigned int i;
3717 if (m->p_type != PT_LOAD)
3718 continue;
3720 new_count = 0;
3721 for (i = 0; i < m->count; i ++)
3723 if ((m->sections[i]->flags & SEC_ALLOC) != 0)
3725 if (i != new_count)
3726 m->sections[new_count] = m->sections[i];
3728 new_count ++;
3732 if (new_count != m->count)
3733 m->count = new_count;
3737 if (bed->elf_backend_modify_segment_map)
3739 if (! (*bed->elf_backend_modify_segment_map) (abfd, link_info))
3740 return FALSE;
3743 count = 0;
3744 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3745 ++count;
3747 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
3748 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
3749 elf_elfheader (abfd)->e_phnum = count;
3751 if (count == 0)
3752 return TRUE;
3754 /* If we already counted the number of program segments, make sure
3755 that we allocated enough space. This happens when SIZEOF_HEADERS
3756 is used in a linker script. */
3757 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
3758 if (alloc != 0 && count > alloc)
3760 ((*_bfd_error_handler)
3761 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3762 bfd_get_filename (abfd), alloc, count));
3763 bfd_set_error (bfd_error_bad_value);
3764 return FALSE;
3767 if (alloc == 0)
3768 alloc = count;
3770 amt = alloc * sizeof (Elf_Internal_Phdr);
3771 phdrs = bfd_alloc (abfd, amt);
3772 if (phdrs == NULL)
3773 return FALSE;
3775 off = bed->s->sizeof_ehdr;
3776 off += alloc * bed->s->sizeof_phdr;
3778 filehdr_vaddr = 0;
3779 filehdr_paddr = 0;
3780 phdrs_vaddr = 0;
3781 phdrs_paddr = 0;
3783 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3784 m != NULL;
3785 m = m->next, p++)
3787 unsigned int i;
3788 asection **secpp;
3790 /* If elf_segment_map is not from map_sections_to_segments, the
3791 sections may not be correctly ordered. NOTE: sorting should
3792 not be done to the PT_NOTE section of a corefile, which may
3793 contain several pseudo-sections artificially created by bfd.
3794 Sorting these pseudo-sections breaks things badly. */
3795 if (m->count > 1
3796 && !(elf_elfheader (abfd)->e_type == ET_CORE
3797 && m->p_type == PT_NOTE))
3798 qsort (m->sections, (size_t) m->count, sizeof (asection *),
3799 elf_sort_sections);
3801 p->p_type = m->p_type;
3802 p->p_flags = m->p_flags;
3804 if (p->p_type == PT_LOAD
3805 && m->count > 0
3806 && (m->sections[0]->flags & SEC_ALLOC) != 0)
3808 if ((abfd->flags & D_PAGED) != 0)
3809 off += vma_page_aligned_bias (m->sections[0]->vma, off,
3810 bed->maxpagesize);
3811 else
3813 bfd_size_type align;
3815 align = 0;
3816 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3818 bfd_size_type secalign;
3820 secalign = bfd_get_section_alignment (abfd, *secpp);
3821 if (secalign > align)
3822 align = secalign;
3825 off += vma_page_aligned_bias (m->sections[0]->vma, off,
3826 1 << align);
3830 if (m->count == 0)
3831 p->p_vaddr = 0;
3832 else
3833 p->p_vaddr = m->sections[0]->vma;
3835 if (m->p_paddr_valid)
3836 p->p_paddr = m->p_paddr;
3837 else if (m->count == 0)
3838 p->p_paddr = 0;
3839 else
3840 p->p_paddr = m->sections[0]->lma;
3842 if (p->p_type == PT_LOAD
3843 && (abfd->flags & D_PAGED) != 0)
3844 p->p_align = bed->maxpagesize;
3845 else if (m->count == 0)
3846 p->p_align = 1 << bed->s->log_file_align;
3847 else
3848 p->p_align = 0;
3850 p->p_offset = 0;
3851 p->p_filesz = 0;
3852 p->p_memsz = 0;
3854 if (m->includes_filehdr)
3856 if (! m->p_flags_valid)
3857 p->p_flags |= PF_R;
3858 p->p_offset = 0;
3859 p->p_filesz = bed->s->sizeof_ehdr;
3860 p->p_memsz = bed->s->sizeof_ehdr;
3861 if (m->count > 0)
3863 BFD_ASSERT (p->p_type == PT_LOAD);
3865 if (p->p_vaddr < (bfd_vma) off)
3867 (*_bfd_error_handler)
3868 (_("%s: Not enough room for program headers, try linking with -N"),
3869 bfd_get_filename (abfd));
3870 bfd_set_error (bfd_error_bad_value);
3871 return FALSE;
3874 p->p_vaddr -= off;
3875 if (! m->p_paddr_valid)
3876 p->p_paddr -= off;
3878 if (p->p_type == PT_LOAD)
3880 filehdr_vaddr = p->p_vaddr;
3881 filehdr_paddr = p->p_paddr;
3885 if (m->includes_phdrs)
3887 if (! m->p_flags_valid)
3888 p->p_flags |= PF_R;
3890 if (m->includes_filehdr)
3892 if (p->p_type == PT_LOAD)
3894 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
3895 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
3898 else
3900 p->p_offset = bed->s->sizeof_ehdr;
3902 if (m->count > 0)
3904 BFD_ASSERT (p->p_type == PT_LOAD);
3905 p->p_vaddr -= off - p->p_offset;
3906 if (! m->p_paddr_valid)
3907 p->p_paddr -= off - p->p_offset;
3910 if (p->p_type == PT_LOAD)
3912 phdrs_vaddr = p->p_vaddr;
3913 phdrs_paddr = p->p_paddr;
3915 else
3916 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
3919 p->p_filesz += alloc * bed->s->sizeof_phdr;
3920 p->p_memsz += alloc * bed->s->sizeof_phdr;
3923 if (p->p_type == PT_LOAD
3924 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
3926 if (! m->includes_filehdr && ! m->includes_phdrs)
3927 p->p_offset = off;
3928 else
3930 file_ptr adjust;
3932 adjust = off - (p->p_offset + p->p_filesz);
3933 p->p_filesz += adjust;
3934 p->p_memsz += adjust;
3938 voff = off;
3940 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3942 asection *sec;
3943 flagword flags;
3944 bfd_size_type align;
3946 sec = *secpp;
3947 flags = sec->flags;
3948 align = 1 << bfd_get_section_alignment (abfd, sec);
3950 /* The section may have artificial alignment forced by a
3951 link script. Notice this case by the gap between the
3952 cumulative phdr lma and the section's lma. */
3953 if (p->p_paddr + p->p_memsz < sec->lma)
3955 bfd_vma adjust = sec->lma - (p->p_paddr + p->p_memsz);
3957 p->p_memsz += adjust;
3958 if (p->p_type == PT_LOAD
3959 || (p->p_type == PT_NOTE
3960 && bfd_get_format (abfd) == bfd_core))
3962 off += adjust;
3963 voff += adjust;
3965 if ((flags & SEC_LOAD) != 0
3966 || (flags & SEC_THREAD_LOCAL) != 0)
3967 p->p_filesz += adjust;
3970 if (p->p_type == PT_LOAD)
3972 bfd_signed_vma adjust;
3974 if ((flags & SEC_LOAD) != 0)
3976 adjust = sec->lma - (p->p_paddr + p->p_memsz);
3977 if (adjust < 0)
3978 adjust = 0;
3980 else if ((flags & SEC_ALLOC) != 0)
3982 /* The section VMA must equal the file position
3983 modulo the page size. FIXME: I'm not sure if
3984 this adjustment is really necessary. We used to
3985 not have the SEC_LOAD case just above, and then
3986 this was necessary, but now I'm not sure. */
3987 if ((abfd->flags & D_PAGED) != 0)
3988 adjust = vma_page_aligned_bias (sec->vma, voff,
3989 bed->maxpagesize);
3990 else
3991 adjust = vma_page_aligned_bias (sec->vma, voff,
3992 align);
3994 else
3995 adjust = 0;
3997 if (adjust != 0)
3999 if (i == 0)
4001 (* _bfd_error_handler) (_("\
4002 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
4003 bfd_section_name (abfd, sec),
4004 sec->lma,
4005 p->p_paddr);
4006 return FALSE;
4008 p->p_memsz += adjust;
4009 off += adjust;
4010 voff += adjust;
4011 if ((flags & SEC_LOAD) != 0)
4012 p->p_filesz += adjust;
4015 sec->filepos = off;
4017 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
4018 used in a linker script we may have a section with
4019 SEC_LOAD clear but which is supposed to have
4020 contents. */
4021 if ((flags & SEC_LOAD) != 0
4022 || (flags & SEC_HAS_CONTENTS) != 0)
4023 off += sec->size;
4025 if ((flags & SEC_ALLOC) != 0
4026 && ((flags & SEC_LOAD) != 0
4027 || (flags & SEC_THREAD_LOCAL) == 0))
4028 voff += sec->size;
4031 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4033 /* The actual "note" segment has i == 0.
4034 This is the one that actually contains everything. */
4035 if (i == 0)
4037 sec->filepos = off;
4038 p->p_filesz = sec->size;
4039 off += sec->size;
4040 voff = off;
4042 else
4044 /* Fake sections -- don't need to be written. */
4045 sec->filepos = 0;
4046 sec->size = 0;
4047 flags = sec->flags = 0;
4049 p->p_memsz = 0;
4050 p->p_align = 1;
4052 else
4054 if ((sec->flags & SEC_LOAD) != 0
4055 || (sec->flags & SEC_THREAD_LOCAL) == 0
4056 || p->p_type == PT_TLS)
4057 p->p_memsz += sec->size;
4059 if ((flags & SEC_LOAD) != 0)
4060 p->p_filesz += sec->size;
4062 if (p->p_type == PT_TLS
4063 && sec->size == 0
4064 && (sec->flags & SEC_HAS_CONTENTS) == 0)
4066 struct bfd_link_order *o;
4067 bfd_vma tbss_size = 0;
4069 for (o = sec->link_order_head; o != NULL; o = o->next)
4070 if (tbss_size < o->offset + o->size)
4071 tbss_size = o->offset + o->size;
4073 p->p_memsz += tbss_size;
4076 if (align > p->p_align
4077 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
4078 p->p_align = align;
4081 if (! m->p_flags_valid)
4083 p->p_flags |= PF_R;
4084 if ((flags & SEC_CODE) != 0)
4085 p->p_flags |= PF_X;
4086 if ((flags & SEC_READONLY) == 0)
4087 p->p_flags |= PF_W;
4092 /* Now that we have set the section file positions, we can set up
4093 the file positions for the non PT_LOAD segments. */
4094 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4095 m != NULL;
4096 m = m->next, p++)
4098 if (p->p_type != PT_LOAD && m->count > 0)
4100 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
4101 p->p_offset = m->sections[0]->filepos;
4103 if (m->count == 0)
4105 if (m->includes_filehdr)
4107 p->p_vaddr = filehdr_vaddr;
4108 if (! m->p_paddr_valid)
4109 p->p_paddr = filehdr_paddr;
4111 else if (m->includes_phdrs)
4113 p->p_vaddr = phdrs_vaddr;
4114 if (! m->p_paddr_valid)
4115 p->p_paddr = phdrs_paddr;
4117 else if (p->p_type == PT_GNU_RELRO)
4119 Elf_Internal_Phdr *lp;
4121 for (lp = phdrs; lp < phdrs + count; ++lp)
4123 if (lp->p_type == PT_LOAD
4124 && lp->p_vaddr <= link_info->relro_end
4125 && lp->p_vaddr >= link_info->relro_start
4126 && lp->p_vaddr + lp->p_filesz
4127 >= link_info->relro_end)
4128 break;
4131 if (lp < phdrs + count
4132 && link_info->relro_end > lp->p_vaddr)
4134 p->p_vaddr = lp->p_vaddr;
4135 p->p_paddr = lp->p_paddr;
4136 p->p_offset = lp->p_offset;
4137 p->p_filesz = link_info->relro_end - lp->p_vaddr;
4138 p->p_memsz = p->p_filesz;
4139 p->p_align = 1;
4140 p->p_flags = (lp->p_flags & ~PF_W);
4142 else
4144 memset (p, 0, sizeof *p);
4145 p->p_type = PT_NULL;
4151 /* Clear out any program headers we allocated but did not use. */
4152 for (; count < alloc; count++, p++)
4154 memset (p, 0, sizeof *p);
4155 p->p_type = PT_NULL;
4158 elf_tdata (abfd)->phdr = phdrs;
4160 elf_tdata (abfd)->next_file_pos = off;
4162 /* Write out the program headers. */
4163 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4164 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
4165 return FALSE;
4167 return TRUE;
4170 /* Get the size of the program header.
4172 If this is called by the linker before any of the section VMA's are set, it
4173 can't calculate the correct value for a strange memory layout. This only
4174 happens when SIZEOF_HEADERS is used in a linker script. In this case,
4175 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
4176 data segment (exclusive of .interp and .dynamic).
4178 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
4179 will be two segments. */
4181 static bfd_size_type
4182 get_program_header_size (bfd *abfd)
4184 size_t segs;
4185 asection *s;
4186 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4188 /* We can't return a different result each time we're called. */
4189 if (elf_tdata (abfd)->program_header_size != 0)
4190 return elf_tdata (abfd)->program_header_size;
4192 if (elf_tdata (abfd)->segment_map != NULL)
4194 struct elf_segment_map *m;
4196 segs = 0;
4197 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4198 ++segs;
4199 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
4200 return elf_tdata (abfd)->program_header_size;
4203 /* Assume we will need exactly two PT_LOAD segments: one for text
4204 and one for data. */
4205 segs = 2;
4207 s = bfd_get_section_by_name (abfd, ".interp");
4208 if (s != NULL && (s->flags & SEC_LOAD) != 0)
4210 /* If we have a loadable interpreter section, we need a
4211 PT_INTERP segment. In this case, assume we also need a
4212 PT_PHDR segment, although that may not be true for all
4213 targets. */
4214 segs += 2;
4217 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4219 /* We need a PT_DYNAMIC segment. */
4220 ++segs;
4223 if (elf_tdata (abfd)->eh_frame_hdr)
4225 /* We need a PT_GNU_EH_FRAME segment. */
4226 ++segs;
4229 if (elf_tdata (abfd)->stack_flags)
4231 /* We need a PT_GNU_STACK segment. */
4232 ++segs;
4235 if (elf_tdata (abfd)->relro)
4237 /* We need a PT_GNU_RELRO segment. */
4238 ++segs;
4241 for (s = abfd->sections; s != NULL; s = s->next)
4243 if ((s->flags & SEC_LOAD) != 0
4244 && strncmp (s->name, ".note", 5) == 0)
4246 /* We need a PT_NOTE segment. */
4247 ++segs;
4251 for (s = abfd->sections; s != NULL; s = s->next)
4253 if (s->flags & SEC_THREAD_LOCAL)
4255 /* We need a PT_TLS segment. */
4256 ++segs;
4257 break;
4261 /* Let the backend count up any program headers it might need. */
4262 if (bed->elf_backend_additional_program_headers)
4264 int a;
4266 a = (*bed->elf_backend_additional_program_headers) (abfd);
4267 if (a == -1)
4268 abort ();
4269 segs += a;
4272 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
4273 return elf_tdata (abfd)->program_header_size;
4276 /* Work out the file positions of all the sections. This is called by
4277 _bfd_elf_compute_section_file_positions. All the section sizes and
4278 VMAs must be known before this is called.
4280 We do not consider reloc sections at this point, unless they form
4281 part of the loadable image. Reloc sections are assigned file
4282 positions in assign_file_positions_for_relocs, which is called by
4283 write_object_contents and final_link.
4285 We also don't set the positions of the .symtab and .strtab here. */
4287 static bfd_boolean
4288 assign_file_positions_except_relocs (bfd *abfd,
4289 struct bfd_link_info *link_info)
4291 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
4292 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
4293 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4294 unsigned int num_sec = elf_numsections (abfd);
4295 file_ptr off;
4296 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4298 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4299 && bfd_get_format (abfd) != bfd_core)
4301 Elf_Internal_Shdr **hdrpp;
4302 unsigned int i;
4304 /* Start after the ELF header. */
4305 off = i_ehdrp->e_ehsize;
4307 /* We are not creating an executable, which means that we are
4308 not creating a program header, and that the actual order of
4309 the sections in the file is unimportant. */
4310 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4312 Elf_Internal_Shdr *hdr;
4314 hdr = *hdrpp;
4315 if (hdr->sh_type == SHT_REL
4316 || hdr->sh_type == SHT_RELA
4317 || i == tdata->symtab_section
4318 || i == tdata->symtab_shndx_section
4319 || i == tdata->strtab_section)
4321 hdr->sh_offset = -1;
4323 else
4324 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4326 if (i == SHN_LORESERVE - 1)
4328 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4329 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4333 else
4335 unsigned int i;
4336 Elf_Internal_Shdr **hdrpp;
4338 /* Assign file positions for the loaded sections based on the
4339 assignment of sections to segments. */
4340 if (! assign_file_positions_for_segments (abfd, link_info))
4341 return FALSE;
4343 /* Assign file positions for the other sections. */
4345 off = elf_tdata (abfd)->next_file_pos;
4346 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4348 Elf_Internal_Shdr *hdr;
4350 hdr = *hdrpp;
4351 if (hdr->bfd_section != NULL
4352 && hdr->bfd_section->filepos != 0)
4353 hdr->sh_offset = hdr->bfd_section->filepos;
4354 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4356 ((*_bfd_error_handler)
4357 (_("%s: warning: allocated section `%s' not in segment"),
4358 bfd_get_filename (abfd),
4359 (hdr->bfd_section == NULL
4360 ? "*unknown*"
4361 : hdr->bfd_section->name)));
4362 if ((abfd->flags & D_PAGED) != 0)
4363 off += vma_page_aligned_bias (hdr->sh_addr, off,
4364 bed->maxpagesize);
4365 else
4366 off += vma_page_aligned_bias (hdr->sh_addr, off,
4367 hdr->sh_addralign);
4368 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4369 FALSE);
4371 else if (hdr->sh_type == SHT_REL
4372 || hdr->sh_type == SHT_RELA
4373 || hdr == i_shdrpp[tdata->symtab_section]
4374 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4375 || hdr == i_shdrpp[tdata->strtab_section])
4376 hdr->sh_offset = -1;
4377 else
4378 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4380 if (i == SHN_LORESERVE - 1)
4382 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4383 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4388 /* Place the section headers. */
4389 off = align_file_position (off, 1 << bed->s->log_file_align);
4390 i_ehdrp->e_shoff = off;
4391 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4393 elf_tdata (abfd)->next_file_pos = off;
4395 return TRUE;
4398 static bfd_boolean
4399 prep_headers (bfd *abfd)
4401 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
4402 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4403 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
4404 struct elf_strtab_hash *shstrtab;
4405 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4407 i_ehdrp = elf_elfheader (abfd);
4408 i_shdrp = elf_elfsections (abfd);
4410 shstrtab = _bfd_elf_strtab_init ();
4411 if (shstrtab == NULL)
4412 return FALSE;
4414 elf_shstrtab (abfd) = shstrtab;
4416 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4417 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4418 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4419 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4421 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4422 i_ehdrp->e_ident[EI_DATA] =
4423 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4424 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4426 if ((abfd->flags & DYNAMIC) != 0)
4427 i_ehdrp->e_type = ET_DYN;
4428 else if ((abfd->flags & EXEC_P) != 0)
4429 i_ehdrp->e_type = ET_EXEC;
4430 else if (bfd_get_format (abfd) == bfd_core)
4431 i_ehdrp->e_type = ET_CORE;
4432 else
4433 i_ehdrp->e_type = ET_REL;
4435 switch (bfd_get_arch (abfd))
4437 case bfd_arch_unknown:
4438 i_ehdrp->e_machine = EM_NONE;
4439 break;
4441 /* There used to be a long list of cases here, each one setting
4442 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4443 in the corresponding bfd definition. To avoid duplication,
4444 the switch was removed. Machines that need special handling
4445 can generally do it in elf_backend_final_write_processing(),
4446 unless they need the information earlier than the final write.
4447 Such need can generally be supplied by replacing the tests for
4448 e_machine with the conditions used to determine it. */
4449 default:
4450 i_ehdrp->e_machine = bed->elf_machine_code;
4453 i_ehdrp->e_version = bed->s->ev_current;
4454 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4456 /* No program header, for now. */
4457 i_ehdrp->e_phoff = 0;
4458 i_ehdrp->e_phentsize = 0;
4459 i_ehdrp->e_phnum = 0;
4461 /* Each bfd section is section header entry. */
4462 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4463 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4465 /* If we're building an executable, we'll need a program header table. */
4466 if (abfd->flags & EXEC_P)
4468 /* It all happens later. */
4469 #if 0
4470 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
4472 /* elf_build_phdrs() returns a (NULL-terminated) array of
4473 Elf_Internal_Phdrs. */
4474 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
4475 i_ehdrp->e_phoff = outbase;
4476 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
4477 #endif
4479 else
4481 i_ehdrp->e_phentsize = 0;
4482 i_phdrp = 0;
4483 i_ehdrp->e_phoff = 0;
4486 elf_tdata (abfd)->symtab_hdr.sh_name =
4487 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
4488 elf_tdata (abfd)->strtab_hdr.sh_name =
4489 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
4490 elf_tdata (abfd)->shstrtab_hdr.sh_name =
4491 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
4492 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4493 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4494 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4495 return FALSE;
4497 return TRUE;
4500 /* Assign file positions for all the reloc sections which are not part
4501 of the loadable file image. */
4503 void
4504 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
4506 file_ptr off;
4507 unsigned int i, num_sec;
4508 Elf_Internal_Shdr **shdrpp;
4510 off = elf_tdata (abfd)->next_file_pos;
4512 num_sec = elf_numsections (abfd);
4513 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
4515 Elf_Internal_Shdr *shdrp;
4517 shdrp = *shdrpp;
4518 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4519 && shdrp->sh_offset == -1)
4520 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
4523 elf_tdata (abfd)->next_file_pos = off;
4526 bfd_boolean
4527 _bfd_elf_write_object_contents (bfd *abfd)
4529 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4530 Elf_Internal_Ehdr *i_ehdrp;
4531 Elf_Internal_Shdr **i_shdrp;
4532 bfd_boolean failed;
4533 unsigned int count, num_sec;
4535 if (! abfd->output_has_begun
4536 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
4537 return FALSE;
4539 i_shdrp = elf_elfsections (abfd);
4540 i_ehdrp = elf_elfheader (abfd);
4542 failed = FALSE;
4543 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4544 if (failed)
4545 return FALSE;
4547 _bfd_elf_assign_file_positions_for_relocs (abfd);
4549 /* After writing the headers, we need to write the sections too... */
4550 num_sec = elf_numsections (abfd);
4551 for (count = 1; count < num_sec; count++)
4553 if (bed->elf_backend_section_processing)
4554 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4555 if (i_shdrp[count]->contents)
4557 bfd_size_type amt = i_shdrp[count]->sh_size;
4559 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
4560 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
4561 return FALSE;
4563 if (count == SHN_LORESERVE - 1)
4564 count += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4567 /* Write out the section header names. */
4568 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
4569 || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))
4570 return FALSE;
4572 if (bed->elf_backend_final_write_processing)
4573 (*bed->elf_backend_final_write_processing) (abfd,
4574 elf_tdata (abfd)->linker);
4576 return bed->s->write_shdrs_and_ehdr (abfd);
4579 bfd_boolean
4580 _bfd_elf_write_corefile_contents (bfd *abfd)
4582 /* Hopefully this can be done just like an object file. */
4583 return _bfd_elf_write_object_contents (abfd);
4586 /* Given a section, search the header to find them. */
4589 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
4591 const struct elf_backend_data *bed;
4592 int index;
4594 if (elf_section_data (asect) != NULL
4595 && elf_section_data (asect)->this_idx != 0)
4596 return elf_section_data (asect)->this_idx;
4598 if (bfd_is_abs_section (asect))
4599 index = SHN_ABS;
4600 else if (bfd_is_com_section (asect))
4601 index = SHN_COMMON;
4602 else if (bfd_is_und_section (asect))
4603 index = SHN_UNDEF;
4604 else
4606 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
4607 int maxindex = elf_numsections (abfd);
4609 for (index = 1; index < maxindex; index++)
4611 Elf_Internal_Shdr *hdr = i_shdrp[index];
4613 if (hdr != NULL && hdr->bfd_section == asect)
4614 return index;
4616 index = -1;
4619 bed = get_elf_backend_data (abfd);
4620 if (bed->elf_backend_section_from_bfd_section)
4622 int retval = index;
4624 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
4625 return retval;
4628 if (index == -1)
4629 bfd_set_error (bfd_error_nonrepresentable_section);
4631 return index;
4634 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4635 on error. */
4638 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
4640 asymbol *asym_ptr = *asym_ptr_ptr;
4641 int idx;
4642 flagword flags = asym_ptr->flags;
4644 /* When gas creates relocations against local labels, it creates its
4645 own symbol for the section, but does put the symbol into the
4646 symbol chain, so udata is 0. When the linker is generating
4647 relocatable output, this section symbol may be for one of the
4648 input sections rather than the output section. */
4649 if (asym_ptr->udata.i == 0
4650 && (flags & BSF_SECTION_SYM)
4651 && asym_ptr->section)
4653 int indx;
4655 if (asym_ptr->section->output_section != NULL)
4656 indx = asym_ptr->section->output_section->index;
4657 else
4658 indx = asym_ptr->section->index;
4659 if (indx < elf_num_section_syms (abfd)
4660 && elf_section_syms (abfd)[indx] != NULL)
4661 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
4664 idx = asym_ptr->udata.i;
4666 if (idx == 0)
4668 /* This case can occur when using --strip-symbol on a symbol
4669 which is used in a relocation entry. */
4670 (*_bfd_error_handler)
4671 (_("%s: symbol `%s' required but not present"),
4672 bfd_archive_filename (abfd), bfd_asymbol_name (asym_ptr));
4673 bfd_set_error (bfd_error_no_symbols);
4674 return -1;
4677 #if DEBUG & 4
4679 fprintf (stderr,
4680 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
4681 (long) asym_ptr, asym_ptr->name, idx, flags,
4682 elf_symbol_flags (flags));
4683 fflush (stderr);
4685 #endif
4687 return idx;
4690 /* Copy private BFD data. This copies any program header information. */
4692 static bfd_boolean
4693 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
4695 Elf_Internal_Ehdr *iehdr;
4696 struct elf_segment_map *map;
4697 struct elf_segment_map *map_first;
4698 struct elf_segment_map **pointer_to_map;
4699 Elf_Internal_Phdr *segment;
4700 asection *section;
4701 unsigned int i;
4702 unsigned int num_segments;
4703 bfd_boolean phdr_included = FALSE;
4704 bfd_vma maxpagesize;
4705 struct elf_segment_map *phdr_adjust_seg = NULL;
4706 unsigned int phdr_adjust_num = 0;
4707 const struct elf_backend_data *bed;
4709 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4710 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4711 return TRUE;
4713 if (elf_tdata (ibfd)->phdr == NULL)
4714 return TRUE;
4716 bed = get_elf_backend_data (ibfd);
4717 iehdr = elf_elfheader (ibfd);
4719 map_first = NULL;
4720 pointer_to_map = &map_first;
4722 num_segments = elf_elfheader (ibfd)->e_phnum;
4723 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
4725 /* Returns the end address of the segment + 1. */
4726 #define SEGMENT_END(segment, start) \
4727 (start + (segment->p_memsz > segment->p_filesz \
4728 ? segment->p_memsz : segment->p_filesz))
4730 #define SECTION_SIZE(section, segment) \
4731 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
4732 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
4733 ? section->size : 0)
4735 /* Returns TRUE if the given section is contained within
4736 the given segment. VMA addresses are compared. */
4737 #define IS_CONTAINED_BY_VMA(section, segment) \
4738 (section->vma >= segment->p_vaddr \
4739 && (section->vma + SECTION_SIZE (section, segment) \
4740 <= (SEGMENT_END (segment, segment->p_vaddr))))
4742 /* Returns TRUE if the given section is contained within
4743 the given segment. LMA addresses are compared. */
4744 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4745 (section->lma >= base \
4746 && (section->lma + SECTION_SIZE (section, segment) \
4747 <= SEGMENT_END (segment, base)))
4749 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4750 #define IS_COREFILE_NOTE(p, s) \
4751 (p->p_type == PT_NOTE \
4752 && bfd_get_format (ibfd) == bfd_core \
4753 && s->vma == 0 && s->lma == 0 \
4754 && (bfd_vma) s->filepos >= p->p_offset \
4755 && ((bfd_vma) s->filepos + s->size \
4756 <= p->p_offset + p->p_filesz))
4758 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4759 linker, which generates a PT_INTERP section with p_vaddr and
4760 p_memsz set to 0. */
4761 #define IS_SOLARIS_PT_INTERP(p, s) \
4762 (p->p_vaddr == 0 \
4763 && p->p_paddr == 0 \
4764 && p->p_memsz == 0 \
4765 && p->p_filesz > 0 \
4766 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4767 && s->size > 0 \
4768 && (bfd_vma) s->filepos >= p->p_offset \
4769 && ((bfd_vma) s->filepos + s->size \
4770 <= p->p_offset + p->p_filesz))
4772 /* Decide if the given section should be included in the given segment.
4773 A section will be included if:
4774 1. It is within the address space of the segment -- we use the LMA
4775 if that is set for the segment and the VMA otherwise,
4776 2. It is an allocated segment,
4777 3. There is an output section associated with it,
4778 4. The section has not already been allocated to a previous segment.
4779 5. PT_GNU_STACK segments do not include any sections.
4780 6. PT_TLS segment includes only SHF_TLS sections.
4781 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. */
4782 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
4783 ((((segment->p_paddr \
4784 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4785 : IS_CONTAINED_BY_VMA (section, segment)) \
4786 && (section->flags & SEC_ALLOC) != 0) \
4787 || IS_COREFILE_NOTE (segment, section)) \
4788 && section->output_section != NULL \
4789 && segment->p_type != PT_GNU_STACK \
4790 && (segment->p_type != PT_TLS \
4791 || (section->flags & SEC_THREAD_LOCAL)) \
4792 && (segment->p_type == PT_LOAD \
4793 || segment->p_type == PT_TLS \
4794 || (section->flags & SEC_THREAD_LOCAL) == 0) \
4795 && ! section->segment_mark)
4797 /* Returns TRUE iff seg1 starts after the end of seg2. */
4798 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
4799 (seg1->field >= SEGMENT_END (seg2, seg2->field))
4801 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
4802 their VMA address ranges and their LMA address ranges overlap.
4803 It is possible to have overlapping VMA ranges without overlapping LMA
4804 ranges. RedBoot images for example can have both .data and .bss mapped
4805 to the same VMA range, but with the .data section mapped to a different
4806 LMA. */
4807 #define SEGMENT_OVERLAPS(seg1, seg2) \
4808 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
4809 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
4810 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
4811 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
4813 /* Initialise the segment mark field. */
4814 for (section = ibfd->sections; section != NULL; section = section->next)
4815 section->segment_mark = FALSE;
4817 /* Scan through the segments specified in the program header
4818 of the input BFD. For this first scan we look for overlaps
4819 in the loadable segments. These can be created by weird
4820 parameters to objcopy. Also, fix some solaris weirdness. */
4821 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4822 i < num_segments;
4823 i++, segment++)
4825 unsigned int j;
4826 Elf_Internal_Phdr *segment2;
4828 if (segment->p_type == PT_INTERP)
4829 for (section = ibfd->sections; section; section = section->next)
4830 if (IS_SOLARIS_PT_INTERP (segment, section))
4832 /* Mininal change so that the normal section to segment
4833 assignment code will work. */
4834 segment->p_vaddr = section->vma;
4835 break;
4838 if (segment->p_type != PT_LOAD)
4839 continue;
4841 /* Determine if this segment overlaps any previous segments. */
4842 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
4844 bfd_signed_vma extra_length;
4846 if (segment2->p_type != PT_LOAD
4847 || ! SEGMENT_OVERLAPS (segment, segment2))
4848 continue;
4850 /* Merge the two segments together. */
4851 if (segment2->p_vaddr < segment->p_vaddr)
4853 /* Extend SEGMENT2 to include SEGMENT and then delete
4854 SEGMENT. */
4855 extra_length =
4856 SEGMENT_END (segment, segment->p_vaddr)
4857 - SEGMENT_END (segment2, segment2->p_vaddr);
4859 if (extra_length > 0)
4861 segment2->p_memsz += extra_length;
4862 segment2->p_filesz += extra_length;
4865 segment->p_type = PT_NULL;
4867 /* Since we have deleted P we must restart the outer loop. */
4868 i = 0;
4869 segment = elf_tdata (ibfd)->phdr;
4870 break;
4872 else
4874 /* Extend SEGMENT to include SEGMENT2 and then delete
4875 SEGMENT2. */
4876 extra_length =
4877 SEGMENT_END (segment2, segment2->p_vaddr)
4878 - SEGMENT_END (segment, segment->p_vaddr);
4880 if (extra_length > 0)
4882 segment->p_memsz += extra_length;
4883 segment->p_filesz += extra_length;
4886 segment2->p_type = PT_NULL;
4891 /* The second scan attempts to assign sections to segments. */
4892 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4893 i < num_segments;
4894 i ++, segment ++)
4896 unsigned int section_count;
4897 asection ** sections;
4898 asection * output_section;
4899 unsigned int isec;
4900 bfd_vma matching_lma;
4901 bfd_vma suggested_lma;
4902 unsigned int j;
4903 bfd_size_type amt;
4905 if (segment->p_type == PT_NULL)
4906 continue;
4908 /* Compute how many sections might be placed into this segment. */
4909 for (section = ibfd->sections, section_count = 0;
4910 section != NULL;
4911 section = section->next)
4912 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
4913 ++section_count;
4915 /* Allocate a segment map big enough to contain
4916 all of the sections we have selected. */
4917 amt = sizeof (struct elf_segment_map);
4918 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
4919 map = bfd_alloc (obfd, amt);
4920 if (map == NULL)
4921 return FALSE;
4923 /* Initialise the fields of the segment map. Default to
4924 using the physical address of the segment in the input BFD. */
4925 map->next = NULL;
4926 map->p_type = segment->p_type;
4927 map->p_flags = segment->p_flags;
4928 map->p_flags_valid = 1;
4929 map->p_paddr = segment->p_paddr;
4930 map->p_paddr_valid = 1;
4932 /* Determine if this segment contains the ELF file header
4933 and if it contains the program headers themselves. */
4934 map->includes_filehdr = (segment->p_offset == 0
4935 && segment->p_filesz >= iehdr->e_ehsize);
4937 map->includes_phdrs = 0;
4939 if (! phdr_included || segment->p_type != PT_LOAD)
4941 map->includes_phdrs =
4942 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
4943 && (segment->p_offset + segment->p_filesz
4944 >= ((bfd_vma) iehdr->e_phoff
4945 + iehdr->e_phnum * iehdr->e_phentsize)));
4947 if (segment->p_type == PT_LOAD && map->includes_phdrs)
4948 phdr_included = TRUE;
4951 if (section_count == 0)
4953 /* Special segments, such as the PT_PHDR segment, may contain
4954 no sections, but ordinary, loadable segments should contain
4955 something. They are allowed by the ELF spec however, so only
4956 a warning is produced. */
4957 if (segment->p_type == PT_LOAD)
4958 (*_bfd_error_handler)
4959 (_("%s: warning: Empty loadable segment detected, is this intentional ?\n"),
4960 bfd_archive_filename (ibfd));
4962 map->count = 0;
4963 *pointer_to_map = map;
4964 pointer_to_map = &map->next;
4966 continue;
4969 /* Now scan the sections in the input BFD again and attempt
4970 to add their corresponding output sections to the segment map.
4971 The problem here is how to handle an output section which has
4972 been moved (ie had its LMA changed). There are four possibilities:
4974 1. None of the sections have been moved.
4975 In this case we can continue to use the segment LMA from the
4976 input BFD.
4978 2. All of the sections have been moved by the same amount.
4979 In this case we can change the segment's LMA to match the LMA
4980 of the first section.
4982 3. Some of the sections have been moved, others have not.
4983 In this case those sections which have not been moved can be
4984 placed in the current segment which will have to have its size,
4985 and possibly its LMA changed, and a new segment or segments will
4986 have to be created to contain the other sections.
4988 4. The sections have been moved, but not by the same amount.
4989 In this case we can change the segment's LMA to match the LMA
4990 of the first section and we will have to create a new segment
4991 or segments to contain the other sections.
4993 In order to save time, we allocate an array to hold the section
4994 pointers that we are interested in. As these sections get assigned
4995 to a segment, they are removed from this array. */
4997 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
4998 to work around this long long bug. */
4999 amt = section_count * sizeof (asection *);
5000 sections = bfd_malloc (amt);
5001 if (sections == NULL)
5002 return FALSE;
5004 /* Step One: Scan for segment vs section LMA conflicts.
5005 Also add the sections to the section array allocated above.
5006 Also add the sections to the current segment. In the common
5007 case, where the sections have not been moved, this means that
5008 we have completely filled the segment, and there is nothing
5009 more to do. */
5010 isec = 0;
5011 matching_lma = 0;
5012 suggested_lma = 0;
5014 for (j = 0, section = ibfd->sections;
5015 section != NULL;
5016 section = section->next)
5018 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5020 output_section = section->output_section;
5022 sections[j ++] = section;
5024 /* The Solaris native linker always sets p_paddr to 0.
5025 We try to catch that case here, and set it to the
5026 correct value. Note - some backends require that
5027 p_paddr be left as zero. */
5028 if (segment->p_paddr == 0
5029 && segment->p_vaddr != 0
5030 && (! bed->want_p_paddr_set_to_zero)
5031 && isec == 0
5032 && output_section->lma != 0
5033 && (output_section->vma == (segment->p_vaddr
5034 + (map->includes_filehdr
5035 ? iehdr->e_ehsize
5036 : 0)
5037 + (map->includes_phdrs
5038 ? (iehdr->e_phnum
5039 * iehdr->e_phentsize)
5040 : 0))))
5041 map->p_paddr = segment->p_vaddr;
5043 /* Match up the physical address of the segment with the
5044 LMA address of the output section. */
5045 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5046 || IS_COREFILE_NOTE (segment, section)
5047 || (bed->want_p_paddr_set_to_zero &&
5048 IS_CONTAINED_BY_VMA (output_section, segment))
5051 if (matching_lma == 0)
5052 matching_lma = output_section->lma;
5054 /* We assume that if the section fits within the segment
5055 then it does not overlap any other section within that
5056 segment. */
5057 map->sections[isec ++] = output_section;
5059 else if (suggested_lma == 0)
5060 suggested_lma = output_section->lma;
5064 BFD_ASSERT (j == section_count);
5066 /* Step Two: Adjust the physical address of the current segment,
5067 if necessary. */
5068 if (isec == section_count)
5070 /* All of the sections fitted within the segment as currently
5071 specified. This is the default case. Add the segment to
5072 the list of built segments and carry on to process the next
5073 program header in the input BFD. */
5074 map->count = section_count;
5075 *pointer_to_map = map;
5076 pointer_to_map = &map->next;
5078 free (sections);
5079 continue;
5081 else
5083 if (matching_lma != 0)
5085 /* At least one section fits inside the current segment.
5086 Keep it, but modify its physical address to match the
5087 LMA of the first section that fitted. */
5088 map->p_paddr = matching_lma;
5090 else
5092 /* None of the sections fitted inside the current segment.
5093 Change the current segment's physical address to match
5094 the LMA of the first section. */
5095 map->p_paddr = suggested_lma;
5098 /* Offset the segment physical address from the lma
5099 to allow for space taken up by elf headers. */
5100 if (map->includes_filehdr)
5101 map->p_paddr -= iehdr->e_ehsize;
5103 if (map->includes_phdrs)
5105 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5107 /* iehdr->e_phnum is just an estimate of the number
5108 of program headers that we will need. Make a note
5109 here of the number we used and the segment we chose
5110 to hold these headers, so that we can adjust the
5111 offset when we know the correct value. */
5112 phdr_adjust_num = iehdr->e_phnum;
5113 phdr_adjust_seg = map;
5117 /* Step Three: Loop over the sections again, this time assigning
5118 those that fit to the current segment and removing them from the
5119 sections array; but making sure not to leave large gaps. Once all
5120 possible sections have been assigned to the current segment it is
5121 added to the list of built segments and if sections still remain
5122 to be assigned, a new segment is constructed before repeating
5123 the loop. */
5124 isec = 0;
5127 map->count = 0;
5128 suggested_lma = 0;
5130 /* Fill the current segment with sections that fit. */
5131 for (j = 0; j < section_count; j++)
5133 section = sections[j];
5135 if (section == NULL)
5136 continue;
5138 output_section = section->output_section;
5140 BFD_ASSERT (output_section != NULL);
5142 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5143 || IS_COREFILE_NOTE (segment, section))
5145 if (map->count == 0)
5147 /* If the first section in a segment does not start at
5148 the beginning of the segment, then something is
5149 wrong. */
5150 if (output_section->lma !=
5151 (map->p_paddr
5152 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5153 + (map->includes_phdrs
5154 ? iehdr->e_phnum * iehdr->e_phentsize
5155 : 0)))
5156 abort ();
5158 else
5160 asection * prev_sec;
5162 prev_sec = map->sections[map->count - 1];
5164 /* If the gap between the end of the previous section
5165 and the start of this section is more than
5166 maxpagesize then we need to start a new segment. */
5167 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
5168 maxpagesize)
5169 < BFD_ALIGN (output_section->lma, maxpagesize))
5170 || ((prev_sec->lma + prev_sec->size)
5171 > output_section->lma))
5173 if (suggested_lma == 0)
5174 suggested_lma = output_section->lma;
5176 continue;
5180 map->sections[map->count++] = output_section;
5181 ++isec;
5182 sections[j] = NULL;
5183 section->segment_mark = TRUE;
5185 else if (suggested_lma == 0)
5186 suggested_lma = output_section->lma;
5189 BFD_ASSERT (map->count > 0);
5191 /* Add the current segment to the list of built segments. */
5192 *pointer_to_map = map;
5193 pointer_to_map = &map->next;
5195 if (isec < section_count)
5197 /* We still have not allocated all of the sections to
5198 segments. Create a new segment here, initialise it
5199 and carry on looping. */
5200 amt = sizeof (struct elf_segment_map);
5201 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5202 map = bfd_alloc (obfd, amt);
5203 if (map == NULL)
5205 free (sections);
5206 return FALSE;
5209 /* Initialise the fields of the segment map. Set the physical
5210 physical address to the LMA of the first section that has
5211 not yet been assigned. */
5212 map->next = NULL;
5213 map->p_type = segment->p_type;
5214 map->p_flags = segment->p_flags;
5215 map->p_flags_valid = 1;
5216 map->p_paddr = suggested_lma;
5217 map->p_paddr_valid = 1;
5218 map->includes_filehdr = 0;
5219 map->includes_phdrs = 0;
5222 while (isec < section_count);
5224 free (sections);
5227 /* The Solaris linker creates program headers in which all the
5228 p_paddr fields are zero. When we try to objcopy or strip such a
5229 file, we get confused. Check for this case, and if we find it
5230 reset the p_paddr_valid fields. */
5231 for (map = map_first; map != NULL; map = map->next)
5232 if (map->p_paddr != 0)
5233 break;
5234 if (map == NULL)
5235 for (map = map_first; map != NULL; map = map->next)
5236 map->p_paddr_valid = 0;
5238 elf_tdata (obfd)->segment_map = map_first;
5240 /* If we had to estimate the number of program headers that were
5241 going to be needed, then check our estimate now and adjust
5242 the offset if necessary. */
5243 if (phdr_adjust_seg != NULL)
5245 unsigned int count;
5247 for (count = 0, map = map_first; map != NULL; map = map->next)
5248 count++;
5250 if (count > phdr_adjust_num)
5251 phdr_adjust_seg->p_paddr
5252 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5255 #if 0
5256 /* Final Step: Sort the segments into ascending order of physical
5257 address. */
5258 if (map_first != NULL)
5260 struct elf_segment_map *prev;
5262 prev = map_first;
5263 for (map = map_first->next; map != NULL; prev = map, map = map->next)
5265 /* Yes I know - its a bubble sort.... */
5266 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
5268 /* Swap map and map->next. */
5269 prev->next = map->next;
5270 map->next = map->next->next;
5271 prev->next->next = map;
5273 /* Restart loop. */
5274 map = map_first;
5278 #endif
5280 #undef SEGMENT_END
5281 #undef SECTION_SIZE
5282 #undef IS_CONTAINED_BY_VMA
5283 #undef IS_CONTAINED_BY_LMA
5284 #undef IS_COREFILE_NOTE
5285 #undef IS_SOLARIS_PT_INTERP
5286 #undef INCLUDE_SECTION_IN_SEGMENT
5287 #undef SEGMENT_AFTER_SEGMENT
5288 #undef SEGMENT_OVERLAPS
5289 return TRUE;
5292 /* Copy private section information. This copies over the entsize
5293 field, and sometimes the info field. */
5295 bfd_boolean
5296 _bfd_elf_copy_private_section_data (bfd *ibfd,
5297 asection *isec,
5298 bfd *obfd,
5299 asection *osec)
5301 Elf_Internal_Shdr *ihdr, *ohdr;
5303 if (ibfd->xvec->flavour != bfd_target_elf_flavour
5304 || obfd->xvec->flavour != bfd_target_elf_flavour)
5305 return TRUE;
5307 ihdr = &elf_section_data (isec)->this_hdr;
5308 ohdr = &elf_section_data (osec)->this_hdr;
5310 ohdr->sh_entsize = ihdr->sh_entsize;
5312 if (ihdr->sh_type == SHT_SYMTAB
5313 || ihdr->sh_type == SHT_DYNSYM
5314 || ihdr->sh_type == SHT_GNU_verneed
5315 || ihdr->sh_type == SHT_GNU_verdef)
5316 ohdr->sh_info = ihdr->sh_info;
5318 /* Set things up for objcopy. The output SHT_GROUP section will
5319 have its elf_next_in_group pointing back to the input group
5320 members. */
5321 elf_next_in_group (osec) = elf_next_in_group (isec);
5322 elf_group_name (osec) = elf_group_name (isec);
5324 osec->use_rela_p = isec->use_rela_p;
5326 return TRUE;
5329 /* Copy private header information. */
5331 bfd_boolean
5332 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
5334 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5335 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5336 return TRUE;
5338 /* Copy over private BFD data if it has not already been copied.
5339 This must be done here, rather than in the copy_private_bfd_data
5340 entry point, because the latter is called after the section
5341 contents have been set, which means that the program headers have
5342 already been worked out. */
5343 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
5345 if (! copy_private_bfd_data (ibfd, obfd))
5346 return FALSE;
5349 return TRUE;
5352 /* Copy private symbol information. If this symbol is in a section
5353 which we did not map into a BFD section, try to map the section
5354 index correctly. We use special macro definitions for the mapped
5355 section indices; these definitions are interpreted by the
5356 swap_out_syms function. */
5358 #define MAP_ONESYMTAB (SHN_HIOS + 1)
5359 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
5360 #define MAP_STRTAB (SHN_HIOS + 3)
5361 #define MAP_SHSTRTAB (SHN_HIOS + 4)
5362 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
5364 bfd_boolean
5365 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
5366 asymbol *isymarg,
5367 bfd *obfd,
5368 asymbol *osymarg)
5370 elf_symbol_type *isym, *osym;
5372 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5373 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5374 return TRUE;
5376 isym = elf_symbol_from (ibfd, isymarg);
5377 osym = elf_symbol_from (obfd, osymarg);
5379 if (isym != NULL
5380 && osym != NULL
5381 && bfd_is_abs_section (isym->symbol.section))
5383 unsigned int shndx;
5385 shndx = isym->internal_elf_sym.st_shndx;
5386 if (shndx == elf_onesymtab (ibfd))
5387 shndx = MAP_ONESYMTAB;
5388 else if (shndx == elf_dynsymtab (ibfd))
5389 shndx = MAP_DYNSYMTAB;
5390 else if (shndx == elf_tdata (ibfd)->strtab_section)
5391 shndx = MAP_STRTAB;
5392 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
5393 shndx = MAP_SHSTRTAB;
5394 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
5395 shndx = MAP_SYM_SHNDX;
5396 osym->internal_elf_sym.st_shndx = shndx;
5399 return TRUE;
5402 /* Swap out the symbols. */
5404 static bfd_boolean
5405 swap_out_syms (bfd *abfd,
5406 struct bfd_strtab_hash **sttp,
5407 int relocatable_p)
5409 const struct elf_backend_data *bed;
5410 int symcount;
5411 asymbol **syms;
5412 struct bfd_strtab_hash *stt;
5413 Elf_Internal_Shdr *symtab_hdr;
5414 Elf_Internal_Shdr *symtab_shndx_hdr;
5415 Elf_Internal_Shdr *symstrtab_hdr;
5416 char *outbound_syms;
5417 char *outbound_shndx;
5418 int idx;
5419 bfd_size_type amt;
5420 bfd_boolean name_local_sections;
5422 if (!elf_map_symbols (abfd))
5423 return FALSE;
5425 /* Dump out the symtabs. */
5426 stt = _bfd_elf_stringtab_init ();
5427 if (stt == NULL)
5428 return FALSE;
5430 bed = get_elf_backend_data (abfd);
5431 symcount = bfd_get_symcount (abfd);
5432 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5433 symtab_hdr->sh_type = SHT_SYMTAB;
5434 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
5435 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
5436 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
5437 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
5439 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5440 symstrtab_hdr->sh_type = SHT_STRTAB;
5442 amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym;
5443 outbound_syms = bfd_alloc (abfd, amt);
5444 if (outbound_syms == NULL)
5446 _bfd_stringtab_free (stt);
5447 return FALSE;
5449 symtab_hdr->contents = outbound_syms;
5451 outbound_shndx = NULL;
5452 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
5453 if (symtab_shndx_hdr->sh_name != 0)
5455 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
5456 outbound_shndx = bfd_zalloc (abfd, amt);
5457 if (outbound_shndx == NULL)
5459 _bfd_stringtab_free (stt);
5460 return FALSE;
5463 symtab_shndx_hdr->contents = outbound_shndx;
5464 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
5465 symtab_shndx_hdr->sh_size = amt;
5466 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
5467 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
5470 /* Now generate the data (for "contents"). */
5472 /* Fill in zeroth symbol and swap it out. */
5473 Elf_Internal_Sym sym;
5474 sym.st_name = 0;
5475 sym.st_value = 0;
5476 sym.st_size = 0;
5477 sym.st_info = 0;
5478 sym.st_other = 0;
5479 sym.st_shndx = SHN_UNDEF;
5480 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5481 outbound_syms += bed->s->sizeof_sym;
5482 if (outbound_shndx != NULL)
5483 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5486 name_local_sections
5487 = (bed->elf_backend_name_local_section_symbols
5488 && bed->elf_backend_name_local_section_symbols (abfd));
5490 syms = bfd_get_outsymbols (abfd);
5491 for (idx = 0; idx < symcount; idx++)
5493 Elf_Internal_Sym sym;
5494 bfd_vma value = syms[idx]->value;
5495 elf_symbol_type *type_ptr;
5496 flagword flags = syms[idx]->flags;
5497 int type;
5499 if (!name_local_sections
5500 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
5502 /* Local section symbols have no name. */
5503 sym.st_name = 0;
5505 else
5507 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
5508 syms[idx]->name,
5509 TRUE, FALSE);
5510 if (sym.st_name == (unsigned long) -1)
5512 _bfd_stringtab_free (stt);
5513 return FALSE;
5517 type_ptr = elf_symbol_from (abfd, syms[idx]);
5519 if ((flags & BSF_SECTION_SYM) == 0
5520 && bfd_is_com_section (syms[idx]->section))
5522 /* ELF common symbols put the alignment into the `value' field,
5523 and the size into the `size' field. This is backwards from
5524 how BFD handles it, so reverse it here. */
5525 sym.st_size = value;
5526 if (type_ptr == NULL
5527 || type_ptr->internal_elf_sym.st_value == 0)
5528 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
5529 else
5530 sym.st_value = type_ptr->internal_elf_sym.st_value;
5531 sym.st_shndx = _bfd_elf_section_from_bfd_section
5532 (abfd, syms[idx]->section);
5534 else
5536 asection *sec = syms[idx]->section;
5537 int shndx;
5539 if (sec->output_section)
5541 value += sec->output_offset;
5542 sec = sec->output_section;
5545 /* Don't add in the section vma for relocatable output. */
5546 if (! relocatable_p)
5547 value += sec->vma;
5548 sym.st_value = value;
5549 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
5551 if (bfd_is_abs_section (sec)
5552 && type_ptr != NULL
5553 && type_ptr->internal_elf_sym.st_shndx != 0)
5555 /* This symbol is in a real ELF section which we did
5556 not create as a BFD section. Undo the mapping done
5557 by copy_private_symbol_data. */
5558 shndx = type_ptr->internal_elf_sym.st_shndx;
5559 switch (shndx)
5561 case MAP_ONESYMTAB:
5562 shndx = elf_onesymtab (abfd);
5563 break;
5564 case MAP_DYNSYMTAB:
5565 shndx = elf_dynsymtab (abfd);
5566 break;
5567 case MAP_STRTAB:
5568 shndx = elf_tdata (abfd)->strtab_section;
5569 break;
5570 case MAP_SHSTRTAB:
5571 shndx = elf_tdata (abfd)->shstrtab_section;
5572 break;
5573 case MAP_SYM_SHNDX:
5574 shndx = elf_tdata (abfd)->symtab_shndx_section;
5575 break;
5576 default:
5577 break;
5580 else
5582 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
5584 if (shndx == -1)
5586 asection *sec2;
5588 /* Writing this would be a hell of a lot easier if
5589 we had some decent documentation on bfd, and
5590 knew what to expect of the library, and what to
5591 demand of applications. For example, it
5592 appears that `objcopy' might not set the
5593 section of a symbol to be a section that is
5594 actually in the output file. */
5595 sec2 = bfd_get_section_by_name (abfd, sec->name);
5596 if (sec2 == NULL)
5598 _bfd_error_handler (_("\
5599 Unable to find equivalent output section for symbol '%s' from section '%s'"),
5600 syms[idx]->name ? syms[idx]->name : "<Local sym>",
5601 sec->name);
5602 bfd_set_error (bfd_error_invalid_operation);
5603 _bfd_stringtab_free (stt);
5604 return FALSE;
5607 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
5608 BFD_ASSERT (shndx != -1);
5612 sym.st_shndx = shndx;
5615 if ((flags & BSF_THREAD_LOCAL) != 0)
5616 type = STT_TLS;
5617 else if ((flags & BSF_FUNCTION) != 0)
5618 type = STT_FUNC;
5619 else if ((flags & BSF_OBJECT) != 0)
5620 type = STT_OBJECT;
5621 else
5622 type = STT_NOTYPE;
5624 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
5625 type = STT_TLS;
5627 /* Processor-specific types. */
5628 if (type_ptr != NULL
5629 && bed->elf_backend_get_symbol_type)
5630 type = ((*bed->elf_backend_get_symbol_type)
5631 (&type_ptr->internal_elf_sym, type));
5633 if (flags & BSF_SECTION_SYM)
5635 if (flags & BSF_GLOBAL)
5636 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5637 else
5638 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5640 else if (bfd_is_com_section (syms[idx]->section))
5641 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
5642 else if (bfd_is_und_section (syms[idx]->section))
5643 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
5644 ? STB_WEAK
5645 : STB_GLOBAL),
5646 type);
5647 else if (flags & BSF_FILE)
5648 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5649 else
5651 int bind = STB_LOCAL;
5653 if (flags & BSF_LOCAL)
5654 bind = STB_LOCAL;
5655 else if (flags & BSF_WEAK)
5656 bind = STB_WEAK;
5657 else if (flags & BSF_GLOBAL)
5658 bind = STB_GLOBAL;
5660 sym.st_info = ELF_ST_INFO (bind, type);
5663 if (type_ptr != NULL)
5664 sym.st_other = type_ptr->internal_elf_sym.st_other;
5665 else
5666 sym.st_other = 0;
5668 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5669 outbound_syms += bed->s->sizeof_sym;
5670 if (outbound_shndx != NULL)
5671 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5674 *sttp = stt;
5675 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
5676 symstrtab_hdr->sh_type = SHT_STRTAB;
5678 symstrtab_hdr->sh_flags = 0;
5679 symstrtab_hdr->sh_addr = 0;
5680 symstrtab_hdr->sh_entsize = 0;
5681 symstrtab_hdr->sh_link = 0;
5682 symstrtab_hdr->sh_info = 0;
5683 symstrtab_hdr->sh_addralign = 1;
5685 return TRUE;
5688 /* Return the number of bytes required to hold the symtab vector.
5690 Note that we base it on the count plus 1, since we will null terminate
5691 the vector allocated based on this size. However, the ELF symbol table
5692 always has a dummy entry as symbol #0, so it ends up even. */
5694 long
5695 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
5697 long symcount;
5698 long symtab_size;
5699 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
5701 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5702 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5703 if (symcount > 0)
5704 symtab_size -= sizeof (asymbol *);
5706 return symtab_size;
5709 long
5710 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
5712 long symcount;
5713 long symtab_size;
5714 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
5716 if (elf_dynsymtab (abfd) == 0)
5718 bfd_set_error (bfd_error_invalid_operation);
5719 return -1;
5722 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5723 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5724 if (symcount > 0)
5725 symtab_size -= sizeof (asymbol *);
5727 return symtab_size;
5730 long
5731 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
5732 sec_ptr asect)
5734 return (asect->reloc_count + 1) * sizeof (arelent *);
5737 /* Canonicalize the relocs. */
5739 long
5740 _bfd_elf_canonicalize_reloc (bfd *abfd,
5741 sec_ptr section,
5742 arelent **relptr,
5743 asymbol **symbols)
5745 arelent *tblptr;
5746 unsigned int i;
5747 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5749 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
5750 return -1;
5752 tblptr = section->relocation;
5753 for (i = 0; i < section->reloc_count; i++)
5754 *relptr++ = tblptr++;
5756 *relptr = NULL;
5758 return section->reloc_count;
5761 long
5762 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
5764 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5765 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
5767 if (symcount >= 0)
5768 bfd_get_symcount (abfd) = symcount;
5769 return symcount;
5772 long
5773 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
5774 asymbol **allocation)
5776 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5777 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
5779 if (symcount >= 0)
5780 bfd_get_dynamic_symcount (abfd) = symcount;
5781 return symcount;
5784 /* Return the size required for the dynamic reloc entries. Any
5785 section that was actually installed in the BFD, and has type
5786 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5787 considered to be a dynamic reloc section. */
5789 long
5790 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
5792 long ret;
5793 asection *s;
5795 if (elf_dynsymtab (abfd) == 0)
5797 bfd_set_error (bfd_error_invalid_operation);
5798 return -1;
5801 ret = sizeof (arelent *);
5802 for (s = abfd->sections; s != NULL; s = s->next)
5803 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5804 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5805 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5806 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
5807 * sizeof (arelent *));
5809 return ret;
5812 /* Canonicalize the dynamic relocation entries. Note that we return
5813 the dynamic relocations as a single block, although they are
5814 actually associated with particular sections; the interface, which
5815 was designed for SunOS style shared libraries, expects that there
5816 is only one set of dynamic relocs. Any section that was actually
5817 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5818 the dynamic symbol table, is considered to be a dynamic reloc
5819 section. */
5821 long
5822 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
5823 arelent **storage,
5824 asymbol **syms)
5826 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
5827 asection *s;
5828 long ret;
5830 if (elf_dynsymtab (abfd) == 0)
5832 bfd_set_error (bfd_error_invalid_operation);
5833 return -1;
5836 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
5837 ret = 0;
5838 for (s = abfd->sections; s != NULL; s = s->next)
5840 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5841 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5842 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5844 arelent *p;
5845 long count, i;
5847 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
5848 return -1;
5849 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
5850 p = s->relocation;
5851 for (i = 0; i < count; i++)
5852 *storage++ = p++;
5853 ret += count;
5857 *storage = NULL;
5859 return ret;
5862 /* Read in the version information. */
5864 bfd_boolean
5865 _bfd_elf_slurp_version_tables (bfd *abfd)
5867 bfd_byte *contents = NULL;
5868 bfd_size_type amt;
5870 if (elf_dynverdef (abfd) != 0)
5872 Elf_Internal_Shdr *hdr;
5873 Elf_External_Verdef *everdef;
5874 Elf_Internal_Verdef *iverdef;
5875 Elf_Internal_Verdef *iverdefarr;
5876 Elf_Internal_Verdef iverdefmem;
5877 unsigned int i;
5878 unsigned int maxidx;
5880 hdr = &elf_tdata (abfd)->dynverdef_hdr;
5882 contents = bfd_malloc (hdr->sh_size);
5883 if (contents == NULL)
5884 goto error_return;
5885 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5886 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
5887 goto error_return;
5889 /* We know the number of entries in the section but not the maximum
5890 index. Therefore we have to run through all entries and find
5891 the maximum. */
5892 everdef = (Elf_External_Verdef *) contents;
5893 maxidx = 0;
5894 for (i = 0; i < hdr->sh_info; ++i)
5896 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5898 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
5899 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
5901 everdef = ((Elf_External_Verdef *)
5902 ((bfd_byte *) everdef + iverdefmem.vd_next));
5905 amt = (bfd_size_type) maxidx * sizeof (Elf_Internal_Verdef);
5906 elf_tdata (abfd)->verdef = bfd_zalloc (abfd, amt);
5907 if (elf_tdata (abfd)->verdef == NULL)
5908 goto error_return;
5910 elf_tdata (abfd)->cverdefs = maxidx;
5912 everdef = (Elf_External_Verdef *) contents;
5913 iverdefarr = elf_tdata (abfd)->verdef;
5914 for (i = 0; i < hdr->sh_info; i++)
5916 Elf_External_Verdaux *everdaux;
5917 Elf_Internal_Verdaux *iverdaux;
5918 unsigned int j;
5920 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5922 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
5923 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
5925 iverdef->vd_bfd = abfd;
5927 amt = (bfd_size_type) iverdef->vd_cnt * sizeof (Elf_Internal_Verdaux);
5928 iverdef->vd_auxptr = bfd_alloc (abfd, amt);
5929 if (iverdef->vd_auxptr == NULL)
5930 goto error_return;
5932 everdaux = ((Elf_External_Verdaux *)
5933 ((bfd_byte *) everdef + iverdef->vd_aux));
5934 iverdaux = iverdef->vd_auxptr;
5935 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
5937 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
5939 iverdaux->vda_nodename =
5940 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5941 iverdaux->vda_name);
5942 if (iverdaux->vda_nodename == NULL)
5943 goto error_return;
5945 if (j + 1 < iverdef->vd_cnt)
5946 iverdaux->vda_nextptr = iverdaux + 1;
5947 else
5948 iverdaux->vda_nextptr = NULL;
5950 everdaux = ((Elf_External_Verdaux *)
5951 ((bfd_byte *) everdaux + iverdaux->vda_next));
5954 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
5956 if (i + 1 < hdr->sh_info)
5957 iverdef->vd_nextdef = iverdef + 1;
5958 else
5959 iverdef->vd_nextdef = NULL;
5961 everdef = ((Elf_External_Verdef *)
5962 ((bfd_byte *) everdef + iverdef->vd_next));
5965 free (contents);
5966 contents = NULL;
5969 if (elf_dynverref (abfd) != 0)
5971 Elf_Internal_Shdr *hdr;
5972 Elf_External_Verneed *everneed;
5973 Elf_Internal_Verneed *iverneed;
5974 unsigned int i;
5976 hdr = &elf_tdata (abfd)->dynverref_hdr;
5978 amt = (bfd_size_type) hdr->sh_info * sizeof (Elf_Internal_Verneed);
5979 elf_tdata (abfd)->verref = bfd_zalloc (abfd, amt);
5980 if (elf_tdata (abfd)->verref == NULL)
5981 goto error_return;
5983 elf_tdata (abfd)->cverrefs = hdr->sh_info;
5985 contents = bfd_malloc (hdr->sh_size);
5986 if (contents == NULL)
5987 goto error_return;
5988 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5989 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
5990 goto error_return;
5992 everneed = (Elf_External_Verneed *) contents;
5993 iverneed = elf_tdata (abfd)->verref;
5994 for (i = 0; i < hdr->sh_info; i++, iverneed++)
5996 Elf_External_Vernaux *evernaux;
5997 Elf_Internal_Vernaux *ivernaux;
5998 unsigned int j;
6000 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
6002 iverneed->vn_bfd = abfd;
6004 iverneed->vn_filename =
6005 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6006 iverneed->vn_file);
6007 if (iverneed->vn_filename == NULL)
6008 goto error_return;
6010 amt = iverneed->vn_cnt;
6011 amt *= sizeof (Elf_Internal_Vernaux);
6012 iverneed->vn_auxptr = bfd_alloc (abfd, amt);
6014 evernaux = ((Elf_External_Vernaux *)
6015 ((bfd_byte *) everneed + iverneed->vn_aux));
6016 ivernaux = iverneed->vn_auxptr;
6017 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
6019 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
6021 ivernaux->vna_nodename =
6022 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6023 ivernaux->vna_name);
6024 if (ivernaux->vna_nodename == NULL)
6025 goto error_return;
6027 if (j + 1 < iverneed->vn_cnt)
6028 ivernaux->vna_nextptr = ivernaux + 1;
6029 else
6030 ivernaux->vna_nextptr = NULL;
6032 evernaux = ((Elf_External_Vernaux *)
6033 ((bfd_byte *) evernaux + ivernaux->vna_next));
6036 if (i + 1 < hdr->sh_info)
6037 iverneed->vn_nextref = iverneed + 1;
6038 else
6039 iverneed->vn_nextref = NULL;
6041 everneed = ((Elf_External_Verneed *)
6042 ((bfd_byte *) everneed + iverneed->vn_next));
6045 free (contents);
6046 contents = NULL;
6049 return TRUE;
6051 error_return:
6052 if (contents != NULL)
6053 free (contents);
6054 return FALSE;
6057 asymbol *
6058 _bfd_elf_make_empty_symbol (bfd *abfd)
6060 elf_symbol_type *newsym;
6061 bfd_size_type amt = sizeof (elf_symbol_type);
6063 newsym = bfd_zalloc (abfd, amt);
6064 if (!newsym)
6065 return NULL;
6066 else
6068 newsym->symbol.the_bfd = abfd;
6069 return &newsym->symbol;
6073 void
6074 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
6075 asymbol *symbol,
6076 symbol_info *ret)
6078 bfd_symbol_info (symbol, ret);
6081 /* Return whether a symbol name implies a local symbol. Most targets
6082 use this function for the is_local_label_name entry point, but some
6083 override it. */
6085 bfd_boolean
6086 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
6087 const char *name)
6089 /* Normal local symbols start with ``.L''. */
6090 if (name[0] == '.' && name[1] == 'L')
6091 return TRUE;
6093 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
6094 DWARF debugging symbols starting with ``..''. */
6095 if (name[0] == '.' && name[1] == '.')
6096 return TRUE;
6098 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
6099 emitting DWARF debugging output. I suspect this is actually a
6100 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
6101 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
6102 underscore to be emitted on some ELF targets). For ease of use,
6103 we treat such symbols as local. */
6104 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
6105 return TRUE;
6107 return FALSE;
6110 alent *
6111 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
6112 asymbol *symbol ATTRIBUTE_UNUSED)
6114 abort ();
6115 return NULL;
6118 bfd_boolean
6119 _bfd_elf_set_arch_mach (bfd *abfd,
6120 enum bfd_architecture arch,
6121 unsigned long machine)
6123 /* If this isn't the right architecture for this backend, and this
6124 isn't the generic backend, fail. */
6125 if (arch != get_elf_backend_data (abfd)->arch
6126 && arch != bfd_arch_unknown
6127 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
6128 return FALSE;
6130 return bfd_default_set_arch_mach (abfd, arch, machine);
6133 /* Find the function to a particular section and offset,
6134 for error reporting. */
6136 static bfd_boolean
6137 elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
6138 asection *section,
6139 asymbol **symbols,
6140 bfd_vma offset,
6141 const char **filename_ptr,
6142 const char **functionname_ptr)
6144 const char *filename;
6145 asymbol *func;
6146 bfd_vma low_func;
6147 asymbol **p;
6149 filename = NULL;
6150 func = NULL;
6151 low_func = 0;
6153 for (p = symbols; *p != NULL; p++)
6155 elf_symbol_type *q;
6157 q = (elf_symbol_type *) *p;
6159 if (bfd_get_section (&q->symbol) != section)
6160 continue;
6162 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
6164 default:
6165 break;
6166 case STT_FILE:
6167 filename = bfd_asymbol_name (&q->symbol);
6168 break;
6169 case STT_NOTYPE:
6170 case STT_FUNC:
6171 if (q->symbol.section == section
6172 && q->symbol.value >= low_func
6173 && q->symbol.value <= offset)
6175 func = (asymbol *) q;
6176 low_func = q->symbol.value;
6178 break;
6182 if (func == NULL)
6183 return FALSE;
6185 if (filename_ptr)
6186 *filename_ptr = filename;
6187 if (functionname_ptr)
6188 *functionname_ptr = bfd_asymbol_name (func);
6190 return TRUE;
6193 /* Find the nearest line to a particular section and offset,
6194 for error reporting. */
6196 bfd_boolean
6197 _bfd_elf_find_nearest_line (bfd *abfd,
6198 asection *section,
6199 asymbol **symbols,
6200 bfd_vma offset,
6201 const char **filename_ptr,
6202 const char **functionname_ptr,
6203 unsigned int *line_ptr)
6205 bfd_boolean found;
6207 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
6208 filename_ptr, functionname_ptr,
6209 line_ptr))
6211 if (!*functionname_ptr)
6212 elf_find_function (abfd, section, symbols, offset,
6213 *filename_ptr ? NULL : filename_ptr,
6214 functionname_ptr);
6216 return TRUE;
6219 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
6220 filename_ptr, functionname_ptr,
6221 line_ptr, 0,
6222 &elf_tdata (abfd)->dwarf2_find_line_info))
6224 if (!*functionname_ptr)
6225 elf_find_function (abfd, section, symbols, offset,
6226 *filename_ptr ? NULL : filename_ptr,
6227 functionname_ptr);
6229 return TRUE;
6232 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
6233 &found, filename_ptr,
6234 functionname_ptr, line_ptr,
6235 &elf_tdata (abfd)->line_info))
6236 return FALSE;
6237 if (found && (*functionname_ptr || *line_ptr))
6238 return TRUE;
6240 if (symbols == NULL)
6241 return FALSE;
6243 if (! elf_find_function (abfd, section, symbols, offset,
6244 filename_ptr, functionname_ptr))
6245 return FALSE;
6247 *line_ptr = 0;
6248 return TRUE;
6252 _bfd_elf_sizeof_headers (bfd *abfd, bfd_boolean reloc)
6254 int ret;
6256 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
6257 if (! reloc)
6258 ret += get_program_header_size (abfd);
6259 return ret;
6262 bfd_boolean
6263 _bfd_elf_set_section_contents (bfd *abfd,
6264 sec_ptr section,
6265 const void *location,
6266 file_ptr offset,
6267 bfd_size_type count)
6269 Elf_Internal_Shdr *hdr;
6270 bfd_signed_vma pos;
6272 if (! abfd->output_has_begun
6273 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
6274 return FALSE;
6276 hdr = &elf_section_data (section)->this_hdr;
6277 pos = hdr->sh_offset + offset;
6278 if (bfd_seek (abfd, pos, SEEK_SET) != 0
6279 || bfd_bwrite (location, count, abfd) != count)
6280 return FALSE;
6282 return TRUE;
6285 void
6286 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
6287 arelent *cache_ptr ATTRIBUTE_UNUSED,
6288 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
6290 abort ();
6293 /* Try to convert a non-ELF reloc into an ELF one. */
6295 bfd_boolean
6296 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
6298 /* Check whether we really have an ELF howto. */
6300 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
6302 bfd_reloc_code_real_type code;
6303 reloc_howto_type *howto;
6305 /* Alien reloc: Try to determine its type to replace it with an
6306 equivalent ELF reloc. */
6308 if (areloc->howto->pc_relative)
6310 switch (areloc->howto->bitsize)
6312 case 8:
6313 code = BFD_RELOC_8_PCREL;
6314 break;
6315 case 12:
6316 code = BFD_RELOC_12_PCREL;
6317 break;
6318 case 16:
6319 code = BFD_RELOC_16_PCREL;
6320 break;
6321 case 24:
6322 code = BFD_RELOC_24_PCREL;
6323 break;
6324 case 32:
6325 code = BFD_RELOC_32_PCREL;
6326 break;
6327 case 64:
6328 code = BFD_RELOC_64_PCREL;
6329 break;
6330 default:
6331 goto fail;
6334 howto = bfd_reloc_type_lookup (abfd, code);
6336 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
6338 if (howto->pcrel_offset)
6339 areloc->addend += areloc->address;
6340 else
6341 areloc->addend -= areloc->address; /* addend is unsigned!! */
6344 else
6346 switch (areloc->howto->bitsize)
6348 case 8:
6349 code = BFD_RELOC_8;
6350 break;
6351 case 14:
6352 code = BFD_RELOC_14;
6353 break;
6354 case 16:
6355 code = BFD_RELOC_16;
6356 break;
6357 case 26:
6358 code = BFD_RELOC_26;
6359 break;
6360 case 32:
6361 code = BFD_RELOC_32;
6362 break;
6363 case 64:
6364 code = BFD_RELOC_64;
6365 break;
6366 default:
6367 goto fail;
6370 howto = bfd_reloc_type_lookup (abfd, code);
6373 if (howto)
6374 areloc->howto = howto;
6375 else
6376 goto fail;
6379 return TRUE;
6381 fail:
6382 (*_bfd_error_handler)
6383 (_("%s: unsupported relocation type %s"),
6384 bfd_archive_filename (abfd), areloc->howto->name);
6385 bfd_set_error (bfd_error_bad_value);
6386 return FALSE;
6389 bfd_boolean
6390 _bfd_elf_close_and_cleanup (bfd *abfd)
6392 if (bfd_get_format (abfd) == bfd_object)
6394 if (elf_shstrtab (abfd) != NULL)
6395 _bfd_elf_strtab_free (elf_shstrtab (abfd));
6398 return _bfd_generic_close_and_cleanup (abfd);
6401 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
6402 in the relocation's offset. Thus we cannot allow any sort of sanity
6403 range-checking to interfere. There is nothing else to do in processing
6404 this reloc. */
6406 bfd_reloc_status_type
6407 _bfd_elf_rel_vtable_reloc_fn
6408 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
6409 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
6410 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
6411 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
6413 return bfd_reloc_ok;
6416 /* Elf core file support. Much of this only works on native
6417 toolchains, since we rely on knowing the
6418 machine-dependent procfs structure in order to pick
6419 out details about the corefile. */
6421 #ifdef HAVE_SYS_PROCFS_H
6422 # include <sys/procfs.h>
6423 #endif
6425 /* FIXME: this is kinda wrong, but it's what gdb wants. */
6427 static int
6428 elfcore_make_pid (bfd *abfd)
6430 return ((elf_tdata (abfd)->core_lwpid << 16)
6431 + (elf_tdata (abfd)->core_pid));
6434 /* If there isn't a section called NAME, make one, using
6435 data from SECT. Note, this function will generate a
6436 reference to NAME, so you shouldn't deallocate or
6437 overwrite it. */
6439 static bfd_boolean
6440 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
6442 asection *sect2;
6444 if (bfd_get_section_by_name (abfd, name) != NULL)
6445 return TRUE;
6447 sect2 = bfd_make_section (abfd, name);
6448 if (sect2 == NULL)
6449 return FALSE;
6451 sect2->size = sect->size;
6452 sect2->filepos = sect->filepos;
6453 sect2->flags = sect->flags;
6454 sect2->alignment_power = sect->alignment_power;
6455 return TRUE;
6458 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
6459 actually creates up to two pseudosections:
6460 - For the single-threaded case, a section named NAME, unless
6461 such a section already exists.
6462 - For the multi-threaded case, a section named "NAME/PID", where
6463 PID is elfcore_make_pid (abfd).
6464 Both pseudosections have identical contents. */
6465 bfd_boolean
6466 _bfd_elfcore_make_pseudosection (bfd *abfd,
6467 char *name,
6468 size_t size,
6469 ufile_ptr filepos)
6471 char buf[100];
6472 char *threaded_name;
6473 size_t len;
6474 asection *sect;
6476 /* Build the section name. */
6478 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
6479 len = strlen (buf) + 1;
6480 threaded_name = bfd_alloc (abfd, len);
6481 if (threaded_name == NULL)
6482 return FALSE;
6483 memcpy (threaded_name, buf, len);
6485 sect = bfd_make_section_anyway (abfd, threaded_name);
6486 if (sect == NULL)
6487 return FALSE;
6488 sect->size = size;
6489 sect->filepos = filepos;
6490 sect->flags = SEC_HAS_CONTENTS;
6491 sect->alignment_power = 2;
6493 return elfcore_maybe_make_sect (abfd, name, sect);
6496 /* prstatus_t exists on:
6497 solaris 2.5+
6498 linux 2.[01] + glibc
6499 unixware 4.2
6502 #if defined (HAVE_PRSTATUS_T)
6504 static bfd_boolean
6505 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
6507 size_t size;
6508 int offset;
6510 if (note->descsz == sizeof (prstatus_t))
6512 prstatus_t prstat;
6514 size = sizeof (prstat.pr_reg);
6515 offset = offsetof (prstatus_t, pr_reg);
6516 memcpy (&prstat, note->descdata, sizeof (prstat));
6518 /* Do not overwrite the core signal if it
6519 has already been set by another thread. */
6520 if (elf_tdata (abfd)->core_signal == 0)
6521 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
6522 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6524 /* pr_who exists on:
6525 solaris 2.5+
6526 unixware 4.2
6527 pr_who doesn't exist on:
6528 linux 2.[01]
6530 #if defined (HAVE_PRSTATUS_T_PR_WHO)
6531 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6532 #endif
6534 #if defined (HAVE_PRSTATUS32_T)
6535 else if (note->descsz == sizeof (prstatus32_t))
6537 /* 64-bit host, 32-bit corefile */
6538 prstatus32_t prstat;
6540 size = sizeof (prstat.pr_reg);
6541 offset = offsetof (prstatus32_t, pr_reg);
6542 memcpy (&prstat, note->descdata, sizeof (prstat));
6544 /* Do not overwrite the core signal if it
6545 has already been set by another thread. */
6546 if (elf_tdata (abfd)->core_signal == 0)
6547 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
6548 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6550 /* pr_who exists on:
6551 solaris 2.5+
6552 unixware 4.2
6553 pr_who doesn't exist on:
6554 linux 2.[01]
6556 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
6557 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6558 #endif
6560 #endif /* HAVE_PRSTATUS32_T */
6561 else
6563 /* Fail - we don't know how to handle any other
6564 note size (ie. data object type). */
6565 return TRUE;
6568 /* Make a ".reg/999" section and a ".reg" section. */
6569 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
6570 size, note->descpos + offset);
6572 #endif /* defined (HAVE_PRSTATUS_T) */
6574 /* Create a pseudosection containing the exact contents of NOTE. */
6575 static bfd_boolean
6576 elfcore_make_note_pseudosection (bfd *abfd,
6577 char *name,
6578 Elf_Internal_Note *note)
6580 return _bfd_elfcore_make_pseudosection (abfd, name,
6581 note->descsz, note->descpos);
6584 /* There isn't a consistent prfpregset_t across platforms,
6585 but it doesn't matter, because we don't have to pick this
6586 data structure apart. */
6588 static bfd_boolean
6589 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
6591 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6594 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6595 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6596 literally. */
6598 static bfd_boolean
6599 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
6601 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
6604 #if defined (HAVE_PRPSINFO_T)
6605 typedef prpsinfo_t elfcore_psinfo_t;
6606 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6607 typedef prpsinfo32_t elfcore_psinfo32_t;
6608 #endif
6609 #endif
6611 #if defined (HAVE_PSINFO_T)
6612 typedef psinfo_t elfcore_psinfo_t;
6613 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6614 typedef psinfo32_t elfcore_psinfo32_t;
6615 #endif
6616 #endif
6618 /* return a malloc'ed copy of a string at START which is at
6619 most MAX bytes long, possibly without a terminating '\0'.
6620 the copy will always have a terminating '\0'. */
6622 char *
6623 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
6625 char *dups;
6626 char *end = memchr (start, '\0', max);
6627 size_t len;
6629 if (end == NULL)
6630 len = max;
6631 else
6632 len = end - start;
6634 dups = bfd_alloc (abfd, len + 1);
6635 if (dups == NULL)
6636 return NULL;
6638 memcpy (dups, start, len);
6639 dups[len] = '\0';
6641 return dups;
6644 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6645 static bfd_boolean
6646 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
6648 if (note->descsz == sizeof (elfcore_psinfo_t))
6650 elfcore_psinfo_t psinfo;
6652 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6654 elf_tdata (abfd)->core_program
6655 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6656 sizeof (psinfo.pr_fname));
6658 elf_tdata (abfd)->core_command
6659 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6660 sizeof (psinfo.pr_psargs));
6662 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6663 else if (note->descsz == sizeof (elfcore_psinfo32_t))
6665 /* 64-bit host, 32-bit corefile */
6666 elfcore_psinfo32_t psinfo;
6668 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6670 elf_tdata (abfd)->core_program
6671 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6672 sizeof (psinfo.pr_fname));
6674 elf_tdata (abfd)->core_command
6675 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6676 sizeof (psinfo.pr_psargs));
6678 #endif
6680 else
6682 /* Fail - we don't know how to handle any other
6683 note size (ie. data object type). */
6684 return TRUE;
6687 /* Note that for some reason, a spurious space is tacked
6688 onto the end of the args in some (at least one anyway)
6689 implementations, so strip it off if it exists. */
6692 char *command = elf_tdata (abfd)->core_command;
6693 int n = strlen (command);
6695 if (0 < n && command[n - 1] == ' ')
6696 command[n - 1] = '\0';
6699 return TRUE;
6701 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6703 #if defined (HAVE_PSTATUS_T)
6704 static bfd_boolean
6705 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
6707 if (note->descsz == sizeof (pstatus_t)
6708 #if defined (HAVE_PXSTATUS_T)
6709 || note->descsz == sizeof (pxstatus_t)
6710 #endif
6713 pstatus_t pstat;
6715 memcpy (&pstat, note->descdata, sizeof (pstat));
6717 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6719 #if defined (HAVE_PSTATUS32_T)
6720 else if (note->descsz == sizeof (pstatus32_t))
6722 /* 64-bit host, 32-bit corefile */
6723 pstatus32_t pstat;
6725 memcpy (&pstat, note->descdata, sizeof (pstat));
6727 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6729 #endif
6730 /* Could grab some more details from the "representative"
6731 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6732 NT_LWPSTATUS note, presumably. */
6734 return TRUE;
6736 #endif /* defined (HAVE_PSTATUS_T) */
6738 #if defined (HAVE_LWPSTATUS_T)
6739 static bfd_boolean
6740 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
6742 lwpstatus_t lwpstat;
6743 char buf[100];
6744 char *name;
6745 size_t len;
6746 asection *sect;
6748 if (note->descsz != sizeof (lwpstat)
6749 #if defined (HAVE_LWPXSTATUS_T)
6750 && note->descsz != sizeof (lwpxstatus_t)
6751 #endif
6753 return TRUE;
6755 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
6757 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
6758 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
6760 /* Make a ".reg/999" section. */
6762 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
6763 len = strlen (buf) + 1;
6764 name = bfd_alloc (abfd, len);
6765 if (name == NULL)
6766 return FALSE;
6767 memcpy (name, buf, len);
6769 sect = bfd_make_section_anyway (abfd, name);
6770 if (sect == NULL)
6771 return FALSE;
6773 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6774 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
6775 sect->filepos = note->descpos
6776 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
6777 #endif
6779 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6780 sect->size = sizeof (lwpstat.pr_reg);
6781 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
6782 #endif
6784 sect->flags = SEC_HAS_CONTENTS;
6785 sect->alignment_power = 2;
6787 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
6788 return FALSE;
6790 /* Make a ".reg2/999" section */
6792 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
6793 len = strlen (buf) + 1;
6794 name = bfd_alloc (abfd, len);
6795 if (name == NULL)
6796 return FALSE;
6797 memcpy (name, buf, len);
6799 sect = bfd_make_section_anyway (abfd, name);
6800 if (sect == NULL)
6801 return FALSE;
6803 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6804 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
6805 sect->filepos = note->descpos
6806 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
6807 #endif
6809 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6810 sect->size = sizeof (lwpstat.pr_fpreg);
6811 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
6812 #endif
6814 sect->flags = SEC_HAS_CONTENTS;
6815 sect->alignment_power = 2;
6817 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
6819 #endif /* defined (HAVE_LWPSTATUS_T) */
6821 #if defined (HAVE_WIN32_PSTATUS_T)
6822 static bfd_boolean
6823 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
6825 char buf[30];
6826 char *name;
6827 size_t len;
6828 asection *sect;
6829 win32_pstatus_t pstatus;
6831 if (note->descsz < sizeof (pstatus))
6832 return TRUE;
6834 memcpy (&pstatus, note->descdata, sizeof (pstatus));
6836 switch (pstatus.data_type)
6838 case NOTE_INFO_PROCESS:
6839 /* FIXME: need to add ->core_command. */
6840 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
6841 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
6842 break;
6844 case NOTE_INFO_THREAD:
6845 /* Make a ".reg/999" section. */
6846 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
6848 len = strlen (buf) + 1;
6849 name = bfd_alloc (abfd, len);
6850 if (name == NULL)
6851 return FALSE;
6853 memcpy (name, buf, len);
6855 sect = bfd_make_section_anyway (abfd, name);
6856 if (sect == NULL)
6857 return FALSE;
6859 sect->size = sizeof (pstatus.data.thread_info.thread_context);
6860 sect->filepos = (note->descpos
6861 + offsetof (struct win32_pstatus,
6862 data.thread_info.thread_context));
6863 sect->flags = SEC_HAS_CONTENTS;
6864 sect->alignment_power = 2;
6866 if (pstatus.data.thread_info.is_active_thread)
6867 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
6868 return FALSE;
6869 break;
6871 case NOTE_INFO_MODULE:
6872 /* Make a ".module/xxxxxxxx" section. */
6873 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
6875 len = strlen (buf) + 1;
6876 name = bfd_alloc (abfd, len);
6877 if (name == NULL)
6878 return FALSE;
6880 memcpy (name, buf, len);
6882 sect = bfd_make_section_anyway (abfd, name);
6884 if (sect == NULL)
6885 return FALSE;
6887 sect->size = note->descsz;
6888 sect->filepos = note->descpos;
6889 sect->flags = SEC_HAS_CONTENTS;
6890 sect->alignment_power = 2;
6891 break;
6893 default:
6894 return TRUE;
6897 return TRUE;
6899 #endif /* HAVE_WIN32_PSTATUS_T */
6901 static bfd_boolean
6902 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
6904 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6906 switch (note->type)
6908 default:
6909 return TRUE;
6911 case NT_PRSTATUS:
6912 if (bed->elf_backend_grok_prstatus)
6913 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
6914 return TRUE;
6915 #if defined (HAVE_PRSTATUS_T)
6916 return elfcore_grok_prstatus (abfd, note);
6917 #else
6918 return TRUE;
6919 #endif
6921 #if defined (HAVE_PSTATUS_T)
6922 case NT_PSTATUS:
6923 return elfcore_grok_pstatus (abfd, note);
6924 #endif
6926 #if defined (HAVE_LWPSTATUS_T)
6927 case NT_LWPSTATUS:
6928 return elfcore_grok_lwpstatus (abfd, note);
6929 #endif
6931 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
6932 return elfcore_grok_prfpreg (abfd, note);
6934 #if defined (HAVE_WIN32_PSTATUS_T)
6935 case NT_WIN32PSTATUS:
6936 return elfcore_grok_win32pstatus (abfd, note);
6937 #endif
6939 case NT_PRXFPREG: /* Linux SSE extension */
6940 if (note->namesz == 6
6941 && strcmp (note->namedata, "LINUX") == 0)
6942 return elfcore_grok_prxfpreg (abfd, note);
6943 else
6944 return TRUE;
6946 case NT_PRPSINFO:
6947 case NT_PSINFO:
6948 if (bed->elf_backend_grok_psinfo)
6949 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
6950 return TRUE;
6951 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6952 return elfcore_grok_psinfo (abfd, note);
6953 #else
6954 return TRUE;
6955 #endif
6957 case NT_AUXV:
6959 asection *sect = bfd_make_section_anyway (abfd, ".auxv");
6961 if (sect == NULL)
6962 return FALSE;
6963 sect->size = note->descsz;
6964 sect->filepos = note->descpos;
6965 sect->flags = SEC_HAS_CONTENTS;
6966 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
6968 return TRUE;
6973 static bfd_boolean
6974 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
6976 char *cp;
6978 cp = strchr (note->namedata, '@');
6979 if (cp != NULL)
6981 *lwpidp = atoi(cp + 1);
6982 return TRUE;
6984 return FALSE;
6987 static bfd_boolean
6988 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
6991 /* Signal number at offset 0x08. */
6992 elf_tdata (abfd)->core_signal
6993 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
6995 /* Process ID at offset 0x50. */
6996 elf_tdata (abfd)->core_pid
6997 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
6999 /* Command name at 0x7c (max 32 bytes, including nul). */
7000 elf_tdata (abfd)->core_command
7001 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
7003 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
7004 note);
7007 static bfd_boolean
7008 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
7010 int lwp;
7012 if (elfcore_netbsd_get_lwpid (note, &lwp))
7013 elf_tdata (abfd)->core_lwpid = lwp;
7015 if (note->type == NT_NETBSDCORE_PROCINFO)
7017 /* NetBSD-specific core "procinfo". Note that we expect to
7018 find this note before any of the others, which is fine,
7019 since the kernel writes this note out first when it
7020 creates a core file. */
7022 return elfcore_grok_netbsd_procinfo (abfd, note);
7025 /* As of Jan 2002 there are no other machine-independent notes
7026 defined for NetBSD core files. If the note type is less
7027 than the start of the machine-dependent note types, we don't
7028 understand it. */
7030 if (note->type < NT_NETBSDCORE_FIRSTMACH)
7031 return TRUE;
7034 switch (bfd_get_arch (abfd))
7036 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
7037 PT_GETFPREGS == mach+2. */
7039 case bfd_arch_alpha:
7040 case bfd_arch_sparc:
7041 switch (note->type)
7043 case NT_NETBSDCORE_FIRSTMACH+0:
7044 return elfcore_make_note_pseudosection (abfd, ".reg", note);
7046 case NT_NETBSDCORE_FIRSTMACH+2:
7047 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7049 default:
7050 return TRUE;
7053 /* On all other arch's, PT_GETREGS == mach+1 and
7054 PT_GETFPREGS == mach+3. */
7056 default:
7057 switch (note->type)
7059 case NT_NETBSDCORE_FIRSTMACH+1:
7060 return elfcore_make_note_pseudosection (abfd, ".reg", note);
7062 case NT_NETBSDCORE_FIRSTMACH+3:
7063 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7065 default:
7066 return TRUE;
7069 /* NOTREACHED */
7072 static bfd_boolean
7073 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, pid_t *tid)
7075 void *ddata = note->descdata;
7076 char buf[100];
7077 char *name;
7078 asection *sect;
7079 short sig;
7080 unsigned flags;
7082 /* nto_procfs_status 'pid' field is at offset 0. */
7083 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
7085 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
7086 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
7088 /* nto_procfs_status 'flags' field is at offset 8. */
7089 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
7091 /* nto_procfs_status 'what' field is at offset 14. */
7092 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
7094 elf_tdata (abfd)->core_signal = sig;
7095 elf_tdata (abfd)->core_lwpid = *tid;
7098 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
7099 do not come from signals so we make sure we set the current
7100 thread just in case. */
7101 if (flags & 0x00000080)
7102 elf_tdata (abfd)->core_lwpid = *tid;
7104 /* Make a ".qnx_core_status/%d" section. */
7105 sprintf (buf, ".qnx_core_status/%d", *tid);
7107 name = bfd_alloc (abfd, strlen (buf) + 1);
7108 if (name == NULL)
7109 return FALSE;
7110 strcpy (name, buf);
7112 sect = bfd_make_section_anyway (abfd, name);
7113 if (sect == NULL)
7114 return FALSE;
7116 sect->size = note->descsz;
7117 sect->filepos = note->descpos;
7118 sect->flags = SEC_HAS_CONTENTS;
7119 sect->alignment_power = 2;
7121 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
7124 static bfd_boolean
7125 elfcore_grok_nto_gregs (bfd *abfd, Elf_Internal_Note *note, pid_t tid)
7127 char buf[100];
7128 char *name;
7129 asection *sect;
7131 /* Make a ".reg/%d" section. */
7132 sprintf (buf, ".reg/%d", tid);
7134 name = bfd_alloc (abfd, strlen (buf) + 1);
7135 if (name == NULL)
7136 return FALSE;
7137 strcpy (name, buf);
7139 sect = bfd_make_section_anyway (abfd, name);
7140 if (sect == NULL)
7141 return FALSE;
7143 sect->size = note->descsz;
7144 sect->filepos = note->descpos;
7145 sect->flags = SEC_HAS_CONTENTS;
7146 sect->alignment_power = 2;
7148 /* This is the current thread. */
7149 if (elf_tdata (abfd)->core_lwpid == tid)
7150 return elfcore_maybe_make_sect (abfd, ".reg", sect);
7152 return TRUE;
7155 #define BFD_QNT_CORE_INFO 7
7156 #define BFD_QNT_CORE_STATUS 8
7157 #define BFD_QNT_CORE_GREG 9
7158 #define BFD_QNT_CORE_FPREG 10
7160 static bfd_boolean
7161 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
7163 /* Every GREG section has a STATUS section before it. Store the
7164 tid from the previous call to pass down to the next gregs
7165 function. */
7166 static pid_t tid = 1;
7168 switch (note->type)
7170 case BFD_QNT_CORE_INFO: return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
7171 case BFD_QNT_CORE_STATUS: return elfcore_grok_nto_status (abfd, note, &tid);
7172 case BFD_QNT_CORE_GREG: return elfcore_grok_nto_gregs (abfd, note, tid);
7173 case BFD_QNT_CORE_FPREG: return elfcore_grok_prfpreg (abfd, note);
7174 default: return TRUE;
7178 /* Function: elfcore_write_note
7180 Inputs:
7181 buffer to hold note
7182 name of note
7183 type of note
7184 data for note
7185 size of data for note
7187 Return:
7188 End of buffer containing note. */
7190 char *
7191 elfcore_write_note (bfd *abfd,
7192 char *buf,
7193 int *bufsiz,
7194 const char *name,
7195 int type,
7196 const void *input,
7197 int size)
7199 Elf_External_Note *xnp;
7200 size_t namesz;
7201 size_t pad;
7202 size_t newspace;
7203 char *p, *dest;
7205 namesz = 0;
7206 pad = 0;
7207 if (name != NULL)
7209 const struct elf_backend_data *bed;
7211 namesz = strlen (name) + 1;
7212 bed = get_elf_backend_data (abfd);
7213 pad = -namesz & ((1 << bed->s->log_file_align) - 1);
7216 newspace = 12 + namesz + pad + size;
7218 p = realloc (buf, *bufsiz + newspace);
7219 dest = p + *bufsiz;
7220 *bufsiz += newspace;
7221 xnp = (Elf_External_Note *) dest;
7222 H_PUT_32 (abfd, namesz, xnp->namesz);
7223 H_PUT_32 (abfd, size, xnp->descsz);
7224 H_PUT_32 (abfd, type, xnp->type);
7225 dest = xnp->name;
7226 if (name != NULL)
7228 memcpy (dest, name, namesz);
7229 dest += namesz;
7230 while (pad != 0)
7232 *dest++ = '\0';
7233 --pad;
7236 memcpy (dest, input, size);
7237 return p;
7240 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7241 char *
7242 elfcore_write_prpsinfo (bfd *abfd,
7243 char *buf,
7244 int *bufsiz,
7245 const char *fname,
7246 const char *psargs)
7248 int note_type;
7249 char *note_name = "CORE";
7251 #if defined (HAVE_PSINFO_T)
7252 psinfo_t data;
7253 note_type = NT_PSINFO;
7254 #else
7255 prpsinfo_t data;
7256 note_type = NT_PRPSINFO;
7257 #endif
7259 memset (&data, 0, sizeof (data));
7260 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
7261 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
7262 return elfcore_write_note (abfd, buf, bufsiz,
7263 note_name, note_type, &data, sizeof (data));
7265 #endif /* PSINFO_T or PRPSINFO_T */
7267 #if defined (HAVE_PRSTATUS_T)
7268 char *
7269 elfcore_write_prstatus (bfd *abfd,
7270 char *buf,
7271 int *bufsiz,
7272 long pid,
7273 int cursig,
7274 const void *gregs)
7276 prstatus_t prstat;
7277 char *note_name = "CORE";
7279 memset (&prstat, 0, sizeof (prstat));
7280 prstat.pr_pid = pid;
7281 prstat.pr_cursig = cursig;
7282 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
7283 return elfcore_write_note (abfd, buf, bufsiz,
7284 note_name, NT_PRSTATUS, &prstat, sizeof (prstat));
7286 #endif /* HAVE_PRSTATUS_T */
7288 #if defined (HAVE_LWPSTATUS_T)
7289 char *
7290 elfcore_write_lwpstatus (bfd *abfd,
7291 char *buf,
7292 int *bufsiz,
7293 long pid,
7294 int cursig,
7295 const void *gregs)
7297 lwpstatus_t lwpstat;
7298 char *note_name = "CORE";
7300 memset (&lwpstat, 0, sizeof (lwpstat));
7301 lwpstat.pr_lwpid = pid >> 16;
7302 lwpstat.pr_cursig = cursig;
7303 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7304 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
7305 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7306 #if !defined(gregs)
7307 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
7308 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
7309 #else
7310 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
7311 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
7312 #endif
7313 #endif
7314 return elfcore_write_note (abfd, buf, bufsiz, note_name,
7315 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
7317 #endif /* HAVE_LWPSTATUS_T */
7319 #if defined (HAVE_PSTATUS_T)
7320 char *
7321 elfcore_write_pstatus (bfd *abfd,
7322 char *buf,
7323 int *bufsiz,
7324 long pid,
7325 int cursig,
7326 const void *gregs)
7328 pstatus_t pstat;
7329 char *note_name = "CORE";
7331 memset (&pstat, 0, sizeof (pstat));
7332 pstat.pr_pid = pid & 0xffff;
7333 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
7334 NT_PSTATUS, &pstat, sizeof (pstat));
7335 return buf;
7337 #endif /* HAVE_PSTATUS_T */
7339 char *
7340 elfcore_write_prfpreg (bfd *abfd,
7341 char *buf,
7342 int *bufsiz,
7343 const void *fpregs,
7344 int size)
7346 char *note_name = "CORE";
7347 return elfcore_write_note (abfd, buf, bufsiz,
7348 note_name, NT_FPREGSET, fpregs, size);
7351 char *
7352 elfcore_write_prxfpreg (bfd *abfd,
7353 char *buf,
7354 int *bufsiz,
7355 const void *xfpregs,
7356 int size)
7358 char *note_name = "LINUX";
7359 return elfcore_write_note (abfd, buf, bufsiz,
7360 note_name, NT_PRXFPREG, xfpregs, size);
7363 static bfd_boolean
7364 elfcore_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
7366 char *buf;
7367 char *p;
7369 if (size <= 0)
7370 return TRUE;
7372 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
7373 return FALSE;
7375 buf = bfd_malloc (size);
7376 if (buf == NULL)
7377 return FALSE;
7379 if (bfd_bread (buf, size, abfd) != size)
7381 error:
7382 free (buf);
7383 return FALSE;
7386 p = buf;
7387 while (p < buf + size)
7389 /* FIXME: bad alignment assumption. */
7390 Elf_External_Note *xnp = (Elf_External_Note *) p;
7391 Elf_Internal_Note in;
7393 in.type = H_GET_32 (abfd, xnp->type);
7395 in.namesz = H_GET_32 (abfd, xnp->namesz);
7396 in.namedata = xnp->name;
7398 in.descsz = H_GET_32 (abfd, xnp->descsz);
7399 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
7400 in.descpos = offset + (in.descdata - buf);
7402 if (strncmp (in.namedata, "NetBSD-CORE", 11) == 0)
7404 if (! elfcore_grok_netbsd_note (abfd, &in))
7405 goto error;
7407 else if (strncmp (in.namedata, "QNX", 3) == 0)
7409 if (! elfcore_grok_nto_note (abfd, &in))
7410 goto error;
7412 else
7414 if (! elfcore_grok_note (abfd, &in))
7415 goto error;
7418 p = in.descdata + BFD_ALIGN (in.descsz, 4);
7421 free (buf);
7422 return TRUE;
7425 /* Providing external access to the ELF program header table. */
7427 /* Return an upper bound on the number of bytes required to store a
7428 copy of ABFD's program header table entries. Return -1 if an error
7429 occurs; bfd_get_error will return an appropriate code. */
7431 long
7432 bfd_get_elf_phdr_upper_bound (bfd *abfd)
7434 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7436 bfd_set_error (bfd_error_wrong_format);
7437 return -1;
7440 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
7443 /* Copy ABFD's program header table entries to *PHDRS. The entries
7444 will be stored as an array of Elf_Internal_Phdr structures, as
7445 defined in include/elf/internal.h. To find out how large the
7446 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
7448 Return the number of program header table entries read, or -1 if an
7449 error occurs; bfd_get_error will return an appropriate code. */
7452 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
7454 int num_phdrs;
7456 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7458 bfd_set_error (bfd_error_wrong_format);
7459 return -1;
7462 num_phdrs = elf_elfheader (abfd)->e_phnum;
7463 memcpy (phdrs, elf_tdata (abfd)->phdr,
7464 num_phdrs * sizeof (Elf_Internal_Phdr));
7466 return num_phdrs;
7469 void
7470 _bfd_elf_sprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, char *buf, bfd_vma value)
7472 #ifdef BFD64
7473 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7475 i_ehdrp = elf_elfheader (abfd);
7476 if (i_ehdrp == NULL)
7477 sprintf_vma (buf, value);
7478 else
7480 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
7482 #if BFD_HOST_64BIT_LONG
7483 sprintf (buf, "%016lx", value);
7484 #else
7485 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
7486 _bfd_int64_low (value));
7487 #endif
7489 else
7490 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
7492 #else
7493 sprintf_vma (buf, value);
7494 #endif
7497 void
7498 _bfd_elf_fprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, void *stream, bfd_vma value)
7500 #ifdef BFD64
7501 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7503 i_ehdrp = elf_elfheader (abfd);
7504 if (i_ehdrp == NULL)
7505 fprintf_vma ((FILE *) stream, value);
7506 else
7508 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
7510 #if BFD_HOST_64BIT_LONG
7511 fprintf ((FILE *) stream, "%016lx", value);
7512 #else
7513 fprintf ((FILE *) stream, "%08lx%08lx",
7514 _bfd_int64_high (value), _bfd_int64_low (value));
7515 #endif
7517 else
7518 fprintf ((FILE *) stream, "%08lx",
7519 (unsigned long) (value & 0xffffffff));
7521 #else
7522 fprintf_vma ((FILE *) stream, value);
7523 #endif
7526 enum elf_reloc_type_class
7527 _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
7529 return reloc_class_normal;
7532 /* For RELA architectures, return the relocation value for a
7533 relocation against a local symbol. */
7535 bfd_vma
7536 _bfd_elf_rela_local_sym (bfd *abfd,
7537 Elf_Internal_Sym *sym,
7538 asection **psec,
7539 Elf_Internal_Rela *rel)
7541 asection *sec = *psec;
7542 bfd_vma relocation;
7544 relocation = (sec->output_section->vma
7545 + sec->output_offset
7546 + sym->st_value);
7547 if ((sec->flags & SEC_MERGE)
7548 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
7549 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
7551 rel->r_addend =
7552 _bfd_merged_section_offset (abfd, psec,
7553 elf_section_data (sec)->sec_info,
7554 sym->st_value + rel->r_addend);
7555 if (sec != *psec)
7557 /* If we have changed the section, and our original section is
7558 marked with SEC_EXCLUDE, it means that the original
7559 SEC_MERGE section has been completely subsumed in some
7560 other SEC_MERGE section. In this case, we need to leave
7561 some info around for --emit-relocs. */
7562 if ((sec->flags & SEC_EXCLUDE) != 0)
7563 sec->kept_section = *psec;
7564 sec = *psec;
7566 rel->r_addend -= relocation;
7567 rel->r_addend += sec->output_section->vma + sec->output_offset;
7569 return relocation;
7572 bfd_vma
7573 _bfd_elf_rel_local_sym (bfd *abfd,
7574 Elf_Internal_Sym *sym,
7575 asection **psec,
7576 bfd_vma addend)
7578 asection *sec = *psec;
7580 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
7581 return sym->st_value + addend;
7583 return _bfd_merged_section_offset (abfd, psec,
7584 elf_section_data (sec)->sec_info,
7585 sym->st_value + addend);
7588 bfd_vma
7589 _bfd_elf_section_offset (bfd *abfd,
7590 struct bfd_link_info *info ATTRIBUTE_UNUSED,
7591 asection *sec,
7592 bfd_vma offset)
7594 switch (sec->sec_info_type)
7596 case ELF_INFO_TYPE_STABS:
7597 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
7598 offset);
7599 case ELF_INFO_TYPE_EH_FRAME:
7600 return _bfd_elf_eh_frame_section_offset (abfd, sec, offset);
7601 default:
7602 return offset;
7606 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
7607 reconstruct an ELF file by reading the segments out of remote memory
7608 based on the ELF file header at EHDR_VMA and the ELF program headers it
7609 points to. If not null, *LOADBASEP is filled in with the difference
7610 between the VMAs from which the segments were read, and the VMAs the
7611 file headers (and hence BFD's idea of each section's VMA) put them at.
7613 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
7614 remote memory at target address VMA into the local buffer at MYADDR; it
7615 should return zero on success or an `errno' code on failure. TEMPL must
7616 be a BFD for an ELF target with the word size and byte order found in
7617 the remote memory. */
7619 bfd *
7620 bfd_elf_bfd_from_remote_memory
7621 (bfd *templ,
7622 bfd_vma ehdr_vma,
7623 bfd_vma *loadbasep,
7624 int (*target_read_memory) (bfd_vma, char *, int))
7626 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
7627 (templ, ehdr_vma, loadbasep, target_read_memory);
7630 long
7631 _bfd_elf_get_synthetic_symtab (bfd *abfd, asymbol **dynsyms, asymbol **ret)
7633 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7634 asection *relplt;
7635 asymbol *s;
7636 const char *relplt_name;
7637 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
7638 arelent *p;
7639 long count, i, n;
7640 size_t size;
7641 Elf_Internal_Shdr *hdr;
7642 char *names;
7643 asection *plt;
7645 *ret = NULL;
7646 if (!bed->plt_sym_val)
7647 return 0;
7649 relplt_name = bed->relplt_name;
7650 if (relplt_name == NULL)
7651 relplt_name = bed->default_use_rela_p ? ".rela.plt" : ".rel.plt";
7652 relplt = bfd_get_section_by_name (abfd, relplt_name);
7653 if (relplt == NULL)
7654 return 0;
7656 hdr = &elf_section_data (relplt)->this_hdr;
7657 if (hdr->sh_link != elf_dynsymtab (abfd)
7658 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
7659 return 0;
7661 plt = bfd_get_section_by_name (abfd, ".plt");
7662 if (plt == NULL)
7663 return 0;
7665 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
7666 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
7667 return -1;
7669 count = relplt->size / hdr->sh_entsize;
7670 size = count * sizeof (asymbol);
7671 p = relplt->relocation;
7672 for (i = 0; i < count; i++, s++, p++)
7673 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
7675 s = *ret = bfd_malloc (size);
7676 if (s == NULL)
7677 return -1;
7679 names = (char *) (s + count);
7680 p = relplt->relocation;
7681 n = 0;
7682 for (i = 0; i < count; i++, s++, p++)
7684 size_t len;
7685 bfd_vma addr;
7687 addr = bed->plt_sym_val (i, plt, p);
7688 if (addr == (bfd_vma) -1)
7689 continue;
7691 *s = **p->sym_ptr_ptr;
7692 s->section = plt;
7693 s->value = addr - plt->vma;
7694 s->name = names;
7695 len = strlen ((*p->sym_ptr_ptr)->name);
7696 memcpy (names, (*p->sym_ptr_ptr)->name, len);
7697 names += len;
7698 memcpy (names, "@plt", sizeof ("@plt"));
7699 names += sizeof ("@plt");
7700 ++n;
7703 return n;