* elf32-arm.h (WILL_CALL_FINISH_DYNAMIC_SYMBOL): Define.
[binutils.git] / bfd / elf.c
blob33848a2b4dc802c4d8073c6d7fbd03525d9a5d3a
1 /* ELF executable support for BFD.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* SECTION
23 ELF backends
25 BFD support for ELF formats is being worked on.
26 Currently, the best supported back ends are for sparc and i386
27 (running svr4 or Solaris 2).
29 Documentation of the internals of the support code still needs
30 to be written. The code is changing quickly enough that we
31 haven't bothered yet. */
33 /* For sparc64-cross-sparc32. */
34 #define _SYSCALL32
35 #include "bfd.h"
36 #include "sysdep.h"
37 #include "bfdlink.h"
38 #include "libbfd.h"
39 #define ARCH_SIZE 0
40 #include "elf-bfd.h"
41 #include "libiberty.h"
43 static int elf_sort_sections (const void *, const void *);
44 static bfd_boolean assign_file_positions_except_relocs (bfd *);
45 static bfd_boolean prep_headers (bfd *);
46 static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ;
47 static bfd_boolean elfcore_read_notes (bfd *, file_ptr, bfd_size_type) ;
49 /* Swap version information in and out. The version information is
50 currently size independent. If that ever changes, this code will
51 need to move into elfcode.h. */
53 /* Swap in a Verdef structure. */
55 void
56 _bfd_elf_swap_verdef_in (bfd *abfd,
57 const Elf_External_Verdef *src,
58 Elf_Internal_Verdef *dst)
60 dst->vd_version = H_GET_16 (abfd, src->vd_version);
61 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
62 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
63 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
64 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
65 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
66 dst->vd_next = H_GET_32 (abfd, src->vd_next);
69 /* Swap out a Verdef structure. */
71 void
72 _bfd_elf_swap_verdef_out (bfd *abfd,
73 const Elf_Internal_Verdef *src,
74 Elf_External_Verdef *dst)
76 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
77 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
78 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
79 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
80 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
81 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
82 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
85 /* Swap in a Verdaux structure. */
87 void
88 _bfd_elf_swap_verdaux_in (bfd *abfd,
89 const Elf_External_Verdaux *src,
90 Elf_Internal_Verdaux *dst)
92 dst->vda_name = H_GET_32 (abfd, src->vda_name);
93 dst->vda_next = H_GET_32 (abfd, src->vda_next);
96 /* Swap out a Verdaux structure. */
98 void
99 _bfd_elf_swap_verdaux_out (bfd *abfd,
100 const Elf_Internal_Verdaux *src,
101 Elf_External_Verdaux *dst)
103 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
104 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
107 /* Swap in a Verneed structure. */
109 void
110 _bfd_elf_swap_verneed_in (bfd *abfd,
111 const Elf_External_Verneed *src,
112 Elf_Internal_Verneed *dst)
114 dst->vn_version = H_GET_16 (abfd, src->vn_version);
115 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
116 dst->vn_file = H_GET_32 (abfd, src->vn_file);
117 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
118 dst->vn_next = H_GET_32 (abfd, src->vn_next);
121 /* Swap out a Verneed structure. */
123 void
124 _bfd_elf_swap_verneed_out (bfd *abfd,
125 const Elf_Internal_Verneed *src,
126 Elf_External_Verneed *dst)
128 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
129 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
130 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
131 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
132 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
135 /* Swap in a Vernaux structure. */
137 void
138 _bfd_elf_swap_vernaux_in (bfd *abfd,
139 const Elf_External_Vernaux *src,
140 Elf_Internal_Vernaux *dst)
142 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
143 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
144 dst->vna_other = H_GET_16 (abfd, src->vna_other);
145 dst->vna_name = H_GET_32 (abfd, src->vna_name);
146 dst->vna_next = H_GET_32 (abfd, src->vna_next);
149 /* Swap out a Vernaux structure. */
151 void
152 _bfd_elf_swap_vernaux_out (bfd *abfd,
153 const Elf_Internal_Vernaux *src,
154 Elf_External_Vernaux *dst)
156 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
157 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
158 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
159 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
160 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
163 /* Swap in a Versym structure. */
165 void
166 _bfd_elf_swap_versym_in (bfd *abfd,
167 const Elf_External_Versym *src,
168 Elf_Internal_Versym *dst)
170 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
173 /* Swap out a Versym structure. */
175 void
176 _bfd_elf_swap_versym_out (bfd *abfd,
177 const Elf_Internal_Versym *src,
178 Elf_External_Versym *dst)
180 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
183 /* Standard ELF hash function. Do not change this function; you will
184 cause invalid hash tables to be generated. */
186 unsigned long
187 bfd_elf_hash (const char *namearg)
189 const unsigned char *name = (const unsigned char *) namearg;
190 unsigned long h = 0;
191 unsigned long g;
192 int ch;
194 while ((ch = *name++) != '\0')
196 h = (h << 4) + ch;
197 if ((g = (h & 0xf0000000)) != 0)
199 h ^= g >> 24;
200 /* The ELF ABI says `h &= ~g', but this is equivalent in
201 this case and on some machines one insn instead of two. */
202 h ^= g;
205 return h & 0xffffffff;
208 /* Read a specified number of bytes at a specified offset in an ELF
209 file, into a newly allocated buffer, and return a pointer to the
210 buffer. */
212 static char *
213 elf_read (bfd *abfd, file_ptr offset, bfd_size_type size)
215 char *buf;
217 if ((buf = bfd_alloc (abfd, size)) == NULL)
218 return NULL;
219 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
220 return NULL;
221 if (bfd_bread (buf, size, abfd) != size)
223 if (bfd_get_error () != bfd_error_system_call)
224 bfd_set_error (bfd_error_file_truncated);
225 return NULL;
227 return buf;
230 bfd_boolean
231 bfd_elf_mkobject (bfd *abfd)
233 /* This just does initialization. */
234 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
235 elf_tdata (abfd) = bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
236 if (elf_tdata (abfd) == 0)
237 return FALSE;
238 /* Since everything is done at close time, do we need any
239 initialization? */
241 return TRUE;
244 bfd_boolean
245 bfd_elf_mkcorefile (bfd *abfd)
247 /* I think this can be done just like an object file. */
248 return bfd_elf_mkobject (abfd);
251 char *
252 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
254 Elf_Internal_Shdr **i_shdrp;
255 char *shstrtab = NULL;
256 file_ptr offset;
257 bfd_size_type shstrtabsize;
259 i_shdrp = elf_elfsections (abfd);
260 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
261 return 0;
263 shstrtab = (char *) i_shdrp[shindex]->contents;
264 if (shstrtab == NULL)
266 /* No cached one, attempt to read, and cache what we read. */
267 offset = i_shdrp[shindex]->sh_offset;
268 shstrtabsize = i_shdrp[shindex]->sh_size;
269 shstrtab = elf_read (abfd, offset, shstrtabsize);
270 i_shdrp[shindex]->contents = shstrtab;
272 return shstrtab;
275 char *
276 bfd_elf_string_from_elf_section (bfd *abfd,
277 unsigned int shindex,
278 unsigned int strindex)
280 Elf_Internal_Shdr *hdr;
282 if (strindex == 0)
283 return "";
285 hdr = elf_elfsections (abfd)[shindex];
287 if (hdr->contents == NULL
288 && bfd_elf_get_str_section (abfd, shindex) == NULL)
289 return NULL;
291 if (strindex >= hdr->sh_size)
293 (*_bfd_error_handler)
294 (_("%s: invalid string offset %u >= %lu for section `%s'"),
295 bfd_archive_filename (abfd), strindex, (unsigned long) hdr->sh_size,
296 ((shindex == elf_elfheader(abfd)->e_shstrndx
297 && strindex == hdr->sh_name)
298 ? ".shstrtab"
299 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
300 return "";
303 return ((char *) hdr->contents) + strindex;
306 /* Read and convert symbols to internal format.
307 SYMCOUNT specifies the number of symbols to read, starting from
308 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
309 are non-NULL, they are used to store the internal symbols, external
310 symbols, and symbol section index extensions, respectively. */
312 Elf_Internal_Sym *
313 bfd_elf_get_elf_syms (bfd *ibfd,
314 Elf_Internal_Shdr *symtab_hdr,
315 size_t symcount,
316 size_t symoffset,
317 Elf_Internal_Sym *intsym_buf,
318 void *extsym_buf,
319 Elf_External_Sym_Shndx *extshndx_buf)
321 Elf_Internal_Shdr *shndx_hdr;
322 void *alloc_ext;
323 const bfd_byte *esym;
324 Elf_External_Sym_Shndx *alloc_extshndx;
325 Elf_External_Sym_Shndx *shndx;
326 Elf_Internal_Sym *isym;
327 Elf_Internal_Sym *isymend;
328 struct elf_backend_data *bed;
329 size_t extsym_size;
330 bfd_size_type amt;
331 file_ptr pos;
333 if (symcount == 0)
334 return intsym_buf;
336 /* Normal syms might have section extension entries. */
337 shndx_hdr = NULL;
338 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
339 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
341 /* Read the symbols. */
342 alloc_ext = NULL;
343 alloc_extshndx = NULL;
344 bed = get_elf_backend_data (ibfd);
345 extsym_size = bed->s->sizeof_sym;
346 amt = symcount * extsym_size;
347 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
348 if (extsym_buf == NULL)
350 alloc_ext = bfd_malloc (amt);
351 extsym_buf = alloc_ext;
353 if (extsym_buf == NULL
354 || bfd_seek (ibfd, pos, SEEK_SET) != 0
355 || bfd_bread (extsym_buf, amt, ibfd) != amt)
357 intsym_buf = NULL;
358 goto out;
361 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
362 extshndx_buf = NULL;
363 else
365 amt = symcount * sizeof (Elf_External_Sym_Shndx);
366 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
367 if (extshndx_buf == NULL)
369 alloc_extshndx = bfd_malloc (amt);
370 extshndx_buf = alloc_extshndx;
372 if (extshndx_buf == NULL
373 || bfd_seek (ibfd, pos, SEEK_SET) != 0
374 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
376 intsym_buf = NULL;
377 goto out;
381 if (intsym_buf == NULL)
383 bfd_size_type amt = symcount * sizeof (Elf_Internal_Sym);
384 intsym_buf = bfd_malloc (amt);
385 if (intsym_buf == NULL)
386 goto out;
389 /* Convert the symbols to internal form. */
390 isymend = intsym_buf + symcount;
391 for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf;
392 isym < isymend;
393 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
394 (*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym);
396 out:
397 if (alloc_ext != NULL)
398 free (alloc_ext);
399 if (alloc_extshndx != NULL)
400 free (alloc_extshndx);
402 return intsym_buf;
405 /* Look up a symbol name. */
406 const char *
407 bfd_elf_local_sym_name (bfd *abfd, Elf_Internal_Sym *isym)
409 unsigned int iname = isym->st_name;
410 unsigned int shindex = elf_tdata (abfd)->symtab_hdr.sh_link;
411 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION)
413 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
414 shindex = elf_elfheader (abfd)->e_shstrndx;
417 return bfd_elf_string_from_elf_section (abfd, shindex, iname);
420 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
421 sections. The first element is the flags, the rest are section
422 pointers. */
424 typedef union elf_internal_group {
425 Elf_Internal_Shdr *shdr;
426 unsigned int flags;
427 } Elf_Internal_Group;
429 /* Return the name of the group signature symbol. Why isn't the
430 signature just a string? */
432 static const char *
433 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
435 Elf_Internal_Shdr *hdr;
436 unsigned char esym[sizeof (Elf64_External_Sym)];
437 Elf_External_Sym_Shndx eshndx;
438 Elf_Internal_Sym isym;
440 /* First we need to ensure the symbol table is available. */
441 if (! bfd_section_from_shdr (abfd, ghdr->sh_link))
442 return NULL;
444 /* Go read the symbol. */
445 hdr = &elf_tdata (abfd)->symtab_hdr;
446 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
447 &isym, esym, &eshndx) == NULL)
448 return NULL;
450 return bfd_elf_local_sym_name (abfd, &isym);
453 /* Set next_in_group list pointer, and group name for NEWSECT. */
455 static bfd_boolean
456 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
458 unsigned int num_group = elf_tdata (abfd)->num_group;
460 /* If num_group is zero, read in all SHT_GROUP sections. The count
461 is set to -1 if there are no SHT_GROUP sections. */
462 if (num_group == 0)
464 unsigned int i, shnum;
466 /* First count the number of groups. If we have a SHT_GROUP
467 section with just a flag word (ie. sh_size is 4), ignore it. */
468 shnum = elf_numsections (abfd);
469 num_group = 0;
470 for (i = 0; i < shnum; i++)
472 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
473 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
474 num_group += 1;
477 if (num_group == 0)
478 num_group = (unsigned) -1;
479 elf_tdata (abfd)->num_group = num_group;
481 if (num_group > 0)
483 /* We keep a list of elf section headers for group sections,
484 so we can find them quickly. */
485 bfd_size_type amt = num_group * sizeof (Elf_Internal_Shdr *);
486 elf_tdata (abfd)->group_sect_ptr = bfd_alloc (abfd, amt);
487 if (elf_tdata (abfd)->group_sect_ptr == NULL)
488 return FALSE;
490 num_group = 0;
491 for (i = 0; i < shnum; i++)
493 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
494 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
496 unsigned char *src;
497 Elf_Internal_Group *dest;
499 /* Add to list of sections. */
500 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
501 num_group += 1;
503 /* Read the raw contents. */
504 BFD_ASSERT (sizeof (*dest) >= 4);
505 amt = shdr->sh_size * sizeof (*dest) / 4;
506 shdr->contents = bfd_alloc (abfd, amt);
507 if (shdr->contents == NULL
508 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
509 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
510 != shdr->sh_size))
511 return FALSE;
513 /* Translate raw contents, a flag word followed by an
514 array of elf section indices all in target byte order,
515 to the flag word followed by an array of elf section
516 pointers. */
517 src = shdr->contents + shdr->sh_size;
518 dest = (Elf_Internal_Group *) (shdr->contents + amt);
519 while (1)
521 unsigned int idx;
523 src -= 4;
524 --dest;
525 idx = H_GET_32 (abfd, src);
526 if (src == shdr->contents)
528 dest->flags = idx;
529 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
530 shdr->bfd_section->flags
531 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
532 break;
534 if (idx >= shnum)
536 ((*_bfd_error_handler)
537 (_("%s: invalid SHT_GROUP entry"),
538 bfd_archive_filename (abfd)));
539 idx = 0;
541 dest->shdr = elf_elfsections (abfd)[idx];
548 if (num_group != (unsigned) -1)
550 unsigned int i;
552 for (i = 0; i < num_group; i++)
554 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
555 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
556 unsigned int n_elt = shdr->sh_size / 4;
558 /* Look through this group's sections to see if current
559 section is a member. */
560 while (--n_elt != 0)
561 if ((++idx)->shdr == hdr)
563 asection *s = NULL;
565 /* We are a member of this group. Go looking through
566 other members to see if any others are linked via
567 next_in_group. */
568 idx = (Elf_Internal_Group *) shdr->contents;
569 n_elt = shdr->sh_size / 4;
570 while (--n_elt != 0)
571 if ((s = (++idx)->shdr->bfd_section) != NULL
572 && elf_next_in_group (s) != NULL)
573 break;
574 if (n_elt != 0)
576 /* Snarf the group name from other member, and
577 insert current section in circular list. */
578 elf_group_name (newsect) = elf_group_name (s);
579 elf_next_in_group (newsect) = elf_next_in_group (s);
580 elf_next_in_group (s) = newsect;
582 else
584 const char *gname;
586 gname = group_signature (abfd, shdr);
587 if (gname == NULL)
588 return FALSE;
589 elf_group_name (newsect) = gname;
591 /* Start a circular list with one element. */
592 elf_next_in_group (newsect) = newsect;
595 /* If the group section has been created, point to the
596 new member. */
597 if (shdr->bfd_section != NULL)
598 elf_next_in_group (shdr->bfd_section) = newsect;
600 i = num_group - 1;
601 break;
606 if (elf_group_name (newsect) == NULL)
608 (*_bfd_error_handler) (_("%s: no group info for section %s"),
609 bfd_archive_filename (abfd), newsect->name);
611 return TRUE;
614 bfd_boolean
615 bfd_elf_discard_group (bfd *abfd ATTRIBUTE_UNUSED, asection *group)
617 asection *first = elf_next_in_group (group);
618 asection *s = first;
620 while (s != NULL)
622 s->output_section = bfd_abs_section_ptr;
623 s = elf_next_in_group (s);
624 /* These lists are circular. */
625 if (s == first)
626 break;
628 return TRUE;
631 /* Make a BFD section from an ELF section. We store a pointer to the
632 BFD section in the bfd_section field of the header. */
634 bfd_boolean
635 _bfd_elf_make_section_from_shdr (bfd *abfd,
636 Elf_Internal_Shdr *hdr,
637 const char *name)
639 asection *newsect;
640 flagword flags;
641 struct elf_backend_data *bed;
643 if (hdr->bfd_section != NULL)
645 BFD_ASSERT (strcmp (name,
646 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
647 return TRUE;
650 newsect = bfd_make_section_anyway (abfd, name);
651 if (newsect == NULL)
652 return FALSE;
654 /* Always use the real type/flags. */
655 elf_section_type (newsect) = hdr->sh_type;
656 elf_section_flags (newsect) = hdr->sh_flags;
658 newsect->filepos = hdr->sh_offset;
660 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
661 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
662 || ! bfd_set_section_alignment (abfd, newsect,
663 bfd_log2 ((bfd_vma) hdr->sh_addralign)))
664 return FALSE;
666 flags = SEC_NO_FLAGS;
667 if (hdr->sh_type != SHT_NOBITS)
668 flags |= SEC_HAS_CONTENTS;
669 if (hdr->sh_type == SHT_GROUP)
670 flags |= SEC_GROUP | SEC_EXCLUDE;
671 if ((hdr->sh_flags & SHF_ALLOC) != 0)
673 flags |= SEC_ALLOC;
674 if (hdr->sh_type != SHT_NOBITS)
675 flags |= SEC_LOAD;
677 if ((hdr->sh_flags & SHF_WRITE) == 0)
678 flags |= SEC_READONLY;
679 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
680 flags |= SEC_CODE;
681 else if ((flags & SEC_LOAD) != 0)
682 flags |= SEC_DATA;
683 if ((hdr->sh_flags & SHF_MERGE) != 0)
685 flags |= SEC_MERGE;
686 newsect->entsize = hdr->sh_entsize;
687 if ((hdr->sh_flags & SHF_STRINGS) != 0)
688 flags |= SEC_STRINGS;
690 if (hdr->sh_flags & SHF_GROUP)
691 if (!setup_group (abfd, hdr, newsect))
692 return FALSE;
693 if ((hdr->sh_flags & SHF_TLS) != 0)
694 flags |= SEC_THREAD_LOCAL;
696 /* The debugging sections appear to be recognized only by name, not
697 any sort of flag. */
699 static const char *debug_sec_names [] =
701 ".debug",
702 ".gnu.linkonce.wi.",
703 ".line",
704 ".stab"
706 int i;
708 for (i = ARRAY_SIZE (debug_sec_names); i--;)
709 if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0)
710 break;
712 if (i >= 0)
713 flags |= SEC_DEBUGGING;
716 /* As a GNU extension, if the name begins with .gnu.linkonce, we
717 only link a single copy of the section. This is used to support
718 g++. g++ will emit each template expansion in its own section.
719 The symbols will be defined as weak, so that multiple definitions
720 are permitted. The GNU linker extension is to actually discard
721 all but one of the sections. */
722 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0
723 && elf_next_in_group (newsect) == NULL)
724 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
726 bed = get_elf_backend_data (abfd);
727 if (bed->elf_backend_section_flags)
728 if (! bed->elf_backend_section_flags (&flags, hdr))
729 return FALSE;
731 if (! bfd_set_section_flags (abfd, newsect, flags))
732 return FALSE;
734 if ((flags & SEC_ALLOC) != 0)
736 Elf_Internal_Phdr *phdr;
737 unsigned int i;
739 /* Look through the phdrs to see if we need to adjust the lma.
740 If all the p_paddr fields are zero, we ignore them, since
741 some ELF linkers produce such output. */
742 phdr = elf_tdata (abfd)->phdr;
743 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
745 if (phdr->p_paddr != 0)
746 break;
748 if (i < elf_elfheader (abfd)->e_phnum)
750 phdr = elf_tdata (abfd)->phdr;
751 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
753 /* This section is part of this segment if its file
754 offset plus size lies within the segment's memory
755 span and, if the section is loaded, the extent of the
756 loaded data lies within the extent of the segment.
758 Note - we used to check the p_paddr field as well, and
759 refuse to set the LMA if it was 0. This is wrong
760 though, as a perfectly valid initialised segment can
761 have a p_paddr of zero. Some architectures, eg ARM,
762 place special significance on the address 0 and
763 executables need to be able to have a segment which
764 covers this address. */
765 if (phdr->p_type == PT_LOAD
766 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
767 && (hdr->sh_offset + hdr->sh_size
768 <= phdr->p_offset + phdr->p_memsz)
769 && ((flags & SEC_LOAD) == 0
770 || (hdr->sh_offset + hdr->sh_size
771 <= phdr->p_offset + phdr->p_filesz)))
773 if ((flags & SEC_LOAD) == 0)
774 newsect->lma = (phdr->p_paddr
775 + hdr->sh_addr - phdr->p_vaddr);
776 else
777 /* We used to use the same adjustment for SEC_LOAD
778 sections, but that doesn't work if the segment
779 is packed with code from multiple VMAs.
780 Instead we calculate the section LMA based on
781 the segment LMA. It is assumed that the
782 segment will contain sections with contiguous
783 LMAs, even if the VMAs are not. */
784 newsect->lma = (phdr->p_paddr
785 + hdr->sh_offset - phdr->p_offset);
787 /* With contiguous segments, we can't tell from file
788 offsets whether a section with zero size should
789 be placed at the end of one segment or the
790 beginning of the next. Decide based on vaddr. */
791 if (hdr->sh_addr >= phdr->p_vaddr
792 && (hdr->sh_addr + hdr->sh_size
793 <= phdr->p_vaddr + phdr->p_memsz))
794 break;
800 hdr->bfd_section = newsect;
801 elf_section_data (newsect)->this_hdr = *hdr;
803 return TRUE;
807 INTERNAL_FUNCTION
808 bfd_elf_find_section
810 SYNOPSIS
811 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
813 DESCRIPTION
814 Helper functions for GDB to locate the string tables.
815 Since BFD hides string tables from callers, GDB needs to use an
816 internal hook to find them. Sun's .stabstr, in particular,
817 isn't even pointed to by the .stab section, so ordinary
818 mechanisms wouldn't work to find it, even if we had some.
821 struct elf_internal_shdr *
822 bfd_elf_find_section (bfd *abfd, char *name)
824 Elf_Internal_Shdr **i_shdrp;
825 char *shstrtab;
826 unsigned int max;
827 unsigned int i;
829 i_shdrp = elf_elfsections (abfd);
830 if (i_shdrp != NULL)
832 shstrtab = bfd_elf_get_str_section (abfd,
833 elf_elfheader (abfd)->e_shstrndx);
834 if (shstrtab != NULL)
836 max = elf_numsections (abfd);
837 for (i = 1; i < max; i++)
838 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
839 return i_shdrp[i];
842 return 0;
845 const char *const bfd_elf_section_type_names[] = {
846 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
847 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
848 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
851 /* ELF relocs are against symbols. If we are producing relocatable
852 output, and the reloc is against an external symbol, and nothing
853 has given us any additional addend, the resulting reloc will also
854 be against the same symbol. In such a case, we don't want to
855 change anything about the way the reloc is handled, since it will
856 all be done at final link time. Rather than put special case code
857 into bfd_perform_relocation, all the reloc types use this howto
858 function. It just short circuits the reloc if producing
859 relocatable output against an external symbol. */
861 bfd_reloc_status_type
862 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
863 arelent *reloc_entry,
864 asymbol *symbol,
865 void *data ATTRIBUTE_UNUSED,
866 asection *input_section,
867 bfd *output_bfd,
868 char **error_message ATTRIBUTE_UNUSED)
870 if (output_bfd != NULL
871 && (symbol->flags & BSF_SECTION_SYM) == 0
872 && (! reloc_entry->howto->partial_inplace
873 || reloc_entry->addend == 0))
875 reloc_entry->address += input_section->output_offset;
876 return bfd_reloc_ok;
879 return bfd_reloc_continue;
882 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
884 static void
885 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
886 asection *sec)
888 BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE);
889 sec->sec_info_type = ELF_INFO_TYPE_NONE;
892 /* Finish SHF_MERGE section merging. */
894 bfd_boolean
895 _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
897 if (!is_elf_hash_table (info))
898 return FALSE;
899 if (elf_hash_table (info)->merge_info)
900 _bfd_merge_sections (abfd, elf_hash_table (info)->merge_info,
901 merge_sections_remove_hook);
902 return TRUE;
905 void
906 _bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
908 sec->output_section = bfd_abs_section_ptr;
909 sec->output_offset = sec->vma;
910 if (!is_elf_hash_table (info))
911 return;
913 sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
916 /* Copy the program header and other data from one object module to
917 another. */
919 bfd_boolean
920 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
922 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
923 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
924 return TRUE;
926 BFD_ASSERT (!elf_flags_init (obfd)
927 || (elf_elfheader (obfd)->e_flags
928 == elf_elfheader (ibfd)->e_flags));
930 elf_gp (obfd) = elf_gp (ibfd);
931 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
932 elf_flags_init (obfd) = TRUE;
933 return TRUE;
936 /* Print out the program headers. */
938 bfd_boolean
939 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
941 FILE *f = farg;
942 Elf_Internal_Phdr *p;
943 asection *s;
944 bfd_byte *dynbuf = NULL;
946 p = elf_tdata (abfd)->phdr;
947 if (p != NULL)
949 unsigned int i, c;
951 fprintf (f, _("\nProgram Header:\n"));
952 c = elf_elfheader (abfd)->e_phnum;
953 for (i = 0; i < c; i++, p++)
955 const char *pt;
956 char buf[20];
958 switch (p->p_type)
960 case PT_NULL: pt = "NULL"; break;
961 case PT_LOAD: pt = "LOAD"; break;
962 case PT_DYNAMIC: pt = "DYNAMIC"; break;
963 case PT_INTERP: pt = "INTERP"; break;
964 case PT_NOTE: pt = "NOTE"; break;
965 case PT_SHLIB: pt = "SHLIB"; break;
966 case PT_PHDR: pt = "PHDR"; break;
967 case PT_TLS: pt = "TLS"; break;
968 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
969 case PT_GNU_STACK: pt = "STACK"; break;
970 default: sprintf (buf, "0x%lx", p->p_type); pt = buf; break;
972 fprintf (f, "%8s off 0x", pt);
973 bfd_fprintf_vma (abfd, f, p->p_offset);
974 fprintf (f, " vaddr 0x");
975 bfd_fprintf_vma (abfd, f, p->p_vaddr);
976 fprintf (f, " paddr 0x");
977 bfd_fprintf_vma (abfd, f, p->p_paddr);
978 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
979 fprintf (f, " filesz 0x");
980 bfd_fprintf_vma (abfd, f, p->p_filesz);
981 fprintf (f, " memsz 0x");
982 bfd_fprintf_vma (abfd, f, p->p_memsz);
983 fprintf (f, " flags %c%c%c",
984 (p->p_flags & PF_R) != 0 ? 'r' : '-',
985 (p->p_flags & PF_W) != 0 ? 'w' : '-',
986 (p->p_flags & PF_X) != 0 ? 'x' : '-');
987 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
988 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
989 fprintf (f, "\n");
993 s = bfd_get_section_by_name (abfd, ".dynamic");
994 if (s != NULL)
996 int elfsec;
997 unsigned long shlink;
998 bfd_byte *extdyn, *extdynend;
999 size_t extdynsize;
1000 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1002 fprintf (f, _("\nDynamic Section:\n"));
1004 dynbuf = bfd_malloc (s->_raw_size);
1005 if (dynbuf == NULL)
1006 goto error_return;
1007 if (! bfd_get_section_contents (abfd, s, dynbuf, 0, s->_raw_size))
1008 goto error_return;
1010 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1011 if (elfsec == -1)
1012 goto error_return;
1013 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1015 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1016 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1018 extdyn = dynbuf;
1019 extdynend = extdyn + s->_raw_size;
1020 for (; extdyn < extdynend; extdyn += extdynsize)
1022 Elf_Internal_Dyn dyn;
1023 const char *name;
1024 char ab[20];
1025 bfd_boolean stringp;
1027 (*swap_dyn_in) (abfd, extdyn, &dyn);
1029 if (dyn.d_tag == DT_NULL)
1030 break;
1032 stringp = FALSE;
1033 switch (dyn.d_tag)
1035 default:
1036 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1037 name = ab;
1038 break;
1040 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1041 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1042 case DT_PLTGOT: name = "PLTGOT"; break;
1043 case DT_HASH: name = "HASH"; break;
1044 case DT_STRTAB: name = "STRTAB"; break;
1045 case DT_SYMTAB: name = "SYMTAB"; break;
1046 case DT_RELA: name = "RELA"; break;
1047 case DT_RELASZ: name = "RELASZ"; break;
1048 case DT_RELAENT: name = "RELAENT"; break;
1049 case DT_STRSZ: name = "STRSZ"; break;
1050 case DT_SYMENT: name = "SYMENT"; break;
1051 case DT_INIT: name = "INIT"; break;
1052 case DT_FINI: name = "FINI"; break;
1053 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1054 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1055 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1056 case DT_REL: name = "REL"; break;
1057 case DT_RELSZ: name = "RELSZ"; break;
1058 case DT_RELENT: name = "RELENT"; break;
1059 case DT_PLTREL: name = "PLTREL"; break;
1060 case DT_DEBUG: name = "DEBUG"; break;
1061 case DT_TEXTREL: name = "TEXTREL"; break;
1062 case DT_JMPREL: name = "JMPREL"; break;
1063 case DT_BIND_NOW: name = "BIND_NOW"; break;
1064 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1065 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1066 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1067 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1068 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1069 case DT_FLAGS: name = "FLAGS"; break;
1070 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1071 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1072 case DT_CHECKSUM: name = "CHECKSUM"; break;
1073 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1074 case DT_MOVEENT: name = "MOVEENT"; break;
1075 case DT_MOVESZ: name = "MOVESZ"; break;
1076 case DT_FEATURE: name = "FEATURE"; break;
1077 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1078 case DT_SYMINSZ: name = "SYMINSZ"; break;
1079 case DT_SYMINENT: name = "SYMINENT"; break;
1080 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1081 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1082 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1083 case DT_PLTPAD: name = "PLTPAD"; break;
1084 case DT_MOVETAB: name = "MOVETAB"; break;
1085 case DT_SYMINFO: name = "SYMINFO"; break;
1086 case DT_RELACOUNT: name = "RELACOUNT"; break;
1087 case DT_RELCOUNT: name = "RELCOUNT"; break;
1088 case DT_FLAGS_1: name = "FLAGS_1"; break;
1089 case DT_VERSYM: name = "VERSYM"; break;
1090 case DT_VERDEF: name = "VERDEF"; break;
1091 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1092 case DT_VERNEED: name = "VERNEED"; break;
1093 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1094 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1095 case DT_USED: name = "USED"; break;
1096 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1099 fprintf (f, " %-11s ", name);
1100 if (! stringp)
1101 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
1102 else
1104 const char *string;
1105 unsigned int tagv = dyn.d_un.d_val;
1107 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1108 if (string == NULL)
1109 goto error_return;
1110 fprintf (f, "%s", string);
1112 fprintf (f, "\n");
1115 free (dynbuf);
1116 dynbuf = NULL;
1119 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1120 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1122 if (! _bfd_elf_slurp_version_tables (abfd))
1123 return FALSE;
1126 if (elf_dynverdef (abfd) != 0)
1128 Elf_Internal_Verdef *t;
1130 fprintf (f, _("\nVersion definitions:\n"));
1131 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1133 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1134 t->vd_flags, t->vd_hash, t->vd_nodename);
1135 if (t->vd_auxptr->vda_nextptr != NULL)
1137 Elf_Internal_Verdaux *a;
1139 fprintf (f, "\t");
1140 for (a = t->vd_auxptr->vda_nextptr;
1141 a != NULL;
1142 a = a->vda_nextptr)
1143 fprintf (f, "%s ", a->vda_nodename);
1144 fprintf (f, "\n");
1149 if (elf_dynverref (abfd) != 0)
1151 Elf_Internal_Verneed *t;
1153 fprintf (f, _("\nVersion References:\n"));
1154 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1156 Elf_Internal_Vernaux *a;
1158 fprintf (f, _(" required from %s:\n"), t->vn_filename);
1159 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1160 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1161 a->vna_flags, a->vna_other, a->vna_nodename);
1165 return TRUE;
1167 error_return:
1168 if (dynbuf != NULL)
1169 free (dynbuf);
1170 return FALSE;
1173 /* Display ELF-specific fields of a symbol. */
1175 void
1176 bfd_elf_print_symbol (bfd *abfd,
1177 void *filep,
1178 asymbol *symbol,
1179 bfd_print_symbol_type how)
1181 FILE *file = filep;
1182 switch (how)
1184 case bfd_print_symbol_name:
1185 fprintf (file, "%s", symbol->name);
1186 break;
1187 case bfd_print_symbol_more:
1188 fprintf (file, "elf ");
1189 bfd_fprintf_vma (abfd, file, symbol->value);
1190 fprintf (file, " %lx", (long) symbol->flags);
1191 break;
1192 case bfd_print_symbol_all:
1194 const char *section_name;
1195 const char *name = NULL;
1196 struct elf_backend_data *bed;
1197 unsigned char st_other;
1198 bfd_vma val;
1200 section_name = symbol->section ? symbol->section->name : "(*none*)";
1202 bed = get_elf_backend_data (abfd);
1203 if (bed->elf_backend_print_symbol_all)
1204 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1206 if (name == NULL)
1208 name = symbol->name;
1209 bfd_print_symbol_vandf (abfd, file, symbol);
1212 fprintf (file, " %s\t", section_name);
1213 /* Print the "other" value for a symbol. For common symbols,
1214 we've already printed the size; now print the alignment.
1215 For other symbols, we have no specified alignment, and
1216 we've printed the address; now print the size. */
1217 if (bfd_is_com_section (symbol->section))
1218 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1219 else
1220 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1221 bfd_fprintf_vma (abfd, file, val);
1223 /* If we have version information, print it. */
1224 if (elf_tdata (abfd)->dynversym_section != 0
1225 && (elf_tdata (abfd)->dynverdef_section != 0
1226 || elf_tdata (abfd)->dynverref_section != 0))
1228 unsigned int vernum;
1229 const char *version_string;
1231 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1233 if (vernum == 0)
1234 version_string = "";
1235 else if (vernum == 1)
1236 version_string = "Base";
1237 else if (vernum <= elf_tdata (abfd)->cverdefs)
1238 version_string =
1239 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1240 else
1242 Elf_Internal_Verneed *t;
1244 version_string = "";
1245 for (t = elf_tdata (abfd)->verref;
1246 t != NULL;
1247 t = t->vn_nextref)
1249 Elf_Internal_Vernaux *a;
1251 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1253 if (a->vna_other == vernum)
1255 version_string = a->vna_nodename;
1256 break;
1262 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1263 fprintf (file, " %-11s", version_string);
1264 else
1266 int i;
1268 fprintf (file, " (%s)", version_string);
1269 for (i = 10 - strlen (version_string); i > 0; --i)
1270 putc (' ', file);
1274 /* If the st_other field is not zero, print it. */
1275 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1277 switch (st_other)
1279 case 0: break;
1280 case STV_INTERNAL: fprintf (file, " .internal"); break;
1281 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1282 case STV_PROTECTED: fprintf (file, " .protected"); break;
1283 default:
1284 /* Some other non-defined flags are also present, so print
1285 everything hex. */
1286 fprintf (file, " 0x%02x", (unsigned int) st_other);
1289 fprintf (file, " %s", name);
1291 break;
1295 /* Create an entry in an ELF linker hash table. */
1297 struct bfd_hash_entry *
1298 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1299 struct bfd_hash_table *table,
1300 const char *string)
1302 /* Allocate the structure if it has not already been allocated by a
1303 subclass. */
1304 if (entry == NULL)
1306 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
1307 if (entry == NULL)
1308 return entry;
1311 /* Call the allocation method of the superclass. */
1312 entry = _bfd_link_hash_newfunc (entry, table, string);
1313 if (entry != NULL)
1315 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
1316 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
1318 /* Set local fields. */
1319 ret->indx = -1;
1320 ret->dynindx = -1;
1321 ret->dynstr_index = 0;
1322 ret->elf_hash_value = 0;
1323 ret->weakdef = NULL;
1324 ret->verinfo.verdef = NULL;
1325 ret->vtable_entries_size = 0;
1326 ret->vtable_entries_used = NULL;
1327 ret->vtable_parent = NULL;
1328 ret->got = htab->init_refcount;
1329 ret->plt = htab->init_refcount;
1330 ret->size = 0;
1331 ret->type = STT_NOTYPE;
1332 ret->other = 0;
1333 /* Assume that we have been called by a non-ELF symbol reader.
1334 This flag is then reset by the code which reads an ELF input
1335 file. This ensures that a symbol created by a non-ELF symbol
1336 reader will have the flag set correctly. */
1337 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
1340 return entry;
1343 /* Copy data from an indirect symbol to its direct symbol, hiding the
1344 old indirect symbol. Also used for copying flags to a weakdef. */
1346 void
1347 _bfd_elf_link_hash_copy_indirect (struct elf_backend_data *bed,
1348 struct elf_link_hash_entry *dir,
1349 struct elf_link_hash_entry *ind)
1351 bfd_signed_vma tmp;
1352 bfd_signed_vma lowest_valid = bed->can_refcount;
1354 /* Copy down any references that we may have already seen to the
1355 symbol which just became indirect. */
1357 dir->elf_link_hash_flags |=
1358 (ind->elf_link_hash_flags
1359 & (ELF_LINK_HASH_REF_DYNAMIC
1360 | ELF_LINK_HASH_REF_REGULAR
1361 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1362 | ELF_LINK_NON_GOT_REF));
1364 if (ind->root.type != bfd_link_hash_indirect)
1365 return;
1367 /* Copy over the global and procedure linkage table refcount entries.
1368 These may have been already set up by a check_relocs routine. */
1369 tmp = dir->got.refcount;
1370 if (tmp < lowest_valid)
1372 dir->got.refcount = ind->got.refcount;
1373 ind->got.refcount = tmp;
1375 else
1376 BFD_ASSERT (ind->got.refcount < lowest_valid);
1378 tmp = dir->plt.refcount;
1379 if (tmp < lowest_valid)
1381 dir->plt.refcount = ind->plt.refcount;
1382 ind->plt.refcount = tmp;
1384 else
1385 BFD_ASSERT (ind->plt.refcount < lowest_valid);
1387 if (dir->dynindx == -1)
1389 dir->dynindx = ind->dynindx;
1390 dir->dynstr_index = ind->dynstr_index;
1391 ind->dynindx = -1;
1392 ind->dynstr_index = 0;
1394 else
1395 BFD_ASSERT (ind->dynindx == -1);
1398 void
1399 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
1400 struct elf_link_hash_entry *h,
1401 bfd_boolean force_local)
1403 h->plt = elf_hash_table (info)->init_offset;
1404 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1405 if (force_local)
1407 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
1408 if (h->dynindx != -1)
1410 h->dynindx = -1;
1411 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1412 h->dynstr_index);
1417 /* Initialize an ELF linker hash table. */
1419 bfd_boolean
1420 _bfd_elf_link_hash_table_init
1421 (struct elf_link_hash_table *table,
1422 bfd *abfd,
1423 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
1424 struct bfd_hash_table *,
1425 const char *))
1427 bfd_boolean ret;
1429 table->dynamic_sections_created = FALSE;
1430 table->dynobj = NULL;
1431 /* Make sure can_refcount is extended to the width and signedness of
1432 init_refcount before we subtract one from it. */
1433 table->init_refcount.refcount = get_elf_backend_data (abfd)->can_refcount;
1434 table->init_refcount.refcount -= 1;
1435 table->init_offset.offset = -(bfd_vma) 1;
1436 /* The first dynamic symbol is a dummy. */
1437 table->dynsymcount = 1;
1438 table->dynstr = NULL;
1439 table->bucketcount = 0;
1440 table->needed = NULL;
1441 table->hgot = NULL;
1442 table->stab_info = NULL;
1443 table->merge_info = NULL;
1444 memset (&table->eh_info, 0, sizeof (table->eh_info));
1445 table->dynlocal = NULL;
1446 table->runpath = NULL;
1447 table->tls_segment = NULL;
1448 table->loaded = NULL;
1450 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc);
1451 table->root.type = bfd_link_elf_hash_table;
1453 return ret;
1456 /* Create an ELF linker hash table. */
1458 struct bfd_link_hash_table *
1459 _bfd_elf_link_hash_table_create (bfd *abfd)
1461 struct elf_link_hash_table *ret;
1462 bfd_size_type amt = sizeof (struct elf_link_hash_table);
1464 ret = bfd_malloc (amt);
1465 if (ret == NULL)
1466 return NULL;
1468 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
1470 free (ret);
1471 return NULL;
1474 return &ret->root;
1477 /* This is a hook for the ELF emulation code in the generic linker to
1478 tell the backend linker what file name to use for the DT_NEEDED
1479 entry for a dynamic object. The generic linker passes name as an
1480 empty string to indicate that no DT_NEEDED entry should be made. */
1482 void
1483 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
1485 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1486 && bfd_get_format (abfd) == bfd_object)
1487 elf_dt_name (abfd) = name;
1490 void
1491 bfd_elf_set_dt_needed_soname (bfd *abfd, const char *name)
1493 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1494 && bfd_get_format (abfd) == bfd_object)
1495 elf_dt_soname (abfd) = name;
1498 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1499 the linker ELF emulation code. */
1501 struct bfd_link_needed_list *
1502 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
1503 struct bfd_link_info *info)
1505 if (! is_elf_hash_table (info))
1506 return NULL;
1507 return elf_hash_table (info)->needed;
1510 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1511 hook for the linker ELF emulation code. */
1513 struct bfd_link_needed_list *
1514 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
1515 struct bfd_link_info *info)
1517 if (! is_elf_hash_table (info))
1518 return NULL;
1519 return elf_hash_table (info)->runpath;
1522 /* Get the name actually used for a dynamic object for a link. This
1523 is the SONAME entry if there is one. Otherwise, it is the string
1524 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1526 const char *
1527 bfd_elf_get_dt_soname (bfd *abfd)
1529 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1530 && bfd_get_format (abfd) == bfd_object)
1531 return elf_dt_name (abfd);
1532 return NULL;
1535 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1536 the ELF linker emulation code. */
1538 bfd_boolean
1539 bfd_elf_get_bfd_needed_list (bfd *abfd,
1540 struct bfd_link_needed_list **pneeded)
1542 asection *s;
1543 bfd_byte *dynbuf = NULL;
1544 int elfsec;
1545 unsigned long shlink;
1546 bfd_byte *extdyn, *extdynend;
1547 size_t extdynsize;
1548 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1550 *pneeded = NULL;
1552 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1553 || bfd_get_format (abfd) != bfd_object)
1554 return TRUE;
1556 s = bfd_get_section_by_name (abfd, ".dynamic");
1557 if (s == NULL || s->_raw_size == 0)
1558 return TRUE;
1560 dynbuf = bfd_malloc (s->_raw_size);
1561 if (dynbuf == NULL)
1562 goto error_return;
1564 if (! bfd_get_section_contents (abfd, s, dynbuf, 0, s->_raw_size))
1565 goto error_return;
1567 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1568 if (elfsec == -1)
1569 goto error_return;
1571 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1573 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1574 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1576 extdyn = dynbuf;
1577 extdynend = extdyn + s->_raw_size;
1578 for (; extdyn < extdynend; extdyn += extdynsize)
1580 Elf_Internal_Dyn dyn;
1582 (*swap_dyn_in) (abfd, extdyn, &dyn);
1584 if (dyn.d_tag == DT_NULL)
1585 break;
1587 if (dyn.d_tag == DT_NEEDED)
1589 const char *string;
1590 struct bfd_link_needed_list *l;
1591 unsigned int tagv = dyn.d_un.d_val;
1592 bfd_size_type amt;
1594 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1595 if (string == NULL)
1596 goto error_return;
1598 amt = sizeof *l;
1599 l = bfd_alloc (abfd, amt);
1600 if (l == NULL)
1601 goto error_return;
1603 l->by = abfd;
1604 l->name = string;
1605 l->next = *pneeded;
1606 *pneeded = l;
1610 free (dynbuf);
1612 return TRUE;
1614 error_return:
1615 if (dynbuf != NULL)
1616 free (dynbuf);
1617 return FALSE;
1620 /* Allocate an ELF string table--force the first byte to be zero. */
1622 struct bfd_strtab_hash *
1623 _bfd_elf_stringtab_init (void)
1625 struct bfd_strtab_hash *ret;
1627 ret = _bfd_stringtab_init ();
1628 if (ret != NULL)
1630 bfd_size_type loc;
1632 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1633 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1634 if (loc == (bfd_size_type) -1)
1636 _bfd_stringtab_free (ret);
1637 ret = NULL;
1640 return ret;
1643 /* ELF .o/exec file reading */
1645 /* Create a new bfd section from an ELF section header. */
1647 bfd_boolean
1648 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1650 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1651 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1652 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1653 const char *name;
1655 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1657 switch (hdr->sh_type)
1659 case SHT_NULL:
1660 /* Inactive section. Throw it away. */
1661 return TRUE;
1663 case SHT_PROGBITS: /* Normal section with contents. */
1664 case SHT_NOBITS: /* .bss section. */
1665 case SHT_HASH: /* .hash section. */
1666 case SHT_NOTE: /* .note section. */
1667 case SHT_INIT_ARRAY: /* .init_array section. */
1668 case SHT_FINI_ARRAY: /* .fini_array section. */
1669 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1670 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1672 case SHT_DYNAMIC: /* Dynamic linking information. */
1673 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1674 return FALSE;
1675 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1677 Elf_Internal_Shdr *dynsymhdr;
1679 /* The shared libraries distributed with hpux11 have a bogus
1680 sh_link field for the ".dynamic" section. Find the
1681 string table for the ".dynsym" section instead. */
1682 if (elf_dynsymtab (abfd) != 0)
1684 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1685 hdr->sh_link = dynsymhdr->sh_link;
1687 else
1689 unsigned int i, num_sec;
1691 num_sec = elf_numsections (abfd);
1692 for (i = 1; i < num_sec; i++)
1694 dynsymhdr = elf_elfsections (abfd)[i];
1695 if (dynsymhdr->sh_type == SHT_DYNSYM)
1697 hdr->sh_link = dynsymhdr->sh_link;
1698 break;
1703 break;
1705 case SHT_SYMTAB: /* A symbol table */
1706 if (elf_onesymtab (abfd) == shindex)
1707 return TRUE;
1709 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1710 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1711 elf_onesymtab (abfd) = shindex;
1712 elf_tdata (abfd)->symtab_hdr = *hdr;
1713 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1714 abfd->flags |= HAS_SYMS;
1716 /* Sometimes a shared object will map in the symbol table. If
1717 SHF_ALLOC is set, and this is a shared object, then we also
1718 treat this section as a BFD section. We can not base the
1719 decision purely on SHF_ALLOC, because that flag is sometimes
1720 set in a relocatable object file, which would confuse the
1721 linker. */
1722 if ((hdr->sh_flags & SHF_ALLOC) != 0
1723 && (abfd->flags & DYNAMIC) != 0
1724 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1725 return FALSE;
1727 return TRUE;
1729 case SHT_DYNSYM: /* A dynamic symbol table */
1730 if (elf_dynsymtab (abfd) == shindex)
1731 return TRUE;
1733 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1734 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1735 elf_dynsymtab (abfd) = shindex;
1736 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1737 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1738 abfd->flags |= HAS_SYMS;
1740 /* Besides being a symbol table, we also treat this as a regular
1741 section, so that objcopy can handle it. */
1742 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1744 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1745 if (elf_symtab_shndx (abfd) == shindex)
1746 return TRUE;
1748 /* Get the associated symbol table. */
1749 if (! bfd_section_from_shdr (abfd, hdr->sh_link)
1750 || hdr->sh_link != elf_onesymtab (abfd))
1751 return FALSE;
1753 elf_symtab_shndx (abfd) = shindex;
1754 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1755 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1756 return TRUE;
1758 case SHT_STRTAB: /* A string table */
1759 if (hdr->bfd_section != NULL)
1760 return TRUE;
1761 if (ehdr->e_shstrndx == shindex)
1763 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1764 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1765 return TRUE;
1768 unsigned int i, num_sec;
1770 num_sec = elf_numsections (abfd);
1771 for (i = 1; i < num_sec; i++)
1773 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1774 if (hdr2->sh_link == shindex)
1776 if (! bfd_section_from_shdr (abfd, i))
1777 return FALSE;
1778 if (elf_onesymtab (abfd) == i)
1780 elf_tdata (abfd)->strtab_hdr = *hdr;
1781 elf_elfsections (abfd)[shindex] =
1782 &elf_tdata (abfd)->strtab_hdr;
1783 return TRUE;
1785 if (elf_dynsymtab (abfd) == i)
1787 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1788 elf_elfsections (abfd)[shindex] = hdr =
1789 &elf_tdata (abfd)->dynstrtab_hdr;
1790 /* We also treat this as a regular section, so
1791 that objcopy can handle it. */
1792 break;
1794 #if 0 /* Not handling other string tables specially right now. */
1795 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1796 /* We have a strtab for some random other section. */
1797 newsect = (asection *) hdr2->bfd_section;
1798 if (!newsect)
1799 break;
1800 hdr->bfd_section = newsect;
1801 hdr2 = &elf_section_data (newsect)->str_hdr;
1802 *hdr2 = *hdr;
1803 elf_elfsections (abfd)[shindex] = hdr2;
1804 #endif
1809 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1811 case SHT_REL:
1812 case SHT_RELA:
1813 /* *These* do a lot of work -- but build no sections! */
1815 asection *target_sect;
1816 Elf_Internal_Shdr *hdr2;
1817 unsigned int num_sec = elf_numsections (abfd);
1819 /* Check for a bogus link to avoid crashing. */
1820 if ((hdr->sh_link >= SHN_LORESERVE && hdr->sh_link <= SHN_HIRESERVE)
1821 || hdr->sh_link >= num_sec)
1823 ((*_bfd_error_handler)
1824 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1825 bfd_archive_filename (abfd), hdr->sh_link, name, shindex));
1826 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1829 /* For some incomprehensible reason Oracle distributes
1830 libraries for Solaris in which some of the objects have
1831 bogus sh_link fields. It would be nice if we could just
1832 reject them, but, unfortunately, some people need to use
1833 them. We scan through the section headers; if we find only
1834 one suitable symbol table, we clobber the sh_link to point
1835 to it. I hope this doesn't break anything. */
1836 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1837 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1839 unsigned int scan;
1840 int found;
1842 found = 0;
1843 for (scan = 1; scan < num_sec; scan++)
1845 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1846 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1848 if (found != 0)
1850 found = 0;
1851 break;
1853 found = scan;
1856 if (found != 0)
1857 hdr->sh_link = found;
1860 /* Get the symbol table. */
1861 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1862 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1863 return FALSE;
1865 /* If this reloc section does not use the main symbol table we
1866 don't treat it as a reloc section. BFD can't adequately
1867 represent such a section, so at least for now, we don't
1868 try. We just present it as a normal section. We also
1869 can't use it as a reloc section if it points to the null
1870 section. */
1871 if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
1872 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1874 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1875 return FALSE;
1876 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1877 if (target_sect == NULL)
1878 return FALSE;
1880 if ((target_sect->flags & SEC_RELOC) == 0
1881 || target_sect->reloc_count == 0)
1882 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1883 else
1885 bfd_size_type amt;
1886 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1887 amt = sizeof (*hdr2);
1888 hdr2 = bfd_alloc (abfd, amt);
1889 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1891 *hdr2 = *hdr;
1892 elf_elfsections (abfd)[shindex] = hdr2;
1893 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1894 target_sect->flags |= SEC_RELOC;
1895 target_sect->relocation = NULL;
1896 target_sect->rel_filepos = hdr->sh_offset;
1897 /* In the section to which the relocations apply, mark whether
1898 its relocations are of the REL or RELA variety. */
1899 if (hdr->sh_size != 0)
1900 target_sect->use_rela_p = hdr->sh_type == SHT_RELA;
1901 abfd->flags |= HAS_RELOC;
1902 return TRUE;
1904 break;
1906 case SHT_GNU_verdef:
1907 elf_dynverdef (abfd) = shindex;
1908 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1909 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1910 break;
1912 case SHT_GNU_versym:
1913 elf_dynversym (abfd) = shindex;
1914 elf_tdata (abfd)->dynversym_hdr = *hdr;
1915 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1916 break;
1918 case SHT_GNU_verneed:
1919 elf_dynverref (abfd) = shindex;
1920 elf_tdata (abfd)->dynverref_hdr = *hdr;
1921 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1922 break;
1924 case SHT_SHLIB:
1925 return TRUE;
1927 case SHT_GROUP:
1928 /* We need a BFD section for objcopy and relocatable linking,
1929 and it's handy to have the signature available as the section
1930 name. */
1931 name = group_signature (abfd, hdr);
1932 if (name == NULL)
1933 return FALSE;
1934 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name))
1935 return FALSE;
1936 if (hdr->contents != NULL)
1938 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1939 unsigned int n_elt = hdr->sh_size / 4;
1940 asection *s;
1942 if (idx->flags & GRP_COMDAT)
1943 hdr->bfd_section->flags
1944 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1946 while (--n_elt != 0)
1947 if ((s = (++idx)->shdr->bfd_section) != NULL
1948 && elf_next_in_group (s) != NULL)
1950 elf_next_in_group (hdr->bfd_section) = s;
1951 break;
1954 break;
1956 default:
1957 /* Check for any processor-specific section types. */
1959 if (bed->elf_backend_section_from_shdr)
1960 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1962 break;
1965 return TRUE;
1968 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1969 Return SEC for sections that have no elf section, and NULL on error. */
1971 asection *
1972 bfd_section_from_r_symndx (bfd *abfd,
1973 struct sym_sec_cache *cache,
1974 asection *sec,
1975 unsigned long r_symndx)
1977 Elf_Internal_Shdr *symtab_hdr;
1978 unsigned char esym[sizeof (Elf64_External_Sym)];
1979 Elf_External_Sym_Shndx eshndx;
1980 Elf_Internal_Sym isym;
1981 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
1983 if (cache->abfd == abfd && cache->indx[ent] == r_symndx)
1984 return cache->sec[ent];
1986 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1987 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
1988 &isym, esym, &eshndx) == NULL)
1989 return NULL;
1991 if (cache->abfd != abfd)
1993 memset (cache->indx, -1, sizeof (cache->indx));
1994 cache->abfd = abfd;
1996 cache->indx[ent] = r_symndx;
1997 cache->sec[ent] = sec;
1998 if ((isym.st_shndx != SHN_UNDEF && isym.st_shndx < SHN_LORESERVE)
1999 || isym.st_shndx > SHN_HIRESERVE)
2001 asection *s;
2002 s = bfd_section_from_elf_index (abfd, isym.st_shndx);
2003 if (s != NULL)
2004 cache->sec[ent] = s;
2006 return cache->sec[ent];
2009 /* Given an ELF section number, retrieve the corresponding BFD
2010 section. */
2012 asection *
2013 bfd_section_from_elf_index (bfd *abfd, unsigned int index)
2015 if (index >= elf_numsections (abfd))
2016 return NULL;
2017 return elf_elfsections (abfd)[index]->bfd_section;
2020 static struct bfd_elf_special_section const special_sections[] =
2022 { ".bss", 0, NULL, 0,
2023 SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2024 { ".comment", 0, NULL, 0,
2025 SHT_PROGBITS, 0 },
2026 { ".data", 0, NULL, 0,
2027 SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2028 { ".data1", 0, NULL, 0,
2029 SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2030 { ".debug", 0, NULL, 0,
2031 SHT_PROGBITS, 0 },
2032 { ".fini", 0, NULL, 0,
2033 SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2034 { ".init", 0, NULL, 0,
2035 SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2036 { ".line", 0, NULL, 0,
2037 SHT_PROGBITS, 0 },
2038 { ".rodata", 0, NULL, 0,
2039 SHT_PROGBITS, SHF_ALLOC },
2040 { ".rodata1", 0, NULL, 0,
2041 SHT_PROGBITS, SHF_ALLOC },
2042 { ".tbss", 0, NULL, 0,
2043 SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2044 { ".tdata", 0, NULL, 0,
2045 SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2046 { ".text", 0, NULL, 0,
2047 SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2048 { ".init_array", 0, NULL, 0,
2049 SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2050 { ".fini_array", 0, NULL, 0,
2051 SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2052 { ".preinit_array", 0, NULL, 0,
2053 SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2054 { ".debug_line", 0, NULL, 0,
2055 SHT_PROGBITS, 0 },
2056 { ".debug_info", 0, NULL, 0,
2057 SHT_PROGBITS, 0 },
2058 { ".debug_abbrev", 0, NULL, 0,
2059 SHT_PROGBITS, 0 },
2060 { ".debug_aranges", 0, NULL, 0,
2061 SHT_PROGBITS, 0 },
2062 { ".dynamic", 0, NULL, 0,
2063 SHT_DYNAMIC, SHF_ALLOC },
2064 { ".dynstr", 0, NULL, 0,
2065 SHT_STRTAB, SHF_ALLOC },
2066 { ".dynsym", 0, NULL, 0,
2067 SHT_DYNSYM, SHF_ALLOC },
2068 { ".got", 0, NULL, 0,
2069 SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2070 { ".hash", 0, NULL, 0,
2071 SHT_HASH, SHF_ALLOC },
2072 { ".interp", 0, NULL, 0,
2073 SHT_PROGBITS, 0 },
2074 { ".plt", 0, NULL, 0,
2075 SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2076 { ".shstrtab", 0, NULL, 0,
2077 SHT_STRTAB, 0 },
2078 { ".strtab", 0, NULL, 0,
2079 SHT_STRTAB, 0 },
2080 { ".symtab", 0, NULL, 0,
2081 SHT_SYMTAB, 0 },
2082 { ".gnu.version", 0, NULL, 0,
2083 SHT_GNU_versym, 0 },
2084 { ".gnu.version_d", 0, NULL, 0,
2085 SHT_GNU_verdef, 0 },
2086 { ".gnu.version_r", 0, NULL, 0,
2087 SHT_GNU_verneed, 0 },
2088 { ".note", 5, NULL, 0,
2089 SHT_NOTE, 0 },
2090 { ".rela", 5, NULL, 0,
2091 SHT_RELA, 0 },
2092 { ".rel", 4, NULL, 0,
2093 SHT_REL, 0 },
2094 { ".stab", 5, "str", 3,
2095 SHT_STRTAB, 0 },
2096 { NULL, 0, NULL, 0,
2097 0, 0 }
2100 static const struct bfd_elf_special_section *
2101 get_special_section (const char *name,
2102 const struct bfd_elf_special_section *special_sections,
2103 unsigned int rela)
2105 int i;
2107 for (i = 0; special_sections[i].prefix != NULL; i++)
2108 if (((special_sections[i].prefix_length
2109 && strncmp (name, special_sections[i].prefix,
2110 special_sections[i].prefix_length) == 0
2111 && (! special_sections[i].suffix_length
2112 || strcmp ((name + strlen (name)
2113 - special_sections[i].suffix_length),
2114 special_sections[i].suffix) == 0))
2115 || strcmp (name, special_sections[i].prefix) == 0)
2116 && (rela || special_sections[i].type != SHT_RELA))
2117 return &special_sections[i];
2119 return NULL;
2122 bfd_boolean
2123 _bfd_elf_get_sec_type_attr (bfd *abfd, const char *name, int *type, int *attr)
2125 bfd_boolean found = FALSE;
2126 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2128 /* See if this is one of the special sections. */
2129 if (name)
2131 const struct bfd_elf_special_section *ssect = NULL;
2132 unsigned int rela = get_elf_backend_data (abfd)->default_use_rela_p;
2134 if (bed->special_sections)
2135 ssect = get_special_section (name, bed->special_sections, rela);
2137 if (! ssect)
2138 ssect = get_special_section (name, special_sections, rela);
2140 if (ssect)
2142 *type = ssect->type;
2143 *attr = ssect->attributes;
2144 found = TRUE;
2148 return found;
2151 bfd_boolean
2152 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2154 struct bfd_elf_section_data *sdata;
2155 int type, attr;
2157 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2158 if (sdata == NULL)
2160 sdata = bfd_zalloc (abfd, sizeof (*sdata));
2161 if (sdata == NULL)
2162 return FALSE;
2163 sec->used_by_bfd = sdata;
2166 elf_section_type (sec) = SHT_NULL;
2167 if (sec->name && _bfd_elf_get_sec_type_attr (abfd, sec->name,
2168 &type, &attr))
2170 elf_section_type (sec) = type;
2171 elf_section_flags (sec) = attr;
2174 /* Indicate whether or not this section should use RELA relocations. */
2175 sec->use_rela_p = get_elf_backend_data (abfd)->default_use_rela_p;
2177 return TRUE;
2180 /* Create a new bfd section from an ELF program header.
2182 Since program segments have no names, we generate a synthetic name
2183 of the form segment<NUM>, where NUM is generally the index in the
2184 program header table. For segments that are split (see below) we
2185 generate the names segment<NUM>a and segment<NUM>b.
2187 Note that some program segments may have a file size that is different than
2188 (less than) the memory size. All this means is that at execution the
2189 system must allocate the amount of memory specified by the memory size,
2190 but only initialize it with the first "file size" bytes read from the
2191 file. This would occur for example, with program segments consisting
2192 of combined data+bss.
2194 To handle the above situation, this routine generates TWO bfd sections
2195 for the single program segment. The first has the length specified by
2196 the file size of the segment, and the second has the length specified
2197 by the difference between the two sizes. In effect, the segment is split
2198 into it's initialized and uninitialized parts.
2202 bfd_boolean
2203 _bfd_elf_make_section_from_phdr (bfd *abfd,
2204 Elf_Internal_Phdr *hdr,
2205 int index,
2206 const char *typename)
2208 asection *newsect;
2209 char *name;
2210 char namebuf[64];
2211 size_t len;
2212 int split;
2214 split = ((hdr->p_memsz > 0)
2215 && (hdr->p_filesz > 0)
2216 && (hdr->p_memsz > hdr->p_filesz));
2217 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
2218 len = strlen (namebuf) + 1;
2219 name = bfd_alloc (abfd, len);
2220 if (!name)
2221 return FALSE;
2222 memcpy (name, namebuf, len);
2223 newsect = bfd_make_section (abfd, name);
2224 if (newsect == NULL)
2225 return FALSE;
2226 newsect->vma = hdr->p_vaddr;
2227 newsect->lma = hdr->p_paddr;
2228 newsect->_raw_size = hdr->p_filesz;
2229 newsect->filepos = hdr->p_offset;
2230 newsect->flags |= SEC_HAS_CONTENTS;
2231 if (hdr->p_type == PT_LOAD)
2233 newsect->flags |= SEC_ALLOC;
2234 newsect->flags |= SEC_LOAD;
2235 if (hdr->p_flags & PF_X)
2237 /* FIXME: all we known is that it has execute PERMISSION,
2238 may be data. */
2239 newsect->flags |= SEC_CODE;
2242 if (!(hdr->p_flags & PF_W))
2244 newsect->flags |= SEC_READONLY;
2247 if (split)
2249 sprintf (namebuf, "%s%db", typename, index);
2250 len = strlen (namebuf) + 1;
2251 name = bfd_alloc (abfd, len);
2252 if (!name)
2253 return FALSE;
2254 memcpy (name, namebuf, len);
2255 newsect = bfd_make_section (abfd, name);
2256 if (newsect == NULL)
2257 return FALSE;
2258 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2259 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2260 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
2261 if (hdr->p_type == PT_LOAD)
2263 newsect->flags |= SEC_ALLOC;
2264 if (hdr->p_flags & PF_X)
2265 newsect->flags |= SEC_CODE;
2267 if (!(hdr->p_flags & PF_W))
2268 newsect->flags |= SEC_READONLY;
2271 return TRUE;
2274 bfd_boolean
2275 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index)
2277 struct elf_backend_data *bed;
2279 switch (hdr->p_type)
2281 case PT_NULL:
2282 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2284 case PT_LOAD:
2285 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2287 case PT_DYNAMIC:
2288 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2290 case PT_INTERP:
2291 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2293 case PT_NOTE:
2294 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2295 return FALSE;
2296 if (! elfcore_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2297 return FALSE;
2298 return TRUE;
2300 case PT_SHLIB:
2301 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2303 case PT_PHDR:
2304 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2306 case PT_GNU_EH_FRAME:
2307 return _bfd_elf_make_section_from_phdr (abfd, hdr, index,
2308 "eh_frame_hdr");
2310 case PT_GNU_STACK:
2311 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "stack");
2313 default:
2314 /* Check for any processor-specific program segment types.
2315 If no handler for them, default to making "segment" sections. */
2316 bed = get_elf_backend_data (abfd);
2317 if (bed->elf_backend_section_from_phdr)
2318 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
2319 else
2320 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
2324 /* Initialize REL_HDR, the section-header for new section, containing
2325 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2326 relocations; otherwise, we use REL relocations. */
2328 bfd_boolean
2329 _bfd_elf_init_reloc_shdr (bfd *abfd,
2330 Elf_Internal_Shdr *rel_hdr,
2331 asection *asect,
2332 bfd_boolean use_rela_p)
2334 char *name;
2335 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2336 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2338 name = bfd_alloc (abfd, amt);
2339 if (name == NULL)
2340 return FALSE;
2341 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2342 rel_hdr->sh_name =
2343 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2344 FALSE);
2345 if (rel_hdr->sh_name == (unsigned int) -1)
2346 return FALSE;
2347 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2348 rel_hdr->sh_entsize = (use_rela_p
2349 ? bed->s->sizeof_rela
2350 : bed->s->sizeof_rel);
2351 rel_hdr->sh_addralign = 1 << bed->s->log_file_align;
2352 rel_hdr->sh_flags = 0;
2353 rel_hdr->sh_addr = 0;
2354 rel_hdr->sh_size = 0;
2355 rel_hdr->sh_offset = 0;
2357 return TRUE;
2360 /* Set up an ELF internal section header for a section. */
2362 static void
2363 elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg)
2365 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2366 bfd_boolean *failedptr = failedptrarg;
2367 Elf_Internal_Shdr *this_hdr;
2369 if (*failedptr)
2371 /* We already failed; just get out of the bfd_map_over_sections
2372 loop. */
2373 return;
2376 this_hdr = &elf_section_data (asect)->this_hdr;
2378 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2379 asect->name, FALSE);
2380 if (this_hdr->sh_name == (unsigned int) -1)
2382 *failedptr = TRUE;
2383 return;
2386 this_hdr->sh_flags = 0;
2388 if ((asect->flags & SEC_ALLOC) != 0
2389 || asect->user_set_vma)
2390 this_hdr->sh_addr = asect->vma;
2391 else
2392 this_hdr->sh_addr = 0;
2394 this_hdr->sh_offset = 0;
2395 this_hdr->sh_size = asect->_raw_size;
2396 this_hdr->sh_link = 0;
2397 this_hdr->sh_addralign = 1 << asect->alignment_power;
2398 /* The sh_entsize and sh_info fields may have been set already by
2399 copy_private_section_data. */
2401 this_hdr->bfd_section = asect;
2402 this_hdr->contents = NULL;
2404 /* If the section type is unspecified, we set it based on
2405 asect->flags. */
2406 if (this_hdr->sh_type == SHT_NULL)
2408 if ((asect->flags & SEC_ALLOC) != 0
2409 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2410 || (asect->flags & SEC_NEVER_LOAD) != 0))
2411 this_hdr->sh_type = SHT_NOBITS;
2412 else
2413 this_hdr->sh_type = SHT_PROGBITS;
2416 switch (this_hdr->sh_type)
2418 default:
2419 break;
2421 case SHT_STRTAB:
2422 case SHT_INIT_ARRAY:
2423 case SHT_FINI_ARRAY:
2424 case SHT_PREINIT_ARRAY:
2425 case SHT_NOTE:
2426 case SHT_NOBITS:
2427 case SHT_PROGBITS:
2428 break;
2430 case SHT_HASH:
2431 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2432 break;
2434 case SHT_DYNSYM:
2435 this_hdr->sh_entsize = bed->s->sizeof_sym;
2436 break;
2438 case SHT_DYNAMIC:
2439 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2440 break;
2442 case SHT_RELA:
2443 if (get_elf_backend_data (abfd)->may_use_rela_p)
2444 this_hdr->sh_entsize = bed->s->sizeof_rela;
2445 break;
2447 case SHT_REL:
2448 if (get_elf_backend_data (abfd)->may_use_rel_p)
2449 this_hdr->sh_entsize = bed->s->sizeof_rel;
2450 break;
2452 case SHT_GNU_versym:
2453 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2454 break;
2456 case SHT_GNU_verdef:
2457 this_hdr->sh_entsize = 0;
2458 /* objcopy or strip will copy over sh_info, but may not set
2459 cverdefs. The linker will set cverdefs, but sh_info will be
2460 zero. */
2461 if (this_hdr->sh_info == 0)
2462 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2463 else
2464 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2465 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2466 break;
2468 case SHT_GNU_verneed:
2469 this_hdr->sh_entsize = 0;
2470 /* objcopy or strip will copy over sh_info, but may not set
2471 cverrefs. The linker will set cverrefs, but sh_info will be
2472 zero. */
2473 if (this_hdr->sh_info == 0)
2474 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2475 else
2476 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2477 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2478 break;
2480 case SHT_GROUP:
2481 this_hdr->sh_entsize = 4;
2482 break;
2485 if ((asect->flags & SEC_ALLOC) != 0)
2486 this_hdr->sh_flags |= SHF_ALLOC;
2487 if ((asect->flags & SEC_READONLY) == 0)
2488 this_hdr->sh_flags |= SHF_WRITE;
2489 if ((asect->flags & SEC_CODE) != 0)
2490 this_hdr->sh_flags |= SHF_EXECINSTR;
2491 if ((asect->flags & SEC_MERGE) != 0)
2493 this_hdr->sh_flags |= SHF_MERGE;
2494 this_hdr->sh_entsize = asect->entsize;
2495 if ((asect->flags & SEC_STRINGS) != 0)
2496 this_hdr->sh_flags |= SHF_STRINGS;
2498 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2499 this_hdr->sh_flags |= SHF_GROUP;
2500 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2502 this_hdr->sh_flags |= SHF_TLS;
2503 if (asect->_raw_size == 0 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2505 struct bfd_link_order *o;
2507 this_hdr->sh_size = 0;
2508 for (o = asect->link_order_head; o != NULL; o = o->next)
2509 if (this_hdr->sh_size < o->offset + o->size)
2510 this_hdr->sh_size = o->offset + o->size;
2511 if (this_hdr->sh_size)
2512 this_hdr->sh_type = SHT_NOBITS;
2516 /* Check for processor-specific section types. */
2517 if (bed->elf_backend_fake_sections
2518 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2519 *failedptr = TRUE;
2521 /* If the section has relocs, set up a section header for the
2522 SHT_REL[A] section. If two relocation sections are required for
2523 this section, it is up to the processor-specific back-end to
2524 create the other. */
2525 if ((asect->flags & SEC_RELOC) != 0
2526 && !_bfd_elf_init_reloc_shdr (abfd,
2527 &elf_section_data (asect)->rel_hdr,
2528 asect,
2529 asect->use_rela_p))
2530 *failedptr = TRUE;
2533 /* Fill in the contents of a SHT_GROUP section. */
2535 void
2536 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2538 bfd_boolean *failedptr = failedptrarg;
2539 unsigned long symindx;
2540 asection *elt, *first;
2541 unsigned char *loc;
2542 struct bfd_link_order *l;
2543 bfd_boolean gas;
2545 if (elf_section_data (sec)->this_hdr.sh_type != SHT_GROUP
2546 || *failedptr)
2547 return;
2549 symindx = 0;
2550 if (elf_group_id (sec) != NULL)
2551 symindx = elf_group_id (sec)->udata.i;
2553 if (symindx == 0)
2555 /* If called from the assembler, swap_out_syms will have set up
2556 elf_section_syms; If called for "ld -r", use target_index. */
2557 if (elf_section_syms (abfd) != NULL)
2558 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2559 else
2560 symindx = sec->target_index;
2562 elf_section_data (sec)->this_hdr.sh_info = symindx;
2564 /* The contents won't be allocated for "ld -r" or objcopy. */
2565 gas = TRUE;
2566 if (sec->contents == NULL)
2568 gas = FALSE;
2569 sec->contents = bfd_alloc (abfd, sec->_raw_size);
2571 /* Arrange for the section to be written out. */
2572 elf_section_data (sec)->this_hdr.contents = sec->contents;
2573 if (sec->contents == NULL)
2575 *failedptr = TRUE;
2576 return;
2580 loc = sec->contents + sec->_raw_size;
2582 /* Get the pointer to the first section in the group that gas
2583 squirreled away here. objcopy arranges for this to be set to the
2584 start of the input section group. */
2585 first = elt = elf_next_in_group (sec);
2587 /* First element is a flag word. Rest of section is elf section
2588 indices for all the sections of the group. Write them backwards
2589 just to keep the group in the same order as given in .section
2590 directives, not that it matters. */
2591 while (elt != NULL)
2593 asection *s;
2594 unsigned int idx;
2596 loc -= 4;
2597 s = elt;
2598 if (!gas)
2599 s = s->output_section;
2600 idx = 0;
2601 if (s != NULL)
2602 idx = elf_section_data (s)->this_idx;
2603 H_PUT_32 (abfd, idx, loc);
2604 elt = elf_next_in_group (elt);
2605 if (elt == first)
2606 break;
2609 /* If this is a relocatable link, then the above did nothing because
2610 SEC is the output section. Look through the input sections
2611 instead. */
2612 for (l = sec->link_order_head; l != NULL; l = l->next)
2613 if (l->type == bfd_indirect_link_order
2614 && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
2617 loc -= 4;
2618 H_PUT_32 (abfd,
2619 elf_section_data (elt->output_section)->this_idx, loc);
2620 elt = elf_next_in_group (elt);
2621 /* During a relocatable link, the lists are circular. */
2623 while (elt != elf_next_in_group (l->u.indirect.section));
2625 /* With ld -r, merging SHT_GROUP sections results in wasted space
2626 due to allowing for the flag word on each input. We may well
2627 duplicate entries too. */
2628 while ((loc -= 4) > sec->contents)
2629 H_PUT_32 (abfd, 0, loc);
2631 if (loc != sec->contents)
2632 abort ();
2634 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2637 /* Assign all ELF section numbers. The dummy first section is handled here
2638 too. The link/info pointers for the standard section types are filled
2639 in here too, while we're at it. */
2641 static bfd_boolean
2642 assign_section_numbers (bfd *abfd)
2644 struct elf_obj_tdata *t = elf_tdata (abfd);
2645 asection *sec;
2646 unsigned int section_number, secn;
2647 Elf_Internal_Shdr **i_shdrp;
2648 bfd_size_type amt;
2650 section_number = 1;
2652 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2654 for (sec = abfd->sections; sec; sec = sec->next)
2656 struct bfd_elf_section_data *d = elf_section_data (sec);
2658 if (section_number == SHN_LORESERVE)
2659 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2660 d->this_idx = section_number++;
2661 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2662 if ((sec->flags & SEC_RELOC) == 0)
2663 d->rel_idx = 0;
2664 else
2666 if (section_number == SHN_LORESERVE)
2667 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2668 d->rel_idx = section_number++;
2669 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2672 if (d->rel_hdr2)
2674 if (section_number == SHN_LORESERVE)
2675 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2676 d->rel_idx2 = section_number++;
2677 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2679 else
2680 d->rel_idx2 = 0;
2683 if (section_number == SHN_LORESERVE)
2684 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2685 t->shstrtab_section = section_number++;
2686 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2687 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2689 if (bfd_get_symcount (abfd) > 0)
2691 if (section_number == SHN_LORESERVE)
2692 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2693 t->symtab_section = section_number++;
2694 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2695 if (section_number > SHN_LORESERVE - 2)
2697 if (section_number == SHN_LORESERVE)
2698 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2699 t->symtab_shndx_section = section_number++;
2700 t->symtab_shndx_hdr.sh_name
2701 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2702 ".symtab_shndx", FALSE);
2703 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2704 return FALSE;
2706 if (section_number == SHN_LORESERVE)
2707 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2708 t->strtab_section = section_number++;
2709 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2712 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2713 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2715 elf_numsections (abfd) = section_number;
2716 elf_elfheader (abfd)->e_shnum = section_number;
2717 if (section_number > SHN_LORESERVE)
2718 elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE;
2720 /* Set up the list of section header pointers, in agreement with the
2721 indices. */
2722 amt = section_number * sizeof (Elf_Internal_Shdr *);
2723 i_shdrp = bfd_zalloc (abfd, amt);
2724 if (i_shdrp == NULL)
2725 return FALSE;
2727 amt = sizeof (Elf_Internal_Shdr);
2728 i_shdrp[0] = bfd_zalloc (abfd, amt);
2729 if (i_shdrp[0] == NULL)
2731 bfd_release (abfd, i_shdrp);
2732 return FALSE;
2735 elf_elfsections (abfd) = i_shdrp;
2737 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2738 if (bfd_get_symcount (abfd) > 0)
2740 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2741 if (elf_numsections (abfd) > SHN_LORESERVE)
2743 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2744 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2746 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2747 t->symtab_hdr.sh_link = t->strtab_section;
2749 for (sec = abfd->sections; sec; sec = sec->next)
2751 struct bfd_elf_section_data *d = elf_section_data (sec);
2752 asection *s;
2753 const char *name;
2755 i_shdrp[d->this_idx] = &d->this_hdr;
2756 if (d->rel_idx != 0)
2757 i_shdrp[d->rel_idx] = &d->rel_hdr;
2758 if (d->rel_idx2 != 0)
2759 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2761 /* Fill in the sh_link and sh_info fields while we're at it. */
2763 /* sh_link of a reloc section is the section index of the symbol
2764 table. sh_info is the section index of the section to which
2765 the relocation entries apply. */
2766 if (d->rel_idx != 0)
2768 d->rel_hdr.sh_link = t->symtab_section;
2769 d->rel_hdr.sh_info = d->this_idx;
2771 if (d->rel_idx2 != 0)
2773 d->rel_hdr2->sh_link = t->symtab_section;
2774 d->rel_hdr2->sh_info = d->this_idx;
2777 switch (d->this_hdr.sh_type)
2779 case SHT_REL:
2780 case SHT_RELA:
2781 /* A reloc section which we are treating as a normal BFD
2782 section. sh_link is the section index of the symbol
2783 table. sh_info is the section index of the section to
2784 which the relocation entries apply. We assume that an
2785 allocated reloc section uses the dynamic symbol table.
2786 FIXME: How can we be sure? */
2787 s = bfd_get_section_by_name (abfd, ".dynsym");
2788 if (s != NULL)
2789 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2791 /* We look up the section the relocs apply to by name. */
2792 name = sec->name;
2793 if (d->this_hdr.sh_type == SHT_REL)
2794 name += 4;
2795 else
2796 name += 5;
2797 s = bfd_get_section_by_name (abfd, name);
2798 if (s != NULL)
2799 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
2800 break;
2802 case SHT_STRTAB:
2803 /* We assume that a section named .stab*str is a stabs
2804 string section. We look for a section with the same name
2805 but without the trailing ``str'', and set its sh_link
2806 field to point to this section. */
2807 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
2808 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
2810 size_t len;
2811 char *alc;
2813 len = strlen (sec->name);
2814 alc = bfd_malloc (len - 2);
2815 if (alc == NULL)
2816 return FALSE;
2817 memcpy (alc, sec->name, len - 3);
2818 alc[len - 3] = '\0';
2819 s = bfd_get_section_by_name (abfd, alc);
2820 free (alc);
2821 if (s != NULL)
2823 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
2825 /* This is a .stab section. */
2826 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
2827 elf_section_data (s)->this_hdr.sh_entsize
2828 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
2831 break;
2833 case SHT_DYNAMIC:
2834 case SHT_DYNSYM:
2835 case SHT_GNU_verneed:
2836 case SHT_GNU_verdef:
2837 /* sh_link is the section header index of the string table
2838 used for the dynamic entries, or the symbol table, or the
2839 version strings. */
2840 s = bfd_get_section_by_name (abfd, ".dynstr");
2841 if (s != NULL)
2842 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2843 break;
2845 case SHT_HASH:
2846 case SHT_GNU_versym:
2847 /* sh_link is the section header index of the symbol table
2848 this hash table or version table is for. */
2849 s = bfd_get_section_by_name (abfd, ".dynsym");
2850 if (s != NULL)
2851 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2852 break;
2854 case SHT_GROUP:
2855 d->this_hdr.sh_link = t->symtab_section;
2859 for (secn = 1; secn < section_number; ++secn)
2860 if (i_shdrp[secn] == NULL)
2861 i_shdrp[secn] = i_shdrp[0];
2862 else
2863 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
2864 i_shdrp[secn]->sh_name);
2865 return TRUE;
2868 /* Map symbol from it's internal number to the external number, moving
2869 all local symbols to be at the head of the list. */
2871 static int
2872 sym_is_global (bfd *abfd, asymbol *sym)
2874 /* If the backend has a special mapping, use it. */
2875 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2876 if (bed->elf_backend_sym_is_global)
2877 return (*bed->elf_backend_sym_is_global) (abfd, sym);
2879 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2880 || bfd_is_und_section (bfd_get_section (sym))
2881 || bfd_is_com_section (bfd_get_section (sym)));
2884 static bfd_boolean
2885 elf_map_symbols (bfd *abfd)
2887 unsigned int symcount = bfd_get_symcount (abfd);
2888 asymbol **syms = bfd_get_outsymbols (abfd);
2889 asymbol **sect_syms;
2890 unsigned int num_locals = 0;
2891 unsigned int num_globals = 0;
2892 unsigned int num_locals2 = 0;
2893 unsigned int num_globals2 = 0;
2894 int max_index = 0;
2895 unsigned int idx;
2896 asection *asect;
2897 asymbol **new_syms;
2898 bfd_size_type amt;
2900 #ifdef DEBUG
2901 fprintf (stderr, "elf_map_symbols\n");
2902 fflush (stderr);
2903 #endif
2905 for (asect = abfd->sections; asect; asect = asect->next)
2907 if (max_index < asect->index)
2908 max_index = asect->index;
2911 max_index++;
2912 amt = max_index * sizeof (asymbol *);
2913 sect_syms = bfd_zalloc (abfd, amt);
2914 if (sect_syms == NULL)
2915 return FALSE;
2916 elf_section_syms (abfd) = sect_syms;
2917 elf_num_section_syms (abfd) = max_index;
2919 /* Init sect_syms entries for any section symbols we have already
2920 decided to output. */
2921 for (idx = 0; idx < symcount; idx++)
2923 asymbol *sym = syms[idx];
2925 if ((sym->flags & BSF_SECTION_SYM) != 0
2926 && sym->value == 0)
2928 asection *sec;
2930 sec = sym->section;
2932 if (sec->owner != NULL)
2934 if (sec->owner != abfd)
2936 if (sec->output_offset != 0)
2937 continue;
2939 sec = sec->output_section;
2941 /* Empty sections in the input files may have had a
2942 section symbol created for them. (See the comment
2943 near the end of _bfd_generic_link_output_symbols in
2944 linker.c). If the linker script discards such
2945 sections then we will reach this point. Since we know
2946 that we cannot avoid this case, we detect it and skip
2947 the abort and the assignment to the sect_syms array.
2948 To reproduce this particular case try running the
2949 linker testsuite test ld-scripts/weak.exp for an ELF
2950 port that uses the generic linker. */
2951 if (sec->owner == NULL)
2952 continue;
2954 BFD_ASSERT (sec->owner == abfd);
2956 sect_syms[sec->index] = syms[idx];
2961 /* Classify all of the symbols. */
2962 for (idx = 0; idx < symcount; idx++)
2964 if (!sym_is_global (abfd, syms[idx]))
2965 num_locals++;
2966 else
2967 num_globals++;
2970 /* We will be adding a section symbol for each BFD section. Most normal
2971 sections will already have a section symbol in outsymbols, but
2972 eg. SHT_GROUP sections will not, and we need the section symbol mapped
2973 at least in that case. */
2974 for (asect = abfd->sections; asect; asect = asect->next)
2976 if (sect_syms[asect->index] == NULL)
2978 if (!sym_is_global (abfd, asect->symbol))
2979 num_locals++;
2980 else
2981 num_globals++;
2985 /* Now sort the symbols so the local symbols are first. */
2986 amt = (num_locals + num_globals) * sizeof (asymbol *);
2987 new_syms = bfd_alloc (abfd, amt);
2989 if (new_syms == NULL)
2990 return FALSE;
2992 for (idx = 0; idx < symcount; idx++)
2994 asymbol *sym = syms[idx];
2995 unsigned int i;
2997 if (!sym_is_global (abfd, sym))
2998 i = num_locals2++;
2999 else
3000 i = num_locals + num_globals2++;
3001 new_syms[i] = sym;
3002 sym->udata.i = i + 1;
3004 for (asect = abfd->sections; asect; asect = asect->next)
3006 if (sect_syms[asect->index] == NULL)
3008 asymbol *sym = asect->symbol;
3009 unsigned int i;
3011 sect_syms[asect->index] = sym;
3012 if (!sym_is_global (abfd, sym))
3013 i = num_locals2++;
3014 else
3015 i = num_locals + num_globals2++;
3016 new_syms[i] = sym;
3017 sym->udata.i = i + 1;
3021 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3023 elf_num_locals (abfd) = num_locals;
3024 elf_num_globals (abfd) = num_globals;
3025 return TRUE;
3028 /* Align to the maximum file alignment that could be required for any
3029 ELF data structure. */
3031 static inline file_ptr
3032 align_file_position (file_ptr off, int align)
3034 return (off + align - 1) & ~(align - 1);
3037 /* Assign a file position to a section, optionally aligning to the
3038 required section alignment. */
3040 file_ptr
3041 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3042 file_ptr offset,
3043 bfd_boolean align)
3045 if (align)
3047 unsigned int al;
3049 al = i_shdrp->sh_addralign;
3050 if (al > 1)
3051 offset = BFD_ALIGN (offset, al);
3053 i_shdrp->sh_offset = offset;
3054 if (i_shdrp->bfd_section != NULL)
3055 i_shdrp->bfd_section->filepos = offset;
3056 if (i_shdrp->sh_type != SHT_NOBITS)
3057 offset += i_shdrp->sh_size;
3058 return offset;
3061 /* Compute the file positions we are going to put the sections at, and
3062 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3063 is not NULL, this is being called by the ELF backend linker. */
3065 bfd_boolean
3066 _bfd_elf_compute_section_file_positions (bfd *abfd,
3067 struct bfd_link_info *link_info)
3069 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3070 bfd_boolean failed;
3071 struct bfd_strtab_hash *strtab;
3072 Elf_Internal_Shdr *shstrtab_hdr;
3074 if (abfd->output_has_begun)
3075 return TRUE;
3077 /* Do any elf backend specific processing first. */
3078 if (bed->elf_backend_begin_write_processing)
3079 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3081 if (! prep_headers (abfd))
3082 return FALSE;
3084 /* Post process the headers if necessary. */
3085 if (bed->elf_backend_post_process_headers)
3086 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3088 failed = FALSE;
3089 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3090 if (failed)
3091 return FALSE;
3093 if (!assign_section_numbers (abfd))
3094 return FALSE;
3096 /* The backend linker builds symbol table information itself. */
3097 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3099 /* Non-zero if doing a relocatable link. */
3100 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3102 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3103 return FALSE;
3106 if (link_info == NULL)
3108 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3109 if (failed)
3110 return FALSE;
3113 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3114 /* sh_name was set in prep_headers. */
3115 shstrtab_hdr->sh_type = SHT_STRTAB;
3116 shstrtab_hdr->sh_flags = 0;
3117 shstrtab_hdr->sh_addr = 0;
3118 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3119 shstrtab_hdr->sh_entsize = 0;
3120 shstrtab_hdr->sh_link = 0;
3121 shstrtab_hdr->sh_info = 0;
3122 /* sh_offset is set in assign_file_positions_except_relocs. */
3123 shstrtab_hdr->sh_addralign = 1;
3125 if (!assign_file_positions_except_relocs (abfd))
3126 return FALSE;
3128 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3130 file_ptr off;
3131 Elf_Internal_Shdr *hdr;
3133 off = elf_tdata (abfd)->next_file_pos;
3135 hdr = &elf_tdata (abfd)->symtab_hdr;
3136 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3138 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3139 if (hdr->sh_size != 0)
3140 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3142 hdr = &elf_tdata (abfd)->strtab_hdr;
3143 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3145 elf_tdata (abfd)->next_file_pos = off;
3147 /* Now that we know where the .strtab section goes, write it
3148 out. */
3149 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3150 || ! _bfd_stringtab_emit (abfd, strtab))
3151 return FALSE;
3152 _bfd_stringtab_free (strtab);
3155 abfd->output_has_begun = TRUE;
3157 return TRUE;
3160 /* Create a mapping from a set of sections to a program segment. */
3162 static struct elf_segment_map *
3163 make_mapping (bfd *abfd,
3164 asection **sections,
3165 unsigned int from,
3166 unsigned int to,
3167 bfd_boolean phdr)
3169 struct elf_segment_map *m;
3170 unsigned int i;
3171 asection **hdrpp;
3172 bfd_size_type amt;
3174 amt = sizeof (struct elf_segment_map);
3175 amt += (to - from - 1) * sizeof (asection *);
3176 m = bfd_zalloc (abfd, amt);
3177 if (m == NULL)
3178 return NULL;
3179 m->next = NULL;
3180 m->p_type = PT_LOAD;
3181 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3182 m->sections[i - from] = *hdrpp;
3183 m->count = to - from;
3185 if (from == 0 && phdr)
3187 /* Include the headers in the first PT_LOAD segment. */
3188 m->includes_filehdr = 1;
3189 m->includes_phdrs = 1;
3192 return m;
3195 /* Set up a mapping from BFD sections to program segments. */
3197 static bfd_boolean
3198 map_sections_to_segments (bfd *abfd)
3200 asection **sections = NULL;
3201 asection *s;
3202 unsigned int i;
3203 unsigned int count;
3204 struct elf_segment_map *mfirst;
3205 struct elf_segment_map **pm;
3206 struct elf_segment_map *m;
3207 asection *last_hdr;
3208 unsigned int phdr_index;
3209 bfd_vma maxpagesize;
3210 asection **hdrpp;
3211 bfd_boolean phdr_in_segment = TRUE;
3212 bfd_boolean writable;
3213 int tls_count = 0;
3214 asection *first_tls = NULL;
3215 asection *dynsec, *eh_frame_hdr;
3216 bfd_size_type amt;
3218 if (elf_tdata (abfd)->segment_map != NULL)
3219 return TRUE;
3221 if (bfd_count_sections (abfd) == 0)
3222 return TRUE;
3224 /* Select the allocated sections, and sort them. */
3226 amt = bfd_count_sections (abfd) * sizeof (asection *);
3227 sections = bfd_malloc (amt);
3228 if (sections == NULL)
3229 goto error_return;
3231 i = 0;
3232 for (s = abfd->sections; s != NULL; s = s->next)
3234 if ((s->flags & SEC_ALLOC) != 0)
3236 sections[i] = s;
3237 ++i;
3240 BFD_ASSERT (i <= bfd_count_sections (abfd));
3241 count = i;
3243 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3245 /* Build the mapping. */
3247 mfirst = NULL;
3248 pm = &mfirst;
3250 /* If we have a .interp section, then create a PT_PHDR segment for
3251 the program headers and a PT_INTERP segment for the .interp
3252 section. */
3253 s = bfd_get_section_by_name (abfd, ".interp");
3254 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3256 amt = sizeof (struct elf_segment_map);
3257 m = bfd_zalloc (abfd, amt);
3258 if (m == NULL)
3259 goto error_return;
3260 m->next = NULL;
3261 m->p_type = PT_PHDR;
3262 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3263 m->p_flags = PF_R | PF_X;
3264 m->p_flags_valid = 1;
3265 m->includes_phdrs = 1;
3267 *pm = m;
3268 pm = &m->next;
3270 amt = sizeof (struct elf_segment_map);
3271 m = bfd_zalloc (abfd, amt);
3272 if (m == NULL)
3273 goto error_return;
3274 m->next = NULL;
3275 m->p_type = PT_INTERP;
3276 m->count = 1;
3277 m->sections[0] = s;
3279 *pm = m;
3280 pm = &m->next;
3283 /* Look through the sections. We put sections in the same program
3284 segment when the start of the second section can be placed within
3285 a few bytes of the end of the first section. */
3286 last_hdr = NULL;
3287 phdr_index = 0;
3288 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
3289 writable = FALSE;
3290 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3291 if (dynsec != NULL
3292 && (dynsec->flags & SEC_LOAD) == 0)
3293 dynsec = NULL;
3295 /* Deal with -Ttext or something similar such that the first section
3296 is not adjacent to the program headers. This is an
3297 approximation, since at this point we don't know exactly how many
3298 program headers we will need. */
3299 if (count > 0)
3301 bfd_size_type phdr_size;
3303 phdr_size = elf_tdata (abfd)->program_header_size;
3304 if (phdr_size == 0)
3305 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
3306 if ((abfd->flags & D_PAGED) == 0
3307 || sections[0]->lma < phdr_size
3308 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3309 phdr_in_segment = FALSE;
3312 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3314 asection *hdr;
3315 bfd_boolean new_segment;
3317 hdr = *hdrpp;
3319 /* See if this section and the last one will fit in the same
3320 segment. */
3322 if (last_hdr == NULL)
3324 /* If we don't have a segment yet, then we don't need a new
3325 one (we build the last one after this loop). */
3326 new_segment = FALSE;
3328 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3330 /* If this section has a different relation between the
3331 virtual address and the load address, then we need a new
3332 segment. */
3333 new_segment = TRUE;
3335 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
3336 < BFD_ALIGN (hdr->lma, maxpagesize))
3338 /* If putting this section in this segment would force us to
3339 skip a page in the segment, then we need a new segment. */
3340 new_segment = TRUE;
3342 else if ((last_hdr->flags & SEC_LOAD) == 0
3343 && (hdr->flags & SEC_LOAD) != 0)
3345 /* We don't want to put a loadable section after a
3346 nonloadable section in the same segment. */
3347 new_segment = TRUE;
3349 else if ((abfd->flags & D_PAGED) == 0)
3351 /* If the file is not demand paged, which means that we
3352 don't require the sections to be correctly aligned in the
3353 file, then there is no other reason for a new segment. */
3354 new_segment = FALSE;
3356 else if (! writable
3357 && (hdr->flags & SEC_READONLY) == 0
3358 && (((last_hdr->lma + last_hdr->_raw_size - 1)
3359 & ~(maxpagesize - 1))
3360 != (hdr->lma & ~(maxpagesize - 1))))
3362 /* We don't want to put a writable section in a read only
3363 segment, unless they are on the same page in memory
3364 anyhow. We already know that the last section does not
3365 bring us past the current section on the page, so the
3366 only case in which the new section is not on the same
3367 page as the previous section is when the previous section
3368 ends precisely on a page boundary. */
3369 new_segment = TRUE;
3371 else
3373 /* Otherwise, we can use the same segment. */
3374 new_segment = FALSE;
3377 if (! new_segment)
3379 if ((hdr->flags & SEC_READONLY) == 0)
3380 writable = TRUE;
3381 last_hdr = hdr;
3382 continue;
3385 /* We need a new program segment. We must create a new program
3386 header holding all the sections from phdr_index until hdr. */
3388 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3389 if (m == NULL)
3390 goto error_return;
3392 *pm = m;
3393 pm = &m->next;
3395 if ((hdr->flags & SEC_READONLY) == 0)
3396 writable = TRUE;
3397 else
3398 writable = FALSE;
3400 last_hdr = hdr;
3401 phdr_index = i;
3402 phdr_in_segment = FALSE;
3405 /* Create a final PT_LOAD program segment. */
3406 if (last_hdr != NULL)
3408 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3409 if (m == NULL)
3410 goto error_return;
3412 *pm = m;
3413 pm = &m->next;
3416 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3417 if (dynsec != NULL)
3419 amt = sizeof (struct elf_segment_map);
3420 m = bfd_zalloc (abfd, amt);
3421 if (m == NULL)
3422 goto error_return;
3423 m->next = NULL;
3424 m->p_type = PT_DYNAMIC;
3425 m->count = 1;
3426 m->sections[0] = dynsec;
3428 *pm = m;
3429 pm = &m->next;
3432 /* For each loadable .note section, add a PT_NOTE segment. We don't
3433 use bfd_get_section_by_name, because if we link together
3434 nonloadable .note sections and loadable .note sections, we will
3435 generate two .note sections in the output file. FIXME: Using
3436 names for section types is bogus anyhow. */
3437 for (s = abfd->sections; s != NULL; s = s->next)
3439 if ((s->flags & SEC_LOAD) != 0
3440 && strncmp (s->name, ".note", 5) == 0)
3442 amt = sizeof (struct elf_segment_map);
3443 m = bfd_zalloc (abfd, amt);
3444 if (m == NULL)
3445 goto error_return;
3446 m->next = NULL;
3447 m->p_type = PT_NOTE;
3448 m->count = 1;
3449 m->sections[0] = s;
3451 *pm = m;
3452 pm = &m->next;
3454 if (s->flags & SEC_THREAD_LOCAL)
3456 if (! tls_count)
3457 first_tls = s;
3458 tls_count++;
3462 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3463 if (tls_count > 0)
3465 int i;
3467 amt = sizeof (struct elf_segment_map);
3468 amt += (tls_count - 1) * sizeof (asection *);
3469 m = bfd_zalloc (abfd, amt);
3470 if (m == NULL)
3471 goto error_return;
3472 m->next = NULL;
3473 m->p_type = PT_TLS;
3474 m->count = tls_count;
3475 /* Mandated PF_R. */
3476 m->p_flags = PF_R;
3477 m->p_flags_valid = 1;
3478 for (i = 0; i < tls_count; ++i)
3480 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3481 m->sections[i] = first_tls;
3482 first_tls = first_tls->next;
3485 *pm = m;
3486 pm = &m->next;
3489 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3490 segment. */
3491 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
3492 if (eh_frame_hdr != NULL
3493 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
3495 amt = sizeof (struct elf_segment_map);
3496 m = bfd_zalloc (abfd, amt);
3497 if (m == NULL)
3498 goto error_return;
3499 m->next = NULL;
3500 m->p_type = PT_GNU_EH_FRAME;
3501 m->count = 1;
3502 m->sections[0] = eh_frame_hdr->output_section;
3504 *pm = m;
3505 pm = &m->next;
3508 if (elf_tdata (abfd)->stack_flags)
3510 amt = sizeof (struct elf_segment_map);
3511 m = bfd_zalloc (abfd, amt);
3512 if (m == NULL)
3513 goto error_return;
3514 m->next = NULL;
3515 m->p_type = PT_GNU_STACK;
3516 m->p_flags = elf_tdata (abfd)->stack_flags;
3517 m->p_flags_valid = 1;
3519 *pm = m;
3520 pm = &m->next;
3523 free (sections);
3524 sections = NULL;
3526 elf_tdata (abfd)->segment_map = mfirst;
3527 return TRUE;
3529 error_return:
3530 if (sections != NULL)
3531 free (sections);
3532 return FALSE;
3535 /* Sort sections by address. */
3537 static int
3538 elf_sort_sections (const void *arg1, const void *arg2)
3540 const asection *sec1 = *(const asection **) arg1;
3541 const asection *sec2 = *(const asection **) arg2;
3542 bfd_size_type size1, size2;
3544 /* Sort by LMA first, since this is the address used to
3545 place the section into a segment. */
3546 if (sec1->lma < sec2->lma)
3547 return -1;
3548 else if (sec1->lma > sec2->lma)
3549 return 1;
3551 /* Then sort by VMA. Normally the LMA and the VMA will be
3552 the same, and this will do nothing. */
3553 if (sec1->vma < sec2->vma)
3554 return -1;
3555 else if (sec1->vma > sec2->vma)
3556 return 1;
3558 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3560 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
3562 if (TOEND (sec1))
3564 if (TOEND (sec2))
3566 /* If the indicies are the same, do not return 0
3567 here, but continue to try the next comparison. */
3568 if (sec1->target_index - sec2->target_index != 0)
3569 return sec1->target_index - sec2->target_index;
3571 else
3572 return 1;
3574 else if (TOEND (sec2))
3575 return -1;
3577 #undef TOEND
3579 /* Sort by size, to put zero sized sections
3580 before others at the same address. */
3582 size1 = (sec1->flags & SEC_LOAD) ? sec1->_raw_size : 0;
3583 size2 = (sec2->flags & SEC_LOAD) ? sec2->_raw_size : 0;
3585 if (size1 < size2)
3586 return -1;
3587 if (size1 > size2)
3588 return 1;
3590 return sec1->target_index - sec2->target_index;
3593 /* Assign file positions to the sections based on the mapping from
3594 sections to segments. This function also sets up some fields in
3595 the file header, and writes out the program headers. */
3597 static bfd_boolean
3598 assign_file_positions_for_segments (bfd *abfd)
3600 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3601 unsigned int count;
3602 struct elf_segment_map *m;
3603 unsigned int alloc;
3604 Elf_Internal_Phdr *phdrs;
3605 file_ptr off, voff;
3606 bfd_vma filehdr_vaddr, filehdr_paddr;
3607 bfd_vma phdrs_vaddr, phdrs_paddr;
3608 Elf_Internal_Phdr *p;
3609 bfd_size_type amt;
3611 if (elf_tdata (abfd)->segment_map == NULL)
3613 if (! map_sections_to_segments (abfd))
3614 return FALSE;
3616 else
3618 /* The placement algorithm assumes that non allocated sections are
3619 not in PT_LOAD segments. We ensure this here by removing such
3620 sections from the segment map. */
3621 for (m = elf_tdata (abfd)->segment_map;
3622 m != NULL;
3623 m = m->next)
3625 unsigned int new_count;
3626 unsigned int i;
3628 if (m->p_type != PT_LOAD)
3629 continue;
3631 new_count = 0;
3632 for (i = 0; i < m->count; i ++)
3634 if ((m->sections[i]->flags & SEC_ALLOC) != 0)
3636 if (i != new_count)
3637 m->sections[new_count] = m->sections[i];
3639 new_count ++;
3643 if (new_count != m->count)
3644 m->count = new_count;
3648 if (bed->elf_backend_modify_segment_map)
3650 if (! (*bed->elf_backend_modify_segment_map) (abfd))
3651 return FALSE;
3654 count = 0;
3655 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3656 ++count;
3658 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
3659 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
3660 elf_elfheader (abfd)->e_phnum = count;
3662 if (count == 0)
3663 return TRUE;
3665 /* If we already counted the number of program segments, make sure
3666 that we allocated enough space. This happens when SIZEOF_HEADERS
3667 is used in a linker script. */
3668 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
3669 if (alloc != 0 && count > alloc)
3671 ((*_bfd_error_handler)
3672 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3673 bfd_get_filename (abfd), alloc, count));
3674 bfd_set_error (bfd_error_bad_value);
3675 return FALSE;
3678 if (alloc == 0)
3679 alloc = count;
3681 amt = alloc * sizeof (Elf_Internal_Phdr);
3682 phdrs = bfd_alloc (abfd, amt);
3683 if (phdrs == NULL)
3684 return FALSE;
3686 off = bed->s->sizeof_ehdr;
3687 off += alloc * bed->s->sizeof_phdr;
3689 filehdr_vaddr = 0;
3690 filehdr_paddr = 0;
3691 phdrs_vaddr = 0;
3692 phdrs_paddr = 0;
3694 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3695 m != NULL;
3696 m = m->next, p++)
3698 unsigned int i;
3699 asection **secpp;
3701 /* If elf_segment_map is not from map_sections_to_segments, the
3702 sections may not be correctly ordered. NOTE: sorting should
3703 not be done to the PT_NOTE section of a corefile, which may
3704 contain several pseudo-sections artificially created by bfd.
3705 Sorting these pseudo-sections breaks things badly. */
3706 if (m->count > 1
3707 && !(elf_elfheader (abfd)->e_type == ET_CORE
3708 && m->p_type == PT_NOTE))
3709 qsort (m->sections, (size_t) m->count, sizeof (asection *),
3710 elf_sort_sections);
3712 p->p_type = m->p_type;
3713 p->p_flags = m->p_flags;
3715 if (p->p_type == PT_LOAD
3716 && m->count > 0
3717 && (m->sections[0]->flags & SEC_ALLOC) != 0)
3719 if ((abfd->flags & D_PAGED) != 0)
3720 off += (m->sections[0]->vma - off) % bed->maxpagesize;
3721 else
3723 bfd_size_type align;
3725 align = 0;
3726 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3728 bfd_size_type secalign;
3730 secalign = bfd_get_section_alignment (abfd, *secpp);
3731 if (secalign > align)
3732 align = secalign;
3735 off += (m->sections[0]->vma - off) % (1 << align);
3739 if (m->count == 0)
3740 p->p_vaddr = 0;
3741 else
3742 p->p_vaddr = m->sections[0]->vma;
3744 if (m->p_paddr_valid)
3745 p->p_paddr = m->p_paddr;
3746 else if (m->count == 0)
3747 p->p_paddr = 0;
3748 else
3749 p->p_paddr = m->sections[0]->lma;
3751 if (p->p_type == PT_LOAD
3752 && (abfd->flags & D_PAGED) != 0)
3753 p->p_align = bed->maxpagesize;
3754 else if (m->count == 0)
3755 p->p_align = 1 << bed->s->log_file_align;
3756 else
3757 p->p_align = 0;
3759 p->p_offset = 0;
3760 p->p_filesz = 0;
3761 p->p_memsz = 0;
3763 if (m->includes_filehdr)
3765 if (! m->p_flags_valid)
3766 p->p_flags |= PF_R;
3767 p->p_offset = 0;
3768 p->p_filesz = bed->s->sizeof_ehdr;
3769 p->p_memsz = bed->s->sizeof_ehdr;
3770 if (m->count > 0)
3772 BFD_ASSERT (p->p_type == PT_LOAD);
3774 if (p->p_vaddr < (bfd_vma) off)
3776 (*_bfd_error_handler)
3777 (_("%s: Not enough room for program headers, try linking with -N"),
3778 bfd_get_filename (abfd));
3779 bfd_set_error (bfd_error_bad_value);
3780 return FALSE;
3783 p->p_vaddr -= off;
3784 if (! m->p_paddr_valid)
3785 p->p_paddr -= off;
3787 if (p->p_type == PT_LOAD)
3789 filehdr_vaddr = p->p_vaddr;
3790 filehdr_paddr = p->p_paddr;
3794 if (m->includes_phdrs)
3796 if (! m->p_flags_valid)
3797 p->p_flags |= PF_R;
3799 if (m->includes_filehdr)
3801 if (p->p_type == PT_LOAD)
3803 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
3804 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
3807 else
3809 p->p_offset = bed->s->sizeof_ehdr;
3811 if (m->count > 0)
3813 BFD_ASSERT (p->p_type == PT_LOAD);
3814 p->p_vaddr -= off - p->p_offset;
3815 if (! m->p_paddr_valid)
3816 p->p_paddr -= off - p->p_offset;
3819 if (p->p_type == PT_LOAD)
3821 phdrs_vaddr = p->p_vaddr;
3822 phdrs_paddr = p->p_paddr;
3824 else
3825 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
3828 p->p_filesz += alloc * bed->s->sizeof_phdr;
3829 p->p_memsz += alloc * bed->s->sizeof_phdr;
3832 if (p->p_type == PT_LOAD
3833 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
3835 if (! m->includes_filehdr && ! m->includes_phdrs)
3836 p->p_offset = off;
3837 else
3839 file_ptr adjust;
3841 adjust = off - (p->p_offset + p->p_filesz);
3842 p->p_filesz += adjust;
3843 p->p_memsz += adjust;
3847 voff = off;
3849 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3851 asection *sec;
3852 flagword flags;
3853 bfd_size_type align;
3855 sec = *secpp;
3856 flags = sec->flags;
3857 align = 1 << bfd_get_section_alignment (abfd, sec);
3859 /* The section may have artificial alignment forced by a
3860 link script. Notice this case by the gap between the
3861 cumulative phdr lma and the section's lma. */
3862 if (p->p_paddr + p->p_memsz < sec->lma)
3864 bfd_vma adjust = sec->lma - (p->p_paddr + p->p_memsz);
3866 p->p_memsz += adjust;
3867 if (p->p_type == PT_LOAD
3868 || (p->p_type == PT_NOTE
3869 && bfd_get_format (abfd) == bfd_core))
3871 off += adjust;
3872 voff += adjust;
3874 if ((flags & SEC_LOAD) != 0
3875 || (flags & SEC_THREAD_LOCAL) != 0)
3876 p->p_filesz += adjust;
3879 if (p->p_type == PT_LOAD)
3881 bfd_signed_vma adjust;
3883 if ((flags & SEC_LOAD) != 0)
3885 adjust = sec->lma - (p->p_paddr + p->p_memsz);
3886 if (adjust < 0)
3887 adjust = 0;
3889 else if ((flags & SEC_ALLOC) != 0)
3891 /* The section VMA must equal the file position
3892 modulo the page size. FIXME: I'm not sure if
3893 this adjustment is really necessary. We used to
3894 not have the SEC_LOAD case just above, and then
3895 this was necessary, but now I'm not sure. */
3896 if ((abfd->flags & D_PAGED) != 0)
3897 adjust = (sec->vma - voff) % bed->maxpagesize;
3898 else
3899 adjust = (sec->vma - voff) % align;
3901 else
3902 adjust = 0;
3904 if (adjust != 0)
3906 if (i == 0)
3908 (* _bfd_error_handler) (_("\
3909 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
3910 bfd_section_name (abfd, sec),
3911 sec->lma,
3912 p->p_paddr);
3913 return FALSE;
3915 p->p_memsz += adjust;
3916 off += adjust;
3917 voff += adjust;
3918 if ((flags & SEC_LOAD) != 0)
3919 p->p_filesz += adjust;
3922 sec->filepos = off;
3924 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3925 used in a linker script we may have a section with
3926 SEC_LOAD clear but which is supposed to have
3927 contents. */
3928 if ((flags & SEC_LOAD) != 0
3929 || (flags & SEC_HAS_CONTENTS) != 0)
3930 off += sec->_raw_size;
3932 if ((flags & SEC_ALLOC) != 0
3933 && ((flags & SEC_LOAD) != 0
3934 || (flags & SEC_THREAD_LOCAL) == 0))
3935 voff += sec->_raw_size;
3938 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
3940 /* The actual "note" segment has i == 0.
3941 This is the one that actually contains everything. */
3942 if (i == 0)
3944 sec->filepos = off;
3945 p->p_filesz = sec->_raw_size;
3946 off += sec->_raw_size;
3947 voff = off;
3949 else
3951 /* Fake sections -- don't need to be written. */
3952 sec->filepos = 0;
3953 sec->_raw_size = 0;
3954 flags = sec->flags = 0;
3956 p->p_memsz = 0;
3957 p->p_align = 1;
3959 else
3961 if ((sec->flags & SEC_LOAD) != 0
3962 || (sec->flags & SEC_THREAD_LOCAL) == 0
3963 || p->p_type == PT_TLS)
3964 p->p_memsz += sec->_raw_size;
3966 if ((flags & SEC_LOAD) != 0)
3967 p->p_filesz += sec->_raw_size;
3969 if (p->p_type == PT_TLS
3970 && sec->_raw_size == 0
3971 && (sec->flags & SEC_HAS_CONTENTS) == 0)
3973 struct bfd_link_order *o;
3974 bfd_vma tbss_size = 0;
3976 for (o = sec->link_order_head; o != NULL; o = o->next)
3977 if (tbss_size < o->offset + o->size)
3978 tbss_size = o->offset + o->size;
3980 p->p_memsz += tbss_size;
3983 if (align > p->p_align
3984 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
3985 p->p_align = align;
3988 if (! m->p_flags_valid)
3990 p->p_flags |= PF_R;
3991 if ((flags & SEC_CODE) != 0)
3992 p->p_flags |= PF_X;
3993 if ((flags & SEC_READONLY) == 0)
3994 p->p_flags |= PF_W;
3999 /* Now that we have set the section file positions, we can set up
4000 the file positions for the non PT_LOAD segments. */
4001 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4002 m != NULL;
4003 m = m->next, p++)
4005 if (p->p_type != PT_LOAD && m->count > 0)
4007 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
4008 p->p_offset = m->sections[0]->filepos;
4010 if (m->count == 0)
4012 if (m->includes_filehdr)
4014 p->p_vaddr = filehdr_vaddr;
4015 if (! m->p_paddr_valid)
4016 p->p_paddr = filehdr_paddr;
4018 else if (m->includes_phdrs)
4020 p->p_vaddr = phdrs_vaddr;
4021 if (! m->p_paddr_valid)
4022 p->p_paddr = phdrs_paddr;
4027 /* Clear out any program headers we allocated but did not use. */
4028 for (; count < alloc; count++, p++)
4030 memset (p, 0, sizeof *p);
4031 p->p_type = PT_NULL;
4034 elf_tdata (abfd)->phdr = phdrs;
4036 elf_tdata (abfd)->next_file_pos = off;
4038 /* Write out the program headers. */
4039 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4040 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
4041 return FALSE;
4043 return TRUE;
4046 /* Get the size of the program header.
4048 If this is called by the linker before any of the section VMA's are set, it
4049 can't calculate the correct value for a strange memory layout. This only
4050 happens when SIZEOF_HEADERS is used in a linker script. In this case,
4051 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
4052 data segment (exclusive of .interp and .dynamic).
4054 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
4055 will be two segments. */
4057 static bfd_size_type
4058 get_program_header_size (bfd *abfd)
4060 size_t segs;
4061 asection *s;
4062 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4064 /* We can't return a different result each time we're called. */
4065 if (elf_tdata (abfd)->program_header_size != 0)
4066 return elf_tdata (abfd)->program_header_size;
4068 if (elf_tdata (abfd)->segment_map != NULL)
4070 struct elf_segment_map *m;
4072 segs = 0;
4073 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4074 ++segs;
4075 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
4076 return elf_tdata (abfd)->program_header_size;
4079 /* Assume we will need exactly two PT_LOAD segments: one for text
4080 and one for data. */
4081 segs = 2;
4083 s = bfd_get_section_by_name (abfd, ".interp");
4084 if (s != NULL && (s->flags & SEC_LOAD) != 0)
4086 /* If we have a loadable interpreter section, we need a
4087 PT_INTERP segment. In this case, assume we also need a
4088 PT_PHDR segment, although that may not be true for all
4089 targets. */
4090 segs += 2;
4093 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4095 /* We need a PT_DYNAMIC segment. */
4096 ++segs;
4099 if (elf_tdata (abfd)->eh_frame_hdr)
4101 /* We need a PT_GNU_EH_FRAME segment. */
4102 ++segs;
4105 if (elf_tdata (abfd)->stack_flags)
4107 /* We need a PT_GNU_STACK segment. */
4108 ++segs;
4111 for (s = abfd->sections; s != NULL; s = s->next)
4113 if ((s->flags & SEC_LOAD) != 0
4114 && strncmp (s->name, ".note", 5) == 0)
4116 /* We need a PT_NOTE segment. */
4117 ++segs;
4121 for (s = abfd->sections; s != NULL; s = s->next)
4123 if (s->flags & SEC_THREAD_LOCAL)
4125 /* We need a PT_TLS segment. */
4126 ++segs;
4127 break;
4131 /* Let the backend count up any program headers it might need. */
4132 if (bed->elf_backend_additional_program_headers)
4134 int a;
4136 a = (*bed->elf_backend_additional_program_headers) (abfd);
4137 if (a == -1)
4138 abort ();
4139 segs += a;
4142 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
4143 return elf_tdata (abfd)->program_header_size;
4146 /* Work out the file positions of all the sections. This is called by
4147 _bfd_elf_compute_section_file_positions. All the section sizes and
4148 VMAs must be known before this is called.
4150 We do not consider reloc sections at this point, unless they form
4151 part of the loadable image. Reloc sections are assigned file
4152 positions in assign_file_positions_for_relocs, which is called by
4153 write_object_contents and final_link.
4155 We also don't set the positions of the .symtab and .strtab here. */
4157 static bfd_boolean
4158 assign_file_positions_except_relocs (bfd *abfd)
4160 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
4161 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
4162 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4163 unsigned int num_sec = elf_numsections (abfd);
4164 file_ptr off;
4165 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4167 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4168 && bfd_get_format (abfd) != bfd_core)
4170 Elf_Internal_Shdr **hdrpp;
4171 unsigned int i;
4173 /* Start after the ELF header. */
4174 off = i_ehdrp->e_ehsize;
4176 /* We are not creating an executable, which means that we are
4177 not creating a program header, and that the actual order of
4178 the sections in the file is unimportant. */
4179 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4181 Elf_Internal_Shdr *hdr;
4183 hdr = *hdrpp;
4184 if (hdr->sh_type == SHT_REL
4185 || hdr->sh_type == SHT_RELA
4186 || i == tdata->symtab_section
4187 || i == tdata->symtab_shndx_section
4188 || i == tdata->strtab_section)
4190 hdr->sh_offset = -1;
4192 else
4193 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4195 if (i == SHN_LORESERVE - 1)
4197 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4198 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4202 else
4204 unsigned int i;
4205 Elf_Internal_Shdr **hdrpp;
4207 /* Assign file positions for the loaded sections based on the
4208 assignment of sections to segments. */
4209 if (! assign_file_positions_for_segments (abfd))
4210 return FALSE;
4212 /* Assign file positions for the other sections. */
4214 off = elf_tdata (abfd)->next_file_pos;
4215 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4217 Elf_Internal_Shdr *hdr;
4219 hdr = *hdrpp;
4220 if (hdr->bfd_section != NULL
4221 && hdr->bfd_section->filepos != 0)
4222 hdr->sh_offset = hdr->bfd_section->filepos;
4223 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4225 ((*_bfd_error_handler)
4226 (_("%s: warning: allocated section `%s' not in segment"),
4227 bfd_get_filename (abfd),
4228 (hdr->bfd_section == NULL
4229 ? "*unknown*"
4230 : hdr->bfd_section->name)));
4231 if ((abfd->flags & D_PAGED) != 0)
4232 off += (hdr->sh_addr - off) % bed->maxpagesize;
4233 else
4234 off += (hdr->sh_addr - off) % hdr->sh_addralign;
4235 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4236 FALSE);
4238 else if (hdr->sh_type == SHT_REL
4239 || hdr->sh_type == SHT_RELA
4240 || hdr == i_shdrpp[tdata->symtab_section]
4241 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4242 || hdr == i_shdrpp[tdata->strtab_section])
4243 hdr->sh_offset = -1;
4244 else
4245 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4247 if (i == SHN_LORESERVE - 1)
4249 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4250 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4255 /* Place the section headers. */
4256 off = align_file_position (off, 1 << bed->s->log_file_align);
4257 i_ehdrp->e_shoff = off;
4258 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4260 elf_tdata (abfd)->next_file_pos = off;
4262 return TRUE;
4265 static bfd_boolean
4266 prep_headers (bfd *abfd)
4268 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
4269 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4270 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
4271 struct elf_strtab_hash *shstrtab;
4272 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4274 i_ehdrp = elf_elfheader (abfd);
4275 i_shdrp = elf_elfsections (abfd);
4277 shstrtab = _bfd_elf_strtab_init ();
4278 if (shstrtab == NULL)
4279 return FALSE;
4281 elf_shstrtab (abfd) = shstrtab;
4283 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4284 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4285 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4286 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4288 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4289 i_ehdrp->e_ident[EI_DATA] =
4290 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4291 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4293 if ((abfd->flags & DYNAMIC) != 0)
4294 i_ehdrp->e_type = ET_DYN;
4295 else if ((abfd->flags & EXEC_P) != 0)
4296 i_ehdrp->e_type = ET_EXEC;
4297 else if (bfd_get_format (abfd) == bfd_core)
4298 i_ehdrp->e_type = ET_CORE;
4299 else
4300 i_ehdrp->e_type = ET_REL;
4302 switch (bfd_get_arch (abfd))
4304 case bfd_arch_unknown:
4305 i_ehdrp->e_machine = EM_NONE;
4306 break;
4308 /* There used to be a long list of cases here, each one setting
4309 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4310 in the corresponding bfd definition. To avoid duplication,
4311 the switch was removed. Machines that need special handling
4312 can generally do it in elf_backend_final_write_processing(),
4313 unless they need the information earlier than the final write.
4314 Such need can generally be supplied by replacing the tests for
4315 e_machine with the conditions used to determine it. */
4316 default:
4317 if (get_elf_backend_data (abfd) != NULL)
4318 i_ehdrp->e_machine = get_elf_backend_data (abfd)->elf_machine_code;
4319 else
4320 i_ehdrp->e_machine = EM_NONE;
4323 i_ehdrp->e_version = bed->s->ev_current;
4324 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4326 /* No program header, for now. */
4327 i_ehdrp->e_phoff = 0;
4328 i_ehdrp->e_phentsize = 0;
4329 i_ehdrp->e_phnum = 0;
4331 /* Each bfd section is section header entry. */
4332 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4333 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4335 /* If we're building an executable, we'll need a program header table. */
4336 if (abfd->flags & EXEC_P)
4338 /* It all happens later. */
4339 #if 0
4340 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
4342 /* elf_build_phdrs() returns a (NULL-terminated) array of
4343 Elf_Internal_Phdrs. */
4344 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
4345 i_ehdrp->e_phoff = outbase;
4346 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
4347 #endif
4349 else
4351 i_ehdrp->e_phentsize = 0;
4352 i_phdrp = 0;
4353 i_ehdrp->e_phoff = 0;
4356 elf_tdata (abfd)->symtab_hdr.sh_name =
4357 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
4358 elf_tdata (abfd)->strtab_hdr.sh_name =
4359 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
4360 elf_tdata (abfd)->shstrtab_hdr.sh_name =
4361 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
4362 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4363 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4364 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4365 return FALSE;
4367 return TRUE;
4370 /* Assign file positions for all the reloc sections which are not part
4371 of the loadable file image. */
4373 void
4374 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
4376 file_ptr off;
4377 unsigned int i, num_sec;
4378 Elf_Internal_Shdr **shdrpp;
4380 off = elf_tdata (abfd)->next_file_pos;
4382 num_sec = elf_numsections (abfd);
4383 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
4385 Elf_Internal_Shdr *shdrp;
4387 shdrp = *shdrpp;
4388 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4389 && shdrp->sh_offset == -1)
4390 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
4393 elf_tdata (abfd)->next_file_pos = off;
4396 bfd_boolean
4397 _bfd_elf_write_object_contents (bfd *abfd)
4399 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4400 Elf_Internal_Ehdr *i_ehdrp;
4401 Elf_Internal_Shdr **i_shdrp;
4402 bfd_boolean failed;
4403 unsigned int count, num_sec;
4405 if (! abfd->output_has_begun
4406 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
4407 return FALSE;
4409 i_shdrp = elf_elfsections (abfd);
4410 i_ehdrp = elf_elfheader (abfd);
4412 failed = FALSE;
4413 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4414 if (failed)
4415 return FALSE;
4417 _bfd_elf_assign_file_positions_for_relocs (abfd);
4419 /* After writing the headers, we need to write the sections too... */
4420 num_sec = elf_numsections (abfd);
4421 for (count = 1; count < num_sec; count++)
4423 if (bed->elf_backend_section_processing)
4424 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4425 if (i_shdrp[count]->contents)
4427 bfd_size_type amt = i_shdrp[count]->sh_size;
4429 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
4430 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
4431 return FALSE;
4433 if (count == SHN_LORESERVE - 1)
4434 count += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4437 /* Write out the section header names. */
4438 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
4439 || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))
4440 return FALSE;
4442 if (bed->elf_backend_final_write_processing)
4443 (*bed->elf_backend_final_write_processing) (abfd,
4444 elf_tdata (abfd)->linker);
4446 return bed->s->write_shdrs_and_ehdr (abfd);
4449 bfd_boolean
4450 _bfd_elf_write_corefile_contents (bfd *abfd)
4452 /* Hopefully this can be done just like an object file. */
4453 return _bfd_elf_write_object_contents (abfd);
4456 /* Given a section, search the header to find them. */
4459 _bfd_elf_section_from_bfd_section (bfd *abfd, struct sec *asect)
4461 struct elf_backend_data *bed;
4462 int index;
4464 if (elf_section_data (asect) != NULL
4465 && elf_section_data (asect)->this_idx != 0)
4466 return elf_section_data (asect)->this_idx;
4468 if (bfd_is_abs_section (asect))
4469 index = SHN_ABS;
4470 else if (bfd_is_com_section (asect))
4471 index = SHN_COMMON;
4472 else if (bfd_is_und_section (asect))
4473 index = SHN_UNDEF;
4474 else
4476 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
4477 int maxindex = elf_numsections (abfd);
4479 for (index = 1; index < maxindex; index++)
4481 Elf_Internal_Shdr *hdr = i_shdrp[index];
4483 if (hdr != NULL && hdr->bfd_section == asect)
4484 return index;
4486 index = -1;
4489 bed = get_elf_backend_data (abfd);
4490 if (bed->elf_backend_section_from_bfd_section)
4492 int retval = index;
4494 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
4495 return retval;
4498 if (index == -1)
4499 bfd_set_error (bfd_error_nonrepresentable_section);
4501 return index;
4504 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4505 on error. */
4508 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
4510 asymbol *asym_ptr = *asym_ptr_ptr;
4511 int idx;
4512 flagword flags = asym_ptr->flags;
4514 /* When gas creates relocations against local labels, it creates its
4515 own symbol for the section, but does put the symbol into the
4516 symbol chain, so udata is 0. When the linker is generating
4517 relocatable output, this section symbol may be for one of the
4518 input sections rather than the output section. */
4519 if (asym_ptr->udata.i == 0
4520 && (flags & BSF_SECTION_SYM)
4521 && asym_ptr->section)
4523 int indx;
4525 if (asym_ptr->section->output_section != NULL)
4526 indx = asym_ptr->section->output_section->index;
4527 else
4528 indx = asym_ptr->section->index;
4529 if (indx < elf_num_section_syms (abfd)
4530 && elf_section_syms (abfd)[indx] != NULL)
4531 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
4534 idx = asym_ptr->udata.i;
4536 if (idx == 0)
4538 /* This case can occur when using --strip-symbol on a symbol
4539 which is used in a relocation entry. */
4540 (*_bfd_error_handler)
4541 (_("%s: symbol `%s' required but not present"),
4542 bfd_archive_filename (abfd), bfd_asymbol_name (asym_ptr));
4543 bfd_set_error (bfd_error_no_symbols);
4544 return -1;
4547 #if DEBUG & 4
4549 fprintf (stderr,
4550 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
4551 (long) asym_ptr, asym_ptr->name, idx, flags,
4552 elf_symbol_flags (flags));
4553 fflush (stderr);
4555 #endif
4557 return idx;
4560 /* Copy private BFD data. This copies any program header information. */
4562 static bfd_boolean
4563 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
4565 Elf_Internal_Ehdr *iehdr;
4566 struct elf_segment_map *map;
4567 struct elf_segment_map *map_first;
4568 struct elf_segment_map **pointer_to_map;
4569 Elf_Internal_Phdr *segment;
4570 asection *section;
4571 unsigned int i;
4572 unsigned int num_segments;
4573 bfd_boolean phdr_included = FALSE;
4574 bfd_vma maxpagesize;
4575 struct elf_segment_map *phdr_adjust_seg = NULL;
4576 unsigned int phdr_adjust_num = 0;
4577 struct elf_backend_data *bed;
4579 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4580 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4581 return TRUE;
4583 if (elf_tdata (ibfd)->phdr == NULL)
4584 return TRUE;
4586 bed = get_elf_backend_data (ibfd);
4587 iehdr = elf_elfheader (ibfd);
4589 map_first = NULL;
4590 pointer_to_map = &map_first;
4592 num_segments = elf_elfheader (ibfd)->e_phnum;
4593 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
4595 /* Returns the end address of the segment + 1. */
4596 #define SEGMENT_END(segment, start) \
4597 (start + (segment->p_memsz > segment->p_filesz \
4598 ? segment->p_memsz : segment->p_filesz))
4600 #define SECTION_SIZE(section, segment) \
4601 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
4602 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
4603 ? section->_raw_size : 0)
4605 /* Returns TRUE if the given section is contained within
4606 the given segment. VMA addresses are compared. */
4607 #define IS_CONTAINED_BY_VMA(section, segment) \
4608 (section->vma >= segment->p_vaddr \
4609 && (section->vma + SECTION_SIZE (section, segment) \
4610 <= (SEGMENT_END (segment, segment->p_vaddr))))
4612 /* Returns TRUE if the given section is contained within
4613 the given segment. LMA addresses are compared. */
4614 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4615 (section->lma >= base \
4616 && (section->lma + SECTION_SIZE (section, segment) \
4617 <= SEGMENT_END (segment, base)))
4619 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4620 #define IS_COREFILE_NOTE(p, s) \
4621 (p->p_type == PT_NOTE \
4622 && bfd_get_format (ibfd) == bfd_core \
4623 && s->vma == 0 && s->lma == 0 \
4624 && (bfd_vma) s->filepos >= p->p_offset \
4625 && ((bfd_vma) s->filepos + s->_raw_size \
4626 <= p->p_offset + p->p_filesz))
4628 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4629 linker, which generates a PT_INTERP section with p_vaddr and
4630 p_memsz set to 0. */
4631 #define IS_SOLARIS_PT_INTERP(p, s) \
4632 (p->p_vaddr == 0 \
4633 && p->p_paddr == 0 \
4634 && p->p_memsz == 0 \
4635 && p->p_filesz > 0 \
4636 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4637 && s->_raw_size > 0 \
4638 && (bfd_vma) s->filepos >= p->p_offset \
4639 && ((bfd_vma) s->filepos + s->_raw_size \
4640 <= p->p_offset + p->p_filesz))
4642 /* Decide if the given section should be included in the given segment.
4643 A section will be included if:
4644 1. It is within the address space of the segment -- we use the LMA
4645 if that is set for the segment and the VMA otherwise,
4646 2. It is an allocated segment,
4647 3. There is an output section associated with it,
4648 4. The section has not already been allocated to a previous segment.
4649 5. PT_TLS segment includes only SHF_TLS sections.
4650 6. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. */
4651 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
4652 ((((segment->p_paddr \
4653 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4654 : IS_CONTAINED_BY_VMA (section, segment)) \
4655 && (section->flags & SEC_ALLOC) != 0) \
4656 || IS_COREFILE_NOTE (segment, section)) \
4657 && section->output_section != NULL \
4658 && (segment->p_type != PT_TLS \
4659 || (section->flags & SEC_THREAD_LOCAL)) \
4660 && (segment->p_type == PT_LOAD \
4661 || segment->p_type == PT_TLS \
4662 || (section->flags & SEC_THREAD_LOCAL) == 0) \
4663 && ! section->segment_mark)
4665 /* Returns TRUE iff seg1 starts after the end of seg2. */
4666 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
4667 (seg1->field >= SEGMENT_END (seg2, seg2->field))
4669 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
4670 their VMA address ranges and their LMA address ranges overlap.
4671 It is possible to have overlapping VMA ranges without overlapping LMA
4672 ranges. RedBoot images for example can have both .data and .bss mapped
4673 to the same VMA range, but with the .data section mapped to a different
4674 LMA. */
4675 #define SEGMENT_OVERLAPS(seg1, seg2) \
4676 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
4677 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
4678 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
4679 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
4681 /* Initialise the segment mark field. */
4682 for (section = ibfd->sections; section != NULL; section = section->next)
4683 section->segment_mark = FALSE;
4685 /* Scan through the segments specified in the program header
4686 of the input BFD. For this first scan we look for overlaps
4687 in the loadable segments. These can be created by weird
4688 parameters to objcopy. Also, fix some solaris weirdness. */
4689 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4690 i < num_segments;
4691 i++, segment++)
4693 unsigned int j;
4694 Elf_Internal_Phdr *segment2;
4696 if (segment->p_type == PT_INTERP)
4697 for (section = ibfd->sections; section; section = section->next)
4698 if (IS_SOLARIS_PT_INTERP (segment, section))
4700 /* Mininal change so that the normal section to segment
4701 assigment code will work. */
4702 segment->p_vaddr = section->vma;
4703 break;
4706 if (segment->p_type != PT_LOAD)
4707 continue;
4709 /* Determine if this segment overlaps any previous segments. */
4710 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
4712 bfd_signed_vma extra_length;
4714 if (segment2->p_type != PT_LOAD
4715 || ! SEGMENT_OVERLAPS (segment, segment2))
4716 continue;
4718 /* Merge the two segments together. */
4719 if (segment2->p_vaddr < segment->p_vaddr)
4721 /* Extend SEGMENT2 to include SEGMENT and then delete
4722 SEGMENT. */
4723 extra_length =
4724 SEGMENT_END (segment, segment->p_vaddr)
4725 - SEGMENT_END (segment2, segment2->p_vaddr);
4727 if (extra_length > 0)
4729 segment2->p_memsz += extra_length;
4730 segment2->p_filesz += extra_length;
4733 segment->p_type = PT_NULL;
4735 /* Since we have deleted P we must restart the outer loop. */
4736 i = 0;
4737 segment = elf_tdata (ibfd)->phdr;
4738 break;
4740 else
4742 /* Extend SEGMENT to include SEGMENT2 and then delete
4743 SEGMENT2. */
4744 extra_length =
4745 SEGMENT_END (segment2, segment2->p_vaddr)
4746 - SEGMENT_END (segment, segment->p_vaddr);
4748 if (extra_length > 0)
4750 segment->p_memsz += extra_length;
4751 segment->p_filesz += extra_length;
4754 segment2->p_type = PT_NULL;
4759 /* The second scan attempts to assign sections to segments. */
4760 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4761 i < num_segments;
4762 i ++, segment ++)
4764 unsigned int section_count;
4765 asection ** sections;
4766 asection * output_section;
4767 unsigned int isec;
4768 bfd_vma matching_lma;
4769 bfd_vma suggested_lma;
4770 unsigned int j;
4771 bfd_size_type amt;
4773 if (segment->p_type == PT_NULL)
4774 continue;
4776 /* Compute how many sections might be placed into this segment. */
4777 for (section = ibfd->sections, section_count = 0;
4778 section != NULL;
4779 section = section->next)
4780 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
4781 ++section_count;
4783 /* Allocate a segment map big enough to contain
4784 all of the sections we have selected. */
4785 amt = sizeof (struct elf_segment_map);
4786 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
4787 map = bfd_alloc (obfd, amt);
4788 if (map == NULL)
4789 return FALSE;
4791 /* Initialise the fields of the segment map. Default to
4792 using the physical address of the segment in the input BFD. */
4793 map->next = NULL;
4794 map->p_type = segment->p_type;
4795 map->p_flags = segment->p_flags;
4796 map->p_flags_valid = 1;
4797 map->p_paddr = segment->p_paddr;
4798 map->p_paddr_valid = 1;
4800 /* Determine if this segment contains the ELF file header
4801 and if it contains the program headers themselves. */
4802 map->includes_filehdr = (segment->p_offset == 0
4803 && segment->p_filesz >= iehdr->e_ehsize);
4805 map->includes_phdrs = 0;
4807 if (! phdr_included || segment->p_type != PT_LOAD)
4809 map->includes_phdrs =
4810 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
4811 && (segment->p_offset + segment->p_filesz
4812 >= ((bfd_vma) iehdr->e_phoff
4813 + iehdr->e_phnum * iehdr->e_phentsize)));
4815 if (segment->p_type == PT_LOAD && map->includes_phdrs)
4816 phdr_included = TRUE;
4819 if (section_count == 0)
4821 /* Special segments, such as the PT_PHDR segment, may contain
4822 no sections, but ordinary, loadable segments should contain
4823 something. They are allowed by the ELF spec however, so only
4824 a warning is produced. */
4825 if (segment->p_type == PT_LOAD)
4826 (*_bfd_error_handler)
4827 (_("%s: warning: Empty loadable segment detected, is this intentional ?\n"),
4828 bfd_archive_filename (ibfd));
4830 map->count = 0;
4831 *pointer_to_map = map;
4832 pointer_to_map = &map->next;
4834 continue;
4837 /* Now scan the sections in the input BFD again and attempt
4838 to add their corresponding output sections to the segment map.
4839 The problem here is how to handle an output section which has
4840 been moved (ie had its LMA changed). There are four possibilities:
4842 1. None of the sections have been moved.
4843 In this case we can continue to use the segment LMA from the
4844 input BFD.
4846 2. All of the sections have been moved by the same amount.
4847 In this case we can change the segment's LMA to match the LMA
4848 of the first section.
4850 3. Some of the sections have been moved, others have not.
4851 In this case those sections which have not been moved can be
4852 placed in the current segment which will have to have its size,
4853 and possibly its LMA changed, and a new segment or segments will
4854 have to be created to contain the other sections.
4856 4. The sections have been moved, but not by the same amount.
4857 In this case we can change the segment's LMA to match the LMA
4858 of the first section and we will have to create a new segment
4859 or segments to contain the other sections.
4861 In order to save time, we allocate an array to hold the section
4862 pointers that we are interested in. As these sections get assigned
4863 to a segment, they are removed from this array. */
4865 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
4866 to work around this long long bug. */
4867 amt = section_count * sizeof (asection *);
4868 sections = bfd_malloc (amt);
4869 if (sections == NULL)
4870 return FALSE;
4872 /* Step One: Scan for segment vs section LMA conflicts.
4873 Also add the sections to the section array allocated above.
4874 Also add the sections to the current segment. In the common
4875 case, where the sections have not been moved, this means that
4876 we have completely filled the segment, and there is nothing
4877 more to do. */
4878 isec = 0;
4879 matching_lma = 0;
4880 suggested_lma = 0;
4882 for (j = 0, section = ibfd->sections;
4883 section != NULL;
4884 section = section->next)
4886 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
4888 output_section = section->output_section;
4890 sections[j ++] = section;
4892 /* The Solaris native linker always sets p_paddr to 0.
4893 We try to catch that case here, and set it to the
4894 correct value. Note - some backends require that
4895 p_paddr be left as zero. */
4896 if (segment->p_paddr == 0
4897 && segment->p_vaddr != 0
4898 && (! bed->want_p_paddr_set_to_zero)
4899 && isec == 0
4900 && output_section->lma != 0
4901 && (output_section->vma == (segment->p_vaddr
4902 + (map->includes_filehdr
4903 ? iehdr->e_ehsize
4904 : 0)
4905 + (map->includes_phdrs
4906 ? (iehdr->e_phnum
4907 * iehdr->e_phentsize)
4908 : 0))))
4909 map->p_paddr = segment->p_vaddr;
4911 /* Match up the physical address of the segment with the
4912 LMA address of the output section. */
4913 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4914 || IS_COREFILE_NOTE (segment, section)
4915 || (bed->want_p_paddr_set_to_zero &&
4916 IS_CONTAINED_BY_VMA (output_section, segment))
4919 if (matching_lma == 0)
4920 matching_lma = output_section->lma;
4922 /* We assume that if the section fits within the segment
4923 then it does not overlap any other section within that
4924 segment. */
4925 map->sections[isec ++] = output_section;
4927 else if (suggested_lma == 0)
4928 suggested_lma = output_section->lma;
4932 BFD_ASSERT (j == section_count);
4934 /* Step Two: Adjust the physical address of the current segment,
4935 if necessary. */
4936 if (isec == section_count)
4938 /* All of the sections fitted within the segment as currently
4939 specified. This is the default case. Add the segment to
4940 the list of built segments and carry on to process the next
4941 program header in the input BFD. */
4942 map->count = section_count;
4943 *pointer_to_map = map;
4944 pointer_to_map = &map->next;
4946 free (sections);
4947 continue;
4949 else
4951 if (matching_lma != 0)
4953 /* At least one section fits inside the current segment.
4954 Keep it, but modify its physical address to match the
4955 LMA of the first section that fitted. */
4956 map->p_paddr = matching_lma;
4958 else
4960 /* None of the sections fitted inside the current segment.
4961 Change the current segment's physical address to match
4962 the LMA of the first section. */
4963 map->p_paddr = suggested_lma;
4966 /* Offset the segment physical address from the lma
4967 to allow for space taken up by elf headers. */
4968 if (map->includes_filehdr)
4969 map->p_paddr -= iehdr->e_ehsize;
4971 if (map->includes_phdrs)
4973 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
4975 /* iehdr->e_phnum is just an estimate of the number
4976 of program headers that we will need. Make a note
4977 here of the number we used and the segment we chose
4978 to hold these headers, so that we can adjust the
4979 offset when we know the correct value. */
4980 phdr_adjust_num = iehdr->e_phnum;
4981 phdr_adjust_seg = map;
4985 /* Step Three: Loop over the sections again, this time assigning
4986 those that fit to the current segment and removing them from the
4987 sections array; but making sure not to leave large gaps. Once all
4988 possible sections have been assigned to the current segment it is
4989 added to the list of built segments and if sections still remain
4990 to be assigned, a new segment is constructed before repeating
4991 the loop. */
4992 isec = 0;
4995 map->count = 0;
4996 suggested_lma = 0;
4998 /* Fill the current segment with sections that fit. */
4999 for (j = 0; j < section_count; j++)
5001 section = sections[j];
5003 if (section == NULL)
5004 continue;
5006 output_section = section->output_section;
5008 BFD_ASSERT (output_section != NULL);
5010 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5011 || IS_COREFILE_NOTE (segment, section))
5013 if (map->count == 0)
5015 /* If the first section in a segment does not start at
5016 the beginning of the segment, then something is
5017 wrong. */
5018 if (output_section->lma !=
5019 (map->p_paddr
5020 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5021 + (map->includes_phdrs
5022 ? iehdr->e_phnum * iehdr->e_phentsize
5023 : 0)))
5024 abort ();
5026 else
5028 asection * prev_sec;
5030 prev_sec = map->sections[map->count - 1];
5032 /* If the gap between the end of the previous section
5033 and the start of this section is more than
5034 maxpagesize then we need to start a new segment. */
5035 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size,
5036 maxpagesize)
5037 < BFD_ALIGN (output_section->lma, maxpagesize))
5038 || ((prev_sec->lma + prev_sec->_raw_size)
5039 > output_section->lma))
5041 if (suggested_lma == 0)
5042 suggested_lma = output_section->lma;
5044 continue;
5048 map->sections[map->count++] = output_section;
5049 ++isec;
5050 sections[j] = NULL;
5051 section->segment_mark = TRUE;
5053 else if (suggested_lma == 0)
5054 suggested_lma = output_section->lma;
5057 BFD_ASSERT (map->count > 0);
5059 /* Add the current segment to the list of built segments. */
5060 *pointer_to_map = map;
5061 pointer_to_map = &map->next;
5063 if (isec < section_count)
5065 /* We still have not allocated all of the sections to
5066 segments. Create a new segment here, initialise it
5067 and carry on looping. */
5068 amt = sizeof (struct elf_segment_map);
5069 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5070 map = bfd_alloc (obfd, amt);
5071 if (map == NULL)
5073 free (sections);
5074 return FALSE;
5077 /* Initialise the fields of the segment map. Set the physical
5078 physical address to the LMA of the first section that has
5079 not yet been assigned. */
5080 map->next = NULL;
5081 map->p_type = segment->p_type;
5082 map->p_flags = segment->p_flags;
5083 map->p_flags_valid = 1;
5084 map->p_paddr = suggested_lma;
5085 map->p_paddr_valid = 1;
5086 map->includes_filehdr = 0;
5087 map->includes_phdrs = 0;
5090 while (isec < section_count);
5092 free (sections);
5095 /* The Solaris linker creates program headers in which all the
5096 p_paddr fields are zero. When we try to objcopy or strip such a
5097 file, we get confused. Check for this case, and if we find it
5098 reset the p_paddr_valid fields. */
5099 for (map = map_first; map != NULL; map = map->next)
5100 if (map->p_paddr != 0)
5101 break;
5102 if (map == NULL)
5103 for (map = map_first; map != NULL; map = map->next)
5104 map->p_paddr_valid = 0;
5106 elf_tdata (obfd)->segment_map = map_first;
5108 /* If we had to estimate the number of program headers that were
5109 going to be needed, then check our estimate now and adjust
5110 the offset if necessary. */
5111 if (phdr_adjust_seg != NULL)
5113 unsigned int count;
5115 for (count = 0, map = map_first; map != NULL; map = map->next)
5116 count++;
5118 if (count > phdr_adjust_num)
5119 phdr_adjust_seg->p_paddr
5120 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5123 #if 0
5124 /* Final Step: Sort the segments into ascending order of physical
5125 address. */
5126 if (map_first != NULL)
5128 struct elf_segment_map *prev;
5130 prev = map_first;
5131 for (map = map_first->next; map != NULL; prev = map, map = map->next)
5133 /* Yes I know - its a bubble sort.... */
5134 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
5136 /* Swap map and map->next. */
5137 prev->next = map->next;
5138 map->next = map->next->next;
5139 prev->next->next = map;
5141 /* Restart loop. */
5142 map = map_first;
5146 #endif
5148 #undef SEGMENT_END
5149 #undef SECTION_SIZE
5150 #undef IS_CONTAINED_BY_VMA
5151 #undef IS_CONTAINED_BY_LMA
5152 #undef IS_COREFILE_NOTE
5153 #undef IS_SOLARIS_PT_INTERP
5154 #undef INCLUDE_SECTION_IN_SEGMENT
5155 #undef SEGMENT_AFTER_SEGMENT
5156 #undef SEGMENT_OVERLAPS
5157 return TRUE;
5160 /* Copy private section information. This copies over the entsize
5161 field, and sometimes the info field. */
5163 bfd_boolean
5164 _bfd_elf_copy_private_section_data (bfd *ibfd,
5165 asection *isec,
5166 bfd *obfd,
5167 asection *osec)
5169 Elf_Internal_Shdr *ihdr, *ohdr;
5171 if (ibfd->xvec->flavour != bfd_target_elf_flavour
5172 || obfd->xvec->flavour != bfd_target_elf_flavour)
5173 return TRUE;
5175 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
5177 asection *s;
5179 /* Only set up the segments if there are no more SEC_ALLOC
5180 sections. FIXME: This won't do the right thing if objcopy is
5181 used to remove the last SEC_ALLOC section, since objcopy
5182 won't call this routine in that case. */
5183 for (s = isec->next; s != NULL; s = s->next)
5184 if ((s->flags & SEC_ALLOC) != 0)
5185 break;
5186 if (s == NULL)
5188 if (! copy_private_bfd_data (ibfd, obfd))
5189 return FALSE;
5193 ihdr = &elf_section_data (isec)->this_hdr;
5194 ohdr = &elf_section_data (osec)->this_hdr;
5196 ohdr->sh_entsize = ihdr->sh_entsize;
5198 if (ihdr->sh_type == SHT_SYMTAB
5199 || ihdr->sh_type == SHT_DYNSYM
5200 || ihdr->sh_type == SHT_GNU_verneed
5201 || ihdr->sh_type == SHT_GNU_verdef)
5202 ohdr->sh_info = ihdr->sh_info;
5204 /* Set things up for objcopy. The output SHT_GROUP section will
5205 have its elf_next_in_group pointing back to the input group
5206 members. */
5207 elf_next_in_group (osec) = elf_next_in_group (isec);
5208 elf_group_name (osec) = elf_group_name (isec);
5210 osec->use_rela_p = isec->use_rela_p;
5212 return TRUE;
5215 /* Copy private symbol information. If this symbol is in a section
5216 which we did not map into a BFD section, try to map the section
5217 index correctly. We use special macro definitions for the mapped
5218 section indices; these definitions are interpreted by the
5219 swap_out_syms function. */
5221 #define MAP_ONESYMTAB (SHN_HIOS + 1)
5222 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
5223 #define MAP_STRTAB (SHN_HIOS + 3)
5224 #define MAP_SHSTRTAB (SHN_HIOS + 4)
5225 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
5227 bfd_boolean
5228 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
5229 asymbol *isymarg,
5230 bfd *obfd,
5231 asymbol *osymarg)
5233 elf_symbol_type *isym, *osym;
5235 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5236 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5237 return TRUE;
5239 isym = elf_symbol_from (ibfd, isymarg);
5240 osym = elf_symbol_from (obfd, osymarg);
5242 if (isym != NULL
5243 && osym != NULL
5244 && bfd_is_abs_section (isym->symbol.section))
5246 unsigned int shndx;
5248 shndx = isym->internal_elf_sym.st_shndx;
5249 if (shndx == elf_onesymtab (ibfd))
5250 shndx = MAP_ONESYMTAB;
5251 else if (shndx == elf_dynsymtab (ibfd))
5252 shndx = MAP_DYNSYMTAB;
5253 else if (shndx == elf_tdata (ibfd)->strtab_section)
5254 shndx = MAP_STRTAB;
5255 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
5256 shndx = MAP_SHSTRTAB;
5257 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
5258 shndx = MAP_SYM_SHNDX;
5259 osym->internal_elf_sym.st_shndx = shndx;
5262 return TRUE;
5265 /* Swap out the symbols. */
5267 static bfd_boolean
5268 swap_out_syms (bfd *abfd,
5269 struct bfd_strtab_hash **sttp,
5270 int relocatable_p)
5272 struct elf_backend_data *bed;
5273 int symcount;
5274 asymbol **syms;
5275 struct bfd_strtab_hash *stt;
5276 Elf_Internal_Shdr *symtab_hdr;
5277 Elf_Internal_Shdr *symtab_shndx_hdr;
5278 Elf_Internal_Shdr *symstrtab_hdr;
5279 char *outbound_syms;
5280 char *outbound_shndx;
5281 int idx;
5282 bfd_size_type amt;
5284 if (!elf_map_symbols (abfd))
5285 return FALSE;
5287 /* Dump out the symtabs. */
5288 stt = _bfd_elf_stringtab_init ();
5289 if (stt == NULL)
5290 return FALSE;
5292 bed = get_elf_backend_data (abfd);
5293 symcount = bfd_get_symcount (abfd);
5294 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5295 symtab_hdr->sh_type = SHT_SYMTAB;
5296 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
5297 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
5298 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
5299 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
5301 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5302 symstrtab_hdr->sh_type = SHT_STRTAB;
5304 amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym;
5305 outbound_syms = bfd_alloc (abfd, amt);
5306 if (outbound_syms == NULL)
5308 _bfd_stringtab_free (stt);
5309 return FALSE;
5311 symtab_hdr->contents = outbound_syms;
5313 outbound_shndx = NULL;
5314 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
5315 if (symtab_shndx_hdr->sh_name != 0)
5317 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
5318 outbound_shndx = bfd_zalloc (abfd, amt);
5319 if (outbound_shndx == NULL)
5321 _bfd_stringtab_free (stt);
5322 return FALSE;
5325 symtab_shndx_hdr->contents = outbound_shndx;
5326 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
5327 symtab_shndx_hdr->sh_size = amt;
5328 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
5329 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
5332 /* Now generate the data (for "contents"). */
5334 /* Fill in zeroth symbol and swap it out. */
5335 Elf_Internal_Sym sym;
5336 sym.st_name = 0;
5337 sym.st_value = 0;
5338 sym.st_size = 0;
5339 sym.st_info = 0;
5340 sym.st_other = 0;
5341 sym.st_shndx = SHN_UNDEF;
5342 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5343 outbound_syms += bed->s->sizeof_sym;
5344 if (outbound_shndx != NULL)
5345 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5348 syms = bfd_get_outsymbols (abfd);
5349 for (idx = 0; idx < symcount; idx++)
5351 Elf_Internal_Sym sym;
5352 bfd_vma value = syms[idx]->value;
5353 elf_symbol_type *type_ptr;
5354 flagword flags = syms[idx]->flags;
5355 int type;
5357 if ((flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
5359 /* Local section symbols have no name. */
5360 sym.st_name = 0;
5362 else
5364 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
5365 syms[idx]->name,
5366 TRUE, FALSE);
5367 if (sym.st_name == (unsigned long) -1)
5369 _bfd_stringtab_free (stt);
5370 return FALSE;
5374 type_ptr = elf_symbol_from (abfd, syms[idx]);
5376 if ((flags & BSF_SECTION_SYM) == 0
5377 && bfd_is_com_section (syms[idx]->section))
5379 /* ELF common symbols put the alignment into the `value' field,
5380 and the size into the `size' field. This is backwards from
5381 how BFD handles it, so reverse it here. */
5382 sym.st_size = value;
5383 if (type_ptr == NULL
5384 || type_ptr->internal_elf_sym.st_value == 0)
5385 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
5386 else
5387 sym.st_value = type_ptr->internal_elf_sym.st_value;
5388 sym.st_shndx = _bfd_elf_section_from_bfd_section
5389 (abfd, syms[idx]->section);
5391 else
5393 asection *sec = syms[idx]->section;
5394 int shndx;
5396 if (sec->output_section)
5398 value += sec->output_offset;
5399 sec = sec->output_section;
5402 /* Don't add in the section vma for relocatable output. */
5403 if (! relocatable_p)
5404 value += sec->vma;
5405 sym.st_value = value;
5406 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
5408 if (bfd_is_abs_section (sec)
5409 && type_ptr != NULL
5410 && type_ptr->internal_elf_sym.st_shndx != 0)
5412 /* This symbol is in a real ELF section which we did
5413 not create as a BFD section. Undo the mapping done
5414 by copy_private_symbol_data. */
5415 shndx = type_ptr->internal_elf_sym.st_shndx;
5416 switch (shndx)
5418 case MAP_ONESYMTAB:
5419 shndx = elf_onesymtab (abfd);
5420 break;
5421 case MAP_DYNSYMTAB:
5422 shndx = elf_dynsymtab (abfd);
5423 break;
5424 case MAP_STRTAB:
5425 shndx = elf_tdata (abfd)->strtab_section;
5426 break;
5427 case MAP_SHSTRTAB:
5428 shndx = elf_tdata (abfd)->shstrtab_section;
5429 break;
5430 case MAP_SYM_SHNDX:
5431 shndx = elf_tdata (abfd)->symtab_shndx_section;
5432 break;
5433 default:
5434 break;
5437 else
5439 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
5441 if (shndx == -1)
5443 asection *sec2;
5445 /* Writing this would be a hell of a lot easier if
5446 we had some decent documentation on bfd, and
5447 knew what to expect of the library, and what to
5448 demand of applications. For example, it
5449 appears that `objcopy' might not set the
5450 section of a symbol to be a section that is
5451 actually in the output file. */
5452 sec2 = bfd_get_section_by_name (abfd, sec->name);
5453 if (sec2 == NULL)
5455 _bfd_error_handler (_("\
5456 Unable to find equivalent output section for symbol '%s' from section '%s'"),
5457 syms[idx]->name ? syms[idx]->name : "<Local sym>",
5458 sec->name);
5459 bfd_set_error (bfd_error_invalid_operation);
5460 _bfd_stringtab_free (stt);
5461 return FALSE;
5464 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
5465 BFD_ASSERT (shndx != -1);
5469 sym.st_shndx = shndx;
5472 if ((flags & BSF_THREAD_LOCAL) != 0)
5473 type = STT_TLS;
5474 else if ((flags & BSF_FUNCTION) != 0)
5475 type = STT_FUNC;
5476 else if ((flags & BSF_OBJECT) != 0)
5477 type = STT_OBJECT;
5478 else
5479 type = STT_NOTYPE;
5481 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
5482 type = STT_TLS;
5484 /* Processor-specific types. */
5485 if (type_ptr != NULL
5486 && bed->elf_backend_get_symbol_type)
5487 type = ((*bed->elf_backend_get_symbol_type)
5488 (&type_ptr->internal_elf_sym, type));
5490 if (flags & BSF_SECTION_SYM)
5492 if (flags & BSF_GLOBAL)
5493 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5494 else
5495 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5497 else if (bfd_is_com_section (syms[idx]->section))
5498 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
5499 else if (bfd_is_und_section (syms[idx]->section))
5500 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
5501 ? STB_WEAK
5502 : STB_GLOBAL),
5503 type);
5504 else if (flags & BSF_FILE)
5505 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5506 else
5508 int bind = STB_LOCAL;
5510 if (flags & BSF_LOCAL)
5511 bind = STB_LOCAL;
5512 else if (flags & BSF_WEAK)
5513 bind = STB_WEAK;
5514 else if (flags & BSF_GLOBAL)
5515 bind = STB_GLOBAL;
5517 sym.st_info = ELF_ST_INFO (bind, type);
5520 if (type_ptr != NULL)
5521 sym.st_other = type_ptr->internal_elf_sym.st_other;
5522 else
5523 sym.st_other = 0;
5525 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5526 outbound_syms += bed->s->sizeof_sym;
5527 if (outbound_shndx != NULL)
5528 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5531 *sttp = stt;
5532 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
5533 symstrtab_hdr->sh_type = SHT_STRTAB;
5535 symstrtab_hdr->sh_flags = 0;
5536 symstrtab_hdr->sh_addr = 0;
5537 symstrtab_hdr->sh_entsize = 0;
5538 symstrtab_hdr->sh_link = 0;
5539 symstrtab_hdr->sh_info = 0;
5540 symstrtab_hdr->sh_addralign = 1;
5542 return TRUE;
5545 /* Return the number of bytes required to hold the symtab vector.
5547 Note that we base it on the count plus 1, since we will null terminate
5548 the vector allocated based on this size. However, the ELF symbol table
5549 always has a dummy entry as symbol #0, so it ends up even. */
5551 long
5552 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
5554 long symcount;
5555 long symtab_size;
5556 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
5558 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5559 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5560 if (symcount > 0)
5561 symtab_size -= sizeof (asymbol *);
5563 return symtab_size;
5566 long
5567 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
5569 long symcount;
5570 long symtab_size;
5571 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
5573 if (elf_dynsymtab (abfd) == 0)
5575 bfd_set_error (bfd_error_invalid_operation);
5576 return -1;
5579 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5580 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5581 if (symcount > 0)
5582 symtab_size -= sizeof (asymbol *);
5584 return symtab_size;
5587 long
5588 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
5589 sec_ptr asect)
5591 return (asect->reloc_count + 1) * sizeof (arelent *);
5594 /* Canonicalize the relocs. */
5596 long
5597 _bfd_elf_canonicalize_reloc (bfd *abfd,
5598 sec_ptr section,
5599 arelent **relptr,
5600 asymbol **symbols)
5602 arelent *tblptr;
5603 unsigned int i;
5604 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5606 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
5607 return -1;
5609 tblptr = section->relocation;
5610 for (i = 0; i < section->reloc_count; i++)
5611 *relptr++ = tblptr++;
5613 *relptr = NULL;
5615 return section->reloc_count;
5618 long
5619 _bfd_elf_get_symtab (bfd *abfd, asymbol **allocation)
5621 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5622 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
5624 if (symcount >= 0)
5625 bfd_get_symcount (abfd) = symcount;
5626 return symcount;
5629 long
5630 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
5631 asymbol **allocation)
5633 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5634 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
5636 if (symcount >= 0)
5637 bfd_get_dynamic_symcount (abfd) = symcount;
5638 return symcount;
5641 /* Return the size required for the dynamic reloc entries. Any
5642 section that was actually installed in the BFD, and has type
5643 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5644 considered to be a dynamic reloc section. */
5646 long
5647 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
5649 long ret;
5650 asection *s;
5652 if (elf_dynsymtab (abfd) == 0)
5654 bfd_set_error (bfd_error_invalid_operation);
5655 return -1;
5658 ret = sizeof (arelent *);
5659 for (s = abfd->sections; s != NULL; s = s->next)
5660 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5661 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5662 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5663 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
5664 * sizeof (arelent *));
5666 return ret;
5669 /* Canonicalize the dynamic relocation entries. Note that we return
5670 the dynamic relocations as a single block, although they are
5671 actually associated with particular sections; the interface, which
5672 was designed for SunOS style shared libraries, expects that there
5673 is only one set of dynamic relocs. Any section that was actually
5674 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5675 the dynamic symbol table, is considered to be a dynamic reloc
5676 section. */
5678 long
5679 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
5680 arelent **storage,
5681 asymbol **syms)
5683 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
5684 asection *s;
5685 long ret;
5687 if (elf_dynsymtab (abfd) == 0)
5689 bfd_set_error (bfd_error_invalid_operation);
5690 return -1;
5693 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
5694 ret = 0;
5695 for (s = abfd->sections; s != NULL; s = s->next)
5697 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5698 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5699 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5701 arelent *p;
5702 long count, i;
5704 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
5705 return -1;
5706 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
5707 p = s->relocation;
5708 for (i = 0; i < count; i++)
5709 *storage++ = p++;
5710 ret += count;
5714 *storage = NULL;
5716 return ret;
5719 /* Read in the version information. */
5721 bfd_boolean
5722 _bfd_elf_slurp_version_tables (bfd *abfd)
5724 bfd_byte *contents = NULL;
5725 bfd_size_type amt;
5727 if (elf_dynverdef (abfd) != 0)
5729 Elf_Internal_Shdr *hdr;
5730 Elf_External_Verdef *everdef;
5731 Elf_Internal_Verdef *iverdef;
5732 Elf_Internal_Verdef *iverdefarr;
5733 Elf_Internal_Verdef iverdefmem;
5734 unsigned int i;
5735 unsigned int maxidx;
5737 hdr = &elf_tdata (abfd)->dynverdef_hdr;
5739 contents = bfd_malloc (hdr->sh_size);
5740 if (contents == NULL)
5741 goto error_return;
5742 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5743 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
5744 goto error_return;
5746 /* We know the number of entries in the section but not the maximum
5747 index. Therefore we have to run through all entries and find
5748 the maximum. */
5749 everdef = (Elf_External_Verdef *) contents;
5750 maxidx = 0;
5751 for (i = 0; i < hdr->sh_info; ++i)
5753 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5755 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
5756 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
5758 everdef = ((Elf_External_Verdef *)
5759 ((bfd_byte *) everdef + iverdefmem.vd_next));
5762 amt = (bfd_size_type) maxidx * sizeof (Elf_Internal_Verdef);
5763 elf_tdata (abfd)->verdef = bfd_zalloc (abfd, amt);
5764 if (elf_tdata (abfd)->verdef == NULL)
5765 goto error_return;
5767 elf_tdata (abfd)->cverdefs = maxidx;
5769 everdef = (Elf_External_Verdef *) contents;
5770 iverdefarr = elf_tdata (abfd)->verdef;
5771 for (i = 0; i < hdr->sh_info; i++)
5773 Elf_External_Verdaux *everdaux;
5774 Elf_Internal_Verdaux *iverdaux;
5775 unsigned int j;
5777 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5779 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
5780 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
5782 iverdef->vd_bfd = abfd;
5784 amt = (bfd_size_type) iverdef->vd_cnt * sizeof (Elf_Internal_Verdaux);
5785 iverdef->vd_auxptr = bfd_alloc (abfd, amt);
5786 if (iverdef->vd_auxptr == NULL)
5787 goto error_return;
5789 everdaux = ((Elf_External_Verdaux *)
5790 ((bfd_byte *) everdef + iverdef->vd_aux));
5791 iverdaux = iverdef->vd_auxptr;
5792 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
5794 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
5796 iverdaux->vda_nodename =
5797 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5798 iverdaux->vda_name);
5799 if (iverdaux->vda_nodename == NULL)
5800 goto error_return;
5802 if (j + 1 < iverdef->vd_cnt)
5803 iverdaux->vda_nextptr = iverdaux + 1;
5804 else
5805 iverdaux->vda_nextptr = NULL;
5807 everdaux = ((Elf_External_Verdaux *)
5808 ((bfd_byte *) everdaux + iverdaux->vda_next));
5811 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
5813 if (i + 1 < hdr->sh_info)
5814 iverdef->vd_nextdef = iverdef + 1;
5815 else
5816 iverdef->vd_nextdef = NULL;
5818 everdef = ((Elf_External_Verdef *)
5819 ((bfd_byte *) everdef + iverdef->vd_next));
5822 free (contents);
5823 contents = NULL;
5826 if (elf_dynverref (abfd) != 0)
5828 Elf_Internal_Shdr *hdr;
5829 Elf_External_Verneed *everneed;
5830 Elf_Internal_Verneed *iverneed;
5831 unsigned int i;
5833 hdr = &elf_tdata (abfd)->dynverref_hdr;
5835 amt = (bfd_size_type) hdr->sh_info * sizeof (Elf_Internal_Verneed);
5836 elf_tdata (abfd)->verref = bfd_zalloc (abfd, amt);
5837 if (elf_tdata (abfd)->verref == NULL)
5838 goto error_return;
5840 elf_tdata (abfd)->cverrefs = hdr->sh_info;
5842 contents = bfd_malloc (hdr->sh_size);
5843 if (contents == NULL)
5844 goto error_return;
5845 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5846 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
5847 goto error_return;
5849 everneed = (Elf_External_Verneed *) contents;
5850 iverneed = elf_tdata (abfd)->verref;
5851 for (i = 0; i < hdr->sh_info; i++, iverneed++)
5853 Elf_External_Vernaux *evernaux;
5854 Elf_Internal_Vernaux *ivernaux;
5855 unsigned int j;
5857 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
5859 iverneed->vn_bfd = abfd;
5861 iverneed->vn_filename =
5862 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5863 iverneed->vn_file);
5864 if (iverneed->vn_filename == NULL)
5865 goto error_return;
5867 amt = iverneed->vn_cnt;
5868 amt *= sizeof (Elf_Internal_Vernaux);
5869 iverneed->vn_auxptr = bfd_alloc (abfd, amt);
5871 evernaux = ((Elf_External_Vernaux *)
5872 ((bfd_byte *) everneed + iverneed->vn_aux));
5873 ivernaux = iverneed->vn_auxptr;
5874 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
5876 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
5878 ivernaux->vna_nodename =
5879 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5880 ivernaux->vna_name);
5881 if (ivernaux->vna_nodename == NULL)
5882 goto error_return;
5884 if (j + 1 < iverneed->vn_cnt)
5885 ivernaux->vna_nextptr = ivernaux + 1;
5886 else
5887 ivernaux->vna_nextptr = NULL;
5889 evernaux = ((Elf_External_Vernaux *)
5890 ((bfd_byte *) evernaux + ivernaux->vna_next));
5893 if (i + 1 < hdr->sh_info)
5894 iverneed->vn_nextref = iverneed + 1;
5895 else
5896 iverneed->vn_nextref = NULL;
5898 everneed = ((Elf_External_Verneed *)
5899 ((bfd_byte *) everneed + iverneed->vn_next));
5902 free (contents);
5903 contents = NULL;
5906 return TRUE;
5908 error_return:
5909 if (contents != NULL)
5910 free (contents);
5911 return FALSE;
5914 asymbol *
5915 _bfd_elf_make_empty_symbol (bfd *abfd)
5917 elf_symbol_type *newsym;
5918 bfd_size_type amt = sizeof (elf_symbol_type);
5920 newsym = bfd_zalloc (abfd, amt);
5921 if (!newsym)
5922 return NULL;
5923 else
5925 newsym->symbol.the_bfd = abfd;
5926 return &newsym->symbol;
5930 void
5931 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
5932 asymbol *symbol,
5933 symbol_info *ret)
5935 bfd_symbol_info (symbol, ret);
5938 /* Return whether a symbol name implies a local symbol. Most targets
5939 use this function for the is_local_label_name entry point, but some
5940 override it. */
5942 bfd_boolean
5943 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
5944 const char *name)
5946 /* Normal local symbols start with ``.L''. */
5947 if (name[0] == '.' && name[1] == 'L')
5948 return TRUE;
5950 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5951 DWARF debugging symbols starting with ``..''. */
5952 if (name[0] == '.' && name[1] == '.')
5953 return TRUE;
5955 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5956 emitting DWARF debugging output. I suspect this is actually a
5957 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5958 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5959 underscore to be emitted on some ELF targets). For ease of use,
5960 we treat such symbols as local. */
5961 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
5962 return TRUE;
5964 return FALSE;
5967 alent *
5968 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
5969 asymbol *symbol ATTRIBUTE_UNUSED)
5971 abort ();
5972 return NULL;
5975 bfd_boolean
5976 _bfd_elf_set_arch_mach (bfd *abfd,
5977 enum bfd_architecture arch,
5978 unsigned long machine)
5980 /* If this isn't the right architecture for this backend, and this
5981 isn't the generic backend, fail. */
5982 if (arch != get_elf_backend_data (abfd)->arch
5983 && arch != bfd_arch_unknown
5984 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
5985 return FALSE;
5987 return bfd_default_set_arch_mach (abfd, arch, machine);
5990 /* Find the function to a particular section and offset,
5991 for error reporting. */
5993 static bfd_boolean
5994 elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
5995 asection *section,
5996 asymbol **symbols,
5997 bfd_vma offset,
5998 const char **filename_ptr,
5999 const char **functionname_ptr)
6001 const char *filename;
6002 asymbol *func;
6003 bfd_vma low_func;
6004 asymbol **p;
6006 filename = NULL;
6007 func = NULL;
6008 low_func = 0;
6010 for (p = symbols; *p != NULL; p++)
6012 elf_symbol_type *q;
6014 q = (elf_symbol_type *) *p;
6016 if (bfd_get_section (&q->symbol) != section)
6017 continue;
6019 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
6021 default:
6022 break;
6023 case STT_FILE:
6024 filename = bfd_asymbol_name (&q->symbol);
6025 break;
6026 case STT_NOTYPE:
6027 case STT_FUNC:
6028 if (q->symbol.section == section
6029 && q->symbol.value >= low_func
6030 && q->symbol.value <= offset)
6032 func = (asymbol *) q;
6033 low_func = q->symbol.value;
6035 break;
6039 if (func == NULL)
6040 return FALSE;
6042 if (filename_ptr)
6043 *filename_ptr = filename;
6044 if (functionname_ptr)
6045 *functionname_ptr = bfd_asymbol_name (func);
6047 return TRUE;
6050 /* Find the nearest line to a particular section and offset,
6051 for error reporting. */
6053 bfd_boolean
6054 _bfd_elf_find_nearest_line (bfd *abfd,
6055 asection *section,
6056 asymbol **symbols,
6057 bfd_vma offset,
6058 const char **filename_ptr,
6059 const char **functionname_ptr,
6060 unsigned int *line_ptr)
6062 bfd_boolean found;
6064 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
6065 filename_ptr, functionname_ptr,
6066 line_ptr))
6068 if (!*functionname_ptr)
6069 elf_find_function (abfd, section, symbols, offset,
6070 *filename_ptr ? NULL : filename_ptr,
6071 functionname_ptr);
6073 return TRUE;
6076 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
6077 filename_ptr, functionname_ptr,
6078 line_ptr, 0,
6079 &elf_tdata (abfd)->dwarf2_find_line_info))
6081 if (!*functionname_ptr)
6082 elf_find_function (abfd, section, symbols, offset,
6083 *filename_ptr ? NULL : filename_ptr,
6084 functionname_ptr);
6086 return TRUE;
6089 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
6090 &found, filename_ptr,
6091 functionname_ptr, line_ptr,
6092 &elf_tdata (abfd)->line_info))
6093 return FALSE;
6094 if (found && (*functionname_ptr || *line_ptr))
6095 return TRUE;
6097 if (symbols == NULL)
6098 return FALSE;
6100 if (! elf_find_function (abfd, section, symbols, offset,
6101 filename_ptr, functionname_ptr))
6102 return FALSE;
6104 *line_ptr = 0;
6105 return TRUE;
6109 _bfd_elf_sizeof_headers (bfd *abfd, bfd_boolean reloc)
6111 int ret;
6113 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
6114 if (! reloc)
6115 ret += get_program_header_size (abfd);
6116 return ret;
6119 bfd_boolean
6120 _bfd_elf_set_section_contents (bfd *abfd,
6121 sec_ptr section,
6122 void *location,
6123 file_ptr offset,
6124 bfd_size_type count)
6126 Elf_Internal_Shdr *hdr;
6127 bfd_signed_vma pos;
6129 if (! abfd->output_has_begun
6130 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
6131 return FALSE;
6133 hdr = &elf_section_data (section)->this_hdr;
6134 pos = hdr->sh_offset + offset;
6135 if (bfd_seek (abfd, pos, SEEK_SET) != 0
6136 || bfd_bwrite (location, count, abfd) != count)
6137 return FALSE;
6139 return TRUE;
6142 void
6143 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
6144 arelent *cache_ptr ATTRIBUTE_UNUSED,
6145 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
6147 abort ();
6150 /* Try to convert a non-ELF reloc into an ELF one. */
6152 bfd_boolean
6153 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
6155 /* Check whether we really have an ELF howto. */
6157 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
6159 bfd_reloc_code_real_type code;
6160 reloc_howto_type *howto;
6162 /* Alien reloc: Try to determine its type to replace it with an
6163 equivalent ELF reloc. */
6165 if (areloc->howto->pc_relative)
6167 switch (areloc->howto->bitsize)
6169 case 8:
6170 code = BFD_RELOC_8_PCREL;
6171 break;
6172 case 12:
6173 code = BFD_RELOC_12_PCREL;
6174 break;
6175 case 16:
6176 code = BFD_RELOC_16_PCREL;
6177 break;
6178 case 24:
6179 code = BFD_RELOC_24_PCREL;
6180 break;
6181 case 32:
6182 code = BFD_RELOC_32_PCREL;
6183 break;
6184 case 64:
6185 code = BFD_RELOC_64_PCREL;
6186 break;
6187 default:
6188 goto fail;
6191 howto = bfd_reloc_type_lookup (abfd, code);
6193 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
6195 if (howto->pcrel_offset)
6196 areloc->addend += areloc->address;
6197 else
6198 areloc->addend -= areloc->address; /* addend is unsigned!! */
6201 else
6203 switch (areloc->howto->bitsize)
6205 case 8:
6206 code = BFD_RELOC_8;
6207 break;
6208 case 14:
6209 code = BFD_RELOC_14;
6210 break;
6211 case 16:
6212 code = BFD_RELOC_16;
6213 break;
6214 case 26:
6215 code = BFD_RELOC_26;
6216 break;
6217 case 32:
6218 code = BFD_RELOC_32;
6219 break;
6220 case 64:
6221 code = BFD_RELOC_64;
6222 break;
6223 default:
6224 goto fail;
6227 howto = bfd_reloc_type_lookup (abfd, code);
6230 if (howto)
6231 areloc->howto = howto;
6232 else
6233 goto fail;
6236 return TRUE;
6238 fail:
6239 (*_bfd_error_handler)
6240 (_("%s: unsupported relocation type %s"),
6241 bfd_archive_filename (abfd), areloc->howto->name);
6242 bfd_set_error (bfd_error_bad_value);
6243 return FALSE;
6246 bfd_boolean
6247 _bfd_elf_close_and_cleanup (bfd *abfd)
6249 if (bfd_get_format (abfd) == bfd_object)
6251 if (elf_shstrtab (abfd) != NULL)
6252 _bfd_elf_strtab_free (elf_shstrtab (abfd));
6255 return _bfd_generic_close_and_cleanup (abfd);
6258 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
6259 in the relocation's offset. Thus we cannot allow any sort of sanity
6260 range-checking to interfere. There is nothing else to do in processing
6261 this reloc. */
6263 bfd_reloc_status_type
6264 _bfd_elf_rel_vtable_reloc_fn
6265 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
6266 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED,
6267 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
6268 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
6270 return bfd_reloc_ok;
6273 /* Elf core file support. Much of this only works on native
6274 toolchains, since we rely on knowing the
6275 machine-dependent procfs structure in order to pick
6276 out details about the corefile. */
6278 #ifdef HAVE_SYS_PROCFS_H
6279 # include <sys/procfs.h>
6280 #endif
6282 /* FIXME: this is kinda wrong, but it's what gdb wants. */
6284 static int
6285 elfcore_make_pid (bfd *abfd)
6287 return ((elf_tdata (abfd)->core_lwpid << 16)
6288 + (elf_tdata (abfd)->core_pid));
6291 /* If there isn't a section called NAME, make one, using
6292 data from SECT. Note, this function will generate a
6293 reference to NAME, so you shouldn't deallocate or
6294 overwrite it. */
6296 static bfd_boolean
6297 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
6299 asection *sect2;
6301 if (bfd_get_section_by_name (abfd, name) != NULL)
6302 return TRUE;
6304 sect2 = bfd_make_section (abfd, name);
6305 if (sect2 == NULL)
6306 return FALSE;
6308 sect2->_raw_size = sect->_raw_size;
6309 sect2->filepos = sect->filepos;
6310 sect2->flags = sect->flags;
6311 sect2->alignment_power = sect->alignment_power;
6312 return TRUE;
6315 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
6316 actually creates up to two pseudosections:
6317 - For the single-threaded case, a section named NAME, unless
6318 such a section already exists.
6319 - For the multi-threaded case, a section named "NAME/PID", where
6320 PID is elfcore_make_pid (abfd).
6321 Both pseudosections have identical contents. */
6322 bfd_boolean
6323 _bfd_elfcore_make_pseudosection (bfd *abfd,
6324 char *name,
6325 size_t size,
6326 ufile_ptr filepos)
6328 char buf[100];
6329 char *threaded_name;
6330 size_t len;
6331 asection *sect;
6333 /* Build the section name. */
6335 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
6336 len = strlen (buf) + 1;
6337 threaded_name = bfd_alloc (abfd, len);
6338 if (threaded_name == NULL)
6339 return FALSE;
6340 memcpy (threaded_name, buf, len);
6342 sect = bfd_make_section (abfd, threaded_name);
6343 if (sect == NULL)
6344 return FALSE;
6345 sect->_raw_size = size;
6346 sect->filepos = filepos;
6347 sect->flags = SEC_HAS_CONTENTS;
6348 sect->alignment_power = 2;
6350 return elfcore_maybe_make_sect (abfd, name, sect);
6353 /* prstatus_t exists on:
6354 solaris 2.5+
6355 linux 2.[01] + glibc
6356 unixware 4.2
6359 #if defined (HAVE_PRSTATUS_T)
6361 static bfd_boolean
6362 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
6364 size_t raw_size;
6365 int offset;
6367 if (note->descsz == sizeof (prstatus_t))
6369 prstatus_t prstat;
6371 raw_size = sizeof (prstat.pr_reg);
6372 offset = offsetof (prstatus_t, pr_reg);
6373 memcpy (&prstat, note->descdata, sizeof (prstat));
6375 /* Do not overwrite the core signal if it
6376 has already been set by another thread. */
6377 if (elf_tdata (abfd)->core_signal == 0)
6378 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
6379 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6381 /* pr_who exists on:
6382 solaris 2.5+
6383 unixware 4.2
6384 pr_who doesn't exist on:
6385 linux 2.[01]
6387 #if defined (HAVE_PRSTATUS_T_PR_WHO)
6388 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6389 #endif
6391 #if defined (HAVE_PRSTATUS32_T)
6392 else if (note->descsz == sizeof (prstatus32_t))
6394 /* 64-bit host, 32-bit corefile */
6395 prstatus32_t prstat;
6397 raw_size = sizeof (prstat.pr_reg);
6398 offset = offsetof (prstatus32_t, pr_reg);
6399 memcpy (&prstat, note->descdata, sizeof (prstat));
6401 /* Do not overwrite the core signal if it
6402 has already been set by another thread. */
6403 if (elf_tdata (abfd)->core_signal == 0)
6404 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
6405 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6407 /* pr_who exists on:
6408 solaris 2.5+
6409 unixware 4.2
6410 pr_who doesn't exist on:
6411 linux 2.[01]
6413 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
6414 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6415 #endif
6417 #endif /* HAVE_PRSTATUS32_T */
6418 else
6420 /* Fail - we don't know how to handle any other
6421 note size (ie. data object type). */
6422 return TRUE;
6425 /* Make a ".reg/999" section and a ".reg" section. */
6426 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
6427 raw_size, note->descpos + offset);
6429 #endif /* defined (HAVE_PRSTATUS_T) */
6431 /* Create a pseudosection containing the exact contents of NOTE. */
6432 static bfd_boolean
6433 elfcore_make_note_pseudosection (bfd *abfd,
6434 char *name,
6435 Elf_Internal_Note *note)
6437 return _bfd_elfcore_make_pseudosection (abfd, name,
6438 note->descsz, note->descpos);
6441 /* There isn't a consistent prfpregset_t across platforms,
6442 but it doesn't matter, because we don't have to pick this
6443 data structure apart. */
6445 static bfd_boolean
6446 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
6448 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6451 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6452 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6453 literally. */
6455 static bfd_boolean
6456 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
6458 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
6461 #if defined (HAVE_PRPSINFO_T)
6462 typedef prpsinfo_t elfcore_psinfo_t;
6463 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6464 typedef prpsinfo32_t elfcore_psinfo32_t;
6465 #endif
6466 #endif
6468 #if defined (HAVE_PSINFO_T)
6469 typedef psinfo_t elfcore_psinfo_t;
6470 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6471 typedef psinfo32_t elfcore_psinfo32_t;
6472 #endif
6473 #endif
6475 /* return a malloc'ed copy of a string at START which is at
6476 most MAX bytes long, possibly without a terminating '\0'.
6477 the copy will always have a terminating '\0'. */
6479 char *
6480 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
6482 char *dups;
6483 char *end = memchr (start, '\0', max);
6484 size_t len;
6486 if (end == NULL)
6487 len = max;
6488 else
6489 len = end - start;
6491 dups = bfd_alloc (abfd, len + 1);
6492 if (dups == NULL)
6493 return NULL;
6495 memcpy (dups, start, len);
6496 dups[len] = '\0';
6498 return dups;
6501 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6502 static bfd_boolean
6503 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
6505 if (note->descsz == sizeof (elfcore_psinfo_t))
6507 elfcore_psinfo_t psinfo;
6509 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6511 elf_tdata (abfd)->core_program
6512 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6513 sizeof (psinfo.pr_fname));
6515 elf_tdata (abfd)->core_command
6516 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6517 sizeof (psinfo.pr_psargs));
6519 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6520 else if (note->descsz == sizeof (elfcore_psinfo32_t))
6522 /* 64-bit host, 32-bit corefile */
6523 elfcore_psinfo32_t psinfo;
6525 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6527 elf_tdata (abfd)->core_program
6528 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6529 sizeof (psinfo.pr_fname));
6531 elf_tdata (abfd)->core_command
6532 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6533 sizeof (psinfo.pr_psargs));
6535 #endif
6537 else
6539 /* Fail - we don't know how to handle any other
6540 note size (ie. data object type). */
6541 return TRUE;
6544 /* Note that for some reason, a spurious space is tacked
6545 onto the end of the args in some (at least one anyway)
6546 implementations, so strip it off if it exists. */
6549 char *command = elf_tdata (abfd)->core_command;
6550 int n = strlen (command);
6552 if (0 < n && command[n - 1] == ' ')
6553 command[n - 1] = '\0';
6556 return TRUE;
6558 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6560 #if defined (HAVE_PSTATUS_T)
6561 static bfd_boolean
6562 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
6564 if (note->descsz == sizeof (pstatus_t)
6565 #if defined (HAVE_PXSTATUS_T)
6566 || note->descsz == sizeof (pxstatus_t)
6567 #endif
6570 pstatus_t pstat;
6572 memcpy (&pstat, note->descdata, sizeof (pstat));
6574 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6576 #if defined (HAVE_PSTATUS32_T)
6577 else if (note->descsz == sizeof (pstatus32_t))
6579 /* 64-bit host, 32-bit corefile */
6580 pstatus32_t pstat;
6582 memcpy (&pstat, note->descdata, sizeof (pstat));
6584 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6586 #endif
6587 /* Could grab some more details from the "representative"
6588 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6589 NT_LWPSTATUS note, presumably. */
6591 return TRUE;
6593 #endif /* defined (HAVE_PSTATUS_T) */
6595 #if defined (HAVE_LWPSTATUS_T)
6596 static bfd_boolean
6597 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
6599 lwpstatus_t lwpstat;
6600 char buf[100];
6601 char *name;
6602 size_t len;
6603 asection *sect;
6605 if (note->descsz != sizeof (lwpstat)
6606 #if defined (HAVE_LWPXSTATUS_T)
6607 && note->descsz != sizeof (lwpxstatus_t)
6608 #endif
6610 return TRUE;
6612 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
6614 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
6615 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
6617 /* Make a ".reg/999" section. */
6619 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
6620 len = strlen (buf) + 1;
6621 name = bfd_alloc (abfd, len);
6622 if (name == NULL)
6623 return FALSE;
6624 memcpy (name, buf, len);
6626 sect = bfd_make_section (abfd, name);
6627 if (sect == NULL)
6628 return FALSE;
6630 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6631 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
6632 sect->filepos = note->descpos
6633 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
6634 #endif
6636 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6637 sect->_raw_size = sizeof (lwpstat.pr_reg);
6638 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
6639 #endif
6641 sect->flags = SEC_HAS_CONTENTS;
6642 sect->alignment_power = 2;
6644 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
6645 return FALSE;
6647 /* Make a ".reg2/999" section */
6649 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
6650 len = strlen (buf) + 1;
6651 name = bfd_alloc (abfd, len);
6652 if (name == NULL)
6653 return FALSE;
6654 memcpy (name, buf, len);
6656 sect = bfd_make_section (abfd, name);
6657 if (sect == NULL)
6658 return FALSE;
6660 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6661 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
6662 sect->filepos = note->descpos
6663 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
6664 #endif
6666 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6667 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
6668 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
6669 #endif
6671 sect->flags = SEC_HAS_CONTENTS;
6672 sect->alignment_power = 2;
6674 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
6676 #endif /* defined (HAVE_LWPSTATUS_T) */
6678 #if defined (HAVE_WIN32_PSTATUS_T)
6679 static bfd_boolean
6680 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
6682 char buf[30];
6683 char *name;
6684 size_t len;
6685 asection *sect;
6686 win32_pstatus_t pstatus;
6688 if (note->descsz < sizeof (pstatus))
6689 return TRUE;
6691 memcpy (&pstatus, note->descdata, sizeof (pstatus));
6693 switch (pstatus.data_type)
6695 case NOTE_INFO_PROCESS:
6696 /* FIXME: need to add ->core_command. */
6697 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
6698 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
6699 break;
6701 case NOTE_INFO_THREAD:
6702 /* Make a ".reg/999" section. */
6703 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
6705 len = strlen (buf) + 1;
6706 name = bfd_alloc (abfd, len);
6707 if (name == NULL)
6708 return FALSE;
6710 memcpy (name, buf, len);
6712 sect = bfd_make_section (abfd, name);
6713 if (sect == NULL)
6714 return FALSE;
6716 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
6717 sect->filepos = (note->descpos
6718 + offsetof (struct win32_pstatus,
6719 data.thread_info.thread_context));
6720 sect->flags = SEC_HAS_CONTENTS;
6721 sect->alignment_power = 2;
6723 if (pstatus.data.thread_info.is_active_thread)
6724 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
6725 return FALSE;
6726 break;
6728 case NOTE_INFO_MODULE:
6729 /* Make a ".module/xxxxxxxx" section. */
6730 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
6732 len = strlen (buf) + 1;
6733 name = bfd_alloc (abfd, len);
6734 if (name == NULL)
6735 return FALSE;
6737 memcpy (name, buf, len);
6739 sect = bfd_make_section (abfd, name);
6741 if (sect == NULL)
6742 return FALSE;
6744 sect->_raw_size = note->descsz;
6745 sect->filepos = note->descpos;
6746 sect->flags = SEC_HAS_CONTENTS;
6747 sect->alignment_power = 2;
6748 break;
6750 default:
6751 return TRUE;
6754 return TRUE;
6756 #endif /* HAVE_WIN32_PSTATUS_T */
6758 static bfd_boolean
6759 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
6761 struct elf_backend_data *bed = get_elf_backend_data (abfd);
6763 switch (note->type)
6765 default:
6766 return TRUE;
6768 case NT_PRSTATUS:
6769 if (bed->elf_backend_grok_prstatus)
6770 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
6771 return TRUE;
6772 #if defined (HAVE_PRSTATUS_T)
6773 return elfcore_grok_prstatus (abfd, note);
6774 #else
6775 return TRUE;
6776 #endif
6778 #if defined (HAVE_PSTATUS_T)
6779 case NT_PSTATUS:
6780 return elfcore_grok_pstatus (abfd, note);
6781 #endif
6783 #if defined (HAVE_LWPSTATUS_T)
6784 case NT_LWPSTATUS:
6785 return elfcore_grok_lwpstatus (abfd, note);
6786 #endif
6788 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
6789 return elfcore_grok_prfpreg (abfd, note);
6791 #if defined (HAVE_WIN32_PSTATUS_T)
6792 case NT_WIN32PSTATUS:
6793 return elfcore_grok_win32pstatus (abfd, note);
6794 #endif
6796 case NT_PRXFPREG: /* Linux SSE extension */
6797 if (note->namesz == 6
6798 && strcmp (note->namedata, "LINUX") == 0)
6799 return elfcore_grok_prxfpreg (abfd, note);
6800 else
6801 return TRUE;
6803 case NT_PRPSINFO:
6804 case NT_PSINFO:
6805 if (bed->elf_backend_grok_psinfo)
6806 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
6807 return TRUE;
6808 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6809 return elfcore_grok_psinfo (abfd, note);
6810 #else
6811 return TRUE;
6812 #endif
6814 case NT_AUXV:
6816 asection *sect = bfd_make_section (abfd, ".auxv");
6818 if (sect == NULL)
6819 return FALSE;
6820 sect->_raw_size = note->descsz;
6821 sect->filepos = note->descpos;
6822 sect->flags = SEC_HAS_CONTENTS;
6823 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
6825 return TRUE;
6830 static bfd_boolean
6831 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
6833 char *cp;
6835 cp = strchr (note->namedata, '@');
6836 if (cp != NULL)
6838 *lwpidp = atoi(cp + 1);
6839 return TRUE;
6841 return FALSE;
6844 static bfd_boolean
6845 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
6848 /* Signal number at offset 0x08. */
6849 elf_tdata (abfd)->core_signal
6850 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
6852 /* Process ID at offset 0x50. */
6853 elf_tdata (abfd)->core_pid
6854 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
6856 /* Command name at 0x7c (max 32 bytes, including nul). */
6857 elf_tdata (abfd)->core_command
6858 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
6860 return TRUE;
6863 static bfd_boolean
6864 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
6866 int lwp;
6868 if (elfcore_netbsd_get_lwpid (note, &lwp))
6869 elf_tdata (abfd)->core_lwpid = lwp;
6871 if (note->type == NT_NETBSDCORE_PROCINFO)
6873 /* NetBSD-specific core "procinfo". Note that we expect to
6874 find this note before any of the others, which is fine,
6875 since the kernel writes this note out first when it
6876 creates a core file. */
6878 return elfcore_grok_netbsd_procinfo (abfd, note);
6881 /* As of Jan 2002 there are no other machine-independent notes
6882 defined for NetBSD core files. If the note type is less
6883 than the start of the machine-dependent note types, we don't
6884 understand it. */
6886 if (note->type < NT_NETBSDCORE_FIRSTMACH)
6887 return TRUE;
6890 switch (bfd_get_arch (abfd))
6892 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
6893 PT_GETFPREGS == mach+2. */
6895 case bfd_arch_alpha:
6896 case bfd_arch_sparc:
6897 switch (note->type)
6899 case NT_NETBSDCORE_FIRSTMACH+0:
6900 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6902 case NT_NETBSDCORE_FIRSTMACH+2:
6903 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6905 default:
6906 return TRUE;
6909 /* On all other arch's, PT_GETREGS == mach+1 and
6910 PT_GETFPREGS == mach+3. */
6912 default:
6913 switch (note->type)
6915 case NT_NETBSDCORE_FIRSTMACH+1:
6916 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6918 case NT_NETBSDCORE_FIRSTMACH+3:
6919 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6921 default:
6922 return TRUE;
6925 /* NOTREACHED */
6928 static bfd_boolean
6929 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, pid_t *tid)
6931 void *ddata = note->descdata;
6932 char buf[100];
6933 char *name;
6934 asection *sect;
6935 short sig;
6936 unsigned flags;
6938 /* nto_procfs_status 'pid' field is at offset 0. */
6939 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
6941 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
6942 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
6944 /* nto_procfs_status 'flags' field is at offset 8. */
6945 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
6947 /* nto_procfs_status 'what' field is at offset 14. */
6948 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
6950 elf_tdata (abfd)->core_signal = sig;
6951 elf_tdata (abfd)->core_lwpid = *tid;
6954 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
6955 do not come from signals so we make sure we set the current
6956 thread just in case. */
6957 if (flags & 0x00000080)
6958 elf_tdata (abfd)->core_lwpid = *tid;
6960 /* Make a ".qnx_core_status/%d" section. */
6961 sprintf (buf, ".qnx_core_status/%d", *tid);
6963 name = bfd_alloc (abfd, strlen (buf) + 1);
6964 if (name == NULL)
6965 return FALSE;
6966 strcpy (name, buf);
6968 sect = bfd_make_section (abfd, name);
6969 if (sect == NULL)
6970 return FALSE;
6972 sect->_raw_size = note->descsz;
6973 sect->filepos = note->descpos;
6974 sect->flags = SEC_HAS_CONTENTS;
6975 sect->alignment_power = 2;
6977 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
6980 static bfd_boolean
6981 elfcore_grok_nto_gregs (bfd *abfd, Elf_Internal_Note *note, pid_t tid)
6983 char buf[100];
6984 char *name;
6985 asection *sect;
6987 /* Make a ".reg/%d" section. */
6988 sprintf (buf, ".reg/%d", tid);
6990 name = bfd_alloc (abfd, strlen (buf) + 1);
6991 if (name == NULL)
6992 return FALSE;
6993 strcpy (name, buf);
6995 sect = bfd_make_section (abfd, name);
6996 if (sect == NULL)
6997 return FALSE;
6999 sect->_raw_size = note->descsz;
7000 sect->filepos = note->descpos;
7001 sect->flags = SEC_HAS_CONTENTS;
7002 sect->alignment_power = 2;
7004 /* This is the current thread. */
7005 if (elf_tdata (abfd)->core_lwpid == tid)
7006 return elfcore_maybe_make_sect (abfd, ".reg", sect);
7008 return TRUE;
7011 #define BFD_QNT_CORE_INFO 7
7012 #define BFD_QNT_CORE_STATUS 8
7013 #define BFD_QNT_CORE_GREG 9
7014 #define BFD_QNT_CORE_FPREG 10
7016 static bfd_boolean
7017 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
7019 /* Every GREG section has a STATUS section before it. Store the
7020 tid from the previous call to pass down to the next gregs
7021 function. */
7022 static pid_t tid = 1;
7024 switch (note->type)
7026 case BFD_QNT_CORE_INFO: return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
7027 case BFD_QNT_CORE_STATUS: return elfcore_grok_nto_status (abfd, note, &tid);
7028 case BFD_QNT_CORE_GREG: return elfcore_grok_nto_gregs (abfd, note, tid);
7029 case BFD_QNT_CORE_FPREG: return elfcore_grok_prfpreg (abfd, note);
7030 default: return TRUE;
7034 /* Function: elfcore_write_note
7036 Inputs:
7037 buffer to hold note
7038 name of note
7039 type of note
7040 data for note
7041 size of data for note
7043 Return:
7044 End of buffer containing note. */
7046 char *
7047 elfcore_write_note (bfd *abfd,
7048 char *buf,
7049 int *bufsiz,
7050 const char *name,
7051 int type,
7052 const void *input,
7053 int size)
7055 Elf_External_Note *xnp;
7056 size_t namesz;
7057 size_t pad;
7058 size_t newspace;
7059 char *p, *dest;
7061 namesz = 0;
7062 pad = 0;
7063 if (name != NULL)
7065 struct elf_backend_data *bed;
7067 namesz = strlen (name) + 1;
7068 bed = get_elf_backend_data (abfd);
7069 pad = -namesz & ((1 << bed->s->log_file_align) - 1);
7072 newspace = sizeof (Elf_External_Note) - 1 + namesz + pad + size;
7074 p = realloc (buf, *bufsiz + newspace);
7075 dest = p + *bufsiz;
7076 *bufsiz += newspace;
7077 xnp = (Elf_External_Note *) dest;
7078 H_PUT_32 (abfd, namesz, xnp->namesz);
7079 H_PUT_32 (abfd, size, xnp->descsz);
7080 H_PUT_32 (abfd, type, xnp->type);
7081 dest = xnp->name;
7082 if (name != NULL)
7084 memcpy (dest, name, namesz);
7085 dest += namesz;
7086 while (pad != 0)
7088 *dest++ = '\0';
7089 --pad;
7092 memcpy (dest, input, size);
7093 return p;
7096 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7097 char *
7098 elfcore_write_prpsinfo (bfd *abfd,
7099 char *buf,
7100 int *bufsiz,
7101 const char *fname,
7102 const char *psargs)
7104 int note_type;
7105 char *note_name = "CORE";
7107 #if defined (HAVE_PSINFO_T)
7108 psinfo_t data;
7109 note_type = NT_PSINFO;
7110 #else
7111 prpsinfo_t data;
7112 note_type = NT_PRPSINFO;
7113 #endif
7115 memset (&data, 0, sizeof (data));
7116 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
7117 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
7118 return elfcore_write_note (abfd, buf, bufsiz,
7119 note_name, note_type, &data, sizeof (data));
7121 #endif /* PSINFO_T or PRPSINFO_T */
7123 #if defined (HAVE_PRSTATUS_T)
7124 char *
7125 elfcore_write_prstatus (bfd *abfd,
7126 char *buf,
7127 int *bufsiz,
7128 long pid,
7129 int cursig,
7130 const void *gregs)
7132 prstatus_t prstat;
7133 char *note_name = "CORE";
7135 memset (&prstat, 0, sizeof (prstat));
7136 prstat.pr_pid = pid;
7137 prstat.pr_cursig = cursig;
7138 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
7139 return elfcore_write_note (abfd, buf, bufsiz,
7140 note_name, NT_PRSTATUS, &prstat, sizeof (prstat));
7142 #endif /* HAVE_PRSTATUS_T */
7144 #if defined (HAVE_LWPSTATUS_T)
7145 char *
7146 elfcore_write_lwpstatus (bfd *abfd,
7147 char *buf,
7148 int *bufsiz,
7149 long pid,
7150 int cursig,
7151 const void *gregs)
7153 lwpstatus_t lwpstat;
7154 char *note_name = "CORE";
7156 memset (&lwpstat, 0, sizeof (lwpstat));
7157 lwpstat.pr_lwpid = pid >> 16;
7158 lwpstat.pr_cursig = cursig;
7159 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7160 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
7161 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7162 #if !defined(gregs)
7163 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
7164 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
7165 #else
7166 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
7167 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
7168 #endif
7169 #endif
7170 return elfcore_write_note (abfd, buf, bufsiz, note_name,
7171 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
7173 #endif /* HAVE_LWPSTATUS_T */
7175 #if defined (HAVE_PSTATUS_T)
7176 char *
7177 elfcore_write_pstatus (bfd *abfd,
7178 char *buf,
7179 int *bufsiz,
7180 long pid,
7181 int cursig,
7182 const void *gregs)
7184 pstatus_t pstat;
7185 char *note_name = "CORE";
7187 memset (&pstat, 0, sizeof (pstat));
7188 pstat.pr_pid = pid & 0xffff;
7189 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
7190 NT_PSTATUS, &pstat, sizeof (pstat));
7191 return buf;
7193 #endif /* HAVE_PSTATUS_T */
7195 char *
7196 elfcore_write_prfpreg (bfd *abfd,
7197 char *buf,
7198 int *bufsiz,
7199 const void *fpregs,
7200 int size)
7202 char *note_name = "CORE";
7203 return elfcore_write_note (abfd, buf, bufsiz,
7204 note_name, NT_FPREGSET, fpregs, size);
7207 char *
7208 elfcore_write_prxfpreg (bfd *abfd,
7209 char *buf,
7210 int *bufsiz,
7211 const void *xfpregs,
7212 int size)
7214 char *note_name = "LINUX";
7215 return elfcore_write_note (abfd, buf, bufsiz,
7216 note_name, NT_PRXFPREG, xfpregs, size);
7219 static bfd_boolean
7220 elfcore_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
7222 char *buf;
7223 char *p;
7225 if (size <= 0)
7226 return TRUE;
7228 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
7229 return FALSE;
7231 buf = bfd_malloc (size);
7232 if (buf == NULL)
7233 return FALSE;
7235 if (bfd_bread (buf, size, abfd) != size)
7237 error:
7238 free (buf);
7239 return FALSE;
7242 p = buf;
7243 while (p < buf + size)
7245 /* FIXME: bad alignment assumption. */
7246 Elf_External_Note *xnp = (Elf_External_Note *) p;
7247 Elf_Internal_Note in;
7249 in.type = H_GET_32 (abfd, xnp->type);
7251 in.namesz = H_GET_32 (abfd, xnp->namesz);
7252 in.namedata = xnp->name;
7254 in.descsz = H_GET_32 (abfd, xnp->descsz);
7255 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
7256 in.descpos = offset + (in.descdata - buf);
7258 if (strncmp (in.namedata, "NetBSD-CORE", 11) == 0)
7260 if (! elfcore_grok_netbsd_note (abfd, &in))
7261 goto error;
7263 else if (strncmp (in.namedata, "QNX", 3) == 0)
7265 if (! elfcore_grok_nto_note (abfd, &in))
7266 goto error;
7268 else
7270 if (! elfcore_grok_note (abfd, &in))
7271 goto error;
7274 p = in.descdata + BFD_ALIGN (in.descsz, 4);
7277 free (buf);
7278 return TRUE;
7281 /* Providing external access to the ELF program header table. */
7283 /* Return an upper bound on the number of bytes required to store a
7284 copy of ABFD's program header table entries. Return -1 if an error
7285 occurs; bfd_get_error will return an appropriate code. */
7287 long
7288 bfd_get_elf_phdr_upper_bound (bfd *abfd)
7290 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7292 bfd_set_error (bfd_error_wrong_format);
7293 return -1;
7296 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
7299 /* Copy ABFD's program header table entries to *PHDRS. The entries
7300 will be stored as an array of Elf_Internal_Phdr structures, as
7301 defined in include/elf/internal.h. To find out how large the
7302 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
7304 Return the number of program header table entries read, or -1 if an
7305 error occurs; bfd_get_error will return an appropriate code. */
7308 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
7310 int num_phdrs;
7312 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7314 bfd_set_error (bfd_error_wrong_format);
7315 return -1;
7318 num_phdrs = elf_elfheader (abfd)->e_phnum;
7319 memcpy (phdrs, elf_tdata (abfd)->phdr,
7320 num_phdrs * sizeof (Elf_Internal_Phdr));
7322 return num_phdrs;
7325 void
7326 _bfd_elf_sprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, char *buf, bfd_vma value)
7328 #ifdef BFD64
7329 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7331 i_ehdrp = elf_elfheader (abfd);
7332 if (i_ehdrp == NULL)
7333 sprintf_vma (buf, value);
7334 else
7336 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
7338 #if BFD_HOST_64BIT_LONG
7339 sprintf (buf, "%016lx", value);
7340 #else
7341 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
7342 _bfd_int64_low (value));
7343 #endif
7345 else
7346 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
7348 #else
7349 sprintf_vma (buf, value);
7350 #endif
7353 void
7354 _bfd_elf_fprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, void *stream, bfd_vma value)
7356 #ifdef BFD64
7357 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7359 i_ehdrp = elf_elfheader (abfd);
7360 if (i_ehdrp == NULL)
7361 fprintf_vma ((FILE *) stream, value);
7362 else
7364 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
7366 #if BFD_HOST_64BIT_LONG
7367 fprintf ((FILE *) stream, "%016lx", value);
7368 #else
7369 fprintf ((FILE *) stream, "%08lx%08lx",
7370 _bfd_int64_high (value), _bfd_int64_low (value));
7371 #endif
7373 else
7374 fprintf ((FILE *) stream, "%08lx",
7375 (unsigned long) (value & 0xffffffff));
7377 #else
7378 fprintf_vma ((FILE *) stream, value);
7379 #endif
7382 enum elf_reloc_type_class
7383 _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
7385 return reloc_class_normal;
7388 /* For RELA architectures, return the relocation value for a
7389 relocation against a local symbol. */
7391 bfd_vma
7392 _bfd_elf_rela_local_sym (bfd *abfd,
7393 Elf_Internal_Sym *sym,
7394 asection *sec,
7395 Elf_Internal_Rela *rel)
7397 bfd_vma relocation;
7399 relocation = (sec->output_section->vma
7400 + sec->output_offset
7401 + sym->st_value);
7402 if ((sec->flags & SEC_MERGE)
7403 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
7404 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
7406 asection *msec;
7408 msec = sec;
7409 rel->r_addend =
7410 _bfd_merged_section_offset (abfd, &msec,
7411 elf_section_data (sec)->sec_info,
7412 sym->st_value + rel->r_addend,
7414 - relocation;
7415 rel->r_addend += msec->output_section->vma + msec->output_offset;
7417 return relocation;
7420 bfd_vma
7421 _bfd_elf_rel_local_sym (bfd *abfd,
7422 Elf_Internal_Sym *sym,
7423 asection **psec,
7424 bfd_vma addend)
7426 asection *sec = *psec;
7428 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
7429 return sym->st_value + addend;
7431 return _bfd_merged_section_offset (abfd, psec,
7432 elf_section_data (sec)->sec_info,
7433 sym->st_value + addend, 0);
7436 bfd_vma
7437 _bfd_elf_section_offset (bfd *abfd,
7438 struct bfd_link_info *info,
7439 asection *sec,
7440 bfd_vma offset)
7442 struct bfd_elf_section_data *sec_data;
7444 sec_data = elf_section_data (sec);
7445 switch (sec->sec_info_type)
7447 case ELF_INFO_TYPE_STABS:
7448 return _bfd_stab_section_offset (abfd,
7449 &elf_hash_table (info)->merge_info,
7450 sec, &sec_data->sec_info, offset);
7451 case ELF_INFO_TYPE_EH_FRAME:
7452 return _bfd_elf_eh_frame_section_offset (abfd, sec, offset);
7453 default:
7454 return offset;
7458 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
7459 reconstruct an ELF file by reading the segments out of remote memory
7460 based on the ELF file header at EHDR_VMA and the ELF program headers it
7461 points to. If not null, *LOADBASEP is filled in with the difference
7462 between the VMAs from which the segments were read, and the VMAs the
7463 file headers (and hence BFD's idea of each section's VMA) put them at.
7465 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
7466 remote memory at target address VMA into the local buffer at MYADDR; it
7467 should return zero on success or an `errno' code on failure. TEMPL must
7468 be a BFD for an ELF target with the word size and byte order found in
7469 the remote memory. */
7471 bfd *
7472 bfd_elf_bfd_from_remote_memory
7473 (bfd *templ,
7474 bfd_vma ehdr_vma,
7475 bfd_vma *loadbasep,
7476 int (*target_read_memory) (bfd_vma, char *, int))
7478 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
7479 (templ, ehdr_vma, loadbasep, target_read_memory);