fixes for host gcc 4.6.1
[zpugcc/jano.git] / toolchain / binutils / bfd / elf.c
bloba14fd35148d25a5698da344b79c34034744ced43
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_discard_group (bfd *abfd ATTRIBUTE_UNUSED, asection *group)
618 asection *first = elf_next_in_group (group);
619 asection *s = first;
621 while (s != NULL)
623 s->output_section = bfd_abs_section_ptr;
624 s = elf_next_in_group (s);
625 /* These lists are circular. */
626 if (s == first)
627 break;
629 return TRUE;
632 /* Make a BFD section from an ELF section. We store a pointer to the
633 BFD section in the bfd_section field of the header. */
635 bfd_boolean
636 _bfd_elf_make_section_from_shdr (bfd *abfd,
637 Elf_Internal_Shdr *hdr,
638 const char *name)
640 asection *newsect;
641 flagword flags;
642 const struct elf_backend_data *bed;
644 if (hdr->bfd_section != NULL)
646 BFD_ASSERT (strcmp (name,
647 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
648 return TRUE;
651 newsect = bfd_make_section_anyway (abfd, name);
652 if (newsect == NULL)
653 return FALSE;
655 /* Always use the real type/flags. */
656 elf_section_type (newsect) = hdr->sh_type;
657 elf_section_flags (newsect) = hdr->sh_flags;
659 newsect->filepos = hdr->sh_offset;
661 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
662 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
663 || ! bfd_set_section_alignment (abfd, newsect,
664 bfd_log2 ((bfd_vma) hdr->sh_addralign)))
665 return FALSE;
667 flags = SEC_NO_FLAGS;
668 if (hdr->sh_type != SHT_NOBITS)
669 flags |= SEC_HAS_CONTENTS;
670 if (hdr->sh_type == SHT_GROUP)
671 flags |= SEC_GROUP | SEC_EXCLUDE;
672 if ((hdr->sh_flags & SHF_ALLOC) != 0)
674 flags |= SEC_ALLOC;
675 if (hdr->sh_type != SHT_NOBITS)
676 flags |= SEC_LOAD;
678 if ((hdr->sh_flags & SHF_WRITE) == 0)
679 flags |= SEC_READONLY;
680 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
681 flags |= SEC_CODE;
682 else if ((flags & SEC_LOAD) != 0)
683 flags |= SEC_DATA;
684 if ((hdr->sh_flags & SHF_MERGE) != 0)
686 flags |= SEC_MERGE;
687 newsect->entsize = hdr->sh_entsize;
688 if ((hdr->sh_flags & SHF_STRINGS) != 0)
689 flags |= SEC_STRINGS;
691 if (hdr->sh_flags & SHF_GROUP)
692 if (!setup_group (abfd, hdr, newsect))
693 return FALSE;
694 if ((hdr->sh_flags & SHF_TLS) != 0)
695 flags |= SEC_THREAD_LOCAL;
697 /* The debugging sections appear to be recognized only by name, not
698 any sort of flag. */
700 static const char *debug_sec_names [] =
702 ".debug",
703 ".gnu.linkonce.wi.",
704 ".line",
705 ".stab"
707 int i;
709 for (i = ARRAY_SIZE (debug_sec_names); i--;)
710 if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0)
711 break;
713 if (i >= 0)
714 flags |= SEC_DEBUGGING;
717 /* As a GNU extension, if the name begins with .gnu.linkonce, we
718 only link a single copy of the section. This is used to support
719 g++. g++ will emit each template expansion in its own section.
720 The symbols will be defined as weak, so that multiple definitions
721 are permitted. The GNU linker extension is to actually discard
722 all but one of the sections. */
723 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0
724 && elf_next_in_group (newsect) == NULL)
725 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
727 bed = get_elf_backend_data (abfd);
728 if (bed->elf_backend_section_flags)
729 if (! bed->elf_backend_section_flags (&flags, hdr))
730 return FALSE;
732 if (! bfd_set_section_flags (abfd, newsect, flags))
733 return FALSE;
735 if ((flags & SEC_ALLOC) != 0)
737 Elf_Internal_Phdr *phdr;
738 unsigned int i;
740 /* Look through the phdrs to see if we need to adjust the lma.
741 If all the p_paddr fields are zero, we ignore them, since
742 some ELF linkers produce such output. */
743 phdr = elf_tdata (abfd)->phdr;
744 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
746 if (phdr->p_paddr != 0)
747 break;
749 if (i < elf_elfheader (abfd)->e_phnum)
751 phdr = elf_tdata (abfd)->phdr;
752 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
754 /* This section is part of this segment if its file
755 offset plus size lies within the segment's memory
756 span and, if the section is loaded, the extent of the
757 loaded data lies within the extent of the segment.
759 Note - we used to check the p_paddr field as well, and
760 refuse to set the LMA if it was 0. This is wrong
761 though, as a perfectly valid initialised segment can
762 have a p_paddr of zero. Some architectures, eg ARM,
763 place special significance on the address 0 and
764 executables need to be able to have a segment which
765 covers this address. */
766 if (phdr->p_type == PT_LOAD
767 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
768 && (hdr->sh_offset + hdr->sh_size
769 <= phdr->p_offset + phdr->p_memsz)
770 && ((flags & SEC_LOAD) == 0
771 || (hdr->sh_offset + hdr->sh_size
772 <= phdr->p_offset + phdr->p_filesz)))
774 if ((flags & SEC_LOAD) == 0)
775 newsect->lma = (phdr->p_paddr
776 + hdr->sh_addr - phdr->p_vaddr);
777 else
778 /* We used to use the same adjustment for SEC_LOAD
779 sections, but that doesn't work if the segment
780 is packed with code from multiple VMAs.
781 Instead we calculate the section LMA based on
782 the segment LMA. It is assumed that the
783 segment will contain sections with contiguous
784 LMAs, even if the VMAs are not. */
785 newsect->lma = (phdr->p_paddr
786 + hdr->sh_offset - phdr->p_offset);
788 /* With contiguous segments, we can't tell from file
789 offsets whether a section with zero size should
790 be placed at the end of one segment or the
791 beginning of the next. Decide based on vaddr. */
792 if (hdr->sh_addr >= phdr->p_vaddr
793 && (hdr->sh_addr + hdr->sh_size
794 <= phdr->p_vaddr + phdr->p_memsz))
795 break;
801 hdr->bfd_section = newsect;
802 elf_section_data (newsect)->this_hdr = *hdr;
804 return TRUE;
808 INTERNAL_FUNCTION
809 bfd_elf_find_section
811 SYNOPSIS
812 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
814 DESCRIPTION
815 Helper functions for GDB to locate the string tables.
816 Since BFD hides string tables from callers, GDB needs to use an
817 internal hook to find them. Sun's .stabstr, in particular,
818 isn't even pointed to by the .stab section, so ordinary
819 mechanisms wouldn't work to find it, even if we had some.
822 struct elf_internal_shdr *
823 bfd_elf_find_section (bfd *abfd, char *name)
825 Elf_Internal_Shdr **i_shdrp;
826 char *shstrtab;
827 unsigned int max;
828 unsigned int i;
830 i_shdrp = elf_elfsections (abfd);
831 if (i_shdrp != NULL)
833 shstrtab = bfd_elf_get_str_section (abfd,
834 elf_elfheader (abfd)->e_shstrndx);
835 if (shstrtab != NULL)
837 max = elf_numsections (abfd);
838 for (i = 1; i < max; i++)
839 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
840 return i_shdrp[i];
843 return 0;
846 const char *const bfd_elf_section_type_names[] = {
847 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
848 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
849 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
852 /* ELF relocs are against symbols. If we are producing relocatable
853 output, and the reloc is against an external symbol, and nothing
854 has given us any additional addend, the resulting reloc will also
855 be against the same symbol. In such a case, we don't want to
856 change anything about the way the reloc is handled, since it will
857 all be done at final link time. Rather than put special case code
858 into bfd_perform_relocation, all the reloc types use this howto
859 function. It just short circuits the reloc if producing
860 relocatable output against an external symbol. */
862 bfd_reloc_status_type
863 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
864 arelent *reloc_entry,
865 asymbol *symbol,
866 void *data ATTRIBUTE_UNUSED,
867 asection *input_section,
868 bfd *output_bfd,
869 char **error_message ATTRIBUTE_UNUSED)
871 if (output_bfd != NULL
872 && (symbol->flags & BSF_SECTION_SYM) == 0
873 && (! reloc_entry->howto->partial_inplace
874 || reloc_entry->addend == 0))
876 reloc_entry->address += input_section->output_offset;
877 return bfd_reloc_ok;
880 return bfd_reloc_continue;
883 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
885 static void
886 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
887 asection *sec)
889 BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE);
890 sec->sec_info_type = ELF_INFO_TYPE_NONE;
893 /* Finish SHF_MERGE section merging. */
895 bfd_boolean
896 _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
898 if (!is_elf_hash_table (info->hash))
899 return FALSE;
900 if (elf_hash_table (info)->merge_info)
901 _bfd_merge_sections (abfd, elf_hash_table (info)->merge_info,
902 merge_sections_remove_hook);
903 return TRUE;
906 void
907 _bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
909 sec->output_section = bfd_abs_section_ptr;
910 sec->output_offset = sec->vma;
911 if (!is_elf_hash_table (info->hash))
912 return;
914 sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
917 /* Copy the program header and other data from one object module to
918 another. */
920 bfd_boolean
921 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
923 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
924 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
925 return TRUE;
927 BFD_ASSERT (!elf_flags_init (obfd)
928 || (elf_elfheader (obfd)->e_flags
929 == elf_elfheader (ibfd)->e_flags));
931 elf_gp (obfd) = elf_gp (ibfd);
932 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
933 elf_flags_init (obfd) = TRUE;
934 return TRUE;
937 /* Print out the program headers. */
939 bfd_boolean
940 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
942 FILE *f = farg;
943 Elf_Internal_Phdr *p;
944 asection *s;
945 bfd_byte *dynbuf = NULL;
947 p = elf_tdata (abfd)->phdr;
948 if (p != NULL)
950 unsigned int i, c;
952 fprintf (f, _("\nProgram Header:\n"));
953 c = elf_elfheader (abfd)->e_phnum;
954 for (i = 0; i < c; i++, p++)
956 const char *pt;
957 char buf[20];
959 switch (p->p_type)
961 case PT_NULL: pt = "NULL"; break;
962 case PT_LOAD: pt = "LOAD"; break;
963 case PT_DYNAMIC: pt = "DYNAMIC"; break;
964 case PT_INTERP: pt = "INTERP"; break;
965 case PT_NOTE: pt = "NOTE"; break;
966 case PT_SHLIB: pt = "SHLIB"; break;
967 case PT_PHDR: pt = "PHDR"; break;
968 case PT_TLS: pt = "TLS"; break;
969 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
970 case PT_GNU_STACK: pt = "STACK"; break;
971 default: sprintf (buf, "0x%lx", p->p_type); pt = buf; break;
973 fprintf (f, "%8s off 0x", pt);
974 bfd_fprintf_vma (abfd, f, p->p_offset);
975 fprintf (f, " vaddr 0x");
976 bfd_fprintf_vma (abfd, f, p->p_vaddr);
977 fprintf (f, " paddr 0x");
978 bfd_fprintf_vma (abfd, f, p->p_paddr);
979 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
980 fprintf (f, " filesz 0x");
981 bfd_fprintf_vma (abfd, f, p->p_filesz);
982 fprintf (f, " memsz 0x");
983 bfd_fprintf_vma (abfd, f, p->p_memsz);
984 fprintf (f, " flags %c%c%c",
985 (p->p_flags & PF_R) != 0 ? 'r' : '-',
986 (p->p_flags & PF_W) != 0 ? 'w' : '-',
987 (p->p_flags & PF_X) != 0 ? 'x' : '-');
988 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
989 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
990 fprintf (f, "\n");
994 s = bfd_get_section_by_name (abfd, ".dynamic");
995 if (s != NULL)
997 int elfsec;
998 unsigned long shlink;
999 bfd_byte *extdyn, *extdynend;
1000 size_t extdynsize;
1001 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1003 fprintf (f, _("\nDynamic Section:\n"));
1005 dynbuf = bfd_malloc (s->_raw_size);
1006 if (dynbuf == NULL)
1007 goto error_return;
1008 if (! bfd_get_section_contents (abfd, s, dynbuf, 0, s->_raw_size))
1009 goto error_return;
1011 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1012 if (elfsec == -1)
1013 goto error_return;
1014 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1016 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1017 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1019 extdyn = dynbuf;
1020 extdynend = extdyn + s->_raw_size;
1021 for (; extdyn < extdynend; extdyn += extdynsize)
1023 Elf_Internal_Dyn dyn;
1024 const char *name;
1025 char ab[20];
1026 bfd_boolean stringp;
1028 (*swap_dyn_in) (abfd, extdyn, &dyn);
1030 if (dyn.d_tag == DT_NULL)
1031 break;
1033 stringp = FALSE;
1034 switch (dyn.d_tag)
1036 default:
1037 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1038 name = ab;
1039 break;
1041 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1042 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1043 case DT_PLTGOT: name = "PLTGOT"; break;
1044 case DT_HASH: name = "HASH"; break;
1045 case DT_STRTAB: name = "STRTAB"; break;
1046 case DT_SYMTAB: name = "SYMTAB"; break;
1047 case DT_RELA: name = "RELA"; break;
1048 case DT_RELASZ: name = "RELASZ"; break;
1049 case DT_RELAENT: name = "RELAENT"; break;
1050 case DT_STRSZ: name = "STRSZ"; break;
1051 case DT_SYMENT: name = "SYMENT"; break;
1052 case DT_INIT: name = "INIT"; break;
1053 case DT_FINI: name = "FINI"; break;
1054 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1055 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1056 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1057 case DT_REL: name = "REL"; break;
1058 case DT_RELSZ: name = "RELSZ"; break;
1059 case DT_RELENT: name = "RELENT"; break;
1060 case DT_PLTREL: name = "PLTREL"; break;
1061 case DT_DEBUG: name = "DEBUG"; break;
1062 case DT_TEXTREL: name = "TEXTREL"; break;
1063 case DT_JMPREL: name = "JMPREL"; break;
1064 case DT_BIND_NOW: name = "BIND_NOW"; break;
1065 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1066 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1067 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1068 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1069 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1070 case DT_FLAGS: name = "FLAGS"; break;
1071 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1072 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1073 case DT_CHECKSUM: name = "CHECKSUM"; break;
1074 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1075 case DT_MOVEENT: name = "MOVEENT"; break;
1076 case DT_MOVESZ: name = "MOVESZ"; break;
1077 case DT_FEATURE: name = "FEATURE"; break;
1078 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1079 case DT_SYMINSZ: name = "SYMINSZ"; break;
1080 case DT_SYMINENT: name = "SYMINENT"; break;
1081 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1082 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1083 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1084 case DT_PLTPAD: name = "PLTPAD"; break;
1085 case DT_MOVETAB: name = "MOVETAB"; break;
1086 case DT_SYMINFO: name = "SYMINFO"; break;
1087 case DT_RELACOUNT: name = "RELACOUNT"; break;
1088 case DT_RELCOUNT: name = "RELCOUNT"; break;
1089 case DT_FLAGS_1: name = "FLAGS_1"; break;
1090 case DT_VERSYM: name = "VERSYM"; break;
1091 case DT_VERDEF: name = "VERDEF"; break;
1092 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1093 case DT_VERNEED: name = "VERNEED"; break;
1094 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1095 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1096 case DT_USED: name = "USED"; break;
1097 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1100 fprintf (f, " %-11s ", name);
1101 if (! stringp)
1102 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
1103 else
1105 const char *string;
1106 unsigned int tagv = dyn.d_un.d_val;
1108 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1109 if (string == NULL)
1110 goto error_return;
1111 fprintf (f, "%s", string);
1113 fprintf (f, "\n");
1116 free (dynbuf);
1117 dynbuf = NULL;
1120 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1121 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1123 if (! _bfd_elf_slurp_version_tables (abfd))
1124 return FALSE;
1127 if (elf_dynverdef (abfd) != 0)
1129 Elf_Internal_Verdef *t;
1131 fprintf (f, _("\nVersion definitions:\n"));
1132 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1134 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1135 t->vd_flags, t->vd_hash, t->vd_nodename);
1136 if (t->vd_auxptr->vda_nextptr != NULL)
1138 Elf_Internal_Verdaux *a;
1140 fprintf (f, "\t");
1141 for (a = t->vd_auxptr->vda_nextptr;
1142 a != NULL;
1143 a = a->vda_nextptr)
1144 fprintf (f, "%s ", a->vda_nodename);
1145 fprintf (f, "\n");
1150 if (elf_dynverref (abfd) != 0)
1152 Elf_Internal_Verneed *t;
1154 fprintf (f, _("\nVersion References:\n"));
1155 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1157 Elf_Internal_Vernaux *a;
1159 fprintf (f, _(" required from %s:\n"), t->vn_filename);
1160 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1161 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1162 a->vna_flags, a->vna_other, a->vna_nodename);
1166 return TRUE;
1168 error_return:
1169 if (dynbuf != NULL)
1170 free (dynbuf);
1171 return FALSE;
1174 /* Display ELF-specific fields of a symbol. */
1176 void
1177 bfd_elf_print_symbol (bfd *abfd,
1178 void *filep,
1179 asymbol *symbol,
1180 bfd_print_symbol_type how)
1182 FILE *file = filep;
1183 switch (how)
1185 case bfd_print_symbol_name:
1186 fprintf (file, "%s", symbol->name);
1187 break;
1188 case bfd_print_symbol_more:
1189 fprintf (file, "elf ");
1190 bfd_fprintf_vma (abfd, file, symbol->value);
1191 fprintf (file, " %lx", (long) symbol->flags);
1192 break;
1193 case bfd_print_symbol_all:
1195 const char *section_name;
1196 const char *name = NULL;
1197 const struct elf_backend_data *bed;
1198 unsigned char st_other;
1199 bfd_vma val;
1201 section_name = symbol->section ? symbol->section->name : "(*none*)";
1203 bed = get_elf_backend_data (abfd);
1204 if (bed->elf_backend_print_symbol_all)
1205 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1207 if (name == NULL)
1209 name = symbol->name;
1210 bfd_print_symbol_vandf (abfd, file, symbol);
1213 fprintf (file, " %s\t", section_name);
1214 /* Print the "other" value for a symbol. For common symbols,
1215 we've already printed the size; now print the alignment.
1216 For other symbols, we have no specified alignment, and
1217 we've printed the address; now print the size. */
1218 if (bfd_is_com_section (symbol->section))
1219 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1220 else
1221 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1222 bfd_fprintf_vma (abfd, file, val);
1224 /* If we have version information, print it. */
1225 if (elf_tdata (abfd)->dynversym_section != 0
1226 && (elf_tdata (abfd)->dynverdef_section != 0
1227 || elf_tdata (abfd)->dynverref_section != 0))
1229 unsigned int vernum;
1230 const char *version_string;
1232 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1234 if (vernum == 0)
1235 version_string = "";
1236 else if (vernum == 1)
1237 version_string = "Base";
1238 else if (vernum <= elf_tdata (abfd)->cverdefs)
1239 version_string =
1240 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1241 else
1243 Elf_Internal_Verneed *t;
1245 version_string = "";
1246 for (t = elf_tdata (abfd)->verref;
1247 t != NULL;
1248 t = t->vn_nextref)
1250 Elf_Internal_Vernaux *a;
1252 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1254 if (a->vna_other == vernum)
1256 version_string = a->vna_nodename;
1257 break;
1263 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1264 fprintf (file, " %-11s", version_string);
1265 else
1267 int i;
1269 fprintf (file, " (%s)", version_string);
1270 for (i = 10 - strlen (version_string); i > 0; --i)
1271 putc (' ', file);
1275 /* If the st_other field is not zero, print it. */
1276 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1278 switch (st_other)
1280 case 0: break;
1281 case STV_INTERNAL: fprintf (file, " .internal"); break;
1282 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1283 case STV_PROTECTED: fprintf (file, " .protected"); break;
1284 default:
1285 /* Some other non-defined flags are also present, so print
1286 everything hex. */
1287 fprintf (file, " 0x%02x", (unsigned int) st_other);
1290 fprintf (file, " %s", name);
1292 break;
1296 /* Create an entry in an ELF linker hash table. */
1298 struct bfd_hash_entry *
1299 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1300 struct bfd_hash_table *table,
1301 const char *string)
1303 /* Allocate the structure if it has not already been allocated by a
1304 subclass. */
1305 if (entry == NULL)
1307 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
1308 if (entry == NULL)
1309 return entry;
1312 /* Call the allocation method of the superclass. */
1313 entry = _bfd_link_hash_newfunc (entry, table, string);
1314 if (entry != NULL)
1316 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
1317 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
1319 /* Set local fields. */
1320 ret->indx = -1;
1321 ret->dynindx = -1;
1322 ret->dynstr_index = 0;
1323 ret->elf_hash_value = 0;
1324 ret->weakdef = NULL;
1325 ret->verinfo.verdef = NULL;
1326 ret->vtable_entries_size = 0;
1327 ret->vtable_entries_used = NULL;
1328 ret->vtable_parent = NULL;
1329 ret->got = htab->init_refcount;
1330 ret->plt = htab->init_refcount;
1331 ret->size = 0;
1332 ret->type = STT_NOTYPE;
1333 ret->other = 0;
1334 /* Assume that we have been called by a non-ELF symbol reader.
1335 This flag is then reset by the code which reads an ELF input
1336 file. This ensures that a symbol created by a non-ELF symbol
1337 reader will have the flag set correctly. */
1338 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
1341 return entry;
1344 /* Copy data from an indirect symbol to its direct symbol, hiding the
1345 old indirect symbol. Also used for copying flags to a weakdef. */
1347 void
1348 _bfd_elf_link_hash_copy_indirect (const struct elf_backend_data *bed,
1349 struct elf_link_hash_entry *dir,
1350 struct elf_link_hash_entry *ind)
1352 bfd_signed_vma tmp;
1353 bfd_signed_vma lowest_valid = bed->can_refcount;
1355 /* Copy down any references that we may have already seen to the
1356 symbol which just became indirect. */
1358 dir->elf_link_hash_flags
1359 |= ind->elf_link_hash_flags & (ELF_LINK_HASH_REF_DYNAMIC
1360 | ELF_LINK_HASH_REF_REGULAR
1361 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1362 | ELF_LINK_NON_GOT_REF
1363 | ELF_LINK_HASH_NEEDS_PLT
1364 | ELF_LINK_POINTER_EQUALITY_NEEDED);
1366 if (ind->root.type != bfd_link_hash_indirect)
1367 return;
1369 /* Copy over the global and procedure linkage table refcount entries.
1370 These may have been already set up by a check_relocs routine. */
1371 tmp = dir->got.refcount;
1372 if (tmp < lowest_valid)
1374 dir->got.refcount = ind->got.refcount;
1375 ind->got.refcount = tmp;
1377 else
1378 BFD_ASSERT (ind->got.refcount < lowest_valid);
1380 tmp = dir->plt.refcount;
1381 if (tmp < lowest_valid)
1383 dir->plt.refcount = ind->plt.refcount;
1384 ind->plt.refcount = tmp;
1386 else
1387 BFD_ASSERT (ind->plt.refcount < lowest_valid);
1389 if (dir->dynindx == -1)
1391 dir->dynindx = ind->dynindx;
1392 dir->dynstr_index = ind->dynstr_index;
1393 ind->dynindx = -1;
1394 ind->dynstr_index = 0;
1396 else
1397 BFD_ASSERT (ind->dynindx == -1);
1400 void
1401 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
1402 struct elf_link_hash_entry *h,
1403 bfd_boolean force_local)
1405 h->plt = elf_hash_table (info)->init_offset;
1406 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1407 if (force_local)
1409 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
1410 if (h->dynindx != -1)
1412 h->dynindx = -1;
1413 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1414 h->dynstr_index);
1419 /* Initialize an ELF linker hash table. */
1421 bfd_boolean
1422 _bfd_elf_link_hash_table_init
1423 (struct elf_link_hash_table *table,
1424 bfd *abfd,
1425 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
1426 struct bfd_hash_table *,
1427 const char *))
1429 bfd_boolean ret;
1431 table->dynamic_sections_created = FALSE;
1432 table->dynobj = NULL;
1433 /* Make sure can_refcount is extended to the width and signedness of
1434 init_refcount before we subtract one from it. */
1435 table->init_refcount.refcount = get_elf_backend_data (abfd)->can_refcount;
1436 table->init_refcount.refcount -= 1;
1437 table->init_offset.offset = -(bfd_vma) 1;
1438 /* The first dynamic symbol is a dummy. */
1439 table->dynsymcount = 1;
1440 table->dynstr = NULL;
1441 table->bucketcount = 0;
1442 table->needed = NULL;
1443 table->hgot = NULL;
1444 table->stab_info = NULL;
1445 table->merge_info = NULL;
1446 memset (&table->eh_info, 0, sizeof (table->eh_info));
1447 table->dynlocal = NULL;
1448 table->runpath = NULL;
1449 table->tls_sec = NULL;
1450 table->tls_size = 0;
1451 table->loaded = NULL;
1453 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc);
1454 table->root.type = bfd_link_elf_hash_table;
1456 return ret;
1459 /* Create an ELF linker hash table. */
1461 struct bfd_link_hash_table *
1462 _bfd_elf_link_hash_table_create (bfd *abfd)
1464 struct elf_link_hash_table *ret;
1465 bfd_size_type amt = sizeof (struct elf_link_hash_table);
1467 ret = bfd_malloc (amt);
1468 if (ret == NULL)
1469 return NULL;
1471 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
1473 free (ret);
1474 return NULL;
1477 return &ret->root;
1480 /* This is a hook for the ELF emulation code in the generic linker to
1481 tell the backend linker what file name to use for the DT_NEEDED
1482 entry for a dynamic object. */
1484 void
1485 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
1487 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1488 && bfd_get_format (abfd) == bfd_object)
1489 elf_dt_name (abfd) = name;
1492 void
1493 bfd_elf_set_dyn_lib_class (bfd *abfd, int lib_class)
1495 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1496 && bfd_get_format (abfd) == bfd_object)
1497 elf_dyn_lib_class (abfd) = lib_class;
1500 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1501 the linker ELF emulation code. */
1503 struct bfd_link_needed_list *
1504 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
1505 struct bfd_link_info *info)
1507 if (! is_elf_hash_table (info->hash))
1508 return NULL;
1509 return elf_hash_table (info)->needed;
1512 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1513 hook for the linker ELF emulation code. */
1515 struct bfd_link_needed_list *
1516 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
1517 struct bfd_link_info *info)
1519 if (! is_elf_hash_table (info->hash))
1520 return NULL;
1521 return elf_hash_table (info)->runpath;
1524 /* Get the name actually used for a dynamic object for a link. This
1525 is the SONAME entry if there is one. Otherwise, it is the string
1526 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1528 const char *
1529 bfd_elf_get_dt_soname (bfd *abfd)
1531 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1532 && bfd_get_format (abfd) == bfd_object)
1533 return elf_dt_name (abfd);
1534 return NULL;
1537 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1538 the ELF linker emulation code. */
1540 bfd_boolean
1541 bfd_elf_get_bfd_needed_list (bfd *abfd,
1542 struct bfd_link_needed_list **pneeded)
1544 asection *s;
1545 bfd_byte *dynbuf = NULL;
1546 int elfsec;
1547 unsigned long shlink;
1548 bfd_byte *extdyn, *extdynend;
1549 size_t extdynsize;
1550 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1552 *pneeded = NULL;
1554 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1555 || bfd_get_format (abfd) != bfd_object)
1556 return TRUE;
1558 s = bfd_get_section_by_name (abfd, ".dynamic");
1559 if (s == NULL || s->_raw_size == 0)
1560 return TRUE;
1562 dynbuf = bfd_malloc (s->_raw_size);
1563 if (dynbuf == NULL)
1564 goto error_return;
1566 if (! bfd_get_section_contents (abfd, s, dynbuf, 0, s->_raw_size))
1567 goto error_return;
1569 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1570 if (elfsec == -1)
1571 goto error_return;
1573 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1575 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1576 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1578 extdyn = dynbuf;
1579 extdynend = extdyn + s->_raw_size;
1580 for (; extdyn < extdynend; extdyn += extdynsize)
1582 Elf_Internal_Dyn dyn;
1584 (*swap_dyn_in) (abfd, extdyn, &dyn);
1586 if (dyn.d_tag == DT_NULL)
1587 break;
1589 if (dyn.d_tag == DT_NEEDED)
1591 const char *string;
1592 struct bfd_link_needed_list *l;
1593 unsigned int tagv = dyn.d_un.d_val;
1594 bfd_size_type amt;
1596 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1597 if (string == NULL)
1598 goto error_return;
1600 amt = sizeof *l;
1601 l = bfd_alloc (abfd, amt);
1602 if (l == NULL)
1603 goto error_return;
1605 l->by = abfd;
1606 l->name = string;
1607 l->next = *pneeded;
1608 *pneeded = l;
1612 free (dynbuf);
1614 return TRUE;
1616 error_return:
1617 if (dynbuf != NULL)
1618 free (dynbuf);
1619 return FALSE;
1622 /* Allocate an ELF string table--force the first byte to be zero. */
1624 struct bfd_strtab_hash *
1625 _bfd_elf_stringtab_init (void)
1627 struct bfd_strtab_hash *ret;
1629 ret = _bfd_stringtab_init ();
1630 if (ret != NULL)
1632 bfd_size_type loc;
1634 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1635 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1636 if (loc == (bfd_size_type) -1)
1638 _bfd_stringtab_free (ret);
1639 ret = NULL;
1642 return ret;
1645 /* ELF .o/exec file reading */
1647 /* Create a new bfd section from an ELF section header. */
1649 bfd_boolean
1650 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1652 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1653 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1654 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1655 const char *name;
1657 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1659 switch (hdr->sh_type)
1661 case SHT_NULL:
1662 /* Inactive section. Throw it away. */
1663 return TRUE;
1665 case SHT_PROGBITS: /* Normal section with contents. */
1666 case SHT_NOBITS: /* .bss section. */
1667 case SHT_HASH: /* .hash section. */
1668 case SHT_NOTE: /* .note section. */
1669 case SHT_INIT_ARRAY: /* .init_array section. */
1670 case SHT_FINI_ARRAY: /* .fini_array section. */
1671 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1672 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1674 case SHT_DYNAMIC: /* Dynamic linking information. */
1675 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1676 return FALSE;
1677 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1679 Elf_Internal_Shdr *dynsymhdr;
1681 /* The shared libraries distributed with hpux11 have a bogus
1682 sh_link field for the ".dynamic" section. Find the
1683 string table for the ".dynsym" section instead. */
1684 if (elf_dynsymtab (abfd) != 0)
1686 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1687 hdr->sh_link = dynsymhdr->sh_link;
1689 else
1691 unsigned int i, num_sec;
1693 num_sec = elf_numsections (abfd);
1694 for (i = 1; i < num_sec; i++)
1696 dynsymhdr = elf_elfsections (abfd)[i];
1697 if (dynsymhdr->sh_type == SHT_DYNSYM)
1699 hdr->sh_link = dynsymhdr->sh_link;
1700 break;
1705 break;
1707 case SHT_SYMTAB: /* A symbol table */
1708 if (elf_onesymtab (abfd) == shindex)
1709 return TRUE;
1711 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1712 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1713 elf_onesymtab (abfd) = shindex;
1714 elf_tdata (abfd)->symtab_hdr = *hdr;
1715 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1716 abfd->flags |= HAS_SYMS;
1718 /* Sometimes a shared object will map in the symbol table. If
1719 SHF_ALLOC is set, and this is a shared object, then we also
1720 treat this section as a BFD section. We can not base the
1721 decision purely on SHF_ALLOC, because that flag is sometimes
1722 set in a relocatable object file, which would confuse the
1723 linker. */
1724 if ((hdr->sh_flags & SHF_ALLOC) != 0
1725 && (abfd->flags & DYNAMIC) != 0
1726 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1727 return FALSE;
1729 return TRUE;
1731 case SHT_DYNSYM: /* A dynamic symbol table */
1732 if (elf_dynsymtab (abfd) == shindex)
1733 return TRUE;
1735 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1736 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1737 elf_dynsymtab (abfd) = shindex;
1738 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1739 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1740 abfd->flags |= HAS_SYMS;
1742 /* Besides being a symbol table, we also treat this as a regular
1743 section, so that objcopy can handle it. */
1744 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1746 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1747 if (elf_symtab_shndx (abfd) == shindex)
1748 return TRUE;
1750 /* Get the associated symbol table. */
1751 if (! bfd_section_from_shdr (abfd, hdr->sh_link)
1752 || hdr->sh_link != elf_onesymtab (abfd))
1753 return FALSE;
1755 elf_symtab_shndx (abfd) = shindex;
1756 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1757 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1758 return TRUE;
1760 case SHT_STRTAB: /* A string table */
1761 if (hdr->bfd_section != NULL)
1762 return TRUE;
1763 if (ehdr->e_shstrndx == shindex)
1765 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1766 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1767 return TRUE;
1770 unsigned int i, num_sec;
1772 num_sec = elf_numsections (abfd);
1773 for (i = 1; i < num_sec; i++)
1775 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1776 if (hdr2->sh_link == shindex)
1778 if (! bfd_section_from_shdr (abfd, i))
1779 return FALSE;
1780 if (elf_onesymtab (abfd) == i)
1782 elf_tdata (abfd)->strtab_hdr = *hdr;
1783 elf_elfsections (abfd)[shindex] =
1784 &elf_tdata (abfd)->strtab_hdr;
1785 return TRUE;
1787 if (elf_dynsymtab (abfd) == i)
1789 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1790 elf_elfsections (abfd)[shindex] = hdr =
1791 &elf_tdata (abfd)->dynstrtab_hdr;
1792 /* We also treat this as a regular section, so
1793 that objcopy can handle it. */
1794 break;
1796 #if 0 /* Not handling other string tables specially right now. */
1797 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1798 /* We have a strtab for some random other section. */
1799 newsect = (asection *) hdr2->bfd_section;
1800 if (!newsect)
1801 break;
1802 hdr->bfd_section = newsect;
1803 hdr2 = &elf_section_data (newsect)->str_hdr;
1804 *hdr2 = *hdr;
1805 elf_elfsections (abfd)[shindex] = hdr2;
1806 #endif
1811 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1813 case SHT_REL:
1814 case SHT_RELA:
1815 /* *These* do a lot of work -- but build no sections! */
1817 asection *target_sect;
1818 Elf_Internal_Shdr *hdr2;
1819 unsigned int num_sec = elf_numsections (abfd);
1821 /* Check for a bogus link to avoid crashing. */
1822 if ((hdr->sh_link >= SHN_LORESERVE && hdr->sh_link <= SHN_HIRESERVE)
1823 || hdr->sh_link >= num_sec)
1825 ((*_bfd_error_handler)
1826 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1827 bfd_archive_filename (abfd), hdr->sh_link, name, shindex));
1828 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1831 /* For some incomprehensible reason Oracle distributes
1832 libraries for Solaris in which some of the objects have
1833 bogus sh_link fields. It would be nice if we could just
1834 reject them, but, unfortunately, some people need to use
1835 them. We scan through the section headers; if we find only
1836 one suitable symbol table, we clobber the sh_link to point
1837 to it. I hope this doesn't break anything. */
1838 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1839 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1841 unsigned int scan;
1842 int found;
1844 found = 0;
1845 for (scan = 1; scan < num_sec; scan++)
1847 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1848 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1850 if (found != 0)
1852 found = 0;
1853 break;
1855 found = scan;
1858 if (found != 0)
1859 hdr->sh_link = found;
1862 /* Get the symbol table. */
1863 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1864 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1865 return FALSE;
1867 /* If this reloc section does not use the main symbol table we
1868 don't treat it as a reloc section. BFD can't adequately
1869 represent such a section, so at least for now, we don't
1870 try. We just present it as a normal section. We also
1871 can't use it as a reloc section if it points to the null
1872 section. */
1873 if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
1874 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1876 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1877 return FALSE;
1878 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1879 if (target_sect == NULL)
1880 return FALSE;
1882 if ((target_sect->flags & SEC_RELOC) == 0
1883 || target_sect->reloc_count == 0)
1884 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1885 else
1887 bfd_size_type amt;
1888 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1889 amt = sizeof (*hdr2);
1890 hdr2 = bfd_alloc (abfd, amt);
1891 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1893 *hdr2 = *hdr;
1894 elf_elfsections (abfd)[shindex] = hdr2;
1895 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1896 target_sect->flags |= SEC_RELOC;
1897 target_sect->relocation = NULL;
1898 target_sect->rel_filepos = hdr->sh_offset;
1899 /* In the section to which the relocations apply, mark whether
1900 its relocations are of the REL or RELA variety. */
1901 if (hdr->sh_size != 0)
1902 target_sect->use_rela_p = hdr->sh_type == SHT_RELA;
1903 abfd->flags |= HAS_RELOC;
1904 return TRUE;
1906 break;
1908 case SHT_GNU_verdef:
1909 elf_dynverdef (abfd) = shindex;
1910 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1911 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1912 break;
1914 case SHT_GNU_versym:
1915 elf_dynversym (abfd) = shindex;
1916 elf_tdata (abfd)->dynversym_hdr = *hdr;
1917 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1918 break;
1920 case SHT_GNU_verneed:
1921 elf_dynverref (abfd) = shindex;
1922 elf_tdata (abfd)->dynverref_hdr = *hdr;
1923 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1924 break;
1926 case SHT_SHLIB:
1927 return TRUE;
1929 case SHT_GROUP:
1930 /* We need a BFD section for objcopy and relocatable linking,
1931 and it's handy to have the signature available as the section
1932 name. */
1933 name = group_signature (abfd, hdr);
1934 if (name == NULL)
1935 return FALSE;
1936 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name))
1937 return FALSE;
1938 if (hdr->contents != NULL)
1940 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1941 unsigned int n_elt = hdr->sh_size / 4;
1942 asection *s;
1944 if (idx->flags & GRP_COMDAT)
1945 hdr->bfd_section->flags
1946 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1948 while (--n_elt != 0)
1949 if ((s = (++idx)->shdr->bfd_section) != NULL
1950 && elf_next_in_group (s) != NULL)
1952 elf_next_in_group (hdr->bfd_section) = s;
1953 break;
1956 break;
1958 default:
1959 /* Check for any processor-specific section types. */
1961 if (bed->elf_backend_section_from_shdr)
1962 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1964 break;
1967 return TRUE;
1970 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1971 Return SEC for sections that have no elf section, and NULL on error. */
1973 asection *
1974 bfd_section_from_r_symndx (bfd *abfd,
1975 struct sym_sec_cache *cache,
1976 asection *sec,
1977 unsigned long r_symndx)
1979 Elf_Internal_Shdr *symtab_hdr;
1980 unsigned char esym[sizeof (Elf64_External_Sym)];
1981 Elf_External_Sym_Shndx eshndx;
1982 Elf_Internal_Sym isym;
1983 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
1985 if (cache->abfd == abfd && cache->indx[ent] == r_symndx)
1986 return cache->sec[ent];
1988 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1989 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
1990 &isym, esym, &eshndx) == NULL)
1991 return NULL;
1993 if (cache->abfd != abfd)
1995 memset (cache->indx, -1, sizeof (cache->indx));
1996 cache->abfd = abfd;
1998 cache->indx[ent] = r_symndx;
1999 cache->sec[ent] = sec;
2000 if ((isym.st_shndx != SHN_UNDEF && isym.st_shndx < SHN_LORESERVE)
2001 || isym.st_shndx > SHN_HIRESERVE)
2003 asection *s;
2004 s = bfd_section_from_elf_index (abfd, isym.st_shndx);
2005 if (s != NULL)
2006 cache->sec[ent] = s;
2008 return cache->sec[ent];
2011 /* Given an ELF section number, retrieve the corresponding BFD
2012 section. */
2014 asection *
2015 bfd_section_from_elf_index (bfd *abfd, unsigned int index)
2017 if (index >= elf_numsections (abfd))
2018 return NULL;
2019 return elf_elfsections (abfd)[index]->bfd_section;
2022 static struct bfd_elf_special_section const special_sections[] =
2024 { ".bss", 4, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2025 { ".comment", 8, 0, SHT_PROGBITS, 0 },
2026 { ".data", 5, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2027 { ".data1", 6, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2028 { ".debug", 6, 0, SHT_PROGBITS, 0 },
2029 { ".fini", 5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2030 { ".init", 5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2031 { ".line", 5, 0, SHT_PROGBITS, 0 },
2032 { ".rodata", 7, -2, SHT_PROGBITS, SHF_ALLOC },
2033 { ".rodata1", 8, 0, SHT_PROGBITS, SHF_ALLOC },
2034 { ".tbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2035 { ".tdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2036 { ".text", 5, -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2037 { ".init_array", 11, 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2038 { ".fini_array", 11, 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2039 { ".preinit_array", 14, 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2040 { ".debug_line", 11, 0, SHT_PROGBITS, 0 },
2041 { ".debug_info", 11, 0, SHT_PROGBITS, 0 },
2042 { ".debug_abbrev", 13, 0, SHT_PROGBITS, 0 },
2043 { ".debug_aranges", 14, 0, SHT_PROGBITS, 0 },
2044 { ".dynamic", 8, 0, SHT_DYNAMIC, SHF_ALLOC },
2045 { ".dynstr", 7, 0, SHT_STRTAB, SHF_ALLOC },
2046 { ".dynsym", 7, 0, SHT_DYNSYM, SHF_ALLOC },
2047 { ".got", 4, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2048 { ".hash", 5, 0, SHT_HASH, SHF_ALLOC },
2049 { ".interp", 7, 0, SHT_PROGBITS, 0 },
2050 { ".plt", 4, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2051 { ".shstrtab", 9, 0, SHT_STRTAB, 0 },
2052 { ".strtab", 7, 0, SHT_STRTAB, 0 },
2053 { ".symtab", 7, 0, SHT_SYMTAB, 0 },
2054 { ".gnu.version", 12, 0, SHT_GNU_versym, 0 },
2055 { ".gnu.version_d", 14, 0, SHT_GNU_verdef, 0 },
2056 { ".gnu.version_r", 14, 0, SHT_GNU_verneed, 0 },
2057 { ".note", 5, -1, SHT_NOTE, 0 },
2058 { ".rela", 5, -1, SHT_RELA, 0 },
2059 { ".rel", 4, -1, SHT_REL, 0 },
2060 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2061 { NULL, 0, 0, 0, 0 }
2064 static const struct bfd_elf_special_section *
2065 get_special_section (const char *name,
2066 const struct bfd_elf_special_section *special_sections,
2067 unsigned int rela)
2069 int i;
2070 int len = strlen (name);
2072 for (i = 0; special_sections[i].prefix != NULL; i++)
2074 int suffix_len;
2075 int prefix_len = special_sections[i].prefix_length;
2077 if (len < prefix_len)
2078 continue;
2079 if (memcmp (name, special_sections[i].prefix, prefix_len) != 0)
2080 continue;
2082 suffix_len = special_sections[i].suffix_length;
2083 if (suffix_len <= 0)
2085 if (name[prefix_len] != 0)
2087 if (suffix_len == 0)
2088 continue;
2089 if (name[prefix_len] != '.'
2090 && (suffix_len == -2
2091 || (rela && special_sections[i].type == SHT_REL)))
2092 continue;
2095 else
2097 if (len < prefix_len + suffix_len)
2098 continue;
2099 if (memcmp (name + len - suffix_len,
2100 special_sections[i].prefix + prefix_len,
2101 suffix_len) != 0)
2102 continue;
2104 return &special_sections[i];
2107 return NULL;
2110 const struct bfd_elf_special_section *
2111 _bfd_elf_get_sec_type_attr (bfd *abfd, const char *name)
2113 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2114 const struct bfd_elf_special_section *ssect = NULL;
2116 /* See if this is one of the special sections. */
2117 if (name)
2119 unsigned int rela = bed->default_use_rela_p;
2121 if (bed->special_sections)
2122 ssect = get_special_section (name, bed->special_sections, rela);
2124 if (! ssect)
2125 ssect = get_special_section (name, special_sections, rela);
2128 return ssect;
2131 bfd_boolean
2132 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2134 struct bfd_elf_section_data *sdata;
2135 const struct bfd_elf_special_section *ssect;
2137 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2138 if (sdata == NULL)
2140 sdata = bfd_zalloc (abfd, sizeof (*sdata));
2141 if (sdata == NULL)
2142 return FALSE;
2143 sec->used_by_bfd = sdata;
2146 elf_section_type (sec) = SHT_NULL;
2147 ssect = _bfd_elf_get_sec_type_attr (abfd, sec->name);
2148 if (ssect != NULL)
2150 elf_section_type (sec) = ssect->type;
2151 elf_section_flags (sec) = ssect->attr;
2154 /* Indicate whether or not this section should use RELA relocations. */
2155 sec->use_rela_p = get_elf_backend_data (abfd)->default_use_rela_p;
2157 return TRUE;
2160 /* Create a new bfd section from an ELF program header.
2162 Since program segments have no names, we generate a synthetic name
2163 of the form segment<NUM>, where NUM is generally the index in the
2164 program header table. For segments that are split (see below) we
2165 generate the names segment<NUM>a and segment<NUM>b.
2167 Note that some program segments may have a file size that is different than
2168 (less than) the memory size. All this means is that at execution the
2169 system must allocate the amount of memory specified by the memory size,
2170 but only initialize it with the first "file size" bytes read from the
2171 file. This would occur for example, with program segments consisting
2172 of combined data+bss.
2174 To handle the above situation, this routine generates TWO bfd sections
2175 for the single program segment. The first has the length specified by
2176 the file size of the segment, and the second has the length specified
2177 by the difference between the two sizes. In effect, the segment is split
2178 into it's initialized and uninitialized parts.
2182 bfd_boolean
2183 _bfd_elf_make_section_from_phdr (bfd *abfd,
2184 Elf_Internal_Phdr *hdr,
2185 int index,
2186 const char *typename)
2188 asection *newsect;
2189 char *name;
2190 char namebuf[64];
2191 size_t len;
2192 int split;
2194 split = ((hdr->p_memsz > 0)
2195 && (hdr->p_filesz > 0)
2196 && (hdr->p_memsz > hdr->p_filesz));
2197 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
2198 len = strlen (namebuf) + 1;
2199 name = bfd_alloc (abfd, len);
2200 if (!name)
2201 return FALSE;
2202 memcpy (name, namebuf, len);
2203 newsect = bfd_make_section (abfd, name);
2204 if (newsect == NULL)
2205 return FALSE;
2206 newsect->vma = hdr->p_vaddr;
2207 newsect->lma = hdr->p_paddr;
2208 newsect->_raw_size = hdr->p_filesz;
2209 newsect->filepos = hdr->p_offset;
2210 newsect->flags |= SEC_HAS_CONTENTS;
2211 newsect->alignment_power = bfd_log2 (hdr->p_align);
2212 if (hdr->p_type == PT_LOAD)
2214 newsect->flags |= SEC_ALLOC;
2215 newsect->flags |= SEC_LOAD;
2216 if (hdr->p_flags & PF_X)
2218 /* FIXME: all we known is that it has execute PERMISSION,
2219 may be data. */
2220 newsect->flags |= SEC_CODE;
2223 if (!(hdr->p_flags & PF_W))
2225 newsect->flags |= SEC_READONLY;
2228 if (split)
2230 sprintf (namebuf, "%s%db", typename, index);
2231 len = strlen (namebuf) + 1;
2232 name = bfd_alloc (abfd, len);
2233 if (!name)
2234 return FALSE;
2235 memcpy (name, namebuf, len);
2236 newsect = bfd_make_section (abfd, name);
2237 if (newsect == NULL)
2238 return FALSE;
2239 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2240 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2241 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
2242 if (hdr->p_type == PT_LOAD)
2244 newsect->flags |= SEC_ALLOC;
2245 if (hdr->p_flags & PF_X)
2246 newsect->flags |= SEC_CODE;
2248 if (!(hdr->p_flags & PF_W))
2249 newsect->flags |= SEC_READONLY;
2252 return TRUE;
2255 bfd_boolean
2256 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index)
2258 const struct elf_backend_data *bed;
2260 switch (hdr->p_type)
2262 case PT_NULL:
2263 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2265 case PT_LOAD:
2266 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2268 case PT_DYNAMIC:
2269 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2271 case PT_INTERP:
2272 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2274 case PT_NOTE:
2275 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2276 return FALSE;
2277 if (! elfcore_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2278 return FALSE;
2279 return TRUE;
2281 case PT_SHLIB:
2282 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2284 case PT_PHDR:
2285 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2287 case PT_GNU_EH_FRAME:
2288 return _bfd_elf_make_section_from_phdr (abfd, hdr, index,
2289 "eh_frame_hdr");
2291 case PT_GNU_STACK:
2292 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "stack");
2294 default:
2295 /* Check for any processor-specific program segment types.
2296 If no handler for them, default to making "segment" sections. */
2297 bed = get_elf_backend_data (abfd);
2298 if (bed->elf_backend_section_from_phdr)
2299 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
2300 else
2301 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
2305 /* Initialize REL_HDR, the section-header for new section, containing
2306 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2307 relocations; otherwise, we use REL relocations. */
2309 bfd_boolean
2310 _bfd_elf_init_reloc_shdr (bfd *abfd,
2311 Elf_Internal_Shdr *rel_hdr,
2312 asection *asect,
2313 bfd_boolean use_rela_p)
2315 char *name;
2316 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2317 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2319 name = bfd_alloc (abfd, amt);
2320 if (name == NULL)
2321 return FALSE;
2322 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2323 rel_hdr->sh_name =
2324 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2325 FALSE);
2326 if (rel_hdr->sh_name == (unsigned int) -1)
2327 return FALSE;
2328 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2329 rel_hdr->sh_entsize = (use_rela_p
2330 ? bed->s->sizeof_rela
2331 : bed->s->sizeof_rel);
2332 rel_hdr->sh_addralign = 1 << bed->s->log_file_align;
2333 rel_hdr->sh_flags = 0;
2334 rel_hdr->sh_addr = 0;
2335 rel_hdr->sh_size = 0;
2336 rel_hdr->sh_offset = 0;
2338 return TRUE;
2341 /* Set up an ELF internal section header for a section. */
2343 static void
2344 elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg)
2346 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2347 bfd_boolean *failedptr = failedptrarg;
2348 Elf_Internal_Shdr *this_hdr;
2350 if (*failedptr)
2352 /* We already failed; just get out of the bfd_map_over_sections
2353 loop. */
2354 return;
2357 this_hdr = &elf_section_data (asect)->this_hdr;
2359 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2360 asect->name, FALSE);
2361 if (this_hdr->sh_name == (unsigned int) -1)
2363 *failedptr = TRUE;
2364 return;
2367 this_hdr->sh_flags = 0;
2369 if ((asect->flags & SEC_ALLOC) != 0
2370 || asect->user_set_vma)
2371 this_hdr->sh_addr = asect->vma;
2372 else
2373 this_hdr->sh_addr = 0;
2375 this_hdr->sh_offset = 0;
2376 this_hdr->sh_size = asect->_raw_size;
2377 this_hdr->sh_link = 0;
2378 this_hdr->sh_addralign = 1 << asect->alignment_power;
2379 /* The sh_entsize and sh_info fields may have been set already by
2380 copy_private_section_data. */
2382 this_hdr->bfd_section = asect;
2383 this_hdr->contents = NULL;
2385 /* If the section type is unspecified, we set it based on
2386 asect->flags. */
2387 if (this_hdr->sh_type == SHT_NULL)
2389 if ((asect->flags & SEC_ALLOC) != 0
2390 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2391 || (asect->flags & SEC_NEVER_LOAD) != 0))
2392 this_hdr->sh_type = SHT_NOBITS;
2393 else
2394 this_hdr->sh_type = SHT_PROGBITS;
2397 switch (this_hdr->sh_type)
2399 default:
2400 break;
2402 case SHT_STRTAB:
2403 case SHT_INIT_ARRAY:
2404 case SHT_FINI_ARRAY:
2405 case SHT_PREINIT_ARRAY:
2406 case SHT_NOTE:
2407 case SHT_NOBITS:
2408 case SHT_PROGBITS:
2409 break;
2411 case SHT_HASH:
2412 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2413 break;
2415 case SHT_DYNSYM:
2416 this_hdr->sh_entsize = bed->s->sizeof_sym;
2417 break;
2419 case SHT_DYNAMIC:
2420 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2421 break;
2423 case SHT_RELA:
2424 if (get_elf_backend_data (abfd)->may_use_rela_p)
2425 this_hdr->sh_entsize = bed->s->sizeof_rela;
2426 break;
2428 case SHT_REL:
2429 if (get_elf_backend_data (abfd)->may_use_rel_p)
2430 this_hdr->sh_entsize = bed->s->sizeof_rel;
2431 break;
2433 case SHT_GNU_versym:
2434 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2435 break;
2437 case SHT_GNU_verdef:
2438 this_hdr->sh_entsize = 0;
2439 /* objcopy or strip will copy over sh_info, but may not set
2440 cverdefs. The linker will set cverdefs, but sh_info will be
2441 zero. */
2442 if (this_hdr->sh_info == 0)
2443 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2444 else
2445 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2446 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2447 break;
2449 case SHT_GNU_verneed:
2450 this_hdr->sh_entsize = 0;
2451 /* objcopy or strip will copy over sh_info, but may not set
2452 cverrefs. The linker will set cverrefs, but sh_info will be
2453 zero. */
2454 if (this_hdr->sh_info == 0)
2455 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2456 else
2457 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2458 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2459 break;
2461 case SHT_GROUP:
2462 this_hdr->sh_entsize = 4;
2463 break;
2466 if ((asect->flags & SEC_ALLOC) != 0)
2467 this_hdr->sh_flags |= SHF_ALLOC;
2468 if ((asect->flags & SEC_READONLY) == 0)
2469 this_hdr->sh_flags |= SHF_WRITE;
2470 if ((asect->flags & SEC_CODE) != 0)
2471 this_hdr->sh_flags |= SHF_EXECINSTR;
2472 if ((asect->flags & SEC_MERGE) != 0)
2474 this_hdr->sh_flags |= SHF_MERGE;
2475 this_hdr->sh_entsize = asect->entsize;
2476 if ((asect->flags & SEC_STRINGS) != 0)
2477 this_hdr->sh_flags |= SHF_STRINGS;
2479 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2480 this_hdr->sh_flags |= SHF_GROUP;
2481 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2483 this_hdr->sh_flags |= SHF_TLS;
2484 if (asect->_raw_size == 0 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2486 struct bfd_link_order *o;
2488 this_hdr->sh_size = 0;
2489 for (o = asect->link_order_head; o != NULL; o = o->next)
2490 if (this_hdr->sh_size < o->offset + o->size)
2491 this_hdr->sh_size = o->offset + o->size;
2492 if (this_hdr->sh_size)
2493 this_hdr->sh_type = SHT_NOBITS;
2497 /* Check for processor-specific section types. */
2498 if (bed->elf_backend_fake_sections
2499 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2500 *failedptr = TRUE;
2502 /* If the section has relocs, set up a section header for the
2503 SHT_REL[A] section. If two relocation sections are required for
2504 this section, it is up to the processor-specific back-end to
2505 create the other. */
2506 if ((asect->flags & SEC_RELOC) != 0
2507 && !_bfd_elf_init_reloc_shdr (abfd,
2508 &elf_section_data (asect)->rel_hdr,
2509 asect,
2510 asect->use_rela_p))
2511 *failedptr = TRUE;
2514 /* Fill in the contents of a SHT_GROUP section. */
2516 void
2517 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2519 bfd_boolean *failedptr = failedptrarg;
2520 unsigned long symindx;
2521 asection *elt, *first;
2522 unsigned char *loc;
2523 struct bfd_link_order *l;
2524 bfd_boolean gas;
2526 if (elf_section_data (sec)->this_hdr.sh_type != SHT_GROUP
2527 || *failedptr)
2528 return;
2530 symindx = 0;
2531 if (elf_group_id (sec) != NULL)
2532 symindx = elf_group_id (sec)->udata.i;
2534 if (symindx == 0)
2536 /* If called from the assembler, swap_out_syms will have set up
2537 elf_section_syms; If called for "ld -r", use target_index. */
2538 if (elf_section_syms (abfd) != NULL)
2539 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2540 else
2541 symindx = sec->target_index;
2543 elf_section_data (sec)->this_hdr.sh_info = symindx;
2545 /* The contents won't be allocated for "ld -r" or objcopy. */
2546 gas = TRUE;
2547 if (sec->contents == NULL)
2549 gas = FALSE;
2550 sec->contents = bfd_alloc (abfd, sec->_raw_size);
2552 /* Arrange for the section to be written out. */
2553 elf_section_data (sec)->this_hdr.contents = sec->contents;
2554 if (sec->contents == NULL)
2556 *failedptr = TRUE;
2557 return;
2561 loc = sec->contents + sec->_raw_size;
2563 /* Get the pointer to the first section in the group that gas
2564 squirreled away here. objcopy arranges for this to be set to the
2565 start of the input section group. */
2566 first = elt = elf_next_in_group (sec);
2568 /* First element is a flag word. Rest of section is elf section
2569 indices for all the sections of the group. Write them backwards
2570 just to keep the group in the same order as given in .section
2571 directives, not that it matters. */
2572 while (elt != NULL)
2574 asection *s;
2575 unsigned int idx;
2577 loc -= 4;
2578 s = elt;
2579 if (!gas)
2580 s = s->output_section;
2581 idx = 0;
2582 if (s != NULL)
2583 idx = elf_section_data (s)->this_idx;
2584 H_PUT_32 (abfd, idx, loc);
2585 elt = elf_next_in_group (elt);
2586 if (elt == first)
2587 break;
2590 /* If this is a relocatable link, then the above did nothing because
2591 SEC is the output section. Look through the input sections
2592 instead. */
2593 for (l = sec->link_order_head; l != NULL; l = l->next)
2594 if (l->type == bfd_indirect_link_order
2595 && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
2598 loc -= 4;
2599 H_PUT_32 (abfd,
2600 elf_section_data (elt->output_section)->this_idx, loc);
2601 elt = elf_next_in_group (elt);
2602 /* During a relocatable link, the lists are circular. */
2604 while (elt != elf_next_in_group (l->u.indirect.section));
2606 /* With ld -r, merging SHT_GROUP sections results in wasted space
2607 due to allowing for the flag word on each input. We may well
2608 duplicate entries too. */
2609 while ((loc -= 4) > sec->contents)
2610 H_PUT_32 (abfd, 0, loc);
2612 if (loc != sec->contents)
2613 abort ();
2615 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2618 /* Assign all ELF section numbers. The dummy first section is handled here
2619 too. The link/info pointers for the standard section types are filled
2620 in here too, while we're at it. */
2622 static bfd_boolean
2623 assign_section_numbers (bfd *abfd)
2625 struct elf_obj_tdata *t = elf_tdata (abfd);
2626 asection *sec;
2627 unsigned int section_number, secn;
2628 Elf_Internal_Shdr **i_shdrp;
2629 bfd_size_type amt;
2631 section_number = 1;
2633 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2635 for (sec = abfd->sections; sec; sec = sec->next)
2637 struct bfd_elf_section_data *d = elf_section_data (sec);
2639 if (section_number == SHN_LORESERVE)
2640 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2641 d->this_idx = section_number++;
2642 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2643 if ((sec->flags & SEC_RELOC) == 0)
2644 d->rel_idx = 0;
2645 else
2647 if (section_number == SHN_LORESERVE)
2648 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2649 d->rel_idx = section_number++;
2650 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2653 if (d->rel_hdr2)
2655 if (section_number == SHN_LORESERVE)
2656 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2657 d->rel_idx2 = section_number++;
2658 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2660 else
2661 d->rel_idx2 = 0;
2664 if (section_number == SHN_LORESERVE)
2665 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2666 t->shstrtab_section = section_number++;
2667 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2668 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2670 if (bfd_get_symcount (abfd) > 0)
2672 if (section_number == SHN_LORESERVE)
2673 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2674 t->symtab_section = section_number++;
2675 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2676 if (section_number > SHN_LORESERVE - 2)
2678 if (section_number == SHN_LORESERVE)
2679 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2680 t->symtab_shndx_section = section_number++;
2681 t->symtab_shndx_hdr.sh_name
2682 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2683 ".symtab_shndx", FALSE);
2684 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2685 return FALSE;
2687 if (section_number == SHN_LORESERVE)
2688 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2689 t->strtab_section = section_number++;
2690 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2693 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2694 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2696 elf_numsections (abfd) = section_number;
2697 elf_elfheader (abfd)->e_shnum = section_number;
2698 if (section_number > SHN_LORESERVE)
2699 elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE;
2701 /* Set up the list of section header pointers, in agreement with the
2702 indices. */
2703 amt = section_number * sizeof (Elf_Internal_Shdr *);
2704 i_shdrp = bfd_zalloc (abfd, amt);
2705 if (i_shdrp == NULL)
2706 return FALSE;
2708 amt = sizeof (Elf_Internal_Shdr);
2709 i_shdrp[0] = bfd_zalloc (abfd, amt);
2710 if (i_shdrp[0] == NULL)
2712 bfd_release (abfd, i_shdrp);
2713 return FALSE;
2716 elf_elfsections (abfd) = i_shdrp;
2718 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2719 if (bfd_get_symcount (abfd) > 0)
2721 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2722 if (elf_numsections (abfd) > SHN_LORESERVE)
2724 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2725 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2727 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2728 t->symtab_hdr.sh_link = t->strtab_section;
2730 for (sec = abfd->sections; sec; sec = sec->next)
2732 struct bfd_elf_section_data *d = elf_section_data (sec);
2733 asection *s;
2734 const char *name;
2736 i_shdrp[d->this_idx] = &d->this_hdr;
2737 if (d->rel_idx != 0)
2738 i_shdrp[d->rel_idx] = &d->rel_hdr;
2739 if (d->rel_idx2 != 0)
2740 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2742 /* Fill in the sh_link and sh_info fields while we're at it. */
2744 /* sh_link of a reloc section is the section index of the symbol
2745 table. sh_info is the section index of the section to which
2746 the relocation entries apply. */
2747 if (d->rel_idx != 0)
2749 d->rel_hdr.sh_link = t->symtab_section;
2750 d->rel_hdr.sh_info = d->this_idx;
2752 if (d->rel_idx2 != 0)
2754 d->rel_hdr2->sh_link = t->symtab_section;
2755 d->rel_hdr2->sh_info = d->this_idx;
2758 switch (d->this_hdr.sh_type)
2760 case SHT_REL:
2761 case SHT_RELA:
2762 /* A reloc section which we are treating as a normal BFD
2763 section. sh_link is the section index of the symbol
2764 table. sh_info is the section index of the section to
2765 which the relocation entries apply. We assume that an
2766 allocated reloc section uses the dynamic symbol table.
2767 FIXME: How can we be sure? */
2768 s = bfd_get_section_by_name (abfd, ".dynsym");
2769 if (s != NULL)
2770 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2772 /* We look up the section the relocs apply to by name. */
2773 name = sec->name;
2774 if (d->this_hdr.sh_type == SHT_REL)
2775 name += 4;
2776 else
2777 name += 5;
2778 s = bfd_get_section_by_name (abfd, name);
2779 if (s != NULL)
2780 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
2781 break;
2783 case SHT_STRTAB:
2784 /* We assume that a section named .stab*str is a stabs
2785 string section. We look for a section with the same name
2786 but without the trailing ``str'', and set its sh_link
2787 field to point to this section. */
2788 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
2789 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
2791 size_t len;
2792 char *alc;
2794 len = strlen (sec->name);
2795 alc = bfd_malloc (len - 2);
2796 if (alc == NULL)
2797 return FALSE;
2798 memcpy (alc, sec->name, len - 3);
2799 alc[len - 3] = '\0';
2800 s = bfd_get_section_by_name (abfd, alc);
2801 free (alc);
2802 if (s != NULL)
2804 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
2806 /* This is a .stab section. */
2807 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
2808 elf_section_data (s)->this_hdr.sh_entsize
2809 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
2812 break;
2814 case SHT_DYNAMIC:
2815 case SHT_DYNSYM:
2816 case SHT_GNU_verneed:
2817 case SHT_GNU_verdef:
2818 /* sh_link is the section header index of the string table
2819 used for the dynamic entries, or the symbol table, or the
2820 version strings. */
2821 s = bfd_get_section_by_name (abfd, ".dynstr");
2822 if (s != NULL)
2823 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2824 break;
2826 case SHT_HASH:
2827 case SHT_GNU_versym:
2828 /* sh_link is the section header index of the symbol table
2829 this hash table or version table is for. */
2830 s = bfd_get_section_by_name (abfd, ".dynsym");
2831 if (s != NULL)
2832 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2833 break;
2835 case SHT_GROUP:
2836 d->this_hdr.sh_link = t->symtab_section;
2840 for (secn = 1; secn < section_number; ++secn)
2841 if (i_shdrp[secn] == NULL)
2842 i_shdrp[secn] = i_shdrp[0];
2843 else
2844 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
2845 i_shdrp[secn]->sh_name);
2846 return TRUE;
2849 /* Map symbol from it's internal number to the external number, moving
2850 all local symbols to be at the head of the list. */
2852 static int
2853 sym_is_global (bfd *abfd, asymbol *sym)
2855 /* If the backend has a special mapping, use it. */
2856 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2857 if (bed->elf_backend_sym_is_global)
2858 return (*bed->elf_backend_sym_is_global) (abfd, sym);
2860 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2861 || bfd_is_und_section (bfd_get_section (sym))
2862 || bfd_is_com_section (bfd_get_section (sym)));
2865 static bfd_boolean
2866 elf_map_symbols (bfd *abfd)
2868 unsigned int symcount = bfd_get_symcount (abfd);
2869 asymbol **syms = bfd_get_outsymbols (abfd);
2870 asymbol **sect_syms;
2871 unsigned int num_locals = 0;
2872 unsigned int num_globals = 0;
2873 unsigned int num_locals2 = 0;
2874 unsigned int num_globals2 = 0;
2875 int max_index = 0;
2876 unsigned int idx;
2877 asection *asect;
2878 asymbol **new_syms;
2879 bfd_size_type amt;
2881 #ifdef DEBUG
2882 fprintf (stderr, "elf_map_symbols\n");
2883 fflush (stderr);
2884 #endif
2886 for (asect = abfd->sections; asect; asect = asect->next)
2888 if (max_index < asect->index)
2889 max_index = asect->index;
2892 max_index++;
2893 amt = max_index * sizeof (asymbol *);
2894 sect_syms = bfd_zalloc (abfd, amt);
2895 if (sect_syms == NULL)
2896 return FALSE;
2897 elf_section_syms (abfd) = sect_syms;
2898 elf_num_section_syms (abfd) = max_index;
2900 /* Init sect_syms entries for any section symbols we have already
2901 decided to output. */
2902 for (idx = 0; idx < symcount; idx++)
2904 asymbol *sym = syms[idx];
2906 if ((sym->flags & BSF_SECTION_SYM) != 0
2907 && sym->value == 0)
2909 asection *sec;
2911 sec = sym->section;
2913 if (sec->owner != NULL)
2915 if (sec->owner != abfd)
2917 if (sec->output_offset != 0)
2918 continue;
2920 sec = sec->output_section;
2922 /* Empty sections in the input files may have had a
2923 section symbol created for them. (See the comment
2924 near the end of _bfd_generic_link_output_symbols in
2925 linker.c). If the linker script discards such
2926 sections then we will reach this point. Since we know
2927 that we cannot avoid this case, we detect it and skip
2928 the abort and the assignment to the sect_syms array.
2929 To reproduce this particular case try running the
2930 linker testsuite test ld-scripts/weak.exp for an ELF
2931 port that uses the generic linker. */
2932 if (sec->owner == NULL)
2933 continue;
2935 BFD_ASSERT (sec->owner == abfd);
2937 sect_syms[sec->index] = syms[idx];
2942 /* Classify all of the symbols. */
2943 for (idx = 0; idx < symcount; idx++)
2945 if (!sym_is_global (abfd, syms[idx]))
2946 num_locals++;
2947 else
2948 num_globals++;
2951 /* We will be adding a section symbol for each BFD section. Most normal
2952 sections will already have a section symbol in outsymbols, but
2953 eg. SHT_GROUP sections will not, and we need the section symbol mapped
2954 at least in that case. */
2955 for (asect = abfd->sections; asect; asect = asect->next)
2957 if (sect_syms[asect->index] == NULL)
2959 if (!sym_is_global (abfd, asect->symbol))
2960 num_locals++;
2961 else
2962 num_globals++;
2966 /* Now sort the symbols so the local symbols are first. */
2967 amt = (num_locals + num_globals) * sizeof (asymbol *);
2968 new_syms = bfd_alloc (abfd, amt);
2970 if (new_syms == NULL)
2971 return FALSE;
2973 for (idx = 0; idx < symcount; idx++)
2975 asymbol *sym = syms[idx];
2976 unsigned int i;
2978 if (!sym_is_global (abfd, sym))
2979 i = num_locals2++;
2980 else
2981 i = num_locals + num_globals2++;
2982 new_syms[i] = sym;
2983 sym->udata.i = i + 1;
2985 for (asect = abfd->sections; asect; asect = asect->next)
2987 if (sect_syms[asect->index] == NULL)
2989 asymbol *sym = asect->symbol;
2990 unsigned int i;
2992 sect_syms[asect->index] = sym;
2993 if (!sym_is_global (abfd, sym))
2994 i = num_locals2++;
2995 else
2996 i = num_locals + num_globals2++;
2997 new_syms[i] = sym;
2998 sym->udata.i = i + 1;
3002 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3004 elf_num_locals (abfd) = num_locals;
3005 elf_num_globals (abfd) = num_globals;
3006 return TRUE;
3009 /* Align to the maximum file alignment that could be required for any
3010 ELF data structure. */
3012 static inline file_ptr
3013 align_file_position (file_ptr off, int align)
3015 return (off + align - 1) & ~(align - 1);
3018 /* Assign a file position to a section, optionally aligning to the
3019 required section alignment. */
3021 file_ptr
3022 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3023 file_ptr offset,
3024 bfd_boolean align)
3026 if (align)
3028 unsigned int al;
3030 al = i_shdrp->sh_addralign;
3031 if (al > 1)
3032 offset = BFD_ALIGN (offset, al);
3034 i_shdrp->sh_offset = offset;
3035 if (i_shdrp->bfd_section != NULL)
3036 i_shdrp->bfd_section->filepos = offset;
3037 if (i_shdrp->sh_type != SHT_NOBITS)
3038 offset += i_shdrp->sh_size;
3039 return offset;
3042 /* Compute the file positions we are going to put the sections at, and
3043 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3044 is not NULL, this is being called by the ELF backend linker. */
3046 bfd_boolean
3047 _bfd_elf_compute_section_file_positions (bfd *abfd,
3048 struct bfd_link_info *link_info)
3050 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3051 bfd_boolean failed;
3052 struct bfd_strtab_hash *strtab;
3053 Elf_Internal_Shdr *shstrtab_hdr;
3055 if (abfd->output_has_begun)
3056 return TRUE;
3058 /* Do any elf backend specific processing first. */
3059 if (bed->elf_backend_begin_write_processing)
3060 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3062 if (! prep_headers (abfd))
3063 return FALSE;
3065 /* Post process the headers if necessary. */
3066 if (bed->elf_backend_post_process_headers)
3067 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3069 failed = FALSE;
3070 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3071 if (failed)
3072 return FALSE;
3074 if (!assign_section_numbers (abfd))
3075 return FALSE;
3077 /* The backend linker builds symbol table information itself. */
3078 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3080 /* Non-zero if doing a relocatable link. */
3081 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3083 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3084 return FALSE;
3087 if (link_info == NULL)
3089 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3090 if (failed)
3091 return FALSE;
3094 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3095 /* sh_name was set in prep_headers. */
3096 shstrtab_hdr->sh_type = SHT_STRTAB;
3097 shstrtab_hdr->sh_flags = 0;
3098 shstrtab_hdr->sh_addr = 0;
3099 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3100 shstrtab_hdr->sh_entsize = 0;
3101 shstrtab_hdr->sh_link = 0;
3102 shstrtab_hdr->sh_info = 0;
3103 /* sh_offset is set in assign_file_positions_except_relocs. */
3104 shstrtab_hdr->sh_addralign = 1;
3106 if (!assign_file_positions_except_relocs (abfd, link_info))
3107 return FALSE;
3109 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3111 file_ptr off;
3112 Elf_Internal_Shdr *hdr;
3114 off = elf_tdata (abfd)->next_file_pos;
3116 hdr = &elf_tdata (abfd)->symtab_hdr;
3117 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3119 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3120 if (hdr->sh_size != 0)
3121 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3123 hdr = &elf_tdata (abfd)->strtab_hdr;
3124 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3126 elf_tdata (abfd)->next_file_pos = off;
3128 /* Now that we know where the .strtab section goes, write it
3129 out. */
3130 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3131 || ! _bfd_stringtab_emit (abfd, strtab))
3132 return FALSE;
3133 _bfd_stringtab_free (strtab);
3136 abfd->output_has_begun = TRUE;
3138 return TRUE;
3141 /* Create a mapping from a set of sections to a program segment. */
3143 static struct elf_segment_map *
3144 make_mapping (bfd *abfd,
3145 asection **sections,
3146 unsigned int from,
3147 unsigned int to,
3148 bfd_boolean phdr)
3150 struct elf_segment_map *m;
3151 unsigned int i;
3152 asection **hdrpp;
3153 bfd_size_type amt;
3155 amt = sizeof (struct elf_segment_map);
3156 amt += (to - from - 1) * sizeof (asection *);
3157 m = bfd_zalloc (abfd, amt);
3158 if (m == NULL)
3159 return NULL;
3160 m->next = NULL;
3161 m->p_type = PT_LOAD;
3162 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3163 m->sections[i - from] = *hdrpp;
3164 m->count = to - from;
3166 if (from == 0 && phdr)
3168 /* Include the headers in the first PT_LOAD segment. */
3169 m->includes_filehdr = 1;
3170 m->includes_phdrs = 1;
3173 return m;
3176 /* Set up a mapping from BFD sections to program segments. */
3178 static bfd_boolean
3179 map_sections_to_segments (bfd *abfd)
3181 asection **sections = NULL;
3182 asection *s;
3183 unsigned int i;
3184 unsigned int count;
3185 struct elf_segment_map *mfirst;
3186 struct elf_segment_map **pm;
3187 struct elf_segment_map *m;
3188 asection *last_hdr;
3189 bfd_vma last_size;
3190 unsigned int phdr_index;
3191 bfd_vma maxpagesize;
3192 asection **hdrpp;
3193 bfd_boolean phdr_in_segment = TRUE;
3194 bfd_boolean writable;
3195 int tls_count = 0;
3196 asection *first_tls = NULL;
3197 asection *dynsec, *eh_frame_hdr;
3198 bfd_size_type amt;
3200 if (elf_tdata (abfd)->segment_map != NULL)
3201 return TRUE;
3203 if (bfd_count_sections (abfd) == 0)
3204 return TRUE;
3206 /* Select the allocated sections, and sort them. */
3208 amt = bfd_count_sections (abfd) * sizeof (asection *);
3209 sections = bfd_malloc (amt);
3210 if (sections == NULL)
3211 goto error_return;
3213 i = 0;
3214 for (s = abfd->sections; s != NULL; s = s->next)
3216 if ((s->flags & SEC_ALLOC) != 0)
3218 sections[i] = s;
3219 ++i;
3222 BFD_ASSERT (i <= bfd_count_sections (abfd));
3223 count = i;
3225 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3227 /* Build the mapping. */
3229 mfirst = NULL;
3230 pm = &mfirst;
3232 /* If we have a .interp section, then create a PT_PHDR segment for
3233 the program headers and a PT_INTERP segment for the .interp
3234 section. */
3235 s = bfd_get_section_by_name (abfd, ".interp");
3236 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3238 amt = sizeof (struct elf_segment_map);
3239 m = bfd_zalloc (abfd, amt);
3240 if (m == NULL)
3241 goto error_return;
3242 m->next = NULL;
3243 m->p_type = PT_PHDR;
3244 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3245 m->p_flags = PF_R | PF_X;
3246 m->p_flags_valid = 1;
3247 m->includes_phdrs = 1;
3249 *pm = m;
3250 pm = &m->next;
3252 amt = sizeof (struct elf_segment_map);
3253 m = bfd_zalloc (abfd, amt);
3254 if (m == NULL)
3255 goto error_return;
3256 m->next = NULL;
3257 m->p_type = PT_INTERP;
3258 m->count = 1;
3259 m->sections[0] = s;
3261 *pm = m;
3262 pm = &m->next;
3265 /* Look through the sections. We put sections in the same program
3266 segment when the start of the second section can be placed within
3267 a few bytes of the end of the first section. */
3268 last_hdr = NULL;
3269 last_size = 0;
3270 phdr_index = 0;
3271 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
3272 writable = FALSE;
3273 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3274 if (dynsec != NULL
3275 && (dynsec->flags & SEC_LOAD) == 0)
3276 dynsec = NULL;
3278 /* Deal with -Ttext or something similar such that the first section
3279 is not adjacent to the program headers. This is an
3280 approximation, since at this point we don't know exactly how many
3281 program headers we will need. */
3282 if (count > 0)
3284 bfd_size_type phdr_size;
3286 phdr_size = elf_tdata (abfd)->program_header_size;
3287 if (phdr_size == 0)
3288 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
3289 if ((abfd->flags & D_PAGED) == 0
3290 || sections[0]->lma < phdr_size
3291 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3292 phdr_in_segment = FALSE;
3295 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3297 asection *hdr;
3298 bfd_boolean new_segment;
3300 hdr = *hdrpp;
3302 /* See if this section and the last one will fit in the same
3303 segment. */
3305 if (last_hdr == NULL)
3307 /* If we don't have a segment yet, then we don't need a new
3308 one (we build the last one after this loop). */
3309 new_segment = FALSE;
3311 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3313 /* If this section has a different relation between the
3314 virtual address and the load address, then we need a new
3315 segment. */
3316 new_segment = TRUE;
3318 else if (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
3319 < BFD_ALIGN (hdr->lma, maxpagesize))
3321 /* If putting this section in this segment would force us to
3322 skip a page in the segment, then we need a new segment. */
3323 new_segment = TRUE;
3325 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3326 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
3328 /* We don't want to put a loadable section after a
3329 nonloadable section in the same segment.
3330 Consider .tbss sections as loadable for this purpose. */
3331 new_segment = TRUE;
3333 else if ((abfd->flags & D_PAGED) == 0)
3335 /* If the file is not demand paged, which means that we
3336 don't require the sections to be correctly aligned in the
3337 file, then there is no other reason for a new segment. */
3338 new_segment = FALSE;
3340 else if (! writable
3341 && (hdr->flags & SEC_READONLY) == 0
3342 && (((last_hdr->lma + last_size - 1)
3343 & ~(maxpagesize - 1))
3344 != (hdr->lma & ~(maxpagesize - 1))))
3346 /* We don't want to put a writable section in a read only
3347 segment, unless they are on the same page in memory
3348 anyhow. We already know that the last section does not
3349 bring us past the current section on the page, so the
3350 only case in which the new section is not on the same
3351 page as the previous section is when the previous section
3352 ends precisely on a page boundary. */
3353 new_segment = TRUE;
3355 else
3357 /* Otherwise, we can use the same segment. */
3358 new_segment = FALSE;
3361 if (! new_segment)
3363 if ((hdr->flags & SEC_READONLY) == 0)
3364 writable = TRUE;
3365 last_hdr = hdr;
3366 /* .tbss sections effectively have zero size. */
3367 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3368 last_size = hdr->_raw_size;
3369 else
3370 last_size = 0;
3371 continue;
3374 /* We need a new program segment. We must create a new program
3375 header holding all the sections from phdr_index until hdr. */
3377 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3378 if (m == NULL)
3379 goto error_return;
3381 *pm = m;
3382 pm = &m->next;
3384 if ((hdr->flags & SEC_READONLY) == 0)
3385 writable = TRUE;
3386 else
3387 writable = FALSE;
3389 last_hdr = hdr;
3390 /* .tbss sections effectively have zero size. */
3391 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3392 last_size = hdr->_raw_size;
3393 else
3394 last_size = 0;
3395 phdr_index = i;
3396 phdr_in_segment = FALSE;
3399 /* Create a final PT_LOAD program segment. */
3400 if (last_hdr != NULL)
3402 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3403 if (m == NULL)
3404 goto error_return;
3406 *pm = m;
3407 pm = &m->next;
3410 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3411 if (dynsec != NULL)
3413 amt = sizeof (struct elf_segment_map);
3414 m = bfd_zalloc (abfd, amt);
3415 if (m == NULL)
3416 goto error_return;
3417 m->next = NULL;
3418 m->p_type = PT_DYNAMIC;
3419 m->count = 1;
3420 m->sections[0] = dynsec;
3422 *pm = m;
3423 pm = &m->next;
3426 /* For each loadable .note section, add a PT_NOTE segment. We don't
3427 use bfd_get_section_by_name, because if we link together
3428 nonloadable .note sections and loadable .note sections, we will
3429 generate two .note sections in the output file. FIXME: Using
3430 names for section types is bogus anyhow. */
3431 for (s = abfd->sections; s != NULL; s = s->next)
3433 if ((s->flags & SEC_LOAD) != 0
3434 && strncmp (s->name, ".note", 5) == 0)
3436 amt = sizeof (struct elf_segment_map);
3437 m = bfd_zalloc (abfd, amt);
3438 if (m == NULL)
3439 goto error_return;
3440 m->next = NULL;
3441 m->p_type = PT_NOTE;
3442 m->count = 1;
3443 m->sections[0] = s;
3445 *pm = m;
3446 pm = &m->next;
3448 if (s->flags & SEC_THREAD_LOCAL)
3450 if (! tls_count)
3451 first_tls = s;
3452 tls_count++;
3456 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3457 if (tls_count > 0)
3459 int i;
3461 amt = sizeof (struct elf_segment_map);
3462 amt += (tls_count - 1) * sizeof (asection *);
3463 m = bfd_zalloc (abfd, amt);
3464 if (m == NULL)
3465 goto error_return;
3466 m->next = NULL;
3467 m->p_type = PT_TLS;
3468 m->count = tls_count;
3469 /* Mandated PF_R. */
3470 m->p_flags = PF_R;
3471 m->p_flags_valid = 1;
3472 for (i = 0; i < tls_count; ++i)
3474 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3475 m->sections[i] = first_tls;
3476 first_tls = first_tls->next;
3479 *pm = m;
3480 pm = &m->next;
3483 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3484 segment. */
3485 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
3486 if (eh_frame_hdr != NULL
3487 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
3489 amt = sizeof (struct elf_segment_map);
3490 m = bfd_zalloc (abfd, amt);
3491 if (m == NULL)
3492 goto error_return;
3493 m->next = NULL;
3494 m->p_type = PT_GNU_EH_FRAME;
3495 m->count = 1;
3496 m->sections[0] = eh_frame_hdr->output_section;
3498 *pm = m;
3499 pm = &m->next;
3502 if (elf_tdata (abfd)->stack_flags)
3504 amt = sizeof (struct elf_segment_map);
3505 m = bfd_zalloc (abfd, amt);
3506 if (m == NULL)
3507 goto error_return;
3508 m->next = NULL;
3509 m->p_type = PT_GNU_STACK;
3510 m->p_flags = elf_tdata (abfd)->stack_flags;
3511 m->p_flags_valid = 1;
3513 *pm = m;
3514 pm = &m->next;
3517 free (sections);
3518 sections = NULL;
3520 elf_tdata (abfd)->segment_map = mfirst;
3521 return TRUE;
3523 error_return:
3524 if (sections != NULL)
3525 free (sections);
3526 return FALSE;
3529 /* Sort sections by address. */
3531 static int
3532 elf_sort_sections (const void *arg1, const void *arg2)
3534 const asection *sec1 = *(const asection **) arg1;
3535 const asection *sec2 = *(const asection **) arg2;
3536 bfd_size_type size1, size2;
3538 /* Sort by LMA first, since this is the address used to
3539 place the section into a segment. */
3540 if (sec1->lma < sec2->lma)
3541 return -1;
3542 else if (sec1->lma > sec2->lma)
3543 return 1;
3545 /* Then sort by VMA. Normally the LMA and the VMA will be
3546 the same, and this will do nothing. */
3547 if (sec1->vma < sec2->vma)
3548 return -1;
3549 else if (sec1->vma > sec2->vma)
3550 return 1;
3552 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3554 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
3556 if (TOEND (sec1))
3558 if (TOEND (sec2))
3560 /* If the indicies are the same, do not return 0
3561 here, but continue to try the next comparison. */
3562 if (sec1->target_index - sec2->target_index != 0)
3563 return sec1->target_index - sec2->target_index;
3565 else
3566 return 1;
3568 else if (TOEND (sec2))
3569 return -1;
3571 #undef TOEND
3573 /* Sort by size, to put zero sized sections
3574 before others at the same address. */
3576 size1 = (sec1->flags & SEC_LOAD) ? sec1->_raw_size : 0;
3577 size2 = (sec2->flags & SEC_LOAD) ? sec2->_raw_size : 0;
3579 if (size1 < size2)
3580 return -1;
3581 if (size1 > size2)
3582 return 1;
3584 return sec1->target_index - sec2->target_index;
3587 /* Ian Lance Taylor writes:
3589 We shouldn't be using % with a negative signed number. That's just
3590 not good. We have to make sure either that the number is not
3591 negative, or that the number has an unsigned type. When the types
3592 are all the same size they wind up as unsigned. When file_ptr is a
3593 larger signed type, the arithmetic winds up as signed long long,
3594 which is wrong.
3596 What we're trying to say here is something like ``increase OFF by
3597 the least amount that will cause it to be equal to the VMA modulo
3598 the page size.'' */
3599 /* In other words, something like:
3601 vma_offset = m->sections[0]->vma % bed->maxpagesize;
3602 off_offset = off % bed->maxpagesize;
3603 if (vma_offset < off_offset)
3604 adjustment = vma_offset + bed->maxpagesize - off_offset;
3605 else
3606 adjustment = vma_offset - off_offset;
3608 which can can be collapsed into the expression below. */
3610 static file_ptr
3611 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
3613 return ((vma - off) % maxpagesize);
3616 /* Assign file positions to the sections based on the mapping from
3617 sections to segments. This function also sets up some fields in
3618 the file header, and writes out the program headers. */
3620 static bfd_boolean
3621 assign_file_positions_for_segments (bfd *abfd, struct bfd_link_info *link_info)
3623 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3624 unsigned int count;
3625 struct elf_segment_map *m;
3626 unsigned int alloc;
3627 Elf_Internal_Phdr *phdrs;
3628 file_ptr off, voff;
3629 bfd_vma filehdr_vaddr, filehdr_paddr;
3630 bfd_vma phdrs_vaddr, phdrs_paddr;
3631 Elf_Internal_Phdr *p;
3632 bfd_size_type amt;
3634 if (elf_tdata (abfd)->segment_map == NULL)
3636 if (! map_sections_to_segments (abfd))
3637 return FALSE;
3639 else
3641 /* The placement algorithm assumes that non allocated sections are
3642 not in PT_LOAD segments. We ensure this here by removing such
3643 sections from the segment map. */
3644 for (m = elf_tdata (abfd)->segment_map;
3645 m != NULL;
3646 m = m->next)
3648 unsigned int new_count;
3649 unsigned int i;
3651 if (m->p_type != PT_LOAD)
3652 continue;
3654 new_count = 0;
3655 for (i = 0; i < m->count; i ++)
3657 if ((m->sections[i]->flags & SEC_ALLOC) != 0)
3659 if (i != new_count)
3660 m->sections[new_count] = m->sections[i];
3662 new_count ++;
3666 if (new_count != m->count)
3667 m->count = new_count;
3671 if (bed->elf_backend_modify_segment_map)
3673 if (! (*bed->elf_backend_modify_segment_map) (abfd, link_info))
3674 return FALSE;
3677 count = 0;
3678 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3679 ++count;
3681 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
3682 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
3683 elf_elfheader (abfd)->e_phnum = count;
3685 if (count == 0)
3686 return TRUE;
3688 /* If we already counted the number of program segments, make sure
3689 that we allocated enough space. This happens when SIZEOF_HEADERS
3690 is used in a linker script. */
3691 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
3692 if (alloc != 0 && count > alloc)
3694 ((*_bfd_error_handler)
3695 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3696 bfd_get_filename (abfd), alloc, count));
3697 bfd_set_error (bfd_error_bad_value);
3698 return FALSE;
3701 if (alloc == 0)
3702 alloc = count;
3704 amt = alloc * sizeof (Elf_Internal_Phdr);
3705 phdrs = bfd_alloc (abfd, amt);
3706 if (phdrs == NULL)
3707 return FALSE;
3709 off = bed->s->sizeof_ehdr;
3710 off += alloc * bed->s->sizeof_phdr;
3712 filehdr_vaddr = 0;
3713 filehdr_paddr = 0;
3714 phdrs_vaddr = 0;
3715 phdrs_paddr = 0;
3717 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3718 m != NULL;
3719 m = m->next, p++)
3721 unsigned int i;
3722 asection **secpp;
3724 /* If elf_segment_map is not from map_sections_to_segments, the
3725 sections may not be correctly ordered. NOTE: sorting should
3726 not be done to the PT_NOTE section of a corefile, which may
3727 contain several pseudo-sections artificially created by bfd.
3728 Sorting these pseudo-sections breaks things badly. */
3729 if (m->count > 1
3730 && !(elf_elfheader (abfd)->e_type == ET_CORE
3731 && m->p_type == PT_NOTE))
3732 qsort (m->sections, (size_t) m->count, sizeof (asection *),
3733 elf_sort_sections);
3735 p->p_type = m->p_type;
3736 p->p_flags = m->p_flags;
3738 if (p->p_type == PT_LOAD
3739 && m->count > 0
3740 && (m->sections[0]->flags & SEC_ALLOC) != 0)
3742 if ((abfd->flags & D_PAGED) != 0)
3743 off += vma_page_aligned_bias (m->sections[0]->vma, off,
3744 bed->maxpagesize);
3745 else
3747 bfd_size_type align;
3749 align = 0;
3750 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3752 bfd_size_type secalign;
3754 secalign = bfd_get_section_alignment (abfd, *secpp);
3755 if (secalign > align)
3756 align = secalign;
3759 off += vma_page_aligned_bias (m->sections[0]->vma, off,
3760 1 << align);
3764 if (m->count == 0)
3765 p->p_vaddr = 0;
3766 else
3767 p->p_vaddr = m->sections[0]->vma;
3769 if (m->p_paddr_valid)
3770 p->p_paddr = m->p_paddr;
3771 else if (m->count == 0)
3772 p->p_paddr = 0;
3773 else
3774 p->p_paddr = m->sections[0]->lma;
3776 if (p->p_type == PT_LOAD
3777 && (abfd->flags & D_PAGED) != 0)
3778 p->p_align = bed->maxpagesize;
3779 else if (m->count == 0)
3780 p->p_align = 1 << bed->s->log_file_align;
3781 else
3782 p->p_align = 0;
3784 p->p_offset = 0;
3785 p->p_filesz = 0;
3786 p->p_memsz = 0;
3788 if (m->includes_filehdr)
3790 if (! m->p_flags_valid)
3791 p->p_flags |= PF_R;
3792 p->p_offset = 0;
3793 p->p_filesz = bed->s->sizeof_ehdr;
3794 p->p_memsz = bed->s->sizeof_ehdr;
3795 if (m->count > 0)
3797 BFD_ASSERT (p->p_type == PT_LOAD);
3799 if (p->p_vaddr < (bfd_vma) off)
3801 (*_bfd_error_handler)
3802 (_("%s: Not enough room for program headers, try linking with -N"),
3803 bfd_get_filename (abfd));
3804 bfd_set_error (bfd_error_bad_value);
3805 return FALSE;
3808 p->p_vaddr -= off;
3809 if (! m->p_paddr_valid)
3810 p->p_paddr -= off;
3812 if (p->p_type == PT_LOAD)
3814 filehdr_vaddr = p->p_vaddr;
3815 filehdr_paddr = p->p_paddr;
3819 if (m->includes_phdrs)
3821 if (! m->p_flags_valid)
3822 p->p_flags |= PF_R;
3824 if (m->includes_filehdr)
3826 if (p->p_type == PT_LOAD)
3828 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
3829 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
3832 else
3834 p->p_offset = bed->s->sizeof_ehdr;
3836 if (m->count > 0)
3838 BFD_ASSERT (p->p_type == PT_LOAD);
3839 p->p_vaddr -= off - p->p_offset;
3840 if (! m->p_paddr_valid)
3841 p->p_paddr -= off - p->p_offset;
3844 if (p->p_type == PT_LOAD)
3846 phdrs_vaddr = p->p_vaddr;
3847 phdrs_paddr = p->p_paddr;
3849 else
3850 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
3853 p->p_filesz += alloc * bed->s->sizeof_phdr;
3854 p->p_memsz += alloc * bed->s->sizeof_phdr;
3857 if (p->p_type == PT_LOAD
3858 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
3860 if (! m->includes_filehdr && ! m->includes_phdrs)
3861 p->p_offset = off;
3862 else
3864 file_ptr adjust;
3866 adjust = off - (p->p_offset + p->p_filesz);
3867 p->p_filesz += adjust;
3868 p->p_memsz += adjust;
3872 voff = off;
3874 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3876 asection *sec;
3877 flagword flags;
3878 bfd_size_type align;
3880 sec = *secpp;
3881 flags = sec->flags;
3882 align = 1 << bfd_get_section_alignment (abfd, sec);
3884 /* The section may have artificial alignment forced by a
3885 link script. Notice this case by the gap between the
3886 cumulative phdr lma and the section's lma. */
3887 if (p->p_paddr + p->p_memsz < sec->lma)
3889 bfd_vma adjust = sec->lma - (p->p_paddr + p->p_memsz);
3891 p->p_memsz += adjust;
3892 if (p->p_type == PT_LOAD
3893 || (p->p_type == PT_NOTE
3894 && bfd_get_format (abfd) == bfd_core))
3896 off += adjust;
3897 voff += adjust;
3899 if ((flags & SEC_LOAD) != 0
3900 || (flags & SEC_THREAD_LOCAL) != 0)
3901 p->p_filesz += adjust;
3904 if (p->p_type == PT_LOAD)
3906 bfd_signed_vma adjust;
3908 if ((flags & SEC_LOAD) != 0)
3910 adjust = sec->lma - (p->p_paddr + p->p_memsz);
3911 if (adjust < 0)
3912 adjust = 0;
3914 else if ((flags & SEC_ALLOC) != 0)
3916 /* The section VMA must equal the file position
3917 modulo the page size. FIXME: I'm not sure if
3918 this adjustment is really necessary. We used to
3919 not have the SEC_LOAD case just above, and then
3920 this was necessary, but now I'm not sure. */
3921 if ((abfd->flags & D_PAGED) != 0)
3922 adjust = vma_page_aligned_bias (sec->vma, voff,
3923 bed->maxpagesize);
3924 else
3925 adjust = vma_page_aligned_bias (sec->vma, voff,
3926 align);
3928 else
3929 adjust = 0;
3931 if (adjust != 0)
3933 if (i == 0)
3935 (* _bfd_error_handler) (_("\
3936 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
3937 bfd_section_name (abfd, sec),
3938 sec->lma,
3939 p->p_paddr);
3940 return FALSE;
3942 p->p_memsz += adjust;
3943 off += adjust;
3944 voff += adjust;
3945 if ((flags & SEC_LOAD) != 0)
3946 p->p_filesz += adjust;
3949 sec->filepos = off;
3951 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3952 used in a linker script we may have a section with
3953 SEC_LOAD clear but which is supposed to have
3954 contents. */
3955 if ((flags & SEC_LOAD) != 0
3956 || (flags & SEC_HAS_CONTENTS) != 0)
3957 off += sec->_raw_size;
3959 if ((flags & SEC_ALLOC) != 0
3960 && ((flags & SEC_LOAD) != 0
3961 || (flags & SEC_THREAD_LOCAL) == 0))
3962 voff += sec->_raw_size;
3965 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
3967 /* The actual "note" segment has i == 0.
3968 This is the one that actually contains everything. */
3969 if (i == 0)
3971 sec->filepos = off;
3972 p->p_filesz = sec->_raw_size;
3973 off += sec->_raw_size;
3974 voff = off;
3976 else
3978 /* Fake sections -- don't need to be written. */
3979 sec->filepos = 0;
3980 sec->_raw_size = 0;
3981 flags = sec->flags = 0;
3983 p->p_memsz = 0;
3984 p->p_align = 1;
3986 else
3988 if ((sec->flags & SEC_LOAD) != 0
3989 || (sec->flags & SEC_THREAD_LOCAL) == 0
3990 || p->p_type == PT_TLS)
3991 p->p_memsz += sec->_raw_size;
3993 if ((flags & SEC_LOAD) != 0)
3994 p->p_filesz += sec->_raw_size;
3996 if (p->p_type == PT_TLS
3997 && sec->_raw_size == 0
3998 && (sec->flags & SEC_HAS_CONTENTS) == 0)
4000 struct bfd_link_order *o;
4001 bfd_vma tbss_size = 0;
4003 for (o = sec->link_order_head; o != NULL; o = o->next)
4004 if (tbss_size < o->offset + o->size)
4005 tbss_size = o->offset + o->size;
4007 p->p_memsz += tbss_size;
4010 if (align > p->p_align
4011 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
4012 p->p_align = align;
4015 if (! m->p_flags_valid)
4017 p->p_flags |= PF_R;
4018 if ((flags & SEC_CODE) != 0)
4019 p->p_flags |= PF_X;
4020 if ((flags & SEC_READONLY) == 0)
4021 p->p_flags |= PF_W;
4026 /* Now that we have set the section file positions, we can set up
4027 the file positions for the non PT_LOAD segments. */
4028 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4029 m != NULL;
4030 m = m->next, p++)
4032 if (p->p_type != PT_LOAD && m->count > 0)
4034 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
4035 p->p_offset = m->sections[0]->filepos;
4037 if (m->count == 0)
4039 if (m->includes_filehdr)
4041 p->p_vaddr = filehdr_vaddr;
4042 if (! m->p_paddr_valid)
4043 p->p_paddr = filehdr_paddr;
4045 else if (m->includes_phdrs)
4047 p->p_vaddr = phdrs_vaddr;
4048 if (! m->p_paddr_valid)
4049 p->p_paddr = phdrs_paddr;
4054 /* Clear out any program headers we allocated but did not use. */
4055 for (; count < alloc; count++, p++)
4057 memset (p, 0, sizeof *p);
4058 p->p_type = PT_NULL;
4061 elf_tdata (abfd)->phdr = phdrs;
4063 elf_tdata (abfd)->next_file_pos = off;
4065 /* Write out the program headers. */
4066 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4067 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
4068 return FALSE;
4070 return TRUE;
4073 /* Get the size of the program header.
4075 If this is called by the linker before any of the section VMA's are set, it
4076 can't calculate the correct value for a strange memory layout. This only
4077 happens when SIZEOF_HEADERS is used in a linker script. In this case,
4078 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
4079 data segment (exclusive of .interp and .dynamic).
4081 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
4082 will be two segments. */
4084 static bfd_size_type
4085 get_program_header_size (bfd *abfd)
4087 size_t segs;
4088 asection *s;
4089 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4091 /* We can't return a different result each time we're called. */
4092 if (elf_tdata (abfd)->program_header_size != 0)
4093 return elf_tdata (abfd)->program_header_size;
4095 if (elf_tdata (abfd)->segment_map != NULL)
4097 struct elf_segment_map *m;
4099 segs = 0;
4100 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4101 ++segs;
4102 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
4103 return elf_tdata (abfd)->program_header_size;
4106 /* Assume we will need exactly two PT_LOAD segments: one for text
4107 and one for data. */
4108 segs = 2;
4110 s = bfd_get_section_by_name (abfd, ".interp");
4111 if (s != NULL && (s->flags & SEC_LOAD) != 0)
4113 /* If we have a loadable interpreter section, we need a
4114 PT_INTERP segment. In this case, assume we also need a
4115 PT_PHDR segment, although that may not be true for all
4116 targets. */
4117 segs += 2;
4120 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4122 /* We need a PT_DYNAMIC segment. */
4123 ++segs;
4126 if (elf_tdata (abfd)->eh_frame_hdr)
4128 /* We need a PT_GNU_EH_FRAME segment. */
4129 ++segs;
4132 if (elf_tdata (abfd)->stack_flags)
4134 /* We need a PT_GNU_STACK segment. */
4135 ++segs;
4138 for (s = abfd->sections; s != NULL; s = s->next)
4140 if ((s->flags & SEC_LOAD) != 0
4141 && strncmp (s->name, ".note", 5) == 0)
4143 /* We need a PT_NOTE segment. */
4144 ++segs;
4148 for (s = abfd->sections; s != NULL; s = s->next)
4150 if (s->flags & SEC_THREAD_LOCAL)
4152 /* We need a PT_TLS segment. */
4153 ++segs;
4154 break;
4158 /* Let the backend count up any program headers it might need. */
4159 if (bed->elf_backend_additional_program_headers)
4161 int a;
4163 a = (*bed->elf_backend_additional_program_headers) (abfd);
4164 if (a == -1)
4165 abort ();
4166 segs += a;
4169 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
4170 return elf_tdata (abfd)->program_header_size;
4173 /* Work out the file positions of all the sections. This is called by
4174 _bfd_elf_compute_section_file_positions. All the section sizes and
4175 VMAs must be known before this is called.
4177 We do not consider reloc sections at this point, unless they form
4178 part of the loadable image. Reloc sections are assigned file
4179 positions in assign_file_positions_for_relocs, which is called by
4180 write_object_contents and final_link.
4182 We also don't set the positions of the .symtab and .strtab here. */
4184 static bfd_boolean
4185 assign_file_positions_except_relocs (bfd *abfd,
4186 struct bfd_link_info *link_info)
4188 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
4189 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
4190 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4191 unsigned int num_sec = elf_numsections (abfd);
4192 file_ptr off;
4193 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4195 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4196 && bfd_get_format (abfd) != bfd_core)
4198 Elf_Internal_Shdr **hdrpp;
4199 unsigned int i;
4201 /* Start after the ELF header. */
4202 off = i_ehdrp->e_ehsize;
4204 /* We are not creating an executable, which means that we are
4205 not creating a program header, and that the actual order of
4206 the sections in the file is unimportant. */
4207 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4209 Elf_Internal_Shdr *hdr;
4211 hdr = *hdrpp;
4212 if (hdr->sh_type == SHT_REL
4213 || hdr->sh_type == SHT_RELA
4214 || i == tdata->symtab_section
4215 || i == tdata->symtab_shndx_section
4216 || i == tdata->strtab_section)
4218 hdr->sh_offset = -1;
4220 else
4221 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4223 if (i == SHN_LORESERVE - 1)
4225 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4226 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4230 else
4232 unsigned int i;
4233 Elf_Internal_Shdr **hdrpp;
4235 /* Assign file positions for the loaded sections based on the
4236 assignment of sections to segments. */
4237 if (! assign_file_positions_for_segments (abfd, link_info))
4238 return FALSE;
4240 /* Assign file positions for the other sections. */
4242 off = elf_tdata (abfd)->next_file_pos;
4243 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4245 Elf_Internal_Shdr *hdr;
4247 hdr = *hdrpp;
4248 if (hdr->bfd_section != NULL
4249 && hdr->bfd_section->filepos != 0)
4250 hdr->sh_offset = hdr->bfd_section->filepos;
4251 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4253 ((*_bfd_error_handler)
4254 (_("%s: warning: allocated section `%s' not in segment"),
4255 bfd_get_filename (abfd),
4256 (hdr->bfd_section == NULL
4257 ? "*unknown*"
4258 : hdr->bfd_section->name)));
4259 if ((abfd->flags & D_PAGED) != 0)
4260 off += vma_page_aligned_bias (hdr->sh_addr, off,
4261 bed->maxpagesize);
4262 else
4263 off += vma_page_aligned_bias (hdr->sh_addr, off,
4264 hdr->sh_addralign);
4265 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4266 FALSE);
4268 else if (hdr == i_shdrpp[tdata->symtab_section]
4269 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4270 || hdr == i_shdrpp[tdata->strtab_section])
4271 hdr->sh_offset = -1;
4272 else
4273 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4275 if (i == SHN_LORESERVE - 1)
4277 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4278 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4283 /* Place the section headers. */
4284 off = align_file_position (off, 1 << bed->s->log_file_align);
4285 i_ehdrp->e_shoff = off;
4286 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4288 elf_tdata (abfd)->next_file_pos = off;
4290 return TRUE;
4293 static bfd_boolean
4294 prep_headers (bfd *abfd)
4296 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
4297 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4298 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
4299 struct elf_strtab_hash *shstrtab;
4300 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4302 i_ehdrp = elf_elfheader (abfd);
4303 i_shdrp = elf_elfsections (abfd);
4305 shstrtab = _bfd_elf_strtab_init ();
4306 if (shstrtab == NULL)
4307 return FALSE;
4309 elf_shstrtab (abfd) = shstrtab;
4311 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4312 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4313 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4314 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4316 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4317 i_ehdrp->e_ident[EI_DATA] =
4318 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4319 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4321 if ((abfd->flags & DYNAMIC) != 0)
4322 i_ehdrp->e_type = ET_DYN;
4323 else if ((abfd->flags & EXEC_P) != 0)
4324 i_ehdrp->e_type = ET_EXEC;
4325 else if (bfd_get_format (abfd) == bfd_core)
4326 i_ehdrp->e_type = ET_CORE;
4327 else
4328 i_ehdrp->e_type = ET_REL;
4330 switch (bfd_get_arch (abfd))
4332 case bfd_arch_unknown:
4333 i_ehdrp->e_machine = EM_NONE;
4334 break;
4336 /* There used to be a long list of cases here, each one setting
4337 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4338 in the corresponding bfd definition. To avoid duplication,
4339 the switch was removed. Machines that need special handling
4340 can generally do it in elf_backend_final_write_processing(),
4341 unless they need the information earlier than the final write.
4342 Such need can generally be supplied by replacing the tests for
4343 e_machine with the conditions used to determine it. */
4344 default:
4345 i_ehdrp->e_machine = bed->elf_machine_code;
4348 i_ehdrp->e_version = bed->s->ev_current;
4349 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4351 /* No program header, for now. */
4352 i_ehdrp->e_phoff = 0;
4353 i_ehdrp->e_phentsize = 0;
4354 i_ehdrp->e_phnum = 0;
4356 /* Each bfd section is section header entry. */
4357 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4358 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4360 /* If we're building an executable, we'll need a program header table. */
4361 if (abfd->flags & EXEC_P)
4363 /* It all happens later. */
4364 #if 0
4365 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
4367 /* elf_build_phdrs() returns a (NULL-terminated) array of
4368 Elf_Internal_Phdrs. */
4369 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
4370 i_ehdrp->e_phoff = outbase;
4371 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
4372 #endif
4374 else
4376 i_ehdrp->e_phentsize = 0;
4377 i_phdrp = 0;
4378 i_ehdrp->e_phoff = 0;
4381 elf_tdata (abfd)->symtab_hdr.sh_name =
4382 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
4383 elf_tdata (abfd)->strtab_hdr.sh_name =
4384 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
4385 elf_tdata (abfd)->shstrtab_hdr.sh_name =
4386 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
4387 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4388 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4389 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4390 return FALSE;
4392 return TRUE;
4395 /* Assign file positions for all the reloc sections which are not part
4396 of the loadable file image. */
4398 void
4399 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
4401 file_ptr off;
4402 unsigned int i, num_sec;
4403 Elf_Internal_Shdr **shdrpp;
4405 off = elf_tdata (abfd)->next_file_pos;
4407 num_sec = elf_numsections (abfd);
4408 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
4410 Elf_Internal_Shdr *shdrp;
4412 shdrp = *shdrpp;
4413 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4414 && shdrp->sh_offset == -1)
4415 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
4418 elf_tdata (abfd)->next_file_pos = off;
4421 bfd_boolean
4422 _bfd_elf_write_object_contents (bfd *abfd)
4424 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4425 Elf_Internal_Ehdr *i_ehdrp;
4426 Elf_Internal_Shdr **i_shdrp;
4427 bfd_boolean failed;
4428 unsigned int count, num_sec;
4430 if (! abfd->output_has_begun
4431 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
4432 return FALSE;
4434 i_shdrp = elf_elfsections (abfd);
4435 i_ehdrp = elf_elfheader (abfd);
4437 failed = FALSE;
4438 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4439 if (failed)
4440 return FALSE;
4442 _bfd_elf_assign_file_positions_for_relocs (abfd);
4444 /* After writing the headers, we need to write the sections too... */
4445 num_sec = elf_numsections (abfd);
4446 for (count = 1; count < num_sec; count++)
4448 if (bed->elf_backend_section_processing)
4449 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4450 if (i_shdrp[count]->contents)
4452 bfd_size_type amt = i_shdrp[count]->sh_size;
4454 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
4455 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
4456 return FALSE;
4458 if (count == SHN_LORESERVE - 1)
4459 count += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4462 /* Write out the section header names. */
4463 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
4464 || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))
4465 return FALSE;
4467 if (bed->elf_backend_final_write_processing)
4468 (*bed->elf_backend_final_write_processing) (abfd,
4469 elf_tdata (abfd)->linker);
4471 return bed->s->write_shdrs_and_ehdr (abfd);
4474 bfd_boolean
4475 _bfd_elf_write_corefile_contents (bfd *abfd)
4477 /* Hopefully this can be done just like an object file. */
4478 return _bfd_elf_write_object_contents (abfd);
4481 /* Given a section, search the header to find them. */
4484 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
4486 const struct elf_backend_data *bed;
4487 int index;
4489 if (elf_section_data (asect) != NULL
4490 && elf_section_data (asect)->this_idx != 0)
4491 return elf_section_data (asect)->this_idx;
4493 if (bfd_is_abs_section (asect))
4494 index = SHN_ABS;
4495 else if (bfd_is_com_section (asect))
4496 index = SHN_COMMON;
4497 else if (bfd_is_und_section (asect))
4498 index = SHN_UNDEF;
4499 else
4501 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
4502 int maxindex = elf_numsections (abfd);
4504 for (index = 1; index < maxindex; index++)
4506 Elf_Internal_Shdr *hdr = i_shdrp[index];
4508 if (hdr != NULL && hdr->bfd_section == asect)
4509 return index;
4511 index = -1;
4514 bed = get_elf_backend_data (abfd);
4515 if (bed->elf_backend_section_from_bfd_section)
4517 int retval = index;
4519 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
4520 return retval;
4523 if (index == -1)
4524 bfd_set_error (bfd_error_nonrepresentable_section);
4526 return index;
4529 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4530 on error. */
4533 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
4535 asymbol *asym_ptr = *asym_ptr_ptr;
4536 int idx;
4537 flagword flags = asym_ptr->flags;
4539 /* When gas creates relocations against local labels, it creates its
4540 own symbol for the section, but does put the symbol into the
4541 symbol chain, so udata is 0. When the linker is generating
4542 relocatable output, this section symbol may be for one of the
4543 input sections rather than the output section. */
4544 if (asym_ptr->udata.i == 0
4545 && (flags & BSF_SECTION_SYM)
4546 && asym_ptr->section)
4548 int indx;
4550 if (asym_ptr->section->output_section != NULL)
4551 indx = asym_ptr->section->output_section->index;
4552 else
4553 indx = asym_ptr->section->index;
4554 if (indx < elf_num_section_syms (abfd)
4555 && elf_section_syms (abfd)[indx] != NULL)
4556 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
4559 idx = asym_ptr->udata.i;
4561 if (idx == 0)
4563 /* This case can occur when using --strip-symbol on a symbol
4564 which is used in a relocation entry. */
4565 (*_bfd_error_handler)
4566 (_("%s: symbol `%s' required but not present"),
4567 bfd_archive_filename (abfd), bfd_asymbol_name (asym_ptr));
4568 bfd_set_error (bfd_error_no_symbols);
4569 return -1;
4572 #if DEBUG & 4
4574 fprintf (stderr,
4575 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
4576 (long) asym_ptr, asym_ptr->name, idx, flags,
4577 elf_symbol_flags (flags));
4578 fflush (stderr);
4580 #endif
4582 return idx;
4585 /* Copy private BFD data. This copies any program header information. */
4587 static bfd_boolean
4588 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
4590 Elf_Internal_Ehdr *iehdr;
4591 struct elf_segment_map *map;
4592 struct elf_segment_map *map_first;
4593 struct elf_segment_map **pointer_to_map;
4594 Elf_Internal_Phdr *segment;
4595 asection *section;
4596 unsigned int i;
4597 unsigned int num_segments;
4598 bfd_boolean phdr_included = FALSE;
4599 bfd_vma maxpagesize;
4600 struct elf_segment_map *phdr_adjust_seg = NULL;
4601 unsigned int phdr_adjust_num = 0;
4602 const struct elf_backend_data *bed;
4604 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4605 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4606 return TRUE;
4608 if (elf_tdata (ibfd)->phdr == NULL)
4609 return TRUE;
4611 bed = get_elf_backend_data (ibfd);
4612 iehdr = elf_elfheader (ibfd);
4614 map_first = NULL;
4615 pointer_to_map = &map_first;
4617 num_segments = elf_elfheader (ibfd)->e_phnum;
4618 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
4620 /* Returns the end address of the segment + 1. */
4621 #define SEGMENT_END(segment, start) \
4622 (start + (segment->p_memsz > segment->p_filesz \
4623 ? segment->p_memsz : segment->p_filesz))
4625 #define SECTION_SIZE(section, segment) \
4626 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
4627 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
4628 ? section->_raw_size : 0)
4630 /* Returns TRUE if the given section is contained within
4631 the given segment. VMA addresses are compared. */
4632 #define IS_CONTAINED_BY_VMA(section, segment) \
4633 (section->vma >= segment->p_vaddr \
4634 && (section->vma + SECTION_SIZE (section, segment) \
4635 <= (SEGMENT_END (segment, segment->p_vaddr))))
4637 /* Returns TRUE if the given section is contained within
4638 the given segment. LMA addresses are compared. */
4639 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4640 (section->lma >= base \
4641 && (section->lma + SECTION_SIZE (section, segment) \
4642 <= SEGMENT_END (segment, base)))
4644 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4645 #define IS_COREFILE_NOTE(p, s) \
4646 (p->p_type == PT_NOTE \
4647 && bfd_get_format (ibfd) == bfd_core \
4648 && s->vma == 0 && s->lma == 0 \
4649 && (bfd_vma) s->filepos >= p->p_offset \
4650 && ((bfd_vma) s->filepos + s->_raw_size \
4651 <= p->p_offset + p->p_filesz))
4653 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4654 linker, which generates a PT_INTERP section with p_vaddr and
4655 p_memsz set to 0. */
4656 #define IS_SOLARIS_PT_INTERP(p, s) \
4657 (p->p_vaddr == 0 \
4658 && p->p_paddr == 0 \
4659 && p->p_memsz == 0 \
4660 && p->p_filesz > 0 \
4661 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4662 && s->_raw_size > 0 \
4663 && (bfd_vma) s->filepos >= p->p_offset \
4664 && ((bfd_vma) s->filepos + s->_raw_size \
4665 <= p->p_offset + p->p_filesz))
4667 /* Decide if the given section should be included in the given segment.
4668 A section will be included if:
4669 1. It is within the address space of the segment -- we use the LMA
4670 if that is set for the segment and the VMA otherwise,
4671 2. It is an allocated segment,
4672 3. There is an output section associated with it,
4673 4. The section has not already been allocated to a previous segment.
4674 5. PT_GNU_STACK segments do not include any sections.
4675 6. PT_TLS segment includes only SHF_TLS sections.
4676 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. */
4677 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
4678 ((((segment->p_paddr \
4679 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4680 : IS_CONTAINED_BY_VMA (section, segment)) \
4681 && (section->flags & SEC_ALLOC) != 0) \
4682 || IS_COREFILE_NOTE (segment, section)) \
4683 && section->output_section != NULL \
4684 && segment->p_type != PT_GNU_STACK \
4685 && (segment->p_type != PT_TLS \
4686 || (section->flags & SEC_THREAD_LOCAL)) \
4687 && (segment->p_type == PT_LOAD \
4688 || segment->p_type == PT_TLS \
4689 || (section->flags & SEC_THREAD_LOCAL) == 0) \
4690 && ! section->segment_mark)
4692 /* Returns TRUE iff seg1 starts after the end of seg2. */
4693 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
4694 (seg1->field >= SEGMENT_END (seg2, seg2->field))
4696 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
4697 their VMA address ranges and their LMA address ranges overlap.
4698 It is possible to have overlapping VMA ranges without overlapping LMA
4699 ranges. RedBoot images for example can have both .data and .bss mapped
4700 to the same VMA range, but with the .data section mapped to a different
4701 LMA. */
4702 #define SEGMENT_OVERLAPS(seg1, seg2) \
4703 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
4704 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
4705 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
4706 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
4708 /* Initialise the segment mark field. */
4709 for (section = ibfd->sections; section != NULL; section = section->next)
4710 section->segment_mark = FALSE;
4712 /* Scan through the segments specified in the program header
4713 of the input BFD. For this first scan we look for overlaps
4714 in the loadable segments. These can be created by weird
4715 parameters to objcopy. Also, fix some solaris weirdness. */
4716 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4717 i < num_segments;
4718 i++, segment++)
4720 unsigned int j;
4721 Elf_Internal_Phdr *segment2;
4723 if (segment->p_type == PT_INTERP)
4724 for (section = ibfd->sections; section; section = section->next)
4725 if (IS_SOLARIS_PT_INTERP (segment, section))
4727 /* Mininal change so that the normal section to segment
4728 assignment code will work. */
4729 segment->p_vaddr = section->vma;
4730 break;
4733 if (segment->p_type != PT_LOAD)
4734 continue;
4736 /* Determine if this segment overlaps any previous segments. */
4737 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
4739 bfd_signed_vma extra_length;
4741 if (segment2->p_type != PT_LOAD
4742 || ! SEGMENT_OVERLAPS (segment, segment2))
4743 continue;
4745 /* Merge the two segments together. */
4746 if (segment2->p_vaddr < segment->p_vaddr)
4748 /* Extend SEGMENT2 to include SEGMENT and then delete
4749 SEGMENT. */
4750 extra_length =
4751 SEGMENT_END (segment, segment->p_vaddr)
4752 - SEGMENT_END (segment2, segment2->p_vaddr);
4754 if (extra_length > 0)
4756 segment2->p_memsz += extra_length;
4757 segment2->p_filesz += extra_length;
4760 segment->p_type = PT_NULL;
4762 /* Since we have deleted P we must restart the outer loop. */
4763 i = 0;
4764 segment = elf_tdata (ibfd)->phdr;
4765 break;
4767 else
4769 /* Extend SEGMENT to include SEGMENT2 and then delete
4770 SEGMENT2. */
4771 extra_length =
4772 SEGMENT_END (segment2, segment2->p_vaddr)
4773 - SEGMENT_END (segment, segment->p_vaddr);
4775 if (extra_length > 0)
4777 segment->p_memsz += extra_length;
4778 segment->p_filesz += extra_length;
4781 segment2->p_type = PT_NULL;
4786 /* The second scan attempts to assign sections to segments. */
4787 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4788 i < num_segments;
4789 i ++, segment ++)
4791 unsigned int section_count;
4792 asection ** sections;
4793 asection * output_section;
4794 unsigned int isec;
4795 bfd_vma matching_lma;
4796 bfd_vma suggested_lma;
4797 unsigned int j;
4798 bfd_size_type amt;
4800 if (segment->p_type == PT_NULL)
4801 continue;
4803 /* Compute how many sections might be placed into this segment. */
4804 for (section = ibfd->sections, section_count = 0;
4805 section != NULL;
4806 section = section->next)
4807 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
4808 ++section_count;
4810 /* Allocate a segment map big enough to contain
4811 all of the sections we have selected. */
4812 amt = sizeof (struct elf_segment_map);
4813 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
4814 map = bfd_alloc (obfd, amt);
4815 if (map == NULL)
4816 return FALSE;
4818 /* Initialise the fields of the segment map. Default to
4819 using the physical address of the segment in the input BFD. */
4820 map->next = NULL;
4821 map->p_type = segment->p_type;
4822 map->p_flags = segment->p_flags;
4823 map->p_flags_valid = 1;
4824 map->p_paddr = segment->p_paddr;
4825 map->p_paddr_valid = 1;
4827 /* Determine if this segment contains the ELF file header
4828 and if it contains the program headers themselves. */
4829 map->includes_filehdr = (segment->p_offset == 0
4830 && segment->p_filesz >= iehdr->e_ehsize);
4832 map->includes_phdrs = 0;
4834 if (! phdr_included || segment->p_type != PT_LOAD)
4836 map->includes_phdrs =
4837 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
4838 && (segment->p_offset + segment->p_filesz
4839 >= ((bfd_vma) iehdr->e_phoff
4840 + iehdr->e_phnum * iehdr->e_phentsize)));
4842 if (segment->p_type == PT_LOAD && map->includes_phdrs)
4843 phdr_included = TRUE;
4846 if (section_count == 0)
4848 /* Special segments, such as the PT_PHDR segment, may contain
4849 no sections, but ordinary, loadable segments should contain
4850 something. They are allowed by the ELF spec however, so only
4851 a warning is produced. */
4852 if (segment->p_type == PT_LOAD)
4853 (*_bfd_error_handler)
4854 (_("%s: warning: Empty loadable segment detected, is this intentional ?\n"),
4855 bfd_archive_filename (ibfd));
4857 map->count = 0;
4858 *pointer_to_map = map;
4859 pointer_to_map = &map->next;
4861 continue;
4864 /* Now scan the sections in the input BFD again and attempt
4865 to add their corresponding output sections to the segment map.
4866 The problem here is how to handle an output section which has
4867 been moved (ie had its LMA changed). There are four possibilities:
4869 1. None of the sections have been moved.
4870 In this case we can continue to use the segment LMA from the
4871 input BFD.
4873 2. All of the sections have been moved by the same amount.
4874 In this case we can change the segment's LMA to match the LMA
4875 of the first section.
4877 3. Some of the sections have been moved, others have not.
4878 In this case those sections which have not been moved can be
4879 placed in the current segment which will have to have its size,
4880 and possibly its LMA changed, and a new segment or segments will
4881 have to be created to contain the other sections.
4883 4. The sections have been moved, but not by the same amount.
4884 In this case we can change the segment's LMA to match the LMA
4885 of the first section and we will have to create a new segment
4886 or segments to contain the other sections.
4888 In order to save time, we allocate an array to hold the section
4889 pointers that we are interested in. As these sections get assigned
4890 to a segment, they are removed from this array. */
4892 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
4893 to work around this long long bug. */
4894 amt = section_count * sizeof (asection *);
4895 sections = bfd_malloc (amt);
4896 if (sections == NULL)
4897 return FALSE;
4899 /* Step One: Scan for segment vs section LMA conflicts.
4900 Also add the sections to the section array allocated above.
4901 Also add the sections to the current segment. In the common
4902 case, where the sections have not been moved, this means that
4903 we have completely filled the segment, and there is nothing
4904 more to do. */
4905 isec = 0;
4906 matching_lma = 0;
4907 suggested_lma = 0;
4909 for (j = 0, section = ibfd->sections;
4910 section != NULL;
4911 section = section->next)
4913 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
4915 output_section = section->output_section;
4917 sections[j ++] = section;
4919 /* The Solaris native linker always sets p_paddr to 0.
4920 We try to catch that case here, and set it to the
4921 correct value. Note - some backends require that
4922 p_paddr be left as zero. */
4923 if (segment->p_paddr == 0
4924 && segment->p_vaddr != 0
4925 && (! bed->want_p_paddr_set_to_zero)
4926 && isec == 0
4927 && output_section->lma != 0
4928 && (output_section->vma == (segment->p_vaddr
4929 + (map->includes_filehdr
4930 ? iehdr->e_ehsize
4931 : 0)
4932 + (map->includes_phdrs
4933 ? (iehdr->e_phnum
4934 * iehdr->e_phentsize)
4935 : 0))))
4936 map->p_paddr = segment->p_vaddr;
4938 /* Match up the physical address of the segment with the
4939 LMA address of the output section. */
4940 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4941 || IS_COREFILE_NOTE (segment, section)
4942 || (bed->want_p_paddr_set_to_zero &&
4943 IS_CONTAINED_BY_VMA (output_section, segment))
4946 if (matching_lma == 0)
4947 matching_lma = output_section->lma;
4949 /* We assume that if the section fits within the segment
4950 then it does not overlap any other section within that
4951 segment. */
4952 map->sections[isec ++] = output_section;
4954 else if (suggested_lma == 0)
4955 suggested_lma = output_section->lma;
4959 BFD_ASSERT (j == section_count);
4961 /* Step Two: Adjust the physical address of the current segment,
4962 if necessary. */
4963 if (isec == section_count)
4965 /* All of the sections fitted within the segment as currently
4966 specified. This is the default case. Add the segment to
4967 the list of built segments and carry on to process the next
4968 program header in the input BFD. */
4969 map->count = section_count;
4970 *pointer_to_map = map;
4971 pointer_to_map = &map->next;
4973 free (sections);
4974 continue;
4976 else
4978 if (matching_lma != 0)
4980 /* At least one section fits inside the current segment.
4981 Keep it, but modify its physical address to match the
4982 LMA of the first section that fitted. */
4983 map->p_paddr = matching_lma;
4985 else
4987 /* None of the sections fitted inside the current segment.
4988 Change the current segment's physical address to match
4989 the LMA of the first section. */
4990 map->p_paddr = suggested_lma;
4993 /* Offset the segment physical address from the lma
4994 to allow for space taken up by elf headers. */
4995 if (map->includes_filehdr)
4996 map->p_paddr -= iehdr->e_ehsize;
4998 if (map->includes_phdrs)
5000 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5002 /* iehdr->e_phnum is just an estimate of the number
5003 of program headers that we will need. Make a note
5004 here of the number we used and the segment we chose
5005 to hold these headers, so that we can adjust the
5006 offset when we know the correct value. */
5007 phdr_adjust_num = iehdr->e_phnum;
5008 phdr_adjust_seg = map;
5012 /* Step Three: Loop over the sections again, this time assigning
5013 those that fit to the current segment and removing them from the
5014 sections array; but making sure not to leave large gaps. Once all
5015 possible sections have been assigned to the current segment it is
5016 added to the list of built segments and if sections still remain
5017 to be assigned, a new segment is constructed before repeating
5018 the loop. */
5019 isec = 0;
5022 map->count = 0;
5023 suggested_lma = 0;
5025 /* Fill the current segment with sections that fit. */
5026 for (j = 0; j < section_count; j++)
5028 section = sections[j];
5030 if (section == NULL)
5031 continue;
5033 output_section = section->output_section;
5035 BFD_ASSERT (output_section != NULL);
5037 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5038 || IS_COREFILE_NOTE (segment, section))
5040 if (map->count == 0)
5042 /* If the first section in a segment does not start at
5043 the beginning of the segment, then something is
5044 wrong. */
5045 if (output_section->lma !=
5046 (map->p_paddr
5047 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5048 + (map->includes_phdrs
5049 ? iehdr->e_phnum * iehdr->e_phentsize
5050 : 0)))
5051 abort ();
5053 else
5055 asection * prev_sec;
5057 prev_sec = map->sections[map->count - 1];
5059 /* If the gap between the end of the previous section
5060 and the start of this section is more than
5061 maxpagesize then we need to start a new segment. */
5062 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size,
5063 maxpagesize)
5064 < BFD_ALIGN (output_section->lma, maxpagesize))
5065 || ((prev_sec->lma + prev_sec->_raw_size)
5066 > output_section->lma))
5068 if (suggested_lma == 0)
5069 suggested_lma = output_section->lma;
5071 continue;
5075 map->sections[map->count++] = output_section;
5076 ++isec;
5077 sections[j] = NULL;
5078 section->segment_mark = TRUE;
5080 else if (suggested_lma == 0)
5081 suggested_lma = output_section->lma;
5084 BFD_ASSERT (map->count > 0);
5086 /* Add the current segment to the list of built segments. */
5087 *pointer_to_map = map;
5088 pointer_to_map = &map->next;
5090 if (isec < section_count)
5092 /* We still have not allocated all of the sections to
5093 segments. Create a new segment here, initialise it
5094 and carry on looping. */
5095 amt = sizeof (struct elf_segment_map);
5096 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5097 map = bfd_alloc (obfd, amt);
5098 if (map == NULL)
5100 free (sections);
5101 return FALSE;
5104 /* Initialise the fields of the segment map. Set the physical
5105 physical address to the LMA of the first section that has
5106 not yet been assigned. */
5107 map->next = NULL;
5108 map->p_type = segment->p_type;
5109 map->p_flags = segment->p_flags;
5110 map->p_flags_valid = 1;
5111 map->p_paddr = suggested_lma;
5112 map->p_paddr_valid = 1;
5113 map->includes_filehdr = 0;
5114 map->includes_phdrs = 0;
5117 while (isec < section_count);
5119 free (sections);
5122 /* The Solaris linker creates program headers in which all the
5123 p_paddr fields are zero. When we try to objcopy or strip such a
5124 file, we get confused. Check for this case, and if we find it
5125 reset the p_paddr_valid fields. */
5126 for (map = map_first; map != NULL; map = map->next)
5127 if (map->p_paddr != 0)
5128 break;
5129 if (map == NULL)
5130 for (map = map_first; map != NULL; map = map->next)
5131 map->p_paddr_valid = 0;
5133 elf_tdata (obfd)->segment_map = map_first;
5135 /* If we had to estimate the number of program headers that were
5136 going to be needed, then check our estimate now and adjust
5137 the offset if necessary. */
5138 if (phdr_adjust_seg != NULL)
5140 unsigned int count;
5142 for (count = 0, map = map_first; map != NULL; map = map->next)
5143 count++;
5145 if (count > phdr_adjust_num)
5146 phdr_adjust_seg->p_paddr
5147 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5150 #if 0
5151 /* Final Step: Sort the segments into ascending order of physical
5152 address. */
5153 if (map_first != NULL)
5155 struct elf_segment_map *prev;
5157 prev = map_first;
5158 for (map = map_first->next; map != NULL; prev = map, map = map->next)
5160 /* Yes I know - its a bubble sort.... */
5161 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
5163 /* Swap map and map->next. */
5164 prev->next = map->next;
5165 map->next = map->next->next;
5166 prev->next->next = map;
5168 /* Restart loop. */
5169 map = map_first;
5173 #endif
5175 #undef SEGMENT_END
5176 #undef SECTION_SIZE
5177 #undef IS_CONTAINED_BY_VMA
5178 #undef IS_CONTAINED_BY_LMA
5179 #undef IS_COREFILE_NOTE
5180 #undef IS_SOLARIS_PT_INTERP
5181 #undef INCLUDE_SECTION_IN_SEGMENT
5182 #undef SEGMENT_AFTER_SEGMENT
5183 #undef SEGMENT_OVERLAPS
5184 return TRUE;
5187 /* Copy private section information. This copies over the entsize
5188 field, and sometimes the info field. */
5190 bfd_boolean
5191 _bfd_elf_copy_private_section_data (bfd *ibfd,
5192 asection *isec,
5193 bfd *obfd,
5194 asection *osec)
5196 Elf_Internal_Shdr *ihdr, *ohdr;
5198 if (ibfd->xvec->flavour != bfd_target_elf_flavour
5199 || obfd->xvec->flavour != bfd_target_elf_flavour)
5200 return TRUE;
5202 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
5204 asection *s;
5206 /* Only set up the segments if there are no more SEC_ALLOC
5207 sections. FIXME: This won't do the right thing if objcopy is
5208 used to remove the last SEC_ALLOC section, since objcopy
5209 won't call this routine in that case. */
5210 for (s = isec->next; s != NULL; s = s->next)
5211 if ((s->flags & SEC_ALLOC) != 0)
5212 break;
5213 if (s == NULL)
5215 if (! copy_private_bfd_data (ibfd, obfd))
5216 return FALSE;
5220 ihdr = &elf_section_data (isec)->this_hdr;
5221 ohdr = &elf_section_data (osec)->this_hdr;
5223 ohdr->sh_entsize = ihdr->sh_entsize;
5225 if (ihdr->sh_type == SHT_SYMTAB
5226 || ihdr->sh_type == SHT_DYNSYM
5227 || ihdr->sh_type == SHT_GNU_verneed
5228 || ihdr->sh_type == SHT_GNU_verdef)
5229 ohdr->sh_info = ihdr->sh_info;
5231 /* Set things up for objcopy. The output SHT_GROUP section will
5232 have its elf_next_in_group pointing back to the input group
5233 members. */
5234 elf_next_in_group (osec) = elf_next_in_group (isec);
5235 elf_group_name (osec) = elf_group_name (isec);
5237 osec->use_rela_p = isec->use_rela_p;
5239 return TRUE;
5242 /* Copy private symbol information. If this symbol is in a section
5243 which we did not map into a BFD section, try to map the section
5244 index correctly. We use special macro definitions for the mapped
5245 section indices; these definitions are interpreted by the
5246 swap_out_syms function. */
5248 #define MAP_ONESYMTAB (SHN_HIOS + 1)
5249 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
5250 #define MAP_STRTAB (SHN_HIOS + 3)
5251 #define MAP_SHSTRTAB (SHN_HIOS + 4)
5252 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
5254 bfd_boolean
5255 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
5256 asymbol *isymarg,
5257 bfd *obfd,
5258 asymbol *osymarg)
5260 elf_symbol_type *isym, *osym;
5262 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5263 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5264 return TRUE;
5266 isym = elf_symbol_from (ibfd, isymarg);
5267 osym = elf_symbol_from (obfd, osymarg);
5269 if (isym != NULL
5270 && osym != NULL
5271 && bfd_is_abs_section (isym->symbol.section))
5273 unsigned int shndx;
5275 shndx = isym->internal_elf_sym.st_shndx;
5276 if (shndx == elf_onesymtab (ibfd))
5277 shndx = MAP_ONESYMTAB;
5278 else if (shndx == elf_dynsymtab (ibfd))
5279 shndx = MAP_DYNSYMTAB;
5280 else if (shndx == elf_tdata (ibfd)->strtab_section)
5281 shndx = MAP_STRTAB;
5282 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
5283 shndx = MAP_SHSTRTAB;
5284 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
5285 shndx = MAP_SYM_SHNDX;
5286 osym->internal_elf_sym.st_shndx = shndx;
5289 return TRUE;
5292 /* Swap out the symbols. */
5294 static bfd_boolean
5295 swap_out_syms (bfd *abfd,
5296 struct bfd_strtab_hash **sttp,
5297 int relocatable_p)
5299 const struct elf_backend_data *bed;
5300 int symcount;
5301 asymbol **syms;
5302 struct bfd_strtab_hash *stt;
5303 Elf_Internal_Shdr *symtab_hdr;
5304 Elf_Internal_Shdr *symtab_shndx_hdr;
5305 Elf_Internal_Shdr *symstrtab_hdr;
5306 char *outbound_syms;
5307 char *outbound_shndx;
5308 int idx;
5309 bfd_size_type amt;
5310 bfd_boolean name_local_sections;
5312 if (!elf_map_symbols (abfd))
5313 return FALSE;
5315 /* Dump out the symtabs. */
5316 stt = _bfd_elf_stringtab_init ();
5317 if (stt == NULL)
5318 return FALSE;
5320 bed = get_elf_backend_data (abfd);
5321 symcount = bfd_get_symcount (abfd);
5322 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5323 symtab_hdr->sh_type = SHT_SYMTAB;
5324 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
5325 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
5326 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
5327 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
5329 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5330 symstrtab_hdr->sh_type = SHT_STRTAB;
5332 amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym;
5333 outbound_syms = bfd_alloc (abfd, amt);
5334 if (outbound_syms == NULL)
5336 _bfd_stringtab_free (stt);
5337 return FALSE;
5339 symtab_hdr->contents = outbound_syms;
5341 outbound_shndx = NULL;
5342 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
5343 if (symtab_shndx_hdr->sh_name != 0)
5345 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
5346 outbound_shndx = bfd_zalloc (abfd, amt);
5347 if (outbound_shndx == NULL)
5349 _bfd_stringtab_free (stt);
5350 return FALSE;
5353 symtab_shndx_hdr->contents = outbound_shndx;
5354 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
5355 symtab_shndx_hdr->sh_size = amt;
5356 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
5357 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
5360 /* Now generate the data (for "contents"). */
5362 /* Fill in zeroth symbol and swap it out. */
5363 Elf_Internal_Sym sym;
5364 sym.st_name = 0;
5365 sym.st_value = 0;
5366 sym.st_size = 0;
5367 sym.st_info = 0;
5368 sym.st_other = 0;
5369 sym.st_shndx = SHN_UNDEF;
5370 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5371 outbound_syms += bed->s->sizeof_sym;
5372 if (outbound_shndx != NULL)
5373 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5376 name_local_sections
5377 = (bed->elf_backend_name_local_section_symbols
5378 && bed->elf_backend_name_local_section_symbols (abfd));
5380 syms = bfd_get_outsymbols (abfd);
5381 for (idx = 0; idx < symcount; idx++)
5383 Elf_Internal_Sym sym;
5384 bfd_vma value = syms[idx]->value;
5385 elf_symbol_type *type_ptr;
5386 flagword flags = syms[idx]->flags;
5387 int type;
5389 if (!name_local_sections
5390 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
5392 /* Local section symbols have no name. */
5393 sym.st_name = 0;
5395 else
5397 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
5398 syms[idx]->name,
5399 TRUE, FALSE);
5400 if (sym.st_name == (unsigned long) -1)
5402 _bfd_stringtab_free (stt);
5403 return FALSE;
5407 type_ptr = elf_symbol_from (abfd, syms[idx]);
5409 if ((flags & BSF_SECTION_SYM) == 0
5410 && bfd_is_com_section (syms[idx]->section))
5412 /* ELF common symbols put the alignment into the `value' field,
5413 and the size into the `size' field. This is backwards from
5414 how BFD handles it, so reverse it here. */
5415 sym.st_size = value;
5416 if (type_ptr == NULL
5417 || type_ptr->internal_elf_sym.st_value == 0)
5418 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
5419 else
5420 sym.st_value = type_ptr->internal_elf_sym.st_value;
5421 sym.st_shndx = _bfd_elf_section_from_bfd_section
5422 (abfd, syms[idx]->section);
5424 else
5426 asection *sec = syms[idx]->section;
5427 int shndx;
5429 if (sec->output_section)
5431 value += sec->output_offset;
5432 sec = sec->output_section;
5435 /* Don't add in the section vma for relocatable output. */
5436 if (! relocatable_p)
5437 value += sec->vma;
5438 sym.st_value = value;
5439 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
5441 if (bfd_is_abs_section (sec)
5442 && type_ptr != NULL
5443 && type_ptr->internal_elf_sym.st_shndx != 0)
5445 /* This symbol is in a real ELF section which we did
5446 not create as a BFD section. Undo the mapping done
5447 by copy_private_symbol_data. */
5448 shndx = type_ptr->internal_elf_sym.st_shndx;
5449 switch (shndx)
5451 case MAP_ONESYMTAB:
5452 shndx = elf_onesymtab (abfd);
5453 break;
5454 case MAP_DYNSYMTAB:
5455 shndx = elf_dynsymtab (abfd);
5456 break;
5457 case MAP_STRTAB:
5458 shndx = elf_tdata (abfd)->strtab_section;
5459 break;
5460 case MAP_SHSTRTAB:
5461 shndx = elf_tdata (abfd)->shstrtab_section;
5462 break;
5463 case MAP_SYM_SHNDX:
5464 shndx = elf_tdata (abfd)->symtab_shndx_section;
5465 break;
5466 default:
5467 break;
5470 else
5472 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
5474 if (shndx == -1)
5476 asection *sec2;
5478 /* Writing this would be a hell of a lot easier if
5479 we had some decent documentation on bfd, and
5480 knew what to expect of the library, and what to
5481 demand of applications. For example, it
5482 appears that `objcopy' might not set the
5483 section of a symbol to be a section that is
5484 actually in the output file. */
5485 sec2 = bfd_get_section_by_name (abfd, sec->name);
5486 if (sec2 == NULL)
5488 _bfd_error_handler (_("\
5489 Unable to find equivalent output section for symbol '%s' from section '%s'"),
5490 syms[idx]->name ? syms[idx]->name : "<Local sym>",
5491 sec->name);
5492 bfd_set_error (bfd_error_invalid_operation);
5493 _bfd_stringtab_free (stt);
5494 return FALSE;
5497 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
5498 BFD_ASSERT (shndx != -1);
5502 sym.st_shndx = shndx;
5505 if ((flags & BSF_THREAD_LOCAL) != 0)
5506 type = STT_TLS;
5507 else if ((flags & BSF_FUNCTION) != 0)
5508 type = STT_FUNC;
5509 else if ((flags & BSF_OBJECT) != 0)
5510 type = STT_OBJECT;
5511 else
5512 type = STT_NOTYPE;
5514 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
5515 type = STT_TLS;
5517 /* Processor-specific types. */
5518 if (type_ptr != NULL
5519 && bed->elf_backend_get_symbol_type)
5520 type = ((*bed->elf_backend_get_symbol_type)
5521 (&type_ptr->internal_elf_sym, type));
5523 if (flags & BSF_SECTION_SYM)
5525 if (flags & BSF_GLOBAL)
5526 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5527 else
5528 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5530 else if (bfd_is_com_section (syms[idx]->section))
5531 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
5532 else if (bfd_is_und_section (syms[idx]->section))
5533 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
5534 ? STB_WEAK
5535 : STB_GLOBAL),
5536 type);
5537 else if (flags & BSF_FILE)
5538 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5539 else
5541 int bind = STB_LOCAL;
5543 if (flags & BSF_LOCAL)
5544 bind = STB_LOCAL;
5545 else if (flags & BSF_WEAK)
5546 bind = STB_WEAK;
5547 else if (flags & BSF_GLOBAL)
5548 bind = STB_GLOBAL;
5550 sym.st_info = ELF_ST_INFO (bind, type);
5553 if (type_ptr != NULL)
5554 sym.st_other = type_ptr->internal_elf_sym.st_other;
5555 else
5556 sym.st_other = 0;
5558 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5559 outbound_syms += bed->s->sizeof_sym;
5560 if (outbound_shndx != NULL)
5561 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5564 *sttp = stt;
5565 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
5566 symstrtab_hdr->sh_type = SHT_STRTAB;
5568 symstrtab_hdr->sh_flags = 0;
5569 symstrtab_hdr->sh_addr = 0;
5570 symstrtab_hdr->sh_entsize = 0;
5571 symstrtab_hdr->sh_link = 0;
5572 symstrtab_hdr->sh_info = 0;
5573 symstrtab_hdr->sh_addralign = 1;
5575 return TRUE;
5578 /* Return the number of bytes required to hold the symtab vector.
5580 Note that we base it on the count plus 1, since we will null terminate
5581 the vector allocated based on this size. However, the ELF symbol table
5582 always has a dummy entry as symbol #0, so it ends up even. */
5584 long
5585 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
5587 long symcount;
5588 long symtab_size;
5589 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
5591 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5592 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5593 if (symcount > 0)
5594 symtab_size -= sizeof (asymbol *);
5596 return symtab_size;
5599 long
5600 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
5602 long symcount;
5603 long symtab_size;
5604 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
5606 if (elf_dynsymtab (abfd) == 0)
5608 bfd_set_error (bfd_error_invalid_operation);
5609 return -1;
5612 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5613 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5614 if (symcount > 0)
5615 symtab_size -= sizeof (asymbol *);
5617 return symtab_size;
5620 long
5621 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
5622 sec_ptr asect)
5624 return (asect->reloc_count + 1) * sizeof (arelent *);
5627 /* Canonicalize the relocs. */
5629 long
5630 _bfd_elf_canonicalize_reloc (bfd *abfd,
5631 sec_ptr section,
5632 arelent **relptr,
5633 asymbol **symbols)
5635 arelent *tblptr;
5636 unsigned int i;
5637 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5639 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
5640 return -1;
5642 tblptr = section->relocation;
5643 for (i = 0; i < section->reloc_count; i++)
5644 *relptr++ = tblptr++;
5646 *relptr = NULL;
5648 return section->reloc_count;
5651 long
5652 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
5654 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5655 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
5657 if (symcount >= 0)
5658 bfd_get_symcount (abfd) = symcount;
5659 return symcount;
5662 long
5663 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
5664 asymbol **allocation)
5666 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5667 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
5669 if (symcount >= 0)
5670 bfd_get_dynamic_symcount (abfd) = symcount;
5671 return symcount;
5674 /* Return the size required for the dynamic reloc entries. Any
5675 section that was actually installed in the BFD, and has type
5676 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5677 considered to be a dynamic reloc section. */
5679 long
5680 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
5682 long ret;
5683 asection *s;
5685 if (elf_dynsymtab (abfd) == 0)
5687 bfd_set_error (bfd_error_invalid_operation);
5688 return -1;
5691 ret = sizeof (arelent *);
5692 for (s = abfd->sections; s != NULL; s = s->next)
5693 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5694 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5695 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5696 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
5697 * sizeof (arelent *));
5699 return ret;
5702 /* Canonicalize the dynamic relocation entries. Note that we return
5703 the dynamic relocations as a single block, although they are
5704 actually associated with particular sections; the interface, which
5705 was designed for SunOS style shared libraries, expects that there
5706 is only one set of dynamic relocs. Any section that was actually
5707 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5708 the dynamic symbol table, is considered to be a dynamic reloc
5709 section. */
5711 long
5712 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
5713 arelent **storage,
5714 asymbol **syms)
5716 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
5717 asection *s;
5718 long ret;
5720 if (elf_dynsymtab (abfd) == 0)
5722 bfd_set_error (bfd_error_invalid_operation);
5723 return -1;
5726 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
5727 ret = 0;
5728 for (s = abfd->sections; s != NULL; s = s->next)
5730 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5731 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5732 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5734 arelent *p;
5735 long count, i;
5737 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
5738 return -1;
5739 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
5740 p = s->relocation;
5741 for (i = 0; i < count; i++)
5742 *storage++ = p++;
5743 ret += count;
5747 *storage = NULL;
5749 return ret;
5752 /* Read in the version information. */
5754 bfd_boolean
5755 _bfd_elf_slurp_version_tables (bfd *abfd)
5757 bfd_byte *contents = NULL;
5758 bfd_size_type amt;
5760 if (elf_dynverdef (abfd) != 0)
5762 Elf_Internal_Shdr *hdr;
5763 Elf_External_Verdef *everdef;
5764 Elf_Internal_Verdef *iverdef;
5765 Elf_Internal_Verdef *iverdefarr;
5766 Elf_Internal_Verdef iverdefmem;
5767 unsigned int i;
5768 unsigned int maxidx;
5770 hdr = &elf_tdata (abfd)->dynverdef_hdr;
5772 contents = bfd_malloc (hdr->sh_size);
5773 if (contents == NULL)
5774 goto error_return;
5775 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5776 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
5777 goto error_return;
5779 /* We know the number of entries in the section but not the maximum
5780 index. Therefore we have to run through all entries and find
5781 the maximum. */
5782 everdef = (Elf_External_Verdef *) contents;
5783 maxidx = 0;
5784 for (i = 0; i < hdr->sh_info; ++i)
5786 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5788 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
5789 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
5791 everdef = ((Elf_External_Verdef *)
5792 ((bfd_byte *) everdef + iverdefmem.vd_next));
5795 amt = (bfd_size_type) maxidx * sizeof (Elf_Internal_Verdef);
5796 elf_tdata (abfd)->verdef = bfd_zalloc (abfd, amt);
5797 if (elf_tdata (abfd)->verdef == NULL)
5798 goto error_return;
5800 elf_tdata (abfd)->cverdefs = maxidx;
5802 everdef = (Elf_External_Verdef *) contents;
5803 iverdefarr = elf_tdata (abfd)->verdef;
5804 for (i = 0; i < hdr->sh_info; i++)
5806 Elf_External_Verdaux *everdaux;
5807 Elf_Internal_Verdaux *iverdaux;
5808 unsigned int j;
5810 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5812 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
5813 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
5815 iverdef->vd_bfd = abfd;
5817 amt = (bfd_size_type) iverdef->vd_cnt * sizeof (Elf_Internal_Verdaux);
5818 iverdef->vd_auxptr = bfd_alloc (abfd, amt);
5819 if (iverdef->vd_auxptr == NULL)
5820 goto error_return;
5822 everdaux = ((Elf_External_Verdaux *)
5823 ((bfd_byte *) everdef + iverdef->vd_aux));
5824 iverdaux = iverdef->vd_auxptr;
5825 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
5827 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
5829 iverdaux->vda_nodename =
5830 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5831 iverdaux->vda_name);
5832 if (iverdaux->vda_nodename == NULL)
5833 goto error_return;
5835 if (j + 1 < iverdef->vd_cnt)
5836 iverdaux->vda_nextptr = iverdaux + 1;
5837 else
5838 iverdaux->vda_nextptr = NULL;
5840 everdaux = ((Elf_External_Verdaux *)
5841 ((bfd_byte *) everdaux + iverdaux->vda_next));
5844 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
5846 if (i + 1 < hdr->sh_info)
5847 iverdef->vd_nextdef = iverdef + 1;
5848 else
5849 iverdef->vd_nextdef = NULL;
5851 everdef = ((Elf_External_Verdef *)
5852 ((bfd_byte *) everdef + iverdef->vd_next));
5855 free (contents);
5856 contents = NULL;
5859 if (elf_dynverref (abfd) != 0)
5861 Elf_Internal_Shdr *hdr;
5862 Elf_External_Verneed *everneed;
5863 Elf_Internal_Verneed *iverneed;
5864 unsigned int i;
5866 hdr = &elf_tdata (abfd)->dynverref_hdr;
5868 amt = (bfd_size_type) hdr->sh_info * sizeof (Elf_Internal_Verneed);
5869 elf_tdata (abfd)->verref = bfd_zalloc (abfd, amt);
5870 if (elf_tdata (abfd)->verref == NULL)
5871 goto error_return;
5873 elf_tdata (abfd)->cverrefs = hdr->sh_info;
5875 contents = bfd_malloc (hdr->sh_size);
5876 if (contents == NULL)
5877 goto error_return;
5878 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5879 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
5880 goto error_return;
5882 everneed = (Elf_External_Verneed *) contents;
5883 iverneed = elf_tdata (abfd)->verref;
5884 for (i = 0; i < hdr->sh_info; i++, iverneed++)
5886 Elf_External_Vernaux *evernaux;
5887 Elf_Internal_Vernaux *ivernaux;
5888 unsigned int j;
5890 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
5892 iverneed->vn_bfd = abfd;
5894 iverneed->vn_filename =
5895 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5896 iverneed->vn_file);
5897 if (iverneed->vn_filename == NULL)
5898 goto error_return;
5900 amt = iverneed->vn_cnt;
5901 amt *= sizeof (Elf_Internal_Vernaux);
5902 iverneed->vn_auxptr = bfd_alloc (abfd, amt);
5904 evernaux = ((Elf_External_Vernaux *)
5905 ((bfd_byte *) everneed + iverneed->vn_aux));
5906 ivernaux = iverneed->vn_auxptr;
5907 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
5909 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
5911 ivernaux->vna_nodename =
5912 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5913 ivernaux->vna_name);
5914 if (ivernaux->vna_nodename == NULL)
5915 goto error_return;
5917 if (j + 1 < iverneed->vn_cnt)
5918 ivernaux->vna_nextptr = ivernaux + 1;
5919 else
5920 ivernaux->vna_nextptr = NULL;
5922 evernaux = ((Elf_External_Vernaux *)
5923 ((bfd_byte *) evernaux + ivernaux->vna_next));
5926 if (i + 1 < hdr->sh_info)
5927 iverneed->vn_nextref = iverneed + 1;
5928 else
5929 iverneed->vn_nextref = NULL;
5931 everneed = ((Elf_External_Verneed *)
5932 ((bfd_byte *) everneed + iverneed->vn_next));
5935 free (contents);
5936 contents = NULL;
5939 return TRUE;
5941 error_return:
5942 if (contents != NULL)
5943 free (contents);
5944 return FALSE;
5947 asymbol *
5948 _bfd_elf_make_empty_symbol (bfd *abfd)
5950 elf_symbol_type *newsym;
5951 bfd_size_type amt = sizeof (elf_symbol_type);
5953 newsym = bfd_zalloc (abfd, amt);
5954 if (!newsym)
5955 return NULL;
5956 else
5958 newsym->symbol.the_bfd = abfd;
5959 return &newsym->symbol;
5963 void
5964 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
5965 asymbol *symbol,
5966 symbol_info *ret)
5968 bfd_symbol_info (symbol, ret);
5971 /* Return whether a symbol name implies a local symbol. Most targets
5972 use this function for the is_local_label_name entry point, but some
5973 override it. */
5975 bfd_boolean
5976 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
5977 const char *name)
5979 /* Normal local symbols start with ``.L''. */
5980 if (name[0] == '.' && name[1] == 'L')
5981 return TRUE;
5983 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5984 DWARF debugging symbols starting with ``..''. */
5985 if (name[0] == '.' && name[1] == '.')
5986 return TRUE;
5988 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5989 emitting DWARF debugging output. I suspect this is actually a
5990 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5991 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5992 underscore to be emitted on some ELF targets). For ease of use,
5993 we treat such symbols as local. */
5994 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
5995 return TRUE;
5997 return FALSE;
6000 alent *
6001 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
6002 asymbol *symbol ATTRIBUTE_UNUSED)
6004 abort ();
6005 return NULL;
6008 bfd_boolean
6009 _bfd_elf_set_arch_mach (bfd *abfd,
6010 enum bfd_architecture arch,
6011 unsigned long machine)
6013 /* If this isn't the right architecture for this backend, and this
6014 isn't the generic backend, fail. */
6015 if (arch != get_elf_backend_data (abfd)->arch
6016 && arch != bfd_arch_unknown
6017 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
6018 return FALSE;
6020 return bfd_default_set_arch_mach (abfd, arch, machine);
6023 /* Find the function to a particular section and offset,
6024 for error reporting. */
6026 static bfd_boolean
6027 elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
6028 asection *section,
6029 asymbol **symbols,
6030 bfd_vma offset,
6031 const char **filename_ptr,
6032 const char **functionname_ptr)
6034 const char *filename;
6035 asymbol *func;
6036 bfd_vma low_func;
6037 asymbol **p;
6039 filename = NULL;
6040 func = NULL;
6041 low_func = 0;
6043 for (p = symbols; *p != NULL; p++)
6045 elf_symbol_type *q;
6047 q = (elf_symbol_type *) *p;
6049 if (bfd_get_section (&q->symbol) != section)
6050 continue;
6052 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
6054 default:
6055 break;
6056 case STT_FILE:
6057 filename = bfd_asymbol_name (&q->symbol);
6058 break;
6059 case STT_NOTYPE:
6060 case STT_FUNC:
6061 if (q->symbol.section == section
6062 && q->symbol.value >= low_func
6063 && q->symbol.value <= offset)
6065 func = (asymbol *) q;
6066 low_func = q->symbol.value;
6068 break;
6072 if (func == NULL)
6073 return FALSE;
6075 if (filename_ptr)
6076 *filename_ptr = filename;
6077 if (functionname_ptr)
6078 *functionname_ptr = bfd_asymbol_name (func);
6080 return TRUE;
6083 /* Find the nearest line to a particular section and offset,
6084 for error reporting. */
6086 bfd_boolean
6087 _bfd_elf_find_nearest_line (bfd *abfd,
6088 asection *section,
6089 asymbol **symbols,
6090 bfd_vma offset,
6091 const char **filename_ptr,
6092 const char **functionname_ptr,
6093 unsigned int *line_ptr)
6095 bfd_boolean found;
6097 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
6098 filename_ptr, functionname_ptr,
6099 line_ptr))
6101 if (!*functionname_ptr)
6102 elf_find_function (abfd, section, symbols, offset,
6103 *filename_ptr ? NULL : filename_ptr,
6104 functionname_ptr);
6106 return TRUE;
6109 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
6110 filename_ptr, functionname_ptr,
6111 line_ptr, 0,
6112 &elf_tdata (abfd)->dwarf2_find_line_info))
6114 if (!*functionname_ptr)
6115 elf_find_function (abfd, section, symbols, offset,
6116 *filename_ptr ? NULL : filename_ptr,
6117 functionname_ptr);
6119 return TRUE;
6122 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
6123 &found, filename_ptr,
6124 functionname_ptr, line_ptr,
6125 &elf_tdata (abfd)->line_info))
6126 return FALSE;
6127 if (found && (*functionname_ptr || *line_ptr))
6128 return TRUE;
6130 if (symbols == NULL)
6131 return FALSE;
6133 if (! elf_find_function (abfd, section, symbols, offset,
6134 filename_ptr, functionname_ptr))
6135 return FALSE;
6137 *line_ptr = 0;
6138 return TRUE;
6142 _bfd_elf_sizeof_headers (bfd *abfd, bfd_boolean reloc)
6144 int ret;
6146 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
6147 if (! reloc)
6148 ret += get_program_header_size (abfd);
6149 return ret;
6152 bfd_boolean
6153 _bfd_elf_set_section_contents (bfd *abfd,
6154 sec_ptr section,
6155 const void *location,
6156 file_ptr offset,
6157 bfd_size_type count)
6159 Elf_Internal_Shdr *hdr;
6160 bfd_signed_vma pos;
6162 if (! abfd->output_has_begun
6163 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
6164 return FALSE;
6166 hdr = &elf_section_data (section)->this_hdr;
6167 pos = hdr->sh_offset + offset;
6168 if (bfd_seek (abfd, pos, SEEK_SET) != 0
6169 || bfd_bwrite (location, count, abfd) != count)
6170 return FALSE;
6172 return TRUE;
6175 void
6176 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
6177 arelent *cache_ptr ATTRIBUTE_UNUSED,
6178 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
6180 abort ();
6183 /* Try to convert a non-ELF reloc into an ELF one. */
6185 bfd_boolean
6186 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
6188 /* Check whether we really have an ELF howto. */
6190 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
6192 bfd_reloc_code_real_type code;
6193 reloc_howto_type *howto;
6195 /* Alien reloc: Try to determine its type to replace it with an
6196 equivalent ELF reloc. */
6198 if (areloc->howto->pc_relative)
6200 switch (areloc->howto->bitsize)
6202 case 8:
6203 code = BFD_RELOC_8_PCREL;
6204 break;
6205 case 12:
6206 code = BFD_RELOC_12_PCREL;
6207 break;
6208 case 16:
6209 code = BFD_RELOC_16_PCREL;
6210 break;
6211 case 24:
6212 code = BFD_RELOC_24_PCREL;
6213 break;
6214 case 32:
6215 code = BFD_RELOC_32_PCREL;
6216 break;
6217 case 64:
6218 code = BFD_RELOC_64_PCREL;
6219 break;
6220 default:
6221 goto fail;
6224 howto = bfd_reloc_type_lookup (abfd, code);
6226 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
6228 if (howto->pcrel_offset)
6229 areloc->addend += areloc->address;
6230 else
6231 areloc->addend -= areloc->address; /* addend is unsigned!! */
6234 else
6236 switch (areloc->howto->bitsize)
6238 case 8:
6239 code = BFD_RELOC_8;
6240 break;
6241 case 14:
6242 code = BFD_RELOC_14;
6243 break;
6244 case 16:
6245 code = BFD_RELOC_16;
6246 break;
6247 case 26:
6248 code = BFD_RELOC_26;
6249 break;
6250 case 32:
6251 code = BFD_RELOC_32;
6252 break;
6253 case 64:
6254 code = BFD_RELOC_64;
6255 break;
6256 default:
6257 goto fail;
6260 howto = bfd_reloc_type_lookup (abfd, code);
6263 if (howto)
6264 areloc->howto = howto;
6265 else
6266 goto fail;
6269 return TRUE;
6271 fail:
6272 (*_bfd_error_handler)
6273 (_("%s: unsupported relocation type %s"),
6274 bfd_archive_filename (abfd), areloc->howto->name);
6275 bfd_set_error (bfd_error_bad_value);
6276 return FALSE;
6279 bfd_boolean
6280 _bfd_elf_close_and_cleanup (bfd *abfd)
6282 if (bfd_get_format (abfd) == bfd_object)
6284 if (elf_shstrtab (abfd) != NULL)
6285 _bfd_elf_strtab_free (elf_shstrtab (abfd));
6288 return _bfd_generic_close_and_cleanup (abfd);
6291 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
6292 in the relocation's offset. Thus we cannot allow any sort of sanity
6293 range-checking to interfere. There is nothing else to do in processing
6294 this reloc. */
6296 bfd_reloc_status_type
6297 _bfd_elf_rel_vtable_reloc_fn
6298 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
6299 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
6300 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
6301 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
6303 return bfd_reloc_ok;
6306 /* Elf core file support. Much of this only works on native
6307 toolchains, since we rely on knowing the
6308 machine-dependent procfs structure in order to pick
6309 out details about the corefile. */
6311 #ifdef HAVE_SYS_PROCFS_H
6312 # include <sys/procfs.h>
6313 #endif
6315 /* FIXME: this is kinda wrong, but it's what gdb wants. */
6317 static int
6318 elfcore_make_pid (bfd *abfd)
6320 return ((elf_tdata (abfd)->core_lwpid << 16)
6321 + (elf_tdata (abfd)->core_pid));
6324 /* If there isn't a section called NAME, make one, using
6325 data from SECT. Note, this function will generate a
6326 reference to NAME, so you shouldn't deallocate or
6327 overwrite it. */
6329 static bfd_boolean
6330 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
6332 asection *sect2;
6334 if (bfd_get_section_by_name (abfd, name) != NULL)
6335 return TRUE;
6337 sect2 = bfd_make_section (abfd, name);
6338 if (sect2 == NULL)
6339 return FALSE;
6341 sect2->_raw_size = sect->_raw_size;
6342 sect2->filepos = sect->filepos;
6343 sect2->flags = sect->flags;
6344 sect2->alignment_power = sect->alignment_power;
6345 return TRUE;
6348 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
6349 actually creates up to two pseudosections:
6350 - For the single-threaded case, a section named NAME, unless
6351 such a section already exists.
6352 - For the multi-threaded case, a section named "NAME/PID", where
6353 PID is elfcore_make_pid (abfd).
6354 Both pseudosections have identical contents. */
6355 bfd_boolean
6356 _bfd_elfcore_make_pseudosection (bfd *abfd,
6357 char *name,
6358 size_t size,
6359 ufile_ptr filepos)
6361 char buf[100];
6362 char *threaded_name;
6363 size_t len;
6364 asection *sect;
6366 /* Build the section name. */
6368 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
6369 len = strlen (buf) + 1;
6370 threaded_name = bfd_alloc (abfd, len);
6371 if (threaded_name == NULL)
6372 return FALSE;
6373 memcpy (threaded_name, buf, len);
6375 sect = bfd_make_section_anyway (abfd, threaded_name);
6376 if (sect == NULL)
6377 return FALSE;
6378 sect->_raw_size = size;
6379 sect->filepos = filepos;
6380 sect->flags = SEC_HAS_CONTENTS;
6381 sect->alignment_power = 2;
6383 return elfcore_maybe_make_sect (abfd, name, sect);
6386 /* prstatus_t exists on:
6387 solaris 2.5+
6388 linux 2.[01] + glibc
6389 unixware 4.2
6392 #if defined (HAVE_PRSTATUS_T)
6394 static bfd_boolean
6395 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
6397 size_t raw_size;
6398 int offset;
6400 if (note->descsz == sizeof (prstatus_t))
6402 prstatus_t prstat;
6404 raw_size = sizeof (prstat.pr_reg);
6405 offset = offsetof (prstatus_t, pr_reg);
6406 memcpy (&prstat, note->descdata, sizeof (prstat));
6408 /* Do not overwrite the core signal if it
6409 has already been set by another thread. */
6410 if (elf_tdata (abfd)->core_signal == 0)
6411 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
6412 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6414 /* pr_who exists on:
6415 solaris 2.5+
6416 unixware 4.2
6417 pr_who doesn't exist on:
6418 linux 2.[01]
6420 #if defined (HAVE_PRSTATUS_T_PR_WHO)
6421 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6422 #endif
6424 #if defined (HAVE_PRSTATUS32_T)
6425 else if (note->descsz == sizeof (prstatus32_t))
6427 /* 64-bit host, 32-bit corefile */
6428 prstatus32_t prstat;
6430 raw_size = sizeof (prstat.pr_reg);
6431 offset = offsetof (prstatus32_t, pr_reg);
6432 memcpy (&prstat, note->descdata, sizeof (prstat));
6434 /* Do not overwrite the core signal if it
6435 has already been set by another thread. */
6436 if (elf_tdata (abfd)->core_signal == 0)
6437 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
6438 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6440 /* pr_who exists on:
6441 solaris 2.5+
6442 unixware 4.2
6443 pr_who doesn't exist on:
6444 linux 2.[01]
6446 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
6447 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6448 #endif
6450 #endif /* HAVE_PRSTATUS32_T */
6451 else
6453 /* Fail - we don't know how to handle any other
6454 note size (ie. data object type). */
6455 return TRUE;
6458 /* Make a ".reg/999" section and a ".reg" section. */
6459 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
6460 raw_size, note->descpos + offset);
6462 #endif /* defined (HAVE_PRSTATUS_T) */
6464 /* Create a pseudosection containing the exact contents of NOTE. */
6465 static bfd_boolean
6466 elfcore_make_note_pseudosection (bfd *abfd,
6467 char *name,
6468 Elf_Internal_Note *note)
6470 return _bfd_elfcore_make_pseudosection (abfd, name,
6471 note->descsz, note->descpos);
6474 /* There isn't a consistent prfpregset_t across platforms,
6475 but it doesn't matter, because we don't have to pick this
6476 data structure apart. */
6478 static bfd_boolean
6479 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
6481 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6484 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6485 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6486 literally. */
6488 static bfd_boolean
6489 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
6491 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
6494 #if defined (HAVE_PRPSINFO_T)
6495 typedef prpsinfo_t elfcore_psinfo_t;
6496 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6497 typedef prpsinfo32_t elfcore_psinfo32_t;
6498 #endif
6499 #endif
6501 #if defined (HAVE_PSINFO_T)
6502 typedef psinfo_t elfcore_psinfo_t;
6503 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6504 typedef psinfo32_t elfcore_psinfo32_t;
6505 #endif
6506 #endif
6508 /* return a malloc'ed copy of a string at START which is at
6509 most MAX bytes long, possibly without a terminating '\0'.
6510 the copy will always have a terminating '\0'. */
6512 char *
6513 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
6515 char *dups;
6516 char *end = memchr (start, '\0', max);
6517 size_t len;
6519 if (end == NULL)
6520 len = max;
6521 else
6522 len = end - start;
6524 dups = bfd_alloc (abfd, len + 1);
6525 if (dups == NULL)
6526 return NULL;
6528 memcpy (dups, start, len);
6529 dups[len] = '\0';
6531 return dups;
6534 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6535 static bfd_boolean
6536 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
6538 if (note->descsz == sizeof (elfcore_psinfo_t))
6540 elfcore_psinfo_t psinfo;
6542 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6544 elf_tdata (abfd)->core_program
6545 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6546 sizeof (psinfo.pr_fname));
6548 elf_tdata (abfd)->core_command
6549 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6550 sizeof (psinfo.pr_psargs));
6552 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6553 else if (note->descsz == sizeof (elfcore_psinfo32_t))
6555 /* 64-bit host, 32-bit corefile */
6556 elfcore_psinfo32_t psinfo;
6558 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6560 elf_tdata (abfd)->core_program
6561 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6562 sizeof (psinfo.pr_fname));
6564 elf_tdata (abfd)->core_command
6565 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6566 sizeof (psinfo.pr_psargs));
6568 #endif
6570 else
6572 /* Fail - we don't know how to handle any other
6573 note size (ie. data object type). */
6574 return TRUE;
6577 /* Note that for some reason, a spurious space is tacked
6578 onto the end of the args in some (at least one anyway)
6579 implementations, so strip it off if it exists. */
6582 char *command = elf_tdata (abfd)->core_command;
6583 int n = strlen (command);
6585 if (0 < n && command[n - 1] == ' ')
6586 command[n - 1] = '\0';
6589 return TRUE;
6591 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6593 #if defined (HAVE_PSTATUS_T)
6594 static bfd_boolean
6595 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
6597 if (note->descsz == sizeof (pstatus_t)
6598 #if defined (HAVE_PXSTATUS_T)
6599 || note->descsz == sizeof (pxstatus_t)
6600 #endif
6603 pstatus_t pstat;
6605 memcpy (&pstat, note->descdata, sizeof (pstat));
6607 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6609 #if defined (HAVE_PSTATUS32_T)
6610 else if (note->descsz == sizeof (pstatus32_t))
6612 /* 64-bit host, 32-bit corefile */
6613 pstatus32_t pstat;
6615 memcpy (&pstat, note->descdata, sizeof (pstat));
6617 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6619 #endif
6620 /* Could grab some more details from the "representative"
6621 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6622 NT_LWPSTATUS note, presumably. */
6624 return TRUE;
6626 #endif /* defined (HAVE_PSTATUS_T) */
6628 #if defined (HAVE_LWPSTATUS_T)
6629 static bfd_boolean
6630 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
6632 lwpstatus_t lwpstat;
6633 char buf[100];
6634 char *name;
6635 size_t len;
6636 asection *sect;
6638 if (note->descsz != sizeof (lwpstat)
6639 #if defined (HAVE_LWPXSTATUS_T)
6640 && note->descsz != sizeof (lwpxstatus_t)
6641 #endif
6643 return TRUE;
6645 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
6647 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
6648 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
6650 /* Make a ".reg/999" section. */
6652 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
6653 len = strlen (buf) + 1;
6654 name = bfd_alloc (abfd, len);
6655 if (name == NULL)
6656 return FALSE;
6657 memcpy (name, buf, len);
6659 sect = bfd_make_section_anyway (abfd, name);
6660 if (sect == NULL)
6661 return FALSE;
6663 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6664 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
6665 sect->filepos = note->descpos
6666 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
6667 #endif
6669 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6670 sect->_raw_size = sizeof (lwpstat.pr_reg);
6671 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
6672 #endif
6674 sect->flags = SEC_HAS_CONTENTS;
6675 sect->alignment_power = 2;
6677 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
6678 return FALSE;
6680 /* Make a ".reg2/999" section */
6682 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
6683 len = strlen (buf) + 1;
6684 name = bfd_alloc (abfd, len);
6685 if (name == NULL)
6686 return FALSE;
6687 memcpy (name, buf, len);
6689 sect = bfd_make_section_anyway (abfd, name);
6690 if (sect == NULL)
6691 return FALSE;
6693 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6694 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
6695 sect->filepos = note->descpos
6696 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
6697 #endif
6699 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6700 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
6701 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
6702 #endif
6704 sect->flags = SEC_HAS_CONTENTS;
6705 sect->alignment_power = 2;
6707 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
6709 #endif /* defined (HAVE_LWPSTATUS_T) */
6711 #if defined (HAVE_WIN32_PSTATUS_T)
6712 static bfd_boolean
6713 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
6715 char buf[30];
6716 char *name;
6717 size_t len;
6718 asection *sect;
6719 win32_pstatus_t pstatus;
6721 if (note->descsz < sizeof (pstatus))
6722 return TRUE;
6724 memcpy (&pstatus, note->descdata, sizeof (pstatus));
6726 switch (pstatus.data_type)
6728 case NOTE_INFO_PROCESS:
6729 /* FIXME: need to add ->core_command. */
6730 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
6731 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
6732 break;
6734 case NOTE_INFO_THREAD:
6735 /* Make a ".reg/999" section. */
6736 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
6738 len = strlen (buf) + 1;
6739 name = bfd_alloc (abfd, len);
6740 if (name == NULL)
6741 return FALSE;
6743 memcpy (name, buf, len);
6745 sect = bfd_make_section_anyway (abfd, name);
6746 if (sect == NULL)
6747 return FALSE;
6749 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
6750 sect->filepos = (note->descpos
6751 + offsetof (struct win32_pstatus,
6752 data.thread_info.thread_context));
6753 sect->flags = SEC_HAS_CONTENTS;
6754 sect->alignment_power = 2;
6756 if (pstatus.data.thread_info.is_active_thread)
6757 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
6758 return FALSE;
6759 break;
6761 case NOTE_INFO_MODULE:
6762 /* Make a ".module/xxxxxxxx" section. */
6763 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
6765 len = strlen (buf) + 1;
6766 name = bfd_alloc (abfd, len);
6767 if (name == NULL)
6768 return FALSE;
6770 memcpy (name, buf, len);
6772 sect = bfd_make_section_anyway (abfd, name);
6774 if (sect == NULL)
6775 return FALSE;
6777 sect->_raw_size = note->descsz;
6778 sect->filepos = note->descpos;
6779 sect->flags = SEC_HAS_CONTENTS;
6780 sect->alignment_power = 2;
6781 break;
6783 default:
6784 return TRUE;
6787 return TRUE;
6789 #endif /* HAVE_WIN32_PSTATUS_T */
6791 static bfd_boolean
6792 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
6794 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6796 switch (note->type)
6798 default:
6799 return TRUE;
6801 case NT_PRSTATUS:
6802 if (bed->elf_backend_grok_prstatus)
6803 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
6804 return TRUE;
6805 #if defined (HAVE_PRSTATUS_T)
6806 return elfcore_grok_prstatus (abfd, note);
6807 #else
6808 return TRUE;
6809 #endif
6811 #if defined (HAVE_PSTATUS_T)
6812 case NT_PSTATUS:
6813 return elfcore_grok_pstatus (abfd, note);
6814 #endif
6816 #if defined (HAVE_LWPSTATUS_T)
6817 case NT_LWPSTATUS:
6818 return elfcore_grok_lwpstatus (abfd, note);
6819 #endif
6821 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
6822 return elfcore_grok_prfpreg (abfd, note);
6824 #if defined (HAVE_WIN32_PSTATUS_T)
6825 case NT_WIN32PSTATUS:
6826 return elfcore_grok_win32pstatus (abfd, note);
6827 #endif
6829 case NT_PRXFPREG: /* Linux SSE extension */
6830 if (note->namesz == 6
6831 && strcmp (note->namedata, "LINUX") == 0)
6832 return elfcore_grok_prxfpreg (abfd, note);
6833 else
6834 return TRUE;
6836 case NT_PRPSINFO:
6837 case NT_PSINFO:
6838 if (bed->elf_backend_grok_psinfo)
6839 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
6840 return TRUE;
6841 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6842 return elfcore_grok_psinfo (abfd, note);
6843 #else
6844 return TRUE;
6845 #endif
6847 case NT_AUXV:
6849 asection *sect = bfd_make_section_anyway (abfd, ".auxv");
6851 if (sect == NULL)
6852 return FALSE;
6853 sect->_raw_size = note->descsz;
6854 sect->filepos = note->descpos;
6855 sect->flags = SEC_HAS_CONTENTS;
6856 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
6858 return TRUE;
6863 static bfd_boolean
6864 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
6866 char *cp;
6868 cp = strchr (note->namedata, '@');
6869 if (cp != NULL)
6871 *lwpidp = atoi(cp + 1);
6872 return TRUE;
6874 return FALSE;
6877 static bfd_boolean
6878 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
6881 /* Signal number at offset 0x08. */
6882 elf_tdata (abfd)->core_signal
6883 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
6885 /* Process ID at offset 0x50. */
6886 elf_tdata (abfd)->core_pid
6887 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
6889 /* Command name at 0x7c (max 32 bytes, including nul). */
6890 elf_tdata (abfd)->core_command
6891 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
6893 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
6894 note);
6897 static bfd_boolean
6898 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
6900 int lwp;
6902 if (elfcore_netbsd_get_lwpid (note, &lwp))
6903 elf_tdata (abfd)->core_lwpid = lwp;
6905 if (note->type == NT_NETBSDCORE_PROCINFO)
6907 /* NetBSD-specific core "procinfo". Note that we expect to
6908 find this note before any of the others, which is fine,
6909 since the kernel writes this note out first when it
6910 creates a core file. */
6912 return elfcore_grok_netbsd_procinfo (abfd, note);
6915 /* As of Jan 2002 there are no other machine-independent notes
6916 defined for NetBSD core files. If the note type is less
6917 than the start of the machine-dependent note types, we don't
6918 understand it. */
6920 if (note->type < NT_NETBSDCORE_FIRSTMACH)
6921 return TRUE;
6924 switch (bfd_get_arch (abfd))
6926 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
6927 PT_GETFPREGS == mach+2. */
6929 case bfd_arch_alpha:
6930 case bfd_arch_sparc:
6931 switch (note->type)
6933 case NT_NETBSDCORE_FIRSTMACH+0:
6934 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6936 case NT_NETBSDCORE_FIRSTMACH+2:
6937 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6939 default:
6940 return TRUE;
6943 /* On all other arch's, PT_GETREGS == mach+1 and
6944 PT_GETFPREGS == mach+3. */
6946 default:
6947 switch (note->type)
6949 case NT_NETBSDCORE_FIRSTMACH+1:
6950 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6952 case NT_NETBSDCORE_FIRSTMACH+3:
6953 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6955 default:
6956 return TRUE;
6959 /* NOTREACHED */
6962 static bfd_boolean
6963 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, pid_t *tid)
6965 void *ddata = note->descdata;
6966 char buf[100];
6967 char *name;
6968 asection *sect;
6969 short sig;
6970 unsigned flags;
6972 /* nto_procfs_status 'pid' field is at offset 0. */
6973 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
6975 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
6976 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
6978 /* nto_procfs_status 'flags' field is at offset 8. */
6979 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
6981 /* nto_procfs_status 'what' field is at offset 14. */
6982 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
6984 elf_tdata (abfd)->core_signal = sig;
6985 elf_tdata (abfd)->core_lwpid = *tid;
6988 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
6989 do not come from signals so we make sure we set the current
6990 thread just in case. */
6991 if (flags & 0x00000080)
6992 elf_tdata (abfd)->core_lwpid = *tid;
6994 /* Make a ".qnx_core_status/%d" section. */
6995 sprintf (buf, ".qnx_core_status/%d", *tid);
6997 name = bfd_alloc (abfd, strlen (buf) + 1);
6998 if (name == NULL)
6999 return FALSE;
7000 strcpy (name, buf);
7002 sect = bfd_make_section_anyway (abfd, name);
7003 if (sect == NULL)
7004 return FALSE;
7006 sect->_raw_size = note->descsz;
7007 sect->filepos = note->descpos;
7008 sect->flags = SEC_HAS_CONTENTS;
7009 sect->alignment_power = 2;
7011 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
7014 static bfd_boolean
7015 elfcore_grok_nto_gregs (bfd *abfd, Elf_Internal_Note *note, pid_t tid)
7017 char buf[100];
7018 char *name;
7019 asection *sect;
7021 /* Make a ".reg/%d" section. */
7022 sprintf (buf, ".reg/%d", tid);
7024 name = bfd_alloc (abfd, strlen (buf) + 1);
7025 if (name == NULL)
7026 return FALSE;
7027 strcpy (name, buf);
7029 sect = bfd_make_section_anyway (abfd, name);
7030 if (sect == NULL)
7031 return FALSE;
7033 sect->_raw_size = note->descsz;
7034 sect->filepos = note->descpos;
7035 sect->flags = SEC_HAS_CONTENTS;
7036 sect->alignment_power = 2;
7038 /* This is the current thread. */
7039 if (elf_tdata (abfd)->core_lwpid == tid)
7040 return elfcore_maybe_make_sect (abfd, ".reg", sect);
7042 return TRUE;
7045 #define BFD_QNT_CORE_INFO 7
7046 #define BFD_QNT_CORE_STATUS 8
7047 #define BFD_QNT_CORE_GREG 9
7048 #define BFD_QNT_CORE_FPREG 10
7050 static bfd_boolean
7051 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
7053 /* Every GREG section has a STATUS section before it. Store the
7054 tid from the previous call to pass down to the next gregs
7055 function. */
7056 static pid_t tid = 1;
7058 switch (note->type)
7060 case BFD_QNT_CORE_INFO: return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
7061 case BFD_QNT_CORE_STATUS: return elfcore_grok_nto_status (abfd, note, &tid);
7062 case BFD_QNT_CORE_GREG: return elfcore_grok_nto_gregs (abfd, note, tid);
7063 case BFD_QNT_CORE_FPREG: return elfcore_grok_prfpreg (abfd, note);
7064 default: return TRUE;
7068 /* Function: elfcore_write_note
7070 Inputs:
7071 buffer to hold note
7072 name of note
7073 type of note
7074 data for note
7075 size of data for note
7077 Return:
7078 End of buffer containing note. */
7080 char *
7081 elfcore_write_note (bfd *abfd,
7082 char *buf,
7083 int *bufsiz,
7084 const char *name,
7085 int type,
7086 const void *input,
7087 int size)
7089 Elf_External_Note *xnp;
7090 size_t namesz;
7091 size_t pad;
7092 size_t newspace;
7093 char *p, *dest;
7095 namesz = 0;
7096 pad = 0;
7097 if (name != NULL)
7099 const struct elf_backend_data *bed;
7101 namesz = strlen (name) + 1;
7102 bed = get_elf_backend_data (abfd);
7103 pad = -namesz & ((1 << bed->s->log_file_align) - 1);
7106 newspace = 12 + namesz + pad + size;
7108 p = realloc (buf, *bufsiz + newspace);
7109 dest = p + *bufsiz;
7110 *bufsiz += newspace;
7111 xnp = (Elf_External_Note *) dest;
7112 H_PUT_32 (abfd, namesz, xnp->namesz);
7113 H_PUT_32 (abfd, size, xnp->descsz);
7114 H_PUT_32 (abfd, type, xnp->type);
7115 dest = xnp->name;
7116 if (name != NULL)
7118 memcpy (dest, name, namesz);
7119 dest += namesz;
7120 while (pad != 0)
7122 *dest++ = '\0';
7123 --pad;
7126 memcpy (dest, input, size);
7127 return p;
7130 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7131 char *
7132 elfcore_write_prpsinfo (bfd *abfd,
7133 char *buf,
7134 int *bufsiz,
7135 const char *fname,
7136 const char *psargs)
7138 int note_type;
7139 char *note_name = "CORE";
7141 #if defined (HAVE_PSINFO_T)
7142 psinfo_t data;
7143 note_type = NT_PSINFO;
7144 #else
7145 prpsinfo_t data;
7146 note_type = NT_PRPSINFO;
7147 #endif
7149 memset (&data, 0, sizeof (data));
7150 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
7151 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
7152 return elfcore_write_note (abfd, buf, bufsiz,
7153 note_name, note_type, &data, sizeof (data));
7155 #endif /* PSINFO_T or PRPSINFO_T */
7157 #if defined (HAVE_PRSTATUS_T)
7158 char *
7159 elfcore_write_prstatus (bfd *abfd,
7160 char *buf,
7161 int *bufsiz,
7162 long pid,
7163 int cursig,
7164 const void *gregs)
7166 prstatus_t prstat;
7167 char *note_name = "CORE";
7169 memset (&prstat, 0, sizeof (prstat));
7170 prstat.pr_pid = pid;
7171 prstat.pr_cursig = cursig;
7172 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
7173 return elfcore_write_note (abfd, buf, bufsiz,
7174 note_name, NT_PRSTATUS, &prstat, sizeof (prstat));
7176 #endif /* HAVE_PRSTATUS_T */
7178 #if defined (HAVE_LWPSTATUS_T)
7179 char *
7180 elfcore_write_lwpstatus (bfd *abfd,
7181 char *buf,
7182 int *bufsiz,
7183 long pid,
7184 int cursig,
7185 const void *gregs)
7187 lwpstatus_t lwpstat;
7188 char *note_name = "CORE";
7190 memset (&lwpstat, 0, sizeof (lwpstat));
7191 lwpstat.pr_lwpid = pid >> 16;
7192 lwpstat.pr_cursig = cursig;
7193 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7194 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
7195 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7196 #if !defined(gregs)
7197 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
7198 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
7199 #else
7200 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
7201 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
7202 #endif
7203 #endif
7204 return elfcore_write_note (abfd, buf, bufsiz, note_name,
7205 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
7207 #endif /* HAVE_LWPSTATUS_T */
7209 #if defined (HAVE_PSTATUS_T)
7210 char *
7211 elfcore_write_pstatus (bfd *abfd,
7212 char *buf,
7213 int *bufsiz,
7214 long pid,
7215 int cursig,
7216 const void *gregs)
7218 pstatus_t pstat;
7219 char *note_name = "CORE";
7221 memset (&pstat, 0, sizeof (pstat));
7222 pstat.pr_pid = pid & 0xffff;
7223 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
7224 NT_PSTATUS, &pstat, sizeof (pstat));
7225 return buf;
7227 #endif /* HAVE_PSTATUS_T */
7229 char *
7230 elfcore_write_prfpreg (bfd *abfd,
7231 char *buf,
7232 int *bufsiz,
7233 const void *fpregs,
7234 int size)
7236 char *note_name = "CORE";
7237 return elfcore_write_note (abfd, buf, bufsiz,
7238 note_name, NT_FPREGSET, fpregs, size);
7241 char *
7242 elfcore_write_prxfpreg (bfd *abfd,
7243 char *buf,
7244 int *bufsiz,
7245 const void *xfpregs,
7246 int size)
7248 char *note_name = "LINUX";
7249 return elfcore_write_note (abfd, buf, bufsiz,
7250 note_name, NT_PRXFPREG, xfpregs, size);
7253 static bfd_boolean
7254 elfcore_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
7256 char *buf;
7257 char *p;
7259 if (size <= 0)
7260 return TRUE;
7262 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
7263 return FALSE;
7265 buf = bfd_malloc (size);
7266 if (buf == NULL)
7267 return FALSE;
7269 if (bfd_bread (buf, size, abfd) != size)
7271 error:
7272 free (buf);
7273 return FALSE;
7276 p = buf;
7277 while (p < buf + size)
7279 /* FIXME: bad alignment assumption. */
7280 Elf_External_Note *xnp = (Elf_External_Note *) p;
7281 Elf_Internal_Note in;
7283 in.type = H_GET_32 (abfd, xnp->type);
7285 in.namesz = H_GET_32 (abfd, xnp->namesz);
7286 in.namedata = xnp->name;
7288 in.descsz = H_GET_32 (abfd, xnp->descsz);
7289 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
7290 in.descpos = offset + (in.descdata - buf);
7292 if (strncmp (in.namedata, "NetBSD-CORE", 11) == 0)
7294 if (! elfcore_grok_netbsd_note (abfd, &in))
7295 goto error;
7297 else if (strncmp (in.namedata, "QNX", 3) == 0)
7299 if (! elfcore_grok_nto_note (abfd, &in))
7300 goto error;
7302 else
7304 if (! elfcore_grok_note (abfd, &in))
7305 goto error;
7308 p = in.descdata + BFD_ALIGN (in.descsz, 4);
7311 free (buf);
7312 return TRUE;
7315 /* Providing external access to the ELF program header table. */
7317 /* Return an upper bound on the number of bytes required to store a
7318 copy of ABFD's program header table entries. Return -1 if an error
7319 occurs; bfd_get_error will return an appropriate code. */
7321 long
7322 bfd_get_elf_phdr_upper_bound (bfd *abfd)
7324 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7326 bfd_set_error (bfd_error_wrong_format);
7327 return -1;
7330 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
7333 /* Copy ABFD's program header table entries to *PHDRS. The entries
7334 will be stored as an array of Elf_Internal_Phdr structures, as
7335 defined in include/elf/internal.h. To find out how large the
7336 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
7338 Return the number of program header table entries read, or -1 if an
7339 error occurs; bfd_get_error will return an appropriate code. */
7342 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
7344 int num_phdrs;
7346 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7348 bfd_set_error (bfd_error_wrong_format);
7349 return -1;
7352 num_phdrs = elf_elfheader (abfd)->e_phnum;
7353 memcpy (phdrs, elf_tdata (abfd)->phdr,
7354 num_phdrs * sizeof (Elf_Internal_Phdr));
7356 return num_phdrs;
7359 void
7360 _bfd_elf_sprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, char *buf, bfd_vma value)
7362 #ifdef BFD64
7363 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7365 i_ehdrp = elf_elfheader (abfd);
7366 if (i_ehdrp == NULL)
7367 sprintf_vma (buf, value);
7368 else
7370 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
7372 #if BFD_HOST_64BIT_LONG
7373 sprintf (buf, "%016lx", value);
7374 #else
7375 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
7376 _bfd_int64_low (value));
7377 #endif
7379 else
7380 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
7382 #else
7383 sprintf_vma (buf, value);
7384 #endif
7387 void
7388 _bfd_elf_fprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, void *stream, bfd_vma value)
7390 #ifdef BFD64
7391 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7393 i_ehdrp = elf_elfheader (abfd);
7394 if (i_ehdrp == NULL)
7395 fprintf_vma ((FILE *) stream, value);
7396 else
7398 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
7400 #if BFD_HOST_64BIT_LONG
7401 fprintf ((FILE *) stream, "%016lx", value);
7402 #else
7403 fprintf ((FILE *) stream, "%08lx%08lx",
7404 _bfd_int64_high (value), _bfd_int64_low (value));
7405 #endif
7407 else
7408 fprintf ((FILE *) stream, "%08lx",
7409 (unsigned long) (value & 0xffffffff));
7411 #else
7412 fprintf_vma ((FILE *) stream, value);
7413 #endif
7416 enum elf_reloc_type_class
7417 _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
7419 return reloc_class_normal;
7422 /* For RELA architectures, return the relocation value for a
7423 relocation against a local symbol. */
7425 bfd_vma
7426 _bfd_elf_rela_local_sym (bfd *abfd,
7427 Elf_Internal_Sym *sym,
7428 asection **psec,
7429 Elf_Internal_Rela *rel)
7431 asection *sec = *psec;
7432 bfd_vma relocation;
7434 relocation = (sec->output_section->vma
7435 + sec->output_offset
7436 + sym->st_value);
7437 if ((sec->flags & SEC_MERGE)
7438 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
7439 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
7441 rel->r_addend =
7442 _bfd_merged_section_offset (abfd, psec,
7443 elf_section_data (sec)->sec_info,
7444 sym->st_value + rel->r_addend,
7446 sec = *psec;
7447 rel->r_addend -= relocation;
7448 rel->r_addend += sec->output_section->vma + sec->output_offset;
7450 return relocation;
7453 bfd_vma
7454 _bfd_elf_rel_local_sym (bfd *abfd,
7455 Elf_Internal_Sym *sym,
7456 asection **psec,
7457 bfd_vma addend)
7459 asection *sec = *psec;
7461 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
7462 return sym->st_value + addend;
7464 return _bfd_merged_section_offset (abfd, psec,
7465 elf_section_data (sec)->sec_info,
7466 sym->st_value + addend, 0);
7469 bfd_vma
7470 _bfd_elf_section_offset (bfd *abfd,
7471 struct bfd_link_info *info,
7472 asection *sec,
7473 bfd_vma offset)
7475 struct bfd_elf_section_data *sec_data;
7477 sec_data = elf_section_data (sec);
7478 switch (sec->sec_info_type)
7480 case ELF_INFO_TYPE_STABS:
7481 return _bfd_stab_section_offset (abfd,
7482 &elf_hash_table (info)->merge_info,
7483 sec, &sec_data->sec_info, offset);
7484 case ELF_INFO_TYPE_EH_FRAME:
7485 return _bfd_elf_eh_frame_section_offset (abfd, sec, offset);
7486 default:
7487 return offset;
7491 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
7492 reconstruct an ELF file by reading the segments out of remote memory
7493 based on the ELF file header at EHDR_VMA and the ELF program headers it
7494 points to. If not null, *LOADBASEP is filled in with the difference
7495 between the VMAs from which the segments were read, and the VMAs the
7496 file headers (and hence BFD's idea of each section's VMA) put them at.
7498 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
7499 remote memory at target address VMA into the local buffer at MYADDR; it
7500 should return zero on success or an `errno' code on failure. TEMPL must
7501 be a BFD for an ELF target with the word size and byte order found in
7502 the remote memory. */
7504 bfd *
7505 bfd_elf_bfd_from_remote_memory
7506 (bfd *templ,
7507 bfd_vma ehdr_vma,
7508 bfd_vma *loadbasep,
7509 int (*target_read_memory) (bfd_vma, char *, int))
7511 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
7512 (templ, ehdr_vma, loadbasep, target_read_memory);